@wemap/maths 10.10.0 → 11.0.0-alpha.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/dist/index.js +817 -0
- package/dist/index.js.map +1 -0
- package/{index.js → index.ts} +2 -0
- package/package.json +8 -5
- package/src/EulerAngles.ts +10 -0
- package/src/{Geometry.js → Geometry.ts} +10 -14
- package/src/{Matrix.js → Matrix.ts} +17 -12
- package/src/{Matrix3.spec.js → Matrix3.spec.ts} +5 -7
- package/src/{Matrix3.js → Matrix3.ts} +26 -23
- package/src/{Matrix4.spec.js → Matrix4.spec.ts} +20 -46
- package/src/{Matrix4.js → Matrix4.ts} +34 -34
- package/src/{Point2.js → Point2.ts} +4 -2
- package/src/{Polygon.spec.js → Polygon.spec.ts} +3 -2
- package/src/{Polygon.js → Polygon.ts} +4 -3
- package/src/{Quaternion.spec.js → Quaternion.spec.ts} +20 -57
- package/src/{Quaternion.js → Quaternion.ts} +32 -31
- package/src/{Rotations.spec.js → Rotations.spec.ts} +4 -8
- package/src/{Rotations.js → Rotations.ts} +14 -16
- package/src/{Utils.spec.js → Utils.spec.ts} +2 -0
- package/src/{Utils.js → Utils.ts} +10 -23
- package/src/{Vector.spec.js → Vector.spec.ts} +1 -0
- package/src/Vector.ts +10 -0
- package/src/{Vector3.spec.js → Vector3.spec.ts} +4 -2
- package/src/{Vector3.js → Vector3.ts} +11 -10
- package/src/types.ts +21 -0
- package/tsconfig.json +3 -0
- package/vite.config.ts +4 -0
- package/dist/wemap-maths.es.js +0 -1100
- package/dist/wemap-maths.es.js.map +0 -1
- package/index.d.ts +0 -177
- package/src/EulerAngles.js +0 -8
- package/src/Vector.js +0 -8
- /package/src/{Matrix.spec.js → Matrix.spec.ts} +0 -0
- /package/src/{Point2.spec.js → Point2.spec.ts} +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Matrix3_t, Quaternion_t, Vector3_t } from './types.js';
|
|
1
2
|
import Vector3 from './Vector3.js';
|
|
2
3
|
|
|
3
4
|
class Quaternion {
|
|
@@ -14,16 +15,16 @@ class Quaternion {
|
|
|
14
15
|
static ROTZ_PI = this.rotZ(Math.PI);
|
|
15
16
|
static ROTZ_MPI2 = this.rotZ(-Math.PI / 2);
|
|
16
17
|
|
|
17
|
-
static get identity() {
|
|
18
|
+
static get identity(): Quaternion_t {
|
|
18
19
|
return [1, 0, 0, 0];
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
static sum(q1, q2) {
|
|
22
|
+
static sum(q1: Quaternion_t, q2: Quaternion_t): Quaternion_t {
|
|
22
23
|
return [q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]];
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
// From https://www.mathworks.com/help/aerotbx/ug/quatrotate.html
|
|
26
|
-
static rotateMatlab(q, v) {
|
|
27
|
+
static rotateMatlab(q: Quaternion_t, v: Vector3_t): Vector3_t {
|
|
27
28
|
const [qw, qx, qy, qz] = q;
|
|
28
29
|
const [vx, vy, vz] = v;
|
|
29
30
|
return [
|
|
@@ -34,9 +35,9 @@ class Quaternion {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
// From: https://eigen.tuxfamily.org/dox/Quaternion_8h_source.html
|
|
37
|
-
static rotate(q, v) {
|
|
38
|
+
static rotate(q: Quaternion_t, v: Vector3_t): Vector3_t {
|
|
38
39
|
const [qw, qx, qy, qz] = q;
|
|
39
|
-
const thisVec = [qx, qy, qz];
|
|
40
|
+
const thisVec: Vector3_t = [qx, qy, qz];
|
|
40
41
|
let uv = Vector3.cross(thisVec, v);
|
|
41
42
|
uv = Vector3.sum(uv, uv);
|
|
42
43
|
const tmp1 = Vector3.sum(v, Vector3.multiplyScalar(uv, qw));
|
|
@@ -47,7 +48,7 @@ class Quaternion {
|
|
|
47
48
|
* Unit Quaternion inverse
|
|
48
49
|
* @param {Array.<Number>} quaternion must be a unit quaternion.
|
|
49
50
|
*/
|
|
50
|
-
static inverse(q) {
|
|
51
|
+
static inverse(q: Quaternion_t): Quaternion_t {
|
|
51
52
|
return [-q[0], q[1], q[2], q[3]];
|
|
52
53
|
}
|
|
53
54
|
|
|
@@ -56,7 +57,7 @@ class Quaternion {
|
|
|
56
57
|
* @param {...Number[]} quaternions
|
|
57
58
|
* @returns {Number[]}
|
|
58
59
|
*/
|
|
59
|
-
static multiply(...quaternions) {
|
|
60
|
+
static multiply(...quaternions: Quaternion_t[]): Quaternion_t {
|
|
60
61
|
|
|
61
62
|
if (quaternions.length === 2) {
|
|
62
63
|
|
|
@@ -79,7 +80,7 @@ class Quaternion {
|
|
|
79
80
|
* @param {Array[Number]} axis unit vector
|
|
80
81
|
* @param {Number} angle in radians
|
|
81
82
|
*/
|
|
82
|
-
static fromAxisAngle(axis, angle) {
|
|
83
|
+
static fromAxisAngle(axis: Vector3_t, angle: number): Quaternion_t {
|
|
83
84
|
const s = Math.sin(angle / 2);
|
|
84
85
|
return [Math.cos(angle / 2), axis[0] * s, axis[1] * s, axis[2] * s];
|
|
85
86
|
}
|
|
@@ -88,7 +89,7 @@ class Quaternion {
|
|
|
88
89
|
* Rotation around x-axis
|
|
89
90
|
* @param {Number} angle in radians
|
|
90
91
|
*/
|
|
91
|
-
static rotX(angle) {
|
|
92
|
+
static rotX(angle: number): Quaternion_t {
|
|
92
93
|
return this.fromAxisAngle([1, 0, 0], angle);
|
|
93
94
|
}
|
|
94
95
|
|
|
@@ -96,7 +97,7 @@ class Quaternion {
|
|
|
96
97
|
* Rotation around x-axis
|
|
97
98
|
* @param {Number} angle in radians
|
|
98
99
|
*/
|
|
99
|
-
static rotY(angle) {
|
|
100
|
+
static rotY(angle: number): Quaternion_t {
|
|
100
101
|
return this.fromAxisAngle([0, 1, 0], angle);
|
|
101
102
|
}
|
|
102
103
|
|
|
@@ -104,11 +105,11 @@ class Quaternion {
|
|
|
104
105
|
* Rotation around x-axis
|
|
105
106
|
* @param {Number} angle in radians
|
|
106
107
|
*/
|
|
107
|
-
static rotZ(angle) {
|
|
108
|
+
static rotZ(angle: number): Quaternion_t {
|
|
108
109
|
return this.fromAxisAngle([0, 0, 1], angle);
|
|
109
110
|
}
|
|
110
111
|
|
|
111
|
-
static fromMatrix3(m) {
|
|
112
|
+
static fromMatrix3(m: Matrix3_t): Quaternion_t {
|
|
112
113
|
const qw = Math.sqrt(1 + m[0][0] + m[1][1] + m[2][2]) / 2;
|
|
113
114
|
const qx = (m[2][1] - m[1][2]) / (4 * qw);
|
|
114
115
|
const qy = (m[0][2] - m[2][0]) / (4 * qw);
|
|
@@ -117,7 +118,7 @@ class Quaternion {
|
|
|
117
118
|
}
|
|
118
119
|
|
|
119
120
|
|
|
120
|
-
static fromMatrix3Matlab(m) {
|
|
121
|
+
static fromMatrix3Matlab(m: Matrix3_t): Quaternion_t {
|
|
121
122
|
const qw = Math.sqrt(1 + m[0][0] + m[1][1] + m[2][2]) / 2;
|
|
122
123
|
const qx = (m[1][2] - m[2][1]) / (4 * qw);
|
|
123
124
|
const qy = (m[2][0] - m[0][2]) / (4 * qw);
|
|
@@ -125,7 +126,7 @@ class Quaternion {
|
|
|
125
126
|
return [qw, qx, qy, qz];
|
|
126
127
|
}
|
|
127
128
|
|
|
128
|
-
static toMatrix3(q) {
|
|
129
|
+
static toMatrix3(q: Quaternion_t): Matrix3_t {
|
|
129
130
|
const [w, x, y, z] = q;
|
|
130
131
|
|
|
131
132
|
const wx = w * x, wy = w * y, wz = w * z;
|
|
@@ -139,57 +140,57 @@ class Quaternion {
|
|
|
139
140
|
|
|
140
141
|
}
|
|
141
142
|
|
|
142
|
-
static wxyz2xyzw(wxyz) {
|
|
143
|
+
static wxyz2xyzw(wxyz: Quaternion_t): Quaternion_t {
|
|
143
144
|
return [wxyz[1], wxyz[2], wxyz[3], wxyz[0]];
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
static xyzw2wxyz(xyzw) {
|
|
147
|
+
static xyzw2wxyz(xyzw: Quaternion_t): Quaternion_t {
|
|
147
148
|
return [xyzw[3], xyzw[0], xyzw[1], xyzw[2]];
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
static distance(q1, q2) {
|
|
151
|
+
static distance(q1: Quaternion_t, q2: Quaternion_t): number {
|
|
151
152
|
return Math.acos(Math.min(2 * (Quaternion.dot(q1, q2) ** 2) - 1, 1));
|
|
152
153
|
}
|
|
153
154
|
|
|
154
|
-
static equals(q1, q2) {
|
|
155
|
+
static equals(q1: Quaternion_t, q2: Quaternion_t): boolean {
|
|
155
156
|
return Quaternion.distance(q1, q2) < 1e-8;
|
|
156
157
|
}
|
|
157
158
|
|
|
158
|
-
static normalize(quaternion) {
|
|
159
|
+
static normalize(quaternion: Quaternion_t): Quaternion_t {
|
|
159
160
|
const norm = this.norm(quaternion);
|
|
160
|
-
const output = new Array(4);
|
|
161
|
+
const output = new Array(4) as Quaternion_t;
|
|
161
162
|
for (let i = 0; i < 4; i++) {
|
|
162
163
|
output[i] = quaternion[i] / norm;
|
|
163
164
|
}
|
|
164
165
|
return output;
|
|
165
166
|
}
|
|
166
167
|
|
|
167
|
-
static norm(quaternion) {
|
|
168
|
+
static norm(quaternion: Quaternion_t): number {
|
|
168
169
|
return Math.sqrt(quaternion[0] ** 2 + quaternion[1] ** 2 + quaternion[2] ** 2 + quaternion[3] ** 2);
|
|
169
170
|
}
|
|
170
171
|
|
|
171
|
-
static dot(q1, q2) {
|
|
172
|
+
static dot(q1: Quaternion_t, q2: Quaternion_t) : number {
|
|
172
173
|
return q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3];
|
|
173
174
|
}
|
|
174
175
|
|
|
175
|
-
static getRotationBetweenTwoVectors(from, to) {
|
|
176
|
+
static getRotationBetweenTwoVectors(from: Vector3_t, to: Vector3_t): Quaternion_t {
|
|
176
177
|
const fromNormalized = Vector3.normalize(from);
|
|
177
178
|
const toNormalized = Vector3.normalize(to);
|
|
178
179
|
const dot = Vector3.dot(fromNormalized, toNormalized);
|
|
179
|
-
let tmp;
|
|
180
|
+
let tmp: Vector3_t;
|
|
180
181
|
if (dot < -0.999999) {
|
|
181
182
|
tmp = Vector3.cross([1, 0, 0], fromNormalized);
|
|
182
183
|
if (Vector3.norm(tmp) < 0.000001) {
|
|
183
184
|
tmp = Vector3.cross([0, 1, 0], fromNormalized);
|
|
184
185
|
}
|
|
185
|
-
Vector3.normalize(tmp
|
|
186
|
+
tmp = Vector3.normalize(tmp);
|
|
186
187
|
return Quaternion.fromAxisAngle(tmp, Math.PI);
|
|
187
188
|
} else if (dot > 0.999999) {
|
|
188
189
|
return Quaternion.identity;
|
|
189
190
|
}
|
|
190
191
|
|
|
191
192
|
tmp = Vector3.cross(fromNormalized, toNormalized);
|
|
192
|
-
const quat = [1 + dot, tmp[0], tmp[1], tmp[2]];
|
|
193
|
+
const quat: Quaternion_t = [1 + dot, tmp[0], tmp[1], tmp[2]];
|
|
193
194
|
return Quaternion.normalize(quat);
|
|
194
195
|
}
|
|
195
196
|
|
|
@@ -202,10 +203,10 @@ class Quaternion {
|
|
|
202
203
|
* @param {number} t interpolation amount between the two inputs
|
|
203
204
|
* @returns {number[]} out
|
|
204
205
|
*/
|
|
205
|
-
static slerp(a, b, t) {
|
|
206
|
+
static slerp(a: Quaternion_t, b: Quaternion_t, t: number): Quaternion_t {
|
|
206
207
|
|
|
207
|
-
|
|
208
|
-
|
|
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];
|
|
209
210
|
|
|
210
211
|
let cosom, scale0, scale1;
|
|
211
212
|
|
|
@@ -236,7 +237,7 @@ class Quaternion {
|
|
|
236
237
|
}
|
|
237
238
|
|
|
238
239
|
// calculate final values
|
|
239
|
-
let output = [
|
|
240
|
+
let output: Quaternion_t = [
|
|
240
241
|
scale0 * aw + scale1 * bw,
|
|
241
242
|
scale0 * ax + scale1 * bx,
|
|
242
243
|
scale0 * ay + scale1 * by,
|
|
@@ -250,7 +251,7 @@ class Quaternion {
|
|
|
250
251
|
return output;
|
|
251
252
|
}
|
|
252
253
|
|
|
253
|
-
static toString(q) {
|
|
254
|
+
static toString(q: Quaternion_t): string {
|
|
254
255
|
return `[${q[0].toFixed(2)}, ${q[1].toFixed(2)}, ${q[2].toFixed(2)}, ${q[3].toFixed(2)}]`;
|
|
255
256
|
}
|
|
256
257
|
|
|
@@ -2,15 +2,16 @@ import chai from 'chai';
|
|
|
2
2
|
import chaiAlmost from 'chai-almost';
|
|
3
3
|
|
|
4
4
|
import Rotations from './Rotations.js';
|
|
5
|
+
import { EulerAnglesDegrees_t, EulerAngles_t, Quaternion_t } from './types.js';
|
|
5
6
|
|
|
6
7
|
const { expect } = chai;
|
|
7
8
|
chai.use(chaiAlmost());
|
|
8
9
|
|
|
9
|
-
const q1 = [0.182574185835055, 0.365148371670111, 0.547722557505166, 0.730296743340221];
|
|
10
|
+
const q1: Quaternion_t = [0.182574185835055, 0.365148371670111, 0.547722557505166, 0.730296743340221];
|
|
10
11
|
|
|
11
|
-
const e1 = [-2.761086276477428, 1.203588306237060, -1.190289949682532];
|
|
12
|
+
const e1: EulerAngles_t = [-2.761086276477428, 1.203588306237060, -1.190289949682532];
|
|
12
13
|
|
|
13
|
-
const E1 = [-158.1985905136482, 68.960530218682791, -68.198590513648199];
|
|
14
|
+
const E1: EulerAnglesDegrees_t = [-158.1985905136482, 68.960530218682791, -68.198590513648199];
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
describe('Rotations', () => {
|
|
@@ -38,13 +39,10 @@ describe('Rotations', () => {
|
|
|
38
39
|
|
|
39
40
|
it('quaternionToEulerZXYDegrees', () => {
|
|
40
41
|
let result;
|
|
41
|
-
|
|
42
42
|
result = Rotations.quaternionToEulerZXYDegrees(q1);
|
|
43
43
|
expect(result).deep.almost(E1);
|
|
44
|
-
|
|
45
44
|
result = Rotations.quaternionToEulerZXYDegrees([Math.SQRT1_2, Math.SQRT2, 0, 0]);
|
|
46
45
|
expect(result).deep.almost([0, 90, 0]);
|
|
47
|
-
|
|
48
46
|
result = Rotations.quaternionToEulerZXYDegrees([-Math.SQRT1_2, Math.SQRT2, 0, 0]);
|
|
49
47
|
expect(result).deep.almost([0, -90, 0]);
|
|
50
48
|
});
|
|
@@ -56,10 +54,8 @@ describe('Rotations', () => {
|
|
|
56
54
|
|
|
57
55
|
it('eulerToQuaternionZXY', () => {
|
|
58
56
|
let result;
|
|
59
|
-
|
|
60
57
|
result = Rotations.eulerToQuaternionZXY(e1);
|
|
61
58
|
expect(result).deep.almost(q1);
|
|
62
|
-
|
|
63
59
|
result = Rotations.eulerToQuaternionZXY([0, 0, Math.PI / 2]);
|
|
64
60
|
expect(result).deep.almost([Math.SQRT1_2, 0, Math.SQRT1_2, 0]);
|
|
65
61
|
});
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
import { EulerAnglesDegrees_t, EulerAngles_t, Quaternion_t, Vector3_t } from './types.js';
|
|
1
2
|
import { deg2rad, rad2deg } from './Utils.js';
|
|
2
3
|
|
|
3
4
|
class Rotations {
|
|
4
5
|
|
|
5
6
|
// ZXY because yaw is around Z axis, pitch around X axis and roll around Y axis.
|
|
6
7
|
// They are conventions for a smartphone frame.
|
|
7
|
-
static eulerToQuaternionZXYDegrees(eulerAnglesDegrees) {
|
|
8
|
-
const eulerAngles = eulerAnglesDegrees.map(x => deg2rad(x));
|
|
8
|
+
static eulerToQuaternionZXYDegrees(eulerAnglesDegrees: EulerAnglesDegrees_t): Quaternion_t {
|
|
9
|
+
const eulerAngles = eulerAnglesDegrees.map(x => deg2rad(x)) as EulerAngles_t;
|
|
9
10
|
return Rotations.eulerToQuaternionZXY(eulerAngles);
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
static eulerToQuaternionZXY(eulerAngles) {
|
|
13
|
+
static eulerToQuaternionZXY(eulerAngles: EulerAngles_t): Quaternion_t {
|
|
13
14
|
const yaw = eulerAngles[0];
|
|
14
15
|
const pitch = eulerAngles[1];
|
|
15
16
|
const roll = eulerAngles[2];
|
|
@@ -34,17 +35,16 @@ class Rotations {
|
|
|
34
35
|
z = -z;
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
return quaternion;
|
|
38
|
+
return [w, x, y, z];
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
|
|
42
|
-
static quaternionToEulerZXYDegrees(quaternion) {
|
|
42
|
+
static quaternionToEulerZXYDegrees(quaternion: Quaternion_t): EulerAnglesDegrees_t {
|
|
43
43
|
const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
|
|
44
|
-
return eulerAngles.map(x => rad2deg(x));
|
|
44
|
+
return eulerAngles.map(x => rad2deg(x)) as EulerAnglesDegrees_t;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
static quaternionToEulerZXY(quaternion) {
|
|
47
|
+
static quaternionToEulerZXY(quaternion: Quaternion_t): EulerAngles_t {
|
|
48
48
|
|
|
49
49
|
const qw = quaternion[0];
|
|
50
50
|
const qx = quaternion[1];
|
|
@@ -78,13 +78,13 @@ class Rotations {
|
|
|
78
78
|
return [alpha, beta, gamma];
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
static quaternionToEulerDegrees(quaternion, order) {
|
|
81
|
+
static quaternionToEulerDegrees(quaternion: Quaternion_t, order: string): EulerAnglesDegrees_t {
|
|
82
82
|
const eulerAngles = Rotations.quaternionToEuler(quaternion, order);
|
|
83
|
-
return eulerAngles.map(x => rad2deg(x));
|
|
83
|
+
return eulerAngles.map(x => rad2deg(x)) as EulerAnglesDegrees_t;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
// https://searchcode.com/codesearch/view/55080466/
|
|
87
|
-
static quaternionToEuler(quaternion, order) {
|
|
87
|
+
static quaternionToEuler(quaternion: Quaternion_t, order: string): EulerAngles_t {
|
|
88
88
|
|
|
89
89
|
const qw = quaternion[0];
|
|
90
90
|
const qx = quaternion[1];
|
|
@@ -179,16 +179,14 @@ class Rotations {
|
|
|
179
179
|
throw new Error('unknown order');
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
-
|
|
183
|
-
static _threeaxisrot(r11, r12, r21, r31, r32) {
|
|
182
|
+
private static _threeaxisrot(r11: number, r12: number, r21: number, r31: number, r32: number): Vector3_t {
|
|
184
183
|
const r1 = Math.atan2(r11, r12);
|
|
185
184
|
const r2 = Math.asin(r21);
|
|
186
185
|
const r3 = Math.atan2(r31, r32);
|
|
187
186
|
return [r1, r2, r3];
|
|
188
187
|
}
|
|
189
188
|
|
|
190
|
-
|
|
191
|
-
static _twoaxisrot(r11, r12, r21, r31, r32) {
|
|
189
|
+
private static _twoaxisrot(r11: number, r12: number, r21: number, r31: number, r32: number): Vector3_t {
|
|
192
190
|
const r1 = Math.atan2(r11, r12);
|
|
193
191
|
const r2 = Math.acos(r21);
|
|
194
192
|
const r3 = Math.atan2(r31, r32);
|
|
@@ -196,7 +194,7 @@ class Rotations {
|
|
|
196
194
|
}
|
|
197
195
|
|
|
198
196
|
|
|
199
|
-
static getHeadingFromQuaternion(quaternion) {
|
|
197
|
+
static getHeadingFromQuaternion(quaternion: Quaternion_t): number {
|
|
200
198
|
// Minus before "yaw" angle is here because ENU attitude is counter-clockwise whereas Coordinates is clockwise.
|
|
201
199
|
|
|
202
200
|
const [qw, qx, qy, qz] = quaternion;
|
|
@@ -16,6 +16,7 @@ chai.use(chaiAlmost());
|
|
|
16
16
|
|
|
17
17
|
describe('MathUtils', () => {
|
|
18
18
|
|
|
19
|
+
|
|
19
20
|
it('deg2rad', () => {
|
|
20
21
|
expect(deg2rad(1)).to.equal(Math.PI / 180);
|
|
21
22
|
expect(deg2rad(90)).to.equal(Math.PI / 2);
|
|
@@ -41,6 +42,7 @@ describe('MathUtils', () => {
|
|
|
41
42
|
expect(diffAngle(Math.PI / 2, 3 * Math.PI)).to.almost.equal(-Math.PI / 2);
|
|
42
43
|
});
|
|
43
44
|
|
|
45
|
+
|
|
44
46
|
it('diffAngleDegrees', () => {
|
|
45
47
|
expect(diffAngleDegrees(0, 0)).to.equal(0);
|
|
46
48
|
expect(diffAngleDegrees(180, 0)).to.equal(180);
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @param {Number} angle
|
|
6
6
|
* @return {Number}
|
|
7
7
|
*/
|
|
8
|
-
export function deg2rad(angle) {
|
|
8
|
+
export function deg2rad(angle: number): number {
|
|
9
9
|
return angle * (Math.PI / 180);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export function rad2deg(rad) {
|
|
12
|
+
export function rad2deg(rad: number): number {
|
|
13
13
|
return rad * 180 / Math.PI;
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -18,7 +18,7 @@ export function rad2deg(rad) {
|
|
|
18
18
|
* @param {Number} angle1 in radians
|
|
19
19
|
* @param {Number} angle2 in radians
|
|
20
20
|
*/
|
|
21
|
-
export function diffAngle(angle1, angle2) {
|
|
21
|
+
export function diffAngle(angle1: number, angle2: number): number {
|
|
22
22
|
return Math.atan2(Math.sin(angle1 - angle2), Math.cos(angle1 - angle2));
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -27,7 +27,7 @@ export function diffAngle(angle1, angle2) {
|
|
|
27
27
|
* @param {Number} angle1 in degrees
|
|
28
28
|
* @param {Number} angle2 in degrees
|
|
29
29
|
*/
|
|
30
|
-
export function diffAngleDegrees(angle1, angle2) {
|
|
30
|
+
export function diffAngleDegrees(angle1: number, angle2: number): number {
|
|
31
31
|
return rad2deg(diffAngle(deg2rad(angle1), deg2rad(angle2)));
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -36,7 +36,7 @@ export function diffAngleDegrees(angle1, angle2) {
|
|
|
36
36
|
* @param {Number} angle1 line1 angle in radians
|
|
37
37
|
* @param {Number} angle2 line2 angle in radians
|
|
38
38
|
*/
|
|
39
|
-
export function diffAngleLines(angle1, angle2) {
|
|
39
|
+
export function diffAngleLines(angle1: number, angle2: number): number {
|
|
40
40
|
let _diffAngle = Math.abs(diffAngle(angle1, angle2));
|
|
41
41
|
if (_diffAngle > Math.PI / 2) {
|
|
42
42
|
_diffAngle = Math.PI - _diffAngle;
|
|
@@ -44,7 +44,7 @@ export function diffAngleLines(angle1, angle2) {
|
|
|
44
44
|
return _diffAngle;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export function positiveMod(n, m) {
|
|
47
|
+
export function positiveMod(n: number, m: number): number {
|
|
48
48
|
return ((n % m) + m) % m;
|
|
49
49
|
}
|
|
50
50
|
|
|
@@ -56,35 +56,22 @@ export function positiveMod(n, m) {
|
|
|
56
56
|
* @param {number} max the maximum value to be returned, inclusive
|
|
57
57
|
* @returns {number} constrained number
|
|
58
58
|
*/
|
|
59
|
-
export function wrap(n, min, max) {
|
|
59
|
+
export function wrap(n: number, min: number, max: number): number {
|
|
60
60
|
const d = max - min;
|
|
61
61
|
const w = ((n - min) % d + d) % d + min;
|
|
62
62
|
return (w === min) ? max : w;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
* @param {number[]} arr
|
|
67
|
-
* @returns {number}
|
|
68
|
-
*/
|
|
69
|
-
export function average(arr) {
|
|
65
|
+
export function average(arr: number[]): number {
|
|
70
66
|
return arr.reduce((a, b) => a + b) / arr.length;
|
|
71
67
|
}
|
|
72
68
|
|
|
73
|
-
|
|
74
|
-
* @param {number[]} arr
|
|
75
|
-
* @returns {number}
|
|
76
|
-
*/
|
|
77
|
-
export function std(arr) {
|
|
69
|
+
export function std(arr: number[]): number {
|
|
78
70
|
const n = arr.length;
|
|
79
71
|
const mean = arr.reduce((a, b) => a + b) / n;
|
|
80
72
|
return Math.sqrt(arr.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n);
|
|
81
73
|
}
|
|
82
74
|
|
|
83
|
-
|
|
84
|
-
* @param {number} num
|
|
85
|
-
* @param {number} factor
|
|
86
|
-
* @returns {number}
|
|
87
|
-
*/
|
|
88
|
-
export function roundFactor(num, factor) {
|
|
75
|
+
export function roundFactor(num: number, factor: number): number {
|
|
89
76
|
return Math.round(num / factor) * factor;
|
|
90
77
|
}
|
package/src/Vector.ts
ADDED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import chai from 'chai';
|
|
2
2
|
|
|
3
3
|
import Vector3 from './Vector3.js';
|
|
4
|
+
import { Vector3_t } from './types.js';
|
|
4
5
|
|
|
5
6
|
const { expect } = chai;
|
|
6
7
|
|
|
7
|
-
const v1 = [1, 2, 3];
|
|
8
|
-
const v2 = [4, 5, 6];
|
|
8
|
+
const v1: Vector3_t = [1, 2, 3];
|
|
9
|
+
const v2: Vector3_t = [4, 5, 6];
|
|
9
10
|
|
|
10
11
|
describe('Vector3', () => {
|
|
12
|
+
|
|
11
13
|
it('norm', () => {
|
|
12
14
|
const result = Vector3.norm(v1);
|
|
13
15
|
expect(result).equal(Math.sqrt(14));
|
|
@@ -1,44 +1,45 @@
|
|
|
1
|
+
import { Vector3_t } from './types.js';
|
|
1
2
|
import Vector from './Vector.js';
|
|
2
3
|
|
|
3
4
|
class Vector3 extends Vector {
|
|
4
5
|
|
|
5
|
-
static norm(vector) {
|
|
6
|
+
static norm(vector: Vector3_t): number {
|
|
6
7
|
return Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
static normalize(vector) {
|
|
10
|
+
static normalize(vector: Vector3_t): Vector3_t {
|
|
10
11
|
const norm = this.norm(vector);
|
|
11
|
-
const output = new Array(3);
|
|
12
|
+
const output = new Array(3) as Vector3_t;
|
|
12
13
|
for (let i = 0; i < 3; i++) {
|
|
13
14
|
output[i] = vector[i] / norm;
|
|
14
15
|
}
|
|
15
16
|
return output;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
static sum(v1, v2) {
|
|
19
|
+
static sum(v1: Vector3_t, v2: Vector3_t): Vector3_t {
|
|
19
20
|
return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
static subtract(v1, v2) {
|
|
23
|
+
static subtract(v1: Vector3_t, v2: Vector3_t): Vector3_t {
|
|
23
24
|
return [v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]];
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
static distance(v1, v2) {
|
|
27
|
+
static distance(v1: Vector3_t, v2: Vector3_t): number {
|
|
27
28
|
return Vector3.norm(Vector3.subtract(v1, v2));
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
static cross(v1, v2) {
|
|
31
|
+
static cross(v1: Vector3_t, v2: Vector3_t): Vector3_t {
|
|
31
32
|
return [v1[1] * v2[2] - v1[2] * v2[1],
|
|
32
33
|
v1[2] * v2[0] - v1[0] * v2[2],
|
|
33
34
|
v1[0] * v2[1] - v1[1] * v2[0]];
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
static dot(v1, v2) {
|
|
37
|
+
static dot(v1: Vector3_t, v2: Vector3_t): number {
|
|
37
38
|
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
static multiplyScalar(vector, scalar) {
|
|
41
|
-
const output = new Array(3);
|
|
41
|
+
static multiplyScalar(vector: Vector3_t, scalar: number): Vector3_t {
|
|
42
|
+
const output = new Array(3) as Vector3_t;
|
|
42
43
|
for (let i = 0; i < 3; i++) {
|
|
43
44
|
output[i] = vector[i] * scalar;
|
|
44
45
|
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type Point2_t = [number, number];
|
|
2
|
+
export type Line_t = [Point2_t, Point2_t];
|
|
3
|
+
export type Polygon_t = Point2_t[];
|
|
4
|
+
export type Quaternion_t = [number, number, number, number];
|
|
5
|
+
export type Vector3_t = [number, number, number];
|
|
6
|
+
export type Vector4_t = [number, number, number, number];
|
|
7
|
+
export type Vector16_t = [number, number, number, number,
|
|
8
|
+
number, number, number, number,
|
|
9
|
+
number, number, number, number,
|
|
10
|
+
number, number, number, number
|
|
11
|
+
];
|
|
12
|
+
export type Vector_t = number[];
|
|
13
|
+
export type Matrix3_t = [[number, number, number], [number, number, number], [number, number, number]];
|
|
14
|
+
export type Matrix4_t = [[number, number, number, number],
|
|
15
|
+
[number, number, number, number],
|
|
16
|
+
[number, number, number, number],
|
|
17
|
+
[number, number, number, number]];
|
|
18
|
+
|
|
19
|
+
export type Matrix_t = number[][];
|
|
20
|
+
export type EulerAngles_t = Vector3_t;
|
|
21
|
+
export type EulerAnglesDegrees_t = Vector3_t;
|
package/tsconfig.json
ADDED
package/vite.config.ts
ADDED