manif-geom-cpp
Loading...
Searching...
No Matches
SO2.h
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4#include <iostream>
5#include <math.h>
6
7using namespace Eigen;
8
12template<typename T>
13class SO2
14{
15private:
16 typedef Matrix<T, 1, 1> Vec1T;
17 typedef Matrix<T, 2, 1> Vec2T;
18 typedef Matrix<T, 2, 2> Mat2T;
19
20 T buf_[2];
21
22public:
28
35 static SO2 random()
36 {
37 SO2 q;
38 q.arr_.setRandom();
39 q.normalize();
40 return q;
41 }
42
46 static SO2 identity()
47 {
48 SO2 q;
49 q.arr_ << 1., 0.;
50 return q;
51 }
52
56 static SO2 nans()
57 {
58 SO2 x;
59 x.arr_.setConstant(std::numeric_limits<T>::quiet_NaN());
60 return x;
61 }
62
66 static SO2 fromAngle(const T angle)
67 {
68 SO2 q;
69 q.arr_ << cos(angle), sin(angle);
70 return q;
71 }
72
76 static SO2 fromR(const Mat2T& m)
77 {
78 SO2 q;
79 q.arr_ << m(0, 0), m(1, 0);
80 return q;
81 }
82
87 static SO2 fromTwoUnitVectors(const Vec2T& u, const Vec2T& v)
88 {
89 SO2 q;
90 T d = u.dot(v);
91
92 if (d < (T)0.99999999 && d > (T)-0.99999999)
93 {
94 q.arr_ << d, u(0) * v(1) - u(1) * v(0);
95 }
96 else if (d < (T)-0.99999999)
97 {
98 q.arr_ << -1., 0.;
99 }
100 else
101 {
102 q = SO2::identity();
103 }
104
105 return q;
106 }
107
113 static SO2 fromComplex(const T qw, const T qx)
114 {
115 SO2 q;
116 q.arr_ << qw, qx;
117 return q;
118 }
119
124 static SO2 fromComplex(const Vec2T& qvec)
125 {
126 SO2 q;
127 q.arr_ << qvec(0), qvec(1);
128 return q;
129 }
130
134 SO2() : arr_(buf_) {}
135
139 SO2(const Ref<const Vec2T>& arr) : arr_(buf_)
140 {
141 arr_ = arr;
142 }
143
147 SO2(const SO2& q) : arr_(buf_)
148 {
149 arr_ = q.arr_;
150 }
151
156 SO2(const T* data) : arr_(const_cast<T*>(data)) {}
157
161 inline T& operator[](int i)
162 {
163 return arr_[i];
164 }
165
169 inline const T& w() const
170 {
171 return arr_(0);
172 }
173
177 inline const T& x() const
178 {
179 return arr_(1);
180 }
181
185 inline T& w()
186 {
187 return arr_(0);
188 }
189
193 inline T& x()
194 {
195 return arr_(1);
196 }
197
201 inline const Vec2T elements() const
202 {
203 return arr_;
204 }
205
209 inline Vec2T array() const
210 {
211 return arr_;
212 }
213
217 inline T* data()
218 {
219 return arr_.data();
220 }
221
225 inline const T* data() const
226 {
227 return arr_.data();
228 }
229
233 SO2 copy() const
234 {
235 SO2 tmp;
236 tmp.arr_ = arr_;
237 return tmp;
238 }
239
244 {
245 arr_ /= arr_.norm();
246 }
247
252 {
253 SO2 tmp = copy();
254 tmp.normalize();
255 return tmp;
256 }
257
262 Mat2T R() const
263 {
264 T costh = w();
265 T sinth = x();
266 Mat2T out;
267 out << costh, -sinth, sinth, costh;
268 return out;
269 }
270
274 SO2 inverse() const
275 {
276 SO2 q;
277 q.arr_(0) = arr_(0);
278 q.arr_(1) = -arr_(1);
279 return q;
280 }
281
286 {
287 arr_(1) *= (T)-1.0;
288 return *this;
289 }
290
296 T angle() const
297 {
298 return atan2(x(), w());
299 }
300
305 template<typename Tout = T, typename T2>
306 SO2<Tout> otimes(const SO2<T2>& q) const
307 {
309 qout.arr_ << w() * q.w() - x() * q.x(), w() * q.x() + x() * q.w();
310 return qout;
311 }
312
317 template<typename Tout = T, typename T2>
319 {
321 }
322
327 template<typename Tout = T, typename T2>
329 {
330 SO2<Tout> dq = q.inverse().template otimes<Tout>(*this);
331 return SO2<Tout>::Log(dq);
332 }
333
337 SO2& operator=(const SO2& q)
338 {
339 arr_ = q.elements();
340 return *this;
341 }
342
346 template<typename T2>
347 SO2 operator*(const SO2<T2>& q) const
348 {
349 return otimes(q);
350 }
351
355 template<typename T2>
357 {
358 arr_ = otimes(q).elements();
359 return *this;
360 }
361
368 SO2& operator*=(const double& s)
369 {
370 arr_ = SO2::Exp(s * SO2::Log(*this)).elements();
371 return *this;
372 }
373
380 SO2& operator/=(const double& s)
381 {
382 arr_ = SO2::Exp(SO2::Log(*this) / s).elements();
383 return *this;
384 }
385
392 SO2 operator/(const double& s) const
393 {
394 SO2 qs;
395 qs.arr_ = SO2::Exp(SO2::Log(*this) / s).elements();
396 return qs;
397 }
398
403 template<typename Tout = T, typename T2>
405 {
406 Vec2T out;
407 out << w() * v.x() - x() * v.y(), w() * v.y() + x() * v.x();
408 return out;
409 }
410
415 Vec2T operator*(const Vec2T& v) const
416 {
417 Vec2T out;
418 out << w() * v.x() - x() * v.y(), w() * v.y() + x() * v.x();
419 return out;
420 }
421
425 SO2 operator+(const Vec1T& v) const
426 {
427 return oplus(v);
428 }
429
433 SO2& operator+=(const Vec1T& v)
434 {
435 arr_ = oplus(v).elements();
436 return *this;
437 }
438
442 template<typename T2>
443 Vec1T operator-(const SO2<T2>& q) const
444 {
445 return ominus(q);
446 }
447
452 static Mat2T hat(const Vec1T& omega)
453 {
454 Mat2T Omega;
455 Omega << (T)0., -omega.x(), omega.x(), (T)0.;
456 return Omega;
457 }
458
463 static Vec1T vee(const Mat2T& Omega)
464 {
465 Vec1T omega;
466 omega << Omega(1, 0);
467 return omega;
468 }
469
473 static Mat2T log(const SO2& q)
474 {
475 return hat(SO2::Log(q));
476 }
477
481 static Vec1T Log(const SO2& q)
482 {
483 Vec1T out;
484 out << q.angle();
485 return out;
486 }
487
491 static SO2 exp(const Mat2T& Omega)
492 {
493 return SO2::Exp(vee(Omega));
494 }
495
499 static SO2 Exp(const Vec1T& omega)
500 {
501 return SO2::fromAngle(omega.x());
502 }
503
507 template<typename T2>
508 SO2<T2> cast() const
509 {
510 SO2<T2> q;
511 q.arr_ = arr_.template cast<T2>();
512 return q;
513 }
514};
515
522template<typename T>
523SO2<T> operator*(const double& l, const SO2<T>& r)
524{
525 SO2<T> lr;
526 lr.arr_ = SO2<T>::Exp(l * SO2<T>::Log(r)).elements();
527 return lr;
528}
529
536template<typename T>
537SO2<T> operator*(const SO2<T>& l, const double& r)
538{
539 SO2<T> lr;
540 lr.arr_ = SO2<T>::Exp(r * SO2<T>::Log(l)).elements();
541 return lr;
542}
543
547template<typename T>
548inline std::ostream& operator<<(std::ostream& os, const SO2<T>& q)
549{
550 os << "SO(2): [ " << q.w() << ", " << q.x() << "i ]";
551 return os;
552}
553
SO2< T > operator*(const double &l, const SO2< T > &r)
Scale a rotation by a scalar.
Definition SO2.h:523
std::ostream & operator<<(std::ostream &os, const SO2< T > &q)
Render the rotation in a stream.
Definition SO2.h:548
SO2< double > SO2d
Definition SO2.h:554
Class representing a member of the manifold, or a 2D rotation.
Definition SO2.h:14
const Vec2T elements() const
Access all elements of .
Definition SO2.h:201
T & x()
Access the imaginary element of the rotation.
Definition SO2.h:193
SO2 operator+(const Vec1T &v) const
Invocation of oplus via addition.
Definition SO2.h:425
static SO2 identity()
Obtain an identity rotation.
Definition SO2.h:46
SO2 operator*(const SO2< T2 > &q) const
Invocation of otimes via multiplication.
Definition SO2.h:347
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 fromAngle(const T angle)
Convert an angle (in radians) into a rotation.
Definition SO2.h:66
static SO2 fromComplex(const Vec2T &qvec)
Construct a rotation from a rotation fields vector.
Definition SO2.h:124
static SO2 Exp(const Vec1T &omega)
Exponential chart map implementation: .
Definition SO2.h:499
static SO2 random()
Obtain a random rotation.
Definition SO2.h:35
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Map< Vec2T > arr_
Memory-mapped array representing all rotation fields in .
Definition SO2.h:27
SO2 & operator*=(const double &s)
Scale a rotation by a scalar.
Definition SO2.h:368
SO2()
Create a rotation (with garbage data).
Definition SO2.h:134
T * data()
Access pointer to all elements of .
Definition SO2.h:217
Matrix< Tout, 2, 1 > operator*(const Matrix< T2, 2, 1 > &v) const
Transform a vector via multiplication: .
Definition SO2.h:404
static Mat2T log(const SO2 &q)
Logarithmic chart map implementation: .
Definition SO2.h:473
Vec2T array() const
Access all elements of .
Definition SO2.h:209
SO2(const Ref< const Vec2T > &arr)
Create a transform from an array representing all rotation fields in .
Definition SO2.h:139
SO2(const T *data)
Create a rotation from a pointer array representing all rotation fields in .
Definition SO2.h:156
SO2 copy() const
Get a deep copy of the current rotation.
Definition SO2.h:233
SO2< T2 > cast() const
Cast the underlying numeric type.
Definition SO2.h:508
SO2(const SO2 &q)
Copy constructor from another rotation.
Definition SO2.h:147
SO2 operator/(const double &s) const
Scale a rotation by a scalar.
Definition SO2.h:392
SO2 & operator=(const SO2 &q)
Copy constructor.
Definition SO2.h:337
const T * data() const
Access pointer to all elements of .
Definition SO2.h:225
SO2 inverse() const
Obtain the inverse rotation .
Definition SO2.h:274
const T & x() const
Access the imaginary element of the rotation.
Definition SO2.h:177
SO2< Tout > otimes(const SO2< T2 > &q) const
Implementation of group composition: .
Definition SO2.h:306
T & operator[](int i)
Access a field from .
Definition SO2.h:161
static Vec1T Log(const SO2 &q)
Logarithmic chart map implementation: .
Definition SO2.h:481
const T & w() const
Access the real element of the rotation.
Definition SO2.h:169
void normalize()
Normalize the elements of to make it a valid rotation.
Definition SO2.h:243
SO2< Tout > oplus(const Matrix< T2, 1, 1 > &delta) const
Implementation of tangent space group perturbations: .
Definition SO2.h:318
T & w()
Access the real element of the rotation.
Definition SO2.h:185
SO2 & operator*=(const SO2< T2 > &q)
Invocation of otimes via multiplication.
Definition SO2.h:356
Mat2T R() const
Convert the rotation to matrix representation .
Definition SO2.h:262
static SO2 exp(const Mat2T &Omega)
Exponential chart map implementation: .
Definition SO2.h:491
static SO2 nans()
Obtain a rotation full of NaNs.
Definition SO2.h:56
SO2 & invert()
Invert the current rotation .
Definition SO2.h:285
SO2 & operator+=(const Vec1T &v)
Invocation of oplus via addition.
Definition SO2.h:433
static Mat2T hat(const Vec1T &omega)
Hat operator implementation, which coverts the tangent-space vector representation to the correspondi...
Definition SO2.h:452
SO2 normalized()
Obtain a normalized copy of the current rotation.
Definition SO2.h:251
Vec2T operator*(const Vec2T &v) const
Transform a vector via multiplication: .
Definition SO2.h:415
Vec1T operator-(const SO2< T2 > &q) const
Invocation of ominus via subtraction.
Definition SO2.h:443
static SO2 fromTwoUnitVectors(const Vec2T &u, const Vec2T &v)
Given two unit vectors , returns the rotation that rotates .
Definition SO2.h:87
static SO2 fromR(const Mat2T &m)
Convert a rotation matrix to a rotation.
Definition SO2.h:76
T angle() const
Obtain the equivalent scalar angle of the rotation.
Definition SO2.h:296
Matrix< Tout, 1, 1 > ominus(const SO2< T2 > &q) const
Implementation of group subtraction: .
Definition SO2.h:328
SO2 & operator/=(const double &s)
Scale a rotation by a scalar.
Definition SO2.h:380
static SO2 fromComplex(const T qw, const T qx)
Construct a rotation from the individual fields.
Definition SO2.h:113