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 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 10 | #include "SkMath.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 11 | #include "SkMatrix.h" |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 12 | #include "SkMatrixUtils.h" |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 13 | #include "SkRandom.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 { |
| 46 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI); |
| 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@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 75 | static void test_matrix_recttorect(skiatest::Reporter* reporter) { |
| 76 | SkRect src, dst; |
| 77 | SkMatrix matrix; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 78 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 79 | src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10); |
| 80 | dst = src; |
| 81 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 82 | REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType()); |
| 83 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 84 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 85 | dst.offset(SK_Scalar1, SK_Scalar1); |
| 86 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 87 | REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType()); |
| 88 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 89 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 90 | dst.fRight += SK_Scalar1; |
| 91 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 93 | (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType()); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 95 | |
| 96 | dst = src; |
| 97 | dst.fRight = src.fRight * 2; |
| 98 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 99 | REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType()); |
| 100 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 101 | } |
| 102 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 103 | static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) { |
| 104 | // add 100 in case we have a bug, I don't want to kill my stack in the test |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 105 | static const size_t kBufferSize = SkMatrix::kMaxFlattenSize + 100; |
| 106 | char buffer[kBufferSize]; |
| 107 | size_t size1 = m.writeToMemory(NULL); |
| 108 | size_t size2 = m.writeToMemory(buffer); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, size1 == size2); |
| 110 | REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 112 | SkMatrix m2; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 113 | size_t size3 = m2.readFromMemory(buffer, kBufferSize); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 114 | REPORTER_ASSERT(reporter, size1 == size3); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 115 | REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 116 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 117 | char buffer2[kBufferSize]; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 118 | size3 = m2.writeToMemory(buffer2); |
| 119 | REPORTER_ASSERT(reporter, size1 == size3); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 120 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
| 121 | } |
| 122 | |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 123 | static void test_matrix_min_max_stretch(skiatest::Reporter* reporter) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 124 | SkMatrix identity; |
| 125 | identity.reset(); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 126 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinStretch()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 127 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch()); |
| 128 | |
| 129 | SkMatrix scale; |
| 130 | scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 131 | REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinStretch()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch()); |
| 133 | |
| 134 | SkMatrix rot90Scale; |
| 135 | rot90Scale.setRotate(90 * SK_Scalar1); |
| 136 | rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 137 | REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinStretch()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 138 | REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch()); |
| 139 | |
| 140 | SkMatrix rotate; |
| 141 | rotate.setRotate(128 * SK_Scalar1); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 142 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinStretch() ,SK_ScalarNearlyZero)); |
| 143 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxStretch(), SK_ScalarNearlyZero)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 144 | |
| 145 | SkMatrix translate; |
| 146 | translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 147 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinStretch()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 148 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch()); |
| 149 | |
| 150 | SkMatrix perspX; |
| 151 | perspX.reset(); |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 152 | perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000)); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinStretch()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch()); |
| 155 | |
| 156 | SkMatrix perspY; |
| 157 | perspY.reset(); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 158 | perspY.setPerspY(SkScalarToPersp(-SK_Scalar1 / 500)); |
| 159 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinStretch()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 160 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch()); |
| 161 | |
| 162 | SkMatrix baseMats[] = {scale, rot90Scale, rotate, |
| 163 | translate, perspX, perspY}; |
| 164 | SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 165 | for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 166 | mats[i] = baseMats[i]; |
| 167 | bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); |
| 168 | REPORTER_ASSERT(reporter, invertable); |
| 169 | } |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 170 | SkRandom rand; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 171 | for (int m = 0; m < 1000; ++m) { |
| 172 | SkMatrix mat; |
| 173 | mat.reset(); |
| 174 | for (int i = 0; i < 4; ++i) { |
| 175 | int x = rand.nextU() % SK_ARRAY_COUNT(mats); |
| 176 | mat.postConcat(mats[x]); |
| 177 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 178 | |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 179 | SkScalar minStretch = mat.getMinStretch(); |
| 180 | SkScalar maxStretch = mat.getMaxStretch(); |
| 181 | REPORTER_ASSERT(reporter, (minStretch < 0) == (maxStretch < 0)); |
| 182 | REPORTER_ASSERT(reporter, (maxStretch < 0) == mat.hasPerspective()); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 183 | |
| 184 | if (mat.hasPerspective()) { |
| 185 | m -= 1; // try another non-persp matrix |
| 186 | continue; |
| 187 | } |
| 188 | |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 189 | // test a bunch of vectors. All should be scaled by between minStretch and maxStretch |
| 190 | // (modulo some error) and we should find a vector that is scaled by almost each. |
| 191 | static const SkScalar gVectorStretchTol = (105 * SK_Scalar1) / 100; |
| 192 | static const SkScalar gClosestStretchTol = (97 * SK_Scalar1) / 100; |
| 193 | SkScalar max = 0, min = SK_ScalarMax; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 194 | SkVector vectors[1000]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 195 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 196 | vectors[i].fX = rand.nextSScalar1(); |
| 197 | vectors[i].fY = rand.nextSScalar1(); |
| 198 | if (!vectors[i].normalize()) { |
| 199 | i -= 1; |
| 200 | continue; |
| 201 | } |
| 202 | } |
| 203 | mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors)); |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 204 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 205 | SkScalar d = vectors[i].length(); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 206 | REPORTER_ASSERT(reporter, SkScalarDiv(d, maxStretch) < gVectorStretchTol); |
| 207 | REPORTER_ASSERT(reporter, SkScalarDiv(minStretch, d) < gVectorStretchTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 208 | if (max < d) { |
| 209 | max = d; |
| 210 | } |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 211 | if (min > d) { |
| 212 | min = d; |
| 213 | } |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 214 | } |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 215 | REPORTER_ASSERT(reporter, SkScalarDiv(max, maxStretch) >= gClosestStretchTol); |
| 216 | REPORTER_ASSERT(reporter, SkScalarDiv(minStretch, min) >= gClosestStretchTol); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 220 | static void test_matrix_is_similarity(skiatest::Reporter* reporter) { |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 221 | SkMatrix mat; |
| 222 | |
| 223 | // identity |
| 224 | mat.setIdentity(); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 225 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 226 | |
| 227 | // translation only |
| 228 | mat.reset(); |
| 229 | mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 230 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 231 | |
| 232 | // scale with same size |
| 233 | mat.reset(); |
| 234 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 235 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 236 | |
| 237 | // scale with one negative |
| 238 | mat.reset(); |
| 239 | mat.setScale(SkIntToScalar(-15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 240 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 241 | |
| 242 | // scale with different size |
| 243 | mat.reset(); |
| 244 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 245 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 246 | |
| 247 | // scale with same size at a pivot point |
| 248 | mat.reset(); |
| 249 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15), |
| 250 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 251 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 252 | |
| 253 | // scale with different size at a pivot point |
| 254 | mat.reset(); |
| 255 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20), |
| 256 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 257 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 258 | |
| 259 | // skew with same size |
| 260 | mat.reset(); |
| 261 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 262 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 263 | |
| 264 | // skew with different size |
| 265 | mat.reset(); |
| 266 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 267 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 268 | |
| 269 | // skew with same size at a pivot point |
| 270 | mat.reset(); |
| 271 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15), |
| 272 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 273 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 274 | |
| 275 | // skew with different size at a pivot point |
| 276 | mat.reset(); |
| 277 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20), |
| 278 | SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 280 | |
| 281 | // perspective x |
| 282 | mat.reset(); |
| 283 | mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 285 | |
| 286 | // perspective y |
| 287 | mat.reset(); |
| 288 | mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 289 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 290 | |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 291 | // rotate |
| 292 | for (int angle = 0; angle < 360; ++angle) { |
| 293 | mat.reset(); |
| 294 | mat.setRotate(SkIntToScalar(angle)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 295 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // see if there are any accumulated precision issues |
| 299 | mat.reset(); |
| 300 | for (int i = 1; i < 360; i++) { |
| 301 | mat.postRotate(SkIntToScalar(1)); |
| 302 | } |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 303 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 304 | |
| 305 | // rotate + translate |
| 306 | mat.reset(); |
| 307 | mat.setRotate(SkIntToScalar(30)); |
| 308 | mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 309 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 310 | |
| 311 | // rotate + uniform scale |
| 312 | mat.reset(); |
| 313 | mat.setRotate(SkIntToScalar(30)); |
| 314 | mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 315 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 316 | |
| 317 | // rotate + non-uniform scale |
| 318 | mat.reset(); |
| 319 | mat.setRotate(SkIntToScalar(30)); |
| 320 | mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 321 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 322 | |
| 323 | // all zero |
| 324 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 325 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 326 | |
| 327 | // all zero except perspective |
| 328 | 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] | 329 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 330 | |
| 331 | // scales zero, only skews |
| 332 | mat.setAll(0, SK_Scalar1, 0, |
| 333 | SK_Scalar1, 0, 0, |
| 334 | 0, 0, SkMatrix::I()[8]); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 335 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 336 | } |
| 337 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 338 | // For test_matrix_decomposition, below. |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 339 | static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b, |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 340 | SkScalar tolerance = SK_ScalarNearlyZero) { |
| 341 | // from Bruce Dawson |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 342 | // absolute check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 343 | SkScalar diff = SkScalarAbs(a - b); |
| 344 | if (diff < tolerance) { |
| 345 | return true; |
| 346 | } |
| 347 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 348 | // relative check |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 349 | a = SkScalarAbs(a); |
| 350 | b = SkScalarAbs(b); |
| 351 | SkScalar largest = (b > a) ? b : a; |
| 352 | |
| 353 | if (diff <= largest*tolerance) { |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 360 | static bool check_matrix_recomposition(const SkMatrix& mat, |
| 361 | const SkPoint& rotation1, |
| 362 | const SkPoint& scale, |
| 363 | const SkPoint& rotation2) { |
| 364 | SkScalar c1 = rotation1.fX; |
| 365 | SkScalar s1 = rotation1.fY; |
| 366 | SkScalar scaleX = scale.fX; |
| 367 | SkScalar scaleY = scale.fY; |
| 368 | SkScalar c2 = rotation2.fX; |
| 369 | SkScalar s2 = rotation2.fY; |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 370 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 371 | // We do a relative check here because large scale factors cause problems with an absolute check |
| 372 | bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 373 | scaleX*c1*c2 - scaleY*s1*s2) && |
| 374 | scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
| 375 | -scaleX*s1*c2 - scaleY*c1*s2) && |
| 376 | scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
| 377 | scaleX*c1*s2 + scaleY*s1*c2) && |
| 378 | scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
| 379 | -scaleX*s1*s2 + scaleY*c1*c2); |
| 380 | return result; |
| 381 | } |
| 382 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 383 | static void test_matrix_decomposition(skiatest::Reporter* reporter) { |
| 384 | SkMatrix mat; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 385 | SkPoint rotation1, scale, rotation2; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 386 | |
| 387 | const float kRotation0 = 15.5f; |
| 388 | const float kRotation1 = -50.f; |
| 389 | const float kScale0 = 5000.f; |
| 390 | const float kScale1 = 0.001f; |
| 391 | |
| 392 | // identity |
| 393 | mat.reset(); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 394 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 395 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 396 | // make sure it doesn't crash if we pass in NULLs |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 397 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 398 | |
| 399 | // rotation only |
| 400 | mat.setRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 401 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 402 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 403 | |
| 404 | // uniform scale only |
| 405 | mat.setScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 406 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 407 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 408 | |
| 409 | // anisotropic scale only |
| 410 | mat.setScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 411 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 412 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 413 | |
| 414 | // rotation then uniform scale |
| 415 | mat.setRotate(kRotation1); |
| 416 | mat.postScale(kScale0, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 417 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 418 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 419 | |
| 420 | // uniform scale then rotation |
| 421 | mat.setScale(kScale0, kScale0); |
| 422 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 423 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 424 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 425 | |
| 426 | // rotation then uniform scale+reflection |
| 427 | mat.setRotate(kRotation0); |
| 428 | mat.postScale(kScale1, -kScale1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 429 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 430 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 431 | |
| 432 | // uniform scale+reflection, then rotate |
| 433 | mat.setScale(kScale0, -kScale0); |
| 434 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 435 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 436 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 437 | |
| 438 | // rotation then anisotropic scale |
| 439 | mat.setRotate(kRotation1); |
| 440 | mat.postScale(kScale1, kScale0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 441 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 442 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 443 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 444 | // rotation then anisotropic scale |
| 445 | mat.setRotate(90); |
| 446 | mat.postScale(kScale1, kScale0); |
| 447 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 448 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 449 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 450 | // anisotropic scale then rotation |
| 451 | mat.setScale(kScale1, kScale0); |
| 452 | mat.postRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 453 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 454 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 455 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 456 | // anisotropic scale then rotation |
| 457 | mat.setScale(kScale1, kScale0); |
| 458 | mat.postRotate(90); |
| 459 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 460 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 461 | |
| 462 | // rotation, uniform scale, then different rotation |
| 463 | mat.setRotate(kRotation1); |
| 464 | mat.postScale(kScale0, kScale0); |
| 465 | mat.postRotate(kRotation0); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 466 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 467 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 468 | |
| 469 | // rotation, anisotropic scale, then different rotation |
| 470 | mat.setRotate(kRotation0); |
| 471 | mat.postScale(kScale1, kScale0); |
| 472 | mat.postRotate(kRotation1); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 473 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 474 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
skia.committer@gmail.com | 85092f0 | 2013-09-04 07:01:39 +0000 | [diff] [blame] | 475 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 476 | // rotation, anisotropic scale + reflection, then different rotation |
| 477 | mat.setRotate(kRotation0); |
| 478 | mat.postScale(-kScale1, kScale0); |
| 479 | mat.postRotate(kRotation1); |
| 480 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 481 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 482 | |
| 483 | // try some random matrices |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 484 | SkRandom rand; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 485 | for (int m = 0; m < 1000; ++m) { |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 486 | SkScalar rot0 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 487 | SkScalar sx = rand.nextRangeF(-3000.f, 3000.f); |
| 488 | SkScalar sy = rand.nextRangeF(-3000.f, 3000.f); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 489 | SkScalar rot1 = rand.nextRangeF(-180, 180); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 490 | mat.setRotate(rot0); |
| 491 | mat.postScale(sx, sy); |
| 492 | mat.postRotate(rot1); |
| 493 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 494 | if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) { |
| 495 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 496 | } else { |
| 497 | // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero |
| 498 | SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] - |
| 499 | mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY]; |
| 500 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot)); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // translation shouldn't affect this |
| 505 | mat.postTranslate(-1000.f, 1000.f); |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 506 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 507 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 508 | |
| 509 | // perspective shouldn't affect this |
jvanverth@google.com | 588f3d3 | 2013-07-24 18:44:10 +0000 | [diff] [blame] | 510 | mat[SkMatrix::kMPersp0] = 12.f; |
| 511 | mat[SkMatrix::kMPersp1] = 4.f; |
| 512 | mat[SkMatrix::kMPersp2] = 1872.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 513 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
| 514 | REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 515 | |
| 516 | // degenerate matrices |
| 517 | // mostly zero entries |
| 518 | mat.reset(); |
| 519 | mat[SkMatrix::kMScaleX] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 520 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 521 | mat.reset(); |
| 522 | mat[SkMatrix::kMScaleY] = 0.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 523 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 524 | mat.reset(); |
| 525 | // linearly dependent entries |
| 526 | mat[SkMatrix::kMScaleX] = 1.f; |
| 527 | mat[SkMatrix::kMSkewX] = 2.f; |
| 528 | mat[SkMatrix::kMSkewY] = 4.f; |
| 529 | mat[SkMatrix::kMScaleY] = 8.f; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 530 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 531 | } |
| 532 | |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 533 | // For test_matrix_homogeneous, below. |
| 534 | static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) { |
| 535 | for (int i = 0; i < count; ++i) { |
| 536 | if (!scalar_nearly_equal_relative(a[i], b[i])) { |
| 537 | return false; |
| 538 | } |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | // For test_matrix_homogeneous, below. |
| 544 | // Maps a single triple in src using m and compares results to those in dst |
| 545 | static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3], |
| 546 | const SkScalar dst[3]) { |
| 547 | SkScalar res[3]; |
| 548 | SkScalar ms[9] = {m[0], m[1], m[2], |
| 549 | m[3], m[4], m[5], |
| 550 | m[6], m[7], m[8]}; |
| 551 | res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2]; |
| 552 | res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5]; |
| 553 | res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8]; |
| 554 | return scalar_array_nearly_equal_relative(res, dst, 3); |
| 555 | } |
| 556 | |
| 557 | static void test_matrix_homogeneous(skiatest::Reporter* reporter) { |
| 558 | SkMatrix mat; |
| 559 | |
| 560 | const float kRotation0 = 15.5f; |
| 561 | const float kRotation1 = -50.f; |
| 562 | const float kScale0 = 5000.f; |
| 563 | |
| 564 | const int kTripleCount = 1000; |
| 565 | const int kMatrixCount = 1000; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 566 | SkRandom rand; |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 567 | |
| 568 | SkScalar randTriples[3*kTripleCount]; |
| 569 | for (int i = 0; i < 3*kTripleCount; ++i) { |
| 570 | randTriples[i] = rand.nextRangeF(-3000.f, 3000.f); |
| 571 | } |
| 572 | |
| 573 | SkMatrix mats[kMatrixCount]; |
| 574 | for (int i = 0; i < kMatrixCount; ++i) { |
| 575 | for (int j = 0; j < 9; ++j) { |
| 576 | mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f)); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // identity |
| 581 | { |
| 582 | mat.reset(); |
| 583 | SkScalar dst[3*kTripleCount]; |
| 584 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 585 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3)); |
| 586 | } |
| 587 | |
| 588 | // zero matrix |
| 589 | { |
| 590 | mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f); |
| 591 | SkScalar dst[3*kTripleCount]; |
| 592 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 593 | SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 594 | for (int i = 0; i < kTripleCount; ++i) { |
| 595 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3)); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // zero point |
| 600 | { |
| 601 | SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 602 | for (int i = 0; i < kMatrixCount; ++i) { |
| 603 | SkScalar dst[3]; |
| 604 | mats[i].mapHomogeneousPoints(dst, zeros, 1); |
| 605 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3)); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // doesn't crash with null dst, src, count == 0 |
| 610 | { |
| 611 | mats[0].mapHomogeneousPoints(NULL, NULL, 0); |
| 612 | } |
| 613 | |
| 614 | // uniform scale of point |
| 615 | { |
| 616 | mat.setScale(kScale0, kScale0); |
| 617 | SkScalar dst[3]; |
| 618 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 619 | SkPoint pnt; |
| 620 | pnt.set(src[0], src[1]); |
| 621 | mat.mapHomogeneousPoints(dst, src, 1); |
| 622 | mat.mapPoints(&pnt, &pnt, 1); |
| 623 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 624 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 625 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 626 | } |
| 627 | |
| 628 | // rotation of point |
| 629 | { |
| 630 | mat.setRotate(kRotation0); |
| 631 | SkScalar dst[3]; |
| 632 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 633 | SkPoint pnt; |
| 634 | pnt.set(src[0], src[1]); |
| 635 | mat.mapHomogeneousPoints(dst, src, 1); |
| 636 | mat.mapPoints(&pnt, &pnt, 1); |
| 637 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 638 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 639 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 640 | } |
| 641 | |
| 642 | // rotation, scale, rotation of point |
| 643 | { |
| 644 | mat.setRotate(kRotation1); |
| 645 | mat.postScale(kScale0, kScale0); |
| 646 | mat.postRotate(kRotation0); |
| 647 | SkScalar dst[3]; |
| 648 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 649 | SkPoint pnt; |
| 650 | pnt.set(src[0], src[1]); |
| 651 | mat.mapHomogeneousPoints(dst, src, 1); |
| 652 | mat.mapPoints(&pnt, &pnt, 1); |
| 653 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 654 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 655 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 656 | } |
| 657 | |
| 658 | // compare with naive approach |
| 659 | { |
| 660 | for (int i = 0; i < kMatrixCount; ++i) { |
| 661 | for (int j = 0; j < kTripleCount; ++j) { |
| 662 | SkScalar dst[3]; |
| 663 | mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1); |
| 664 | REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst)); |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | } |
| 670 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 671 | DEF_TEST(Matrix, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 672 | SkMatrix mat, inverse, iden1, iden2; |
| 673 | |
| 674 | mat.reset(); |
| 675 | mat.setTranslate(SK_Scalar1, SK_Scalar1); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 676 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 677 | iden1.setConcat(mat, inverse); |
| 678 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 679 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 680 | mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 681 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 682 | iden1.setConcat(mat, inverse); |
| 683 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 684 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 685 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 686 | mat.setScale(SK_Scalar1/2, SkIntToScalar(2)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 687 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 688 | iden1.setConcat(mat, inverse); |
| 689 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 690 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 691 | |
| 692 | mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0); |
| 693 | mat.postRotate(SkIntToScalar(25)); |
| 694 | REPORTER_ASSERT(reporter, mat.invert(NULL)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 695 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 696 | iden1.setConcat(mat, inverse); |
| 697 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 698 | iden2.setConcat(inverse, mat); |
| 699 | REPORTER_ASSERT(reporter, is_identity(iden2)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 700 | test_flatten(reporter, mat); |
| 701 | test_flatten(reporter, iden2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 702 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 703 | mat.setScale(0, SK_Scalar1); |
reed@google.com | e40591d | 2013-01-30 15:47:42 +0000 | [diff] [blame] | 704 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 705 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 706 | mat.setScale(SK_Scalar1, 0); |
reed@google.com | e40591d | 2013-01-30 15:47:42 +0000 | [diff] [blame] | 707 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 708 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 709 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 710 | // rectStaysRect test |
| 711 | { |
| 712 | static const struct { |
| 713 | SkScalar m00, m01, m10, m11; |
| 714 | bool mStaysRect; |
| 715 | } |
| 716 | gRectStaysRectSamples[] = { |
| 717 | { 0, 0, 0, 0, false }, |
| 718 | { 0, 0, 0, SK_Scalar1, false }, |
| 719 | { 0, 0, SK_Scalar1, 0, false }, |
| 720 | { 0, 0, SK_Scalar1, SK_Scalar1, false }, |
| 721 | { 0, SK_Scalar1, 0, 0, false }, |
| 722 | { 0, SK_Scalar1, 0, SK_Scalar1, false }, |
| 723 | { 0, SK_Scalar1, SK_Scalar1, 0, true }, |
| 724 | { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }, |
| 725 | { SK_Scalar1, 0, 0, 0, false }, |
| 726 | { SK_Scalar1, 0, 0, SK_Scalar1, true }, |
| 727 | { SK_Scalar1, 0, SK_Scalar1, 0, false }, |
| 728 | { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false }, |
| 729 | { SK_Scalar1, SK_Scalar1, 0, 0, false }, |
| 730 | { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false }, |
| 731 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false }, |
| 732 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false } |
| 733 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 734 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 735 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) { |
| 736 | SkMatrix m; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 737 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 738 | m.reset(); |
| 739 | m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00); |
| 740 | m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01); |
| 741 | m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10); |
| 742 | m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11); |
| 743 | REPORTER_ASSERT(reporter, |
| 744 | m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect); |
| 745 | } |
| 746 | } |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 747 | |
bungeman@google.com | ba7983e | 2011-07-13 20:18:16 +0000 | [diff] [blame] | 748 | mat.reset(); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 749 | mat.set(SkMatrix::kMScaleX, SkIntToScalar(1)); |
| 750 | mat.set(SkMatrix::kMSkewX, SkIntToScalar(2)); |
| 751 | mat.set(SkMatrix::kMTransX, SkIntToScalar(3)); |
| 752 | mat.set(SkMatrix::kMSkewY, SkIntToScalar(4)); |
| 753 | mat.set(SkMatrix::kMScaleY, SkIntToScalar(5)); |
| 754 | mat.set(SkMatrix::kMTransY, SkIntToScalar(6)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 755 | SkScalar affine[6]; |
| 756 | REPORTER_ASSERT(reporter, mat.asAffine(affine)); |
| 757 | |
| 758 | #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e) |
| 759 | REPORTER_ASSERT(reporter, affineEqual(ScaleX)); |
| 760 | REPORTER_ASSERT(reporter, affineEqual(SkewY)); |
| 761 | REPORTER_ASSERT(reporter, affineEqual(SkewX)); |
| 762 | REPORTER_ASSERT(reporter, affineEqual(ScaleY)); |
| 763 | REPORTER_ASSERT(reporter, affineEqual(TransX)); |
| 764 | REPORTER_ASSERT(reporter, affineEqual(TransY)); |
| 765 | #undef affineEqual |
| 766 | |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 767 | mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 768 | REPORTER_ASSERT(reporter, !mat.asAffine(affine)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 769 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 770 | SkMatrix mat2; |
| 771 | mat2.reset(); |
| 772 | mat.reset(); |
| 773 | SkScalar zero = 0; |
| 774 | mat.set(SkMatrix::kMSkewX, -zero); |
| 775 | REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 776 | |
| 777 | mat2.reset(); |
| 778 | mat.reset(); |
| 779 | mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 780 | mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 781 | REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
| 782 | |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 783 | test_matrix_min_max_stretch(reporter); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 784 | test_matrix_is_similarity(reporter); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 785 | test_matrix_recttorect(reporter); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 786 | test_matrix_decomposition(reporter); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame] | 787 | test_matrix_homogeneous(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 788 | } |