@wemap/maths 10.9.2 → 10.10.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.d.ts CHANGED
@@ -21,7 +21,6 @@ declare module '@wemap/maths' {
21
21
  export type Matrix_t = number[][];
22
22
  export type EulerAngles_t = Vector3_t;
23
23
  export type EulerAnglesDegrees_t = Vector3_t;
24
- export type Transformation_t = Matrix4_t;
25
24
 
26
25
  // Geometry
27
26
  export function linesIntersect(line1: Line_t, line2: Line_t, detectIntersectionAtBounds?: number): boolean;
@@ -112,6 +111,7 @@ declare module '@wemap/maths' {
112
111
  static get identity(): Matrix3_t;
113
112
  static get zeros(): Matrix3_t;
114
113
 
114
+ static determinant(m: Matrix3_t): number;
115
115
  static diag(arr: Vector3_t): Matrix3_t;
116
116
  static inverse(q: Matrix3_t): Matrix3_t;
117
117
  static multiply(a: Matrix3_t, b: Matrix3_t): Matrix3_t;
@@ -121,6 +121,7 @@ declare module '@wemap/maths' {
121
121
  static rotY(theta: number): Matrix3_t;
122
122
  static rotZ(theta: number): Matrix3_t;
123
123
  static skew(v: Vector3_t): Matrix3_t;
124
+ static subtract(m1: Matrix3_t, m2: Matrix3_t): Matrix3_t;
124
125
  static sum(m1: Matrix3_t, m2: Matrix3_t): Matrix3_t;
125
126
  static transpose(m: Matrix3_t): Matrix3_t;
126
127
  }
@@ -128,6 +129,12 @@ declare module '@wemap/maths' {
128
129
  export class Matrix4 {
129
130
  static get identity(): Matrix4_t;
130
131
 
132
+ static compose(translation: Vector3_t, rotation: Matrix3_t, scale?: Vector3_t): Matrix4_t;
133
+ static decompose(m: Matrix4_t): {translation: Vector3_t, rotation: Matrix3_t, scale: Vector3_t};
134
+ static determinant(m: Matrix4_t): number;
135
+ static extractRotation(m: Matrix4_t): Matrix3_t;
136
+ static extractTranslation(m: Matrix4_t): Vector3_t;
137
+ static fromDiagVector(v: Vector4_t): Matrix4_t;
131
138
  static fromMatrix3(m: Matrix3_t): Matrix4_t;
132
139
  // Column-Major ordering like Eigen and ThreeJS
133
140
  static fromVector(v: Vector16_t): Matrix4_t;
@@ -136,8 +143,10 @@ declare module '@wemap/maths' {
136
143
  static multiplyScalar(a: Matrix4_t, b: number): Matrix4_t;
137
144
  static multiplyVector(a: Matrix4_t, v: Vector4_t): Vector4_t;
138
145
  static multiplyVector3(a: Matrix4_t, v: Vector3_t): Vector3_t;
139
- static sum(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t;
146
+ static scaleTranslation(m: Matrix4_t, v: Vector3_t): Matrix4_t;
140
147
  static subtract(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t;
148
+ static sum(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t;
149
+ static toString(m: Matrix4_t): string;
141
150
  // Column-Major ordering like Eigen and ThreeJS
142
151
  static toVector(m: Matrix4_t): Vector16_t;
143
152
  static transpose(m: Matrix4_t): Matrix4_t;
@@ -164,13 +173,5 @@ declare module '@wemap/maths' {
164
173
  static quaternionToEuler(quaternion: Quaternion_t, order: string): EulerAngles_t;
165
174
  }
166
175
 
167
- export class Transformation {
168
- static extractRotation(m: Transformation_t): Matrix3_t;
169
- static extractTranslation(m: Transformation_t): Vector3_t;
170
- static fromPose(rot: Matrix3_t, tr: Vector3_t): Transformation_t;
171
- static invert(m: Transformation_t): Transformation_t;
172
- static scaleScalar(a: Transformation_t, b: number): Transformation_t;
173
- }
174
-
175
176
  }
176
177
 
package/index.js CHANGED
@@ -6,7 +6,6 @@ export { default as Point2 } from './src/Point2.js';
6
6
  export { default as Polygon } from './src/Polygon.js';
7
7
  export { default as Quaternion } from './src/Quaternion.js';
8
8
  export { default as Rotations } from './src/Rotations.js';
9
- export { default as Transformation } from './src/Transformation.js';
10
9
  export { default as Vector } from './src/Vector.js';
11
10
  export { default as Vector3 } from './src/Vector3.js';
12
11
 
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "directory": "packages/maths"
14
14
  },
15
15
  "name": "@wemap/maths",
16
- "version": "10.9.2",
16
+ "version": "10.10.0",
17
17
  "bugs": {
18
18
  "url": "https://github.com/wemap/wemap-modules-js/issues"
19
19
  },
@@ -26,5 +26,5 @@
26
26
  "wemap"
27
27
  ],
28
28
  "license": "ISC",
29
- "gitHead": "0afef92de08cc4f39da9ef182b093fe54ac9b7da"
29
+ "gitHead": "61c5899e7feb7b75f228a9ee6dc6e8c3e39beb11"
30
30
  }
package/src/Matrix3.js CHANGED
@@ -24,6 +24,14 @@ class Matrix3 {
24
24
  ];
25
25
  }
26
26
 
27
+ static subtract(m1, m2) {
28
+ return [
29
+ [m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2]],
30
+ [m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2]],
31
+ [m1[2][0] - m2[2][0], m1[2][1] - m2[2][1], m1[2][2] - m2[2][2]]
32
+ ];
33
+ }
34
+
27
35
  static rotX(theta) {
28
36
  return [
29
37
  [1, 0, 0],
@@ -43,7 +51,7 @@ class Matrix3 {
43
51
  static rotZ(theta) {
44
52
  return [
45
53
  [Math.cos(theta), -Math.sin(theta), 0],
46
- [-Math.sin(theta), Math.cos(theta), 0],
54
+ [Math.sin(theta), Math.cos(theta), 0],
47
55
  [0, 0, 1]
48
56
  ];
49
57
  }
@@ -136,5 +144,19 @@ class Matrix3 {
136
144
 
137
145
  return te;
138
146
  }
147
+
148
+ static determinant(m) {
149
+
150
+ const a = m[0][0], b = m[1][0], c = m[2][0],
151
+ d = m[0][1], e = m[1][1], f = m[2][1],
152
+ g = m[0][2], h = m[1][2], i = m[2][2];
153
+
154
+ return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
155
+
156
+ }
157
+
158
+ static trace(m) {
159
+ return m[0][0] + m[1][1] + m[2][2];
160
+ }
139
161
  }
140
162
  export default Matrix3;
@@ -6,6 +6,11 @@ import Matrix3 from './Matrix3.js';
6
6
  const { expect } = chai;
7
7
  chai.use(chaiAlmost());
8
8
 
9
+ export const expectIsRotMat = (m) => {
10
+ expect(Matrix3.determinant(m)).almost(1);
11
+ expect(Matrix3.multiply(m, Matrix3.transpose(m))).deep.almost(Matrix3.identity);
12
+ };
13
+
9
14
  const m1 = [
10
15
  [1, 2, 3],
11
16
  [4, 5, 6],
@@ -110,6 +115,7 @@ describe('Matrix3', () => {
110
115
  [0, 0, -1],
111
116
  [0, 1, 0]
112
117
  ]);
118
+ expectIsRotMat(result);
113
119
  });
114
120
 
115
121
  it('rotY', () => {
@@ -119,14 +125,16 @@ describe('Matrix3', () => {
119
125
  [0, 1, 0],
120
126
  [-1, 0, 0]
121
127
  ]);
128
+ expectIsRotMat(result);
122
129
  });
123
130
 
124
131
  it('rotZ', () => {
125
132
  const result = Matrix3.rotZ(Math.PI / 2);
126
133
  expect(result).deep.almost([
127
134
  [0, -1, 0],
128
- [-1, 0, 0],
135
+ [1, 0, 0],
129
136
  [0, 0, 1]
130
137
  ]);
138
+ expectIsRotMat(result);
131
139
  });
132
140
  });
package/src/Matrix4.js CHANGED
@@ -1,3 +1,5 @@
1
+ import Vector3 from './Vector3.js';
2
+
1
3
  class Matrix4 {
2
4
 
3
5
  static get identity() {
@@ -36,6 +38,15 @@ class Matrix4 {
36
38
  ];
37
39
  }
38
40
 
41
+ static fromDiagVector(v) {
42
+ return [
43
+ [v[0], 0, 0, 0],
44
+ [0, v[1], 0, 0],
45
+ [0, 0, v[2], 0],
46
+ [0, 0, 0, v[3]]
47
+ ];
48
+ }
49
+
39
50
  /**
40
51
  * Column-Major ordering like Eigen and ThreeJS
41
52
  */
@@ -77,9 +88,9 @@ class Matrix4 {
77
88
  static multiplyScalar(a, s) {
78
89
  const output = new Array(4);
79
90
  for (let r = 0; r < 4; ++r) {
80
- output[r] = 0;
91
+ output[r] = new Array(4);
81
92
  for (let c = 0; c < 4; ++c) {
82
- output[r] += a[r][c] * s;
93
+ output[r][c] = a[r][c] * s;
83
94
  }
84
95
  }
85
96
  return output;
@@ -154,5 +165,129 @@ class Matrix4 {
154
165
 
155
166
  return Matrix4.fromVector(te);
156
167
  }
168
+
169
+ static determinant(m) {
170
+ // Based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
171
+ // implementation from ThreeJS
172
+
173
+ const n11 = m[0][0], n12 = m[1][0], n13 = m[2][0], n14 = m[3][0];
174
+ const n21 = m[0][1], n22 = m[1][1], n23 = m[2][1], n24 = m[3][1];
175
+ const n31 = m[0][2], n32 = m[1][2], n33 = m[2][2], n34 = m[3][2];
176
+ const n41 = m[0][3], n42 = m[1][3], n43 = m[2][3], n44 = m[3][3];
177
+
178
+ return (
179
+ n41 * (
180
+ Number(n14) * n23 * n32
181
+ - n13 * n24 * n32
182
+ - n14 * n22 * n33
183
+ + n12 * n24 * n33
184
+ + n13 * n22 * n34
185
+ - n12 * n23 * n34
186
+ )
187
+ + n42 * (
188
+ Number(n11) * n23 * n34
189
+ - n11 * n24 * n33
190
+ + n14 * n21 * n33
191
+ - n13 * n21 * n34
192
+ + n13 * n24 * n31
193
+ - n14 * n23 * n31
194
+ )
195
+ + n43 * (
196
+ Number(n11) * n24 * n32
197
+ - n11 * n22 * n34
198
+ - n14 * n21 * n32
199
+ + n12 * n21 * n34
200
+ + n14 * n22 * n31
201
+ - n12 * n24 * n31
202
+ )
203
+ + n44 * (
204
+ - n13 * n22 * n31
205
+ - n11 * n23 * n32
206
+ + n11 * n22 * n33
207
+ + n13 * n21 * n32
208
+ - n12 * n21 * n33
209
+ + n12 * n23 * n31
210
+ )
211
+
212
+ );
213
+ }
214
+
215
+ static compose(translation, rotation, scale = [1, 1, 1]) {
216
+ // From Three JS
217
+ // This composition corresponds to:
218
+ // => translation * rotation * scale
219
+ const [sx, sy, sz] = scale;
220
+ return [
221
+ [rotation[0][0] * sx, rotation[0][1] * sy, rotation[0][2] * sz, translation[0]],
222
+ [rotation[1][0] * sx, rotation[1][1] * sy, rotation[1][2] * sz, translation[1]],
223
+ [rotation[2][0] * sx, rotation[2][1] * sy, rotation[2][2] * sz, translation[2]],
224
+ [0, 0, 0, 1]
225
+ ];
226
+ }
227
+
228
+ static decompose(m) {
229
+
230
+ // Inspired by Three JS
231
+ // m corresponds to:
232
+ // => translation * rotation * scale
233
+ const scale = [
234
+ Vector3.norm([m[0][0], m[1][0], m[2][0]]),
235
+ Vector3.norm([m[0][1], m[1][1], m[2][1]]),
236
+ Vector3.norm([m[0][2], m[1][2], m[2][2]])
237
+ ];
238
+
239
+ // if determine is negative, we need to invert one scale
240
+ const det = Matrix4.determinant(m);
241
+ if (det < 0) {
242
+ scale[0] = -scale[0];
243
+ }
244
+
245
+ const translation = [m[0][3], m[1][3], m[2][3]];
246
+
247
+ const invSX = 1 / scale[0];
248
+ const invSY = 1 / scale[1];
249
+ const invSZ = 1 / scale[2];
250
+
251
+ const rotation = [
252
+ [m[0][0] * invSX, m[0][1] * invSY, m[0][2] * invSZ],
253
+ [m[1][0] * invSX, m[1][1] * invSY, m[1][2] * invSZ],
254
+ [m[2][0] * invSX, m[2][1] * invSY, m[2][2] * invSZ]
255
+ ];
256
+
257
+ return { translation, scale, rotation };
258
+
259
+ }
260
+
261
+ static extractRotation(m) {
262
+ const scaleX = 1 / Vector3.norm([m[0][0], m[1][0], m[2][0]]);
263
+ const scaleY = 1 / Vector3.norm([m[0][1], m[1][1], m[2][1]]);
264
+ const scaleZ = 1 / Vector3.norm([m[0][2], m[1][2], m[2][2]]);
265
+ return [
266
+ [m[0][0] * scaleX, m[0][1] * scaleY, m[0][2] * scaleZ],
267
+ [m[1][0] * scaleX, m[1][1] * scaleY, m[1][2] * scaleZ],
268
+ [m[2][0] * scaleX, m[2][1] * scaleY, m[2][2] * scaleZ]
269
+ ];
270
+ }
271
+
272
+ static extractTranslation(m) {
273
+ return [m[0][3], m[1][3], m[2][3]];
274
+ }
275
+
276
+ static scaleTranslation(m, s) {
277
+ return [
278
+ [m[0][0], m[0][1], m[0][2], m[0][3] * s[0]],
279
+ [m[1][0], m[1][1], m[1][2], m[1][3] * s[1]],
280
+ [m[2][0], m[2][1], m[2][2], m[2][3] * s[2]],
281
+ [m[3][0], m[3][1], m[3][2], m[3][3]]
282
+ ];
283
+ }
284
+
285
+ static toString(m) {
286
+ return '[\n'
287
+ + m.map(row =>
288
+ '\t[' + row.map(val => val.toFixed(2)).join(', ') + ']'
289
+ ).join(',\n')
290
+ + '\n]';
291
+ }
157
292
  }
158
293
  export default Matrix4;
@@ -1,8 +1,26 @@
1
+ /* eslint-disable max-statements */
1
2
  import chai from 'chai';
3
+ import chaiAlmost from 'chai-almost';
2
4
 
5
+ import Matrix3 from './Matrix3.js';
3
6
  import Matrix4 from './Matrix4.js';
4
7
 
8
+ import { expectIsRotMat } from './Matrix3.spec.js';
9
+
5
10
  const { expect } = chai;
11
+ chai.use(chaiAlmost());
12
+
13
+ const expectIsPose = (m) => {
14
+ expectIsRotMat(Matrix4.extractRotation(m));
15
+ expect(m[3]).deep.almost([0, 0, 0, 1]);
16
+ expect(Matrix4.multiply(m, Matrix4.invert(m))).deep.almost(Matrix4.identity);
17
+ };
18
+
19
+ const expectIsAffineTransform = (m) => {
20
+ expectIsRotMat(Matrix4.extractRotation(m));
21
+ expect(m[3]).deep.almost([0, 0, 0, 1]);
22
+ // expect(Matrix4.determinant(m)).almost(1); // not true in case of scale
23
+ };
6
24
 
7
25
  const m1 = [
8
26
  [1, 2, 3, 4],
@@ -48,4 +66,148 @@ describe('Matrix4', () => {
48
66
  [0, 3, -4, 3]
49
67
  ]);
50
68
  });
69
+
70
+ let t1, t2;
71
+ let rot1, tr1, rot2, tr2, sc2;
72
+
73
+ it('compose/decompose', () => {
74
+ rot1 = Matrix3.rotX(Math.PI / 2);
75
+ tr1 = [0, 1, 0];
76
+ t1 = Matrix4.compose(tr1, rot1);
77
+ expect(t1).deep.almost([
78
+ [1, 0, 0, 0],
79
+ [0, 0, -1, 1],
80
+ [0, 1, 0, 0],
81
+ [0, 0, 0, 1]
82
+ ]);
83
+
84
+ rot2 = Matrix3.multiply(rot1, Matrix3.rotZ(Math.PI / 2));
85
+ tr2 = [2, 1, 0];
86
+ t2 = Matrix4.compose(tr2, rot2);
87
+ expect(t2).deep.almost([
88
+ [0, -1, 0, 2],
89
+ [0, 0, -1, 1],
90
+ [1, 0, 0, 0],
91
+ [0, 0, 0, 1]
92
+ ]);
93
+
94
+ sc2 = [2, 3, 3];
95
+ const translationMat = Matrix4.compose(tr2, Matrix3.identity);
96
+ const rotationMat = Matrix4.compose([0, 0, 0], rot2);
97
+ const scaleMat = Matrix4.fromDiagVector([...sc2, 1]);
98
+ const expectedComposed = Matrix4.multiply(Matrix4.multiply(translationMat, rotationMat), scaleMat);
99
+
100
+ const composed = Matrix4.compose(tr2, rot2, sc2);
101
+ expect(composed).deep.almost(expectedComposed);
102
+
103
+ const { rotation: estRot2, translation: estTr2, scale: estSc2 } = Matrix4.decompose(composed);
104
+ expect(estRot2).deep.almost(rot2);
105
+ expect(estTr2).deep.almost(tr2);
106
+ expect(estSc2).deep.almost(sc2);
107
+ });
108
+
109
+
110
+ it('extractRotation', () => {
111
+ expect(Matrix4.extractRotation(t1)).deep.almost(rot1);
112
+ expect(Matrix4.extractRotation(t2)).deep.almost(rot2);
113
+ });
114
+
115
+ it('extractTranslation', () => {
116
+ expect(Matrix4.extractTranslation(t1)).deep.almost(tr1);
117
+ expect(Matrix4.extractTranslation(t2)).deep.almost(tr2);
118
+ });
119
+
120
+ it('invert', () => {
121
+ expect(Matrix4.invert(t1)).deep.almost([
122
+ [1, 0, 0, 0],
123
+ [0, 0, 1, 0],
124
+ [0, -1, 0, 1],
125
+ [0, 0, 0, 1]
126
+ ]);
127
+
128
+ expect(Matrix4.invert(t2)).deep.almost([
129
+ [0, 0, 1, 0],
130
+ [-1, 0, 0, 2],
131
+ [0, -1, 0, 1],
132
+ [0, 0, 0, 1]
133
+ ]);
134
+
135
+ const tr3 = Matrix4.compose([1, 2, 3], Matrix3.rotY(Math.PI / 2), [2, 2, 4]);
136
+ const invertTr3 = Matrix4.invert(tr3);
137
+ expect(invertTr3).deep.almost([
138
+ [0, 0, -0.5, 1.5],
139
+ [0, 0.5, 0, -1],
140
+ [0.25, 0, 0, -0.25],
141
+ [0, 0, 0, 1]
142
+ ]);
143
+ expect(Matrix4.multiply(tr3, invertTr3)).deep.almost(Matrix4.identity);
144
+ });
145
+
146
+
147
+ it('multiply - Translation', () => {
148
+
149
+ const position = [1, 2, 3];
150
+ const rotation = Matrix3.rotY(Math.PI / 2);
151
+ const pose = Matrix4.compose(position, rotation);
152
+
153
+ const transform = Matrix4.compose([8, 1, 5], Matrix3.identity);
154
+ expectIsAffineTransform(transform);
155
+
156
+ const positionTransformed = Matrix4.multiplyVector3(transform, position);
157
+ expect(positionTransformed).deep.almost([9, 3, 8]);
158
+ const poseTransformed = Matrix4.multiply(transform, pose);
159
+ expectIsPose(poseTransformed);
160
+ expect(Matrix4.extractTranslation(poseTransformed)).deep.almost(positionTransformed);
161
+ expect(Matrix4.extractRotation(poseTransformed)).deep.almost(rotation);
162
+ });
163
+
164
+
165
+ it('multiply - Rotation', () => {
166
+
167
+ const position = [1, 2, 3];
168
+ const rotation = Matrix3.rotY(Math.PI / 2);
169
+ const pose = Matrix4.compose(position, rotation);
170
+
171
+ const transformRotation = Matrix3.multiply(Matrix3.rotY(Math.PI / 2), Matrix3.rotZ(Math.PI / 4));
172
+ const transform = Matrix4.compose([0, 0, 0], transformRotation);
173
+
174
+ expectIsAffineTransform(transform);
175
+
176
+ const positionTransformed = Matrix4.multiplyVector3(transform, position);
177
+ const rotationTransformed = Matrix3.multiply(transformRotation, rotation);
178
+ const poseTransformed = Matrix4.multiply(transform, pose);
179
+ expectIsPose(poseTransformed);
180
+ expect(Matrix4.extractTranslation(poseTransformed)).deep.almost(positionTransformed);
181
+ expect(Matrix4.extractRotation(poseTransformed)).deep.almost(rotationTransformed);
182
+
183
+ });
184
+
185
+ it('multiply - Translation + Rotation + Scale', () => {
186
+
187
+ // Pose
188
+ 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
+
191
+ // Transform
192
+ const transform = Matrix4.compose([3, 1, 8], Matrix3.rotX(-Math.PI / 8));
193
+ // scale must be a scalar
194
+ const scale = [3, 3, 3];
195
+ const transformInverse = Matrix4.invert(transform);
196
+
197
+ // Local pose transform
198
+ const localPoseTransformed = Matrix4.scaleTranslation(
199
+ Matrix4.multiply(transform, localPose),
200
+ scale
201
+ );
202
+
203
+ // Camera pose transform
204
+ const cameraPoseTransformed = Matrix4.scaleTranslation(
205
+ Matrix4.multiply(cameraPose, transformInverse),
206
+ scale
207
+ );
208
+ const localPoseTransformedEstimated = Matrix4.invert(cameraPoseTransformed);
209
+
210
+ expect(localPoseTransformed).deep.almost(localPoseTransformedEstimated);
211
+ });
212
+
51
213
  });
@@ -1,40 +0,0 @@
1
- import Matrix4 from './Matrix4.js';
2
-
3
- class Transformation {
4
-
5
- static extractRotation(m) {
6
- return [
7
- [m[0][0], m[0][1], m[0][2]],
8
- [m[1][0], m[1][1], m[1][2]],
9
- [m[2][0], m[2][1], m[2][2]]
10
- ];
11
- }
12
-
13
- static extractTranslation(m) {
14
- return [m[0][3], m[1][3], m[2][3]];
15
- }
16
-
17
- static fromPose(m, v) {
18
- return [
19
- [m[0][0], m[0][1], m[0][2], v[0]],
20
- [m[1][0], m[1][1], m[1][2], v[1]],
21
- [m[2][0], m[2][1], m[2][2], v[2]],
22
- [0, 0, 0, 1]
23
- ];
24
- }
25
-
26
- static scaleScalar(m, s) {
27
- return [
28
- [m[0][0], m[0][1], m[0][2], m[0][3]],
29
- [m[1][0], m[1][1], m[1][2], m[1][3]],
30
- [m[2][0], m[2][1], m[2][2], m[2][3]],
31
- [0, 0, 0, 1 / s]
32
- ];
33
- }
34
-
35
- static invert(m) {
36
- return Matrix4.invert(m);
37
- }
38
- }
39
-
40
- export default Transformation;
@@ -1,79 +0,0 @@
1
- import chai from 'chai';
2
- import chaiAlmost from 'chai-almost';
3
-
4
- import Matrix3 from './Matrix3.js';
5
- import Matrix4 from './Matrix4.js';
6
- import Transformation from './Transformation.js';
7
-
8
-
9
- const { expect } = chai;
10
- chai.use(chaiAlmost());
11
-
12
- describe('Transformation', () => {
13
-
14
- let t1, t2;
15
- let rot1, tr1, rot2, tr2;
16
-
17
- it('fromPose', () => {
18
- rot1 = Matrix3.rotX(Math.PI / 2);
19
- tr1 = [0, 1, 0];
20
- t1 = Transformation.fromPose(rot1, tr1);
21
- expect(t1).deep.almost([
22
- [1, 0, 0, 0],
23
- [0, 0, -1, 1],
24
- [0, 1, 0, 0],
25
- [0, 0, 0, 1]
26
- ]);
27
-
28
- rot2 = Matrix3.multiply(rot1, Matrix3.rotZ(Math.PI / 2));
29
- tr2 = [2, 1, 0];
30
- t2 = Transformation.fromPose(rot2, tr2);
31
- expect(t2).deep.almost([
32
- [0, -1, 0, 2],
33
- [0, 0, -1, 1],
34
- [-1, 0, 0, 0],
35
- [0, 0, 0, 1]
36
- ]);
37
- });
38
-
39
- it('extractRotation', () => {
40
- expect(Transformation.extractRotation(t1)).deep.almost(rot1);
41
- expect(Transformation.extractRotation(t2)).deep.almost(rot2);
42
- });
43
-
44
- it('extractTranslation', () => {
45
- expect(Transformation.extractTranslation(t1)).deep.almost(tr1);
46
- expect(Transformation.extractTranslation(t2)).deep.almost(tr2);
47
- });
48
-
49
- it('invert', () => {
50
- expect(Transformation.invert(t1)).deep.almost([
51
- [1, 0, 0, 0],
52
- [0, 0, 1, 0],
53
- [0, -1, 0, 1],
54
- [0, 0, 0, 1]
55
- ]);
56
-
57
- expect(Transformation.invert(t2)).deep.almost([
58
- [0, 0, -1, 0],
59
- [-1, 0, 0, 2],
60
- [0, -1, 0, 1],
61
- [0, 0, 0, 1]
62
- ]);
63
-
64
- const tr3 = Transformation.scaleScalar(
65
- Transformation.fromPose(
66
- Matrix3.rotY(Math.PI / 2),
67
- [1, 2, 3]
68
- ), 2);
69
- const invertTr3 = Transformation.invert(tr3);
70
- expect(invertTr3).deep.almost([
71
- [0, 0, -1, 6],
72
- [0, 1, 0, -4],
73
- [1, 0, 0, -2],
74
- [0, 0, 0, 2]
75
- ]);
76
- expect(Matrix4.multiply(tr3, invertTr3)).deep.almost(Matrix4.identity);
77
- });
78
-
79
- });