blob: e210fb74105f868a196a2661670e3c748d7a52c2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.com7d683352012-12-03 21:19:52 +00007
reed@google.com125002a2011-06-09 19:13:41 +00008#include "Test.h"
9#include "SkMatrix44.h"
10
vollick@chromium.org3959a762012-11-13 15:08:22 +000011static bool nearly_equal_double(double a, double b) {
12 const double tolerance = 1e-7;
13 double diff = a - b;
14 if (diff < 0)
15 diff = -diff;
16 return diff <= tolerance;
17}
18
reed@google.comda9fac02011-06-13 14:46:52 +000019static bool nearly_equal_scalar(SkMScalar a, SkMScalar b) {
reed@google.com125002a2011-06-09 19:13:41 +000020 // Note that we get more compounded error for multiple operations when
21 // SK_SCALAR_IS_FIXED.
22#ifdef SK_SCALAR_IS_FLOAT
23 const SkScalar tolerance = SK_Scalar1 / 200000;
24#else
25 const SkScalar tolerance = SK_Scalar1 / 1024;
26#endif
27
reed@google.com2b57dc62013-01-08 13:23:32 +000028 return SkTAbs<SkMScalar>(a - b) <= tolerance;
reed@google.com125002a2011-06-09 19:13:41 +000029}
30
reed@google.comda9fac02011-06-13 14:46:52 +000031template <typename T> void assert16(skiatest::Reporter* reporter, const T data[],
32 T m0, T m1, T m2, T m3,
33 T m4, T m5, T m6, T m7,
34 T m8, T m9, T m10, T m11,
35 T m12, T m13, T m14, T m15) {
36 REPORTER_ASSERT(reporter, data[0] == m0);
37 REPORTER_ASSERT(reporter, data[1] == m1);
38 REPORTER_ASSERT(reporter, data[2] == m2);
39 REPORTER_ASSERT(reporter, data[3] == m3);
40
41 REPORTER_ASSERT(reporter, data[4] == m4);
42 REPORTER_ASSERT(reporter, data[5] == m5);
43 REPORTER_ASSERT(reporter, data[6] == m6);
44 REPORTER_ASSERT(reporter, data[7] == m7);
45
46 REPORTER_ASSERT(reporter, data[8] == m8);
47 REPORTER_ASSERT(reporter, data[9] == m9);
48 REPORTER_ASSERT(reporter, data[10] == m10);
49 REPORTER_ASSERT(reporter, data[11] == m11);
50
51 REPORTER_ASSERT(reporter, data[12] == m12);
52 REPORTER_ASSERT(reporter, data[13] == m13);
53 REPORTER_ASSERT(reporter, data[14] == m14);
54 REPORTER_ASSERT(reporter, data[15] == m15);
55}
56
reed@google.com125002a2011-06-09 19:13:41 +000057static bool nearly_equal(const SkMatrix44& a, const SkMatrix44& b) {
58 for (int i = 0; i < 4; ++i) {
59 for (int j = 0; j < 4; ++j) {
60 if (!nearly_equal_scalar(a.get(i, j), b.get(i, j))) {
reed@google.comda9fac02011-06-13 14:46:52 +000061 printf("not equal %g %g\n", a.get(i, j), b.get(i, j));
reed@google.com125002a2011-06-09 19:13:41 +000062 return false;
63 }
64 }
65 }
66 return true;
67}
68
69static bool is_identity(const SkMatrix44& m) {
70 SkMatrix44 identity;
71 identity.reset();
72 return nearly_equal(m, identity);
73}
74
reed@google.com99b5c7f2012-12-05 22:13:59 +000075///////////////////////////////////////////////////////////////////////////////
76static bool bits_isonly(int value, int mask) {
77 return 0 == (value & ~mask);
78}
79
vollick@chromium.org57a54e32012-12-10 20:16:10 +000080static void test_constructor(skiatest::Reporter* reporter) {
81 // Allocate a matrix on the heap
82 SkMatrix44* placeholderMatrix = new SkMatrix44();
robertphillips@google.com35300c42013-03-21 17:38:49 +000083 SkAutoTDelete<SkMatrix44> deleteMe(placeholderMatrix);
84
vollick@chromium.org57a54e32012-12-10 20:16:10 +000085 for (int row = 0; row < 4; ++row) {
86 for (int col = 0; col < 4; ++col) {
87 placeholderMatrix->setDouble(row, col, row * col);
88 }
89 }
90
91 // Use placement-new syntax to trigger the constructor on top of the heap
92 // address we already initialized. This allows us to check that the
93 // constructor did avoid initializing the matrix contents.
94 SkMatrix44* testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kUninitialized_Constructor);
95 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix);
96 REPORTER_ASSERT(reporter, !testMatrix->isIdentity());
97 for (int row = 0; row < 4; ++row) {
98 for (int col = 0; col < 4; ++col) {
99 REPORTER_ASSERT(reporter, nearly_equal_double(row * col, testMatrix->getDouble(row, col)));
100 }
101 }
102
103 // Verify that kIdentity_Constructor really does initialize to an identity matrix.
104 testMatrix = 0;
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000105 testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kIdentity_Constructor);
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000106 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix);
107 REPORTER_ASSERT(reporter, testMatrix->isIdentity());
108 REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I());
109}
110
reed@google.com99b5c7f2012-12-05 22:13:59 +0000111static void test_translate(skiatest::Reporter* reporter) {
112 SkMatrix44 mat, inverse;
113
114 mat.setTranslate(0, 0, 0);
115 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
116 mat.setTranslate(1, 2, 3);
117 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kTranslate_Mask));
118 REPORTER_ASSERT(reporter, mat.invert(&inverse));
119 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kTranslate_Mask));
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000120
reed@google.com99b5c7f2012-12-05 22:13:59 +0000121 SkMatrix44 a, b, c;
122 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
123 b.setTranslate(10, 11, 12);
124
125 c.setConcat(a, b);
126 mat = a;
127 mat.preTranslate(10, 11, 12);
128 REPORTER_ASSERT(reporter, mat == c);
129
130 c.setConcat(b, a);
131 mat = a;
132 mat.postTranslate(10, 11, 12);
133 REPORTER_ASSERT(reporter, mat == c);
134}
135
136static void test_scale(skiatest::Reporter* reporter) {
137 SkMatrix44 mat, inverse;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000138
reed@google.com99b5c7f2012-12-05 22:13:59 +0000139 mat.setScale(1, 1, 1);
140 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
141 mat.setScale(1, 2, 3);
142 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask));
143 REPORTER_ASSERT(reporter, mat.invert(&inverse));
144 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_Mask));
145
146 SkMatrix44 a, b, c;
147 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
148 b.setScale(10, 11, 12);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000149
reed@google.com99b5c7f2012-12-05 22:13:59 +0000150 c.setConcat(a, b);
151 mat = a;
152 mat.preScale(10, 11, 12);
153 REPORTER_ASSERT(reporter, mat == c);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000154
reed@google.com99b5c7f2012-12-05 22:13:59 +0000155 c.setConcat(b, a);
156 mat = a;
157 mat.postScale(10, 11, 12);
158 REPORTER_ASSERT(reporter, mat == c);
159}
160
161static void make_i(SkMatrix44* mat) { mat->setIdentity(); }
162static void make_t(SkMatrix44* mat) { mat->setTranslate(1, 2, 3); }
163static void make_s(SkMatrix44* mat) { mat->setScale(1, 2, 3); }
164static void make_st(SkMatrix44* mat) {
165 mat->setScale(1, 2, 3);
166 mat->postTranslate(1, 2, 3);
167}
168static void make_a(SkMatrix44* mat) {
169 mat->setRotateDegreesAbout(1, 2, 3, 45);
170}
171static void make_p(SkMatrix44* mat) {
172 SkMScalar data[] = {
173 1, 2, 3, 4, 5, 6, 7, 8,
174 1, 2, 3, 4, 5, 6, 7, 8,
175 };
176 mat->setRowMajor(data);
177}
178
179typedef void (*Make44Proc)(SkMatrix44*);
180
181static const Make44Proc gMakeProcs[] = {
182 make_i, make_t, make_s, make_st, make_a, make_p
183};
184
185static void test_map2(skiatest::Reporter* reporter, const SkMatrix44& mat) {
186 SkMScalar src2[] = { 1, 2 };
187 SkMScalar src4[] = { src2[0], src2[1], 0, 1 };
188 SkMScalar dstA[4], dstB[4];
189
190 for (int i = 0; i < 4; ++i) {
191 dstA[i] = 123456789;
192 dstB[i] = 987654321;
193 }
194
195 mat.map2(src2, 1, dstA);
196 mat.mapMScalars(src4, dstB);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000197
reed@google.com99b5c7f2012-12-05 22:13:59 +0000198 for (int i = 0; i < 4; ++i) {
199 REPORTER_ASSERT(reporter, dstA[i] == dstB[i]);
200 }
201}
202
203static void test_map2(skiatest::Reporter* reporter) {
204 SkMatrix44 mat;
205
206 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) {
207 gMakeProcs[i](&mat);
208 test_map2(reporter, mat);
209 }
210}
211
reed@google.com7d683352012-12-03 21:19:52 +0000212static void test_gettype(skiatest::Reporter* reporter) {
213 SkMatrix44 matrix;
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000214
reed@google.com7d683352012-12-03 21:19:52 +0000215 REPORTER_ASSERT(reporter, matrix.isIdentity());
216 REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType());
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000217
reed@google.com7d683352012-12-03 21:19:52 +0000218 int expectedMask;
219
220 matrix.set(1, 1, 0);
221 expectedMask = SkMatrix44::kScale_Mask;
222 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
223
224 matrix.set(0, 3, 1); // translate-x
225 expectedMask |= SkMatrix44::kTranslate_Mask;
226 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
227
228 matrix.set(2, 0, 1);
229 expectedMask |= SkMatrix44::kAffine_Mask;
230 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000231
reed@google.com7d683352012-12-03 21:19:52 +0000232 matrix.set(3, 2, 1);
233 REPORTER_ASSERT(reporter, matrix.getType() & SkMatrix44::kPerspective_Mask);
reed@google.com87f99cb2013-04-19 12:25:00 +0000234
235 // ensure that negative zero is treated as zero
236 SkMScalar dx = 0;
237 SkMScalar dy = 0;
238 SkMScalar dz = 0;
239 matrix.setTranslate(-dx, -dy, -dz);
240 REPORTER_ASSERT(reporter, matrix.isIdentity());
241 matrix.preTranslate(-dx, -dy, -dz);
242 REPORTER_ASSERT(reporter, matrix.isIdentity());
243 matrix.postTranslate(-dx, -dy, -dz);
244 REPORTER_ASSERT(reporter, matrix.isIdentity());
reed@google.com7d683352012-12-03 21:19:52 +0000245}
246
reed@google.com6f2b44d2011-06-24 18:13:39 +0000247static void test_common_angles(skiatest::Reporter* reporter) {
248 SkMatrix44 rot;
249 // Test precision of rotation in common cases
250 int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 };
251 for (int i = 0; i < 9; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000252 rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i]));
reed@google.com6f2b44d2011-06-24 18:13:39 +0000253
254 SkMatrix rot3x3 = rot;
255 REPORTER_ASSERT(reporter, rot3x3.rectStaysRect());
256 }
257}
258
reed@google.com80b577e2012-11-09 21:25:06 +0000259static void test_concat(skiatest::Reporter* reporter) {
260 int i;
261 SkMatrix44 a, b, c, d;
262
263 a.setTranslate(10, 10, 10);
264 b.setScale(2, 2, 2);
265
266 SkScalar src[8] = {
267 0, 0, 0, 1,
268 1, 1, 1, 1
269 };
270 SkScalar dst[8];
271
272 c.setConcat(a, b);
273
274 d = a;
275 d.preConcat(b);
276 REPORTER_ASSERT(reporter, d == c);
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000277
reed@google.com1ea95be2012-11-09 21:39:48 +0000278 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000279 for (i = 0; i < 3; ++i) {
280 REPORTER_ASSERT(reporter, 10 == dst[i]);
281 REPORTER_ASSERT(reporter, 12 == dst[i + 4]);
282 }
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000283
reed@google.com80b577e2012-11-09 21:25:06 +0000284 c.setConcat(b, a);
285
286 d = a;
287 d.postConcat(b);
288 REPORTER_ASSERT(reporter, d == c);
289
reed@google.com1ea95be2012-11-09 21:39:48 +0000290 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000291 for (i = 0; i < 3; ++i) {
292 REPORTER_ASSERT(reporter, 20 == dst[i]);
293 REPORTER_ASSERT(reporter, 22 == dst[i + 4]);
294 }
295}
296
vollick@chromium.org3959a762012-11-13 15:08:22 +0000297static void test_determinant(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000298 SkMatrix44 a;
299 REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000300 a.set(1, 1, 2);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000301 REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant()));
302 SkMatrix44 b;
303 REPORTER_ASSERT(reporter, a.invert(&b));
304 REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant()));
305 SkMatrix44 c = b = a;
reed@google.com7d683352012-12-03 21:19:52 +0000306 c.set(0, 1, 4);
307 b.set(1, 0, 4);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000308 REPORTER_ASSERT(reporter,
309 nearly_equal_double(a.determinant(),
310 b.determinant()));
311 SkMatrix44 d = a;
reed@google.com7d683352012-12-03 21:19:52 +0000312 d.set(0, 0, 8);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000313 REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000314
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000315 SkMatrix44 e = a;
316 e.postConcat(d);
317 REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000318 e.set(0, 0, 0);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000319 REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000320}
321
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000322static void test_invert(skiatest::Reporter* reporter) {
323 SkMatrix44 inverse;
324 double inverseData[16];
325
326 SkMatrix44 identity;
327 identity.setIdentity();
328 identity.invert(&inverse);
329 inverse.asRowMajord(inverseData);
330 assert16<double>(reporter, inverseData,
331 1, 0, 0, 0,
332 0, 1, 0, 0,
333 0, 0, 1, 0,
334 0, 0, 0, 1);
335
336 SkMatrix44 translation;
337 translation.setTranslate(2, 3, 4);
338 translation.invert(&inverse);
339 inverse.asRowMajord(inverseData);
340 assert16<double>(reporter, inverseData,
341 1, 0, 0, -2,
342 0, 1, 0, -3,
343 0, 0, 1, -4,
344 0, 0, 0, 1);
345
346 SkMatrix44 scale;
347 scale.setScale(2, 4, 8);
348 scale.invert(&inverse);
349 inverse.asRowMajord(inverseData);
350 assert16<double>(reporter, inverseData,
351 0.5, 0, 0, 0,
352 0, 0.25, 0, 0,
353 0, 0, 0.125, 0,
354 0, 0, 0, 1);
355
356 SkMatrix44 scaleTranslation;
357 scaleTranslation.setScale(10, 100, 1000);
358 scaleTranslation.preTranslate(2, 3, 4);
359 scaleTranslation.invert(&inverse);
360 inverse.asRowMajord(inverseData);
361 assert16<double>(reporter, inverseData,
362 0.1, 0, 0, -2,
363 0, 0.01, 0, -3,
364 0, 0, 0.001, -4,
365 0, 0, 0, 1);
366
367 SkMatrix44 rotation;
368 rotation.setRotateDegreesAbout(0, 0, 1, 90);
369 rotation.invert(&inverse);
370 SkMatrix44 expected;
371 double expectedInverseRotation[16] =
372 {0, 1, 0, 0,
373 -1, 0, 0, 0,
374 0, 0, 1, 0,
375 0, 0, 0, 1};
376 expected.setRowMajord(expectedInverseRotation);
377 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
378
379 SkMatrix44 affine;
380 affine.setRotateDegreesAbout(0, 0, 1, 90);
381 affine.preScale(10, 20, 100);
382 affine.preTranslate(2, 3, 4);
383 affine.invert(&inverse);
384 double expectedInverseAffine[16] =
385 {0, 0.1, 0, -2,
386 -0.05, 0, 0, -3,
387 0, 0, 0.01, -4,
388 0, 0, 0, 1};
389 expected.setRowMajord(expectedInverseAffine);
390 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
391
392 SkMatrix44 perspective;
393 perspective.setIdentity();
394 perspective.setDouble(3, 2, 1.0);
395 perspective.invert(&inverse);
396 double expectedInversePerspective[16] =
397 {1, 0, 0, 0,
398 0, 1, 0, 0,
399 0, 0, 1, 0,
400 0, 0, -1, 1};
401 expected.setRowMajord(expectedInversePerspective);
402 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
403
404 SkMatrix44 affineAndPerspective;
405 affineAndPerspective.setIdentity();
406 affineAndPerspective.setDouble(3, 2, 1.0);
407 affineAndPerspective.preScale(10, 20, 100);
408 affineAndPerspective.preTranslate(2, 3, 4);
409 affineAndPerspective.invert(&inverse);
410 double expectedInverseAffineAndPerspective[16] =
411 {0.1, 0, 2, -2,
412 0, 0.05, 3, -3,
413 0, 0, 4.01, -4,
414 0, 0, -1, 1};
415 expected.setRowMajord(expectedInverseAffineAndPerspective);
416 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
417}
418
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000419static void test_transpose(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000420 SkMatrix44 a;
421 SkMatrix44 b;
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000422
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000423 int i = 0;
424 for (int row = 0; row < 4; ++row) {
425 for (int col = 0; col < 4; ++col) {
426 a.setDouble(row, col, i);
427 b.setDouble(col, row, i++);
428 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000429 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000430
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000431 a.transpose();
432 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000433}
434
435static void test_get_set_double(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000436 SkMatrix44 a;
437 for (int row = 0; row < 4; ++row) {
438 for (int col = 0; col < 4; ++col) {
439 a.setDouble(row, col, 3.141592653589793);
440 REPORTER_ASSERT(reporter,
441 nearly_equal_double(3.141592653589793,
442 a.getDouble(row, col)));
443 a.setDouble(row, col, 0);
444 REPORTER_ASSERT(reporter,
445 nearly_equal_double(0, a.getDouble(row, col)));
446 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000447 }
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000448}
449
450static void test_set_row_col_major(skiatest::Reporter* reporter) {
451 SkMatrix44 a, b, c, d;
reed@google.com7d683352012-12-03 21:19:52 +0000452 for (int row = 0; row < 4; ++row) {
453 for (int col = 0; col < 4; ++col) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000454 a.setDouble(row, col, row * 4 + col);
reed@google.com7d683352012-12-03 21:19:52 +0000455 }
456 }
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000457
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000458 double bufferd[16];
459 float bufferf[16];
460 a.asColMajord(bufferd);
461 b.setColMajord(bufferd);
462 REPORTER_ASSERT(reporter, nearly_equal(a, b));
463 b.setRowMajord(bufferd);
464 b.transpose();
465 REPORTER_ASSERT(reporter, nearly_equal(a, b));
466 a.asColMajorf(bufferf);
467 b.setColMajorf(bufferf);
468 REPORTER_ASSERT(reporter, nearly_equal(a, b));
469 b.setRowMajorf(bufferf);
470 b.transpose();
471 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000472}
473
caryclark@google.com42639cd2012-06-06 12:03:39 +0000474static void TestMatrix44(skiatest::Reporter* reporter) {
reed@google.com125002a2011-06-09 19:13:41 +0000475 SkMatrix44 mat, inverse, iden1, iden2, rot;
476
477 mat.reset();
reed@google.com7d683352012-12-03 21:19:52 +0000478 mat.setTranslate(1, 1, 1);
reed@google.com125002a2011-06-09 19:13:41 +0000479 mat.invert(&inverse);
480 iden1.setConcat(mat, inverse);
481 REPORTER_ASSERT(reporter, is_identity(iden1));
482
reed@google.com7d683352012-12-03 21:19:52 +0000483 mat.setScale(2, 2, 2);
reed@google.com125002a2011-06-09 19:13:41 +0000484 mat.invert(&inverse);
485 iden1.setConcat(mat, inverse);
486 REPORTER_ASSERT(reporter, is_identity(iden1));
487
reed@google.com7d683352012-12-03 21:19:52 +0000488 mat.setScale(SK_MScalar1/2, SK_MScalar1/2, SK_MScalar1/2);
reed@google.com125002a2011-06-09 19:13:41 +0000489 mat.invert(&inverse);
490 iden1.setConcat(mat, inverse);
491 REPORTER_ASSERT(reporter, is_identity(iden1));
492
reed@google.com7d683352012-12-03 21:19:52 +0000493 mat.setScale(3, 3, 3);
494 rot.setRotateDegreesAbout(0, 0, -1, 90);
reed@google.com125002a2011-06-09 19:13:41 +0000495 mat.postConcat(rot);
496 REPORTER_ASSERT(reporter, mat.invert(NULL));
497 mat.invert(&inverse);
498 iden1.setConcat(mat, inverse);
499 REPORTER_ASSERT(reporter, is_identity(iden1));
500 iden2.setConcat(inverse, mat);
501 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@google.comda9fac02011-06-13 14:46:52 +0000502
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000503 // test tiny-valued matrix inverse
504 mat.reset();
505 mat.setScale(1.0e-12, 1.0e-12, 1.0e-12);
506 rot.setRotateDegreesAbout(0, 0, -1, 90);
507 mat.postConcat(rot);
508 mat.postTranslate(1.0e-12, 1.0e-12, 1.0e-12);
509 REPORTER_ASSERT(reporter, mat.invert(NULL));
510 mat.invert(&inverse);
511 iden1.setConcat(mat, inverse);
512 REPORTER_ASSERT(reporter, is_identity(iden1));
513
514 // test mixed-valued matrix inverse
515 mat.reset();
commit-bot@chromium.orgedd18982013-08-20 16:28:42 +0000516 mat.setScale(1.0e-10, 3.0, 1.0e+10);
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000517 rot.setRotateDegreesAbout(0, 0, -1, 90);
518 mat.postConcat(rot);
commit-bot@chromium.orgedd18982013-08-20 16:28:42 +0000519 mat.postTranslate(1.0e+10, 3.0, 1.0e-10);
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000520 REPORTER_ASSERT(reporter, mat.invert(NULL));
521 mat.invert(&inverse);
522 iden1.setConcat(mat, inverse);
523 REPORTER_ASSERT(reporter, is_identity(iden1));
524
525 // test degenerate matrix
526 mat.reset();
527 mat.set3x3(1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0);
528 REPORTER_ASSERT(reporter, !mat.invert(NULL));
529
reed@google.comda9fac02011-06-13 14:46:52 +0000530 // test rol/col Major getters
531 {
532 mat.setTranslate(2, 3, 4);
533 float dataf[16];
534 double datad[16];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000535
reed@google.comda9fac02011-06-13 14:46:52 +0000536 mat.asColMajorf(dataf);
537 assert16<float>(reporter, dataf,
538 1, 0, 0, 0,
539 0, 1, 0, 0,
540 0, 0, 1, 0,
541 2, 3, 4, 1);
542 mat.asColMajord(datad);
543 assert16<double>(reporter, datad, 1, 0, 0, 0,
544 0, 1, 0, 0,
545 0, 0, 1, 0,
546 2, 3, 4, 1);
547 mat.asRowMajorf(dataf);
548 assert16<float>(reporter, dataf, 1, 0, 0, 2,
549 0, 1, 0, 3,
550 0, 0, 1, 4,
551 0, 0, 0, 1);
552 mat.asRowMajord(datad);
553 assert16<double>(reporter, datad, 1, 0, 0, 2,
554 0, 1, 0, 3,
555 0, 0, 1, 4,
556 0, 0, 0, 1);
557 }
reed@google.com6f2b44d2011-06-24 18:13:39 +0000558
reed@google.com80b577e2012-11-09 21:25:06 +0000559 test_concat(reporter);
560
caryclark@google.com42639cd2012-06-06 12:03:39 +0000561 if (false) { // avoid bit rot, suppress warning (working on making this pass)
562 test_common_angles(reporter);
563 }
vollick@chromium.org3959a762012-11-13 15:08:22 +0000564
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000565 test_constructor(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000566 test_gettype(reporter);
vollick@chromium.org3959a762012-11-13 15:08:22 +0000567 test_determinant(reporter);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000568 test_invert(reporter);
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000569 test_transpose(reporter);
570 test_get_set_double(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000571 test_set_row_col_major(reporter);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000572 test_translate(reporter);
573 test_scale(reporter);
574 test_map2(reporter);
reed@google.com125002a2011-06-09 19:13:41 +0000575}
576
577#include "TestClassDef.h"
578DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44)