forked from software-mansion/react-native-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixUtils.test.tsx
215 lines (192 loc) · 5.67 KB
/
matrixUtils.test.tsx
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
AffineMatrix,
AffineMatrixFlat,
addMatrices,
decomposeMatrix,
flatten,
isAffineMatrix,
isAffineMatrixFlat,
multiplyMatrices,
subtractMatrices,
unflatten,
} from '../src/reanimated2/animation/transformationMatrix/matrixUtils';
const identityMatrix: AffineMatrix = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
];
const flatIdentityMatrix: AffineMatrixFlat = [
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
];
const consecutiveNumMatrix: AffineMatrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
];
describe('Matrix util functions', () => {
describe('Matrix multiplication', () => {
it('Multiply two identity matrices', () => {
expect(multiplyMatrices(identityMatrix, identityMatrix)).toEqual(
identityMatrix
);
});
it('Multiply arbitrary matrix by identity matrix', () => {
expect(multiplyMatrices(identityMatrix, consecutiveNumMatrix)).toEqual(
consecutiveNumMatrix
);
expect(multiplyMatrices(consecutiveNumMatrix, identityMatrix)).toEqual(
consecutiveNumMatrix
);
});
it('Multiply two arbitrary matrices', () => {
expect(
multiplyMatrices(consecutiveNumMatrix, consecutiveNumMatrix)
).toEqual([
[90, 100, 110, 120],
[202, 228, 254, 280],
[314, 356, 398, 440],
[426, 484, 542, 600],
]);
});
});
it('Test flatten & unflatten functions', () => {
expect(flatten(identityMatrix)).toEqual(flatIdentityMatrix);
// @ts-expect-error I know this is not the correct way to call this function, but I'm testing that it still works
expect(flatten(flatten(identityMatrix))).toEqual(flatIdentityMatrix);
expect(unflatten(flatIdentityMatrix)).toEqual(identityMatrix);
expect(unflatten(flatten(identityMatrix))).toEqual(identityMatrix);
expect(flatten(unflatten(flatIdentityMatrix))).toEqual(flatIdentityMatrix);
});
describe('Type assertions:', () => {
describe('Test isAffineMatrix function', () => {
it('Return true if given argument is a valid affine matrix', () => {
expect(isAffineMatrix(identityMatrix)).toEqual(true);
});
it('Return false if given matrix has incorrect shape', () => {
expect(
isAffineMatrix([
[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
])
).toEqual(false);
});
it('Return false if given matrix contains null or NaN', () => {
expect(
isAffineMatrix([
[1, 0, 0, 0],
[0, 1, 0, null],
[0, 0, 1, 0],
[0, 0, 0, 1],
])
).toEqual(false);
expect(
isAffineMatrix([
[1, 0, 0, 0],
[0, 1, 0, NaN],
[0, 0, 1, 0],
[0, 0, 0, 1],
])
).toEqual(false);
});
});
describe('Test isAffineMatrixFlat function ', () => {
it('Return true if given argument is has valid type', () => {
expect(isAffineMatrixFlat(flatIdentityMatrix)).toEqual(true);
});
it('Return false if given matrix has incorrect number of elements', () => {
expect(isAffineMatrixFlat([1, 0, 0, 0, 0, 1])).toEqual(false);
});
it('Return false if given matrix contains null or NaN', () => {
expect(
isAffineMatrixFlat([
1,
0,
null,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
])
).toEqual(false);
});
});
});
describe('Matrix calculations: ', () => {
const a = consecutiveNumMatrix;
const b: AffineMatrix = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
];
const aMinusB: AffineMatrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[8, 9, 10, 11],
[12, 13, 14, 15],
];
const aPlusB: AffineMatrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[10, 11, 12, 13],
[14, 15, 16, 17],
];
it('subtract matrices', () => {
expect(subtractMatrices(a, b)).toEqual(aMinusB);
expect(subtractMatrices(flatten(a), flatten(b))).toEqual(
flatten(aMinusB)
);
});
it('add matrices', () => {
expect(addMatrices(a, b)).toEqual(aPlusB);
expect(addMatrices(flatten(a), flatten(b))).toEqual(flatten(aPlusB));
});
});
describe('Matrix decomposition', () => {
it('Throw an error if matrix is incorrect (has zero as its last element)', () => {
const incorrectMatrix: AffineMatrix = [
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 0],
];
expect(() => decomposeMatrix(incorrectMatrix)).toThrowError();
});
it('Decompose identity into identities', () => {
const { translationMatrix, scaleMatrix, rotationMatrix, skewMatrix } =
decomposeMatrix(flatten(identityMatrix));
expect(translationMatrix).toEqual(identityMatrix);
expect(scaleMatrix).toEqual(identityMatrix);
expect(rotationMatrix).toEqual(identityMatrix);
expect(skewMatrix).toEqual(identityMatrix);
});
it('Decompose arbitrary matrix ', () => {
const m2: AffineMatrixFlat = [
1, 2, 3, 0, 1, 1, 1, 0, 1, 2, 0, 0, 4, 5, 6, 1,
];
const { translationMatrix, scaleMatrix, rotationMatrix, skewMatrix } =
decomposeMatrix(m2);
flatten(
multiplyMatrices(
multiplyMatrices(
scaleMatrix,
multiplyMatrices(skewMatrix, rotationMatrix)
),
translationMatrix
)
).forEach((v, i) => expect(v).toBeCloseTo(m2[i]));
});
});
});