@wemap/maths 10.9.2 → 11.0.0-11.0.0-alpha.11.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.
- package/{index.js → index.ts} +2 -1
- package/package.json +14 -5
- package/src/EulerAngles.ts +10 -0
- package/src/{Geometry.js → Geometry.ts} +10 -14
- package/src/{Matrix.js → Matrix.ts} +17 -12
- package/src/{Matrix3.spec.js → Matrix3.spec.ts} +13 -7
- package/src/{Matrix3.js → Matrix3.ts} +46 -21
- package/src/Matrix4.spec.ts +187 -0
- package/src/Matrix4.ts +302 -0
- package/src/{Point2.js → Point2.ts} +4 -2
- package/src/{Polygon.spec.js → Polygon.spec.ts} +3 -2
- package/src/{Polygon.js → Polygon.ts} +4 -3
- package/src/{Quaternion.spec.js → Quaternion.spec.ts} +20 -57
- package/src/{Quaternion.js → Quaternion.ts} +32 -31
- package/src/{Rotations.spec.js → Rotations.spec.ts} +4 -8
- package/src/{Rotations.js → Rotations.ts} +14 -16
- package/src/{Utils.spec.js → Utils.spec.ts} +2 -0
- package/src/{Utils.js → Utils.ts} +10 -23
- package/src/{Vector.spec.js → Vector.spec.ts} +1 -0
- package/src/Vector.ts +10 -0
- package/src/{Vector3.spec.js → Vector3.spec.ts} +4 -2
- package/src/{Vector3.js → Vector3.ts} +11 -10
- package/src/types.ts +21 -0
- package/tsconfig.json +3 -0
- package/vite.config.ts +4 -0
- package/dist/wemap-maths.es.js +0 -1100
- package/dist/wemap-maths.es.js.map +0 -1
- package/index.d.ts +0 -176
- package/src/EulerAngles.js +0 -8
- package/src/Matrix4.js +0 -158
- package/src/Matrix4.spec.js +0 -51
- package/src/Transformation.js +0 -40
- package/src/Transformation.spec.js +0 -79
- package/src/Vector.js +0 -8
- /package/src/{Matrix.spec.js → Matrix.spec.ts} +0 -0
- /package/src/{Point2.spec.js → Point2.spec.ts} +0 -0
package/src/Matrix4.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { Matrix3_t, Vector3_t, Vector4_t, Matrix4_t, Vector16_t } from './types.js';
|
|
2
|
+
import Vector3 from './Vector3.js';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export default class Matrix4 {
|
|
6
|
+
|
|
7
|
+
static get identity(): Matrix4_t {
|
|
8
|
+
return [
|
|
9
|
+
[1, 0, 0, 0],
|
|
10
|
+
[0, 1, 0, 0],
|
|
11
|
+
[0, 0, 1, 0],
|
|
12
|
+
[0, 0, 0, 1]
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static clone(mat: Matrix4_t): Matrix4_t {
|
|
17
|
+
return [
|
|
18
|
+
mat[0].slice(0) as Vector4_t,
|
|
19
|
+
mat[1].slice(0) as Vector4_t,
|
|
20
|
+
mat[2].slice(0) as Vector4_t,
|
|
21
|
+
mat[3].slice(0) as Vector4_t
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static sum(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t {
|
|
26
|
+
return [
|
|
27
|
+
[m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2], m1[0][3] + m2[0][3]],
|
|
28
|
+
[m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2], m1[1][3] + m2[1][3]],
|
|
29
|
+
[m1[2][0] + m2[2][0], m1[2][1] + m2[2][1], m1[2][2] + m2[2][2], m1[2][3] + m2[2][3]],
|
|
30
|
+
[m1[3][0] + m2[3][0], m1[3][1] + m2[3][1], m1[3][2] + m2[3][2], m1[3][3] + m2[3][3]]
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static subtract(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t {
|
|
35
|
+
return [
|
|
36
|
+
[m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2], m1[0][3] - m2[0][3]],
|
|
37
|
+
[m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2], m1[1][3] - m2[1][3]],
|
|
38
|
+
[m1[2][0] - m2[2][0], m1[2][1] - m2[2][1], m1[2][2] - m2[2][2], m1[2][3] - m2[2][3]],
|
|
39
|
+
[m1[3][0] - m2[3][0], m1[3][1] - m2[3][1], m1[3][2] - m2[3][2], m1[3][3] - m2[3][3]]
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static fromMatrix3(m: Matrix3_t): Matrix4_t {
|
|
44
|
+
return [
|
|
45
|
+
[m[0][0], m[0][1], m[0][2], 0],
|
|
46
|
+
[m[1][0], m[1][1], m[1][2], 0],
|
|
47
|
+
[m[2][0], m[2][1], m[2][2], 0],
|
|
48
|
+
[0, 0, 0, 1]
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static fromDiagVector(v: Vector4_t): Matrix4_t {
|
|
53
|
+
return [
|
|
54
|
+
[v[0], 0, 0, 0],
|
|
55
|
+
[0, v[1], 0, 0],
|
|
56
|
+
[0, 0, v[2], 0],
|
|
57
|
+
[0, 0, 0, v[3]]
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Column-Major ordering like Eigen and ThreeJS
|
|
63
|
+
*/
|
|
64
|
+
static fromVector(v: Vector16_t): Matrix4_t {
|
|
65
|
+
return [
|
|
66
|
+
[v[0], v[4], v[8], v[12]],
|
|
67
|
+
[v[1], v[5], v[9], v[13]],
|
|
68
|
+
[v[2], v[6], v[10], v[14]],
|
|
69
|
+
[v[3], v[7], v[11], v[15]]
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Column-Major ordering like Eigen and ThreeJS
|
|
75
|
+
*/
|
|
76
|
+
static toVector(m: Matrix4_t): Vector16_t {
|
|
77
|
+
return [
|
|
78
|
+
m[0][0], m[1][0], m[2][0], m[3][0],
|
|
79
|
+
m[0][1], m[1][1], m[2][1], m[3][1],
|
|
80
|
+
m[0][2], m[1][2], m[2][2], m[3][2],
|
|
81
|
+
m[0][3], m[1][3], m[2][3], m[3][3]
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static multiply(a: Matrix4_t, b: Matrix4_t): Matrix4_t {
|
|
86
|
+
const m = new Array(4) as Matrix4_t;
|
|
87
|
+
for (let r = 0; r < 4; ++r) {
|
|
88
|
+
m[r] = new Array(4) as Vector4_t;
|
|
89
|
+
for (let c = 0; c < 4; ++c) {
|
|
90
|
+
m[r][c] = 0;
|
|
91
|
+
for (let i = 0; i < 4; ++i) {
|
|
92
|
+
m[r][c] += a[r][i] * b[i][c];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return m;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
static multiplyScalar(a: Matrix4_t, s: number): Matrix4_t {
|
|
100
|
+
const output = new Array(4) as Matrix4_t;
|
|
101
|
+
for (let r = 0; r < 4; ++r) {
|
|
102
|
+
output[r] = new Array(4) as Vector4_t;
|
|
103
|
+
for (let c = 0; c < 4; ++c) {
|
|
104
|
+
output[r][c] = a[r][c] * s;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return output;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static multiplyVector(a: Matrix4_t, v: Vector4_t): Vector4_t {
|
|
111
|
+
const output = new Array(4) as Vector4_t;
|
|
112
|
+
for (let r = 0; r < 4; ++r) {
|
|
113
|
+
output[r] = 0;
|
|
114
|
+
for (let c = 0; c < 4; ++c) {
|
|
115
|
+
output[r] += a[r][c] * v[c];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return output;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static multiplyVector3(a: Matrix4_t, v: Vector3_t): Vector3_t {
|
|
122
|
+
return Matrix4.multiplyVector(a, [...v, 1]).slice(0, 3) as Vector3_t;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static transpose(m: Matrix4_t): Matrix4_t {
|
|
126
|
+
return [
|
|
127
|
+
[m[0][0], m[1][0], m[2][0], m[3][0]],
|
|
128
|
+
[m[0][1], m[1][1], m[2][1], m[3][1]],
|
|
129
|
+
[m[0][2], m[1][2], m[2][2], m[3][2]],
|
|
130
|
+
[m[0][3], m[1][3], m[2][3], m[3][3]]
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static invert(m: Matrix4_t): Matrix4_t | null {
|
|
135
|
+
// based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
|
|
136
|
+
// implementation from ThreeJS
|
|
137
|
+
const te = Matrix4.toVector(m),
|
|
138
|
+
|
|
139
|
+
n11 = te[0], n21 = te[1], n31 = te[2], n41 = te[3],
|
|
140
|
+
n12 = te[4], n22 = te[5], n32 = te[6], n42 = te[7],
|
|
141
|
+
n13 = te[8], n23 = te[9], n33 = te[10], n43 = te[11],
|
|
142
|
+
n14 = te[12], n24 = te[13], n34 = te[14], n44 = te[15],
|
|
143
|
+
|
|
144
|
+
t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
|
|
145
|
+
t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
|
|
146
|
+
t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
|
|
147
|
+
t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
|
|
148
|
+
|
|
149
|
+
const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
|
|
150
|
+
|
|
151
|
+
if (det === 0) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const detInv = 1 / det;
|
|
156
|
+
|
|
157
|
+
te[0] = t11 * detInv;
|
|
158
|
+
te[1] = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv;
|
|
159
|
+
te[2] = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv;
|
|
160
|
+
te[3] = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv;
|
|
161
|
+
|
|
162
|
+
te[4] = t12 * detInv;
|
|
163
|
+
te[5] = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv;
|
|
164
|
+
te[6] = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv;
|
|
165
|
+
te[7] = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv;
|
|
166
|
+
|
|
167
|
+
te[8] = t13 * detInv;
|
|
168
|
+
te[9] = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv;
|
|
169
|
+
te[10] = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv;
|
|
170
|
+
te[11] = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv;
|
|
171
|
+
|
|
172
|
+
te[12] = t14 * detInv;
|
|
173
|
+
te[13] = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv;
|
|
174
|
+
te[14] = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv;
|
|
175
|
+
te[15] = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv;
|
|
176
|
+
|
|
177
|
+
return Matrix4.fromVector(te);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static determinant(m: Matrix4_t): number {
|
|
181
|
+
// Based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
|
|
182
|
+
// implementation from ThreeJS
|
|
183
|
+
|
|
184
|
+
const n11 = m[0][0], n12 = m[1][0], n13 = m[2][0], n14 = m[3][0];
|
|
185
|
+
const n21 = m[0][1], n22 = m[1][1], n23 = m[2][1], n24 = m[3][1];
|
|
186
|
+
const n31 = m[0][2], n32 = m[1][2], n33 = m[2][2], n34 = m[3][2];
|
|
187
|
+
const n41 = m[0][3], n42 = m[1][3], n43 = m[2][3], n44 = m[3][3];
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
n41 * (
|
|
191
|
+
Number(n14) * n23 * n32
|
|
192
|
+
- n13 * n24 * n32
|
|
193
|
+
- n14 * n22 * n33
|
|
194
|
+
+ n12 * n24 * n33
|
|
195
|
+
+ n13 * n22 * n34
|
|
196
|
+
- n12 * n23 * n34
|
|
197
|
+
)
|
|
198
|
+
+ n42 * (
|
|
199
|
+
Number(n11) * n23 * n34
|
|
200
|
+
- n11 * n24 * n33
|
|
201
|
+
+ n14 * n21 * n33
|
|
202
|
+
- n13 * n21 * n34
|
|
203
|
+
+ n13 * n24 * n31
|
|
204
|
+
- n14 * n23 * n31
|
|
205
|
+
)
|
|
206
|
+
+ n43 * (
|
|
207
|
+
Number(n11) * n24 * n32
|
|
208
|
+
- n11 * n22 * n34
|
|
209
|
+
- n14 * n21 * n32
|
|
210
|
+
+ n12 * n21 * n34
|
|
211
|
+
+ n14 * n22 * n31
|
|
212
|
+
- n12 * n24 * n31
|
|
213
|
+
)
|
|
214
|
+
+ n44 * (
|
|
215
|
+
- n13 * n22 * n31
|
|
216
|
+
- n11 * n23 * n32
|
|
217
|
+
+ n11 * n22 * n33
|
|
218
|
+
+ n13 * n21 * n32
|
|
219
|
+
- n12 * n21 * n33
|
|
220
|
+
+ n12 * n23 * n31
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
static compose(translation: Vector3_t, rotation: Matrix3_t, scale: Vector3_t = [1, 1, 1]): Matrix4_t {
|
|
227
|
+
// From Three JS
|
|
228
|
+
// This composition corresponds to:
|
|
229
|
+
// => translation * rotation * scale
|
|
230
|
+
const [sx, sy, sz] = scale;
|
|
231
|
+
return [
|
|
232
|
+
[rotation[0][0] * sx, rotation[0][1] * sy, rotation[0][2] * sz, translation[0]],
|
|
233
|
+
[rotation[1][0] * sx, rotation[1][1] * sy, rotation[1][2] * sz, translation[1]],
|
|
234
|
+
[rotation[2][0] * sx, rotation[2][1] * sy, rotation[2][2] * sz, translation[2]],
|
|
235
|
+
[0, 0, 0, 1]
|
|
236
|
+
];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static decompose(m: Matrix4_t): { translation: Vector3_t, rotation: Matrix3_t, scale: Vector3_t } {
|
|
240
|
+
|
|
241
|
+
// Inspired by Three JS
|
|
242
|
+
// m corresponds to:
|
|
243
|
+
// => translation * rotation * scale
|
|
244
|
+
const scale: Vector3_t = [
|
|
245
|
+
Vector3.norm([m[0][0], m[1][0], m[2][0]]),
|
|
246
|
+
Vector3.norm([m[0][1], m[1][1], m[2][1]]),
|
|
247
|
+
Vector3.norm([m[0][2], m[1][2], m[2][2]])
|
|
248
|
+
];
|
|
249
|
+
|
|
250
|
+
// if determine is negative, we need to invert one scale
|
|
251
|
+
const det = Matrix4.determinant(m);
|
|
252
|
+
if (det < 0) {
|
|
253
|
+
scale[0] = -scale[0];
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const translation: Vector3_t = [m[0][3], m[1][3], m[2][3]];
|
|
257
|
+
|
|
258
|
+
const invSX = 1 / scale[0];
|
|
259
|
+
const invSY = 1 / scale[1];
|
|
260
|
+
const invSZ = 1 / scale[2];
|
|
261
|
+
|
|
262
|
+
const rotation: Matrix3_t = [
|
|
263
|
+
[m[0][0] * invSX, m[0][1] * invSY, m[0][2] * invSZ],
|
|
264
|
+
[m[1][0] * invSX, m[1][1] * invSY, m[1][2] * invSZ],
|
|
265
|
+
[m[2][0] * invSX, m[2][1] * invSY, m[2][2] * invSZ]
|
|
266
|
+
];
|
|
267
|
+
|
|
268
|
+
return { translation, scale, rotation };
|
|
269
|
+
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
static extractRotation(m: Matrix4_t): Matrix3_t {
|
|
273
|
+
const scaleX = 1 / Vector3.norm([m[0][0], m[1][0], m[2][0]]);
|
|
274
|
+
const scaleY = 1 / Vector3.norm([m[0][1], m[1][1], m[2][1]]);
|
|
275
|
+
const scaleZ = 1 / Vector3.norm([m[0][2], m[1][2], m[2][2]]);
|
|
276
|
+
return [
|
|
277
|
+
[m[0][0] * scaleX, m[0][1] * scaleY, m[0][2] * scaleZ],
|
|
278
|
+
[m[1][0] * scaleX, m[1][1] * scaleY, m[1][2] * scaleZ],
|
|
279
|
+
[m[2][0] * scaleX, m[2][1] * scaleY, m[2][2] * scaleZ]
|
|
280
|
+
];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
static extractTranslation(m: Matrix4_t): Vector3_t {
|
|
284
|
+
return [m[0][3], m[1][3], m[2][3]];
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
static scaleTranslation(m: Matrix4_t, s: Vector3_t): Matrix4_t {
|
|
288
|
+
return [
|
|
289
|
+
[m[0][0], m[0][1], m[0][2], m[0][3] * s[0]],
|
|
290
|
+
[m[1][0], m[1][1], m[1][2], m[1][3] * s[1]],
|
|
291
|
+
[m[2][0], m[2][1], m[2][2], m[2][3] * s[2]],
|
|
292
|
+
[m[3][0], m[3][1], m[3][2], m[3][3]]
|
|
293
|
+
];
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
static toString(m: Matrix4_t): string {
|
|
297
|
+
return '[\n'
|
|
298
|
+
+ m.map(row => '\t[' + row.map(val => val.toFixed(2)).join(', ') + ']'
|
|
299
|
+
).join(',\n')
|
|
300
|
+
+ '\n]';
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Point2_t } from './types';
|
|
2
|
+
|
|
1
3
|
class Point2 {
|
|
2
4
|
|
|
3
5
|
/**
|
|
@@ -6,7 +8,7 @@ class Point2 {
|
|
|
6
8
|
* @param {Number[2]} v segment point 1
|
|
7
9
|
* @param {Number[2]} w segment point 2
|
|
8
10
|
*/
|
|
9
|
-
static distanceToSegment(p, v, w) {
|
|
11
|
+
static distanceToSegment(p: Point2_t, v: Point2_t, w: Point2_t): number {
|
|
10
12
|
const A = p[0] - v[0];
|
|
11
13
|
const B = p[1] - v[1];
|
|
12
14
|
const C = w[0] - v[0];
|
|
@@ -43,7 +45,7 @@ class Point2 {
|
|
|
43
45
|
* @param {Number[2]} p1
|
|
44
46
|
* @param {Number[2]} p2
|
|
45
47
|
*/
|
|
46
|
-
static distanceToPoint(p1, p2) {
|
|
48
|
+
static distanceToPoint(p1: Point2_t, p2: Point2_t): number {
|
|
47
49
|
return Math.sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2));
|
|
48
50
|
}
|
|
49
51
|
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import chai from 'chai';
|
|
2
2
|
|
|
3
3
|
import Polygon from './Polygon.js';
|
|
4
|
+
import { Polygon_t } from './types.js';
|
|
4
5
|
|
|
5
6
|
const { expect } = chai;
|
|
6
7
|
|
|
7
8
|
describe('Polygon', () => {
|
|
8
9
|
|
|
9
|
-
const polygon1 = [[0, 0], [0, 2], [1, 2], [1, 0]];
|
|
10
|
+
const polygon1: Polygon_t = [[0, 0], [0, 2], [1, 2], [1, 0]];
|
|
10
11
|
|
|
11
12
|
it('isPointInside', () => {
|
|
12
13
|
|
|
@@ -31,7 +32,7 @@ describe('Polygon', () => {
|
|
|
31
32
|
|
|
32
33
|
it('distanceToOtherRectangle', () => {
|
|
33
34
|
|
|
34
|
-
let polygon2;
|
|
35
|
+
let polygon2: Polygon_t;
|
|
35
36
|
|
|
36
37
|
polygon2 = [[3, -1], [8, -1], [8, 4], [3, 4]];
|
|
37
38
|
expect(Polygon.distanceToOtherRectangle(polygon1, polygon2)).equal(2);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import Point2 from './Point2.js';
|
|
2
|
+
import { Point2_t, Polygon_t } from './types.js';
|
|
2
3
|
|
|
3
4
|
class Polygon {
|
|
4
5
|
|
|
5
6
|
// https://github.com/substack/point-in-polygon/blob/master/index.js
|
|
6
|
-
static isPointInside(polygon, point) {
|
|
7
|
+
static isPointInside(polygon: Polygon_t, point: Point2_t): boolean {
|
|
7
8
|
|
|
8
9
|
const x = point[0], y = point[1];
|
|
9
10
|
|
|
@@ -22,7 +23,7 @@ class Polygon {
|
|
|
22
23
|
return inside;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
|
-
static distanceToOtherRectangle(poly1, poly2) {
|
|
26
|
+
static distanceToOtherRectangle(poly1: Polygon_t, poly2: Polygon_t): number {
|
|
26
27
|
|
|
27
28
|
let minDist = Number.MAX_VALUE;
|
|
28
29
|
|
|
@@ -43,7 +44,7 @@ class Polygon {
|
|
|
43
44
|
return minDist;
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
static distanceToPoint(polygon, point) {
|
|
47
|
+
static distanceToPoint(polygon: Polygon_t, point: Point2_t): number {
|
|
47
48
|
|
|
48
49
|
let minDist = Number.MAX_VALUE;
|
|
49
50
|
|
|
@@ -4,17 +4,18 @@ import chaiAlmost from 'chai-almost';
|
|
|
4
4
|
import Quaternion from './Quaternion.js';
|
|
5
5
|
import Matrix3 from './Matrix3.js';
|
|
6
6
|
import Vector3 from './Vector3.js';
|
|
7
|
+
import { Matrix3_t, Quaternion_t, Vector3_t } from './types.js';
|
|
7
8
|
|
|
8
9
|
const { expect } = chai;
|
|
9
10
|
chai.use(chaiAlmost());
|
|
10
11
|
|
|
11
|
-
const q1 = [0.182574185835055, 0.365148371670111, 0.547722557505166, 0.730296743340221];
|
|
12
|
-
const q2 = [0.379049021789452, 0.454858826147342, 0.530668630505232, 0.606478434863123];
|
|
12
|
+
const q1: Quaternion_t = [0.182574185835055, 0.365148371670111, 0.547722557505166, 0.730296743340221];
|
|
13
|
+
const q2: Quaternion_t = [0.379049021789452, 0.454858826147342, 0.530668630505232, 0.606478434863123];
|
|
13
14
|
|
|
14
|
-
const v1 = [1, 2, 3];
|
|
15
|
+
const v1: Vector3_t = [1, 2, 3];
|
|
15
16
|
const v1Normalized = Vector3.normalize([1, 2, 3]);
|
|
16
17
|
|
|
17
|
-
const r1 = [
|
|
18
|
+
const r1: Matrix3_t = [
|
|
18
19
|
[0.763133109231345, -0.514739781660985, 0.390731128489274],
|
|
19
20
|
[0.614322194361606, 0.765493640108917, -0.191383720487251],
|
|
20
21
|
[-0.200589379354045, 0.386086057930615, 0.900389613868328]
|
|
@@ -26,55 +27,39 @@ describe('Quaternion', () => {
|
|
|
26
27
|
|
|
27
28
|
it('add', () => {
|
|
28
29
|
const result = Quaternion.sum(q1, q2);
|
|
29
|
-
expect(result).deep.almost(
|
|
30
|
-
[0.561623207624507, 0.820007197817453, 1.078391188010398, 1.336775178203344]
|
|
31
|
-
);
|
|
30
|
+
expect(result).deep.almost([0.561623207624507, 0.820007197817453, 1.078391188010398, 1.336775178203344]);
|
|
32
31
|
});
|
|
33
32
|
|
|
34
33
|
it('rotate', () => {
|
|
35
34
|
const result = Quaternion.rotate(q1, v1);
|
|
36
|
-
expect(result).deep.almost(
|
|
37
|
-
[1.8, 2, 2.6]
|
|
38
|
-
);
|
|
35
|
+
expect(result).deep.almost([1.8, 2, 2.6]);
|
|
39
36
|
const resultWithMat = Matrix3.multiplyVector(Quaternion.toMatrix3(q1), v1);
|
|
40
|
-
expect(resultWithMat).deep.almost(
|
|
41
|
-
[1.8, 2, 2.6]
|
|
42
|
-
);
|
|
37
|
+
expect(resultWithMat).deep.almost([1.8, 2, 2.6]);
|
|
43
38
|
});
|
|
44
39
|
|
|
45
40
|
it('rotateMatlab', () => {
|
|
46
41
|
const result = Quaternion.rotateMatlab(q1, v1);
|
|
47
|
-
expect(result).deep.almost(
|
|
48
|
-
[1.666666666666667, 2.266666666666667, 2.466666666666667]
|
|
49
|
-
);
|
|
42
|
+
expect(result).deep.almost([1.666666666666667, 2.266666666666667, 2.466666666666667]);
|
|
50
43
|
});
|
|
51
44
|
|
|
52
45
|
it('multiply', () => {
|
|
53
46
|
const result = Quaternion.multiply(q1, q2);
|
|
54
|
-
expect(result).deep.almost(
|
|
55
|
-
[-0.830454798537400, 0.166090959707480, 0.415227399268700, 0.332181919414960]
|
|
56
|
-
);
|
|
47
|
+
expect(result).deep.almost([-0.830454798537400, 0.166090959707480, 0.415227399268700, 0.332181919414960]);
|
|
57
48
|
});
|
|
58
49
|
|
|
59
50
|
it('fromAxisAngle', () => {
|
|
60
51
|
const result = Quaternion.fromAxisAngle(v1Normalized, angle);
|
|
61
|
-
expect(result).deep.almost(
|
|
62
|
-
[0.731688868873821, 0.182175621539481, 0.364351243078963, 0.546526864618444]
|
|
63
|
-
);
|
|
52
|
+
expect(result).deep.almost([0.731688868873821, 0.182175621539481, 0.364351243078963, 0.546526864618444]);
|
|
64
53
|
});
|
|
65
54
|
|
|
66
55
|
it('fromMatrix3', () => {
|
|
67
56
|
const result = Quaternion.fromMatrix3(r1);
|
|
68
|
-
expect(result).deep.almost(
|
|
69
|
-
[0.925880170865619, 0.155924545256753, 0.159664427009622, 0.304861798413669]
|
|
70
|
-
);
|
|
57
|
+
expect(result).deep.almost([0.925880170865619, 0.155924545256753, 0.159664427009622, 0.304861798413669]);
|
|
71
58
|
});
|
|
72
59
|
|
|
73
60
|
it('fromMatrix3Matlab', () => {
|
|
74
61
|
const result = Quaternion.fromMatrix3Matlab(r1);
|
|
75
|
-
expect(result).deep.almost(
|
|
76
|
-
[0.925880170865619, -0.155924545256753, -0.159664427009622, -0.304861798413669]
|
|
77
|
-
);
|
|
62
|
+
expect(result).deep.almost([0.925880170865619, -0.155924545256753, -0.159664427009622, -0.304861798413669]);
|
|
78
63
|
});
|
|
79
64
|
|
|
80
65
|
it('wxyz2xyzw', () => {
|
|
@@ -114,50 +99,28 @@ describe('Quaternion', () => {
|
|
|
114
99
|
|
|
115
100
|
it('getRotationBetweenTwoVectors', () => {
|
|
116
101
|
let result;
|
|
117
|
-
|
|
118
|
-
result = Quaternion.getRotationBetweenTwoVectors(
|
|
119
|
-
[0, 0, 1],
|
|
120
|
-
[0, 0, 1]
|
|
121
|
-
);
|
|
102
|
+
result = Quaternion.getRotationBetweenTwoVectors([0, 0, 1], [0, 0, 1]);
|
|
122
103
|
expect(result).deep.almost(Quaternion.identity);
|
|
123
|
-
|
|
124
|
-
result = Quaternion.getRotationBetweenTwoVectors(
|
|
125
|
-
[0, 0, 1],
|
|
126
|
-
[0, 0, -1]
|
|
127
|
-
);
|
|
104
|
+
result = Quaternion.getRotationBetweenTwoVectors([0, 0, 1], [0, 0, -1]);
|
|
128
105
|
expect(result).deep.almost([0, 0, -1, 0]);
|
|
129
|
-
|
|
130
|
-
result = Quaternion.getRotationBetweenTwoVectors(
|
|
131
|
-
[1, 0, 0],
|
|
132
|
-
[-1, 0, 0]
|
|
133
|
-
);
|
|
106
|
+
result = Quaternion.getRotationBetweenTwoVectors([1, 0, 0], [-1, 0, 0]);
|
|
134
107
|
expect(result).deep.almost([0, 0, 0, -1]);
|
|
135
|
-
|
|
136
|
-
result = Quaternion.getRotationBetweenTwoVectors(
|
|
137
|
-
[0, 1, 0],
|
|
138
|
-
[0, 0, 1]
|
|
139
|
-
);
|
|
108
|
+
result = Quaternion.getRotationBetweenTwoVectors([0, 1, 0], [0, 0, 1]);
|
|
140
109
|
expect(result).deep.almost([Math.SQRT1_2, Math.SQRT1_2, 0, 0]);
|
|
141
110
|
});
|
|
142
111
|
|
|
143
|
-
|
|
144
112
|
it('slerp', () => {
|
|
145
|
-
const qFrom = [1, 0, 0, 0];
|
|
146
|
-
let qTo = [Math.SQRT1_2, 0, 0, Math.SQRT1_2];
|
|
147
|
-
let qSlerp;
|
|
148
|
-
|
|
113
|
+
const qFrom: Quaternion_t = [1, 0, 0, 0];
|
|
114
|
+
let qTo: Quaternion_t = [Math.SQRT1_2, 0, 0, Math.SQRT1_2];
|
|
115
|
+
let qSlerp: Quaternion_t;
|
|
149
116
|
qSlerp = Quaternion.slerp(qFrom, qTo, 1 / 3);
|
|
150
117
|
expect(Quaternion.equals(qSlerp, [0.9659258262890684, 0, 0, 0.2588190451025208])).true;
|
|
151
|
-
|
|
152
118
|
qSlerp = Quaternion.slerp(qFrom, qTo, 2 / 3);
|
|
153
119
|
expect(Quaternion.equals(qSlerp, [0.8660254037844387, 0, 0, 0.5])).true;
|
|
154
|
-
|
|
155
120
|
qSlerp = Quaternion.slerp([-1, 0, 0, 0], qTo, 2 / 3);
|
|
156
121
|
expect(Quaternion.equals(qSlerp, [0.8660254037844387, 0, 0, 0.5])).true;
|
|
157
|
-
|
|
158
122
|
qTo = Quaternion.normalize([1, 1e-4, 0, 0]);
|
|
159
123
|
qSlerp = Quaternion.slerp([1, 0, 0, 0], qTo, 1 / 2);
|
|
160
124
|
expect(Quaternion.equals(qSlerp, [0.99999999875, 0.0000499999998125, 0, 0])).true;
|
|
161
|
-
|
|
162
125
|
});
|
|
163
126
|
});
|