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" |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 9 | #include "SkMatrixPriv.h" |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 10 | #include "SkMatrixUtils.h" |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 11 | #include "SkPoint3.h" |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 14 | |
| 15 | static bool nearly_equal_scalar(SkScalar a, SkScalar b) { |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 16 | const SkScalar tolerance = SK_Scalar1 / 200000; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 17 | return SkScalarAbs(a - b) <= tolerance; |
| 18 | } |
| 19 | |
| 20 | static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) { |
| 21 | for (int i = 0; i < 9; i++) { |
| 22 | if (!nearly_equal_scalar(a[i], b[i])) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 23 | 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] | 24 | return false; |
| 25 | } |
| 26 | } |
| 27 | return true; |
| 28 | } |
| 29 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 30 | static bool are_equal(skiatest::Reporter* reporter, |
| 31 | const SkMatrix& a, |
| 32 | const SkMatrix& b) { |
| 33 | bool equal = a == b; |
| 34 | bool cheapEqual = a.cheapEqualTo(b); |
| 35 | if (equal != cheapEqual) { |
bsalomon@google.com | 39d4f3a | 2012-03-26 17:25:45 +0000 | [diff] [blame] | 36 | if (equal) { |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 37 | bool foundZeroSignDiff = false; |
| 38 | for (int i = 0; i < 9; ++i) { |
| 39 | float aVal = a.get(i); |
| 40 | float bVal = b.get(i); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 41 | int aValI = *SkTCast<int*>(&aVal); |
| 42 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 43 | if (0 == aVal && 0 == bVal && aValI != bValI) { |
| 44 | foundZeroSignDiff = true; |
| 45 | } else { |
halcanary | e76ca8b | 2016-05-20 10:36:50 -0700 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | REPORTER_ASSERT(reporter, foundZeroSignDiff); |
| 50 | } else { |
| 51 | bool foundNaN = false; |
| 52 | for (int i = 0; i < 9; ++i) { |
| 53 | float aVal = a.get(i); |
| 54 | float bVal = b.get(i); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 55 | int aValI = *SkTCast<int*>(&aVal); |
| 56 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 57 | if (sk_float_isnan(aVal) && aValI == bValI) { |
| 58 | foundNaN = true; |
| 59 | } else { |
| 60 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI); |
| 61 | } |
| 62 | } |
| 63 | REPORTER_ASSERT(reporter, foundNaN); |
| 64 | } |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 65 | } |
| 66 | return equal; |
| 67 | } |
| 68 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 69 | static bool is_identity(const SkMatrix& m) { |
| 70 | SkMatrix identity; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 71 | identity.reset(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 72 | return nearly_equal(m, identity); |
| 73 | } |
| 74 | |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 75 | static void assert9(skiatest::Reporter* reporter, const SkMatrix& m, |
| 76 | SkScalar a, SkScalar b, SkScalar c, |
| 77 | SkScalar d, SkScalar e, SkScalar f, |
| 78 | SkScalar g, SkScalar h, SkScalar i) { |
| 79 | SkScalar buffer[9]; |
| 80 | m.get9(buffer); |
| 81 | REPORTER_ASSERT(reporter, buffer[0] == a); |
| 82 | REPORTER_ASSERT(reporter, buffer[1] == b); |
| 83 | REPORTER_ASSERT(reporter, buffer[2] == c); |
| 84 | REPORTER_ASSERT(reporter, buffer[3] == d); |
| 85 | REPORTER_ASSERT(reporter, buffer[4] == e); |
| 86 | REPORTER_ASSERT(reporter, buffer[5] == f); |
| 87 | REPORTER_ASSERT(reporter, buffer[6] == g); |
| 88 | REPORTER_ASSERT(reporter, buffer[7] == h); |
| 89 | REPORTER_ASSERT(reporter, buffer[8] == i); |
| 90 | } |
| 91 | |
| 92 | static void test_set9(skiatest::Reporter* reporter) { |
| 93 | |
| 94 | SkMatrix m; |
| 95 | m.reset(); |
| 96 | assert9(reporter, m, 1, 0, 0, 0, 1, 0, 0, 0, 1); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 97 | |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 98 | m.setScale(2, 3); |
| 99 | assert9(reporter, m, 2, 0, 0, 0, 3, 0, 0, 0, 1); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 100 | |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 101 | m.postTranslate(4, 5); |
| 102 | assert9(reporter, m, 2, 0, 4, 0, 3, 5, 0, 0, 1); |
| 103 | |
| 104 | SkScalar buffer[9]; |
| 105 | sk_bzero(buffer, sizeof(buffer)); |
| 106 | buffer[SkMatrix::kMScaleX] = 1; |
| 107 | buffer[SkMatrix::kMScaleY] = 1; |
| 108 | buffer[SkMatrix::kMPersp2] = 1; |
| 109 | REPORTER_ASSERT(reporter, !m.isIdentity()); |
| 110 | m.set9(buffer); |
| 111 | REPORTER_ASSERT(reporter, m.isIdentity()); |
| 112 | } |
| 113 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 114 | static void test_matrix_recttorect(skiatest::Reporter* reporter) { |
| 115 | SkRect src, dst; |
| 116 | SkMatrix matrix; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 117 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 118 | src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10); |
| 119 | dst = src; |
| 120 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 121 | REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType()); |
| 122 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 123 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 124 | dst.offset(SK_Scalar1, SK_Scalar1); |
| 125 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 126 | REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType()); |
| 127 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 128 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 129 | dst.fRight += SK_Scalar1; |
| 130 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 131 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 132 | (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType()); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 134 | |
| 135 | dst = src; |
| 136 | dst.fRight = src.fRight * 2; |
| 137 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 138 | REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType()); |
| 139 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 140 | } |
| 141 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 142 | static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) { |
| 143 | // add 100 in case we have a bug, I don't want to kill my stack in the test |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 144 | static const size_t kBufferSize = SkMatrixPriv::kMaxFlattenSize + 100; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 145 | char buffer[kBufferSize]; |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 146 | size_t size1 = SkMatrixPriv::WriteToMemory(m, nullptr); |
| 147 | size_t size2 = SkMatrixPriv::WriteToMemory(m, buffer); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 148 | REPORTER_ASSERT(reporter, size1 == size2); |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 149 | REPORTER_ASSERT(reporter, size1 <= SkMatrixPriv::kMaxFlattenSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 150 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 151 | SkMatrix m2; |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 152 | size_t size3 = SkMatrixPriv::ReadFromMemory(&m2, buffer, kBufferSize); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, size1 == size3); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 155 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 156 | char buffer2[kBufferSize]; |
Cary Clark | 6d6d603 | 2017-10-20 12:14:33 -0400 | [diff] [blame] | 157 | size3 = SkMatrixPriv::WriteToMemory(m2, buffer2); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, size1 == size3); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 159 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
| 160 | } |
| 161 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 162 | static void test_matrix_min_max_scale(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 163 | SkScalar scales[2]; |
| 164 | bool success; |
| 165 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 166 | SkMatrix identity; |
| 167 | identity.reset(); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinScale()); |
| 169 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 170 | success = identity.getMinMaxScales(scales); |
| 171 | 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] | 172 | |
| 173 | SkMatrix scale; |
| 174 | scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinScale()); |
| 176 | REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 177 | success = scale.getMinMaxScales(scales); |
| 178 | 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] | 179 | |
| 180 | SkMatrix rot90Scale; |
| 181 | rot90Scale.setRotate(90 * SK_Scalar1); |
| 182 | rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 183 | REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale()); |
| 184 | REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 185 | success = rot90Scale.getMinMaxScales(scales); |
| 186 | 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] | 187 | |
| 188 | SkMatrix rotate; |
| 189 | rotate.setRotate(128 * SK_Scalar1); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinScale(), SK_ScalarNearlyZero)); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 191 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxScale(), SK_ScalarNearlyZero)); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 192 | success = rotate.getMinMaxScales(scales); |
| 193 | REPORTER_ASSERT(reporter, success); |
| 194 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[0], SK_ScalarNearlyZero)); |
| 195 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[1], SK_ScalarNearlyZero)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 196 | |
| 197 | SkMatrix translate; |
| 198 | translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 199 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinScale()); |
| 200 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 201 | success = translate.getMinMaxScales(scales); |
| 202 | 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] | 203 | |
| 204 | SkMatrix perspX; |
| 205 | perspX.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 206 | perspX.setPerspX(SK_Scalar1 / 1000); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 207 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinScale()); |
| 208 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 209 | success = perspX.getMinMaxScales(scales); |
bsalomon | ef2c7c7 | 2015-12-17 15:33:13 -0800 | [diff] [blame] | 210 | REPORTER_ASSERT(reporter, !success); |
| 211 | |
| 212 | // skbug.com/4718 |
| 213 | SkMatrix big; |
| 214 | big.setAll(2.39394089e+36f, 8.85347779e+36f, 9.26526204e+36f, |
| 215 | 3.9159619e+36f, 1.44823453e+37f, 1.51559342e+37f, |
| 216 | 0.f, 0.f, 1.f); |
bsalomon | ef2c7c7 | 2015-12-17 15:33:13 -0800 | [diff] [blame] | 217 | success = big.getMinMaxScales(scales); |
| 218 | REPORTER_ASSERT(reporter, !success); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 219 | |
kolczyk | cea22ae | 2016-07-16 11:52:37 -0700 | [diff] [blame] | 220 | // skbug.com/4718 |
| 221 | SkMatrix givingNegativeNearlyZeros; |
| 222 | givingNegativeNearlyZeros.setAll(0.00436534f, 0.114138f, 0.37141f, |
| 223 | 0.00358857f, 0.0936228f, -0.0174198f, |
| 224 | 0.f, 0.f, 1.f); |
| 225 | success = givingNegativeNearlyZeros.getMinMaxScales(scales); |
| 226 | REPORTER_ASSERT(reporter, success && 0 == scales[0]); |
| 227 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 228 | SkMatrix perspY; |
| 229 | perspY.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 230 | perspY.setPerspY(-SK_Scalar1 / 500); |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 231 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale()); |
| 232 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale()); |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 233 | scales[0] = -5; |
| 234 | scales[1] = -5; |
| 235 | success = perspY.getMinMaxScales(scales); |
| 236 | 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] | 237 | |
| 238 | SkMatrix baseMats[] = {scale, rot90Scale, rotate, |
| 239 | translate, perspX, perspY}; |
| 240 | SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 241 | for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 242 | mats[i] = baseMats[i]; |
bungeman | e55131c | 2016-08-24 12:01:31 -0700 | [diff] [blame] | 243 | bool invertible = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); |
| 244 | REPORTER_ASSERT(reporter, invertible); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 245 | } |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 246 | SkRandom rand; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 247 | for (int m = 0; m < 1000; ++m) { |
| 248 | SkMatrix mat; |
| 249 | mat.reset(); |
| 250 | for (int i = 0; i < 4; ++i) { |
| 251 | int x = rand.nextU() % SK_ARRAY_COUNT(mats); |
| 252 | mat.postConcat(mats[x]); |
| 253 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 254 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 255 | SkScalar minScale = mat.getMinScale(); |
| 256 | SkScalar maxScale = mat.getMaxScale(); |
| 257 | REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0)); |
| 258 | REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 259 | |
commit-bot@chromium.org | 311a3cd | 2014-05-20 17:02:03 +0000 | [diff] [blame] | 260 | SkScalar scales[2]; |
| 261 | bool success = mat.getMinMaxScales(scales); |
| 262 | REPORTER_ASSERT(reporter, success == !mat.hasPerspective()); |
| 263 | REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale)); |
| 264 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 265 | if (mat.hasPerspective()) { |
| 266 | m -= 1; // try another non-persp matrix |
| 267 | continue; |
| 268 | } |
| 269 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 270 | // 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] | 271 | // (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] | 272 | static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100; |
| 273 | static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100; |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 274 | SkScalar max = 0, min = SK_ScalarMax; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 275 | SkVector vectors[1000]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 276 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 277 | vectors[i].fX = rand.nextSScalar1(); |
| 278 | vectors[i].fY = rand.nextSScalar1(); |
| 279 | if (!vectors[i].normalize()) { |
| 280 | i -= 1; |
| 281 | continue; |
| 282 | } |
| 283 | } |
| 284 | mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors)); |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 285 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 286 | SkScalar d = vectors[i].length(); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 287 | REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol); |
| 288 | REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 289 | if (max < d) { |
| 290 | max = d; |
| 291 | } |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 292 | if (min > d) { |
| 293 | min = d; |
| 294 | } |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 295 | } |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 296 | REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol); |
| 297 | REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 301 | static void test_matrix_preserve_shape(skiatest::Reporter* reporter) { |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 302 | SkMatrix mat; |
| 303 | |
| 304 | // identity |
| 305 | mat.setIdentity(); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 306 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 307 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 308 | |
| 309 | // translation only |
| 310 | mat.reset(); |
| 311 | mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 312 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 314 | |
| 315 | // scale with same size |
| 316 | mat.reset(); |
| 317 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 318 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 319 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 320 | |
| 321 | // scale with one negative |
| 322 | mat.reset(); |
| 323 | mat.setScale(SkIntToScalar(-15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 324 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 325 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 326 | |
| 327 | // scale with different size |
| 328 | mat.reset(); |
| 329 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20)); |
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 | // scale with same size at a pivot point |
| 334 | mat.reset(); |
| 335 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15), |
| 336 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 337 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 338 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 339 | |
| 340 | // scale with different size at a pivot point |
| 341 | mat.reset(); |
| 342 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20), |
| 343 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 344 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 345 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 346 | |
| 347 | // skew with same size |
| 348 | mat.reset(); |
| 349 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 350 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 351 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 352 | |
| 353 | // skew with different size |
| 354 | mat.reset(); |
| 355 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20)); |
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 | // skew with same size at a pivot point |
| 360 | mat.reset(); |
| 361 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15), |
| 362 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 363 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 364 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 365 | |
| 366 | // skew with different size at a pivot point |
| 367 | mat.reset(); |
| 368 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20), |
| 369 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 370 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 371 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 372 | |
| 373 | // perspective x |
| 374 | mat.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 375 | mat.setPerspX(SK_Scalar1 / 2); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 376 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 377 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 378 | |
| 379 | // perspective y |
| 380 | mat.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 381 | mat.setPerspY(SK_Scalar1 / 2); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 382 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 383 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 384 | |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 385 | // rotate |
| 386 | for (int angle = 0; angle < 360; ++angle) { |
| 387 | mat.reset(); |
| 388 | mat.setRotate(SkIntToScalar(angle)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 389 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 390 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // see if there are any accumulated precision issues |
| 394 | mat.reset(); |
| 395 | for (int i = 1; i < 360; i++) { |
| 396 | mat.postRotate(SkIntToScalar(1)); |
| 397 | } |
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 + translate |
| 402 | mat.reset(); |
| 403 | mat.setRotate(SkIntToScalar(30)); |
| 404 | mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20)); |
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()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 407 | |
| 408 | // rotate + uniform scale |
| 409 | mat.reset(); |
| 410 | mat.setRotate(SkIntToScalar(30)); |
| 411 | mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 412 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 413 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 414 | |
| 415 | // rotate + non-uniform scale |
| 416 | mat.reset(); |
| 417 | mat.setRotate(SkIntToScalar(30)); |
| 418 | mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 419 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 420 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
| 421 | |
| 422 | // non-uniform scale + rotate |
| 423 | mat.reset(); |
| 424 | mat.setScale(SkIntToScalar(3), SkIntToScalar(2)); |
| 425 | mat.postRotate(SkIntToScalar(30)); |
| 426 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
| 427 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 428 | |
| 429 | // all zero |
| 430 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 431 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 432 | REPORTER_ASSERT(reporter, !mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 433 | |
| 434 | // all zero except perspective |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 435 | mat.reset(); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 436 | 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] | 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 | |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 440 | // scales zero, only skews (rotation) |
| 441 | mat.setAll(0, SK_Scalar1, 0, |
| 442 | -SK_Scalar1, 0, 0, |
| 443 | 0, 0, SkMatrix::I()[8]); |
| 444 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
| 445 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
| 446 | |
| 447 | // scales zero, only skews (reflection) |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 448 | mat.setAll(0, SK_Scalar1, 0, |
| 449 | SK_Scalar1, 0, 0, |
| 450 | 0, 0, SkMatrix::I()[8]); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 451 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 452 | REPORTER_ASSERT(reporter, mat.preservesRightAngles()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 453 | } |
| 454 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 455 | // For test_matrix_decomposition, below. |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 456 | static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b, |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 457 | SkScalar tolerance = SK_ScalarNearlyZero) { |
| 458 | // from Bruce Dawson |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 459 | // absolute check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 460 | SkScalar diff = SkScalarAbs(a - b); |
| 461 | if (diff < tolerance) { |
| 462 | return true; |
| 463 | } |
| 464 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 465 | // relative check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 466 | a = SkScalarAbs(a); |
| 467 | b = SkScalarAbs(b); |
| 468 | SkScalar largest = (b > a) ? b : a; |
| 469 | |
| 470 | if (diff <= largest*tolerance) { |
| 471 | return true; |
| 472 | } |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 477 | static bool check_matrix_recomposition(const SkMatrix& mat, |
| 478 | const SkPoint& rotation1, |
| 479 | const SkPoint& scale, |
| 480 | const SkPoint& rotation2) { |
| 481 | SkScalar c1 = rotation1.fX; |
| 482 | SkScalar s1 = rotation1.fY; |
| 483 | SkScalar scaleX = scale.fX; |
| 484 | SkScalar scaleY = scale.fY; |
| 485 | SkScalar c2 = rotation2.fX; |
| 486 | SkScalar s2 = rotation2.fY; |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 487 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 488 | // We do a relative check here because large scale factors cause problems with an absolute check |
| 489 | bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 490 | scaleX*c1*c2 - scaleY*s1*s2) && |
| 491 | scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
| 492 | -scaleX*s1*c2 - scaleY*c1*s2) && |
| 493 | scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
| 494 | scaleX*c1*s2 + scaleY*s1*c2) && |
| 495 | scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
| 496 | -scaleX*s1*s2 + scaleY*c1*c2); |
| 497 | return result; |
| 498 | } |
| 499 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 500 | static void test_matrix_decomposition(skiatest::Reporter* reporter) { |
| 501 | SkMatrix mat; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 502 | SkPoint rotation1, scale, rotation2; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 503 | |
| 504 | const float kRotation0 = 15.5f; |
| 505 | const float kRotation1 = -50.f; |
| 506 | const float kScale0 = 5000.f; |
| 507 | const float kScale1 = 0.001f; |
| 508 | |
| 509 | // identity |
| 510 | mat.reset(); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 511 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 512 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 513 | // make sure it doesn't crash if we pass in NULLs |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 514 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, nullptr, nullptr, nullptr)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 515 | |
| 516 | // rotation only |
| 517 | mat.setRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 518 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 519 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 520 | |
| 521 | // uniform scale only |
| 522 | mat.setScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 523 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 524 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 525 | |
| 526 | // anisotropic scale only |
| 527 | mat.setScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 528 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 529 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 530 | |
| 531 | // rotation then uniform scale |
| 532 | mat.setRotate(kRotation1); |
| 533 | mat.postScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 534 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 535 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 536 | |
| 537 | // uniform scale then rotation |
| 538 | mat.setScale(kScale0, kScale0); |
| 539 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 540 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 541 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 542 | |
| 543 | // rotation then uniform scale+reflection |
| 544 | mat.setRotate(kRotation0); |
| 545 | mat.postScale(kScale1, -kScale1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 546 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 547 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 548 | |
| 549 | // uniform scale+reflection, then rotate |
| 550 | mat.setScale(kScale0, -kScale0); |
| 551 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 552 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 553 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 554 | |
| 555 | // rotation then anisotropic scale |
| 556 | mat.setRotate(kRotation1); |
| 557 | mat.postScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 558 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 559 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 560 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 561 | // rotation then anisotropic scale |
| 562 | mat.setRotate(90); |
| 563 | mat.postScale(kScale1, kScale0); |
| 564 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 565 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 566 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 567 | // anisotropic scale then rotation |
| 568 | mat.setScale(kScale1, kScale0); |
| 569 | mat.postRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 570 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 571 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 572 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 573 | // anisotropic scale then rotation |
| 574 | mat.setScale(kScale1, kScale0); |
| 575 | mat.postRotate(90); |
| 576 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 577 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 578 | |
| 579 | // rotation, uniform scale, then different rotation |
| 580 | mat.setRotate(kRotation1); |
| 581 | mat.postScale(kScale0, kScale0); |
| 582 | mat.postRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 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 | // rotation, anisotropic scale, then different rotation |
| 587 | mat.setRotate(kRotation0); |
| 588 | mat.postScale(kScale1, kScale0); |
| 589 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 590 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 591 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 592 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 593 | // rotation, anisotropic scale + reflection, then different rotation |
| 594 | mat.setRotate(kRotation0); |
| 595 | mat.postScale(-kScale1, kScale0); |
| 596 | mat.postRotate(kRotation1); |
| 597 | REPORTER_ASSERT(reporter, 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 | |
| 600 | // try some random matrices |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 601 | SkRandom rand; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 602 | for (int m = 0; m < 1000; ++m) { |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 603 | SkScalar rot0 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 604 | SkScalar sx = rand.nextRangeF(-3000.f, 3000.f); |
| 605 | SkScalar sy = rand.nextRangeF(-3000.f, 3000.f); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 606 | SkScalar rot1 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 607 | mat.setRotate(rot0); |
| 608 | mat.postScale(sx, sy); |
| 609 | mat.postRotate(rot1); |
| 610 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 611 | if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) { |
| 612 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 613 | } else { |
| 614 | // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero |
| 615 | SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] - |
| 616 | mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY]; |
| 617 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot)); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // translation shouldn't affect this |
| 622 | mat.postTranslate(-1000.f, 1000.f); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 623 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 624 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 625 | |
| 626 | // perspective shouldn't affect this |
jvanverth@google.com | 588f3d3 | 2013-07-24 18:44:10 +0000 | [diff] [blame] | 627 | mat[SkMatrix::kMPersp0] = 12.f; |
| 628 | mat[SkMatrix::kMPersp1] = 4.f; |
| 629 | mat[SkMatrix::kMPersp2] = 1872.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 630 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 631 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 632 | |
| 633 | // degenerate matrices |
| 634 | // mostly zero entries |
| 635 | mat.reset(); |
| 636 | mat[SkMatrix::kMScaleX] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 637 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 638 | mat.reset(); |
| 639 | mat[SkMatrix::kMScaleY] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 640 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 641 | mat.reset(); |
| 642 | // linearly dependent entries |
| 643 | mat[SkMatrix::kMScaleX] = 1.f; |
| 644 | mat[SkMatrix::kMSkewX] = 2.f; |
| 645 | mat[SkMatrix::kMSkewY] = 4.f; |
| 646 | mat[SkMatrix::kMScaleY] = 8.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 647 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 648 | } |
| 649 | |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 650 | // For test_matrix_homogeneous, below. |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 651 | static bool point3_array_nearly_equal_relative(const SkPoint3 a[], const SkPoint3 b[], int count) { |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 652 | for (int i = 0; i < count; ++i) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 653 | if (!scalar_nearly_equal_relative(a[i].fX, b[i].fX)) { |
| 654 | return false; |
| 655 | } |
| 656 | if (!scalar_nearly_equal_relative(a[i].fY, b[i].fY)) { |
| 657 | return false; |
| 658 | } |
| 659 | if (!scalar_nearly_equal_relative(a[i].fZ, b[i].fZ)) { |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 660 | return false; |
| 661 | } |
| 662 | } |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | // For test_matrix_homogeneous, below. |
| 667 | // Maps a single triple in src using m and compares results to those in dst |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 668 | static bool naive_homogeneous_mapping(const SkMatrix& m, const SkPoint3& src, |
| 669 | const SkPoint3& dst) { |
| 670 | SkPoint3 res; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 671 | SkScalar ms[9] = {m[0], m[1], m[2], |
| 672 | m[3], m[4], m[5], |
| 673 | m[6], m[7], m[8]}; |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 674 | res.fX = src.fX * ms[0] + src.fY * ms[1] + src.fZ * ms[2]; |
| 675 | res.fY = src.fX * ms[3] + src.fY * ms[4] + src.fZ * ms[5]; |
| 676 | res.fZ = src.fX * ms[6] + src.fY * ms[7] + src.fZ * ms[8]; |
| 677 | return point3_array_nearly_equal_relative(&res, &dst, 1); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static void test_matrix_homogeneous(skiatest::Reporter* reporter) { |
| 681 | SkMatrix mat; |
| 682 | |
| 683 | const float kRotation0 = 15.5f; |
| 684 | const float kRotation1 = -50.f; |
| 685 | const float kScale0 = 5000.f; |
| 686 | |
Mike Klein | 6613cc5 | 2017-12-19 09:09:33 -0500 | [diff] [blame] | 687 | #if defined(SK_BUILD_FOR_GOOGLE3) |
| 688 | // Stack frame size is limited in SK_BUILD_FOR_GOOGLE3. |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 689 | const int kTripleCount = 100; |
| 690 | const int kMatrixCount = 100; |
| 691 | #else |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 692 | const int kTripleCount = 1000; |
| 693 | const int kMatrixCount = 1000; |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 694 | #endif |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 695 | SkRandom rand; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 696 | |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 697 | SkPoint3 randTriples[kTripleCount]; |
| 698 | for (int i = 0; i < kTripleCount; ++i) { |
| 699 | randTriples[i].fX = rand.nextRangeF(-3000.f, 3000.f); |
| 700 | randTriples[i].fY = rand.nextRangeF(-3000.f, 3000.f); |
| 701 | randTriples[i].fZ = rand.nextRangeF(-3000.f, 3000.f); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | SkMatrix mats[kMatrixCount]; |
| 705 | for (int i = 0; i < kMatrixCount; ++i) { |
| 706 | for (int j = 0; j < 9; ++j) { |
| 707 | mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f)); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | // identity |
| 712 | { |
| 713 | mat.reset(); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 714 | SkPoint3 dst[kTripleCount]; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 715 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 716 | REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(randTriples, dst, kTripleCount)); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 719 | const SkPoint3 zeros = {0.f, 0.f, 0.f}; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 720 | // zero matrix |
| 721 | { |
| 722 | mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 723 | SkPoint3 dst[kTripleCount]; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 724 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 725 | for (int i = 0; i < kTripleCount; ++i) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 726 | REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(&dst[i], &zeros, 1)); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | // zero point |
| 731 | { |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 732 | for (int i = 0; i < kMatrixCount; ++i) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 733 | SkPoint3 dst; |
| 734 | mats[i].mapHomogeneousPoints(&dst, &zeros, 1); |
| 735 | REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(&dst, &zeros, 1)); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | // doesn't crash with null dst, src, count == 0 |
| 740 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 741 | mats[0].mapHomogeneousPoints(nullptr, nullptr, 0); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // uniform scale of point |
| 745 | { |
| 746 | mat.setScale(kScale0, kScale0); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 747 | SkPoint3 dst; |
| 748 | SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f}; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 749 | SkPoint pnt; |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 750 | pnt.set(src.fX, src.fY); |
| 751 | mat.mapHomogeneousPoints(&dst, &src, 1); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 752 | mat.mapPoints(&pnt, &pnt, 1); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 753 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX)); |
| 754 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY)); |
| 755 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, SK_Scalar1)); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | // rotation of point |
| 759 | { |
| 760 | mat.setRotate(kRotation0); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 761 | SkPoint3 dst; |
| 762 | SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f}; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 763 | SkPoint pnt; |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 764 | pnt.set(src.fX, src.fY); |
| 765 | mat.mapHomogeneousPoints(&dst, &src, 1); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 766 | mat.mapPoints(&pnt, &pnt, 1); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 767 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX)); |
| 768 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY)); |
| 769 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, SK_Scalar1)); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // rotation, scale, rotation of point |
| 773 | { |
| 774 | mat.setRotate(kRotation1); |
| 775 | mat.postScale(kScale0, kScale0); |
| 776 | mat.postRotate(kRotation0); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 777 | SkPoint3 dst; |
| 778 | SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f}; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 779 | SkPoint pnt; |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 780 | pnt.set(src.fX, src.fY); |
| 781 | mat.mapHomogeneousPoints(&dst, &src, 1); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 782 | mat.mapPoints(&pnt, &pnt, 1); |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 783 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX)); |
| 784 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY)); |
| 785 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, SK_Scalar1)); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | // compare with naive approach |
| 789 | { |
| 790 | for (int i = 0; i < kMatrixCount; ++i) { |
| 791 | for (int j = 0; j < kTripleCount; ++j) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 792 | SkPoint3 dst; |
| 793 | mats[i].mapHomogeneousPoints(&dst, &randTriples[j], 1); |
| 794 | REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], randTriples[j], dst)); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | } |
| 800 | |
reed | adf9990 | 2015-03-19 16:10:54 -0700 | [diff] [blame] | 801 | static bool check_decompScale(const SkMatrix& matrix) { |
| 802 | SkSize scale; |
| 803 | SkMatrix remaining; |
| 804 | |
| 805 | if (!matrix.decomposeScale(&scale, &remaining)) { |
| 806 | return false; |
| 807 | } |
| 808 | if (scale.width() <= 0 || scale.height() <= 0) { |
| 809 | return false; |
| 810 | } |
| 811 | remaining.preScale(scale.width(), scale.height()); |
| 812 | return nearly_equal(matrix, remaining); |
| 813 | } |
| 814 | |
| 815 | static void test_decompScale(skiatest::Reporter* reporter) { |
| 816 | SkMatrix m; |
| 817 | |
| 818 | m.reset(); |
| 819 | REPORTER_ASSERT(reporter, check_decompScale(m)); |
| 820 | m.setScale(2, 3); |
| 821 | REPORTER_ASSERT(reporter, check_decompScale(m)); |
| 822 | m.setRotate(35, 0, 0); |
| 823 | REPORTER_ASSERT(reporter, check_decompScale(m)); |
| 824 | |
| 825 | m.setScale(1, 0); |
| 826 | REPORTER_ASSERT(reporter, !check_decompScale(m)); |
| 827 | } |
| 828 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 829 | DEF_TEST(Matrix, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 830 | SkMatrix mat, inverse, iden1, iden2; |
| 831 | |
| 832 | mat.reset(); |
| 833 | mat.setTranslate(SK_Scalar1, SK_Scalar1); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 834 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 835 | iden1.setConcat(mat, inverse); |
| 836 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 837 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 838 | mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 839 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 840 | iden1.setConcat(mat, inverse); |
| 841 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 842 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 843 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 844 | mat.setScale(SK_Scalar1/2, SkIntToScalar(2)); |
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)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 848 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 849 | |
| 850 | mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0); |
| 851 | mat.postRotate(SkIntToScalar(25)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 852 | REPORTER_ASSERT(reporter, mat.invert(nullptr)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 853 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 854 | iden1.setConcat(mat, inverse); |
| 855 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 856 | iden2.setConcat(inverse, mat); |
| 857 | REPORTER_ASSERT(reporter, is_identity(iden2)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 858 | test_flatten(reporter, mat); |
| 859 | test_flatten(reporter, iden2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 860 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 861 | mat.setScale(0, SK_Scalar1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 862 | REPORTER_ASSERT(reporter, !mat.invert(nullptr)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 863 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 864 | mat.setScale(SK_Scalar1, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 865 | REPORTER_ASSERT(reporter, !mat.invert(nullptr)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 866 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 867 | |
robertphillips | 20eee3f | 2015-06-19 05:14:26 -0700 | [diff] [blame] | 868 | // Inverting this matrix results in a non-finite matrix |
| 869 | mat.setAll(0.0f, 1.0f, 2.0f, |
| 870 | 0.0f, 1.0f, -3.40277175e+38f, |
| 871 | 1.00003040f, 1.0f, 0.0f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 872 | REPORTER_ASSERT(reporter, !mat.invert(nullptr)); |
robertphillips | 20eee3f | 2015-06-19 05:14:26 -0700 | [diff] [blame] | 873 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 874 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 875 | // rectStaysRect test |
| 876 | { |
| 877 | static const struct { |
| 878 | SkScalar m00, m01, m10, m11; |
| 879 | bool mStaysRect; |
| 880 | } |
| 881 | gRectStaysRectSamples[] = { |
| 882 | { 0, 0, 0, 0, false }, |
| 883 | { 0, 0, 0, SK_Scalar1, false }, |
| 884 | { 0, 0, SK_Scalar1, 0, false }, |
| 885 | { 0, 0, SK_Scalar1, SK_Scalar1, false }, |
| 886 | { 0, SK_Scalar1, 0, 0, false }, |
| 887 | { 0, SK_Scalar1, 0, SK_Scalar1, false }, |
| 888 | { 0, SK_Scalar1, SK_Scalar1, 0, true }, |
| 889 | { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }, |
| 890 | { SK_Scalar1, 0, 0, 0, false }, |
| 891 | { SK_Scalar1, 0, 0, SK_Scalar1, true }, |
| 892 | { SK_Scalar1, 0, SK_Scalar1, 0, false }, |
| 893 | { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false }, |
| 894 | { SK_Scalar1, SK_Scalar1, 0, 0, false }, |
| 895 | { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false }, |
| 896 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false }, |
| 897 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false } |
| 898 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 899 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 900 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) { |
| 901 | SkMatrix m; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 902 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 903 | m.reset(); |
| 904 | m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00); |
| 905 | m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01); |
| 906 | m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10); |
| 907 | m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11); |
| 908 | REPORTER_ASSERT(reporter, |
| 909 | m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect); |
| 910 | } |
| 911 | } |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 912 | |
bungeman@google.com | ba7983e | 2011-07-13 20:18:16 +0000 | [diff] [blame] | 913 | mat.reset(); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 914 | mat.set(SkMatrix::kMScaleX, SkIntToScalar(1)); |
| 915 | mat.set(SkMatrix::kMSkewX, SkIntToScalar(2)); |
| 916 | mat.set(SkMatrix::kMTransX, SkIntToScalar(3)); |
| 917 | mat.set(SkMatrix::kMSkewY, SkIntToScalar(4)); |
| 918 | mat.set(SkMatrix::kMScaleY, SkIntToScalar(5)); |
| 919 | mat.set(SkMatrix::kMTransY, SkIntToScalar(6)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 920 | SkScalar affine[6]; |
| 921 | REPORTER_ASSERT(reporter, mat.asAffine(affine)); |
| 922 | |
Mike Reed | f0cb7a0 | 2017-10-13 13:26:00 +0000 | [diff] [blame] | 923 | #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e) |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 924 | REPORTER_ASSERT(reporter, affineEqual(ScaleX)); |
| 925 | REPORTER_ASSERT(reporter, affineEqual(SkewY)); |
| 926 | REPORTER_ASSERT(reporter, affineEqual(SkewX)); |
| 927 | REPORTER_ASSERT(reporter, affineEqual(ScaleY)); |
| 928 | REPORTER_ASSERT(reporter, affineEqual(TransX)); |
| 929 | REPORTER_ASSERT(reporter, affineEqual(TransY)); |
| 930 | #undef affineEqual |
| 931 | |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 932 | mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 933 | REPORTER_ASSERT(reporter, !mat.asAffine(affine)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 934 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 935 | SkMatrix mat2; |
| 936 | mat2.reset(); |
| 937 | mat.reset(); |
| 938 | SkScalar zero = 0; |
| 939 | mat.set(SkMatrix::kMSkewX, -zero); |
| 940 | REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 941 | |
| 942 | mat2.reset(); |
| 943 | mat.reset(); |
| 944 | mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 945 | mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 946 | REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
| 947 | |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 948 | test_matrix_min_max_scale(reporter); |
jvanverth | 17a845f | 2014-09-02 13:15:40 -0700 | [diff] [blame] | 949 | test_matrix_preserve_shape(reporter); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 950 | test_matrix_recttorect(reporter); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 951 | test_matrix_decomposition(reporter); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 952 | test_matrix_homogeneous(reporter); |
reed | 451e822 | 2014-12-13 08:46:48 -0800 | [diff] [blame] | 953 | test_set9(reporter); |
reed | adf9990 | 2015-03-19 16:10:54 -0700 | [diff] [blame] | 954 | |
| 955 | test_decompScale(reporter); |
reed | 2dcb615 | 2016-07-05 20:10:42 -0700 | [diff] [blame] | 956 | |
| 957 | mat.setScaleTranslate(2, 3, 1, 4); |
| 958 | mat2.setScale(2, 3); |
| 959 | mat2.postTranslate(1, 4); |
| 960 | REPORTER_ASSERT(reporter, mat == mat2); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 961 | } |
commit-bot@chromium.org | 99bd7d8 | 2014-05-19 15:51:12 +0000 | [diff] [blame] | 962 | |
| 963 | DEF_TEST(Matrix_Concat, r) { |
| 964 | SkMatrix a; |
| 965 | a.setTranslate(10, 20); |
| 966 | |
| 967 | SkMatrix b; |
| 968 | b.setScale(3, 5); |
| 969 | |
| 970 | SkMatrix expected; |
| 971 | expected.setConcat(a,b); |
| 972 | |
| 973 | REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b)); |
| 974 | } |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 975 | |
| 976 | // Test that all variants of maprect are correct. |
| 977 | DEF_TEST(Matrix_maprects, r) { |
| 978 | const SkScalar scale = 1000; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 979 | |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 980 | SkMatrix mat; |
| 981 | mat.setScale(2, 3); |
| 982 | mat.postTranslate(1, 4); |
| 983 | |
| 984 | SkRandom rand; |
| 985 | for (int i = 0; i < 10000; ++i) { |
| 986 | SkRect src = SkRect::MakeLTRB(rand.nextSScalar1() * scale, |
| 987 | rand.nextSScalar1() * scale, |
| 988 | rand.nextSScalar1() * scale, |
| 989 | rand.nextSScalar1() * scale); |
Cary Clark | c06754b | 2018-05-16 21:28:55 -0400 | [diff] [blame^] | 990 | SkRect dst[4]; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 991 | |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 992 | mat.mapPoints((SkPoint*)&dst[0].fLeft, (SkPoint*)&src.fLeft, 2); |
| 993 | dst[0].sort(); |
| 994 | mat.mapRect(&dst[1], src); |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 995 | mat.mapRectScaleTranslate(&dst[2], src); |
Cary Clark | c06754b | 2018-05-16 21:28:55 -0400 | [diff] [blame^] | 996 | dst[3] = mat.mapRect(src); |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 997 | |
| 998 | REPORTER_ASSERT(r, dst[0] == dst[1]); |
| 999 | REPORTER_ASSERT(r, dst[0] == dst[2]); |
Cary Clark | c06754b | 2018-05-16 21:28:55 -0400 | [diff] [blame^] | 1000 | REPORTER_ASSERT(r, dst[0] == dst[3]); |
| 1001 | } |
| 1002 | |
| 1003 | // We should report nonfinite-ness after a mapping |
| 1004 | { |
| 1005 | // We have special-cases in mapRect for different matrix types |
| 1006 | SkMatrix m0 = SkMatrix::MakeScale(1e20f, 1e20f); |
| 1007 | SkMatrix m1; m1.setRotate(30); m1.postScale(1e20f, 1e20f); |
| 1008 | |
| 1009 | for (const auto& m : { m0, m1 }) { |
| 1010 | SkRect rect = { 0, 0, 1e20f, 1e20f }; |
| 1011 | REPORTER_ASSERT(r, rect.isFinite()); |
| 1012 | rect = m.mapRect(rect); |
| 1013 | REPORTER_ASSERT(r, !rect.isFinite()); |
| 1014 | } |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |