epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 8 | #include "SkMath.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 9 | #include "SkMatrix.h" |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 10 | #include "SkMatrixUtils.h" |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 13 | |
| 14 | static bool nearly_equal_scalar(SkScalar a, SkScalar b) { |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 15 | const SkScalar tolerance = SK_Scalar1 / 200000; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | return SkScalarAbs(a - b) <= tolerance; |
| 17 | } |
| 18 | |
| 19 | static 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.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 22 | SkDebugf("not equal %g %g\n", (float)a[i], (float)b[i]); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 23 | return false; |
| 24 | } |
| 25 | } |
| 26 | return true; |
| 27 | } |
| 28 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 29 | static 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.com | 39d4f3a | 2012-03-26 17:25:45 +0000 | [diff] [blame] | 35 | if (equal) { |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 36 | 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.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 40 | int aValI = *SkTCast<int*>(&aVal); |
| 41 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 42 | if (0 == aVal && 0 == bVal && aValI != bValI) { |
| 43 | foundZeroSignDiff = true; |
| 44 | } else { |
halcanary | e76ca8b | 2016-05-20 10:36:50 -0700 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 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.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 54 | int aValI = *SkTCast<int*>(&aVal); |
| 55 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 56 | 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.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 64 | } |
| 65 | return equal; |
| 66 | } |
| 67 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 68 | static bool is_identity(const SkMatrix& m) { |
| 69 | SkMatrix identity; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 70 | identity.reset(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 71 | return nearly_equal(m, identity); |
| 72 | } |
| 73 | |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 74 | static 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 | |
| 91 | static 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); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 96 | |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 97 | m.setScale(2, 3); |
| 98 | assert9(reporter, m, 2, 0, 0, 0, 3, 0, 0, 0, 1); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 99 | |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 100 | m.postTranslate(4, 5); |
| 101 | assert9(reporter, m, 2, 0, 4, 0, 3, 5, 0, 0, 1); |
| 102 | |
| 103 | SkScalar buffer[9]; |
| 104 | sk_bzero(buffer, sizeof(buffer)); |
| 105 | buffer[SkMatrix::kMScaleX] = 1; |
| 106 | buffer[SkMatrix::kMScaleY] = 1; |
| 107 | buffer[SkMatrix::kMPersp2] = 1; |
| 108 | REPORTER_ASSERT(reporter, !m.isIdentity()); |
| 109 | m.set9(buffer); |
| 110 | REPORTER_ASSERT(reporter, m.isIdentity()); |
| 111 | } |
| 112 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 113 | static void test_matrix_recttorect(skiatest::Reporter* reporter) { |
| 114 | SkRect src, dst; |
| 115 | SkMatrix matrix; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 116 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 117 | 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.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 122 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 123 | 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.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 127 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 128 | dst.fRight += SK_Scalar1; |
| 129 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 130 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 131 | (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType()); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 132 | 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.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 141 | static 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.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 143 | static const size_t kBufferSize = SkMatrix::kMaxFlattenSize + 100; |
| 144 | char buffer[kBufferSize]; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | size_t size1 = m.writeToMemory(nullptr); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 146 | size_t size2 = m.writeToMemory(buffer); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 147 | REPORTER_ASSERT(reporter, size1 == size2); |
| 148 | REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 149 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 150 | SkMatrix m2; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 151 | size_t size3 = m2.readFromMemory(buffer, kBufferSize); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 152 | REPORTER_ASSERT(reporter, size1 == size3); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 154 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 155 | char buffer2[kBufferSize]; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 156 | size3 = m2.writeToMemory(buffer2); |
| 157 | REPORTER_ASSERT(reporter, size1 == size3); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
| 159 | } |
| 160 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 161 | static void test_matrix_min_max_scale(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 162 | SkScalar scales[2]; |
| 163 | bool success; |
| 164 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 165 | SkMatrix identity; |
| 166 | identity.reset(); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 167 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinScale()); |
| 168 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 169 | success = identity.getMinMaxScales(scales); |
| 170 | REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 171 | |
| 172 | SkMatrix scale; |
| 173 | scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 174 | REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinScale()); |
| 175 | REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 176 | success = scale.getMinMaxScales(scales); |
| 177 | REPORTER_ASSERT(reporter, success && SK_Scalar1 * 2 == scales[0] && SK_Scalar1 * 4 == scales[1]); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 178 | |
| 179 | SkMatrix rot90Scale; |
| 180 | rot90Scale.setRotate(90 * SK_Scalar1); |
| 181 | rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 182 | REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale()); |
| 183 | REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 184 | success = rot90Scale.getMinMaxScales(scales); |
| 185 | REPORTER_ASSERT(reporter, success && SK_Scalar1 / 4 == scales[0] && SK_Scalar1 / 2 == scales[1]); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 186 | |
| 187 | SkMatrix rotate; |
| 188 | rotate.setRotate(128 * SK_Scalar1); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 189 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinScale(), SK_ScalarNearlyZero)); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxScale(), SK_ScalarNearlyZero)); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 191 | 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.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 195 | |
| 196 | SkMatrix translate; |
| 197 | translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 198 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinScale()); |
| 199 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 200 | success = translate.getMinMaxScales(scales); |
| 201 | REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 202 | |
| 203 | SkMatrix perspX; |
| 204 | perspX.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 205 | perspX.setPerspX(SK_Scalar1 / 1000); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 206 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinScale()); |
| 207 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 208 | success = perspX.getMinMaxScales(scales); |
bsalomon | ef2c7c7 | 2015-12-17 15:33:13 -0800 | [diff] [blame] | 209 | 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); |
bsalomon | ef2c7c7 | 2015-12-17 15:33:13 -0800 | [diff] [blame] | 216 | success = big.getMinMaxScales(scales); |
| 217 | REPORTER_ASSERT(reporter, !success); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 218 | |
kolczyk | cea22ae | 2016-07-16 11:52:37 -0700 | [diff] [blame] | 219 | // 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.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 227 | SkMatrix perspY; |
| 228 | perspY.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 229 | perspY.setPerspY(-SK_Scalar1 / 500); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 230 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale()); |
| 231 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 232 | 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.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 236 | |
| 237 | SkMatrix baseMats[] = {scale, rot90Scale, rotate, |
| 238 | translate, perspX, perspY}; |
| 239 | SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 240 | for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 241 | mats[i] = baseMats[i]; |
| 242 | bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); |
| 243 | REPORTER_ASSERT(reporter, invertable); |
| 244 | } |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 245 | SkRandom rand; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 246 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 253 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 254 | 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.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 258 | |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 259 | 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.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 264 | if (mat.hasPerspective()) { |
| 265 | m -= 1; // try another non-persp matrix |
| 266 | continue; |
| 267 | } |
| 268 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 269 | // test a bunch of vectors. All should be scaled by between minScale and maxScale |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 270 | // (modulo some error) and we should find a vector that is scaled by almost each. |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 271 | static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100; |
| 272 | static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100; |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 273 | SkScalar max = 0, min = SK_ScalarMax; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 274 | SkVector vectors[1000]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 275 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 276 | 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.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 284 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 285 | SkScalar d = vectors[i].length(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 286 | REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol); |
| 287 | REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 288 | if (max < d) { |
| 289 | max = d; |
| 290 | } |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 291 | if (min > d) { |
| 292 | min = d; |
| 293 | } |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 294 | } |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 295 | REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol); |
| 296 | REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 300 | static void test_matrix_preserve_shape(skiatest::Reporter* reporter) { |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 301 | SkMatrix mat; |
| 302 | |
| 303 | // identity |
| 304 | mat.setIdentity(); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 306 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 307 | |
| 308 | // translation only |
| 309 | mat.reset(); |
| 310 | mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 311 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 312 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 313 | |
| 314 | // scale with same size |
| 315 | mat.reset(); |
| 316 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 317 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 318 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 319 | |
| 320 | // scale with one negative |
| 321 | mat.reset(); |
| 322 | mat.setScale(SkIntToScalar(-15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 323 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 324 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 325 | |
| 326 | // scale with different size |
| 327 | mat.reset(); |
| 328 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 329 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 330 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 331 | |
| 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.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 336 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 337 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 338 | |
| 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.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 343 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 344 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 345 | |
| 346 | // skew with same size |
| 347 | mat.reset(); |
| 348 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 349 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 350 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 351 | |
| 352 | // skew with different size |
| 353 | mat.reset(); |
| 354 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 355 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 356 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 357 | |
| 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.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 362 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 363 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 364 | |
| 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.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 369 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 370 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 371 | |
| 372 | // perspective x |
| 373 | mat.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 374 | mat.setPerspX(SK_Scalar1 / 2); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 375 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 376 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 377 | |
| 378 | // perspective y |
| 379 | mat.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 380 | mat.setPerspY(SK_Scalar1 / 2); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 381 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 382 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 383 | |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 384 | // rotate |
| 385 | for (int angle = 0; angle < 360; ++angle) { |
| 386 | mat.reset(); |
| 387 | mat.setRotate(SkIntToScalar(angle)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 388 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 389 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 390 | } |
| 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.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 397 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 398 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 399 | |
| 400 | // rotate + translate |
| 401 | mat.reset(); |
| 402 | mat.setRotate(SkIntToScalar(30)); |
| 403 | mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 404 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 405 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 406 | |
| 407 | // rotate + uniform scale |
| 408 | mat.reset(); |
| 409 | mat.setRotate(SkIntToScalar(30)); |
| 410 | mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 411 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 412 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 413 | |
| 414 | // rotate + non-uniform scale |
| 415 | mat.reset(); |
| 416 | mat.setRotate(SkIntToScalar(30)); |
| 417 | mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 418 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 419 | 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.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 427 | |
| 428 | // all zero |
| 429 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 430 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 431 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 432 | |
| 433 | // all zero except perspective |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 434 | mat.reset(); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 435 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 436 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 437 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 438 | |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 439 | // 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.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 447 | mat.setAll(0, SK_Scalar1, 0, |
| 448 | SK_Scalar1, 0, 0, |
| 449 | 0, 0, SkMatrix::I()[8]); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 450 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 451 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 452 | } |
| 453 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 454 | // For test_matrix_decomposition, below. |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 455 | static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b, |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 456 | SkScalar tolerance = SK_ScalarNearlyZero) { |
| 457 | // from Bruce Dawson |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 458 | // absolute check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 459 | SkScalar diff = SkScalarAbs(a - b); |
| 460 | if (diff < tolerance) { |
| 461 | return true; |
| 462 | } |
| 463 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 464 | // relative check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 465 | 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.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 476 | static 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.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 486 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 487 | // 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.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 499 | static void test_matrix_decomposition(skiatest::Reporter* reporter) { |
| 500 | SkMatrix mat; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 501 | SkPoint rotation1, scale, rotation2; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 502 | |
| 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.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 510 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 511 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 512 | // make sure it doesn't crash if we pass in NULLs |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 513 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, nullptr, nullptr, nullptr)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 514 | |
| 515 | // rotation only |
| 516 | mat.setRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 517 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 518 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 519 | |
| 520 | // uniform scale only |
| 521 | mat.setScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 522 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 523 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 524 | |
| 525 | // anisotropic scale only |
| 526 | mat.setScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 527 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 528 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 529 | |
| 530 | // rotation then uniform scale |
| 531 | mat.setRotate(kRotation1); |
| 532 | mat.postScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 533 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 534 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 535 | |
| 536 | // uniform scale then rotation |
| 537 | mat.setScale(kScale0, kScale0); |
| 538 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 539 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 540 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 541 | |
| 542 | // rotation then uniform scale+reflection |
| 543 | mat.setRotate(kRotation0); |
| 544 | mat.postScale(kScale1, -kScale1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 545 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 546 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 547 | |
| 548 | // uniform scale+reflection, then rotate |
| 549 | mat.setScale(kScale0, -kScale0); |
| 550 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 551 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 552 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 553 | |
| 554 | // rotation then anisotropic scale |
| 555 | mat.setRotate(kRotation1); |
| 556 | mat.postScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 557 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 558 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 559 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 560 | // 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.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 565 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 566 | // anisotropic scale then rotation |
| 567 | mat.setScale(kScale1, kScale0); |
| 568 | mat.postRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 569 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 570 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 571 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 572 | // 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.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 577 | |
| 578 | // rotation, uniform scale, then different rotation |
| 579 | mat.setRotate(kRotation1); |
| 580 | mat.postScale(kScale0, kScale0); |
| 581 | mat.postRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 582 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 583 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 584 | |
| 585 | // rotation, anisotropic scale, then different rotation |
| 586 | mat.setRotate(kRotation0); |
| 587 | mat.postScale(kScale1, kScale0); |
| 588 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 589 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 590 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 591 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 592 | // 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.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 598 | |
| 599 | // try some random matrices |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 600 | SkRandom rand; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 601 | for (int m = 0; m < 1000; ++m) { |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 602 | SkScalar rot0 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 603 | SkScalar sx = rand.nextRangeF(-3000.f, 3000.f); |
| 604 | SkScalar sy = rand.nextRangeF(-3000.f, 3000.f); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 605 | SkScalar rot1 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 606 | mat.setRotate(rot0); |
| 607 | mat.postScale(sx, sy); |
| 608 | mat.postRotate(rot1); |
| 609 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 610 | if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) { |
| 611 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 612 | } 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.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 622 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 623 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 624 | |
| 625 | // perspective shouldn't affect this |
jvanverth@google.com | 588f3d3 | 2013-07-24 18:44:10 +0000 | [diff] [blame] | 626 | mat[SkMatrix::kMPersp0] = 12.f; |
| 627 | mat[SkMatrix::kMPersp1] = 4.f; |
| 628 | mat[SkMatrix::kMPersp2] = 1872.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 629 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 630 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 631 | |
| 632 | // degenerate matrices |
| 633 | // mostly zero entries |
| 634 | mat.reset(); |
| 635 | mat[SkMatrix::kMScaleX] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 636 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 637 | mat.reset(); |
| 638 | mat[SkMatrix::kMScaleY] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 639 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 640 | 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.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 646 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 647 | } |
| 648 | |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 649 | // For test_matrix_homogeneous, below. |
| 650 | static 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 |
| 661 | static 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 | |
| 673 | static 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 | |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 680 | #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.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 685 | const int kTripleCount = 1000; |
| 686 | const int kMatrixCount = 1000; |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 687 | #endif |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 688 | SkRandom rand; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 689 | |
| 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 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 733 | mats[0].mapHomogeneousPoints(nullptr, nullptr, 0); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 734 | } |
| 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 | |
reed | adf9990 | 2015-03-19 16:10:54 -0700 | [diff] [blame] | 793 | static 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 | |
| 807 | static 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 821 | DEF_TEST(Matrix, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 822 | SkMatrix mat, inverse, iden1, iden2; |
| 823 | |
| 824 | mat.reset(); |
| 825 | mat.setTranslate(SK_Scalar1, SK_Scalar1); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 826 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 827 | iden1.setConcat(mat, inverse); |
| 828 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 829 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 830 | mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 831 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 832 | iden1.setConcat(mat, inverse); |
| 833 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 834 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 835 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 836 | mat.setScale(SK_Scalar1/2, SkIntToScalar(2)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 837 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 838 | iden1.setConcat(mat, inverse); |
| 839 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 840 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 841 | |
| 842 | mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0); |
| 843 | mat.postRotate(SkIntToScalar(25)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 844 | REPORTER_ASSERT(reporter, mat.invert(nullptr)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 845 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 846 | 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.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 850 | test_flatten(reporter, mat); |
| 851 | test_flatten(reporter, iden2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 852 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 853 | mat.setScale(0, SK_Scalar1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 854 | REPORTER_ASSERT(reporter, !mat.invert(nullptr)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 855 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 856 | mat.setScale(SK_Scalar1, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 857 | REPORTER_ASSERT(reporter, !mat.invert(nullptr)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 858 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 859 | |
robertphillips | 20eee3f | 2015-06-19 05:14:26 -0700 | [diff] [blame] | 860 | // 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 864 | REPORTER_ASSERT(reporter, !mat.invert(nullptr)); |
robertphillips | 20eee3f | 2015-06-19 05:14:26 -0700 | [diff] [blame] | 865 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 866 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 867 | // 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.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 891 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 892 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) { |
| 893 | SkMatrix m; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 894 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 895 | 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.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 904 | |
bungeman@google.com | ba7983e | 2011-07-13 20:18:16 +0000 | [diff] [blame] | 905 | mat.reset(); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 906 | 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.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 912 | 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 | |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 924 | mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 925 | REPORTER_ASSERT(reporter, !mat.asAffine(affine)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 926 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 927 | 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.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 940 | test_matrix_min_max_scale(reporter); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 941 | test_matrix_preserve_shape(reporter); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 942 | test_matrix_recttorect(reporter); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 943 | test_matrix_decomposition(reporter); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 944 | test_matrix_homogeneous(reporter); |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 945 | test_set9(reporter); |
reed | adf9990 | 2015-03-19 16:10:54 -0700 | [diff] [blame] | 946 | |
| 947 | test_decompScale(reporter); |
reed | 2dcb615 | 2016-07-05 20:10:42 -0700 | [diff] [blame] | 948 | |
| 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.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 953 | } |
commit-bot@chromium.org | 99bd7d8 | 2014-05-19 15:51:12 +0000 | [diff] [blame] | 954 | |
| 955 | DEF_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 | } |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 967 | |
| 968 | // Test that all variants of maprect are correct. |
| 969 | DEF_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); |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame^] | 987 | mat.mapRectScaleTranslate(&dst[2], src); |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 988 | |
| 989 | REPORTER_ASSERT(r, dst[0] == dst[1]); |
| 990 | REPORTER_ASSERT(r, dst[0] == dst[2]); |
| 991 | } |
| 992 | } |