@wemap/maths 3.2.15 → 3.2.17
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/Rotations.js +117 -0
- package/src/Rotations.spec.js +10 -0
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"directory": "packages/maths"
|
|
13
13
|
},
|
|
14
14
|
"name": "@wemap/maths",
|
|
15
|
-
"version": "3.2.
|
|
15
|
+
"version": "3.2.17",
|
|
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": "8b5e0144472bfaa8e07ac24cb227294c83a7541a"
|
|
29
29
|
}
|
package/src/Rotations.js
CHANGED
|
@@ -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 => Utils.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,11 @@ describe('Rotations', () => {
|
|
|
20
20
|
expect(result).deep.almost(e1);
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
+
it('quaternionToEuler', () => {
|
|
24
|
+
const result = Rotations.quaternionToEuler(q1, 'zxy');
|
|
25
|
+
expect(result).deep.almost(e1);
|
|
26
|
+
});
|
|
27
|
+
|
|
23
28
|
it('quaternionToEulerZXYDegrees', () => {
|
|
24
29
|
let result;
|
|
25
30
|
|
|
@@ -33,6 +38,11 @@ describe('Rotations', () => {
|
|
|
33
38
|
expect(result).deep.almost([0, -90, 0]);
|
|
34
39
|
});
|
|
35
40
|
|
|
41
|
+
it('quaternionToEulerDegrees', () => {
|
|
42
|
+
const result = Rotations.quaternionToEulerDegrees(q1, 'zxy');
|
|
43
|
+
expect(result).deep.almost(E1);
|
|
44
|
+
});
|
|
45
|
+
|
|
36
46
|
it('eulerToQuaternionZXY', () => {
|
|
37
47
|
let result;
|
|
38
48
|
|