-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquaternion.cpp
269 lines (244 loc) · 6.7 KB
/
quaternion.cpp
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
/**
* Quaternion.cpp v1.0.0 30/08/2023
*
* Copyright (c) 2023, Robert Eisele (raw.org)
* Licensed under the MIT license.
**/
#include "quaternion.h"
/**
* Assigns a quaternion to the current quaternion
*/
Quaternion &Quaternion::operator=(const Quaternion &q)
{
w = q.w;
x = q.x;
y = q.y;
z = q.z;
return *this;
}
/**
* Adds two quaternions Q1 and Q2
*/
Quaternion &Quaternion::operator+=(const Quaternion &q)
{
w += q.w;
x += q.x;
y += q.y;
z += q.z;
return *this;
}
/**
* Subtracts a quaternions Q2 from Q1
*/
Quaternion &Quaternion::operator-=(const Quaternion &q)
{
w -= q.w;
x -= q.x;
y -= q.y;
z -= q.z;
return *this;
}
/**
* Scales a quaternion by a scalar
*/
Quaternion &Quaternion::operator*=(float scale)
{
w *= scale;
x *= scale;
y *= scale;
z *= scale;
return *this;
}
/**
* Calculates the Hamilton product of two quaternions
*/
Quaternion &Quaternion::operator*=(const Quaternion &q)
{
float w1 = w;
float x1 = x;
float y1 = y;
float z1 = z;
float w2 = q.w;
float x2 = q.x;
float y2 = q.y;
float z2 = q.z;
w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2;
x = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2;
y = w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2;
z = w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2;
return *this;
}
/**
* Calculates the dot product of two quaternions
*/
float Quaternion::dot(const Quaternion &q) const
{
return w * q.w + x * q.x + y * q.y + z * q.z;
}
/**
* Calculates the length/modulus/magnitude or the norm of a quaternion
*/
float Quaternion::norm() const
{
return sqrtf(w * w + x * x + y * y + z * z);
}
/**
* Calculates the squared length/modulus/magnitude or the norm of a quaternion
*/
float Quaternion::normSq() const
{
return w * w + x * x + y * y + z * z;
}
/**
* Normalizes the quaternion to have |Q| = 1 as long as the norm is not zero
*/
Quaternion &Quaternion::normalize()
{
float iLen = 1 / norm();
w *= iLen;
x *= iLen;
y *= iLen;
z *= iLen;
return *this;
}
/**
* Calculates the conjugate of a quaternion
*/
const Quaternion Quaternion::conjugate() const
{
return Quaternion(w, -x, -y, -z);
}
/**
* Rotates a vector according to the current quaternion, assumes |q|=1
*
* @link https://raw.org/proof/vector-rotation-using-quaternions/
*/
void Quaternion::rotateVector(float &vx, float &vy, float &vz)
{
// t = 2q x v
float tx = 2. * (y * vz - z * vy);
float ty = 2. * (z * vx - x * vz);
float tz = 2. * (x * vy - y * vx);
// v + w t + q x t
vx = vx + w * tx + y * tz - z * ty;
vy = vy + w * ty + z * tx - x * tz;
vz = vz + w * tz + x * ty - y * tx;
}
/**
* Creates a quaternion by a rotation given by Euler angles (multiplication order from right to left)
*
* If needed, define QUATERNION_EULER_ORDER for another order
*/
static const Quaternion fromEuler(float x, float y, float z)
{
x = x * 0.5;
y = y * 0.5;
z = z * 0.5;
float cX = cosf(x);
float cY = cosf(y);
float cZ = cosf(z);
float sX = sinf(x);
float sY = sinf(y);
float sZ = sinf(z);
#if QUATERNION_EULER_ORDER == QUATERNION_EULER_ZXY
// axisAngle([0, 0, 1], φ) * axisAngle([1, 0, 0], θ) * axisAngle([0, 1, 0], ψ)
return Quaternion(
cX * cY * cZ - sX * sY * sZ,
sY * cX * cZ - sX * sZ * cY,
sX * sY * cZ + sZ * cX * cY,
sX * cY * cZ + sY * sZ * cX);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_XYZ // roll around X, pitch around Y, yaw around Z
// axisAngle([1, 0, 0], φ) * axisAngle([0, 1, 0], θ) * axisAngle([0, 0, 1], ψ)
return Quaternion(
cX * cY * cZ - sX * sY * sZ,
sX * cY * cZ + sY * sZ * cX,
sY * cX * cZ - sX * sZ * cY,
sX * sY * cZ + sZ * cX * cY);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_YXZ // deviceorientation
// axisAngle([0, 1, 0], φ) * axisAngle([1, 0, 0], θ) * axisAngle([0, 0, 1], ψ)
return Quaternion(
sX * sY * sZ + cX * cY * cZ,
sX * sZ * cY + sY * cX * cZ,
sX * cY * cZ - sY * sZ * cX,
sZ * cX * cY - sX * sY * cZ);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_ZYX
// axisAngle([0, 0, 1], φ) * axisAngle([0, 1, 0], θ) * axisAngle([1, 0, 0], ψ)
return Quaternion(
sX * sY * sZ + cX * cY * cZ,
sZ * cX * cY - sX * sY * cZ,
sX * sZ * cY + sY * cX * cZ,
sX * cY * cZ - sY * sZ * cX);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_YZX
// axisAngle([0, 1, 0], φ) * axisAngle([0, 0, 1], θ) * axisAngle([1, 0, 0], ψ)
return Quaternion(
cX * cY * cZ - sX * sY * sZ,
sX * sY * cZ + sZ * cX * cY,
sX * cY * cZ + sY * sZ * cX,
sY * cX * cZ - sX * sZ * cY);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_XZY
// axisAngle([1, 0, 0], φ) * axisAngle([0, 0, 1], θ) * axisAngle([0, 1, 0], ψ)
return Quaternion(
sX * sY * sZ + cX * cY * cZ,
sX * cY * cZ - sY * sZ * cX,
sZ * cX * cY - sX * sY * cZ,
sX * sZ * cY + sY * cX * cZ);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_ZYZ
// axisAngle([0, 0, 1], φ) * axisAngle([0, 1, 0], θ) * axisAngle([0, 0, 1], ψ)
return Quaternion(
cX * cY * cZ - sX * sZ * cY,
sY * sZ * cX - sX * sY * cZ,
sX * sY * sZ + sY * cX * cZ,
sX * cY * cZ + sZ * cX * cY);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_ZXZ
// axisAngle([0, 0, 1], φ) * axisAngle([1, 0, 0], θ) * axisAngle([0, 0, 1], ψ)
return Quaternion(
cX * cY * cZ - sX * sZ * cY,
sX * sY * sZ + sY * cX * cZ,
sX * sY * cZ - sY * sZ * cX,
sX * cY * cZ + sZ * cX * cY);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_YXY
// axisAngle([0, 1, 0], φ) * axisAngle([1, 0, 0], θ) * axisAngle([0, 1, 0], ψ)
return Quaternion(
cX * cY * cZ - sX * sZ * cY,
sX * sY * sZ + sY * cX * cZ,
sX * cY * cZ + sZ * cX * cY,
sY * sZ * cX - sX * sY * cZ);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_YZY
// axisAngle([0, 1, 0], φ) * axisAngle([0, 0, 1], θ) * axisAngle([0, 1, 0], ψ)
return Quaternion(
cX * cY * cZ - sX * sZ * cY,
sX * sY * cZ - sY * sZ * cX,
sX * cY * cZ + sZ * cX * cY,
sX * sY * sZ + sY * cX * cZ);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_XYX
// axisAngle([1, 0, 0], φ) * axisAngle([0, 1, 0], θ) * axisAngle([1, 0, 0], ψ)
return Quaternion(
cX * cY * cZ - sX * sZ * cY,
sX * cY * cZ + sZ * cX * cY,
sX * sY * sZ + sY * cX * cZ,
sX * sY * cZ - sY * sZ * cX);
#elif QUATERNION_EULER_ORDER == QUATERNION_EULER_XZX
// axisAngle([1, 0, 0], φ) * axisAngle([0, 0, 1], θ) * axisAngle([1, 0, 0], ψ)
return Quaternion(
cX * cY * cZ - sX * sZ * cY,
sX * cY * cZ + sZ * cX * cY,
sY * sZ * cX - sX * sY * cZ,
sX * sY * sZ + sY * cX * cZ);
#endif
}
/**
* Creates quaternion by a rotation given as axis-angle orientation
*/
const Quaternion Quaternion::fromAxisAngle(float x, float y, float z, float angle)
{
Quaternion ret;
float halfAngle = angle * 0.5;
float sin_2 = sinf(halfAngle);
float cos_2 = cosf(halfAngle);
float sin_norm = sin_2 / sqrtf(x * x + y * y + z * z);
ret.w = cos_2;
ret.x = x * sin_norm;
ret.y = y * sin_norm;
ret.z = z * sin_norm;
return ret;
}