gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Rot3.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 // \callgraph
21 
22 #pragma once
23 
24 #include <gtsam/config.h> // Get GTSAM_USE_QUATERNIONS macro
25 
26 // You can override the default coordinate mode using this flag
27 #ifndef ROT3_DEFAULT_COORDINATES_MODE
28  #ifdef GTSAM_USE_QUATERNIONS
29  // Exponential map is very cheap for quaternions
30  #define ROT3_DEFAULT_COORDINATES_MODE Rot3::EXPMAP
31  #else
32  // If user doesn't require GTSAM_ROT3_EXPMAP in cmake when building
33  #ifndef GTSAM_ROT3_EXPMAP
34  // For rotation matrices, the Cayley transform is a fast retract alternative
35  #define ROT3_DEFAULT_COORDINATES_MODE Rot3::CAYLEY
36  #else
37  #define ROT3_DEFAULT_COORDINATES_MODE Rot3::EXPMAP
38  #endif
39  #endif
40 #endif
41 
42 #include <gtsam/base/DerivedValue.h>
43 #include <gtsam/base/Matrix.h>
44 #include <gtsam/geometry/Point3.h>
45 #include <gtsam/geometry/Unit3.h>
46 
47 namespace gtsam {
48 
52  typedef Eigen::Quaternion<double, Eigen::DontAlign> Quaternion;
53 
61  class GTSAM_EXPORT Rot3 : public DerivedValue<Rot3> {
62  public:
63  static const size_t dimension = 3;
64 
65  private:
66 #ifdef GTSAM_USE_QUATERNIONS
67 
68  Quaternion quaternion_;
69 #else
70  Matrix3 rot_;
71 #endif
72 
73  public:
74 
77 
79  Rot3();
80 
87  Rot3(const Point3& col1, const Point3& col2, const Point3& col3);
88 
90  Rot3(double R11, double R12, double R13,
91  double R21, double R22, double R23,
92  double R31, double R32, double R33);
93 
95  Rot3(const Matrix3& R);
96 
98  Rot3(const Matrix& R);
99 
104  Rot3(const Quaternion& q);
105 
107  static Rot3 Random(boost::mt19937 & rng);
108 
110  virtual ~Rot3() {}
111 
112  /* Static member function to generate some well known rotations */
113 
115  static Rot3 Rx(double t);
116 
118  static Rot3 Ry(double t);
119 
121  static Rot3 Rz(double t);
122 
124  static Rot3 RzRyRx(double x, double y, double z);
125 
127  inline static Rot3 RzRyRx(const Vector& xyz) {
128  assert(xyz.size() == 3);
129  return RzRyRx(xyz(0), xyz(1), xyz(2));
130  }
131 
133  static Rot3 yaw (double t) { return Rz(t); }
134 
136  static Rot3 pitch(double t) { return Ry(t); }
137 
139  static Rot3 roll (double t) { return Rx(t); }
140 
150  static Rot3 ypr (double y, double p, double r) { return RzRyRx(r,p,y);}
151 
153  static Rot3 quaternion(double w, double x, double y, double z) {
154  Quaternion q(w, x, y, z);
155  return Rot3(q);
156  }
157 
164  static Rot3 rodriguez(const Vector& w, double theta);
165 
172  static Rot3 rodriguez(const Point3& w, double theta);
173 
180  static Rot3 rodriguez(const Unit3& w, double theta);
181 
187  static Rot3 rodriguez(const Vector& v);
188 
196  static Rot3 rodriguez(double wx, double wy, double wz)
197  { return rodriguez((Vector(3) << wx, wy, wz));}
198 
202 
204  void print(const std::string& s="R") const;
205 
207  bool equals(const Rot3& p, double tol = 1e-9) const;
208 
212 
214  inline static Rot3 identity() {
215  return Rot3();
216  }
217 
219  Rot3 inverse(boost::optional<Matrix&> H1=boost::none) const;
220 
222  Rot3 compose(const Rot3& R2,
223  boost::optional<Matrix&> H1=boost::none, boost::optional<Matrix&> H2=boost::none) const;
224 
226  Rot3 operator*(const Rot3& R2) const;
227 
233  Rot3 conjugate(const Rot3& cRb) const {
234  // TODO: do more efficiently by using Eigen or quaternion properties
235  return cRb * (*this) * cRb.inverse();
236  }
237 
241  Rot3 between(const Rot3& R2,
242  boost::optional<Matrix&> H1=boost::none,
243  boost::optional<Matrix&> H2=boost::none) const;
244 
248 
250  static size_t Dim() { return dimension; }
251 
253  size_t dim() const { return dimension; }
254 
266 #ifndef GTSAM_USE_QUATERNIONS
268  SLOW_CAYLEY
269 #endif
270  };
271 
272 #ifndef GTSAM_USE_QUATERNIONS
273  Rot3 retractCayley(const Vector& omega) const;
275 #endif
276 
278  Rot3 retract(const Vector& omega, Rot3::CoordinatesMode mode = ROT3_DEFAULT_COORDINATES_MODE) const;
279 
281  Vector3 localCoordinates(const Rot3& t2, Rot3::CoordinatesMode mode = ROT3_DEFAULT_COORDINATES_MODE) const;
282 
286 
291  static Rot3 Expmap(const Vector& v) {
292  if(zero(v)) return Rot3();
293  else return rodriguez(v);
294  }
295 
299  static Vector3 Logmap(const Rot3& R);
300 
302  static Matrix3 dexpL(const Vector3& v);
303 
305  static Matrix3 dexpInvL(const Vector3& v);
306 
311  static Matrix3 rightJacobianExpMapSO3(const Vector3& x);
312 
316  static Matrix3 rightJacobianExpMapSO3inverse(const Vector3& x);
317 
321 
325  Point3 rotate(const Point3& p, boost::optional<Matrix&> H1 = boost::none,
326  boost::optional<Matrix&> H2 = boost::none) const;
327 
329  Point3 operator*(const Point3& p) const;
330 
334  Point3 unrotate(const Point3& p, boost::optional<Matrix&> H1 = boost::none,
335  boost::optional<Matrix&> H2 = boost::none) const;
336 
340 
342  Unit3 rotate(const Unit3& p, boost::optional<Matrix&> HR = boost::none,
343  boost::optional<Matrix&> Hp = boost::none) const;
344 
346  Unit3 unrotate(const Unit3& p, boost::optional<Matrix&> HR = boost::none,
347  boost::optional<Matrix&> Hp = boost::none) const;
348 
350  Unit3 operator*(const Unit3& p) const;
351 
355 
357  Matrix3 matrix() const;
358 
360  Matrix3 transpose() const;
361 
363  Point3 column(int index) const;
364 
365  Point3 r1() const;
366  Point3 r2() const;
367  Point3 r3() const;
368 
373  Vector3 xyz() const;
374 
379  Vector3 ypr() const;
380 
385  Vector3 rpy() const;
386 
393  inline double roll() const { return ypr()(2); }
394 
401  inline double pitch() const { return ypr()(1); }
402 
409  inline double yaw() const { return ypr()(0); }
410 
414 
418  Quaternion toQuaternion() const;
419 
424  Vector quaternion() const;
425 
427  GTSAM_EXPORT friend std::ostream &operator<<(std::ostream &os, const Rot3& p);
428 
429  private:
431  friend class boost::serialization::access;
432  template<class ARCHIVE>
433  void serialize(ARCHIVE & ar, const unsigned int version)
434  {
435  ar & boost::serialization::make_nvp("Rot3",
436  boost::serialization::base_object<Value>(*this));
437 #ifndef GTSAM_USE_QUATERNIONS
438  ar & boost::serialization::make_nvp("rot11", rot_(0,0));
439  ar & boost::serialization::make_nvp("rot12", rot_(0,1));
440  ar & boost::serialization::make_nvp("rot13", rot_(0,2));
441  ar & boost::serialization::make_nvp("rot21", rot_(1,0));
442  ar & boost::serialization::make_nvp("rot22", rot_(1,1));
443  ar & boost::serialization::make_nvp("rot23", rot_(1,2));
444  ar & boost::serialization::make_nvp("rot31", rot_(2,0));
445  ar & boost::serialization::make_nvp("rot32", rot_(2,1));
446  ar & boost::serialization::make_nvp("rot33", rot_(2,2));
447 #else
448  ar & boost::serialization::make_nvp("w", quaternion_.w());
449  ar & boost::serialization::make_nvp("x", quaternion_.x());
450  ar & boost::serialization::make_nvp("y", quaternion_.y());
451  ar & boost::serialization::make_nvp("z", quaternion_.z());
452 #endif
453  }
454  };
455 
457 
468  GTSAM_EXPORT std::pair<Matrix3,Vector3> RQ(const Matrix3& A);
469 }
static size_t Dim()
dimension of the variable - used to autodetect sizes
Definition: Rot3.h:250
pair< Matrix3, Vector3 > RQ(const Matrix3 &A)
[RQ] receives a 3 by 3 matrix and returns an upper triangular matrix R and 3 rotation angles correspo...
Definition: Rot3.cpp:211
Matrix inverse(const Matrix &A)
invert A
Definition: Matrix.cpp:289
Rot3 conjugate(const Rot3 &cRb) const
Conjugation: given a rotation acting in frame B, compute rotation c1Rc2 acting in a frame C...
Definition: Rot3.h:233
static Rot3 quaternion(double w, double x, double y, double z)
Create from Quaternion coefficients.
Definition: Rot3.h:153
double roll() const
Accessor to get to component of angle representations NOTE: these are not efficient to get to multipl...
Definition: Rot3.h:393
static Rot3 identity()
identity rotation for group operation
Definition: Rot3.h:214
static Rot3 ypr(double y, double p, double r)
Returns rotation nRb from body to nav frame.
Definition: Rot3.h:150
double pitch() const
Accessor to get to component of angle representations NOTE: these are not efficient to get to multipl...
Definition: Rot3.h:401
Use the Lie group exponential map to retract.
Definition: Rot3.h:265
Retract and localCoordinates using the Cayley transform.
Definition: Rot3.h:267
static Rot3 rodriguez(double wx, double wy, double wz)
Rodriguez' formula to compute an incremental rotation matrix.
Definition: Rot3.h:196
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:249
bool zero(const Vector &v)
check if all zero
Definition: Vector.cpp:39
double yaw() const
Accessor to get to component of angle representations NOTE: these are not efficient to get to multipl...
Definition: Rot3.h:409
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
Rot3 inverse(boost::optional< Matrix & > H1=boost::none) const
derivative of inverse rotation R^T s.t. inverse(R)*R = identity
Definition: Rot3M.cpp:160
Eigen::Quaternion< double, Eigen::DontAlign > Quaternion
Typedef to an Eigen Quaternion<double>, we disable alignment because geometry objects are stored in b...
Definition: Rot3.h:52
Represents a 3D point on a unit sphere.
Definition: Unit3.h:31
CoordinatesMode
The method retract() is used to map from the tangent space back to the manifold.
Definition: Rot3.h:264
Definition: Rot3.h:61
Template to create a binary predicate.
Definition: Testable.h:102
virtual ~Rot3()
Virtual destructor.
Definition: Rot3.h:110
static Rot3 yaw(double t)
Positive yaw is to right (as in aircraft heading). See ypr.
Definition: Rot3.h:133
typedef and functions to augment Eigen's MatrixXd
static Rot3 pitch(double t)
Positive pitch is up (increasing aircraft altitude).See ypr.
Definition: Rot3.h:136
static Rot3 RzRyRx(const Vector &xyz)
Rotations around Z, Y, then X axes as in http://en.wikipedia.org/wiki/Rotation_matrix, counterclockwise when looking from unchanging axis.
Definition: Rot3.h:127
static Rot3 Expmap(const Vector &v)
Exponential map at identity - create a rotation from canonical coordinates using Rodriguez' formula...
Definition: Rot3.h:291
size_t dim() const
return dimensionality of tangent space, DOF = 3
Definition: Rot3.h:253
3D Point
Definition: DerivedValue.h:44
Definition: Point3.h:39
const MATRIX::ConstColXpr column(const MATRIX &A, size_t j)
Extracts a column view from a matrix that avoids a copy.
Definition: Matrix.h:235