g2o
isometry3d_mappings.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 H. Strasdat, G. Grisetti, R. Kümmerle, 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 "isometry3d_mappings.h"
28 
29 namespace g2o {
30  namespace internal {
31 
32  Eigen::Quaterniond normalized(const Eigen::Quaterniond& q) {
33  Eigen::Quaterniond q2(q);
34  normalize(q2);
35  return q2;
36  }
37 
38  Eigen::Quaterniond& normalize(Eigen::Quaterniond& q){
39  q.normalize();
40  if (q.w()<0) {
41  q.coeffs() *= -1;
42  }
43  return q;
44  }
45 
46  // functions to handle the rotation part
48  Eigen::Quaterniond q(R);
49  const double& q0 = q.w();
50  const double& q1 = q.x();
51  const double& q2 = q.y();
52  const double& q3 = q.z();
53  double roll = atan2(2*(q0*q1+q2*q3), 1-2*(q1*q1+q2*q2));
54  double pitch = asin(2*(q0*q2-q3*q1));
55  double yaw = atan2(2*(q0*q3+q1*q2), 1-2*(q2*q2+q3*q3));
56  return Vector3D(roll, pitch, yaw);
57  }
58 
60  //UNOPTIMIZED
61  double roll = v[0];
62  double pitch = v[1];
63  double yaw = v[2];
64  double sy = sin(yaw*0.5);
65  double cy = cos(yaw*0.5);
66  double sp = sin(pitch*0.5);
67  double cp = cos(pitch*0.5);
68  double sr = sin(roll*0.5);
69  double cr = cos(roll*0.5);
70  double w = cr*cp*cy + sr*sp*sy;
71  double x = sr*cp*cy - cr*sp*sy;
72  double y = cr*sp*cy + sr*cp*sy;
73  double z = cr*cp*sy - sr*sp*cy;
74  return Eigen::Quaterniond(w,x,y,z).toRotationMatrix();
75  }
76 
78  Eigen::Quaterniond q(R);
79  normalize(q);
80  // return (x,y,z) of the quaternion
81  return q.coeffs().head<3>();
82  }
83 
85  double w = 1-v.squaredNorm();
86  if (w<0)
87  return Matrix3D::Identity();
88  else
89  w=sqrt(w);
90  return Eigen::Quaterniond(w, v[0], v[1], v[2]).toRotationMatrix();
91  }
92 
93  // functions to handle the toVector of the whole transformations
95  Vector6d v;
96  v.block<3,1>(3,0) = toCompactQuaternion(extractRotation(t));
97  v.block<3,1>(0,0) = t.translation();
98  return v;
99  }
100 
102  Vector6d v;
103  v.block<3,1>(3,0)=toEuler(extractRotation(t));
104  v.block<3,1>(0,0) = t.translation();
105  return v;
106  }
107 
109  Eigen::Quaterniond q(extractRotation(t));
110  q.normalize();
111  Vector7d v;
112  v[3] = q.x(); v[4] = q.y(); v[5] = q.z(); v[6] = q.w();
113  v.block<3,1>(0,0) = t.translation();
114  return v;
115  }
116 
118  Isometry3D t;
119  t = fromCompactQuaternion(v.block<3,1>(3,0));
120  t.translation() = v.block<3,1>(0,0);
121  return t;
122  }
123 
125  Isometry3D t;
126  t = fromEuler(v.block<3,1>(3,0));
127  t.translation() = v.block<3,1>(0,0);
128  return t;
129  }
130 
132  Isometry3D t;
133  t=Eigen::Quaterniond(v[6], v[3], v[4], v[5]).toRotationMatrix();
134  t.translation() = v.head<3>();
135  return t;
136  }
137 
139  {
140  SE3Quat result(t.matrix().topLeftCorner<3,3>(), t.translation());
141  return result;
142  }
143 
145  {
146  Isometry3D result = (Isometry3D) t.rotation();
147  result.translation() = t.translation();
148  return result;
149  }
150 
151  } // end namespace internal
152 } // end namespace g2o
Vector3D toEuler(const Matrix3D &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)
SE3Quat toSE3Quat(const Isometry3D &t)
Vector6d toVectorET(const Isometry3D &t)
Eigen::Transform< double, 3, Eigen::Isometry, Eigen::ColMajor > Isometry3D
Definition: eigen_types.h:66
Eigen::Quaterniond & normalize(Eigen::Quaterniond &q)
Vector6d toVectorMQT(const Isometry3D &t)
const Eigen::Quaterniond & rotation() const
Definition: se3quat.h:97
Isometry3D::ConstLinearPart extractRotation(const Isometry3D &A)
Eigen::Matrix< double, 3, 3, Eigen::ColMajor > Matrix3D
Definition: eigen_types.h:61
Eigen::Quaterniond normalized(const Eigen::Quaterniond &q)
Vector7d toVectorQT(const Isometry3D &t)
Isometry3D fromVectorMQT(const Vector6d &v)
Isometry3D fromVectorET(const Vector6d &v)
Isometry3D fromSE3Quat(const SE3Quat &t)
const Vector3D & translation() const
Definition: se3quat.h:93
Matrix< double, 6, 1, Eigen::ColMajor > Vector6d
Definition: types_icp.cpp:75
Matrix3D fromEuler(const Vector3D &v)