@wemap/maths 10.10.0 → 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 -0
- 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} +5 -7
- package/src/{Matrix3.js → Matrix3.ts} +26 -23
- package/src/{Matrix4.spec.js → Matrix4.spec.ts} +20 -46
- package/src/{Matrix4.js → Matrix4.ts} +43 -34
- 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 -177
- package/src/EulerAngles.js +0 -8
- 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
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,28 +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
|
-
export const expectIsRotMat = (m) => {
|
|
10
|
+
export const expectIsRotMat = (m: Matrix3_t) => {
|
|
10
11
|
expect(Matrix3.determinant(m)).almost(1);
|
|
11
12
|
expect(Matrix3.multiply(m, Matrix3.transpose(m))).deep.almost(Matrix3.identity);
|
|
12
13
|
};
|
|
13
14
|
|
|
14
|
-
const m1 = [
|
|
15
|
+
const m1: Matrix3_t = [
|
|
15
16
|
[1, 2, 3],
|
|
16
17
|
[4, 5, 6],
|
|
17
18
|
[7, 8, 9]
|
|
18
19
|
];
|
|
19
20
|
|
|
20
|
-
const m2 = [
|
|
21
|
+
const m2: Matrix3_t = [
|
|
21
22
|
[1, 2, 3],
|
|
22
23
|
[0, 1, 5],
|
|
23
24
|
[5, 6, 0]
|
|
24
25
|
];
|
|
25
26
|
|
|
26
|
-
const v1 = [1, 2, 3];
|
|
27
|
+
const v1: Vector3_t = [1, 2, 3];
|
|
27
28
|
|
|
28
29
|
describe('Matrix3', () => {
|
|
29
30
|
|
|
@@ -95,17 +96,14 @@ describe('Matrix3', () => {
|
|
|
95
96
|
|
|
96
97
|
it('inverse', () => {
|
|
97
98
|
let result;
|
|
98
|
-
|
|
99
99
|
result = Matrix3.inverse(m2);
|
|
100
100
|
expect(result).deep.almost([
|
|
101
101
|
[-6, 18 / 5, 7 / 5],
|
|
102
102
|
[5, -3, -1],
|
|
103
103
|
[-1, 4 / 5, 1 / 5]
|
|
104
104
|
]);
|
|
105
|
-
|
|
106
105
|
result = Matrix3.inverse([[1, 2, 0], [3, 4, 0], [5, 6, 0]]);
|
|
107
106
|
expect(result).is.null;
|
|
108
|
-
|
|
109
107
|
});
|
|
110
108
|
|
|
111
109
|
it('rotX', () => {
|
|
@@ -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,7 @@ class Matrix3 {
|
|
|
24
26
|
];
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
static subtract(m1, m2) {
|
|
29
|
+
static subtract(m1: Matrix3_t, m2: Matrix3_t): Matrix3_t {
|
|
28
30
|
return [
|
|
29
31
|
[m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2]],
|
|
30
32
|
[m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2]],
|
|
@@ -32,7 +34,7 @@ class Matrix3 {
|
|
|
32
34
|
];
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
static rotX(theta) {
|
|
37
|
+
static rotX(theta: number): Matrix3_t {
|
|
36
38
|
return [
|
|
37
39
|
[1, 0, 0],
|
|
38
40
|
[0, Math.cos(theta), -Math.sin(theta)],
|
|
@@ -40,7 +42,7 @@ class Matrix3 {
|
|
|
40
42
|
];
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
static rotY(theta) {
|
|
45
|
+
static rotY(theta: number): Matrix3_t {
|
|
44
46
|
return [
|
|
45
47
|
[Math.cos(theta), 0, Math.sin(theta)],
|
|
46
48
|
[0, 1, 0],
|
|
@@ -48,7 +50,7 @@ class Matrix3 {
|
|
|
48
50
|
];
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
static rotZ(theta) {
|
|
53
|
+
static rotZ(theta: number): Matrix3_t {
|
|
52
54
|
return [
|
|
53
55
|
[Math.cos(theta), -Math.sin(theta), 0],
|
|
54
56
|
[Math.sin(theta), Math.cos(theta), 0],
|
|
@@ -56,7 +58,7 @@ class Matrix3 {
|
|
|
56
58
|
];
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
static transpose(m) {
|
|
61
|
+
static transpose(m: Matrix3_t): Matrix3_t {
|
|
60
62
|
return [
|
|
61
63
|
[m[0][0], m[1][0], m[2][0]],
|
|
62
64
|
[m[0][1], m[1][1], m[2][1]],
|
|
@@ -64,7 +66,7 @@ class Matrix3 {
|
|
|
64
66
|
];
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
static multiplyScalar(m, s) {
|
|
69
|
+
static multiplyScalar(m: Matrix3_t, s: number): Matrix3_t {
|
|
68
70
|
return [
|
|
69
71
|
[m[0][0] * s, m[0][1] * s, m[0][2] * s],
|
|
70
72
|
[m[1][0] * s, m[1][1] * s, m[1][2] * s],
|
|
@@ -72,8 +74,8 @@ class Matrix3 {
|
|
|
72
74
|
];
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
static multiplyVector(a, v) {
|
|
76
|
-
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;
|
|
77
79
|
for (let r = 0; r < 3; ++r) {
|
|
78
80
|
output[r] = 0;
|
|
79
81
|
for (let c = 0; c < 3; ++c) {
|
|
@@ -83,10 +85,10 @@ class Matrix3 {
|
|
|
83
85
|
return output;
|
|
84
86
|
}
|
|
85
87
|
|
|
86
|
-
static multiply(a, b) {
|
|
87
|
-
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;
|
|
88
90
|
for (let r = 0; r < 3; ++r) {
|
|
89
|
-
m[r] = new Array(3);
|
|
91
|
+
m[r] = new Array(3) as [number, number, number];
|
|
90
92
|
for (let c = 0; c < 3; ++c) {
|
|
91
93
|
m[r][c] = 0;
|
|
92
94
|
for (let i = 0; i < 3; ++i) {
|
|
@@ -97,7 +99,7 @@ class Matrix3 {
|
|
|
97
99
|
return m;
|
|
98
100
|
}
|
|
99
101
|
|
|
100
|
-
static skew(v) {
|
|
102
|
+
static skew(v: Vector3_t): Matrix3_t {
|
|
101
103
|
return [
|
|
102
104
|
[0, -v[2], v[1]],
|
|
103
105
|
[v[2], 0, -v[0]],
|
|
@@ -106,9 +108,9 @@ class Matrix3 {
|
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
// Helped from https://github.com/mrdoob/three.js/blob/34dc2478c684066257e4e39351731a93c6107ef5/src/math/Matrix3.js#L183
|
|
109
|
-
static inverse(me) {
|
|
111
|
+
static inverse(me: Matrix3_t): Matrix3_t | null {
|
|
110
112
|
|
|
111
|
-
const te = new Array(3),
|
|
113
|
+
const te = new Array(3) as Matrix3_t,
|
|
112
114
|
|
|
113
115
|
n11 = me[0][0], n21 = me[0][1], n31 = me[0][2],
|
|
114
116
|
n12 = me[1][0], n22 = me[1][1], n32 = me[1][2],
|
|
@@ -124,9 +126,9 @@ class Matrix3 {
|
|
|
124
126
|
return null;
|
|
125
127
|
}
|
|
126
128
|
|
|
127
|
-
te[0] = new Array(3);
|
|
128
|
-
te[1] = new Array(3);
|
|
129
|
-
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;
|
|
130
132
|
|
|
131
133
|
const detInv = 1 / det;
|
|
132
134
|
|
|
@@ -145,7 +147,7 @@ class Matrix3 {
|
|
|
145
147
|
return te;
|
|
146
148
|
}
|
|
147
149
|
|
|
148
|
-
static determinant(m) {
|
|
150
|
+
static determinant(m: Matrix3_t): number {
|
|
149
151
|
|
|
150
152
|
const a = m[0][0], b = m[1][0], c = m[2][0],
|
|
151
153
|
d = m[0][1], e = m[1][1], f = m[2][1],
|
|
@@ -155,8 +157,9 @@ class Matrix3 {
|
|
|
155
157
|
|
|
156
158
|
}
|
|
157
159
|
|
|
158
|
-
static trace(m) {
|
|
160
|
+
static trace(m: Matrix3_t): number {
|
|
159
161
|
return m[0][0] + m[1][1] + m[2][2];
|
|
160
162
|
}
|
|
161
163
|
}
|
|
164
|
+
|
|
162
165
|
export default Matrix3;
|
|
@@ -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
|
-
|
|
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
|
|
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
|
});
|