@wemap/maths 12.10.0 → 12.10.8-alpha.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/{index.ts → dist/index.d.ts} +0 -2
- package/dist/index.js +1 -1155
- package/dist/index.js.map +1 -1
- package/{src/EulerAngles.ts → dist/src/EulerAngles.d.ts} +2 -7
- package/dist/src/Geometry.d.ts +10 -0
- package/dist/src/Matrix.d.ts +13 -0
- package/dist/src/Matrix.spec.d.ts +1 -0
- package/dist/src/Matrix3.d.ts +20 -0
- package/dist/src/Matrix3.spec.d.ts +2 -0
- package/dist/src/Matrix4.d.ts +35 -0
- package/dist/src/Matrix4.spec.d.ts +1 -0
- package/dist/src/Point2.d.ts +17 -0
- package/dist/src/Point2.spec.d.ts +1 -0
- package/dist/src/Polygon.d.ts +7 -0
- package/dist/src/Polygon.spec.d.ts +1 -0
- package/dist/src/Quaternion.d.ts +71 -0
- package/dist/src/Quaternion.spec.d.ts +1 -0
- package/dist/src/Rotations.d.ts +13 -0
- package/dist/src/Rotations.spec.d.ts +1 -0
- package/dist/src/Utils.d.ts +38 -0
- package/dist/src/Utils.spec.d.ts +1 -0
- package/dist/src/Vector.d.ts +5 -0
- package/dist/src/Vector.spec.d.ts +1 -0
- package/dist/src/Vector3.d.ts +13 -0
- package/dist/src/Vector3.spec.d.ts +1 -0
- package/{src/types.ts → dist/src/types.d.ts} +37 -8
- package/dist/vite.config.d.ts +19 -0
- package/package.json +7 -4
- package/src/Geometry.ts +0 -39
- package/src/Matrix.spec.ts +0 -130
- package/src/Matrix.ts +0 -242
- package/src/Matrix3.spec.ts +0 -138
- package/src/Matrix3.ts +0 -165
- package/src/Matrix4.spec.ts +0 -187
- package/src/Matrix4.ts +0 -309
- package/src/Point2.spec.ts +0 -19
- package/src/Point2.ts +0 -54
- package/src/Polygon.spec.ts +0 -48
- package/src/Polygon.ts +0 -63
- package/src/Quaternion.spec.ts +0 -126
- package/src/Quaternion.ts +0 -292
- package/src/Rotations.spec.ts +0 -67
- package/src/Rotations.ts +0 -223
- package/src/Utils.spec.ts +0 -83
- package/src/Utils.ts +0 -77
- package/src/Vector.spec.ts +0 -15
- package/src/Vector.ts +0 -10
- package/src/Vector3.spec.ts +0 -52
- package/src/Vector3.ts +0 -51
- package/tsconfig.json +0 -3
- package/vite.config.ts +0 -4
package/src/Quaternion.ts
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
import { Matrix3_t, Quaternion_t, Vector3_t } from './types.js';
|
|
2
|
-
import Vector3 from './Vector3.js';
|
|
3
|
-
|
|
4
|
-
class Quaternion {
|
|
5
|
-
|
|
6
|
-
static ROTX_PI2 = this.rotX(Math.PI / 2);
|
|
7
|
-
static ROTX_PI = this.rotX(Math.PI);
|
|
8
|
-
static ROTX_MPI2 = this.rotX(-Math.PI / 2);
|
|
9
|
-
|
|
10
|
-
static ROTY_PI2 = this.rotY(Math.PI / 2);
|
|
11
|
-
static ROTY_PI = this.rotY(Math.PI);
|
|
12
|
-
static ROTY_MPI2 = this.rotY(-Math.PI / 2);
|
|
13
|
-
|
|
14
|
-
static ROTZ_PI2 = this.rotZ(Math.PI / 2);
|
|
15
|
-
static ROTZ_PI = this.rotZ(Math.PI);
|
|
16
|
-
static ROTZ_MPI2 = this.rotZ(-Math.PI / 2);
|
|
17
|
-
|
|
18
|
-
static get identity(): Quaternion_t {
|
|
19
|
-
return [1, 0, 0, 0];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static sum(q1: Quaternion_t, q2: Quaternion_t): Quaternion_t {
|
|
23
|
-
return [q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// From https://www.mathworks.com/help/aerotbx/ug/quatrotate.html
|
|
27
|
-
static rotateMatlab(q: Quaternion_t, v: Vector3_t): Vector3_t {
|
|
28
|
-
const [qw, qx, qy, qz] = q;
|
|
29
|
-
const [vx, vy, vz] = v;
|
|
30
|
-
return [
|
|
31
|
-
qw * (qw * vx - qy * vz + qz * vy) - qy * (qw * vz - qx * vy + qy * vx) + qx * (qx * vx + qy * vy + qz * vz) + qz * (qw * vy + qx * vz - qz * vx),
|
|
32
|
-
qw * (qw * vy + qx * vz - qz * vx) + qx * (qw * vz - qx * vy + qy * vx) + qy * (qx * vx + qy * vy + qz * vz) - qz * (qw * vx - qy * vz + qz * vy),
|
|
33
|
-
qw * (qw * vz - qx * vy + qy * vx) - qx * (qw * vy + qx * vz - qz * vx) + qy * (qw * vx - qy * vz + qz * vy) + qz * (qx * vx + qy * vy + qz * vz)
|
|
34
|
-
];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// From: https://eigen.tuxfamily.org/dox/Quaternion_8h_source.html
|
|
38
|
-
static rotate(q: Quaternion_t, v: Vector3_t): Vector3_t {
|
|
39
|
-
const [qw, qx, qy, qz] = q;
|
|
40
|
-
const thisVec: Vector3_t = [qx, qy, qz];
|
|
41
|
-
let uv = Vector3.cross(thisVec, v);
|
|
42
|
-
uv = Vector3.sum(uv, uv);
|
|
43
|
-
const tmp1 = Vector3.sum(v, Vector3.multiplyScalar(uv, qw));
|
|
44
|
-
return Vector3.sum(tmp1, Vector3.cross(thisVec, uv));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Unit Quaternion inverse
|
|
49
|
-
* @param {Array.<Number>} quaternion must be a unit quaternion.
|
|
50
|
-
*/
|
|
51
|
-
static inverse(q: Quaternion_t): Quaternion_t {
|
|
52
|
-
return [-q[0], q[1], q[2], q[3]];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Multiply quaternions
|
|
57
|
-
* @param {...Number[]} quaternions
|
|
58
|
-
* @returns {Number[]}
|
|
59
|
-
*/
|
|
60
|
-
static multiply(...quaternions: Quaternion_t[]): Quaternion_t {
|
|
61
|
-
|
|
62
|
-
if (quaternions.length === 2) {
|
|
63
|
-
|
|
64
|
-
const [qw, qx, qy, qz] = quaternions[0];
|
|
65
|
-
const [rw, rx, ry, rz] = quaternions[1];
|
|
66
|
-
|
|
67
|
-
return [
|
|
68
|
-
qw * rw - qx * rx - qy * ry - qz * rz,
|
|
69
|
-
qw * rx + rw * qx + qy * rz - qz * ry,
|
|
70
|
-
qw * ry + rw * qy + qz * rx - qx * rz,
|
|
71
|
-
qw * rz + rw * qz + qx * ry - qy * rx
|
|
72
|
-
];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return quaternions.reduce((acc, quat) => this.multiply(acc, quat), Quaternion.identity);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Axis-angle to quaternion
|
|
80
|
-
* @param {Array[Number]} axis unit vector
|
|
81
|
-
* @param {Number} angle in radians
|
|
82
|
-
*/
|
|
83
|
-
static fromAxisAngle(axis: Vector3_t, angle: number): Quaternion_t {
|
|
84
|
-
const s = Math.sin(angle / 2);
|
|
85
|
-
return [Math.cos(angle / 2), axis[0] * s, axis[1] * s, axis[2] * s];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Rotation around x-axis
|
|
90
|
-
* @param {Number} angle in radians
|
|
91
|
-
*/
|
|
92
|
-
static rotX(angle: number): Quaternion_t {
|
|
93
|
-
return this.fromAxisAngle([1, 0, 0], angle);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Rotation around x-axis
|
|
98
|
-
* @param {Number} angle in radians
|
|
99
|
-
*/
|
|
100
|
-
static rotY(angle: number): Quaternion_t {
|
|
101
|
-
return this.fromAxisAngle([0, 1, 0], angle);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Rotation around x-axis
|
|
106
|
-
* @param {Number} angle in radians
|
|
107
|
-
*/
|
|
108
|
-
static rotZ(angle: number): Quaternion_t {
|
|
109
|
-
return this.fromAxisAngle([0, 0, 1], angle);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
static fromMatrix3(m: Matrix3_t): Quaternion_t {
|
|
113
|
-
const qw = Math.sqrt(1 + m[0][0] + m[1][1] + m[2][2]) / 2;
|
|
114
|
-
const qx = (m[2][1] - m[1][2]) / (4 * qw);
|
|
115
|
-
const qy = (m[0][2] - m[2][0]) / (4 * qw);
|
|
116
|
-
const qz = (m[1][0] - m[0][1]) / (4 * qw);
|
|
117
|
-
return [qw, qx, qy, qz];
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
static fromMatrix3Matlab(m: Matrix3_t): Quaternion_t {
|
|
122
|
-
const qw = Math.sqrt(1 + m[0][0] + m[1][1] + m[2][2]) / 2;
|
|
123
|
-
const qx = (m[1][2] - m[2][1]) / (4 * qw);
|
|
124
|
-
const qy = (m[2][0] - m[0][2]) / (4 * qw);
|
|
125
|
-
const qz = (m[0][1] - m[1][0]) / (4 * qw);
|
|
126
|
-
return [qw, qx, qy, qz];
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
static toMatrix3(q: Quaternion_t): Matrix3_t {
|
|
130
|
-
const [w, x, y, z] = q;
|
|
131
|
-
|
|
132
|
-
const wx = w * x, wy = w * y, wz = w * z;
|
|
133
|
-
const xx = x * x, xy = x * y, xz = x * z;
|
|
134
|
-
const yy = y * y, yz = y * z, zz = z * z;
|
|
135
|
-
|
|
136
|
-
return [
|
|
137
|
-
[1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy)],
|
|
138
|
-
[2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx)],
|
|
139
|
-
[2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)]];
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
static wxyz2xyzw(wxyz: Quaternion_t): Quaternion_t {
|
|
144
|
-
return [wxyz[1], wxyz[2], wxyz[3], wxyz[0]];
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
static xyzw2wxyz(xyzw: Quaternion_t): Quaternion_t {
|
|
148
|
-
return [xyzw[3], xyzw[0], xyzw[1], xyzw[2]];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
static distance(q1: Quaternion_t, q2: Quaternion_t): number {
|
|
152
|
-
return Math.acos(Math.min(2 * (Quaternion.dot(q1, q2) ** 2) - 1, 1));
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
static equals(q1: Quaternion_t, q2: Quaternion_t): boolean {
|
|
156
|
-
return Quaternion.distance(q1, q2) < 1e-8;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
static normalize(quaternion: Quaternion_t): Quaternion_t {
|
|
160
|
-
const norm = this.norm(quaternion);
|
|
161
|
-
const output = new Array(4) as Quaternion_t;
|
|
162
|
-
for (let i = 0; i < 4; i++) {
|
|
163
|
-
output[i] = quaternion[i] / norm;
|
|
164
|
-
}
|
|
165
|
-
return output;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
static norm(quaternion: Quaternion_t): number {
|
|
169
|
-
return Math.sqrt(quaternion[0] ** 2 + quaternion[1] ** 2 + quaternion[2] ** 2 + quaternion[3] ** 2);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
static dot(q1: Quaternion_t, q2: Quaternion_t) : number {
|
|
173
|
-
return q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
static getRotationBetweenTwoVectors(from: Vector3_t, to: Vector3_t): Quaternion_t {
|
|
177
|
-
const fromNormalized = Vector3.normalize(from);
|
|
178
|
-
const toNormalized = Vector3.normalize(to);
|
|
179
|
-
const dot = Vector3.dot(fromNormalized, toNormalized);
|
|
180
|
-
let tmp: Vector3_t;
|
|
181
|
-
if (dot < -0.999999) {
|
|
182
|
-
tmp = Vector3.cross([1, 0, 0], fromNormalized);
|
|
183
|
-
if (Vector3.norm(tmp) < 0.000001) {
|
|
184
|
-
tmp = Vector3.cross([0, 1, 0], fromNormalized);
|
|
185
|
-
}
|
|
186
|
-
tmp = Vector3.normalize(tmp);
|
|
187
|
-
return Quaternion.fromAxisAngle(tmp, Math.PI);
|
|
188
|
-
} else if (dot > 0.999999) {
|
|
189
|
-
return Quaternion.identity;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
tmp = Vector3.cross(fromNormalized, toNormalized);
|
|
193
|
-
const quat: Quaternion_t = [1 + dot, tmp[0], tmp[1], tmp[2]];
|
|
194
|
-
return Quaternion.normalize(quat);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// https://github.com/stackgl/gl-quat/blob/master/slerp.js
|
|
198
|
-
/**
|
|
199
|
-
* Performs a spherical linear interpolation between two quat
|
|
200
|
-
*
|
|
201
|
-
* @param {number[]} a the first operand
|
|
202
|
-
* @param {number[]} b the second operand
|
|
203
|
-
* @param {number} t interpolation amount between the two inputs
|
|
204
|
-
* @returns {number[]} out
|
|
205
|
-
*/
|
|
206
|
-
static slerp(a: Quaternion_t, b: Quaternion_t, t: number): Quaternion_t {
|
|
207
|
-
|
|
208
|
-
const ax = a[1], ay = a[2], az = a[3], aw = a[0];
|
|
209
|
-
let bx = b[1], by = b[2], bz = b[3], bw = b[0];
|
|
210
|
-
|
|
211
|
-
let cosom, scale0, scale1;
|
|
212
|
-
|
|
213
|
-
// calc cosine
|
|
214
|
-
cosom = ax * bx + ay * by + az * bz + aw * bw;
|
|
215
|
-
// adjust signs (if necessary)
|
|
216
|
-
if (cosom < 0.0) {
|
|
217
|
-
cosom = -cosom;
|
|
218
|
-
bw = -bw;
|
|
219
|
-
bx = -bx;
|
|
220
|
-
by = -by;
|
|
221
|
-
bz = -bz;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// calculate coefficients
|
|
225
|
-
const useLinearInterp = (1.0 - cosom) < 0.000001;
|
|
226
|
-
if (!useLinearInterp) {
|
|
227
|
-
// standard case (slerp)
|
|
228
|
-
const omega = Math.acos(cosom);
|
|
229
|
-
const sinom = Math.sin(omega);
|
|
230
|
-
scale0 = Math.sin((1.0 - t) * omega) / sinom;
|
|
231
|
-
scale1 = Math.sin(t * omega) / sinom;
|
|
232
|
-
} else {
|
|
233
|
-
// "from" and "to" quaternions are very close
|
|
234
|
-
// ... so we can do a linear interpolation
|
|
235
|
-
scale0 = 1.0 - t;
|
|
236
|
-
scale1 = t;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// calculate final values
|
|
240
|
-
let output: Quaternion_t = [
|
|
241
|
-
scale0 * aw + scale1 * bw,
|
|
242
|
-
scale0 * ax + scale1 * bx,
|
|
243
|
-
scale0 * ay + scale1 * by,
|
|
244
|
-
scale0 * az + scale1 * bz
|
|
245
|
-
];
|
|
246
|
-
|
|
247
|
-
if (useLinearInterp) {
|
|
248
|
-
output = Quaternion.normalize(output);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return output;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
static toString(q: Quaternion_t): string {
|
|
255
|
-
return `[${q[0].toFixed(2)}, ${q[1].toFixed(2)}, ${q[2].toFixed(2)}, ${q[3].toFixed(2)}]`;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
static fromUnitVectors(vFrom: Vector3_t, vTo: Vector3_t): Quaternion_t {
|
|
259
|
-
// assumes direction vectors vFrom and vTo are normalized
|
|
260
|
-
let r = Vector3.dot(vFrom, vTo) + 1;
|
|
261
|
-
|
|
262
|
-
let x, y, z, w;
|
|
263
|
-
|
|
264
|
-
if (r < Number.EPSILON) {
|
|
265
|
-
// vFrom and vTo point in opposite directions
|
|
266
|
-
r = 0;
|
|
267
|
-
|
|
268
|
-
if (Math.abs(vFrom[1]) > Math.abs(vFrom[2])) {
|
|
269
|
-
x = -vFrom[1];
|
|
270
|
-
y = vFrom[0];
|
|
271
|
-
z = 0;
|
|
272
|
-
w = r;
|
|
273
|
-
} else {
|
|
274
|
-
x = 0;
|
|
275
|
-
y = -vFrom[2];
|
|
276
|
-
z = vFrom[1];
|
|
277
|
-
w = r;
|
|
278
|
-
}
|
|
279
|
-
} else {
|
|
280
|
-
// Cross product of vFrom and vTo
|
|
281
|
-
x = vFrom[1] * vTo[2] - vFrom[2] * vTo[1];
|
|
282
|
-
y = vFrom[2] * vTo[0] - vFrom[0] * vTo[2];
|
|
283
|
-
z = vFrom[0] * vTo[1] - vFrom[1] * vTo[0];
|
|
284
|
-
w = r;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const quat: Quaternion_t = [w, x, y, z];
|
|
288
|
-
return Quaternion.normalize(quat);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
export default Quaternion;
|
package/src/Rotations.spec.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import chai from 'chai';
|
|
2
|
-
import chaiAlmost from 'chai-almost';
|
|
3
|
-
|
|
4
|
-
import Rotations from './Rotations.js';
|
|
5
|
-
import { EulerAnglesDegrees_t, EulerAngles_t, Quaternion_t } from './types.js';
|
|
6
|
-
|
|
7
|
-
const { expect } = chai;
|
|
8
|
-
chai.use(chaiAlmost());
|
|
9
|
-
|
|
10
|
-
const q1: Quaternion_t = [0.182574185835055, 0.365148371670111, 0.547722557505166, 0.730296743340221];
|
|
11
|
-
|
|
12
|
-
const e1: EulerAngles_t = [-2.761086276477428, 1.203588306237060, -1.190289949682532];
|
|
13
|
-
|
|
14
|
-
const E1: EulerAnglesDegrees_t = [-158.1985905136482, 68.960530218682791, -68.198590513648199];
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
describe('Rotations', () => {
|
|
18
|
-
|
|
19
|
-
it('quaternionToEulerZXY', () => {
|
|
20
|
-
const result = Rotations.quaternionToEulerZXY(q1);
|
|
21
|
-
expect(result).deep.almost(e1);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('quaternionToEuler', () => {
|
|
25
|
-
expect(Rotations.quaternionToEuler(q1, 'zyx')).deep.almost([2.356194490192345, -0.3398369094541224, 1.4288992721907334]);
|
|
26
|
-
expect(Rotations.quaternionToEuler(q1, 'zyz')).deep.almost([0.7378150601204648, 1.4370647373849559, 1.9138202672156008]);
|
|
27
|
-
expect(Rotations.quaternionToEuler(q1, 'zxy')).deep.almost(e1);
|
|
28
|
-
expect(Rotations.quaternionToEuler(q1, 'zxz')).deep.almost([2.3086113869153615, 1.4370647373849559, 0.34302394042070417]);
|
|
29
|
-
expect(Rotations.quaternionToEuler(q1, 'yxz')).deep.almost([1.3909428270024193, -0.7297276562269656, 2.0344439357957027]);
|
|
30
|
-
expect(Rotations.quaternionToEuler(q1, 'yxy')).deep.almost([0.141897054604165, 1.9106332362490184, 2.356194490192345]);
|
|
31
|
-
expect(Rotations.quaternionToEuler(q1, 'yzx')).deep.almost([-2.677945044588986, 0.7297276562269656, -2.0344439357957027]);
|
|
32
|
-
expect(Rotations.quaternionToEuler(q1, 'yzy')).deep.almost([1.7126933813990617, 1.9106332362490184, 0.7853981633974483]);
|
|
33
|
-
expect(Rotations.quaternionToEuler(q1, 'xyz')).deep.almost([-1.3734007669450168, 0.8232119771258751, -2.944197093739911]);
|
|
34
|
-
expect(Rotations.quaternionToEuler(q1, 'xyx')).deep.almost([2.0344439357957036, 2.300523983021862, 0.17985349979247958]);
|
|
35
|
-
expect(Rotations.quaternionToEuler(q1, 'xzy')).deep.almost([1.9138202672156002, -0.13373158940994245, 2.308611386915361]);
|
|
36
|
-
expect(Rotations.quaternionToEuler(q1, 'xzx')).deep.almost([0.4636476090008071, 2.300523983021862, 1.7506498265873762]);
|
|
37
|
-
expect(() => Rotations.quaternionToEuler(q1, 'abc')).throw(Error);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('quaternionToEulerZXYDegrees', () => {
|
|
41
|
-
let result;
|
|
42
|
-
result = Rotations.quaternionToEulerZXYDegrees(q1);
|
|
43
|
-
expect(result).deep.almost(E1);
|
|
44
|
-
result = Rotations.quaternionToEulerZXYDegrees([Math.SQRT1_2, Math.SQRT2, 0, 0]);
|
|
45
|
-
expect(result).deep.almost([0, 90, 0]);
|
|
46
|
-
result = Rotations.quaternionToEulerZXYDegrees([-Math.SQRT1_2, Math.SQRT2, 0, 0]);
|
|
47
|
-
expect(result).deep.almost([0, -90, 0]);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('quaternionToEulerDegrees', () => {
|
|
51
|
-
const result = Rotations.quaternionToEulerDegrees(q1, 'zxy');
|
|
52
|
-
expect(result).deep.almost(E1);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('eulerToQuaternionZXY', () => {
|
|
56
|
-
let result;
|
|
57
|
-
result = Rotations.eulerToQuaternionZXY(e1);
|
|
58
|
-
expect(result).deep.almost(q1);
|
|
59
|
-
result = Rotations.eulerToQuaternionZXY([0, 0, Math.PI / 2]);
|
|
60
|
-
expect(result).deep.almost([Math.SQRT1_2, 0, Math.SQRT1_2, 0]);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('eulerToQuaternionZXYDegrees', () => {
|
|
64
|
-
const result = Rotations.eulerToQuaternionZXYDegrees(E1);
|
|
65
|
-
expect(result).deep.almost(q1);
|
|
66
|
-
});
|
|
67
|
-
});
|
package/src/Rotations.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import { EulerAnglesDegrees_t, EulerAngles_t, Quaternion_t, Vector3_t } from './types.js';
|
|
2
|
-
import { deg2rad, rad2deg } from './Utils.js';
|
|
3
|
-
|
|
4
|
-
class Rotations {
|
|
5
|
-
|
|
6
|
-
// ZXY because yaw is around Z axis, pitch around X axis and roll around Y axis.
|
|
7
|
-
// They are conventions for a smartphone frame.
|
|
8
|
-
static eulerToQuaternionZXYDegrees(eulerAnglesDegrees: EulerAnglesDegrees_t): Quaternion_t {
|
|
9
|
-
const eulerAngles = eulerAnglesDegrees.map(x => deg2rad(x)) as EulerAngles_t;
|
|
10
|
-
return Rotations.eulerToQuaternionZXY(eulerAngles);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
static eulerToQuaternionZXY(eulerAngles: EulerAngles_t): Quaternion_t {
|
|
14
|
-
const yaw = eulerAngles[0];
|
|
15
|
-
const pitch = eulerAngles[1];
|
|
16
|
-
const roll = eulerAngles[2];
|
|
17
|
-
|
|
18
|
-
const c1 = Math.cos(yaw / 2);
|
|
19
|
-
const c2 = Math.cos(pitch / 2);
|
|
20
|
-
const c3 = Math.cos(roll / 2);
|
|
21
|
-
const s1 = Math.sin(yaw / 2);
|
|
22
|
-
const s2 = Math.sin(pitch / 2);
|
|
23
|
-
const s3 = Math.sin(roll / 2);
|
|
24
|
-
|
|
25
|
-
let w = c1 * c2 * c3 - s1 * s2 * s3;
|
|
26
|
-
let x = c1 * s2 * c3 - s1 * c2 * s3;
|
|
27
|
-
let y = s1 * s2 * c3 + c1 * c2 * s3;
|
|
28
|
-
let z = s1 * c2 * c3 + c1 * s2 * s3;
|
|
29
|
-
|
|
30
|
-
// Let's quaternion has a positive scalar.
|
|
31
|
-
if (w < 0) {
|
|
32
|
-
w = -w;
|
|
33
|
-
x = -x;
|
|
34
|
-
y = -y;
|
|
35
|
-
z = -z;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return [w, x, y, z];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
static quaternionToEulerZXYDegrees(quaternion: Quaternion_t): EulerAnglesDegrees_t {
|
|
43
|
-
const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
|
|
44
|
-
return eulerAngles.map(x => rad2deg(x)) as EulerAnglesDegrees_t;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
static quaternionToEulerZXY(quaternion: Quaternion_t): EulerAngles_t {
|
|
48
|
-
|
|
49
|
-
const qw = quaternion[0];
|
|
50
|
-
const qx = quaternion[1];
|
|
51
|
-
const qy = quaternion[2];
|
|
52
|
-
const qz = quaternion[3];
|
|
53
|
-
|
|
54
|
-
let alpha, beta, gamma;
|
|
55
|
-
|
|
56
|
-
// From http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
|
|
57
|
-
const test = qy * qz + qx * qw;
|
|
58
|
-
if (test > 0.499) {
|
|
59
|
-
alpha = 2 * Math.atan2(qy, qw);
|
|
60
|
-
beta = Math.PI / 2;
|
|
61
|
-
gamma = 0;
|
|
62
|
-
} else if (test < - 0.499) {
|
|
63
|
-
alpha = -2 * Math.atan2(qy, qw);
|
|
64
|
-
beta = - Math.PI / 2;
|
|
65
|
-
gamma = 0;
|
|
66
|
-
} else {
|
|
67
|
-
alpha = Math.atan2(2 * qz * qw - 2 * qy * qx,
|
|
68
|
-
1 - 2 * Math.pow(qz, 2) - 2 * Math.pow(qx, 2));
|
|
69
|
-
beta = Math.asin(2 * test);
|
|
70
|
-
gamma = Math.atan2(2 * qy * qw - 2 * qz * qx,
|
|
71
|
-
1 - 2 * Math.pow(qy, 2) - 2 * Math.pow(qx, 2));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
alpha = alpha % (2 * Math.PI);
|
|
75
|
-
beta = beta % (Math.PI);
|
|
76
|
-
gamma = gamma % (2 * Math.PI);
|
|
77
|
-
|
|
78
|
-
return [alpha, beta, gamma];
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
static quaternionToEulerDegrees(quaternion: Quaternion_t, order: string): EulerAnglesDegrees_t {
|
|
82
|
-
const eulerAngles = Rotations.quaternionToEuler(quaternion, order);
|
|
83
|
-
return eulerAngles.map(x => rad2deg(x)) as EulerAnglesDegrees_t;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// https://searchcode.com/codesearch/view/55080466/
|
|
87
|
-
static quaternionToEuler(quaternion: Quaternion_t, order: string): EulerAngles_t {
|
|
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
|
-
private static _threeaxisrot(r11: number, r12: number, r21: number, r31: number, r32: number): Vector3_t {
|
|
183
|
-
const r1 = Math.atan2(r11, r12);
|
|
184
|
-
const r2 = Math.asin(r21);
|
|
185
|
-
const r3 = Math.atan2(r31, r32);
|
|
186
|
-
return [r1, r2, r3];
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
private static _twoaxisrot(r11: number, r12: number, r21: number, r31: number, r32: number): Vector3_t {
|
|
190
|
-
const r1 = Math.atan2(r11, r12);
|
|
191
|
-
const r2 = Math.acos(r21);
|
|
192
|
-
const r3 = Math.atan2(r31, r32);
|
|
193
|
-
return [r1, r2, r3];
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
static getHeadingFromQuaternion(quaternion: Quaternion_t): number {
|
|
198
|
-
// Minus before "yaw" angle is here because ENU attitude is counter-clockwise whereas Coordinates is clockwise.
|
|
199
|
-
|
|
200
|
-
const [qw, qx, qy, qz] = quaternion;
|
|
201
|
-
const qwqx = qw * qx;
|
|
202
|
-
const qyqz = qy * qz;
|
|
203
|
-
const qxqx = qx ** 2;
|
|
204
|
-
|
|
205
|
-
if (Math.asin(2 * (qwqx + qyqz)) < Math.PI / 4 && (qxqx + qy ** 2) < 0.5) {
|
|
206
|
-
// Map version
|
|
207
|
-
return - Math.atan2(qw * qz - qx * qy, 0.5 - qxqx - qz ** 2);
|
|
208
|
-
}
|
|
209
|
-
// AR version
|
|
210
|
-
return - Math.atan2(qx * qz + qw * qy, qwqx - qyqz);
|
|
211
|
-
|
|
212
|
-
// // ---- Not optimized version ----
|
|
213
|
-
// const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
|
|
214
|
-
// const eulerAnglesAR = Rotations.quaternionToEulerZXY(Quaternion.multiply(quaternion, Quaternion.fromAxisAngle([1, 0, 0], - Math.PI / 2)));
|
|
215
|
-
|
|
216
|
-
// if (eulerAngles[1] < Math.PI / 4 && eulerAnglesAR[1] < 0) {
|
|
217
|
-
// return -eulerAngles[0];
|
|
218
|
-
// }
|
|
219
|
-
// return -eulerAnglesAR[0];
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export default Rotations;
|
package/src/Utils.spec.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import chai from 'chai';
|
|
2
|
-
import chaiAlmost from 'chai-almost';
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
deg2rad,
|
|
6
|
-
rad2deg,
|
|
7
|
-
diffAngle,
|
|
8
|
-
diffAngleDegrees,
|
|
9
|
-
diffAngleLines,
|
|
10
|
-
positiveMod,
|
|
11
|
-
wrap
|
|
12
|
-
} from './Utils.js';
|
|
13
|
-
|
|
14
|
-
const { expect } = chai;
|
|
15
|
-
chai.use(chaiAlmost());
|
|
16
|
-
|
|
17
|
-
describe('MathUtils', () => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
it('deg2rad', () => {
|
|
21
|
-
expect(deg2rad(1)).to.equal(Math.PI / 180);
|
|
22
|
-
expect(deg2rad(90)).to.equal(Math.PI / 2);
|
|
23
|
-
expect(deg2rad(0)).to.equal(0);
|
|
24
|
-
expect(deg2rad(135)).to.equal(3 * Math.PI / 4);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
it('rad2deg', () => {
|
|
29
|
-
expect(rad2deg(Math.PI / 180)).to.equal(1);
|
|
30
|
-
expect(rad2deg(Math.PI / 2)).to.equal(90);
|
|
31
|
-
expect(rad2deg(0)).to.equal(0);
|
|
32
|
-
expect(rad2deg(3 * Math.PI / 4)).to.equal(135);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
it('diffAngle', () => {
|
|
37
|
-
expect(diffAngle(0, 0)).to.equal(0);
|
|
38
|
-
expect(diffAngle(Math.PI, 0)).to.equal(Math.PI);
|
|
39
|
-
expect(diffAngle(3 * Math.PI, 0)).to.almost.equal(Math.PI);
|
|
40
|
-
expect(diffAngle(Math.PI, Math.PI / 2)).to.almost.equal(Math.PI / 2);
|
|
41
|
-
expect(diffAngle(Math.PI / 2, Math.PI)).to.almost.equal(-Math.PI / 2);
|
|
42
|
-
expect(diffAngle(Math.PI / 2, 3 * Math.PI)).to.almost.equal(-Math.PI / 2);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
it('diffAngleDegrees', () => {
|
|
47
|
-
expect(diffAngleDegrees(0, 0)).to.equal(0);
|
|
48
|
-
expect(diffAngleDegrees(180, 0)).to.equal(180);
|
|
49
|
-
expect(diffAngleDegrees(540, 0)).to.almost.equal(180);
|
|
50
|
-
expect(diffAngleDegrees(180, 90)).to.almost.equal(90);
|
|
51
|
-
expect(diffAngleDegrees(90, 180)).to.almost.equal(-90);
|
|
52
|
-
expect(diffAngleDegrees(90, 540)).to.almost.equal(-90);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
it('diffAngleLines', () => {
|
|
57
|
-
expect(diffAngleLines(0, 0)).to.equal(0);
|
|
58
|
-
expect(diffAngleLines(Math.PI, 0)).to.equal(0);
|
|
59
|
-
expect(diffAngleLines(3 * Math.PI, 0)).to.almost.equal(0);
|
|
60
|
-
expect(diffAngleLines(3 * Math.PI / 4, 0)).to.almost.equal(Math.PI / 4);
|
|
61
|
-
expect(diffAngleLines(Math.PI, Math.PI / 2)).to.almost.equal(Math.PI / 2);
|
|
62
|
-
expect(diffAngleLines(Math.PI / 2, Math.PI)).to.almost.equal(Math.PI / 2);
|
|
63
|
-
expect(diffAngleLines(Math.PI / 2, 3 * Math.PI)).to.almost.equal(Math.PI / 2);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
it('positiveMod', () => {
|
|
68
|
-
expect(positiveMod(6, 4)).to.equal(2);
|
|
69
|
-
expect(positiveMod(-6, 4)).to.equal(2);
|
|
70
|
-
expect(positiveMod(-1, 4)).to.equal(3);
|
|
71
|
-
expect(positiveMod(-12, 4)).to.equal(0);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
it('wrap', () => {
|
|
76
|
-
expect(wrap(5, 0, 10)).to.equal(5);
|
|
77
|
-
expect(wrap(24, 0, 10)).to.equal(4);
|
|
78
|
-
expect(wrap(-4, 0, 10)).to.equal(6);
|
|
79
|
-
expect(wrap(0, 0, 10)).to.equal(10);
|
|
80
|
-
expect(wrap(10, 0, 10)).to.equal(10);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
});
|