blob: ca6a8e4081720bebcba53e9246c52103a09328d8 [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.coma3f05fa2012-06-01 17:44:28 +00007#include "CurveIntersection.h"
caryclark@google.comfa0588f2012-04-26 21:01:06 +00008#include "Intersections.h"
caryclark@google.com639df892012-01-10 21:46:10 +00009#include "LineIntersection.h"
caryclark@google.com45a8fc62013-02-14 15:29:11 +000010#include "LineUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +000011
caryclark@google.com73ca6242013-01-17 21:02:47 +000012/* Determine the intersection point of two lines. This assumes the lines are not parallel,
13 and that that the lines are infinite.
14 From http://en.wikipedia.org/wiki/Line-line_intersection
15 */
16void lineIntersect(const _Line& a, const _Line& b, _Point& p) {
17 double axLen = a[1].x - a[0].x;
18 double ayLen = a[1].y - a[0].y;
19 double bxLen = b[1].x - b[0].x;
20 double byLen = b[1].y - b[0].y;
21 double denom = byLen * axLen - ayLen * bxLen;
caryclark@google.comaa358312013-01-29 20:28:49 +000022 SkASSERT(denom);
caryclark@google.com73ca6242013-01-17 21:02:47 +000023 double term1 = a[1].x * a[0].y - a[1].y * a[0].x;
24 double term2 = b[1].x * b[0].y - b[1].y * b[0].x;
25 p.x = (term1 * bxLen - axLen * term2) / denom;
26 p.y = (term1 * byLen - ayLen * term2) / denom;
27}
caryclark@google.com639df892012-01-10 21:46:10 +000028
caryclark@google.com45a8fc62013-02-14 15:29:11 +000029static int computePoints(const _Line& a, int used, Intersections& i) {
30 i.fPt[0] = xy_at_t(a, i.fT[0][0]);
31 if ((i.fUsed = used) == 2) {
32 i.fPt[1] = xy_at_t(a, i.fT[0][1]);
33 }
34 return i.fUsed;
35}
36
caryclark@google.com639df892012-01-10 21:46:10 +000037/*
38 Determine the intersection point of two line segments
39 Return FALSE if the lines don't intersect
40 from: http://paulbourke.net/geometry/lineline2d/
caryclark@google.com73ca6242013-01-17 21:02:47 +000041 */
caryclark@google.com639df892012-01-10 21:46:10 +000042
caryclark@google.com45a8fc62013-02-14 15:29:11 +000043int intersect(const _Line& a, const _Line& b, Intersections& i) {
caryclark@google.com639df892012-01-10 21:46:10 +000044 double axLen = a[1].x - a[0].x;
45 double ayLen = a[1].y - a[0].y;
46 double bxLen = b[1].x - b[0].x;
47 double byLen = b[1].y - b[0].y;
rmistry@google.comd6176b02012-08-23 18:14:13 +000048 /* Slopes match when denom goes to zero:
caryclark@google.comc6825902012-02-03 22:07:47 +000049 axLen / ayLen == bxLen / byLen
50 (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen
51 byLen * axLen == ayLen * bxLen
52 byLen * axLen - ayLen * bxLen == 0 ( == denom )
53 */
caryclark@google.com73ca6242013-01-17 21:02:47 +000054 double denom = byLen * axLen - ayLen * bxLen;
caryclark@google.com639df892012-01-10 21:46:10 +000055 double ab0y = a[0].y - b[0].y;
56 double ab0x = a[0].x - b[0].x;
57 double numerA = ab0y * bxLen - byLen * ab0x;
caryclark@google.comc6825902012-02-03 22:07:47 +000058 double numerB = ab0y * axLen - ayLen * ab0x;
caryclark@google.comf9502d72013-02-04 14:06:49 +000059 bool mayNotOverlap = (numerA < 0 && denom > numerA) || (numerA > 0 && denom < numerA)
60 || (numerB < 0 && denom > numerB) || (numerB > 0 && denom < numerB);
61 numerA /= denom;
62 numerB /= denom;
caryclark@google.combeda3892013-02-07 13:13:41 +000063 if ((!approximately_zero(denom) || (!approximately_zero_inverse(numerA)
64 && !approximately_zero_inverse(numerB))) && !sk_double_isnan(numerA)
65 && !sk_double_isnan(numerB)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +000066 if (mayNotOverlap) {
67 return 0;
68 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +000069 i.fT[0][0] = numerA;
70 i.fT[1][0] = numerB;
71 i.fPt[0] = xy_at_t(a, numerA);
72 return computePoints(a, 1, i);
caryclark@google.comf9502d72013-02-04 14:06:49 +000073 }
74 /* See if the axis intercepts match:
75 ay - ax * ayLen / axLen == by - bx * ayLen / axLen
76 axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen)
77 axLen * ay - ax * ayLen == axLen * by - bx * ayLen
78 */
79 // FIXME: need to use AlmostEqualUlps variant instead
80 if (!approximately_equal_squared(axLen * a[0].y - ayLen * a[0].x,
81 axLen * b[0].y - ayLen * b[0].x)) {
caryclark@google.comc6825902012-02-03 22:07:47 +000082 return 0;
caryclark@google.com639df892012-01-10 21:46:10 +000083 }
caryclark@google.comf9502d72013-02-04 14:06:49 +000084 const double* aPtr;
85 const double* bPtr;
86 if (fabs(axLen) > fabs(ayLen) || fabs(bxLen) > fabs(byLen)) {
87 aPtr = &a[0].x;
88 bPtr = &b[0].x;
89 } else {
90 aPtr = &a[0].y;
91 bPtr = &b[0].y;
caryclark@google.comc6825902012-02-03 22:07:47 +000092 }
caryclark@google.comf9502d72013-02-04 14:06:49 +000093 double a0 = aPtr[0];
94 double a1 = aPtr[2];
95 double b0 = bPtr[0];
96 double b1 = bPtr[2];
97 // OPTIMIZATION: restructure to reject before the divide
98 // e.g., if ((a0 - b0) * (a0 - a1) < 0 || abs(a0 - b0) > abs(a0 - a1))
99 // (except efficient)
100 double aDenom = a0 - a1;
101 if (approximately_zero(aDenom)) {
102 if (!between(b0, a0, b1)) {
103 return 0;
104 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000105 i.fT[0][0] = i.fT[0][1] = 0;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000106 } else {
107 double at0 = (a0 - b0) / aDenom;
108 double at1 = (a0 - b1) / aDenom;
109 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
110 return 0;
111 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000112 i.fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
113 i.fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.comc6825902012-02-03 22:07:47 +0000114 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000115 double bDenom = b0 - b1;
116 if (approximately_zero(bDenom)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000117 i.fT[1][0] = i.fT[1][1] = 0;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000118 } else {
119 int bIn = aDenom * bDenom < 0;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000120 i.fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / bDenom, 1.0), 0.0);
121 i.fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / bDenom, 1.0), 0.0);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000122 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000123 bool second = fabs(i.fT[0][0] - i.fT[0][1]) > FLT_EPSILON;
124 SkASSERT((fabs(i.fT[1][0] - i.fT[1][1]) <= FLT_EPSILON) ^ second);
125 return computePoints(a, 1 + second, i);
caryclark@google.com639df892012-01-10 21:46:10 +0000126}
127
caryclark@google.comc6825902012-02-03 22:07:47 +0000128int horizontalIntersect(const _Line& line, double y, double tRange[2]) {
129 double min = line[0].y;
130 double max = line[1].y;
131 if (min > max) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000132 SkTSwap(min, max);
caryclark@google.comc6825902012-02-03 22:07:47 +0000133 }
134 if (min > y || max < y) {
135 return 0;
136 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000137 if (AlmostEqualUlps(min, max)) {
caryclark@google.comc6825902012-02-03 22:07:47 +0000138 tRange[0] = 0;
139 tRange[1] = 1;
140 return 2;
141 }
142 tRange[0] = (y - line[0].y) / (line[1].y - line[0].y);
143 return 1;
144}
caryclark@google.com639df892012-01-10 21:46:10 +0000145
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000146// OPTIMIZATION Given: dy = line[1].y - line[0].y
147// and: xIntercept / (y - line[0].y) == (line[1].x - line[0].x) / dy
148// then: xIntercept * dy == (line[1].x - line[0].x) * (y - line[0].y)
149// Assuming that dy is always > 0, the line segment intercepts if:
150// left * dy <= xIntercept * dy <= right * dy
151// thus: left * dy <= (line[1].x - line[0].x) * (y - line[0].y) <= right * dy
152// (clever as this is, it does not give us the t value, so may be useful only
153// as a quick reject -- and maybe not then; it takes 3 muls, 3 adds, 2 cmps)
154int horizontalLineIntersect(const _Line& line, double left, double right,
155 double y, double tRange[2]) {
156 int result = horizontalIntersect(line, y, tRange);
157 if (result != 1) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000158 // FIXME: this is incorrect if result == 2
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000159 return result;
160 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000161 double xIntercept = line[0].x + tRange[0] * (line[1].x - line[0].x);
162 if (xIntercept > right || xIntercept < left) {
163 return 0;
164 }
165 return result;
166}
167
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000168int horizontalIntersect(const _Line& line, double left, double right,
169 double y, bool flipped, Intersections& intersections) {
170 int result = horizontalIntersect(line, y, intersections.fT[0]);
171 switch (result) {
172 case 0:
173 break;
174 case 1: {
175 double xIntercept = line[0].x + intersections.fT[0][0]
176 * (line[1].x - line[0].x);
177 if (xIntercept > right || xIntercept < left) {
178 return 0;
179 }
180 intersections.fT[1][0] = (xIntercept - left) / (right - left);
181 break;
182 }
183 case 2:
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000184 #if 0 // sorting edges fails to preserve original direction
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000185 double lineL = line[0].x;
186 double lineR = line[1].x;
187 if (lineL > lineR) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000188 SkTSwap(lineL, lineR);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000189 }
caryclark@google.comaa358312013-01-29 20:28:49 +0000190 double overlapL = SkTMax(left, lineL);
191 double overlapR = SkTMin(right, lineR);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000192 if (overlapL > overlapR) {
193 return 0;
194 }
195 if (overlapL == overlapR) {
196 result = 1;
197 }
198 intersections.fT[0][0] = (overlapL - line[0].x) / (line[1].x - line[0].x);
199 intersections.fT[1][0] = (overlapL - left) / (right - left);
200 if (result > 1) {
201 intersections.fT[0][1] = (overlapR - line[0].x) / (line[1].x - line[0].x);
202 intersections.fT[1][1] = (overlapR - left) / (right - left);
203 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000204 #else
205 double a0 = line[0].x;
206 double a1 = line[1].x;
207 double b0 = flipped ? right : left;
208 double b1 = flipped ? left : right;
209 // FIXME: share common code below
210 double at0 = (a0 - b0) / (a0 - a1);
211 double at1 = (a0 - b1) / (a0 - a1);
212 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
213 return 0;
214 }
caryclark@google.comaa358312013-01-29 20:28:49 +0000215 intersections.fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
216 intersections.fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000217 int bIn = (a0 - a1) * (b0 - b1) < 0;
caryclark@google.comaa358312013-01-29 20:28:49 +0000218 intersections.fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1),
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000219 1.0), 0.0);
caryclark@google.comaa358312013-01-29 20:28:49 +0000220 intersections.fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1),
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000221 1.0), 0.0);
222 bool second = fabs(intersections.fT[0][0] - intersections.fT[0][1])
223 > FLT_EPSILON;
caryclark@google.comaa358312013-01-29 20:28:49 +0000224 SkASSERT((fabs(intersections.fT[1][0] - intersections.fT[1][1])
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000225 <= FLT_EPSILON) ^ second);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000226 return computePoints(line, 1 + second, intersections);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000227 #endif
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000228 break;
229 }
230 if (flipped) {
231 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
232 for (int index = 0; index < result; ++index) {
233 intersections.fT[1][index] = 1 - intersections.fT[1][index];
234 }
235 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000236 return computePoints(line, result, intersections);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000237}
238
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000239static int verticalIntersect(const _Line& line, double x, double tRange[2]) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000240 double min = line[0].x;
241 double max = line[1].x;
242 if (min > max) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000243 SkTSwap(min, max);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000244 }
245 if (min > x || max < x) {
246 return 0;
247 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000248 if (AlmostEqualUlps(min, max)) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000249 tRange[0] = 0;
250 tRange[1] = 1;
251 return 2;
252 }
253 tRange[0] = (x - line[0].x) / (line[1].x - line[0].x);
254 return 1;
255}
256
257int verticalIntersect(const _Line& line, double top, double bottom,
258 double x, bool flipped, Intersections& intersections) {
259 int result = verticalIntersect(line, x, intersections.fT[0]);
260 switch (result) {
261 case 0:
262 break;
263 case 1: {
264 double yIntercept = line[0].y + intersections.fT[0][0]
265 * (line[1].y - line[0].y);
266 if (yIntercept > bottom || yIntercept < top) {
267 return 0;
268 }
269 intersections.fT[1][0] = (yIntercept - top) / (bottom - top);
270 break;
271 }
272 case 2:
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000273 #if 0 // sorting edges fails to preserve original direction
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000274 double lineT = line[0].y;
275 double lineB = line[1].y;
276 if (lineT > lineB) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000277 SkTSwap(lineT, lineB);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000278 }
caryclark@google.comaa358312013-01-29 20:28:49 +0000279 double overlapT = SkTMax(top, lineT);
280 double overlapB = SkTMin(bottom, lineB);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000281 if (overlapT > overlapB) {
282 return 0;
283 }
284 if (overlapT == overlapB) {
285 result = 1;
286 }
287 intersections.fT[0][0] = (overlapT - line[0].y) / (line[1].y - line[0].y);
288 intersections.fT[1][0] = (overlapT - top) / (bottom - top);
289 if (result > 1) {
290 intersections.fT[0][1] = (overlapB - line[0].y) / (line[1].y - line[0].y);
291 intersections.fT[1][1] = (overlapB - top) / (bottom - top);
292 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000293 #else
294 double a0 = line[0].y;
295 double a1 = line[1].y;
296 double b0 = flipped ? bottom : top;
297 double b1 = flipped ? top : bottom;
298 // FIXME: share common code above
299 double at0 = (a0 - b0) / (a0 - a1);
300 double at1 = (a0 - b1) / (a0 - a1);
301 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
302 return 0;
303 }
caryclark@google.comaa358312013-01-29 20:28:49 +0000304 intersections.fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
305 intersections.fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000306 int bIn = (a0 - a1) * (b0 - b1) < 0;
caryclark@google.comaa358312013-01-29 20:28:49 +0000307 intersections.fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1),
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000308 1.0), 0.0);
caryclark@google.comaa358312013-01-29 20:28:49 +0000309 intersections.fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1),
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000310 1.0), 0.0);
311 bool second = fabs(intersections.fT[0][0] - intersections.fT[0][1])
312 > FLT_EPSILON;
caryclark@google.comaa358312013-01-29 20:28:49 +0000313 SkASSERT((fabs(intersections.fT[1][0] - intersections.fT[1][1])
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000314 <= FLT_EPSILON) ^ second);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000315 return computePoints(line, 1 + second, intersections);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000316 #endif
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000317 break;
318 }
319 if (flipped) {
320 // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
321 for (int index = 0; index < result; ++index) {
322 intersections.fT[1][index] = 1 - intersections.fT[1][index];
323 }
324 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000325 return computePoints(line, result, intersections);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000326}
327
caryclark@google.com639df892012-01-10 21:46:10 +0000328// from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py
329// 4 subs, 2 muls, 1 cmp
330static bool ccw(const _Point& A, const _Point& B, const _Point& C) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000331 return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x);
caryclark@google.com639df892012-01-10 21:46:10 +0000332}
333
334// 16 subs, 8 muls, 6 cmps
335bool testIntersect(const _Line& a, const _Line& b) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000336 return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1])
caryclark@google.com639df892012-01-10 21:46:10 +0000337 && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]);
338}