blob: da2ae35cd715f3b34c1d576a5c7fdfcb7ddf3176 [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 {
halcanarye76ca8b2016-05-20 10:36:50 -070045 REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000046 }
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);
halcanary9d524f22016-03-29 09:03:52 -070096
reed451e8222014-12-13 08:46:48 -080097 m.setScale(2, 3);
98 assert9(reporter, m, 2, 0, 0, 0, 3, 0, 0, 0, 1);
halcanary9d524f22016-03-29 09:03:52 -070099
reed451e8222014-12-13 08:46:48 -0800100 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];
halcanary96fcdcc2015-08-27 07:41:13 -0700145 size_t size1 = m.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000146 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 success = perspX.getMinMaxScales(scales);
bsalomonef2c7c72015-12-17 15:33:13 -0800209 REPORTER_ASSERT(reporter, !success);
210
211 // skbug.com/4718
212 SkMatrix big;
213 big.setAll(2.39394089e+36f, 8.85347779e+36f, 9.26526204e+36f,
214 3.9159619e+36f, 1.44823453e+37f, 1.51559342e+37f,
215 0.f, 0.f, 1.f);
bsalomonef2c7c72015-12-17 15:33:13 -0800216 success = big.getMinMaxScales(scales);
217 REPORTER_ASSERT(reporter, !success);
bsalomon@google.com38396322011-09-09 19:32:04 +0000218
kolczykcea22ae2016-07-16 11:52:37 -0700219 // skbug.com/4718
220 SkMatrix givingNegativeNearlyZeros;
221 givingNegativeNearlyZeros.setAll(0.00436534f, 0.114138f, 0.37141f,
222 0.00358857f, 0.0936228f, -0.0174198f,
223 0.f, 0.f, 1.f);
224 success = givingNegativeNearlyZeros.getMinMaxScales(scales);
225 REPORTER_ASSERT(reporter, success && 0 == scales[0]);
226
bsalomon@google.com38396322011-09-09 19:32:04 +0000227 SkMatrix perspY;
228 perspY.reset();
reed3f43f8a2015-01-20 19:58:36 -0800229 perspY.setPerspY(-SK_Scalar1 / 500);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000230 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale());
231 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000232 scales[0] = -5;
233 scales[1] = -5;
234 success = perspY.getMinMaxScales(scales);
235 REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000236
237 SkMatrix baseMats[] = {scale, rot90Scale, rotate,
238 translate, perspX, perspY};
239 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000240 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000241 mats[i] = baseMats[i];
bungemane55131c2016-08-24 12:01:31 -0700242 bool invertible = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
243 REPORTER_ASSERT(reporter, invertible);
bsalomon@google.com38396322011-09-09 19:32:04 +0000244 }
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000245 SkRandom rand;
bsalomon@google.com38396322011-09-09 19:32:04 +0000246 for (int m = 0; m < 1000; ++m) {
247 SkMatrix mat;
248 mat.reset();
249 for (int i = 0; i < 4; ++i) {
250 int x = rand.nextU() % SK_ARRAY_COUNT(mats);
251 mat.postConcat(mats[x]);
252 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000253
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000254 SkScalar minScale = mat.getMinScale();
255 SkScalar maxScale = mat.getMaxScale();
256 REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0));
257 REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective());
bsalomon@google.com38396322011-09-09 19:32:04 +0000258
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000259 SkScalar scales[2];
260 bool success = mat.getMinMaxScales(scales);
261 REPORTER_ASSERT(reporter, success == !mat.hasPerspective());
262 REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale));
263
bsalomon@google.com38396322011-09-09 19:32:04 +0000264 if (mat.hasPerspective()) {
265 m -= 1; // try another non-persp matrix
266 continue;
267 }
268
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000269 // test a bunch of vectors. All should be scaled by between minScale and maxScale
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000270 // (modulo some error) and we should find a vector that is scaled by almost each.
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000271 static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100;
272 static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100;
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000273 SkScalar max = 0, min = SK_ScalarMax;
bsalomon@google.com38396322011-09-09 19:32:04 +0000274 SkVector vectors[1000];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000275 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000276 vectors[i].fX = rand.nextSScalar1();
277 vectors[i].fY = rand.nextSScalar1();
278 if (!vectors[i].normalize()) {
279 i -= 1;
280 continue;
281 }
282 }
283 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
tomhudson@google.com83a44462011-10-27 15:27:51 +0000284 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000285 SkScalar d = vectors[i].length();
reed80ea19c2015-05-12 10:37:34 -0700286 REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol);
287 REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000288 if (max < d) {
289 max = d;
290 }
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000291 if (min > d) {
292 min = d;
293 }
bsalomon@google.com38396322011-09-09 19:32:04 +0000294 }
reed80ea19c2015-05-12 10:37:34 -0700295 REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol);
296 REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000297 }
298}
299
jvanverth17a845f2014-09-02 13:15:40 -0700300static void test_matrix_preserve_shape(skiatest::Reporter* reporter) {
bsalomon@google.com69afee12012-04-25 15:07:40 +0000301 SkMatrix mat;
302
303 // identity
304 mat.setIdentity();
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000305 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700306 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000307
308 // translation only
309 mat.reset();
310 mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000311 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700312 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000313
314 // scale with same size
315 mat.reset();
316 mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000317 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700318 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000319
320 // scale with one negative
321 mat.reset();
322 mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
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
327 mat.reset();
328 mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000329 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700330 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000331
332 // scale with same size at a pivot point
333 mat.reset();
334 mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
335 SkIntToScalar(2), SkIntToScalar(2));
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 // scale with different size at a pivot point
340 mat.reset();
341 mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
342 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000343 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700344 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000345
346 // skew with same size
347 mat.reset();
348 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
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
353 mat.reset();
354 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000355 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700356 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000357
358 // skew with same size at a pivot point
359 mat.reset();
360 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
361 SkIntToScalar(2), SkIntToScalar(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 // skew with different size at a pivot point
366 mat.reset();
367 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
368 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000369 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700370 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000371
372 // perspective x
373 mat.reset();
reed3f43f8a2015-01-20 19:58:36 -0800374 mat.setPerspX(SK_Scalar1 / 2);
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 // perspective y
379 mat.reset();
reed3f43f8a2015-01-20 19:58:36 -0800380 mat.setPerspY(SK_Scalar1 / 2);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000381 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700382 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000383
bsalomon@google.com69afee12012-04-25 15:07:40 +0000384 // rotate
385 for (int angle = 0; angle < 360; ++angle) {
386 mat.reset();
387 mat.setRotate(SkIntToScalar(angle));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000388 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700389 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000390 }
391
392 // see if there are any accumulated precision issues
393 mat.reset();
394 for (int i = 1; i < 360; i++) {
395 mat.postRotate(SkIntToScalar(1));
396 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000397 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700398 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000399
400 // rotate + translate
401 mat.reset();
402 mat.setRotate(SkIntToScalar(30));
403 mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000404 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700405 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000406
407 // rotate + uniform scale
408 mat.reset();
409 mat.setRotate(SkIntToScalar(30));
410 mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000411 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700412 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000413
414 // rotate + non-uniform scale
415 mat.reset();
416 mat.setRotate(SkIntToScalar(30));
417 mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000418 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700419 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
420
421 // non-uniform scale + rotate
422 mat.reset();
423 mat.setScale(SkIntToScalar(3), SkIntToScalar(2));
424 mat.postRotate(SkIntToScalar(30));
425 REPORTER_ASSERT(reporter, !mat.isSimilarity());
426 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000427
428 // all zero
429 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000430 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700431 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000432
433 // all zero except perspective
jvanverth17a845f2014-09-02 13:15:40 -0700434 mat.reset();
bsalomon@google.com69afee12012-04-25 15:07:40 +0000435 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000436 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700437 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000438
jvanverth17a845f2014-09-02 13:15:40 -0700439 // scales zero, only skews (rotation)
440 mat.setAll(0, SK_Scalar1, 0,
441 -SK_Scalar1, 0, 0,
442 0, 0, SkMatrix::I()[8]);
443 REPORTER_ASSERT(reporter, mat.isSimilarity());
444 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
445
446 // scales zero, only skews (reflection)
bsalomon@google.com69afee12012-04-25 15:07:40 +0000447 mat.setAll(0, SK_Scalar1, 0,
448 SK_Scalar1, 0, 0,
449 0, 0, SkMatrix::I()[8]);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000450 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700451 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000452}
453
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000454// For test_matrix_decomposition, below.
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000455static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000456 SkScalar tolerance = SK_ScalarNearlyZero) {
457 // from Bruce Dawson
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000458 // absolute check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000459 SkScalar diff = SkScalarAbs(a - b);
460 if (diff < tolerance) {
461 return true;
462 }
463
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000464 // relative check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000465 a = SkScalarAbs(a);
466 b = SkScalarAbs(b);
467 SkScalar largest = (b > a) ? b : a;
468
469 if (diff <= largest*tolerance) {
470 return true;
471 }
472
473 return false;
474}
475
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000476static bool check_matrix_recomposition(const SkMatrix& mat,
477 const SkPoint& rotation1,
478 const SkPoint& scale,
479 const SkPoint& rotation2) {
480 SkScalar c1 = rotation1.fX;
481 SkScalar s1 = rotation1.fY;
482 SkScalar scaleX = scale.fX;
483 SkScalar scaleY = scale.fY;
484 SkScalar c2 = rotation2.fX;
485 SkScalar s2 = rotation2.fY;
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000486
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000487 // We do a relative check here because large scale factors cause problems with an absolute check
488 bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
489 scaleX*c1*c2 - scaleY*s1*s2) &&
490 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
491 -scaleX*s1*c2 - scaleY*c1*s2) &&
492 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
493 scaleX*c1*s2 + scaleY*s1*c2) &&
494 scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
495 -scaleX*s1*s2 + scaleY*c1*c2);
496 return result;
497}
498
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000499static void test_matrix_decomposition(skiatest::Reporter* reporter) {
500 SkMatrix mat;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000501 SkPoint rotation1, scale, rotation2;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000502
503 const float kRotation0 = 15.5f;
504 const float kRotation1 = -50.f;
505 const float kScale0 = 5000.f;
506 const float kScale1 = 0.001f;
507
508 // identity
509 mat.reset();
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000510 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
511 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000512 // make sure it doesn't crash if we pass in NULLs
halcanary96fcdcc2015-08-27 07:41:13 -0700513 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, nullptr, nullptr, nullptr));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000514
515 // rotation only
516 mat.setRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000517 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
518 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000519
520 // uniform scale only
521 mat.setScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000522 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
523 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000524
525 // anisotropic scale only
526 mat.setScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000527 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
528 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000529
530 // rotation then uniform scale
531 mat.setRotate(kRotation1);
532 mat.postScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000533 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
534 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000535
536 // uniform scale then rotation
537 mat.setScale(kScale0, kScale0);
538 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000539 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
540 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000541
542 // rotation then uniform scale+reflection
543 mat.setRotate(kRotation0);
544 mat.postScale(kScale1, -kScale1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000545 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
546 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000547
548 // uniform scale+reflection, then rotate
549 mat.setScale(kScale0, -kScale0);
550 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000551 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
552 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000553
554 // rotation then anisotropic scale
555 mat.setRotate(kRotation1);
556 mat.postScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000557 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
558 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000559
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000560 // rotation then anisotropic scale
561 mat.setRotate(90);
562 mat.postScale(kScale1, kScale0);
563 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
564 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000565
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000566 // anisotropic scale then rotation
567 mat.setScale(kScale1, 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));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000571
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000572 // anisotropic scale then rotation
573 mat.setScale(kScale1, kScale0);
574 mat.postRotate(90);
575 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
576 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000577
578 // rotation, uniform scale, then different rotation
579 mat.setRotate(kRotation1);
580 mat.postScale(kScale0, kScale0);
581 mat.postRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000582 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
583 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000584
585 // rotation, anisotropic scale, then different rotation
586 mat.setRotate(kRotation0);
587 mat.postScale(kScale1, kScale0);
588 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000589 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
590 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000591
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000592 // rotation, anisotropic scale + reflection, then different rotation
593 mat.setRotate(kRotation0);
594 mat.postScale(-kScale1, kScale0);
595 mat.postRotate(kRotation1);
596 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
597 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000598
599 // try some random matrices
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000600 SkRandom rand;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000601 for (int m = 0; m < 1000; ++m) {
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000602 SkScalar rot0 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000603 SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
604 SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000605 SkScalar rot1 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000606 mat.setRotate(rot0);
607 mat.postScale(sx, sy);
608 mat.postRotate(rot1);
609
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000610 if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
611 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000612 } else {
613 // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
614 SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
615 mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
616 REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
617 }
618 }
619
620 // translation shouldn't affect this
621 mat.postTranslate(-1000.f, 1000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000622 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
623 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000624
625 // perspective shouldn't affect this
jvanverth@google.com588f3d32013-07-24 18:44:10 +0000626 mat[SkMatrix::kMPersp0] = 12.f;
627 mat[SkMatrix::kMPersp1] = 4.f;
628 mat[SkMatrix::kMPersp2] = 1872.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000629 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
630 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000631
632 // degenerate matrices
633 // mostly zero entries
634 mat.reset();
635 mat[SkMatrix::kMScaleX] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000636 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000637 mat.reset();
638 mat[SkMatrix::kMScaleY] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000639 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000640 mat.reset();
641 // linearly dependent entries
642 mat[SkMatrix::kMScaleX] = 1.f;
643 mat[SkMatrix::kMSkewX] = 2.f;
644 mat[SkMatrix::kMSkewY] = 4.f;
645 mat[SkMatrix::kMScaleY] = 8.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000646 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000647}
648
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000649// For test_matrix_homogeneous, below.
650static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) {
651 for (int i = 0; i < count; ++i) {
652 if (!scalar_nearly_equal_relative(a[i], b[i])) {
653 return false;
654 }
655 }
656 return true;
657}
658
659// For test_matrix_homogeneous, below.
660// Maps a single triple in src using m and compares results to those in dst
661static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3],
662 const SkScalar dst[3]) {
663 SkScalar res[3];
664 SkScalar ms[9] = {m[0], m[1], m[2],
665 m[3], m[4], m[5],
666 m[6], m[7], m[8]};
667 res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2];
668 res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5];
669 res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8];
670 return scalar_array_nearly_equal_relative(res, dst, 3);
671}
672
673static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
674 SkMatrix mat;
675
676 const float kRotation0 = 15.5f;
677 const float kRotation1 = -50.f;
678 const float kScale0 = 5000.f;
679
benjaminwagner7d974f52015-10-19 13:55:55 -0700680#if defined(GOOGLE3)
681 // Stack frame size is limited in GOOGLE3.
682 const int kTripleCount = 100;
683 const int kMatrixCount = 100;
684#else
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000685 const int kTripleCount = 1000;
686 const int kMatrixCount = 1000;
benjaminwagner7d974f52015-10-19 13:55:55 -0700687#endif
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000688 SkRandom rand;
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000689
690 SkScalar randTriples[3*kTripleCount];
691 for (int i = 0; i < 3*kTripleCount; ++i) {
692 randTriples[i] = rand.nextRangeF(-3000.f, 3000.f);
693 }
694
695 SkMatrix mats[kMatrixCount];
696 for (int i = 0; i < kMatrixCount; ++i) {
697 for (int j = 0; j < 9; ++j) {
698 mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
699 }
700 }
701
702 // identity
703 {
704 mat.reset();
705 SkScalar dst[3*kTripleCount];
706 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
707 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3));
708 }
709
710 // zero matrix
711 {
712 mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
713 SkScalar dst[3*kTripleCount];
714 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
715 SkScalar zeros[3] = {0.f, 0.f, 0.f};
716 for (int i = 0; i < kTripleCount; ++i) {
717 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3));
718 }
719 }
720
721 // zero point
722 {
723 SkScalar zeros[3] = {0.f, 0.f, 0.f};
724 for (int i = 0; i < kMatrixCount; ++i) {
725 SkScalar dst[3];
726 mats[i].mapHomogeneousPoints(dst, zeros, 1);
727 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3));
728 }
729 }
730
731 // doesn't crash with null dst, src, count == 0
732 {
halcanary96fcdcc2015-08-27 07:41:13 -0700733 mats[0].mapHomogeneousPoints(nullptr, nullptr, 0);
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000734 }
735
736 // uniform scale of point
737 {
738 mat.setScale(kScale0, kScale0);
739 SkScalar dst[3];
740 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
741 SkPoint pnt;
742 pnt.set(src[0], src[1]);
743 mat.mapHomogeneousPoints(dst, src, 1);
744 mat.mapPoints(&pnt, &pnt, 1);
745 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
746 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
747 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
748 }
749
750 // rotation of point
751 {
752 mat.setRotate(kRotation0);
753 SkScalar dst[3];
754 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
755 SkPoint pnt;
756 pnt.set(src[0], src[1]);
757 mat.mapHomogeneousPoints(dst, src, 1);
758 mat.mapPoints(&pnt, &pnt, 1);
759 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
760 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
761 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
762 }
763
764 // rotation, scale, rotation of point
765 {
766 mat.setRotate(kRotation1);
767 mat.postScale(kScale0, kScale0);
768 mat.postRotate(kRotation0);
769 SkScalar dst[3];
770 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
771 SkPoint pnt;
772 pnt.set(src[0], src[1]);
773 mat.mapHomogeneousPoints(dst, src, 1);
774 mat.mapPoints(&pnt, &pnt, 1);
775 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
776 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
777 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
778 }
779
780 // compare with naive approach
781 {
782 for (int i = 0; i < kMatrixCount; ++i) {
783 for (int j = 0; j < kTripleCount; ++j) {
784 SkScalar dst[3];
785 mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1);
786 REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst));
787 }
788 }
789 }
790
791}
792
reedadf99902015-03-19 16:10:54 -0700793static bool check_decompScale(const SkMatrix& matrix) {
794 SkSize scale;
795 SkMatrix remaining;
796
797 if (!matrix.decomposeScale(&scale, &remaining)) {
798 return false;
799 }
800 if (scale.width() <= 0 || scale.height() <= 0) {
801 return false;
802 }
803 remaining.preScale(scale.width(), scale.height());
804 return nearly_equal(matrix, remaining);
805}
806
807static void test_decompScale(skiatest::Reporter* reporter) {
808 SkMatrix m;
809
810 m.reset();
811 REPORTER_ASSERT(reporter, check_decompScale(m));
812 m.setScale(2, 3);
813 REPORTER_ASSERT(reporter, check_decompScale(m));
814 m.setRotate(35, 0, 0);
815 REPORTER_ASSERT(reporter, check_decompScale(m));
816
817 m.setScale(1, 0);
818 REPORTER_ASSERT(reporter, !check_decompScale(m));
819}
820
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000821DEF_TEST(Matrix, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000822 SkMatrix mat, inverse, iden1, iden2;
823
824 mat.reset();
825 mat.setTranslate(SK_Scalar1, SK_Scalar1);
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
reed@google.com2fb96cc2013-01-04 17:02:33 +0000830 mat.setScale(SkIntToScalar(2), SkIntToScalar(4));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000831 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000832 iden1.setConcat(mat, inverse);
833 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000834 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000835
reed@google.com2fb96cc2013-01-04 17:02:33 +0000836 mat.setScale(SK_Scalar1/2, SkIntToScalar(2));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000837 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000838 iden1.setConcat(mat, inverse);
839 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000840 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000841
842 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
843 mat.postRotate(SkIntToScalar(25));
halcanary96fcdcc2015-08-27 07:41:13 -0700844 REPORTER_ASSERT(reporter, mat.invert(nullptr));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000845 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000846 iden1.setConcat(mat, inverse);
847 REPORTER_ASSERT(reporter, is_identity(iden1));
848 iden2.setConcat(inverse, mat);
849 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@android.com4b7577b2009-06-29 16:14:41 +0000850 test_flatten(reporter, mat);
851 test_flatten(reporter, iden2);
reed@android.com80e39a72009-04-02 16:59:40 +0000852
reed@google.com2fb96cc2013-01-04 17:02:33 +0000853 mat.setScale(0, SK_Scalar1);
halcanary96fcdcc2015-08-27 07:41:13 -0700854 REPORTER_ASSERT(reporter, !mat.invert(nullptr));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000855 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
856 mat.setScale(SK_Scalar1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700857 REPORTER_ASSERT(reporter, !mat.invert(nullptr));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000858 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
859
robertphillips20eee3f2015-06-19 05:14:26 -0700860 // Inverting this matrix results in a non-finite matrix
861 mat.setAll(0.0f, 1.0f, 2.0f,
862 0.0f, 1.0f, -3.40277175e+38f,
863 1.00003040f, 1.0f, 0.0f);
halcanary96fcdcc2015-08-27 07:41:13 -0700864 REPORTER_ASSERT(reporter, !mat.invert(nullptr));
robertphillips20eee3f2015-06-19 05:14:26 -0700865 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
866
reed@android.comed673312009-02-27 16:24:51 +0000867 // rectStaysRect test
868 {
869 static const struct {
870 SkScalar m00, m01, m10, m11;
871 bool mStaysRect;
872 }
873 gRectStaysRectSamples[] = {
874 { 0, 0, 0, 0, false },
875 { 0, 0, 0, SK_Scalar1, false },
876 { 0, 0, SK_Scalar1, 0, false },
877 { 0, 0, SK_Scalar1, SK_Scalar1, false },
878 { 0, SK_Scalar1, 0, 0, false },
879 { 0, SK_Scalar1, 0, SK_Scalar1, false },
880 { 0, SK_Scalar1, SK_Scalar1, 0, true },
881 { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
882 { SK_Scalar1, 0, 0, 0, false },
883 { SK_Scalar1, 0, 0, SK_Scalar1, true },
884 { SK_Scalar1, 0, SK_Scalar1, 0, false },
885 { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
886 { SK_Scalar1, SK_Scalar1, 0, 0, false },
887 { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
888 { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
889 { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
890 };
reed@android.com80e39a72009-04-02 16:59:40 +0000891
reed@android.comed673312009-02-27 16:24:51 +0000892 for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
893 SkMatrix m;
reed@android.com80e39a72009-04-02 16:59:40 +0000894
reed@android.comed673312009-02-27 16:24:51 +0000895 m.reset();
896 m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
897 m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
898 m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
899 m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
900 REPORTER_ASSERT(reporter,
901 m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
902 }
903 }
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000904
bungeman@google.comba7983e2011-07-13 20:18:16 +0000905 mat.reset();
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000906 mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
907 mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
908 mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
909 mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
910 mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
911 mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000912 SkScalar affine[6];
913 REPORTER_ASSERT(reporter, mat.asAffine(affine));
914
915 #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
916 REPORTER_ASSERT(reporter, affineEqual(ScaleX));
917 REPORTER_ASSERT(reporter, affineEqual(SkewY));
918 REPORTER_ASSERT(reporter, affineEqual(SkewX));
919 REPORTER_ASSERT(reporter, affineEqual(ScaleY));
920 REPORTER_ASSERT(reporter, affineEqual(TransX));
921 REPORTER_ASSERT(reporter, affineEqual(TransY));
922 #undef affineEqual
923
reed3f43f8a2015-01-20 19:58:36 -0800924 mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2);
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000925 REPORTER_ASSERT(reporter, !mat.asAffine(affine));
bsalomon@google.com38396322011-09-09 19:32:04 +0000926
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000927 SkMatrix mat2;
928 mat2.reset();
929 mat.reset();
930 SkScalar zero = 0;
931 mat.set(SkMatrix::kMSkewX, -zero);
932 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
933
934 mat2.reset();
935 mat.reset();
936 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
937 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
938 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
939
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000940 test_matrix_min_max_scale(reporter);
jvanverth17a845f2014-09-02 13:15:40 -0700941 test_matrix_preserve_shape(reporter);
reed@google.com97cd69c2012-10-12 14:35:48 +0000942 test_matrix_recttorect(reporter);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000943 test_matrix_decomposition(reporter);
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000944 test_matrix_homogeneous(reporter);
reed451e8222014-12-13 08:46:48 -0800945 test_set9(reporter);
reedadf99902015-03-19 16:10:54 -0700946
947 test_decompScale(reporter);
reed2dcb6152016-07-05 20:10:42 -0700948
949 mat.setScaleTranslate(2, 3, 1, 4);
950 mat2.setScale(2, 3);
951 mat2.postTranslate(1, 4);
952 REPORTER_ASSERT(reporter, mat == mat2);
reed@android.comed673312009-02-27 16:24:51 +0000953}
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +0000954
955DEF_TEST(Matrix_Concat, r) {
956 SkMatrix a;
957 a.setTranslate(10, 20);
958
959 SkMatrix b;
960 b.setScale(3, 5);
961
962 SkMatrix expected;
963 expected.setConcat(a,b);
964
965 REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b));
966}
reed47df89e2016-06-30 06:38:54 -0700967
968// Test that all variants of maprect are correct.
969DEF_TEST(Matrix_maprects, r) {
970 const SkScalar scale = 1000;
971
972 SkMatrix mat;
973 mat.setScale(2, 3);
974 mat.postTranslate(1, 4);
975
976 SkRandom rand;
977 for (int i = 0; i < 10000; ++i) {
978 SkRect src = SkRect::MakeLTRB(rand.nextSScalar1() * scale,
979 rand.nextSScalar1() * scale,
980 rand.nextSScalar1() * scale,
981 rand.nextSScalar1() * scale);
982 SkRect dst[3];
983
984 mat.mapPoints((SkPoint*)&dst[0].fLeft, (SkPoint*)&src.fLeft, 2);
985 dst[0].sort();
986 mat.mapRect(&dst[1], src);
halcanaryc5769b22016-08-10 07:13:21 -0700987 mat.mapRectScaleTranslate(&dst[2], src);
reed47df89e2016-06-30 06:38:54 -0700988
989 REPORTER_ASSERT(r, dst[0] == dst[1]);
990 REPORTER_ASSERT(r, dst[0] == dst[2]);
991 }
992}