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