blob: 72e0dcc0214be24967c4f65ef0b78557eae0a2d5 [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
tomhudson@google.com889bd8b2011-09-27 17:38:17 +00008#include "SkMath.h"
reed@android.comed673312009-02-27 16:24:51 +00009#include "SkMatrix.h"
commit-bot@chromium.org08284e42013-07-24 18:08:08 +000010#include "SkMatrixUtils.h"
bsalomon@google.com38396322011-09-09 19:32:04 +000011#include "SkRandom.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.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 const SkScalar tolerance = SK_Scalar1 / 200000;
reed@android.comed673312009-02-27 16:24:51 +000016 return SkScalarAbs(a - b) <= tolerance;
17}
18
19static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) {
20 for (int i = 0; i < 9; i++) {
21 if (!nearly_equal_scalar(a[i], b[i])) {
bungeman@google.comfab44db2013-10-11 18:50:45 +000022 SkDebugf("not equal %g %g\n", (float)a[i], (float)b[i]);
reed@android.comed673312009-02-27 16:24:51 +000023 return false;
24 }
25 }
26 return true;
27}
28
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000029static bool are_equal(skiatest::Reporter* reporter,
30 const SkMatrix& a,
31 const SkMatrix& b) {
32 bool equal = a == b;
33 bool cheapEqual = a.cheapEqualTo(b);
34 if (equal != cheapEqual) {
bsalomon@google.com39d4f3a2012-03-26 17:25:45 +000035 if (equal) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000036 bool foundZeroSignDiff = false;
37 for (int i = 0; i < 9; ++i) {
38 float aVal = a.get(i);
39 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000040 int aValI = *SkTCast<int*>(&aVal);
41 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000042 if (0 == aVal && 0 == bVal && aValI != bValI) {
43 foundZeroSignDiff = true;
44 } else {
45 REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI);
46 }
47 }
48 REPORTER_ASSERT(reporter, foundZeroSignDiff);
49 } else {
50 bool foundNaN = false;
51 for (int i = 0; i < 9; ++i) {
52 float aVal = a.get(i);
53 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000054 int aValI = *SkTCast<int*>(&aVal);
55 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000056 if (sk_float_isnan(aVal) && aValI == bValI) {
57 foundNaN = true;
58 } else {
59 REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
60 }
61 }
62 REPORTER_ASSERT(reporter, foundNaN);
63 }
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000064 }
65 return equal;
66}
67
reed@android.comed673312009-02-27 16:24:51 +000068static bool is_identity(const SkMatrix& m) {
69 SkMatrix identity;
reed@android.com80e39a72009-04-02 16:59:40 +000070 identity.reset();
reed@android.comed673312009-02-27 16:24:51 +000071 return nearly_equal(m, identity);
72}
73
reed451e8222014-12-13 08:46:48 -080074static void assert9(skiatest::Reporter* reporter, const SkMatrix& m,
75 SkScalar a, SkScalar b, SkScalar c,
76 SkScalar d, SkScalar e, SkScalar f,
77 SkScalar g, SkScalar h, SkScalar i) {
78 SkScalar buffer[9];
79 m.get9(buffer);
80 REPORTER_ASSERT(reporter, buffer[0] == a);
81 REPORTER_ASSERT(reporter, buffer[1] == b);
82 REPORTER_ASSERT(reporter, buffer[2] == c);
83 REPORTER_ASSERT(reporter, buffer[3] == d);
84 REPORTER_ASSERT(reporter, buffer[4] == e);
85 REPORTER_ASSERT(reporter, buffer[5] == f);
86 REPORTER_ASSERT(reporter, buffer[6] == g);
87 REPORTER_ASSERT(reporter, buffer[7] == h);
88 REPORTER_ASSERT(reporter, buffer[8] == i);
89}
90
91static void test_set9(skiatest::Reporter* reporter) {
92
93 SkMatrix m;
94 m.reset();
95 assert9(reporter, m, 1, 0, 0, 0, 1, 0, 0, 0, 1);
96
97 m.setScale(2, 3);
98 assert9(reporter, m, 2, 0, 0, 0, 3, 0, 0, 0, 1);
99
100 m.postTranslate(4, 5);
101 assert9(reporter, m, 2, 0, 4, 0, 3, 5, 0, 0, 1);
102
103 SkScalar buffer[9];
104 sk_bzero(buffer, sizeof(buffer));
105 buffer[SkMatrix::kMScaleX] = 1;
106 buffer[SkMatrix::kMScaleY] = 1;
107 buffer[SkMatrix::kMPersp2] = 1;
108 REPORTER_ASSERT(reporter, !m.isIdentity());
109 m.set9(buffer);
110 REPORTER_ASSERT(reporter, m.isIdentity());
111}
112
reed@google.com97cd69c2012-10-12 14:35:48 +0000113static void test_matrix_recttorect(skiatest::Reporter* reporter) {
114 SkRect src, dst;
115 SkMatrix matrix;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000116
reed@google.com97cd69c2012-10-12 14:35:48 +0000117 src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10);
118 dst = src;
119 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
120 REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
121 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000122
reed@google.com97cd69c2012-10-12 14:35:48 +0000123 dst.offset(SK_Scalar1, SK_Scalar1);
124 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
125 REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
126 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000127
reed@google.com97cd69c2012-10-12 14:35:48 +0000128 dst.fRight += SK_Scalar1;
129 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000130 REPORTER_ASSERT(reporter,
robertphillips@google.com93f03322012-12-03 17:35:19 +0000131 (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
reed@google.com97cd69c2012-10-12 14:35:48 +0000132 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
133
134 dst = src;
135 dst.fRight = src.fRight * 2;
136 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
137 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
138 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
139}
140
reed@android.com4b7577b2009-06-29 16:14:41 +0000141static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
142 // 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 +0000143 static const size_t kBufferSize = SkMatrix::kMaxFlattenSize + 100;
144 char buffer[kBufferSize];
145 size_t size1 = m.writeToMemory(NULL);
146 size_t size2 = m.writeToMemory(buffer);
reed@android.com4b7577b2009-06-29 16:14:41 +0000147 REPORTER_ASSERT(reporter, size1 == size2);
148 REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149
reed@android.com4b7577b2009-06-29 16:14:41 +0000150 SkMatrix m2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000151 size_t size3 = m2.readFromMemory(buffer, kBufferSize);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000152 REPORTER_ASSERT(reporter, size1 == size3);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000153 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000155 char buffer2[kBufferSize];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000156 size3 = m2.writeToMemory(buffer2);
157 REPORTER_ASSERT(reporter, size1 == size3);
reed@android.com4b7577b2009-06-29 16:14:41 +0000158 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
159}
160
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000161static void test_matrix_min_max_scale(skiatest::Reporter* reporter) {
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000162 SkScalar scales[2];
163 bool success;
164
bsalomon@google.com38396322011-09-09 19:32:04 +0000165 SkMatrix identity;
166 identity.reset();
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000167 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinScale());
168 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000169 success = identity.getMinMaxScales(scales);
170 REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000171
172 SkMatrix scale;
173 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000174 REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinScale());
175 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000176 success = scale.getMinMaxScales(scales);
177 REPORTER_ASSERT(reporter, success && SK_Scalar1 * 2 == scales[0] && SK_Scalar1 * 4 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000178
179 SkMatrix rot90Scale;
180 rot90Scale.setRotate(90 * SK_Scalar1);
181 rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000182 REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale());
183 REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000184 success = rot90Scale.getMinMaxScales(scales);
185 REPORTER_ASSERT(reporter, success && SK_Scalar1 / 4 == scales[0] && SK_Scalar1 / 2 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000186
187 SkMatrix rotate;
188 rotate.setRotate(128 * SK_Scalar1);
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000189 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinScale(), SK_ScalarNearlyZero));
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000190 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxScale(), SK_ScalarNearlyZero));
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000191 success = rotate.getMinMaxScales(scales);
192 REPORTER_ASSERT(reporter, success);
193 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[0], SK_ScalarNearlyZero));
194 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[1], SK_ScalarNearlyZero));
bsalomon@google.com38396322011-09-09 19:32:04 +0000195
196 SkMatrix translate;
197 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000198 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinScale());
199 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000200 success = translate.getMinMaxScales(scales);
201 REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000202
203 SkMatrix perspX;
204 perspX.reset();
reed3f43f8a2015-01-20 19:58:36 -0800205 perspX.setPerspX(SK_Scalar1 / 1000);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000206 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinScale());
207 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000208 // Verify that getMinMaxScales() doesn't update the scales array on failure.
209 scales[0] = -5;
210 scales[1] = -5;
211 success = perspX.getMinMaxScales(scales);
212 REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000213
214 SkMatrix perspY;
215 perspY.reset();
reed3f43f8a2015-01-20 19:58:36 -0800216 perspY.setPerspY(-SK_Scalar1 / 500);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000217 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale());
218 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000219 scales[0] = -5;
220 scales[1] = -5;
221 success = perspY.getMinMaxScales(scales);
222 REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000223
224 SkMatrix baseMats[] = {scale, rot90Scale, rotate,
225 translate, perspX, perspY};
226 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000227 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000228 mats[i] = baseMats[i];
229 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
230 REPORTER_ASSERT(reporter, invertable);
231 }
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000232 SkRandom rand;
bsalomon@google.com38396322011-09-09 19:32:04 +0000233 for (int m = 0; m < 1000; ++m) {
234 SkMatrix mat;
235 mat.reset();
236 for (int i = 0; i < 4; ++i) {
237 int x = rand.nextU() % SK_ARRAY_COUNT(mats);
238 mat.postConcat(mats[x]);
239 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000240
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000241 SkScalar minScale = mat.getMinScale();
242 SkScalar maxScale = mat.getMaxScale();
243 REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0));
244 REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective());
bsalomon@google.com38396322011-09-09 19:32:04 +0000245
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000246 SkScalar scales[2];
247 bool success = mat.getMinMaxScales(scales);
248 REPORTER_ASSERT(reporter, success == !mat.hasPerspective());
249 REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale));
250
bsalomon@google.com38396322011-09-09 19:32:04 +0000251 if (mat.hasPerspective()) {
252 m -= 1; // try another non-persp matrix
253 continue;
254 }
255
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000256 // test a bunch of vectors. All should be scaled by between minScale and maxScale
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000257 // (modulo some error) and we should find a vector that is scaled by almost each.
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000258 static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100;
259 static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100;
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000260 SkScalar max = 0, min = SK_ScalarMax;
bsalomon@google.com38396322011-09-09 19:32:04 +0000261 SkVector vectors[1000];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000262 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000263 vectors[i].fX = rand.nextSScalar1();
264 vectors[i].fY = rand.nextSScalar1();
265 if (!vectors[i].normalize()) {
266 i -= 1;
267 continue;
268 }
269 }
270 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
tomhudson@google.com83a44462011-10-27 15:27:51 +0000271 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000272 SkScalar d = vectors[i].length();
reed80ea19c2015-05-12 10:37:34 -0700273 REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol);
274 REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000275 if (max < d) {
276 max = d;
277 }
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000278 if (min > d) {
279 min = d;
280 }
bsalomon@google.com38396322011-09-09 19:32:04 +0000281 }
reed80ea19c2015-05-12 10:37:34 -0700282 REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol);
283 REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000284 }
285}
286
jvanverth17a845f2014-09-02 13:15:40 -0700287static void test_matrix_preserve_shape(skiatest::Reporter* reporter) {
bsalomon@google.com69afee12012-04-25 15:07:40 +0000288 SkMatrix mat;
289
290 // identity
291 mat.setIdentity();
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000292 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700293 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000294
295 // translation only
296 mat.reset();
297 mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000298 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700299 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000300
301 // scale with same size
302 mat.reset();
303 mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000304 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700305 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000306
307 // scale with one negative
308 mat.reset();
309 mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000310 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700311 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000312
313 // scale with different size
314 mat.reset();
315 mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000316 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700317 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000318
319 // scale with same size at a pivot point
320 mat.reset();
321 mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
322 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000323 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700324 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000325
326 // scale with different size at a pivot point
327 mat.reset();
328 mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
329 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000330 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700331 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000332
333 // skew with same size
334 mat.reset();
335 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000336 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700337 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000338
339 // skew with different size
340 mat.reset();
341 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000342 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700343 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000344
345 // skew with same size at a pivot point
346 mat.reset();
347 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
348 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000349 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700350 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000351
352 // skew with different size at a pivot point
353 mat.reset();
354 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
355 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000356 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700357 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000358
359 // perspective x
360 mat.reset();
reed3f43f8a2015-01-20 19:58:36 -0800361 mat.setPerspX(SK_Scalar1 / 2);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000362 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700363 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000364
365 // perspective y
366 mat.reset();
reed3f43f8a2015-01-20 19:58:36 -0800367 mat.setPerspY(SK_Scalar1 / 2);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000368 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700369 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000370
bsalomon@google.com69afee12012-04-25 15:07:40 +0000371 // rotate
372 for (int angle = 0; angle < 360; ++angle) {
373 mat.reset();
374 mat.setRotate(SkIntToScalar(angle));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000375 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700376 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000377 }
378
379 // see if there are any accumulated precision issues
380 mat.reset();
381 for (int i = 1; i < 360; i++) {
382 mat.postRotate(SkIntToScalar(1));
383 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000384 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700385 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000386
387 // rotate + translate
388 mat.reset();
389 mat.setRotate(SkIntToScalar(30));
390 mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000391 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700392 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000393
394 // rotate + uniform scale
395 mat.reset();
396 mat.setRotate(SkIntToScalar(30));
397 mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000398 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700399 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000400
401 // rotate + non-uniform scale
402 mat.reset();
403 mat.setRotate(SkIntToScalar(30));
404 mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000405 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700406 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
407
408 // non-uniform scale + rotate
409 mat.reset();
410 mat.setScale(SkIntToScalar(3), SkIntToScalar(2));
411 mat.postRotate(SkIntToScalar(30));
412 REPORTER_ASSERT(reporter, !mat.isSimilarity());
413 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000414
415 // all zero
416 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000417 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700418 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000419
420 // all zero except perspective
jvanverth17a845f2014-09-02 13:15:40 -0700421 mat.reset();
bsalomon@google.com69afee12012-04-25 15:07:40 +0000422 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000423 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700424 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000425
jvanverth17a845f2014-09-02 13:15:40 -0700426 // scales zero, only skews (rotation)
427 mat.setAll(0, SK_Scalar1, 0,
428 -SK_Scalar1, 0, 0,
429 0, 0, SkMatrix::I()[8]);
430 REPORTER_ASSERT(reporter, mat.isSimilarity());
431 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
432
433 // scales zero, only skews (reflection)
bsalomon@google.com69afee12012-04-25 15:07:40 +0000434 mat.setAll(0, SK_Scalar1, 0,
435 SK_Scalar1, 0, 0,
436 0, 0, SkMatrix::I()[8]);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000437 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700438 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000439}
440
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000441// For test_matrix_decomposition, below.
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000442static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000443 SkScalar tolerance = SK_ScalarNearlyZero) {
444 // from Bruce Dawson
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000445 // absolute check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000446 SkScalar diff = SkScalarAbs(a - b);
447 if (diff < tolerance) {
448 return true;
449 }
450
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000451 // relative check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000452 a = SkScalarAbs(a);
453 b = SkScalarAbs(b);
454 SkScalar largest = (b > a) ? b : a;
455
456 if (diff <= largest*tolerance) {
457 return true;
458 }
459
460 return false;
461}
462
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000463static bool check_matrix_recomposition(const SkMatrix& mat,
464 const SkPoint& rotation1,
465 const SkPoint& scale,
466 const SkPoint& rotation2) {
467 SkScalar c1 = rotation1.fX;
468 SkScalar s1 = rotation1.fY;
469 SkScalar scaleX = scale.fX;
470 SkScalar scaleY = scale.fY;
471 SkScalar c2 = rotation2.fX;
472 SkScalar s2 = rotation2.fY;
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000473
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000474 // We do a relative check here because large scale factors cause problems with an absolute check
475 bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
476 scaleX*c1*c2 - scaleY*s1*s2) &&
477 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
478 -scaleX*s1*c2 - scaleY*c1*s2) &&
479 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
480 scaleX*c1*s2 + scaleY*s1*c2) &&
481 scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
482 -scaleX*s1*s2 + scaleY*c1*c2);
483 return result;
484}
485
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000486static void test_matrix_decomposition(skiatest::Reporter* reporter) {
487 SkMatrix mat;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000488 SkPoint rotation1, scale, rotation2;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000489
490 const float kRotation0 = 15.5f;
491 const float kRotation1 = -50.f;
492 const float kScale0 = 5000.f;
493 const float kScale1 = 0.001f;
494
495 // identity
496 mat.reset();
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000497 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
498 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000499 // make sure it doesn't crash if we pass in NULLs
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000500 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000501
502 // rotation only
503 mat.setRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000504 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
505 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000506
507 // uniform scale only
508 mat.setScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000509 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
510 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000511
512 // anisotropic scale only
513 mat.setScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000514 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
515 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000516
517 // rotation then uniform scale
518 mat.setRotate(kRotation1);
519 mat.postScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000520 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
521 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000522
523 // uniform scale then rotation
524 mat.setScale(kScale0, kScale0);
525 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000526 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
527 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000528
529 // rotation then uniform scale+reflection
530 mat.setRotate(kRotation0);
531 mat.postScale(kScale1, -kScale1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000532 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
533 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000534
535 // uniform scale+reflection, then rotate
536 mat.setScale(kScale0, -kScale0);
537 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000538 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
539 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000540
541 // rotation then anisotropic scale
542 mat.setRotate(kRotation1);
543 mat.postScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000544 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
545 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000546
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000547 // rotation then anisotropic scale
548 mat.setRotate(90);
549 mat.postScale(kScale1, kScale0);
550 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
551 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000552
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000553 // anisotropic scale then rotation
554 mat.setScale(kScale1, kScale0);
555 mat.postRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000556 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
557 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000558
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000559 // anisotropic scale then rotation
560 mat.setScale(kScale1, kScale0);
561 mat.postRotate(90);
562 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
563 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000564
565 // rotation, uniform scale, then different rotation
566 mat.setRotate(kRotation1);
567 mat.postScale(kScale0, kScale0);
568 mat.postRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000569 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
570 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000571
572 // rotation, anisotropic scale, then different rotation
573 mat.setRotate(kRotation0);
574 mat.postScale(kScale1, kScale0);
575 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000576 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
577 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000578
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000579 // rotation, anisotropic scale + reflection, then different rotation
580 mat.setRotate(kRotation0);
581 mat.postScale(-kScale1, kScale0);
582 mat.postRotate(kRotation1);
583 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
584 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000585
586 // try some random matrices
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000587 SkRandom rand;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000588 for (int m = 0; m < 1000; ++m) {
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000589 SkScalar rot0 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000590 SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
591 SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000592 SkScalar rot1 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000593 mat.setRotate(rot0);
594 mat.postScale(sx, sy);
595 mat.postRotate(rot1);
596
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000597 if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
598 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000599 } else {
600 // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
601 SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
602 mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
603 REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
604 }
605 }
606
607 // translation shouldn't affect this
608 mat.postTranslate(-1000.f, 1000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000609 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
610 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000611
612 // perspective shouldn't affect this
jvanverth@google.com588f3d32013-07-24 18:44:10 +0000613 mat[SkMatrix::kMPersp0] = 12.f;
614 mat[SkMatrix::kMPersp1] = 4.f;
615 mat[SkMatrix::kMPersp2] = 1872.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000616 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
617 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000618
619 // degenerate matrices
620 // mostly zero entries
621 mat.reset();
622 mat[SkMatrix::kMScaleX] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000623 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000624 mat.reset();
625 mat[SkMatrix::kMScaleY] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000626 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000627 mat.reset();
628 // linearly dependent entries
629 mat[SkMatrix::kMScaleX] = 1.f;
630 mat[SkMatrix::kMSkewX] = 2.f;
631 mat[SkMatrix::kMSkewY] = 4.f;
632 mat[SkMatrix::kMScaleY] = 8.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000633 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000634}
635
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000636// For test_matrix_homogeneous, below.
637static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) {
638 for (int i = 0; i < count; ++i) {
639 if (!scalar_nearly_equal_relative(a[i], b[i])) {
640 return false;
641 }
642 }
643 return true;
644}
645
646// For test_matrix_homogeneous, below.
647// Maps a single triple in src using m and compares results to those in dst
648static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3],
649 const SkScalar dst[3]) {
650 SkScalar res[3];
651 SkScalar ms[9] = {m[0], m[1], m[2],
652 m[3], m[4], m[5],
653 m[6], m[7], m[8]};
654 res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2];
655 res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5];
656 res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8];
657 return scalar_array_nearly_equal_relative(res, dst, 3);
658}
659
660static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
661 SkMatrix mat;
662
663 const float kRotation0 = 15.5f;
664 const float kRotation1 = -50.f;
665 const float kScale0 = 5000.f;
666
667 const int kTripleCount = 1000;
668 const int kMatrixCount = 1000;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000669 SkRandom rand;
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000670
671 SkScalar randTriples[3*kTripleCount];
672 for (int i = 0; i < 3*kTripleCount; ++i) {
673 randTriples[i] = rand.nextRangeF(-3000.f, 3000.f);
674 }
675
676 SkMatrix mats[kMatrixCount];
677 for (int i = 0; i < kMatrixCount; ++i) {
678 for (int j = 0; j < 9; ++j) {
679 mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
680 }
681 }
682
683 // identity
684 {
685 mat.reset();
686 SkScalar dst[3*kTripleCount];
687 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
688 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3));
689 }
690
691 // zero matrix
692 {
693 mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
694 SkScalar dst[3*kTripleCount];
695 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
696 SkScalar zeros[3] = {0.f, 0.f, 0.f};
697 for (int i = 0; i < kTripleCount; ++i) {
698 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3));
699 }
700 }
701
702 // zero point
703 {
704 SkScalar zeros[3] = {0.f, 0.f, 0.f};
705 for (int i = 0; i < kMatrixCount; ++i) {
706 SkScalar dst[3];
707 mats[i].mapHomogeneousPoints(dst, zeros, 1);
708 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3));
709 }
710 }
711
712 // doesn't crash with null dst, src, count == 0
713 {
714 mats[0].mapHomogeneousPoints(NULL, NULL, 0);
715 }
716
717 // uniform scale of point
718 {
719 mat.setScale(kScale0, kScale0);
720 SkScalar dst[3];
721 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
722 SkPoint pnt;
723 pnt.set(src[0], src[1]);
724 mat.mapHomogeneousPoints(dst, src, 1);
725 mat.mapPoints(&pnt, &pnt, 1);
726 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
727 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
728 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
729 }
730
731 // rotation of point
732 {
733 mat.setRotate(kRotation0);
734 SkScalar dst[3];
735 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
736 SkPoint pnt;
737 pnt.set(src[0], src[1]);
738 mat.mapHomogeneousPoints(dst, src, 1);
739 mat.mapPoints(&pnt, &pnt, 1);
740 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
741 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
742 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
743 }
744
745 // rotation, scale, rotation of point
746 {
747 mat.setRotate(kRotation1);
748 mat.postScale(kScale0, kScale0);
749 mat.postRotate(kRotation0);
750 SkScalar dst[3];
751 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
752 SkPoint pnt;
753 pnt.set(src[0], src[1]);
754 mat.mapHomogeneousPoints(dst, src, 1);
755 mat.mapPoints(&pnt, &pnt, 1);
756 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
757 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
758 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
759 }
760
761 // compare with naive approach
762 {
763 for (int i = 0; i < kMatrixCount; ++i) {
764 for (int j = 0; j < kTripleCount; ++j) {
765 SkScalar dst[3];
766 mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1);
767 REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst));
768 }
769 }
770 }
771
772}
773
reedadf99902015-03-19 16:10:54 -0700774static bool check_decompScale(const SkMatrix& matrix) {
775 SkSize scale;
776 SkMatrix remaining;
777
778 if (!matrix.decomposeScale(&scale, &remaining)) {
779 return false;
780 }
781 if (scale.width() <= 0 || scale.height() <= 0) {
782 return false;
783 }
784 remaining.preScale(scale.width(), scale.height());
785 return nearly_equal(matrix, remaining);
786}
787
788static void test_decompScale(skiatest::Reporter* reporter) {
789 SkMatrix m;
790
791 m.reset();
792 REPORTER_ASSERT(reporter, check_decompScale(m));
793 m.setScale(2, 3);
794 REPORTER_ASSERT(reporter, check_decompScale(m));
795 m.setRotate(35, 0, 0);
796 REPORTER_ASSERT(reporter, check_decompScale(m));
797
798 m.setScale(1, 0);
799 REPORTER_ASSERT(reporter, !check_decompScale(m));
800}
801
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000802DEF_TEST(Matrix, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000803 SkMatrix mat, inverse, iden1, iden2;
804
805 mat.reset();
806 mat.setTranslate(SK_Scalar1, SK_Scalar1);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000807 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000808 iden1.setConcat(mat, inverse);
809 REPORTER_ASSERT(reporter, is_identity(iden1));
810
reed@google.com2fb96cc2013-01-04 17:02:33 +0000811 mat.setScale(SkIntToScalar(2), SkIntToScalar(4));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000812 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000813 iden1.setConcat(mat, inverse);
814 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000815 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000816
reed@google.com2fb96cc2013-01-04 17:02:33 +0000817 mat.setScale(SK_Scalar1/2, SkIntToScalar(2));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000818 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000819 iden1.setConcat(mat, inverse);
820 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000821 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000822
823 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
824 mat.postRotate(SkIntToScalar(25));
825 REPORTER_ASSERT(reporter, mat.invert(NULL));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000826 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000827 iden1.setConcat(mat, inverse);
828 REPORTER_ASSERT(reporter, is_identity(iden1));
829 iden2.setConcat(inverse, mat);
830 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@android.com4b7577b2009-06-29 16:14:41 +0000831 test_flatten(reporter, mat);
832 test_flatten(reporter, iden2);
reed@android.com80e39a72009-04-02 16:59:40 +0000833
reed@google.com2fb96cc2013-01-04 17:02:33 +0000834 mat.setScale(0, SK_Scalar1);
reed@google.come40591d2013-01-30 15:47:42 +0000835 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000836 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
837 mat.setScale(SK_Scalar1, 0);
reed@google.come40591d2013-01-30 15:47:42 +0000838 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000839 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
840
reed@android.comed673312009-02-27 16:24:51 +0000841 // rectStaysRect test
842 {
843 static const struct {
844 SkScalar m00, m01, m10, m11;
845 bool mStaysRect;
846 }
847 gRectStaysRectSamples[] = {
848 { 0, 0, 0, 0, false },
849 { 0, 0, 0, SK_Scalar1, false },
850 { 0, 0, SK_Scalar1, 0, false },
851 { 0, 0, SK_Scalar1, SK_Scalar1, false },
852 { 0, SK_Scalar1, 0, 0, false },
853 { 0, SK_Scalar1, 0, SK_Scalar1, false },
854 { 0, SK_Scalar1, SK_Scalar1, 0, true },
855 { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
856 { SK_Scalar1, 0, 0, 0, false },
857 { SK_Scalar1, 0, 0, SK_Scalar1, true },
858 { SK_Scalar1, 0, SK_Scalar1, 0, false },
859 { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
860 { SK_Scalar1, SK_Scalar1, 0, 0, false },
861 { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
862 { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
863 { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
864 };
reed@android.com80e39a72009-04-02 16:59:40 +0000865
reed@android.comed673312009-02-27 16:24:51 +0000866 for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
867 SkMatrix m;
reed@android.com80e39a72009-04-02 16:59:40 +0000868
reed@android.comed673312009-02-27 16:24:51 +0000869 m.reset();
870 m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
871 m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
872 m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
873 m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
874 REPORTER_ASSERT(reporter,
875 m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
876 }
877 }
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000878
bungeman@google.comba7983e2011-07-13 20:18:16 +0000879 mat.reset();
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000880 mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
881 mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
882 mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
883 mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
884 mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
885 mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000886 SkScalar affine[6];
887 REPORTER_ASSERT(reporter, mat.asAffine(affine));
888
889 #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
890 REPORTER_ASSERT(reporter, affineEqual(ScaleX));
891 REPORTER_ASSERT(reporter, affineEqual(SkewY));
892 REPORTER_ASSERT(reporter, affineEqual(SkewX));
893 REPORTER_ASSERT(reporter, affineEqual(ScaleY));
894 REPORTER_ASSERT(reporter, affineEqual(TransX));
895 REPORTER_ASSERT(reporter, affineEqual(TransY));
896 #undef affineEqual
897
reed3f43f8a2015-01-20 19:58:36 -0800898 mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2);
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000899 REPORTER_ASSERT(reporter, !mat.asAffine(affine));
bsalomon@google.com38396322011-09-09 19:32:04 +0000900
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000901 SkMatrix mat2;
902 mat2.reset();
903 mat.reset();
904 SkScalar zero = 0;
905 mat.set(SkMatrix::kMSkewX, -zero);
906 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
907
908 mat2.reset();
909 mat.reset();
910 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
911 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
912 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
913
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000914 test_matrix_min_max_scale(reporter);
jvanverth17a845f2014-09-02 13:15:40 -0700915 test_matrix_preserve_shape(reporter);
reed@google.com97cd69c2012-10-12 14:35:48 +0000916 test_matrix_recttorect(reporter);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000917 test_matrix_decomposition(reporter);
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000918 test_matrix_homogeneous(reporter);
reed451e8222014-12-13 08:46:48 -0800919 test_set9(reporter);
reedadf99902015-03-19 16:10:54 -0700920
921 test_decompScale(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000922}
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +0000923
924DEF_TEST(Matrix_Concat, r) {
925 SkMatrix a;
926 a.setTranslate(10, 20);
927
928 SkMatrix b;
929 b.setScale(3, 5);
930
931 SkMatrix expected;
932 expected.setConcat(a,b);
933
934 REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b));
935}