-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuaternion.pde
441 lines (382 loc) · 9.65 KB
/
Quaternion.pde
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
class Quaternion implements Transformable < Quaternion > {
static final String printformat = "(%.2f, %.2f, %.2f, %.2f)";
float x = 0.0;
float y = 0.0;
float z = 0.0;
float w = 1.0;
Quaternion() {
}
Quaternion(float x, float y, float z, float w) {
set(x, y, z, w);
}
Quaternion(float angle, Vector axis) {
set(angle, axis);
}
Quaternion (PMatrix2D mat3x3) {
set(mat3x3);
}
Quaternion (PMatrix3D mat4x4) {
set(mat4x4);
}
Quaternion (Vector i, Vector j, Vector k) {
set(i, j, k);
}
Quaternion (float ix, float jx, float kx,
float iy, float jy, float ky,
float iz, float jz, float kz) {
set(ix, jx, kx, iy, jy, ky, iz, jz, kz);
}
String toString() {
return String.format(printformat, x, y, z, w);
}
int compareTo(Quaternion q) {
return w > q.w ? 1 : w < q.w ? -1 :
z > q.z ? 1 : z < q.z ? -1 :
y > q.y ? 1 : y < q.y ? -1 :
x > q.x ? 1 : x < q.x ? -1 : 0;
}
Quaternion add(Quaternion q) {
return add(q.x, q.y, q.z, q.w);
}
Quaternion add(Quaternion a, Quaternion b) {
x = a.x + b.x;
x = a.y + b.y;
x = a.z + b.z;
x = a.w + b.w;
return this;
}
Quaternion add(float qx, float qy, float qz, float qw) {
x += qx;
y += qy;
z += qz;
w += qw;
return this;
}
float angle() {
return acos(w) * 2.0;
}
boolean approx(Quaternion q) {
return approx(q.x, q.y, q.z, q.w, EPSILON);
}
boolean approx(Quaternion q, float tolerance) {
return approx(q.x, q.y, q.z, q.w, tolerance);
}
boolean approx(float qx, float qy, float qz, float qw,
float tolerance) {
return approximates(w, qw, tolerance) &&
approximates(z, qz, tolerance) &&
approximates(y, qy, tolerance) &&
approximates(x, qx, tolerance);
}
Vector axis() {
return axis(new Vector());
}
Vector axis(Vector out) {
float sin = asin(w);
if (sin != 0.0) {
sin = 1.0 / sin;
out.set(x * sin, y * sin, z * sin);
out.normalize();
return out;
}
out.set(1.0, 0.0, 0.0);
return out;
}
Quaternion div(Quaternion q) {
mult(new Quaternion().invert(q));
return this;
}
Quaternion div(float scalar) {
scalar = 1.0 / scalar;
x *= scalar;
y *= scalar;
z *= scalar;
w *= scalar;
return this;
}
float dot(Quaternion q) {
return x * q.x + y * q.y + z * q.z + w * q.w;
}
float dot(float qx, float qy, float qz, float qw) {
return x * qx + y * qy + z * qz + w * qw;
}
Vector forward() {
return forward(new Vector());
}
Vector forward(Vector out) {
// Equivalent to
// return mult(0.0, 0.0, 1.0, out);
out.x = y * w + w * y + z * x + x * z;
out.y = -x * w + y * z + z * y - w * x;
out.z = w * w - x * x + z * z - y * y;
return out;
}
Quaternion invert() {
float dot = magnitudeSq();
dot = dot == 0.0 ? 0.0 : 1.0 / dot;
x = -x * dot;
y = -y * dot;
z = -z * dot;
w = w * dot;
return this;
}
Quaternion invert(Quaternion in) {
float dot = in.magnitudeSq();
dot = dot == 0.0 ? 0.0 : 1.0 / dot;
x = -in.x * dot;
y = -in.y * dot;
z = -in.z * dot;
w = in.w * dot;
return this;
}
Quaternion lookAt(Coord origin, Coord target, Vector up) {
Vector k = new Vector(origin, target);
k.normalize();
return lookAt(k, up);
}
Quaternion lookAt(Vector forward, Vector up) {
if (forward.approx(0.0, 0.0, 0.0, EPSILON)) {
return this;
}
Vector i = new Vector();
Vector j = new Vector();
i.cross(forward, up);
i.normalize();
j.cross(forward, i);
j.normalize();
return set(i.x, j.x, forward.x,
i.y, j.y, forward.y,
i.z, j.z, forward.z);
}
float magnitude() {
return sqrt(x * x + y * y + z * z + w * w);
}
float magnitudeSq() {
return x * x + y * y + z * z + w * w;
}
Quaternion mult(float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
w *= scalar;
return this;
}
Quaternion mult(Quaternion b) {
float tx = x;
float ty = y;
float tz = z;
x = x * b.w + w * b.x + y * b.z - z * b.y;
y = y * b.w + w * b.y + z * b.x - tx * b.z;
z = z * b.w + w * b.z + tx * b.y - ty * b.x;
w = w * b.w - tx * b.x - ty * b.y - tz * b.z;
return this;
}
Quaternion mult(Quaternion a, Quaternion b) {
x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;
y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;
z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;
w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
return this;
}
Quaternion normalize() {
float m = magnitudeSq();
if (m != 0.0 && m != 1.0) {
div(sqrt(m));
}
return this;
}
Quaternion normalize(Quaternion in) {
float m = magnitudeSq();
if (m != 0.0 && m != 1.0) {
m = 1.0 / sqrt(m);
x = in.x * m;
y = in.y * m;
z = in.z * m;
w = in.w * m;
}
return this;
}
Quaternion random() {
// Alternatively, create a randomSpherical
// axis and a random angle.
x = (float)Math.random() * 2.0 - 1.0;
y = (float)Math.random() * 2.0 - 1.0;
z = (float)Math.random() * 2.0 - 1.0;
w = (float)Math.random() * 2.0 - 1.0;
return normalize();
}
Quaternion reset() {
return set(0.0, 0.0, 0.0, 1.0);
}
Quaternion rescale(float scalar) {
float mag = magnitudeSq();
if (mag == 0.0) {
return this;
} else if (mag == 1.0) {
mult(scalar);
return this;
}
mult(scalar / sqrt(mag));
return this;
}
Vector right() {
return right(new Vector());
}
Vector right(Vector out) {
// Equivalent to
// return mult(1.0, 0.0, 0.0, out);
out.x = w * w - y * y + x * x - z * z;
out.y = z * w + w * z + x * y + y * x;
out.z = -y * w + z * x + x * z - w * y;
return out;
}
Quaternion rotateBy(float angle, Vector axis) {
rotateBy(angle, axis.x, axis.y, axis.z);
return this;
}
Quaternion rotateBy(float angle, float axisx,
float axisy, float axisz) {
float a = acos(w) * 2.0 + angle;
float halfangle = 0.5 * floorMod(a, TWO_PI);
float sinhalf = sin(halfangle);
x = axisx * sinhalf;
y = axisy * sinhalf;
z = axisz * sinhalf;
w = cos(halfangle);
return this;
}
Quaternion rotateX(float angle) {
float cosa = cos(angle * 0.5);
float sina = sin(angle * 0.5);
float tempx = x;
float tempy = y;
x = cosa * x + sina * w;
y = cosa * y + sina * z;
z = cosa * z - sina * tempy;
w = cosa * w - sina * tempx;
return this;
}
Quaternion rotateY(float angle) {
float cosa = cos(angle * 0.5);
float sina = sin(angle * 0.5);
float tempx = x;
float tempy = y;
x = cosa * x - sina * z;
y = cosa * y + sina * w;
z = cosa * z + sina * tempx;
w = cosa * w - sina * tempy;
return this;
}
Quaternion rotateZ(float angle) {
float cosa = cos(angle * 0.5);
float sina = sin(angle * 0.5);
float tempx = x;
float tempz = z;
x = cosa * x + sina * y;
y = cosa * y - sina * tempx;
z = cosa * z + sina * w;
w = cosa * w - sina * tempz;
return this;
}
Quaternion set(float x, float y, float z, float w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return this;
}
Quaternion set(Quaternion q) {
x = q.x;
y = q.y;
z = q.z;
w = q.w;
return this;
}
Quaternion set(float angle, Vector axis) {
float halfangle = 0.5 * angle;
float sinhalf = sin(halfangle);
x = axis.x * sinhalf;
y = axis.y * sinhalf;
z = axis.z * sinhalf;
w = cos(halfangle);
return this;
}
Quaternion set(PMatrix2D mat3x3) {
return set(mat3x3.m00, mat3x3.m01, 0.0,
mat3x3.m10, mat3x3.m11, 0.0,
0.0, 0.0, 1.0);
}
Quaternion set(PMatrix3D mat4x4) {
return set(mat4x4.m00, mat4x4.m01, mat4x4.m02,
mat4x4.m10, mat4x4.m11, mat4x4.m12,
mat4x4.m20, mat4x4.m21, mat4x4.m22);
}
Quaternion set(Vector i, Vector j, Vector k) {
return set(i.x, j.x, k.x,
i.y, j.y, k.y,
i.z, j.z, k.z);
}
Quaternion set(float ix, float jx, float kx,
float iy, float jy, float ky,
float iz, float jz, float kz) {
w = sqrt(max(0.0, 1.0 + ix + jy + kz)) * 0.5;
x = sqrt(max(0.0, 1.0 + ix - jy - kz)) * 0.5;
y = sqrt(max(0.0, 1.0 - ix + jy - kz)) * 0.5;
z = sqrt(max(0.0, 1.0 - ix - jy + kz)) * 0.5;
x *= Math.signum(jz - ky);
y *= Math.signum(kx - iz);
z *= Math.signum(iy - jx);
return this;
}
Quaternion sub(Quaternion q) {
return sub(q.x, q.y, q.z, q.w);
}
Quaternion sub(Quaternion a, Quaternion b) {
x = a.x - b.x;
x = a.y - b.y;
x = a.z - b.z;
x = a.w - b.w;
return this;
}
Quaternion sub(float qx, float qy, float qz, float qw) {
x -= qx;
y -= qy;
z -= qz;
w -= qw;
return this;
}
PMatrix3D toMatrix(PMatrix3D out) {
float x2 = x + x;
float y2 = y + y;
float z2 = z + z;
float xsq2 = x * x2;
float ysq2 = y * y2;
float zsq2 = z * z2;
float xy2 = x * y2;
float xz2 = x * z2;
float yz2 = y * z2;
float wx2 = w * x2;
float wy2 = w * y2;
float wz2 = w * z2;
out.set(
1.0 - ysq2 - zsq2, xy2 - wz2, xz2 + wy2, 0.0,
xy2 + wz2, 1.0 - xsq2 - zsq2, yz2 - wx2, 0.0,
xz2 - wy2, yz2 + wx2, 1.0 - xsq2 - ysq2, 0.0,
0.0, 0.0, 0.0, 1.0);
return out;
}
PMatrix3D toPMatrix3D() {
return toMatrix(new PMatrix3D());
}
Vector up() {
return up(new Vector());
}
Vector up(Vector out) {
// Equivalent to
// return mult(0.0, 1.0, 0.0, out);
out.x = -z * w + x * y + y * x - w * z;
out.y = w * w - z * z + y * y - x * x;
out.z = x * w + w * x + y * z + z * y;
return out;
}
}