blob: 33c8480cd57f646886ddb6bbd0ccf10021d07993 [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
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000029void SkIntersections::cleanUpCoincidence() {
30 SkASSERT(fUsed == 2);
31 // both t values are good
32 bool startMatch = fT[0][0] == 0 && (fT[1][0] == 0 || fT[1][0] == 1);
33 bool endMatch = fT[0][1] == 1 && (fT[1][1] == 0 || fT[1][1] == 1);
34 if (startMatch || endMatch) {
35 removeOne(startMatch);
36 return;
37 }
38 // either t value is good
39 bool pStartMatch = fT[0][0] == 0 || fT[1][0] == 0 || fT[1][0] == 1;
40 bool pEndMatch = fT[0][1] == 1 || fT[1][1] == 0 || fT[1][1] == 1;
41 removeOne(pStartMatch || !pEndMatch);
42}
43
44void SkIntersections::cleanUpParallelLines(bool parallel) {
45 while (fUsed > 2) {
46 removeOne(1);
47 }
48 if (fUsed == 2 && !parallel) {
49 bool startMatch = fT[0][0] == 0 || fT[1][0] == 0 || fT[1][0] == 1;
50 bool endMatch = fT[0][1] == 1 || fT[1][1] == 0 || fT[1][1] == 1;
51 if ((!startMatch && !endMatch) || approximately_equal(fT[0][0], fT[0][1])) {
52 SkASSERT(startMatch || endMatch);
53 removeOne(endMatch);
54 }
55 }
56}
57
58void SkIntersections::computePoints(const SkDLine& line, int used) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +000059 fPt[0] = line.ptAtT(fT[0][0]);
caryclark@google.com07393ca2013-04-08 11:47:37 +000060 if ((fUsed = used) == 2) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +000061 fPt[1] = line.ptAtT(fT[0][1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +000062 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000063}
64
caryclark@google.comcffbcc32013-06-04 17:59:42 +000065int SkIntersections::intersectRay(const SkDLine& a, const SkDLine& b) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000066 fMax = 2;
caryclark@google.com570863f2013-09-16 15:55:01 +000067 SkDVector aLen = a[1] - a[0];
68 SkDVector bLen = b[1] - b[0];
caryclark@google.comcffbcc32013-06-04 17:59:42 +000069 /* Slopes match when denom goes to zero:
70 axLen / ayLen == bxLen / byLen
71 (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen
72 byLen * axLen == ayLen * bxLen
73 byLen * axLen - ayLen * bxLen == 0 ( == denom )
74 */
caryclark@google.com570863f2013-09-16 15:55:01 +000075 double denom = bLen.fY * aLen.fX - aLen.fY * bLen.fX;
76 SkDVector ab0 = a[0] - b[0];
77 double numerA = ab0.fY * bLen.fX - bLen.fY * ab0.fX;
78 double numerB = ab0.fY * aLen.fX - aLen.fY * ab0.fX;
caryclark@google.comcffbcc32013-06-04 17:59:42 +000079 numerA /= denom;
80 numerB /= denom;
81 int used;
82 if (!approximately_zero(denom)) {
83 fT[0][0] = numerA;
84 fT[1][0] = numerB;
85 used = 1;
86 } else {
87 /* See if the axis intercepts match:
88 ay - ax * ayLen / axLen == by - bx * ayLen / axLen
89 axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen)
90 axLen * ay - ax * ayLen == axLen * by - bx * ayLen
91 */
caryclark@google.com570863f2013-09-16 15:55:01 +000092 if (!AlmostEqualUlps(aLen.fX * a[0].fY - aLen.fY * a[0].fX,
93 aLen.fX * b[0].fY - aLen.fY * b[0].fX)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +000094 return fUsed = 0;
95 }
96 // there's no great answer for intersection points for coincident rays, but return something
97 fT[0][0] = fT[1][0] = 0;
98 fT[1][0] = fT[1][1] = 1;
99 used = 2;
100 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000101 computePoints(a, used);
102 return fUsed;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000103}
104
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000105// note that this only works if both lines are neither horizontal nor vertical
caryclark@google.com07393ca2013-04-08 11:47:37 +0000106int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000107 fMax = 3; // note that we clean up so that there is no more than two in the end
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000108 // see if end points intersect the opposite line
109 double t;
110 for (int iA = 0; iA < 2; ++iA) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000111 if ((t = b.exactPoint(a[iA])) >= 0) {
112 insert(iA, t, a[iA]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000113 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000114 }
115 for (int iB = 0; iB < 2; ++iB) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000116 if ((t = a.exactPoint(b[iB])) >= 0) {
117 insert(t, iB, b[iB]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000118 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000119 }
120 /* Determine the intersection point of two line segments
121 Return FALSE if the lines don't intersect
122 from: http://paulbourke.net/geometry/lineline2d/ */
caryclark@google.com07393ca2013-04-08 11:47:37 +0000123 double axLen = a[1].fX - a[0].fX;
124 double ayLen = a[1].fY - a[0].fY;
125 double bxLen = b[1].fX - b[0].fX;
126 double byLen = b[1].fY - b[0].fY;
127 /* Slopes match when denom goes to zero:
128 axLen / ayLen == bxLen / byLen
129 (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen
130 byLen * axLen == ayLen * bxLen
131 byLen * axLen - ayLen * bxLen == 0 ( == denom )
132 */
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000133 double axByLen = axLen * byLen;
134 double ayBxLen = ayLen * bxLen;
135 // detect parallel lines the same way here and in SkOpAngle operator <
136 // so that non-parallel means they are also sortable
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000137 bool unparallel = fAllowNear ? NotAlmostEqualUlps(axByLen, ayBxLen)
138 : NotAlmostDequalUlps(axByLen, ayBxLen);
139 if (unparallel && fUsed == 0) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000140 double ab0y = a[0].fY - b[0].fY;
141 double ab0x = a[0].fX - b[0].fX;
142 double numerA = ab0y * bxLen - byLen * ab0x;
143 double numerB = ab0y * axLen - ayLen * ab0x;
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000144 double denom = axByLen - ayBxLen;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000145 if (between(0, numerA, denom) && between(0, numerB, denom)) {
146 fT[0][0] = numerA / denom;
147 fT[1][0] = numerB / denom;
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000148 computePoints(a, 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000149 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000150 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000151 if (fAllowNear || !unparallel) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000152 for (int iA = 0; iA < 2; ++iA) {
153 if ((t = b.nearPoint(a[iA])) >= 0) {
154 insert(iA, t, a[iA]);
155 }
156 }
157 for (int iB = 0; iB < 2; ++iB) {
158 if ((t = a.nearPoint(b[iB])) >= 0) {
159 insert(t, iB, b[iB]);
160 }
161 }
162 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000163 cleanUpParallelLines(!unparallel);
164 SkASSERT(fUsed <= 2);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000165 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000166}
167
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000168static int horizontal_coincident(const SkDLine& line, double y) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000169 double min = line[0].fY;
170 double max = line[1].fY;
171 if (min > max) {
172 SkTSwap(min, max);
173 }
174 if (min > y || max < y) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000175 return 0;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000176 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000177 if (AlmostEqualUlps(min, max) && max - min < fabs(line[0].fX - line[1].fX)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000178 return 2;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000179 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000180 return 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000181}
182
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000183static double horizontal_intercept(const SkDLine& line, double y) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000184 return SkPinT((y - line[0].fY) / (line[1].fY - line[0].fY));
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000185}
186
187int SkIntersections::horizontal(const SkDLine& line, double y) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000188 fMax = 2;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000189 int horizontalType = horizontal_coincident(line, y);
190 if (horizontalType == 1) {
191 fT[0][0] = horizontal_intercept(line, y);
192 } else if (horizontalType == 2) {
193 fT[0][0] = 0;
194 fT[0][1] = 1;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000195 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000196 return fUsed = horizontalType;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000197}
198
caryclark@google.com07393ca2013-04-08 11:47:37 +0000199int SkIntersections::horizontal(const SkDLine& line, double left, double right,
200 double y, bool flipped) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000201 fMax = 2;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000202 // see if end points intersect the opposite line
203 double t;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000204 const SkDPoint leftPt = { left, y };
205 if ((t = line.exactPoint(leftPt)) >= 0) {
206 insert(t, (double) flipped, leftPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000207 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000208 if (left != right) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000209 const SkDPoint rightPt = { right, y };
210 if ((t = line.exactPoint(rightPt)) >= 0) {
211 insert(t, (double) !flipped, rightPt);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000212 }
213 for (int index = 0; index < 2; ++index) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000214 if ((t = SkDLine::ExactPointH(line[index], left, right, y)) >= 0) {
215 insert((double) index, flipped ? 1 - t : t, line[index]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000216 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000217 }
218 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000219 int result = horizontal_coincident(line, y);
220 if (result == 1 && fUsed == 0) {
221 fT[0][0] = horizontal_intercept(line, y);
222 double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX);
223 if (between(left, xIntercept, right)) {
224 fT[1][0] = (xIntercept - left) / (right - left);
225 if (flipped) {
226 // OPTIMIZATION: ? instead of swapping, pass original line, use [1].fX - [0].fX
227 for (int index = 0; index < result; ++index) {
228 fT[1][index] = 1 - fT[1][index];
229 }
230 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000231 computePoints(line, result);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000232 }
233 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000234 if (fAllowNear || result == 2) {
235 if ((t = line.nearPoint(leftPt)) >= 0) {
236 insert(t, (double) flipped, leftPt);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000237 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000238 if (left != right) {
239 const SkDPoint rightPt = { right, y };
240 if ((t = line.nearPoint(rightPt)) >= 0) {
241 insert(t, (double) !flipped, rightPt);
242 }
243 for (int index = 0; index < 2; ++index) {
244 if ((t = SkDLine::NearPointH(line[index], left, right, y)) >= 0) {
245 insert((double) index, flipped ? 1 - t : t, line[index]);
246 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000247 }
248 }
249 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000250 cleanUpParallelLines(result == 2);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000251 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000252}
253
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000254static int vertical_coincident(const SkDLine& line, double x) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000255 double min = line[0].fX;
256 double max = line[1].fX;
257 if (min > max) {
258 SkTSwap(min, max);
259 }
caryclark@google.com03610322013-04-18 15:58:21 +0000260 if (!precisely_between(min, x, max)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000261 return 0;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000262 }
263 if (AlmostEqualUlps(min, max)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000264 return 2;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000265 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000266 return 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000267}
268
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000269static double vertical_intercept(const SkDLine& line, double x) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000270 return SkPinT((x - line[0].fX) / (line[1].fX - line[0].fX));
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000271}
272
273int SkIntersections::vertical(const SkDLine& line, double x) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000274 fMax = 2;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000275 int verticalType = vertical_coincident(line, x);
276 if (verticalType == 1) {
277 fT[0][0] = vertical_intercept(line, x);
278 } else if (verticalType == 2) {
279 fT[0][0] = 0;
280 fT[0][1] = 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000281 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000282 return fUsed = verticalType;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000283}
284
285int SkIntersections::vertical(const SkDLine& line, double top, double bottom,
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000286 double x, bool flipped) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000287 fMax = 2;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000288 // see if end points intersect the opposite line
289 double t;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000290 SkDPoint topPt = { x, top };
291 if ((t = line.exactPoint(topPt)) >= 0) {
292 insert(t, (double) flipped, topPt);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000293 }
294 if (top != bottom) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000295 SkDPoint bottomPt = { x, bottom };
296 if ((t = line.exactPoint(bottomPt)) >= 0) {
297 insert(t, (double) !flipped, bottomPt);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000298 }
299 for (int index = 0; index < 2; ++index) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000300 if ((t = SkDLine::ExactPointV(line[index], top, bottom, x)) >= 0) {
301 insert((double) index, flipped ? 1 - t : t, line[index]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000302 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000303 }
304 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000305 int result = vertical_coincident(line, x);
306 if (result == 1 && fUsed == 0) {
307 fT[0][0] = vertical_intercept(line, x);
308 double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY);
309 if (between(top, yIntercept, bottom)) {
310 fT[1][0] = (yIntercept - top) / (bottom - top);
311 if (flipped) {
312 // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY
313 for (int index = 0; index < result; ++index) {
314 fT[1][index] = 1 - fT[1][index];
315 }
316 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000317 computePoints(line, result);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000318 }
319 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000320 if (fAllowNear || result == 2) {
321 if ((t = line.nearPoint(topPt)) >= 0) {
322 insert(t, (double) flipped, topPt);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000323 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000324 if (top != bottom) {
325 SkDPoint bottomPt = { x, bottom };
326 if ((t = line.nearPoint(bottomPt)) >= 0) {
327 insert(t, (double) !flipped, bottomPt);
328 }
329 for (int index = 0; index < 2; ++index) {
330 if ((t = SkDLine::NearPointV(line[index], top, bottom, x)) >= 0) {
331 insert((double) index, flipped ? 1 - t : t, line[index]);
332 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000333 }
334 }
335 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000336 cleanUpParallelLines(result == 2);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000337 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000338}
339
340// from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py
341// 4 subs, 2 muls, 1 cmp
342static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) {
343 return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX);
344}
345
346// 16 subs, 8 muls, 6 cmps
347bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) {
348 return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1])
349 && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]);
350}