caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +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 | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "Intersection_Tests.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 8 | #include "LineParameters.h" |
| 9 | |
| 10 | |
| 11 | // tests to verify that distance calculations are coded correctly |
| 12 | const Cubic tests[] = { |
| 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}}, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 22 | {{0, PointEpsilon * 2}, {1, 0}, {2, 0}, {3, 0}}, |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | 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.333333333e-06, 6.666666667e-07}, |
| 36 | }; |
| 37 | |
| 38 | const size_t tests_count = sizeof(tests) / sizeof(tests[0]); |
| 39 | |
| 40 | static size_t firstLineParameterTest = 0; |
| 41 | |
| 42 | void LineParameter_Test() { |
| 43 | for (size_t index = firstLineParameterTest; index < tests_count; ++index) { |
| 44 | LineParameters lineParameters; |
| 45 | const Cubic& cubic = tests[index]; |
| 46 | lineParameters.cubicEndPoints(cubic); |
| 47 | double denormalizedDistance[2]; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 48 | denormalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1); |
| 49 | denormalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 50 | double normalSquared = lineParameters.normalSquared(); |
| 51 | size_t inner; |
| 52 | for (inner = 0; inner < 2; ++inner) { |
| 53 | double distSq = denormalizedDistance[inner]; |
| 54 | distSq *= distSq; |
| 55 | double answersSq = answers[index][inner]; |
| 56 | answersSq *= answersSq; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 57 | if (AlmostEqualUlps(distSq, normalSquared * answersSq)) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 58 | continue; |
| 59 | } |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 60 | SkDebugf("%s [%d,%d] denormalizedDistance:%g != answer:%g" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 61 | " distSq:%g answerSq:%g normalSquared:%g\n", |
| 62 | __FUNCTION__, (int)index, (int)inner, |
| 63 | denormalizedDistance[inner], answers[index][inner], |
| 64 | distSq, answersSq, normalSquared); |
| 65 | } |
| 66 | lineParameters.normalize(); |
| 67 | double normalizedDistance[2]; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 68 | normalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1); |
| 69 | normalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 70 | for (inner = 0; inner < 2; ++inner) { |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 71 | if (AlmostEqualUlps(fabs(normalizedDistance[inner]), answers[index][inner])) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 72 | continue; |
| 73 | } |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 74 | SkDebugf("%s [%d,%d] normalizedDistance:%1.10g != answer:%g\n", |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 75 | __FUNCTION__, (int)index, (int)inner, |
| 76 | normalizedDistance[inner], answers[index][inner]); |
| 77 | } |
| 78 | } |
| 79 | } |