caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1 | #include "Intersection_Tests.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 2 | #include "LineParameters.h" |
| 3 | |
| 4 | |
| 5 | // tests to verify that distance calculations are coded correctly |
| 6 | const Cubic tests[] = { |
| 7 | {{0, 0}, {1, 1}, {2, 2}, {0, 3}}, |
| 8 | {{0, 0}, {1, 1}, {2, 2}, {3, 0}}, |
| 9 | {{0, 0}, {5, 0}, {-2,4}, {3, 4}}, |
| 10 | {{0, 2}, {1, 0}, {2, 0}, {3, 0}}, |
| 11 | {{0, .2}, {1, 0}, {2, 0}, {3, 0}}, |
| 12 | {{0, .02}, {1, 0}, {2, 0}, {3, 0}}, |
| 13 | {{0, .002}, {1, 0}, {2, 0}, {3, 0}}, |
| 14 | {{0, .0002}, {1, 0}, {2, 0}, {3, 0}}, |
| 15 | {{0, .00002}, {1, 0}, {2, 0}, {3, 0}}, |
| 16 | {{0, PointEpsilon * 2}, {1, 0}, {2, 0}, {3, 0}}, |
| 17 | }; |
| 18 | |
| 19 | const double answers[][2] = { |
| 20 | {1, 2}, |
| 21 | {1, 2}, |
| 22 | {4, 4}, |
| 23 | {1.1094003924, 0.5547001962}, |
| 24 | {0.133038021, 0.06651901052}, |
| 25 | {0.0133330370, 0.006666518523}, |
| 26 | {0.001333333037, 0.0006666665185}, |
| 27 | {0.000133333333, 6.666666652e-05}, |
| 28 | {1.333333333e-05, 6.666666667e-06}, |
| 29 | {1.333333333e-06, 6.666666667e-07}, |
| 30 | }; |
| 31 | |
| 32 | const size_t tests_count = sizeof(tests) / sizeof(tests[0]); |
| 33 | |
| 34 | static size_t firstLineParameterTest = 0; |
| 35 | |
| 36 | void LineParameter_Test() { |
| 37 | for (size_t index = firstLineParameterTest; index < tests_count; ++index) { |
| 38 | LineParameters lineParameters; |
| 39 | const Cubic& cubic = tests[index]; |
| 40 | lineParameters.cubicEndPoints(cubic); |
| 41 | double denormalizedDistance[2]; |
| 42 | lineParameters.controlPtDistance(cubic, denormalizedDistance); |
| 43 | double normalSquared = lineParameters.normalSquared(); |
| 44 | size_t inner; |
| 45 | for (inner = 0; inner < 2; ++inner) { |
| 46 | double distSq = denormalizedDistance[inner]; |
| 47 | distSq *= distSq; |
| 48 | double answersSq = answers[index][inner]; |
| 49 | answersSq *= answersSq; |
| 50 | if (approximately_equal(distSq, normalSquared * answersSq)) { |
| 51 | continue; |
| 52 | } |
| 53 | printf("%s [%d,%d] denormalizedDistance:%g != answer:%g" |
| 54 | " distSq:%g answerSq:%g normalSquared:%g\n", |
| 55 | __FUNCTION__, (int)index, (int)inner, |
| 56 | denormalizedDistance[inner], answers[index][inner], |
| 57 | distSq, answersSq, normalSquared); |
| 58 | } |
| 59 | lineParameters.normalize(); |
| 60 | double normalizedDistance[2]; |
| 61 | lineParameters.controlPtDistance(cubic, normalizedDistance); |
| 62 | for (inner = 0; inner < 2; ++inner) { |
| 63 | if (approximately_equal(fabs(normalizedDistance[inner]), |
| 64 | answers[index][inner])) { |
| 65 | continue; |
| 66 | } |
| 67 | printf("%s [%d,%d] normalizedDistance:%1.10g != answer:%g\n", |
| 68 | __FUNCTION__, (int)index, (int)inner, |
| 69 | normalizedDistance[inner], answers[index][inner]); |
| 70 | } |
| 71 | } |
| 72 | } |