Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rotation.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_ROTATION_H_
19 #define ION_MATH_ROTATION_H_
20 
21 #include <istream> // NOLINT
22 #include <ostream> // NOLINT
23 #include <string>
24 
25 #include "ion/base/stringutils.h"
26 #include "ion/math/angle.h"
27 #include "ion/math/matrix.h"
28 #include "ion/math/vector.h"
29 #include "ion/math/vectorutils.h"
30 
31 namespace ion {
32 namespace math {
33 
36 template <typename T>
37 class Rotation {
38  public:
43 
46  quat_.Set(static_cast<T>(0), static_cast<T>(0), static_cast<T>(0),
47  static_cast<T>(1));
48  }
49 
51  static Rotation Identity() {
52  return Rotation();
53  }
54 
56  bool IsIdentity() const {
57  return quat_[3] == static_cast<T>(1) ||
58  quat_[3] == static_cast<T>(-1);
59  }
60 
62  void SetQuaternion(const QuaternionType& quaternion) {
63  quat_ = Normalized(quaternion);
64  }
65 
67  const QuaternionType& GetQuaternion() const { return quat_; }
68 
72  void SetAxisAndAngle(const VectorType& axis, const AngleType& angle);
73 
77  void GetAxisAndAngle(VectorType* axis, AngleType* angle) const;
78 
81  void GetEulerAngles(AngleType* yaw, AngleType* pitch, AngleType* roll) const;
82 
85  static Rotation FromAxisAndAngle(const VectorType& axis,
86  const AngleType& angle) {
87  Rotation r;
88  r.SetAxisAndAngle(axis, angle);
89  return r;
90  }
91 
94  static Rotation FromQuaternion(const QuaternionType& quat) {
95  Rotation r;
96  r.SetQuaternion(quat);
97  return r;
98  }
99 
102  static Rotation FromRotationMatrix(const Matrix<3, T>& mat);
103 
107  static Rotation RotateInto(const VectorType& from,
108  const VectorType& to);
109 
111  friend Rotation operator-(const Rotation& r) {
114  return Rotation(-r.quat_[0], -r.quat_[1], -r.quat_[2], r.quat_[3]);
115  }
116 
119  const QuaternionType &qr = r.quat_;
120  QuaternionType &qt = quat_;
122  qr[3] * qt[0] + qr[0] * qt[3] + qr[2] * qt[1] - qr[1] * qt[2],
123  qr[3] * qt[1] + qr[1] * qt[3] + qr[0] * qt[2] - qr[2] * qt[0],
124  qr[3] * qt[2] + qr[2] * qt[3] + qr[1] * qt[0] - qr[0] * qt[1],
125  qr[3] * qt[3] - qr[0] * qt[0] - qr[1] * qt[1] - qr[2] * qt[2]));
126  return *this;
127  }
128 
130  friend const Rotation operator*(const Rotation& r0, const Rotation& r1) {
131  Rotation r = r0;
132  r *= r1;
133  return r;
134  }
135 
137  const VectorType operator*(const VectorType& v) const;
138  const Point<3, T> operator*(const Point<3, T>& p) const;
139 
141  friend bool operator==(const Rotation& v0, const Rotation& v1) {
142  return v0.quat_ == v1.quat_ || v0.quat_ == -v1.quat_;
143  }
144  friend bool operator!=(const Rotation& v0, const Rotation& v1) {
145  return v0.quat_ != v1.quat_ && v0.quat_ != -v1.quat_;
146  }
147 
151  static Rotation Slerp(const Rotation& r0, const Rotation& r1, T t);
152 
153  private:
155  Rotation(T q0, T q1, T q2, T q3) : quat_(q0, q1, q2, q3) {}
156 
159  VectorType ApplyToVector(const VectorType& v) const {
160  VectorType im(quat_[0], quat_[1], quat_[2]);
161  VectorType temp = static_cast<T>(2) * Cross(im, v);
162  return v + quat_[3] * temp + Cross(im, temp);
163  }
164 
169  QuaternionType quat_;
170 };
171 
172 template <typename T>
173 const typename Rotation<T>::VectorType Rotation<T>::operator*(
174  const typename Rotation<T>::VectorType& v) const {
175  return ApplyToVector(v);
176 }
177 
178 template <typename T>
180  return ApplyToVector(p - Point<3, T>::Zero()) + Point<3, T>::Zero();
181 }
182 
184 template <typename T>
185 std::ostream& operator<<(std::ostream& out, const Rotation<T>& a) {
186  Vector<3, T> axis;
187  Angle<T> angle;
188  a.GetAxisAndAngle(&axis, &angle);
189  return out << "ROT[" << axis << ": " << angle << "]";
190 }
191 
193 template <typename T>
194 std::istream& operator>>(std::istream& in, Rotation<T>& a) {
195  if (base::GetExpectedString(in, "ROT[")) {
196  Vector<3, T> axis;
197  Angle<T> angle;
198  if (in >> axis >> base::GetExpectedChar<':'> >> angle
199  >> base::GetExpectedChar<']'>)
200  a.SetAxisAndAngle(axis, angle);
201  }
202 
203  return in;
204 }
205 
207 
210 
211 
214 
215 } // namespace math
216 } // namespace ion
217 
218 #endif // ION_MATH_ROTATION_H_
static Rotation Slerp(const Rotation &r0, const Rotation &r1, T t)
Performs spherical linear interpolation between two Rotation instances.
Definition: rotation.cc:178
friend const Rotation operator*(const Rotation &r0, const Rotation &r1)
Binary multiplication operator - returns a composite Rotation.
Definition: rotation.h:130
friend bool operator==(const Rotation &v0, const Rotation &v1)
Exact equality and inequality comparisons.
Definition: rotation.h:141
Rotation< double > Rotationd
Definition: rotation.h:213
void GetEulerAngles(AngleType *yaw, AngleType *pitch, AngleType *roll) const
Returns the Euler angles which would result in this rotation if done in the order of rotate-Y by yaw...
Definition: rotation.cc:115
bool IsIdentity() const
Returns true if this represents an identity Rotation.
Definition: rotation.h:56
A simple class to represent angles.
Definition: angle.h:33
Vector< 4, T > QuaternionType
Definition: rotation.h:42
static Rotation FromQuaternion(const QuaternionType &quat)
Convienance function that constructs and returns a Rotation given a quaternion.
Definition: rotation.h:94
void Set(T e0)
Sets the vector values.
Definition: vector.h:464
The Matrix class defines a square N-dimensional matrix.
Definition: matrix.h:35
friend Rotation operator-(const Rotation &r)
The negation operator returns the inverse rotation.
Definition: rotation.h:111
const QuaternionType & GetQuaternion() const
Returns the Rotation as a normalized quaternion (4D vector).
Definition: rotation.h:67
static Rotation Identity()
Returns an identity Rotation, which has no effect.
Definition: rotation.h:51
void SetQuaternion(const QuaternionType &quaternion)
Sets the Rotation from a quaternion (4D vector), which is first normalized.
Definition: rotation.h:62
const Vector< Dimension, T > Normalized(const Vector< Dimension, T > &v)
Returns a unit-length version of a Vector.
Definition: vectorutils.h:104
static Rotation FromRotationMatrix(const Matrix< 3, T > &mat)
Convienance function that constructs and returns a Rotation given a rotation matrix R with $R^ R = I ...
Definition: rotation.cc:52
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
Definition: stringutils.h:215
Vector< 3, T > VectorType
Definition: rotation.h:41
Vector.
Definition: vector.h:180
friend bool operator!=(const Rotation &v0, const Rotation &v1)
Definition: rotation.h:144
The Rotation class represents a rotation around a 3-dimensional axis.
Definition: rotation.h:37
std::istream & GetExpectedString(std::istream &in, const std::string &expected)
Attempts to read a string from the stream and returns the stream.
Definition: stringutils.h:228
void GetAxisAndAngle(VectorType *axis, AngleType *angle) const
Returns the right-hand rule axis and angle corresponding to the Rotation.
Definition: rotation.cc:100
static Rotation RotateInto(const VectorType &from, const VectorType &to)
Constructs and returns a Rotation that rotates one vector to another along the shortest arc...
Definition: rotation.cc:150
static const Point Zero()
Returns a Point containing all zeroes.
Definition: vector.h:320
Point.
Definition: vector.h:296
Rotation & operator*=(const Rotation &r)
Appends a rotation to this one.
Definition: rotation.h:118
Rotation()
The default constructor creates an identity Rotation, which has no effect.
Definition: rotation.h:45
std::istream & operator>>(std::istream &in, Angle< T > &a)
Definition: angle.h:135
Rotation< float > Rotationf
Type-specific typedefs.
Definition: rotation.h:212
void SetAxisAndAngle(const VectorType &axis, const AngleType &angle)
Sets the Rotation to rotate by the given angle around the given axis, following the right-hand rule...
Definition: rotation.cc:39
Angle< T > AngleType
Convenience typedefs for Angle and 3D vector of the correct type.
Definition: rotation.h:40
Vector< 3, T > Cross(const Vector< 3, T > &v0, const Vector< 3, T > &v1)
Returns the 3-dimensional cross product of 2 Vectors.
Definition: vectorutils.h:48
static Rotation FromAxisAndAngle(const VectorType &axis, const AngleType &angle)
Convenience function that constructs and returns a Rotation given an axis and angle.
Definition: rotation.h:85