manif-geom-cpp
Loading...
Searching...
No Matches
SE2.h
Go to the documentation of this file.
1#pragma once
2
3#include "SO2.h"
4#include <Eigen/Core>
5#include <Eigen/Geometry>
6#include <iostream>
7#include <math.h>
8#include <limits>
9
10using namespace Eigen;
11
15template<typename T>
16class SE2
17{
18private:
19 typedef Matrix<T, 1, 1> Vec1T;
20 typedef Matrix<T, 2, 1> Vec2T;
21 typedef Matrix<T, 3, 1> Vec3T;
22 typedef Matrix<T, 4, 1> Vec4T;
23 typedef Matrix<T, 2, 2> Mat2T;
24 typedef Matrix<T, 3, 3> Mat3T;
25
26 T buf_[4];
27
28public:
29 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34 Map<Vec4T> arr_;
38 Map<Vec2T> t_;
43
49 static SE2 random()
50 {
51 SE2 x;
52 x.arr_.setRandom();
53 x.q_.normalize();
54 return x;
55 }
56
60 static SE2 identity()
61 {
62 SE2 x;
63 x.arr_.setZero();
64 x.arr_(2) = (T)1.0;
65 return x;
66 }
67
71 static SE2 nans()
72 {
73 SE2 x;
74 x.arr_.setConstant(std::numeric_limits<T>::quiet_NaN());
75 return x;
76 }
77
82 static SE2 fromH(const Mat3T& m)
83 {
84 return SE2::fromVecAndRot(m.template block<2, 1>(0, 2), SO2<T>::fromR(m.template block<2, 2>(0, 0)));
85 }
86
94 static SE2 fromVecAndRot(const T tx, const T ty, const T qw, const T qx)
95 {
96 SE2 x;
97 x.arr_ << tx, ty, qw, qx;
98 return x;
99 }
100
106 static SE2 fromVecAndRot(const Vec2T& tvec, const Vec2T& qvec)
107 {
108 SE2 x;
109 x.arr_ << tvec(0), tvec(1), qvec(0), qvec(1);
110 return x;
111 }
112
118 static SE2 fromVecAndRot(const Vec2T& t, const SO2<T>& q)
119 {
120 SE2 x;
121 x.arr_ << t(0), t(1), q.w(), q.x();
122 return x;
123 }
124
128 SE2() : arr_(buf_), t_(arr_.data()), q_(arr_.data() + 2) {}
129
134 SE2(const Ref<const Vec4T>& arr) : arr_(buf_), t_(arr_.data()), q_(arr_.data() + 2)
135 {
136 arr_ = arr;
137 }
138
142 SE2(const SE2& x) : arr_(buf_), t_(arr_.data()), q_(arr_.data() + 2)
143 {
144 arr_ = x.arr_;
145 }
146
151 SE2(const T* data) : arr_(const_cast<T*>(data)), t_(arr_.data()), q_(arr_.data() + 2) {}
152
156 inline T& operator[](int i)
157 {
158 return arr_[i];
159 }
160
164 inline const Map<Vec2T>& t() const
165 {
166 return t_;
167 }
168
172 inline const SO2<T>& q() const
173 {
174 return q_;
175 }
176
180 inline Map<Vec2T>& t()
181 {
182 return t_;
183 }
184
188 inline SO2<T>& q()
189 {
190 return q_;
191 }
192
196 inline const Vec4T elements() const
197 {
198 return arr_;
199 }
200
204 inline Vec4T array() const
205 {
206 return arr_;
207 }
208
212 inline T* data()
213 {
214 return arr_.data();
215 }
216
220 inline const T* data() const
221 {
222 return arr_.data();
223 }
224
228 SE2 copy() const
229 {
230 SE2 tmp;
231 tmp.arr_ = arr_;
232 return tmp;
233 }
234
239 Mat3T H() const
240 {
241 Mat3T out;
242 out << q_.R(), t_, (T)0., (T)0., (T)1.;
243 return out;
244 }
245
249 SE2 inverse() const
250 {
251 SE2 x;
252 SO2<T> q_inv = q_.inverse();
253 return SE2::fromVecAndRot(-(q_inv * t_), q_inv);
254 }
255
260 {
261 q().invert();
262 t() = -(q() * t());
263 return *this;
264 }
265
270 template<typename Tout = T, typename T2>
271 SE2<Tout> otimes(const SE2<T2>& x) const
272 {
273 SE2<Tout> xout;
274 xout.t() = t_ + q_ * x.t_;
275 xout.q() = q_ * x.q_;
276 return xout;
277 }
278
283 template<typename Tout = T, typename T2>
284 SE2<Tout> oplus(const Matrix<T2, 3, 1>& delta) const
285 {
287 }
288
293 template<typename Tout = T, typename T2>
295 {
296 return SE2<Tout>::Log(x.inverse().template otimes<Tout>(*this));
297 }
298
302 SE2& operator=(const SE2& x)
303 {
304 arr_ = x.elements();
305 return *this;
306 }
307
311 template<typename T2>
312 SE2 operator*(const SE2<T2>& x) const
313 {
314 return otimes(x);
315 }
316
320 template<typename T2>
322 {
323 arr_ = otimes(x).elements();
324 return *this;
325 }
326
333 SE2& operator*=(const double& s)
334 {
335 arr_ = SE2::Exp(s * SE2::Log(*this)).elements();
336 return *this;
337 }
338
345 SE2& operator/=(const double& s)
346 {
347 arr_ = SE2::Exp(SE2::Log(*this) / s).elements();
348 return *this;
349 }
350
357 SE2 operator/(const double& s) const
358 {
359 SE2 qs;
360 qs.arr_ = SE2::Exp(SE2::Log(*this) / s).elements();
361 return qs;
362 }
363
368 template<typename Tout = T, typename T2>
370 {
371 return q_ * v + t_;
372 }
373
378 Vec2T operator*(const Vec2T& v) const
379 {
380 return q_ * v + t_;
381 }
382
386 SE2 operator+(const Vec3T& v) const
387 {
388 return oplus(v);
389 }
390
394 SE2& operator+=(const Vec3T& v)
395 {
396 arr_ = oplus(v).elements();
397 return *this;
398 }
399
403 template<typename T2>
404 Vec3T operator-(const SE2<T2>& x) const
405 {
406 return ominus(x);
407 }
408
413 static Mat3T hat(const Vec3T& omega)
414 {
415 Mat3T Omega;
416 Omega << SO2<T>::hat(omega.template block<1, 1>(2, 0)), omega.template block<2, 1>(0, 0), (T)0., (T)0., (T)0.;
417 return Omega;
418 }
419
424 static Vec3T vee(const Mat3T& Omega)
425 {
426 Vec3T omega;
427 omega << Omega.template block<2, 1>(0, 2), SO2<T>::vee(Omega.template block<2, 2>(0, 0));
428 return omega;
429 }
430
434 static Mat3T log(const SE2& x)
435 {
436 return SE2::hat(SE2::Log(x));
437 }
438
442 static Vec3T Log(const SE2& x)
443 {
444 Vec1T w = SO2<T>::Log(x.q_);
445 T th = w.norm();
446
448 if (th > (T)1e-4)
449 {
450 T a = sin(th) / ((T)1. - cos(th));
451 Mat2T skew1;
452 skew1 << (T)0, (T)-1., (T)1., (T)0;
453 leftJacobianInverse = th / (T)2. * (a * Mat2T::Identity() - skew1);
454 }
455 else
456 {
457 leftJacobianInverse = Mat2T::Identity();
458 }
459
460 Vec3T out;
461 out << leftJacobianInverse * x.t_, w;
462
463 return out;
464 }
465
469 static SE2 exp(const Mat3T& Omega)
470 {
471 return SE2::Exp(SE2::vee(Omega));
472 }
473
477 static SE2 Exp(const Vec3T& omega)
478 {
479 Vec2T rho = omega.template block<2, 1>(0, 0);
480 Vec1T w = omega.template block<1, 1>(2, 0);
481 SO2<T> q = SO2<T>::Exp(w);
482 T th = w.norm();
483
484 Mat2T leftJacobian;
485 if (abs(th) > (T)1e-4)
486 {
487 T a = sin(th) / th;
488 T b = ((T)1. - cos(th)) / th;
489 Mat2T skew1;
490 skew1 << (T)0, (T)-1., (T)1., (T)0;
491 leftJacobian = a * Mat2T::Identity() + b * skew1;
492 }
493 else
494 {
495 leftJacobian = Mat2T::Identity();
496 }
497
499 }
500
504 template<typename T2>
505 SE2<T2> cast() const
506 {
507 SE2<T2> x;
508 x.arr_ = arr_.template cast<T2>();
509 return x;
510 }
511};
512
519template<typename T>
520SE2<T> operator*(const double& l, const SE2<T>& r)
521{
522 SE2<T> lr;
523 lr.arr_ = SE2<T>::Exp(l * SE2<T>::Log(r)).elements();
524 return lr;
525}
526
533template<typename T>
534SE2<T> operator*(const SE2<T>& l, const double& r)
535{
536 SE2<T> lr;
537 lr.arr_ = SE2<T>::Exp(r * SE2<T>::Log(l)).elements();
538 return lr;
539}
540
544template<typename T>
545inline std::ostream& operator<<(std::ostream& os, const SE2<T>& x)
546{
547 os << "SE(2): [ " << x.t_.x() << "i, " << x.t_.y() << "j ] [ " << x.q_.w() << ", " << x.q_.x() << "i ]";
548 return os;
549}
550
SE2< double > SE2d
Definition SE2.h:551
SE2< T > operator*(const double &l, const SE2< T > &r)
Scale a transform by a scalar.
Definition SE2.h:520
std::ostream & operator<<(std::ostream &os, const SE2< T > &x)
Render the transform in a stream.
Definition SE2.h:545
Class representing a member of the manifold, or a 2D rigid body transform.
Definition SE2.h:17
SE2 operator*(const SE2< T2 > &x) const
Invocation of otimes via multiplication.
Definition SE2.h:312
const SO2< T > & q() const
Access the rotation .
Definition SE2.h:172
SE2(const Ref< const Vec4T > &arr)
Create a transform from an array representing all transform fields in .
Definition SE2.h:134
static Mat3T hat(const Vec3T &omega)
Hat operator implementation, which coverts the tangent-space vector representation to the correspondi...
Definition SE2.h:413
SE2()
Create a transform (with garbage data).
Definition SE2.h:128
static SE2 nans()
Obtain a transform full of NaNs.
Definition SE2.h:71
const Map< Vec2T > & t() const
Access the translation vector .
Definition SE2.h:164
Map< Vec2T > & t()
Access the translation vector .
Definition SE2.h:180
SE2 operator/(const double &s) const
Scale a transform by a scalar.
Definition SE2.h:357
Vec4T array() const
Access all elements of .
Definition SE2.h:204
SE2 & operator*=(const double &s)
Scale a transform by a scalar.
Definition SE2.h:333
static SE2 exp(const Mat3T &Omega)
Exponential chart map implementation: .
Definition SE2.h:469
SE2< Tout > oplus(const Matrix< T2, 3, 1 > &delta) const
Implementation of tangent space group perturbations: .
Definition SE2.h:284
static SE2 identity()
Obtain an identity transform.
Definition SE2.h:60
Map< Vec2T > t_
Memory-mapped array representing only the translation component of the transform .
Definition SE2.h:38
const T * data() const
Access pointer to all elements of .
Definition SE2.h:220
SE2 inverse() const
Obtain the inverse transform .
Definition SE2.h:249
T * data()
Access pointer to all elements of .
Definition SE2.h:212
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Map< Vec4T > arr_
Memory-mapped array representing all transform fields in .
Definition SE2.h:34
SE2 & operator+=(const Vec3T &v)
Invocation of oplus via addition.
Definition SE2.h:394
Vec2T operator*(const Vec2T &v) const
Transform a vector via multiplication: .
Definition SE2.h:378
static SE2 fromVecAndRot(const Vec2T &tvec, const Vec2T &qvec)
Construct a transform from a translation vector and rotation fields vector.
Definition SE2.h:106
SE2< T2 > cast() const
Cast the underlying numeric type.
Definition SE2.h:505
static SE2 fromVecAndRot(const T tx, const T ty, const T qw, const T qx)
Construct a transform from the individual fields.
Definition SE2.h:94
SE2 & invert()
Invert the current transform .
Definition SE2.h:259
Matrix< Tout, 3, 1 > ominus(const SE2< T2 > &x) const
Implementation of group subtraction: .
Definition SE2.h:294
T & operator[](int i)
Access a field from .
Definition SE2.h:156
SE2< Tout > otimes(const SE2< T2 > &x) const
Implementation of group composition: .
Definition SE2.h:271
SE2 copy() const
Get a deep copy of the current transform.
Definition SE2.h:228
SE2(const SE2 &x)
Copy constructor from another transform.
Definition SE2.h:142
SE2 operator+(const Vec3T &v) const
Invocation of oplus via addition.
Definition SE2.h:386
static SE2 random()
Obtain a random transform.
Definition SE2.h:49
SE2 & operator=(const SE2 &x)
Copy constructor.
Definition SE2.h:302
static SE2 fromVecAndRot(const Vec2T &t, const SO2< T > &q)
Construct a transform from a translation vector and a rotation object.
Definition SE2.h:118
Matrix< Tout, 2, 1 > operator*(const Matrix< T2, 2, 1 > &v) const
Transform a vector via multiplication: .
Definition SE2.h:369
static Vec3T Log(const SE2 &x)
Logarithmic chart map implementation: .
Definition SE2.h:442
Vec3T operator-(const SE2< T2 > &x) const
Invocation of ominus via subtraction.
Definition SE2.h:404
SO2< T > q_
Memory-mapped array representing only the rotation component of the transform .
Definition SE2.h:42
static Vec3T vee(const Mat3T &Omega)
Vee operator implementation, which coverts the Lie algebra representation to a tangent-space vector r...
Definition SE2.h:424
static Mat3T log(const SE2 &x)
Logarithmic chart map implementation: .
Definition SE2.h:434
SE2(const T *data)
Create a transform from a pointer array representing all transform fields in .
Definition SE2.h:151
SE2 & operator/=(const double &s)
Scale a transform by a scalar.
Definition SE2.h:345
SE2 & operator*=(const SE2< T2 > &x)
Invocation of otimes via multiplication.
Definition SE2.h:321
const Vec4T elements() const
Access all elements of .
Definition SE2.h:196
Mat3T H() const
Convert the transform to matrix representation .
Definition SE2.h:239
static SE2 Exp(const Vec3T &omega)
Exponential chart map implementation: .
Definition SE2.h:477
static SE2 fromH(const Mat3T &m)
Convert a transform matrix into a transform.
Definition SE2.h:82
SO2< T > & q()
Access the rotation .
Definition SE2.h:188
Class representing a member of the manifold, or a 2D rotation.
Definition SO2.h:14
static Vec1T vee(const Mat2T &Omega)
Vee operator implementation, which coverts the Lie algebra representation to a tangent-space vector r...
Definition SO2.h:463
static SO2 Exp(const Vec1T &omega)
Exponential chart map implementation: .
Definition SO2.h:499
SO2 inverse() const
Obtain the inverse rotation .
Definition SO2.h:274
static Vec1T Log(const SO2 &q)
Logarithmic chart map implementation: .
Definition SO2.h:481