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" |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 12 | |
| 13 | static bool nearly_equal_scalar(SkScalar a, SkScalar b) { |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 14 | // Note that we get more compounded error for multiple operations when |
| 15 | // SK_SCALAR_IS_FIXED. |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | #ifdef SK_SCALAR_IS_FLOAT |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 17 | const SkScalar tolerance = SK_Scalar1 / 200000; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 18 | #else |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 19 | const SkScalar tolerance = SK_Scalar1 / 1024; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | return SkScalarAbs(a - b) <= tolerance; |
| 23 | } |
| 24 | |
| 25 | static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) { |
| 26 | for (int i = 0; i < 9; i++) { |
| 27 | if (!nearly_equal_scalar(a[i], b[i])) { |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 28 | 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] | 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 35 | static bool are_equal(skiatest::Reporter* reporter, |
| 36 | const SkMatrix& a, |
| 37 | const SkMatrix& b) { |
| 38 | bool equal = a == b; |
| 39 | bool cheapEqual = a.cheapEqualTo(b); |
| 40 | if (equal != cheapEqual) { |
| 41 | #if SK_SCALAR_IS_FLOAT |
bsalomon@google.com | 39d4f3a | 2012-03-26 17:25:45 +0000 | [diff] [blame] | 42 | if (equal) { |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 43 | bool foundZeroSignDiff = false; |
| 44 | for (int i = 0; i < 9; ++i) { |
| 45 | float aVal = a.get(i); |
| 46 | float bVal = b.get(i); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 47 | int aValI = *SkTCast<int*>(&aVal); |
| 48 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 49 | if (0 == aVal && 0 == bVal && aValI != bValI) { |
| 50 | foundZeroSignDiff = true; |
| 51 | } else { |
| 52 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI); |
| 53 | } |
| 54 | } |
| 55 | REPORTER_ASSERT(reporter, foundZeroSignDiff); |
| 56 | } else { |
| 57 | bool foundNaN = false; |
| 58 | for (int i = 0; i < 9; ++i) { |
| 59 | float aVal = a.get(i); |
| 60 | float bVal = b.get(i); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 61 | int aValI = *SkTCast<int*>(&aVal); |
| 62 | int bValI = *SkTCast<int*>(&bVal); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 63 | if (sk_float_isnan(aVal) && aValI == bValI) { |
| 64 | foundNaN = true; |
| 65 | } else { |
| 66 | REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI); |
| 67 | } |
| 68 | } |
| 69 | REPORTER_ASSERT(reporter, foundNaN); |
| 70 | } |
| 71 | #else |
| 72 | REPORTER_ASSERT(reporter, false); |
| 73 | #endif |
| 74 | } |
| 75 | return equal; |
| 76 | } |
| 77 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 78 | static bool is_identity(const SkMatrix& m) { |
| 79 | SkMatrix identity; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 80 | identity.reset(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 81 | return nearly_equal(m, identity); |
| 82 | } |
| 83 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 84 | static void test_matrix_recttorect(skiatest::Reporter* reporter) { |
| 85 | SkRect src, dst; |
| 86 | SkMatrix matrix; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 87 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 88 | src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10); |
| 89 | dst = src; |
| 90 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 91 | REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType()); |
| 92 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 93 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 94 | dst.offset(SK_Scalar1, SK_Scalar1); |
| 95 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 96 | REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType()); |
| 97 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 98 | |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 99 | dst.fRight += SK_Scalar1; |
| 100 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame^] | 101 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 102 | (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType()); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 104 | |
| 105 | dst = src; |
| 106 | dst.fRight = src.fRight * 2; |
| 107 | matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); |
| 108 | REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType()); |
| 109 | REPORTER_ASSERT(reporter, matrix.rectStaysRect()); |
| 110 | } |
| 111 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 112 | static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) { |
| 113 | // add 100 in case we have a bug, I don't want to kill my stack in the test |
| 114 | char buffer[SkMatrix::kMaxFlattenSize + 100]; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 115 | uint32_t size1 = m.writeToMemory(NULL); |
| 116 | uint32_t size2 = m.writeToMemory(buffer); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, size1 == size2); |
| 118 | REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 120 | SkMatrix m2; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 121 | uint32_t size3 = m2.readFromMemory(buffer); |
| 122 | REPORTER_ASSERT(reporter, size1 == size3); |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 124 | |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 125 | char buffer2[SkMatrix::kMaxFlattenSize + 100]; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 126 | size3 = m2.writeToMemory(buffer2); |
| 127 | REPORTER_ASSERT(reporter, size1 == size3); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 128 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
| 129 | } |
| 130 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 131 | static void test_matrix_max_stretch(skiatest::Reporter* reporter) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 132 | SkMatrix identity; |
| 133 | identity.reset(); |
| 134 | REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch()); |
| 135 | |
| 136 | SkMatrix scale; |
| 137 | scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4); |
| 138 | REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch()); |
| 139 | |
| 140 | SkMatrix rot90Scale; |
| 141 | rot90Scale.setRotate(90 * SK_Scalar1); |
| 142 | rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2); |
| 143 | REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch()); |
| 144 | |
| 145 | SkMatrix rotate; |
| 146 | rotate.setRotate(128 * SK_Scalar1); |
| 147 | REPORTER_ASSERT(reporter, SkScalarAbs(SK_Scalar1 - rotate.getMaxStretch()) <= SK_ScalarNearlyZero); |
| 148 | |
| 149 | SkMatrix translate; |
| 150 | translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1); |
| 151 | REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch()); |
| 152 | |
| 153 | SkMatrix perspX; |
| 154 | perspX.reset(); |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 155 | perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch()); |
| 157 | |
| 158 | SkMatrix perspY; |
| 159 | perspY.reset(); |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 160 | perspY.setPerspX(SkScalarToPersp(-SK_Scalar1 / 500)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 161 | REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch()); |
| 162 | |
| 163 | SkMatrix baseMats[] = {scale, rot90Scale, rotate, |
| 164 | translate, perspX, perspY}; |
| 165 | SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 166 | for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 167 | mats[i] = baseMats[i]; |
| 168 | bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); |
| 169 | REPORTER_ASSERT(reporter, invertable); |
| 170 | } |
| 171 | SkRandom rand; |
| 172 | for (int m = 0; m < 1000; ++m) { |
| 173 | SkMatrix mat; |
| 174 | mat.reset(); |
| 175 | for (int i = 0; i < 4; ++i) { |
| 176 | int x = rand.nextU() % SK_ARRAY_COUNT(mats); |
| 177 | mat.postConcat(mats[x]); |
| 178 | } |
| 179 | SkScalar stretch = mat.getMaxStretch(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 180 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 181 | if ((stretch < 0) != mat.hasPerspective()) { |
| 182 | stretch = mat.getMaxStretch(); |
| 183 | } |
| 184 | |
| 185 | REPORTER_ASSERT(reporter, (stretch < 0) == mat.hasPerspective()); |
| 186 | |
| 187 | if (mat.hasPerspective()) { |
| 188 | m -= 1; // try another non-persp matrix |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | // test a bunch of vectors. None should be scaled by more than stretch |
| 193 | // (modulo some error) and we should find a vector that is scaled by |
| 194 | // almost stretch. |
| 195 | static const SkScalar gStretchTol = (105 * SK_Scalar1) / 100; |
| 196 | static const SkScalar gMaxStretchTol = (97 * SK_Scalar1) / 100; |
| 197 | SkScalar max = 0; |
| 198 | SkVector vectors[1000]; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 199 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 200 | vectors[i].fX = rand.nextSScalar1(); |
| 201 | vectors[i].fY = rand.nextSScalar1(); |
| 202 | if (!vectors[i].normalize()) { |
| 203 | i -= 1; |
| 204 | continue; |
| 205 | } |
| 206 | } |
| 207 | mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors)); |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 208 | for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 209 | SkScalar d = vectors[i].length(); |
| 210 | REPORTER_ASSERT(reporter, SkScalarDiv(d, stretch) < gStretchTol); |
| 211 | if (max < d) { |
| 212 | max = d; |
| 213 | } |
| 214 | } |
| 215 | REPORTER_ASSERT(reporter, SkScalarDiv(max, stretch) >= gMaxStretchTol); |
| 216 | } |
| 217 | } |
| 218 | |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 219 | // This function is extracted from src/gpu/SkGpuDevice.cpp, |
| 220 | // in order to make sure this function works correctly. |
| 221 | static bool isSimilarityTransformation(const SkMatrix& matrix, |
| 222 | SkScalar tol = SK_ScalarNearlyZero) { |
| 223 | if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) { |
| 224 | return true; |
| 225 | } |
| 226 | if (matrix.hasPerspective()) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | SkScalar mx = matrix.get(SkMatrix::kMScaleX); |
| 231 | SkScalar sx = matrix.get(SkMatrix::kMSkewX); |
| 232 | SkScalar my = matrix.get(SkMatrix::kMScaleY); |
| 233 | SkScalar sy = matrix.get(SkMatrix::kMSkewY); |
| 234 | |
| 235 | if (mx == 0 && sx == 0 && my == 0 && sy == 0) { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | // it has scales or skews, but it could also be rotation, check it out. |
| 240 | SkVector vec[2]; |
| 241 | vec[0].set(mx, sx); |
| 242 | vec[1].set(sy, my); |
| 243 | |
| 244 | return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) && |
| 245 | SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(), |
| 246 | SkScalarSquare(tol)); |
| 247 | } |
| 248 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 249 | static void test_matrix_is_similarity_transform(skiatest::Reporter* reporter) { |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 250 | SkMatrix mat; |
| 251 | |
| 252 | // identity |
| 253 | mat.setIdentity(); |
| 254 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 255 | |
| 256 | // translation only |
| 257 | mat.reset(); |
| 258 | mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100)); |
| 259 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 260 | |
| 261 | // scale with same size |
| 262 | mat.reset(); |
| 263 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15)); |
| 264 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 265 | |
| 266 | // scale with one negative |
| 267 | mat.reset(); |
| 268 | mat.setScale(SkIntToScalar(-15), SkIntToScalar(15)); |
| 269 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 270 | |
| 271 | // scale with different size |
| 272 | mat.reset(); |
| 273 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20)); |
| 274 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 275 | |
| 276 | // scale with same size at a pivot point |
| 277 | mat.reset(); |
| 278 | mat.setScale(SkIntToScalar(15), SkIntToScalar(15), |
| 279 | SkIntToScalar(2), SkIntToScalar(2)); |
| 280 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 281 | |
| 282 | // scale with different size at a pivot point |
| 283 | mat.reset(); |
| 284 | mat.setScale(SkIntToScalar(15), SkIntToScalar(20), |
| 285 | SkIntToScalar(2), SkIntToScalar(2)); |
| 286 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 287 | |
| 288 | // skew with same size |
| 289 | mat.reset(); |
| 290 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15)); |
| 291 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 292 | |
| 293 | // skew with different size |
| 294 | mat.reset(); |
| 295 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20)); |
| 296 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 297 | |
| 298 | // skew with same size at a pivot point |
| 299 | mat.reset(); |
| 300 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(15), |
| 301 | SkIntToScalar(2), SkIntToScalar(2)); |
| 302 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 303 | |
| 304 | // skew with different size at a pivot point |
| 305 | mat.reset(); |
| 306 | mat.setSkew(SkIntToScalar(15), SkIntToScalar(20), |
| 307 | SkIntToScalar(2), SkIntToScalar(2)); |
| 308 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 309 | |
| 310 | // perspective x |
| 311 | mat.reset(); |
| 312 | mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2)); |
| 313 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 314 | |
| 315 | // perspective y |
| 316 | mat.reset(); |
| 317 | mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2)); |
| 318 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 319 | |
| 320 | #if SK_SCALAR_IS_FLOAT |
| 321 | /* We bypass the following tests for SK_SCALAR_IS_FIXED build. |
| 322 | * The long discussion can be found in this issue: |
| 323 | * http://codereview.appspot.com/5999050/ |
| 324 | * In short, we haven't found a perfect way to fix the precision |
| 325 | * issue, i.e. the way we use tolerance in isSimilarityTransformation |
| 326 | * is incorrect. The situation becomes worse in fixed build, so |
| 327 | * we disabled rotation related tests for fixed build. |
| 328 | */ |
| 329 | |
| 330 | // rotate |
| 331 | for (int angle = 0; angle < 360; ++angle) { |
| 332 | mat.reset(); |
| 333 | mat.setRotate(SkIntToScalar(angle)); |
| 334 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 335 | } |
| 336 | |
| 337 | // see if there are any accumulated precision issues |
| 338 | mat.reset(); |
| 339 | for (int i = 1; i < 360; i++) { |
| 340 | mat.postRotate(SkIntToScalar(1)); |
| 341 | } |
| 342 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 343 | |
| 344 | // rotate + translate |
| 345 | mat.reset(); |
| 346 | mat.setRotate(SkIntToScalar(30)); |
| 347 | mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20)); |
| 348 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 349 | |
| 350 | // rotate + uniform scale |
| 351 | mat.reset(); |
| 352 | mat.setRotate(SkIntToScalar(30)); |
| 353 | mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 354 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 355 | |
| 356 | // rotate + non-uniform scale |
| 357 | mat.reset(); |
| 358 | mat.setRotate(SkIntToScalar(30)); |
| 359 | mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); |
| 360 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 361 | #endif |
| 362 | |
| 363 | // all zero |
| 364 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 365 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 366 | |
| 367 | // all zero except perspective |
| 368 | mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1); |
| 369 | REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat)); |
| 370 | |
| 371 | // scales zero, only skews |
| 372 | mat.setAll(0, SK_Scalar1, 0, |
| 373 | SK_Scalar1, 0, 0, |
| 374 | 0, 0, SkMatrix::I()[8]); |
| 375 | REPORTER_ASSERT(reporter, isSimilarityTransformation(mat)); |
| 376 | } |
| 377 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 378 | static void TestMatrix(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 379 | SkMatrix mat, inverse, iden1, iden2; |
| 380 | |
| 381 | mat.reset(); |
| 382 | mat.setTranslate(SK_Scalar1, SK_Scalar1); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 383 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 384 | iden1.setConcat(mat, inverse); |
| 385 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 386 | |
| 387 | mat.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 388 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 389 | iden1.setConcat(mat, inverse); |
| 390 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 391 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 392 | |
| 393 | mat.setScale(SK_Scalar1/2, SK_Scalar1/2); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 394 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 395 | iden1.setConcat(mat, inverse); |
| 396 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 397 | test_flatten(reporter, mat); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 398 | |
| 399 | mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0); |
| 400 | mat.postRotate(SkIntToScalar(25)); |
| 401 | REPORTER_ASSERT(reporter, mat.invert(NULL)); |
reed@google.com | 5bfa55b | 2012-04-19 18:59:25 +0000 | [diff] [blame] | 402 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 403 | iden1.setConcat(mat, inverse); |
| 404 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 405 | iden2.setConcat(inverse, mat); |
| 406 | REPORTER_ASSERT(reporter, is_identity(iden2)); |
reed@android.com | 4b7577b | 2009-06-29 16:14:41 +0000 | [diff] [blame] | 407 | test_flatten(reporter, mat); |
| 408 | test_flatten(reporter, iden2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 409 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 410 | // rectStaysRect test |
| 411 | { |
| 412 | static const struct { |
| 413 | SkScalar m00, m01, m10, m11; |
| 414 | bool mStaysRect; |
| 415 | } |
| 416 | gRectStaysRectSamples[] = { |
| 417 | { 0, 0, 0, 0, false }, |
| 418 | { 0, 0, 0, SK_Scalar1, false }, |
| 419 | { 0, 0, SK_Scalar1, 0, false }, |
| 420 | { 0, 0, SK_Scalar1, SK_Scalar1, false }, |
| 421 | { 0, SK_Scalar1, 0, 0, false }, |
| 422 | { 0, SK_Scalar1, 0, SK_Scalar1, false }, |
| 423 | { 0, SK_Scalar1, SK_Scalar1, 0, true }, |
| 424 | { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }, |
| 425 | { SK_Scalar1, 0, 0, 0, false }, |
| 426 | { SK_Scalar1, 0, 0, SK_Scalar1, true }, |
| 427 | { SK_Scalar1, 0, SK_Scalar1, 0, false }, |
| 428 | { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false }, |
| 429 | { SK_Scalar1, SK_Scalar1, 0, 0, false }, |
| 430 | { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false }, |
| 431 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false }, |
| 432 | { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false } |
| 433 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 434 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 435 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) { |
| 436 | SkMatrix m; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 437 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 438 | m.reset(); |
| 439 | m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00); |
| 440 | m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01); |
| 441 | m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10); |
| 442 | m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11); |
| 443 | REPORTER_ASSERT(reporter, |
| 444 | m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect); |
| 445 | } |
| 446 | } |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 447 | |
bungeman@google.com | ba7983e | 2011-07-13 20:18:16 +0000 | [diff] [blame] | 448 | mat.reset(); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 449 | mat.set(SkMatrix::kMScaleX, SkIntToScalar(1)); |
| 450 | mat.set(SkMatrix::kMSkewX, SkIntToScalar(2)); |
| 451 | mat.set(SkMatrix::kMTransX, SkIntToScalar(3)); |
| 452 | mat.set(SkMatrix::kMSkewY, SkIntToScalar(4)); |
| 453 | mat.set(SkMatrix::kMScaleY, SkIntToScalar(5)); |
| 454 | mat.set(SkMatrix::kMTransY, SkIntToScalar(6)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 455 | SkScalar affine[6]; |
| 456 | REPORTER_ASSERT(reporter, mat.asAffine(affine)); |
| 457 | |
| 458 | #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e) |
| 459 | REPORTER_ASSERT(reporter, affineEqual(ScaleX)); |
| 460 | REPORTER_ASSERT(reporter, affineEqual(SkewY)); |
| 461 | REPORTER_ASSERT(reporter, affineEqual(SkewX)); |
| 462 | REPORTER_ASSERT(reporter, affineEqual(ScaleY)); |
| 463 | REPORTER_ASSERT(reporter, affineEqual(TransX)); |
| 464 | REPORTER_ASSERT(reporter, affineEqual(TransY)); |
| 465 | #undef affineEqual |
| 466 | |
bungeman@google.com | 07faed1 | 2011-10-07 21:55:56 +0000 | [diff] [blame] | 467 | mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2)); |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 468 | REPORTER_ASSERT(reporter, !mat.asAffine(affine)); |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 469 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 470 | SkMatrix mat2; |
| 471 | mat2.reset(); |
| 472 | mat.reset(); |
| 473 | SkScalar zero = 0; |
| 474 | mat.set(SkMatrix::kMSkewX, -zero); |
| 475 | REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 476 | |
| 477 | mat2.reset(); |
| 478 | mat.reset(); |
| 479 | mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
| 480 | mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
bsalomon@google.com | 9ed2ecd | 2012-03-26 15:57:37 +0000 | [diff] [blame] | 481 | // fixed pt doesn't have the property that NaN does not equal itself. |
| 482 | #ifdef SK_SCALAR_IS_FIXED |
| 483 | REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 484 | #else |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 485 | REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
bsalomon@google.com | 9ed2ecd | 2012-03-26 15:57:37 +0000 | [diff] [blame] | 486 | #endif |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 487 | |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 488 | test_matrix_max_stretch(reporter); |
bsalomon@google.com | 69afee1 | 2012-04-25 15:07:40 +0000 | [diff] [blame] | 489 | test_matrix_is_similarity_transform(reporter); |
reed@google.com | 97cd69c | 2012-10-12 14:35:48 +0000 | [diff] [blame] | 490 | test_matrix_recttorect(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 491 | } |
| 492 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 493 | #include "TestClassDef.h" |
| 494 | DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) |