@wemap/maths 10.9.0 → 10.9.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.
package/index.d.ts CHANGED
@@ -131,6 +131,7 @@ declare module '@wemap/maths' {
131
131
  static fromMatrix3(m: Matrix3_t): Matrix4_t;
132
132
  // Column-Major ordering like Eigen and ThreeJS
133
133
  static fromVector(v: Vector16_t): Matrix4_t;
134
+ static invert(m: Matrix4_t): Matrix4_t;
134
135
  static multiply(a: Matrix4_t, b: Matrix4_t): Matrix4_t;
135
136
  static multiplyScalar(a: Matrix4_t, b: number): Matrix4_t;
136
137
  static multiplyVector(a: Matrix4_t, v: Vector4_t): Vector4_t;
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "directory": "packages/maths"
14
14
  },
15
15
  "name": "@wemap/maths",
16
- "version": "10.9.0",
16
+ "version": "10.9.2",
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": "9cfaff0891646b7a7f888fe220011b3261884f15"
29
+ "gitHead": "0afef92de08cc4f39da9ef182b093fe54ac9b7da"
30
30
  }
package/src/Matrix4.js CHANGED
@@ -108,5 +108,51 @@ class Matrix4 {
108
108
  [m[0][3], m[1][3], m[2][3], m[3][3]]
109
109
  ];
110
110
  }
111
+
112
+ static invert(m) {
113
+ // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
114
+ // implementation from ThreeJS
115
+ const te = Matrix4.toVector(m),
116
+
117
+ n11 = te[0], n21 = te[1], n31 = te[2], n41 = te[3],
118
+ n12 = te[4], n22 = te[5], n32 = te[6], n42 = te[7],
119
+ n13 = te[8], n23 = te[9], n33 = te[10], n43 = te[11],
120
+ n14 = te[12], n24 = te[13], n34 = te[14], n44 = te[15],
121
+
122
+ t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
123
+ t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
124
+ t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
125
+ t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
126
+
127
+ const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
128
+
129
+ if (det === 0) {
130
+ throw Error('Matrix det = 0');
131
+ }
132
+
133
+ const detInv = 1 / det;
134
+
135
+ te[0] = t11 * detInv;
136
+ te[1] = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv;
137
+ te[2] = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv;
138
+ te[3] = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv;
139
+
140
+ te[4] = t12 * detInv;
141
+ te[5] = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv;
142
+ te[6] = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv;
143
+ te[7] = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv;
144
+
145
+ te[8] = t13 * detInv;
146
+ te[9] = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv;
147
+ te[10] = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv;
148
+ te[11] = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv;
149
+
150
+ te[12] = t14 * detInv;
151
+ te[13] = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv;
152
+ te[14] = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv;
153
+ te[15] = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv;
154
+
155
+ return Matrix4.fromVector(te);
156
+ }
111
157
  }
112
158
  export default Matrix4;
@@ -1,4 +1,4 @@
1
- import Matrix3 from './Matrix3.js';
1
+ import Matrix4 from './Matrix4.js';
2
2
 
3
3
  class Transformation {
4
4
 
@@ -25,20 +25,15 @@ class Transformation {
25
25
 
26
26
  static scaleScalar(m, s) {
27
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]
28
+ [m[0][0], m[0][1], m[0][2], m[0][3]],
29
+ [m[1][0], m[1][1], m[1][2], m[1][3]],
30
+ [m[2][0], m[2][1], m[2][2], m[2][3]],
31
+ [0, 0, 0, 1 / s]
32
32
  ];
33
33
  }
34
34
 
35
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);
36
+ return Matrix4.invert(m);
42
37
  }
43
38
  }
44
39
 
@@ -2,6 +2,7 @@ import chai from 'chai';
2
2
  import chaiAlmost from 'chai-almost';
3
3
 
4
4
  import Matrix3 from './Matrix3.js';
5
+ import Matrix4 from './Matrix4.js';
5
6
  import Transformation from './Transformation.js';
6
7
 
7
8
 
@@ -65,12 +66,14 @@ describe('Transformation', () => {
65
66
  Matrix3.rotY(Math.PI / 2),
66
67
  [1, 2, 3]
67
68
  ), 2);
68
- expect(Transformation.invert(tr3)).deep.almost([
69
+ const invertTr3 = Transformation.invert(tr3);
70
+ expect(invertTr3).deep.almost([
69
71
  [0, 0, -1, 6],
70
- [0, 0.5, 0, -2],
72
+ [0, 1, 0, -4],
71
73
  [1, 0, 0, -2],
72
- [0, 0, 0, 1]
74
+ [0, 0, 0, 2]
73
75
  ]);
76
+ expect(Matrix4.multiply(tr3, invertTr3)).deep.almost(Matrix4.identity);
74
77
  });
75
78
 
76
79
  });