blob: d0311f3fecffdc5d8905dc7692a92d888268dc95 [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 "SkMatrix44.h"
Cary Clarke4442cb2017-10-18 11:46:18 -04009#include "SkPoint3.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
reed@google.com125002a2011-06-09 19:13:41 +000011
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
Mike Klein4429a4f2018-10-04 09:06:00 -040081 SkMatrix44* placeholderMatrix = new SkMatrix44;
Ben Wagner145dbcd2016-11-03 14:40:50 -040082 std::unique_ptr<SkMatrix44> deleteMe(placeholderMatrix);
robertphillips@google.com35300c42013-03-21 17:38:49 +000083
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());
fs88640cf2014-12-16 08:36:11 -0800108
109 // Verify that that constructing from an SkMatrix initializes everything.
Mike Klein4429a4f2018-10-04 09:06:00 -0400110 SkMatrix44 scaleMatrix;
fs88640cf2014-12-16 08:36:11 -0800111 scaleMatrix.setScale(3, 4, 5);
112 REPORTER_ASSERT(reporter, scaleMatrix.isScale());
113 testMatrix = new(&scaleMatrix) SkMatrix44(SkMatrix::I());
114 REPORTER_ASSERT(reporter, testMatrix->isIdentity());
115 REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I());
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000116}
117
reed@google.com99b5c7f2012-12-05 22:13:59 +0000118static void test_translate(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400119 SkMatrix44 mat;
120 SkMatrix44 inverse;
reed@google.com99b5c7f2012-12-05 22:13:59 +0000121
122 mat.setTranslate(0, 0, 0);
123 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
124 mat.setTranslate(1, 2, 3);
125 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kTranslate_Mask));
126 REPORTER_ASSERT(reporter, mat.invert(&inverse));
127 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kTranslate_Mask));
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000128
Mike Klein4429a4f2018-10-04 09:06:00 -0400129 SkMatrix44 a,b,c;
reed@google.com99b5c7f2012-12-05 22:13:59 +0000130 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
131 b.setTranslate(10, 11, 12);
132
133 c.setConcat(a, b);
134 mat = a;
135 mat.preTranslate(10, 11, 12);
136 REPORTER_ASSERT(reporter, mat == c);
137
138 c.setConcat(b, a);
139 mat = a;
140 mat.postTranslate(10, 11, 12);
141 REPORTER_ASSERT(reporter, mat == c);
142}
143
144static void test_scale(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400145 SkMatrix44 mat;
146 SkMatrix44 inverse;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000147
reed@google.com99b5c7f2012-12-05 22:13:59 +0000148 mat.setScale(1, 1, 1);
149 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
150 mat.setScale(1, 2, 3);
151 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask));
152 REPORTER_ASSERT(reporter, mat.invert(&inverse));
153 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_Mask));
154
Mike Klein4429a4f2018-10-04 09:06:00 -0400155 SkMatrix44 a,b,c;
reed@google.com99b5c7f2012-12-05 22:13:59 +0000156 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
157 b.setScale(10, 11, 12);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000158
reed@google.com99b5c7f2012-12-05 22:13:59 +0000159 c.setConcat(a, b);
160 mat = a;
161 mat.preScale(10, 11, 12);
162 REPORTER_ASSERT(reporter, mat == c);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000163
reed@google.com99b5c7f2012-12-05 22:13:59 +0000164 c.setConcat(b, a);
165 mat = a;
166 mat.postScale(10, 11, 12);
167 REPORTER_ASSERT(reporter, mat == c);
168}
169
170static void make_i(SkMatrix44* mat) { mat->setIdentity(); }
171static void make_t(SkMatrix44* mat) { mat->setTranslate(1, 2, 3); }
172static void make_s(SkMatrix44* mat) { mat->setScale(1, 2, 3); }
173static void make_st(SkMatrix44* mat) {
174 mat->setScale(1, 2, 3);
175 mat->postTranslate(1, 2, 3);
176}
177static void make_a(SkMatrix44* mat) {
178 mat->setRotateDegreesAbout(1, 2, 3, 45);
179}
180static void make_p(SkMatrix44* mat) {
181 SkMScalar data[] = {
182 1, 2, 3, 4, 5, 6, 7, 8,
183 1, 2, 3, 4, 5, 6, 7, 8,
184 };
185 mat->setRowMajor(data);
186}
187
188typedef void (*Make44Proc)(SkMatrix44*);
189
190static const Make44Proc gMakeProcs[] = {
191 make_i, make_t, make_s, make_st, make_a, make_p
192};
193
194static void test_map2(skiatest::Reporter* reporter, const SkMatrix44& mat) {
195 SkMScalar src2[] = { 1, 2 };
196 SkMScalar src4[] = { src2[0], src2[1], 0, 1 };
197 SkMScalar dstA[4], dstB[4];
198
199 for (int i = 0; i < 4; ++i) {
mtklein1831f992015-06-09 11:47:01 -0700200 dstA[i] = SkDoubleToMScalar(123456789);
201 dstB[i] = SkDoubleToMScalar(987654321);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000202 }
203
204 mat.map2(src2, 1, dstA);
205 mat.mapMScalars(src4, dstB);
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000206
reed@google.com99b5c7f2012-12-05 22:13:59 +0000207 for (int i = 0; i < 4; ++i) {
208 REPORTER_ASSERT(reporter, dstA[i] == dstB[i]);
209 }
210}
211
212static void test_map2(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400213 SkMatrix44 mat;
reed@google.com99b5c7f2012-12-05 22:13:59 +0000214
215 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) {
216 gMakeProcs[i](&mat);
217 test_map2(reporter, mat);
218 }
219}
220
reed@google.com7d683352012-12-03 21:19:52 +0000221static void test_gettype(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000222 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000223
reed@google.com7d683352012-12-03 21:19:52 +0000224 REPORTER_ASSERT(reporter, matrix.isIdentity());
225 REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType());
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000226
reed@google.com7d683352012-12-03 21:19:52 +0000227 int expectedMask;
228
229 matrix.set(1, 1, 0);
230 expectedMask = SkMatrix44::kScale_Mask;
231 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
232
233 matrix.set(0, 3, 1); // translate-x
234 expectedMask |= SkMatrix44::kTranslate_Mask;
235 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
236
237 matrix.set(2, 0, 1);
238 expectedMask |= SkMatrix44::kAffine_Mask;
239 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000240
reed@google.com7d683352012-12-03 21:19:52 +0000241 matrix.set(3, 2, 1);
242 REPORTER_ASSERT(reporter, matrix.getType() & SkMatrix44::kPerspective_Mask);
reed@google.com87f99cb2013-04-19 12:25:00 +0000243
244 // ensure that negative zero is treated as zero
245 SkMScalar dx = 0;
246 SkMScalar dy = 0;
247 SkMScalar dz = 0;
248 matrix.setTranslate(-dx, -dy, -dz);
249 REPORTER_ASSERT(reporter, matrix.isIdentity());
250 matrix.preTranslate(-dx, -dy, -dz);
251 REPORTER_ASSERT(reporter, matrix.isIdentity());
252 matrix.postTranslate(-dx, -dy, -dz);
253 REPORTER_ASSERT(reporter, matrix.isIdentity());
reed@google.com7d683352012-12-03 21:19:52 +0000254}
255
reed@google.com6f2b44d2011-06-24 18:13:39 +0000256static void test_common_angles(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400257 SkMatrix44 rot;
reed@google.com6f2b44d2011-06-24 18:13:39 +0000258 // Test precision of rotation in common cases
259 int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 };
260 for (int i = 0; i < 9; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000261 rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i]));
reed@google.com6f2b44d2011-06-24 18:13:39 +0000262
263 SkMatrix rot3x3 = rot;
264 REPORTER_ASSERT(reporter, rot3x3.rectStaysRect());
265 }
266}
267
reed@google.com80b577e2012-11-09 21:25:06 +0000268static void test_concat(skiatest::Reporter* reporter) {
269 int i;
Mike Klein4429a4f2018-10-04 09:06:00 -0400270 SkMatrix44 a,b,c,d;
reed@google.com80b577e2012-11-09 21:25:06 +0000271
272 a.setTranslate(10, 10, 10);
273 b.setScale(2, 2, 2);
274
275 SkScalar src[8] = {
276 0, 0, 0, 1,
277 1, 1, 1, 1
278 };
279 SkScalar dst[8];
280
281 c.setConcat(a, b);
282
283 d = a;
284 d.preConcat(b);
285 REPORTER_ASSERT(reporter, d == c);
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000286
reed@google.com1ea95be2012-11-09 21:39:48 +0000287 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000288 for (i = 0; i < 3; ++i) {
289 REPORTER_ASSERT(reporter, 10 == dst[i]);
290 REPORTER_ASSERT(reporter, 12 == dst[i + 4]);
291 }
skia.committer@gmail.com453995e2012-11-10 02:01:26 +0000292
reed@google.com80b577e2012-11-09 21:25:06 +0000293 c.setConcat(b, a);
294
295 d = a;
296 d.postConcat(b);
297 REPORTER_ASSERT(reporter, d == c);
298
reed@google.com1ea95be2012-11-09 21:39:48 +0000299 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
reed@google.com80b577e2012-11-09 21:25:06 +0000300 for (i = 0; i < 3; ++i) {
301 REPORTER_ASSERT(reporter, 20 == dst[i]);
302 REPORTER_ASSERT(reporter, 22 == dst[i + 4]);
303 }
304}
305
vollick@chromium.org3959a762012-11-13 15:08:22 +0000306static void test_determinant(skiatest::Reporter* reporter) {
reed@google.com44699382013-10-31 17:28:30 +0000307 SkMatrix44 a(SkMatrix44::kIdentity_Constructor);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000308 REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000309 a.set(1, 1, 2);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000310 REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant()));
Mike Klein4429a4f2018-10-04 09:06:00 -0400311 SkMatrix44 b;
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000312 REPORTER_ASSERT(reporter, a.invert(&b));
313 REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant()));
314 SkMatrix44 c = b = a;
reed@google.com7d683352012-12-03 21:19:52 +0000315 c.set(0, 1, 4);
316 b.set(1, 0, 4);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000317 REPORTER_ASSERT(reporter,
318 nearly_equal_double(a.determinant(),
319 b.determinant()));
320 SkMatrix44 d = a;
reed@google.com7d683352012-12-03 21:19:52 +0000321 d.set(0, 0, 8);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000322 REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000323
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000324 SkMatrix44 e = a;
325 e.postConcat(d);
326 REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant()));
reed@google.com7d683352012-12-03 21:19:52 +0000327 e.set(0, 0, 0);
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000328 REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant()));
vollick@chromium.org3959a762012-11-13 15:08:22 +0000329}
330
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000331static void test_invert(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400332 SkMatrix44 inverse;
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000333 double inverseData[16];
334
reed@google.com44699382013-10-31 17:28:30 +0000335 SkMatrix44 identity(SkMatrix44::kIdentity_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000336 identity.invert(&inverse);
337 inverse.asRowMajord(inverseData);
338 assert16<double>(reporter, inverseData,
339 1, 0, 0, 0,
340 0, 1, 0, 0,
341 0, 0, 1, 0,
342 0, 0, 0, 1);
343
Mike Klein4429a4f2018-10-04 09:06:00 -0400344 SkMatrix44 translation;
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000345 translation.setTranslate(2, 3, 4);
346 translation.invert(&inverse);
347 inverse.asRowMajord(inverseData);
348 assert16<double>(reporter, inverseData,
349 1, 0, 0, -2,
350 0, 1, 0, -3,
351 0, 0, 1, -4,
352 0, 0, 0, 1);
353
Mike Klein4429a4f2018-10-04 09:06:00 -0400354 SkMatrix44 scale;
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000355 scale.setScale(2, 4, 8);
356 scale.invert(&inverse);
357 inverse.asRowMajord(inverseData);
358 assert16<double>(reporter, inverseData,
359 0.5, 0, 0, 0,
360 0, 0.25, 0, 0,
361 0, 0, 0.125, 0,
362 0, 0, 0, 1);
363
Mike Klein4429a4f2018-10-04 09:06:00 -0400364 SkMatrix44 scaleTranslation;
mtklein51958052015-06-09 15:06:22 -0700365 scaleTranslation.setScale(32, 128, 1024);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000366 scaleTranslation.preTranslate(2, 3, 4);
367 scaleTranslation.invert(&inverse);
368 inverse.asRowMajord(inverseData);
369 assert16<double>(reporter, inverseData,
mtklein51958052015-06-09 15:06:22 -0700370 0.03125, 0, 0, -2,
371 0, 0.0078125, 0, -3,
372 0, 0, 0.0009765625, -4,
373 0, 0, 0, 1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000374
Mike Klein4429a4f2018-10-04 09:06:00 -0400375 SkMatrix44 rotation;
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000376 rotation.setRotateDegreesAbout(0, 0, 1, 90);
377 rotation.invert(&inverse);
Mike Klein4429a4f2018-10-04 09:06:00 -0400378 SkMatrix44 expected;
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000379 double expectedInverseRotation[16] =
380 {0, 1, 0, 0,
381 -1, 0, 0, 0,
382 0, 0, 1, 0,
383 0, 0, 0, 1};
384 expected.setRowMajord(expectedInverseRotation);
385 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000386
Mike Klein4429a4f2018-10-04 09:06:00 -0400387 SkMatrix44 affine;
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000388 affine.setRotateDegreesAbout(0, 0, 1, 90);
389 affine.preScale(10, 20, 100);
390 affine.preTranslate(2, 3, 4);
391 affine.invert(&inverse);
392 double expectedInverseAffine[16] =
393 {0, 0.1, 0, -2,
394 -0.05, 0, 0, -3,
395 0, 0, 0.01, -4,
396 0, 0, 0, 1};
397 expected.setRowMajord(expectedInverseAffine);
398 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
399
reed@google.com44699382013-10-31 17:28:30 +0000400 SkMatrix44 perspective(SkMatrix44::kIdentity_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000401 perspective.setDouble(3, 2, 1.0);
402 perspective.invert(&inverse);
403 double expectedInversePerspective[16] =
404 {1, 0, 0, 0,
405 0, 1, 0, 0,
406 0, 0, 1, 0,
407 0, 0, -1, 1};
408 expected.setRowMajord(expectedInversePerspective);
409 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
410
reed@google.com44699382013-10-31 17:28:30 +0000411 SkMatrix44 affineAndPerspective(SkMatrix44::kIdentity_Constructor);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000412 affineAndPerspective.setDouble(3, 2, 1.0);
413 affineAndPerspective.preScale(10, 20, 100);
414 affineAndPerspective.preTranslate(2, 3, 4);
415 affineAndPerspective.invert(&inverse);
416 double expectedInverseAffineAndPerspective[16] =
417 {0.1, 0, 2, -2,
418 0, 0.05, 3, -3,
419 0, 0, 4.01, -4,
420 0, 0, -1, 1};
421 expected.setRowMajord(expectedInverseAffineAndPerspective);
422 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
vmpstra8d45592015-06-30 13:36:04 -0700423
424 SkMatrix44 tinyScale(SkMatrix44::kIdentity_Constructor);
425 tinyScale.setDouble(0, 0, 1e-39);
426 REPORTER_ASSERT(reporter, tinyScale.getType() == SkMatrix44::kScale_Mask);
halcanary96fcdcc2015-08-27 07:41:13 -0700427 REPORTER_ASSERT(reporter, !tinyScale.invert(nullptr));
vmpstra8d45592015-06-30 13:36:04 -0700428 REPORTER_ASSERT(reporter, !tinyScale.invert(&inverse));
429
430 SkMatrix44 tinyScaleTranslate(SkMatrix44::kIdentity_Constructor);
431 tinyScaleTranslate.setDouble(0, 0, 1e-38);
halcanary96fcdcc2015-08-27 07:41:13 -0700432 REPORTER_ASSERT(reporter, tinyScaleTranslate.invert(nullptr));
vmpstra8d45592015-06-30 13:36:04 -0700433 tinyScaleTranslate.setDouble(0, 3, 10);
434 REPORTER_ASSERT(
435 reporter, tinyScaleTranslate.getType() ==
436 (SkMatrix44::kScale_Mask | SkMatrix44::kTranslate_Mask));
halcanary96fcdcc2015-08-27 07:41:13 -0700437 REPORTER_ASSERT(reporter, !tinyScaleTranslate.invert(nullptr));
vmpstra8d45592015-06-30 13:36:04 -0700438 REPORTER_ASSERT(reporter, !tinyScaleTranslate.invert(&inverse));
439
440 SkMatrix44 tinyScalePerspective(SkMatrix44::kIdentity_Constructor);
441 tinyScalePerspective.setDouble(0, 0, 1e-39);
442 tinyScalePerspective.setDouble(3, 2, -1);
443 REPORTER_ASSERT(reporter, (tinyScalePerspective.getType() &
444 SkMatrix44::kPerspective_Mask) ==
445 SkMatrix44::kPerspective_Mask);
halcanary96fcdcc2015-08-27 07:41:13 -0700446 REPORTER_ASSERT(reporter, !tinyScalePerspective.invert(nullptr));
vmpstra8d45592015-06-30 13:36:04 -0700447 REPORTER_ASSERT(reporter, !tinyScalePerspective.invert(&inverse));
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000448}
449
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000450static void test_transpose(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400451 SkMatrix44 a,b;
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000452
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000453 int i = 0;
454 for (int row = 0; row < 4; ++row) {
455 for (int col = 0; col < 4; ++col) {
456 a.setDouble(row, col, i);
457 b.setDouble(col, row, i++);
458 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000459 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000460
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000461 a.transpose();
462 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000463}
464
465static void test_get_set_double(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400466 SkMatrix44 a;
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000467 for (int row = 0; row < 4; ++row) {
468 for (int col = 0; col < 4; ++col) {
469 a.setDouble(row, col, 3.141592653589793);
470 REPORTER_ASSERT(reporter,
471 nearly_equal_double(3.141592653589793,
472 a.getDouble(row, col)));
473 a.setDouble(row, col, 0);
474 REPORTER_ASSERT(reporter,
475 nearly_equal_double(0, a.getDouble(row, col)));
476 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000477 }
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000478}
479
msarettc1a3e242016-06-23 12:42:29 -0700480static void test_set_3x3(skiatest::Reporter* r) {
481 static float vals[9] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, };
482
Mike Klein4429a4f2018-10-04 09:06:00 -0400483 SkMatrix44 mat;
msarettc1a3e242016-06-23 12:42:29 -0700484 mat.set3x3RowMajorf(vals);
485
486 REPORTER_ASSERT(r, 1.0f == mat.getFloat(0, 0));
487 REPORTER_ASSERT(r, 2.0f == mat.getFloat(0, 1));
488 REPORTER_ASSERT(r, 3.0f == mat.getFloat(0, 2));
489 REPORTER_ASSERT(r, 4.0f == mat.getFloat(1, 0));
490 REPORTER_ASSERT(r, 5.0f == mat.getFloat(1, 1));
491 REPORTER_ASSERT(r, 6.0f == mat.getFloat(1, 2));
492 REPORTER_ASSERT(r, 7.0f == mat.getFloat(2, 0));
493 REPORTER_ASSERT(r, 8.0f == mat.getFloat(2, 1));
494 REPORTER_ASSERT(r, 9.0f == mat.getFloat(2, 2));
495}
496
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000497static void test_set_row_col_major(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400498 SkMatrix44 a,b;
reed@google.com44699382013-10-31 17:28:30 +0000499
reed@google.com7d683352012-12-03 21:19:52 +0000500 for (int row = 0; row < 4; ++row) {
501 for (int col = 0; col < 4; ++col) {
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000502 a.setDouble(row, col, row * 4 + col);
reed@google.com7d683352012-12-03 21:19:52 +0000503 }
504 }
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000505
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000506 double bufferd[16];
507 float bufferf[16];
508 a.asColMajord(bufferd);
509 b.setColMajord(bufferd);
510 REPORTER_ASSERT(reporter, nearly_equal(a, b));
511 b.setRowMajord(bufferd);
512 b.transpose();
513 REPORTER_ASSERT(reporter, nearly_equal(a, b));
514 a.asColMajorf(bufferf);
515 b.setColMajorf(bufferf);
516 REPORTER_ASSERT(reporter, nearly_equal(a, b));
517 b.setRowMajorf(bufferf);
518 b.transpose();
519 REPORTER_ASSERT(reporter, nearly_equal(a, b));
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000520}
521
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000522static void test_3x3_conversion(skiatest::Reporter* reporter) {
523 SkMScalar values4x4[16] = { 1, 2, 3, 4,
524 5, 6, 7, 8,
525 9, 10, 11, 12,
526 13, 14, 15, 16 };
527 SkScalar values3x3[9] = { 1, 2, 4,
528 5, 6, 8,
529 13, 14, 16 };
530 SkMScalar values4x4flattened[16] = { 1, 2, 0, 4,
531 5, 6, 0, 8,
532 0, 0, 1, 0,
533 13, 14, 0, 16 };
Mike Klein4429a4f2018-10-04 09:06:00 -0400534 SkMatrix44 a44;
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000535 a44.setRowMajor(values4x4);
536
537 SkMatrix a33 = a44;
538 SkMatrix expected33;
539 for (int i = 0; i < 9; i++) expected33[i] = values3x3[i];
540 REPORTER_ASSERT(reporter, expected33 == a33);
541
542 SkMatrix44 a44flattened = a33;
Mike Klein4429a4f2018-10-04 09:06:00 -0400543 SkMatrix44 expected44flattened;
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000544 expected44flattened.setRowMajor(values4x4flattened);
545 REPORTER_ASSERT(reporter, nearly_equal(a44flattened, expected44flattened));
546
547 // Test that a point with a Z value of 0 is transformed the same way.
548 SkScalar vec4[4] = { 2, 4, 0, 8 };
Cary Clarke4442cb2017-10-18 11:46:18 -0400549 SkPoint3 vec3 = { 2, 4, 8 };
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000550
551 SkScalar vec4transformed[4];
Cary Clarke4442cb2017-10-18 11:46:18 -0400552 SkPoint3 vec3transformed;
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000553 SkScalar vec4transformed2[4];
554 a44.mapScalars(vec4, vec4transformed);
Cary Clarke4442cb2017-10-18 11:46:18 -0400555 a33.mapHomogeneousPoints(&vec3transformed, &vec3, 1);
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000556 a44flattened.mapScalars(vec4, vec4transformed2);
Cary Clarke4442cb2017-10-18 11:46:18 -0400557 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec3transformed.fX));
558 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec3transformed.fY));
559 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec3transformed.fZ));
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000560 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec4transformed2[0]));
561 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec4transformed2[1]));
562 REPORTER_ASSERT(reporter, !nearly_equal_scalar(vec4transformed[2], vec4transformed2[2]));
563 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec4transformed2[3]));
564}
565
tomhudsona32f1752014-09-16 08:29:29 -0700566static void test_has_perspective(skiatest::Reporter* reporter) {
567 SkMatrix44 transform(SkMatrix44::kIdentity_Constructor);
568
mtklein1831f992015-06-09 11:47:01 -0700569 transform.setDouble(3, 2, -0.1);
tomhudsona32f1752014-09-16 08:29:29 -0700570 REPORTER_ASSERT(reporter, transform.hasPerspective());
571
572 transform.reset();
573 REPORTER_ASSERT(reporter, !transform.hasPerspective());
574
mtklein1831f992015-06-09 11:47:01 -0700575 transform.setDouble(3, 0, -1.0);
tomhudsona32f1752014-09-16 08:29:29 -0700576 REPORTER_ASSERT(reporter, transform.hasPerspective());
577
578 transform.reset();
mtklein1831f992015-06-09 11:47:01 -0700579 transform.setDouble(3, 1, -1.0);
tomhudsona32f1752014-09-16 08:29:29 -0700580 REPORTER_ASSERT(reporter, transform.hasPerspective());
581
582 transform.reset();
mtklein1831f992015-06-09 11:47:01 -0700583 transform.setDouble(3, 2, -0.3);
tomhudsona32f1752014-09-16 08:29:29 -0700584 REPORTER_ASSERT(reporter, transform.hasPerspective());
585
586 transform.reset();
mtklein1831f992015-06-09 11:47:01 -0700587 transform.setDouble(3, 3, 0.5);
tomhudsona32f1752014-09-16 08:29:29 -0700588 REPORTER_ASSERT(reporter, transform.hasPerspective());
mtklein1831f992015-06-09 11:47:01 -0700589
tomhudsona32f1752014-09-16 08:29:29 -0700590 transform.reset();
mtklein1831f992015-06-09 11:47:01 -0700591 transform.setDouble(3, 3, 0.0);
tomhudsona32f1752014-09-16 08:29:29 -0700592 REPORTER_ASSERT(reporter, transform.hasPerspective());
593}
594
tomhudsonfaccb8e2014-09-26 11:45:48 -0700595static bool is_rectilinear (SkVector4& p1, SkVector4& p2, SkVector4& p3, SkVector4& p4) {
596 return (SkScalarNearlyEqual(p1.fData[0], p2.fData[0]) &&
597 SkScalarNearlyEqual(p2.fData[1], p3.fData[1]) &&
598 SkScalarNearlyEqual(p3.fData[0], p4.fData[0]) &&
599 SkScalarNearlyEqual(p4.fData[1], p1.fData[1])) ||
600 (SkScalarNearlyEqual(p1.fData[1], p2.fData[1]) &&
601 SkScalarNearlyEqual(p2.fData[0], p3.fData[0]) &&
602 SkScalarNearlyEqual(p3.fData[1], p4.fData[1]) &&
603 SkScalarNearlyEqual(p4.fData[0], p1.fData[0]));
604}
605
606static SkVector4 mul_with_persp_divide(const SkMatrix44& transform, const SkVector4& target) {
607 SkVector4 result = transform * target;
608 if (result.fData[3] != 0.0f && result.fData[3] != SK_Scalar1) {
609 float wInverse = SK_Scalar1 / result.fData[3];
610 result.set(result.fData[0] * wInverse,
611 result.fData[1] * wInverse,
612 result.fData[2] * wInverse,
613 SK_Scalar1);
614 }
615 return result;
616}
617
618static bool empirically_preserves_2d_axis_alignment(skiatest::Reporter* reporter,
619 const SkMatrix44& transform) {
620 SkVector4 p1(5.0f, 5.0f, 0.0f);
621 SkVector4 p2(10.0f, 5.0f, 0.0f);
622 SkVector4 p3(10.0f, 20.0f, 0.0f);
623 SkVector4 p4(5.0f, 20.0f, 0.0f);
624
625 REPORTER_ASSERT(reporter, is_rectilinear(p1, p2, p3, p4));
626
627 p1 = mul_with_persp_divide(transform, p1);
628 p2 = mul_with_persp_divide(transform, p2);
629 p3 = mul_with_persp_divide(transform, p3);
630 p4 = mul_with_persp_divide(transform, p4);
631
632 return is_rectilinear(p1, p2, p3, p4);
633}
634
635static void test(bool expected, skiatest::Reporter* reporter, const SkMatrix44& transform) {
636 if (expected) {
637 REPORTER_ASSERT(reporter, empirically_preserves_2d_axis_alignment(reporter, transform));
638 REPORTER_ASSERT(reporter, transform.preserves2dAxisAlignment());
639 } else {
640 REPORTER_ASSERT(reporter, !empirically_preserves_2d_axis_alignment(reporter, transform));
641 REPORTER_ASSERT(reporter, !transform.preserves2dAxisAlignment());
642 }
643}
644
645static void test_preserves_2d_axis_alignment(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400646 SkMatrix44 transform;
647 SkMatrix44 transform2;
tomhudsonfaccb8e2014-09-26 11:45:48 -0700648
649 static const struct TestCase {
650 SkMScalar a; // row 1, column 1
651 SkMScalar b; // row 1, column 2
652 SkMScalar c; // row 2, column 1
653 SkMScalar d; // row 2, column 2
654 bool expected;
655 } test_cases[] = {
656 { 3.f, 0.f,
657 0.f, 4.f, true }, // basic case
658 { 0.f, 4.f,
659 3.f, 0.f, true }, // rotate by 90
660 { 0.f, 0.f,
661 0.f, 4.f, true }, // degenerate x
662 { 3.f, 0.f,
663 0.f, 0.f, true }, // degenerate y
664 { 0.f, 0.f,
665 3.f, 0.f, true }, // degenerate x + rotate by 90
666 { 0.f, 4.f,
667 0.f, 0.f, true }, // degenerate y + rotate by 90
668 { 3.f, 4.f,
669 0.f, 0.f, false },
670 { 0.f, 0.f,
671 3.f, 4.f, false },
672 { 0.f, 3.f,
673 0.f, 4.f, false },
674 { 3.f, 0.f,
675 4.f, 0.f, false },
676 { 3.f, 4.f,
677 5.f, 0.f, false },
678 { 3.f, 4.f,
679 0.f, 5.f, false },
680 { 3.f, 0.f,
681 4.f, 5.f, false },
682 { 0.f, 3.f,
683 4.f, 5.f, false },
684 { 2.f, 3.f,
685 4.f, 5.f, false },
686 };
687
688 for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) {
689 const TestCase& value = test_cases[i];
690 transform.setIdentity();
691 transform.set(0, 0, value.a);
692 transform.set(0, 1, value.b);
693 transform.set(1, 0, value.c);
694 transform.set(1, 1, value.d);
695
696 test(value.expected, reporter, transform);
697 }
698
699 // Try the same test cases again, but this time make sure that other matrix
700 // elements (except perspective) have entries, to test that they are ignored.
701 for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) {
702 const TestCase& value = test_cases[i];
703 transform.setIdentity();
704 transform.set(0, 0, value.a);
705 transform.set(0, 1, value.b);
706 transform.set(1, 0, value.c);
707 transform.set(1, 1, value.d);
708
709 transform.set(0, 2, 1.f);
710 transform.set(0, 3, 2.f);
711 transform.set(1, 2, 3.f);
712 transform.set(1, 3, 4.f);
713 transform.set(2, 0, 5.f);
714 transform.set(2, 1, 6.f);
715 transform.set(2, 2, 7.f);
716 transform.set(2, 3, 8.f);
717
718 test(value.expected, reporter, transform);
719 }
720
721 // Try the same test cases again, but this time add perspective which is
722 // always assumed to not-preserve axis alignment.
723 for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) {
724 const TestCase& value = test_cases[i];
725 transform.setIdentity();
726 transform.set(0, 0, value.a);
727 transform.set(0, 1, value.b);
728 transform.set(1, 0, value.c);
729 transform.set(1, 1, value.d);
730
731 transform.set(0, 2, 1.f);
732 transform.set(0, 3, 2.f);
733 transform.set(1, 2, 3.f);
734 transform.set(1, 3, 4.f);
735 transform.set(2, 0, 5.f);
736 transform.set(2, 1, 6.f);
737 transform.set(2, 2, 7.f);
738 transform.set(2, 3, 8.f);
739 transform.set(3, 0, 9.f);
740 transform.set(3, 1, 10.f);
741 transform.set(3, 2, 11.f);
742 transform.set(3, 3, 12.f);
743
744 test(false, reporter, transform);
745 }
746
747 // Try a few more practical situations to check precision
748 // Reuse TestCase (a, b, c, d) as (x, y, z, degrees) axis to rotate about.
749 TestCase rotation_tests[] = {
750 { 0.0, 0.0, 1.0, 90.0, true },
751 { 0.0, 0.0, 1.0, 180.0, true },
752 { 0.0, 0.0, 1.0, 270.0, true },
753 { 0.0, 1.0, 0.0, 90.0, true },
754 { 1.0, 0.0, 0.0, 90.0, true },
755 { 0.0, 0.0, 1.0, 45.0, false },
756 // In 3d these next two are non-preserving, but we're testing in 2d after
757 // orthographic projection, where they are.
758 { 0.0, 1.0, 0.0, 45.0, true },
759 { 1.0, 0.0, 0.0, 45.0, true },
760 };
761
762 for (size_t i = 0; i < sizeof(rotation_tests)/sizeof(TestCase); ++i) {
763 const TestCase& value = rotation_tests[i];
764 transform.setRotateDegreesAbout(value.a, value.b, value.c, value.d);
765 test(value.expected, reporter, transform);
766 }
767
768 static const struct DoubleRotationCase {
769 SkMScalar x1;
770 SkMScalar y1;
771 SkMScalar z1;
772 SkMScalar degrees1;
773 SkMScalar x2;
774 SkMScalar y2;
775 SkMScalar z2;
776 SkMScalar degrees2;
777 bool expected;
778 } double_rotation_tests[] = {
779 { 0.0, 0.0, 1.0, 90.0, 0.0, 1.0, 0.0, 90.0, true },
780 { 0.0, 0.0, 1.0, 90.0, 1.0, 0.0, 0.0, 90.0, true },
781 { 0.0, 1.0, 0.0, 90.0, 0.0, 0.0, 1.0, 90.0, true },
782 };
783
784 for (size_t i = 0; i < sizeof(double_rotation_tests)/sizeof(DoubleRotationCase); ++i) {
785 const DoubleRotationCase& value = double_rotation_tests[i];
786 transform.setRotateDegreesAbout(value.x1, value.y1, value.z1, value.degrees1);
787 transform2.setRotateDegreesAbout(value.x2, value.y2, value.z2, value.degrees2);
788 transform.postConcat(transform2);
789 test(value.expected, reporter, transform);
790 }
791
792 // Perspective cases.
793 transform.setIdentity();
mtklein1831f992015-06-09 11:47:01 -0700794 transform.setDouble(3, 2, -0.1); // Perspective depth 10
tomhudsonfaccb8e2014-09-26 11:45:48 -0700795 transform2.setRotateDegreesAbout(0.0, 1.0, 0.0, 45.0);
796 transform.preConcat(transform2);
797 test(false, reporter, transform);
798
799 transform.setIdentity();
mtklein1831f992015-06-09 11:47:01 -0700800 transform.setDouble(3, 2, -0.1); // Perspective depth 10
tomhudsonfaccb8e2014-09-26 11:45:48 -0700801 transform2.setRotateDegreesAbout(0.0, 0.0, 1.0, 90.0);
802 transform.preConcat(transform2);
803 test(true, reporter, transform);
804}
805
reed39393e32014-10-21 12:33:21 -0700806// just want to exercise the various converters for MScalar
807static void test_toint(skiatest::Reporter* reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400808 SkMatrix44 mat;
reed39393e32014-10-21 12:33:21 -0700809 mat.setScale(3, 3, 3);
810
811 SkMScalar sum = SkMScalarFloor(mat.get(0, 0)) +
812 SkMScalarRound(mat.get(1, 0)) +
813 SkMScalarCeil(mat.get(2, 0));
814 int isum = SkMScalarFloorToInt(mat.get(0, 1)) +
815 SkMScalarRoundToInt(mat.get(1, 2)) +
816 SkMScalarCeilToInt(mat.get(2, 3));
817 REPORTER_ASSERT(reporter, sum >= 0);
818 REPORTER_ASSERT(reporter, isum >= 0);
819 REPORTER_ASSERT(reporter, static_cast<SkMScalar>(isum) == SkIntToMScalar(isum));
820}
tomhudsonfaccb8e2014-09-26 11:45:48 -0700821
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000822DEF_TEST(Matrix44, reporter) {
Mike Klein4429a4f2018-10-04 09:06:00 -0400823 SkMatrix44 mat;
824 SkMatrix44 inverse;
825 SkMatrix44 iden1;
826 SkMatrix44 iden2;
827 SkMatrix44 rot;
reed@google.com125002a2011-06-09 19:13:41 +0000828
reed@google.com7d683352012-12-03 21:19:52 +0000829 mat.setTranslate(1, 1, 1);
reed@google.com125002a2011-06-09 19:13:41 +0000830 mat.invert(&inverse);
831 iden1.setConcat(mat, inverse);
832 REPORTER_ASSERT(reporter, is_identity(iden1));
833
reed@google.com7d683352012-12-03 21:19:52 +0000834 mat.setScale(2, 2, 2);
reed@google.com125002a2011-06-09 19:13:41 +0000835 mat.invert(&inverse);
836 iden1.setConcat(mat, inverse);
837 REPORTER_ASSERT(reporter, is_identity(iden1));
838
reed@google.com7d683352012-12-03 21:19:52 +0000839 mat.setScale(SK_MScalar1/2, SK_MScalar1/2, SK_MScalar1/2);
reed@google.com125002a2011-06-09 19:13:41 +0000840 mat.invert(&inverse);
841 iden1.setConcat(mat, inverse);
842 REPORTER_ASSERT(reporter, is_identity(iden1));
843
reed@google.com7d683352012-12-03 21:19:52 +0000844 mat.setScale(3, 3, 3);
845 rot.setRotateDegreesAbout(0, 0, -1, 90);
reed@google.com125002a2011-06-09 19:13:41 +0000846 mat.postConcat(rot);
halcanary96fcdcc2015-08-27 07:41:13 -0700847 REPORTER_ASSERT(reporter, mat.invert(nullptr));
reed@google.com125002a2011-06-09 19:13:41 +0000848 mat.invert(&inverse);
849 iden1.setConcat(mat, inverse);
850 REPORTER_ASSERT(reporter, is_identity(iden1));
851 iden2.setConcat(inverse, mat);
852 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@google.comda9fac02011-06-13 14:46:52 +0000853
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000854 // test tiny-valued matrix inverse
855 mat.reset();
mtklein1831f992015-06-09 11:47:01 -0700856 auto v = SkDoubleToMScalar(1.0e-12);
857 mat.setScale(v,v,v);
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000858 rot.setRotateDegreesAbout(0, 0, -1, 90);
859 mat.postConcat(rot);
mtklein1831f992015-06-09 11:47:01 -0700860 mat.postTranslate(v,v,v);
halcanary96fcdcc2015-08-27 07:41:13 -0700861 REPORTER_ASSERT(reporter, mat.invert(nullptr));
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000862 mat.invert(&inverse);
863 iden1.setConcat(mat, inverse);
864 REPORTER_ASSERT(reporter, is_identity(iden1));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000865
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000866 // test mixed-valued matrix inverse
867 mat.reset();
mtklein51958052015-06-09 15:06:22 -0700868 mat.setScale(SkDoubleToMScalar(1.0e-2),
mtklein1831f992015-06-09 11:47:01 -0700869 SkDoubleToMScalar(3.0),
mtklein51958052015-06-09 15:06:22 -0700870 SkDoubleToMScalar(1.0e+2));
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000871 rot.setRotateDegreesAbout(0, 0, -1, 90);
872 mat.postConcat(rot);
mtklein51958052015-06-09 15:06:22 -0700873 mat.postTranslate(SkDoubleToMScalar(1.0e+2),
mtklein1831f992015-06-09 11:47:01 -0700874 SkDoubleToMScalar(3.0),
mtklein51958052015-06-09 15:06:22 -0700875 SkDoubleToMScalar(1.0e-2));
halcanary96fcdcc2015-08-27 07:41:13 -0700876 REPORTER_ASSERT(reporter, mat.invert(nullptr));
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000877 mat.invert(&inverse);
878 iden1.setConcat(mat, inverse);
879 REPORTER_ASSERT(reporter, is_identity(iden1));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000880
commit-bot@chromium.orgf02f0782013-08-20 15:25:04 +0000881 // test degenerate matrix
882 mat.reset();
883 mat.set3x3(1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0);
halcanary96fcdcc2015-08-27 07:41:13 -0700884 REPORTER_ASSERT(reporter, !mat.invert(nullptr));
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000885
reed@google.comda9fac02011-06-13 14:46:52 +0000886 // test rol/col Major getters
887 {
888 mat.setTranslate(2, 3, 4);
889 float dataf[16];
890 double datad[16];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000891
reed@google.comda9fac02011-06-13 14:46:52 +0000892 mat.asColMajorf(dataf);
893 assert16<float>(reporter, dataf,
894 1, 0, 0, 0,
895 0, 1, 0, 0,
896 0, 0, 1, 0,
897 2, 3, 4, 1);
898 mat.asColMajord(datad);
899 assert16<double>(reporter, datad, 1, 0, 0, 0,
900 0, 1, 0, 0,
901 0, 0, 1, 0,
902 2, 3, 4, 1);
903 mat.asRowMajorf(dataf);
904 assert16<float>(reporter, dataf, 1, 0, 0, 2,
905 0, 1, 0, 3,
906 0, 0, 1, 4,
907 0, 0, 0, 1);
908 mat.asRowMajord(datad);
909 assert16<double>(reporter, datad, 1, 0, 0, 2,
910 0, 1, 0, 3,
911 0, 0, 1, 4,
912 0, 0, 0, 1);
913 }
reed@google.com6f2b44d2011-06-24 18:13:39 +0000914
reed@google.com80b577e2012-11-09 21:25:06 +0000915 test_concat(reporter);
916
caryclark@google.com42639cd2012-06-06 12:03:39 +0000917 if (false) { // avoid bit rot, suppress warning (working on making this pass)
918 test_common_angles(reporter);
919 }
vollick@chromium.org3959a762012-11-13 15:08:22 +0000920
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000921 test_constructor(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000922 test_gettype(reporter);
vollick@chromium.org3959a762012-11-13 15:08:22 +0000923 test_determinant(reporter);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000924 test_invert(reporter);
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000925 test_transpose(reporter);
926 test_get_set_double(reporter);
reed@google.com7d683352012-12-03 21:19:52 +0000927 test_set_row_col_major(reporter);
msarettc1a3e242016-06-23 12:42:29 -0700928 test_set_3x3(reporter);
reed@google.com99b5c7f2012-12-05 22:13:59 +0000929 test_translate(reporter);
930 test_scale(reporter);
931 test_map2(reporter);
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000932 test_3x3_conversion(reporter);
tomhudsona32f1752014-09-16 08:29:29 -0700933 test_has_perspective(reporter);
tomhudsonfaccb8e2014-09-26 11:45:48 -0700934 test_preserves_2d_axis_alignment(reporter);
reed39393e32014-10-21 12:33:21 -0700935 test_toint(reporter);
reed@google.com125002a2011-06-09 19:13:41 +0000936}