@wemap/maths 0.2.1 → 1.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/package.json CHANGED
@@ -12,13 +12,13 @@
12
12
  "directory": "packages/maths"
13
13
  },
14
14
  "name": "@wemap/maths",
15
- "version": "0.2.1",
15
+ "version": "1.0.0",
16
16
  "bugs": {
17
17
  "url": "https://github.com/wemap/wemap-utils-js/issues"
18
18
  },
19
19
  "homepage": "https://github.com/wemap/wemap-utils-js#readme",
20
20
  "scripts": {
21
- "test": "mocha -r esm \\\"src/**/*.spec.js\\\""
21
+ "test": "mocha -r esm \"src/**/*.spec.js\""
22
22
  },
23
23
  "keywords": [
24
24
  "utils",
@@ -26,5 +26,5 @@
26
26
  "wemap"
27
27
  ],
28
28
  "license": "ISC",
29
- "gitHead": "7b84c9d55646761f75f19235a15be8a1b73b3dbf"
29
+ "gitHead": "cab48c01923850987968f072a8f7075a34b3cc32"
30
30
  }
@@ -23,18 +23,17 @@ const m5 = [[1, 2], [3, 4]];
23
23
 
24
24
  const v1 = [1, 3, 5, 7];
25
25
 
26
- describe('Matrix.add', () => {
27
- it('Should return the good value', () => {
26
+ describe('Matrix', () => {
27
+
28
+ it('add', () => {
28
29
  const result = Matrix.add(m5, m5);
29
30
  expect(result).deep.equal([
30
31
  [2, 4],
31
32
  [6, 8]
32
33
  ]);
33
34
  });
34
- });
35
35
 
36
- describe('Matrix.concatRow', () => {
37
- it('Should return the good value', () => {
36
+ it('concatRow', () => {
38
37
  const result = Matrix.concatRow(m1, m3);
39
38
  expect(result).deep.equal([
40
39
  [1, 2, 3, 4],
@@ -43,10 +42,8 @@ describe('Matrix.concatRow', () => {
43
42
  [3, 4, 5, 6]
44
43
  ]);
45
44
  });
46
- });
47
45
 
48
- describe('Matrix.concatLine', () => {
49
- it('Should return the good value', () => {
46
+ it('concatLine', () => {
50
47
  const result = Matrix.concatLine(m1, m4);
51
48
  expect(result).deep.equal([
52
49
  [1, 2, 3, 4, 5, 6],
@@ -54,10 +51,8 @@ describe('Matrix.concatLine', () => {
54
51
  [9, 0, 1, 2, 3, 4]
55
52
  ]);
56
53
  });
57
- });
58
54
 
59
- describe('Matrix.diag', () => {
60
- it('Should return the good value', () => {
55
+ it('diag', () => {
61
56
  const result = Matrix.diag([1, 2, 3, 4]);
62
57
  expect(result).deep.equal([
63
58
  [1, 0, 0, 0],
@@ -66,10 +61,8 @@ describe('Matrix.diag', () => {
66
61
  [0, 0, 0, 4]
67
62
  ]);
68
63
  });
69
- });
70
64
 
71
- describe('Matrix.transpose', () => {
72
- it('Should return the good value', () => {
65
+ it('transpose', () => {
73
66
  const result = Matrix.transpose(m1);
74
67
  expect(result).deep.equal([
75
68
  [1, 5, 9],
@@ -78,10 +71,8 @@ describe('Matrix.transpose', () => {
78
71
  [4, 8, 2]
79
72
  ]);
80
73
  });
81
- });
82
74
 
83
- describe('Matrix.multiply', () => {
84
- it('Should return the good value', () => {
75
+ it('multiply', () => {
85
76
  const result = Matrix.multiply(m1, m2);
86
77
  expect(result).deep.equal([
87
78
  [50, 60],
@@ -89,10 +80,8 @@ describe('Matrix.multiply', () => {
89
80
  [28, 40]
90
81
  ]);
91
82
  });
92
- });
93
83
 
94
- describe('Matrix.multiplyScalar', () => {
95
- it('Should return the good value', () => {
84
+ it('multiplyScalar', () => {
96
85
  const result = Matrix.multiplyScalar(m2, 3);
97
86
  expect(result).deep.equal([
98
87
  [3, 6],
@@ -101,17 +90,13 @@ describe('Matrix.multiplyScalar', () => {
101
90
  [21, 24]
102
91
  ]);
103
92
  });
104
- });
105
93
 
106
- describe('Matrix.multiplyVector', () => {
107
- it('Should return the good value', () => {
94
+ it('multiplyVector', () => {
108
95
  const result = Matrix.multiplyVector(m1, v1);
109
96
  expect(result).deep.equal([50, 114, 28]);
110
97
  });
111
- });
112
98
 
113
- describe('Matrix.inverse', () => {
114
- it('Should return the good value', () => {
99
+ it('inverse', () => {
115
100
  const result = Matrix.inverse([[1, 2], [3, 4]]);
116
101
  expect(result).deep.equal([
117
102
  [-2, 1],
@@ -20,8 +20,9 @@ const m2 = [
20
20
 
21
21
  const v1 = [1, 2, 3];
22
22
 
23
- describe('Matrix3.add', () => {
24
- it('Should return the good value', () => {
23
+ describe('Matrix3', () => {
24
+
25
+ it('add', () => {
25
26
  const result = Matrix3.add(m1, m1);
26
27
  expect(result).deep.equal([
27
28
  [2, 4, 6],
@@ -29,10 +30,8 @@ describe('Matrix3.add', () => {
29
30
  [14, 16, 18]
30
31
  ]);
31
32
  });
32
- });
33
33
 
34
- describe('Matrix3.transpose', () => {
35
- it('Should return the good value', () => {
34
+ it('transpose', () => {
36
35
  const result = Matrix3.transpose(m1);
37
36
  expect(result).deep.equal([
38
37
  [1, 4, 7],
@@ -40,10 +39,8 @@ describe('Matrix3.transpose', () => {
40
39
  [3, 6, 9]
41
40
  ]);
42
41
  });
43
- });
44
42
 
45
- describe('Matrix3.multiplyScalar', () => {
46
- it('Should return the good value', () => {
43
+ it('multiplyScalar', () => {
47
44
  const result = Matrix3.multiplyScalar(m1, 2);
48
45
  expect(result).deep.equal([
49
46
  [2, 4, 6],
@@ -51,17 +48,13 @@ describe('Matrix3.multiplyScalar', () => {
51
48
  [14, 16, 18]
52
49
  ]);
53
50
  });
54
- });
55
51
 
56
- describe('Matrix3.multiplyVector', () => {
57
- it('Should return the good value', () => {
52
+ it('multiplyVector', () => {
58
53
  const result = Matrix3.multiplyVector(m1, v1);
59
54
  expect(result).deep.equal([14, 32, 50]);
60
55
  });
61
- });
62
56
 
63
- describe('Matrix3.multiply', () => {
64
- it('Should return the good value', () => {
57
+ it('multiply', () => {
65
58
  const result = Matrix3.multiply(m1, m1);
66
59
  expect(result).deep.equal([
67
60
  [30, 36, 42],
@@ -69,10 +62,8 @@ describe('Matrix3.multiply', () => {
69
62
  [102, 126, 150]
70
63
  ]);
71
64
  });
72
- });
73
65
 
74
- describe('Matrix3.skew', () => {
75
- it('Should return the good value', () => {
66
+ it('skew', () => {
76
67
  const result = Matrix3.skew(v1);
77
68
  expect(result).deep.equal([
78
69
  [0, -3, 2],
@@ -80,10 +71,8 @@ describe('Matrix3.skew', () => {
80
71
  [-2, 1, 0]
81
72
  ]);
82
73
  });
83
- });
84
74
 
85
- describe('Matrix3.inverse', () => {
86
- it('Should return the good value', () => {
75
+ it('inverse', () => {
87
76
  const result = Matrix3.inverse(m2);
88
77
  expect(result).deep.almost([
89
78
  [-6, 18 / 5, 7 / 5],
@@ -0,0 +1,40 @@
1
+ import { expect } from 'chai';
2
+
3
+ import Matrix4 from './Matrix4';
4
+
5
+ const m1 = [
6
+ [1, 2, 3, 4],
7
+ [5, 6, 7, 8],
8
+ [9, 0, 1, 2],
9
+ [3, 4, 5, 6]
10
+ ];
11
+
12
+ const m2 = [
13
+ [1, 2, 3, 4],
14
+ [0, 1, 5, 2],
15
+ [5, 6, 0, 9],
16
+ [3, 1, 9, 3]
17
+ ];
18
+
19
+ describe('Matrix4', () => {
20
+
21
+ it('add', () => {
22
+ const result = Matrix4.add(m1, m2);
23
+ expect(result).deep.equal([
24
+ [2, 4, 6, 8],
25
+ [5, 7, 12, 10],
26
+ [14, 6, 1, 11],
27
+ [6, 5, 14, 9]
28
+ ]);
29
+ });
30
+
31
+ it('subtract', () => {
32
+ const result = Matrix4.subtract(m1, m2);
33
+ expect(result).deep.equal([
34
+ [0, 0, 0, 0],
35
+ [5, 5, 2, 6],
36
+ [4, -6, 1, -7],
37
+ [0, 3, -4, 3]
38
+ ]);
39
+ });
40
+ });
@@ -21,88 +21,69 @@ const r1 = [
21
21
 
22
22
  const angle = 1.5;
23
23
 
24
- describe('Quaternion.add', () => {
25
- it('Should return the good value', () => {
24
+ describe('Quaternion', () => {
25
+
26
+ it('add', () => {
26
27
  const result = Quaternion.add(q1, q2);
27
28
  expect(result).deep.almost(
28
29
  [0.561623207624507, 0.820007197817453, 1.078391188010398, 1.336775178203344]
29
30
  );
30
31
  });
31
- });
32
32
 
33
- describe('Quaternion.rotate', () => {
34
- it('Should return the good value', () => {
33
+ it('rotate', () => {
35
34
  const result = Quaternion.rotate(q1, v1);
36
35
  expect(result).deep.almost(
37
36
  [1.666666666666667, 2.266666666666667, 2.466666666666667]
38
37
  );
39
38
  });
40
- });
41
39
 
42
- describe('Quaternion.multiply', () => {
43
- it('Should return the good value', () => {
40
+ it('multiply', () => {
44
41
  const result = Quaternion.multiply(q1, q2);
45
42
  expect(result).deep.almost(
46
43
  [-0.830454798537400, 0.166090959707480, 0.415227399268700, 0.332181919414960]
47
44
  );
48
45
  });
49
- });
50
46
 
51
- describe('Quaternion.fromAxisAngle', () => {
52
- it('Should return the good value', () => {
47
+ it('fromAxisAngle', () => {
53
48
  const result = Quaternion.fromAxisAngle(v1Normalized, angle);
54
49
  expect(result).deep.almost(
55
50
  [0.731688868873821, 0.182175621539481, 0.364351243078963, 0.546526864618444]
56
51
  );
57
52
  });
58
- });
59
53
 
60
- describe('Quaternion.fromMatrix3', () => {
61
- it('Should return the good value', () => {
54
+ it('fromMatrix3', () => {
62
55
  const result = Quaternion.fromMatrix3(r1);
63
56
  expect(result).deep.almost(
64
57
  [0.925880170865619, -0.155924545256753, -0.159664427009622, -0.304861798413669]
65
58
  );
66
59
  });
67
- });
68
60
 
69
- describe('Quaternion.wxyz2xyzw', () => {
70
- it('Should return the good value', () => {
61
+ it('wxyz2xyzw', () => {
71
62
  const result = Quaternion.wxyz2xyzw(q1);
72
63
  expect(result).deep.almost([q1[1], q1[2], q1[3], q1[0]]);
73
64
  });
74
- });
75
65
 
76
- describe('Quaternion.xyzw2wxyz', () => {
77
- it('Should return the good value', () => {
66
+ it('xyzw2wxyz', () => {
78
67
  const result = Quaternion.xyzw2wxyz(q1);
79
68
  expect(result).deep.almost([q1[3], q1[0], q1[1], q1[2]]);
80
69
  });
81
- });
82
70
 
83
- describe('Quaternion.distance', () => {
84
- it('Should return the good value', () => {
71
+ it('distance', () => {
85
72
  const result = Quaternion.distance(q1, q2);
86
73
  expect(result).almost(0.500391840845023);
87
74
  });
88
- });
89
75
 
90
- describe('Quaternion.norm', () => {
91
- it('Should return the good value', () => {
76
+ it('norm', () => {
92
77
  const result = Quaternion.norm(q1);
93
78
  expect(result).almost(1);
94
79
  });
95
- });
96
80
 
97
- describe('Quaternion.normalize', () => {
98
- it('Should return the good value', () => {
81
+ it('normalize', () => {
99
82
  const result = Quaternion.normalize(q1);
100
83
  expect(result).deep.almost(q1);
101
84
  });
102
- });
103
85
 
104
- describe('Quaternion.dot', () => {
105
- it('Should return the good value', () => {
86
+ it('dot', () => {
106
87
  const result = Quaternion.dot(q1, q2);
107
88
  expect(result).almost(0.968863931626966);
108
89
  });
@@ -13,29 +13,24 @@ const e1 = [-2.761086276477428, 1.203588306237060, -1.190289949682532];
13
13
  const E1 = [-158.1985905136482, 68.960530218682791, -68.198590513648199];
14
14
 
15
15
 
16
- describe('Rotations.quaternionToEulerZXY', () => {
17
- it('Should return the good value', () => {
16
+ describe('Rotations', () => {
17
+
18
+ it('quaternionToEulerZXY', () => {
18
19
  const result = Rotations.quaternionToEulerZXY(q1);
19
20
  expect(result).deep.almost(e1);
20
21
  });
21
- });
22
22
 
23
- describe('Rotations.quaternionToEulerZXYDegrees', () => {
24
- it('Should return the good value', () => {
23
+ it('quaternionToEulerZXYDegrees', () => {
25
24
  const result = Rotations.quaternionToEulerZXYDegrees(q1);
26
25
  expect(result).deep.almost(E1);
27
26
  });
28
- });
29
27
 
30
- describe('Rotations.eulerToQuaternionZXY', () => {
31
- it('Should return the good value', () => {
28
+ it('eulerToQuaternionZXY', () => {
32
29
  const result = Rotations.eulerToQuaternionZXY(e1);
33
30
  expect(result).deep.almost(q1);
34
31
  });
35
- });
36
32
 
37
- describe('Rotations.eulerToQuaternionZXYDegrees', () => {
38
- it('Should return the good value', () => {
33
+ it('eulerToQuaternionZXYDegrees', () => {
39
34
  const result = Rotations.eulerToQuaternionZXYDegrees(E1);
40
35
  expect(result).deep.almost(q1);
41
36
  });
package/src/Utils.spec.js CHANGED
@@ -1,30 +1,43 @@
1
- import { expect } from 'chai';
1
+ import chai from 'chai';
2
+ import chaiAlmost from 'chai-almost';
3
+
2
4
  import MathUtils from './Utils';
3
5
 
4
- let result, expected;
6
+ const expect = chai.expect;
7
+ chai.use(chaiAlmost());
8
+
9
+ describe('MathUtils', () => {
5
10
 
6
- describe('deg2rad', () => {
11
+ 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);
16
+ });
7
17
 
8
- it('an angle in radian', () => {
9
- var factor = Math.pow(10, 7);
10
- expected = 0.0174533;
11
- result = MathUtils.deg2rad(1);
12
- result = (Math.round(result * factor) / factor);
13
- expect(result).to.equal(expected);
14
18
 
15
- expected = 1.5707963;
16
- result = MathUtils.deg2rad(90);
17
- result = (Math.round(result * factor) / factor);
18
- expect(result).to.equal(expected);
19
+ 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);
24
+ });
25
+
26
+
27
+ 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);
34
+ });
19
35
 
20
- expected = 0;
21
- result = MathUtils.deg2rad(0);
22
- result = (Math.round(result * factor) / factor);
23
- expect(result).to.equal(expected);
24
36
 
25
- expected = 2.3561945;
26
- result = MathUtils.deg2rad(135);
27
- result = (Math.round(result * factor) / factor);
28
- expect(result).to.equal(expected);
37
+ 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);
29
42
  });
30
43
  });
@@ -4,8 +4,8 @@ import Vector from './Vector';
4
4
 
5
5
  const v1 = [1, 2, 3, 4];
6
6
 
7
- describe('Vector.concat', () => {
8
- it('Should return the good value', () => {
7
+ describe('Vector', () => {
8
+ it('concat', () => {
9
9
  const result = Vector.concat(v1, v1);
10
10
  expect(result).deep.equal([1, 2, 3, 4, 1, 2, 3, 4]);
11
11
  });
@@ -6,57 +6,43 @@ import Vector3 from './Vector3';
6
6
  const v1 = [1, 2, 3];
7
7
  const v2 = [4, 5, 6];
8
8
 
9
- describe('Vector3.norm', () => {
10
- it('Should return the good value', () => {
9
+ describe('Vector3', () => {
10
+ it('norm', () => {
11
11
  const result = Vector3.norm(v1);
12
12
  expect(result).equal(Math.sqrt(14));
13
13
  });
14
- });
15
14
 
16
- describe('Vector3.normalize', () => {
17
- it('Should return the good value', () => {
15
+ it('normalize', () => {
18
16
  const result = Vector3.normalize(v1);
19
17
  expect(result).deep.equal([1 / Math.sqrt(14), 2 / Math.sqrt(14), 3 / Math.sqrt(14)]);
20
18
  });
21
- });
22
19
 
23
- describe('Vector3.add', () => {
24
- it('Should return the good value', () => {
20
+ it('add', () => {
25
21
  const result = Vector3.add(v1, v1);
26
22
  expect(result).deep.equal([2, 4, 6]);
27
23
  });
28
- });
29
24
 
30
- describe('Vector3.subtract', () => {
31
- it('Should return the good value', () => {
25
+ it('subtract', () => {
32
26
  const result = Vector3.subtract(v1, v1);
33
27
  expect(result).deep.equal([0, 0, 0]);
34
28
  });
35
- });
36
29
 
37
- describe('Vector3.distance', () => {
38
- it('Should return the good value', () => {
30
+ it('distance', () => {
39
31
  const result = Vector3.distance(v1, v2);
40
32
  expect(result).equal(Math.sqrt(27));
41
33
  });
42
- });
43
34
 
44
- describe('Vector3.cross', () => {
45
- it('Should return the good value', () => {
35
+ it('cross', () => {
46
36
  const result = Vector3.cross(v1, v2);
47
37
  expect(result).deep.equal([-3, 6, -3]);
48
38
  });
49
- });
50
39
 
51
- describe('Vector3.dot', () => {
52
- it('Should return the good value', () => {
40
+ it('dot', () => {
53
41
  const result = Vector3.dot(v1, v1);
54
42
  expect(result).equal(14);
55
43
  });
56
- });
57
44
 
58
- describe('Vector3.multiplyScalar', () => {
59
- it('Should return the good value', () => {
45
+ it('multiplyScalar', () => {
60
46
  const result = Vector3.multiplyScalar(v1, 2);
61
47
  expect(result).deep.equal([2, 4, 6]);
62
48
  });