@wemap/maths 3.2.3 → 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/index.js CHANGED
@@ -10,7 +10,7 @@ import Vector from './src/Vector.js';
10
10
  import Vector3 from './src/Vector3.js';
11
11
 
12
12
  const {
13
- deg2rad, diffAngle, positiveMod, rad2deg
13
+ deg2rad, diffAngle, positiveMod, rad2deg, wrap
14
14
  } = Utils;
15
15
 
16
16
  export {
@@ -18,6 +18,7 @@ export {
18
18
  diffAngle,
19
19
  positiveMod,
20
20
  rad2deg,
21
+ wrap,
21
22
  Matrix,
22
23
  Matrix3,
23
24
  Matrix4,
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/maths"
13
13
  },
14
14
  "name": "@wemap/maths",
15
- "version": "3.2.3",
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": "f7075f59fc3227c7f4cb7f1fa8cc69b98705364e"
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.
@@ -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
 
package/src/Utils.js CHANGED
@@ -26,6 +26,21 @@ class Utils {
26
26
  static positiveMod(n, m) {
27
27
  return ((n % m) + m) % m;
28
28
  }
29
+
30
+ /**
31
+ * constrain n to the given range, excluding the minimum, via modular arithmetic
32
+ *
33
+ * @param {number} n value
34
+ * @param {number} min the minimum value to be returned, exclusive
35
+ * @param {number} max the maximum value to be returned, inclusive
36
+ * @returns {number} constrained number
37
+ */
38
+ static wrap(n, min, max) {
39
+ const d = max - min;
40
+ const w = ((n - min) % d + d) % d + min;
41
+ return (w === min) ? max : w;
42
+ }
43
+
29
44
  }
30
45
 
31
46
  export default Utils;