-
Notifications
You must be signed in to change notification settings - Fork 24.5k
/
Copy pathMatrixMath.js
executable file
·753 lines (689 loc) · 19.7 KB
/
MatrixMath.js
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @noflow
*/
/* eslint-disable space-infix-ops */
'use strict';
const invariant = require('invariant');
/**
* Memory conservative (mutative) matrix math utilities. Uses "command"
* matrices, which are reusable.
*/
const MatrixMath = {
createIdentityMatrix: function() {
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
},
createCopy: function(m) {
return [
m[0],
m[1],
m[2],
m[3],
m[4],
m[5],
m[6],
m[7],
m[8],
m[9],
m[10],
m[11],
m[12],
m[13],
m[14],
m[15],
];
},
createOrthographic: function(left, right, bottom, top, near, far) {
const a = 2 / (right - left);
const b = 2 / (top - bottom);
const c = -2 / (far - near);
const tx = -(right + left) / (right - left);
const ty = -(top + bottom) / (top - bottom);
const tz = -(far + near) / (far - near);
return [a, 0, 0, 0, 0, b, 0, 0, 0, 0, c, 0, tx, ty, tz, 1];
},
createFrustum: function(left, right, bottom, top, near, far) {
const r_width = 1 / (right - left);
const r_height = 1 / (top - bottom);
const r_depth = 1 / (near - far);
const x = 2 * (near * r_width);
const y = 2 * (near * r_height);
const A = (right + left) * r_width;
const B = (top + bottom) * r_height;
const C = (far + near) * r_depth;
const D = 2 * (far * near * r_depth);
return [x, 0, 0, 0, 0, y, 0, 0, A, B, C, -1, 0, 0, D, 0];
},
/**
* This create a perspective projection towards negative z
* Clipping the z range of [-near, -far]
*
* @param fovInRadians - field of view in randians
*/
createPerspective: function(fovInRadians, aspect, near, far) {
const h = 1 / Math.tan(fovInRadians / 2);
const r_depth = 1 / (near - far);
const C = (far + near) * r_depth;
const D = 2 * (far * near * r_depth);
return [h / aspect, 0, 0, 0, 0, h, 0, 0, 0, 0, C, -1, 0, 0, D, 0];
},
createTranslate2d: function(x, y) {
const mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseTranslate2dCommand(mat, x, y);
return mat;
},
reuseTranslate2dCommand: function(matrixCommand, x, y) {
matrixCommand[12] = x;
matrixCommand[13] = y;
},
reuseTranslate3dCommand: function(matrixCommand, x, y, z) {
matrixCommand[12] = x;
matrixCommand[13] = y;
matrixCommand[14] = z;
},
createScale: function(factor) {
const mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseScaleCommand(mat, factor);
return mat;
},
reuseScaleCommand: function(matrixCommand, factor) {
matrixCommand[0] = factor;
matrixCommand[5] = factor;
},
reuseScale3dCommand: function(matrixCommand, x, y, z) {
matrixCommand[0] = x;
matrixCommand[5] = y;
matrixCommand[10] = z;
},
reusePerspectiveCommand: function(matrixCommand, p) {
matrixCommand[11] = -1 / p;
},
reuseScaleXCommand(matrixCommand, factor) {
matrixCommand[0] = factor;
},
reuseScaleYCommand(matrixCommand, factor) {
matrixCommand[5] = factor;
},
reuseScaleZCommand(matrixCommand, factor) {
matrixCommand[10] = factor;
},
reuseRotateXCommand: function(matrixCommand, radians) {
matrixCommand[5] = Math.cos(radians);
matrixCommand[6] = Math.sin(radians);
matrixCommand[9] = -Math.sin(radians);
matrixCommand[10] = Math.cos(radians);
},
reuseRotateYCommand: function(matrixCommand, amount) {
matrixCommand[0] = Math.cos(amount);
matrixCommand[2] = -Math.sin(amount);
matrixCommand[8] = Math.sin(amount);
matrixCommand[10] = Math.cos(amount);
},
// http://www.w3.org/TR/css3-transforms/#recomposing-to-a-2d-matrix
reuseRotateZCommand: function(matrixCommand, radians) {
matrixCommand[0] = Math.cos(radians);
matrixCommand[1] = Math.sin(radians);
matrixCommand[4] = -Math.sin(radians);
matrixCommand[5] = Math.cos(radians);
},
createRotateZ: function(radians) {
const mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateZCommand(mat, radians);
return mat;
},
reuseSkewXCommand: function(matrixCommand, radians) {
matrixCommand[4] = Math.tan(radians);
},
reuseSkewYCommand: function(matrixCommand, radians) {
matrixCommand[1] = Math.tan(radians);
},
multiplyInto: function(out, a, b) {
const a00 = a[0],
a01 = a[1],
a02 = a[2],
a03 = a[3],
a10 = a[4],
a11 = a[5],
a12 = a[6],
a13 = a[7],
a20 = a[8],
a21 = a[9],
a22 = a[10],
a23 = a[11],
a30 = a[12],
a31 = a[13],
a32 = a[14],
a33 = a[15];
let b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3];
out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[4];
b1 = b[5];
b2 = b[6];
b3 = b[7];
out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[8];
b1 = b[9];
b2 = b[10];
b3 = b[11];
out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[12];
b1 = b[13];
b2 = b[14];
b3 = b[15];
out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
},
determinant(matrix: Array<number>): number {
const [
m00,
m01,
m02,
m03,
m10,
m11,
m12,
m13,
m20,
m21,
m22,
m23,
m30,
m31,
m32,
m33,
] = matrix;
return (
m03 * m12 * m21 * m30 -
m02 * m13 * m21 * m30 -
m03 * m11 * m22 * m30 +
m01 * m13 * m22 * m30 +
m02 * m11 * m23 * m30 -
m01 * m12 * m23 * m30 -
m03 * m12 * m20 * m31 +
m02 * m13 * m20 * m31 +
m03 * m10 * m22 * m31 -
m00 * m13 * m22 * m31 -
m02 * m10 * m23 * m31 +
m00 * m12 * m23 * m31 +
m03 * m11 * m20 * m32 -
m01 * m13 * m20 * m32 -
m03 * m10 * m21 * m32 +
m00 * m13 * m21 * m32 +
m01 * m10 * m23 * m32 -
m00 * m11 * m23 * m32 -
m02 * m11 * m20 * m33 +
m01 * m12 * m20 * m33 +
m02 * m10 * m21 * m33 -
m00 * m12 * m21 * m33 -
m01 * m10 * m22 * m33 +
m00 * m11 * m22 * m33
);
},
/**
* Inverse of a matrix. Multiplying by the inverse is used in matrix math
* instead of division.
*
* Formula from:
* http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
*/
inverse(matrix: Array<number>): Array<number> {
const det = MatrixMath.determinant(matrix);
if (!det) {
return matrix;
}
const [
m00,
m01,
m02,
m03,
m10,
m11,
m12,
m13,
m20,
m21,
m22,
m23,
m30,
m31,
m32,
m33,
] = matrix;
return [
(m12 * m23 * m31 -
m13 * m22 * m31 +
m13 * m21 * m32 -
m11 * m23 * m32 -
m12 * m21 * m33 +
m11 * m22 * m33) /
det,
(m03 * m22 * m31 -
m02 * m23 * m31 -
m03 * m21 * m32 +
m01 * m23 * m32 +
m02 * m21 * m33 -
m01 * m22 * m33) /
det,
(m02 * m13 * m31 -
m03 * m12 * m31 +
m03 * m11 * m32 -
m01 * m13 * m32 -
m02 * m11 * m33 +
m01 * m12 * m33) /
det,
(m03 * m12 * m21 -
m02 * m13 * m21 -
m03 * m11 * m22 +
m01 * m13 * m22 +
m02 * m11 * m23 -
m01 * m12 * m23) /
det,
(m13 * m22 * m30 -
m12 * m23 * m30 -
m13 * m20 * m32 +
m10 * m23 * m32 +
m12 * m20 * m33 -
m10 * m22 * m33) /
det,
(m02 * m23 * m30 -
m03 * m22 * m30 +
m03 * m20 * m32 -
m00 * m23 * m32 -
m02 * m20 * m33 +
m00 * m22 * m33) /
det,
(m03 * m12 * m30 -
m02 * m13 * m30 -
m03 * m10 * m32 +
m00 * m13 * m32 +
m02 * m10 * m33 -
m00 * m12 * m33) /
det,
(m02 * m13 * m20 -
m03 * m12 * m20 +
m03 * m10 * m22 -
m00 * m13 * m22 -
m02 * m10 * m23 +
m00 * m12 * m23) /
det,
(m11 * m23 * m30 -
m13 * m21 * m30 +
m13 * m20 * m31 -
m10 * m23 * m31 -
m11 * m20 * m33 +
m10 * m21 * m33) /
det,
(m03 * m21 * m30 -
m01 * m23 * m30 -
m03 * m20 * m31 +
m00 * m23 * m31 +
m01 * m20 * m33 -
m00 * m21 * m33) /
det,
(m01 * m13 * m30 -
m03 * m11 * m30 +
m03 * m10 * m31 -
m00 * m13 * m31 -
m01 * m10 * m33 +
m00 * m11 * m33) /
det,
(m03 * m11 * m20 -
m01 * m13 * m20 -
m03 * m10 * m21 +
m00 * m13 * m21 +
m01 * m10 * m23 -
m00 * m11 * m23) /
det,
(m12 * m21 * m30 -
m11 * m22 * m30 -
m12 * m20 * m31 +
m10 * m22 * m31 +
m11 * m20 * m32 -
m10 * m21 * m32) /
det,
(m01 * m22 * m30 -
m02 * m21 * m30 +
m02 * m20 * m31 -
m00 * m22 * m31 -
m01 * m20 * m32 +
m00 * m21 * m32) /
det,
(m02 * m11 * m30 -
m01 * m12 * m30 -
m02 * m10 * m31 +
m00 * m12 * m31 +
m01 * m10 * m32 -
m00 * m11 * m32) /
det,
(m01 * m12 * m20 -
m02 * m11 * m20 +
m02 * m10 * m21 -
m00 * m12 * m21 -
m01 * m10 * m22 +
m00 * m11 * m22) /
det,
];
},
/**
* Turns columns into rows and rows into columns.
*/
transpose(m: Array<number>): Array<number> {
return [
m[0],
m[4],
m[8],
m[12],
m[1],
m[5],
m[9],
m[13],
m[2],
m[6],
m[10],
m[14],
m[3],
m[7],
m[11],
m[15],
];
},
/**
* Based on: http://tog.acm.org/resources/GraphicsGems/gemsii/unmatrix.c
*/
multiplyVectorByMatrix(v: Array<number>, m: Array<number>): Array<number> {
const [vx, vy, vz, vw] = v;
return [
vx * m[0] + vy * m[4] + vz * m[8] + vw * m[12],
vx * m[1] + vy * m[5] + vz * m[9] + vw * m[13],
vx * m[2] + vy * m[6] + vz * m[10] + vw * m[14],
vx * m[3] + vy * m[7] + vz * m[11] + vw * m[15],
];
},
/**
* From: https://code.google.com/p/webgl-mjs/source/browse/mjs.js
*/
v3Length(a: Array<number>): number {
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
},
/**
* Based on: https://code.google.com/p/webgl-mjs/source/browse/mjs.js
*/
v3Normalize(vector: Array<number>, v3Length: number): Array<number> {
const im = 1 / (v3Length || MatrixMath.v3Length(vector));
return [vector[0] * im, vector[1] * im, vector[2] * im];
},
/**
* The dot product of a and b, two 3-element vectors.
* From: https://code.google.com/p/webgl-mjs/source/browse/mjs.js
*/
v3Dot(a, b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
},
/**
* From:
* http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
*/
v3Combine(
a: Array<number>,
b: Array<number>,
aScale: number,
bScale: number,
): Array<number> {
return [
aScale * a[0] + bScale * b[0],
aScale * a[1] + bScale * b[1],
aScale * a[2] + bScale * b[2],
];
},
/**
* From:
* http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
*/
v3Cross(a: Array<number>, b: Array<number>): Array<number> {
return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
];
},
/**
* Based on:
* http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/
* and:
* http://quat.zachbennett.com/
*
* Note that this rounds degrees to the thousandth of a degree, due to
* floating point errors in the creation of the quaternion.
*
* Also note that this expects the qw value to be last, not first.
*
* Also, when researching this, remember that:
* yaw === heading === z-axis
* pitch === elevation/attitude === y-axis
* roll === bank === x-axis
*/
quaternionToDegreesXYZ(q: Array<number>, matrix, row): Array<number> {
const [qx, qy, qz, qw] = q;
const qw2 = qw * qw;
const qx2 = qx * qx;
const qy2 = qy * qy;
const qz2 = qz * qz;
const test = qx * qy + qz * qw;
const unit = qw2 + qx2 + qy2 + qz2;
const conv = 180 / Math.PI;
if (test > 0.49999 * unit) {
return [0, 2 * Math.atan2(qx, qw) * conv, 90];
}
if (test < -0.49999 * unit) {
return [0, -2 * Math.atan2(qx, qw) * conv, -90];
}
return [
MatrixMath.roundTo3Places(
Math.atan2(2 * qx * qw - 2 * qy * qz, 1 - 2 * qx2 - 2 * qz2) * conv,
),
MatrixMath.roundTo3Places(
Math.atan2(2 * qy * qw - 2 * qx * qz, 1 - 2 * qy2 - 2 * qz2) * conv,
),
MatrixMath.roundTo3Places(Math.asin(2 * qx * qy + 2 * qz * qw) * conv),
];
},
/**
* Based on:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
*/
roundTo3Places(n: number): number {
const arr = n.toString().split('e');
return Math.round(arr[0] + 'e' + (arr[1] ? +arr[1] - 3 : 3)) * 0.001;
},
/**
* Decompose a matrix into separate transform values, for use on platforms
* where applying a precomposed matrix is not possible, and transforms are
* applied in an inflexible ordering (e.g. Android).
*
* Implementation based on
* http://www.w3.org/TR/css3-transforms/#decomposing-a-2d-matrix
* http://www.w3.org/TR/css3-transforms/#decomposing-a-3d-matrix
* which was based on
* http://tog.acm.org/resources/GraphicsGems/gemsii/unmatrix.c
*/
decomposeMatrix(transformMatrix: Array<number>): ?Object {
invariant(
transformMatrix.length === 16,
'Matrix decomposition needs a list of 3d matrix values, received %s',
transformMatrix,
);
// output values
let perspective = [];
const quaternion = [];
const scale = [];
const skew = [];
const translation = [];
// create normalized, 2d array matrix
// and normalized 1d array perspectiveMatrix with redefined 4th column
if (!transformMatrix[15]) {
return;
}
const matrix = [];
const perspectiveMatrix = [];
for (let i = 0; i < 4; i++) {
matrix.push([]);
for (let j = 0; j < 4; j++) {
const value = transformMatrix[i * 4 + j] / transformMatrix[15];
matrix[i].push(value);
perspectiveMatrix.push(j === 3 ? 0 : value);
}
}
perspectiveMatrix[15] = 1;
// test for singularity of upper 3x3 part of the perspective matrix
if (!MatrixMath.determinant(perspectiveMatrix)) {
return;
}
// isolate perspective
if (matrix[0][3] !== 0 || matrix[1][3] !== 0 || matrix[2][3] !== 0) {
// rightHandSide is the right hand side of the equation.
// rightHandSide is a vector, or point in 3d space relative to the origin.
const rightHandSide = [
matrix[0][3],
matrix[1][3],
matrix[2][3],
matrix[3][3],
];
// Solve the equation by inverting perspectiveMatrix and multiplying
// rightHandSide by the inverse.
const inversePerspectiveMatrix = MatrixMath.inverse(perspectiveMatrix);
const transposedInversePerspectiveMatrix = MatrixMath.transpose(
inversePerspectiveMatrix,
);
perspective = MatrixMath.multiplyVectorByMatrix(
rightHandSide,
transposedInversePerspectiveMatrix,
);
} else {
// no perspective
perspective[0] = perspective[1] = perspective[2] = 0;
perspective[3] = 1;
}
// translation is simple
for (let i = 0; i < 3; i++) {
translation[i] = matrix[3][i];
}
// Now get scale and shear.
// 'row' is a 3 element array of 3 component vectors
const row = [];
for (let i = 0; i < 3; i++) {
row[i] = [matrix[i][0], matrix[i][1], matrix[i][2]];
}
// Compute X scale factor and normalize first row.
scale[0] = MatrixMath.v3Length(row[0]);
row[0] = MatrixMath.v3Normalize(row[0], scale[0]);
// Compute XY shear factor and make 2nd row orthogonal to 1st.
skew[0] = MatrixMath.v3Dot(row[0], row[1]);
row[1] = MatrixMath.v3Combine(row[1], row[0], 1.0, -skew[0]);
// Compute XY shear factor and make 2nd row orthogonal to 1st.
skew[0] = MatrixMath.v3Dot(row[0], row[1]);
row[1] = MatrixMath.v3Combine(row[1], row[0], 1.0, -skew[0]);
// Now, compute Y scale and normalize 2nd row.
scale[1] = MatrixMath.v3Length(row[1]);
row[1] = MatrixMath.v3Normalize(row[1], scale[1]);
skew[0] /= scale[1];
// Compute XZ and YZ shears, orthogonalize 3rd row
skew[1] = MatrixMath.v3Dot(row[0], row[2]);
row[2] = MatrixMath.v3Combine(row[2], row[0], 1.0, -skew[1]);
skew[2] = MatrixMath.v3Dot(row[1], row[2]);
row[2] = MatrixMath.v3Combine(row[2], row[1], 1.0, -skew[2]);
// Next, get Z scale and normalize 3rd row.
scale[2] = MatrixMath.v3Length(row[2]);
row[2] = MatrixMath.v3Normalize(row[2], scale[2]);
skew[1] /= scale[2];
skew[2] /= scale[2];
// At this point, the matrix (in rows) is orthonormal.
// Check for a coordinate system flip. If the determinant
// is -1, then negate the matrix and the scaling factors.
const pdum3 = MatrixMath.v3Cross(row[1], row[2]);
if (MatrixMath.v3Dot(row[0], pdum3) < 0) {
for (let i = 0; i < 3; i++) {
scale[i] *= -1;
row[i][0] *= -1;
row[i][1] *= -1;
row[i][2] *= -1;
}
}
// Now, get the rotations out
quaternion[0] =
0.5 * Math.sqrt(Math.max(1 + row[0][0] - row[1][1] - row[2][2], 0));
quaternion[1] =
0.5 * Math.sqrt(Math.max(1 - row[0][0] + row[1][1] - row[2][2], 0));
quaternion[2] =
0.5 * Math.sqrt(Math.max(1 - row[0][0] - row[1][1] + row[2][2], 0));
quaternion[3] =
0.5 * Math.sqrt(Math.max(1 + row[0][0] + row[1][1] + row[2][2], 0));
if (row[2][1] > row[1][2]) {
quaternion[0] = -quaternion[0];
}
if (row[0][2] > row[2][0]) {
quaternion[1] = -quaternion[1];
}
if (row[1][0] > row[0][1]) {
quaternion[2] = -quaternion[2];
}
// correct for occasional, weird Euler synonyms for 2d rotation
let rotationDegrees;
if (
quaternion[0] < 0.001 &&
quaternion[0] >= 0 &&
quaternion[1] < 0.001 &&
quaternion[1] >= 0
) {
// this is a 2d rotation on the z-axis
rotationDegrees = [
0,
0,
MatrixMath.roundTo3Places(
(Math.atan2(row[0][1], row[0][0]) * 180) / Math.PI,
),
];
} else {
rotationDegrees = MatrixMath.quaternionToDegreesXYZ(
quaternion,
matrix,
row,
);
}
// expose both base data and convenience names
return {
rotationDegrees,
perspective,
quaternion,
scale,
skew,
translation,
rotate: rotationDegrees[2],
rotateX: rotationDegrees[0],
rotateY: rotationDegrees[1],
scaleX: scale[0],
scaleY: scale[1],
translateX: translation[0],
translateY: translation[1],
};
},
};
module.exports = MatrixMath;