Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
MathMatrixTests.cpp
Go to the documentation of this file.
2
3// make sure mat4 test include separe floating point and integer calls
4// floating point mat4 uses SSE for performance reasons and results might differ
5namespace Divide
6{
7
8TEST_CASE( "Mat Size Tests", "[math_matrix_test]" )
9{
10 const mat2<I8> a1;
11 const mat2<U8> a2;
12 const mat2<I16> a3;
13 const mat2<U16> a4;
14 const mat2<I32> a5;
15 const mat2<U32> a6;
16 const mat2<I64> a7;
17 const mat2<U64> a8;
18 const mat2<F32> a9;
19 const mat2<D64> a10;
20
21 CHECK_EQUAL(sizeof(a5), (32 * 2 * 2) / 8);
22
23 CHECK_EQUAL(sizeof(a7), sizeof(vec2<I64>) * 2);
24
25 CHECK_EQUAL(sizeof(a1), sizeof(I8) * 2 * 2);
26 CHECK_EQUAL(sizeof(a2), sizeof(U8) * 2 * 2);
27 CHECK_EQUAL(sizeof(a3), sizeof(I16) * 2 * 2);
28 CHECK_EQUAL(sizeof(a4), sizeof(U16) * 2 * 2);
29 CHECK_EQUAL(sizeof(a5), sizeof(I32) * 2 * 2);
30 CHECK_EQUAL(sizeof(a6), sizeof(U32) * 2 * 2);
31 CHECK_EQUAL(sizeof(a7), sizeof(I64) * 2 * 2);
32 CHECK_EQUAL(sizeof(a8), sizeof(U64) * 2 * 2);
33 CHECK_EQUAL(sizeof(a9), sizeof(F32) * 2 * 2);
34 CHECK_EQUAL(sizeof(a10), sizeof(D64) * 2 * 2);
35 CHECK_EQUAL(sizeof(a10), sizeof(a9) * 2);
36
37 const mat3<I8> b1;
38 const mat3<U8> b2;
39 const mat3<I16> b3;
40 const mat3<U16> b4;
41 const mat3<I32> b5;
42 const mat3<U32> b6;
43 const mat3<I64> b7;
44 const mat3<U64> b8;
45 const mat3<F32> b9;
46 const mat3<D64> b10;
47
48 CHECK_EQUAL(sizeof(b5), (32 * 3 * 3) / 8);
49
50 CHECK_EQUAL(sizeof(b7), sizeof(vec3<I64>) * 3);
51
52 CHECK_EQUAL(sizeof(b1), sizeof(I8) * 3 * 3);
53 CHECK_EQUAL(sizeof(b2), sizeof(U8) * 3 * 3);
54 CHECK_EQUAL(sizeof(b3), sizeof(I16) * 3 * 3);
55 CHECK_EQUAL(sizeof(b4), sizeof(U16) * 3 * 3);
56 CHECK_EQUAL(sizeof(b5), sizeof(I32) * 3 * 3);
57 CHECK_EQUAL(sizeof(b6), sizeof(U32) * 3 * 3);
58 CHECK_EQUAL(sizeof(b7), sizeof(I64) * 3 * 3);
59 CHECK_EQUAL(sizeof(b8), sizeof(U64) * 3 * 3);
60 CHECK_EQUAL(sizeof(b9), sizeof(F32) * 3 * 3);
61 CHECK_EQUAL(sizeof(b10), sizeof(D64) * 3 * 3);
62 CHECK_EQUAL(sizeof(b10), sizeof(b9) * 2);
63
64 const mat4<I8> c1;
65 const mat4<U8> c2;
66 const mat4<I16> c3;
67 const mat4<U16> c4;
68 const mat4<I32> c5;
69 const mat4<U32> c6;
70 const mat4<I64> c7;
71 const mat4<U64> c8;
72 const mat4<F32> c9;
73 const mat4<D64> c10;
74
75 CHECK_EQUAL(sizeof(c5), (32 * 4 * 4) / 8);
76
77 CHECK_EQUAL(sizeof(c7), sizeof(vec4<I64>) * 4);
78 CHECK_EQUAL(sizeof(c9), sizeof(vec4<F32>) * 4);
79 CHECK_EQUAL(sizeof(c3), sizeof(a3) * 4);
80
81 CHECK_EQUAL(sizeof(c1), sizeof(I8) * 4 * 4);
82 CHECK_EQUAL(sizeof(c2), sizeof(U8) * 4 * 4);
83 CHECK_EQUAL(sizeof(c3), sizeof(I16) * 4 * 4);
84 CHECK_EQUAL(sizeof(c4), sizeof(U16) * 4 * 4);
85 CHECK_EQUAL(sizeof(c5), sizeof(I32) * 4 * 4);
86 CHECK_EQUAL(sizeof(c6), sizeof(U32) * 4 * 4);
87 CHECK_EQUAL(sizeof(c7), sizeof(I64) * 4 * 4);
88 CHECK_EQUAL(sizeof(c8), sizeof(U64) * 4 * 4);
89 CHECK_EQUAL(sizeof(c9), sizeof(F32) * 4 * 4);
90 CHECK_EQUAL(sizeof(c10), sizeof(D64) * 4 * 4);
91 CHECK_EQUAL(sizeof(b10), sizeof(b9) * 2);
92}
93
94TEST_CASE( "Mat Union Tests", "[math_matrix_test]" )
95{
96 mat2<I32> input1;
97 mat3<U8> input2;
98 mat4<I16> input3;
99 mat4<F32> input4;
100
101 // Random, unique values
102 input1.set(-1, 0, 1, 2);
103 input2.set(1, 2, 3, 4, 5, 6, 7, 8, 9);
104
105 input3.set({ -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8 });
106 for (U8 i = 0; i < 16; ++i) {
107 input4.mat[i] = 22.345f * (i + 1);
108 }
109
110 // Quick constructor check
111 CHECK_EQUAL(input2.element(1, 2), 6);
112 // Check that element is just a direct access to m-member
113 CHECK_EQUAL(input1.element(1, 0), input1.m[1][0]);
114 CHECK_EQUAL(input2.element(2, 1), input2.m[2][1]);
115 CHECK_EQUAL(input3.element(3, 2), input3.m[3][2]);
116 CHECK_TRUE(COMPARE(input4.element(2, 2), input4.m[2][2]));
117
118 U8 row = 0u, column = 0u;
119 U8 elementsPerLine = 2;
120 for (row = 0; row < elementsPerLine; ++row) {
121 for (column = 0; column < elementsPerLine; ++column) {
122 CHECK_EQUAL(input1.element(row, column), input1.mat[row * elementsPerLine + column]);
123 }
124 }
125 elementsPerLine = 3;
126 for (row = 0; row < elementsPerLine; ++row) {
127 for (column = 0; column < elementsPerLine; ++column) {
128 CHECK_EQUAL(input2.element(row, column), input2.mat[row * elementsPerLine + column]);
129 }
130 }
131 elementsPerLine = 4;
132 for (row = 0; row < elementsPerLine; ++row) {
133 for (column = 0; column < elementsPerLine; ++column) {
134 CHECK_EQUAL(input3.element(row, column), input3.mat[row * elementsPerLine + column]);
135 }
136 }
137
138 for (row = 0; row < elementsPerLine; ++row) {
139 for (column = 0; column < elementsPerLine; ++column) {
140 CHECK_TRUE(COMPARE(input4.element(row, column), input4.mat[row * elementsPerLine + column]));
141 }
142 }
143}
144
145TEST_CASE( "Mat getCol Tests", "[math_matrix_test]" )
146{
147 const mat2<I32> input1(4, 4,
148 10, 8);
149 const mat3<I32> input2(1, 2, 3,
150 4, 5, 6,
151 7, 8, 9);
152 const mat4<F32> input3({1.0f, 2.0f, 3.0f, 4.0f,
153 5.0f, 6.0f, 7.0f, 8.0f,
154 9.0f, 10.0f, 11.0f, 12.0f,
155 13.0f, 14.0f, 15.0f, 16.0f});
156
157 const vec2<I32> result1A(4, 10);
158 const vec2<I32> result1B(4, 8);
159
160 const vec3<I32> result2A(1, 4, 7);
161 const vec3<I32> result2B(2, 5, 8);
162 const vec3<I32> result2C(3, 6, 9);
163
164 const vec4<F32> result3A(1.0f, 5.0f, 9.0f, 13.0f);
165 const vec4<F32> result3B(2.0f, 6.0f, 10.0f, 14.0f);
166 const vec4<F32> result3C(3.0f, 7.0f, 11.0f, 15.0f);
167 const vec4<F32> result3D(4.0f, 8.0f, 12.0f, 16.0f);
168
169 CHECK_EQUAL(input1.getCol(0), result1A);
170 CHECK_EQUAL(input1.getCol(1), result1B);
171
172 CHECK_EQUAL(input2.getCol(0), result2A);
173 CHECK_EQUAL(input2.getCol(1), result2B);
174 CHECK_EQUAL(input2.getCol(2), result2C);
175
176 CHECK_EQUAL(input3.getCol(0), result3A);
177 CHECK_EQUAL(input3.getCol(1), result3B);
178 CHECK_EQUAL(input3.getCol(2), result3C);
179 CHECK_EQUAL(input3.getCol(3), result3D);
180
181}
182
183TEST_CASE( "Mat getRow Tests", "[math_matrix_test]" )
184{
185 const mat2<I32> input1(4, 4,
186 10, 8);
187 const mat3<I32> input2(1, 2, 3,
188 4, 5, 6,
189 7, 8, 9);
190 const mat4<F32> input3({1.0f, 2.0f, 3.0f, 4.0f,
191 5.0f, 6.0f, 7.0f, 8.0f,
192 9.0f, 10.0f, 11.0f, 12.0f,
193 13.0f, 14.0f, 15.0f, 16.0f});
194
195
196 const vec2<I32> result1A(4, 4);
197 const vec2<I32> result1B(10, 8);
198
199 const vec3<I32> result2A(1, 2, 3);
200 const vec3<I32> result2B(4, 5, 6);
201 const vec3<I32> result2C(7, 8, 9);
202
203 const vec4<F32> result3A(1.0f, 2.0f, 3.0f, 4.0f);
204 const vec4<F32> result3B(5.0f, 6.0f, 7.0f, 8.0f);
205 const vec4<F32> result3C(9.0f, 10.0f, 11.0f, 12.0f);
206 const vec4<F32> result3D(13.0f, 14.0f, 15.0f, 16.0f);
207
208 CHECK_EQUAL(input1.getRow(0), result1A);
209 CHECK_EQUAL(input1.getRow(1), result1B);
210
211 CHECK_EQUAL(input2.getRow(0), result2A);
212 CHECK_EQUAL(input2.getRow(1), result2B);
213 CHECK_EQUAL(input2.getRow(2), result2C);
214
215 CHECK_EQUAL(input3.getRow(0), result3A);
216 CHECK_EQUAL(input3.getRow(1), result3B);
217 CHECK_EQUAL(input3.getRow(2), result3C);
218 CHECK_EQUAL(input3.getRow(3), result3D);
219}
220
221TEST_CASE( "Mat equalityOperator Tests", "[math_matrix_test]" )
222{
223 mat2<I32> input1[2];
224 mat3<U8> input2[2];
225 mat4<I16> input3[2];
226 mat4<F32> input4[2];
227
228 // Random, unique values
229 input1[0].set(-1, 0, 1, 2);
230 input1[1].set(-1, 0, 1, 2);
231 CHECK_TRUE(input1[0] == input1[1]);
232 CHECK_FALSE(input1[0] != input1[1]);
233 input1[1].mat[2] = 2;
234 CHECK_TRUE(input1[0] != input1[1]);
235 CHECK_FALSE(input1[0] == input1[1]);
236
237 input2[0].set(1, 2, 3, 4, 5, 6, 7, 8, 9);
238 input2[1].set(1, 2, 3, 4, 5, 6, 7, 8, 9);
239
240 CHECK_TRUE(input2[0] == input2[1]);
241 CHECK_FALSE(input2[0] != input2[1]);
242 input2[1].m[2][0] = 22;
243 CHECK_TRUE(input2[0] != input2[1]);
244 CHECK_FALSE(input2[0] == input2[1]);
245
246 input3[0].set({ -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8 });
247 input3[1].set({ -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8 });
248 CHECK_TRUE(input3[0] == input3[1]);
249 CHECK_FALSE(input3[0] != input3[1]);
250 input3[1].m[1][3] = 22;
251 CHECK_TRUE(input3[0] != input3[1]);
252 CHECK_FALSE(input3[0] == input3[1]);
253
254 for (U8 i = 0; i < 2; ++i) {
255 for (U8 j = 0; j < 16; ++j) {
256 input4[i].mat[j] = 22.345f * j * (j % 2 == 0 ? -1.0f : 1.0f);
257 }
258 }
259 CHECK_TRUE(input4[0] == input4[1]);
260 CHECK_FALSE(input4[0] != input4[1]);
261
262 for (U8 i = 0; i < 2; ++i) {
263 for (U8 j = 0; j < 16; ++j) {
264 input4[i].mat[j] = 22.345f * (j + i + 1);
265 }
266 }
267
268 CHECK_TRUE(input4[0] != input4[1]);
269 CHECK_FALSE(input4[0] == input4[1]);
270}
271
272TEST_CASE( "Mat Identity Tests", "[math_matrix_test]" )
273{
274 mat2<F32> input1; input1.zero();
275 const mat2<F32> result1(1.0f, 0.0f,
276 0.0f, 1.0f);
277 CHECK_NOT_EQUAL(result1, input1);
278 input1.identity();
279 CHECK_TRUE(result1 == input1);
280
281 mat3<F32> input2; input2.zero();
282 const mat3<F32> result2(1.0f, 0.0f, 0.0f,
283 0.0f, 1.0f, 0.0f,
284 0.0f, 0.0f, 1.0f);
285
286 CHECK_NOT_EQUAL(result2, input2);
287 input2.identity();
288 CHECK_EQUAL(result2, input2);
289
290 mat4<F32> input3; input3.zero();
291 const mat4<F32> result3({1.0f, 0.0f, 0.0f, 0.0f,
292 0.0f, 1.0f, 0.0f, 0.0f,
293 0.0f, 0.0f, 1.0f, 0.0f,
294 0.0f, 0.0f, 0.0f, 1.0f});
295 CHECK_NOT_EQUAL(result3, input3);
296 input3.identity();
297 CHECK_EQUAL(result3, input3);
298}
299
300TEST_CASE( "Mat Transpose Tests", "[math_matrix_test]" )
301{
302 mat2<F32> input1(1.0f, 2.0f, 3.0f, 4.0f);
303 const mat2<F32> result1(1.0f, 3.0f, 2.0f, 4.0f);
304 const mat2<F32> result2(1.0f, 2.0f, 3.0f, 4.0f);
305 CHECK_TRUE(input1.getTranspose() == result1);
306 CHECK_TRUE(input1 != result1);
307 input1.transpose();
308 CHECK_TRUE(input1 == result1);
309 input1.transpose();
310 CHECK_TRUE(input1 == result2);
311
312
313 mat3<I32> input2(1, 2, 3, 4, 5, 6, 7, 8, 9);
314 const mat3<I32> result3(1, 4, 7, 2, 5, 8, 3, 6, 9);
315 const mat3<I32> result4(1, 2, 3, 4, 5, 6, 7, 8, 9);
316 CHECK_TRUE(input2.getTranspose() == result3);
317 CHECK_TRUE(input2 != result3);
318 input2.transpose();
319 CHECK_TRUE(input2 == result3);
320 input2.transpose();
321 CHECK_TRUE(input2 == result4);
322
323
324 mat4<F32> input3({1.0f, 2.0f, 3.0f, 4.0f,
325 5.0f, 6.0f, 7.0f, 8.0f,
326 9.0f, 10.0f, 11.0f, 12.0f,
327 13.0f, 14.0f, 15.0f, 16.0f});
328 const mat4<F32> result5({1.0f, 5.0f, 9.0f, 13.0f,
329 2.0f, 6.0f, 10.0f, 14.0f,
330 3.0f, 7.0f, 11.0f, 15.0f,
331 4.0f, 8.0f, 12.0f, 16.0f});
332 const mat4<F32> result6({1.0f, 2.0f, 3.0f, 4.0f,
333 5.0f, 6.0f, 7.0f, 8.0f,
334 9.0f, 10.0f, 11.0f, 12.0f,
335 13.0f, 14.0f, 15.0f, 16.0f});
336 CHECK_TRUE(input3.getTranspose() == result5);
337 CHECK_TRUE(input3 != result5);
338 input3.transpose();
339 CHECK_TRUE(input3 == result5);
340 input3.transpose();
341 CHECK_TRUE(input3 == result6);
342}
343
344TEST_CASE( "Mat-Scalar Multiply Tests", "[math_matrix_test]" )
345{
346 const mat2<I32> input1(-2, -1,
347 1, 2);
348
349 const mat2<I32> result1(-4, -2,
350 2, 4);
351
352 CHECK_FALSE(input1 == result1);
353 CHECK_TRUE((input1 * 2) == result1);
354
355
356 const mat3<F32> input2(-5.0f, -4.0f, -3.0f,
357 -2.0f, -1.0f, 1.0f,
358 2.0f, 3.0f, 4.0f);
359 const mat3<F32> result2( 2.5f, 2.0f, 1.5f,
360 1.0f, 0.5f, -0.5f,
361 -1.0f, -1.5f, -2.0f);
362
363 CHECK_FALSE(input2 == result2);
364 CHECK_TRUE((input2 * (-0.5f)) == result2);
365
366
367 const mat4<I32> input3({1, 2, 3, 4,
368 5, 6, 7, 8,
369 9, 8, 7, 6,
370 5, 4, 3, 2});
371 const mat4<I32> result3({ 3, 6, 9, 12,
372 15, 18, 21, 24,
373 27, 24, 21, 18,
374 15, 12, 9, 6});
375
376 CHECK_FALSE(input3 == result3);
377 CHECK_TRUE((input3 * 3) == result3);
378
379 const mat4<F32> input4({1.0f, 2.0f, 3.0f, 4.0f,
380 5.0f, 6.0f, 7.0f, 8.0f,
381 9.0f, 8.0f, 7.0f, 6.0f,
382 5.0f, 4.0f, 3.0f, 2.0f});
383
384 const mat4<F32> result4A({ 3.0f, 6.0f, 9.0f, 12.0f,
385 15.0f, 18.0f, 21.0f, 24.0f,
386 27.0f, 24.0f, 21.0f, 18.0f,
387 15.0f, 12.0f, 9.0f, 6.0f});
388
389 const mat4<F32> result4B({ 0.5f, 1.0f, 1.5f, 2.0f,
390 2.5f, 3.0f, 3.5f, 4.0f,
391 4.5f, 4.0f, 3.5f, 3.0f,
392 2.5f, 2.0f, 1.5f, 1.0f });
393
394 CHECK_FALSE(input4 == result4A);
395
396 const mat4<F32> something(input4);
397 CHECK_EQUAL(something, input4);
398 CHECK_EQUAL((something * 3), result4A);
399 CHECK_EQUAL((something / 2), result4B);
400}
401
402TEST_CASE( "Mat-Scalar Add-Subtract Tests", "[math_matrix_test]" )
403{
404 const mat4<F32> input({1.0f, 2.0f, 3.0f, 4.0f,
405 5.0f, 6.0f, 7.0f, 8.0f,
406 9.0f, 8.0f, 7.0f, 6.0f,
407 5.0f, 4.0f, 3.0f, 2.0f});
408
409 const mat4<F32> resultA({-2.0f, -1.0f, 0.0f, 1.0f,
410 2.0f, 3.0f, 4.0f, 5.0f,
411 6.0f, 5.0f, 4.0f, 3.0f,
412 2.0f, 1.0f, 0.0f, -1.0f});
413
414 const mat4<F32> resultB({ 4.0f, 5.0f, 6.0f, 7.0f,
415 8.0f, 9.0f, 10.0f, 11.0f,
416 12.0f, 11.0f, 10.0f, 9.0f,
417 8.0f, 7.0f, 6.0f, 5.0f });
418
419 CHECK_TRUE((input - 3) == resultA);
420 CHECK_TRUE((input + 3) == resultB);
421}
422
423// this depends on multiply tests!
424TEST_CASE( "Mat-Scalar Division Tests", "[math_matrix_test]" )
425{
426 const mat2<I32> input1(-2, -2,
427 2, 2);
428 CHECK_FALSE((input1 / 2) == (input1 * 2));
429 CHECK_TRUE((input1 / 2) == (input1 * 0.5f));
430
431 const mat3<F32> input2(-5.0f, -4.0f, -3.0f,
432 -2.0f, -1.0f, 1.0f,
433 2.0f, 3.0f, 4.0f);
434 CHECK_FALSE((input2 / 2) == (input2 * 2));
435 CHECK_TRUE((input2 / 2) == (input2 * 0.5f));
436
437 const mat4<I32> input3({1, 2, 3, 4,
438 5, 6, 7, 8,
439 9, 8, 7, 6,
440 5, 4, 3, 2});
441 CHECK_FALSE((input3 / 2) == (input3 * 2));
442
443 CHECK_TRUE((input3 / 2) == (input3 * 0.5f));
444
445 const mat4<F32> input4({1.0f, 2.0f, 3.0f, 4.0f,
446 5.0f, 6.0f, 7.0f, 8.0f,
447 9.0f, 8.0f, 7.0f, 6.0f,
448 5.0f, 4.0f, 3.0f, 2.0f});
449 CHECK_FALSE((input4 / 2) == (input4 * 2));
450 CHECK_EQUAL((input4 / 2), (input4 * 0.5f));
451}
452
453TEST_CASE( "Mat-Plane Reflect Tests", "[math_matrix_test]" )
454{
455 const vec3<F32> input(2, 2, 2);
456 const vec3<F32> result(2, -2, 2);
457
458 const Plane<F32> reflectPlane1(vec3<F32>(0.0f, 1.0f, 0.0f), 0.0f);
459 const Plane<F32> reflectPlane2(vec3<F32>(0.0f, -1.0f, 0.0f), 0.0f);
460
461 mat4<F32> reflectMat;
462 reflectMat.reflect(reflectPlane1);
463
464 CHECK_TRUE(result == reflectMat * input);
465
466 reflectMat.reflect(reflectPlane2);
467
468 CHECK_TRUE(input == reflectMat * input);
469}
470
471TEST_CASE( "Mat-Mat Multiply Tests", "[math_matrix_test]" )
472{
473 mat2<I32> inputIdentity2x2;
474 inputIdentity2x2.identity();
475
476 const mat2<I32> input1A(1, 2,
477 3, 4);
478 const mat2<I32> input1B(2, 0,
479 1, 2);
480 const mat2<I32> result1A( 4, 4,
481 10, 8);
482 const mat2<I32> result1B(2, 4,
483 7, 10);
484 CHECK_EQUAL(input1A * input1B, result1A);
485 CHECK_NOT_EQUAL(input1B * input1A, result1A);
486
487 CHECK_NOT_EQUAL(input1A * input1B, result1B);
488 CHECK_EQUAL(input1B * input1A, result1B);
489
490 CHECK_EQUAL(input1A * inputIdentity2x2, input1A);
491
492
493 mat3<I32> inputIdentity3x3;
494 inputIdentity3x3.identity();
495
496 const mat3<F32> input2A( 5.0f, 2.0f, 6.0f,
497 23.0f, 29.0f, 11.0f,
498 64.0f, 0.0f, 32.0f);
499
500 const mat3<F32> input2B(24.0f, 92.0f, 112.0f,
501 5.0f, 0.0f, 95.0f,
502 43.0f, 2.0f, 9.0f);
503
504 const mat3<F32> result2A( 388.0f, 472.0f, 804.0f,
505 1170.0f, 2138.0f, 5430.0f,
506 2912.0f, 5952.0f, 7456.0f);
507
508 const mat3<F32> result2B(9404.0f, 2716.0f, 4740.0f,
509 6105.0f, 10.0f, 3070.0f,
510 837.0f, 144.0f, 568.0f);
511
512 CHECK_EQUAL(input2A * input2B, result2A);
513 CHECK_NOT_EQUAL(input2B * input2A, result2A);
514
515 CHECK_NOT_EQUAL(input2A * input2B, result2B);
516 CHECK_EQUAL(input2B * input2A, result2B);
517
518 CHECK_EQUAL(input2A * inputIdentity3x3, input2A);
519
520
521 mat4<I32> inputIdentity4x4;
522 inputIdentity4x4.identity();
523
524 const mat4<I32> input3A({ 5, 2, 6, 8,
525 23, 29, 11, 5,
526 64, 0, 32, 8,
527 -2, 5, 7, 1 });
528
529 const mat4<I32> input3B({24, 92, 112, 4,
530 5, 0, 95, 2,
531 43, 2, 9, 9,
532 5, 7, 11, 67});
533
534 const mat4<I32> result3A({ 428, 528, 892, 614,
535 1195, 2173, 5485, 584,
536 2952, 6008, 7544, 1080,
537 283, -163, 325, 132 });
538
539 const mat4<I32> result3B({9396, 2736, 4768, 1552,
540 6101, 20, 3084, 802,
541 819, 189, 631, 435,
542 756, 548, 928, 230});
543
544 CHECK_EQUAL(input3A * input3B, result3A);
545 CHECK_NOT_EQUAL(input3B * input3A, result3A);
546
547 CHECK_NOT_EQUAL(input3A * input3B, result3B);
548 CHECK_EQUAL(input3B * input3A, result3B);
549
550 CHECK_EQUAL(input3A * inputIdentity4x4, input3A);
551
552
553 mat4<F32> inputIdentity4x4f;
554 inputIdentity4x4f.identity();
555
556 const mat4<F32> input4A({24.300f, 92.000f, 1.200f, 4.300f,
557 5.000f, 0.000f, 95.500f, 0.200f,
558 43.000f, 2.110f, 9.000f, 9.200f,
559 5.000f, 7.000f, 11.000f, 6.435f});
560
561 const mat4<F32> input4B({ 12.500f, 2.000f, 6.000f, 8.400f,
562 23.000f, 29.340f, 11.000f, 5.120f,
563 -64.500f, 22.000f, 3.200f, 8.000f,
564 -2.000f, 5.000f, 7.000f, 1.000f });
565
566 const mat4<F32> result4A({ 2333.750f, 2795.780f, 1191.740f, 689.060f,
567 -6097.650f, 2112.000f, 337.000f, 806.200f,
568 -12.870f, 391.907f, 374.410f, 453.203f,
569 -498.869f, 489.554f, 187.243f, 172.275f });
570
571 const mat4<F32> result4B({ 613.750f, 1221.460f, 352.400f, 163.401f,
572 1204.200f, 2175.050f, 2984.890f, 238.914f,
573 -1279.750f, -5871.248f, 2140.400f, -192.032f,
574 282.400f, -162.230f, 549.100f, 63.235f});
575
576 // Use compare instead of == as rounding errors bellow a certain threshold aren't as important with floating point matrices
577 CHECK_TRUE(result4A.compare(input4A * input4B, 0.02f));
578 CHECK_FALSE(result4A.compare(input4B * input4A, 0.02f));
579
580 CHECK_FALSE(result4B.compare(input4A * input4B, 0.02f));
581 CHECK_TRUE(result4B.compare(input4B * input4A, 0.02f));
582
583 CHECK_TRUE(input4A.compare(input4A * inputIdentity4x4f, 0.02f));
584}
585
586TEST_CASE( "Mat Construct Tests", "[math_matrix_test]" )
587{
588 const mat4<F32> input4A({ 24.300f, 92.000f, 1.200f, 4.300f,
589 5.000f, 0.000f, 95.500f, 0.200f,
590 43.000f, 2.110f, 9.000f, 9.200f,
591 5.000f, 7.000f, 11.000f, 6.435f });
592
593 const mat4<F32> resultA(&input4A.mat[0]);
594 const mat4<F32> resultB({input4A.mat[0], input4A.mat[1], input4A.mat[2], input4A.mat[3],
595 input4A.mat[4], input4A.mat[5], input4A.mat[6], input4A.mat[7],
596 input4A.mat[8], input4A.mat[9], input4A.mat[10], input4A.mat[11],
597 input4A.mat[12], input4A.mat[13], input4A.mat[14], input4A.mat[15]});
598
599 CHECK_EQUAL(input4A, resultA);
600 CHECK_EQUAL(input4A, resultB);
601}
602
603TEST_CASE( "Mat Inverse Tests", "[math_matrix_test]" )
604{
605 //Floating point 2x2 inversion
606 mat2<F32> input2x2(4.0f, 3.0f,
607 3.0f, 2.0f);
608 const mat2<F32> rezult2x2(-2.0f, 3.0f,
609 3.0f, -4.0f);
610 input2x2.inverse();
611 CHECK_EQUAL(input2x2, rezult2x2);
612
613 //Floating point 3x3 inversion
614 mat3<F32> input3x3(2.0f, 1.0f, 0.0f,
615 0.0f, 2.0f, 0.0f,
616 2.0f, 0.0f, 1.0f);
617 const mat3<F32> rezult3x3( 0.5f, -0.25f, 0.0f,
618 0.0f, 0.5f, 0.0f,
619 -1.0f, 0.5f, 1.0f);
620 input3x3.inverse();
621 CHECK_EQUAL(input3x3, rezult3x3);
622
623 //Zero determinate inversion (F32 version is undefined)
624 mat4<I32> zeroInput(0.0f);
625 const mat4<I32> zeroResult(zeroInput);
626 zeroInput.inverse();
627
628 CHECK_EQUAL(zeroInput, zeroResult);
629
630 //Floating point 4x4 inversion
631 mat4<F32> input1({1.0f, 1.0f, 0.0f, 0.0f,
632 1.0f, 1.0f, 1.0f, 0.0f,
633 0.0f, 1.0f, 1.0f, 0.0f,
634 0.0f, 0.0f, 0.0f, 1.0f});
635 const mat4<F32> result1({ 0.0f, 1.0f, -1.0f, 0.0f,
636 1.0f, -1.0f, 1.0f, 0.0f,
637 -1.0f, 1.0f, 0.0f, 0.0f,
638 0.0f, 0.0f, 0.0f, 1.0f});
639
640 input1.inverse();
641 CHECK_EQUAL(input1, result1);
642
643
644 //Integer 4x4 inversion
645 mat4<I32> input2({ 1, 1, 0, 0,
646 1, 1, 1, 0,
647 0, 1, 1, 0,
648 0, 0, 0, 1 });
649 const mat4<I32> result2({ 0, 1, -1, 0,
650 1, -1, 1, 0,
651 -1, 1, 0, 0,
652 0, 0, 0, 1 });
653 input2.inverse();
654 CHECK_EQUAL(input2, result2);
655}
656
657} //namespace Divide
void set(U m0, U m1, U m2, U m3) noexcept
vec2< T > getRow(I32 index) const noexcept
void identity() noexcept
vec2< T > getCol(I32 index) const noexcept
void inverse() noexcept
mat2 getTranspose() const noexcept
void transpose() noexcept
void zero() noexcept
T & element(U8 row, U8 column) noexcept
mat3 getTranspose() const noexcept
const vec3< T > & getRow(I32 index) const noexcept
vec3< T > getCol(I32 index) const noexcept
void inverse() noexcept
T & element(U8 row, U8 column) noexcept
void identity() noexcept
void set(U m0, U m1, U m2, U m3, U m4, U m5, U m6, U m7, U m8) noexcept
void zero() noexcept
void transpose() noexcept
T & element(U8 row, U8 column) noexcept
void zero() noexcept
void inverse() noexcept
const mat4 & reflect(U x, U y, U z, U w) noexcept
void identity() noexcept
void set(std::initializer_list< T > matrix) noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
int32_t I32
uint8_t U8
int16_t I16
uint16_t U16
double D64
bool COMPARE(T X, U Y) noexcept
TEST_CASE("ByteBuffer RW Bool", "[byte_buffer]")
int64_t I64
uint32_t U32
uint64_t U64
#define CHECK_NOT_EQUAL(LHS, RHS)
#define CHECK_EQUAL(LHS, RHS)
#define CHECK_TRUE(...)