epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 8 | #include "Test.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 9 | #include "SkMath.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 10 | #include "SkMatrix.h" |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 11 | #include "SkMatrixUtils.h" |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 13 | |
| 14 | static bool nearly_equal_scalar(SkScalar a, SkScalar b) { |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 15 | // Note that we get more compounded error for multiple operations when |
| 16 | // SK_SCALAR_IS_FIXED. |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 17 | #ifdef SK_SCALAR_IS_FLOAT |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 18 | const SkScalar tolerance = SK_Scalar1 / 200000; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 19 | #else |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 20 | const SkScalar tolerance = SK_Scalar1 / 1024; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | return SkScalarAbs(a - b) <= tolerance; |
| 24 | } |
| 25 | |
| 26 | static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) { |
| 27 | for (int i = 0; i < 9; i++) { |
| 28 | if (!nearly_equal_scalar(a[i], b[i])) { |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 29 | printf("not equal %g %g\n", (float)a[i], (float)b[i]); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | return true; |
| 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; |
| 40 | bool cheapEqual = a.cheapEqualTo(b); |
| 41 | if (equal != cheapEqual) { |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 42 | #ifdef SK_SCALAR_IS_FLOAT |
bsalomon@google.com | 39d4f3a | 2012-03-26 17:25:45 +0000 | [diff] [blame] | 43 | if (equal) { |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 44 | bool foundZeroSignDiff = false; |
| 45 | for (int i = 0; i < 9; ++i) { |
| 46 | float aVal = a.get(i); |
| 47 | float bVal = b.get(i); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 48 | int aValI = *SkTCast<int*>(&aVal); |
| 49 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 50 | if (0 == aVal && 0 == bVal && aValI != bValI) { |
| 51 | foundZeroSignDiff = true; |
| 52 | } else { |
| 53 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI); |
| 54 | } |
| 55 | } |
| 56 | REPORTER_ASSERT(reporter, foundZeroSignDiff); |
| 57 | } else { |
| 58 | bool foundNaN = false; |
| 59 | for (int i = 0; i < 9; ++i) { |
| 60 | float aVal = a.get(i); |
| 61 | float bVal = b.get(i); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 62 | int aValI = *SkTCast<int*>(&aVal); |
| 63 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 64 | if (sk_float_isnan(aVal) && aValI == bValI) { |
| 65 | foundNaN = true; |
| 66 | } else { |
| 67 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI); |
| 68 | } |
| 69 | } |
| 70 | REPORTER_ASSERT(reporter, foundNaN); |
| 71 | } |
| 72 | #else |
| 73 | REPORTER_ASSERT(reporter, false); |
| 74 | #endif |
| 75 | } |
| 76 | return equal; |
| 77 | } |
| 78 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 79 | static bool is_identity(const SkMatrix& m) { |
| 80 | SkMatrix identity; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 81 | identity.reset(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 82 | return nearly_equal(m, identity); |
| 83 | } |
| 84 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 85 | static void test_matrix_recttorect(skiatest::Reporter* reporter) { |
| 86 | SkRect src, dst; |
| 87 | SkMatrix matrix; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 88 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 89 | src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10); |
| 90 | dst = src; |
| 91 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 92 | REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType()); |
| 93 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 94 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 95 | dst.offset(SK_Scalar1, SK_Scalar1); |
| 96 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 97 | REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType()); |
| 98 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 99 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 100 | dst.fRight += SK_Scalar1; |
| 101 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 103 | (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType()); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 105 | |
| 106 | dst = src; |
| 107 | dst.fRight = src.fRight * 2; |
| 108 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 109 | REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType()); |
| 110 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 111 | } |
| 112 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 113 | static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) { |
| 114 | // add 100 in case we have a bug, I don't want to kill my stack in the test |
| 115 | char buffer[SkMatrix::kMaxFlattenSize + 100]; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 116 | uint32_t size1 = m.writeToMemory(NULL); |
| 117 | uint32_t size2 = m.writeToMemory(buffer); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, size1 == size2); |
| 119 | REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 120 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 121 | SkMatrix m2; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 122 | uint32_t size3 = m2.readFromMemory(buffer); |
| 123 | REPORTER_ASSERT(reporter, size1 == size3); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 125 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 126 | char buffer2[SkMatrix::kMaxFlattenSize + 100]; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 127 | size3 = m2.writeToMemory(buffer2); |
| 128 | REPORTER_ASSERT(reporter, size1 == size3); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 129 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
| 130 | } |
| 131 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 132 | static void test_matrix_max_stretch(skiatest::Reporter* reporter) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 133 | SkMatrix identity; |
| 134 | identity.reset(); |
| 135 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch()); |
| 136 | |
| 137 | SkMatrix scale; |
| 138 | scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4); |
| 139 | REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch()); |
| 140 | |
| 141 | SkMatrix rot90Scale; |
| 142 | rot90Scale.setRotate(90 * SK_Scalar1); |
| 143 | rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2); |
| 144 | REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch()); |
| 145 | |
| 146 | SkMatrix rotate; |
| 147 | rotate.setRotate(128 * SK_Scalar1); |
| 148 | REPORTER_ASSERT(reporter, SkScalarAbs(SK_Scalar1 - rotate.getMaxStretch()) <= SK_ScalarNearlyZero); |
| 149 | |
| 150 | SkMatrix translate; |
| 151 | translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1); |
| 152 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch()); |
| 153 | |
| 154 | SkMatrix perspX; |
| 155 | perspX.reset(); |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 156 | perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch()); |
| 158 | |
| 159 | SkMatrix perspY; |
| 160 | perspY.reset(); |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 161 | perspY.setPerspX(SkScalarToPersp(-SK_Scalar1 / 500)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch()); |
| 163 | |
| 164 | SkMatrix baseMats[] = {scale, rot90Scale, rotate, |
| 165 | translate, perspX, perspY}; |
| 166 | SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 167 | for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 168 | mats[i] = baseMats[i]; |
| 169 | bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); |
| 170 | REPORTER_ASSERT(reporter, invertable); |
| 171 | } |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 172 | SkMWCRandom rand; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 173 | for (int m = 0; m < 1000; ++m) { |
| 174 | SkMatrix mat; |
| 175 | mat.reset(); |
| 176 | for (int i = 0; i < 4; ++i) { |
| 177 | int x = rand.nextU() % SK_ARRAY_COUNT(mats); |
| 178 | mat.postConcat(mats[x]); |
| 179 | } |
| 180 | SkScalar stretch = mat.getMaxStretch(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 182 | if ((stretch < 0) != mat.hasPerspective()) { |
| 183 | stretch = mat.getMaxStretch(); |
| 184 | } |
| 185 | |
| 186 | REPORTER_ASSERT(reporter, (stretch < 0) == mat.hasPerspective()); |
| 187 | |
| 188 | if (mat.hasPerspective()) { |
| 189 | m -= 1; // try another non-persp matrix |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | // test a bunch of vectors. None should be scaled by more than stretch |
| 194 | // (modulo some error) and we should find a vector that is scaled by |
| 195 | // almost stretch. |
| 196 | static const SkScalar gStretchTol = (105 * SK_Scalar1) / 100; |
| 197 | static const SkScalar gMaxStretchTol = (97 * SK_Scalar1) / 100; |
| 198 | SkScalar max = 0; |
| 199 | SkVector vectors[1000]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 200 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 201 | vectors[i].fX = rand.nextSScalar1(); |
| 202 | vectors[i].fY = rand.nextSScalar1(); |
| 203 | if (!vectors[i].normalize()) { |
| 204 | i -= 1; |
| 205 | continue; |
| 206 | } |
| 207 | } |
| 208 | mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors)); |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 209 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 210 | SkScalar d = vectors[i].length(); |
| 211 | REPORTER_ASSERT(reporter, SkScalarDiv(d, stretch) < gStretchTol); |
| 212 | if (max < d) { |
| 213 | max = d; |
| 214 | } |
| 215 | } |
| 216 | REPORTER_ASSERT(reporter, SkScalarDiv(max, stretch) >= gMaxStretchTol); |
| 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 | |
djsollen@google.com | 4bd2bdb | 2013-03-08 18:35:13 +0000 | [diff] [blame] | 291 | #ifdef SK_SCALAR_IS_FLOAT |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 292 | /* We bypass the following tests for SK_SCALAR_IS_FIXED build. |
| 293 | * The long discussion can be found in this issue: |
| 294 | * http://codereview.appspot.com/5999050/ |
| 295 | * In short, we haven't found a perfect way to fix the precision |
| 296 | * issue, i.e. the way we use tolerance in isSimilarityTransformation |
| 297 | * is incorrect. The situation becomes worse in fixed build, so |
| 298 | * we disabled rotation related tests for fixed build. |
| 299 | */ |
| 300 | |
| 301 | // rotate |
| 302 | for (int angle = 0; angle < 360; ++angle) { |
| 303 | mat.reset(); |
| 304 | mat.setRotate(SkIntToScalar(angle)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // see if there are any accumulated precision issues |
| 309 | mat.reset(); |
| 310 | for (int i = 1; i < 360; i++) { |
| 311 | mat.postRotate(SkIntToScalar(1)); |
| 312 | } |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 314 | |
| 315 | // rotate + translate |
| 316 | mat.reset(); |
| 317 | mat.setRotate(SkIntToScalar(30)); |
| 318 | mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 319 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 320 | |
| 321 | // rotate + uniform scale |
| 322 | mat.reset(); |
| 323 | mat.setRotate(SkIntToScalar(30)); |
| 324 | mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); |
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 | // rotate + non-uniform scale |
| 328 | mat.reset(); |
| 329 | mat.setRotate(SkIntToScalar(30)); |
| 330 | mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 331 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 332 | #endif |
| 333 | |
| 334 | // all zero |
| 335 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 336 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 337 | |
| 338 | // all zero except perspective |
| 339 | 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] | 340 | REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 341 | |
| 342 | // scales zero, only skews |
| 343 | mat.setAll(0, SK_Scalar1, 0, |
| 344 | SK_Scalar1, 0, 0, |
| 345 | 0, 0, SkMatrix::I()[8]); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 346 | REPORTER_ASSERT(reporter, mat.isSimilarity()); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 347 | } |
| 348 | |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 349 | // For test_matrix_decomposition, below. |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 350 | static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b, |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 351 | SkScalar tolerance = SK_ScalarNearlyZero) { |
| 352 | // from Bruce Dawson |
| 353 | SkScalar diff = SkScalarAbs(a - b); |
| 354 | if (diff < tolerance) { |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | a = SkScalarAbs(a); |
| 359 | b = SkScalarAbs(b); |
| 360 | SkScalar largest = (b > a) ? b : a; |
| 361 | |
| 362 | if (diff <= largest*tolerance) { |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | static void test_matrix_decomposition(skiatest::Reporter* reporter) { |
| 370 | SkMatrix mat; |
| 371 | SkScalar rotation0, scaleX, scaleY, rotation1; |
| 372 | |
| 373 | const float kRotation0 = 15.5f; |
| 374 | const float kRotation1 = -50.f; |
| 375 | const float kScale0 = 5000.f; |
| 376 | const float kScale1 = 0.001f; |
| 377 | |
| 378 | // identity |
| 379 | mat.reset(); |
| 380 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 381 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); |
| 382 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1)); |
| 383 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1)); |
| 384 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 385 | // make sure it doesn't crash if we pass in NULLs |
| 386 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL, NULL)); |
| 387 | |
| 388 | // rotation only |
| 389 | mat.setRotate(kRotation0); |
| 390 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 391 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0))); |
| 392 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1)); |
| 393 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1)); |
| 394 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 395 | |
| 396 | // uniform scale only |
| 397 | mat.setScale(kScale0, kScale0); |
| 398 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 399 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); |
| 400 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); |
| 401 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); |
| 402 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 403 | |
| 404 | // anisotropic scale only |
| 405 | mat.setScale(kScale1, kScale0); |
| 406 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 407 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); |
| 408 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); |
| 409 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); |
| 410 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 411 | |
| 412 | // rotation then uniform scale |
| 413 | mat.setRotate(kRotation1); |
| 414 | mat.postScale(kScale0, kScale0); |
| 415 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 416 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1))); |
| 417 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); |
| 418 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); |
| 419 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 420 | |
| 421 | // uniform scale then rotation |
| 422 | mat.setScale(kScale0, kScale0); |
| 423 | mat.postRotate(kRotation1); |
| 424 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 425 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1))); |
| 426 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); |
| 427 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); |
| 428 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 429 | |
| 430 | // rotation then uniform scale+reflection |
| 431 | mat.setRotate(kRotation0); |
| 432 | mat.postScale(kScale1, -kScale1); |
| 433 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 434 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0))); |
| 435 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); |
| 436 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale1)); |
| 437 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 438 | |
| 439 | // uniform scale+reflection, then rotate |
| 440 | mat.setScale(kScale0, -kScale0); |
| 441 | mat.postRotate(kRotation1); |
| 442 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 443 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(-kRotation1))); |
| 444 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); |
| 445 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale0)); |
| 446 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 447 | |
| 448 | // rotation then anisotropic scale |
| 449 | mat.setRotate(kRotation1); |
| 450 | mat.postScale(kScale1, kScale0); |
| 451 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 452 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1))); |
| 453 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); |
| 454 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); |
| 455 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 456 | |
| 457 | // anisotropic scale then rotation |
| 458 | mat.setScale(kScale1, kScale0); |
| 459 | mat.postRotate(kRotation0); |
| 460 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 461 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); |
| 462 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); |
| 463 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); |
| 464 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation1, SkDegreesToRadians(kRotation0))); |
| 465 | |
| 466 | // rotation, uniform scale, then different rotation |
| 467 | mat.setRotate(kRotation1); |
| 468 | mat.postScale(kScale0, kScale0); |
| 469 | mat.postRotate(kRotation0); |
| 470 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 471 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, |
| 472 | SkDegreesToRadians(kRotation0 + kRotation1))); |
| 473 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); |
| 474 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); |
| 475 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); |
| 476 | |
| 477 | // rotation, anisotropic scale, then different rotation |
| 478 | mat.setRotate(kRotation0); |
| 479 | mat.postScale(kScale1, kScale0); |
| 480 | mat.postRotate(kRotation1); |
| 481 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 482 | // Because of the shear/skew we won't get the same results, so we need to multiply it out. |
| 483 | // Generating the matrices requires doing a radian-to-degree calculation, then degree-to-radian |
| 484 | // calculation (in setRotate()), which adds error, so this just computes the matrix elements |
| 485 | // directly. |
| 486 | SkScalar c0; |
| 487 | SkScalar s0 = SkScalarSinCos(rotation0, &c0); |
| 488 | SkScalar c1; |
| 489 | SkScalar s1 = SkScalarSinCos(rotation1, &c1); |
| 490 | // We do a relative check here because large scale factors cause problems with an absolute check |
| 491 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 492 | scaleX*c0*c1 - scaleY*s0*s1)); |
| 493 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
| 494 | -scaleX*s0*c1 - scaleY*c0*s1)); |
| 495 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
| 496 | scaleX*c0*s1 + scaleY*s0*c1)); |
| 497 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
| 498 | -scaleX*s0*s1 + scaleY*c0*c1)); |
| 499 | |
| 500 | // try some random matrices |
| 501 | SkMWCRandom rand; |
| 502 | for (int m = 0; m < 1000; ++m) { |
| 503 | SkScalar rot0 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI); |
| 504 | SkScalar sx = rand.nextRangeF(-3000.f, 3000.f); |
| 505 | SkScalar sy = rand.nextRangeF(-3000.f, 3000.f); |
| 506 | SkScalar rot1 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI); |
| 507 | mat.setRotate(rot0); |
| 508 | mat.postScale(sx, sy); |
| 509 | mat.postRotate(rot1); |
| 510 | |
| 511 | if (SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)) { |
| 512 | SkScalar c0; |
| 513 | SkScalar s0 = SkScalarSinCos(rotation0, &c0); |
| 514 | SkScalar c1; |
| 515 | SkScalar s1 = SkScalarSinCos(rotation1, &c1); |
| 516 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 517 | scaleX*c0*c1 - scaleY*s0*s1)); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 518 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 519 | -scaleX*s0*c1 - scaleY*c0*s1)); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 520 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 521 | scaleX*c0*s1 + scaleY*s0*c1)); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 522 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 523 | -scaleX*s0*s1 + scaleY*c0*c1)); |
| 524 | } else { |
| 525 | // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero |
| 526 | SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] - |
| 527 | mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY]; |
| 528 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot)); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // translation shouldn't affect this |
| 533 | mat.postTranslate(-1000.f, 1000.f); |
| 534 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 535 | s0 = SkScalarSinCos(rotation0, &c0); |
| 536 | s1 = SkScalarSinCos(rotation1, &c1); |
| 537 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 538 | scaleX*c0*c1 - scaleY*s0*s1)); |
| 539 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
| 540 | -scaleX*s0*c1 - scaleY*c0*s1)); |
| 541 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
| 542 | scaleX*c0*s1 + scaleY*s0*c1)); |
| 543 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
| 544 | -scaleX*s0*s1 + scaleY*c0*c1)); |
| 545 | |
| 546 | // perspective shouldn't affect this |
jvanverth@google.com | 588f3d3 | 2013-07-24 18:44:10 +0000 | [diff] [blame] | 547 | mat[SkMatrix::kMPersp0] = 12.f; |
| 548 | mat[SkMatrix::kMPersp1] = 4.f; |
| 549 | mat[SkMatrix::kMPersp2] = 1872.f; |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 550 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 551 | s0 = SkScalarSinCos(rotation0, &c0); |
| 552 | s1 = SkScalarSinCos(rotation1, &c1); |
| 553 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 554 | scaleX*c0*c1 - scaleY*s0*s1)); |
| 555 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
| 556 | -scaleX*s0*c1 - scaleY*c0*s1)); |
| 557 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
| 558 | scaleX*c0*s1 + scaleY*s0*c1)); |
| 559 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
| 560 | -scaleX*s0*s1 + scaleY*c0*c1)); |
| 561 | |
| 562 | // rotation, anisotropic scale + reflection, then different rotation |
| 563 | mat.setRotate(kRotation0); |
| 564 | mat.postScale(-kScale1, kScale0); |
| 565 | mat.postRotate(kRotation1); |
| 566 | REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 567 | s0 = SkScalarSinCos(rotation0, &c0); |
| 568 | s1 = SkScalarSinCos(rotation1, &c1); |
| 569 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], |
| 570 | scaleX*c0*c1 - scaleY*s0*s1)); |
| 571 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], |
| 572 | -scaleX*s0*c1 - scaleY*c0*s1)); |
| 573 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], |
| 574 | scaleX*c0*s1 + scaleY*s0*c1)); |
| 575 | REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], |
| 576 | -scaleX*s0*s1 + scaleY*c0*c1)); |
| 577 | |
| 578 | // degenerate matrices |
| 579 | // mostly zero entries |
| 580 | mat.reset(); |
| 581 | mat[SkMatrix::kMScaleX] = 0.f; |
| 582 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 583 | mat.reset(); |
| 584 | mat[SkMatrix::kMScaleY] = 0.f; |
| 585 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 586 | mat.reset(); |
| 587 | // linearly dependent entries |
| 588 | mat[SkMatrix::kMScaleX] = 1.f; |
| 589 | mat[SkMatrix::kMSkewX] = 2.f; |
| 590 | mat[SkMatrix::kMSkewY] = 4.f; |
| 591 | mat[SkMatrix::kMScaleY] = 8.f; |
| 592 | REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); |
| 593 | } |
| 594 | |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame^] | 595 | // For test_matrix_homogeneous, below. |
| 596 | static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) { |
| 597 | for (int i = 0; i < count; ++i) { |
| 598 | if (!scalar_nearly_equal_relative(a[i], b[i])) { |
| 599 | return false; |
| 600 | } |
| 601 | } |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | // For test_matrix_homogeneous, below. |
| 606 | // Maps a single triple in src using m and compares results to those in dst |
| 607 | static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3], |
| 608 | const SkScalar dst[3]) { |
| 609 | SkScalar res[3]; |
| 610 | SkScalar ms[9] = {m[0], m[1], m[2], |
| 611 | m[3], m[4], m[5], |
| 612 | m[6], m[7], m[8]}; |
| 613 | res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2]; |
| 614 | res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5]; |
| 615 | res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8]; |
| 616 | return scalar_array_nearly_equal_relative(res, dst, 3); |
| 617 | } |
| 618 | |
| 619 | static void test_matrix_homogeneous(skiatest::Reporter* reporter) { |
| 620 | SkMatrix mat; |
| 621 | |
| 622 | const float kRotation0 = 15.5f; |
| 623 | const float kRotation1 = -50.f; |
| 624 | const float kScale0 = 5000.f; |
| 625 | |
| 626 | const int kTripleCount = 1000; |
| 627 | const int kMatrixCount = 1000; |
| 628 | SkRandom rand; |
| 629 | |
| 630 | SkScalar randTriples[3*kTripleCount]; |
| 631 | for (int i = 0; i < 3*kTripleCount; ++i) { |
| 632 | randTriples[i] = rand.nextRangeF(-3000.f, 3000.f); |
| 633 | } |
| 634 | |
| 635 | SkMatrix mats[kMatrixCount]; |
| 636 | for (int i = 0; i < kMatrixCount; ++i) { |
| 637 | for (int j = 0; j < 9; ++j) { |
| 638 | mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f)); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // identity |
| 643 | { |
| 644 | mat.reset(); |
| 645 | SkScalar dst[3*kTripleCount]; |
| 646 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 647 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3)); |
| 648 | } |
| 649 | |
| 650 | // zero matrix |
| 651 | { |
| 652 | mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f); |
| 653 | SkScalar dst[3*kTripleCount]; |
| 654 | mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 655 | SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 656 | for (int i = 0; i < kTripleCount; ++i) { |
| 657 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3)); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // zero point |
| 662 | { |
| 663 | SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 664 | for (int i = 0; i < kMatrixCount; ++i) { |
| 665 | SkScalar dst[3]; |
| 666 | mats[i].mapHomogeneousPoints(dst, zeros, 1); |
| 667 | REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3)); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // doesn't crash with null dst, src, count == 0 |
| 672 | { |
| 673 | mats[0].mapHomogeneousPoints(NULL, NULL, 0); |
| 674 | } |
| 675 | |
| 676 | // uniform scale of point |
| 677 | { |
| 678 | mat.setScale(kScale0, kScale0); |
| 679 | SkScalar dst[3]; |
| 680 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 681 | SkPoint pnt; |
| 682 | pnt.set(src[0], src[1]); |
| 683 | mat.mapHomogeneousPoints(dst, src, 1); |
| 684 | mat.mapPoints(&pnt, &pnt, 1); |
| 685 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 686 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 687 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 688 | } |
| 689 | |
| 690 | // rotation of point |
| 691 | { |
| 692 | mat.setRotate(kRotation0); |
| 693 | SkScalar dst[3]; |
| 694 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 695 | SkPoint pnt; |
| 696 | pnt.set(src[0], src[1]); |
| 697 | mat.mapHomogeneousPoints(dst, src, 1); |
| 698 | mat.mapPoints(&pnt, &pnt, 1); |
| 699 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 700 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 701 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 702 | } |
| 703 | |
| 704 | // rotation, scale, rotation of point |
| 705 | { |
| 706 | mat.setRotate(kRotation1); |
| 707 | mat.postScale(kScale0, kScale0); |
| 708 | mat.postRotate(kRotation0); |
| 709 | SkScalar dst[3]; |
| 710 | SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 711 | SkPoint pnt; |
| 712 | pnt.set(src[0], src[1]); |
| 713 | mat.mapHomogeneousPoints(dst, src, 1); |
| 714 | mat.mapPoints(&pnt, &pnt, 1); |
| 715 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 716 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 717 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 718 | } |
| 719 | |
| 720 | // compare with naive approach |
| 721 | { |
| 722 | for (int i = 0; i < kMatrixCount; ++i) { |
| 723 | for (int j = 0; j < kTripleCount; ++j) { |
| 724 | SkScalar dst[3]; |
| 725 | mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1); |
| 726 | REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst)); |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | } |
| 732 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 733 | static void TestMatrix(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 734 | SkMatrix mat, inverse, iden1, iden2; |
| 735 | |
| 736 | mat.reset(); |
| 737 | mat.setTranslate(SK_Scalar1, SK_Scalar1); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 738 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 739 | iden1.setConcat(mat, inverse); |
| 740 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 741 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 742 | mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 743 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 744 | iden1.setConcat(mat, inverse); |
| 745 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 746 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 747 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 748 | mat.setScale(SK_Scalar1/2, SkIntToScalar(2)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 749 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 750 | iden1.setConcat(mat, inverse); |
| 751 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 752 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 753 | |
| 754 | mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0); |
| 755 | mat.postRotate(SkIntToScalar(25)); |
| 756 | REPORTER_ASSERT(reporter, mat.invert(NULL)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 757 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 758 | iden1.setConcat(mat, inverse); |
| 759 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 760 | iden2.setConcat(inverse, mat); |
| 761 | REPORTER_ASSERT(reporter, is_identity(iden2)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 762 | test_flatten(reporter, mat); |
| 763 | test_flatten(reporter, iden2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 764 | |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 765 | mat.setScale(0, SK_Scalar1); |
reed@google.com | e40591d | 2013-01-30 15:47:42 +0000 | [diff] [blame] | 766 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 767 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 768 | mat.setScale(SK_Scalar1, 0); |
reed@google.com | e40591d | 2013-01-30 15:47:42 +0000 | [diff] [blame] | 769 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
reed@google.com | 2fb96cc | 2013-01-04 17:02:33 +0000 | [diff] [blame] | 770 | REPORTER_ASSERT(reporter, !mat.invert(&inverse)); |
| 771 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 772 | // rectStaysRect test |
| 773 | { |
| 774 | static const struct { |
| 775 | SkScalar m00, m01, m10, m11; |
| 776 | bool mStaysRect; |
| 777 | } |
| 778 | gRectStaysRectSamples[] = { |
| 779 | { 0, 0, 0, 0, false }, |
| 780 | { 0, 0, 0, SK_Scalar1, false }, |
| 781 | { 0, 0, SK_Scalar1, 0, false }, |
| 782 | { 0, 0, SK_Scalar1, SK_Scalar1, false }, |
| 783 | { 0, SK_Scalar1, 0, 0, false }, |
| 784 | { 0, SK_Scalar1, 0, SK_Scalar1, false }, |
| 785 | { 0, SK_Scalar1, SK_Scalar1, 0, true }, |
| 786 | { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }, |
| 787 | { SK_Scalar1, 0, 0, 0, false }, |
| 788 | { SK_Scalar1, 0, 0, SK_Scalar1, true }, |
| 789 | { SK_Scalar1, 0, SK_Scalar1, 0, false }, |
| 790 | { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false }, |
| 791 | { SK_Scalar1, SK_Scalar1, 0, 0, false }, |
| 792 | { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false }, |
| 793 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false }, |
| 794 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false } |
| 795 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 796 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 797 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) { |
| 798 | SkMatrix m; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 799 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 800 | m.reset(); |
| 801 | m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00); |
| 802 | m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01); |
| 803 | m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10); |
| 804 | m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11); |
| 805 | REPORTER_ASSERT(reporter, |
| 806 | m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect); |
| 807 | } |
| 808 | } |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 809 | |
bungeman@google.com | ba7983e | 2011-07-13 20:18:16 +0000 | [diff] [blame] | 810 | mat.reset(); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 811 | mat.set(SkMatrix::kMScaleX, SkIntToScalar(1)); |
| 812 | mat.set(SkMatrix::kMSkewX, SkIntToScalar(2)); |
| 813 | mat.set(SkMatrix::kMTransX, SkIntToScalar(3)); |
| 814 | mat.set(SkMatrix::kMSkewY, SkIntToScalar(4)); |
| 815 | mat.set(SkMatrix::kMScaleY, SkIntToScalar(5)); |
| 816 | mat.set(SkMatrix::kMTransY, SkIntToScalar(6)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 817 | SkScalar affine[6]; |
| 818 | REPORTER_ASSERT(reporter, mat.asAffine(affine)); |
| 819 | |
| 820 | #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e) |
| 821 | REPORTER_ASSERT(reporter, affineEqual(ScaleX)); |
| 822 | REPORTER_ASSERT(reporter, affineEqual(SkewY)); |
| 823 | REPORTER_ASSERT(reporter, affineEqual(SkewX)); |
| 824 | REPORTER_ASSERT(reporter, affineEqual(ScaleY)); |
| 825 | REPORTER_ASSERT(reporter, affineEqual(TransX)); |
| 826 | REPORTER_ASSERT(reporter, affineEqual(TransY)); |
| 827 | #undef affineEqual |
| 828 | |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 829 | mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 830 | REPORTER_ASSERT(reporter, !mat.asAffine(affine)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 831 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 832 | SkMatrix mat2; |
| 833 | mat2.reset(); |
| 834 | mat.reset(); |
| 835 | SkScalar zero = 0; |
| 836 | mat.set(SkMatrix::kMSkewX, -zero); |
| 837 | REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 838 | |
| 839 | mat2.reset(); |
| 840 | mat.reset(); |
| 841 | mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 842 | mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
bsalomon@google.com | 9ed2ecd | 2012-03-26 15:57:37 +0000 | [diff] [blame] | 843 | // fixed pt doesn't have the property that NaN does not equal itself. |
| 844 | #ifdef SK_SCALAR_IS_FIXED |
| 845 | REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 846 | #else |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 847 | REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
bsalomon@google.com | 9ed2ecd | 2012-03-26 15:57:37 +0000 | [diff] [blame] | 848 | #endif |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 849 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 850 | test_matrix_max_stretch(reporter); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 851 | test_matrix_is_similarity(reporter); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 852 | test_matrix_recttorect(reporter); |
commit-bot@chromium.org | 08284e4 | 2013-07-24 18:08:08 +0000 | [diff] [blame] | 853 | test_matrix_decomposition(reporter); |
egdaniel@google.com | 259fbaf | 2013-08-15 21:12:11 +0000 | [diff] [blame^] | 854 | test_matrix_homogeneous(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 855 | } |
| 856 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 857 | #include "TestClassDef.h" |
| 858 | DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) |