forked from react/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMathHelperTest.java
More file actions
147 lines (113 loc) · 4.74 KB
/
Copy pathMatrixMathHelperTest.java
File metadata and controls
147 lines (113 loc) · 4.74 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
144
145
146
147
package com.facebook.react.uimanager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.fest.assertions.api.Assertions.assertThat;
/**
* Test for {@link MatrixMathHelper}
*/
@RunWith(RobolectricTestRunner.class)
public class MatrixMathHelperTest {
private void verifyZRotatedMatrix(double degrees, double rotX, double rotY, double rotZ) {
MatrixMathHelper.MatrixDecompositionContext ctx =
new MatrixMathHelper.MatrixDecompositionContext();
double[] matrix = createRotateZ(degreesToRadians(degrees));
MatrixMathHelper.decomposeMatrix(matrix, ctx);
assertThat(ctx.rotationDegrees).containsSequence(rotX, rotY, rotZ);
}
private void verifyYRotatedMatrix(double degrees, double rotX, double rotY, double rotZ) {
MatrixMathHelper.MatrixDecompositionContext ctx =
new MatrixMathHelper.MatrixDecompositionContext();
double[] matrix = createRotateY(degreesToRadians(degrees));
MatrixMathHelper.decomposeMatrix(matrix, ctx);
assertThat(ctx.rotationDegrees).containsSequence(rotX, rotY, rotZ);
}
private void verifyXRotatedMatrix(double degrees, double rotX, double rotY, double rotZ) {
MatrixMathHelper.MatrixDecompositionContext ctx =
new MatrixMathHelper.MatrixDecompositionContext();
double[] matrix = createRotateX(degreesToRadians(degrees));
MatrixMathHelper.decomposeMatrix(matrix, ctx);
assertThat(ctx.rotationDegrees).containsSequence(rotX, rotY, rotZ);
}
@Test
public void testDecomposing4x4MatrixToProduceAccurateZaxisAngles() {
MatrixMathHelper.MatrixDecompositionContext ctx =
new MatrixMathHelper.MatrixDecompositionContext();
MatrixMathHelper.decomposeMatrix(
new double[]{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
ctx);
assertThat(ctx.rotationDegrees).containsSequence(0d, 0d, 0d);
double[] angles = new double[]{30, 45, 60, 75, 90, 100, 115, 120, 133, 167};
for (double angle : angles) {
verifyZRotatedMatrix(angle, 0d, 0d, angle);
verifyZRotatedMatrix(-angle, 0d, 0d, -angle);
}
verifyZRotatedMatrix(180d, 0d, 0d, 180d);
// all values are between 0 and 180;
// change of sign and direction in the third and fourth quadrant
verifyZRotatedMatrix(222, 0d, 0d, -138d);
verifyZRotatedMatrix(270, 0d, 0d, -90d);
// 360 is expressed as 0
verifyZRotatedMatrix(360, 0d, 0d, 0d);
verifyZRotatedMatrix(33.33333333, 0d, 0d, 33.333d);
verifyZRotatedMatrix(86.75309, 0d, 0d, 86.753d);
verifyZRotatedMatrix(42.00000000001, 0d, 0d, 42d);
verifyZRotatedMatrix(42.99999999999, 0d, 0d, 43d);
verifyZRotatedMatrix(42.99999999999, 0d, 0d, 43d);
verifyZRotatedMatrix(42.49999999999, 0d, 0d, 42.5d);
verifyZRotatedMatrix(42.55555555555, 0d, 0d, 42.556d);
}
@Test
public void testDecomposing4x4MatrixToProduceAccurateYaxisAngles() {
double[] angles = new double[]{30, 45, 60, 75, 90, 100, 110, 120, 133, 167};
for (double angle : angles) {
verifyYRotatedMatrix(angle, 0d, angle, 0d);
verifyYRotatedMatrix(-angle, 0d, -angle, 0d);
}
// all values are between 0 and 180;
// change of sign and direction in the third and fourth quadrant
verifyYRotatedMatrix(222, 0d, -138d, 0d);
verifyYRotatedMatrix(270, 0d, -90d, 0d);
verifyYRotatedMatrix(360, 0d, 0d, 0d);
}
@Test
public void testDecomposing4x4MatrixToProduceAccurateXaxisAngles() {
double[] angles = new double[]{30, 45, 60, 75, 90, 100, 110, 120, 133, 167};
for (double angle : angles) {
verifyXRotatedMatrix(angle, angle, 0d, 0d);
verifyXRotatedMatrix(-angle, -angle, 0d, 0d);
}
// all values are between 0 and 180;
// change of sign and direction in the third and fourth quadrant
verifyXRotatedMatrix(222, -138d, 0d, 0d);
verifyXRotatedMatrix(270, -90d, 0d, 0d);
verifyXRotatedMatrix(360, 0d, 0d, 0d);
}
private static double degreesToRadians(double degrees) {
return degrees * Math.PI / 180;
}
private static double[] createRotateZ(double radians) {
double[] mat = MatrixMathHelper.createIdentityMatrix();
mat[0] = Math.cos(radians);
mat[1] = Math.sin(radians);
mat[4] = -Math.sin(radians);
mat[5] = Math.cos(radians);
return mat;
}
private static double[] createRotateY(double radians) {
double[] mat = MatrixMathHelper.createIdentityMatrix();
mat[0] = Math.cos(radians);
mat[2] = -Math.sin(radians);
mat[8] = Math.sin(radians);
mat[10] = Math.cos(radians);
return mat;
}
private static double[] createRotateX(double radians) {
double[] mat = MatrixMathHelper.createIdentityMatrix();
mat[5] = Math.cos(radians);
mat[6] = Math.sin(radians);
mat[9] = -Math.sin(radians);
mat[10] = Math.cos(radians);
return mat;
}
}