@wemap/maths 10.9.1 → 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 +12 -10
- package/index.js +0 -1
- package/package.json +2 -2
- package/src/Matrix3.js +23 -1
- package/src/Matrix3.spec.js +9 -1
- package/src/Matrix4.js +183 -2
- package/src/Matrix4.spec.js +162 -0
- package/src/Transformation.js +0 -45
- package/src/Transformation.spec.js +0 -76
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,15 +129,24 @@ 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;
|
|
141
|
+
static invert(m: Matrix4_t): Matrix4_t;
|
|
134
142
|
static multiply(a: Matrix4_t, b: Matrix4_t): Matrix4_t;
|
|
135
143
|
static multiplyScalar(a: Matrix4_t, b: number): Matrix4_t;
|
|
136
144
|
static multiplyVector(a: Matrix4_t, v: Vector4_t): Vector4_t;
|
|
137
145
|
static multiplyVector3(a: Matrix4_t, v: Vector3_t): Vector3_t;
|
|
138
|
-
static
|
|
146
|
+
static scaleTranslation(m: Matrix4_t, v: Vector3_t): Matrix4_t;
|
|
139
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;
|
|
140
150
|
// Column-Major ordering like Eigen and ThreeJS
|
|
141
151
|
static toVector(m: Matrix4_t): Vector16_t;
|
|
142
152
|
static transpose(m: Matrix4_t): Matrix4_t;
|
|
@@ -163,13 +173,5 @@ declare module '@wemap/maths' {
|
|
|
163
173
|
static quaternionToEuler(quaternion: Quaternion_t, order: string): EulerAngles_t;
|
|
164
174
|
}
|
|
165
175
|
|
|
166
|
-
export class Transformation {
|
|
167
|
-
static extractRotation(m: Transformation_t): Matrix3_t;
|
|
168
|
-
static extractTranslation(m: Transformation_t): Vector3_t;
|
|
169
|
-
static fromPose(rot: Matrix3_t, tr: Vector3_t): Transformation_t;
|
|
170
|
-
static invert(m: Transformation_t): Transformation_t;
|
|
171
|
-
static scaleScalar(a: Transformation_t, b: number): Transformation_t;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
176
|
}
|
|
175
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.
|
|
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": "
|
|
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
|
-
[
|
|
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;
|
package/src/Matrix3.spec.js
CHANGED
|
@@ -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
|
-
[
|
|
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] =
|
|
91
|
+
output[r] = new Array(4);
|
|
81
92
|
for (let c = 0; c < 4; ++c) {
|
|
82
|
-
output[r]
|
|
93
|
+
output[r][c] = a[r][c] * s;
|
|
83
94
|
}
|
|
84
95
|
}
|
|
85
96
|
return output;
|
|
@@ -108,5 +119,175 @@ class Matrix4 {
|
|
|
108
119
|
[m[0][3], m[1][3], m[2][3], m[3][3]]
|
|
109
120
|
];
|
|
110
121
|
}
|
|
122
|
+
|
|
123
|
+
static invert(m) {
|
|
124
|
+
// based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
|
|
125
|
+
// implementation from ThreeJS
|
|
126
|
+
const te = Matrix4.toVector(m),
|
|
127
|
+
|
|
128
|
+
n11 = te[0], n21 = te[1], n31 = te[2], n41 = te[3],
|
|
129
|
+
n12 = te[4], n22 = te[5], n32 = te[6], n42 = te[7],
|
|
130
|
+
n13 = te[8], n23 = te[9], n33 = te[10], n43 = te[11],
|
|
131
|
+
n14 = te[12], n24 = te[13], n34 = te[14], n44 = te[15],
|
|
132
|
+
|
|
133
|
+
t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
|
|
134
|
+
t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
|
|
135
|
+
t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
|
|
136
|
+
t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
|
|
137
|
+
|
|
138
|
+
const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
|
|
139
|
+
|
|
140
|
+
if (det === 0) {
|
|
141
|
+
throw Error('Matrix det = 0');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const detInv = 1 / det;
|
|
145
|
+
|
|
146
|
+
te[0] = t11 * detInv;
|
|
147
|
+
te[1] = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv;
|
|
148
|
+
te[2] = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv;
|
|
149
|
+
te[3] = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv;
|
|
150
|
+
|
|
151
|
+
te[4] = t12 * detInv;
|
|
152
|
+
te[5] = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv;
|
|
153
|
+
te[6] = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv;
|
|
154
|
+
te[7] = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv;
|
|
155
|
+
|
|
156
|
+
te[8] = t13 * detInv;
|
|
157
|
+
te[9] = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv;
|
|
158
|
+
te[10] = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv;
|
|
159
|
+
te[11] = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv;
|
|
160
|
+
|
|
161
|
+
te[12] = t14 * detInv;
|
|
162
|
+
te[13] = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv;
|
|
163
|
+
te[14] = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv;
|
|
164
|
+
te[15] = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv;
|
|
165
|
+
|
|
166
|
+
return Matrix4.fromVector(te);
|
|
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
|
+
}
|
|
111
292
|
}
|
|
112
293
|
export default Matrix4;
|
package/src/Matrix4.spec.js
CHANGED
|
@@ -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
|
});
|
package/src/Transformation.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import Matrix3 from './Matrix3.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] * s, m[0][1], m[0][2], m[0][3]],
|
|
29
|
-
[m[1][0], m[1][1] * s, m[1][2], m[1][3]],
|
|
30
|
-
[m[2][0], m[2][1], m[2][2] * s, m[2][3]],
|
|
31
|
-
[0, 0, 0, 1]
|
|
32
|
-
];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static invert(m) {
|
|
36
|
-
const rot = Transformation.extractRotation(m);
|
|
37
|
-
// Do not use transpose here due to scale on transformation matrix
|
|
38
|
-
const invRot = Matrix3.inverse(rot);
|
|
39
|
-
const tr = Transformation.extractTranslation(m);
|
|
40
|
-
const invTr = Matrix3.multiplyVector(invRot, tr).map((val) => -1 * val);
|
|
41
|
-
return Transformation.fromPose(invRot, invTr);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export default Transformation;
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import chai from 'chai';
|
|
2
|
-
import chaiAlmost from 'chai-almost';
|
|
3
|
-
|
|
4
|
-
import Matrix3 from './Matrix3.js';
|
|
5
|
-
import Transformation from './Transformation.js';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const { expect } = chai;
|
|
9
|
-
chai.use(chaiAlmost());
|
|
10
|
-
|
|
11
|
-
describe('Transformation', () => {
|
|
12
|
-
|
|
13
|
-
let t1, t2;
|
|
14
|
-
let rot1, tr1, rot2, tr2;
|
|
15
|
-
|
|
16
|
-
it('fromPose', () => {
|
|
17
|
-
rot1 = Matrix3.rotX(Math.PI / 2);
|
|
18
|
-
tr1 = [0, 1, 0];
|
|
19
|
-
t1 = Transformation.fromPose(rot1, tr1);
|
|
20
|
-
expect(t1).deep.almost([
|
|
21
|
-
[1, 0, 0, 0],
|
|
22
|
-
[0, 0, -1, 1],
|
|
23
|
-
[0, 1, 0, 0],
|
|
24
|
-
[0, 0, 0, 1]
|
|
25
|
-
]);
|
|
26
|
-
|
|
27
|
-
rot2 = Matrix3.multiply(rot1, Matrix3.rotZ(Math.PI / 2));
|
|
28
|
-
tr2 = [2, 1, 0];
|
|
29
|
-
t2 = Transformation.fromPose(rot2, tr2);
|
|
30
|
-
expect(t2).deep.almost([
|
|
31
|
-
[0, -1, 0, 2],
|
|
32
|
-
[0, 0, -1, 1],
|
|
33
|
-
[-1, 0, 0, 0],
|
|
34
|
-
[0, 0, 0, 1]
|
|
35
|
-
]);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('extractRotation', () => {
|
|
39
|
-
expect(Transformation.extractRotation(t1)).deep.almost(rot1);
|
|
40
|
-
expect(Transformation.extractRotation(t2)).deep.almost(rot2);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('extractTranslation', () => {
|
|
44
|
-
expect(Transformation.extractTranslation(t1)).deep.almost(tr1);
|
|
45
|
-
expect(Transformation.extractTranslation(t2)).deep.almost(tr2);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('invert', () => {
|
|
49
|
-
expect(Transformation.invert(t1)).deep.almost([
|
|
50
|
-
[1, 0, 0, 0],
|
|
51
|
-
[0, 0, 1, 0],
|
|
52
|
-
[0, -1, 0, 1],
|
|
53
|
-
[0, 0, 0, 1]
|
|
54
|
-
]);
|
|
55
|
-
|
|
56
|
-
expect(Transformation.invert(t2)).deep.almost([
|
|
57
|
-
[0, 0, -1, 0],
|
|
58
|
-
[-1, 0, 0, 2],
|
|
59
|
-
[0, -1, 0, 1],
|
|
60
|
-
[0, 0, 0, 1]
|
|
61
|
-
]);
|
|
62
|
-
|
|
63
|
-
const tr3 = Transformation.scaleScalar(
|
|
64
|
-
Transformation.fromPose(
|
|
65
|
-
Matrix3.rotY(Math.PI / 2),
|
|
66
|
-
[1, 2, 3]
|
|
67
|
-
), 2);
|
|
68
|
-
expect(Transformation.invert(tr3)).deep.almost([
|
|
69
|
-
[0, 0, -1, 6],
|
|
70
|
-
[0, 0.5, 0, -2],
|
|
71
|
-
[1, 0, 0, -2],
|
|
72
|
-
[0, 0, 0, 1]
|
|
73
|
-
]);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
});
|