@wemap/maths 10.10.0 → 11.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/index.js +817 -0
  2. package/dist/index.js.map +1 -0
  3. package/{index.js → index.ts} +2 -0
  4. package/package.json +8 -5
  5. package/src/EulerAngles.ts +10 -0
  6. package/src/{Geometry.js → Geometry.ts} +10 -14
  7. package/src/{Matrix.js → Matrix.ts} +17 -12
  8. package/src/{Matrix3.spec.js → Matrix3.spec.ts} +5 -7
  9. package/src/{Matrix3.js → Matrix3.ts} +26 -23
  10. package/src/{Matrix4.spec.js → Matrix4.spec.ts} +20 -46
  11. package/src/{Matrix4.js → Matrix4.ts} +34 -34
  12. package/src/{Point2.js → Point2.ts} +4 -2
  13. package/src/{Polygon.spec.js → Polygon.spec.ts} +3 -2
  14. package/src/{Polygon.js → Polygon.ts} +4 -3
  15. package/src/{Quaternion.spec.js → Quaternion.spec.ts} +20 -57
  16. package/src/{Quaternion.js → Quaternion.ts} +32 -31
  17. package/src/{Rotations.spec.js → Rotations.spec.ts} +4 -8
  18. package/src/{Rotations.js → Rotations.ts} +14 -16
  19. package/src/{Utils.spec.js → Utils.spec.ts} +2 -0
  20. package/src/{Utils.js → Utils.ts} +10 -23
  21. package/src/{Vector.spec.js → Vector.spec.ts} +1 -0
  22. package/src/Vector.ts +10 -0
  23. package/src/{Vector3.spec.js → Vector3.spec.ts} +4 -2
  24. package/src/{Vector3.js → Vector3.ts} +11 -10
  25. package/src/types.ts +21 -0
  26. package/tsconfig.json +3 -0
  27. package/vite.config.ts +4 -0
  28. package/dist/wemap-maths.es.js +0 -1100
  29. package/dist/wemap-maths.es.js.map +0 -1
  30. package/index.d.ts +0 -177
  31. package/src/EulerAngles.js +0 -8
  32. package/src/Vector.js +0 -8
  33. /package/src/{Matrix.spec.js → Matrix.spec.ts} +0 -0
  34. /package/src/{Point2.spec.js → Point2.spec.ts} +0 -0
@@ -6,36 +6,40 @@ import Matrix3 from './Matrix3.js';
6
6
  import Matrix4 from './Matrix4.js';
7
7
 
8
8
  import { expectIsRotMat } from './Matrix3.spec.js';
9
+ import { Matrix3_t, Matrix4_t, Vector3_t } from './types.js';
9
10
 
10
11
  const { expect } = chai;
11
12
  chai.use(chaiAlmost());
12
13
 
13
- const expectIsPose = (m) => {
14
+ const expectIsPose = (m: Matrix4_t) => {
14
15
  expectIsRotMat(Matrix4.extractRotation(m));
15
16
  expect(m[3]).deep.almost([0, 0, 0, 1]);
16
- expect(Matrix4.multiply(m, Matrix4.invert(m))).deep.almost(Matrix4.identity);
17
+ const inverse = Matrix4.invert(m);
18
+ expect(inverse).is.not.null;
19
+ expect(Matrix4.multiply(m, inverse as Matrix4_t)).deep.almost(Matrix4.identity);
17
20
  };
18
21
 
19
- const expectIsAffineTransform = (m) => {
22
+ const expectIsAffineTransform = (m: Matrix4_t) => {
20
23
  expectIsRotMat(Matrix4.extractRotation(m));
21
24
  expect(m[3]).deep.almost([0, 0, 0, 1]);
22
25
  // expect(Matrix4.determinant(m)).almost(1); // not true in case of scale
23
26
  };
24
27
 
25
- const m1 = [
28
+ const m1: Matrix4_t = [
26
29
  [1, 2, 3, 4],
27
30
  [5, 6, 7, 8],
28
31
  [9, 0, 1, 2],
29
32
  [3, 4, 5, 6]
30
33
  ];
31
34
 
32
- const m2 = [
35
+ const m2: Matrix4_t = [
33
36
  [1, 2, 3, 4],
34
37
  [0, 1, 5, 2],
35
38
  [5, 6, 0, 9],
36
39
  [3, 1, 9, 3]
37
40
  ];
38
41
 
42
+
39
43
  describe('Matrix4', () => {
40
44
 
41
45
  it('identity', () => {
@@ -66,21 +70,19 @@ describe('Matrix4', () => {
66
70
  [0, 3, -4, 3]
67
71
  ]);
68
72
  });
69
-
70
- let t1, t2;
71
- let rot1, tr1, rot2, tr2, sc2;
73
+ let t1: Matrix4_t, t2: Matrix4_t;
74
+ let rot1: Matrix3_t, tr1: Vector3_t, rot2: Matrix3_t, tr2: Vector3_t, sc2: Vector3_t;
72
75
 
73
76
  it('compose/decompose', () => {
74
77
  rot1 = Matrix3.rotX(Math.PI / 2);
75
78
  tr1 = [0, 1, 0];
76
79
  t1 = Matrix4.compose(tr1, rot1);
77
- expect(t1).deep.almost([
80
+ expect(t1).deep .almost([
78
81
  [1, 0, 0, 0],
79
82
  [0, 0, -1, 1],
80
83
  [0, 1, 0, 0],
81
84
  [0, 0, 0, 1]
82
85
  ]);
83
-
84
86
  rot2 = Matrix3.multiply(rot1, Matrix3.rotZ(Math.PI / 2));
85
87
  tr2 = [2, 1, 0];
86
88
  t2 = Matrix4.compose(tr2, rot2);
@@ -90,23 +92,19 @@ describe('Matrix4', () => {
90
92
  [1, 0, 0, 0],
91
93
  [0, 0, 0, 1]
92
94
  ]);
93
-
94
95
  sc2 = [2, 3, 3];
95
96
  const translationMat = Matrix4.compose(tr2, Matrix3.identity);
96
97
  const rotationMat = Matrix4.compose([0, 0, 0], rot2);
97
98
  const scaleMat = Matrix4.fromDiagVector([...sc2, 1]);
98
99
  const expectedComposed = Matrix4.multiply(Matrix4.multiply(translationMat, rotationMat), scaleMat);
99
-
100
100
  const composed = Matrix4.compose(tr2, rot2, sc2);
101
101
  expect(composed).deep.almost(expectedComposed);
102
-
103
102
  const { rotation: estRot2, translation: estTr2, scale: estSc2 } = Matrix4.decompose(composed);
104
103
  expect(estRot2).deep.almost(rot2);
105
104
  expect(estTr2).deep.almost(tr2);
106
105
  expect(estSc2).deep.almost(sc2);
107
106
  });
108
107
 
109
-
110
108
  it('extractRotation', () => {
111
109
  expect(Matrix4.extractRotation(t1)).deep.almost(rot1);
112
110
  expect(Matrix4.extractRotation(t2)).deep.almost(rot2);
@@ -124,14 +122,12 @@ describe('Matrix4', () => {
124
122
  [0, -1, 0, 1],
125
123
  [0, 0, 0, 1]
126
124
  ]);
127
-
128
125
  expect(Matrix4.invert(t2)).deep.almost([
129
126
  [0, 0, 1, 0],
130
127
  [-1, 0, 0, 2],
131
128
  [0, -1, 0, 1],
132
129
  [0, 0, 0, 1]
133
130
  ]);
134
-
135
131
  const tr3 = Matrix4.compose([1, 2, 3], Matrix3.rotY(Math.PI / 2), [2, 2, 4]);
136
132
  const invertTr3 = Matrix4.invert(tr3);
137
133
  expect(invertTr3).deep.almost([
@@ -140,19 +136,15 @@ describe('Matrix4', () => {
140
136
  [0.25, 0, 0, -0.25],
141
137
  [0, 0, 0, 1]
142
138
  ]);
143
- expect(Matrix4.multiply(tr3, invertTr3)).deep.almost(Matrix4.identity);
139
+ expect(Matrix4.multiply(tr3, invertTr3 as Matrix4_t)).deep.almost(Matrix4.identity);
144
140
  });
145
141
 
146
-
147
142
  it('multiply - Translation', () => {
148
-
149
- const position = [1, 2, 3];
143
+ const position: Vector3_t = [1, 2, 3];
150
144
  const rotation = Matrix3.rotY(Math.PI / 2);
151
145
  const pose = Matrix4.compose(position, rotation);
152
-
153
146
  const transform = Matrix4.compose([8, 1, 5], Matrix3.identity);
154
147
  expectIsAffineTransform(transform);
155
-
156
148
  const positionTransformed = Matrix4.multiplyVector3(transform, position);
157
149
  expect(positionTransformed).deep.almost([9, 3, 8]);
158
150
  const poseTransformed = Matrix4.multiply(transform, pose);
@@ -161,53 +153,35 @@ describe('Matrix4', () => {
161
153
  expect(Matrix4.extractRotation(poseTransformed)).deep.almost(rotation);
162
154
  });
163
155
 
164
-
165
156
  it('multiply - Rotation', () => {
166
-
167
- const position = [1, 2, 3];
157
+ const position: Vector3_t = [1, 2, 3];
168
158
  const rotation = Matrix3.rotY(Math.PI / 2);
169
159
  const pose = Matrix4.compose(position, rotation);
170
-
171
160
  const transformRotation = Matrix3.multiply(Matrix3.rotY(Math.PI / 2), Matrix3.rotZ(Math.PI / 4));
172
161
  const transform = Matrix4.compose([0, 0, 0], transformRotation);
173
-
174
162
  expectIsAffineTransform(transform);
175
-
176
163
  const positionTransformed = Matrix4.multiplyVector3(transform, position);
177
164
  const rotationTransformed = Matrix3.multiply(transformRotation, rotation);
178
165
  const poseTransformed = Matrix4.multiply(transform, pose);
179
166
  expectIsPose(poseTransformed);
180
167
  expect(Matrix4.extractTranslation(poseTransformed)).deep.almost(positionTransformed);
181
168
  expect(Matrix4.extractRotation(poseTransformed)).deep.almost(rotationTransformed);
182
-
183
169
  });
184
170
 
185
171
  it('multiply - Translation + Rotation + Scale', () => {
186
-
187
172
  // Pose
188
173
  const localPose = Matrix4.compose([1, 2, 3], Matrix3.multiply(Matrix3.rotX(Math.PI / 2), Matrix3.rotZ(Math.PI / 4)));
189
- const cameraPose = Matrix4.invert(localPose);
190
-
174
+ const cameraPose = Matrix4.invert(localPose) as Matrix4_t;
191
175
  // Transform
192
176
  const transform = Matrix4.compose([3, 1, 8], Matrix3.rotX(-Math.PI / 8));
193
177
  // scale must be a scalar
194
- const scale = [3, 3, 3];
195
- const transformInverse = Matrix4.invert(transform);
196
-
178
+ const scale: Vector3_t = [3, 3, 3];
179
+ const transformInverse = Matrix4.invert(transform) as Matrix4_t;
197
180
  // Local pose transform
198
- const localPoseTransformed = Matrix4.scaleTranslation(
199
- Matrix4.multiply(transform, localPose),
200
- scale
201
- );
202
-
181
+ const localPoseTransformed = Matrix4.scaleTranslation(Matrix4.multiply(transform, localPose), scale);
203
182
  // Camera pose transform
204
- const cameraPoseTransformed = Matrix4.scaleTranslation(
205
- Matrix4.multiply(cameraPose, transformInverse),
206
- scale
207
- );
183
+ const cameraPoseTransformed = Matrix4.scaleTranslation(Matrix4.multiply(cameraPose, transformInverse), scale);
208
184
  const localPoseTransformedEstimated = Matrix4.invert(cameraPoseTransformed);
209
-
210
185
  expect(localPoseTransformed).deep.almost(localPoseTransformedEstimated);
211
186
  });
212
-
213
187
  });
@@ -1,8 +1,10 @@
1
+ import { Matrix3_t, Vector3_t, Vector4_t, Matrix4_t, Vector16_t } from './types.js';
1
2
  import Vector3 from './Vector3.js';
2
3
 
3
- class Matrix4 {
4
4
 
5
- static get identity() {
5
+ export default class Matrix4 {
6
+
7
+ static get identity(): Matrix4_t {
6
8
  return [
7
9
  [1, 0, 0, 0],
8
10
  [0, 1, 0, 0],
@@ -11,7 +13,7 @@ class Matrix4 {
11
13
  ];
12
14
  }
13
15
 
14
- static sum(m1, m2) {
16
+ static sum(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t {
15
17
  return [
16
18
  [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2], m1[0][3] + m2[0][3]],
17
19
  [m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2], m1[1][3] + m2[1][3]],
@@ -20,7 +22,7 @@ class Matrix4 {
20
22
  ];
21
23
  }
22
24
 
23
- static subtract(m1, m2) {
25
+ static subtract(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t {
24
26
  return [
25
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]],
26
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,7 +31,7 @@ class Matrix4 {
29
31
  ];
30
32
  }
31
33
 
32
- static fromMatrix3(m) {
34
+ static fromMatrix3(m: Matrix3_t): Matrix4_t {
33
35
  return [
34
36
  [m[0][0], m[0][1], m[0][2], 0],
35
37
  [m[1][0], m[1][1], m[1][2], 0],
@@ -38,7 +40,7 @@ class Matrix4 {
38
40
  ];
39
41
  }
40
42
 
41
- static fromDiagVector(v) {
43
+ static fromDiagVector(v: Vector4_t): Matrix4_t {
42
44
  return [
43
45
  [v[0], 0, 0, 0],
44
46
  [0, v[1], 0, 0],
@@ -50,7 +52,7 @@ class Matrix4 {
50
52
  /**
51
53
  * Column-Major ordering like Eigen and ThreeJS
52
54
  */
53
- static fromVector(v) {
55
+ static fromVector(v: Vector16_t): Matrix4_t {
54
56
  return [
55
57
  [v[0], v[4], v[8], v[12]],
56
58
  [v[1], v[5], v[9], v[13]],
@@ -62,7 +64,7 @@ class Matrix4 {
62
64
  /**
63
65
  * Column-Major ordering like Eigen and ThreeJS
64
66
  */
65
- static toVector(m) {
67
+ static toVector(m: Matrix4_t): Vector16_t {
66
68
  return [
67
69
  m[0][0], m[1][0], m[2][0], m[3][0],
68
70
  m[0][1], m[1][1], m[2][1], m[3][1],
@@ -71,10 +73,10 @@ class Matrix4 {
71
73
  ];
72
74
  }
73
75
 
74
- static multiply(a, b) {
75
- const m = new Array(4);
76
+ static multiply(a: Matrix4_t, b: Matrix4_t): Matrix4_t {
77
+ const m = new Array(4) as Matrix4_t;
76
78
  for (let r = 0; r < 4; ++r) {
77
- m[r] = new Array(4);
79
+ m[r] = new Array(4) as Vector4_t;
78
80
  for (let c = 0; c < 4; ++c) {
79
81
  m[r][c] = 0;
80
82
  for (let i = 0; i < 4; ++i) {
@@ -85,10 +87,10 @@ class Matrix4 {
85
87
  return m;
86
88
  }
87
89
 
88
- static multiplyScalar(a, s) {
89
- const output = new Array(4);
90
+ static multiplyScalar(a: Matrix4_t, s: number): Matrix4_t {
91
+ const output = new Array(4) as Matrix4_t;
90
92
  for (let r = 0; r < 4; ++r) {
91
- output[r] = new Array(4);
93
+ output[r] = new Array(4) as Vector4_t;
92
94
  for (let c = 0; c < 4; ++c) {
93
95
  output[r][c] = a[r][c] * s;
94
96
  }
@@ -96,8 +98,8 @@ class Matrix4 {
96
98
  return output;
97
99
  }
98
100
 
99
- static multiplyVector(a, v) {
100
- const output = new Array(4);
101
+ static multiplyVector(a: Matrix4_t, v: Vector4_t): Vector4_t {
102
+ const output = new Array(4) as Vector4_t;
101
103
  for (let r = 0; r < 4; ++r) {
102
104
  output[r] = 0;
103
105
  for (let c = 0; c < 4; ++c) {
@@ -107,11 +109,11 @@ class Matrix4 {
107
109
  return output;
108
110
  }
109
111
 
110
- static multiplyVector3(a, v) {
111
- return Matrix4.multiplyVector(a, [...v, 1]).slice(0, 3);
112
+ static multiplyVector3(a: Matrix4_t, v: Vector3_t): Vector3_t {
113
+ return Matrix4.multiplyVector(a, [...v, 1]).slice(0, 3) as Vector3_t;
112
114
  }
113
115
 
114
- static transpose(m) {
116
+ static transpose(m: Matrix4_t): Matrix4_t {
115
117
  return [
116
118
  [m[0][0], m[1][0], m[2][0], m[3][0]],
117
119
  [m[0][1], m[1][1], m[2][1], m[3][1]],
@@ -120,7 +122,7 @@ class Matrix4 {
120
122
  ];
121
123
  }
122
124
 
123
- static invert(m) {
125
+ static invert(m: Matrix4_t): Matrix4_t | null {
124
126
  // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
125
127
  // implementation from ThreeJS
126
128
  const te = Matrix4.toVector(m),
@@ -138,7 +140,7 @@ class Matrix4 {
138
140
  const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
139
141
 
140
142
  if (det === 0) {
141
- throw Error('Matrix det = 0');
143
+ return null;
142
144
  }
143
145
 
144
146
  const detInv = 1 / det;
@@ -166,7 +168,7 @@ class Matrix4 {
166
168
  return Matrix4.fromVector(te);
167
169
  }
168
170
 
169
- static determinant(m) {
171
+ static determinant(m: Matrix4_t): number {
170
172
  // Based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
171
173
  // implementation from ThreeJS
172
174
 
@@ -212,7 +214,7 @@ class Matrix4 {
212
214
  );
213
215
  }
214
216
 
215
- static compose(translation, rotation, scale = [1, 1, 1]) {
217
+ static compose(translation: Vector3_t, rotation: Matrix3_t, scale: Vector3_t = [1, 1, 1]): Matrix4_t {
216
218
  // From Three JS
217
219
  // This composition corresponds to:
218
220
  // => translation * rotation * scale
@@ -225,12 +227,12 @@ class Matrix4 {
225
227
  ];
226
228
  }
227
229
 
228
- static decompose(m) {
230
+ static decompose(m: Matrix4_t): { translation: Vector3_t, rotation: Matrix3_t, scale: Vector3_t } {
229
231
 
230
232
  // Inspired by Three JS
231
233
  // m corresponds to:
232
234
  // => translation * rotation * scale
233
- const scale = [
235
+ const scale: Vector3_t = [
234
236
  Vector3.norm([m[0][0], m[1][0], m[2][0]]),
235
237
  Vector3.norm([m[0][1], m[1][1], m[2][1]]),
236
238
  Vector3.norm([m[0][2], m[1][2], m[2][2]])
@@ -242,13 +244,13 @@ class Matrix4 {
242
244
  scale[0] = -scale[0];
243
245
  }
244
246
 
245
- const translation = [m[0][3], m[1][3], m[2][3]];
247
+ const translation: Vector3_t = [m[0][3], m[1][3], m[2][3]];
246
248
 
247
249
  const invSX = 1 / scale[0];
248
250
  const invSY = 1 / scale[1];
249
251
  const invSZ = 1 / scale[2];
250
252
 
251
- const rotation = [
253
+ const rotation: Matrix3_t = [
252
254
  [m[0][0] * invSX, m[0][1] * invSY, m[0][2] * invSZ],
253
255
  [m[1][0] * invSX, m[1][1] * invSY, m[1][2] * invSZ],
254
256
  [m[2][0] * invSX, m[2][1] * invSY, m[2][2] * invSZ]
@@ -258,7 +260,7 @@ class Matrix4 {
258
260
 
259
261
  }
260
262
 
261
- static extractRotation(m) {
263
+ static extractRotation(m: Matrix4_t): Matrix3_t {
262
264
  const scaleX = 1 / Vector3.norm([m[0][0], m[1][0], m[2][0]]);
263
265
  const scaleY = 1 / Vector3.norm([m[0][1], m[1][1], m[2][1]]);
264
266
  const scaleZ = 1 / Vector3.norm([m[0][2], m[1][2], m[2][2]]);
@@ -269,11 +271,11 @@ class Matrix4 {
269
271
  ];
270
272
  }
271
273
 
272
- static extractTranslation(m) {
274
+ static extractTranslation(m: Matrix4_t): Vector3_t {
273
275
  return [m[0][3], m[1][3], m[2][3]];
274
276
  }
275
277
 
276
- static scaleTranslation(m, s) {
278
+ static scaleTranslation(m: Matrix4_t, s: Vector3_t): Matrix4_t {
277
279
  return [
278
280
  [m[0][0], m[0][1], m[0][2], m[0][3] * s[0]],
279
281
  [m[1][0], m[1][1], m[1][2], m[1][3] * s[1]],
@@ -282,12 +284,10 @@ class Matrix4 {
282
284
  ];
283
285
  }
284
286
 
285
- static toString(m) {
287
+ static toString(m: Matrix4_t): string {
286
288
  return '[\n'
287
- + m.map(row =>
288
- '\t[' + row.map(val => val.toFixed(2)).join(', ') + ']'
289
+ + m.map(row => '\t[' + row.map(val => val.toFixed(2)).join(', ') + ']'
289
290
  ).join(',\n')
290
291
  + '\n]';
291
292
  }
292
293
  }
293
- export default Matrix4;
@@ -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
  });