liballofw
quaternion.h
Go to the documentation of this file.
1 #ifndef IV_MATH_QUATERNION_H
2 #define IV_MATH_QUATERNION_H
3 
4 #include "matrix.h"
5 
6 namespace allofw {
7 
8  template<typename T>
9  struct Quaternion_ {
10  union {
12  struct {
13  T x, y, z;
14  };
15  };
16  T w;
17 
18  inline Quaternion_() = default;
19 
20  inline Quaternion_(T x_, T y_, T z_, T w_)
21  : v(x_, y_, z_), w(w_) { }
22 
23  template<typename T1>
24  inline Quaternion_(const Vector3_<T1>& v_, T w_)
25  : v(v_), w(w_) { }
26 
27  template<typename OtherT>
28  inline Quaternion_(const OtherT& other)
29  : v(other.x, other.y, other.z), w(other.w) { }
30 
31  static inline Quaternion_ Rotation(const Vector3_<T>& v, double alpha) {
32  return Quaternion_(v.unit() * std::sin(alpha / 2), std::cos(alpha / 2));
33  }
34 
35  static inline Quaternion_ Zero() { return Quaternion_(0, 0, 0, 0); }
36  static inline Quaternion_ One() { return Quaternion_(0, 0, 0, 1); }
37 
38  inline T len() const{
39  return std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z + w * w);
40  }
41 
42  inline Quaternion_ operator + (const Quaternion_& q) const {
43  return Quaternion_(v + q.v, w + q.w);
44  }
45  inline Quaternion_ operator - (const Quaternion_& q) const {
46  return Quaternion_(v - q.v, w - q.w);
47  }
48  inline Quaternion_ operator - () const {
49  return Quaternion_(-v, -w);
50  }
51  inline Quaternion_ operator * (T s) const {
52  return Quaternion_(v * s, w * s);
53  }
54  inline Quaternion_ operator / (T s) const {
55  return Quaternion_(v / s, w / s);
56  }
57  inline Quaternion_ unit() const {
58  T l = len();
59  return *this / l;
60  }
61  inline Quaternion_ operator * (const Quaternion_& q) const {
62  return Quaternion_(v.cross(q.v) + q.v * w + v * q.w, w * q.w - v.dot(q.v));
63  }
64  template<typename T1>
65  inline Quaternion_ operator * (const Vector3_<T1>& v) const {
66  return *this * Quaternion_(v, 0);
67  }
68  inline Quaternion_ inversion() const {
69  return Quaternion_(-v, w);
70  }
71  template<typename T1>
72  inline Vector3_<T1> rotate(const Vector3_<T1>& v) const {
73  return ((*this) * v * inversion()).v;
74  }
75 
76  inline Matrix3_<T> matrix() const {
77  Vector3_<T> e1 = rotate(Vector3_<T>(1, 0, 0));
78  Vector3_<T> e2 = rotate(Vector3_<T>(0, 1, 0));
79  Vector3_<T> e3 = rotate(Vector3_<T>(0, 0, 1));
80  return Matrix3_<T>::fromColumns(e1, e2, e3);
81  }
82  };
83 
84  template<typename T1, typename T2>
85  inline Vector3_<T1> operator * (const Vector3_<T1>& v, const Matrix3_<T2>& m) {
86  return Vector3_<T1>(m.a11 * v.x + m.a21 * v.y + m.a31 * v.z,
87  m.a12 * v.x + m.a22 * v.y + m.a32 * v.z,
88  m.a13 * v.x + m.a23 * v.y + m.a33 * v.z);
89  }
90 
91  template<typename T1, typename T2>
93  return Quaternion_<T2>(v, 0) * q;
94  }
95 
98  typedef Quaterniond Quaternion;
99 
100 }
101 
102 #endif
Quaternion_(T x_, T y_, T z_, T w_)
Definition: quaternion.h:20
number_t a12
Definition: matrix.h:36
number_t a13
Definition: matrix.h:37
T len() const
Definition: quaternion.h:38
Quaternion_< float > Quaternionf
Definition: quaternion.h:96
T y
Definition: quaternion.h:13
Vector3_< T > v
Definition: quaternion.h:11
number_t a33
Definition: matrix.h:37
number_t x
Definition: vector.h:110
number_t a21
Definition: matrix.h:35
Quaterniond Quaternion
Definition: quaternion.h:98
T x
Definition: quaternion.h:13
number_t a11
Definition: matrix.h:35
Definition: allofw.h:12
Quaternion_ inversion() const
Definition: quaternion.h:68
Matrix3_< T > matrix() const
Definition: quaternion.h:76
Quaternion_(const Vector3_< T1 > &v_, T w_)
Definition: quaternion.h:24
static Quaternion_ One()
Definition: quaternion.h:36
Quaternion_ unit() const
Definition: quaternion.h:57
Vector3_ unit() const
Definition: vector.h:91
T w
Definition: quaternion.h:16
Quaternion_ operator*(T s) const
Definition: quaternion.h:51
Definition: matrix.h:28
Quaternion_ operator/(T s) const
Definition: quaternion.h:54
number_t a23
Definition: matrix.h:37
static Matrix3_ fromColumns(const vector_t &e1, const vector_t &e2, const vector_t &e3)
Definition: matrix.h:79
number_t y
Definition: vector.h:110
static Quaternion_ Zero()
Definition: quaternion.h:35
Quaternion_< double > Quaterniond
Definition: quaternion.h:97
Definition: quaternion.h:9
Definition: vector.h:63
Quaternion_ operator-() const
Definition: quaternion.h:48
number_t z
Definition: vector.h:110
T z
Definition: quaternion.h:13
Quaternion_ operator+(const Quaternion_ &q) const
Definition: quaternion.h:42
number_t a22
Definition: matrix.h:36
static Quaternion_ Rotation(const Vector3_< T > &v, double alpha)
Definition: quaternion.h:31
Vector3_< T1 > rotate(const Vector3_< T1 > &v) const
Definition: quaternion.h:72
Quaternion_(const OtherT &other)
Definition: quaternion.h:28
number_t a31
Definition: matrix.h:35
number_t a32
Definition: matrix.h:36