blob: 9dc77c2b5dfc987b2196e47ab43b1438042f9eda [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
28 return SkScalarAbs(a - b) <= tolerance;
29}
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
80static void test_translate(skiatest::Reporter* reporter) {
81 SkMatrix44 mat, inverse;
82
83 mat.setTranslate(0, 0, 0);
84 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
85 mat.setTranslate(1, 2, 3);
86 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kTranslate_Mask));
87 REPORTER_ASSERT(reporter, mat.invert(&inverse));
88 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kTranslate_Mask));
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +000089
reed@google.com99b5c7f2012-12-05 22:13:59 +000090 SkMatrix44 a, b, c;
91 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
92 b.setTranslate(10, 11, 12);
93
94 c.setConcat(a, b);
95 mat = a;
96 mat.preTranslate(10, 11, 12);
97 REPORTER_ASSERT(reporter, mat == c);
98
99 c.setConcat(b, a);
100 mat = a;
101 mat.postTranslate(10, 11, 12);
102 REPORTER_ASSERT(reporter, mat == c);
103}
104
105static void test_scale(skiatest::Reporter* reporter) {
106 SkMatrix44 mat, inverse;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000107
reed@google.com99b5c7f2012-12-05 22:13:59 +0000108 mat.setScale(1, 1, 1);
109 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
110 mat.setScale(1, 2, 3);
111 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask));
112 REPORTER_ASSERT(reporter, mat.invert(&inverse));
113 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_Mask));
114
115 SkMatrix44 a, b, c;
116 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
117 b.setScale(10, 11, 12);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000118
reed@google.com99b5c7f2012-12-05 22:13:59 +0000119 c.setConcat(a, b);
120 mat = a;
121 mat.preScale(10, 11, 12);
122 REPORTER_ASSERT(reporter, mat == c);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000123
reed@google.com99b5c7f2012-12-05 22:13:59 +0000124 c.setConcat(b, a);
125 mat = a;
126 mat.postScale(10, 11, 12);
127 REPORTER_ASSERT(reporter, mat == c);
128}
129
130static void make_i(SkMatrix44* mat) { mat->setIdentity(); }
131static void make_t(SkMatrix44* mat) { mat->setTranslate(1, 2, 3); }
132static void make_s(SkMatrix44* mat) { mat->setScale(1, 2, 3); }
133static void make_st(SkMatrix44* mat) {
134 mat->setScale(1, 2, 3);
135 mat->postTranslate(1, 2, 3);
136}
137static void make_a(SkMatrix44* mat) {
138 mat->setRotateDegreesAbout(1, 2, 3, 45);
139}
140static void make_p(SkMatrix44* mat) {
141 SkMScalar data[] = {
142 1, 2, 3, 4, 5, 6, 7, 8,
143 1, 2, 3, 4, 5, 6, 7, 8,
144 };
145 mat->setRowMajor(data);
146}
147
148typedef void (*Make44Proc)(SkMatrix44*);
149
150static const Make44Proc gMakeProcs[] = {
151 make_i, make_t, make_s, make_st, make_a, make_p
152};
153
154static void test_map2(skiatest::Reporter* reporter, const SkMatrix44& mat) {
155 SkMScalar src2[] = { 1, 2 };
156 SkMScalar src4[] = { src2[0], src2[1], 0, 1 };
157 SkMScalar dstA[4], dstB[4];
158
159 for (int i = 0; i < 4; ++i) {
160 dstA[i] = 123456789;
161 dstB[i] = 987654321;
162 }
163
164 mat.map2(src2, 1, dstA);
165 mat.mapMScalars(src4, dstB);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000166
reed@google.com99b5c7f2012-12-05 22:13:59 +0000167 for (int i = 0; i < 4; ++i) {
168 REPORTER_ASSERT(reporter, dstA[i] == dstB[i]);
169 }
170}
171
172static void test_map2(skiatest::Reporter* reporter) {
173 SkMatrix44 mat;
174
175 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) {
176 gMakeProcs[i](&mat);
177 test_map2(reporter, mat);
178 }
179}
180
reed@google.com7d683352012-12-03 21:19:52 +0000181static void test_gettype(skiatest::Reporter* reporter) {
182 SkMatrix44 matrix;
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000183
reed@google.com7d683352012-12-03 21:19:52 +0000184 REPORTER_ASSERT(reporter, matrix.isIdentity());
185 REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType());
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000186
reed@google.com7d683352012-12-03 21:19:52 +0000187 int expectedMask;
188
189 matrix.set(1, 1, 0);
190 expectedMask = SkMatrix44::kScale_Mask;
191 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
192
193 matrix.set(0, 3, 1); // translate-x
194 expectedMask |= SkMatrix44::kTranslate_Mask;
195 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
196
197 matrix.set(2, 0, 1);
198 expectedMask |= SkMatrix44::kAffine_Mask;
199 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000200
reed@google.com7d683352012-12-03 21:19:52 +0000201 matrix.set(3, 2, 1);
202 REPORTER_ASSERT(reporter, matrix.getType() & SkMatrix44::kPerspective_Mask);
203}
204
reed@google.com6f2b44d2011-06-24 18:13:39 +0000205static void test_common_angles(skiatest::Reporter* reporter) {
206 SkMatrix44 rot;
207 // Test precision of rotation in common cases
208 int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 };
209 for (int i = 0; i < 9; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000210 rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i]));
reed@google.com6f2b44d2011-06-24 18:13:39 +0000211
212 SkMatrix rot3x3 = rot;
213 REPORTER_ASSERT(reporter, rot3x3.rectStaysRect());
214 }
215}
216
reed@google.com80b577e2012-11-09 21:25:06 +0000217static void test_concat(skiatest::Reporter* reporter) {
218 int i;
219 SkMatrix44 a, b, c, d;
220
221 a.setTranslate(10, 10, 10);
222 b.setScale(2, 2, 2);
223
224 SkScalar src[8] = {
225 0, 0, 0, 1,
226 1, 1, 1, 1
227 };
228 SkScalar dst[8];
229
230 c.setConcat(a, b);
231
232 d = a;
233 d.preConcat(b);
234 REPORTER_ASSERT(reporter, d == c);
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000235
reed@google.com1ea95be2012-11-09 21:39:48 +0000236 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000237 for (i = 0; i < 3; ++i) {
238 REPORTER_ASSERT(reporter, 10 == dst[i]);
239 REPORTER_ASSERT(reporter, 12 == dst[i + 4]);
240 }
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000241
reed@google.com80b577e2012-11-09 21:25:06 +0000242 c.setConcat(b, a);
243
244 d = a;
245 d.postConcat(b);
246 REPORTER_ASSERT(reporter, d == c);
247
reed@google.com1ea95be2012-11-09 21:39:48 +0000248 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000249 for (i = 0; i < 3; ++i) {
250 REPORTER_ASSERT(reporter, 20 == dst[i]);
251 REPORTER_ASSERT(reporter, 22 == dst[i + 4]);
252 }
253}
254
vollick@chromium.org3959a762012-11-13 15:08:22 +0000255static void test_determinant(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000256 SkMatrix44 a;
257 REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000258 a.set(1, 1, 2);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000259 REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant()));
260 SkMatrix44 b;
261 REPORTER_ASSERT(reporter, a.invert(&b));
262 REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant()));
263 SkMatrix44 c = b = a;
reed@google.com7d683352012-12-03 21:19:52 +0000264 c.set(0, 1, 4);
265 b.set(1, 0, 4);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000266 REPORTER_ASSERT(reporter,
267 nearly_equal_double(a.determinant(),
268 b.determinant()));
269 SkMatrix44 d = a;
reed@google.com7d683352012-12-03 21:19:52 +0000270 d.set(0, 0, 8);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000271 REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000272
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000273 SkMatrix44 e = a;
274 e.postConcat(d);
275 REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000276 e.set(0, 0, 0);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000277 REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000278}
279
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000280static void test_transpose(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000281 SkMatrix44 a;
282 SkMatrix44 b;
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000283
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000284 int i = 0;
285 for (int row = 0; row < 4; ++row) {
286 for (int col = 0; col < 4; ++col) {
287 a.setDouble(row, col, i);
288 b.setDouble(col, row, i++);
289 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000290 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000291
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000292 a.transpose();
293 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000294}
295
296static void test_get_set_double(skiatest::Reporter* reporter) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000297 SkMatrix44 a;
298 for (int row = 0; row < 4; ++row) {
299 for (int col = 0; col < 4; ++col) {
300 a.setDouble(row, col, 3.141592653589793);
301 REPORTER_ASSERT(reporter,
302 nearly_equal_double(3.141592653589793,
303 a.getDouble(row, col)));
304 a.setDouble(row, col, 0);
305 REPORTER_ASSERT(reporter,
306 nearly_equal_double(0, a.getDouble(row, col)));
307 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000308 }
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000309}
310
311static void test_set_row_col_major(skiatest::Reporter* reporter) {
312 SkMatrix44 a, b, c, d;
reed@google.com7d683352012-12-03 21:19:52 +0000313 for (int row = 0; row < 4; ++row) {
314 for (int col = 0; col < 4; ++col) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000315 a.setDouble(row, col, row * 4 + col);
reed@google.com7d683352012-12-03 21:19:52 +0000316 }
317 }
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000318
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000319 double bufferd[16];
320 float bufferf[16];
321 a.asColMajord(bufferd);
322 b.setColMajord(bufferd);
323 REPORTER_ASSERT(reporter, nearly_equal(a, b));
324 b.setRowMajord(bufferd);
325 b.transpose();
326 REPORTER_ASSERT(reporter, nearly_equal(a, b));
327 a.asColMajorf(bufferf);
328 b.setColMajorf(bufferf);
329 REPORTER_ASSERT(reporter, nearly_equal(a, b));
330 b.setRowMajorf(bufferf);
331 b.transpose();
332 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000333}
334
caryclark@google.com42639cd2012-06-06 12:03:39 +0000335static void TestMatrix44(skiatest::Reporter* reporter) {
reed@google.com125002a2011-06-09 19:13:41 +0000336 SkMatrix44 mat, inverse, iden1, iden2, rot;
337
338 mat.reset();
reed@google.com7d683352012-12-03 21:19:52 +0000339 mat.setTranslate(1, 1, 1);
reed@google.com125002a2011-06-09 19:13:41 +0000340 mat.invert(&inverse);
341 iden1.setConcat(mat, inverse);
342 REPORTER_ASSERT(reporter, is_identity(iden1));
343
reed@google.com7d683352012-12-03 21:19:52 +0000344 mat.setScale(2, 2, 2);
reed@google.com125002a2011-06-09 19:13:41 +0000345 mat.invert(&inverse);
346 iden1.setConcat(mat, inverse);
347 REPORTER_ASSERT(reporter, is_identity(iden1));
348
reed@google.com7d683352012-12-03 21:19:52 +0000349 mat.setScale(SK_MScalar1/2, SK_MScalar1/2, SK_MScalar1/2);
reed@google.com125002a2011-06-09 19:13:41 +0000350 mat.invert(&inverse);
351 iden1.setConcat(mat, inverse);
352 REPORTER_ASSERT(reporter, is_identity(iden1));
353
reed@google.com7d683352012-12-03 21:19:52 +0000354 mat.setScale(3, 3, 3);
355 rot.setRotateDegreesAbout(0, 0, -1, 90);
reed@google.com125002a2011-06-09 19:13:41 +0000356 mat.postConcat(rot);
357 REPORTER_ASSERT(reporter, mat.invert(NULL));
358 mat.invert(&inverse);
359 iden1.setConcat(mat, inverse);
360 REPORTER_ASSERT(reporter, is_identity(iden1));
361 iden2.setConcat(inverse, mat);
362 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@google.comda9fac02011-06-13 14:46:52 +0000363
364 // test rol/col Major getters
365 {
366 mat.setTranslate(2, 3, 4);
367 float dataf[16];
368 double datad[16];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000369
reed@google.comda9fac02011-06-13 14:46:52 +0000370 mat.asColMajorf(dataf);
371 assert16<float>(reporter, dataf,
372 1, 0, 0, 0,
373 0, 1, 0, 0,
374 0, 0, 1, 0,
375 2, 3, 4, 1);
376 mat.asColMajord(datad);
377 assert16<double>(reporter, datad, 1, 0, 0, 0,
378 0, 1, 0, 0,
379 0, 0, 1, 0,
380 2, 3, 4, 1);
381 mat.asRowMajorf(dataf);
382 assert16<float>(reporter, dataf, 1, 0, 0, 2,
383 0, 1, 0, 3,
384 0, 0, 1, 4,
385 0, 0, 0, 1);
386 mat.asRowMajord(datad);
387 assert16<double>(reporter, datad, 1, 0, 0, 2,
388 0, 1, 0, 3,
389 0, 0, 1, 4,
390 0, 0, 0, 1);
391 }
reed@google.com6f2b44d2011-06-24 18:13:39 +0000392
reed@google.com80b577e2012-11-09 21:25:06 +0000393 test_concat(reporter);
394
caryclark@google.com42639cd2012-06-06 12:03:39 +0000395 if (false) { // avoid bit rot, suppress warning (working on making this pass)
396 test_common_angles(reporter);
397 }
vollick@chromium.org3959a762012-11-13 15:08:22 +0000398
reed@google.com7d683352012-12-03 21:19:52 +0000399 test_gettype(reporter);
vollick@chromium.org3959a762012-11-13 15:08:22 +0000400 test_determinant(reporter);
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000401 test_transpose(reporter);
402 test_get_set_double(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000403 test_set_row_col_major(reporter);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000404 test_translate(reporter);
405 test_scale(reporter);
406 test_map2(reporter);
reed@google.com125002a2011-06-09 19:13:41 +0000407}
408
409#include "TestClassDef.h"
410DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44)