manif-geom-cpp
Loading...
Searching...
No Matches
SE3.h
Go to the documentation of this file.
1#pragma once
2
3#include "SO3.h"
4#include <Eigen/Core>
5#include <Eigen/Geometry>
6#include <iostream>
7#include <math.h>
8
9using namespace Eigen;
10
14template<typename T>
15class SE3
16{
17private:
18 typedef Matrix<T, 3, 1> Vec3T;
19 typedef Matrix<T, 4, 1> Vec4T;
20 typedef Matrix<T, 6, 1> Vec6T;
21 typedef Matrix<T, 7, 1> Vec7T;
22 typedef Matrix<T, 3, 3> Mat3T;
23 typedef Matrix<T, 4, 4> Mat4T;
24 typedef Matrix<T, 6, 6> Mat6T;
25
26 T buf_[7];
27
28public:
43
51 static SE3 random()
52 {
53 SE3 x;
54 x.arr_.setRandom();
55 x.q_.normalize();
56 return x;
57 }
58
62 static SE3 identity()
63 {
64 SE3 x;
65 x.arr_.setZero();
66 x.arr_(3) = (T)1.0;
67 return x;
68 }
69
73 static SE3 nans()
74 {
75 SE3 x;
76 x.arr_.setConstant(std::numeric_limits<T>::quiet_NaN());
77 return x;
78 }
79
84 static SE3 fromH(const Mat4T& m)
85 {
86 return SE3::fromVecAndQuat(m.template block<3, 1>(0, 3), SO3<T>::fromR(m.template block<3, 3>(0, 0)));
87 }
88
99 static SE3 fromVecAndQuat(const T tx, const T ty, const T tz, const T qw, const T qx, const T qy, const T qz)
100 {
101 SE3 x;
102 x.arr_ << tx, ty, tz, qw, qx, qy, qz;
103 return x;
104 }
105
111 static SE3 fromVecAndQuat(const Vec3T& tvec, const Vec4T& qvec)
112 {
113 SE3 x;
114 x.arr_ << tvec(0), tvec(1), tvec(2), qvec(0), qvec(1), qvec(2), qvec(3);
115 return x;
116 }
117
123 static SE3 fromVecAndQuat(const Vec3T& t, const SO3<T>& q)
124 {
125 SE3 x;
126 x.arr_ << t(0), t(1), t(2), q.w(), q.x(), q.y(), q.z();
127 return x;
128 }
129
135 static SE3 fromVecAndQuat(const Vec3T& tvec, const Quaternion<T> quat)
136 {
137 SE3 x;
138 x.arr_ << tvec(0), tvec(1), tvec(2), quat.w(), quat.x(), quat.y(), quat.z();
139 return x;
140 }
141
145 SE3() : arr_(buf_), t_(arr_.data()), q_(arr_.data() + 3) {}
146
151 SE3(const Ref<const Vec7T>& arr) : arr_(buf_), t_(arr_.data()), q_(arr_.data() + 3)
152 {
153 arr_ = arr;
154 }
155
159 SE3(const SE3& x) : arr_(buf_), t_(arr_.data()), q_(arr_.data() + 3)
160 {
161 arr_ = x.arr_;
162 }
163
168 SE3(const T* data) : arr_(const_cast<T*>(data)), t_(arr_.data()), q_(arr_.data() + 3) {}
169
173 inline T& operator[](int i)
174 {
175 return arr_[i];
176 }
177
181 inline const Map<Vec3T>& t() const
182 {
183 return t_;
184 }
185
189 inline const SO3<T>& q() const
190 {
191 return q_;
192 }
193
197 inline Map<Vec3T>& t()
198 {
199 return t_;
200 }
201
205 inline SO3<T>& q()
206 {
207 return q_;
208 }
209
213 inline const Vec7T elements() const
214 {
215 return arr_;
216 }
217
221 inline Vec7T array() const
222 {
223 return arr_;
224 }
225
229 inline T* data()
230 {
231 return arr_.data();
232 }
233
237 inline const T* data() const
238 {
239 return arr_.data();
240 }
241
245 SE3 copy() const
246 {
247 SE3 tmp;
248 tmp.arr_ = arr_;
249 return tmp;
250 }
251
256 Mat4T H() const
257 {
258 Mat4T out;
259 out << q_.R(), t_, (T)0., (T)0., (T)0., (T)1.;
260 return out;
261 }
262
266 SE3 inverse() const
267 {
268 SE3 x;
270 return SE3::fromVecAndQuat(-(q_inv * t_), q_inv);
271 }
272
277 {
278 q().invert();
279 t() = -(q() * t());
280 return *this;
281 }
282
287 template<typename Tout = T, typename T2>
288 SE3<Tout> otimes(const SE3<T2>& x) const
289 {
291 xout.t() = t_ + q_ * x.t_;
292 xout.q() = q_ * x.q_;
293 return xout;
294 }
295
300 template<typename Tout = T, typename T2>
302 {
304 }
305
310 template<typename Tout = T, typename T2>
312 {
313 return SE3<Tout>::Log(x.inverse().template otimes<Tout>(*this));
314 }
315
319 SE3& operator=(const SE3& x)
320 {
321 arr_ = x.elements();
322 return *this;
323 }
324
328 template<typename T2>
329 SE3 operator*(const SE3<T2>& x) const
330 {
331 return otimes(x);
332 }
333
337 template<typename T2>
339 {
340 arr_ = otimes(x).elements();
341 return *this;
342 }
343
350 SE3& operator*=(const double& s)
351 {
352 arr_ = SE3::Exp(s * SE3::Log(*this)).elements();
353 return *this;
354 }
355
362 SE3& operator/=(const double& s)
363 {
364 arr_ = SE3::Exp(SE3::Log(*this) / s).elements();
365 return *this;
366 }
367
374 SE3 operator/(const double& s) const
375 {
376 SE3 qs;
377 qs.arr_ = SE3::Exp(SE3::Log(*this) / s).elements();
378 return qs;
379 }
380
385 template<typename Tout = T, typename T2>
387 {
388 return q_ * v + t_;
389 }
390
395 Vec3T operator*(const Vec3T& v) const
396 {
397 return q_ * v + t_;
398 }
399
403 SE3 operator+(const Vec6T& v) const
404 {
405 return oplus(v);
406 }
407
411 SE3& operator+=(const Vec6T& v)
412 {
413 arr_ = oplus(v).elements();
414 return *this;
415 }
416
420 template<typename T2>
421 Vec6T operator-(const SE3<T2>& x) const
422 {
423 return ominus(x);
424 }
425
430 static Mat4T hat(const Vec6T& omega)
431 {
432 Mat4T Omega;
433 Omega << SO3<T>::hat(omega.template block<3, 1>(3, 0)), omega.template block<3, 1>(0, 0), (T)0., (T)0., (T)0.,
434 (T)0.;
435 return Omega;
436 }
437
442 static Vec6T vee(const Mat4T& Omega)
443 {
444 Vec6T omega;
445 omega << Omega.template block<3, 1>(0, 3), SO3<T>::vee(Omega.template block<3, 3>(0, 0));
446 return omega;
447 }
448
452 static Mat4T log(const SE3& x)
453 {
454 return SE3::hat(SE3::Log(x));
455 }
456
460 static Vec6T Log(const SE3& x)
461 {
462 Vec3T w = SO3<T>::Log(x.q_);
463 T th = w.norm();
464 Mat3T W = SO3<T>::hat(w);
465
467 if (th > (T)1e-4)
468 {
469 T a = sin(th) / th;
470 T b = ((T)1. - cos(th)) / (th * th);
471 T c = ((T)1. - a) / (th * th);
472 T e = (b - 2. * c) / (2. * a);
473 leftJacobianInverse = Mat3T::Identity() - 0.5 * W + e * (W * W);
474 }
475 else
476 {
477 leftJacobianInverse = Mat3T::Identity();
478 }
479
480 Vec6T out;
481 out << leftJacobianInverse * x.t_, w;
482
483 return out;
484 }
485
489 static SE3 exp(const Mat4T& Omega)
490 {
491 return SE3::Exp(SE3::vee(Omega));
492 }
493
497 static SE3 Exp(const Vec6T& omega)
498 {
499 Vec3T rho = omega.template block<3, 1>(0, 0);
500 Vec3T w = omega.template block<3, 1>(3, 0);
501 SO3<T> q = SO3<T>::Exp(w);
502 Mat3T W = SO3<T>::hat(w);
503 T th = w.norm();
504
505 Mat3T leftJacobian;
506 if (th > (T)1e-4)
507 {
508 T a = sin(th) / th;
509 T b = ((T)1. - cos(th)) / (th * th);
510 T c = ((T)1. - a) / (th * th);
511 leftJacobian = a * Mat3T::Identity() + b * W + c * (w * w.transpose());
512 }
513 else
514 {
515 leftJacobian = Mat3T::Identity();
516 }
517
519 }
520
524 template<typename T2>
525 SE3<T2> cast() const
526 {
527 SE3<T2> x;
528 x.arr_ = arr_.template cast<T2>();
529 return x;
530 }
531};
532
539template<typename T>
540SE3<T> operator*(const double& l, const SE3<T>& r)
541{
542 SE3<T> lr;
543 lr.arr_ = SE3<T>::Exp(l * SE3<T>::Log(r)).elements();
544 return lr;
545}
546
553template<typename T>
554SE3<T> operator*(const SE3<T>& l, const double& r)
555{
556 SE3<T> lr;
557 lr.arr_ = SE3<T>::Exp(r * SE3<T>::Log(l)).elements();
558 return lr;
559}
560
564template<typename T>
565inline std::ostream& operator<<(std::ostream& os, const SE3<T>& x)
566{
567 os << "SE(3): [ " << x.t_.x() << "i, " << x.t_.y() << "j, " << x.t_.z() << "k ] [ " << x.q_.w() << ", " << x.q_.x()
568 << "i, " << x.q_.y() << "j, " << x.q_.z() << "k ]";
569 return os;
570}
571
SE3< double > SE3d
Definition SE3.h:572
SE3< T > operator*(const double &l, const SE3< T > &r)
Scale a transform by a scalar.
Definition SE3.h:540
std::ostream & operator<<(std::ostream &os, const SE3< T > &x)
Render the transform in a stream.
Definition SE3.h:565
Class representing a member of the manifold, or a 3D rigid body transform.
Definition SE3.h:16
T * data()
Access pointer to all elements of .
Definition SE3.h:229
static SE3 fromVecAndQuat(const Vec3T &t, const SO3< T > &q)
Construct a transform from a translation vector and a rotation object.
Definition SE3.h:123
SE3< Tout > otimes(const SE3< T2 > &x) const
Implementation of group composition: .
Definition SE3.h:288
static SE3 identity()
Obtain an identity transform.
Definition SE3.h:62
SO3< T > q_
Memory-mapped array representing only the rotation component of the transform .
Definition SE3.h:42
SE3 inverse() const
Obtain the inverse transform .
Definition SE3.h:266
Vec6T operator-(const SE3< T2 > &x) const
Invocation of ominus via subtraction.
Definition SE3.h:421
const Map< Vec3T > & t() const
Access the translation vector .
Definition SE3.h:181
Map< Vec3T > t_
Memory-mapped array representing only the translation component of the transform .
Definition SE3.h:38
Mat4T H() const
Convert the transform to matrix representation .
Definition SE3.h:256
Map< Vec3T > & t()
Access the translation vector .
Definition SE3.h:197
SE3 & operator*=(const SE3< T2 > &x)
Invocation of otimes via multiplication.
Definition SE3.h:338
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Map< Vec7T > arr_
Memory-mapped array representing all transform fields in .
Definition SE3.h:34
SE3(const Ref< const Vec7T > &arr)
Create a transform from an array representing all transform fields in .
Definition SE3.h:151
static Mat4T hat(const Vec6T &omega)
Hat operator implementation, which coverts the tangent-space vector representation to the correspondi...
Definition SE3.h:430
static SE3 fromH(const Mat4T &m)
Convert a transform matrix into a transform.
Definition SE3.h:84
Vec3T operator*(const Vec3T &v) const
Transform a vector via multiplication: .
Definition SE3.h:395
SE3 & operator/=(const double &s)
Scale a transform by a scalar.
Definition SE3.h:362
Vec7T array() const
Access all elements of .
Definition SE3.h:221
static SE3 fromVecAndQuat(const Vec3T &tvec, const Vec4T &qvec)
Construct a transform from a translation vector and rotation fields vector.
Definition SE3.h:111
SE3 & operator*=(const double &s)
Scale a transform by a scalar.
Definition SE3.h:350
const Vec7T elements() const
Access all elements of .
Definition SE3.h:213
SO3< T > & q()
Access the rotation .
Definition SE3.h:205
static Vec6T vee(const Mat4T &Omega)
Vee operator implementation, which coverts the Lie algebra representation to a tangent-space vector r...
Definition SE3.h:442
SE3 & operator=(const SE3 &x)
Copy constructor.
Definition SE3.h:319
static SE3 fromVecAndQuat(const T tx, const T ty, const T tz, const T qw, const T qx, const T qy, const T qz)
Construct a transform from the individual fields.
Definition SE3.h:99
SE3 operator/(const double &s) const
Scale a transform by a scalar.
Definition SE3.h:374
SE3(const SE3 &x)
Copy constructor from another transform.
Definition SE3.h:159
SE3 & operator+=(const Vec6T &v)
Invocation of oplus via addition.
Definition SE3.h:411
SE3 operator+(const Vec6T &v) const
Invocation of oplus via addition.
Definition SE3.h:403
SE3(const T *data)
Create a transform from a pointer array representing all transform fields in .
Definition SE3.h:168
SE3 operator*(const SE3< T2 > &x) const
Invocation of otimes via multiplication.
Definition SE3.h:329
SE3< Tout > oplus(const Matrix< T2, 6, 1 > &delta) const
Implementation of tangent space group perturbations: .
Definition SE3.h:301
SE3 copy() const
Get a deep copy of the current transform.
Definition SE3.h:245
static Vec6T Log(const SE3 &x)
Logarithmic chart map implementation: .
Definition SE3.h:460
const T * data() const
Access pointer to all elements of .
Definition SE3.h:237
SE3< T2 > cast() const
Cast the underlying numeric type.
Definition SE3.h:525
SE3()
Create a transform (with garbage data).
Definition SE3.h:145
Matrix< Tout, 3, 1 > operator*(const Matrix< T2, 3, 1 > &v) const
Transform a vector via multiplication: .
Definition SE3.h:386
static Mat4T log(const SE3 &x)
Logarithmic chart map implementation: .
Definition SE3.h:452
static SE3 nans()
Obtain a transform full of NaNs.
Definition SE3.h:73
static SE3 fromVecAndQuat(const Vec3T &tvec, const Quaternion< T > quat)
Construct a transform from a translation vector and an Eigen Quaternion object.
Definition SE3.h:135
const SO3< T > & q() const
Access the rotation .
Definition SE3.h:189
static SE3 Exp(const Vec6T &omega)
Exponential chart map implementation: .
Definition SE3.h:497
static SE3 random()
Obtain a random rigid body transform.
Definition SE3.h:51
static SE3 exp(const Mat4T &Omega)
Exponential chart map implementation: .
Definition SE3.h:489
T & operator[](int i)
Access a field from .
Definition SE3.h:173
SE3 & invert()
Invert the current transform .
Definition SE3.h:276
Matrix< Tout, 6, 1 > ominus(const SE3< T2 > &x) const
Implementation of group subtraction: .
Definition SE3.h:311
Class representing a member of the manifold, or a 3D rotation.
Definition SO3.h:15
static SO3 Exp(const Vec3T &omega)
Exponential chart map implementation: .
Definition SO3.h:681
static Vec3T vee(const Mat3T &Omega)
Vee operator implementation, which coverts the Lie algebra representation to a tangent-space vector r...
Definition SO3.h:640
static Mat3T hat(const Vec3T &omega)
Hat operator implementation, which coverts the tangent-space vector representation to the correspondi...
Definition SO3.h:629
SO3 inverse() const
Obtain the inverse rotation .
Definition SO3.h:400
static Vec3T Log(const SO3 &q)
Logarithmic chart map implementation: .
Definition SO3.h:658