OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "TestClassDef.h" | 9 #include "TestClassDef.h" |
10 #include "SkMath.h" | 10 #include "SkMath.h" |
11 #include "SkMatrix.h" | 11 #include "SkMatrix.h" |
12 #include "SkMatrixUtils.h" | 12 #include "SkMatrixUtils.h" |
13 #include "SkRandom.h" | 13 #include "SkRandom.h" |
14 | 14 |
15 static bool nearly_equal_scalar(SkScalar a, SkScalar b) { | 15 static bool nearly_equal_scalar(SkScalar a, SkScalar b) { |
16 // Note that we get more compounded error for multiple operations when | |
17 // SK_SCALAR_IS_FIXED. | |
18 #ifdef SK_SCALAR_IS_FLOAT | |
19 const SkScalar tolerance = SK_Scalar1 / 200000; | 16 const SkScalar tolerance = SK_Scalar1 / 200000; |
20 #else | |
21 const SkScalar tolerance = SK_Scalar1 / 1024; | |
22 #endif | |
23 | |
24 return SkScalarAbs(a - b) <= tolerance; | 17 return SkScalarAbs(a - b) <= tolerance; |
25 } | 18 } |
26 | 19 |
27 static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) { | 20 static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) { |
28 for (int i = 0; i < 9; i++) { | 21 for (int i = 0; i < 9; i++) { |
29 if (!nearly_equal_scalar(a[i], b[i])) { | 22 if (!nearly_equal_scalar(a[i], b[i])) { |
30 SkDebugf("not equal %g %g\n", (float)a[i], (float)b[i]); | 23 SkDebugf("not equal %g %g\n", (float)a[i], (float)b[i]); |
31 return false; | 24 return false; |
32 } | 25 } |
33 } | 26 } |
34 return true; | 27 return true; |
35 } | 28 } |
36 | 29 |
37 static bool are_equal(skiatest::Reporter* reporter, | 30 static bool are_equal(skiatest::Reporter* reporter, |
38 const SkMatrix& a, | 31 const SkMatrix& a, |
39 const SkMatrix& b) { | 32 const SkMatrix& b) { |
40 bool equal = a == b; | 33 bool equal = a == b; |
41 bool cheapEqual = a.cheapEqualTo(b); | 34 bool cheapEqual = a.cheapEqualTo(b); |
42 if (equal != cheapEqual) { | 35 if (equal != cheapEqual) { |
43 #ifdef SK_SCALAR_IS_FLOAT | |
44 if (equal) { | 36 if (equal) { |
45 bool foundZeroSignDiff = false; | 37 bool foundZeroSignDiff = false; |
46 for (int i = 0; i < 9; ++i) { | 38 for (int i = 0; i < 9; ++i) { |
47 float aVal = a.get(i); | 39 float aVal = a.get(i); |
48 float bVal = b.get(i); | 40 float bVal = b.get(i); |
49 int aValI = *SkTCast<int*>(&aVal); | 41 int aValI = *SkTCast<int*>(&aVal); |
50 int bValI = *SkTCast<int*>(&bVal); | 42 int bValI = *SkTCast<int*>(&bVal); |
51 if (0 == aVal && 0 == bVal && aValI != bValI) { | 43 if (0 == aVal && 0 == bVal && aValI != bValI) { |
52 foundZeroSignDiff = true; | 44 foundZeroSignDiff = true; |
53 } else { | 45 } else { |
54 REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI); | 46 REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI); |
55 } | 47 } |
56 } | 48 } |
57 REPORTER_ASSERT(reporter, foundZeroSignDiff); | 49 REPORTER_ASSERT(reporter, foundZeroSignDiff); |
58 } else { | 50 } else { |
59 bool foundNaN = false; | 51 bool foundNaN = false; |
60 for (int i = 0; i < 9; ++i) { | 52 for (int i = 0; i < 9; ++i) { |
61 float aVal = a.get(i); | 53 float aVal = a.get(i); |
62 float bVal = b.get(i); | 54 float bVal = b.get(i); |
63 int aValI = *SkTCast<int*>(&aVal); | 55 int aValI = *SkTCast<int*>(&aVal); |
64 int bValI = *SkTCast<int*>(&bVal); | 56 int bValI = *SkTCast<int*>(&bVal); |
65 if (sk_float_isnan(aVal) && aValI == bValI) { | 57 if (sk_float_isnan(aVal) && aValI == bValI) { |
66 foundNaN = true; | 58 foundNaN = true; |
67 } else { | 59 } else { |
68 REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI); | 60 REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI); |
69 } | 61 } |
70 } | 62 } |
71 REPORTER_ASSERT(reporter, foundNaN); | 63 REPORTER_ASSERT(reporter, foundNaN); |
72 } | 64 } |
73 #else | |
74 REPORTER_ASSERT(reporter, false); | |
75 #endif | |
76 } | 65 } |
77 return equal; | 66 return equal; |
78 } | 67 } |
79 | 68 |
80 static bool is_identity(const SkMatrix& m) { | 69 static bool is_identity(const SkMatrix& m) { |
81 SkMatrix identity; | 70 SkMatrix identity; |
82 identity.reset(); | 71 identity.reset(); |
83 return nearly_equal(m, identity); | 72 return nearly_equal(m, identity); |
84 } | 73 } |
85 | 74 |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 // perspective x | 281 // perspective x |
293 mat.reset(); | 282 mat.reset(); |
294 mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2)); | 283 mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2)); |
295 REPORTER_ASSERT(reporter, !mat.isSimilarity()); | 284 REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
296 | 285 |
297 // perspective y | 286 // perspective y |
298 mat.reset(); | 287 mat.reset(); |
299 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2)); | 288 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2)); |
300 REPORTER_ASSERT(reporter, !mat.isSimilarity()); | 289 REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
301 | 290 |
302 #ifdef SK_SCALAR_IS_FLOAT | |
303 /* We bypass the following tests for SK_SCALAR_IS_FIXED build. | |
304 * The long discussion can be found in this issue: | |
305 * http://codereview.appspot.com/5999050/ | |
306 * In short, we haven't found a perfect way to fix the precision | |
307 * issue, i.e. the way we use tolerance in isSimilarityTransformation | |
308 * is incorrect. The situation becomes worse in fixed build, so | |
309 * we disabled rotation related tests for fixed build. | |
310 */ | |
311 | |
312 // rotate | 291 // rotate |
313 for (int angle = 0; angle < 360; ++angle) { | 292 for (int angle = 0; angle < 360; ++angle) { |
314 mat.reset(); | 293 mat.reset(); |
315 mat.setRotate(SkIntToScalar(angle)); | 294 mat.setRotate(SkIntToScalar(angle)); |
316 REPORTER_ASSERT(reporter, mat.isSimilarity()); | 295 REPORTER_ASSERT(reporter, mat.isSimilarity()); |
317 } | 296 } |
318 | 297 |
319 // see if there are any accumulated precision issues | 298 // see if there are any accumulated precision issues |
320 mat.reset(); | 299 mat.reset(); |
321 for (int i = 1; i < 360; i++) { | 300 for (int i = 1; i < 360; i++) { |
(...skipping 11 matching lines...) Expand all Loading... |
333 mat.reset(); | 312 mat.reset(); |
334 mat.setRotate(SkIntToScalar(30)); | 313 mat.setRotate(SkIntToScalar(30)); |
335 mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); | 314 mat.postScale(SkIntToScalar(2), SkIntToScalar(2)); |
336 REPORTER_ASSERT(reporter, mat.isSimilarity()); | 315 REPORTER_ASSERT(reporter, mat.isSimilarity()); |
337 | 316 |
338 // rotate + non-uniform scale | 317 // rotate + non-uniform scale |
339 mat.reset(); | 318 mat.reset(); |
340 mat.setRotate(SkIntToScalar(30)); | 319 mat.setRotate(SkIntToScalar(30)); |
341 mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); | 320 mat.postScale(SkIntToScalar(3), SkIntToScalar(2)); |
342 REPORTER_ASSERT(reporter, !mat.isSimilarity()); | 321 REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
343 #endif | |
344 | 322 |
345 // all zero | 323 // all zero |
346 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); | 324 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0); |
347 REPORTER_ASSERT(reporter, !mat.isSimilarity()); | 325 REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
348 | 326 |
349 // all zero except perspective | 327 // all zero except perspective |
350 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1); | 328 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1); |
351 REPORTER_ASSERT(reporter, !mat.isSimilarity()); | 329 REPORTER_ASSERT(reporter, !mat.isSimilarity()); |
352 | 330 |
353 // scales zero, only skews | 331 // scales zero, only skews |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 mat2.reset(); | 771 mat2.reset(); |
794 mat.reset(); | 772 mat.reset(); |
795 SkScalar zero = 0; | 773 SkScalar zero = 0; |
796 mat.set(SkMatrix::kMSkewX, -zero); | 774 mat.set(SkMatrix::kMSkewX, -zero); |
797 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); | 775 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
798 | 776 |
799 mat2.reset(); | 777 mat2.reset(); |
800 mat.reset(); | 778 mat.reset(); |
801 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); | 779 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
802 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); | 780 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
803 // fixed pt doesn't have the property that NaN does not equal itself. | |
804 #ifdef SK_SCALAR_IS_FIXED | |
805 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); | |
806 #else | |
807 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); | 781 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
808 #endif | |
809 | 782 |
810 test_matrix_min_max_stretch(reporter); | 783 test_matrix_min_max_stretch(reporter); |
811 test_matrix_is_similarity(reporter); | 784 test_matrix_is_similarity(reporter); |
812 test_matrix_recttorect(reporter); | 785 test_matrix_recttorect(reporter); |
813 test_matrix_decomposition(reporter); | 786 test_matrix_decomposition(reporter); |
814 test_matrix_homogeneous(reporter); | 787 test_matrix_homogeneous(reporter); |
815 } | 788 } |
OLD | NEW |