g2o
test_isometry3d_mappings.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include <iostream>
28 #include "isometry3d_mappings.h"
29 #include "g2o/stuff/macros.h"
30 
31 #include <cstdio>
32 
33 using namespace std;
34 using namespace g2o;
35 using namespace g2o::internal;
36 using namespace Eigen;
37 
38 int main(int , char** ){
39 
40 Matrix3D I;
41  Matrix3D R = Matrix3D::Identity();
42  Matrix3D rot = (Matrix3D)AngleAxisd(0.01, Vector3D::UnitZ());
43  rot = rot * (Matrix3D)AngleAxisd(0.01, Vector3D::UnitX());
44 
45  cerr << "Initial rotation matrix accuracy" << endl;
46  I = R * R.transpose();
47  for (int i = 0; i < 3; ++i) {
48  for (int j = 0; j < 3; ++j) {
49  printf("%.30f ", I(i,j));
50  }
51  printf("\n");
52  }
53 
54  cerr << "After further multiplications" << endl;
55  for (int i = 0; i < 10000; ++i)
56  R = R * rot;
57  I = R * R.transpose();
58  for (int i = 0; i < 3; ++i) {
59  for (int j = 0; j < 3; ++j) {
60  printf("%.30f ", I(i,j));
61  }
62  printf("\n");
63  }
64 
65  cerr << PVAR(R) << endl;
66  printf("det %.30f\n", R.determinant());
67  printf("\nUsing nearest orthogonal matrix\n");
68  Matrix3D approxSolution = R;
69  approximateNearestOrthogonalMatrix(approxSolution);
71  cerr << PVAR(R) << endl;
72  printf("det %.30f\n", R.determinant());
73  I = R * R.transpose();
74  for (int i = 0; i < 3; ++i) {
75  for (int j = 0; j < 3; ++j) {
76  printf("%.30f ", I(i,j));
77  }
78  printf("\n");
79  }
80  cerr << "Norm of the columns" << endl;
81  for (int i = 0; i < 3; ++i)
82  printf("%.30f ", R.col(i).norm());
83  printf("\nUsing approximate nearest orthogonal matrix\n");
84  I = approxSolution * approxSolution.transpose();
85  cerr << PVAR(approxSolution) << endl;
86  printf("det %.30f\n", approxSolution.determinant());
87  for (int i = 0; i < 3; ++i) {
88  for (int j = 0; j < 3; ++j) {
89  printf("%.30f ", I(i,j));
90  }
91  printf("\n");
92  }
93  cerr << "Norm of the columns" << endl;
94  for (int i = 0; i < 3; ++i)
95  printf("%.30f ", approxSolution.col(i).norm());
96 
97  cerr << endl;
98  return 0;
99 
100  Vector3D eulerAngles(.1,.2,.3);
101  Matrix3D m1=fromEuler(eulerAngles);
102  cerr << "m1=fromEuler(eulerAngles)" << endl;
103  cerr << "eulerAngles:" << endl;
104  cerr << eulerAngles << endl;
105  cerr << "m1:" << endl;
106  cerr << m1 << endl;
107 
108  Vector3D eulerAngles1 = toEuler(m1);
109  cerr << "eulerAngles1 = toEuler(m1) " << endl;
110  cerr << "eulerAngles1:" << endl;
111  cerr << eulerAngles1 << endl;
112 
114  cerr << "q=toCompactQuaternion(m1)" << endl;
115  cerr << "q:" << endl;
116  cerr << q << endl;
117 
119  cerr << "m2=fromCompactQuaternion(q);" << endl;
120  cerr << "m2:" << endl;
121  cerr << m2 << endl;
122 
123  Vector6d et;
124  Vector3D t(1.,2.,3.);
125  et.block<3,1>(0,0)=eulerAngles;
126  et.block<3,1>(3,0)=t;
127  Isometry3D i1 = fromVectorET(et);
128  cerr << "i1 = fromVectorET(et);" << endl;
129  cerr << "et:" << endl;
130  cerr << et << endl;
131  cerr << "i1" << endl;
132  cerr << i1.rotation() << endl;
133  cerr << i1.translation() << endl;
134  Vector6d et2=toVectorET(i1);
135  cerr << "et2=toVectorET(i1);" << endl;
136  cerr << "et2" << endl;
137  cerr << et2 << endl;
138 
139  Vector6d qt1=toVectorMQT(i1);
140  cerr << "qt1=toVectorMQT(i1)" << endl;
141  cerr << "qt1:" << endl;
142  cerr << qt1 << endl;
143 
144  Isometry3D i2 = fromVectorMQT(qt1);
145  cerr << "i2 = fromVectorMQT(qt1)" << endl;
146  cerr << "i2" << endl;
147  cerr << i2.rotation() << endl;
148  cerr << i2.translation() << endl;
149 
150  Vector7d qt2=toVectorQT(i1);
151  cerr << "qt2=toVectorQT(i1)" << endl;
152  cerr << "qt2:" << endl;
153  cerr << qt2 << endl;
154 
155  Isometry3D i3 = fromVectorQT(qt2);
156  cerr << "i3 = fromVectorQT(qt2)" << endl;
157  cerr << "i3" << endl;
158  cerr << i3.rotation() << endl;
159  cerr << i3.translation() << endl;
160 
161 }
Vector3D toEuler(const Matrix3D &R)
void nearestOrthogonalMatrix(const Eigen::MatrixBase< Derived > &R)
Matrix3D fromCompactQuaternion(const Vector3D &v)
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
Definition: eigen_types.h:46
Isometry3D fromVectorQT(const Vector7d &v)
Eigen::Matrix< double, 7, 1 > Vector7d
Definition: sim3.h:35
Vector3D toCompactQuaternion(const Matrix3D &R)
Vector6d toVectorET(const Isometry3D &t)
Eigen::Transform< double, 3, Eigen::Isometry, Eigen::ColMajor > Isometry3D
Definition: eigen_types.h:66
Vector6d toVectorMQT(const Isometry3D &t)
int main(int, char **)
Eigen::Matrix< double, 3, 3, Eigen::ColMajor > Matrix3D
Definition: eigen_types.h:61
void approximateNearestOrthogonalMatrix(const Eigen::MatrixBase< Derived > &R)
Eigen::Matrix< double, 6, 1 > Vector6d
Vector7d toVectorQT(const Isometry3D &t)
Isometry3D fromVectorMQT(const Vector6d &v)
Isometry3D fromVectorET(const Vector6d &v)
Matrix3D fromEuler(const Vector3D &v)