@wemap/maths 0.1.1

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 ADDED
@@ -0,0 +1,19 @@
1
+ import Matrix from './src/Matrix';
2
+ import Matrix3 from './src/Matrix3';
3
+ import Matrix4 from './src/Matrix4';
4
+ import Quaternion from './src/Quaternion';
5
+ import Rotations from './src/Rotations';
6
+ import Utils from './src/Utils';
7
+ import Vector from './src/Vector';
8
+ import Vector3 from './src/Vector3';
9
+
10
+ export {
11
+ Matrix,
12
+ Matrix3,
13
+ Matrix4,
14
+ Quaternion,
15
+ Rotations,
16
+ Utils,
17
+ Vector,
18
+ Vector3
19
+ };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "author": "Wemap",
3
+ "contributors": [
4
+ "Thibaud Michel <thibaud@getwemap.com>",
5
+ "Guillaume Pannetier <guillaume.pannetier@getwemap.com>"
6
+ ],
7
+ "description": "Wemap maths utils package",
8
+ "main": "index.js",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/wemap/wemap-utils-js.git",
12
+ "directory": "packages/maths"
13
+ },
14
+ "name": "@wemap/maths",
15
+ "version": "0.1.1",
16
+ "bugs": {
17
+ "url": "https://github.com/wemap/wemap-utils-js/issues"
18
+ },
19
+ "homepage": "https://github.com/wemap/wemap-utils-js#readme",
20
+ "scripts": {
21
+ "test": "mocha -r esm \\\"src/**/*.spec.js\\\""
22
+ },
23
+ "keywords": [
24
+ "utils",
25
+ "maths",
26
+ "wemap"
27
+ ],
28
+ "license": "ISC",
29
+ "gitHead": "00ed0bae7da0086bb47d6e958343fd2020bac2b1"
30
+ }
package/src/Matrix.js ADDED
@@ -0,0 +1,245 @@
1
+ /* eslint max-depth: 0 */
2
+ class Matrix {
3
+
4
+ static identity4() {
5
+ return [
6
+ [1, 0, 0, 0],
7
+ [0, 1, 0, 0],
8
+ [0, 0, 1, 0],
9
+ [0, 0, 0, 1]
10
+ ];
11
+ }
12
+
13
+ static add(a, b) {
14
+ const aNumRows = a.length;
15
+ const aNumCols = a[0].length;
16
+
17
+ const m = new Array(aNumRows);
18
+ for (let r = 0; r < aNumRows; ++r) {
19
+ m[r] = new Array(aNumCols);
20
+ for (let c = 0; c < aNumCols; ++c) {
21
+ m[r][c] = a[r][c] + b[r][c];
22
+ }
23
+ }
24
+ return m;
25
+ }
26
+
27
+ static concatRow(a, b) {
28
+ const aNumRows = a.length;
29
+ const bNumRows = b.length;
30
+
31
+ const m = new Array(aNumRows + bNumRows);
32
+
33
+ for (let r = 0; r < aNumRows; ++r) {
34
+ m[r] = a[r].slice();
35
+ }
36
+ for (let r = 0; r < bNumRows; ++r) {
37
+ m[aNumRows + r] = b[r].slice();
38
+ }
39
+
40
+ return m;
41
+ }
42
+
43
+ static concatLine(a, b) {
44
+ const aNumRows = a.length;
45
+ const m = new Array(aNumRows);
46
+ for (let r = 0; r < aNumRows; ++r) {
47
+ m[r] = a[r].concat(b[r]);
48
+ }
49
+ return m;
50
+ }
51
+
52
+ static diag(arr) {
53
+ const m = Array(arr.length);
54
+ for (let i = 0; i < arr.length; i++) {
55
+ m[i] = Array(arr.length);
56
+ for (let j = 0; j < arr.length; j++) {
57
+ m[i][j] = i === j ? arr[i] : 0;
58
+ }
59
+ }
60
+ return m;
61
+ }
62
+
63
+ static transpose(a) {
64
+
65
+ const numRows = a.length;
66
+ const numCols = a[0].length;
67
+
68
+ const m = Array(numCols);
69
+ for (let i = 0; i < numCols; i++) {
70
+ m[i] = Array(numRows);
71
+ for (let j = 0; j < numRows; j++) {
72
+ m[i][j] = a[j][i];
73
+ }
74
+ }
75
+
76
+ return m;
77
+ }
78
+
79
+
80
+ static multiply(a, b) {
81
+ const aNumRows = a.length;
82
+ const aNumCols = a[0].length;
83
+ const bNumCols = b[0].length;
84
+
85
+ const m = new Array(aNumRows);
86
+ for (let r = 0; r < aNumRows; ++r) {
87
+
88
+ m[r] = new Array(bNumCols);
89
+ for (let c = 0; c < bNumCols; ++c) {
90
+
91
+ m[r][c] = 0;
92
+ for (let i = 0; i < aNumCols; ++i) {
93
+ m[r][c] += a[r][i] * b[i][c];
94
+ }
95
+ }
96
+ }
97
+ return m;
98
+ }
99
+
100
+ static multiplyScalar(a, s) {
101
+ const aNumRows = a.length;
102
+ const aNumCols = a[0].length;
103
+
104
+ const m = new Array(aNumRows);
105
+ for (let r = 0; r < aNumRows; ++r) {
106
+ m[r] = new Array(aNumCols);
107
+ for (let c = 0; c < aNumCols; ++c) {
108
+ m[r][c] = a[r][c] * s;
109
+ }
110
+ }
111
+ return m;
112
+ }
113
+
114
+ static multiplyVector(a, v) {
115
+ const aNumRows = a.length;
116
+ const aNumCols = a[0].length;
117
+
118
+ const output = new Array(aNumRows);
119
+ for (let r = 0; r < aNumRows; ++r) {
120
+ output[r] = 0;
121
+ for (let c = 0; c < aNumCols; ++c) {
122
+ output[r] += a[r][c] * v[c];
123
+ }
124
+ }
125
+ return output;
126
+ }
127
+
128
+ // Returns the inverse of matrix M
129
+ // Helped from http://blog.acipo.com/matrix-inversion-in-javascript/
130
+ static inverse(M) {
131
+ // I use Guassian Elimination to calculate the inverse:
132
+ // (1) 'augment' the matrix (left) by the identity (on the right)
133
+ // (2) Turn the matrix on the left into the identity by elemetry row ops
134
+ // (3) The matrix on the right is the inverse (was the identity matrix)
135
+ // There are 3 elemtary row ops: (I combine b and c in my code)
136
+ // (a) Swap 2 rows
137
+ // (b) Multiply a row by a scalar
138
+ // (c) Add 2 rows
139
+
140
+ // if the matrix isn't square: exit (error)
141
+ if (M.length !== M[0].length) {
142
+ return null;
143
+ }
144
+
145
+ // create the identity matrix (I), and a copy (C) of the original
146
+ let i = 0, ii = 0, j = 0, dim = M.length, e = 0;
147
+ const I = [], C = [];
148
+ for (i = 0; i < dim; i += 1) {
149
+ // Create the row
150
+ I[I.length] = [];
151
+ C[C.length] = [];
152
+ for (j = 0; j < dim; j += 1) {
153
+
154
+ // if we're on the diagonal, put a 1 (for identity)
155
+ if (i === j) {
156
+ I[i][j] = 1;
157
+ } else {
158
+ I[i][j] = 0;
159
+ }
160
+
161
+ // Also, make the copy of the original
162
+ C[i][j] = M[i][j];
163
+ }
164
+ }
165
+
166
+ // Perform elementary row operations
167
+ for (i = 0; i < dim; i += 1) {
168
+ // get the element e on the diagonal
169
+ e = C[i][i];
170
+
171
+ // if we have a 0 on the diagonal (we'll need to swap with a lower row)
172
+ if (e === 0) {
173
+ // look through every row below the i'th row
174
+ for (ii = i + 1; ii < dim; ii += 1) {
175
+ // if the ii'th row has a non-0 in the i'th col
176
+ if (C[ii][i] !== 0) {
177
+ // it would make the diagonal have a non-0 so swap it
178
+ for (j = 0; j < dim; j++) {
179
+ // temp store i'th row
180
+ e = C[i][j];
181
+ // replace i'th row by ii'th
182
+ C[i][j] = C[ii][j];
183
+ // repace ii'th by temp
184
+ C[ii][j] = e;
185
+ // temp store i'th row
186
+ e = I[i][j];
187
+ // replace i'th row by ii'th
188
+ I[i][j] = I[ii][j];
189
+ // repace ii'th by temp
190
+ I[ii][j] = e;
191
+ }
192
+ // don't bother checking other rows since we've swapped
193
+ break;
194
+ }
195
+ }
196
+ // get the new diagonal
197
+ e = C[i][i];
198
+ // if it's still 0, not invertable (error)
199
+ if (e === 0) {
200
+ return null;
201
+ }
202
+ }
203
+
204
+ // Scale this row down by e (so we have a 1 on the diagonal)
205
+ for (j = 0; j < dim; j++) {
206
+ // apply to original matrix
207
+ C[i][j] = C[i][j] / e;
208
+
209
+ // apply to identity
210
+ I[i][j] = I[i][j] / e;
211
+ }
212
+
213
+ // Subtract this row (scaled appropriately for each row) from ALL of
214
+ // the other rows so that there will be 0's in this column in the
215
+ // rows above and below this one
216
+ for (ii = 0; ii < dim; ii++) {
217
+ // Only apply to other rows (we want a 1 on the diagonal)
218
+ if (ii === i) {
219
+ continue;
220
+ }
221
+
222
+ // We want to change this element to 0
223
+ e = C[ii][i];
224
+
225
+ // Subtract (the row above(or below) scaled by e) from (the
226
+ // current row) but start at the i'th column and assume all the
227
+ // stuff left of diagonal is 0 (which it should be if we made this
228
+ // algorithm correctly)
229
+ for (j = 0; j < dim; j++) {
230
+ // apply to original matrix
231
+ C[ii][j] -= e * C[i][j];
232
+
233
+ // apply to identity
234
+ I[ii][j] -= e * I[i][j];
235
+ }
236
+ }
237
+ }
238
+
239
+ // we've done all operations, C should be the identity
240
+ // matrix I should be the inverse:
241
+ return I;
242
+ }
243
+
244
+ }
245
+ export default Matrix;
@@ -0,0 +1,121 @@
1
+
2
+ import { expect } from 'chai';
3
+ import Matrix from './Matrix';
4
+
5
+ const m1 = [
6
+ [1, 2, 3, 4],
7
+ [5, 6, 7, 8],
8
+ [9, 0, 1, 2]
9
+ ];
10
+
11
+ const m2 = [
12
+ [1, 2],
13
+ [3, 4],
14
+ [5, 6],
15
+ [7, 8]
16
+ ];
17
+
18
+ const m3 = [[3, 4, 5, 6]];
19
+
20
+ const m4 = [[5, 6], [9, 0], [3, 4]];
21
+
22
+ const m5 = [[1, 2], [3, 4]];
23
+
24
+ const v1 = [1, 3, 5, 7];
25
+
26
+ describe('Matrix.add', () => {
27
+ it('Should return the good value', () => {
28
+ const result = Matrix.add(m5, m5);
29
+ expect(result).deep.equal([
30
+ [2, 4],
31
+ [6, 8]
32
+ ]);
33
+ });
34
+ });
35
+
36
+ describe('Matrix.concatRow', () => {
37
+ it('Should return the good value', () => {
38
+ const result = Matrix.concatRow(m1, m3);
39
+ expect(result).deep.equal([
40
+ [1, 2, 3, 4],
41
+ [5, 6, 7, 8],
42
+ [9, 0, 1, 2],
43
+ [3, 4, 5, 6]
44
+ ]);
45
+ });
46
+ });
47
+
48
+ describe('Matrix.concatLine', () => {
49
+ it('Should return the good value', () => {
50
+ const result = Matrix.concatLine(m1, m4);
51
+ expect(result).deep.equal([
52
+ [1, 2, 3, 4, 5, 6],
53
+ [5, 6, 7, 8, 9, 0],
54
+ [9, 0, 1, 2, 3, 4]
55
+ ]);
56
+ });
57
+ });
58
+
59
+ describe('Matrix.diag', () => {
60
+ it('Should return the good value', () => {
61
+ const result = Matrix.diag([1, 2, 3, 4]);
62
+ expect(result).deep.equal([
63
+ [1, 0, 0, 0],
64
+ [0, 2, 0, 0],
65
+ [0, 0, 3, 0],
66
+ [0, 0, 0, 4]
67
+ ]);
68
+ });
69
+ });
70
+
71
+ describe('Matrix.transpose', () => {
72
+ it('Should return the good value', () => {
73
+ const result = Matrix.transpose(m1);
74
+ expect(result).deep.equal([
75
+ [1, 5, 9],
76
+ [2, 6, 0],
77
+ [3, 7, 1],
78
+ [4, 8, 2]
79
+ ]);
80
+ });
81
+ });
82
+
83
+ describe('Matrix.multiply', () => {
84
+ it('Should return the good value', () => {
85
+ const result = Matrix.multiply(m1, m2);
86
+ expect(result).deep.equal([
87
+ [50, 60],
88
+ [114, 140],
89
+ [28, 40]
90
+ ]);
91
+ });
92
+ });
93
+
94
+ describe('Matrix.multiplyScalar', () => {
95
+ it('Should return the good value', () => {
96
+ const result = Matrix.multiplyScalar(m2, 3);
97
+ expect(result).deep.equal([
98
+ [3, 6],
99
+ [9, 12],
100
+ [15, 18],
101
+ [21, 24]
102
+ ]);
103
+ });
104
+ });
105
+
106
+ describe('Matrix.multiplyVector', () => {
107
+ it('Should return the good value', () => {
108
+ const result = Matrix.multiplyVector(m1, v1);
109
+ expect(result).deep.equal([50, 114, 28]);
110
+ });
111
+ });
112
+
113
+ describe('Matrix.inverse', () => {
114
+ it('Should return the good value', () => {
115
+ const result = Matrix.inverse([[1, 2], [3, 4]]);
116
+ expect(result).deep.equal([
117
+ [-2, 1],
118
+ [1.5, -0.5]
119
+ ]);
120
+ });
121
+ });
package/src/Matrix3.js ADDED
@@ -0,0 +1,106 @@
1
+ import Matrix from './Matrix';
2
+
3
+ class Matrix3 extends Matrix {
4
+
5
+ static add(m1, m2) {
6
+ return [
7
+ [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2]],
8
+ [m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2]],
9
+ [m1[2][0] + m2[2][0], m1[2][1] + m2[2][1], m1[2][2] + m2[2][2]]
10
+ ];
11
+ }
12
+
13
+ static transpose(m) {
14
+ return [
15
+ [m[0][0], m[1][0], m[2][0]],
16
+ [m[0][1], m[1][1], m[2][1]],
17
+ [m[0][2], m[1][2], m[2][2]]
18
+ ];
19
+ }
20
+
21
+ static zeros() {
22
+ return [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
23
+ }
24
+
25
+ static multiplyScalar(m, s) {
26
+ return [
27
+ [m[0][0] * s, m[0][1] * s, m[0][2] * s],
28
+ [m[1][0] * s, m[1][1] * s, m[1][2] * s],
29
+ [m[2][0] * s, m[2][1] * s, m[2][2] * s]
30
+ ];
31
+ }
32
+
33
+ static multiplyVector(a, v) {
34
+ const output = new Array(3);
35
+ for (let r = 0; r < 3; ++r) {
36
+ output[r] = 0;
37
+ for (let c = 0; c < 3; ++c) {
38
+ output[r] += a[r][c] * v[c];
39
+ }
40
+ }
41
+ return output;
42
+ }
43
+
44
+ static multiply(a, b) {
45
+ const m = new Array(3);
46
+ for (let r = 0; r < 3; ++r) {
47
+ m[r] = new Array(3);
48
+ for (let c = 0; c < 3; ++c) {
49
+ m[r][c] = 0;
50
+ for (let i = 0; i < 3; ++i) {
51
+ m[r][c] += a[r][i] * b[i][c];
52
+ }
53
+ }
54
+ }
55
+ return m;
56
+ }
57
+
58
+ static skew(v) {
59
+ return [
60
+ [0, -v[2], v[1]],
61
+ [v[2], 0, -v[0]],
62
+ [-v[1], v[0], 0]
63
+ ];
64
+ }
65
+
66
+ // Helped from https://github.com/mrdoob/three.js/blob/34dc2478c684066257e4e39351731a93c6107ef5/src/math/Matrix3.js#L183
67
+ static inverse(me) {
68
+
69
+ const te = new Array(3),
70
+
71
+ n11 = me[0][0], n21 = me[0][1], n31 = me[0][2],
72
+ n12 = me[1][0], n22 = me[1][1], n32 = me[1][2],
73
+ n13 = me[2][0], n23 = me[2][1], n33 = me[2][2],
74
+
75
+ t11 = n33 * n22 - n32 * n23,
76
+ t12 = n32 * n13 - n33 * n12,
77
+ t13 = n23 * n12 - n22 * n13,
78
+
79
+ det = n11 * t11 + n21 * t12 + n31 * t13;
80
+
81
+ if (det === 0) {
82
+ return null;
83
+ }
84
+
85
+ te[0] = new Array(3);
86
+ te[1] = new Array(3);
87
+ te[2] = new Array(3);
88
+
89
+ const detInv = 1 / det;
90
+
91
+ te[0][0] = t11 * detInv;
92
+ te[0][1] = (n31 * n23 - n33 * n21) * detInv;
93
+ te[0][2] = (n32 * n21 - n31 * n22) * detInv;
94
+
95
+ te[1][0] = t12 * detInv;
96
+ te[1][1] = (n33 * n11 - n31 * n13) * detInv;
97
+ te[1][2] = (n31 * n12 - n32 * n11) * detInv;
98
+
99
+ te[2][0] = t13 * detInv;
100
+ te[2][1] = (n21 * n13 - n23 * n11) * detInv;
101
+ te[2][2] = (n22 * n11 - n21 * n12) * detInv;
102
+
103
+ return te;
104
+ }
105
+ }
106
+ export default Matrix3;
@@ -0,0 +1,94 @@
1
+ import chai from 'chai';
2
+ import chaiAlmost from 'chai-almost';
3
+
4
+ import Matrix3 from './Matrix3';
5
+
6
+ const expect = chai.expect;
7
+ chai.use(chaiAlmost());
8
+
9
+ const m1 = [
10
+ [1, 2, 3],
11
+ [4, 5, 6],
12
+ [7, 8, 9]
13
+ ];
14
+
15
+ const m2 = [
16
+ [1, 2, 3],
17
+ [0, 1, 5],
18
+ [5, 6, 0]
19
+ ];
20
+
21
+ const v1 = [1, 2, 3];
22
+
23
+ describe('Matrix3.add', () => {
24
+ it('Should return the good value', () => {
25
+ const result = Matrix3.add(m1, m1);
26
+ expect(result).deep.equal([
27
+ [2, 4, 6],
28
+ [8, 10, 12],
29
+ [14, 16, 18]
30
+ ]);
31
+ });
32
+ });
33
+
34
+ describe('Matrix3.transpose', () => {
35
+ it('Should return the good value', () => {
36
+ const result = Matrix3.transpose(m1);
37
+ expect(result).deep.equal([
38
+ [1, 4, 7],
39
+ [2, 5, 8],
40
+ [3, 6, 9]
41
+ ]);
42
+ });
43
+ });
44
+
45
+ describe('Matrix3.multiplyScalar', () => {
46
+ it('Should return the good value', () => {
47
+ const result = Matrix3.multiplyScalar(m1, 2);
48
+ expect(result).deep.equal([
49
+ [2, 4, 6],
50
+ [8, 10, 12],
51
+ [14, 16, 18]
52
+ ]);
53
+ });
54
+ });
55
+
56
+ describe('Matrix3.multiplyVector', () => {
57
+ it('Should return the good value', () => {
58
+ const result = Matrix3.multiplyVector(m1, v1);
59
+ expect(result).deep.equal([14, 32, 50]);
60
+ });
61
+ });
62
+
63
+ describe('Matrix3.multiply', () => {
64
+ it('Should return the good value', () => {
65
+ const result = Matrix3.multiply(m1, m1);
66
+ expect(result).deep.equal([
67
+ [30, 36, 42],
68
+ [66, 81, 96],
69
+ [102, 126, 150]
70
+ ]);
71
+ });
72
+ });
73
+
74
+ describe('Matrix3.skew', () => {
75
+ it('Should return the good value', () => {
76
+ const result = Matrix3.skew(v1);
77
+ expect(result).deep.equal([
78
+ [0, -3, 2],
79
+ [3, 0, -1],
80
+ [-2, 1, 0]
81
+ ]);
82
+ });
83
+ });
84
+
85
+ describe('Matrix3.inverse', () => {
86
+ it('Should return the good value', () => {
87
+ const result = Matrix3.inverse(m2);
88
+ expect(result).deep.almost([
89
+ [-6, 18 / 5, 7 / 5],
90
+ [5, -3, -1],
91
+ [-1, 4 / 5, 1 / 5]
92
+ ]);
93
+ });
94
+ });
package/src/Matrix4.js ADDED
@@ -0,0 +1,24 @@
1
+ import Matrix from './Matrix';
2
+
3
+ class Matrix4 extends Matrix {
4
+
5
+ static add(m1, m2) {
6
+ return [
7
+ [m1[0][0] + m2[0][0], m1[0][1] + m2[0][1], m1[0][2] + m2[0][2], m1[0][3] + m2[0][3]],
8
+ [m1[1][0] + m2[1][0], m1[1][1] + m2[1][1], m1[1][2] + m2[1][2], m1[1][3] + m2[1][3]],
9
+ [m1[2][0] + m2[2][0], m1[2][1] + m2[2][1], m1[2][2] + m2[2][2], m1[2][3] + m2[2][3]],
10
+ [m1[3][0] + m2[3][0], m1[3][1] + m2[3][1], m1[3][2] + m2[3][2], m1[3][3] + m2[3][3]]
11
+ ];
12
+ }
13
+
14
+ static subtract(m1, m2) {
15
+ return [
16
+ [m1[0][0] - m2[0][0], m1[0][1] - m2[0][1], m1[0][2] - m2[0][2], m1[0][3] - m2[0][3]],
17
+ [m1[1][0] - m2[1][0], m1[1][1] - m2[1][1], m1[1][2] - m2[1][2], m1[1][3] - m2[1][3]],
18
+ [m1[2][0] - m2[2][0], m1[2][1] - m2[2][1], m1[2][2] - m2[2][2], m1[2][3] - m2[2][3]],
19
+ [m1[3][0] - m2[3][0], m1[3][1] - m2[3][1], m1[3][2] - m2[3][2], m1[3][3] - m2[3][3]]
20
+ ];
21
+ }
22
+
23
+ }
24
+ export default Matrix4;
@@ -0,0 +1,80 @@
1
+ class Quaternion {
2
+
3
+ static add(v1, v2) {
4
+ return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2], v1[3] + v2[3]];
5
+ }
6
+
7
+ static rotate(q, v) {
8
+ const [qw, qx, qy, qz] = q;
9
+ const [vx, vy, vz] = v;
10
+ return [
11
+ 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),
12
+ 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),
13
+ 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)
14
+ ];
15
+ }
16
+
17
+ static inverse(q) {
18
+ return [-q[0], q[1], q[2], q[3]];
19
+ }
20
+
21
+ static multiply(q, r) {
22
+ const [qw, qx, qy, qz] = q;
23
+ const [rw, rx, ry, rz] = r;
24
+
25
+ return [
26
+ qw * rw - qx * rx - qy * ry - qz * rz,
27
+ qw * rx + rw * qx + qy * rz - qz * ry,
28
+ qw * ry + rw * qy + qz * rx - qx * rz,
29
+ qw * rz + rw * qz + qx * ry - qy * rx
30
+ ];
31
+ }
32
+
33
+ /**
34
+ * Axis-angle to quaternion
35
+ * @param {Array[Number]} axis unit vector
36
+ * @param {Number} angle in radians
37
+ */
38
+ static fromAxisAngle(axis, angle) {
39
+ const s = Math.sin(angle / 2);
40
+ return [Math.cos(angle / 2), axis[0] * s, axis[1] * s, axis[2] * s];
41
+ }
42
+
43
+ static fromMatrix3(m) {
44
+ const qw = Math.sqrt(1 + m[0][0] + m[1][1] + m[2][2]) / 2;
45
+ const qx = (m[1][2] - m[2][1]) / (4 * qw);
46
+ const qy = (m[2][0] - m[0][2]) / (4 * qw);
47
+ const qz = (m[0][1] - m[1][0]) / (4 * qw);
48
+ return [qw, qx, qy, qz];
49
+ }
50
+
51
+ static wxyz2xyzw(wxyz) {
52
+ return [wxyz[1], wxyz[2], wxyz[3], wxyz[0]];
53
+ }
54
+
55
+ static xyzw2wxyz(xyzw) {
56
+ return [xyzw[3], xyzw[0], xyzw[1], xyzw[2]];
57
+ }
58
+
59
+ static distance(q1, q2) {
60
+ return Math.acos(Math.min(2 * (Quaternion.dot(q1, q2) ** 2) - 1, 1));
61
+ }
62
+
63
+ static normalize(quaternion) {
64
+ const norm = this.norm(quaternion);
65
+ const output = new Array(4);
66
+ for (let i = 0; i < 4; i++) {
67
+ output[i] = quaternion[i] / norm;
68
+ }
69
+ return output;
70
+ }
71
+
72
+ static norm(quaternion) {
73
+ return Math.sqrt(quaternion[0] ** 2 + quaternion[1] ** 2 + quaternion[2] ** 2 + quaternion[3] ** 2);
74
+ }
75
+
76
+ static dot(q1, q2) {
77
+ return q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3];
78
+ }
79
+ }
80
+ export default Quaternion;
@@ -0,0 +1,109 @@
1
+ import chai from 'chai';
2
+ import chaiAlmost from 'chai-almost';
3
+
4
+ import Quaternion from './Quaternion';
5
+ import Vector3 from './Vector3';
6
+
7
+ const expect = chai.expect;
8
+ chai.use(chaiAlmost());
9
+
10
+ const q1 = [0.182574185835055, 0.365148371670111, 0.547722557505166, 0.730296743340221];
11
+ const q2 = [0.379049021789452, 0.454858826147342, 0.530668630505232, 0.606478434863123];
12
+
13
+ const v1 = [1, 2, 3];
14
+ const v1Normalized = Vector3.normalize([1, 2, 3]);
15
+
16
+ const r1 = [
17
+ [0.763133109231345, -0.514739781660985, 0.390731128489274],
18
+ [0.614322194361606, 0.765493640108917, -0.191383720487251],
19
+ [-0.200589379354045, 0.386086057930615, 0.900389613868328]
20
+ ];
21
+
22
+ const angle = 1.5;
23
+
24
+ describe('Quaternion.add', () => {
25
+ it('Should return the good value', () => {
26
+ const result = Quaternion.add(q1, q2);
27
+ expect(result).deep.almost(
28
+ [0.561623207624507, 0.820007197817453, 1.078391188010398, 1.336775178203344]
29
+ );
30
+ });
31
+ });
32
+
33
+ describe('Quaternion.rotate', () => {
34
+ it('Should return the good value', () => {
35
+ const result = Quaternion.rotate(q1, v1);
36
+ expect(result).deep.almost(
37
+ [1.666666666666667, 2.266666666666667, 2.466666666666667]
38
+ );
39
+ });
40
+ });
41
+
42
+ describe('Quaternion.multiply', () => {
43
+ it('Should return the good value', () => {
44
+ const result = Quaternion.multiply(q1, q2);
45
+ expect(result).deep.almost(
46
+ [-0.830454798537400, 0.166090959707480, 0.415227399268700, 0.332181919414960]
47
+ );
48
+ });
49
+ });
50
+
51
+ describe('Quaternion.fromAxisAngle', () => {
52
+ it('Should return the good value', () => {
53
+ const result = Quaternion.fromAxisAngle(v1Normalized, angle);
54
+ expect(result).deep.almost(
55
+ [0.731688868873821, 0.182175621539481, 0.364351243078963, 0.546526864618444]
56
+ );
57
+ });
58
+ });
59
+
60
+ describe('Quaternion.fromMatrix3', () => {
61
+ it('Should return the good value', () => {
62
+ const result = Quaternion.fromMatrix3(r1);
63
+ expect(result).deep.almost(
64
+ [0.925880170865619, -0.155924545256753, -0.159664427009622, -0.304861798413669]
65
+ );
66
+ });
67
+ });
68
+
69
+ describe('Quaternion.wxyz2xyzw', () => {
70
+ it('Should return the good value', () => {
71
+ const result = Quaternion.wxyz2xyzw(q1);
72
+ expect(result).deep.almost([q1[1], q1[2], q1[3], q1[0]]);
73
+ });
74
+ });
75
+
76
+ describe('Quaternion.xyzw2wxyz', () => {
77
+ it('Should return the good value', () => {
78
+ const result = Quaternion.xyzw2wxyz(q1);
79
+ expect(result).deep.almost([q1[3], q1[0], q1[1], q1[2]]);
80
+ });
81
+ });
82
+
83
+ describe('Quaternion.distance', () => {
84
+ it('Should return the good value', () => {
85
+ const result = Quaternion.distance(q1, q2);
86
+ expect(result).almost(0.500391840845023);
87
+ });
88
+ });
89
+
90
+ describe('Quaternion.norm', () => {
91
+ it('Should return the good value', () => {
92
+ const result = Quaternion.norm(q1);
93
+ expect(result).almost(1);
94
+ });
95
+ });
96
+
97
+ describe('Quaternion.normalize', () => {
98
+ it('Should return the good value', () => {
99
+ const result = Quaternion.normalize(q1);
100
+ expect(result).deep.almost(q1);
101
+ });
102
+ });
103
+
104
+ describe('Quaternion.dot', () => {
105
+ it('Should return the good value', () => {
106
+ const result = Quaternion.dot(q1, q2);
107
+ expect(result).almost(0.968863931626966);
108
+ });
109
+ });
@@ -0,0 +1,143 @@
1
+ import Utils from './Utils';
2
+ import Vector3 from './Vector3';
3
+ import Quaternion from './Quaternion';
4
+
5
+ // import Constants from '../../geo/src/Constants';
6
+
7
+
8
+ // Shortcuts to avoid reprocessing
9
+ // const quatRotX90 = [Math.sqrt(2) / 2, -Math.sqrt(2) / 2, 0, 0];
10
+ const rw = Math.sqrt(2) / 2;
11
+ const rx = - Math.sqrt(2) / 2;
12
+
13
+ class Rotations {
14
+
15
+ // ZXY because yaw is around Z axis, pitch around X axis and roll around Y axis.
16
+ // They are conventions for a smartphone frame.
17
+ static eulerToQuaternionZXYDegrees(eulerAnglesDegrees) {
18
+ const eulerAngles = eulerAnglesDegrees.slice(0).map(function(x) {
19
+ return Utils.deg2rad(x);
20
+ });
21
+ return Rotations.eulerToQuaternionZXY(eulerAngles);
22
+ }
23
+
24
+ static eulerToQuaternionZXY(eulerAngles) {
25
+ const yaw = eulerAngles[0];
26
+ const pitch = eulerAngles[1];
27
+ const roll = eulerAngles[2];
28
+
29
+ const c1 = Math.cos(yaw / 2);
30
+ const c2 = Math.cos(pitch / 2);
31
+ const c3 = Math.cos(roll / 2);
32
+ const s1 = Math.sin(yaw / 2);
33
+ const s2 = Math.sin(pitch / 2);
34
+ const s3 = Math.sin(roll / 2);
35
+
36
+ let w = c1 * c2 * c3 - s1 * s2 * s3;
37
+ let x = c1 * s2 * c3 - s1 * c2 * s3;
38
+ let y = s1 * s2 * c3 + c1 * c2 * s3;
39
+ let z = s1 * c2 * c3 + c1 * s2 * s3;
40
+
41
+ // Let's quaternion has a positive scalar.
42
+ if (w < 0) {
43
+ w = -w;
44
+ x = -x;
45
+ y = -y;
46
+ z = -z;
47
+ }
48
+
49
+ const quaternion = [w, x, y, z];
50
+ return quaternion;
51
+ }
52
+
53
+
54
+ static quaternionToEulerZXYDegrees(quaternion) {
55
+ const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
56
+ return eulerAngles.map(function(x) {
57
+ return Utils.rad2deg(x);
58
+ });
59
+ }
60
+
61
+ static quaternionToEulerZXY(quaternion) {
62
+
63
+ const qw = quaternion[0];
64
+ const qx = quaternion[1];
65
+ const qy = quaternion[2];
66
+ const qz = quaternion[3];
67
+
68
+ let alpha, gamma;
69
+
70
+ // From http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
71
+ if (qy * qz + qx * qw === 0.5) {
72
+ alpha = 2 * Math.atan2(qy, qw);
73
+ gamma = 0;
74
+ } else if (qy * qz + qx * qw === -0.5) {
75
+ alpha = -2 * Math.atan2(qy, qw);
76
+ gamma = 0;
77
+ } else {
78
+ alpha = Math.atan2(2 * qz * qw - 2 * qy * qx, 1 - 2 * Math.pow(qz, 2) - 2 * Math.pow(qx, 2));
79
+ gamma = Math.atan2(2 * qy * qw - 2 * qz * qx, 1 - 2 * Math.pow(qy, 2) - 2 * Math.pow(qx, 2));
80
+ }
81
+
82
+ const beta = Math.asin(2 * qy * qz + 2 * qx * qw);
83
+ return [alpha, beta, gamma];
84
+ }
85
+
86
+
87
+ static getHeadingFromQuaternion(quaternion) {
88
+ // Minus before "yaw" angle is here because ENU attitude is counter-clockwise whereas WGS84 is clockwise.
89
+
90
+ const [qw, qx, qy, qz] = quaternion;
91
+ if (Math.asin(2 * qy * qz + 2 * qx * qw) < Math.PI / 4) {
92
+ // Map version
93
+ return -Math.atan2(2 * qz * qw - 2 * qy * qx, 1 - 2 * qz * qz - 2 * qx * qx);
94
+ }
95
+ // AR version
96
+ const [pw, px, py, pz] = [
97
+ qw * rw - qx * rx,
98
+ qw * rx + rw * qx,
99
+ rw * qy + qz * rx,
100
+ rw * qz - qy * rx
101
+ ];
102
+ return -Math.atan2(2 * pz * pw - 2 * py * px, 1 - 2 * pz * pz - 2 * px * px);
103
+
104
+ // ---- Not optimized version ----
105
+ // const eulerAngles = Rotations.quaternionToEulerZXY(quaternion);
106
+ // if (eulerAngles[1] < Math.PI / 4) {
107
+ // return -eulerAngles[0];
108
+ // }
109
+ // const eulerAnglesAR = Rotations.quaternionToEulerZXY(Quaternion.multiply(quaternion, quatRotX90));
110
+ // return -eulerAnglesAR[0];
111
+ }
112
+
113
+ static getRotationBetweenTwoVectors(from, to) {
114
+ const fromNormalized = Vector3.normalize(from);
115
+ const toNormalized = Vector3.normalize(to);
116
+ const dot = Vector3.dot(fromNormalized, toNormalized);
117
+ let tmp;
118
+ if (dot < -0.999999) {
119
+ tmp = Vector3.cross([1, 0, 0], fromNormalized);
120
+ if (Vector3.norm(tmp) < 0.000001) {
121
+ tmp = Vector3.cross([0, 1, 0], fromNormalized);
122
+ }
123
+ Vector3.normalize(tmp, tmp);
124
+ return Quaternion.fromAxisAngle(tmp, Math.PI);
125
+ } else if (dot > 0.999999) {
126
+ return Quaternion.identity();
127
+ }
128
+
129
+ tmp = Vector3.cross(fromNormalized, toNormalized);
130
+ const quat = [1 + dot, tmp[0], tmp[1], tmp[2]];
131
+ return Quaternion.normalize(quat);
132
+ }
133
+
134
+ // TODO: Should be moved in wemap-positioning
135
+ // // Linear acceleration in ENU
136
+ // static computeLinearAcceleration(quaternion, acc) {
137
+ // const linearAcc = Quaternion.rotate(Quaternion.inverse(quaternion), acc);
138
+ // linearAcc[2] -= Constants.EARTH_GRAVITY;
139
+ // return linearAcc;
140
+ // }
141
+ }
142
+
143
+ export default Rotations;
@@ -0,0 +1,42 @@
1
+ import chai from 'chai';
2
+ import chaiAlmost from 'chai-almost';
3
+
4
+ import Rotations from './Rotations';
5
+
6
+ const expect = chai.expect;
7
+ chai.use(chaiAlmost());
8
+
9
+ const q1 = [0.182574185835055, 0.365148371670111, 0.547722557505166, 0.730296743340221];
10
+
11
+ const e1 = [-2.761086276477428, 1.203588306237060, -1.190289949682532];
12
+
13
+ const E1 = [-158.1985905136482, 68.960530218682791, -68.198590513648199];
14
+
15
+
16
+ describe('Rotations.quaternionToEulerZXY', () => {
17
+ it('Should return the good value', () => {
18
+ const result = Rotations.quaternionToEulerZXY(q1);
19
+ expect(result).deep.almost(e1);
20
+ });
21
+ });
22
+
23
+ describe('Rotations.quaternionToEulerZXYDegrees', () => {
24
+ it('Should return the good value', () => {
25
+ const result = Rotations.quaternionToEulerZXYDegrees(q1);
26
+ expect(result).deep.almost(E1);
27
+ });
28
+ });
29
+
30
+ describe('Rotations.eulerToQuaternionZXY', () => {
31
+ it('Should return the good value', () => {
32
+ const result = Rotations.eulerToQuaternionZXY(e1);
33
+ expect(result).deep.almost(q1);
34
+ });
35
+ });
36
+
37
+ describe('Rotations.eulerToQuaternionZXYDegrees', () => {
38
+ it('Should return the good value', () => {
39
+ const result = Rotations.eulerToQuaternionZXYDegrees(E1);
40
+ expect(result).deep.almost(q1);
41
+ });
42
+ });
package/src/Utils.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * [MathUtils description]
3
+ * @type {Object}
4
+ */
5
+ class Utils {
6
+
7
+ /**
8
+ * Transform an angle in degre to radian
9
+ * @param {Number} angle
10
+ * @return {Number}
11
+ */
12
+ static deg2rad(angle) {
13
+ return angle * (Math.PI / 180);
14
+ }
15
+
16
+ static rad2deg(rad) {
17
+ return rad * 180 / Math.PI;
18
+ }
19
+
20
+ static diffAngle(angle1, angle2) {
21
+ return (angle1 - angle2 + Math.PI) % (2 * Math.PI) - Math.PI;
22
+ }
23
+
24
+ }
25
+
26
+ export default Utils;
@@ -0,0 +1,30 @@
1
+ import { expect } from 'chai';
2
+ import MathUtils from './Utils';
3
+
4
+ let result, expected;
5
+
6
+ describe('deg2rad', () => {
7
+
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
+
15
+ expected = 1.5707963;
16
+ result = MathUtils.deg2rad(90);
17
+ result = (Math.round(result * factor) / factor);
18
+ expect(result).to.equal(expected);
19
+
20
+ expected = 0;
21
+ result = MathUtils.deg2rad(0);
22
+ result = (Math.round(result * factor) / factor);
23
+ expect(result).to.equal(expected);
24
+
25
+ expected = 2.3561945;
26
+ result = MathUtils.deg2rad(135);
27
+ result = (Math.round(result * factor) / factor);
28
+ expect(result).to.equal(expected);
29
+ });
30
+ });
package/src/Vector.js ADDED
@@ -0,0 +1,8 @@
1
+ class Vector {
2
+
3
+ static concat(v1, v2) {
4
+ return v1.concat(v2);
5
+ }
6
+ }
7
+
8
+ export default Vector;
@@ -0,0 +1,12 @@
1
+ import { expect } from 'chai';
2
+
3
+ import Vector from './Vector';
4
+
5
+ const v1 = [1, 2, 3, 4];
6
+
7
+ describe('Vector.concat', () => {
8
+ it('Should return the good value', () => {
9
+ const result = Vector.concat(v1, v1);
10
+ expect(result).deep.equal([1, 2, 3, 4, 1, 2, 3, 4]);
11
+ });
12
+ });
package/src/Vector3.js ADDED
@@ -0,0 +1,50 @@
1
+ import Vector from './Vector';
2
+
3
+ class Vector3 extends Vector {
4
+
5
+ static norm(vector) {
6
+ return Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
7
+ }
8
+
9
+ static normalize(vector) {
10
+ const norm = this.norm(vector);
11
+ const output = new Array(3);
12
+ for (let i = 0; i < 3; i++) {
13
+ output[i] = vector[i] / norm;
14
+ }
15
+ return output;
16
+ }
17
+
18
+ static add(v1, v2) {
19
+ return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
20
+ }
21
+
22
+ static subtract(v1, v2) {
23
+ return [v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]];
24
+ }
25
+
26
+ static distance(v1, v2) {
27
+ return Vector3.norm(Vector3.subtract(v1, v2));
28
+ }
29
+
30
+ static cross(v1, v2) {
31
+ return [v1[1] * v2[2] - v1[2] * v2[1],
32
+ v1[2] * v2[0] - v1[0] * v2[2],
33
+ v1[0] * v2[1] - v1[1] * v2[0]];
34
+ }
35
+
36
+ static dot(v1, v2) {
37
+ return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
38
+ }
39
+
40
+ static multiplyScalar(vector, scalar) {
41
+ const output = new Array(3);
42
+ for (let i = 0; i < 3; i++) {
43
+ output[i] = vector[i] * scalar;
44
+ }
45
+ return output;
46
+ }
47
+
48
+ }
49
+
50
+ export default Vector3;
@@ -0,0 +1,63 @@
1
+ import { expect } from 'chai';
2
+
3
+ import Vector3 from './Vector3';
4
+
5
+
6
+ const v1 = [1, 2, 3];
7
+ const v2 = [4, 5, 6];
8
+
9
+ describe('Vector3.norm', () => {
10
+ it('Should return the good value', () => {
11
+ const result = Vector3.norm(v1);
12
+ expect(result).equal(Math.sqrt(14));
13
+ });
14
+ });
15
+
16
+ describe('Vector3.normalize', () => {
17
+ it('Should return the good value', () => {
18
+ const result = Vector3.normalize(v1);
19
+ expect(result).deep.equal([1 / Math.sqrt(14), 2 / Math.sqrt(14), 3 / Math.sqrt(14)]);
20
+ });
21
+ });
22
+
23
+ describe('Vector3.add', () => {
24
+ it('Should return the good value', () => {
25
+ const result = Vector3.add(v1, v1);
26
+ expect(result).deep.equal([2, 4, 6]);
27
+ });
28
+ });
29
+
30
+ describe('Vector3.subtract', () => {
31
+ it('Should return the good value', () => {
32
+ const result = Vector3.subtract(v1, v1);
33
+ expect(result).deep.equal([0, 0, 0]);
34
+ });
35
+ });
36
+
37
+ describe('Vector3.distance', () => {
38
+ it('Should return the good value', () => {
39
+ const result = Vector3.distance(v1, v2);
40
+ expect(result).equal(Math.sqrt(27));
41
+ });
42
+ });
43
+
44
+ describe('Vector3.cross', () => {
45
+ it('Should return the good value', () => {
46
+ const result = Vector3.cross(v1, v2);
47
+ expect(result).deep.equal([-3, 6, -3]);
48
+ });
49
+ });
50
+
51
+ describe('Vector3.dot', () => {
52
+ it('Should return the good value', () => {
53
+ const result = Vector3.dot(v1, v1);
54
+ expect(result).equal(14);
55
+ });
56
+ });
57
+
58
+ describe('Vector3.multiplyScalar', () => {
59
+ it('Should return the good value', () => {
60
+ const result = Vector3.multiplyScalar(v1, 2);
61
+ expect(result).deep.equal([2, 4, 6]);
62
+ });
63
+ });