-
Notifications
You must be signed in to change notification settings - Fork 27
/
Quaternion.h
376 lines (333 loc) · 12.8 KB
/
Quaternion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*!
* A Quaternion class for computing rotations in the visualization classes
* (morph::Visual, morph::HexGridVisual, etc).
*
* This Quaternion class adopts the Hamiltonian convention - w,x,y,z.
*/
#pragma once
#include <morph/mathconst.h>
#include <morph/vec.h>
#include <limits>
#include <cmath>
#include <array>
#include <iostream>
#include <sstream>
namespace morph {
// Forward declare class and stream operator
template <typename Flt> class Quaternion;
template <typename Flt> std::ostream& operator<< (std::ostream&, const Quaternion<Flt>&);
/*!
* Quaternion computations
*/
template <typename Flt>
class Quaternion
{
public:
// Note, we need a Quaternion which has magnitude 1 as the default.
Quaternion()
: w(Flt{1})
, x(Flt{0})
, y(Flt{0})
, z(Flt{0}) {}
Quaternion (Flt _w, Flt _x, Flt _y, Flt _z)
: w(_w)
, x(_x)
, y(_y)
, z(_z) {}
//! User-declared destructor
~Quaternion() {}
//! User-declared copy constructor
Quaternion (const Quaternion<Flt>& rhs)
: w(rhs.w)
, x(rhs.x)
, y(rhs.y)
, z(rhs.z) {}
//! User-declared copy assignment constructor
Quaternion<Flt>& operator= (Quaternion<Flt>& other)
{
w = other.w;
x = other.x;
y = other.y;
z = other.z;
return *this;
}
//! Explicitly defaulted move constructor
Quaternion(Quaternion<Flt>&& other) = default;
//! Explicitly defaulted move assignment constructor
Quaternion<Flt>& operator=(Quaternion<Flt>&& other) = default;
alignas(Flt) Flt w;
alignas(Flt) Flt x;
alignas(Flt) Flt y;
alignas(Flt) Flt z;
//! String output
std::string str() const
{
std::stringstream ss;
ss << "Quaternion[wxyz]=(" << w << "," << x << "," << y << "," << z << ")";
return ss.str();
}
/*!
* Renormalize the Quaternion, in case floating point precision errors have
* caused it to have a magnitude significantly different from 1.
*/
void renormalize()
{
Flt oneovermag = Flt{1} / std::sqrt (w*w + x*x + y*y + z*z);
this->w *= oneovermag;
this->x *= oneovermag;
this->y *= oneovermag;
this->z *= oneovermag;
}
/*!
* The threshold outside of which the Quaternion is no longer considered to be a
* unit Quaternion.
*/
const Flt unitThresh = 0.001;
//! Test to see if this Quaternion is a unit Quaternion.
bool checkunit()
{
bool rtn = true;
Flt metric = Flt{1} - (w*w + x*x + y*y + z*z);
if (std::abs(metric) > morph::Quaternion<Flt>::unitThresh) {
rtn = false;
}
return rtn;
}
//! Initialize the Quaternion from the given axis and angle *in degrees*
void initFromAxisAngle (const vec<Flt>& axis, const Flt& angle)
{
Flt a = morph::mathconst<Flt>::pi_over_360 * angle; // angle/2 converted to rads
Flt s = std::sin(a);
Flt c = std::cos(a);
vec<Flt> ax = axis;
ax.renormalize();
this->w = c;
this->x = ax.x() * s;
this->y = ax.y() * s;
this->z = ax.z() * s;
this->renormalize();
}
//! Assignment operators
void operator= (const Quaternion<Flt>& q2)
{
this->w = q2.w;
this->x = q2.x;
this->y = q2.y;
this->z = q2.z;
}
//! Equality operator. True if all elements match
bool operator==(const Quaternion<Flt>& rhs) const
{
return (std::abs(this->w - rhs.w) < std::numeric_limits<Flt>::epsilon()
&& std::abs(this->x - rhs.x) < std::numeric_limits<Flt>::epsilon()
&& std::abs(this->y - rhs.y) < std::numeric_limits<Flt>::epsilon()
&& std::abs(this->z - rhs.z) < std::numeric_limits<Flt>::epsilon());
}
//! Not equals
bool operator!=(const Quaternion<Flt>& rhs) const
{
return (std::abs(this->w - rhs.w) >= std::numeric_limits<Flt>::epsilon()
|| std::abs(this->x - rhs.x) >= std::numeric_limits<Flt>::epsilon()
|| std::abs(this->y - rhs.y) >= std::numeric_limits<Flt>::epsilon()
|| std::abs(this->z - rhs.z) >= std::numeric_limits<Flt>::epsilon());
}
//! Overload * operator. q1 is 'this->'
template <typename F=Flt>
Quaternion<Flt> operator* (const Quaternion<F>& q2) const
{
Quaternion<Flt> q;
q.w = this->w * q2.w - this->x * q2.x - this->y * q2.y - this->z * q2.z;
q.x = this->w * q2.x + this->x * q2.w + this->y * q2.z - this->z * q2.y;
q.y = this->w * q2.y - this->x * q2.z + this->y * q2.w + this->z * q2.x;
q.z = this->w * q2.z + this->x * q2.y - this->y * q2.x + this->z * q2.w;
return q;
}
//! Overload / operator. q1 is 'this->', so this is q = q1 / q2
Quaternion<Flt> operator/ (const Quaternion<Flt>& q2) const
{
Quaternion<Flt> q;
Flt denom = (w*w + x*x + y*y + z*z);
q.w = (this->w * q2.w + this->x * q2.x + this->y * q2.y + this->z * q2.z) / denom;
q.x = (this->w * q2.x - this->x * q2.w - this->y * q2.z + this->z * q2.y) / denom;
q.y = (this->w * q2.y + this->x * q2.z - this->y * q2.w - this->z * q2.x) / denom;
q.z = (this->w * q2.z - this->x * q2.y + this->y * q2.x - this->z * q2.w) / denom;
return q;
}
//! Division by a scalar
Quaternion<Flt> operator/ (const Flt f) const
{
Quaternion<Flt> q;
q.w = this->w / f;
q.x = this->x / f;
q.y = this->y / f;
q.z = this->z / f;
return q;
}
//! Invert the rotation represented by this Quaternion and return the result.
Quaternion<Flt> invert() const
{
Quaternion<Flt> qi = *this;
qi.w = -this->w;
return qi;
}
//! Conjugate of the Quaternion. This happens to give a quaternion representing the same
//! rotation as that returned by invert() because -q represents an quivalent rotation to q.
Quaternion<Flt> conjugate() const
{
Quaternion<Flt> qconj (this->w, -this->x, -this->y, -this->z);
return qconj;
}
//! Compute the inverse, q^-1. Also known as the reciprocal, q^-1 * q = I.
Quaternion<Flt> inverse() const
{
return (this->conjugate() / (w*w + x*x + y*y + z*z));
}
//! Return the magnitude of the Quaternion
Flt magnitude() const { return std::sqrt (w*w + x*x + y*y + z*z); }
//! Reset to a zero rotation
void reset()
{
this->w = Flt{1};
this->x = Flt{0};
this->y = Flt{0};
this->z = Flt{0};
}
//! Multiply this quaternion by other as: this = this * q2, i.e. q1 is 'this->'
void postmultiply (const Quaternion<Flt>& q2)
{
// First make copies of w, x, y, z
Flt q1_w = this->w;
Flt q1_x = this->x;
Flt q1_y = this->y;
Flt q1_z = this->z;
// Now compute
this->w = q1_w * q2.w - q1_x * q2.x - q1_y * q2.y - q1_z * q2.z;
this->x = q1_w * q2.x + q1_x * q2.w + q1_y * q2.z - q1_z * q2.y;
this->y = q1_w * q2.y - q1_x * q2.z + q1_y * q2.w + q1_z * q2.x;
this->z = q1_w * q2.z + q1_x * q2.y - q1_y * q2.x + q1_z * q2.w;
}
//! Multiply this quaternion by other as: this = q1 * this
void premultiply (const Quaternion<Flt>& q1)
{
// First make copies of w, x, y, z
Flt q2_w = this->w;
Flt q2_x = this->x;
Flt q2_y = this->y;
Flt q2_z = this->z;
// Now compute
this->w = q1.w * q2_w - q1.x * q2_x - q1.y * q2_y - q1.z * q2_z;
this->x = q1.w * q2_x + q1.x * q2_w + q1.y * q2_z - q1.z * q2_y;
this->y = q1.w * q2_y - q1.x * q2_z + q1.y * q2_w + q1.z * q2_x;
this->z = q1.w * q2_z + q1.x * q2_y - q1.y * q2_x + q1.z * q2_w;
}
/*!
* Change this Quaternion to represent a new rotation by rotating it \a angle
* (radians) around the axis given by \a axis_x, \a axis_y, \a axis_z.
*/
void rotate (const Flt axis_x, const Flt axis_y, const Flt axis_z, const Flt angle)
{
Flt halfangle = angle * Flt{0.5};
Flt cosHalf = std::cos (halfangle);
Flt sinHalf = std::sin (halfangle);
Quaternion<Flt> local(cosHalf, axis_x * sinHalf, axis_y * sinHalf, axis_z * sinHalf);
this->premultiply (local);
}
/*!
* Change this Quaternion to represent a new rotation by rotating it \a angle
* (radians) around the axis given by \a axis.
*/
void rotate (const std::array<Flt, 3>& axis, const Flt angle)
{
Flt halfangle = angle * Flt{0.5};
Flt cosHalf = std::cos (halfangle);
Flt sinHalf = std::sin (halfangle);
Quaternion<Flt> local(cosHalf, axis[0] * sinHalf, axis[1] * sinHalf, axis[2] * sinHalf);
this->premultiply (local);
}
/*!
* Change this Quaternion to represent a new rotation by rotating it \a angle
* (radians) around the axis given by \a axis.
*/
void rotate (const vec<Flt, 3>& axis, const Flt angle)
{
Flt halfangle = angle * Flt{0.5};
Flt cosHalf = std::cos (halfangle);
Flt sinHalf = std::sin (halfangle);
Quaternion<Flt> local(cosHalf, axis[0] * sinHalf, axis[1] * sinHalf, axis[2] * sinHalf);
this->premultiply (local);
}
/*!
* Obtain the rotation matrix (without assumption that this is a unit
* Quaternion)
*
* std::array represents a matrix with indices like this (i.e. column major
* format, which is OpenGL friendly)
*
* 0 4 8 12
* 1 5 9 13
* 2 6 10 14
* 3 7 11 15
*/
std::array<Flt, 16> rotationMatrix() const
{
std::array<Flt, 16> mat;
this->rotationMatrix (mat);
return mat;
}
//! Rotate the matrix \a mat by this Quaternion witout assuming it's a unit Quaternion
void rotationMatrix (std::array<Flt, 16>& mat) const
{
mat[0] = w*w + x*x - y*y - z*z;
mat[1] = Flt{2}*x*y + Flt{2}*w*z;
mat[2] = Flt{2}*x*z - Flt{2}*w*y;
mat[3] = Flt{0};
mat[4] = Flt{2}*x*y - Flt{2}*w*z;
mat[5] = w*w - x*x + y*y - z*z;
mat[6] = Flt{2}*y*z - Flt{2}*w*x;
mat[7] = Flt{0};
mat[8] = Flt{2}*x*z + Flt{2}*w*y;
mat[9] = Flt{2}*y*z + Flt{2}*w*x;
mat[10] = w*w - x*x - y*y + z*z;
mat[11] = Flt{0};
mat[12] = Flt{0};
mat[13] = Flt{0};
mat[14] = Flt{0};
mat[15] = Flt{1};
}
//! Obtain rotation matrix assuming this IS a unit Quaternion
std::array<Flt, 16> unitRotationMatrix() const
{
std::array<Flt, 16> mat;
this->unitRotationMatrix (mat);
return mat;
}
//! Rotate the matrix \a mat by this Quaternion, assuming it's a unit Quaternion
void unitRotationMatrix (std::array<Flt, 16>& mat) const
{
mat[0] = Flt{1} - Flt{2}*y*y - Flt{2}*z*z;
mat[1] = Flt{2}*x*y + Flt{2}*w*z;
mat[2] = Flt{2}*x*z - Flt{2}*w*y;
mat[3] = Flt{0};
mat[4] = Flt{2}*x*y - Flt{2}*w*z;
mat[5] = 1.0 - Flt{2}*x*x - Flt{2}*z*z;
mat[6] = Flt{2}*y*z - Flt{2}*w*x;
mat[7] = Flt{0};
mat[8] = Flt{2}*x*z + Flt{2}*w*y;
mat[9] = Flt{2}*y*z + Flt{2}*w*x;
mat[10] = Flt{1} - Flt{2}*x*x - Flt{2}*y*y;
mat[11] = Flt{0};
mat[12] = Flt{0};
mat[13] = Flt{0};
mat[14] = Flt{0};
mat[15] = Flt{1};
}
//! Overload the stream output operator
friend std::ostream& operator<< <Flt> (std::ostream& os, const Quaternion<Flt>& q);
};
template <typename Flt>
std::ostream& operator<< (std::ostream& os, const Quaternion<Flt>& q)
{
os << q.str();
return os;
}
} // namespace morph