blob: 43c37decb8de0c62e939250db45660f1ec63d7eb [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
Ben Wagnerf08d1d02018-06-18 15:11:00 -040010#include <utility>
11
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000012void SkIntersections::cleanUpParallelLines(bool parallel) {
13 while (fUsed > 2) {
14 removeOne(1);
15 }
16 if (fUsed == 2 && !parallel) {
caryclark94c902e2015-08-18 07:12:43 -070017 bool startMatch = fT[0][0] == 0 || zero_or_one(fT[1][0]);
18 bool endMatch = fT[0][1] == 1 || zero_or_one(fT[1][1]);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000019 if ((!startMatch && !endMatch) || approximately_equal(fT[0][0], fT[0][1])) {
20 SkASSERT(startMatch || endMatch);
caryclark94c902e2015-08-18 07:12:43 -070021 if (startMatch && endMatch && (fT[0][0] != 0 || !zero_or_one(fT[1][0]))
22 && fT[0][1] == 1 && zero_or_one(fT[1][1])) {
23 removeOne(0);
24 } else {
25 removeOne(endMatch);
26 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000027 }
28 }
caryclark54359292015-03-26 07:52:43 -070029 if (fUsed == 2) {
30 fIsCoincident[0] = fIsCoincident[1] = 0x03;
31 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000032}
33
34void SkIntersections::computePoints(const SkDLine& line, int used) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +000035 fPt[0] = line.ptAtT(fT[0][0]);
caryclark@google.com07393ca2013-04-08 11:47:37 +000036 if ((fUsed = used) == 2) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +000037 fPt[1] = line.ptAtT(fT[0][1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +000038 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000039}
40
caryclark@google.comcffbcc32013-06-04 17:59:42 +000041int SkIntersections::intersectRay(const SkDLine& a, const SkDLine& b) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000042 fMax = 2;
caryclark@google.com570863f2013-09-16 15:55:01 +000043 SkDVector aLen = a[1] - a[0];
44 SkDVector bLen = b[1] - b[0];
caryclark@google.comcffbcc32013-06-04 17:59:42 +000045 /* Slopes match when denom goes to zero:
46 axLen / ayLen == bxLen / byLen
47 (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen
48 byLen * axLen == ayLen * bxLen
49 byLen * axLen - ayLen * bxLen == 0 ( == denom )
50 */
caryclark@google.com570863f2013-09-16 15:55:01 +000051 double denom = bLen.fY * aLen.fX - aLen.fY * bLen.fX;
caryclark@google.comcffbcc32013-06-04 17:59:42 +000052 int used;
53 if (!approximately_zero(denom)) {
Cary Clark22582502017-12-13 14:56:53 -050054 SkDVector ab0 = a[0] - b[0];
55 double numerA = ab0.fY * bLen.fX - bLen.fY * ab0.fX;
56 double numerB = ab0.fY * aLen.fX - aLen.fY * ab0.fX;
57 numerA /= denom;
58 numerB /= denom;
caryclark@google.comcffbcc32013-06-04 17:59:42 +000059 fT[0][0] = numerA;
60 fT[1][0] = numerB;
61 used = 1;
62 } else {
63 /* See if the axis intercepts match:
64 ay - ax * ayLen / axLen == by - bx * ayLen / axLen
65 axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen)
66 axLen * ay - ax * ayLen == axLen * by - bx * ayLen
67 */
caryclark@google.com570863f2013-09-16 15:55:01 +000068 if (!AlmostEqualUlps(aLen.fX * a[0].fY - aLen.fY * a[0].fX,
69 aLen.fX * b[0].fY - aLen.fY * b[0].fX)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +000070 return fUsed = 0;
71 }
72 // there's no great answer for intersection points for coincident rays, but return something
73 fT[0][0] = fT[1][0] = 0;
74 fT[1][0] = fT[1][1] = 1;
75 used = 2;
76 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000077 computePoints(a, used);
78 return fUsed;
caryclark@google.comcffbcc32013-06-04 17:59:42 +000079}
80
caryclark@google.com07e97fc2013-07-08 17:17:02 +000081// note that this only works if both lines are neither horizontal nor vertical
caryclark@google.com07393ca2013-04-08 11:47:37 +000082int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000083 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 +000084 // see if end points intersect the opposite line
85 double t;
86 for (int iA = 0; iA < 2; ++iA) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000087 if ((t = b.exactPoint(a[iA])) >= 0) {
88 insert(iA, t, a[iA]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000089 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000090 }
91 for (int iB = 0; iB < 2; ++iB) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000092 if ((t = a.exactPoint(b[iB])) >= 0) {
93 insert(t, iB, b[iB]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000094 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000095 }
96 /* Determine the intersection point of two line segments
97 Return FALSE if the lines don't intersect
98 from: http://paulbourke.net/geometry/lineline2d/ */
caryclark@google.com07393ca2013-04-08 11:47:37 +000099 double axLen = a[1].fX - a[0].fX;
100 double ayLen = a[1].fY - a[0].fY;
101 double bxLen = b[1].fX - b[0].fX;
102 double byLen = b[1].fY - b[0].fY;
103 /* Slopes match when denom goes to zero:
104 axLen / ayLen == bxLen / byLen
105 (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen
106 byLen * axLen == ayLen * bxLen
107 byLen * axLen - ayLen * bxLen == 0 ( == denom )
108 */
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000109 double axByLen = axLen * byLen;
110 double ayBxLen = ayLen * bxLen;
111 // detect parallel lines the same way here and in SkOpAngle operator <
112 // so that non-parallel means they are also sortable
caryclarkb6693002015-12-16 12:28:35 -0800113 bool unparallel = fAllowNear ? NotAlmostEqualUlps_Pin(axByLen, ayBxLen)
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000114 : NotAlmostDequalUlps(axByLen, ayBxLen);
115 if (unparallel && fUsed == 0) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000116 double ab0y = a[0].fY - b[0].fY;
117 double ab0x = a[0].fX - b[0].fX;
118 double numerA = ab0y * bxLen - byLen * ab0x;
119 double numerB = ab0y * axLen - ayLen * ab0x;
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000120 double denom = axByLen - ayBxLen;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000121 if (between(0, numerA, denom) && between(0, numerB, denom)) {
122 fT[0][0] = numerA / denom;
123 fT[1][0] = numerB / denom;
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000124 computePoints(a, 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000125 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000126 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400127/* Allow tracking that both sets of end points are near each other -- the lines are entirely
caryclarkdac1d172014-06-17 05:15:38 -0700128 coincident -- even when the end points are not exactly the same.
129 Mark this as a 'wild card' for the end points, so that either point is considered totally
Ben Wagner63fd7602017-10-09 15:45:33 -0400130 coincident. Then, avoid folding the lines over each other, but allow either end to mate
caryclarkdac1d172014-06-17 05:15:38 -0700131 to the next set of lines.
132 */
caryclark@google.com570863f2013-09-16 15:55:01 +0000133 if (fAllowNear || !unparallel) {
caryclarkdac1d172014-06-17 05:15:38 -0700134 double aNearB[2];
135 double bNearA[2];
136 bool aNotB[2] = {false, false};
137 bool bNotA[2] = {false, false};
138 int nearCount = 0;
139 for (int index = 0; index < 2; ++index) {
140 aNearB[index] = t = b.nearPoint(a[index], &aNotB[index]);
141 nearCount += t >= 0;
142 bNearA[index] = t = a.nearPoint(b[index], &bNotA[index]);
143 nearCount += t >= 0;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000144 }
caryclarkdac1d172014-06-17 05:15:38 -0700145 if (nearCount > 0) {
caryclark19eb3b22014-07-18 05:08:14 -0700146 // Skip if each segment contributes to one end point.
147 if (nearCount != 2 || aNotB[0] == aNotB[1]) {
148 for (int iA = 0; iA < 2; ++iA) {
149 if (!aNotB[iA]) {
150 continue;
151 }
152 int nearer = aNearB[iA] > 0.5;
153 if (!bNotA[nearer]) {
154 continue;
155 }
156 SkASSERT(a[iA] != b[nearer]);
caryclarka35ab3e2016-10-20 08:32:18 -0700157 SkOPASSERT(iA == (bNearA[nearer] > 0.5));
caryclark19eb3b22014-07-18 05:08:14 -0700158 insertNear(iA, nearer, a[iA], b[nearer]);
159 aNearB[iA] = -1;
160 bNearA[nearer] = -1;
161 nearCount -= 2;
caryclarkdac1d172014-06-17 05:15:38 -0700162 }
caryclarkdac1d172014-06-17 05:15:38 -0700163 }
164 if (nearCount > 0) {
165 for (int iA = 0; iA < 2; ++iA) {
166 if (aNearB[iA] >= 0) {
167 insert(iA, aNearB[iA], a[iA]);
168 }
169 }
170 for (int iB = 0; iB < 2; ++iB) {
171 if (bNearA[iB] >= 0) {
172 insert(bNearA[iB], iB, b[iB]);
173 }
174 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000175 }
176 }
177 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000178 cleanUpParallelLines(!unparallel);
179 SkASSERT(fUsed <= 2);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000180 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000181}
182
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000183static int horizontal_coincident(const SkDLine& line, double y) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000184 double min = line[0].fY;
185 double max = line[1].fY;
186 if (min > max) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400187 using std::swap;
188 swap(min, max);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000189 }
190 if (min > y || max < y) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000191 return 0;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000192 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000193 if (AlmostEqualUlps(min, max) && max - min < fabs(line[0].fX - line[1].fX)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000194 return 2;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000195 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000196 return 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000197}
198
caryclark624637c2015-05-11 07:21:27 -0700199double SkIntersections::HorizontalIntercept(const SkDLine& line, double y) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000200 return SkPinT((y - line[0].fY) / (line[1].fY - line[0].fY));
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000201}
202
caryclark@google.com07393ca2013-04-08 11:47:37 +0000203int SkIntersections::horizontal(const SkDLine& line, double left, double right,
204 double y, bool flipped) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000205 fMax = 3; // clean up parallel at the end will limit the result to 2 at the most
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000206 // see if end points intersect the opposite line
207 double t;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000208 const SkDPoint leftPt = { left, y };
209 if ((t = line.exactPoint(leftPt)) >= 0) {
210 insert(t, (double) flipped, leftPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000211 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000212 if (left != right) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000213 const SkDPoint rightPt = { right, y };
214 if ((t = line.exactPoint(rightPt)) >= 0) {
215 insert(t, (double) !flipped, rightPt);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000216 }
217 for (int index = 0; index < 2; ++index) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000218 if ((t = SkDLine::ExactPointH(line[index], left, right, y)) >= 0) {
219 insert((double) index, flipped ? 1 - t : t, line[index]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000220 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000221 }
222 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000223 int result = horizontal_coincident(line, y);
224 if (result == 1 && fUsed == 0) {
caryclark624637c2015-05-11 07:21:27 -0700225 fT[0][0] = HorizontalIntercept(line, y);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000226 double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX);
227 if (between(left, xIntercept, right)) {
228 fT[1][0] = (xIntercept - left) / (right - left);
229 if (flipped) {
230 // OPTIMIZATION: ? instead of swapping, pass original line, use [1].fX - [0].fX
231 for (int index = 0; index < result; ++index) {
232 fT[1][index] = 1 - fT[1][index];
233 }
234 }
caryclark@google.com15107262013-11-08 18:00:01 +0000235 fPt[0].fX = xIntercept;
236 fPt[0].fY = y;
237 fUsed = 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000238 }
239 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000240 if (fAllowNear || result == 2) {
halcanary96fcdcc2015-08-27 07:41:13 -0700241 if ((t = line.nearPoint(leftPt, nullptr)) >= 0) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000242 insert(t, (double) flipped, leftPt);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000243 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000244 if (left != right) {
245 const SkDPoint rightPt = { right, y };
halcanary96fcdcc2015-08-27 07:41:13 -0700246 if ((t = line.nearPoint(rightPt, nullptr)) >= 0) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000247 insert(t, (double) !flipped, rightPt);
248 }
249 for (int index = 0; index < 2; ++index) {
250 if ((t = SkDLine::NearPointH(line[index], left, right, y)) >= 0) {
251 insert((double) index, flipped ? 1 - t : t, line[index]);
252 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000253 }
254 }
255 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000256 cleanUpParallelLines(result == 2);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000257 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000258}
259
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000260static int vertical_coincident(const SkDLine& line, double x) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000261 double min = line[0].fX;
262 double max = line[1].fX;
263 if (min > max) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400264 using std::swap;
265 swap(min, max);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000266 }
caryclark@google.com03610322013-04-18 15:58:21 +0000267 if (!precisely_between(min, x, max)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000268 return 0;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000269 }
270 if (AlmostEqualUlps(min, max)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000271 return 2;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000272 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000273 return 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000274}
275
caryclark624637c2015-05-11 07:21:27 -0700276double SkIntersections::VerticalIntercept(const SkDLine& line, double x) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000277 return SkPinT((x - line[0].fX) / (line[1].fX - line[0].fX));
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000278}
279
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000280int SkIntersections::vertical(const SkDLine& line, double top, double bottom,
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000281 double x, bool flipped) {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000282 fMax = 3; // cleanup parallel lines will bring this back line
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000283 // see if end points intersect the opposite line
284 double t;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000285 SkDPoint topPt = { x, top };
286 if ((t = line.exactPoint(topPt)) >= 0) {
287 insert(t, (double) flipped, topPt);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000288 }
289 if (top != bottom) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000290 SkDPoint bottomPt = { x, bottom };
291 if ((t = line.exactPoint(bottomPt)) >= 0) {
292 insert(t, (double) !flipped, bottomPt);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000293 }
294 for (int index = 0; index < 2; ++index) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000295 if ((t = SkDLine::ExactPointV(line[index], top, bottom, x)) >= 0) {
296 insert((double) index, flipped ? 1 - t : t, line[index]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000297 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000298 }
299 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000300 int result = vertical_coincident(line, x);
301 if (result == 1 && fUsed == 0) {
caryclark624637c2015-05-11 07:21:27 -0700302 fT[0][0] = VerticalIntercept(line, x);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000303 double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY);
304 if (between(top, yIntercept, bottom)) {
305 fT[1][0] = (yIntercept - top) / (bottom - top);
306 if (flipped) {
307 // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY
308 for (int index = 0; index < result; ++index) {
309 fT[1][index] = 1 - fT[1][index];
310 }
311 }
caryclark@google.com15107262013-11-08 18:00:01 +0000312 fPt[0].fX = x;
313 fPt[0].fY = yIntercept;
314 fUsed = 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000315 }
316 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000317 if (fAllowNear || result == 2) {
halcanary96fcdcc2015-08-27 07:41:13 -0700318 if ((t = line.nearPoint(topPt, nullptr)) >= 0) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000319 insert(t, (double) flipped, topPt);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000320 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000321 if (top != bottom) {
322 SkDPoint bottomPt = { x, bottom };
halcanary96fcdcc2015-08-27 07:41:13 -0700323 if ((t = line.nearPoint(bottomPt, nullptr)) >= 0) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000324 insert(t, (double) !flipped, bottomPt);
325 }
326 for (int index = 0; index < 2; ++index) {
327 if ((t = SkDLine::NearPointV(line[index], top, bottom, x)) >= 0) {
328 insert((double) index, flipped ? 1 - t : t, line[index]);
329 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000330 }
331 }
332 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000333 cleanUpParallelLines(result == 2);
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000334 SkASSERT(fUsed <= 2);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000335 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000336}
337