blob: 5fd7d61d7d68bfa4897e61869083c2e1528cdd31 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +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 */
7#include "SkIntersections.h"
8#include "SkPathOpsLine.h"
9
10/* Determine the intersection point of two lines. This assumes the lines are not parallel,
11 and that that the lines are infinite.
12 From http://en.wikipedia.org/wiki/Line-line_intersection
13 */
14SkDPoint SkIntersections::Line(const SkDLine& a, const SkDLine& b) {
15 double axLen = a[1].fX - a[0].fX;
16 double ayLen = a[1].fY - a[0].fY;
17 double bxLen = b[1].fX - b[0].fX;
18 double byLen = b[1].fY - b[0].fY;
19 double denom = byLen * axLen - ayLen * bxLen;
20 SkASSERT(denom);
21 double term1 = a[1].fX * a[0].fY - a[1].fY * a[0].fX;
22 double term2 = b[1].fX * b[0].fY - b[1].fY * b[0].fX;
23 SkDPoint p;
24 p.fX = (term1 * bxLen - axLen * term2) / denom;
25 p.fY = (term1 * byLen - ayLen * term2) / denom;
26 return p;
27}
28
29int SkIntersections::computePoints(const SkDLine& line, int used) {
30 fPt[0] = line.xyAtT(fT[0][0]);
31 if ((fUsed = used) == 2) {
32 fPt[1] = line.xyAtT(fT[0][1]);
33 }
34 return fUsed;
35}
36
37/*
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/
41 */
42
43int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) {
44 double axLen = a[1].fX - a[0].fX;
45 double ayLen = a[1].fY - a[0].fY;
46 double bxLen = b[1].fX - b[0].fX;
47 double byLen = b[1].fY - b[0].fY;
48 /* Slopes match when denom goes to zero:
49 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 */
54 double denom = byLen * axLen - ayLen * bxLen;
55 double ab0y = a[0].fY - b[0].fY;
56 double ab0x = a[0].fX - b[0].fX;
57 double numerA = ab0y * bxLen - byLen * ab0x;
58 double numerB = ab0y * axLen - ayLen * ab0x;
59 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;
63 if ((!approximately_zero(denom) || (!approximately_zero_inverse(numerA)
64 && !approximately_zero_inverse(numerB))) && !sk_double_isnan(numerA)
65 && !sk_double_isnan(numerB)) {
66 if (mayNotOverlap) {
67 return fUsed = 0;
68 }
69 fT[0][0] = numerA;
70 fT[1][0] = numerB;
71 fPt[0] = a.xyAtT(numerA);
72 return computePoints(a, 1);
73 }
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 */
caryclark@google.comc3f63572013-04-23 12:04:05 +000079 if (!AlmostEqualUlps(axLen * a[0].fY - ayLen * a[0].fX,
caryclark@google.com07393ca2013-04-08 11:47:37 +000080 axLen * b[0].fY - ayLen * b[0].fX)) {
81 return fUsed = 0;
82 }
83 const double* aPtr;
84 const double* bPtr;
85 if (fabs(axLen) > fabs(ayLen) || fabs(bxLen) > fabs(byLen)) {
86 aPtr = &a[0].fX;
87 bPtr = &b[0].fX;
88 } else {
89 aPtr = &a[0].fY;
90 bPtr = &b[0].fY;
91 }
92 double a0 = aPtr[0];
93 double a1 = aPtr[2];
94 double b0 = bPtr[0];
95 double b1 = bPtr[2];
96 // OPTIMIZATION: restructure to reject before the divide
97 // e.g., if ((a0 - b0) * (a0 - a1) < 0 || abs(a0 - b0) > abs(a0 - a1))
98 // (except efficient)
99 double aDenom = a0 - a1;
100 if (approximately_zero(aDenom)) {
101 if (!between(b0, a0, b1)) {
102 return fUsed = 0;
103 }
104 fT[0][0] = fT[0][1] = 0;
105 } else {
106 double at0 = (a0 - b0) / aDenom;
107 double at1 = (a0 - b1) / aDenom;
108 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
109 return fUsed = 0;
110 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000111 fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
112 fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113 }
114 double bDenom = b0 - b1;
115 if (approximately_zero(bDenom)) {
116 fT[1][0] = fT[1][1] = 0;
117 } else {
118 int bIn = aDenom * bDenom < 0;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000119 fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / bDenom, 1.0), 0.0);
120 fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / bDenom, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000121 }
122 bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON;
123 SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second);
124 return computePoints(a, 1 + second);
125}
126
127int SkIntersections::horizontal(const SkDLine& line, double y) {
128 double min = line[0].fY;
129 double max = line[1].fY;
130 if (min > max) {
131 SkTSwap(min, max);
132 }
133 if (min > y || max < y) {
134 return fUsed = 0;
135 }
136 if (AlmostEqualUlps(min, max)) {
137 fT[0][0] = 0;
138 fT[0][1] = 1;
139 return fUsed = 2;
140 }
141 fT[0][0] = (y - line[0].fY) / (line[1].fY - line[0].fY);
142 return fUsed = 1;
143}
144
145// OPTIMIZATION Given: dy = line[1].fY - line[0].fY
146// and: xIntercept / (y - line[0].fY) == (line[1].fX - line[0].fX) / dy
147// then: xIntercept * dy == (line[1].fX - line[0].fX) * (y - line[0].fY)
148// Assuming that dy is always > 0, the line segment intercepts if:
149// left * dy <= xIntercept * dy <= right * dy
150// thus: left * dy <= (line[1].fX - line[0].fX) * (y - line[0].fY) <= right * dy
151// (clever as this is, it does not give us the t value, so may be useful only
152// as a quick reject -- and maybe not then; it takes 3 muls, 3 adds, 2 cmps)
153int SkIntersections::horizontal(const SkDLine& line, double left, double right, double y) {
154 int result = horizontal(line, y);
155 if (result != 1) {
caryclark@google.comc3f63572013-04-23 12:04:05 +0000156 SkASSERT(0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000157 return result;
158 }
159 double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX);
caryclark@google.com03610322013-04-18 15:58:21 +0000160 if (!precisely_between(left, xIntercept, right)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000161 return fUsed = 0;
162 }
163 return result;
164}
165
166int SkIntersections::horizontal(const SkDLine& line, double left, double right,
167 double y, bool flipped) {
168 int result = horizontal(line, y);
169 switch (result) {
170 case 0:
171 break;
172 case 1: {
173 double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX);
caryclark@google.com03610322013-04-18 15:58:21 +0000174 if (!precisely_between(left, xIntercept, right)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000175 return fUsed = 0;
176 }
177 fT[1][0] = (xIntercept - left) / (right - left);
178 break;
179 }
180 case 2:
181 double a0 = line[0].fX;
182 double a1 = line[1].fX;
183 double b0 = flipped ? right : left;
184 double b1 = flipped ? left : right;
185 // FIXME: share common code below
186 double at0 = (a0 - b0) / (a0 - a1);
187 double at1 = (a0 - b1) / (a0 - a1);
188 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
189 return fUsed = 0;
190 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000191 fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
192 fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000193 int bIn = (a0 - a1) * (b0 - b1) < 0;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000194 fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1), 1.0), 0.0);
195 fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1), 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000196 bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON;
197 SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second);
198 return computePoints(line, 1 + second);
199 }
200 if (flipped) {
201 // OPTIMIZATION: instead of swapping, pass original line, use [1].fX - [0].fX
202 for (int index = 0; index < result; ++index) {
203 fT[1][index] = 1 - fT[1][index];
204 }
205 }
206 return computePoints(line, result);
207}
208
209int SkIntersections::vertical(const SkDLine& line, double x) {
210 double min = line[0].fX;
211 double max = line[1].fX;
212 if (min > max) {
213 SkTSwap(min, max);
214 }
caryclark@google.com03610322013-04-18 15:58:21 +0000215 if (!precisely_between(min, x, max)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000216 return fUsed = 0;
217 }
218 if (AlmostEqualUlps(min, max)) {
219 fT[0][0] = 0;
220 fT[0][1] = 1;
221 return fUsed = 2;
222 }
223 fT[0][0] = (x - line[0].fX) / (line[1].fX - line[0].fX);
224 return fUsed = 1;
225}
226
227int SkIntersections::vertical(const SkDLine& line, double top, double bottom,
228 double x, bool flipped) {
229 int result = vertical(line, x);
230 switch (result) {
231 case 0:
232 break;
233 case 1: {
234 double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY);
caryclark@google.com03610322013-04-18 15:58:21 +0000235 if (!precisely_between(top, yIntercept, bottom)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000236 return fUsed = 0;
237 }
238 fT[1][0] = (yIntercept - top) / (bottom - top);
239 break;
240 }
241 case 2:
242 double a0 = line[0].fY;
243 double a1 = line[1].fY;
244 double b0 = flipped ? bottom : top;
245 double b1 = flipped ? top : bottom;
246 // FIXME: share common code above
247 double at0 = (a0 - b0) / (a0 - a1);
248 double at1 = (a0 - b1) / (a0 - a1);
249 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
250 return fUsed = 0;
251 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000252 fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
253 fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000254 int bIn = (a0 - a1) * (b0 - b1) < 0;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000255 fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1), 1.0), 0.0);
256 fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1), 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000257 bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON;
258 SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second);
259 return computePoints(line, 1 + second);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000260 }
261 if (flipped) {
262 // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY
263 for (int index = 0; index < result; ++index) {
264 fT[1][index] = 1 - fT[1][index];
265 }
266 }
267 return computePoints(line, result);
268}
269
270// from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py
271// 4 subs, 2 muls, 1 cmp
272static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) {
273 return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX);
274}
275
276// 16 subs, 8 muls, 6 cmps
277bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) {
278 return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1])
279 && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]);
280}