gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Rot2.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 
19 #pragma once
20 
21 #include <boost/optional.hpp>
22 
23 #include <gtsam/base/DerivedValue.h>
24 #include <gtsam/geometry/Point2.h>
25 
26 namespace gtsam {
27 
34  class GTSAM_EXPORT Rot2 : public DerivedValue<Rot2> {
35 
36  public:
38  static const size_t dimension = 1;
39 
40  private:
41 
43  double c_, s_;
44 
45 
47  Rot2& normalize();
48 
50  inline Rot2(double c, double s) :
51  c_(c), s_(s) {
52  }
53 
54  public:
55 
58 
60  Rot2() :
61  c_(1.0), s_(0.0) {
62  }
63 
65  Rot2(double theta) :
66  c_(cos(theta)), s_(sin(theta)) {
67  }
68 
70  static Rot2 fromAngle(double theta) {
71  return Rot2(theta);
72  }
73 
75  static Rot2 fromDegrees(double theta) {
76  const double degree = M_PI / 180;
77  return fromAngle(theta * degree);
78  }
79 
81  static Rot2 fromCosSin(double c, double s);
82 
90  static Rot2 relativeBearing(const Point2& d, boost::optional<Matrix&> H =
91  boost::none);
92 
94  static Rot2 atan2(double y, double x);
95 
99 
101  void print(const std::string& s = "theta") const;
102 
104  bool equals(const Rot2& R, double tol = 1e-9) const;
105 
109 
111  inline static Rot2 identity() { return Rot2(); }
112 
114  Rot2 inverse() const {
115  return Rot2(c_, -s_);
116  }
117 
119  inline Rot2 compose(const Rot2& R, boost::optional<Matrix&> H1 =
120  boost::none, boost::optional<Matrix&> H2 = boost::none) const {
121  if (H1) *H1 = eye(1);
122  if (H2) *H2 = eye(1);
123  return fromCosSin(c_ * R.c_ - s_ * R.s_, s_ * R.c_ + c_ * R.s_);
124  }
125 
127  Rot2 operator*(const Rot2& R) const {
128  return fromCosSin(c_ * R.c_ - s_ * R.s_, s_ * R.c_ + c_ * R.s_);
129  }
130 
132  inline Rot2 between(const Rot2& R, boost::optional<Matrix&> H1 =
133  boost::none, boost::optional<Matrix&> H2 = boost::none) const {
134  if (H1) *H1 = -eye(1);
135  if (H2) *H2 = eye(1);
136  return fromCosSin(c_ * R.c_ + s_ * R.s_, -s_ * R.c_ + c_ * R.s_);
137  }
138 
142 
144  inline static size_t Dim() {
145  return dimension;
146  }
147 
149  inline size_t dim() const {
150  return dimension;
151  }
152 
154  inline Rot2 retract(const Vector& v) const { return *this * Expmap(v); }
155 
157  inline Vector localCoordinates(const Rot2& t2) const { return Logmap(between(t2)); }
158 
162 
164  static Rot2 Expmap(const Vector& v) {
165  if (zero(v))
166  return (Rot2());
167  else
168  return Rot2::fromAngle(v(0));
169  }
170 
172  static inline Vector Logmap(const Rot2& r) {
173  return (Vector(1) << r.theta());
174  }
175 
179 
183  Point2 rotate(const Point2& p, boost::optional<Matrix&> H1 = boost::none,
184  boost::optional<Matrix&> H2 = boost::none) const;
185 
187  inline Point2 operator*(const Point2& p) const {
188  return rotate(p);
189  }
190 
194  Point2 unrotate(const Point2& p, boost::optional<Matrix&> H1 = boost::none,
195  boost::optional<Matrix&> H2 = boost::none) const;
196 
200 
202  inline Point2 unit() const {
203  return Point2(c_, s_);
204  }
205 
207  double theta() const {
208  return ::atan2(s_, c_);
209  }
210 
212  double degrees() const {
213  const double degree = M_PI / 180;
214  return theta() / degree;
215  }
216 
218  inline double c() const {
219  return c_;
220  }
221 
223  inline double s() const {
224  return s_;
225  }
226 
228  Matrix matrix() const;
229 
231  Matrix transpose() const;
232 
236 
237  private:
239  friend class boost::serialization::access;
240  template<class ARCHIVE>
241  void serialize(ARCHIVE & ar, const unsigned int version) {
242  ar & boost::serialization::make_nvp("Rot2",
243  boost::serialization::base_object<Value>(*this));
244  ar & BOOST_SERIALIZATION_NVP(c_);
245  ar & BOOST_SERIALIZATION_NVP(s_);
246  }
247 
249 
250  };
251 
252 } // gtsam
Rot2 compose(const Rot2 &R, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
Compose - make a new rotation by adding angles.
Definition: Rot2.h:119
double c() const
return cos
Definition: Rot2.h:218
Point2 operator*(const Point2 &p) const
syntactic sugar for rotate
Definition: Rot2.h:187
Rot2 inverse() const
The inverse rotation - negative angle.
Definition: Rot2.h:114
Matrix eye(size_t m, size_t n)
Creates an identity matrix, with matlab-like syntax.
Definition: Matrix.cpp:50
Rot2 operator*(const Rot2 &R) const
Compose - make a new rotation by adding angles.
Definition: Rot2.h:127
Rot2()
default constructor, zero rotation
Definition: Rot2.h:60
Definition: Point2.h:35
double theta() const
return angle (RADIANS)
Definition: Rot2.h:207
2D Point
bool zero(const Vector &v)
check if all zero
Definition: Vector.cpp:39
Rot2(double theta)
Constructor from angle in radians == exponential map at identity.
Definition: Rot2.h:65
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
double s() const
return sin
Definition: Rot2.h:223
Rot2 retract(const Vector &v) const
Updates a with tangent space delta.
Definition: Rot2.h:154
Template to create a binary predicate.
Definition: Testable.h:102
Point2 unit() const
Creates a unit vector as a Point2.
Definition: Rot2.h:202
size_t dim() const
Dimensionality of the tangent space, DOF = 1.
Definition: Rot2.h:149
static Rot2 identity()
identity
Definition: Rot2.h:111
static size_t Dim()
dimension of the variable - used to autodetect sizes
Definition: Rot2.h:144
static Rot2 fromDegrees(double theta)
Named constructor from angle in degrees.
Definition: Rot2.h:75
double degrees() const
return angle (DEGREES)
Definition: Rot2.h:212
Vector localCoordinates(const Rot2 &t2) const
Returns inverse retraction.
Definition: Rot2.h:157
static Vector Logmap(const Rot2 &r)
Log map at identity - return the canonical coordinates of this rotation.
Definition: Rot2.h:172
Definition: DerivedValue.h:44
Definition: Rot2.h:34
static Rot2 fromAngle(double theta)
Named constructor from angle in radians.
Definition: Rot2.h:70
static Rot2 Expmap(const Vector &v)
Exponential map at identity - create a rotation from canonical coordinates.
Definition: Rot2.h:164
Rot2 between(const Rot2 &R, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
Between using the default implementation.
Definition: Rot2.h:132