blob: a6d4edabc095f135fa32721f849489b104efaf29 [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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@android.comed673312009-02-27 16:24:51 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000010#include "SkMath.h"
reed@android.comed673312009-02-27 16:24:51 +000011#include "SkMatrix.h"
commit-bot@chromium.org08284e42013-07-24 18:08:08 +000012#include "SkMatrixUtils.h"
bsalomon@google.com38396322011-09-09 19:32:04 +000013#include "SkRandom.h"
reed@android.comed673312009-02-27 16:24:51 +000014
15static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
epoger@google.com2047f002011-05-17 17:36:59 +000016 // Note that we get more compounded error for multiple operations when
17 // SK_SCALAR_IS_FIXED.
reed@android.comed673312009-02-27 16:24:51 +000018#ifdef SK_SCALAR_IS_FLOAT
epoger@google.com2047f002011-05-17 17:36:59 +000019 const SkScalar tolerance = SK_Scalar1 / 200000;
reed@android.comed673312009-02-27 16:24:51 +000020#else
epoger@google.com2047f002011-05-17 17:36:59 +000021 const SkScalar tolerance = SK_Scalar1 / 1024;
reed@android.comed673312009-02-27 16:24:51 +000022#endif
23
24 return SkScalarAbs(a - b) <= tolerance;
25}
26
27static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) {
28 for (int i = 0; i < 9; i++) {
29 if (!nearly_equal_scalar(a[i], b[i])) {
bungeman@google.comfab44db2013-10-11 18:50:45 +000030 SkDebugf("not equal %g %g\n", (float)a[i], (float)b[i]);
reed@android.comed673312009-02-27 16:24:51 +000031 return false;
32 }
33 }
34 return true;
35}
36
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000037static bool are_equal(skiatest::Reporter* reporter,
38 const SkMatrix& a,
39 const SkMatrix& b) {
40 bool equal = a == b;
41 bool cheapEqual = a.cheapEqualTo(b);
42 if (equal != cheapEqual) {
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +000043#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com39d4f3a2012-03-26 17:25:45 +000044 if (equal) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000045 bool foundZeroSignDiff = false;
46 for (int i = 0; i < 9; ++i) {
47 float aVal = a.get(i);
48 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000049 int aValI = *SkTCast<int*>(&aVal);
50 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000051 if (0 == aVal && 0 == bVal && aValI != bValI) {
52 foundZeroSignDiff = true;
53 } else {
54 REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI);
55 }
56 }
57 REPORTER_ASSERT(reporter, foundZeroSignDiff);
58 } else {
59 bool foundNaN = false;
60 for (int i = 0; i < 9; ++i) {
61 float aVal = a.get(i);
62 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000063 int aValI = *SkTCast<int*>(&aVal);
64 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000065 if (sk_float_isnan(aVal) && aValI == bValI) {
66 foundNaN = true;
67 } else {
68 REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
69 }
70 }
71 REPORTER_ASSERT(reporter, foundNaN);
72 }
73#else
74 REPORTER_ASSERT(reporter, false);
75#endif
76 }
77 return equal;
78}
79
reed@android.comed673312009-02-27 16:24:51 +000080static bool is_identity(const SkMatrix& m) {
81 SkMatrix identity;
reed@android.com80e39a72009-04-02 16:59:40 +000082 identity.reset();
reed@android.comed673312009-02-27 16:24:51 +000083 return nearly_equal(m, identity);
84}
85
reed@google.com97cd69c2012-10-12 14:35:48 +000086static void test_matrix_recttorect(skiatest::Reporter* reporter) {
87 SkRect src, dst;
88 SkMatrix matrix;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000089
reed@google.com97cd69c2012-10-12 14:35:48 +000090 src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10);
91 dst = src;
92 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
93 REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
94 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000095
reed@google.com97cd69c2012-10-12 14:35:48 +000096 dst.offset(SK_Scalar1, SK_Scalar1);
97 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
98 REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
99 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000100
reed@google.com97cd69c2012-10-12 14:35:48 +0000101 dst.fRight += SK_Scalar1;
102 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000103 REPORTER_ASSERT(reporter,
robertphillips@google.com93f03322012-12-03 17:35:19 +0000104 (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
reed@google.com97cd69c2012-10-12 14:35:48 +0000105 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
106
107 dst = src;
108 dst.fRight = src.fRight * 2;
109 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
110 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
111 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
112}
113
reed@android.com4b7577b2009-06-29 16:14:41 +0000114static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
115 // add 100 in case we have a bug, I don't want to kill my stack in the test
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000116 static const size_t kBufferSize = SkMatrix::kMaxFlattenSize + 100;
117 char buffer[kBufferSize];
118 size_t size1 = m.writeToMemory(NULL);
119 size_t size2 = m.writeToMemory(buffer);
reed@android.com4b7577b2009-06-29 16:14:41 +0000120 REPORTER_ASSERT(reporter, size1 == size2);
121 REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
reed@android.com4b7577b2009-06-29 16:14:41 +0000123 SkMatrix m2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000124 size_t size3 = m2.readFromMemory(buffer, kBufferSize);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000125 REPORTER_ASSERT(reporter, size1 == size3);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000126 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000128 char buffer2[kBufferSize];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000129 size3 = m2.writeToMemory(buffer2);
130 REPORTER_ASSERT(reporter, size1 == size3);
reed@android.com4b7577b2009-06-29 16:14:41 +0000131 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
132}
133
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000134static void test_matrix_min_max_stretch(skiatest::Reporter* reporter) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000135 SkMatrix identity;
136 identity.reset();
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000137 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinStretch());
bsalomon@google.com38396322011-09-09 19:32:04 +0000138 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch());
139
140 SkMatrix scale;
141 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000142 REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinStretch());
bsalomon@google.com38396322011-09-09 19:32:04 +0000143 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch());
144
145 SkMatrix rot90Scale;
146 rot90Scale.setRotate(90 * SK_Scalar1);
147 rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000148 REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinStretch());
bsalomon@google.com38396322011-09-09 19:32:04 +0000149 REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch());
150
151 SkMatrix rotate;
152 rotate.setRotate(128 * SK_Scalar1);
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000153 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinStretch() ,SK_ScalarNearlyZero));
154 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxStretch(), SK_ScalarNearlyZero));
bsalomon@google.com38396322011-09-09 19:32:04 +0000155
156 SkMatrix translate;
157 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000158 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinStretch());
bsalomon@google.com38396322011-09-09 19:32:04 +0000159 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch());
160
161 SkMatrix perspX;
162 perspX.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000163 perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000));
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000164 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinStretch());
bsalomon@google.com38396322011-09-09 19:32:04 +0000165 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch());
166
167 SkMatrix perspY;
168 perspY.reset();
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000169 perspY.setPerspY(SkScalarToPersp(-SK_Scalar1 / 500));
170 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinStretch());
bsalomon@google.com38396322011-09-09 19:32:04 +0000171 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch());
172
173 SkMatrix baseMats[] = {scale, rot90Scale, rotate,
174 translate, perspX, perspY};
175 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000176 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000177 mats[i] = baseMats[i];
178 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
179 REPORTER_ASSERT(reporter, invertable);
180 }
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000181 SkRandom rand;
bsalomon@google.com38396322011-09-09 19:32:04 +0000182 for (int m = 0; m < 1000; ++m) {
183 SkMatrix mat;
184 mat.reset();
185 for (int i = 0; i < 4; ++i) {
186 int x = rand.nextU() % SK_ARRAY_COUNT(mats);
187 mat.postConcat(mats[x]);
188 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000190 SkScalar minStretch = mat.getMinStretch();
191 SkScalar maxStretch = mat.getMaxStretch();
192 REPORTER_ASSERT(reporter, (minStretch < 0) == (maxStretch < 0));
193 REPORTER_ASSERT(reporter, (maxStretch < 0) == mat.hasPerspective());
bsalomon@google.com38396322011-09-09 19:32:04 +0000194
195 if (mat.hasPerspective()) {
196 m -= 1; // try another non-persp matrix
197 continue;
198 }
199
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000200 // test a bunch of vectors. All should be scaled by between minStretch and maxStretch
201 // (modulo some error) and we should find a vector that is scaled by almost each.
202 static const SkScalar gVectorStretchTol = (105 * SK_Scalar1) / 100;
203 static const SkScalar gClosestStretchTol = (97 * SK_Scalar1) / 100;
204 SkScalar max = 0, min = SK_ScalarMax;
bsalomon@google.com38396322011-09-09 19:32:04 +0000205 SkVector vectors[1000];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000206 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000207 vectors[i].fX = rand.nextSScalar1();
208 vectors[i].fY = rand.nextSScalar1();
209 if (!vectors[i].normalize()) {
210 i -= 1;
211 continue;
212 }
213 }
214 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
tomhudson@google.com83a44462011-10-27 15:27:51 +0000215 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000216 SkScalar d = vectors[i].length();
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000217 REPORTER_ASSERT(reporter, SkScalarDiv(d, maxStretch) < gVectorStretchTol);
218 REPORTER_ASSERT(reporter, SkScalarDiv(minStretch, d) < gVectorStretchTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000219 if (max < d) {
220 max = d;
221 }
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000222 if (min > d) {
223 min = d;
224 }
bsalomon@google.com38396322011-09-09 19:32:04 +0000225 }
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000226 REPORTER_ASSERT(reporter, SkScalarDiv(max, maxStretch) >= gClosestStretchTol);
227 REPORTER_ASSERT(reporter, SkScalarDiv(minStretch, min) >= gClosestStretchTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000228 }
229}
230
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000231static void test_matrix_is_similarity(skiatest::Reporter* reporter) {
bsalomon@google.com69afee12012-04-25 15:07:40 +0000232 SkMatrix mat;
233
234 // identity
235 mat.setIdentity();
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000236 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000237
238 // translation only
239 mat.reset();
240 mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000241 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000242
243 // scale with same size
244 mat.reset();
245 mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000246 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000247
248 // scale with one negative
249 mat.reset();
250 mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000251 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000252
253 // scale with different size
254 mat.reset();
255 mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000256 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000257
258 // scale with same size at a pivot point
259 mat.reset();
260 mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
261 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000262 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000263
264 // scale with different size at a pivot point
265 mat.reset();
266 mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
267 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000268 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000269
270 // skew with same size
271 mat.reset();
272 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000273 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000274
275 // skew with different size
276 mat.reset();
277 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000278 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000279
280 // skew with same size at a pivot point
281 mat.reset();
282 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
283 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000284 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000285
286 // skew with different size at a pivot point
287 mat.reset();
288 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
289 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000290 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000291
292 // perspective x
293 mat.reset();
294 mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000295 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000296
297 // perspective y
298 mat.reset();
299 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000300 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000301
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +0000302#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com69afee12012-04-25 15:07:40 +0000303 /* We bypass the following tests for SK_SCALAR_IS_FIXED build.
304 * The long discussion can be found in this issue:
305 * http://codereview.appspot.com/5999050/
306 * In short, we haven't found a perfect way to fix the precision
307 * issue, i.e. the way we use tolerance in isSimilarityTransformation
308 * is incorrect. The situation becomes worse in fixed build, so
309 * we disabled rotation related tests for fixed build.
310 */
311
312 // rotate
313 for (int angle = 0; angle < 360; ++angle) {
314 mat.reset();
315 mat.setRotate(SkIntToScalar(angle));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000316 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000317 }
318
319 // see if there are any accumulated precision issues
320 mat.reset();
321 for (int i = 1; i < 360; i++) {
322 mat.postRotate(SkIntToScalar(1));
323 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000324 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000325
326 // rotate + translate
327 mat.reset();
328 mat.setRotate(SkIntToScalar(30));
329 mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000330 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000331
332 // rotate + uniform scale
333 mat.reset();
334 mat.setRotate(SkIntToScalar(30));
335 mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000336 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000337
338 // rotate + non-uniform scale
339 mat.reset();
340 mat.setRotate(SkIntToScalar(30));
341 mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000342 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000343#endif
344
345 // all zero
346 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000347 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000348
349 // all zero except perspective
350 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000351 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000352
353 // scales zero, only skews
354 mat.setAll(0, SK_Scalar1, 0,
355 SK_Scalar1, 0, 0,
356 0, 0, SkMatrix::I()[8]);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000357 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000358}
359
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000360// For test_matrix_decomposition, below.
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000361static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000362 SkScalar tolerance = SK_ScalarNearlyZero) {
363 // from Bruce Dawson
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000364 // absolute check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000365 SkScalar diff = SkScalarAbs(a - b);
366 if (diff < tolerance) {
367 return true;
368 }
369
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000370 // relative check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000371 a = SkScalarAbs(a);
372 b = SkScalarAbs(b);
373 SkScalar largest = (b > a) ? b : a;
374
375 if (diff <= largest*tolerance) {
376 return true;
377 }
378
379 return false;
380}
381
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000382static bool check_matrix_recomposition(const SkMatrix& mat,
383 const SkPoint& rotation1,
384 const SkPoint& scale,
385 const SkPoint& rotation2) {
386 SkScalar c1 = rotation1.fX;
387 SkScalar s1 = rotation1.fY;
388 SkScalar scaleX = scale.fX;
389 SkScalar scaleY = scale.fY;
390 SkScalar c2 = rotation2.fX;
391 SkScalar s2 = rotation2.fY;
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000392
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000393 // We do a relative check here because large scale factors cause problems with an absolute check
394 bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
395 scaleX*c1*c2 - scaleY*s1*s2) &&
396 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
397 -scaleX*s1*c2 - scaleY*c1*s2) &&
398 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
399 scaleX*c1*s2 + scaleY*s1*c2) &&
400 scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
401 -scaleX*s1*s2 + scaleY*c1*c2);
402 return result;
403}
404
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000405static void test_matrix_decomposition(skiatest::Reporter* reporter) {
406 SkMatrix mat;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000407 SkPoint rotation1, scale, rotation2;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000408
409 const float kRotation0 = 15.5f;
410 const float kRotation1 = -50.f;
411 const float kScale0 = 5000.f;
412 const float kScale1 = 0.001f;
413
414 // identity
415 mat.reset();
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000416 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
417 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000418 // make sure it doesn't crash if we pass in NULLs
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000419 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000420
421 // rotation only
422 mat.setRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000423 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
424 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000425
426 // uniform scale only
427 mat.setScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000428 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
429 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000430
431 // anisotropic scale only
432 mat.setScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000433 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
434 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000435
436 // rotation then uniform scale
437 mat.setRotate(kRotation1);
438 mat.postScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000439 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
440 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000441
442 // uniform scale then rotation
443 mat.setScale(kScale0, kScale0);
444 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000445 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
446 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000447
448 // rotation then uniform scale+reflection
449 mat.setRotate(kRotation0);
450 mat.postScale(kScale1, -kScale1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000451 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
452 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000453
454 // uniform scale+reflection, then rotate
455 mat.setScale(kScale0, -kScale0);
456 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000457 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
458 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000459
460 // rotation then anisotropic scale
461 mat.setRotate(kRotation1);
462 mat.postScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000463 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
464 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000465
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000466 // rotation then anisotropic scale
467 mat.setRotate(90);
468 mat.postScale(kScale1, kScale0);
469 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
470 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000471
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000472 // anisotropic scale then rotation
473 mat.setScale(kScale1, kScale0);
474 mat.postRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000475 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
476 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000477
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000478 // anisotropic scale then rotation
479 mat.setScale(kScale1, kScale0);
480 mat.postRotate(90);
481 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
482 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000483
484 // rotation, uniform scale, then different rotation
485 mat.setRotate(kRotation1);
486 mat.postScale(kScale0, kScale0);
487 mat.postRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000488 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
489 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000490
491 // rotation, anisotropic scale, then different rotation
492 mat.setRotate(kRotation0);
493 mat.postScale(kScale1, kScale0);
494 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000495 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
496 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000497
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000498 // rotation, anisotropic scale + reflection, then different rotation
499 mat.setRotate(kRotation0);
500 mat.postScale(-kScale1, kScale0);
501 mat.postRotate(kRotation1);
502 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
503 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000504
505 // try some random matrices
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000506 SkRandom rand;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000507 for (int m = 0; m < 1000; ++m) {
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000508 SkScalar rot0 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000509 SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
510 SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000511 SkScalar rot1 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000512 mat.setRotate(rot0);
513 mat.postScale(sx, sy);
514 mat.postRotate(rot1);
515
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000516 if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
517 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000518 } else {
519 // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
520 SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
521 mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
522 REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
523 }
524 }
525
526 // translation shouldn't affect this
527 mat.postTranslate(-1000.f, 1000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000528 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
529 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000530
531 // perspective shouldn't affect this
jvanverth@google.com588f3d32013-07-24 18:44:10 +0000532 mat[SkMatrix::kMPersp0] = 12.f;
533 mat[SkMatrix::kMPersp1] = 4.f;
534 mat[SkMatrix::kMPersp2] = 1872.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000535 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
536 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000537
538 // degenerate matrices
539 // mostly zero entries
540 mat.reset();
541 mat[SkMatrix::kMScaleX] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000542 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000543 mat.reset();
544 mat[SkMatrix::kMScaleY] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000545 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000546 mat.reset();
547 // linearly dependent entries
548 mat[SkMatrix::kMScaleX] = 1.f;
549 mat[SkMatrix::kMSkewX] = 2.f;
550 mat[SkMatrix::kMSkewY] = 4.f;
551 mat[SkMatrix::kMScaleY] = 8.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000552 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000553}
554
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000555// For test_matrix_homogeneous, below.
556static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) {
557 for (int i = 0; i < count; ++i) {
558 if (!scalar_nearly_equal_relative(a[i], b[i])) {
559 return false;
560 }
561 }
562 return true;
563}
564
565// For test_matrix_homogeneous, below.
566// Maps a single triple in src using m and compares results to those in dst
567static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3],
568 const SkScalar dst[3]) {
569 SkScalar res[3];
570 SkScalar ms[9] = {m[0], m[1], m[2],
571 m[3], m[4], m[5],
572 m[6], m[7], m[8]};
573 res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2];
574 res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5];
575 res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8];
576 return scalar_array_nearly_equal_relative(res, dst, 3);
577}
578
579static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
580 SkMatrix mat;
581
582 const float kRotation0 = 15.5f;
583 const float kRotation1 = -50.f;
584 const float kScale0 = 5000.f;
585
586 const int kTripleCount = 1000;
587 const int kMatrixCount = 1000;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000588 SkRandom rand;
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000589
590 SkScalar randTriples[3*kTripleCount];
591 for (int i = 0; i < 3*kTripleCount; ++i) {
592 randTriples[i] = rand.nextRangeF(-3000.f, 3000.f);
593 }
594
595 SkMatrix mats[kMatrixCount];
596 for (int i = 0; i < kMatrixCount; ++i) {
597 for (int j = 0; j < 9; ++j) {
598 mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
599 }
600 }
601
602 // identity
603 {
604 mat.reset();
605 SkScalar dst[3*kTripleCount];
606 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
607 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3));
608 }
609
610 // zero matrix
611 {
612 mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
613 SkScalar dst[3*kTripleCount];
614 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
615 SkScalar zeros[3] = {0.f, 0.f, 0.f};
616 for (int i = 0; i < kTripleCount; ++i) {
617 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3));
618 }
619 }
620
621 // zero point
622 {
623 SkScalar zeros[3] = {0.f, 0.f, 0.f};
624 for (int i = 0; i < kMatrixCount; ++i) {
625 SkScalar dst[3];
626 mats[i].mapHomogeneousPoints(dst, zeros, 1);
627 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3));
628 }
629 }
630
631 // doesn't crash with null dst, src, count == 0
632 {
633 mats[0].mapHomogeneousPoints(NULL, NULL, 0);
634 }
635
636 // uniform scale of point
637 {
638 mat.setScale(kScale0, kScale0);
639 SkScalar dst[3];
640 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
641 SkPoint pnt;
642 pnt.set(src[0], src[1]);
643 mat.mapHomogeneousPoints(dst, src, 1);
644 mat.mapPoints(&pnt, &pnt, 1);
645 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
646 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
647 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
648 }
649
650 // rotation of point
651 {
652 mat.setRotate(kRotation0);
653 SkScalar dst[3];
654 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
655 SkPoint pnt;
656 pnt.set(src[0], src[1]);
657 mat.mapHomogeneousPoints(dst, src, 1);
658 mat.mapPoints(&pnt, &pnt, 1);
659 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
660 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
661 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
662 }
663
664 // rotation, scale, rotation of point
665 {
666 mat.setRotate(kRotation1);
667 mat.postScale(kScale0, kScale0);
668 mat.postRotate(kRotation0);
669 SkScalar dst[3];
670 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
671 SkPoint pnt;
672 pnt.set(src[0], src[1]);
673 mat.mapHomogeneousPoints(dst, src, 1);
674 mat.mapPoints(&pnt, &pnt, 1);
675 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
676 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
677 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
678 }
679
680 // compare with naive approach
681 {
682 for (int i = 0; i < kMatrixCount; ++i) {
683 for (int j = 0; j < kTripleCount; ++j) {
684 SkScalar dst[3];
685 mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1);
686 REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst));
687 }
688 }
689 }
690
691}
692
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000693DEF_TEST(Matrix, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000694 SkMatrix mat, inverse, iden1, iden2;
695
696 mat.reset();
697 mat.setTranslate(SK_Scalar1, SK_Scalar1);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000698 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000699 iden1.setConcat(mat, inverse);
700 REPORTER_ASSERT(reporter, is_identity(iden1));
701
reed@google.com2fb96cc2013-01-04 17:02:33 +0000702 mat.setScale(SkIntToScalar(2), SkIntToScalar(4));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000703 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000704 iden1.setConcat(mat, inverse);
705 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000706 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000707
reed@google.com2fb96cc2013-01-04 17:02:33 +0000708 mat.setScale(SK_Scalar1/2, SkIntToScalar(2));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000709 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000710 iden1.setConcat(mat, inverse);
711 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000712 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000713
714 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
715 mat.postRotate(SkIntToScalar(25));
716 REPORTER_ASSERT(reporter, mat.invert(NULL));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000717 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000718 iden1.setConcat(mat, inverse);
719 REPORTER_ASSERT(reporter, is_identity(iden1));
720 iden2.setConcat(inverse, mat);
721 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@android.com4b7577b2009-06-29 16:14:41 +0000722 test_flatten(reporter, mat);
723 test_flatten(reporter, iden2);
reed@android.com80e39a72009-04-02 16:59:40 +0000724
reed@google.com2fb96cc2013-01-04 17:02:33 +0000725 mat.setScale(0, SK_Scalar1);
reed@google.come40591d2013-01-30 15:47:42 +0000726 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000727 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
728 mat.setScale(SK_Scalar1, 0);
reed@google.come40591d2013-01-30 15:47:42 +0000729 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000730 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
731
reed@android.comed673312009-02-27 16:24:51 +0000732 // rectStaysRect test
733 {
734 static const struct {
735 SkScalar m00, m01, m10, m11;
736 bool mStaysRect;
737 }
738 gRectStaysRectSamples[] = {
739 { 0, 0, 0, 0, false },
740 { 0, 0, 0, SK_Scalar1, false },
741 { 0, 0, SK_Scalar1, 0, false },
742 { 0, 0, SK_Scalar1, SK_Scalar1, false },
743 { 0, SK_Scalar1, 0, 0, false },
744 { 0, SK_Scalar1, 0, SK_Scalar1, false },
745 { 0, SK_Scalar1, SK_Scalar1, 0, true },
746 { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
747 { SK_Scalar1, 0, 0, 0, false },
748 { SK_Scalar1, 0, 0, SK_Scalar1, true },
749 { SK_Scalar1, 0, SK_Scalar1, 0, false },
750 { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
751 { SK_Scalar1, SK_Scalar1, 0, 0, false },
752 { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
753 { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
754 { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
755 };
reed@android.com80e39a72009-04-02 16:59:40 +0000756
reed@android.comed673312009-02-27 16:24:51 +0000757 for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
758 SkMatrix m;
reed@android.com80e39a72009-04-02 16:59:40 +0000759
reed@android.comed673312009-02-27 16:24:51 +0000760 m.reset();
761 m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
762 m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
763 m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
764 m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
765 REPORTER_ASSERT(reporter,
766 m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
767 }
768 }
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000769
bungeman@google.comba7983e2011-07-13 20:18:16 +0000770 mat.reset();
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000771 mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
772 mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
773 mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
774 mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
775 mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
776 mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000777 SkScalar affine[6];
778 REPORTER_ASSERT(reporter, mat.asAffine(affine));
779
780 #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
781 REPORTER_ASSERT(reporter, affineEqual(ScaleX));
782 REPORTER_ASSERT(reporter, affineEqual(SkewY));
783 REPORTER_ASSERT(reporter, affineEqual(SkewX));
784 REPORTER_ASSERT(reporter, affineEqual(ScaleY));
785 REPORTER_ASSERT(reporter, affineEqual(TransX));
786 REPORTER_ASSERT(reporter, affineEqual(TransY));
787 #undef affineEqual
788
bungeman@google.com07faed12011-10-07 21:55:56 +0000789 mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000790 REPORTER_ASSERT(reporter, !mat.asAffine(affine));
bsalomon@google.com38396322011-09-09 19:32:04 +0000791
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000792 SkMatrix mat2;
793 mat2.reset();
794 mat.reset();
795 SkScalar zero = 0;
796 mat.set(SkMatrix::kMSkewX, -zero);
797 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
798
799 mat2.reset();
800 mat.reset();
801 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
802 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000803 // fixed pt doesn't have the property that NaN does not equal itself.
804#ifdef SK_SCALAR_IS_FIXED
805 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
806#else
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000807 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000808#endif
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000809
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000810 test_matrix_min_max_stretch(reporter);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000811 test_matrix_is_similarity(reporter);
reed@google.com97cd69c2012-10-12 14:35:48 +0000812 test_matrix_recttorect(reporter);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000813 test_matrix_decomposition(reporter);
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000814 test_matrix_homogeneous(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000815}