@wemap/maths 3.2.17 → 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 CHANGED
@@ -1,32 +1,11 @@
1
- import Matrix from './src/Matrix.js';
2
- import Matrix3 from './src/Matrix3.js';
3
- import Matrix4 from './src/Matrix4.js';
4
- import Point2 from './src/Point2.js';
5
- import Polygon from './src/Polygon.js';
6
- import Quaternion from './src/Quaternion.js';
7
- import Rotations from './src/Rotations.js';
8
- import Utils from './src/Utils.js';
9
- import Vector from './src/Vector.js';
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
- const {
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": "3.2.17",
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": "8b5e0144472bfaa8e07ac24cb227294c83a7541a"
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 < (xj - xi) * (y - yi) / (yj - yi) + xi);
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;
@@ -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 Utils from './Utils.js';
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 => Utils.deg2rad(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 => Utils.rad2deg(x));
44
+ return eulerAngles.map(x => rad2deg(x));
45
45
  }
46
46
 
47
47
  static quaternionToEulerZXY(quaternion) {
@@ -80,7 +80,7 @@ class Rotations {
80
80
 
81
81
  static quaternionToEulerDegrees(quaternion, order) {
82
82
  const eulerAngles = Rotations.quaternionToEuler(quaternion, order);
83
- return eulerAngles.map(x => Utils.rad2deg(x));
83
+ return eulerAngles.map(x => rad2deg(x));
84
84
  }
85
85
 
86
86
  // https://searchcode.com/codesearch/view/55080466/
@@ -21,8 +21,19 @@ describe('Rotations', () => {
21
21
  });
22
22
 
23
23
  it('quaternionToEuler', () => {
24
- const result = Rotations.quaternionToEuler(q1, 'zxy');
25
- expect(result).deep.almost(e1);
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);
26
37
  });
27
38
 
28
39
  it('quaternionToEulerZXYDegrees', () => {
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
- static rad2deg(rad) {
14
- return rad * 180 / Math.PI;
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
- * Difference between two angles
19
- * @param {Number} angle1 in radians
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
- static positiveMod(n, m) {
27
- return ((n % m) + m) % m;
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
- * 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;
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
- export default Utils;
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 MathUtils from './Utils.js';
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(MathUtils.deg2rad(1)).to.equal(Math.PI / 180);
13
- expect(MathUtils.deg2rad(90)).to.equal(Math.PI / 2);
14
- expect(MathUtils.deg2rad(0)).to.equal(0);
15
- expect(MathUtils.deg2rad(135)).to.equal(3 * Math.PI / 4);
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(MathUtils.rad2deg(Math.PI / 180)).to.equal(1);
21
- expect(MathUtils.rad2deg(Math.PI / 2)).to.equal(90);
22
- expect(MathUtils.rad2deg(0)).to.equal(0);
23
- expect(MathUtils.rad2deg(3 * Math.PI / 4)).to.equal(135);
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(MathUtils.diffAngle(0, 0)).to.equal(0);
29
- expect(MathUtils.diffAngle(Math.PI, 0)).to.equal(Math.PI);
30
- expect(MathUtils.diffAngle(3 * Math.PI, 0)).to.almost.equal(Math.PI);
31
- expect(MathUtils.diffAngle(Math.PI, Math.PI / 2)).to.almost.equal(Math.PI / 2);
32
- expect(MathUtils.diffAngle(Math.PI / 2, Math.PI)).to.almost.equal(-Math.PI / 2);
33
- expect(MathUtils.diffAngle(Math.PI / 2, 3 * Math.PI)).to.almost.equal(-Math.PI / 2);
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(MathUtils.positiveMod(6, 4)).to.equal(2);
39
- expect(MathUtils.positiveMod(-6, 4)).to.equal(2);
40
- expect(MathUtils.positiveMod(-1, 4)).to.equal(3);
41
- expect(MathUtils.positiveMod(-12, 4)).to.equal(0);
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
  });