Quaternion.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_QUATERNION_HPP
12 #define CUBBYFLOW_QUATERNION_HPP
13 
14 #include <Core/Matrix/Matrix.hpp>
15 
16 namespace CubbyFlow
17 {
21 template <typename T>
23 {
24  public:
25  static_assert(
26  std::is_floating_point<T>::value,
27  "Quaternion only can be instantiated with floating point types");
28 
30  Quaternion();
31 
33  Quaternion(T newW, T newX, T newY, T newZ);
34 
36  Quaternion(const std::initializer_list<T>& list);
37 
39  Quaternion(const Vector3<T>& axis, T angle);
40 
42  Quaternion(const Vector3<T>& from, const Vector3<T>& to);
43 
45  Quaternion(const Vector3<T>& axis0, const Vector3<T>& axis1,
46  const Vector3<T>& axis2);
47 
49  explicit Quaternion(const Matrix3x3<T>& m33);
50 
52  Quaternion(const Quaternion& other);
53 
55  Quaternion(Quaternion&& other) noexcept;
56 
58  ~Quaternion() = default;
59 
61  Quaternion& operator=(const Quaternion& other);
62 
64  Quaternion& operator=(Quaternion&& other) noexcept;
65 
67  void Set(const Quaternion& other);
68 
70  void Set(T newW, T newX, T newY, T newZ);
71 
73  void Set(const std::initializer_list<T>& list);
74 
76  void Set(const Vector3<T>& axis, T angle);
77 
79  void Set(const Vector3<T>& from, const Vector3<T>& to);
80 
82  void Set(const Vector3<T>& rotationBasis0, const Vector3<T>& rotationBasis1,
83  const Vector3<T>& rotationBasis2);
84 
86  void Set(const Matrix3x3<T>& m);
87 
89  template <typename U>
90  [[nodiscard]] Quaternion<U> CastTo() const;
91 
93  [[nodiscard]] Quaternion Normalized() const;
94 
96  [[nodiscard]] Vector3<T> Mul(const Vector3<T>& v) const;
97 
99  [[nodiscard]] Quaternion Mul(const Quaternion& other) const;
100 
102  [[nodiscard]] T Dot(const Quaternion<T>& other) const;
103 
105  [[nodiscard]] Quaternion RMul(const Quaternion& other) const;
106 
108  void IMul(const Quaternion& other);
109 
111  void SetIdentity();
112 
114  void Rotate(T angleInRadians);
115 
117  void Normalize();
118 
120  [[nodiscard]] Vector3<T> Axis() const;
121 
123  [[nodiscard]] T Angle() const;
124 
126  void GetAxisAngle(Vector3<T>* axis, T* angle) const;
127 
129  [[nodiscard]] Quaternion Inverse() const;
130 
132  [[nodiscard]] Matrix3x3<T> Matrix3() const;
133 
135  [[nodiscard]] Matrix4x4<T> Matrix4() const;
136 
138  [[nodiscard]] T L2Norm() const;
139 
141  Quaternion& operator*=(const Quaternion& other);
142 
144  T& operator[](size_t i);
145 
147  const T& operator[](size_t i) const;
148 
150  bool operator==(const Quaternion& other) const;
151 
153  bool operator!=(const Quaternion& other) const;
154 
156  [[nodiscard]] static Quaternion MakeIdentity();
157 
159  T w;
160 
162  T x;
163 
165  T y;
166 
168  T z;
169 };
170 
172 template <typename T>
173 [[nodiscard]] Quaternion<T> Slerp(const Quaternion<T>& a,
174  const Quaternion<T>& b, T t);
175 
177 template <typename T>
178 [[nodiscard]] Vector<T, 3> operator*(const Quaternion<T>& q,
179  const Vector<T, 3>& v);
180 
182 template <typename T>
183 [[nodiscard]] Quaternion<T> operator*(const Quaternion<T>& a,
184  const Quaternion<T>& b);
185 
188 
191 } // namespace CubbyFlow
192 
194 
195 #endif
T z
Definition: Quaternion.hpp:168
~Quaternion()=default
Default destructor.
T L2Norm() const
Returns L2 norm of this quaternion.
Definition: Quaternion-Impl.hpp:426
Quaternion RMul(const Quaternion &other) const
Returns other quaternion * this quaternion.
Definition: Quaternion-Impl.hpp:275
static Quaternion MakeIdentity()
Returns identity matrix.
Definition: Quaternion-Impl.hpp:497
Quaternion< T > Slerp(const Quaternion< T > &a, const Quaternion< T > &b, T t)
Computes spherical linear interpolation.
Definition: Quaternion-Impl.hpp:503
T w
Real part.
Definition: Quaternion.hpp:159
Matrix4x4< T > Matrix4() const
Converts to the 4x4 rotation matrix.
Definition: Quaternion-Impl.hpp:393
Quaternion Normalized() const
Returns normalized quaternion.
Definition: Quaternion-Impl.hpp:232
Matrix3x3< T > Matrix3() const
Converts to the 3x3 rotation matrix.
Definition: Quaternion-Impl.hpp:373
T Angle() const
Returns the rotational angle.
Definition: Quaternion-Impl.hpp:337
void Rotate(T angleInRadians)
Rotate this quaternion with given angle in radians.
Definition: Quaternion-Impl.hpp:296
Quaternion()
Make an identity quaternion.
Definition: Quaternion-Impl.hpp:17
Definition: Matrix.hpp:27
bool operator==(const Quaternion &other) const
Returns true if equal.
Definition: Quaternion-Impl.hpp:485
Quaternion Inverse() const
Returns the inverse quaternion.
Definition: Quaternion-Impl.hpp:366
Definition: pybind11Utils.hpp:20
void Normalize()
Normalizes the quaternion.
Definition: Quaternion-Impl.hpp:309
Quaternion & operator*=(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.hpp:432
Quaternion< U > CastTo() const
Returns quaternion with other base type.
Definition: Quaternion-Impl.hpp:225
T y
Imaginary part (k).
Definition: Quaternion.hpp:165
T & operator[](size_t i)
Returns the reference to the i-th element.
Definition: Quaternion-Impl.hpp:439
Vector3< T > Mul(const Vector3< T > &v) const
Returns this quaternion * vector.
Definition: Quaternion-Impl.hpp:240
void IMul(const Quaternion &other)
Returns this quaternion *= other quaternion.
Definition: Quaternion-Impl.hpp:284
T Dot(const Quaternion< T > &other) const
Computes the dot product with other quaternion.
Definition: Quaternion-Impl.hpp:269
Vector3< T > Axis() const
Returns the rotational axis.
Definition: Quaternion-Impl.hpp:323
void Set(const Quaternion &other)
Sets the quaternion with other quaternion.
Definition: Quaternion-Impl.hpp:86
bool operator!=(const Quaternion &other) const
Returns true if not equal.
Definition: Quaternion-Impl.hpp:491
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.hpp:543
void GetAxisAngle(Vector3< T > *axis, T *angle) const
Returns the axis and angle.
Definition: Quaternion-Impl.hpp:351
Quaternion & operator=(const Quaternion &other)
Copy assignment operator.
Definition: Quaternion-Impl.hpp:72
void SetIdentity()
Makes this quaternion identity.
Definition: Quaternion-Impl.hpp:290
Quaternion class defined as q = w + xi + yj + zk.
Definition: Quaternion.hpp:22
T x
Imaginary part (j).
Definition: Quaternion.hpp:162