@wemap/maths 12.10.0 → 12.10.8-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/{index.ts → dist/index.d.ts} +0 -2
  2. package/dist/index.js +1 -1155
  3. package/dist/index.js.map +1 -1
  4. package/{src/EulerAngles.ts → dist/src/EulerAngles.d.ts} +2 -7
  5. package/dist/src/Geometry.d.ts +10 -0
  6. package/dist/src/Matrix.d.ts +13 -0
  7. package/dist/src/Matrix.spec.d.ts +1 -0
  8. package/dist/src/Matrix3.d.ts +20 -0
  9. package/dist/src/Matrix3.spec.d.ts +2 -0
  10. package/dist/src/Matrix4.d.ts +35 -0
  11. package/dist/src/Matrix4.spec.d.ts +1 -0
  12. package/dist/src/Point2.d.ts +17 -0
  13. package/dist/src/Point2.spec.d.ts +1 -0
  14. package/dist/src/Polygon.d.ts +7 -0
  15. package/dist/src/Polygon.spec.d.ts +1 -0
  16. package/dist/src/Quaternion.d.ts +71 -0
  17. package/dist/src/Quaternion.spec.d.ts +1 -0
  18. package/dist/src/Rotations.d.ts +13 -0
  19. package/dist/src/Rotations.spec.d.ts +1 -0
  20. package/dist/src/Utils.d.ts +38 -0
  21. package/dist/src/Utils.spec.d.ts +1 -0
  22. package/dist/src/Vector.d.ts +5 -0
  23. package/dist/src/Vector.spec.d.ts +1 -0
  24. package/dist/src/Vector3.d.ts +13 -0
  25. package/dist/src/Vector3.spec.d.ts +1 -0
  26. package/{src/types.ts → dist/src/types.d.ts} +37 -8
  27. package/dist/vite.config.d.ts +19 -0
  28. package/package.json +7 -4
  29. package/src/Geometry.ts +0 -39
  30. package/src/Matrix.spec.ts +0 -130
  31. package/src/Matrix.ts +0 -242
  32. package/src/Matrix3.spec.ts +0 -138
  33. package/src/Matrix3.ts +0 -165
  34. package/src/Matrix4.spec.ts +0 -187
  35. package/src/Matrix4.ts +0 -309
  36. package/src/Point2.spec.ts +0 -19
  37. package/src/Point2.ts +0 -54
  38. package/src/Polygon.spec.ts +0 -48
  39. package/src/Polygon.ts +0 -63
  40. package/src/Quaternion.spec.ts +0 -126
  41. package/src/Quaternion.ts +0 -292
  42. package/src/Rotations.spec.ts +0 -67
  43. package/src/Rotations.ts +0 -223
  44. package/src/Utils.spec.ts +0 -83
  45. package/src/Utils.ts +0 -77
  46. package/src/Vector.spec.ts +0 -15
  47. package/src/Vector.ts +0 -10
  48. package/src/Vector3.spec.ts +0 -52
  49. package/src/Vector3.ts +0 -51
  50. package/tsconfig.json +0 -3
  51. package/vite.config.ts +0 -4
@@ -1,138 +0,0 @@
1
- import chai from 'chai';
2
- import chaiAlmost from 'chai-almost';
3
-
4
- import Matrix3 from './Matrix3.js';
5
- import { Matrix3_t, Vector3_t } from './types.js';
6
-
7
- const { expect } = chai;
8
- chai.use(chaiAlmost());
9
-
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 = [
16
- [1, 2, 3],
17
- [4, 5, 6],
18
- [7, 8, 9]
19
- ];
20
-
21
- const m2: Matrix3_t = [
22
- [1, 2, 3],
23
- [0, 1, 5],
24
- [5, 6, 0]
25
- ];
26
-
27
- const v1: Vector3_t = [1, 2, 3];
28
-
29
- describe('Matrix3', () => {
30
-
31
- it('identity', () => {
32
- expect(Matrix3.identity).deep.equal([
33
- [1, 0, 0],
34
- [0, 1, 0],
35
- [0, 0, 1]
36
- ]);
37
- });
38
-
39
- it('zeros', () => {
40
- expect(Matrix3.zeros).deep.equal([
41
- [0, 0, 0],
42
- [0, 0, 0],
43
- [0, 0, 0]
44
- ]);
45
- });
46
-
47
- it('sum', () => {
48
- const result = Matrix3.sum(m1, m1);
49
- expect(result).deep.equal([
50
- [2, 4, 6],
51
- [8, 10, 12],
52
- [14, 16, 18]
53
- ]);
54
- });
55
-
56
- it('transpose', () => {
57
- const result = Matrix3.transpose(m1);
58
- expect(result).deep.equal([
59
- [1, 4, 7],
60
- [2, 5, 8],
61
- [3, 6, 9]
62
- ]);
63
- });
64
-
65
- it('multiplyScalar', () => {
66
- const result = Matrix3.multiplyScalar(m1, 2);
67
- expect(result).deep.equal([
68
- [2, 4, 6],
69
- [8, 10, 12],
70
- [14, 16, 18]
71
- ]);
72
- });
73
-
74
- it('multiplyVector', () => {
75
- const result = Matrix3.multiplyVector(m1, v1);
76
- expect(result).deep.equal([14, 32, 50]);
77
- });
78
-
79
- it('multiply', () => {
80
- const result = Matrix3.multiply(m1, m1);
81
- expect(result).deep.equal([
82
- [30, 36, 42],
83
- [66, 81, 96],
84
- [102, 126, 150]
85
- ]);
86
- });
87
-
88
- it('skew', () => {
89
- const result = Matrix3.skew(v1);
90
- expect(result).deep.equal([
91
- [0, -3, 2],
92
- [3, 0, -1],
93
- [-2, 1, 0]
94
- ]);
95
- });
96
-
97
- it('inverse', () => {
98
- let result;
99
- result = Matrix3.inverse(m2);
100
- expect(result).deep.almost([
101
- [-6, 18 / 5, 7 / 5],
102
- [5, -3, -1],
103
- [-1, 4 / 5, 1 / 5]
104
- ]);
105
- result = Matrix3.inverse([[1, 2, 0], [3, 4, 0], [5, 6, 0]]);
106
- expect(result).is.null;
107
- });
108
-
109
- it('rotX', () => {
110
- const result = Matrix3.rotX(Math.PI / 2);
111
- expect(result).deep.almost([
112
- [1, 0, 0],
113
- [0, 0, -1],
114
- [0, 1, 0]
115
- ]);
116
- expectIsRotMat(result);
117
- });
118
-
119
- it('rotY', () => {
120
- const result = Matrix3.rotY(Math.PI / 2);
121
- expect(result).deep.almost([
122
- [0, 0, 1],
123
- [0, 1, 0],
124
- [-1, 0, 0]
125
- ]);
126
- expectIsRotMat(result);
127
- });
128
-
129
- it('rotZ', () => {
130
- const result = Matrix3.rotZ(Math.PI / 2);
131
- expect(result).deep.almost([
132
- [0, -1, 0],
133
- [1, 0, 0],
134
- [0, 0, 1]
135
- ]);
136
- expectIsRotMat(result);
137
- });
138
- });
package/src/Matrix3.ts DELETED
@@ -1,165 +0,0 @@
1
- import { Matrix3_t, Vector3_t } from "./types";
2
-
3
- class Matrix3 {
4
-
5
- static get identity(): Matrix3_t {
6
- return [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
7
- }
8
-
9
- static get zeros(): Matrix3_t {
10
- return [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
11
- }
12
-
13
- static diag(arr: Vector3_t): Matrix3_t {
14
- return [
15
- [arr[0], 0, 0],
16
- [0, arr[1], 0],
17
- [0, 0, arr[2]]
18
- ];
19
- }
20
-
21
- static sum(m1: Matrix3_t, m2: Matrix3_t): Matrix3_t {
22
- return [
23
- [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2]],
24
- [m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2]],
25
- [m1[2][0] + m2[2][0], m1[2][1] + m2[2][1], m1[2][2] + m2[2][2]]
26
- ];
27
- }
28
-
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 {
38
- return [
39
- [1, 0, 0],
40
- [0, Math.cos(theta), -Math.sin(theta)],
41
- [0, Math.sin(theta), Math.cos(theta)]
42
- ];
43
- }
44
-
45
- static rotY(theta: number): Matrix3_t {
46
- return [
47
- [Math.cos(theta), 0, Math.sin(theta)],
48
- [0, 1, 0],
49
- [-Math.sin(theta), 0, Math.cos(theta)]
50
- ];
51
- }
52
-
53
- static rotZ(theta: number): Matrix3_t {
54
- return [
55
- [Math.cos(theta), -Math.sin(theta), 0],
56
- [Math.sin(theta), Math.cos(theta), 0],
57
- [0, 0, 1]
58
- ];
59
- }
60
-
61
- static transpose(m: Matrix3_t): Matrix3_t {
62
- return [
63
- [m[0][0], m[1][0], m[2][0]],
64
- [m[0][1], m[1][1], m[2][1]],
65
- [m[0][2], m[1][2], m[2][2]]
66
- ];
67
- }
68
-
69
- static multiplyScalar(m: Matrix3_t, s: number): Matrix3_t {
70
- return [
71
- [m[0][0] * s, m[0][1] * s, m[0][2] * s],
72
- [m[1][0] * s, m[1][1] * s, m[1][2] * s],
73
- [m[2][0] * s, m[2][1] * s, m[2][2] * s]
74
- ];
75
- }
76
-
77
- static multiplyVector(a: Matrix3_t, v: Vector3_t): Vector3_t {
78
- const output = new Array(3) as Vector3_t;
79
- for (let r = 0; r < 3; ++r) {
80
- output[r] = 0;
81
- for (let c = 0; c < 3; ++c) {
82
- output[r] += a[r][c] * v[c];
83
- }
84
- }
85
- return output;
86
- }
87
-
88
- static multiply(a: Matrix3_t, b: Matrix3_t): Matrix3_t {
89
- const m = new Array(3) as Matrix3_t;
90
- for (let r = 0; r < 3; ++r) {
91
- m[r] = new Array(3) as [number, number, number];
92
- for (let c = 0; c < 3; ++c) {
93
- m[r][c] = 0;
94
- for (let i = 0; i < 3; ++i) {
95
- m[r][c] += a[r][i] * b[i][c];
96
- }
97
- }
98
- }
99
- return m;
100
- }
101
-
102
- static skew(v: Vector3_t): Matrix3_t {
103
- return [
104
- [0, -v[2], v[1]],
105
- [v[2], 0, -v[0]],
106
- [-v[1], v[0], 0]
107
- ];
108
- }
109
-
110
- // Helped from https://github.com/mrdoob/three.js/blob/34dc2478c684066257e4e39351731a93c6107ef5/src/math/Matrix3.js#L183
111
- static inverse(me: Matrix3_t): Matrix3_t | null {
112
-
113
- const te = new Array(3) as Matrix3_t,
114
-
115
- n11 = me[0][0], n21 = me[0][1], n31 = me[0][2],
116
- n12 = me[1][0], n22 = me[1][1], n32 = me[1][2],
117
- n13 = me[2][0], n23 = me[2][1], n33 = me[2][2],
118
-
119
- t11 = n33 * n22 - n32 * n23,
120
- t12 = n32 * n13 - n33 * n12,
121
- t13 = n23 * n12 - n22 * n13,
122
-
123
- det = n11 * t11 + n21 * t12 + n31 * t13;
124
-
125
- if (det === 0) {
126
- return null;
127
- }
128
-
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;
132
-
133
- const detInv = 1 / det;
134
-
135
- te[0][0] = t11 * detInv;
136
- te[0][1] = (n31 * n23 - n33 * n21) * detInv;
137
- te[0][2] = (n32 * n21 - n31 * n22) * detInv;
138
-
139
- te[1][0] = t12 * detInv;
140
- te[1][1] = (n33 * n11 - n31 * n13) * detInv;
141
- te[1][2] = (n31 * n12 - n32 * n11) * detInv;
142
-
143
- te[2][0] = t13 * detInv;
144
- te[2][1] = (n21 * n13 - n23 * n11) * detInv;
145
- te[2][2] = (n22 * n11 - n21 * n12) * detInv;
146
-
147
- return te;
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
- }
163
- }
164
-
165
- export default Matrix3;
@@ -1,187 +0,0 @@
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
- });