blob: 40e046cff59ed0fddac2a897a90d67561afb1784 [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001#include "DataTypes.h"
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002#include "Intersections.h"
caryclark@google.com639df892012-01-10 21:46:10 +00003#include "LineIntersection.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00004#include <algorithm> // used for std::swap
caryclark@google.com639df892012-01-10 21:46:10 +00005
6
caryclark@google.com639df892012-01-10 21:46:10 +00007/*
8 Determine the intersection point of two line segments
9 Return FALSE if the lines don't intersect
10 from: http://paulbourke.net/geometry/lineline2d/
caryclark@google.com639df892012-01-10 21:46:10 +000011*/
12
caryclark@google.comc6825902012-02-03 22:07:47 +000013int intersect(const _Line& a, const _Line& b, double aRange[2], double bRange[2]) {
caryclark@google.com639df892012-01-10 21:46:10 +000014 double axLen = a[1].x - a[0].x;
15 double ayLen = a[1].y - a[0].y;
16 double bxLen = b[1].x - b[0].x;
17 double byLen = b[1].y - b[0].y;
caryclark@google.comc6825902012-02-03 22:07:47 +000018 /* Slopes match when denom goes to zero:
19 axLen / ayLen == bxLen / byLen
20 (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen
21 byLen * axLen == ayLen * bxLen
22 byLen * axLen - ayLen * bxLen == 0 ( == denom )
23 */
caryclark@google.com639df892012-01-10 21:46:10 +000024 double denom = byLen * axLen - ayLen * bxLen;
caryclark@google.comc6825902012-02-03 22:07:47 +000025 if (approximately_zero_squared(denom)) {
26 /* See if the axis intercepts match:
27 ay - ax * ayLen / axLen == by - bx * ayLen / axLen
28 axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen)
29 axLen * ay - ax * ayLen == axLen * by - bx * ayLen
30 */
31 if (approximately_equal_squared(axLen * a[0].y - ayLen * a[0].x,
32 axLen * b[0].y - ayLen * b[0].x)) {
33 const double* aPtr;
34 const double* bPtr;
35 if (fabs(axLen) > fabs(ayLen) || fabs(bxLen) > fabs(byLen)) {
36 aPtr = &a[0].x;
37 bPtr = &b[0].x;
caryclark@google.com639df892012-01-10 21:46:10 +000038 } else {
caryclark@google.comc6825902012-02-03 22:07:47 +000039 aPtr = &a[0].y;
40 bPtr = &b[0].y;
caryclark@google.com639df892012-01-10 21:46:10 +000041 }
caryclark@google.comc6825902012-02-03 22:07:47 +000042 double aMin = aPtr[0];
43 double aMax = aPtr[2];
44 double bMin = bPtr[0];
45 double bMax = bPtr[2];
46 if (aMin > aMax) {
47 std::swap(aMin, aMax);
caryclark@google.com639df892012-01-10 21:46:10 +000048 }
caryclark@google.comc6825902012-02-03 22:07:47 +000049 if (bMin > bMax) {
50 std::swap(bMin, bMax);
51 }
52 if (aMax < bMin || bMax < aMin) {
53 return 0;
54 }
55 if (aRange) {
56 aRange[0] = bMin <= aMin ? 0 : (bMin - aMin) / (aMax - aMin);
57 aRange[1] = bMax >= aMax ? 1 : (bMax - aMin) / (aMax - aMin);
58 }
59 if (bRange) {
60 bRange[0] = aMin <= bMin ? 0 : (aMin - bMin) / (bMax - bMin);
61 bRange[1] = aMax >= bMax ? 1 : (aMax - bMin) / (bMax - bMin);
62 }
caryclark@google.com4917f172012-03-05 22:01:21 +000063 return 1 + ((aRange[0] != aRange[1]) || (bRange[0] != bRange[1]));
caryclark@google.com639df892012-01-10 21:46:10 +000064 }
caryclark@google.com639df892012-01-10 21:46:10 +000065 }
66 double ab0y = a[0].y - b[0].y;
67 double ab0x = a[0].x - b[0].x;
68 double numerA = ab0y * bxLen - byLen * ab0x;
caryclark@google.com639df892012-01-10 21:46:10 +000069 if (numerA < 0 && denom > numerA || numerA > 0 && denom < numerA) {
caryclark@google.comc6825902012-02-03 22:07:47 +000070 return 0;
caryclark@google.com639df892012-01-10 21:46:10 +000071 }
caryclark@google.comc6825902012-02-03 22:07:47 +000072 double numerB = ab0y * axLen - ayLen * ab0x;
caryclark@google.com639df892012-01-10 21:46:10 +000073 if (numerB < 0 && denom > numerB || numerB > 0 && denom < numerB) {
caryclark@google.comc6825902012-02-03 22:07:47 +000074 return 0;
caryclark@google.com639df892012-01-10 21:46:10 +000075 }
caryclark@google.com639df892012-01-10 21:46:10 +000076 /* Is the intersection along the the segments */
caryclark@google.comc6825902012-02-03 22:07:47 +000077 if (aRange) {
78 aRange[0] = numerA / denom;
79 }
80 if (bRange) {
81 bRange[0] = numerB / denom;
82 }
83 return 1;
caryclark@google.com639df892012-01-10 21:46:10 +000084}
85
caryclark@google.comc6825902012-02-03 22:07:47 +000086int horizontalIntersect(const _Line& line, double y, double tRange[2]) {
87 double min = line[0].y;
88 double max = line[1].y;
89 if (min > max) {
90 std::swap(min, max);
91 }
92 if (min > y || max < y) {
93 return 0;
94 }
95 if (approximately_equal(min, max)) {
96 tRange[0] = 0;
97 tRange[1] = 1;
98 return 2;
99 }
100 tRange[0] = (y - line[0].y) / (line[1].y - line[0].y);
101 return 1;
102}
caryclark@google.com639df892012-01-10 21:46:10 +0000103
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000104// OPTIMIZATION Given: dy = line[1].y - line[0].y
105// and: xIntercept / (y - line[0].y) == (line[1].x - line[0].x) / dy
106// then: xIntercept * dy == (line[1].x - line[0].x) * (y - line[0].y)
107// Assuming that dy is always > 0, the line segment intercepts if:
108// left * dy <= xIntercept * dy <= right * dy
109// thus: left * dy <= (line[1].x - line[0].x) * (y - line[0].y) <= right * dy
110// (clever as this is, it does not give us the t value, so may be useful only
111// as a quick reject -- and maybe not then; it takes 3 muls, 3 adds, 2 cmps)
112int horizontalLineIntersect(const _Line& line, double left, double right,
113 double y, double tRange[2]) {
114 int result = horizontalIntersect(line, y, tRange);
115 if (result != 1) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000116 // FIXME: this is incorrect if result == 2
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000117 return result;
118 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000119 double xIntercept = line[0].x + tRange[0] * (line[1].x - line[0].x);
120 if (xIntercept > right || xIntercept < left) {
121 return 0;
122 }
123 return result;
124}
125
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000126int horizontalIntersect(const _Line& line, double left, double right,
127 double y, bool flipped, Intersections& intersections) {
128 int result = horizontalIntersect(line, y, intersections.fT[0]);
129 switch (result) {
130 case 0:
131 break;
132 case 1: {
133 double xIntercept = line[0].x + intersections.fT[0][0]
134 * (line[1].x - line[0].x);
135 if (xIntercept > right || xIntercept < left) {
136 return 0;
137 }
138 intersections.fT[1][0] = (xIntercept - left) / (right - left);
139 break;
140 }
141 case 2:
142 double lineL = line[0].x;
143 double lineR = line[1].x;
144 if (lineL > lineR) {
145 std::swap(lineL, lineR);
146 }
147 double overlapL = std::max(left, lineL);
148 double overlapR = std::min(right, lineR);
149 if (overlapL > overlapR) {
150 return 0;
151 }
152 if (overlapL == overlapR) {
153 result = 1;
154 }
155 intersections.fT[0][0] = (overlapL - line[0].x) / (line[1].x - line[0].x);
156 intersections.fT[1][0] = (overlapL - left) / (right - left);
157 if (result > 1) {
158 intersections.fT[0][1] = (overlapR - line[0].x) / (line[1].x - line[0].x);
159 intersections.fT[1][1] = (overlapR - left) / (right - left);
160 }
161 break;
162 }
163 if (flipped) {
164 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
165 for (int index = 0; index < result; ++index) {
166 intersections.fT[1][index] = 1 - intersections.fT[1][index];
167 }
168 }
169 return result;
170}
171
172int verticalIntersect(const _Line& line, double x, double tRange[2]) {
173 double min = line[0].x;
174 double max = line[1].x;
175 if (min > max) {
176 std::swap(min, max);
177 }
178 if (min > x || max < x) {
179 return 0;
180 }
181 if (approximately_equal(min, max)) {
182 tRange[0] = 0;
183 tRange[1] = 1;
184 return 2;
185 }
186 tRange[0] = (x - line[0].x) / (line[1].x - line[0].x);
187 return 1;
188}
189
190int verticalIntersect(const _Line& line, double top, double bottom,
191 double x, bool flipped, Intersections& intersections) {
192 int result = verticalIntersect(line, x, intersections.fT[0]);
193 switch (result) {
194 case 0:
195 break;
196 case 1: {
197 double yIntercept = line[0].y + intersections.fT[0][0]
198 * (line[1].y - line[0].y);
199 if (yIntercept > bottom || yIntercept < top) {
200 return 0;
201 }
202 intersections.fT[1][0] = (yIntercept - top) / (bottom - top);
203 break;
204 }
205 case 2:
206 double lineT = line[0].y;
207 double lineB = line[1].y;
208 if (lineT > lineB) {
209 std::swap(lineT, lineB);
210 }
211 double overlapT = std::max(top, lineT);
212 double overlapB = std::min(bottom, lineB);
213 if (overlapT > overlapB) {
214 return 0;
215 }
216 if (overlapT == overlapB) {
217 result = 1;
218 }
219 intersections.fT[0][0] = (overlapT - line[0].y) / (line[1].y - line[0].y);
220 intersections.fT[1][0] = (overlapT - top) / (bottom - top);
221 if (result > 1) {
222 intersections.fT[0][1] = (overlapB - line[0].y) / (line[1].y - line[0].y);
223 intersections.fT[1][1] = (overlapB - top) / (bottom - top);
224 }
225 break;
226 }
227 if (flipped) {
228 // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
229 for (int index = 0; index < result; ++index) {
230 intersections.fT[1][index] = 1 - intersections.fT[1][index];
231 }
232 }
233 return result;
234}
235
caryclark@google.com639df892012-01-10 21:46:10 +0000236// from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py
237// 4 subs, 2 muls, 1 cmp
238static bool ccw(const _Point& A, const _Point& B, const _Point& C) {
239 return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x);
240}
241
242// 16 subs, 8 muls, 6 cmps
243bool testIntersect(const _Line& a, const _Line& b) {
244 return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1])
245 && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]);
246}