blob: dc80479f602072fd1c365923264a01bf540bee3c [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 "SkPathOpsCubic.h"
9#include "SkPathOpsLine.h"
10
11/*
12Find the interection of a line and cubic by solving for valid t values.
13
14Analogous to line-quadratic intersection, solve line-cubic intersection by
15representing the cubic as:
16 x = a(1-t)^3 + 2b(1-t)^2t + c(1-t)t^2 + dt^3
17 y = e(1-t)^3 + 2f(1-t)^2t + g(1-t)t^2 + ht^3
18and the line as:
19 y = i*x + j (if the line is more horizontal)
20or:
21 x = i*y + j (if the line is more vertical)
22
23Then using Mathematica, solve for the values of t where the cubic intersects the
24line:
25
26 (in) Resultant[
27 a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - x,
28 e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - i*x - j, x]
29 (out) -e + j +
30 3 e t - 3 f t -
31 3 e t^2 + 6 f t^2 - 3 g t^2 +
32 e t^3 - 3 f t^3 + 3 g t^3 - h t^3 +
33 i ( a -
34 3 a t + 3 b t +
35 3 a t^2 - 6 b t^2 + 3 c t^2 -
36 a t^3 + 3 b t^3 - 3 c t^3 + d t^3 )
37
38if i goes to infinity, we can rewrite the line in terms of x. Mathematica:
39
40 (in) Resultant[
41 a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - i*y - j,
42 e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y]
43 (out) a - j -
44 3 a t + 3 b t +
45 3 a t^2 - 6 b t^2 + 3 c t^2 -
46 a t^3 + 3 b t^3 - 3 c t^3 + d t^3 -
47 i ( e -
48 3 e t + 3 f t +
49 3 e t^2 - 6 f t^2 + 3 g t^2 -
50 e t^3 + 3 f t^3 - 3 g t^3 + h t^3 )
51
52Solving this with Mathematica produces an expression with hundreds of terms;
53instead, use Numeric Solutions recipe to solve the cubic.
54
55The near-horizontal case, in terms of: Ax^3 + Bx^2 + Cx + D == 0
56 A = (-(-e + 3*f - 3*g + h) + i*(-a + 3*b - 3*c + d) )
57 B = 3*(-( e - 2*f + g ) + i*( a - 2*b + c ) )
58 C = 3*(-(-e + f ) + i*(-a + b ) )
59 D = (-( e ) + i*( a ) + j )
60
61The near-vertical case, in terms of: Ax^3 + Bx^2 + Cx + D == 0
62 A = ( (-a + 3*b - 3*c + d) - i*(-e + 3*f - 3*g + h) )
63 B = 3*( ( a - 2*b + c ) - i*( e - 2*f + g ) )
64 C = 3*( (-a + b ) - i*(-e + f ) )
65 D = ( ( a ) - i*( e ) - j )
66
67For horizontal lines:
68(in) Resultant[
69 a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - j,
70 e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y]
71(out) e - j -
72 3 e t + 3 f t +
73 3 e t^2 - 6 f t^2 + 3 g t^2 -
74 e t^3 + 3 f t^3 - 3 g t^3 + h t^3
75 */
76
77class LineCubicIntersections {
78public:
79
80LineCubicIntersections(const SkDCubic& c, const SkDLine& l, SkIntersections& i)
81 : cubic(c)
82 , line(l)
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000083 , intersections(i)
84 , fAllowNear(true) {
85}
86
87void allowNear(bool allow) {
88 fAllowNear = allow;
caryclark@google.com07393ca2013-04-08 11:47:37 +000089}
90
91// see parallel routine in line quadratic intersections
92int intersectRay(double roots[3]) {
93 double adj = line[1].fX - line[0].fX;
94 double opp = line[1].fY - line[0].fY;
95 SkDCubic r;
96 for (int n = 0; n < 4; ++n) {
97 r[n].fX = (cubic[n].fY - line[0].fY) * adj - (cubic[n].fX - line[0].fX) * opp;
98 }
99 double A, B, C, D;
100 SkDCubic::Coefficients(&r[0].fX, &A, &B, &C, &D);
101 return SkDCubic::RootsValidT(A, B, C, D, roots);
102}
103
104int intersect() {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000105 addExactEndPoints();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000106 double rootVals[3];
107 int roots = intersectRay(rootVals);
108 for (int index = 0; index < roots; ++index) {
109 double cubicT = rootVals[index];
110 double lineT = findLineT(cubicT);
111 if (pinTs(&cubicT, &lineT)) {
112 SkDPoint pt = line.xyAtT(lineT);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000113#if ONE_OFF_DEBUG
114 SkDPoint cPt = cubic.xyAtT(cubicT);
115 SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY,
116 cPt.fX, cPt.fY);
117#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000118 intersections.insert(cubicT, lineT, pt);
119 }
120 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000121 if (fAllowNear) {
122 addNearEndPoints();
123 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000124 return intersections.used();
125}
126
127int horizontalIntersect(double axisIntercept, double roots[3]) {
128 double A, B, C, D;
129 SkDCubic::Coefficients(&cubic[0].fY, &A, &B, &C, &D);
130 D -= axisIntercept;
131 return SkDCubic::RootsValidT(A, B, C, D, roots);
132}
133
134int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000135 addExactHorizontalEndPoints(left, right, axisIntercept);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000136 double rootVals[3];
137 int roots = horizontalIntersect(axisIntercept, rootVals);
138 for (int index = 0; index < roots; ++index) {
139 double cubicT = rootVals[index];
140 SkDPoint pt = cubic.xyAtT(cubicT);
141 double lineT = (pt.fX - left) / (right - left);
142 if (pinTs(&cubicT, &lineT)) {
143 intersections.insert(cubicT, lineT, pt);
144 }
145 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000146 if (fAllowNear) {
147 addNearHorizontalEndPoints(left, right, axisIntercept);
148 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000149 if (flipped) {
150 intersections.flip();
151 }
152 return intersections.used();
153}
154
155int verticalIntersect(double axisIntercept, double roots[3]) {
156 double A, B, C, D;
157 SkDCubic::Coefficients(&cubic[0].fX, &A, &B, &C, &D);
158 D -= axisIntercept;
159 return SkDCubic::RootsValidT(A, B, C, D, roots);
160}
161
162int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000163 addExactVerticalEndPoints(top, bottom, axisIntercept);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000164 double rootVals[3];
165 int roots = verticalIntersect(axisIntercept, rootVals);
166 for (int index = 0; index < roots; ++index) {
167 double cubicT = rootVals[index];
168 SkDPoint pt = cubic.xyAtT(cubicT);
169 double lineT = (pt.fY - top) / (bottom - top);
170 if (pinTs(&cubicT, &lineT)) {
171 intersections.insert(cubicT, lineT, pt);
172 }
173 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000174 if (fAllowNear) {
175 addNearVerticalEndPoints(top, bottom, axisIntercept);
176 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000177 if (flipped) {
178 intersections.flip();
179 }
180 return intersections.used();
181}
182
183protected:
184
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000185void addExactEndPoints() {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000186 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000187 double lineT = line.exactPoint(cubic[cIndex]);
188 if (lineT < 0) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000189 continue;
190 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000191 double cubicT = (double) (cIndex >> 1);
192 intersections.insert(cubicT, lineT, cubic[cIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000193 }
194}
195
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000196void addNearEndPoints() {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000197 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000198 double cubicT = (double) (cIndex >> 1);
199 if (intersections.hasT(cubicT)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000200 continue;
201 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000202 double lineT = line.nearPoint(cubic[cIndex]);
203 if (lineT < 0) {
204 continue;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000205 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000206 intersections.insert(cubicT, lineT, cubic[cIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000207 }
208}
209
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000210void addExactHorizontalEndPoints(double left, double right, double y) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000211 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000212 double lineT = SkDLine::ExactPointH(cubic[cIndex], left, right, y);
213 if (lineT < 0) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000214 continue;
215 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000216 double cubicT = (double) (cIndex >> 1);
217 intersections.insert(cubicT, lineT, cubic[cIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000218 }
219}
220
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000221void addNearHorizontalEndPoints(double left, double right, double y) {
222 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
223 double cubicT = (double) (cIndex >> 1);
224 if (intersections.hasT(cubicT)) {
225 continue;
226 }
227 double lineT = SkDLine::NearPointH(cubic[cIndex], left, right, y);
228 if (lineT < 0) {
229 continue;
230 }
231 intersections.insert(cubicT, lineT, cubic[cIndex]);
232 }
233 // FIXME: see if line end is nearly on cubic
234}
235
236void addExactVerticalEndPoints(double top, double bottom, double x) {
237 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
238 double lineT = SkDLine::ExactPointV(cubic[cIndex], top, bottom, x);
239 if (lineT < 0) {
240 continue;
241 }
242 double cubicT = (double) (cIndex >> 1);
243 intersections.insert(cubicT, lineT, cubic[cIndex]);
244 }
245}
246
247void addNearVerticalEndPoints(double top, double bottom, double x) {
248 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
249 double cubicT = (double) (cIndex >> 1);
250 if (intersections.hasT(cubicT)) {
251 continue;
252 }
253 double lineT = SkDLine::NearPointV(cubic[cIndex], top, bottom, x);
254 if (lineT < 0) {
255 continue;
256 }
257 intersections.insert(cubicT, lineT, cubic[cIndex]);
258 }
259 // FIXME: see if line end is nearly on cubic
260}
261
caryclark@google.com07393ca2013-04-08 11:47:37 +0000262double findLineT(double t) {
263 SkDPoint xy = cubic.xyAtT(t);
264 double dx = line[1].fX - line[0].fX;
265 double dy = line[1].fY - line[0].fY;
266 if (fabs(dx) > fabs(dy)) {
267 return (xy.fX - line[0].fX) / dx;
268 }
269 return (xy.fY - line[0].fY) / dy;
270}
271
272static bool pinTs(double* cubicT, double* lineT) {
273 if (!approximately_one_or_less(*lineT)) {
274 return false;
275 }
276 if (!approximately_zero_or_more(*lineT)) {
277 return false;
278 }
279 if (precisely_less_than_zero(*cubicT)) {
280 *cubicT = 0;
281 } else if (precisely_greater_than_one(*cubicT)) {
282 *cubicT = 1;
283 }
284 if (precisely_less_than_zero(*lineT)) {
285 *lineT = 0;
286 } else if (precisely_greater_than_one(*lineT)) {
287 *lineT = 1;
288 }
289 return true;
290}
291
292private:
293
294const SkDCubic& cubic;
295const SkDLine& line;
296SkIntersections& intersections;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000297bool fAllowNear;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000298};
299
300int SkIntersections::horizontal(const SkDCubic& cubic, double left, double right, double y,
301 bool flipped) {
302 LineCubicIntersections c(cubic, *(static_cast<SkDLine*>(0)), *this);
303 return c.horizontalIntersect(y, left, right, flipped);
304}
305
306int SkIntersections::vertical(const SkDCubic& cubic, double top, double bottom, double x,
307 bool flipped) {
308 LineCubicIntersections c(cubic, *(static_cast<SkDLine*>(0)), *this);
309 return c.verticalIntersect(x, top, bottom, flipped);
310}
311
312int SkIntersections::intersect(const SkDCubic& cubic, const SkDLine& line) {
313 LineCubicIntersections c(cubic, line, *this);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000314 c.allowNear(fAllowNear);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000315 return c.intersect();
316}
317
318int SkIntersections::intersectRay(const SkDCubic& cubic, const SkDLine& line) {
319 LineCubicIntersections c(cubic, line, *this);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000320 fUsed = c.intersectRay(fT[0]);
321 for (int index = 0; index < fUsed; ++index) {
322 fPt[index] = cubic.xyAtT(fT[0][index]);
323 }
324 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000325}