@wemap/maths 10.9.2 → 11.0.0-11.0.0-alpha.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{index.js → index.ts} +2 -1
- package/package.json +14 -5
- package/src/EulerAngles.ts +10 -0
- package/src/{Geometry.js → Geometry.ts} +10 -14
- package/src/{Matrix.js → Matrix.ts} +17 -12
- package/src/{Matrix3.spec.js → Matrix3.spec.ts} +13 -7
- package/src/{Matrix3.js → Matrix3.ts} +46 -21
- package/src/Matrix4.spec.ts +187 -0
- package/src/Matrix4.ts +302 -0
- package/src/{Point2.js → Point2.ts} +4 -2
- package/src/{Polygon.spec.js → Polygon.spec.ts} +3 -2
- package/src/{Polygon.js → Polygon.ts} +4 -3
- package/src/{Quaternion.spec.js → Quaternion.spec.ts} +20 -57
- package/src/{Quaternion.js → Quaternion.ts} +32 -31
- package/src/{Rotations.spec.js → Rotations.spec.ts} +4 -8
- package/src/{Rotations.js → Rotations.ts} +14 -16
- package/src/{Utils.spec.js → Utils.spec.ts} +2 -0
- package/src/{Utils.js → Utils.ts} +10 -23
- package/src/{Vector.spec.js → Vector.spec.ts} +1 -0
- package/src/Vector.ts +10 -0
- package/src/{Vector3.spec.js → Vector3.spec.ts} +4 -2
- package/src/{Vector3.js → Vector3.ts} +11 -10
- package/src/types.ts +21 -0
- package/tsconfig.json +3 -0
- package/vite.config.ts +4 -0
- package/dist/wemap-maths.es.js +0 -1100
- package/dist/wemap-maths.es.js.map +0 -1
- package/index.d.ts +0 -176
- package/src/EulerAngles.js +0 -8
- package/src/Matrix4.js +0 -158
- package/src/Matrix4.spec.js +0 -51
- package/src/Transformation.js +0 -40
- package/src/Transformation.spec.js +0 -79
- package/src/Vector.js +0 -8
- /package/src/{Matrix.spec.js → Matrix.spec.ts} +0 -0
- /package/src/{Point2.spec.js → Point2.spec.ts} +0 -0
package/{index.js → index.ts}
RENAMED
|
@@ -6,9 +6,10 @@ 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
|
|
|
13
12
|
export * from './src/Geometry.js';
|
|
14
13
|
export * from './src/Utils.js';
|
|
14
|
+
|
|
15
|
+
export * from './src/types.js';
|
package/package.json
CHANGED
|
@@ -5,20 +5,23 @@
|
|
|
5
5
|
"Guillaume Pannetier <guillaume.pannetier@getwemap.com>"
|
|
6
6
|
],
|
|
7
7
|
"description": "Wemap maths utils package",
|
|
8
|
-
"main": "index.js",
|
|
9
|
-
"types": "index.
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "index.ts",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "git+https://github.com/wemap/wemap-modules-js.git",
|
|
13
13
|
"directory": "packages/maths"
|
|
14
14
|
},
|
|
15
15
|
"name": "@wemap/maths",
|
|
16
|
-
"version": "
|
|
16
|
+
"version": "11.0.0-11.0.0-alpha.11.0",
|
|
17
17
|
"bugs": {
|
|
18
18
|
"url": "https://github.com/wemap/wemap-modules-js/issues"
|
|
19
19
|
},
|
|
20
20
|
"homepage": "https://github.com/wemap/wemap-modules-js#readme",
|
|
21
|
-
"scripts": {
|
|
21
|
+
"scripts": {
|
|
22
|
+
"dev": "vite build --watch",
|
|
23
|
+
"build": "vite build"
|
|
24
|
+
},
|
|
22
25
|
"type": "module",
|
|
23
26
|
"keywords": [
|
|
24
27
|
"utils",
|
|
@@ -26,5 +29,11 @@
|
|
|
26
29
|
"wemap"
|
|
27
30
|
],
|
|
28
31
|
"license": "ISC",
|
|
29
|
-
"
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": "./dist/index.mjs",
|
|
35
|
+
"require": "./dist/index.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"gitHead": "2f6d0bf397ca7d3cf6a2664a6e55c0e4c07fb65a"
|
|
30
39
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EulerAnglesDegrees_t, EulerAngles_t } from './types';
|
|
2
|
+
|
|
3
|
+
class EulerAngles {
|
|
4
|
+
|
|
5
|
+
static toString([yaw, pitch, roll]: EulerAngles_t | EulerAnglesDegrees_t) {
|
|
6
|
+
return `[${yaw.toFixed(2)}, ${pitch.toFixed(2)}, ${roll.toFixed(2)}]`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
export default EulerAngles;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Line_t, Polygon_t } from './types';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @param {Number[][]} line1
|
|
3
5
|
* @param {Number[][]} line2
|
|
@@ -5,20 +7,18 @@
|
|
|
5
7
|
* line extremity intersect the other line
|
|
6
8
|
* @returns {Boolean}
|
|
7
9
|
*/
|
|
8
|
-
export function linesIntersect(line1, line2, detectIntersectionAtBounds = true) {
|
|
10
|
+
export function linesIntersect(line1: Line_t, line2: Line_t, detectIntersectionAtBounds = true): boolean {
|
|
9
11
|
|
|
10
12
|
const [[p0x, p0y], [p1x, p1y]] = line1;
|
|
11
13
|
const [[p2x, p2y], [p3x, p3y]] = line2;
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
s2y = p3y - p2y;
|
|
15
|
+
const s1x = p1x - p0x;
|
|
16
|
+
const s1y = p1y - p0y;
|
|
17
|
+
const s2x = p3x - p2x;
|
|
18
|
+
const s2y = p3y - p2y;
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
t = (s2x * (p0y - p2y) - s2y * (p0x - p2x)) / (-s2x * s1y + s1x * s2y);
|
|
20
|
+
const s = (-s1y * (p0x - p2x) + s1x * (p0y - p2y)) / (-s2x * s1y + s1x * s2y);
|
|
21
|
+
const t = (s2x * (p0y - p2y) - s2y * (p0x - p2x)) / (-s2x * s1y + s1x * s2y);
|
|
22
22
|
|
|
23
23
|
if (!detectIntersectionAtBounds) {
|
|
24
24
|
return s > 0 && s < 1 && t > 0 && t < 1;
|
|
@@ -26,11 +26,7 @@ export function linesIntersect(line1, line2, detectIntersectionAtBounds = true)
|
|
|
26
26
|
return s >= 0 && s <= 1 && t >= 0 && t <= 1;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
* @param {Number[][]} polygon
|
|
31
|
-
* @returns {Boolean}
|
|
32
|
-
*/
|
|
33
|
-
export function isPolygonClockwise(polygon) {
|
|
29
|
+
export function isPolygonClockwise(polygon: Polygon_t): boolean {
|
|
34
30
|
|
|
35
31
|
let sum = 0;
|
|
36
32
|
for (let i = 0; i < polygon.length; i++) {
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/* eslint-disable max-statements */
|
|
2
2
|
/* eslint max-depth: 0 */
|
|
3
|
+
|
|
4
|
+
import { Matrix_t, Vector3_t, Vector_t } from './types';
|
|
5
|
+
|
|
3
6
|
class Matrix {
|
|
4
7
|
|
|
5
|
-
static sum(a, b) {
|
|
8
|
+
static sum(a: Matrix_t, b: Matrix_t): Matrix_t {
|
|
6
9
|
const aNumRows = a.length;
|
|
7
10
|
const aNumCols = a[0].length;
|
|
8
11
|
|
|
@@ -16,7 +19,7 @@ class Matrix {
|
|
|
16
19
|
return m;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
static concatRow(a, b) {
|
|
22
|
+
static concatRow(a: Matrix_t, b: Matrix_t): Matrix_t {
|
|
20
23
|
const aNumRows = a.length;
|
|
21
24
|
const bNumRows = b.length;
|
|
22
25
|
|
|
@@ -32,7 +35,7 @@ class Matrix {
|
|
|
32
35
|
return m;
|
|
33
36
|
}
|
|
34
37
|
|
|
35
|
-
static concatLine(a, b) {
|
|
38
|
+
static concatLine(a: Matrix_t, b: Matrix_t): Matrix_t {
|
|
36
39
|
const aNumRows = a.length;
|
|
37
40
|
const m = new Array(aNumRows);
|
|
38
41
|
for (let r = 0; r < aNumRows; ++r) {
|
|
@@ -41,7 +44,7 @@ class Matrix {
|
|
|
41
44
|
return m;
|
|
42
45
|
}
|
|
43
46
|
|
|
44
|
-
static diag(arr) {
|
|
47
|
+
static diag(arr: Vector_t): Matrix_t {
|
|
45
48
|
const m = Array(arr.length);
|
|
46
49
|
for (let i = 0; i < arr.length; i++) {
|
|
47
50
|
m[i] = Array(arr.length);
|
|
@@ -52,7 +55,7 @@ class Matrix {
|
|
|
52
55
|
return m;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
static transpose(a) {
|
|
58
|
+
static transpose(a: Matrix_t): Matrix_t {
|
|
56
59
|
|
|
57
60
|
const numRows = a.length;
|
|
58
61
|
const numCols = a[0].length;
|
|
@@ -69,7 +72,7 @@ class Matrix {
|
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
|
|
72
|
-
static multiply(a, b) {
|
|
75
|
+
static multiply(a: Matrix_t, b: Matrix_t): Matrix_t {
|
|
73
76
|
const aNumRows = a.length;
|
|
74
77
|
const aNumCols = a[0].length;
|
|
75
78
|
const bNumCols = b[0].length;
|
|
@@ -89,7 +92,7 @@ class Matrix {
|
|
|
89
92
|
return m;
|
|
90
93
|
}
|
|
91
94
|
|
|
92
|
-
static multiplyScalar(a, s) {
|
|
95
|
+
static multiplyScalar(a: Matrix_t, s: number): Matrix_t {
|
|
93
96
|
const aNumRows = a.length;
|
|
94
97
|
const aNumCols = a[0].length;
|
|
95
98
|
|
|
@@ -103,11 +106,11 @@ class Matrix {
|
|
|
103
106
|
return m;
|
|
104
107
|
}
|
|
105
108
|
|
|
106
|
-
static multiplyVector(a, v) {
|
|
109
|
+
static multiplyVector(a: Matrix_t, v: Vector_t): Vector_t {
|
|
107
110
|
const aNumRows = a.length;
|
|
108
111
|
const aNumCols = a[0].length;
|
|
109
112
|
|
|
110
|
-
const output = new Array(aNumRows);
|
|
113
|
+
const output = new Array(aNumRows) as Vector3_t;
|
|
111
114
|
for (let r = 0; r < aNumRows; ++r) {
|
|
112
115
|
output[r] = 0;
|
|
113
116
|
for (let c = 0; c < aNumCols; ++c) {
|
|
@@ -119,7 +122,7 @@ class Matrix {
|
|
|
119
122
|
|
|
120
123
|
// Returns the inverse of matrix M
|
|
121
124
|
// Helped from http://blog.acipo.com/matrix-inversion-in-javascript/
|
|
122
|
-
static inverse(M) {
|
|
125
|
+
static inverse(M: Matrix_t): Matrix_t | null {
|
|
123
126
|
// I use Guassian Elimination to calculate the inverse:
|
|
124
127
|
// (1) 'augment' the matrix (left) by the identity (on the right)
|
|
125
128
|
// (2) Turn the matrix on the left into the identity by elemetry row ops
|
|
@@ -135,8 +138,9 @@ class Matrix {
|
|
|
135
138
|
}
|
|
136
139
|
|
|
137
140
|
// create the identity matrix (I), and a copy (C) of the original
|
|
138
|
-
let i = 0, ii = 0, j = 0,
|
|
139
|
-
const
|
|
141
|
+
let i = 0, ii = 0, j = 0, e = 0;
|
|
142
|
+
const dim = M.length;
|
|
143
|
+
const I: Matrix_t = [], C: Matrix_t = [];
|
|
140
144
|
for (i = 0; i < dim; i += 1) {
|
|
141
145
|
// Create the row
|
|
142
146
|
I[I.length] = [];
|
|
@@ -234,4 +238,5 @@ class Matrix {
|
|
|
234
238
|
}
|
|
235
239
|
|
|
236
240
|
}
|
|
241
|
+
|
|
237
242
|
export default Matrix;
|
|
@@ -2,23 +2,29 @@ import chai from 'chai';
|
|
|
2
2
|
import chaiAlmost from 'chai-almost';
|
|
3
3
|
|
|
4
4
|
import Matrix3 from './Matrix3.js';
|
|
5
|
+
import { Matrix3_t, Vector3_t } from './types.js';
|
|
5
6
|
|
|
6
7
|
const { expect } = chai;
|
|
7
8
|
chai.use(chaiAlmost());
|
|
8
9
|
|
|
9
|
-
const
|
|
10
|
+
export const expectIsRotMat = (m: Matrix3_t) => {
|
|
11
|
+
expect(Matrix3.determinant(m)).almost(1);
|
|
12
|
+
expect(Matrix3.multiply(m, Matrix3.transpose(m))).deep.almost(Matrix3.identity);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const m1: Matrix3_t = [
|
|
10
16
|
[1, 2, 3],
|
|
11
17
|
[4, 5, 6],
|
|
12
18
|
[7, 8, 9]
|
|
13
19
|
];
|
|
14
20
|
|
|
15
|
-
const m2 = [
|
|
21
|
+
const m2: Matrix3_t = [
|
|
16
22
|
[1, 2, 3],
|
|
17
23
|
[0, 1, 5],
|
|
18
24
|
[5, 6, 0]
|
|
19
25
|
];
|
|
20
26
|
|
|
21
|
-
const v1 = [1, 2, 3];
|
|
27
|
+
const v1: Vector3_t = [1, 2, 3];
|
|
22
28
|
|
|
23
29
|
describe('Matrix3', () => {
|
|
24
30
|
|
|
@@ -90,17 +96,14 @@ describe('Matrix3', () => {
|
|
|
90
96
|
|
|
91
97
|
it('inverse', () => {
|
|
92
98
|
let result;
|
|
93
|
-
|
|
94
99
|
result = Matrix3.inverse(m2);
|
|
95
100
|
expect(result).deep.almost([
|
|
96
101
|
[-6, 18 / 5, 7 / 5],
|
|
97
102
|
[5, -3, -1],
|
|
98
103
|
[-1, 4 / 5, 1 / 5]
|
|
99
104
|
]);
|
|
100
|
-
|
|
101
105
|
result = Matrix3.inverse([[1, 2, 0], [3, 4, 0], [5, 6, 0]]);
|
|
102
106
|
expect(result).is.null;
|
|
103
|
-
|
|
104
107
|
});
|
|
105
108
|
|
|
106
109
|
it('rotX', () => {
|
|
@@ -110,6 +113,7 @@ describe('Matrix3', () => {
|
|
|
110
113
|
[0, 0, -1],
|
|
111
114
|
[0, 1, 0]
|
|
112
115
|
]);
|
|
116
|
+
expectIsRotMat(result);
|
|
113
117
|
});
|
|
114
118
|
|
|
115
119
|
it('rotY', () => {
|
|
@@ -119,14 +123,16 @@ describe('Matrix3', () => {
|
|
|
119
123
|
[0, 1, 0],
|
|
120
124
|
[-1, 0, 0]
|
|
121
125
|
]);
|
|
126
|
+
expectIsRotMat(result);
|
|
122
127
|
});
|
|
123
128
|
|
|
124
129
|
it('rotZ', () => {
|
|
125
130
|
const result = Matrix3.rotZ(Math.PI / 2);
|
|
126
131
|
expect(result).deep.almost([
|
|
127
132
|
[0, -1, 0],
|
|
128
|
-
[
|
|
133
|
+
[1, 0, 0],
|
|
129
134
|
[0, 0, 1]
|
|
130
135
|
]);
|
|
136
|
+
expectIsRotMat(result);
|
|
131
137
|
});
|
|
132
138
|
});
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
import { Matrix3_t, Vector3_t } from "./types";
|
|
2
|
+
|
|
1
3
|
class Matrix3 {
|
|
2
4
|
|
|
3
|
-
static get identity() {
|
|
5
|
+
static get identity(): Matrix3_t {
|
|
4
6
|
return [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
5
7
|
}
|
|
6
8
|
|
|
7
|
-
static get zeros() {
|
|
9
|
+
static get zeros(): Matrix3_t {
|
|
8
10
|
return [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
static diag(arr) {
|
|
13
|
+
static diag(arr: Vector3_t): Matrix3_t {
|
|
12
14
|
return [
|
|
13
15
|
[arr[0], 0, 0],
|
|
14
16
|
[0, arr[1], 0],
|
|
@@ -16,7 +18,7 @@ class Matrix3 {
|
|
|
16
18
|
];
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
static sum(m1, m2) {
|
|
21
|
+
static sum(m1: Matrix3_t, m2: Matrix3_t): Matrix3_t {
|
|
20
22
|
return [
|
|
21
23
|
[m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2]],
|
|
22
24
|
[m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2]],
|
|
@@ -24,7 +26,15 @@ class Matrix3 {
|
|
|
24
26
|
];
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
static
|
|
29
|
+
static subtract(m1: Matrix3_t, m2: Matrix3_t): Matrix3_t {
|
|
30
|
+
return [
|
|
31
|
+
[m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2]],
|
|
32
|
+
[m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2]],
|
|
33
|
+
[m1[2][0] - m2[2][0], m1[2][1] - m2[2][1], m1[2][2] - m2[2][2]]
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static rotX(theta: number): Matrix3_t {
|
|
28
38
|
return [
|
|
29
39
|
[1, 0, 0],
|
|
30
40
|
[0, Math.cos(theta), -Math.sin(theta)],
|
|
@@ -32,7 +42,7 @@ class Matrix3 {
|
|
|
32
42
|
];
|
|
33
43
|
}
|
|
34
44
|
|
|
35
|
-
static rotY(theta) {
|
|
45
|
+
static rotY(theta: number): Matrix3_t {
|
|
36
46
|
return [
|
|
37
47
|
[Math.cos(theta), 0, Math.sin(theta)],
|
|
38
48
|
[0, 1, 0],
|
|
@@ -40,15 +50,15 @@ class Matrix3 {
|
|
|
40
50
|
];
|
|
41
51
|
}
|
|
42
52
|
|
|
43
|
-
static rotZ(theta) {
|
|
53
|
+
static rotZ(theta: number): Matrix3_t {
|
|
44
54
|
return [
|
|
45
55
|
[Math.cos(theta), -Math.sin(theta), 0],
|
|
46
|
-
[
|
|
56
|
+
[Math.sin(theta), Math.cos(theta), 0],
|
|
47
57
|
[0, 0, 1]
|
|
48
58
|
];
|
|
49
59
|
}
|
|
50
60
|
|
|
51
|
-
static transpose(m) {
|
|
61
|
+
static transpose(m: Matrix3_t): Matrix3_t {
|
|
52
62
|
return [
|
|
53
63
|
[m[0][0], m[1][0], m[2][0]],
|
|
54
64
|
[m[0][1], m[1][1], m[2][1]],
|
|
@@ -56,7 +66,7 @@ class Matrix3 {
|
|
|
56
66
|
];
|
|
57
67
|
}
|
|
58
68
|
|
|
59
|
-
static multiplyScalar(m, s) {
|
|
69
|
+
static multiplyScalar(m: Matrix3_t, s: number): Matrix3_t {
|
|
60
70
|
return [
|
|
61
71
|
[m[0][0] * s, m[0][1] * s, m[0][2] * s],
|
|
62
72
|
[m[1][0] * s, m[1][1] * s, m[1][2] * s],
|
|
@@ -64,8 +74,8 @@ class Matrix3 {
|
|
|
64
74
|
];
|
|
65
75
|
}
|
|
66
76
|
|
|
67
|
-
static multiplyVector(a, v) {
|
|
68
|
-
const output = new Array(3);
|
|
77
|
+
static multiplyVector(a: Matrix3_t, v: Vector3_t): Vector3_t {
|
|
78
|
+
const output = new Array(3) as Vector3_t;
|
|
69
79
|
for (let r = 0; r < 3; ++r) {
|
|
70
80
|
output[r] = 0;
|
|
71
81
|
for (let c = 0; c < 3; ++c) {
|
|
@@ -75,10 +85,10 @@ class Matrix3 {
|
|
|
75
85
|
return output;
|
|
76
86
|
}
|
|
77
87
|
|
|
78
|
-
static multiply(a, b) {
|
|
79
|
-
const m = new Array(3);
|
|
88
|
+
static multiply(a: Matrix3_t, b: Matrix3_t): Matrix3_t {
|
|
89
|
+
const m = new Array(3) as Matrix3_t;
|
|
80
90
|
for (let r = 0; r < 3; ++r) {
|
|
81
|
-
m[r] = new Array(3);
|
|
91
|
+
m[r] = new Array(3) as [number, number, number];
|
|
82
92
|
for (let c = 0; c < 3; ++c) {
|
|
83
93
|
m[r][c] = 0;
|
|
84
94
|
for (let i = 0; i < 3; ++i) {
|
|
@@ -89,7 +99,7 @@ class Matrix3 {
|
|
|
89
99
|
return m;
|
|
90
100
|
}
|
|
91
101
|
|
|
92
|
-
static skew(v) {
|
|
102
|
+
static skew(v: Vector3_t): Matrix3_t {
|
|
93
103
|
return [
|
|
94
104
|
[0, -v[2], v[1]],
|
|
95
105
|
[v[2], 0, -v[0]],
|
|
@@ -98,9 +108,9 @@ class Matrix3 {
|
|
|
98
108
|
}
|
|
99
109
|
|
|
100
110
|
// Helped from https://github.com/mrdoob/three.js/blob/34dc2478c684066257e4e39351731a93c6107ef5/src/math/Matrix3.js#L183
|
|
101
|
-
static inverse(me) {
|
|
111
|
+
static inverse(me: Matrix3_t): Matrix3_t | null {
|
|
102
112
|
|
|
103
|
-
const te = new Array(3),
|
|
113
|
+
const te = new Array(3) as Matrix3_t,
|
|
104
114
|
|
|
105
115
|
n11 = me[0][0], n21 = me[0][1], n31 = me[0][2],
|
|
106
116
|
n12 = me[1][0], n22 = me[1][1], n32 = me[1][2],
|
|
@@ -116,9 +126,9 @@ class Matrix3 {
|
|
|
116
126
|
return null;
|
|
117
127
|
}
|
|
118
128
|
|
|
119
|
-
te[0] = new Array(3);
|
|
120
|
-
te[1] = new Array(3);
|
|
121
|
-
te[2] = new Array(3);
|
|
129
|
+
te[0] = new Array(3) as Vector3_t;
|
|
130
|
+
te[1] = new Array(3) as Vector3_t;
|
|
131
|
+
te[2] = new Array(3) as Vector3_t;
|
|
122
132
|
|
|
123
133
|
const detInv = 1 / det;
|
|
124
134
|
|
|
@@ -136,5 +146,20 @@ class Matrix3 {
|
|
|
136
146
|
|
|
137
147
|
return te;
|
|
138
148
|
}
|
|
149
|
+
|
|
150
|
+
static determinant(m: Matrix3_t): number {
|
|
151
|
+
|
|
152
|
+
const a = m[0][0], b = m[1][0], c = m[2][0],
|
|
153
|
+
d = m[0][1], e = m[1][1], f = m[2][1],
|
|
154
|
+
g = m[0][2], h = m[1][2], i = m[2][2];
|
|
155
|
+
|
|
156
|
+
return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
static trace(m: Matrix3_t): number {
|
|
161
|
+
return m[0][0] + m[1][1] + m[2][2];
|
|
162
|
+
}
|
|
139
163
|
}
|
|
164
|
+
|
|
140
165
|
export default Matrix3;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/* eslint-disable max-statements */
|
|
2
|
+
import chai from 'chai';
|
|
3
|
+
import chaiAlmost from 'chai-almost';
|
|
4
|
+
|
|
5
|
+
import Matrix3 from './Matrix3.js';
|
|
6
|
+
import Matrix4 from './Matrix4.js';
|
|
7
|
+
|
|
8
|
+
import { expectIsRotMat } from './Matrix3.spec.js';
|
|
9
|
+
import { Matrix3_t, Matrix4_t, Vector3_t } from './types.js';
|
|
10
|
+
|
|
11
|
+
const { expect } = chai;
|
|
12
|
+
chai.use(chaiAlmost());
|
|
13
|
+
|
|
14
|
+
const expectIsPose = (m: Matrix4_t) => {
|
|
15
|
+
expectIsRotMat(Matrix4.extractRotation(m));
|
|
16
|
+
expect(m[3]).deep.almost([0, 0, 0, 1]);
|
|
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);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const expectIsAffineTransform = (m: Matrix4_t) => {
|
|
23
|
+
expectIsRotMat(Matrix4.extractRotation(m));
|
|
24
|
+
expect(m[3]).deep.almost([0, 0, 0, 1]);
|
|
25
|
+
// expect(Matrix4.determinant(m)).almost(1); // not true in case of scale
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const m1: Matrix4_t = [
|
|
29
|
+
[1, 2, 3, 4],
|
|
30
|
+
[5, 6, 7, 8],
|
|
31
|
+
[9, 0, 1, 2],
|
|
32
|
+
[3, 4, 5, 6]
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const m2: Matrix4_t = [
|
|
36
|
+
[1, 2, 3, 4],
|
|
37
|
+
[0, 1, 5, 2],
|
|
38
|
+
[5, 6, 0, 9],
|
|
39
|
+
[3, 1, 9, 3]
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
describe('Matrix4', () => {
|
|
44
|
+
|
|
45
|
+
it('identity', () => {
|
|
46
|
+
expect(Matrix4.identity).deep.equal([
|
|
47
|
+
[1, 0, 0, 0],
|
|
48
|
+
[0, 1, 0, 0],
|
|
49
|
+
[0, 0, 1, 0],
|
|
50
|
+
[0, 0, 0, 1]
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('sum', () => {
|
|
55
|
+
const result = Matrix4.sum(m1, m2);
|
|
56
|
+
expect(result).deep.equal([
|
|
57
|
+
[2, 4, 6, 8],
|
|
58
|
+
[5, 7, 12, 10],
|
|
59
|
+
[14, 6, 1, 11],
|
|
60
|
+
[6, 5, 14, 9]
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('subtract', () => {
|
|
65
|
+
const result = Matrix4.subtract(m1, m2);
|
|
66
|
+
expect(result).deep.equal([
|
|
67
|
+
[0, 0, 0, 0],
|
|
68
|
+
[5, 5, 2, 6],
|
|
69
|
+
[4, -6, 1, -7],
|
|
70
|
+
[0, 3, -4, 3]
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
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;
|
|
75
|
+
|
|
76
|
+
it('compose/decompose', () => {
|
|
77
|
+
rot1 = Matrix3.rotX(Math.PI / 2);
|
|
78
|
+
tr1 = [0, 1, 0];
|
|
79
|
+
t1 = Matrix4.compose(tr1, rot1);
|
|
80
|
+
expect(t1).deep .almost([
|
|
81
|
+
[1, 0, 0, 0],
|
|
82
|
+
[0, 0, -1, 1],
|
|
83
|
+
[0, 1, 0, 0],
|
|
84
|
+
[0, 0, 0, 1]
|
|
85
|
+
]);
|
|
86
|
+
rot2 = Matrix3.multiply(rot1, Matrix3.rotZ(Math.PI / 2));
|
|
87
|
+
tr2 = [2, 1, 0];
|
|
88
|
+
t2 = Matrix4.compose(tr2, rot2);
|
|
89
|
+
expect(t2).deep.almost([
|
|
90
|
+
[0, -1, 0, 2],
|
|
91
|
+
[0, 0, -1, 1],
|
|
92
|
+
[1, 0, 0, 0],
|
|
93
|
+
[0, 0, 0, 1]
|
|
94
|
+
]);
|
|
95
|
+
sc2 = [2, 3, 3];
|
|
96
|
+
const translationMat = Matrix4.compose(tr2, Matrix3.identity);
|
|
97
|
+
const rotationMat = Matrix4.compose([0, 0, 0], rot2);
|
|
98
|
+
const scaleMat = Matrix4.fromDiagVector([...sc2, 1]);
|
|
99
|
+
const expectedComposed = Matrix4.multiply(Matrix4.multiply(translationMat, rotationMat), scaleMat);
|
|
100
|
+
const composed = Matrix4.compose(tr2, rot2, sc2);
|
|
101
|
+
expect(composed).deep.almost(expectedComposed);
|
|
102
|
+
const { rotation: estRot2, translation: estTr2, scale: estSc2 } = Matrix4.decompose(composed);
|
|
103
|
+
expect(estRot2).deep.almost(rot2);
|
|
104
|
+
expect(estTr2).deep.almost(tr2);
|
|
105
|
+
expect(estSc2).deep.almost(sc2);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('extractRotation', () => {
|
|
109
|
+
expect(Matrix4.extractRotation(t1)).deep.almost(rot1);
|
|
110
|
+
expect(Matrix4.extractRotation(t2)).deep.almost(rot2);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('extractTranslation', () => {
|
|
114
|
+
expect(Matrix4.extractTranslation(t1)).deep.almost(tr1);
|
|
115
|
+
expect(Matrix4.extractTranslation(t2)).deep.almost(tr2);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('invert', () => {
|
|
119
|
+
expect(Matrix4.invert(t1)).deep.almost([
|
|
120
|
+
[1, 0, 0, 0],
|
|
121
|
+
[0, 0, 1, 0],
|
|
122
|
+
[0, -1, 0, 1],
|
|
123
|
+
[0, 0, 0, 1]
|
|
124
|
+
]);
|
|
125
|
+
expect(Matrix4.invert(t2)).deep.almost([
|
|
126
|
+
[0, 0, 1, 0],
|
|
127
|
+
[-1, 0, 0, 2],
|
|
128
|
+
[0, -1, 0, 1],
|
|
129
|
+
[0, 0, 0, 1]
|
|
130
|
+
]);
|
|
131
|
+
const tr3 = Matrix4.compose([1, 2, 3], Matrix3.rotY(Math.PI / 2), [2, 2, 4]);
|
|
132
|
+
const invertTr3 = Matrix4.invert(tr3);
|
|
133
|
+
expect(invertTr3).deep.almost([
|
|
134
|
+
[0, 0, -0.5, 1.5],
|
|
135
|
+
[0, 0.5, 0, -1],
|
|
136
|
+
[0.25, 0, 0, -0.25],
|
|
137
|
+
[0, 0, 0, 1]
|
|
138
|
+
]);
|
|
139
|
+
expect(Matrix4.multiply(tr3, invertTr3 as Matrix4_t)).deep.almost(Matrix4.identity);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('multiply - Translation', () => {
|
|
143
|
+
const position: Vector3_t = [1, 2, 3];
|
|
144
|
+
const rotation = Matrix3.rotY(Math.PI / 2);
|
|
145
|
+
const pose = Matrix4.compose(position, rotation);
|
|
146
|
+
const transform = Matrix4.compose([8, 1, 5], Matrix3.identity);
|
|
147
|
+
expectIsAffineTransform(transform);
|
|
148
|
+
const positionTransformed = Matrix4.multiplyVector3(transform, position);
|
|
149
|
+
expect(positionTransformed).deep.almost([9, 3, 8]);
|
|
150
|
+
const poseTransformed = Matrix4.multiply(transform, pose);
|
|
151
|
+
expectIsPose(poseTransformed);
|
|
152
|
+
expect(Matrix4.extractTranslation(poseTransformed)).deep.almost(positionTransformed);
|
|
153
|
+
expect(Matrix4.extractRotation(poseTransformed)).deep.almost(rotation);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('multiply - Rotation', () => {
|
|
157
|
+
const position: Vector3_t = [1, 2, 3];
|
|
158
|
+
const rotation = Matrix3.rotY(Math.PI / 2);
|
|
159
|
+
const pose = Matrix4.compose(position, rotation);
|
|
160
|
+
const transformRotation = Matrix3.multiply(Matrix3.rotY(Math.PI / 2), Matrix3.rotZ(Math.PI / 4));
|
|
161
|
+
const transform = Matrix4.compose([0, 0, 0], transformRotation);
|
|
162
|
+
expectIsAffineTransform(transform);
|
|
163
|
+
const positionTransformed = Matrix4.multiplyVector3(transform, position);
|
|
164
|
+
const rotationTransformed = Matrix3.multiply(transformRotation, rotation);
|
|
165
|
+
const poseTransformed = Matrix4.multiply(transform, pose);
|
|
166
|
+
expectIsPose(poseTransformed);
|
|
167
|
+
expect(Matrix4.extractTranslation(poseTransformed)).deep.almost(positionTransformed);
|
|
168
|
+
expect(Matrix4.extractRotation(poseTransformed)).deep.almost(rotationTransformed);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('multiply - Translation + Rotation + Scale', () => {
|
|
172
|
+
// Pose
|
|
173
|
+
const localPose = Matrix4.compose([1, 2, 3], Matrix3.multiply(Matrix3.rotX(Math.PI / 2), Matrix3.rotZ(Math.PI / 4)));
|
|
174
|
+
const cameraPose = Matrix4.invert(localPose) as Matrix4_t;
|
|
175
|
+
// Transform
|
|
176
|
+
const transform = Matrix4.compose([3, 1, 8], Matrix3.rotX(-Math.PI / 8));
|
|
177
|
+
// scale must be a scalar
|
|
178
|
+
const scale: Vector3_t = [3, 3, 3];
|
|
179
|
+
const transformInverse = Matrix4.invert(transform) as Matrix4_t;
|
|
180
|
+
// Local pose transform
|
|
181
|
+
const localPoseTransformed = Matrix4.scaleTranslation(Matrix4.multiply(transform, localPose), scale);
|
|
182
|
+
// Camera pose transform
|
|
183
|
+
const cameraPoseTransformed = Matrix4.scaleTranslation(Matrix4.multiply(cameraPose, transformInverse), scale);
|
|
184
|
+
const localPoseTransformedEstimated = Matrix4.invert(cameraPoseTransformed);
|
|
185
|
+
expect(localPoseTransformed).deep.almost(localPoseTransformedEstimated);
|
|
186
|
+
});
|
|
187
|
+
});
|