@wemap/maths 3.2.15 → 4.0.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.js +10 -31
- package/package.json +2 -2
- package/src/Point2.spec.js +19 -0
- package/src/Polygon.js +1 -1
- package/src/Polygon.spec.js +47 -0
- package/src/Quaternion.js +61 -0
- package/src/Quaternion.spec.js +21 -0
- package/src/Rotations.js +120 -3
- package/src/Rotations.spec.js +21 -0
- package/src/Utils.js +54 -37
- package/src/Utils.spec.js +57 -19
package/index.js
CHANGED
|
@@ -1,32 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import Vector3 from './src/Vector3.js';
|
|
1
|
+
export { default as Matrix } from './src/Matrix.js';
|
|
2
|
+
export { default as Matrix3 } from './src/Matrix3.js';
|
|
3
|
+
export { default as Matrix4 } from './src/Matrix4.js';
|
|
4
|
+
export { default as Point2 } from './src/Point2.js';
|
|
5
|
+
export { default as Polygon } from './src/Polygon.js';
|
|
6
|
+
export { default as Quaternion } from './src/Quaternion.js';
|
|
7
|
+
export { default as Rotations } from './src/Rotations.js';
|
|
8
|
+
export { default as Vector } from './src/Vector.js';
|
|
9
|
+
export { default as Vector3 } from './src/Vector3.js';
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
deg2rad, diffAngle, positiveMod, rad2deg, wrap
|
|
14
|
-
} = Utils;
|
|
15
|
-
|
|
16
|
-
export {
|
|
17
|
-
deg2rad,
|
|
18
|
-
diffAngle,
|
|
19
|
-
positiveMod,
|
|
20
|
-
rad2deg,
|
|
21
|
-
wrap,
|
|
22
|
-
Matrix,
|
|
23
|
-
Matrix3,
|
|
24
|
-
Matrix4,
|
|
25
|
-
Point2,
|
|
26
|
-
Polygon,
|
|
27
|
-
Quaternion,
|
|
28
|
-
Rotations,
|
|
29
|
-
Utils,
|
|
30
|
-
Vector,
|
|
31
|
-
Vector3
|
|
32
|
-
};
|
|
11
|
+
export * from './src/Utils.js';
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"directory": "packages/maths"
|
|
13
13
|
},
|
|
14
14
|
"name": "@wemap/maths",
|
|
15
|
-
"version": "
|
|
15
|
+
"version": "4.0.0",
|
|
16
16
|
"bugs": {
|
|
17
17
|
"url": "https://github.com/wemap/wemap-modules-js/issues"
|
|
18
18
|
},
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"wemap"
|
|
26
26
|
],
|
|
27
27
|
"license": "ISC",
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "8b72858590fc2ff33fbea4f090de8cd353a7445f"
|
|
29
29
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import chai from 'chai';
|
|
2
|
+
|
|
3
|
+
import Point2 from './Point2.js';
|
|
4
|
+
|
|
5
|
+
const { expect } = chai;
|
|
6
|
+
|
|
7
|
+
describe('Point2', () => {
|
|
8
|
+
|
|
9
|
+
it('distanceToPoint', () => {
|
|
10
|
+
expect(Point2.distanceToPoint([0, 0], [1, 1])).equal(Math.SQRT2);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('distanceToSegment', () => {
|
|
14
|
+
expect(Point2.distanceToSegment([1, 1], [0, 0], [0, 2])).equal(1);
|
|
15
|
+
expect(Point2.distanceToSegment([0, -1], [0, 0], [0, 2])).equal(1);
|
|
16
|
+
expect(Point2.distanceToSegment([0, 3], [0, 0], [0, 2])).equal(1);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
});
|
package/src/Polygon.js
CHANGED
|
@@ -13,7 +13,7 @@ class Polygon {
|
|
|
13
13
|
const xj = polygon[j][0], yj = polygon[j][1];
|
|
14
14
|
|
|
15
15
|
const intersect = ((yi > y) !== (yj > y))
|
|
16
|
-
&& (x
|
|
16
|
+
&& (x <= (xj - xi) * (y - yi) / (yj - yi) + xi);
|
|
17
17
|
if (intersect) {
|
|
18
18
|
inside = !inside;
|
|
19
19
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import chai from 'chai';
|
|
2
|
+
|
|
3
|
+
import Polygon from './Polygon.js';
|
|
4
|
+
|
|
5
|
+
const { expect } = chai;
|
|
6
|
+
|
|
7
|
+
describe('Polygon', () => {
|
|
8
|
+
|
|
9
|
+
const polygon1 = [[0, 0], [0, 2], [1, 2], [1, 0]];
|
|
10
|
+
|
|
11
|
+
it('isPointInside', () => {
|
|
12
|
+
|
|
13
|
+
expect(Polygon.isPointInside(polygon1, [0.5, 1])).true;
|
|
14
|
+
expect(Polygon.isPointInside(polygon1, [1, 1])).true;
|
|
15
|
+
expect(Polygon.isPointInside(polygon1, [0.5, 0])).true;
|
|
16
|
+
|
|
17
|
+
expect(Polygon.isPointInside(polygon1, [2, 1])).false;
|
|
18
|
+
expect(Polygon.isPointInside(polygon1, [-1, 1])).false;
|
|
19
|
+
expect(Polygon.isPointInside(polygon1, [0.5, 3])).false;
|
|
20
|
+
expect(Polygon.isPointInside(polygon1, [0.5, -1])).false;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
it('distanceToPoint', () => {
|
|
25
|
+
|
|
26
|
+
expect(Polygon.distanceToPoint(polygon1, [0.5, 1])).equal(0.5);
|
|
27
|
+
expect(Polygon.distanceToPoint(polygon1, [0, 1])).equal(0);
|
|
28
|
+
expect(Polygon.distanceToPoint(polygon1, [2, 1])).equal(1);
|
|
29
|
+
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('distanceToOtherRectangle', () => {
|
|
33
|
+
|
|
34
|
+
let polygon2;
|
|
35
|
+
|
|
36
|
+
polygon2 = [[3, -1], [8, -1], [8, 4], [3, 4]];
|
|
37
|
+
expect(Polygon.distanceToOtherRectangle(polygon1, polygon2)).equal(2);
|
|
38
|
+
|
|
39
|
+
polygon2 = [[0.5, 1], [0.5, 3], [1.5, 3], [1.5, 1]];
|
|
40
|
+
expect(Polygon.distanceToOtherRectangle(polygon1, polygon2)).equal(0);
|
|
41
|
+
|
|
42
|
+
polygon2 = [[-1, -1], [-1, 3], [2, 3], [2, -1]];
|
|
43
|
+
expect(Polygon.distanceToOtherRectangle(polygon2, polygon1)).equal(0);
|
|
44
|
+
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
});
|
package/src/Quaternion.js
CHANGED
|
@@ -106,6 +106,10 @@ class Quaternion {
|
|
|
106
106
|
return Math.acos(Math.min(2 * (Quaternion.dot(q1, q2) ** 2) - 1, 1));
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
static equalsTo(q1, q2) {
|
|
110
|
+
return Quaternion.distance(q1, q2) < 1e-8;
|
|
111
|
+
}
|
|
112
|
+
|
|
109
113
|
static normalize(quaternion) {
|
|
110
114
|
const norm = this.norm(quaternion);
|
|
111
115
|
const output = new Array(4);
|
|
@@ -144,5 +148,62 @@ class Quaternion {
|
|
|
144
148
|
return Quaternion.normalize(quat);
|
|
145
149
|
}
|
|
146
150
|
|
|
151
|
+
// https://github.com/stackgl/gl-quat/blob/master/slerp.js
|
|
152
|
+
/**
|
|
153
|
+
* Performs a spherical linear interpolation between two quat
|
|
154
|
+
*
|
|
155
|
+
* @param {number[]} a the first operand
|
|
156
|
+
* @param {number[]} b the second operand
|
|
157
|
+
* @param {number} t interpolation amount between the two inputs
|
|
158
|
+
* @returns {number[]} out
|
|
159
|
+
*/
|
|
160
|
+
static slerp(a, b, t) {
|
|
161
|
+
|
|
162
|
+
let ax = a[1], ay = a[2], az = a[3], aw = a[0],
|
|
163
|
+
bx = b[1], by = b[2], bz = b[3], bw = b[0];
|
|
164
|
+
|
|
165
|
+
let cosom, scale0, scale1;
|
|
166
|
+
|
|
167
|
+
// calc cosine
|
|
168
|
+
cosom = ax * bx + ay * by + az * bz + aw * bw;
|
|
169
|
+
// adjust signs (if necessary)
|
|
170
|
+
if (cosom < 0.0) {
|
|
171
|
+
cosom = -cosom;
|
|
172
|
+
bw = -bw;
|
|
173
|
+
bx = -bx;
|
|
174
|
+
by = -by;
|
|
175
|
+
bz = -bz;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// calculate coefficients
|
|
179
|
+
const useLinearInterp = (1.0 - cosom) < 0.000001;
|
|
180
|
+
if (!useLinearInterp) {
|
|
181
|
+
// standard case (slerp)
|
|
182
|
+
const omega = Math.acos(cosom);
|
|
183
|
+
const sinom = Math.sin(omega);
|
|
184
|
+
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
|
185
|
+
scale1 = Math.sin(t * omega) / sinom;
|
|
186
|
+
} else {
|
|
187
|
+
// "from" and "to" quaternions are very close
|
|
188
|
+
// ... so we can do a linear interpolation
|
|
189
|
+
scale0 = 1.0 - t;
|
|
190
|
+
scale1 = t;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// calculate final values
|
|
194
|
+
let output = [
|
|
195
|
+
scale0 * aw + scale1 * bw,
|
|
196
|
+
scale0 * ax + scale1 * bx,
|
|
197
|
+
scale0 * ay + scale1 * by,
|
|
198
|
+
scale0 * az + scale1 * bz
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
if (useLinearInterp) {
|
|
202
|
+
output = Quaternion.normalize(output);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return output;
|
|
206
|
+
}
|
|
207
|
+
|
|
147
208
|
}
|
|
148
209
|
export default Quaternion;
|
package/src/Quaternion.spec.js
CHANGED
|
@@ -120,4 +120,25 @@ describe('Quaternion', () => {
|
|
|
120
120
|
);
|
|
121
121
|
expect(result).deep.almost([Math.SQRT1_2, Math.SQRT1_2, 0, 0]);
|
|
122
122
|
});
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
it('slerp', () => {
|
|
126
|
+
const qFrom = [1, 0, 0, 0];
|
|
127
|
+
let qTo = [Math.SQRT1_2, 0, 0, Math.SQRT1_2];
|
|
128
|
+
let qSlerp;
|
|
129
|
+
|
|
130
|
+
qSlerp = Quaternion.slerp(qFrom, qTo, 1 / 3);
|
|
131
|
+
expect(Quaternion.equalsTo(qSlerp, [0.9659258262890684, 0, 0, 0.2588190451025208])).true;
|
|
132
|
+
|
|
133
|
+
qSlerp = Quaternion.slerp(qFrom, qTo, 2 / 3);
|
|
134
|
+
expect(Quaternion.equalsTo(qSlerp, [0.8660254037844387, 0, 0, 0.5])).true;
|
|
135
|
+
|
|
136
|
+
qSlerp = Quaternion.slerp([-1, 0, 0, 0], qTo, 2 / 3);
|
|
137
|
+
expect(Quaternion.equalsTo(qSlerp, [0.8660254037844387, 0, 0, 0.5])).true;
|
|
138
|
+
|
|
139
|
+
qTo = Quaternion.normalize([1, 1e-4, 0, 0]);
|
|
140
|
+
qSlerp = Quaternion.slerp([1, 0, 0, 0], qTo, 1 / 2);
|
|
141
|
+
expect(Quaternion.equalsTo(qSlerp, [0.99999999875, 0.0000499999998125, 0, 0])).true;
|
|
142
|
+
|
|
143
|
+
});
|
|
123
144
|
});
|
package/src/Rotations.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { deg2rad, rad2deg } from './Utils.js';
|
|
2
2
|
|
|
3
3
|
class Rotations {
|
|
4
4
|
|
|
5
5
|
// ZXY because yaw is around Z axis, pitch around X axis and roll around Y axis.
|
|
6
6
|
// They are conventions for a smartphone frame.
|
|
7
7
|
static eulerToQuaternionZXYDegrees(eulerAnglesDegrees) {
|
|
8
|
-
const eulerAngles = eulerAnglesDegrees.map(x =>
|
|
8
|
+
const eulerAngles = eulerAnglesDegrees.map(x => deg2rad(x));
|
|
9
9
|
return Rotations.eulerToQuaternionZXY(eulerAngles);
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -41,7 +41,7 @@ class Rotations {
|
|
|
41
41
|
|
|
42
42
|
static quaternionToEulerZXYDegrees(quaternion) {
|
|
43
43
|
const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
|
|
44
|
-
return eulerAngles.map(x =>
|
|
44
|
+
return eulerAngles.map(x => rad2deg(x));
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
static quaternionToEulerZXY(quaternion) {
|
|
@@ -78,6 +78,123 @@ class Rotations {
|
|
|
78
78
|
return [alpha, beta, gamma];
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
static quaternionToEulerDegrees(quaternion, order) {
|
|
82
|
+
const eulerAngles = Rotations.quaternionToEuler(quaternion, order);
|
|
83
|
+
return eulerAngles.map(x => rad2deg(x));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// https://searchcode.com/codesearch/view/55080466/
|
|
87
|
+
static quaternionToEuler(quaternion, order) {
|
|
88
|
+
|
|
89
|
+
const qw = quaternion[0];
|
|
90
|
+
const qx = quaternion[1];
|
|
91
|
+
const qy = quaternion[2];
|
|
92
|
+
const qz = quaternion[3];
|
|
93
|
+
|
|
94
|
+
switch (order.toLowerCase()) {
|
|
95
|
+
case 'zyx':
|
|
96
|
+
return Rotations._threeaxisrot(2 * (qx * qy + qw * qz),
|
|
97
|
+
qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
|
|
98
|
+
-2 * (qx * qz - qw * qy),
|
|
99
|
+
2 * (qy * qz + qw * qx),
|
|
100
|
+
qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2);
|
|
101
|
+
case 'zyz':
|
|
102
|
+
return Rotations._twoaxisrot(2 * (qy * qz - qw * qx),
|
|
103
|
+
2 * (qx * qz + qw * qy),
|
|
104
|
+
qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
|
|
105
|
+
2 * (qy * qz + qw * qx),
|
|
106
|
+
-2 * (qx * qz - qw * qy));
|
|
107
|
+
|
|
108
|
+
case 'zxy':
|
|
109
|
+
return Rotations._threeaxisrot(-2 * (qx * qy - qw * qz),
|
|
110
|
+
qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
|
|
111
|
+
2 * (qy * qz + qw * qx),
|
|
112
|
+
-2 * (qx * qz - qw * qy),
|
|
113
|
+
qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2);
|
|
114
|
+
|
|
115
|
+
case 'zxz':
|
|
116
|
+
return Rotations._twoaxisrot(2 * (qx * qz + qw * qy),
|
|
117
|
+
-2 * (qy * qz - qw * qx),
|
|
118
|
+
qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
|
|
119
|
+
2 * (qx * qz - qw * qy),
|
|
120
|
+
2 * (qy * qz + qw * qx));
|
|
121
|
+
|
|
122
|
+
case 'yxz':
|
|
123
|
+
return Rotations._threeaxisrot(2 * (qx * qz + qw * qy),
|
|
124
|
+
qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
|
|
125
|
+
-2 * (qy * qz - qw * qx),
|
|
126
|
+
2 * (qx * qy + qw * qz),
|
|
127
|
+
qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2);
|
|
128
|
+
|
|
129
|
+
case 'yxy':
|
|
130
|
+
return Rotations._twoaxisrot(2 * (qx * qy - qw * qz),
|
|
131
|
+
2 * (qy * qz + qw * qx),
|
|
132
|
+
qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
|
|
133
|
+
2 * (qx * qy + qw * qz),
|
|
134
|
+
-2 * (qy * qz - qw * qx));
|
|
135
|
+
|
|
136
|
+
case 'yzx':
|
|
137
|
+
return Rotations._threeaxisrot(-2 * (qx * qz - qw * qy),
|
|
138
|
+
qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
|
|
139
|
+
2 * (qx * qy + qw * qz),
|
|
140
|
+
-2 * (qy * qz - qw * qx),
|
|
141
|
+
qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2);
|
|
142
|
+
|
|
143
|
+
case 'yzy':
|
|
144
|
+
return Rotations._twoaxisrot(2 * (qy * qz + qw * qx),
|
|
145
|
+
-2 * (qx * qy - qw * qz),
|
|
146
|
+
qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
|
|
147
|
+
2 * (qy * qz - qw * qx),
|
|
148
|
+
2 * (qx * qy + qw * qz));
|
|
149
|
+
|
|
150
|
+
case 'xyz':
|
|
151
|
+
return Rotations._threeaxisrot(-2 * (qy * qz - qw * qx),
|
|
152
|
+
qw ** 2 - qx ** 2 - qy ** 2 + qz ** 2,
|
|
153
|
+
2 * (qx * qz + qw * qy),
|
|
154
|
+
-2 * (qx * qy - qw * qz),
|
|
155
|
+
qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2);
|
|
156
|
+
|
|
157
|
+
case 'xyx':
|
|
158
|
+
return Rotations._twoaxisrot(2 * (qx * qy + qw * qz),
|
|
159
|
+
-2 * (qx * qz - qw * qy),
|
|
160
|
+
qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
|
|
161
|
+
2 * (qx * qy - qw * qz),
|
|
162
|
+
2 * (qx * qz + qw * qy));
|
|
163
|
+
|
|
164
|
+
case 'xzy':
|
|
165
|
+
return Rotations._threeaxisrot(2 * (qy * qz + qw * qx),
|
|
166
|
+
qw ** 2 - qx ** 2 + qy ** 2 - qz ** 2,
|
|
167
|
+
-2 * (qx * qy - qw * qz),
|
|
168
|
+
2 * (qx * qz + qw * qy),
|
|
169
|
+
qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2);
|
|
170
|
+
|
|
171
|
+
case 'xzx':
|
|
172
|
+
return Rotations._twoaxisrot(2 * (qx * qz - qw * qy),
|
|
173
|
+
2 * (qx * qy + qw * qz),
|
|
174
|
+
qw ** 2 + qx ** 2 - qy ** 2 - qz ** 2,
|
|
175
|
+
2 * (qx * qz + qw * qy),
|
|
176
|
+
-2 * (qx * qy - qw * qz));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
throw new Error('unknown order');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// eslint-disable-next-line max-params
|
|
183
|
+
static _threeaxisrot(r11, r12, r21, r31, r32) {
|
|
184
|
+
const r1 = Math.atan2(r11, r12);
|
|
185
|
+
const r2 = Math.asin(r21);
|
|
186
|
+
const r3 = Math.atan2(r31, r32);
|
|
187
|
+
return [r1, r2, r3];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// eslint-disable-next-line max-params
|
|
191
|
+
static _twoaxisrot(r11, r12, r21, r31, r32) {
|
|
192
|
+
const r1 = Math.atan2(r11, r12);
|
|
193
|
+
const r2 = Math.acos(r21);
|
|
194
|
+
const r3 = Math.atan2(r31, r32);
|
|
195
|
+
return [r1, r2, r3];
|
|
196
|
+
}
|
|
197
|
+
|
|
81
198
|
|
|
82
199
|
static getHeadingFromQuaternion(quaternion) {
|
|
83
200
|
// Minus before "yaw" angle is here because ENU attitude is counter-clockwise whereas Coordinates is clockwise.
|
package/src/Rotations.spec.js
CHANGED
|
@@ -20,6 +20,22 @@ describe('Rotations', () => {
|
|
|
20
20
|
expect(result).deep.almost(e1);
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
+
it('quaternionToEuler', () => {
|
|
24
|
+
expect(Rotations.quaternionToEuler(q1, 'zyx')).deep.almost([2.356194490192345, -0.3398369094541224, 1.4288992721907334]);
|
|
25
|
+
expect(Rotations.quaternionToEuler(q1, 'zyz')).deep.almost([0.7378150601204648, 1.4370647373849559, 1.9138202672156008]);
|
|
26
|
+
expect(Rotations.quaternionToEuler(q1, 'zxy')).deep.almost(e1);
|
|
27
|
+
expect(Rotations.quaternionToEuler(q1, 'zxz')).deep.almost([2.3086113869153615, 1.4370647373849559, 0.34302394042070417]);
|
|
28
|
+
expect(Rotations.quaternionToEuler(q1, 'yxz')).deep.almost([1.3909428270024193, -0.7297276562269656, 2.0344439357957027]);
|
|
29
|
+
expect(Rotations.quaternionToEuler(q1, 'yxy')).deep.almost([0.141897054604165, 1.9106332362490184, 2.356194490192345]);
|
|
30
|
+
expect(Rotations.quaternionToEuler(q1, 'yzx')).deep.almost([-2.677945044588986, 0.7297276562269656, -2.0344439357957027]);
|
|
31
|
+
expect(Rotations.quaternionToEuler(q1, 'yzy')).deep.almost([1.7126933813990617, 1.9106332362490184, 0.7853981633974483]);
|
|
32
|
+
expect(Rotations.quaternionToEuler(q1, 'xyz')).deep.almost([-1.3734007669450168, 0.8232119771258751, -2.944197093739911]);
|
|
33
|
+
expect(Rotations.quaternionToEuler(q1, 'xyx')).deep.almost([2.0344439357957036, 2.300523983021862, 0.17985349979247958]);
|
|
34
|
+
expect(Rotations.quaternionToEuler(q1, 'xzy')).deep.almost([1.9138202672156002, -0.13373158940994245, 2.308611386915361]);
|
|
35
|
+
expect(Rotations.quaternionToEuler(q1, 'xzx')).deep.almost([0.4636476090008071, 2.300523983021862, 1.7506498265873762]);
|
|
36
|
+
expect(() => Rotations.quaternionToEuler(q1, 'abc')).throw(Error);
|
|
37
|
+
});
|
|
38
|
+
|
|
23
39
|
it('quaternionToEulerZXYDegrees', () => {
|
|
24
40
|
let result;
|
|
25
41
|
|
|
@@ -33,6 +49,11 @@ describe('Rotations', () => {
|
|
|
33
49
|
expect(result).deep.almost([0, -90, 0]);
|
|
34
50
|
});
|
|
35
51
|
|
|
52
|
+
it('quaternionToEulerDegrees', () => {
|
|
53
|
+
const result = Rotations.quaternionToEulerDegrees(q1, 'zxy');
|
|
54
|
+
expect(result).deep.almost(E1);
|
|
55
|
+
});
|
|
56
|
+
|
|
36
57
|
it('eulerToQuaternionZXY', () => {
|
|
37
58
|
let result;
|
|
38
59
|
|
package/src/Utils.js
CHANGED
|
@@ -1,46 +1,63 @@
|
|
|
1
1
|
|
|
2
|
-
class Utils {
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Transform an angle in degre to radian
|
|
6
|
-
* @param {Number} angle
|
|
7
|
-
* @return {Number}
|
|
8
|
-
*/
|
|
9
|
-
static deg2rad(angle) {
|
|
10
|
-
return angle * (Math.PI / 180);
|
|
11
|
-
}
|
|
12
2
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Transform an angle in degre to radian
|
|
5
|
+
* @param {Number} angle
|
|
6
|
+
* @return {Number}
|
|
7
|
+
*/
|
|
8
|
+
export function deg2rad(angle) {
|
|
9
|
+
return angle * (Math.PI / 180);
|
|
10
|
+
}
|
|
16
11
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* @param {Number} angle2 in radians
|
|
21
|
-
*/
|
|
22
|
-
static diffAngle(angle1, angle2) {
|
|
23
|
-
return Math.atan2(Math.sin(angle1 - angle2), Math.cos(angle1 - angle2));
|
|
24
|
-
}
|
|
12
|
+
export function rad2deg(rad) {
|
|
13
|
+
return rad * 180 / Math.PI;
|
|
14
|
+
}
|
|
25
15
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Difference between two angles
|
|
18
|
+
* @param {Number} angle1 in radians
|
|
19
|
+
* @param {Number} angle2 in radians
|
|
20
|
+
*/
|
|
21
|
+
export function diffAngle(angle1, angle2) {
|
|
22
|
+
return Math.atan2(Math.sin(angle1 - angle2), Math.cos(angle1 - angle2));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Difference between two angles in degrees
|
|
27
|
+
* @param {Number} angle1 in degrees
|
|
28
|
+
* @param {Number} angle2 in degrees
|
|
29
|
+
*/
|
|
30
|
+
export function diffAngleDegrees(angle1, angle2) {
|
|
31
|
+
return rad2deg(diffAngle(deg2rad(angle1), deg2rad(angle2)));
|
|
32
|
+
}
|
|
29
33
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const d = max - min;
|
|
40
|
-
const w = ((n - min) % d + d) % d + min;
|
|
41
|
-
return (w === min) ? max : w;
|
|
34
|
+
/**
|
|
35
|
+
* Difference angle between two lines
|
|
36
|
+
* @param {Number} angle1 line1 angle in radians
|
|
37
|
+
* @param {Number} angle2 line2 angle in radians
|
|
38
|
+
*/
|
|
39
|
+
export function diffAngleLines(angle1, angle2) {
|
|
40
|
+
let _diffAngle = Math.abs(diffAngle(angle1, angle2));
|
|
41
|
+
if (_diffAngle > Math.PI / 2) {
|
|
42
|
+
_diffAngle = Math.PI - _diffAngle;
|
|
42
43
|
}
|
|
44
|
+
return _diffAngle;
|
|
45
|
+
}
|
|
43
46
|
|
|
47
|
+
export function positiveMod(n, m) {
|
|
48
|
+
return ((n % m) + m) % m;
|
|
44
49
|
}
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
/**
|
|
52
|
+
* constrain n to the given range, excluding the minimum, via modular arithmetic
|
|
53
|
+
*
|
|
54
|
+
* @param {number} n value
|
|
55
|
+
* @param {number} min the minimum value to be returned, exclusive
|
|
56
|
+
* @param {number} max the maximum value to be returned, inclusive
|
|
57
|
+
* @returns {number} constrained number
|
|
58
|
+
*/
|
|
59
|
+
export function wrap(n, min, max) {
|
|
60
|
+
const d = max - min;
|
|
61
|
+
const w = ((n - min) % d + d) % d + min;
|
|
62
|
+
return (w === min) ? max : w;
|
|
63
|
+
}
|
package/src/Utils.spec.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import chai from 'chai';
|
|
2
2
|
import chaiAlmost from 'chai-almost';
|
|
3
3
|
|
|
4
|
-
import
|
|
4
|
+
import {
|
|
5
|
+
deg2rad,
|
|
6
|
+
rad2deg,
|
|
7
|
+
diffAngle,
|
|
8
|
+
diffAngleDegrees,
|
|
9
|
+
diffAngleLines,
|
|
10
|
+
positiveMod,
|
|
11
|
+
wrap
|
|
12
|
+
} from './Utils.js';
|
|
5
13
|
|
|
6
14
|
const { expect } = chai;
|
|
7
15
|
chai.use(chaiAlmost());
|
|
@@ -9,35 +17,65 @@ chai.use(chaiAlmost());
|
|
|
9
17
|
describe('MathUtils', () => {
|
|
10
18
|
|
|
11
19
|
it('deg2rad', () => {
|
|
12
|
-
expect(
|
|
13
|
-
expect(
|
|
14
|
-
expect(
|
|
15
|
-
expect(
|
|
20
|
+
expect(deg2rad(1)).to.equal(Math.PI / 180);
|
|
21
|
+
expect(deg2rad(90)).to.equal(Math.PI / 2);
|
|
22
|
+
expect(deg2rad(0)).to.equal(0);
|
|
23
|
+
expect(deg2rad(135)).to.equal(3 * Math.PI / 4);
|
|
16
24
|
});
|
|
17
25
|
|
|
18
26
|
|
|
19
27
|
it('rad2deg', () => {
|
|
20
|
-
expect(
|
|
21
|
-
expect(
|
|
22
|
-
expect(
|
|
23
|
-
expect(
|
|
28
|
+
expect(rad2deg(Math.PI / 180)).to.equal(1);
|
|
29
|
+
expect(rad2deg(Math.PI / 2)).to.equal(90);
|
|
30
|
+
expect(rad2deg(0)).to.equal(0);
|
|
31
|
+
expect(rad2deg(3 * Math.PI / 4)).to.equal(135);
|
|
24
32
|
});
|
|
25
33
|
|
|
26
34
|
|
|
27
35
|
it('diffAngle', () => {
|
|
28
|
-
expect(
|
|
29
|
-
expect(
|
|
30
|
-
expect(
|
|
31
|
-
expect(
|
|
32
|
-
expect(
|
|
33
|
-
expect(
|
|
36
|
+
expect(diffAngle(0, 0)).to.equal(0);
|
|
37
|
+
expect(diffAngle(Math.PI, 0)).to.equal(Math.PI);
|
|
38
|
+
expect(diffAngle(3 * Math.PI, 0)).to.almost.equal(Math.PI);
|
|
39
|
+
expect(diffAngle(Math.PI, Math.PI / 2)).to.almost.equal(Math.PI / 2);
|
|
40
|
+
expect(diffAngle(Math.PI / 2, Math.PI)).to.almost.equal(-Math.PI / 2);
|
|
41
|
+
expect(diffAngle(Math.PI / 2, 3 * Math.PI)).to.almost.equal(-Math.PI / 2);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('diffAngleDegrees', () => {
|
|
45
|
+
expect(diffAngleDegrees(0, 0)).to.equal(0);
|
|
46
|
+
expect(diffAngleDegrees(180, 0)).to.equal(180);
|
|
47
|
+
expect(diffAngleDegrees(540, 0)).to.almost.equal(180);
|
|
48
|
+
expect(diffAngleDegrees(180, 90)).to.almost.equal(90);
|
|
49
|
+
expect(diffAngleDegrees(90, 180)).to.almost.equal(-90);
|
|
50
|
+
expect(diffAngleDegrees(90, 540)).to.almost.equal(-90);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
it('diffAngleLines', () => {
|
|
55
|
+
expect(diffAngleLines(0, 0)).to.equal(0);
|
|
56
|
+
expect(diffAngleLines(Math.PI, 0)).to.equal(0);
|
|
57
|
+
expect(diffAngleLines(3 * Math.PI, 0)).to.almost.equal(0);
|
|
58
|
+
expect(diffAngleLines(3 * Math.PI / 4, 0)).to.almost.equal(Math.PI / 4);
|
|
59
|
+
expect(diffAngleLines(Math.PI, Math.PI / 2)).to.almost.equal(Math.PI / 2);
|
|
60
|
+
expect(diffAngleLines(Math.PI / 2, Math.PI)).to.almost.equal(Math.PI / 2);
|
|
61
|
+
expect(diffAngleLines(Math.PI / 2, 3 * Math.PI)).to.almost.equal(Math.PI / 2);
|
|
34
62
|
});
|
|
35
63
|
|
|
36
64
|
|
|
37
65
|
it('positiveMod', () => {
|
|
38
|
-
expect(
|
|
39
|
-
expect(
|
|
40
|
-
expect(
|
|
41
|
-
expect(
|
|
66
|
+
expect(positiveMod(6, 4)).to.equal(2);
|
|
67
|
+
expect(positiveMod(-6, 4)).to.equal(2);
|
|
68
|
+
expect(positiveMod(-1, 4)).to.equal(3);
|
|
69
|
+
expect(positiveMod(-12, 4)).to.equal(0);
|
|
42
70
|
});
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
it('wrap', () => {
|
|
74
|
+
expect(wrap(5, 0, 10)).to.equal(5);
|
|
75
|
+
expect(wrap(24, 0, 10)).to.equal(4);
|
|
76
|
+
expect(wrap(-4, 0, 10)).to.equal(6);
|
|
77
|
+
expect(wrap(0, 0, 10)).to.equal(10);
|
|
78
|
+
expect(wrap(10, 0, 10)).to.equal(10);
|
|
79
|
+
});
|
|
80
|
+
|
|
43
81
|
});
|