@wemap/maths 10.7.0 → 10.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -21,6 +21,7 @@ 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;
24
25
 
25
26
  // Geometry
26
27
  export function linesIntersect(line1: Line_t, line2: Line_t, detectIntersectionAtBounds?: number): boolean;
@@ -116,6 +117,9 @@ declare module '@wemap/maths' {
116
117
  static multiply(a: Matrix3_t, b: Matrix3_t): Matrix3_t;
117
118
  static multiplyScalar(m: Matrix3_t, s: number): Matrix3_t;
118
119
  static multiplyVector(a: Matrix3_t, v: Vector3_t): Vector3_t;
120
+ static rotX(theta: number): Matrix3_t;
121
+ static rotY(theta: number): Matrix3_t;
122
+ static rotZ(theta: number): Matrix3_t;
119
123
  static skew(v: Vector3_t): Matrix3_t;
120
124
  static sum(m1: Matrix3_t, m2: Matrix3_t): Matrix3_t;
121
125
  static transpose(m: Matrix3_t): Matrix3_t;
@@ -125,13 +129,17 @@ declare module '@wemap/maths' {
125
129
  static get identity(): Matrix4_t;
126
130
 
127
131
  static fromMatrix3(m: Matrix3_t): Matrix4_t;
132
+ // Column-Major ordering like Eigen and ThreeJS
128
133
  static fromVector(v: Vector16_t): Matrix4_t;
129
134
  static multiply(a: Matrix4_t, b: Matrix4_t): Matrix4_t;
135
+ static multiplyScalar(a: Matrix4_t, b: number): Matrix4_t;
130
136
  static multiplyVector(a: Matrix4_t, v: Vector4_t): Vector4_t;
131
137
  static multiplyVector3(a: Matrix4_t, v: Vector3_t): Vector3_t;
132
138
  static sum(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t;
133
139
  static subtract(m1: Matrix4_t, m2: Matrix4_t): Matrix4_t;
140
+ // Column-Major ordering like Eigen and ThreeJS
134
141
  static toVector(m: Matrix4_t): Vector16_t;
142
+ static transpose(m: Matrix4_t): Matrix4_t;
135
143
  }
136
144
 
137
145
  export class Point2 {
@@ -155,5 +163,13 @@ declare module '@wemap/maths' {
155
163
  static quaternionToEuler(quaternion: Quaternion_t, order: string): EulerAngles_t;
156
164
  }
157
165
 
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
+
158
174
  }
159
175
 
package/index.js CHANGED
@@ -6,6 +6,7 @@ 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';
9
10
  export { default as Vector } from './src/Vector.js';
10
11
  export { default as Vector3 } from './src/Vector3.js';
11
12
 
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "directory": "packages/maths"
14
14
  },
15
15
  "name": "@wemap/maths",
16
- "version": "10.7.0",
16
+ "version": "10.9.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": "d9e85b1ff0458dc3f904dbdbe8fa098f02cf8d87"
29
+ "gitHead": "9cfaff0891646b7a7f888fe220011b3261884f15"
30
30
  }
package/src/Matrix3.js CHANGED
@@ -24,6 +24,30 @@ class Matrix3 {
24
24
  ];
25
25
  }
26
26
 
27
+ static rotX(theta) {
28
+ return [
29
+ [1, 0, 0],
30
+ [0, Math.cos(theta), -Math.sin(theta)],
31
+ [0, Math.sin(theta), Math.cos(theta)]
32
+ ];
33
+ }
34
+
35
+ static rotY(theta) {
36
+ return [
37
+ [Math.cos(theta), 0, Math.sin(theta)],
38
+ [0, 1, 0],
39
+ [-Math.sin(theta), 0, Math.cos(theta)]
40
+ ];
41
+ }
42
+
43
+ static rotZ(theta) {
44
+ return [
45
+ [Math.cos(theta), -Math.sin(theta), 0],
46
+ [-Math.sin(theta), Math.cos(theta), 0],
47
+ [0, 0, 1]
48
+ ];
49
+ }
50
+
27
51
  static transpose(m) {
28
52
  return [
29
53
  [m[0][0], m[1][0], m[2][0]],
@@ -102,4 +102,31 @@ describe('Matrix3', () => {
102
102
  expect(result).is.null;
103
103
 
104
104
  });
105
+
106
+ it('rotX', () => {
107
+ const result = Matrix3.rotX(Math.PI / 2);
108
+ expect(result).deep.almost([
109
+ [1, 0, 0],
110
+ [0, 0, -1],
111
+ [0, 1, 0]
112
+ ]);
113
+ });
114
+
115
+ it('rotY', () => {
116
+ const result = Matrix3.rotY(Math.PI / 2);
117
+ expect(result).deep.almost([
118
+ [0, 0, 1],
119
+ [0, 1, 0],
120
+ [-1, 0, 0]
121
+ ]);
122
+ });
123
+
124
+ it('rotZ', () => {
125
+ const result = Matrix3.rotZ(Math.PI / 2);
126
+ expect(result).deep.almost([
127
+ [0, -1, 0],
128
+ [-1, 0, 0],
129
+ [0, 0, 1]
130
+ ]);
131
+ });
105
132
  });
package/src/Matrix4.js CHANGED
@@ -36,21 +36,27 @@ class Matrix4 {
36
36
  ];
37
37
  }
38
38
 
39
+ /**
40
+ * Column-Major ordering like Eigen and ThreeJS
41
+ */
39
42
  static fromVector(v) {
40
43
  return [
41
- [v[0], v[1], v[2], v[3]],
42
- [v[4], v[5], v[6], v[7]],
43
- [v[8], v[9], v[10], v[11]],
44
- [v[12], v[13], v[14], v[15]]
44
+ [v[0], v[4], v[8], v[12]],
45
+ [v[1], v[5], v[9], v[13]],
46
+ [v[2], v[6], v[10], v[14]],
47
+ [v[3], v[7], v[11], v[15]]
45
48
  ];
46
49
  }
47
50
 
51
+ /**
52
+ * Column-Major ordering like Eigen and ThreeJS
53
+ */
48
54
  static toVector(m) {
49
55
  return [
50
- m[0][0], m[0][1], m[0][2], m[0][3],
51
- m[1][0], m[1][1], m[1][2], m[1][3],
52
- m[2][0], m[2][1], m[2][2], m[2][3],
53
- m[3][0], m[3][1], m[3][2], m[3][3]
56
+ m[0][0], m[1][0], m[2][0], m[3][0],
57
+ m[0][1], m[1][1], m[2][1], m[3][1],
58
+ m[0][2], m[1][2], m[2][2], m[3][2],
59
+ m[0][3], m[1][3], m[2][3], m[3][3]
54
60
  ];
55
61
  }
56
62
 
@@ -68,6 +74,17 @@ class Matrix4 {
68
74
  return m;
69
75
  }
70
76
 
77
+ static multiplyScalar(a, s) {
78
+ const output = new Array(4);
79
+ for (let r = 0; r < 4; ++r) {
80
+ output[r] = 0;
81
+ for (let c = 0; c < 4; ++c) {
82
+ output[r] += a[r][c] * s;
83
+ }
84
+ }
85
+ return output;
86
+ }
87
+
71
88
  static multiplyVector(a, v) {
72
89
  const output = new Array(4);
73
90
  for (let r = 0; r < 4; ++r) {
@@ -82,5 +99,14 @@ class Matrix4 {
82
99
  static multiplyVector3(a, v) {
83
100
  return Matrix4.multiplyVector(a, [...v, 1]).slice(0, 3);
84
101
  }
102
+
103
+ static transpose(m) {
104
+ return [
105
+ [m[0][0], m[1][0], m[2][0], m[3][0]],
106
+ [m[0][1], m[1][1], m[2][1], m[3][1]],
107
+ [m[0][2], m[1][2], m[2][2], m[3][2]],
108
+ [m[0][3], m[1][3], m[2][3], m[3][3]]
109
+ ];
110
+ }
85
111
  }
86
112
  export default Matrix4;
@@ -0,0 +1,45 @@
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] * s],
29
+ [m[1][0], m[1][1] * s, m[1][2], m[1][3] * s],
30
+ [m[2][0], m[2][1], m[2][2] * s, m[2][3] * s],
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;
@@ -0,0 +1,76 @@
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
+ });