blob: 283d9dfe4b83d54b4fa40d46ac11cf2af4e04dae [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
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.com8dcf1142012-07-02 20:27:02 +00007#include "CurveUtilities.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00008#include "Intersection_Tests.h"
caryclark@google.com639df892012-01-10 21:46:10 +00009#include "LineIntersection.h"
10
caryclark@google.com27accef2012-01-25 18:57:23 +000011// FIXME: add tests for intersecting, non-intersecting, degenerate, coincident
caryclark@google.com639df892012-01-10 21:46:10 +000012const _Line tests[][2] = {
caryclark@google.comc6825902012-02-03 22:07:47 +000013 {{{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.comd6176b02012-08-23 18:14:13 +000018 {{{166.86950047022856, 112.69654129527828}, {166.86948801592692, 112.69655741235339}},
caryclark@google.comc6825902012-02-03 22:07:47 +000019 {{166.86960700313026, 112.6965477747386}, {166.86925794355412, 112.69656471103423}}}
caryclark@google.com639df892012-01-10 21:46:10 +000020};
21
22const size_t tests_count = sizeof(tests) / sizeof(tests[0]);
23
caryclark@google.comc6825902012-02-03 22:07:47 +000024const _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
32const size_t noIntersect_count = sizeof(noIntersect) / sizeof(noIntersect[0]);
33
caryclark@google.com639df892012-01-10 21:46:10 +000034static size_t firstLineIntersectionTest = 0;
caryclark@google.comc6825902012-02-03 22:07:47 +000035static size_t firstNoIntersectionTest = 0;
caryclark@google.com639df892012-01-10 21:46:10 +000036
37void LineIntersection_Test() {
caryclark@google.comc6825902012-02-03 22:07:47 +000038 size_t index;
39 for (index = firstLineIntersectionTest; index < tests_count; ++index) {
caryclark@google.com639df892012-01-10 21:46:10 +000040 const _Line& line1 = tests[index][0];
41 const _Line& line2 = tests[index][1];
caryclark@google.com45a8fc62013-02-14 15:29:11 +000042 Intersections ts;
43 int pts = intersect(line1, line2, ts);
caryclark@google.comc6825902012-02-03 22:07:47 +000044 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;
caryclark@google.com45a8fc62013-02-14 15:29:11 +000049 xy_at_t(line1, ts.fT[0][i], result1.x, result1.y);
50 xy_at_t(line2, ts.fT[1][i], result2.x, result2.y);
caryclark@google.comc6825902012-02-03 22:07:47 +000051 if (!result1.approximatelyEqual(result2)) {
52 if (pts == 1) {
53 printf("%s [%zu] not equal\n", __FUNCTION__, index);
54 } else {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000055 xy_at_t(line2, ts.fT[1][i ^ 1], result2.x, result2.y);
caryclark@google.comc6825902012-02-03 22:07:47 +000056 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];
caryclark@google.com45a8fc62013-02-14 15:29:11 +000066 Intersections ts;
67 int pts = intersect(line1, line2, ts);
caryclark@google.comc6825902012-02-03 22:07:47 +000068 if (pts) {
69 printf("%s [%zu] no intersection expected\n", __FUNCTION__, index);
70 }
caryclark@google.com639df892012-01-10 21:46:10 +000071 }
72}