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 { |
| 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.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); |
| 96 | |
| 97 | m.setScale(2, 3); |
| 98 | assert9(reporter, m, 2, 0, 0, 0, 3, 0, 0, 0, 1); |
| 99 | |
| 100 | m.postTranslate(4, 5); |
| 101 | assert9(reporter, m, 2, 0, 4, 0, 3, 5, 0, 0, 1); |
| 102 | |
| 103 | SkScalar buffer[9]; |
| 104 | sk_bzero(buffer, sizeof(buffer)); |
| 105 | buffer[SkMatrix::kMScaleX] = 1; |
| 106 | buffer[SkMatrix::kMScaleY] = 1; |
| 107 | buffer[SkMatrix::kMPersp2] = 1; |
| 108 | REPORTER_ASSERT(reporter, !m.isIdentity()); |
| 109 | m.set9(buffer); |
| 110 | REPORTER_ASSERT(reporter, m.isIdentity()); |
| 111 | } |
| 112 | |
reed@google.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]; |
| 145 | size_t size1 = m.writeToMemory(NULL); |
| 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 | // Verify that getMinMaxScales() doesn't update the scales array on failure. |
| 209 | scales[0] = -5; |
| 210 | scales[1] = -5; |
| 211 | success = perspX.getMinMaxScales(scales); |
| 212 | REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1 == scales[1]); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 213 | |
| 214 | SkMatrix perspY; |
| 215 | perspY.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 216 | perspY.setPerspY(-SK_Scalar1 / 500); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 217 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale()); |
| 218 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 219 | scales[0] = -5; |
| 220 | scales[1] = -5; |
| 221 | success = perspY.getMinMaxScales(scales); |
| 222 | REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1 == scales[1]); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 223 | |
| 224 | SkMatrix baseMats[] = {scale, rot90Scale, rotate, |
| 225 | translate, perspX, perspY}; |
| 226 | SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 227 | for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 228 | mats[i] = baseMats[i]; |
| 229 | bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); |
| 230 | REPORTER_ASSERT(reporter, invertable); |
| 231 | } |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 232 | SkRandom rand; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 233 | for (int m = 0; m < 1000; ++m) { |
| 234 | SkMatrix mat; |
| 235 | mat.reset(); |
| 236 | for (int i = 0; i < 4; ++i) { |
| 237 | int x = rand.nextU() % SK_ARRAY_COUNT(mats); |
| 238 | mat.postConcat(mats[x]); |
| 239 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 240 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 241 | SkScalar minScale = mat.getMinScale(); |
| 242 | SkScalar maxScale = mat.getMaxScale(); |
| 243 | REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0)); |
| 244 | REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 245 | |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 246 | SkScalar scales[2]; |
| 247 | bool success = mat.getMinMaxScales(scales); |
| 248 | REPORTER_ASSERT(reporter, success == !mat.hasPerspective()); |
| 249 | REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale)); |
| 250 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 251 | if (mat.hasPerspective()) { |
| 252 | m -= 1; // try another non-persp matrix |
| 253 | continue; |
| 254 | } |
| 255 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 256 | // 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] | 257 | // (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] | 258 | static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100; |
| 259 | static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100; |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 260 | SkScalar max = 0, min = SK_ScalarMax; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 261 | SkVector vectors[1000]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 262 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 263 | vectors[i].fX = rand.nextSScalar1(); |
| 264 | vectors[i].fY = rand.nextSScalar1(); |
| 265 | if (!vectors[i].normalize()) { |
| 266 | i -= 1; |
| 267 | continue; |
| 268 | } |
| 269 | } |
| 270 | mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors)); |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 271 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 272 | SkScalar d = vectors[i].length(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 273 | REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol); |
| 274 | REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 275 | if (max < d) { |
| 276 | max = d; |
| 277 | } |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 278 | if (min > d) { |
| 279 | min = d; |
| 280 | } |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 281 | } |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 282 | REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol); |
| 283 | REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 287 | static void test_matrix_preserve_shape(skiatest::Reporter* reporter) { |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 288 | SkMatrix mat; |
| 289 | |
| 290 | // identity |
| 291 | mat.setIdentity(); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 292 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 293 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 294 | |
| 295 | // translation only |
| 296 | mat.reset(); |
| 297 | mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 298 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 299 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 300 | |
| 301 | // scale with same size |
| 302 | mat.reset(); |
| 303 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 304 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 306 | |
| 307 | // scale with one negative |
| 308 | mat.reset(); |
| 309 | mat.setScale(SkIntToScalar(-15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 310 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 311 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 312 | |
| 313 | // scale with different size |
| 314 | mat.reset(); |
| 315 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 316 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 317 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 318 | |
| 319 | // scale with same size at a pivot point |
| 320 | mat.reset(); |
| 321 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15), |
| 322 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.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 at a pivot point |
| 327 | mat.reset(); |
| 328 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20), |
| 329 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 330 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 331 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 332 | |
| 333 | // skew with same size |
| 334 | mat.reset(); |
| 335 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15)); |
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 | // skew with different size |
| 340 | mat.reset(); |
| 341 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 342 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 343 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 344 | |
| 345 | // skew with same size at a pivot point |
| 346 | mat.reset(); |
| 347 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15), |
| 348 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.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 at a pivot point |
| 353 | mat.reset(); |
| 354 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20), |
| 355 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 356 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 357 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 358 | |
| 359 | // perspective x |
| 360 | mat.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 361 | mat.setPerspX(SK_Scalar1 / 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 | // perspective y |
| 366 | mat.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 367 | mat.setPerspY(SK_Scalar1 / 2); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 368 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 369 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 370 | |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 371 | // rotate |
| 372 | for (int angle = 0; angle < 360; ++angle) { |
| 373 | mat.reset(); |
| 374 | mat.setRotate(SkIntToScalar(angle)); |
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 | |
| 379 | // see if there are any accumulated precision issues |
| 380 | mat.reset(); |
| 381 | for (int i = 1; i < 360; i++) { |
| 382 | mat.postRotate(SkIntToScalar(1)); |
| 383 | } |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 384 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 385 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 386 | |
| 387 | // rotate + translate |
| 388 | mat.reset(); |
| 389 | mat.setRotate(SkIntToScalar(30)); |
| 390 | mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 391 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 392 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 393 | |
| 394 | // rotate + uniform scale |
| 395 | mat.reset(); |
| 396 | mat.setRotate(SkIntToScalar(30)); |
| 397 | mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 398 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 399 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 400 | |
| 401 | // rotate + non-uniform scale |
| 402 | mat.reset(); |
| 403 | mat.setRotate(SkIntToScalar(30)); |
| 404 | mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 405 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 406 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
| 407 | |
| 408 | // non-uniform scale + rotate |
| 409 | mat.reset(); |
| 410 | mat.setScale(SkIntToScalar(3), SkIntToScalar(2)); |
| 411 | mat.postRotate(SkIntToScalar(30)); |
| 412 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
| 413 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 414 | |
| 415 | // all zero |
| 416 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 417 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 418 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 419 | |
| 420 | // all zero except perspective |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 421 | mat.reset(); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 422 | 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] | 423 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 424 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 425 | |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 426 | // scales zero, only skews (rotation) |
| 427 | mat.setAll(0, SK_Scalar1, 0, |
| 428 | -SK_Scalar1, 0, 0, |
| 429 | 0, 0, SkMatrix::I()[8]); |
| 430 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
| 431 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
| 432 | |
| 433 | // scales zero, only skews (reflection) |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 434 | mat.setAll(0, SK_Scalar1, 0, |
| 435 | SK_Scalar1, 0, 0, |
| 436 | 0, 0, SkMatrix::I()[8]); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 437 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 438 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 439 | } |
| 440 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 441 | // For test_matrix_decomposition, below. |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 442 | static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b, |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 443 | SkScalar tolerance = SK_ScalarNearlyZero) { |
| 444 | // from Bruce Dawson |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 445 | // absolute check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 446 | SkScalar diff = SkScalarAbs(a - b); |
| 447 | if (diff < tolerance) { |
| 448 | return true; |
| 449 | } |
| 450 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 451 | // relative check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 452 | a = SkScalarAbs(a); |
| 453 | b = SkScalarAbs(b); |
| 454 | SkScalar largest = (b > a) ? b : a; |
| 455 | |
| 456 | if (diff <= largest*tolerance) { |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | return false; |
| 461 | } |
| 462 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 463 | static bool check_matrix_recomposition(const SkMatrix& mat, |
| 464 | const SkPoint& rotation1, |
| 465 | const SkPoint& scale, |
| 466 | const SkPoint& rotation2) { |
| 467 | SkScalar c1 = rotation1.fX; |
| 468 | SkScalar s1 = rotation1.fY; |
| 469 | SkScalar scaleX = scale.fX; |
| 470 | SkScalar scaleY = scale.fY; |
| 471 | SkScalar c2 = rotation2.fX; |
| 472 | SkScalar s2 = rotation2.fY; |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 473 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 474 | // We do a relative check here because large scale factors cause problems with an absolute check |
| 475 | bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 476 | scaleX*c1*c2 - scaleY*s1*s2) && |
| 477 | scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
| 478 | -scaleX*s1*c2 - scaleY*c1*s2) && |
| 479 | scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
| 480 | scaleX*c1*s2 + scaleY*s1*c2) && |
| 481 | scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
| 482 | -scaleX*s1*s2 + scaleY*c1*c2); |
| 483 | return result; |
| 484 | } |
| 485 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 486 | static void test_matrix_decomposition(skiatest::Reporter* reporter) { |
| 487 | SkMatrix mat; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 488 | SkPoint rotation1, scale, rotation2; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 489 | |
| 490 | const float kRotation0 = 15.5f; |
| 491 | const float kRotation1 = -50.f; |
| 492 | const float kScale0 = 5000.f; |
| 493 | const float kScale1 = 0.001f; |
| 494 | |
| 495 | // identity |
| 496 | mat.reset(); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 497 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 498 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 499 | // make sure it doesn't crash if we pass in NULLs |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 500 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 501 | |
| 502 | // rotation only |
| 503 | mat.setRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 504 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 505 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 506 | |
| 507 | // uniform scale only |
| 508 | mat.setScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 509 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 510 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 511 | |
| 512 | // anisotropic scale only |
| 513 | mat.setScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 514 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 515 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 516 | |
| 517 | // rotation then uniform scale |
| 518 | mat.setRotate(kRotation1); |
| 519 | mat.postScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 520 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 521 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 522 | |
| 523 | // uniform scale then rotation |
| 524 | mat.setScale(kScale0, kScale0); |
| 525 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 526 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 527 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 528 | |
| 529 | // rotation then uniform scale+reflection |
| 530 | mat.setRotate(kRotation0); |
| 531 | mat.postScale(kScale1, -kScale1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 532 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 533 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 534 | |
| 535 | // uniform scale+reflection, then rotate |
| 536 | mat.setScale(kScale0, -kScale0); |
| 537 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 538 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 539 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 540 | |
| 541 | // rotation then anisotropic scale |
| 542 | mat.setRotate(kRotation1); |
| 543 | mat.postScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 544 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 545 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 546 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 547 | // rotation then anisotropic scale |
| 548 | mat.setRotate(90); |
| 549 | mat.postScale(kScale1, kScale0); |
| 550 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 551 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 552 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 553 | // anisotropic scale then rotation |
| 554 | mat.setScale(kScale1, kScale0); |
| 555 | mat.postRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 556 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 557 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 558 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 559 | // anisotropic scale then rotation |
| 560 | mat.setScale(kScale1, kScale0); |
| 561 | mat.postRotate(90); |
| 562 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 563 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 564 | |
| 565 | // rotation, uniform scale, then different rotation |
| 566 | mat.setRotate(kRotation1); |
| 567 | mat.postScale(kScale0, 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)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 571 | |
| 572 | // rotation, anisotropic scale, then different rotation |
| 573 | mat.setRotate(kRotation0); |
| 574 | mat.postScale(kScale1, kScale0); |
| 575 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 576 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 577 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 578 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 579 | // rotation, anisotropic scale + reflection, then different rotation |
| 580 | mat.setRotate(kRotation0); |
| 581 | mat.postScale(-kScale1, kScale0); |
| 582 | mat.postRotate(kRotation1); |
| 583 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 584 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 585 | |
| 586 | // try some random matrices |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 587 | SkRandom rand; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 588 | for (int m = 0; m < 1000; ++m) { |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 589 | SkScalar rot0 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 590 | SkScalar sx = rand.nextRangeF(-3000.f, 3000.f); |
| 591 | SkScalar sy = rand.nextRangeF(-3000.f, 3000.f); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 592 | SkScalar rot1 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 593 | mat.setRotate(rot0); |
| 594 | mat.postScale(sx, sy); |
| 595 | mat.postRotate(rot1); |
| 596 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 597 | if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) { |
| 598 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 599 | } else { |
| 600 | // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero |
| 601 | SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] - |
| 602 | mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY]; |
| 603 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot)); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // translation shouldn't affect this |
| 608 | mat.postTranslate(-1000.f, 1000.f); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 609 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 610 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 611 | |
| 612 | // perspective shouldn't affect this |
jvanverth@google.com | 588f3d3 | 2013-07-24 18:44:10 +0000 | [diff] [blame] | 613 | mat[SkMatrix::kMPersp0] = 12.f; |
| 614 | mat[SkMatrix::kMPersp1] = 4.f; |
| 615 | mat[SkMatrix::kMPersp2] = 1872.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 616 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 617 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 618 | |
| 619 | // degenerate matrices |
| 620 | // mostly zero entries |
| 621 | mat.reset(); |
| 622 | mat[SkMatrix::kMScaleX] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 623 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 624 | mat.reset(); |
| 625 | mat[SkMatrix::kMScaleY] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 626 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 627 | mat.reset(); |
| 628 | // linearly dependent entries |
| 629 | mat[SkMatrix::kMScaleX] = 1.f; |
| 630 | mat[SkMatrix::kMSkewX] = 2.f; |
| 631 | mat[SkMatrix::kMSkewY] = 4.f; |
| 632 | mat[SkMatrix::kMScaleY] = 8.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 633 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 634 | } |
| 635 | |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 636 | // For test_matrix_homogeneous, below. |
| 637 | static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) { |
| 638 | for (int i = 0; i < count; ++i) { |
| 639 | if (!scalar_nearly_equal_relative(a[i], b[i])) { |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | // For test_matrix_homogeneous, below. |
| 647 | // Maps a single triple in src using m and compares results to those in dst |
| 648 | static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3], |
| 649 | const SkScalar dst[3]) { |
| 650 | SkScalar res[3]; |
| 651 | SkScalar ms[9] = {m[0], m[1], m[2], |
| 652 | m[3], m[4], m[5], |
| 653 | m[6], m[7], m[8]}; |
| 654 | res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2]; |
| 655 | res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5]; |
| 656 | res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8]; |
| 657 | return scalar_array_nearly_equal_relative(res, dst, 3); |
| 658 | } |
| 659 | |
| 660 | static void test_matrix_homogeneous(skiatest::Reporter* reporter) { |
| 661 | SkMatrix mat; |
| 662 | |
| 663 | const float kRotation0 = 15.5f; |
| 664 | const float kRotation1 = -50.f; |
| 665 | const float kScale0 = 5000.f; |
| 666 | |
| 667 | const int kTripleCount = 1000; |
| 668 | const int kMatrixCount = 1000; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 669 | SkRandom rand; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 670 | |
| 671 | SkScalar randTriples[3*kTripleCount]; |
| 672 | for (int i = 0; i < 3*kTripleCount; ++i) { |
| 673 | randTriples[i] = rand.nextRangeF(-3000.f, 3000.f); |
| 674 | } |
| 675 | |
| 676 | SkMatrix mats[kMatrixCount]; |
| 677 | for (int i = 0; i < kMatrixCount; ++i) { |
| 678 | for (int j = 0; j < 9; ++j) { |
| 679 | mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f)); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | // identity |
| 684 | { |
| 685 | mat.reset(); |
| 686 | SkScalar dst[3*kTripleCount]; |
| 687 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 688 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3)); |
| 689 | } |
| 690 | |
| 691 | // zero matrix |
| 692 | { |
| 693 | mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f); |
| 694 | SkScalar dst[3*kTripleCount]; |
| 695 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 696 | SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 697 | for (int i = 0; i < kTripleCount; ++i) { |
| 698 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3)); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // zero point |
| 703 | { |
| 704 | SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 705 | for (int i = 0; i < kMatrixCount; ++i) { |
| 706 | SkScalar dst[3]; |
| 707 | mats[i].mapHomogeneousPoints(dst, zeros, 1); |
| 708 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3)); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // doesn't crash with null dst, src, count == 0 |
| 713 | { |
| 714 | mats[0].mapHomogeneousPoints(NULL, NULL, 0); |
| 715 | } |
| 716 | |
| 717 | // uniform scale of point |
| 718 | { |
| 719 | mat.setScale(kScale0, kScale0); |
| 720 | SkScalar dst[3]; |
| 721 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 722 | SkPoint pnt; |
| 723 | pnt.set(src[0], src[1]); |
| 724 | mat.mapHomogeneousPoints(dst, src, 1); |
| 725 | mat.mapPoints(&pnt, &pnt, 1); |
| 726 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 727 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 728 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 729 | } |
| 730 | |
| 731 | // rotation of point |
| 732 | { |
| 733 | mat.setRotate(kRotation0); |
| 734 | SkScalar dst[3]; |
| 735 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 736 | SkPoint pnt; |
| 737 | pnt.set(src[0], src[1]); |
| 738 | mat.mapHomogeneousPoints(dst, src, 1); |
| 739 | mat.mapPoints(&pnt, &pnt, 1); |
| 740 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 741 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 742 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 743 | } |
| 744 | |
| 745 | // rotation, scale, rotation of point |
| 746 | { |
| 747 | mat.setRotate(kRotation1); |
| 748 | mat.postScale(kScale0, kScale0); |
| 749 | mat.postRotate(kRotation0); |
| 750 | SkScalar dst[3]; |
| 751 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 752 | SkPoint pnt; |
| 753 | pnt.set(src[0], src[1]); |
| 754 | mat.mapHomogeneousPoints(dst, src, 1); |
| 755 | mat.mapPoints(&pnt, &pnt, 1); |
| 756 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 757 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 758 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 759 | } |
| 760 | |
| 761 | // compare with naive approach |
| 762 | { |
| 763 | for (int i = 0; i < kMatrixCount; ++i) { |
| 764 | for (int j = 0; j < kTripleCount; ++j) { |
| 765 | SkScalar dst[3]; |
| 766 | mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1); |
| 767 | REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst)); |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | } |
| 773 | |
reed | adf9990 | 2015-03-19 16:10:54 -0700 | [diff] [blame] | 774 | static bool check_decompScale(const SkMatrix& matrix) { |
| 775 | SkSize scale; |
| 776 | SkMatrix remaining; |
| 777 | |
| 778 | if (!matrix.decomposeScale(&scale, &remaining)) { |
| 779 | return false; |
| 780 | } |
| 781 | if (scale.width() <= 0 || scale.height() <= 0) { |
| 782 | return false; |
| 783 | } |
| 784 | remaining.preScale(scale.width(), scale.height()); |
| 785 | return nearly_equal(matrix, remaining); |
| 786 | } |
| 787 | |
| 788 | static void test_decompScale(skiatest::Reporter* reporter) { |
| 789 | SkMatrix m; |
| 790 | |
| 791 | m.reset(); |
| 792 | REPORTER_ASSERT(reporter, check_decompScale(m)); |
| 793 | m.setScale(2, 3); |
| 794 | REPORTER_ASSERT(reporter, check_decompScale(m)); |
| 795 | m.setRotate(35, 0, 0); |
| 796 | REPORTER_ASSERT(reporter, check_decompScale(m)); |
| 797 | |
| 798 | m.setScale(1, 0); |
| 799 | REPORTER_ASSERT(reporter, !check_decompScale(m)); |
| 800 | } |
| 801 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 802 | DEF_TEST(Matrix, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 803 | SkMatrix mat, inverse, iden1, iden2; |
| 804 | |
| 805 | mat.reset(); |
| 806 | mat.setTranslate(SK_Scalar1, SK_Scalar1); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 807 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 808 | iden1.setConcat(mat, inverse); |
| 809 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 810 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 811 | mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 812 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 813 | iden1.setConcat(mat, inverse); |
| 814 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 815 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 816 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 817 | mat.setScale(SK_Scalar1/2, SkIntToScalar(2)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 818 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 819 | iden1.setConcat(mat, inverse); |
| 820 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 821 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 822 | |
| 823 | mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0); |
| 824 | mat.postRotate(SkIntToScalar(25)); |
| 825 | REPORTER_ASSERT(reporter, mat.invert(NULL)); |
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 | iden2.setConcat(inverse, mat); |
| 830 | REPORTER_ASSERT(reporter, is_identity(iden2)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 831 | test_flatten(reporter, mat); |
| 832 | test_flatten(reporter, iden2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 833 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 834 | mat.setScale(0, SK_Scalar1); |
reed@google.com | e40591d | 2013-01-30 15:47:42 +0000 | [diff] [blame] | 835 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 836 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 837 | mat.setScale(SK_Scalar1, 0); |
reed@google.com | e40591d | 2013-01-30 15:47:42 +0000 | [diff] [blame] | 838 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 839 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 840 | |
robertphillips | 20eee3f | 2015-06-19 05:14:26 -0700 | [diff] [blame^] | 841 | // Inverting this matrix results in a non-finite matrix |
| 842 | mat.setAll(0.0f, 1.0f, 2.0f, |
| 843 | 0.0f, 1.0f, -3.40277175e+38f, |
| 844 | 1.00003040f, 1.0f, 0.0f); |
| 845 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
| 846 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 847 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 848 | // rectStaysRect test |
| 849 | { |
| 850 | static const struct { |
| 851 | SkScalar m00, m01, m10, m11; |
| 852 | bool mStaysRect; |
| 853 | } |
| 854 | gRectStaysRectSamples[] = { |
| 855 | { 0, 0, 0, 0, false }, |
| 856 | { 0, 0, 0, SK_Scalar1, false }, |
| 857 | { 0, 0, SK_Scalar1, 0, false }, |
| 858 | { 0, 0, SK_Scalar1, SK_Scalar1, false }, |
| 859 | { 0, SK_Scalar1, 0, 0, false }, |
| 860 | { 0, SK_Scalar1, 0, SK_Scalar1, false }, |
| 861 | { 0, SK_Scalar1, SK_Scalar1, 0, true }, |
| 862 | { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }, |
| 863 | { SK_Scalar1, 0, 0, 0, false }, |
| 864 | { SK_Scalar1, 0, 0, SK_Scalar1, true }, |
| 865 | { SK_Scalar1, 0, SK_Scalar1, 0, false }, |
| 866 | { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false }, |
| 867 | { SK_Scalar1, SK_Scalar1, 0, 0, false }, |
| 868 | { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false }, |
| 869 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false }, |
| 870 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false } |
| 871 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 872 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 873 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) { |
| 874 | SkMatrix m; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 875 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 876 | m.reset(); |
| 877 | m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00); |
| 878 | m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01); |
| 879 | m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10); |
| 880 | m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11); |
| 881 | REPORTER_ASSERT(reporter, |
| 882 | m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect); |
| 883 | } |
| 884 | } |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 885 | |
bungeman@google.com | ba7983e | 2011-07-13 20:18:16 +0000 | [diff] [blame] | 886 | mat.reset(); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 887 | mat.set(SkMatrix::kMScaleX, SkIntToScalar(1)); |
| 888 | mat.set(SkMatrix::kMSkewX, SkIntToScalar(2)); |
| 889 | mat.set(SkMatrix::kMTransX, SkIntToScalar(3)); |
| 890 | mat.set(SkMatrix::kMSkewY, SkIntToScalar(4)); |
| 891 | mat.set(SkMatrix::kMScaleY, SkIntToScalar(5)); |
| 892 | mat.set(SkMatrix::kMTransY, SkIntToScalar(6)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 893 | SkScalar affine[6]; |
| 894 | REPORTER_ASSERT(reporter, mat.asAffine(affine)); |
| 895 | |
| 896 | #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e) |
| 897 | REPORTER_ASSERT(reporter, affineEqual(ScaleX)); |
| 898 | REPORTER_ASSERT(reporter, affineEqual(SkewY)); |
| 899 | REPORTER_ASSERT(reporter, affineEqual(SkewX)); |
| 900 | REPORTER_ASSERT(reporter, affineEqual(ScaleY)); |
| 901 | REPORTER_ASSERT(reporter, affineEqual(TransX)); |
| 902 | REPORTER_ASSERT(reporter, affineEqual(TransY)); |
| 903 | #undef affineEqual |
| 904 | |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 905 | mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 906 | REPORTER_ASSERT(reporter, !mat.asAffine(affine)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 907 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 908 | SkMatrix mat2; |
| 909 | mat2.reset(); |
| 910 | mat.reset(); |
| 911 | SkScalar zero = 0; |
| 912 | mat.set(SkMatrix::kMSkewX, -zero); |
| 913 | REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 914 | |
| 915 | mat2.reset(); |
| 916 | mat.reset(); |
| 917 | mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 918 | mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 919 | REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
| 920 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 921 | test_matrix_min_max_scale(reporter); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 922 | test_matrix_preserve_shape(reporter); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 923 | test_matrix_recttorect(reporter); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 924 | test_matrix_decomposition(reporter); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 925 | test_matrix_homogeneous(reporter); |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 926 | test_set9(reporter); |
reed | adf9990 | 2015-03-19 16:10:54 -0700 | [diff] [blame] | 927 | |
| 928 | test_decompScale(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 929 | } |
commit-bot@chromium.org | 99bd7d8 | 2014-05-19 15:51:12 +0000 | [diff] [blame] | 930 | |
| 931 | DEF_TEST(Matrix_Concat, r) { |
| 932 | SkMatrix a; |
| 933 | a.setTranslate(10, 20); |
| 934 | |
| 935 | SkMatrix b; |
| 936 | b.setScale(3, 5); |
| 937 | |
| 938 | SkMatrix expected; |
| 939 | expected.setConcat(a,b); |
| 940 | |
| 941 | REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b)); |
| 942 | } |