MatrixMath-test.js
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+react_native
*/
'use strict';
var MatrixMath = require('MatrixMath');
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}
function convertZeroes(degrees) {
return degrees.map(value => value === -0 ? 0 : value);
}
describe('MatrixMath', () => {
it('decomposes a 4x4 matrix to produce accurate Z-axis angles', () => {
expect(MatrixMath.decomposeMatrix([
1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
]).rotationDegrees).toEqual([0, 0, 0]);
[30, 45, 60, 75, 90, 100, 115, 120, 133, 167].forEach(angle => {
var mat = MatrixMath.createRotateZ(degreesToRadians(angle));
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
.toEqual([0, 0, angle]);
mat = MatrixMath.createRotateZ(degreesToRadians(-angle));
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
.toEqual([0, 0, -angle]);
});
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(180))
).rotationDegrees).toEqual([0, 0, 180]);
// all values are between 0 and 180;
// change of sign and direction in the third and fourth quadrant
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(222))
).rotationDegrees).toEqual([0, 0, -138]);
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(270))
).rotationDegrees).toEqual([0, 0, -90]);
// 360 is expressed as 0
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(360))
).rotationDegrees).toEqual([0, 0, -0]);
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(33.33333333))
).rotationDegrees).toEqual([0, 0, 33.333]);
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(86.75309))
).rotationDegrees).toEqual([0, 0, 86.753]);
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(42.00000000001))
).rotationDegrees).toEqual([0, 0, 42]);
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(42.99999999999))
).rotationDegrees).toEqual([0, 0, 43]);
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(42.49999999999))
).rotationDegrees).toEqual([0, 0, 42.5]);
expect(MatrixMath.decomposeMatrix(
MatrixMath.createRotateZ(degreesToRadians(42.55555555555))
).rotationDegrees).toEqual([0, 0, 42.556]);
});
it('decomposes a 4x4 matrix to produce accurate Y-axis angles', () => {
var mat;
[30, 45, 60, 75, 90, 100, 110, 120, 133, 167].forEach(angle => {
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(angle));
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
.toEqual([0, angle, 0]);
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(-angle));
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
.toEqual([0, -angle, 0]);
});
// all values are between 0 and 180;
// change of sign and direction in the third and fourth quadrant
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(222));
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
.toEqual([0, -138, 0]);
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(270));
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
.toEqual([0, -90, 0]);
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(360));
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
.toEqual([0, 0, 0]);
});
it('decomposes a 4x4 matrix to produce accurate X-axis angles', () => {
var mat;
[30, 45, 60, 75, 90, 100, 110, 120, 133, 167].forEach(angle => {
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(angle));
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
.toEqual([angle, 0, 0]);
});
// all values are between 0 and 180;
// change of sign and direction in the third and fourth quadrant
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(222));
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
.toEqual([-138, 0, 0]);
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(270));
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
.toEqual([-90, 0, 0]);
mat = MatrixMath.createIdentityMatrix();
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(360));
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
.toEqual([0, 0, 0]);
});
});