@wemap/maths 10.10.0 → 11.0.0-alpha.0

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