Lugdunum  0.1.0
Quaternion.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <lug/Math/Export.hpp>
4 #include <lug/Math/Matrix.hpp>
5 #include <lug/Math/Vector.hpp>
6 #include <lug/Math/Constant.hpp>
7 
8 namespace lug {
9 namespace Math {
10 
11 template <typename T = double>
12 class Quaternion {
13 public:
14  Quaternion() = default;
15  Quaternion(T w, T x, T y, T z);
16  Quaternion(T data[4]);
17  Quaternion(T angle, const Vector<3, T>& axis);
18 
19  Quaternion(const Quaternion<T>&) = default;
20  Quaternion(Quaternion<T>&&) = default;
21 
22  Quaternion<T>& operator=(const Quaternion<T>&) = default;
24 
25  ~Quaternion() = default;
26 
27  T& operator[](std::size_t idx);
28  const T& operator[](std::size_t idx) const;
29 
30  void conjugate();
31  void inverse();
32 
33  void normalize();
34  constexpr T length() const;
35  constexpr T squaredLength() const;
36 
37  T getAngle() const;
38  Vector<3, T> getAxis() const;
39 
40  Mat4x4<T> transform() const;
41 
42 #define DEFINE_QUATERNION_ACCESS(name, rows) \
43  const T& name() const { \
44  return (*this)[rows]; \
45  } \
46  \
47  T& name() { \
48  return (*this)[rows]; \
49  }
50 
53 
56 
59 
62 
63 #undef DEFINE_QUATERNION_ACCESS
64 
65  static Quaternion<T> identity();
66 
67  static Quaternion<T> fromAxes(const Vector<3, T>& xAxis, const Vector<3, T>& yAxis, const Vector<3, T>& zAxis);
68  static Quaternion<T> fromRotationMatrix(const Matrix<4, 4, T>& rotMatrix);
69 
70 private:
71  T _data[4];
72 };
73 
75 using Quatf = Quaternion<float>;
76 
78 using Quatd = Quaternion<double>;
79 
80 template <typename T>
82 
83 template <typename T>
85 
86 template <typename T>
88 
89 template <typename T>
90 T dot(const Quaternion<T>& lhs, const Quaternion<T>& rhs);
91 
92 template <typename T>
93 Quaternion<T> directionTo(const Vector<3, T>& original, const Vector<3, T>& expected);
94 
95 // TODO: Reflection / rotation
96 
97 // Quaternion operator
98 
99 template <typename T>
101 
102 // Quaternion/Quaternion operator
103 template <typename T>
104 Quaternion<T> operator+(const Quaternion<T>& lhs, const Quaternion<T>& rhs);
105 
106 template <typename T>
107 Quaternion<T> operator-(const Quaternion<T>& lhs, const Quaternion<T>& rhs);
108 
109 template <typename T>
110 Quaternion<T> operator*(const Quaternion<T>& lhs, const Quaternion<T>& rhs);
111 
112 template <typename T>
113 Quaternion<T> operator/(const Quaternion<T>& lhs, const Quaternion<T>& rhs);
114 
115 template <typename T>
116 bool operator==(const Quaternion<T>& lhs, const Quaternion<T>& rhs);
117 
118 template <typename T>
119 bool operator!=(const Quaternion<T>& lhs, const Quaternion<T>& rhs);
120 
121 template <typename T>
122 std::ostream& operator<<(std::ostream& os, const Quaternion<T>& quaternion);
123 
124 #include <lug/Math/Quaternion.inl>
125 
126 } // Math
127 } // lug
bool operator!=(const Matrix< Rows, Columns, T > &lhs, const Matrix< Rows, Columns, T > &rhs)
bool operator==(const Matrix< Rows, Columns, T > &lhs, const Matrix< Rows, Columns, T > &rhs)
T & operator[](std::size_t idx)
Definition: Quaternion.inl:28
static Quaternion< T > fromAxes(const Vector< 3, T > &xAxis, const Vector< 3, T > &yAxis, const Vector< 3, T > &zAxis)
Definition: Quaternion.inl:114
Matrix< Rows, Columns, T > operator-(const Matrix< Rows, Columns, T > &lhs)
Quaternion< T > & operator=(const Quaternion< T > &)=default
static Quaternion< T > identity()
Definition: Quaternion.inl:109
Vector< 3, T > getAxis() const
Definition: Quaternion.inl:68
Mat4x4< T > transform() const
Definition: Quaternion.inl:78
Quaternion< T > directionTo(const Vector< 3, T > &original, const Vector< 3, T > &expected)
#define DEFINE_QUATERNION_ACCESS(name, rows)
Definition: Quaternion.hpp:42
constexpr T length() const
Definition: Quaternion.inl:53
#define LUG_MATH_API
Definition: Export.hpp:11
Matrix< Rows, Columns, T > operator+(const Matrix< Rows, Columns, T > &lhs, T rhs)
Matrix< Rows, Columns, T > operator/(const Matrix< Rows, Columns, T > &lhs, T rhs)
Matrix< Rows, Columns, T > operator*(const Matrix< Rows, Columns, T > &lhs, T rhs)
T dot(const Quaternion< T > &lhs, const Quaternion< T > &rhs)
constexpr T squaredLength() const
Definition: Quaternion.inl:58
Quaternion< float > Quatf
Definition: Quaternion.hpp:75
static Quaternion< T > fromRotationMatrix(const Matrix< 4, 4, T > &rotMatrix)
Definition: Quaternion.inl:133