@wemap/maths 12.10.0 → 12.10.8-alpha.2

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 (51) hide show
  1. package/{index.ts → dist/index.d.ts} +0 -2
  2. package/dist/index.js +1 -1155
  3. package/dist/index.js.map +1 -1
  4. package/{src/EulerAngles.ts → dist/src/EulerAngles.d.ts} +2 -7
  5. package/dist/src/Geometry.d.ts +10 -0
  6. package/dist/src/Matrix.d.ts +13 -0
  7. package/dist/src/Matrix.spec.d.ts +1 -0
  8. package/dist/src/Matrix3.d.ts +20 -0
  9. package/dist/src/Matrix3.spec.d.ts +2 -0
  10. package/dist/src/Matrix4.d.ts +35 -0
  11. package/dist/src/Matrix4.spec.d.ts +1 -0
  12. package/dist/src/Point2.d.ts +17 -0
  13. package/dist/src/Point2.spec.d.ts +1 -0
  14. package/dist/src/Polygon.d.ts +7 -0
  15. package/dist/src/Polygon.spec.d.ts +1 -0
  16. package/dist/src/Quaternion.d.ts +71 -0
  17. package/dist/src/Quaternion.spec.d.ts +1 -0
  18. package/dist/src/Rotations.d.ts +13 -0
  19. package/dist/src/Rotations.spec.d.ts +1 -0
  20. package/dist/src/Utils.d.ts +38 -0
  21. package/dist/src/Utils.spec.d.ts +1 -0
  22. package/dist/src/Vector.d.ts +5 -0
  23. package/dist/src/Vector.spec.d.ts +1 -0
  24. package/dist/src/Vector3.d.ts +13 -0
  25. package/dist/src/Vector3.spec.d.ts +1 -0
  26. package/{src/types.ts → dist/src/types.d.ts} +37 -8
  27. package/dist/vite.config.d.ts +19 -0
  28. package/package.json +7 -4
  29. package/src/Geometry.ts +0 -39
  30. package/src/Matrix.spec.ts +0 -130
  31. package/src/Matrix.ts +0 -242
  32. package/src/Matrix3.spec.ts +0 -138
  33. package/src/Matrix3.ts +0 -165
  34. package/src/Matrix4.spec.ts +0 -187
  35. package/src/Matrix4.ts +0 -309
  36. package/src/Point2.spec.ts +0 -19
  37. package/src/Point2.ts +0 -54
  38. package/src/Polygon.spec.ts +0 -48
  39. package/src/Polygon.ts +0 -63
  40. package/src/Quaternion.spec.ts +0 -126
  41. package/src/Quaternion.ts +0 -292
  42. package/src/Rotations.spec.ts +0 -67
  43. package/src/Rotations.ts +0 -223
  44. package/src/Utils.spec.ts +0 -83
  45. package/src/Utils.ts +0 -77
  46. package/src/Vector.spec.ts +0 -15
  47. package/src/Vector.ts +0 -10
  48. package/src/Vector3.spec.ts +0 -52
  49. package/src/Vector3.ts +0 -51
  50. package/tsconfig.json +0 -3
  51. package/vite.config.ts +0 -4
package/src/Matrix4.ts DELETED
@@ -1,309 +0,0 @@
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 setTranslation(m: Matrix4_t, v: Vector3_t): Matrix4_t {
297
- m[0][3] = v[0];
298
- m[1][3] = v[1];
299
- m[2][3] = v[2];
300
- return m;
301
- }
302
-
303
- static toString(m: Matrix4_t): string {
304
- return '[\n'
305
- + m.map(row => '\t[' + row.map(val => val.toFixed(2)).join(', ') + ']'
306
- ).join(',\n')
307
- + '\n]';
308
- }
309
- }
@@ -1,19 +0,0 @@
1
- import chai from 'chai';
2
-
3
- import Point2 from './Point2.js';
4
-
5
- const { expect } = chai;
6
-
7
- describe('Point2', () => {
8
-
9
- it('distanceToPoint', () => {
10
- expect(Point2.distanceToPoint([0, 0], [1, 1])).equal(Math.SQRT2);
11
- });
12
-
13
- it('distanceToSegment', () => {
14
- expect(Point2.distanceToSegment([1, 1], [0, 0], [0, 2])).equal(1);
15
- expect(Point2.distanceToSegment([0, -1], [0, 0], [0, 2])).equal(1);
16
- expect(Point2.distanceToSegment([0, 3], [0, 0], [0, 2])).equal(1);
17
- });
18
-
19
- });
package/src/Point2.ts DELETED
@@ -1,54 +0,0 @@
1
- import { Point2_t } from './types';
2
-
3
- class Point2 {
4
-
5
- /**
6
- * Distance from a point to a segment
7
- * @param {Number[2]} p point
8
- * @param {Number[2]} v segment point 1
9
- * @param {Number[2]} w segment point 2
10
- */
11
- static distanceToSegment(p: Point2_t, v: Point2_t, w: Point2_t): number {
12
- const A = p[0] - v[0];
13
- const B = p[1] - v[1];
14
- const C = w[0] - v[0];
15
- const D = w[1] - v[1];
16
-
17
- const dot = A * C + B * D;
18
- const lenSq = C * C + D * D;
19
- let param = -1;
20
- if (lenSq !== 0) {
21
- param = dot / lenSq;
22
- }
23
-
24
- let xx;
25
- let yy;
26
-
27
- if (param < 0) {
28
- xx = v[0];
29
- yy = v[1];
30
- } else if (param > 1) {
31
- xx = w[0];
32
- yy = w[1];
33
- } else {
34
- xx = v[0] + param * C;
35
- yy = v[1] + param * D;
36
- }
37
-
38
- const dx = p[0] - xx;
39
- const dy = p[1] - yy;
40
- return Math.sqrt(dx * dx + dy * dy);
41
- }
42
-
43
- /**
44
- * Distance between two points
45
- * @param {Number[2]} p1
46
- * @param {Number[2]} p2
47
- */
48
- static distanceToPoint(p1: Point2_t, p2: Point2_t): number {
49
- return Math.sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2));
50
- }
51
-
52
- }
53
-
54
- export default Point2;
@@ -1,48 +0,0 @@
1
- import chai from 'chai';
2
-
3
- import Polygon from './Polygon.js';
4
- import { Polygon_t } from './types.js';
5
-
6
- const { expect } = chai;
7
-
8
- describe('Polygon', () => {
9
-
10
- const polygon1: Polygon_t = [[0, 0], [0, 2], [1, 2], [1, 0]];
11
-
12
- it('isPointInside', () => {
13
-
14
- expect(Polygon.isPointInside(polygon1, [0.5, 1])).true;
15
- expect(Polygon.isPointInside(polygon1, [1, 1])).true;
16
- expect(Polygon.isPointInside(polygon1, [0.5, 0])).true;
17
-
18
- expect(Polygon.isPointInside(polygon1, [2, 1])).false;
19
- expect(Polygon.isPointInside(polygon1, [-1, 1])).false;
20
- expect(Polygon.isPointInside(polygon1, [0.5, 3])).false;
21
- expect(Polygon.isPointInside(polygon1, [0.5, -1])).false;
22
- });
23
-
24
-
25
- it('distanceToPoint', () => {
26
-
27
- expect(Polygon.distanceToPoint(polygon1, [0.5, 1])).equal(0.5);
28
- expect(Polygon.distanceToPoint(polygon1, [0, 1])).equal(0);
29
- expect(Polygon.distanceToPoint(polygon1, [2, 1])).equal(1);
30
-
31
- });
32
-
33
- it('distanceToOtherRectangle', () => {
34
-
35
- let polygon2: Polygon_t;
36
-
37
- polygon2 = [[3, -1], [8, -1], [8, 4], [3, 4]];
38
- expect(Polygon.distanceToOtherRectangle(polygon1, polygon2)).equal(2);
39
-
40
- polygon2 = [[0.5, 1], [0.5, 3], [1.5, 3], [1.5, 1]];
41
- expect(Polygon.distanceToOtherRectangle(polygon1, polygon2)).equal(0);
42
-
43
- polygon2 = [[-1, -1], [-1, 3], [2, 3], [2, -1]];
44
- expect(Polygon.distanceToOtherRectangle(polygon2, polygon1)).equal(0);
45
-
46
- });
47
-
48
- });
package/src/Polygon.ts DELETED
@@ -1,63 +0,0 @@
1
- import Point2 from './Point2.js';
2
- import { Point2_t, Polygon_t } from './types.js';
3
-
4
- class Polygon {
5
-
6
- // https://github.com/substack/point-in-polygon/blob/master/index.js
7
- static isPointInside(polygon: Polygon_t, point: Point2_t): boolean {
8
-
9
- const x = point[0], y = point[1];
10
-
11
- let inside = false;
12
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
13
- const xi = polygon[i][0], yi = polygon[i][1];
14
- const xj = polygon[j][0], yj = polygon[j][1];
15
-
16
- const intersect = ((yi > y) !== (yj > y))
17
- && (x <= (xj - xi) * (y - yi) / (yj - yi) + xi);
18
- if (intersect) {
19
- inside = !inside;
20
- }
21
- }
22
-
23
- return inside;
24
- }
25
-
26
- static distanceToOtherRectangle(poly1: Polygon_t, poly2: Polygon_t): number {
27
-
28
- let minDist = Number.MAX_VALUE;
29
-
30
- for (const point of poly1) {
31
- if (Polygon.isPointInside(poly2, point)) {
32
- return 0;
33
- }
34
- minDist = Math.min(minDist, Polygon.distanceToPoint(poly2, point));
35
- }
36
-
37
- for (const point of poly2) {
38
- if (Polygon.isPointInside(poly1, point)) {
39
- return 0;
40
- }
41
- minDist = Math.min(minDist, Polygon.distanceToPoint(poly1, point));
42
- }
43
-
44
- return minDist;
45
- }
46
-
47
- static distanceToPoint(polygon: Polygon_t, point: Point2_t): number {
48
-
49
- let minDist = Number.MAX_VALUE;
50
-
51
- polygon.forEach((segPoint1, idx) => {
52
- const segPoint2 = polygon[(idx + 1) % polygon.length];
53
- minDist = Math.min(
54
- minDist,
55
- Point2.distanceToSegment(point, segPoint1, segPoint2)
56
- );
57
- });
58
-
59
- return minDist;
60
- }
61
- }
62
-
63
- export default Polygon;
@@ -1,126 +0,0 @@
1
- import chai from 'chai';
2
- import chaiAlmost from 'chai-almost';
3
-
4
- import Quaternion from './Quaternion.js';
5
- import Matrix3 from './Matrix3.js';
6
- import Vector3 from './Vector3.js';
7
- import { Matrix3_t, Quaternion_t, Vector3_t } from './types.js';
8
-
9
- const { expect } = chai;
10
- chai.use(chaiAlmost());
11
-
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];
14
-
15
- const v1: Vector3_t = [1, 2, 3];
16
- const v1Normalized = Vector3.normalize([1, 2, 3]);
17
-
18
- const r1: Matrix3_t = [
19
- [0.763133109231345, -0.514739781660985, 0.390731128489274],
20
- [0.614322194361606, 0.765493640108917, -0.191383720487251],
21
- [-0.200589379354045, 0.386086057930615, 0.900389613868328]
22
- ];
23
-
24
- const angle = 1.5;
25
-
26
- describe('Quaternion', () => {
27
-
28
- it('add', () => {
29
- const result = Quaternion.sum(q1, q2);
30
- expect(result).deep.almost([0.561623207624507, 0.820007197817453, 1.078391188010398, 1.336775178203344]);
31
- });
32
-
33
- it('rotate', () => {
34
- const result = Quaternion.rotate(q1, v1);
35
- expect(result).deep.almost([1.8, 2, 2.6]);
36
- const resultWithMat = Matrix3.multiplyVector(Quaternion.toMatrix3(q1), v1);
37
- expect(resultWithMat).deep.almost([1.8, 2, 2.6]);
38
- });
39
-
40
- it('rotateMatlab', () => {
41
- const result = Quaternion.rotateMatlab(q1, v1);
42
- expect(result).deep.almost([1.666666666666667, 2.266666666666667, 2.466666666666667]);
43
- });
44
-
45
- it('multiply', () => {
46
- const result = Quaternion.multiply(q1, q2);
47
- expect(result).deep.almost([-0.830454798537400, 0.166090959707480, 0.415227399268700, 0.332181919414960]);
48
- });
49
-
50
- it('fromAxisAngle', () => {
51
- const result = Quaternion.fromAxisAngle(v1Normalized, angle);
52
- expect(result).deep.almost([0.731688868873821, 0.182175621539481, 0.364351243078963, 0.546526864618444]);
53
- });
54
-
55
- it('fromMatrix3', () => {
56
- const result = Quaternion.fromMatrix3(r1);
57
- expect(result).deep.almost([0.925880170865619, 0.155924545256753, 0.159664427009622, 0.304861798413669]);
58
- });
59
-
60
- it('fromMatrix3Matlab', () => {
61
- const result = Quaternion.fromMatrix3Matlab(r1);
62
- expect(result).deep.almost([0.925880170865619, -0.155924545256753, -0.159664427009622, -0.304861798413669]);
63
- });
64
-
65
- it('wxyz2xyzw', () => {
66
- const result = Quaternion.wxyz2xyzw(q1);
67
- expect(result).deep.almost([q1[1], q1[2], q1[3], q1[0]]);
68
- });
69
-
70
- it('xyzw2wxyz', () => {
71
- const result = Quaternion.xyzw2wxyz(q1);
72
- expect(result).deep.almost([q1[3], q1[0], q1[1], q1[2]]);
73
- });
74
-
75
- it('distance', () => {
76
- const result = Quaternion.distance(q1, q2);
77
- expect(result).almost(0.500391840845023);
78
- });
79
-
80
- it('norm', () => {
81
- const result = Quaternion.norm(q1);
82
- expect(result).almost(1);
83
- });
84
-
85
- it('normalize', () => {
86
- const result = Quaternion.normalize(q1);
87
- expect(result).deep.almost(q1);
88
- });
89
-
90
- it('dot', () => {
91
- const result = Quaternion.dot(q1, q2);
92
- expect(result).almost(0.968863931626966);
93
- });
94
-
95
- it('inverse', () => {
96
- const result = Quaternion.inverse(q1);
97
- expect(result).deep.equal([-q1[0], q1[1], q1[2], q1[3]]);
98
- });
99
-
100
- it('getRotationBetweenTwoVectors', () => {
101
- let result;
102
- result = Quaternion.getRotationBetweenTwoVectors([0, 0, 1], [0, 0, 1]);
103
- expect(result).deep.almost(Quaternion.identity);
104
- result = Quaternion.getRotationBetweenTwoVectors([0, 0, 1], [0, 0, -1]);
105
- expect(result).deep.almost([0, 0, -1, 0]);
106
- result = Quaternion.getRotationBetweenTwoVectors([1, 0, 0], [-1, 0, 0]);
107
- expect(result).deep.almost([0, 0, 0, -1]);
108
- result = Quaternion.getRotationBetweenTwoVectors([0, 1, 0], [0, 0, 1]);
109
- expect(result).deep.almost([Math.SQRT1_2, Math.SQRT1_2, 0, 0]);
110
- });
111
-
112
- it('slerp', () => {
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;
116
- qSlerp = Quaternion.slerp(qFrom, qTo, 1 / 3);
117
- expect(Quaternion.equals(qSlerp, [0.9659258262890684, 0, 0, 0.2588190451025208])).true;
118
- qSlerp = Quaternion.slerp(qFrom, qTo, 2 / 3);
119
- expect(Quaternion.equals(qSlerp, [0.8660254037844387, 0, 0, 0.5])).true;
120
- qSlerp = Quaternion.slerp([-1, 0, 0, 0], qTo, 2 / 3);
121
- expect(Quaternion.equals(qSlerp, [0.8660254037844387, 0, 0, 0.5])).true;
122
- qTo = Quaternion.normalize([1, 1e-4, 0, 0]);
123
- qSlerp = Quaternion.slerp([1, 0, 0, 0], qTo, 1 / 2);
124
- expect(Quaternion.equals(qSlerp, [0.99999999875, 0.0000499999998125, 0, 0])).true;
125
- });
126
- });