@wemap/maths 1.0.1 → 2.7.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/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/maths"
13
13
  },
14
14
  "name": "@wemap/maths",
15
- "version": "1.0.1",
15
+ "version": "2.7.2",
16
16
  "bugs": {
17
17
  "url": "https://github.com/wemap/wemap-modules-js/issues"
18
18
  },
@@ -26,5 +26,5 @@
26
26
  "wemap"
27
27
  ],
28
28
  "license": "ISC",
29
- "gitHead": "1ccef947faf3f37aba592594efe06575b6542170"
29
+ "gitHead": "ec928b7890e3c37be2a32b1ba1c108aeba9e15b7"
30
30
  }
package/src/Matrix.js CHANGED
@@ -1,15 +1,7 @@
1
+ /* eslint-disable max-statements */
1
2
  /* eslint max-depth: 0 */
2
3
  class Matrix {
3
4
 
4
- static identity4() {
5
- return [
6
- [1, 0, 0, 0],
7
- [0, 1, 0, 0],
8
- [0, 0, 1, 0],
9
- [0, 0, 0, 1]
10
- ];
11
- }
12
-
13
5
  static add(a, b) {
14
6
  const aNumRows = a.length;
15
7
  const aNumCols = a[0].length;
@@ -97,10 +97,32 @@ describe('Matrix', () => {
97
97
  });
98
98
 
99
99
  it('inverse', () => {
100
- const result = Matrix.inverse([[1, 2], [3, 4]]);
100
+ let result;
101
+
102
+ result = Matrix.inverse([[1, 2]]);
103
+ expect(result).is.null;
104
+
105
+ result = Matrix.inverse([[1, 2], [3, 4]]);
101
106
  expect(result).deep.equal([
102
107
  [-2, 1],
103
108
  [1.5, -0.5]
104
109
  ]);
110
+
111
+ result = Matrix.inverse([[0, 2], [1, 9]]);
112
+ expect(result).deep.equal([
113
+ [-4.5, 1],
114
+ [0.5, 0]
115
+ ]);
116
+
117
+ result = Matrix.inverse([[0, 2, 3], [0, 1, 2], [1, 2, 1]]);
118
+ expect(result).deep.equal([
119
+ [-3, 4, 1],
120
+ [2, -3, 0],
121
+ [-1, 2, -0]
122
+ ]);
123
+
124
+ result = Matrix.inverse([[1, 2], [1, 2]]);
125
+ expect(result).is.null;
126
+
105
127
  });
106
128
  });
package/src/Matrix3.js CHANGED
@@ -2,6 +2,14 @@ import Matrix from './Matrix';
2
2
 
3
3
  class Matrix3 extends Matrix {
4
4
 
5
+ static get identity() {
6
+ return [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
7
+ }
8
+
9
+ static get zeros() {
10
+ return [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
11
+ }
12
+
5
13
  static add(m1, m2) {
6
14
  return [
7
15
  [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2]],
@@ -18,10 +26,6 @@ class Matrix3 extends Matrix {
18
26
  ];
19
27
  }
20
28
 
21
- static zeros() {
22
- return [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
23
- }
24
-
25
29
  static multiplyScalar(m, s) {
26
30
  return [
27
31
  [m[0][0] * s, m[0][1] * s, m[0][2] * s],
@@ -22,6 +22,22 @@ const v1 = [1, 2, 3];
22
22
 
23
23
  describe('Matrix3', () => {
24
24
 
25
+ it('identity', () => {
26
+ expect(Matrix3.identity).deep.equal([
27
+ [1, 0, 0],
28
+ [0, 1, 0],
29
+ [0, 0, 1]
30
+ ]);
31
+ });
32
+
33
+ it('zeros', () => {
34
+ expect(Matrix3.zeros).deep.equal([
35
+ [0, 0, 0],
36
+ [0, 0, 0],
37
+ [0, 0, 0]
38
+ ]);
39
+ });
40
+
25
41
  it('add', () => {
26
42
  const result = Matrix3.add(m1, m1);
27
43
  expect(result).deep.equal([
@@ -73,11 +89,17 @@ describe('Matrix3', () => {
73
89
  });
74
90
 
75
91
  it('inverse', () => {
76
- const result = Matrix3.inverse(m2);
92
+ let result;
93
+
94
+ result = Matrix3.inverse(m2);
77
95
  expect(result).deep.almost([
78
96
  [-6, 18 / 5, 7 / 5],
79
97
  [5, -3, -1],
80
98
  [-1, 4 / 5, 1 / 5]
81
99
  ]);
100
+
101
+ result = Matrix3.inverse([[1, 2, 0], [3, 4, 0], [5, 6, 0]]);
102
+ expect(result).is.null;
103
+
82
104
  });
83
105
  });
package/src/Matrix4.js CHANGED
@@ -2,6 +2,15 @@ import Matrix from './Matrix';
2
2
 
3
3
  class Matrix4 extends Matrix {
4
4
 
5
+ static get identity() {
6
+ return [
7
+ [1, 0, 0, 0],
8
+ [0, 1, 0, 0],
9
+ [0, 0, 1, 0],
10
+ [0, 0, 0, 1]
11
+ ];
12
+ }
13
+
5
14
  static add(m1, m2) {
6
15
  return [
7
16
  [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2], m1[0][3] + m2[0][3]],
@@ -18,6 +18,15 @@ const m2 = [
18
18
 
19
19
  describe('Matrix4', () => {
20
20
 
21
+ it('identity', () => {
22
+ expect(Matrix4.identity).deep.equal([
23
+ [1, 0, 0, 0],
24
+ [0, 1, 0, 0],
25
+ [0, 0, 1, 0],
26
+ [0, 0, 0, 1]
27
+ ]);
28
+ });
29
+
21
30
  it('add', () => {
22
31
  const result = Matrix4.add(m1, m2);
23
32
  expect(result).deep.equal([
package/src/Quaternion.js CHANGED
@@ -1,5 +1,11 @@
1
+ import Vector3 from './Vector3';
2
+
1
3
  class Quaternion {
2
4
 
5
+ static get identity() {
6
+ return [1, 0, 0, 0];
7
+ }
8
+
3
9
  static add(v1, v2) {
4
10
  return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2], v1[3] + v2[3]];
5
11
  }
@@ -14,6 +20,10 @@ class Quaternion {
14
20
  ];
15
21
  }
16
22
 
23
+ /**
24
+ * Unit Quaternion inverse
25
+ * @param {Array.<Number>} quaternion must be a unit quaternion.
26
+ */
17
27
  static inverse(q) {
18
28
  return [-q[0], q[1], q[2], q[3]];
19
29
  }
@@ -76,5 +86,27 @@ class Quaternion {
76
86
  static dot(q1, q2) {
77
87
  return q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3];
78
88
  }
89
+
90
+ static getRotationBetweenTwoVectors(from, to) {
91
+ const fromNormalized = Vector3.normalize(from);
92
+ const toNormalized = Vector3.normalize(to);
93
+ const dot = Vector3.dot(fromNormalized, toNormalized);
94
+ let tmp;
95
+ if (dot < -0.999999) {
96
+ tmp = Vector3.cross([1, 0, 0], fromNormalized);
97
+ if (Vector3.norm(tmp) < 0.000001) {
98
+ tmp = Vector3.cross([0, 1, 0], fromNormalized);
99
+ }
100
+ Vector3.normalize(tmp, tmp);
101
+ return Quaternion.fromAxisAngle(tmp, Math.PI);
102
+ } else if (dot > 0.999999) {
103
+ return Quaternion.identity;
104
+ }
105
+
106
+ tmp = Vector3.cross(fromNormalized, toNormalized);
107
+ const quat = [1 + dot, tmp[0], tmp[1], tmp[2]];
108
+ return Quaternion.normalize(quat);
109
+ }
110
+
79
111
  }
80
112
  export default Quaternion;
@@ -87,4 +87,37 @@ describe('Quaternion', () => {
87
87
  const result = Quaternion.dot(q1, q2);
88
88
  expect(result).almost(0.968863931626966);
89
89
  });
90
+
91
+ it('inverse', () => {
92
+ const result = Quaternion.inverse(q1);
93
+ expect(result).deep.equal([-q1[0], q1[1], q1[2], q1[3]]);
94
+ });
95
+
96
+ it('getRotationBetweenTwoVectors', () => {
97
+ let result;
98
+
99
+ result = Quaternion.getRotationBetweenTwoVectors(
100
+ [0, 0, 1],
101
+ [0, 0, 1]
102
+ );
103
+ expect(result).deep.almost(Quaternion.identity);
104
+
105
+ result = Quaternion.getRotationBetweenTwoVectors(
106
+ [0, 0, 1],
107
+ [0, 0, -1]
108
+ );
109
+ expect(result).deep.almost([0, 0, -1, 0]);
110
+
111
+ result = Quaternion.getRotationBetweenTwoVectors(
112
+ [1, 0, 0],
113
+ [-1, 0, 0]
114
+ );
115
+ expect(result).deep.almost([0, 0, 0, -1]);
116
+
117
+ result = Quaternion.getRotationBetweenTwoVectors(
118
+ [0, 1, 0],
119
+ [0, 0, 1]
120
+ );
121
+ expect(result).deep.almost([Math.SQRT1_2, Math.SQRT1_2, 0, 0]);
122
+ });
90
123
  });
package/src/Rotations.js CHANGED
@@ -1,6 +1,4 @@
1
1
  import Utils from './Utils';
2
- import Vector3 from './Vector3';
3
- import Quaternion from './Quaternion';
4
2
 
5
3
  // import Constants from '../../geo/src/Constants';
6
4
 
@@ -15,9 +13,7 @@ class Rotations {
15
13
  // ZXY because yaw is around Z axis, pitch around X axis and roll around Y axis.
16
14
  // They are conventions for a smartphone frame.
17
15
  static eulerToQuaternionZXYDegrees(eulerAnglesDegrees) {
18
- const eulerAngles = eulerAnglesDegrees.slice(0).map(function(x) {
19
- return Utils.deg2rad(x);
20
- });
16
+ const eulerAngles = eulerAnglesDegrees.map(x => Utils.deg2rad(x));
21
17
  return Rotations.eulerToQuaternionZXY(eulerAngles);
22
18
  }
23
19
 
@@ -53,9 +49,7 @@ class Rotations {
53
49
 
54
50
  static quaternionToEulerZXYDegrees(quaternion) {
55
51
  const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
56
- return eulerAngles.map(function(x) {
57
- return Utils.rad2deg(x);
58
- });
52
+ return eulerAngles.map(x => Utils.rad2deg(x));
59
53
  }
60
54
 
61
55
  static quaternionToEulerZXY(quaternion) {
@@ -65,21 +59,30 @@ class Rotations {
65
59
  const qy = quaternion[2];
66
60
  const qz = quaternion[3];
67
61
 
68
- let alpha, gamma;
62
+ let alpha, beta, gamma;
69
63
 
70
64
  // From http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
71
- if (qy * qz + qx * qw === 0.5) {
65
+ const test = qy * qz + qx * qw;
66
+ if (test > 0.499) {
72
67
  alpha = 2 * Math.atan2(qy, qw);
68
+ beta = Math.PI / 2;
73
69
  gamma = 0;
74
- } else if (qy * qz + qx * qw === -0.5) {
70
+ } else if (test < - 0.499) {
75
71
  alpha = -2 * Math.atan2(qy, qw);
72
+ beta = - Math.PI / 2;
76
73
  gamma = 0;
77
74
  } else {
78
- alpha = Math.atan2(2 * qz * qw - 2 * qy * qx, 1 - 2 * Math.pow(qz, 2) - 2 * Math.pow(qx, 2));
79
- gamma = Math.atan2(2 * qy * qw - 2 * qz * qx, 1 - 2 * Math.pow(qy, 2) - 2 * Math.pow(qx, 2));
75
+ alpha = Math.atan2(2 * qz * qw - 2 * qy * qx,
76
+ 1 - 2 * Math.pow(qz, 2) - 2 * Math.pow(qx, 2));
77
+ beta = Math.asin(2 * test);
78
+ gamma = Math.atan2(2 * qy * qw - 2 * qz * qx,
79
+ 1 - 2 * Math.pow(qy, 2) - 2 * Math.pow(qx, 2));
80
80
  }
81
81
 
82
- const beta = Math.asin(2 * qy * qz + 2 * qx * qw);
82
+ alpha = alpha % (2 * Math.PI);
83
+ beta = beta % (Math.PI);
84
+ gamma = gamma % (2 * Math.PI);
85
+
83
86
  return [alpha, beta, gamma];
84
87
  }
85
88
 
@@ -109,28 +112,6 @@ class Rotations {
109
112
  // const eulerAnglesAR = Rotations.quaternionToEulerZXY(Quaternion.multiply(quaternion, quatRotX90));
110
113
  // return -eulerAnglesAR[0];
111
114
  }
112
-
113
- static getRotationBetweenTwoVectors(from, to) {
114
- const fromNormalized = Vector3.normalize(from);
115
- const toNormalized = Vector3.normalize(to);
116
- const dot = Vector3.dot(fromNormalized, toNormalized);
117
- let tmp;
118
- if (dot < -0.999999) {
119
- tmp = Vector3.cross([1, 0, 0], fromNormalized);
120
- if (Vector3.norm(tmp) < 0.000001) {
121
- tmp = Vector3.cross([0, 1, 0], fromNormalized);
122
- }
123
- Vector3.normalize(tmp, tmp);
124
- return Quaternion.fromAxisAngle(tmp, Math.PI);
125
- } else if (dot > 0.999999) {
126
- return Quaternion.identity();
127
- }
128
-
129
- tmp = Vector3.cross(fromNormalized, toNormalized);
130
- const quat = [1 + dot, tmp[0], tmp[1], tmp[2]];
131
- return Quaternion.normalize(quat);
132
- }
133
-
134
115
  }
135
116
 
136
117
  export default Rotations;
@@ -21,13 +21,26 @@ describe('Rotations', () => {
21
21
  });
22
22
 
23
23
  it('quaternionToEulerZXYDegrees', () => {
24
- const result = Rotations.quaternionToEulerZXYDegrees(q1);
24
+ let result;
25
+
26
+ result = Rotations.quaternionToEulerZXYDegrees(q1);
25
27
  expect(result).deep.almost(E1);
28
+
29
+ result = Rotations.quaternionToEulerZXYDegrees([Math.SQRT1_2, Math.SQRT2, 0, 0]);
30
+ expect(result).deep.almost([0, 90, 0]);
31
+
32
+ result = Rotations.quaternionToEulerZXYDegrees([-Math.SQRT1_2, Math.SQRT2, 0, 0]);
33
+ expect(result).deep.almost([0, -90, 0]);
26
34
  });
27
35
 
28
36
  it('eulerToQuaternionZXY', () => {
29
- const result = Rotations.eulerToQuaternionZXY(e1);
37
+ let result;
38
+
39
+ result = Rotations.eulerToQuaternionZXY(e1);
30
40
  expect(result).deep.almost(q1);
41
+
42
+ result = Rotations.eulerToQuaternionZXY([0, 0, Math.PI / 2]);
43
+ expect(result).deep.almost([Math.SQRT1_2, 0, Math.SQRT1_2, 0]);
31
44
  });
32
45
 
33
46
  it('eulerToQuaternionZXYDegrees', () => {