@wemap/maths 13.0.0 → 14.6.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs DELETED
@@ -1,1156 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
- class EulerAngles {
8
- static toString([yaw, pitch, roll]) {
9
- return `[${yaw.toFixed(2)}, ${pitch.toFixed(2)}, ${roll.toFixed(2)}]`;
10
- }
11
- }
12
- class Matrix {
13
- static sum(a, b) {
14
- const aNumRows = a.length;
15
- const aNumCols = a[0].length;
16
- const m = new Array(aNumRows);
17
- for (let r = 0; r < aNumRows; ++r) {
18
- m[r] = new Array(aNumCols);
19
- for (let c = 0; c < aNumCols; ++c) {
20
- m[r][c] = a[r][c] + b[r][c];
21
- }
22
- }
23
- return m;
24
- }
25
- static concatRow(a, b) {
26
- const aNumRows = a.length;
27
- const bNumRows = b.length;
28
- const m = new Array(aNumRows + bNumRows);
29
- for (let r = 0; r < aNumRows; ++r) {
30
- m[r] = a[r].slice();
31
- }
32
- for (let r = 0; r < bNumRows; ++r) {
33
- m[aNumRows + r] = b[r].slice();
34
- }
35
- return m;
36
- }
37
- static concatLine(a, b) {
38
- const aNumRows = a.length;
39
- const m = new Array(aNumRows);
40
- for (let r = 0; r < aNumRows; ++r) {
41
- m[r] = a[r].concat(b[r]);
42
- }
43
- return m;
44
- }
45
- static diag(arr) {
46
- const m = Array(arr.length);
47
- for (let i = 0; i < arr.length; i++) {
48
- m[i] = Array(arr.length);
49
- for (let j = 0; j < arr.length; j++) {
50
- m[i][j] = i === j ? arr[i] : 0;
51
- }
52
- }
53
- return m;
54
- }
55
- static transpose(a) {
56
- const numRows = a.length;
57
- const numCols = a[0].length;
58
- const m = Array(numCols);
59
- for (let i = 0; i < numCols; i++) {
60
- m[i] = Array(numRows);
61
- for (let j = 0; j < numRows; j++) {
62
- m[i][j] = a[j][i];
63
- }
64
- }
65
- return m;
66
- }
67
- static multiply(a, b) {
68
- const aNumRows = a.length;
69
- const aNumCols = a[0].length;
70
- const bNumCols = b[0].length;
71
- const m = new Array(aNumRows);
72
- for (let r = 0; r < aNumRows; ++r) {
73
- m[r] = new Array(bNumCols);
74
- for (let c = 0; c < bNumCols; ++c) {
75
- m[r][c] = 0;
76
- for (let i = 0; i < aNumCols; ++i) {
77
- m[r][c] += a[r][i] * b[i][c];
78
- }
79
- }
80
- }
81
- return m;
82
- }
83
- static multiplyScalar(a, s) {
84
- const aNumRows = a.length;
85
- const aNumCols = a[0].length;
86
- const m = new Array(aNumRows);
87
- for (let r = 0; r < aNumRows; ++r) {
88
- m[r] = new Array(aNumCols);
89
- for (let c = 0; c < aNumCols; ++c) {
90
- m[r][c] = a[r][c] * s;
91
- }
92
- }
93
- return m;
94
- }
95
- static multiplyVector(a, v) {
96
- const aNumRows = a.length;
97
- const aNumCols = a[0].length;
98
- const output = new Array(aNumRows);
99
- for (let r = 0; r < aNumRows; ++r) {
100
- output[r] = 0;
101
- for (let c = 0; c < aNumCols; ++c) {
102
- output[r] += a[r][c] * v[c];
103
- }
104
- }
105
- return output;
106
- }
107
- // Returns the inverse of matrix M
108
- // Helped from http://blog.acipo.com/matrix-inversion-in-javascript/
109
- static inverse(M) {
110
- if (M.length !== M[0].length) {
111
- return null;
112
- }
113
- let i = 0, ii = 0, j = 0, e = 0;
114
- const dim = M.length;
115
- const I = [], C = [];
116
- for (i = 0; i < dim; i += 1) {
117
- I[I.length] = [];
118
- C[C.length] = [];
119
- for (j = 0; j < dim; j += 1) {
120
- if (i === j) {
121
- I[i][j] = 1;
122
- } else {
123
- I[i][j] = 0;
124
- }
125
- C[i][j] = M[i][j];
126
- }
127
- }
128
- for (i = 0; i < dim; i += 1) {
129
- e = C[i][i];
130
- if (e === 0) {
131
- for (ii = i + 1; ii < dim; ii += 1) {
132
- if (C[ii][i] !== 0) {
133
- for (j = 0; j < dim; j++) {
134
- e = C[i][j];
135
- C[i][j] = C[ii][j];
136
- C[ii][j] = e;
137
- e = I[i][j];
138
- I[i][j] = I[ii][j];
139
- I[ii][j] = e;
140
- }
141
- break;
142
- }
143
- }
144
- e = C[i][i];
145
- if (e === 0) {
146
- return null;
147
- }
148
- }
149
- for (j = 0; j < dim; j++) {
150
- C[i][j] = C[i][j] / e;
151
- I[i][j] = I[i][j] / e;
152
- }
153
- for (ii = 0; ii < dim; ii++) {
154
- if (ii === i) {
155
- continue;
156
- }
157
- e = C[ii][i];
158
- for (j = 0; j < dim; j++) {
159
- C[ii][j] -= e * C[i][j];
160
- I[ii][j] -= e * I[i][j];
161
- }
162
- }
163
- }
164
- return I;
165
- }
166
- }
167
- class Matrix3 {
168
- static get identity() {
169
- return [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
170
- }
171
- static get zeros() {
172
- return [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
173
- }
174
- static diag(arr) {
175
- return [
176
- [arr[0], 0, 0],
177
- [0, arr[1], 0],
178
- [0, 0, arr[2]]
179
- ];
180
- }
181
- static sum(m1, m2) {
182
- return [
183
- [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2]],
184
- [m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2]],
185
- [m1[2][0] + m2[2][0], m1[2][1] + m2[2][1], m1[2][2] + m2[2][2]]
186
- ];
187
- }
188
- static subtract(m1, m2) {
189
- return [
190
- [m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2]],
191
- [m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2]],
192
- [m1[2][0] - m2[2][0], m1[2][1] - m2[2][1], m1[2][2] - m2[2][2]]
193
- ];
194
- }
195
- static rotX(theta) {
196
- return [
197
- [1, 0, 0],
198
- [0, Math.cos(theta), -Math.sin(theta)],
199
- [0, Math.sin(theta), Math.cos(theta)]
200
- ];
201
- }
202
- static rotY(theta) {
203
- return [
204
- [Math.cos(theta), 0, Math.sin(theta)],
205
- [0, 1, 0],
206
- [-Math.sin(theta), 0, Math.cos(theta)]
207
- ];
208
- }
209
- static rotZ(theta) {
210
- return [
211
- [Math.cos(theta), -Math.sin(theta), 0],
212
- [Math.sin(theta), Math.cos(theta), 0],
213
- [0, 0, 1]
214
- ];
215
- }
216
- static transpose(m) {
217
- return [
218
- [m[0][0], m[1][0], m[2][0]],
219
- [m[0][1], m[1][1], m[2][1]],
220
- [m[0][2], m[1][2], m[2][2]]
221
- ];
222
- }
223
- static multiplyScalar(m, s) {
224
- return [
225
- [m[0][0] * s, m[0][1] * s, m[0][2] * s],
226
- [m[1][0] * s, m[1][1] * s, m[1][2] * s],
227
- [m[2][0] * s, m[2][1] * s, m[2][2] * s]
228
- ];
229
- }
230
- static multiplyVector(a, v) {
231
- const output = new Array(3);
232
- for (let r = 0; r < 3; ++r) {
233
- output[r] = 0;
234
- for (let c = 0; c < 3; ++c) {
235
- output[r] += a[r][c] * v[c];
236
- }
237
- }
238
- return output;
239
- }
240
- static multiply(a, b) {
241
- const m = new Array(3);
242
- for (let r = 0; r < 3; ++r) {
243
- m[r] = new Array(3);
244
- for (let c = 0; c < 3; ++c) {
245
- m[r][c] = 0;
246
- for (let i = 0; i < 3; ++i) {
247
- m[r][c] += a[r][i] * b[i][c];
248
- }
249
- }
250
- }
251
- return m;
252
- }
253
- static skew(v) {
254
- return [
255
- [0, -v[2], v[1]],
256
- [v[2], 0, -v[0]],
257
- [-v[1], v[0], 0]
258
- ];
259
- }
260
- // Helped from https://github.com/mrdoob/three.js/blob/34dc2478c684066257e4e39351731a93c6107ef5/src/math/Matrix3.js#L183
261
- static inverse(me) {
262
- const te = new Array(3), n11 = me[0][0], n21 = me[0][1], n31 = me[0][2], n12 = me[1][0], n22 = me[1][1], n32 = me[1][2], n13 = me[2][0], n23 = me[2][1], n33 = me[2][2], t11 = n33 * n22 - n32 * n23, t12 = n32 * n13 - n33 * n12, t13 = n23 * n12 - n22 * n13, det = n11 * t11 + n21 * t12 + n31 * t13;
263
- if (det === 0) {
264
- return null;
265
- }
266
- te[0] = new Array(3);
267
- te[1] = new Array(3);
268
- te[2] = new Array(3);
269
- const detInv = 1 / det;
270
- te[0][0] = t11 * detInv;
271
- te[0][1] = (n31 * n23 - n33 * n21) * detInv;
272
- te[0][2] = (n32 * n21 - n31 * n22) * detInv;
273
- te[1][0] = t12 * detInv;
274
- te[1][1] = (n33 * n11 - n31 * n13) * detInv;
275
- te[1][2] = (n31 * n12 - n32 * n11) * detInv;
276
- te[2][0] = t13 * detInv;
277
- te[2][1] = (n21 * n13 - n23 * n11) * detInv;
278
- te[2][2] = (n22 * n11 - n21 * n12) * detInv;
279
- return te;
280
- }
281
- static determinant(m) {
282
- const a = m[0][0], b = m[1][0], c = m[2][0], d = m[0][1], e = m[1][1], f = m[2][1], g = m[0][2], h = m[1][2], i = m[2][2];
283
- return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
284
- }
285
- static trace(m) {
286
- return m[0][0] + m[1][1] + m[2][2];
287
- }
288
- }
289
- class Vector {
290
- static concat(v1, v2) {
291
- return v1.concat(v2);
292
- }
293
- }
294
- class Vector3 extends Vector {
295
- static norm(vector) {
296
- return Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
297
- }
298
- static normalize(vector) {
299
- const norm = this.norm(vector);
300
- const output = new Array(3);
301
- for (let i = 0; i < 3; i++) {
302
- output[i] = vector[i] / norm;
303
- }
304
- return output;
305
- }
306
- static sum(v1, v2) {
307
- return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
308
- }
309
- static subtract(v1, v2) {
310
- return [v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]];
311
- }
312
- static distance(v1, v2) {
313
- return Vector3.norm(Vector3.subtract(v1, v2));
314
- }
315
- static cross(v1, v2) {
316
- return [
317
- v1[1] * v2[2] - v1[2] * v2[1],
318
- v1[2] * v2[0] - v1[0] * v2[2],
319
- v1[0] * v2[1] - v1[1] * v2[0]
320
- ];
321
- }
322
- static dot(v1, v2) {
323
- return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
324
- }
325
- static multiplyScalar(vector, scalar) {
326
- const output = new Array(3);
327
- for (let i = 0; i < 3; i++) {
328
- output[i] = vector[i] * scalar;
329
- }
330
- return output;
331
- }
332
- }
333
- class Matrix4 {
334
- static get identity() {
335
- return [
336
- [1, 0, 0, 0],
337
- [0, 1, 0, 0],
338
- [0, 0, 1, 0],
339
- [0, 0, 0, 1]
340
- ];
341
- }
342
- static clone(mat) {
343
- return [
344
- mat[0].slice(0),
345
- mat[1].slice(0),
346
- mat[2].slice(0),
347
- mat[3].slice(0)
348
- ];
349
- }
350
- static sum(m1, m2) {
351
- return [
352
- [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2], m1[0][3] + m2[0][3]],
353
- [m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2], m1[1][3] + m2[1][3]],
354
- [m1[2][0] + m2[2][0], m1[2][1] + m2[2][1], m1[2][2] + m2[2][2], m1[2][3] + m2[2][3]],
355
- [m1[3][0] + m2[3][0], m1[3][1] + m2[3][1], m1[3][2] + m2[3][2], m1[3][3] + m2[3][3]]
356
- ];
357
- }
358
- static subtract(m1, m2) {
359
- return [
360
- [m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2], m1[0][3] - m2[0][3]],
361
- [m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2], m1[1][3] - m2[1][3]],
362
- [m1[2][0] - m2[2][0], m1[2][1] - m2[2][1], m1[2][2] - m2[2][2], m1[2][3] - m2[2][3]],
363
- [m1[3][0] - m2[3][0], m1[3][1] - m2[3][1], m1[3][2] - m2[3][2], m1[3][3] - m2[3][3]]
364
- ];
365
- }
366
- static fromMatrix3(m) {
367
- return [
368
- [m[0][0], m[0][1], m[0][2], 0],
369
- [m[1][0], m[1][1], m[1][2], 0],
370
- [m[2][0], m[2][1], m[2][2], 0],
371
- [0, 0, 0, 1]
372
- ];
373
- }
374
- static fromDiagVector(v) {
375
- return [
376
- [v[0], 0, 0, 0],
377
- [0, v[1], 0, 0],
378
- [0, 0, v[2], 0],
379
- [0, 0, 0, v[3]]
380
- ];
381
- }
382
- /**
383
- * Column-Major ordering like Eigen and ThreeJS
384
- */
385
- static fromVector(v) {
386
- return [
387
- [v[0], v[4], v[8], v[12]],
388
- [v[1], v[5], v[9], v[13]],
389
- [v[2], v[6], v[10], v[14]],
390
- [v[3], v[7], v[11], v[15]]
391
- ];
392
- }
393
- /**
394
- * Column-Major ordering like Eigen and ThreeJS
395
- */
396
- static toVector(m) {
397
- return [
398
- m[0][0],
399
- m[1][0],
400
- m[2][0],
401
- m[3][0],
402
- m[0][1],
403
- m[1][1],
404
- m[2][1],
405
- m[3][1],
406
- m[0][2],
407
- m[1][2],
408
- m[2][2],
409
- m[3][2],
410
- m[0][3],
411
- m[1][3],
412
- m[2][3],
413
- m[3][3]
414
- ];
415
- }
416
- static multiply(a, b) {
417
- const m = new Array(4);
418
- for (let r = 0; r < 4; ++r) {
419
- m[r] = new Array(4);
420
- for (let c = 0; c < 4; ++c) {
421
- m[r][c] = 0;
422
- for (let i = 0; i < 4; ++i) {
423
- m[r][c] += a[r][i] * b[i][c];
424
- }
425
- }
426
- }
427
- return m;
428
- }
429
- static multiplyScalar(a, s) {
430
- const output = new Array(4);
431
- for (let r = 0; r < 4; ++r) {
432
- output[r] = new Array(4);
433
- for (let c = 0; c < 4; ++c) {
434
- output[r][c] = a[r][c] * s;
435
- }
436
- }
437
- return output;
438
- }
439
- static multiplyVector(a, v) {
440
- const output = new Array(4);
441
- for (let r = 0; r < 4; ++r) {
442
- output[r] = 0;
443
- for (let c = 0; c < 4; ++c) {
444
- output[r] += a[r][c] * v[c];
445
- }
446
- }
447
- return output;
448
- }
449
- static multiplyVector3(a, v) {
450
- return Matrix4.multiplyVector(a, [...v, 1]).slice(0, 3);
451
- }
452
- static transpose(m) {
453
- return [
454
- [m[0][0], m[1][0], m[2][0], m[3][0]],
455
- [m[0][1], m[1][1], m[2][1], m[3][1]],
456
- [m[0][2], m[1][2], m[2][2], m[3][2]],
457
- [m[0][3], m[1][3], m[2][3], m[3][3]]
458
- ];
459
- }
460
- static invert(m) {
461
- const te = Matrix4.toVector(m), n11 = te[0], n21 = te[1], n31 = te[2], n41 = te[3], n12 = te[4], n22 = te[5], n32 = te[6], n42 = te[7], n13 = te[8], n23 = te[9], n33 = te[10], n43 = te[11], n14 = te[12], n24 = te[13], n34 = te[14], n44 = te[15], t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44, t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44, t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44, t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
462
- const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
463
- if (det === 0) {
464
- return null;
465
- }
466
- const detInv = 1 / det;
467
- te[0] = t11 * detInv;
468
- te[1] = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv;
469
- te[2] = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv;
470
- te[3] = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv;
471
- te[4] = t12 * detInv;
472
- te[5] = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv;
473
- te[6] = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv;
474
- te[7] = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv;
475
- te[8] = t13 * detInv;
476
- te[9] = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv;
477
- te[10] = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv;
478
- te[11] = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv;
479
- te[12] = t14 * detInv;
480
- te[13] = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv;
481
- te[14] = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv;
482
- te[15] = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv;
483
- return Matrix4.fromVector(te);
484
- }
485
- static determinant(m) {
486
- const n11 = m[0][0], n12 = m[1][0], n13 = m[2][0], n14 = m[3][0];
487
- const n21 = m[0][1], n22 = m[1][1], n23 = m[2][1], n24 = m[3][1];
488
- const n31 = m[0][2], n32 = m[1][2], n33 = m[2][2], n34 = m[3][2];
489
- const n41 = m[0][3], n42 = m[1][3], n43 = m[2][3], n44 = m[3][3];
490
- return n41 * (Number(n14) * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34) + n42 * (Number(n11) * n23 * n34 - n11 * n24 * n33 + n14 * n21 * n33 - n13 * n21 * n34 + n13 * n24 * n31 - n14 * n23 * n31) + n43 * (Number(n11) * n24 * n32 - n11 * n22 * n34 - n14 * n21 * n32 + n12 * n21 * n34 + n14 * n22 * n31 - n12 * n24 * n31) + n44 * (-n13 * n22 * n31 - n11 * n23 * n32 + n11 * n22 * n33 + n13 * n21 * n32 - n12 * n21 * n33 + n12 * n23 * n31);
491
- }
492
- static compose(translation, rotation, scale = [1, 1, 1]) {
493
- const [sx, sy, sz] = scale;
494
- return [
495
- [rotation[0][0] * sx, rotation[0][1] * sy, rotation[0][2] * sz, translation[0]],
496
- [rotation[1][0] * sx, rotation[1][1] * sy, rotation[1][2] * sz, translation[1]],
497
- [rotation[2][0] * sx, rotation[2][1] * sy, rotation[2][2] * sz, translation[2]],
498
- [0, 0, 0, 1]
499
- ];
500
- }
501
- static decompose(m) {
502
- const scale = [
503
- Vector3.norm([m[0][0], m[1][0], m[2][0]]),
504
- Vector3.norm([m[0][1], m[1][1], m[2][1]]),
505
- Vector3.norm([m[0][2], m[1][2], m[2][2]])
506
- ];
507
- const det = Matrix4.determinant(m);
508
- if (det < 0) {
509
- scale[0] = -scale[0];
510
- }
511
- const translation = [m[0][3], m[1][3], m[2][3]];
512
- const invSX = 1 / scale[0];
513
- const invSY = 1 / scale[1];
514
- const invSZ = 1 / scale[2];
515
- const rotation = [
516
- [m[0][0] * invSX, m[0][1] * invSY, m[0][2] * invSZ],
517
- [m[1][0] * invSX, m[1][1] * invSY, m[1][2] * invSZ],
518
- [m[2][0] * invSX, m[2][1] * invSY, m[2][2] * invSZ]
519
- ];
520
- return { translation, scale, rotation };
521
- }
522
- static extractRotation(m) {
523
- const scaleX = 1 / Vector3.norm([m[0][0], m[1][0], m[2][0]]);
524
- const scaleY = 1 / Vector3.norm([m[0][1], m[1][1], m[2][1]]);
525
- const scaleZ = 1 / Vector3.norm([m[0][2], m[1][2], m[2][2]]);
526
- return [
527
- [m[0][0] * scaleX, m[0][1] * scaleY, m[0][2] * scaleZ],
528
- [m[1][0] * scaleX, m[1][1] * scaleY, m[1][2] * scaleZ],
529
- [m[2][0] * scaleX, m[2][1] * scaleY, m[2][2] * scaleZ]
530
- ];
531
- }
532
- static extractTranslation(m) {
533
- return [m[0][3], m[1][3], m[2][3]];
534
- }
535
- static scaleTranslation(m, s) {
536
- return [
537
- [m[0][0], m[0][1], m[0][2], m[0][3] * s[0]],
538
- [m[1][0], m[1][1], m[1][2], m[1][3] * s[1]],
539
- [m[2][0], m[2][1], m[2][2], m[2][3] * s[2]],
540
- [m[3][0], m[3][1], m[3][2], m[3][3]]
541
- ];
542
- }
543
- static setTranslation(m, v) {
544
- m[0][3] = v[0];
545
- m[1][3] = v[1];
546
- m[2][3] = v[2];
547
- return m;
548
- }
549
- static toString(m) {
550
- return "[\n" + m.map(
551
- (row) => " [" + row.map((val) => val.toFixed(2)).join(", ") + "]"
552
- ).join(",\n") + "\n]";
553
- }
554
- }
555
- class Point2 {
556
- /**
557
- * Distance from a point to a segment
558
- * @param {Number[2]} p point
559
- * @param {Number[2]} v segment point 1
560
- * @param {Number[2]} w segment point 2
561
- */
562
- static distanceToSegment(p, v, w) {
563
- const A = p[0] - v[0];
564
- const B = p[1] - v[1];
565
- const C = w[0] - v[0];
566
- const D = w[1] - v[1];
567
- const dot = A * C + B * D;
568
- const lenSq = C * C + D * D;
569
- let param = -1;
570
- if (lenSq !== 0) {
571
- param = dot / lenSq;
572
- }
573
- let xx;
574
- let yy;
575
- if (param < 0) {
576
- xx = v[0];
577
- yy = v[1];
578
- } else if (param > 1) {
579
- xx = w[0];
580
- yy = w[1];
581
- } else {
582
- xx = v[0] + param * C;
583
- yy = v[1] + param * D;
584
- }
585
- const dx = p[0] - xx;
586
- const dy = p[1] - yy;
587
- return Math.sqrt(dx * dx + dy * dy);
588
- }
589
- /**
590
- * Distance between two points
591
- * @param {Number[2]} p1
592
- * @param {Number[2]} p2
593
- */
594
- static distanceToPoint(p1, p2) {
595
- return Math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2);
596
- }
597
- }
598
- class Polygon {
599
- // https://github.com/substack/point-in-polygon/blob/master/index.js
600
- static isPointInside(polygon, point) {
601
- const x = point[0], y = point[1];
602
- let inside = false;
603
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
604
- const xi = polygon[i][0], yi = polygon[i][1];
605
- const xj = polygon[j][0], yj = polygon[j][1];
606
- const intersect = yi > y !== yj > y && x <= (xj - xi) * (y - yi) / (yj - yi) + xi;
607
- if (intersect) {
608
- inside = !inside;
609
- }
610
- }
611
- return inside;
612
- }
613
- static distanceToOtherRectangle(poly1, poly2) {
614
- let minDist = Number.MAX_VALUE;
615
- for (const point of poly1) {
616
- if (Polygon.isPointInside(poly2, point)) {
617
- return 0;
618
- }
619
- minDist = Math.min(minDist, Polygon.distanceToPoint(poly2, point));
620
- }
621
- for (const point of poly2) {
622
- if (Polygon.isPointInside(poly1, point)) {
623
- return 0;
624
- }
625
- minDist = Math.min(minDist, Polygon.distanceToPoint(poly1, point));
626
- }
627
- return minDist;
628
- }
629
- static distanceToPoint(polygon, point) {
630
- let minDist = Number.MAX_VALUE;
631
- polygon.forEach((segPoint1, idx) => {
632
- const segPoint2 = polygon[(idx + 1) % polygon.length];
633
- minDist = Math.min(
634
- minDist,
635
- Point2.distanceToSegment(point, segPoint1, segPoint2)
636
- );
637
- });
638
- return minDist;
639
- }
640
- }
641
- const _Quaternion = class _Quaternion {
642
- static get identity() {
643
- return [1, 0, 0, 0];
644
- }
645
- static sum(q1, q2) {
646
- return [q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]];
647
- }
648
- // From https://www.mathworks.com/help/aerotbx/ug/quatrotate.html
649
- static rotateMatlab(q, v) {
650
- const [qw, qx, qy, qz] = q;
651
- const [vx, vy, vz] = v;
652
- return [
653
- qw * (qw * vx - qy * vz + qz * vy) - qy * (qw * vz - qx * vy + qy * vx) + qx * (qx * vx + qy * vy + qz * vz) + qz * (qw * vy + qx * vz - qz * vx),
654
- qw * (qw * vy + qx * vz - qz * vx) + qx * (qw * vz - qx * vy + qy * vx) + qy * (qx * vx + qy * vy + qz * vz) - qz * (qw * vx - qy * vz + qz * vy),
655
- qw * (qw * vz - qx * vy + qy * vx) - qx * (qw * vy + qx * vz - qz * vx) + qy * (qw * vx - qy * vz + qz * vy) + qz * (qx * vx + qy * vy + qz * vz)
656
- ];
657
- }
658
- // From: https://eigen.tuxfamily.org/dox/Quaternion_8h_source.html
659
- static rotate(q, v) {
660
- const [qw, qx, qy, qz] = q;
661
- const thisVec = [qx, qy, qz];
662
- let uv = Vector3.cross(thisVec, v);
663
- uv = Vector3.sum(uv, uv);
664
- const tmp1 = Vector3.sum(v, Vector3.multiplyScalar(uv, qw));
665
- return Vector3.sum(tmp1, Vector3.cross(thisVec, uv));
666
- }
667
- /**
668
- * Unit Quaternion inverse
669
- * @param {Array.<Number>} quaternion must be a unit quaternion.
670
- */
671
- static inverse(q) {
672
- return [-q[0], q[1], q[2], q[3]];
673
- }
674
- /**
675
- * Multiply quaternions
676
- * @param {...Number[]} quaternions
677
- * @returns {Number[]}
678
- */
679
- static multiply(...quaternions) {
680
- if (quaternions.length === 2) {
681
- const [qw, qx, qy, qz] = quaternions[0];
682
- const [rw, rx, ry, rz] = quaternions[1];
683
- return [
684
- qw * rw - qx * rx - qy * ry - qz * rz,
685
- qw * rx + rw * qx + qy * rz - qz * ry,
686
- qw * ry + rw * qy + qz * rx - qx * rz,
687
- qw * rz + rw * qz + qx * ry - qy * rx
688
- ];
689
- }
690
- return quaternions.reduce((acc, quat) => this.multiply(acc, quat), _Quaternion.identity);
691
- }
692
- /**
693
- * Axis-angle to quaternion
694
- * @param {Array[Number]} axis unit vector
695
- * @param {Number} angle in radians
696
- */
697
- static fromAxisAngle(axis, angle) {
698
- const s = Math.sin(angle / 2);
699
- return [Math.cos(angle / 2), axis[0] * s, axis[1] * s, axis[2] * s];
700
- }
701
- /**
702
- * Rotation around x-axis
703
- * @param {Number} angle in radians
704
- */
705
- static rotX(angle) {
706
- return this.fromAxisAngle([1, 0, 0], angle);
707
- }
708
- /**
709
- * Rotation around x-axis
710
- * @param {Number} angle in radians
711
- */
712
- static rotY(angle) {
713
- return this.fromAxisAngle([0, 1, 0], angle);
714
- }
715
- /**
716
- * Rotation around x-axis
717
- * @param {Number} angle in radians
718
- */
719
- static rotZ(angle) {
720
- return this.fromAxisAngle([0, 0, 1], angle);
721
- }
722
- static fromMatrix3(m) {
723
- const qw = Math.sqrt(1 + m[0][0] + m[1][1] + m[2][2]) / 2;
724
- const qx = (m[2][1] - m[1][2]) / (4 * qw);
725
- const qy = (m[0][2] - m[2][0]) / (4 * qw);
726
- const qz = (m[1][0] - m[0][1]) / (4 * qw);
727
- return [qw, qx, qy, qz];
728
- }
729
- static fromMatrix3Matlab(m) {
730
- const qw = Math.sqrt(1 + m[0][0] + m[1][1] + m[2][2]) / 2;
731
- const qx = (m[1][2] - m[2][1]) / (4 * qw);
732
- const qy = (m[2][0] - m[0][2]) / (4 * qw);
733
- const qz = (m[0][1] - m[1][0]) / (4 * qw);
734
- return [qw, qx, qy, qz];
735
- }
736
- static toMatrix3(q) {
737
- const [w, x, y, z] = q;
738
- const wx = w * x, wy = w * y, wz = w * z;
739
- const xx = x * x, xy = x * y, xz = x * z;
740
- const yy = y * y, yz = y * z, zz = z * z;
741
- return [
742
- [1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy)],
743
- [2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx)],
744
- [2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)]
745
- ];
746
- }
747
- static wxyz2xyzw(wxyz) {
748
- return [wxyz[1], wxyz[2], wxyz[3], wxyz[0]];
749
- }
750
- static xyzw2wxyz(xyzw) {
751
- return [xyzw[3], xyzw[0], xyzw[1], xyzw[2]];
752
- }
753
- static distance(q1, q2) {
754
- return Math.acos(Math.min(2 * _Quaternion.dot(q1, q2) ** 2 - 1, 1));
755
- }
756
- static equals(q1, q2) {
757
- return _Quaternion.distance(q1, q2) < 1e-8;
758
- }
759
- static normalize(quaternion) {
760
- const norm = this.norm(quaternion);
761
- const output = new Array(4);
762
- for (let i = 0; i < 4; i++) {
763
- output[i] = quaternion[i] / norm;
764
- }
765
- return output;
766
- }
767
- static norm(quaternion) {
768
- return Math.sqrt(quaternion[0] ** 2 + quaternion[1] ** 2 + quaternion[2] ** 2 + quaternion[3] ** 2);
769
- }
770
- static dot(q1, q2) {
771
- return q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3];
772
- }
773
- static getRotationBetweenTwoVectors(from, to) {
774
- const fromNormalized = Vector3.normalize(from);
775
- const toNormalized = Vector3.normalize(to);
776
- const dot = Vector3.dot(fromNormalized, toNormalized);
777
- let tmp;
778
- if (dot < -0.999999) {
779
- tmp = Vector3.cross([1, 0, 0], fromNormalized);
780
- if (Vector3.norm(tmp) < 1e-6) {
781
- tmp = Vector3.cross([0, 1, 0], fromNormalized);
782
- }
783
- tmp = Vector3.normalize(tmp);
784
- return _Quaternion.fromAxisAngle(tmp, Math.PI);
785
- } else if (dot > 0.999999) {
786
- return _Quaternion.identity;
787
- }
788
- tmp = Vector3.cross(fromNormalized, toNormalized);
789
- const quat = [1 + dot, tmp[0], tmp[1], tmp[2]];
790
- return _Quaternion.normalize(quat);
791
- }
792
- // https://github.com/stackgl/gl-quat/blob/master/slerp.js
793
- /**
794
- * Performs a spherical linear interpolation between two quat
795
- *
796
- * @param {number[]} a the first operand
797
- * @param {number[]} b the second operand
798
- * @param {number} t interpolation amount between the two inputs
799
- * @returns {number[]} out
800
- */
801
- static slerp(a, b, t) {
802
- const ax = a[1], ay = a[2], az = a[3], aw = a[0];
803
- let bx = b[1], by = b[2], bz = b[3], bw = b[0];
804
- let cosom, scale0, scale1;
805
- cosom = ax * bx + ay * by + az * bz + aw * bw;
806
- if (cosom < 0) {
807
- cosom = -cosom;
808
- bw = -bw;
809
- bx = -bx;
810
- by = -by;
811
- bz = -bz;
812
- }
813
- const useLinearInterp = 1 - cosom < 1e-6;
814
- if (!useLinearInterp) {
815
- const omega = Math.acos(cosom);
816
- const sinom = Math.sin(omega);
817
- scale0 = Math.sin((1 - t) * omega) / sinom;
818
- scale1 = Math.sin(t * omega) / sinom;
819
- } else {
820
- scale0 = 1 - t;
821
- scale1 = t;
822
- }
823
- let output = [
824
- scale0 * aw + scale1 * bw,
825
- scale0 * ax + scale1 * bx,
826
- scale0 * ay + scale1 * by,
827
- scale0 * az + scale1 * bz
828
- ];
829
- if (useLinearInterp) {
830
- output = _Quaternion.normalize(output);
831
- }
832
- return output;
833
- }
834
- static toString(q) {
835
- return `[${q[0].toFixed(2)}, ${q[1].toFixed(2)}, ${q[2].toFixed(2)}, ${q[3].toFixed(2)}]`;
836
- }
837
- static fromUnitVectors(vFrom, vTo) {
838
- let r = Vector3.dot(vFrom, vTo) + 1;
839
- let x, y, z, w;
840
- if (r < Number.EPSILON) {
841
- r = 0;
842
- if (Math.abs(vFrom[1]) > Math.abs(vFrom[2])) {
843
- x = -vFrom[1];
844
- y = vFrom[0];
845
- z = 0;
846
- w = r;
847
- } else {
848
- x = 0;
849
- y = -vFrom[2];
850
- z = vFrom[1];
851
- w = r;
852
- }
853
- } else {
854
- x = vFrom[1] * vTo[2] - vFrom[2] * vTo[1];
855
- y = vFrom[2] * vTo[0] - vFrom[0] * vTo[2];
856
- z = vFrom[0] * vTo[1] - vFrom[1] * vTo[0];
857
- w = r;
858
- }
859
- const quat = [w, x, y, z];
860
- return _Quaternion.normalize(quat);
861
- }
862
- };
863
- __publicField(_Quaternion, "ROTX_PI2", _Quaternion.rotX(Math.PI / 2));
864
- __publicField(_Quaternion, "ROTX_PI", _Quaternion.rotX(Math.PI));
865
- __publicField(_Quaternion, "ROTX_MPI2", _Quaternion.rotX(-Math.PI / 2));
866
- __publicField(_Quaternion, "ROTY_PI2", _Quaternion.rotY(Math.PI / 2));
867
- __publicField(_Quaternion, "ROTY_PI", _Quaternion.rotY(Math.PI));
868
- __publicField(_Quaternion, "ROTY_MPI2", _Quaternion.rotY(-Math.PI / 2));
869
- __publicField(_Quaternion, "ROTZ_PI2", _Quaternion.rotZ(Math.PI / 2));
870
- __publicField(_Quaternion, "ROTZ_PI", _Quaternion.rotZ(Math.PI));
871
- __publicField(_Quaternion, "ROTZ_MPI2", _Quaternion.rotZ(-Math.PI / 2));
872
- let Quaternion = _Quaternion;
873
- function deg2rad(angle) {
874
- return angle * (Math.PI / 180);
875
- }
876
- function rad2deg(rad) {
877
- return rad * 180 / Math.PI;
878
- }
879
- function diffAngle(angle1, angle2) {
880
- return Math.atan2(Math.sin(angle1 - angle2), Math.cos(angle1 - angle2));
881
- }
882
- function diffAngleDegrees(angle1, angle2) {
883
- return rad2deg(diffAngle(deg2rad(angle1), deg2rad(angle2)));
884
- }
885
- function diffAngleLines(angle1, angle2) {
886
- let _diffAngle = Math.abs(diffAngle(angle1, angle2));
887
- if (_diffAngle > Math.PI / 2) {
888
- _diffAngle = Math.PI - _diffAngle;
889
- }
890
- return _diffAngle;
891
- }
892
- function positiveMod(n, m) {
893
- return (n % m + m) % m;
894
- }
895
- function wrap(n, min, max) {
896
- const d = max - min;
897
- const w = ((n - min) % d + d) % d + min;
898
- return w === min ? max : w;
899
- }
900
- function average(arr) {
901
- return arr.reduce((a, b) => a + b) / arr.length;
902
- }
903
- function std(arr) {
904
- const n = arr.length;
905
- const mean = arr.reduce((a, b) => a + b) / n;
906
- return Math.sqrt(arr.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n);
907
- }
908
- function roundFactor(num, factor) {
909
- return Math.round(num / factor) * factor;
910
- }
911
- class Rotations {
912
- // ZXY because yaw is around Z axis, pitch around X axis and roll around Y axis.
913
- // They are conventions for a smartphone frame.
914
- static eulerToQuaternionZXYDegrees(eulerAnglesDegrees) {
915
- const eulerAngles = eulerAnglesDegrees.map((x) => deg2rad(x));
916
- return Rotations.eulerToQuaternionZXY(eulerAngles);
917
- }
918
- static eulerToQuaternionZXY(eulerAngles) {
919
- const yaw = eulerAngles[0];
920
- const pitch = eulerAngles[1];
921
- const roll = eulerAngles[2];
922
- const c1 = Math.cos(yaw / 2);
923
- const c2 = Math.cos(pitch / 2);
924
- const c3 = Math.cos(roll / 2);
925
- const s1 = Math.sin(yaw / 2);
926
- const s2 = Math.sin(pitch / 2);
927
- const s3 = Math.sin(roll / 2);
928
- let w = c1 * c2 * c3 - s1 * s2 * s3;
929
- let x = c1 * s2 * c3 - s1 * c2 * s3;
930
- let y = s1 * s2 * c3 + c1 * c2 * s3;
931
- let z = s1 * c2 * c3 + c1 * s2 * s3;
932
- if (w < 0) {
933
- w = -w;
934
- x = -x;
935
- y = -y;
936
- z = -z;
937
- }
938
- return [w, x, y, z];
939
- }
940
- static quaternionToEulerZXYDegrees(quaternion) {
941
- const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
942
- return eulerAngles.map((x) => rad2deg(x));
943
- }
944
- static quaternionToEulerZXY(quaternion) {
945
- const qw = quaternion[0];
946
- const qx = quaternion[1];
947
- const qy = quaternion[2];
948
- const qz = quaternion[3];
949
- let alpha, beta, gamma;
950
- const test = qy * qz + qx * qw;
951
- if (test > 0.499) {
952
- alpha = 2 * Math.atan2(qy, qw);
953
- beta = Math.PI / 2;
954
- gamma = 0;
955
- } else if (test < -0.499) {
956
- alpha = -2 * Math.atan2(qy, qw);
957
- beta = -Math.PI / 2;
958
- gamma = 0;
959
- } else {
960
- alpha = Math.atan2(
961
- 2 * qz * qw - 2 * qy * qx,
962
- 1 - 2 * Math.pow(qz, 2) - 2 * Math.pow(qx, 2)
963
- );
964
- beta = Math.asin(2 * test);
965
- gamma = Math.atan2(
966
- 2 * qy * qw - 2 * qz * qx,
967
- 1 - 2 * Math.pow(qy, 2) - 2 * Math.pow(qx, 2)
968
- );
969
- }
970
- alpha = alpha % (2 * Math.PI);
971
- beta = beta % Math.PI;
972
- gamma = gamma % (2 * Math.PI);
973
- return [alpha, beta, gamma];
974
- }
975
- static quaternionToEulerDegrees(quaternion, order) {
976
- const eulerAngles = Rotations.quaternionToEuler(quaternion, order);
977
- return eulerAngles.map((x) => rad2deg(x));
978
- }
979
- // https://searchcode.com/codesearch/view/55080466/
980
- static quaternionToEuler(quaternion, order) {
981
- const qw = quaternion[0];
982
- const qx = quaternion[1];
983
- const qy = quaternion[2];
984
- const qz = quaternion[3];
985
- switch (order.toLowerCase()) {
986
- case "zyx":
987
- return Rotations._threeaxisrot(
988
- 2 * (qx * qy + qw * qz),
989
- qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
990
- -2 * (qx * qz - qw * qy),
991
- 2 * (qy * qz + qw * qx),
992
- qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2
993
- );
994
- case "zyz":
995
- return Rotations._twoaxisrot(
996
- 2 * (qy * qz - qw * qx),
997
- 2 * (qx * qz + qw * qy),
998
- qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
999
- 2 * (qy * qz + qw * qx),
1000
- -2 * (qx * qz - qw * qy)
1001
- );
1002
- case "zxy":
1003
- return Rotations._threeaxisrot(
1004
- -2 * (qx * qy - qw * qz),
1005
- qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
1006
- 2 * (qy * qz + qw * qx),
1007
- -2 * (qx * qz - qw * qy),
1008
- qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2
1009
- );
1010
- case "zxz":
1011
- return Rotations._twoaxisrot(
1012
- 2 * (qx * qz + qw * qy),
1013
- -2 * (qy * qz - qw * qx),
1014
- qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
1015
- 2 * (qx * qz - qw * qy),
1016
- 2 * (qy * qz + qw * qx)
1017
- );
1018
- case "yxz":
1019
- return Rotations._threeaxisrot(
1020
- 2 * (qx * qz + qw * qy),
1021
- qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
1022
- -2 * (qy * qz - qw * qx),
1023
- 2 * (qx * qy + qw * qz),
1024
- qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2
1025
- );
1026
- case "yxy":
1027
- return Rotations._twoaxisrot(
1028
- 2 * (qx * qy - qw * qz),
1029
- 2 * (qy * qz + qw * qx),
1030
- qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
1031
- 2 * (qx * qy + qw * qz),
1032
- -2 * (qy * qz - qw * qx)
1033
- );
1034
- case "yzx":
1035
- return Rotations._threeaxisrot(
1036
- -2 * (qx * qz - qw * qy),
1037
- qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
1038
- 2 * (qx * qy + qw * qz),
1039
- -2 * (qy * qz - qw * qx),
1040
- qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2
1041
- );
1042
- case "yzy":
1043
- return Rotations._twoaxisrot(
1044
- 2 * (qy * qz + qw * qx),
1045
- -2 * (qx * qy - qw * qz),
1046
- qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
1047
- 2 * (qy * qz - qw * qx),
1048
- 2 * (qx * qy + qw * qz)
1049
- );
1050
- case "xyz":
1051
- return Rotations._threeaxisrot(
1052
- -2 * (qy * qz - qw * qx),
1053
- qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
1054
- 2 * (qx * qz + qw * qy),
1055
- -2 * (qx * qy - qw * qz),
1056
- qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2
1057
- );
1058
- case "xyx":
1059
- return Rotations._twoaxisrot(
1060
- 2 * (qx * qy + qw * qz),
1061
- -2 * (qx * qz - qw * qy),
1062
- qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
1063
- 2 * (qx * qy - qw * qz),
1064
- 2 * (qx * qz + qw * qy)
1065
- );
1066
- case "xzy":
1067
- return Rotations._threeaxisrot(
1068
- 2 * (qy * qz + qw * qx),
1069
- qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
1070
- -2 * (qx * qy - qw * qz),
1071
- 2 * (qx * qz + qw * qy),
1072
- qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2
1073
- );
1074
- case "xzx":
1075
- return Rotations._twoaxisrot(
1076
- 2 * (qx * qz - qw * qy),
1077
- 2 * (qx * qy + qw * qz),
1078
- qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
1079
- 2 * (qx * qz + qw * qy),
1080
- -2 * (qx * qy - qw * qz)
1081
- );
1082
- }
1083
- throw new Error("unknown order");
1084
- }
1085
- static _threeaxisrot(r11, r12, r21, r31, r32) {
1086
- const r1 = Math.atan2(r11, r12);
1087
- const r2 = Math.asin(r21);
1088
- const r3 = Math.atan2(r31, r32);
1089
- return [r1, r2, r3];
1090
- }
1091
- static _twoaxisrot(r11, r12, r21, r31, r32) {
1092
- const r1 = Math.atan2(r11, r12);
1093
- const r2 = Math.acos(r21);
1094
- const r3 = Math.atan2(r31, r32);
1095
- return [r1, r2, r3];
1096
- }
1097
- static getHeadingFromQuaternion(quaternion) {
1098
- const [qw, qx, qy, qz] = quaternion;
1099
- const qwqx = qw * qx;
1100
- const qyqz = qy * qz;
1101
- const qxqx = qx ** 2;
1102
- if (Math.asin(2 * (qwqx + qyqz)) < Math.PI / 4 && qxqx + qy ** 2 < 0.5) {
1103
- return -Math.atan2(qw * qz - qx * qy, 0.5 - qxqx - qz ** 2);
1104
- }
1105
- return -Math.atan2(qx * qz + qw * qy, qwqx - qyqz);
1106
- }
1107
- }
1108
- function linesIntersect(line1, line2, detectIntersectionAtBounds = true) {
1109
- const [[p0x, p0y], [p1x, p1y]] = line1;
1110
- const [[p2x, p2y], [p3x, p3y]] = line2;
1111
- const s1x = p1x - p0x;
1112
- const s1y = p1y - p0y;
1113
- const s2x = p3x - p2x;
1114
- const s2y = p3y - p2y;
1115
- const s = (-s1y * (p0x - p2x) + s1x * (p0y - p2y)) / (-s2x * s1y + s1x * s2y);
1116
- const t = (s2x * (p0y - p2y) - s2y * (p0x - p2x)) / (-s2x * s1y + s1x * s2y);
1117
- if (!detectIntersectionAtBounds) {
1118
- return s > 0 && s < 1 && t > 0 && t < 1;
1119
- }
1120
- return s >= 0 && s <= 1 && t >= 0 && t <= 1;
1121
- }
1122
- function isPolygonClockwise(polygon) {
1123
- let sum = 0;
1124
- for (let i = 0; i < polygon.length; i++) {
1125
- const p1 = polygon[i];
1126
- const p2 = polygon[(i + 1) % polygon.length];
1127
- const edge = (p2[0] - p1[0]) * (p2[1] + p1[1]);
1128
- sum += edge;
1129
- }
1130
- return sum > 0;
1131
- }
1132
- export {
1133
- EulerAngles,
1134
- Matrix,
1135
- Matrix3,
1136
- Matrix4,
1137
- Point2,
1138
- Polygon,
1139
- Quaternion,
1140
- Rotations,
1141
- Vector,
1142
- Vector3,
1143
- average,
1144
- deg2rad,
1145
- diffAngle,
1146
- diffAngleDegrees,
1147
- diffAngleLines,
1148
- isPolygonClockwise,
1149
- linesIntersect,
1150
- positiveMod,
1151
- rad2deg,
1152
- roundFactor,
1153
- std,
1154
- wrap
1155
- };
1156
- //# sourceMappingURL=index.mjs.map