blob: 8785730322f52dfdc47bc76130f4f23c72339efc [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comed673312009-02-27 16:24:51 +00008#include "Test.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +00009#include "SkMath.h"
reed@android.comed673312009-02-27 16:24:51 +000010#include "SkMatrix.h"
commit-bot@chromium.org08284e42013-07-24 18:08:08 +000011#include "SkMatrixUtils.h"
bsalomon@google.com38396322011-09-09 19:32:04 +000012#include "SkRandom.h"
reed@android.comed673312009-02-27 16:24:51 +000013
14static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
epoger@google.com2047f002011-05-17 17:36:59 +000015 // Note that we get more compounded error for multiple operations when
16 // SK_SCALAR_IS_FIXED.
reed@android.comed673312009-02-27 16:24:51 +000017#ifdef SK_SCALAR_IS_FLOAT
epoger@google.com2047f002011-05-17 17:36:59 +000018 const SkScalar tolerance = SK_Scalar1 / 200000;
reed@android.comed673312009-02-27 16:24:51 +000019#else
epoger@google.com2047f002011-05-17 17:36:59 +000020 const SkScalar tolerance = SK_Scalar1 / 1024;
reed@android.comed673312009-02-27 16:24:51 +000021#endif
22
23 return SkScalarAbs(a - b) <= tolerance;
24}
25
26static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) {
27 for (int i = 0; i < 9; i++) {
28 if (!nearly_equal_scalar(a[i], b[i])) {
reed@android.comd4134452011-02-09 02:24:26 +000029 printf("not equal %g %g\n", (float)a[i], (float)b[i]);
reed@android.comed673312009-02-27 16:24:51 +000030 return false;
31 }
32 }
33 return true;
34}
35
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000036static bool are_equal(skiatest::Reporter* reporter,
37 const SkMatrix& a,
38 const SkMatrix& b) {
39 bool equal = a == b;
40 bool cheapEqual = a.cheapEqualTo(b);
41 if (equal != cheapEqual) {
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +000042#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com39d4f3a2012-03-26 17:25:45 +000043 if (equal) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000044 bool foundZeroSignDiff = false;
45 for (int i = 0; i < 9; ++i) {
46 float aVal = a.get(i);
47 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000048 int aValI = *SkTCast<int*>(&aVal);
49 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000050 if (0 == aVal && 0 == bVal && aValI != bValI) {
51 foundZeroSignDiff = true;
52 } else {
53 REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI);
54 }
55 }
56 REPORTER_ASSERT(reporter, foundZeroSignDiff);
57 } else {
58 bool foundNaN = false;
59 for (int i = 0; i < 9; ++i) {
60 float aVal = a.get(i);
61 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000062 int aValI = *SkTCast<int*>(&aVal);
63 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000064 if (sk_float_isnan(aVal) && aValI == bValI) {
65 foundNaN = true;
66 } else {
67 REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
68 }
69 }
70 REPORTER_ASSERT(reporter, foundNaN);
71 }
72#else
73 REPORTER_ASSERT(reporter, false);
74#endif
75 }
76 return equal;
77}
78
reed@android.comed673312009-02-27 16:24:51 +000079static bool is_identity(const SkMatrix& m) {
80 SkMatrix identity;
reed@android.com80e39a72009-04-02 16:59:40 +000081 identity.reset();
reed@android.comed673312009-02-27 16:24:51 +000082 return nearly_equal(m, identity);
83}
84
reed@google.com97cd69c2012-10-12 14:35:48 +000085static void test_matrix_recttorect(skiatest::Reporter* reporter) {
86 SkRect src, dst;
87 SkMatrix matrix;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000088
reed@google.com97cd69c2012-10-12 14:35:48 +000089 src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10);
90 dst = src;
91 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
92 REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
93 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000094
reed@google.com97cd69c2012-10-12 14:35:48 +000095 dst.offset(SK_Scalar1, SK_Scalar1);
96 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
97 REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
98 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000099
reed@google.com97cd69c2012-10-12 14:35:48 +0000100 dst.fRight += SK_Scalar1;
101 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000102 REPORTER_ASSERT(reporter,
robertphillips@google.com93f03322012-12-03 17:35:19 +0000103 (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
reed@google.com97cd69c2012-10-12 14:35:48 +0000104 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
105
106 dst = src;
107 dst.fRight = src.fRight * 2;
108 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
109 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
110 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
111}
112
reed@android.com4b7577b2009-06-29 16:14:41 +0000113static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
114 // add 100 in case we have a bug, I don't want to kill my stack in the test
115 char buffer[SkMatrix::kMaxFlattenSize + 100];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000116 uint32_t size1 = m.writeToMemory(NULL);
117 uint32_t size2 = m.writeToMemory(buffer);
reed@android.com4b7577b2009-06-29 16:14:41 +0000118 REPORTER_ASSERT(reporter, size1 == size2);
119 REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
reed@android.com4b7577b2009-06-29 16:14:41 +0000121 SkMatrix m2;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000122 uint32_t size3 = m2.readFromMemory(buffer);
123 REPORTER_ASSERT(reporter, size1 == size3);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000124 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125
reed@android.com4b7577b2009-06-29 16:14:41 +0000126 char buffer2[SkMatrix::kMaxFlattenSize + 100];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000127 size3 = m2.writeToMemory(buffer2);
128 REPORTER_ASSERT(reporter, size1 == size3);
reed@android.com4b7577b2009-06-29 16:14:41 +0000129 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
130}
131
caryclark@google.com42639cd2012-06-06 12:03:39 +0000132static void test_matrix_max_stretch(skiatest::Reporter* reporter) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000133 SkMatrix identity;
134 identity.reset();
135 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch());
136
137 SkMatrix scale;
138 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
139 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch());
140
141 SkMatrix rot90Scale;
142 rot90Scale.setRotate(90 * SK_Scalar1);
143 rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
144 REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch());
145
146 SkMatrix rotate;
147 rotate.setRotate(128 * SK_Scalar1);
148 REPORTER_ASSERT(reporter, SkScalarAbs(SK_Scalar1 - rotate.getMaxStretch()) <= SK_ScalarNearlyZero);
149
150 SkMatrix translate;
151 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
152 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch());
153
154 SkMatrix perspX;
155 perspX.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000156 perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000));
bsalomon@google.com38396322011-09-09 19:32:04 +0000157 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch());
158
159 SkMatrix perspY;
160 perspY.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000161 perspY.setPerspX(SkScalarToPersp(-SK_Scalar1 / 500));
bsalomon@google.com38396322011-09-09 19:32:04 +0000162 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch());
163
164 SkMatrix baseMats[] = {scale, rot90Scale, rotate,
165 translate, perspX, perspY};
166 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000167 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000168 mats[i] = baseMats[i];
169 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
170 REPORTER_ASSERT(reporter, invertable);
171 }
jvanverth@google.comc490f802013-03-04 13:56:38 +0000172 SkMWCRandom rand;
bsalomon@google.com38396322011-09-09 19:32:04 +0000173 for (int m = 0; m < 1000; ++m) {
174 SkMatrix mat;
175 mat.reset();
176 for (int i = 0; i < 4; ++i) {
177 int x = rand.nextU() % SK_ARRAY_COUNT(mats);
178 mat.postConcat(mats[x]);
179 }
180 SkScalar stretch = mat.getMaxStretch();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181
bsalomon@google.com38396322011-09-09 19:32:04 +0000182 if ((stretch < 0) != mat.hasPerspective()) {
183 stretch = mat.getMaxStretch();
184 }
185
186 REPORTER_ASSERT(reporter, (stretch < 0) == mat.hasPerspective());
187
188 if (mat.hasPerspective()) {
189 m -= 1; // try another non-persp matrix
190 continue;
191 }
192
193 // test a bunch of vectors. None should be scaled by more than stretch
194 // (modulo some error) and we should find a vector that is scaled by
195 // almost stretch.
196 static const SkScalar gStretchTol = (105 * SK_Scalar1) / 100;
197 static const SkScalar gMaxStretchTol = (97 * SK_Scalar1) / 100;
198 SkScalar max = 0;
199 SkVector vectors[1000];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000200 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000201 vectors[i].fX = rand.nextSScalar1();
202 vectors[i].fY = rand.nextSScalar1();
203 if (!vectors[i].normalize()) {
204 i -= 1;
205 continue;
206 }
207 }
208 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
tomhudson@google.com83a44462011-10-27 15:27:51 +0000209 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000210 SkScalar d = vectors[i].length();
211 REPORTER_ASSERT(reporter, SkScalarDiv(d, stretch) < gStretchTol);
212 if (max < d) {
213 max = d;
214 }
215 }
216 REPORTER_ASSERT(reporter, SkScalarDiv(max, stretch) >= gMaxStretchTol);
217 }
218}
219
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000220static void test_matrix_is_similarity(skiatest::Reporter* reporter) {
bsalomon@google.com69afee12012-04-25 15:07:40 +0000221 SkMatrix mat;
222
223 // identity
224 mat.setIdentity();
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000225 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000226
227 // translation only
228 mat.reset();
229 mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000230 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000231
232 // scale with same size
233 mat.reset();
234 mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000235 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000236
237 // scale with one negative
238 mat.reset();
239 mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000240 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000241
242 // scale with different size
243 mat.reset();
244 mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000245 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000246
247 // scale with same size at a pivot point
248 mat.reset();
249 mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
250 SkIntToScalar(2), SkIntToScalar(2));
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 at a pivot point
254 mat.reset();
255 mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
256 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000257 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000258
259 // skew with same size
260 mat.reset();
261 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000262 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000263
264 // skew with different size
265 mat.reset();
266 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000267 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000268
269 // skew with same size at a pivot point
270 mat.reset();
271 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
272 SkIntToScalar(2), SkIntToScalar(2));
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 at a pivot point
276 mat.reset();
277 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
278 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000279 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000280
281 // perspective x
282 mat.reset();
283 mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 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 // perspective y
287 mat.reset();
288 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000289 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000290
djsollen@google.com4bd2bdb2013-03-08 18:35:13 +0000291#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com69afee12012-04-25 15:07:40 +0000292 /* We bypass the following tests for SK_SCALAR_IS_FIXED build.
293 * The long discussion can be found in this issue:
294 * http://codereview.appspot.com/5999050/
295 * In short, we haven't found a perfect way to fix the precision
296 * issue, i.e. the way we use tolerance in isSimilarityTransformation
297 * is incorrect. The situation becomes worse in fixed build, so
298 * we disabled rotation related tests for fixed build.
299 */
300
301 // rotate
302 for (int angle = 0; angle < 360; ++angle) {
303 mat.reset();
304 mat.setRotate(SkIntToScalar(angle));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000305 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000306 }
307
308 // see if there are any accumulated precision issues
309 mat.reset();
310 for (int i = 1; i < 360; i++) {
311 mat.postRotate(SkIntToScalar(1));
312 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000313 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000314
315 // rotate + translate
316 mat.reset();
317 mat.setRotate(SkIntToScalar(30));
318 mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000319 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000320
321 // rotate + uniform scale
322 mat.reset();
323 mat.setRotate(SkIntToScalar(30));
324 mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000325 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000326
327 // rotate + non-uniform scale
328 mat.reset();
329 mat.setRotate(SkIntToScalar(30));
330 mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000331 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000332#endif
333
334 // all zero
335 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000336 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000337
338 // all zero except perspective
339 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000340 REPORTER_ASSERT(reporter, !mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000341
342 // scales zero, only skews
343 mat.setAll(0, SK_Scalar1, 0,
344 SK_Scalar1, 0, 0,
345 0, 0, SkMatrix::I()[8]);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000346 REPORTER_ASSERT(reporter, mat.isSimilarity());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000347}
348
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000349// For test_matrix_decomposition, below.
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000350static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000351 SkScalar tolerance = SK_ScalarNearlyZero) {
352 // from Bruce Dawson
353 SkScalar diff = SkScalarAbs(a - b);
354 if (diff < tolerance) {
355 return true;
356 }
357
358 a = SkScalarAbs(a);
359 b = SkScalarAbs(b);
360 SkScalar largest = (b > a) ? b : a;
361
362 if (diff <= largest*tolerance) {
363 return true;
364 }
365
366 return false;
367}
368
369static void test_matrix_decomposition(skiatest::Reporter* reporter) {
370 SkMatrix mat;
371 SkScalar rotation0, scaleX, scaleY, rotation1;
372
373 const float kRotation0 = 15.5f;
374 const float kRotation1 = -50.f;
375 const float kScale0 = 5000.f;
376 const float kScale1 = 0.001f;
377
378 // identity
379 mat.reset();
380 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
381 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
382 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1));
383 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1));
384 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
385 // make sure it doesn't crash if we pass in NULLs
386 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL, NULL));
387
388 // rotation only
389 mat.setRotate(kRotation0);
390 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
391 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0)));
392 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1));
393 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1));
394 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
395
396 // uniform scale only
397 mat.setScale(kScale0, kScale0);
398 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
399 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
400 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
401 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
402 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
403
404 // anisotropic scale only
405 mat.setScale(kScale1, kScale0);
406 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
407 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
408 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
409 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
410 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
411
412 // rotation then uniform scale
413 mat.setRotate(kRotation1);
414 mat.postScale(kScale0, kScale0);
415 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
416 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1)));
417 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
418 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
419 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
420
421 // uniform scale then rotation
422 mat.setScale(kScale0, kScale0);
423 mat.postRotate(kRotation1);
424 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
425 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1)));
426 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
427 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
428 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
429
430 // rotation then uniform scale+reflection
431 mat.setRotate(kRotation0);
432 mat.postScale(kScale1, -kScale1);
433 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
434 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0)));
435 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
436 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale1));
437 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
438
439 // uniform scale+reflection, then rotate
440 mat.setScale(kScale0, -kScale0);
441 mat.postRotate(kRotation1);
442 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
443 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(-kRotation1)));
444 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
445 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale0));
446 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
447
448 // rotation then anisotropic scale
449 mat.setRotate(kRotation1);
450 mat.postScale(kScale1, kScale0);
451 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
452 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1)));
453 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
454 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
455 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
456
457 // anisotropic scale then rotation
458 mat.setScale(kScale1, kScale0);
459 mat.postRotate(kRotation0);
460 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
461 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
462 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
463 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
464 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation1, SkDegreesToRadians(kRotation0)));
465
466 // rotation, uniform scale, then different rotation
467 mat.setRotate(kRotation1);
468 mat.postScale(kScale0, kScale0);
469 mat.postRotate(kRotation0);
470 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
471 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0,
472 SkDegreesToRadians(kRotation0 + kRotation1)));
473 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
474 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
475 REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
476
477 // rotation, anisotropic scale, then different rotation
478 mat.setRotate(kRotation0);
479 mat.postScale(kScale1, kScale0);
480 mat.postRotate(kRotation1);
481 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
482 // Because of the shear/skew we won't get the same results, so we need to multiply it out.
483 // Generating the matrices requires doing a radian-to-degree calculation, then degree-to-radian
484 // calculation (in setRotate()), which adds error, so this just computes the matrix elements
485 // directly.
486 SkScalar c0;
487 SkScalar s0 = SkScalarSinCos(rotation0, &c0);
488 SkScalar c1;
489 SkScalar s1 = SkScalarSinCos(rotation1, &c1);
490 // We do a relative check here because large scale factors cause problems with an absolute check
491 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
492 scaleX*c0*c1 - scaleY*s0*s1));
493 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
494 -scaleX*s0*c1 - scaleY*c0*s1));
495 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
496 scaleX*c0*s1 + scaleY*s0*c1));
497 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
498 -scaleX*s0*s1 + scaleY*c0*c1));
499
500 // try some random matrices
501 SkMWCRandom rand;
502 for (int m = 0; m < 1000; ++m) {
503 SkScalar rot0 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI);
504 SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
505 SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
506 SkScalar rot1 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI);
507 mat.setRotate(rot0);
508 mat.postScale(sx, sy);
509 mat.postRotate(rot1);
510
511 if (SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)) {
512 SkScalar c0;
513 SkScalar s0 = SkScalarSinCos(rotation0, &c0);
514 SkScalar c1;
515 SkScalar s1 = SkScalarSinCos(rotation1, &c1);
516 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
517 scaleX*c0*c1 - scaleY*s0*s1));
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000518 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000519 -scaleX*s0*c1 - scaleY*c0*s1));
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000520 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000521 scaleX*c0*s1 + scaleY*s0*c1));
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000522 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000523 -scaleX*s0*s1 + scaleY*c0*c1));
524 } else {
525 // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
526 SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
527 mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
528 REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
529 }
530 }
531
532 // translation shouldn't affect this
533 mat.postTranslate(-1000.f, 1000.f);
534 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
535 s0 = SkScalarSinCos(rotation0, &c0);
536 s1 = SkScalarSinCos(rotation1, &c1);
537 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
538 scaleX*c0*c1 - scaleY*s0*s1));
539 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
540 -scaleX*s0*c1 - scaleY*c0*s1));
541 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
542 scaleX*c0*s1 + scaleY*s0*c1));
543 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
544 -scaleX*s0*s1 + scaleY*c0*c1));
545
546 // perspective shouldn't affect this
jvanverth@google.com588f3d32013-07-24 18:44:10 +0000547 mat[SkMatrix::kMPersp0] = 12.f;
548 mat[SkMatrix::kMPersp1] = 4.f;
549 mat[SkMatrix::kMPersp2] = 1872.f;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000550 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
551 s0 = SkScalarSinCos(rotation0, &c0);
552 s1 = SkScalarSinCos(rotation1, &c1);
553 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
554 scaleX*c0*c1 - scaleY*s0*s1));
555 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
556 -scaleX*s0*c1 - scaleY*c0*s1));
557 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
558 scaleX*c0*s1 + scaleY*s0*c1));
559 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
560 -scaleX*s0*s1 + scaleY*c0*c1));
561
562 // rotation, anisotropic scale + reflection, then different rotation
563 mat.setRotate(kRotation0);
564 mat.postScale(-kScale1, kScale0);
565 mat.postRotate(kRotation1);
566 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
567 s0 = SkScalarSinCos(rotation0, &c0);
568 s1 = SkScalarSinCos(rotation1, &c1);
569 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
570 scaleX*c0*c1 - scaleY*s0*s1));
571 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
572 -scaleX*s0*c1 - scaleY*c0*s1));
573 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
574 scaleX*c0*s1 + scaleY*s0*c1));
575 REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
576 -scaleX*s0*s1 + scaleY*c0*c1));
577
578 // degenerate matrices
579 // mostly zero entries
580 mat.reset();
581 mat[SkMatrix::kMScaleX] = 0.f;
582 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
583 mat.reset();
584 mat[SkMatrix::kMScaleY] = 0.f;
585 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
586 mat.reset();
587 // linearly dependent entries
588 mat[SkMatrix::kMScaleX] = 1.f;
589 mat[SkMatrix::kMSkewX] = 2.f;
590 mat[SkMatrix::kMSkewY] = 4.f;
591 mat[SkMatrix::kMScaleY] = 8.f;
592 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
593}
594
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000595// For test_matrix_homogeneous, below.
596static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) {
597 for (int i = 0; i < count; ++i) {
598 if (!scalar_nearly_equal_relative(a[i], b[i])) {
599 return false;
600 }
601 }
602 return true;
603}
604
605// For test_matrix_homogeneous, below.
606// Maps a single triple in src using m and compares results to those in dst
607static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3],
608 const SkScalar dst[3]) {
609 SkScalar res[3];
610 SkScalar ms[9] = {m[0], m[1], m[2],
611 m[3], m[4], m[5],
612 m[6], m[7], m[8]};
613 res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2];
614 res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5];
615 res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8];
616 return scalar_array_nearly_equal_relative(res, dst, 3);
617}
618
619static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
620 SkMatrix mat;
621
622 const float kRotation0 = 15.5f;
623 const float kRotation1 = -50.f;
624 const float kScale0 = 5000.f;
625
626 const int kTripleCount = 1000;
627 const int kMatrixCount = 1000;
628 SkRandom rand;
629
630 SkScalar randTriples[3*kTripleCount];
631 for (int i = 0; i < 3*kTripleCount; ++i) {
632 randTriples[i] = rand.nextRangeF(-3000.f, 3000.f);
633 }
634
635 SkMatrix mats[kMatrixCount];
636 for (int i = 0; i < kMatrixCount; ++i) {
637 for (int j = 0; j < 9; ++j) {
638 mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
639 }
640 }
641
642 // identity
643 {
644 mat.reset();
645 SkScalar dst[3*kTripleCount];
646 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
647 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3));
648 }
649
650 // zero matrix
651 {
652 mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
653 SkScalar dst[3*kTripleCount];
654 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
655 SkScalar zeros[3] = {0.f, 0.f, 0.f};
656 for (int i = 0; i < kTripleCount; ++i) {
657 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3));
658 }
659 }
660
661 // zero point
662 {
663 SkScalar zeros[3] = {0.f, 0.f, 0.f};
664 for (int i = 0; i < kMatrixCount; ++i) {
665 SkScalar dst[3];
666 mats[i].mapHomogeneousPoints(dst, zeros, 1);
667 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3));
668 }
669 }
670
671 // doesn't crash with null dst, src, count == 0
672 {
673 mats[0].mapHomogeneousPoints(NULL, NULL, 0);
674 }
675
676 // uniform scale of point
677 {
678 mat.setScale(kScale0, kScale0);
679 SkScalar dst[3];
680 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
681 SkPoint pnt;
682 pnt.set(src[0], src[1]);
683 mat.mapHomogeneousPoints(dst, src, 1);
684 mat.mapPoints(&pnt, &pnt, 1);
685 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
686 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
687 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
688 }
689
690 // rotation of point
691 {
692 mat.setRotate(kRotation0);
693 SkScalar dst[3];
694 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
695 SkPoint pnt;
696 pnt.set(src[0], src[1]);
697 mat.mapHomogeneousPoints(dst, src, 1);
698 mat.mapPoints(&pnt, &pnt, 1);
699 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
700 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
701 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
702 }
703
704 // rotation, scale, rotation of point
705 {
706 mat.setRotate(kRotation1);
707 mat.postScale(kScale0, kScale0);
708 mat.postRotate(kRotation0);
709 SkScalar dst[3];
710 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
711 SkPoint pnt;
712 pnt.set(src[0], src[1]);
713 mat.mapHomogeneousPoints(dst, src, 1);
714 mat.mapPoints(&pnt, &pnt, 1);
715 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
716 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
717 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
718 }
719
720 // compare with naive approach
721 {
722 for (int i = 0; i < kMatrixCount; ++i) {
723 for (int j = 0; j < kTripleCount; ++j) {
724 SkScalar dst[3];
725 mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1);
726 REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst));
727 }
728 }
729 }
730
731}
732
caryclark@google.com42639cd2012-06-06 12:03:39 +0000733static void TestMatrix(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000734 SkMatrix mat, inverse, iden1, iden2;
735
736 mat.reset();
737 mat.setTranslate(SK_Scalar1, SK_Scalar1);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000738 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000739 iden1.setConcat(mat, inverse);
740 REPORTER_ASSERT(reporter, is_identity(iden1));
741
reed@google.com2fb96cc2013-01-04 17:02:33 +0000742 mat.setScale(SkIntToScalar(2), SkIntToScalar(4));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000743 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000744 iden1.setConcat(mat, inverse);
745 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000746 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000747
reed@google.com2fb96cc2013-01-04 17:02:33 +0000748 mat.setScale(SK_Scalar1/2, SkIntToScalar(2));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000749 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000750 iden1.setConcat(mat, inverse);
751 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000752 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000753
754 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
755 mat.postRotate(SkIntToScalar(25));
756 REPORTER_ASSERT(reporter, mat.invert(NULL));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000757 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000758 iden1.setConcat(mat, inverse);
759 REPORTER_ASSERT(reporter, is_identity(iden1));
760 iden2.setConcat(inverse, mat);
761 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@android.com4b7577b2009-06-29 16:14:41 +0000762 test_flatten(reporter, mat);
763 test_flatten(reporter, iden2);
reed@android.com80e39a72009-04-02 16:59:40 +0000764
reed@google.com2fb96cc2013-01-04 17:02:33 +0000765 mat.setScale(0, SK_Scalar1);
reed@google.come40591d2013-01-30 15:47:42 +0000766 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000767 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
768 mat.setScale(SK_Scalar1, 0);
reed@google.come40591d2013-01-30 15:47:42 +0000769 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000770 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
771
reed@android.comed673312009-02-27 16:24:51 +0000772 // rectStaysRect test
773 {
774 static const struct {
775 SkScalar m00, m01, m10, m11;
776 bool mStaysRect;
777 }
778 gRectStaysRectSamples[] = {
779 { 0, 0, 0, 0, false },
780 { 0, 0, 0, SK_Scalar1, false },
781 { 0, 0, SK_Scalar1, 0, false },
782 { 0, 0, SK_Scalar1, SK_Scalar1, false },
783 { 0, SK_Scalar1, 0, 0, false },
784 { 0, SK_Scalar1, 0, SK_Scalar1, false },
785 { 0, SK_Scalar1, SK_Scalar1, 0, true },
786 { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
787 { SK_Scalar1, 0, 0, 0, false },
788 { SK_Scalar1, 0, 0, SK_Scalar1, true },
789 { SK_Scalar1, 0, SK_Scalar1, 0, false },
790 { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
791 { SK_Scalar1, SK_Scalar1, 0, 0, false },
792 { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
793 { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
794 { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
795 };
reed@android.com80e39a72009-04-02 16:59:40 +0000796
reed@android.comed673312009-02-27 16:24:51 +0000797 for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
798 SkMatrix m;
reed@android.com80e39a72009-04-02 16:59:40 +0000799
reed@android.comed673312009-02-27 16:24:51 +0000800 m.reset();
801 m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
802 m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
803 m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
804 m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
805 REPORTER_ASSERT(reporter,
806 m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
807 }
808 }
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000809
bungeman@google.comba7983e2011-07-13 20:18:16 +0000810 mat.reset();
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000811 mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
812 mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
813 mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
814 mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
815 mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
816 mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000817 SkScalar affine[6];
818 REPORTER_ASSERT(reporter, mat.asAffine(affine));
819
820 #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
821 REPORTER_ASSERT(reporter, affineEqual(ScaleX));
822 REPORTER_ASSERT(reporter, affineEqual(SkewY));
823 REPORTER_ASSERT(reporter, affineEqual(SkewX));
824 REPORTER_ASSERT(reporter, affineEqual(ScaleY));
825 REPORTER_ASSERT(reporter, affineEqual(TransX));
826 REPORTER_ASSERT(reporter, affineEqual(TransY));
827 #undef affineEqual
828
bungeman@google.com07faed12011-10-07 21:55:56 +0000829 mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000830 REPORTER_ASSERT(reporter, !mat.asAffine(affine));
bsalomon@google.com38396322011-09-09 19:32:04 +0000831
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000832 SkMatrix mat2;
833 mat2.reset();
834 mat.reset();
835 SkScalar zero = 0;
836 mat.set(SkMatrix::kMSkewX, -zero);
837 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
838
839 mat2.reset();
840 mat.reset();
841 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
842 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000843 // fixed pt doesn't have the property that NaN does not equal itself.
844#ifdef SK_SCALAR_IS_FIXED
845 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
846#else
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000847 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000848#endif
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000849
bsalomon@google.com38396322011-09-09 19:32:04 +0000850 test_matrix_max_stretch(reporter);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000851 test_matrix_is_similarity(reporter);
reed@google.com97cd69c2012-10-12 14:35:48 +0000852 test_matrix_recttorect(reporter);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000853 test_matrix_decomposition(reporter);
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000854 test_matrix_homogeneous(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000855}
856
reed@android.comd8730ea2009-02-27 22:06:06 +0000857#include "TestClassDef.h"
858DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix)