blob: bdeafd4ff9f7ee8bb8ba4ee713cdc86237a3b451 [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);
234}
235
reed@google.com6f2b44d2011-06-24 18:13:39 +0000236static void test_common_angles(skiatest::Reporter* reporter) {
237 SkMatrix44 rot;
238 // Test precision of rotation in common cases
239 int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 };
240 for (int i = 0; i < 9; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000241 rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i]));
reed@google.com6f2b44d2011-06-24 18:13:39 +0000242
243 SkMatrix rot3x3 = rot;
244 REPORTER_ASSERT(reporter, rot3x3.rectStaysRect());
245 }
246}
247
reed@google.com80b577e2012-11-09 21:25:06 +0000248static void test_concat(skiatest::Reporter* reporter) {
249 int i;
250 SkMatrix44 a, b, c, d;
251
252 a.setTranslate(10, 10, 10);
253 b.setScale(2, 2, 2);
254
255 SkScalar src[8] = {
256 0, 0, 0, 1,
257 1, 1, 1, 1
258 };
259 SkScalar dst[8];
260
261 c.setConcat(a, b);
262
263 d = a;
264 d.preConcat(b);
265 REPORTER_ASSERT(reporter, d == c);
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000266
reed@google.com1ea95be2012-11-09 21:39:48 +0000267 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000268 for (i = 0; i < 3; ++i) {
269 REPORTER_ASSERT(reporter, 10 == dst[i]);
270 REPORTER_ASSERT(reporter, 12 == dst[i + 4]);
271 }
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000272
reed@google.com80b577e2012-11-09 21:25:06 +0000273 c.setConcat(b, a);
274
275 d = a;
276 d.postConcat(b);
277 REPORTER_ASSERT(reporter, d == c);
278
reed@google.com1ea95be2012-11-09 21:39:48 +0000279 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000280 for (i = 0; i < 3; ++i) {
281 REPORTER_ASSERT(reporter, 20 == dst[i]);
282 REPORTER_ASSERT(reporter, 22 == dst[i + 4]);
283 }
284}
285
vollick@chromium.org3959a762012-11-13 15:08:22 +0000286static void test_determinant(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000287 SkMatrix44 a;
288 REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000289 a.set(1, 1, 2);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000290 REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant()));
291 SkMatrix44 b;
292 REPORTER_ASSERT(reporter, a.invert(&b));
293 REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant()));
294 SkMatrix44 c = b = a;
reed@google.com7d683352012-12-03 21:19:52 +0000295 c.set(0, 1, 4);
296 b.set(1, 0, 4);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000297 REPORTER_ASSERT(reporter,
298 nearly_equal_double(a.determinant(),
299 b.determinant()));
300 SkMatrix44 d = a;
reed@google.com7d683352012-12-03 21:19:52 +0000301 d.set(0, 0, 8);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000302 REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000303
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000304 SkMatrix44 e = a;
305 e.postConcat(d);
306 REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000307 e.set(0, 0, 0);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000308 REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000309}
310
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000311static void test_transpose(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000312 SkMatrix44 a;
313 SkMatrix44 b;
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000314
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000315 int i = 0;
316 for (int row = 0; row < 4; ++row) {
317 for (int col = 0; col < 4; ++col) {
318 a.setDouble(row, col, i);
319 b.setDouble(col, row, i++);
320 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000321 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000322
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000323 a.transpose();
324 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000325}
326
327static void test_get_set_double(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000328 SkMatrix44 a;
329 for (int row = 0; row < 4; ++row) {
330 for (int col = 0; col < 4; ++col) {
331 a.setDouble(row, col, 3.141592653589793);
332 REPORTER_ASSERT(reporter,
333 nearly_equal_double(3.141592653589793,
334 a.getDouble(row, col)));
335 a.setDouble(row, col, 0);
336 REPORTER_ASSERT(reporter,
337 nearly_equal_double(0, a.getDouble(row, col)));
338 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000339 }
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000340}
341
342static void test_set_row_col_major(skiatest::Reporter* reporter) {
343 SkMatrix44 a, b, c, d;
reed@google.com7d683352012-12-03 21:19:52 +0000344 for (int row = 0; row < 4; ++row) {
345 for (int col = 0; col < 4; ++col) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000346 a.setDouble(row, col, row * 4 + col);
reed@google.com7d683352012-12-03 21:19:52 +0000347 }
348 }
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000349
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000350 double bufferd[16];
351 float bufferf[16];
352 a.asColMajord(bufferd);
353 b.setColMajord(bufferd);
354 REPORTER_ASSERT(reporter, nearly_equal(a, b));
355 b.setRowMajord(bufferd);
356 b.transpose();
357 REPORTER_ASSERT(reporter, nearly_equal(a, b));
358 a.asColMajorf(bufferf);
359 b.setColMajorf(bufferf);
360 REPORTER_ASSERT(reporter, nearly_equal(a, b));
361 b.setRowMajorf(bufferf);
362 b.transpose();
363 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000364}
365
caryclark@google.com42639cd2012-06-06 12:03:39 +0000366static void TestMatrix44(skiatest::Reporter* reporter) {
reed@google.com125002a2011-06-09 19:13:41 +0000367 SkMatrix44 mat, inverse, iden1, iden2, rot;
368
369 mat.reset();
reed@google.com7d683352012-12-03 21:19:52 +0000370 mat.setTranslate(1, 1, 1);
reed@google.com125002a2011-06-09 19:13:41 +0000371 mat.invert(&inverse);
372 iden1.setConcat(mat, inverse);
373 REPORTER_ASSERT(reporter, is_identity(iden1));
374
reed@google.com7d683352012-12-03 21:19:52 +0000375 mat.setScale(2, 2, 2);
reed@google.com125002a2011-06-09 19:13:41 +0000376 mat.invert(&inverse);
377 iden1.setConcat(mat, inverse);
378 REPORTER_ASSERT(reporter, is_identity(iden1));
379
reed@google.com7d683352012-12-03 21:19:52 +0000380 mat.setScale(SK_MScalar1/2, SK_MScalar1/2, SK_MScalar1/2);
reed@google.com125002a2011-06-09 19:13:41 +0000381 mat.invert(&inverse);
382 iden1.setConcat(mat, inverse);
383 REPORTER_ASSERT(reporter, is_identity(iden1));
384
reed@google.com7d683352012-12-03 21:19:52 +0000385 mat.setScale(3, 3, 3);
386 rot.setRotateDegreesAbout(0, 0, -1, 90);
reed@google.com125002a2011-06-09 19:13:41 +0000387 mat.postConcat(rot);
388 REPORTER_ASSERT(reporter, mat.invert(NULL));
389 mat.invert(&inverse);
390 iden1.setConcat(mat, inverse);
391 REPORTER_ASSERT(reporter, is_identity(iden1));
392 iden2.setConcat(inverse, mat);
393 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@google.comda9fac02011-06-13 14:46:52 +0000394
395 // test rol/col Major getters
396 {
397 mat.setTranslate(2, 3, 4);
398 float dataf[16];
399 double datad[16];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000400
reed@google.comda9fac02011-06-13 14:46:52 +0000401 mat.asColMajorf(dataf);
402 assert16<float>(reporter, dataf,
403 1, 0, 0, 0,
404 0, 1, 0, 0,
405 0, 0, 1, 0,
406 2, 3, 4, 1);
407 mat.asColMajord(datad);
408 assert16<double>(reporter, datad, 1, 0, 0, 0,
409 0, 1, 0, 0,
410 0, 0, 1, 0,
411 2, 3, 4, 1);
412 mat.asRowMajorf(dataf);
413 assert16<float>(reporter, dataf, 1, 0, 0, 2,
414 0, 1, 0, 3,
415 0, 0, 1, 4,
416 0, 0, 0, 1);
417 mat.asRowMajord(datad);
418 assert16<double>(reporter, datad, 1, 0, 0, 2,
419 0, 1, 0, 3,
420 0, 0, 1, 4,
421 0, 0, 0, 1);
422 }
reed@google.com6f2b44d2011-06-24 18:13:39 +0000423
reed@google.com80b577e2012-11-09 21:25:06 +0000424 test_concat(reporter);
425
caryclark@google.com42639cd2012-06-06 12:03:39 +0000426 if (false) { // avoid bit rot, suppress warning (working on making this pass)
427 test_common_angles(reporter);
428 }
vollick@chromium.org3959a762012-11-13 15:08:22 +0000429
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000430 test_constructor(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000431 test_gettype(reporter);
vollick@chromium.org3959a762012-11-13 15:08:22 +0000432 test_determinant(reporter);
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000433 test_transpose(reporter);
434 test_get_set_double(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000435 test_set_row_col_major(reporter);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000436 test_translate(reporter);
437 test_scale(reporter);
438 test_map2(reporter);
reed@google.com125002a2011-06-09 19:13:41 +0000439}
440
441#include "TestClassDef.h"
442DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44)