@wemap/maths 1.0.1 → 2.7.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/package.json +2 -2
- package/src/Matrix.js +1 -0
- package/src/Matrix.spec.js +23 -1
- package/src/Matrix3.spec.js +7 -1
- package/src/Quaternion.js +32 -0
- package/src/Quaternion.spec.js +33 -0
- package/src/Rotations.js +17 -36
- package/src/Rotations.spec.js +15 -2
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"directory": "packages/maths"
|
|
13
13
|
},
|
|
14
14
|
"name": "@wemap/maths",
|
|
15
|
-
"version": "
|
|
15
|
+
"version": "2.7.0",
|
|
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": "
|
|
29
|
+
"gitHead": "6a8ce3404ef84070ad5210c7cd50ee9f4807afc1"
|
|
30
30
|
}
|
package/src/Matrix.js
CHANGED
package/src/Matrix.spec.js
CHANGED
|
@@ -97,10 +97,32 @@ describe('Matrix', () => {
|
|
|
97
97
|
});
|
|
98
98
|
|
|
99
99
|
it('inverse', () => {
|
|
100
|
-
|
|
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.spec.js
CHANGED
|
@@ -73,11 +73,17 @@ describe('Matrix3', () => {
|
|
|
73
73
|
});
|
|
74
74
|
|
|
75
75
|
it('inverse', () => {
|
|
76
|
-
|
|
76
|
+
let result;
|
|
77
|
+
|
|
78
|
+
result = Matrix3.inverse(m2);
|
|
77
79
|
expect(result).deep.almost([
|
|
78
80
|
[-6, 18 / 5, 7 / 5],
|
|
79
81
|
[5, -3, -1],
|
|
80
82
|
[-1, 4 / 5, 1 / 5]
|
|
81
83
|
]);
|
|
84
|
+
|
|
85
|
+
result = Matrix3.inverse([[1, 2, 0], [3, 4, 0], [5, 6, 0]]);
|
|
86
|
+
expect(result).is.null;
|
|
87
|
+
|
|
82
88
|
});
|
|
83
89
|
});
|
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;
|
package/src/Quaternion.spec.js
CHANGED
|
@@ -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.
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
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,
|
|
79
|
-
|
|
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
|
-
|
|
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;
|
package/src/Rotations.spec.js
CHANGED
|
@@ -21,13 +21,26 @@ describe('Rotations', () => {
|
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
it('quaternionToEulerZXYDegrees', () => {
|
|
24
|
-
|
|
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
|
-
|
|
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', () => {
|