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