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