blob: fc7ac42a99690d1269d1f2c04f360a918011b25a [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
reed@google.com97cd69c2012-10-12 14:35:48 +000074static void test_matrix_recttorect(skiatest::Reporter* reporter) {
75 SkRect src, dst;
76 SkMatrix matrix;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000077
reed@google.com97cd69c2012-10-12 14:35:48 +000078 src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10);
79 dst = src;
80 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
81 REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
82 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000083
reed@google.com97cd69c2012-10-12 14:35:48 +000084 dst.offset(SK_Scalar1, SK_Scalar1);
85 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
86 REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
87 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000088
reed@google.com97cd69c2012-10-12 14:35:48 +000089 dst.fRight += SK_Scalar1;
90 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +000091 REPORTER_ASSERT(reporter,
robertphillips@google.com93f03322012-12-03 17:35:19 +000092 (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
reed@google.com97cd69c2012-10-12 14:35:48 +000093 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
94
95 dst = src;
96 dst.fRight = src.fRight * 2;
97 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
98 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
99 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
100}
101
reed@android.com4b7577b2009-06-29 16:14:41 +0000102static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
103 // 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 +0000104 static const size_t kBufferSize = SkMatrix::kMaxFlattenSize + 100;
105 char buffer[kBufferSize];
106 size_t size1 = m.writeToMemory(NULL);
107 size_t size2 = m.writeToMemory(buffer);
reed@android.com4b7577b2009-06-29 16:14:41 +0000108 REPORTER_ASSERT(reporter, size1 == size2);
109 REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110
reed@android.com4b7577b2009-06-29 16:14:41 +0000111 SkMatrix m2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000112 size_t size3 = m2.readFromMemory(buffer, kBufferSize);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000113 REPORTER_ASSERT(reporter, size1 == size3);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000114 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000116 char buffer2[kBufferSize];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000117 size3 = m2.writeToMemory(buffer2);
118 REPORTER_ASSERT(reporter, size1 == size3);
reed@android.com4b7577b2009-06-29 16:14:41 +0000119 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
120}
121
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000122static void test_matrix_min_max_scale(skiatest::Reporter* reporter) {
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000123 SkScalar scales[2];
124 bool success;
125
bsalomon@google.com38396322011-09-09 19:32:04 +0000126 SkMatrix identity;
127 identity.reset();
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000128 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinScale());
129 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000130 success = identity.getMinMaxScales(scales);
131 REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000132
133 SkMatrix scale;
134 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000135 REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinScale());
136 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000137 success = scale.getMinMaxScales(scales);
138 REPORTER_ASSERT(reporter, success && SK_Scalar1 * 2 == scales[0] && SK_Scalar1 * 4 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000139
140 SkMatrix rot90Scale;
141 rot90Scale.setRotate(90 * SK_Scalar1);
142 rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000143 REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale());
144 REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000145 success = rot90Scale.getMinMaxScales(scales);
146 REPORTER_ASSERT(reporter, success && SK_Scalar1 / 4 == scales[0] && SK_Scalar1 / 2 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000147
148 SkMatrix rotate;
149 rotate.setRotate(128 * SK_Scalar1);
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000150 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinScale(), SK_ScalarNearlyZero));
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000151 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxScale(), SK_ScalarNearlyZero));
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000152 success = rotate.getMinMaxScales(scales);
153 REPORTER_ASSERT(reporter, success);
154 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[0], SK_ScalarNearlyZero));
155 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[1], SK_ScalarNearlyZero));
bsalomon@google.com38396322011-09-09 19:32:04 +0000156
157 SkMatrix translate;
158 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000159 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinScale());
160 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000161 success = translate.getMinMaxScales(scales);
162 REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000163
164 SkMatrix perspX;
165 perspX.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000166 perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000));
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000167 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinScale());
168 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000169 // Verify that getMinMaxScales() doesn't update the scales array on failure.
170 scales[0] = -5;
171 scales[1] = -5;
172 success = perspX.getMinMaxScales(scales);
173 REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000174
175 SkMatrix perspY;
176 perspY.reset();
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000177 perspY.setPerspY(SkScalarToPersp(-SK_Scalar1 / 500));
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000178 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale());
179 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale());
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000180 scales[0] = -5;
181 scales[1] = -5;
182 success = perspY.getMinMaxScales(scales);
183 REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1 == scales[1]);
bsalomon@google.com38396322011-09-09 19:32:04 +0000184
185 SkMatrix baseMats[] = {scale, rot90Scale, rotate,
186 translate, perspX, perspY};
187 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000188 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000189 mats[i] = baseMats[i];
190 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
191 REPORTER_ASSERT(reporter, invertable);
192 }
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000193 SkRandom rand;
bsalomon@google.com38396322011-09-09 19:32:04 +0000194 for (int m = 0; m < 1000; ++m) {
195 SkMatrix mat;
196 mat.reset();
197 for (int i = 0; i < 4; ++i) {
198 int x = rand.nextU() % SK_ARRAY_COUNT(mats);
199 mat.postConcat(mats[x]);
200 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000201
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000202 SkScalar minScale = mat.getMinScale();
203 SkScalar maxScale = mat.getMaxScale();
204 REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0));
205 REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective());
bsalomon@google.com38396322011-09-09 19:32:04 +0000206
commit-bot@chromium.org311a3cd2014-05-20 17:02:03 +0000207 SkScalar scales[2];
208 bool success = mat.getMinMaxScales(scales);
209 REPORTER_ASSERT(reporter, success == !mat.hasPerspective());
210 REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale));
211
bsalomon@google.com38396322011-09-09 19:32:04 +0000212 if (mat.hasPerspective()) {
213 m -= 1; // try another non-persp matrix
214 continue;
215 }
216
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000217 // test a bunch of vectors. All should be scaled by between minScale and maxScale
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000218 // (modulo some error) and we should find a vector that is scaled by almost each.
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000219 static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100;
220 static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100;
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000221 SkScalar max = 0, min = SK_ScalarMax;
bsalomon@google.com38396322011-09-09 19:32:04 +0000222 SkVector vectors[1000];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000223 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000224 vectors[i].fX = rand.nextSScalar1();
225 vectors[i].fY = rand.nextSScalar1();
226 if (!vectors[i].normalize()) {
227 i -= 1;
228 continue;
229 }
230 }
231 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
tomhudson@google.com83a44462011-10-27 15:27:51 +0000232 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000233 SkScalar d = vectors[i].length();
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000234 REPORTER_ASSERT(reporter, SkScalarDiv(d, maxScale) < gVectorScaleTol);
235 REPORTER_ASSERT(reporter, SkScalarDiv(minScale, d) < gVectorScaleTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000236 if (max < d) {
237 max = d;
238 }
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000239 if (min > d) {
240 min = d;
241 }
bsalomon@google.com38396322011-09-09 19:32:04 +0000242 }
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000243 REPORTER_ASSERT(reporter, SkScalarDiv(max, maxScale) >= gCloseScaleTol);
244 REPORTER_ASSERT(reporter, SkScalarDiv(minScale, min) >= gCloseScaleTol);
bsalomon@google.com38396322011-09-09 19:32:04 +0000245 }
246}
247
jvanverth17a845f2014-09-02 13:15:40 -0700248static void test_matrix_preserve_shape(skiatest::Reporter* reporter) {
bsalomon@google.com69afee12012-04-25 15:07:40 +0000249 SkMatrix mat;
250
251 // identity
252 mat.setIdentity();
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000253 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700254 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000255
256 // translation only
257 mat.reset();
258 mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000259 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700260 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000261
262 // scale with same size
263 mat.reset();
264 mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000265 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700266 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000267
268 // scale with one negative
269 mat.reset();
270 mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000271 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700272 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000273
274 // scale with different size
275 mat.reset();
276 mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000277 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700278 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000279
280 // scale with same size at a pivot point
281 mat.reset();
282 mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
283 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000284 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700285 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000286
287 // scale with different size at a pivot point
288 mat.reset();
289 mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
290 SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000291 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700292 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000293
294 // skew with same size
295 mat.reset();
296 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000297 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700298 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000299
300 // skew with different size
301 mat.reset();
302 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000303 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700304 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000305
306 // skew with same size at a pivot point
307 mat.reset();
308 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
309 SkIntToScalar(2), SkIntToScalar(2));
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 // skew with different size at a pivot point
314 mat.reset();
315 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
316 SkIntToScalar(2), SkIntToScalar(2));
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 // perspective x
321 mat.reset();
322 mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 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 // perspective y
327 mat.reset();
328 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2));
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
bsalomon@google.com69afee12012-04-25 15:07:40 +0000332 // rotate
333 for (int angle = 0; angle < 360; ++angle) {
334 mat.reset();
335 mat.setRotate(SkIntToScalar(angle));
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
340 // see if there are any accumulated precision issues
341 mat.reset();
342 for (int i = 1; i < 360; i++) {
343 mat.postRotate(SkIntToScalar(1));
344 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000345 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700346 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000347
348 // rotate + translate
349 mat.reset();
350 mat.setRotate(SkIntToScalar(30));
351 mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000352 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700353 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000354
355 // rotate + uniform scale
356 mat.reset();
357 mat.setRotate(SkIntToScalar(30));
358 mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000359 REPORTER_ASSERT(reporter, mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700360 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000361
362 // rotate + non-uniform scale
363 mat.reset();
364 mat.setRotate(SkIntToScalar(30));
365 mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000366 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700367 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
368
369 // non-uniform scale + rotate
370 mat.reset();
371 mat.setScale(SkIntToScalar(3), SkIntToScalar(2));
372 mat.postRotate(SkIntToScalar(30));
373 REPORTER_ASSERT(reporter, !mat.isSimilarity());
374 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000375
376 // all zero
377 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000378 REPORTER_ASSERT(reporter, !mat.isSimilarity());
jvanverth17a845f2014-09-02 13:15:40 -0700379 REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
bsalomon@google.com69afee12012-04-25 15:07:40 +0000380
381 // all zero except perspective
jvanverth17a845f2014-09-02 13:15:40 -0700382 mat.reset();
bsalomon@google.com69afee12012-04-25 15:07:40 +0000383 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
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
jvanverth17a845f2014-09-02 13:15:40 -0700387 // scales zero, only skews (rotation)
388 mat.setAll(0, SK_Scalar1, 0,
389 -SK_Scalar1, 0, 0,
390 0, 0, SkMatrix::I()[8]);
391 REPORTER_ASSERT(reporter, mat.isSimilarity());
392 REPORTER_ASSERT(reporter, mat.preservesRightAngles());
393
394 // scales zero, only skews (reflection)
bsalomon@google.com69afee12012-04-25 15:07:40 +0000395 mat.setAll(0, SK_Scalar1, 0,
396 SK_Scalar1, 0, 0,
397 0, 0, SkMatrix::I()[8]);
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
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000402// For test_matrix_decomposition, below.
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000403static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000404 SkScalar tolerance = SK_ScalarNearlyZero) {
405 // from Bruce Dawson
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000406 // absolute check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000407 SkScalar diff = SkScalarAbs(a - b);
408 if (diff < tolerance) {
409 return true;
410 }
411
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000412 // relative check
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000413 a = SkScalarAbs(a);
414 b = SkScalarAbs(b);
415 SkScalar largest = (b > a) ? b : a;
416
417 if (diff <= largest*tolerance) {
418 return true;
419 }
420
421 return false;
422}
423
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000424static bool check_matrix_recomposition(const SkMatrix& mat,
425 const SkPoint& rotation1,
426 const SkPoint& scale,
427 const SkPoint& rotation2) {
428 SkScalar c1 = rotation1.fX;
429 SkScalar s1 = rotation1.fY;
430 SkScalar scaleX = scale.fX;
431 SkScalar scaleY = scale.fY;
432 SkScalar c2 = rotation2.fX;
433 SkScalar s2 = rotation2.fY;
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000434
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000435 // We do a relative check here because large scale factors cause problems with an absolute check
436 bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
437 scaleX*c1*c2 - scaleY*s1*s2) &&
438 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
439 -scaleX*s1*c2 - scaleY*c1*s2) &&
440 scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
441 scaleX*c1*s2 + scaleY*s1*c2) &&
442 scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
443 -scaleX*s1*s2 + scaleY*c1*c2);
444 return result;
445}
446
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000447static void test_matrix_decomposition(skiatest::Reporter* reporter) {
448 SkMatrix mat;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000449 SkPoint rotation1, scale, rotation2;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000450
451 const float kRotation0 = 15.5f;
452 const float kRotation1 = -50.f;
453 const float kScale0 = 5000.f;
454 const float kScale1 = 0.001f;
455
456 // identity
457 mat.reset();
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000458 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
459 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000460 // make sure it doesn't crash if we pass in NULLs
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000461 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000462
463 // rotation only
464 mat.setRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000465 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
466 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000467
468 // uniform scale only
469 mat.setScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000470 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
471 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000472
473 // anisotropic scale only
474 mat.setScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000475 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
476 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000477
478 // rotation then uniform scale
479 mat.setRotate(kRotation1);
480 mat.postScale(kScale0, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000481 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
482 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000483
484 // uniform scale then rotation
485 mat.setScale(kScale0, kScale0);
486 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000487 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
488 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000489
490 // rotation then uniform scale+reflection
491 mat.setRotate(kRotation0);
492 mat.postScale(kScale1, -kScale1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000493 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
494 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000495
496 // uniform scale+reflection, then rotate
497 mat.setScale(kScale0, -kScale0);
498 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000499 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
500 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000501
502 // rotation then anisotropic scale
503 mat.setRotate(kRotation1);
504 mat.postScale(kScale1, kScale0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000505 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
506 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000507
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000508 // rotation then anisotropic scale
509 mat.setRotate(90);
510 mat.postScale(kScale1, kScale0);
511 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
512 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000513
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000514 // anisotropic scale then rotation
515 mat.setScale(kScale1, kScale0);
516 mat.postRotate(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));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000519
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000520 // anisotropic scale then rotation
521 mat.setScale(kScale1, kScale0);
522 mat.postRotate(90);
523 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
524 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000525
526 // rotation, uniform scale, then different rotation
527 mat.setRotate(kRotation1);
528 mat.postScale(kScale0, kScale0);
529 mat.postRotate(kRotation0);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000530 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
531 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000532
533 // rotation, anisotropic scale, then different rotation
534 mat.setRotate(kRotation0);
535 mat.postScale(kScale1, kScale0);
536 mat.postRotate(kRotation1);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000537 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
538 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
skia.committer@gmail.com85092f02013-09-04 07:01:39 +0000539
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000540 // rotation, anisotropic scale + reflection, then different rotation
541 mat.setRotate(kRotation0);
542 mat.postScale(-kScale1, kScale0);
543 mat.postRotate(kRotation1);
544 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
547 // try some random matrices
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000548 SkRandom rand;
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000549 for (int m = 0; m < 1000; ++m) {
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000550 SkScalar rot0 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000551 SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
552 SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000553 SkScalar rot1 = rand.nextRangeF(-180, 180);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000554 mat.setRotate(rot0);
555 mat.postScale(sx, sy);
556 mat.postRotate(rot1);
557
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000558 if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
559 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000560 } else {
561 // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
562 SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
563 mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
564 REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
565 }
566 }
567
568 // translation shouldn't affect this
569 mat.postTranslate(-1000.f, 1000.f);
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000570 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
571 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000572
573 // perspective shouldn't affect this
jvanverth@google.com588f3d32013-07-24 18:44:10 +0000574 mat[SkMatrix::kMPersp0] = 12.f;
575 mat[SkMatrix::kMPersp1] = 4.f;
576 mat[SkMatrix::kMPersp2] = 1872.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000577 REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
578 REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000579
580 // degenerate matrices
581 // mostly zero entries
582 mat.reset();
583 mat[SkMatrix::kMScaleX] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000584 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000585 mat.reset();
586 mat[SkMatrix::kMScaleY] = 0.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000587 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000588 mat.reset();
589 // linearly dependent entries
590 mat[SkMatrix::kMScaleX] = 1.f;
591 mat[SkMatrix::kMSkewX] = 2.f;
592 mat[SkMatrix::kMSkewY] = 4.f;
593 mat[SkMatrix::kMScaleY] = 8.f;
commit-bot@chromium.org5b2e2642013-09-03 19:08:14 +0000594 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000595}
596
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000597// For test_matrix_homogeneous, below.
598static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) {
599 for (int i = 0; i < count; ++i) {
600 if (!scalar_nearly_equal_relative(a[i], b[i])) {
601 return false;
602 }
603 }
604 return true;
605}
606
607// For test_matrix_homogeneous, below.
608// Maps a single triple in src using m and compares results to those in dst
609static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3],
610 const SkScalar dst[3]) {
611 SkScalar res[3];
612 SkScalar ms[9] = {m[0], m[1], m[2],
613 m[3], m[4], m[5],
614 m[6], m[7], m[8]};
615 res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2];
616 res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5];
617 res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8];
618 return scalar_array_nearly_equal_relative(res, dst, 3);
619}
620
621static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
622 SkMatrix mat;
623
624 const float kRotation0 = 15.5f;
625 const float kRotation1 = -50.f;
626 const float kScale0 = 5000.f;
627
628 const int kTripleCount = 1000;
629 const int kMatrixCount = 1000;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000630 SkRandom rand;
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000631
632 SkScalar randTriples[3*kTripleCount];
633 for (int i = 0; i < 3*kTripleCount; ++i) {
634 randTriples[i] = rand.nextRangeF(-3000.f, 3000.f);
635 }
636
637 SkMatrix mats[kMatrixCount];
638 for (int i = 0; i < kMatrixCount; ++i) {
639 for (int j = 0; j < 9; ++j) {
640 mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
641 }
642 }
643
644 // identity
645 {
646 mat.reset();
647 SkScalar dst[3*kTripleCount];
648 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
649 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3));
650 }
651
652 // zero matrix
653 {
654 mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
655 SkScalar dst[3*kTripleCount];
656 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
657 SkScalar zeros[3] = {0.f, 0.f, 0.f};
658 for (int i = 0; i < kTripleCount; ++i) {
659 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3));
660 }
661 }
662
663 // zero point
664 {
665 SkScalar zeros[3] = {0.f, 0.f, 0.f};
666 for (int i = 0; i < kMatrixCount; ++i) {
667 SkScalar dst[3];
668 mats[i].mapHomogeneousPoints(dst, zeros, 1);
669 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3));
670 }
671 }
672
673 // doesn't crash with null dst, src, count == 0
674 {
675 mats[0].mapHomogeneousPoints(NULL, NULL, 0);
676 }
677
678 // uniform scale of point
679 {
680 mat.setScale(kScale0, kScale0);
681 SkScalar dst[3];
682 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
683 SkPoint pnt;
684 pnt.set(src[0], src[1]);
685 mat.mapHomogeneousPoints(dst, src, 1);
686 mat.mapPoints(&pnt, &pnt, 1);
687 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
688 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
689 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
690 }
691
692 // rotation of point
693 {
694 mat.setRotate(kRotation0);
695 SkScalar dst[3];
696 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
697 SkPoint pnt;
698 pnt.set(src[0], src[1]);
699 mat.mapHomogeneousPoints(dst, src, 1);
700 mat.mapPoints(&pnt, &pnt, 1);
701 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
702 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
703 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
704 }
705
706 // rotation, scale, rotation of point
707 {
708 mat.setRotate(kRotation1);
709 mat.postScale(kScale0, kScale0);
710 mat.postRotate(kRotation0);
711 SkScalar dst[3];
712 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
713 SkPoint pnt;
714 pnt.set(src[0], src[1]);
715 mat.mapHomogeneousPoints(dst, src, 1);
716 mat.mapPoints(&pnt, &pnt, 1);
717 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
718 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
719 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
720 }
721
722 // compare with naive approach
723 {
724 for (int i = 0; i < kMatrixCount; ++i) {
725 for (int j = 0; j < kTripleCount; ++j) {
726 SkScalar dst[3];
727 mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1);
728 REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst));
729 }
730 }
731 }
732
733}
734
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000735DEF_TEST(Matrix, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000736 SkMatrix mat, inverse, iden1, iden2;
737
738 mat.reset();
739 mat.setTranslate(SK_Scalar1, SK_Scalar1);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000740 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000741 iden1.setConcat(mat, inverse);
742 REPORTER_ASSERT(reporter, is_identity(iden1));
743
reed@google.com2fb96cc2013-01-04 17:02:33 +0000744 mat.setScale(SkIntToScalar(2), SkIntToScalar(4));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000745 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000746 iden1.setConcat(mat, inverse);
747 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000748 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000749
reed@google.com2fb96cc2013-01-04 17:02:33 +0000750 mat.setScale(SK_Scalar1/2, SkIntToScalar(2));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000751 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000752 iden1.setConcat(mat, inverse);
753 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000754 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000755
756 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
757 mat.postRotate(SkIntToScalar(25));
758 REPORTER_ASSERT(reporter, mat.invert(NULL));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000759 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000760 iden1.setConcat(mat, inverse);
761 REPORTER_ASSERT(reporter, is_identity(iden1));
762 iden2.setConcat(inverse, mat);
763 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@android.com4b7577b2009-06-29 16:14:41 +0000764 test_flatten(reporter, mat);
765 test_flatten(reporter, iden2);
reed@android.com80e39a72009-04-02 16:59:40 +0000766
reed@google.com2fb96cc2013-01-04 17:02:33 +0000767 mat.setScale(0, SK_Scalar1);
reed@google.come40591d2013-01-30 15:47:42 +0000768 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000769 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
770 mat.setScale(SK_Scalar1, 0);
reed@google.come40591d2013-01-30 15:47:42 +0000771 REPORTER_ASSERT(reporter, !mat.invert(NULL));
reed@google.com2fb96cc2013-01-04 17:02:33 +0000772 REPORTER_ASSERT(reporter, !mat.invert(&inverse));
773
reed@android.comed673312009-02-27 16:24:51 +0000774 // rectStaysRect test
775 {
776 static const struct {
777 SkScalar m00, m01, m10, m11;
778 bool mStaysRect;
779 }
780 gRectStaysRectSamples[] = {
781 { 0, 0, 0, 0, false },
782 { 0, 0, 0, SK_Scalar1, false },
783 { 0, 0, SK_Scalar1, 0, false },
784 { 0, 0, SK_Scalar1, SK_Scalar1, false },
785 { 0, SK_Scalar1, 0, 0, false },
786 { 0, SK_Scalar1, 0, SK_Scalar1, false },
787 { 0, SK_Scalar1, SK_Scalar1, 0, true },
788 { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
789 { SK_Scalar1, 0, 0, 0, false },
790 { SK_Scalar1, 0, 0, SK_Scalar1, true },
791 { SK_Scalar1, 0, SK_Scalar1, 0, false },
792 { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
793 { SK_Scalar1, SK_Scalar1, 0, 0, false },
794 { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
795 { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
796 { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
797 };
reed@android.com80e39a72009-04-02 16:59:40 +0000798
reed@android.comed673312009-02-27 16:24:51 +0000799 for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
800 SkMatrix m;
reed@android.com80e39a72009-04-02 16:59:40 +0000801
reed@android.comed673312009-02-27 16:24:51 +0000802 m.reset();
803 m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
804 m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
805 m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
806 m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
807 REPORTER_ASSERT(reporter,
808 m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
809 }
810 }
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000811
bungeman@google.comba7983e2011-07-13 20:18:16 +0000812 mat.reset();
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000813 mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
814 mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
815 mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
816 mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
817 mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
818 mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000819 SkScalar affine[6];
820 REPORTER_ASSERT(reporter, mat.asAffine(affine));
821
822 #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
823 REPORTER_ASSERT(reporter, affineEqual(ScaleX));
824 REPORTER_ASSERT(reporter, affineEqual(SkewY));
825 REPORTER_ASSERT(reporter, affineEqual(SkewX));
826 REPORTER_ASSERT(reporter, affineEqual(ScaleY));
827 REPORTER_ASSERT(reporter, affineEqual(TransX));
828 REPORTER_ASSERT(reporter, affineEqual(TransY));
829 #undef affineEqual
830
bungeman@google.com07faed12011-10-07 21:55:56 +0000831 mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000832 REPORTER_ASSERT(reporter, !mat.asAffine(affine));
bsalomon@google.com38396322011-09-09 19:32:04 +0000833
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000834 SkMatrix mat2;
835 mat2.reset();
836 mat.reset();
837 SkScalar zero = 0;
838 mat.set(SkMatrix::kMSkewX, -zero);
839 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
840
841 mat2.reset();
842 mat.reset();
843 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
844 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
845 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
846
commit-bot@chromium.org18786512014-05-20 14:53:45 +0000847 test_matrix_min_max_scale(reporter);
jvanverth17a845f2014-09-02 13:15:40 -0700848 test_matrix_preserve_shape(reporter);
reed@google.com97cd69c2012-10-12 14:35:48 +0000849 test_matrix_recttorect(reporter);
commit-bot@chromium.org08284e42013-07-24 18:08:08 +0000850 test_matrix_decomposition(reporter);
egdaniel@google.com259fbaf2013-08-15 21:12:11 +0000851 test_matrix_homogeneous(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000852}
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +0000853
854DEF_TEST(Matrix_Concat, r) {
855 SkMatrix a;
856 a.setTranslate(10, 20);
857
858 SkMatrix b;
859 b.setScale(3, 5);
860
861 SkMatrix expected;
862 expected.setConcat(a,b);
863
864 REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b));
865}