caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 7 | #include "PathOpsTestCommon.h" |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 8 | #include "SkLineParameters.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | // tests to verify that distance calculations are coded correctly |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 12 | static const CubicPts tests[] = { |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 13 | {{{0, 0}, {1, 1}, {2, 2}, {0, 3}}}, |
| 14 | {{{0, 0}, {1, 1}, {2, 2}, {3, 0}}}, |
| 15 | {{{0, 0}, {5, 0}, {-2, 4}, {3, 4}}}, |
| 16 | {{{0, 2}, {1, 0}, {2, 0}, {3, 0}}}, |
| 17 | {{{0, .2}, {1, 0}, {2, 0}, {3, 0}}}, |
| 18 | {{{0, .02}, {1, 0}, {2, 0}, {3, 0}}}, |
| 19 | {{{0, .002}, {1, 0}, {2, 0}, {3, 0}}}, |
| 20 | {{{0, .0002}, {1, 0}, {2, 0}, {3, 0}}}, |
| 21 | {{{0, .00002}, {1, 0}, {2, 0}, {3, 0}}}, |
| 22 | {{{0, FLT_EPSILON * 2}, {1, 0}, {2, 0}, {3, 0}}}, |
| 23 | }; |
| 24 | |
| 25 | static const double answers[][2] = { |
| 26 | {1, 2}, |
| 27 | {1, 2}, |
| 28 | {4, 4}, |
| 29 | {1.1094003924, 0.5547001962}, |
| 30 | {0.133038021, 0.06651901052}, |
| 31 | {0.0133330370, 0.006666518523}, |
| 32 | {0.001333333037, 0.0006666665185}, |
| 33 | {0.000133333333, 6.666666652e-05}, |
| 34 | {1.333333333e-05, 6.666666667e-06}, |
| 35 | {1.5894571940104115e-07, 7.9472859700520577e-08}, |
| 36 | }; |
| 37 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 38 | static const size_t tests_count = SK_ARRAY_COUNT(tests); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 39 | |
tfarina@chromium.org | 78e7b4e | 2014-01-02 21:45:03 +0000 | [diff] [blame] | 40 | DEF_TEST(PathOpsLineParameters, reporter) { |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 41 | for (size_t index = 0; index < tests_count; ++index) { |
| 42 | SkLineParameters lineParameters; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 43 | const CubicPts& c = tests[index]; |
| 44 | SkDCubic cubic; |
| 45 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 46 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 47 | lineParameters.cubicEndPoints(cubic, 0, 3); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 48 | double denormalizedDistance[2]; |
| 49 | denormalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1); |
| 50 | denormalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2); |
| 51 | double normalSquared = lineParameters.normalSquared(); |
| 52 | size_t inner; |
| 53 | for (inner = 0; inner < 2; ++inner) { |
| 54 | double distSq = denormalizedDistance[inner]; |
| 55 | distSq *= distSq; |
| 56 | double answersSq = answers[index][inner]; |
| 57 | answersSq *= answersSq; |
| 58 | if (AlmostEqualUlps(distSq, normalSquared * answersSq)) { |
| 59 | continue; |
| 60 | } |
| 61 | SkDebugf("%s [%d,%d] denormalizedDistance:%g != answer:%g" |
| 62 | " distSq:%g answerSq:%g normalSquared:%g\n", |
| 63 | __FUNCTION__, static_cast<int>(index), (int)inner, |
| 64 | denormalizedDistance[inner], answers[index][inner], |
| 65 | distSq, answersSq, normalSquared); |
| 66 | } |
| 67 | lineParameters.normalize(); |
| 68 | double normalizedDistance[2]; |
| 69 | normalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1); |
| 70 | normalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2); |
| 71 | for (inner = 0; inner < 2; ++inner) { |
| 72 | if (AlmostEqualUlps(fabs(normalizedDistance[inner]), answers[index][inner])) { |
| 73 | continue; |
| 74 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 75 | SkDebugf("%s [%d,%d] normalizedDistance:%1.9g != answer:%g\n", |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 76 | __FUNCTION__, static_cast<int>(index), (int)inner, |
| 77 | normalizedDistance[inner], answers[index][inner]); |
| 78 | REPORTER_ASSERT(reporter, 0); |
| 79 | } |
| 80 | } |
| 81 | } |