Jet  v1.3.3
quaternion.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_QUATERNION_H_
8 #define INCLUDE_JET_QUATERNION_H_
9 
10 #include <jet/matrix4x4.h>
11 
12 namespace jet {
13 
17 template <typename T>
18 class Quaternion {
19  public:
20  static_assert(
21  std::is_floating_point<T>::value,
22  "Quaternion only can be instantiated with floating point types");
23 
25  T w;
26 
28  T x;
29 
31  T y;
32 
34  T z;
35 
36  // MARK: Constructors
37 
40 
42  Quaternion(T newW, T newX, T newY, T newZ);
43 
45  Quaternion(const std::initializer_list<T>& lst);
46 
49 
51  Quaternion(const Vector3<T>& from, const Vector3<T>& to);
52 
55  const Vector3<T>& axis0,
56  const Vector3<T>& axis1,
57  const Vector3<T>& axis2);
58 
60  explicit Quaternion(const Matrix3x3<T>& m33);
61 
63  Quaternion(const Quaternion& other);
64 
65 
66  // MARK: Basic setters
67 
69  void set(const Quaternion& other);
70 
72  void set(T newW, T newX, T newY, T newZ);
73 
75  void set(const std::initializer_list<T>& lst);
76 
78  void set(const Vector3<T>& axis, T angle);
79 
81  void set(const Vector3<T>& from, const Vector3<T>& to);
82 
84  void set(
85  const Vector3<T>& rotationBasis0,
86  const Vector3<T>& rotationBasis1,
87  const Vector3<T>& rotationBasis2);
88 
90  void set(const Matrix3x3<T>& matrix);
91 
92 
93  // MARK: Basic getters
94 
96  template <typename U>
98 
101 
102 
103  // MARK: Binary operator methods - new instance = this instance (+) input
104 
106  Vector3<T> mul(const Vector3<T>& v) const;
107 
109  Quaternion mul(const Quaternion& other) const;
110 
112  T dot(const Quaternion<T>& other);
113 
114 
115  // MARK: Binary operator methods - new instance = input (+) this instance
116 
118  Quaternion rmul(const Quaternion& other) const;
119 
120  // MARK: Augmented operator methods - this instance (+)= input
121 
123  void imul(const Quaternion& other);
124 
125 
126  // MARK: Modifiers
127 
129  void setIdentity();
130 
132  void rotate(T angleInRadians);
133 
135  void normalize();
136 
137 
138  // MARK: Complex getters
139 
141  Vector3<T> axis() const;
142 
144  T angle() const;
145 
147  void getAxisAngle(Vector3<T>* axis, T* angle) const;
148 
151 
154 
157 
159  T l2Norm() const;
160 
161 
162  // MARK: Setter operators
163 
166 
169 
170 
171  // MARK: Getter operators
172 
174  T& operator[](size_t i);
175 
177  const T& operator[](size_t i) const;
178 
180  bool operator==(const Quaternion& other) const;
181 
183  bool operator!=(const Quaternion& other) const;
184 
185 
186  // MARK: Builders
187 
190 };
191 
193 template <typename T>
195  const Quaternion<T>& a,
196  const Quaternion<T>& b,
197  T t);
198 
200 template <typename T>
202 
204 template <typename T>
206 
209 
212 
213 } // namespace jet
214 
215 #include "detail/quaternion-inl.h"
216 
217 #endif // INCLUDE_JET_QUATERNION_H_
jet::Quaternion::w
T w
Real part.
Definition: quaternion.h:22
jet::Quaternion::set
void set(const Vector3< T > &from, const Vector3< T > &to)
Sets the quaternion with from and to vectors.
jet::Quaternion::rmul
Quaternion rmul(const Quaternion &other) const
Returns other quaternion * this quaternion.
jet::Quaternion::castTo
Quaternion< U > castTo() const
Returns quaternion with other base type.
jet::Quaternion::Quaternion
Quaternion(const Vector3< T > &from, const Vector3< T > &to)
Constructs a quaternion with from and to vectors.
jet::Quaternion::normalize
void normalize()
Normalizes the quaternion.
jet::Quaternion::operator=
Quaternion & operator=(const Quaternion &other)
Assigns other quaternion.
jet::QuaternionD
Quaternion< double > QuaternionD
Double-type quaternion.
Definition: quaternion.h:211
jet::Quaternion::l2Norm
T l2Norm() const
Returns L2 norm of this quaternion.
jet::slerp
Quaternion< T > slerp(const Quaternion< T > &a, const Quaternion< T > &b, T t)
Computes spherical linear interpolation.
jet::Quaternion::Quaternion
Quaternion(const std::initializer_list< T > &lst)
Constructs a quaternion with given elements.
jet::Quaternion::mul
Quaternion mul(const Quaternion &other) const
Returns this quaternion * other quaternion.
jet::Quaternion::operator[]
T & operator[](size_t i)
Returns the reference to the i-th element.
jet::Quaternion::operator!=
bool operator!=(const Quaternion &other) const
Returns true if not equal.
jet::Quaternion::matrix4
Matrix4x4< T > matrix4() const
Converts to the 4x4 rotation matrix.
jet::Quaternion::set
void set(const std::initializer_list< T > &lst)
Sets the quaternion with given elements.
jet::Quaternion::Quaternion
Quaternion(T newW, T newX, T newY, T newZ)
Constructs a quaternion with given elements.
jet::Matrix< T, 3, 3 >
3-D matrix class.
Definition: matrix3x3.h:28
jet::Quaternion::set
void set(const Vector3< T > &axis, T angle)
Sets the quaternion with given rotation axis and angle.
jet::Quaternion::y
T y
Imaginary part (k).
Definition: quaternion.h:31
jet::QuaternionF
Quaternion< float > QuaternionF
Float-type quaternion.
Definition: quaternion.h:208
jet::Quaternion::set
void set(const Matrix3x3< T > &matrix)
Sets the quaternion with 3x3 rotational matrix.
jet::Quaternion
Quaternion class defined as q = w + xi + yj + zk.
Definition: quaternion.h:18
jet
Definition: advection_solver2.h:18
jet::Quaternion::normalized
Quaternion normalized() const
Returns normalized quaternion.
matrix4x4.h
jet::Quaternion::set
void set(const Vector3< T > &rotationBasis0, const Vector3< T > &rotationBasis1, const Vector3< T > &rotationBasis2)
Sets quaternion with three basis vectors.
jet::Quaternion::x
T x
Imaginary part (j).
Definition: quaternion.h:28
jet::Quaternion::operator*=
Quaternion & operator*=(const Quaternion &other)
Returns this quaternion *= other quaternion.
jet::Quaternion::Quaternion
Quaternion()
Make an identity quaternion.
jet::Quaternion::dot
T dot(const Quaternion< T > &other)
Computes the dot product with other quaternion.
jet::Matrix< T, 4, 4 >
4-D matrix class.
Definition: matrix4x4.h:27
jet::Quaternion::getAxisAngle
void getAxisAngle(Vector3< T > *axis, T *angle) const
Returns the axis and angle.
jet::Quaternion::set
void set(T newW, T newX, T newY, T newZ)
Sets the quaternion with given elements.
jet::Quaternion::angle
T angle() const
Returns the rotational angle.
jet::Quaternion::Quaternion
Quaternion(const Vector3< T > &axis, T angle)
Constructs a quaternion with given rotation axis and angle.
jet::Quaternion::Quaternion
Quaternion(const Vector3< T > &axis0, const Vector3< T > &axis1, const Vector3< T > &axis2)
Constructs a quaternion with three basis vectors.
jet::Quaternion::inverse
Quaternion inverse() const
Returns the inverse quaternion.
jet::Quaternion::mul
Vector3< T > mul(const Vector3< T > &v) const
Returns this quaternion * vector.
jet::Vector< T, 3 >
3-D vector class.
Definition: vector3.h:25
jet::Quaternion::imul
void imul(const Quaternion &other)
Returns this quaternion *= other quaternion.
jet::Quaternion::Quaternion
Quaternion(const Quaternion &other)
Copy constructor.
jet::Quaternion::setIdentity
void setIdentity()
Makes this quaternion identity.
jet::Quaternion::operator==
bool operator==(const Quaternion &other) const
Returns true if equal.
jet::Quaternion::matrix3
Matrix3x3< T > matrix3() const
Converts to the 3x3 rotation matrix.
jet::Quaternion::operator[]
const T & operator[](size_t i) const
Returns the const reference to the i-th element.
jet::operator*
Matrix2x2< T > operator*(const Matrix2x2< T > &a, T b)
Returns a * b', where every element of matrix b' is b.
jet::Quaternion::set
void set(const Quaternion &other)
Sets the quaternion with other quaternion.
jet::Quaternion::Quaternion
Quaternion(const Matrix3x3< T > &m33)
Constructs a quaternion with 3x3 rotational matrix.
jet::Quaternion::makeIdentity
static Quaternion makeIdentity()
Returns identity matrix.
jet::Quaternion::rotate
void rotate(T angleInRadians)
Rotate this quaternion with given angle in radians.
jet::Quaternion::z
T z
Definition: quaternion.h:34
jet::Quaternion::axis
Vector3< T > axis() const
Returns the rotational axis.