blob: 8fd84d858c5d0c1d6bd8baafeefb89a0777a8a40 [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.com125002a2011-06-09 19:13:41 +000010#include "SkMatrix44.h"
11
vollick@chromium.org3959a762012-11-13 15:08:22 +000012static bool nearly_equal_double(double a, double b) {
13 const double tolerance = 1e-7;
14 double diff = a - b;
15 if (diff < 0)
16 diff = -diff;
17 return diff <= tolerance;
18}
19
commit-bot@chromium.org722555b2013-10-05 01:16:30 +000020static bool nearly_equal_mscalar(SkMScalar a, SkMScalar b) {
21 const SkMScalar tolerance = SK_MScalar1 / 200000;
22
23 return SkTAbs<SkMScalar>(a - b) <= tolerance;
24}
25
26static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
reed@google.com125002a2011-06-09 19:13:41 +000027 const SkScalar tolerance = SK_Scalar1 / 200000;
commit-bot@chromium.org722555b2013-10-05 01:16:30 +000028 return SkScalarAbs(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) {
commit-bot@chromium.org722555b2013-10-05 01:16:30 +000060 if (!nearly_equal_mscalar(a.get(i, j), b.get(i, j))) {
bungeman@google.comfab44db2013-10-11 18:50:45 +000061 SkDebugf("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) {
reed@google.com44699382013-10-31 17:28:30 +000070 SkMatrix44 identity(SkMatrix44::kIdentity_Constructor);
reed@google.com125002a2011-06-09 19:13:41 +000071 return nearly_equal(m, identity);
72}
73
reed@google.com99b5c7f2012-12-05 22:13:59 +000074///////////////////////////////////////////////////////////////////////////////
75static bool bits_isonly(int value, int mask) {
76 return 0 == (value & ~mask);
77}
78
vollick@chromium.org57a54e32012-12-10 20:16:10 +000079static void test_constructor(skiatest::Reporter* reporter) {
80 // Allocate a matrix on the heap
reed@google.com44699382013-10-31 17:28:30 +000081 SkMatrix44* placeholderMatrix = new SkMatrix44(SkMatrix44::kUninitialized_Constructor);
robertphillips@google.com35300c42013-03-21 17:38:49 +000082 SkAutoTDelete<SkMatrix44> deleteMe(placeholderMatrix);
83
vollick@chromium.org57a54e32012-12-10 20:16:10 +000084 for (int row = 0; row < 4; ++row) {
85 for (int col = 0; col < 4; ++col) {
86 placeholderMatrix->setDouble(row, col, row * col);
87 }
88 }
89
90 // Use placement-new syntax to trigger the constructor on top of the heap
91 // address we already initialized. This allows us to check that the
92 // constructor did avoid initializing the matrix contents.
93 SkMatrix44* testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kUninitialized_Constructor);
94 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix);
95 REPORTER_ASSERT(reporter, !testMatrix->isIdentity());
96 for (int row = 0; row < 4; ++row) {
97 for (int col = 0; col < 4; ++col) {
98 REPORTER_ASSERT(reporter, nearly_equal_double(row * col, testMatrix->getDouble(row, col)));
99 }
100 }
101
102 // Verify that kIdentity_Constructor really does initialize to an identity matrix.
103 testMatrix = 0;
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000104 testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kIdentity_Constructor);
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000105 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix);
106 REPORTER_ASSERT(reporter, testMatrix->isIdentity());
107 REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I());
108}
109
reed@google.com99b5c7f2012-12-05 22:13:59 +0000110static void test_translate(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000111 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor);
112 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000113
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.com44699382013-10-31 17:28:30 +0000121 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor);
122 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor);
123 SkMatrix44 c(SkMatrix44::kUninitialized_Constructor);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000124 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
125 b.setTranslate(10, 11, 12);
126
127 c.setConcat(a, b);
128 mat = a;
129 mat.preTranslate(10, 11, 12);
130 REPORTER_ASSERT(reporter, mat == c);
131
132 c.setConcat(b, a);
133 mat = a;
134 mat.postTranslate(10, 11, 12);
135 REPORTER_ASSERT(reporter, mat == c);
136}
137
138static void test_scale(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000139 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor);
140 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000141
reed@google.com99b5c7f2012-12-05 22:13:59 +0000142 mat.setScale(1, 1, 1);
143 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
144 mat.setScale(1, 2, 3);
145 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask));
146 REPORTER_ASSERT(reporter, mat.invert(&inverse));
147 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_Mask));
148
reed@google.com44699382013-10-31 17:28:30 +0000149 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor);
150 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor);
151 SkMatrix44 c(SkMatrix44::kUninitialized_Constructor);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000152 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
153 b.setScale(10, 11, 12);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000154
reed@google.com99b5c7f2012-12-05 22:13:59 +0000155 c.setConcat(a, b);
156 mat = a;
157 mat.preScale(10, 11, 12);
158 REPORTER_ASSERT(reporter, mat == c);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000159
reed@google.com99b5c7f2012-12-05 22:13:59 +0000160 c.setConcat(b, a);
161 mat = a;
162 mat.postScale(10, 11, 12);
163 REPORTER_ASSERT(reporter, mat == c);
164}
165
166static void make_i(SkMatrix44* mat) { mat->setIdentity(); }
167static void make_t(SkMatrix44* mat) { mat->setTranslate(1, 2, 3); }
168static void make_s(SkMatrix44* mat) { mat->setScale(1, 2, 3); }
169static void make_st(SkMatrix44* mat) {
170 mat->setScale(1, 2, 3);
171 mat->postTranslate(1, 2, 3);
172}
173static void make_a(SkMatrix44* mat) {
174 mat->setRotateDegreesAbout(1, 2, 3, 45);
175}
176static void make_p(SkMatrix44* mat) {
177 SkMScalar data[] = {
178 1, 2, 3, 4, 5, 6, 7, 8,
179 1, 2, 3, 4, 5, 6, 7, 8,
180 };
181 mat->setRowMajor(data);
182}
183
184typedef void (*Make44Proc)(SkMatrix44*);
185
186static const Make44Proc gMakeProcs[] = {
187 make_i, make_t, make_s, make_st, make_a, make_p
188};
189
190static void test_map2(skiatest::Reporter* reporter, const SkMatrix44& mat) {
191 SkMScalar src2[] = { 1, 2 };
192 SkMScalar src4[] = { src2[0], src2[1], 0, 1 };
193 SkMScalar dstA[4], dstB[4];
194
195 for (int i = 0; i < 4; ++i) {
196 dstA[i] = 123456789;
197 dstB[i] = 987654321;
198 }
199
200 mat.map2(src2, 1, dstA);
201 mat.mapMScalars(src4, dstB);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000202
reed@google.com99b5c7f2012-12-05 22:13:59 +0000203 for (int i = 0; i < 4; ++i) {
204 REPORTER_ASSERT(reporter, dstA[i] == dstB[i]);
205 }
206}
207
208static void test_map2(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000209 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000210
211 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) {
212 gMakeProcs[i](&mat);
213 test_map2(reporter, mat);
214 }
215}
216
reed@google.com7d683352012-12-03 21:19:52 +0000217static void test_gettype(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000218 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000219
reed@google.com7d683352012-12-03 21:19:52 +0000220 REPORTER_ASSERT(reporter, matrix.isIdentity());
221 REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType());
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000222
reed@google.com7d683352012-12-03 21:19:52 +0000223 int expectedMask;
224
225 matrix.set(1, 1, 0);
226 expectedMask = SkMatrix44::kScale_Mask;
227 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
228
229 matrix.set(0, 3, 1); // translate-x
230 expectedMask |= SkMatrix44::kTranslate_Mask;
231 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
232
233 matrix.set(2, 0, 1);
234 expectedMask |= SkMatrix44::kAffine_Mask;
235 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000236
reed@google.com7d683352012-12-03 21:19:52 +0000237 matrix.set(3, 2, 1);
238 REPORTER_ASSERT(reporter, matrix.getType() & SkMatrix44::kPerspective_Mask);
reed@google.com87f99cb2013-04-19 12:25:00 +0000239
240 // ensure that negative zero is treated as zero
241 SkMScalar dx = 0;
242 SkMScalar dy = 0;
243 SkMScalar dz = 0;
244 matrix.setTranslate(-dx, -dy, -dz);
245 REPORTER_ASSERT(reporter, matrix.isIdentity());
246 matrix.preTranslate(-dx, -dy, -dz);
247 REPORTER_ASSERT(reporter, matrix.isIdentity());
248 matrix.postTranslate(-dx, -dy, -dz);
249 REPORTER_ASSERT(reporter, matrix.isIdentity());
reed@google.com7d683352012-12-03 21:19:52 +0000250}
251
reed@google.com6f2b44d2011-06-24 18:13:39 +0000252static void test_common_angles(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000253 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor);
reed@google.com6f2b44d2011-06-24 18:13:39 +0000254 // Test precision of rotation in common cases
255 int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 };
256 for (int i = 0; i < 9; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000257 rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i]));
reed@google.com6f2b44d2011-06-24 18:13:39 +0000258
259 SkMatrix rot3x3 = rot;
260 REPORTER_ASSERT(reporter, rot3x3.rectStaysRect());
261 }
262}
263
reed@google.com80b577e2012-11-09 21:25:06 +0000264static void test_concat(skiatest::Reporter* reporter) {
265 int i;
reed@google.com44699382013-10-31 17:28:30 +0000266 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor);
267 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor);
268 SkMatrix44 c(SkMatrix44::kUninitialized_Constructor);
269 SkMatrix44 d(SkMatrix44::kUninitialized_Constructor);
reed@google.com80b577e2012-11-09 21:25:06 +0000270
271 a.setTranslate(10, 10, 10);
272 b.setScale(2, 2, 2);
273
274 SkScalar src[8] = {
275 0, 0, 0, 1,
276 1, 1, 1, 1
277 };
278 SkScalar dst[8];
279
280 c.setConcat(a, b);
281
282 d = a;
283 d.preConcat(b);
284 REPORTER_ASSERT(reporter, d == c);
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000285
reed@google.com1ea95be2012-11-09 21:39:48 +0000286 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000287 for (i = 0; i < 3; ++i) {
288 REPORTER_ASSERT(reporter, 10 == dst[i]);
289 REPORTER_ASSERT(reporter, 12 == dst[i + 4]);
290 }
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000291
reed@google.com80b577e2012-11-09 21:25:06 +0000292 c.setConcat(b, a);
293
294 d = a;
295 d.postConcat(b);
296 REPORTER_ASSERT(reporter, d == c);
297
reed@google.com1ea95be2012-11-09 21:39:48 +0000298 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000299 for (i = 0; i < 3; ++i) {
300 REPORTER_ASSERT(reporter, 20 == dst[i]);
301 REPORTER_ASSERT(reporter, 22 == dst[i + 4]);
302 }
303}
304
vollick@chromium.org3959a762012-11-13 15:08:22 +0000305static void test_determinant(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000306 SkMatrix44 a(SkMatrix44::kIdentity_Constructor);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000307 REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000308 a.set(1, 1, 2);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000309 REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant()));
reed@google.com44699382013-10-31 17:28:30 +0000310 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000311 REPORTER_ASSERT(reporter, a.invert(&b));
312 REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant()));
313 SkMatrix44 c = b = a;
reed@google.com7d683352012-12-03 21:19:52 +0000314 c.set(0, 1, 4);
315 b.set(1, 0, 4);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000316 REPORTER_ASSERT(reporter,
317 nearly_equal_double(a.determinant(),
318 b.determinant()));
319 SkMatrix44 d = a;
reed@google.com7d683352012-12-03 21:19:52 +0000320 d.set(0, 0, 8);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000321 REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000322
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000323 SkMatrix44 e = a;
324 e.postConcat(d);
325 REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000326 e.set(0, 0, 0);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000327 REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000328}
329
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000330static void test_invert(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000331 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000332 double inverseData[16];
333
reed@google.com44699382013-10-31 17:28:30 +0000334 SkMatrix44 identity(SkMatrix44::kIdentity_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000335 identity.invert(&inverse);
336 inverse.asRowMajord(inverseData);
337 assert16<double>(reporter, inverseData,
338 1, 0, 0, 0,
339 0, 1, 0, 0,
340 0, 0, 1, 0,
341 0, 0, 0, 1);
342
reed@google.com44699382013-10-31 17:28:30 +0000343 SkMatrix44 translation(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000344 translation.setTranslate(2, 3, 4);
345 translation.invert(&inverse);
346 inverse.asRowMajord(inverseData);
347 assert16<double>(reporter, inverseData,
348 1, 0, 0, -2,
349 0, 1, 0, -3,
350 0, 0, 1, -4,
351 0, 0, 0, 1);
352
reed@google.com44699382013-10-31 17:28:30 +0000353 SkMatrix44 scale(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000354 scale.setScale(2, 4, 8);
355 scale.invert(&inverse);
356 inverse.asRowMajord(inverseData);
357 assert16<double>(reporter, inverseData,
358 0.5, 0, 0, 0,
359 0, 0.25, 0, 0,
360 0, 0, 0.125, 0,
361 0, 0, 0, 1);
362
reed@google.com44699382013-10-31 17:28:30 +0000363 SkMatrix44 scaleTranslation(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000364 scaleTranslation.setScale(10, 100, 1000);
365 scaleTranslation.preTranslate(2, 3, 4);
366 scaleTranslation.invert(&inverse);
367 inverse.asRowMajord(inverseData);
368 assert16<double>(reporter, inverseData,
369 0.1, 0, 0, -2,
370 0, 0.01, 0, -3,
371 0, 0, 0.001, -4,
372 0, 0, 0, 1);
373
reed@google.com44699382013-10-31 17:28:30 +0000374 SkMatrix44 rotation(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000375 rotation.setRotateDegreesAbout(0, 0, 1, 90);
376 rotation.invert(&inverse);
reed@google.com44699382013-10-31 17:28:30 +0000377 SkMatrix44 expected(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000378 double expectedInverseRotation[16] =
379 {0, 1, 0, 0,
380 -1, 0, 0, 0,
381 0, 0, 1, 0,
382 0, 0, 0, 1};
383 expected.setRowMajord(expectedInverseRotation);
384 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000385
reed@google.com44699382013-10-31 17:28:30 +0000386 SkMatrix44 affine(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000387 affine.setRotateDegreesAbout(0, 0, 1, 90);
388 affine.preScale(10, 20, 100);
389 affine.preTranslate(2, 3, 4);
390 affine.invert(&inverse);
391 double expectedInverseAffine[16] =
392 {0, 0.1, 0, -2,
393 -0.05, 0, 0, -3,
394 0, 0, 0.01, -4,
395 0, 0, 0, 1};
396 expected.setRowMajord(expectedInverseAffine);
397 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
398
reed@google.com44699382013-10-31 17:28:30 +0000399 SkMatrix44 perspective(SkMatrix44::kIdentity_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000400 perspective.setDouble(3, 2, 1.0);
401 perspective.invert(&inverse);
402 double expectedInversePerspective[16] =
403 {1, 0, 0, 0,
404 0, 1, 0, 0,
405 0, 0, 1, 0,
406 0, 0, -1, 1};
407 expected.setRowMajord(expectedInversePerspective);
408 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
409
reed@google.com44699382013-10-31 17:28:30 +0000410 SkMatrix44 affineAndPerspective(SkMatrix44::kIdentity_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000411 affineAndPerspective.setDouble(3, 2, 1.0);
412 affineAndPerspective.preScale(10, 20, 100);
413 affineAndPerspective.preTranslate(2, 3, 4);
414 affineAndPerspective.invert(&inverse);
415 double expectedInverseAffineAndPerspective[16] =
416 {0.1, 0, 2, -2,
417 0, 0.05, 3, -3,
418 0, 0, 4.01, -4,
419 0, 0, -1, 1};
420 expected.setRowMajord(expectedInverseAffineAndPerspective);
421 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
422}
423
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000424static void test_transpose(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000425 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor);
426 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor);
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000427
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000428 int i = 0;
429 for (int row = 0; row < 4; ++row) {
430 for (int col = 0; col < 4; ++col) {
431 a.setDouble(row, col, i);
432 b.setDouble(col, row, i++);
433 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000434 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000435
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000436 a.transpose();
437 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000438}
439
440static void test_get_set_double(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000441 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000442 for (int row = 0; row < 4; ++row) {
443 for (int col = 0; col < 4; ++col) {
444 a.setDouble(row, col, 3.141592653589793);
445 REPORTER_ASSERT(reporter,
446 nearly_equal_double(3.141592653589793,
447 a.getDouble(row, col)));
448 a.setDouble(row, col, 0);
449 REPORTER_ASSERT(reporter,
450 nearly_equal_double(0, a.getDouble(row, col)));
451 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000452 }
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000453}
454
455static void test_set_row_col_major(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000456 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor);
457 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor);
458
reed@google.com7d683352012-12-03 21:19:52 +0000459 for (int row = 0; row < 4; ++row) {
460 for (int col = 0; col < 4; ++col) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000461 a.setDouble(row, col, row * 4 + col);
reed@google.com7d683352012-12-03 21:19:52 +0000462 }
463 }
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000464
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000465 double bufferd[16];
466 float bufferf[16];
467 a.asColMajord(bufferd);
468 b.setColMajord(bufferd);
469 REPORTER_ASSERT(reporter, nearly_equal(a, b));
470 b.setRowMajord(bufferd);
471 b.transpose();
472 REPORTER_ASSERT(reporter, nearly_equal(a, b));
473 a.asColMajorf(bufferf);
474 b.setColMajorf(bufferf);
475 REPORTER_ASSERT(reporter, nearly_equal(a, b));
476 b.setRowMajorf(bufferf);
477 b.transpose();
478 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000479}
480
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000481static void test_3x3_conversion(skiatest::Reporter* reporter) {
482 SkMScalar values4x4[16] = { 1, 2, 3, 4,
483 5, 6, 7, 8,
484 9, 10, 11, 12,
485 13, 14, 15, 16 };
486 SkScalar values3x3[9] = { 1, 2, 4,
487 5, 6, 8,
488 13, 14, 16 };
489 SkMScalar values4x4flattened[16] = { 1, 2, 0, 4,
490 5, 6, 0, 8,
491 0, 0, 1, 0,
492 13, 14, 0, 16 };
reed@google.com44699382013-10-31 17:28:30 +0000493 SkMatrix44 a44(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000494 a44.setRowMajor(values4x4);
495
496 SkMatrix a33 = a44;
497 SkMatrix expected33;
498 for (int i = 0; i < 9; i++) expected33[i] = values3x3[i];
499 REPORTER_ASSERT(reporter, expected33 == a33);
500
501 SkMatrix44 a44flattened = a33;
reed@google.com44699382013-10-31 17:28:30 +0000502 SkMatrix44 expected44flattened(SkMatrix44::kUninitialized_Constructor);
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000503 expected44flattened.setRowMajor(values4x4flattened);
504 REPORTER_ASSERT(reporter, nearly_equal(a44flattened, expected44flattened));
505
506 // Test that a point with a Z value of 0 is transformed the same way.
507 SkScalar vec4[4] = { 2, 4, 0, 8 };
508 SkScalar vec3[3] = { 2, 4, 8 };
509
510 SkScalar vec4transformed[4];
511 SkScalar vec3transformed[3];
512 SkScalar vec4transformed2[4];
513 a44.mapScalars(vec4, vec4transformed);
514 a33.mapHomogeneousPoints(vec3transformed, vec3, 1);
515 a44flattened.mapScalars(vec4, vec4transformed2);
516 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec3transformed[0]));
517 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec3transformed[1]));
518 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec3transformed[2]));
519 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec4transformed2[0]));
520 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec4transformed2[1]));
521 REPORTER_ASSERT(reporter, !nearly_equal_scalar(vec4transformed[2], vec4transformed2[2]));
522 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec4transformed2[3]));
523}
524
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000525DEF_TEST(Matrix44, reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000526 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor);
527 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor);
528 SkMatrix44 iden1(SkMatrix44::kUninitialized_Constructor);
529 SkMatrix44 iden2(SkMatrix44::kUninitialized_Constructor);
530 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor);
reed@google.com125002a2011-06-09 19:13:41 +0000531
reed@google.com7d683352012-12-03 21:19:52 +0000532 mat.setTranslate(1, 1, 1);
reed@google.com125002a2011-06-09 19:13:41 +0000533 mat.invert(&inverse);
534 iden1.setConcat(mat, inverse);
535 REPORTER_ASSERT(reporter, is_identity(iden1));
536
reed@google.com7d683352012-12-03 21:19:52 +0000537 mat.setScale(2, 2, 2);
reed@google.com125002a2011-06-09 19:13:41 +0000538 mat.invert(&inverse);
539 iden1.setConcat(mat, inverse);
540 REPORTER_ASSERT(reporter, is_identity(iden1));
541
reed@google.com7d683352012-12-03 21:19:52 +0000542 mat.setScale(SK_MScalar1/2, SK_MScalar1/2, SK_MScalar1/2);
reed@google.com125002a2011-06-09 19:13:41 +0000543 mat.invert(&inverse);
544 iden1.setConcat(mat, inverse);
545 REPORTER_ASSERT(reporter, is_identity(iden1));
546
reed@google.com7d683352012-12-03 21:19:52 +0000547 mat.setScale(3, 3, 3);
548 rot.setRotateDegreesAbout(0, 0, -1, 90);
reed@google.com125002a2011-06-09 19:13:41 +0000549 mat.postConcat(rot);
550 REPORTER_ASSERT(reporter, mat.invert(NULL));
551 mat.invert(&inverse);
552 iden1.setConcat(mat, inverse);
553 REPORTER_ASSERT(reporter, is_identity(iden1));
554 iden2.setConcat(inverse, mat);
555 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@google.comda9fac02011-06-13 14:46:52 +0000556
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000557 // test tiny-valued matrix inverse
558 mat.reset();
559 mat.setScale(1.0e-12, 1.0e-12, 1.0e-12);
560 rot.setRotateDegreesAbout(0, 0, -1, 90);
561 mat.postConcat(rot);
562 mat.postTranslate(1.0e-12, 1.0e-12, 1.0e-12);
563 REPORTER_ASSERT(reporter, mat.invert(NULL));
564 mat.invert(&inverse);
565 iden1.setConcat(mat, inverse);
566 REPORTER_ASSERT(reporter, is_identity(iden1));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000567
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000568 // test mixed-valued matrix inverse
569 mat.reset();
jvanverth@google.com25f72ed2013-09-03 19:46:16 +0000570 mat.setScale(1.0e-10, 3.0, 1.0e+10);
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000571 rot.setRotateDegreesAbout(0, 0, -1, 90);
572 mat.postConcat(rot);
jvanverth@google.com25f72ed2013-09-03 19:46:16 +0000573 mat.postTranslate(1.0e+10, 3.0, 1.0e-10);
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000574 REPORTER_ASSERT(reporter, mat.invert(NULL));
575 mat.invert(&inverse);
576 iden1.setConcat(mat, inverse);
577 REPORTER_ASSERT(reporter, is_identity(iden1));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000578
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000579 // test degenerate matrix
580 mat.reset();
581 mat.set3x3(1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0);
582 REPORTER_ASSERT(reporter, !mat.invert(NULL));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000583
reed@google.comda9fac02011-06-13 14:46:52 +0000584 // test rol/col Major getters
585 {
586 mat.setTranslate(2, 3, 4);
587 float dataf[16];
588 double datad[16];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000589
reed@google.comda9fac02011-06-13 14:46:52 +0000590 mat.asColMajorf(dataf);
591 assert16<float>(reporter, dataf,
592 1, 0, 0, 0,
593 0, 1, 0, 0,
594 0, 0, 1, 0,
595 2, 3, 4, 1);
596 mat.asColMajord(datad);
597 assert16<double>(reporter, datad, 1, 0, 0, 0,
598 0, 1, 0, 0,
599 0, 0, 1, 0,
600 2, 3, 4, 1);
601 mat.asRowMajorf(dataf);
602 assert16<float>(reporter, dataf, 1, 0, 0, 2,
603 0, 1, 0, 3,
604 0, 0, 1, 4,
605 0, 0, 0, 1);
606 mat.asRowMajord(datad);
607 assert16<double>(reporter, datad, 1, 0, 0, 2,
608 0, 1, 0, 3,
609 0, 0, 1, 4,
610 0, 0, 0, 1);
611 }
reed@google.com6f2b44d2011-06-24 18:13:39 +0000612
reed@google.com80b577e2012-11-09 21:25:06 +0000613 test_concat(reporter);
614
caryclark@google.com42639cd2012-06-06 12:03:39 +0000615 if (false) { // avoid bit rot, suppress warning (working on making this pass)
616 test_common_angles(reporter);
617 }
vollick@chromium.org3959a762012-11-13 15:08:22 +0000618
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000619 test_constructor(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000620 test_gettype(reporter);
vollick@chromium.org3959a762012-11-13 15:08:22 +0000621 test_determinant(reporter);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000622 test_invert(reporter);
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000623 test_transpose(reporter);
624 test_get_set_double(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000625 test_set_row_col_major(reporter);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000626 test_translate(reporter);
627 test_scale(reporter);
628 test_map2(reporter);
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000629 test_3x3_conversion(reporter);
reed@google.com125002a2011-06-09 19:13:41 +0000630}