caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1 | #include "CurveUtilities.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2 | #include "Intersection_Tests.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 3 | #include "LineIntersection.h" |
| 4 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 5 | // FIXME: add tests for intersecting, non-intersecting, degenerate, coincident |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 6 | const _Line tests[][2] = { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | {{{0, 0}, {1, 0}}, {{1, 0}, {0, 0}}}, |
| 8 | {{{0, 0}, {0, 0}}, {{0, 0}, {1, 0}}}, |
| 9 | {{{0, 1}, {0, 1}}, {{0, 0}, {0, 2}}}, |
| 10 | {{{0, 0}, {1, 0}}, {{0, 0}, {2, 0}}}, |
| 11 | {{{1, 1}, {2, 2}}, {{0, 0}, {3, 3}}}, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 12 | {{{166.86950047022856, 112.69654129527828}, {166.86948801592692, 112.69655741235339}}, |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 13 | {{166.86960700313026, 112.6965477747386}, {166.86925794355412, 112.69656471103423}}} |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 14 | }; |
| 15 | |
| 16 | const size_t tests_count = sizeof(tests) / sizeof(tests[0]); |
| 17 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 18 | const _Line noIntersect[][2] = { |
| 19 | {{{0, 0}, {1, 0}}, {{3, 0}, {2, 0}}}, |
| 20 | {{{0, 0}, {0, 0}}, {{1, 0}, {2, 0}}}, |
| 21 | {{{0, 1}, {0, 1}}, {{0, 3}, {0, 2}}}, |
| 22 | {{{0, 0}, {1, 0}}, {{2, 0}, {3, 0}}}, |
| 23 | {{{1, 1}, {2, 2}}, {{4, 4}, {3, 3}}}, |
| 24 | }; |
| 25 | |
| 26 | const size_t noIntersect_count = sizeof(noIntersect) / sizeof(noIntersect[0]); |
| 27 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 28 | static size_t firstLineIntersectionTest = 0; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 29 | static size_t firstNoIntersectionTest = 0; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 30 | |
| 31 | void LineIntersection_Test() { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 32 | size_t index; |
| 33 | for (index = firstLineIntersectionTest; index < tests_count; ++index) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 34 | const _Line& line1 = tests[index][0]; |
| 35 | const _Line& line2 = tests[index][1]; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 36 | double t1[2], t2[2]; |
| 37 | int pts = intersect(line1, line2, t1, t2); |
| 38 | if (!pts) { |
| 39 | printf("%s [%zu] no intersection found\n", __FUNCTION__, index); |
| 40 | } |
| 41 | for (int i = 0; i < pts; ++i) { |
| 42 | _Point result1, result2; |
| 43 | xy_at_t(line1, t1[i], result1.x, result1.y); |
| 44 | xy_at_t(line2, t2[i], result2.x, result2.y); |
| 45 | if (!result1.approximatelyEqual(result2)) { |
| 46 | if (pts == 1) { |
| 47 | printf("%s [%zu] not equal\n", __FUNCTION__, index); |
| 48 | } else { |
| 49 | xy_at_t(line2, t2[i ^ 1], result2.x, result2.y); |
| 50 | if (!result1.approximatelyEqual(result2)) { |
| 51 | printf("%s [%zu] not equal\n", __FUNCTION__, index); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | for (index = firstNoIntersectionTest; index < noIntersect_count; ++index) { |
| 58 | const _Line& line1 = noIntersect[index][0]; |
| 59 | const _Line& line2 = noIntersect[index][1]; |
| 60 | double t1[2], t2[2]; |
| 61 | int pts = intersect(line1, line2, t1, t2); |
| 62 | if (pts) { |
| 63 | printf("%s [%zu] no intersection expected\n", __FUNCTION__, index); |
| 64 | } |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 65 | } |
| 66 | } |