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