Quaternion.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Quaternion.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Quaternion class defined as q = w + xi + yj + zk.
6 > Created Time: 2017/03/21
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_QUATERNION_H
10 #define CUBBYFLOW_QUATERNION_H
11 
12 #include <Core/Matrix/Matrix4x4.h>
13 
14 namespace CubbyFlow
15 {
19  template <typename T>
20  class Quaternion
21  {
22  public:
23  static_assert(std::is_floating_point<T>::value, "Quaternion only can be instantiated with floating point types");
24 
26  T w;
27 
29  T x;
30 
32  T y;
33 
35  T z;
36 
37  // MARK: Constructors
39  Quaternion();
40 
42  Quaternion(T newW, T newX, T newY, T newZ);
43 
45  Quaternion(const std::initializer_list<T>& list);
46 
48  Quaternion(const Vector3<T>& axis, T angle);
49 
51  Quaternion(const Vector3<T>& from, const Vector3<T>& to);
52 
54  Quaternion(const Vector3<T>& axis0, const Vector3<T>& axis1, const Vector3<T>& axis2);
55 
57  explicit Quaternion(const Matrix3x3<T>& m33);
58 
60  Quaternion(const Quaternion& other);
61 
62  // MARK: Basic setters
64  void Set(const Quaternion& other);
65 
67  void Set(T newW, T newX, T newY, T newZ);
68 
70  void Set(const std::initializer_list<T>& list);
71 
73  void Set(const Vector3<T>& axis, T angle);
74 
76  void Set(const Vector3<T>& from, const Vector3<T>& to);
77 
79  void Set(const Vector3<T>& rotationBasis0, const Vector3<T>& rotationBasis1, const Vector3<T>& rotationBasis2);
80 
82  void Set(const Matrix3x3<T>& m);
83 
84  // MARK: Basic getters
86  template <typename U>
87  Quaternion<U> CastTo() const;
88 
90  Quaternion Normalized() const;
91 
92  // MARK: Binary operator methods - new instance = this instance (+) input
94  Vector3<T> Mul(const Vector3<T>& v) const;
95 
97  Quaternion Mul(const Quaternion& other) const;
98 
100  T Dot(const Quaternion<T>& other);
101 
102  // MARK: Binary operator methods - new instance = input (+) this instance
104  Quaternion RMul(const Quaternion& other) const;
105 
106  // MARK: Augmented operator methods - this instance (+)= input
108  void IMul(const Quaternion& other);
109 
110  // MARK: Modifiers
112  void SetIdentity();
113 
115  void Rotate(T angleInRadians);
116 
118  void Normalize();
119 
120  // MARK: Complex getters
122  Vector3<T> Axis() const;
123 
125  T Angle() const;
126 
128  void GetAxisAngle(Vector3<T>* axis, T* angle) const;
129 
131  Quaternion Inverse() const;
132 
134  Matrix3x3<T> Matrix3() const;
135 
137  Matrix4x4<T> Matrix4() const;
138 
140  T L2Norm() const;
141 
142  // MARK: Setter operators
144  Quaternion& operator=(const Quaternion& other);
145 
147  Quaternion& operator*=(const Quaternion& other);
148 
149  // MARK: Getter operators
151  T& operator[](size_t i);
152 
154  const T& operator[](size_t i) const;
155 
157  bool operator==(const Quaternion& other) const;
158 
160  bool operator!=(const Quaternion& other) const;
161 
162  // MARK: Builders
164  static Quaternion MakeIdentity();
165  };
166 
168  template <typename T>
169  Quaternion<T> Slerp(const Quaternion<T>& a, const Quaternion<T>& b, T t);
170 
172  template <typename T>
173  Vector<T, 3> operator*(const Quaternion<T>& q, const Vector<T, 3>& v);
174 
176  template <typename T>
178 
181 
184 }
185 
187 
188 #endif
T z
Definition: Quaternion.h:35
3-D vector class.
Definition: Vector3.h:26
T L2Norm() const
Returns L2 norm of this quaternion.
Definition: Quaternion-Impl.h:390
Quaternion RMul(const Quaternion &other) const
Returns other quaternion * this quaternion.
Definition: Quaternion-Impl.h:248
static Quaternion MakeIdentity()
Returns identity matrix.
Definition: Quaternion-Impl.h:434
Quaternion< T > Slerp(const Quaternion< T > &a, const Quaternion< T > &b, T t)
Computes spherical linear interpolation.
Definition: Quaternion-Impl.h:440
T w
Real part.
Definition: Quaternion.h:23
Matrix4x4< T > Matrix4() const
Converts to the 4x4 rotation matrix.
Definition: Quaternion-Impl.h:368
Quaternion Normalized() const
Returns normalized quaternion.
Definition: Quaternion-Impl.h:205
Matrix3x3< T > Matrix3() const
Converts to the 3x3 rotation matrix.
Definition: Quaternion-Impl.h:347
T Angle() const
Returns the rotational angle.
Definition: Quaternion-Impl.h:311
void Rotate(T angleInRadians)
Rotate this quaternion with given angle in radians.
Definition: Quaternion-Impl.h:270
Quaternion()
Make an identity quaternion.
Definition: Quaternion-Impl.h:15
bool operator==(const Quaternion &other) const
Returns true if equal.
Definition: Quaternion-Impl.h:422
Quaternion Inverse() const
Returns the inverse quaternion.
Definition: Quaternion-Impl.h:340
Definition: pybind11Utils.h:24
3-D matrix class.
Definition: Matrix3x3.h:30
4-D matrix class.
Definition: Matrix4x4.h:29
void Normalize()
Normalizes the quaternion.
Definition: Quaternion-Impl.h:283
Quaternion & operator*=(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.h:403
Quaternion< U > CastTo() const
Returns quaternion with other base type.
Definition: Quaternion-Impl.h:199
T y
Imaginary part (k).
Definition: Quaternion.h:32
T & operator[](size_t i)
Returns the reference to the i-th element.
Definition: Quaternion-Impl.h:410
Vector3< T > Mul(const Vector3< T > &v) const
Returns this quaternion * vector.
Definition: Quaternion-Impl.h:213
void IMul(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.h:258
Vector3< T > Axis() const
Returns the rotational axis.
Definition: Quaternion-Impl.h:297
void Set(const Quaternion &other)
Sets the quaternion with other quaternion.
Definition: Quaternion-Impl.h:63
bool operator!=(const Quaternion &other) const
Returns true if not equal.
Definition: Quaternion-Impl.h:428
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.h:481
T Dot(const Quaternion< T > &other)
Computes the dot product with other quaternion.
Definition: Quaternion-Impl.h:242
void GetAxisAngle(Vector3< T > *axis, T *angle) const
Returns the axis and angle.
Definition: Quaternion-Impl.h:325
Quaternion & operator=(const Quaternion &other)
Assigns other quaternion.
Definition: Quaternion-Impl.h:396
void SetIdentity()
Makes this quaternion identity.
Definition: Quaternion-Impl.h:264
Quaternion class defined as q = w + xi + yj + zk.
Definition: Quaternion.h:20
T x
Imaginary part (j).
Definition: Quaternion.h:29