Pioneer
Quaternion.h
Go to the documentation of this file.
1 // Copyright © 2008-2022 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _QUATERNION_H
5 #define _QUATERNION_H
6 
7 #include "matrix4x4.h"
8 #include "vector3.h"
9 #include <math.h>
10 #include <type_traits>
11 
12 template <typename T>
13 class Quaternion {
14  using other_float_t = typename std::conditional<std::is_same<T, float>::value, double, float>::type;
15 
16 public:
17  T w, x, y, z;
18 
19  // Constructor definitions are outside class declaration to enforce that
20  // only float and double versions are possible.
22  Quaternion(T w, T x, T y, T z);
23  // from angle and axis
24  Quaternion(T ang, vector3<T> axis)
25  {
26  const T halfAng = ang * T(0.5);
27  const T sinHalfAng = sin(halfAng);
28  w = cos(halfAng);
29  x = axis.x * sinHalfAng;
30  y = axis.y * sinHalfAng;
31  z = axis.z * sinHalfAng;
32  }
33  // from axis, assume angle == PI
34  // optimized fast path using sin(PI/2) = 1
36  {
37  w = 0;
38  x = axis.x;
39  y = axis.y;
40  z = axis.z;
41  }
43  w(o.w),
44  x(o.x),
45  y(o.y),
46  z(o.z) {}
47 
48  void GetAxisAngle(T &angle, vector3<T> &axis)
49  {
50  if (w > 1.0) *this = Normalized(); // if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
51  angle = 2.0 * acos(w);
52  double s = sqrt(1.0 - w * w); // assuming quaternion normalised then w is less than 1, so term always positive.
53  if (s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
54  // if s close to zero then direction of axis not important
55  axis.x = x; // if it is important that axis is normalised then replace with x=1; y=z=0;
56  axis.y = y;
57  axis.z = z;
58  } else {
59  axis.x = x / s; // normalise axis
60  axis.y = y / s;
61  axis.z = z / s;
62  }
63  }
64  bool operator==(const Quaternion &a) const
65  {
66  return is_equal_exact(a.w, w) && is_equal_exact(a.x, x) && is_equal_exact(a.y, y) && is_equal_exact(a.z, z);
67  }
68  bool ExactlyEqual(const Quaternion &a) const
69  {
70  return is_equal_exact(a.w, w) && is_equal_exact(a.x, x) && is_equal_exact(a.y, y) && is_equal_exact(a.z, z);
71  }
72 
73  // conjugate (inverse)
74  friend Quaternion operator~(const Quaternion &a)
75  {
76  Quaternion r;
77  r.w = a.w;
78  r.x = -a.x;
79  r.y = -a.y;
80  r.z = -a.z;
81  return r;
82  }
83  friend Quaternion operator*(const Quaternion &a, const Quaternion &b)
84  {
85  Quaternion r;
86  r.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
87  r.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y;
88  r.y = a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x;
89  r.z = a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w;
90  return r;
91  }
92  friend Quaternion operator*(const T s, const Quaternion &a) { return a * s; }
93  friend Quaternion operator*(const Quaternion &a, const T s)
94  {
95  Quaternion r;
96  r.w = a.w * s;
97  r.x = a.x * s;
98  r.y = a.y * s;
99  r.z = a.z * s;
100  return r;
101  }
102  friend Quaternion operator+(const Quaternion &a, const Quaternion &b)
103  {
104  Quaternion r;
105  r.w = a.w + b.w;
106  r.x = a.x + b.x;
107  r.y = a.y + b.y;
108  r.z = a.z + b.z;
109  return r;
110  }
111  friend Quaternion operator-(const Quaternion &a, const Quaternion &b)
112  {
113  Quaternion r;
114  r.w = a.w - b.w;
115  r.x = a.x - b.x;
116  r.y = a.y - b.y;
117  r.z = a.z - b.z;
118  return r;
119  }
120 
122  {
123  T l = 1.0 / sqrt(w * w + x * x + y * y + z * z);
124  return Quaternion(w * l, x * l, y * l, z * l);
125  }
126  static T Dot(const Quaternion &a, const Quaternion &b) { return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z; }
127 
128  template <typename U>
130  {
131  Quaternion r;
132  if (m[0] + m[4] + m[8] > 0.0f) {
133  U t = m[0] + m[4] + m[8] + 1.0;
134  U s = 0.5 / sqrt(t);
135  r.w = s * t;
136  r.z = (m[3] - m[1]) * s;
137  r.y = (m[2] - m[6]) * s;
138  r.x = (m[7] - m[5]) * s;
139  } else if ((m[0] > m[4]) && (m[0] > m[8])) {
140  U t = m[0] - m[4] - m[8] + 1.0;
141  U s = 0.5 / sqrt(t);
142  r.x = s * t;
143  r.y = (m[1] + m[3]) * s;
144  r.z = (m[2] + m[6]) * s;
145  r.w = (m[7] - m[5]) * s;
146  } else if (m[4] > m[8]) {
147  U t = -m[0] + m[4] - m[8] + 1.0;
148  U s = 0.5 / sqrt(t);
149  r.w = (m[2] - m[6]) * s;
150  r.x = (m[1] + m[3]) * s;
151  r.y = s * t;
152  r.z = (m[5] + m[7]) * s;
153  } else {
154  U t = -m[0] - m[4] + m[8] + 1.0;
155  U s = 0.5 / sqrt(t);
156  r.w = (m[3] - m[1]) * s;
157  r.x = (m[2] + m[6]) * s;
158  r.y = (m[7] + m[5]) * s;
159  r.z = s * t;
160  }
161  return r;
162  }
163 
164  template <typename U>
166  {
167  matrix3x3<U> m;
168  U xx = x * x;
169  U xy = x * y;
170  U xz = x * z;
171  U xw = x * w;
172 
173  U yy = y * y;
174  U yz = y * z;
175  U yw = y * w;
176 
177  U zz = z * z;
178  U zw = z * w;
179 
180  m[0] = 1.0 - 2.0 * (yy + zz);
181  m[1] = 2.0 * (xy - zw);
182  m[2] = 2.0 * (xz + yw);
183 
184  m[3] = 2.0 * (xy + zw);
185  m[4] = 1.0 - 2.0 * (xx + zz);
186  m[5] = 2.0 * (yz - xw);
187 
188  m[6] = 2.0 * (xz - yw);
189  m[7] = 2.0 * (yz + xw);
190  m[8] = 1.0 - 2.0 * (xx + yy);
191  return m;
192  }
193  /* normalized linear interpolation between 2 quaternions */
194  static Quaternion Nlerp(const Quaternion &a, const Quaternion &b, T t)
195  {
196  //printf("a: %f,%f,%f,%f\n", a.x, a.y, a.z, a.w);
197  //printf("b: %f,%f,%f,%f\n", b.x, b.y, b.z, b.w);
198  return (a + t * (b - a)).Normalized();
199  }
200 
201  // spherical linear interpolation between two quaternions
202  // taken from assimp via #2514
203  static Quaternion Slerp(const Quaternion &a, const Quaternion &b, T t)
204  {
205  // calc cosine theta
206  T cosom = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
207 
208  // adjust signs (if necessary)
209  Quaternion end = b;
210  if (cosom < T(0.0)) {
211  cosom = -cosom;
212  end.x = -end.x; // Reverse all signs
213  end.y = -end.y;
214  end.z = -end.z;
215  end.w = -end.w;
216  }
217 
218  // Calculate coefficients
219  T sclp, sclq;
220  if ((T(1.0) - cosom) > T(0.0001)) { // 0.0001 -> some epsillon
221  // Standard case (slerp)
222  T omega, sinom;
223  omega = acos(cosom); // extract theta from dot product's cos theta
224  sinom = sin(omega);
225  sclp = sin((T(1.0) - t) * omega) / sinom;
226  sclq = sin(t * omega) / sinom;
227  } else {
228  // Very close, do linear interp (because it's faster)
229  sclp = T(1.0) - t;
230  sclq = t;
231  }
232 
233  return Quaternion(
234  sclp * a.w + sclq * end.w,
235  sclp * a.x + sclq * end.x,
236  sclp * a.y + sclq * end.y,
237  sclp * a.z + sclq * end.z);
238  }
239 
240  //void Print() const {
241  // printf("%f,%f,%f,%f\n", w, x, y, z);
242  //}
243 };
244 
245 template <>
247  w(1.f),
248  x(0.f),
249  y(0.f),
250  z(0.f) {}
251 template <>
253  w(1.),
254  x(0.),
255  y(0.),
256  z(0.) {}
257 template <>
258 inline Quaternion<float>::Quaternion(float w_, float x_, float y_, float z_) :
259  w(w_),
260  x(x_),
261  y(y_),
262  z(z_) {}
263 template <>
264 inline Quaternion<double>::Quaternion(double w_, double x_, double y_, double z_) :
265  w(w_),
266  x(x_),
267  y(y_),
268  z(z_) {}
269 
272 
273 #endif /* _QUATERNION_H */
bool is_equal_exact(float a, float b)
Definition: FloatComparison.h:112
Quaternion< double > Quaterniond
Definition: Quaternion.h:271
Quaternion< float > Quaternionf
Definition: Quaternion.h:270
Definition: Quaternion.h:13
friend Quaternion operator-(const Quaternion &a, const Quaternion &b)
Definition: Quaternion.h:111
Quaternion Normalized() const
Definition: Quaternion.h:121
static Quaternion Slerp(const Quaternion &a, const Quaternion &b, T t)
Definition: Quaternion.h:203
T y
Definition: Quaternion.h:17
Quaternion(T w, T x, T y, T z)
friend Quaternion operator~(const Quaternion &a)
Definition: Quaternion.h:74
static Quaternion Nlerp(const Quaternion &a, const Quaternion &b, T t)
Definition: Quaternion.h:194
friend Quaternion operator*(const T s, const Quaternion &a)
Definition: Quaternion.h:92
Quaternion(T ang, vector3< T > axis)
Definition: Quaternion.h:24
friend Quaternion operator*(const Quaternion &a, const Quaternion &b)
Definition: Quaternion.h:83
void GetAxisAngle(T &angle, vector3< T > &axis)
Definition: Quaternion.h:48
static Quaternion FromMatrix3x3(const matrix3x3< U > &m)
Definition: Quaternion.h:129
matrix3x3< U > ToMatrix3x3() const
Definition: Quaternion.h:165
T x
Definition: Quaternion.h:17
friend Quaternion operator*(const Quaternion &a, const T s)
Definition: Quaternion.h:93
T w
Definition: Quaternion.h:17
Quaternion(const Quaternion< other_float_t > &o)
Definition: Quaternion.h:42
friend Quaternion operator+(const Quaternion &a, const Quaternion &b)
Definition: Quaternion.h:102
Quaternion(vector3< T > axis)
Definition: Quaternion.h:35
bool ExactlyEqual(const Quaternion &a) const
Definition: Quaternion.h:68
bool operator==(const Quaternion &a) const
Definition: Quaternion.h:64
static T Dot(const Quaternion &a, const Quaternion &b)
Definition: Quaternion.h:126
T z
Definition: Quaternion.h:17
Definition: matrix3x3.h:13
Definition: vector3.h:16
T y
Definition: vector3.h:18
T x
Definition: vector3.h:18
T z
Definition: vector3.h:18