blob: 0abb75b394984476cfcf644ff6e4e0a0a513cf47 [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:
caryclark@google.com4fdbb222013-07-23 15:27:41 +000079 enum PinTPoint {
80 kPointUninitialized,
81 kPointInitialized
82 };
caryclark@google.com07393ca2013-04-08 11:47:37 +000083
caryclark@google.com4fdbb222013-07-23 15:27:41 +000084 LineCubicIntersections(const SkDCubic& c, const SkDLine& l, SkIntersections* i)
85 : fCubic(c)
86 , fLine(l)
87 , fIntersections(i)
88 , fAllowNear(true) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000089 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000090
caryclark@google.com4fdbb222013-07-23 15:27:41 +000091 void allowNear(bool allow) {
92 fAllowNear = allow;
93 }
94
95 // see parallel routine in line quadratic intersections
96 int intersectRay(double roots[3]) {
97 double adj = fLine[1].fX - fLine[0].fX;
98 double opp = fLine[1].fY - fLine[0].fY;
99 SkDCubic r;
100 for (int n = 0; n < 4; ++n) {
101 r[n].fX = (fCubic[n].fY - fLine[0].fY) * adj - (fCubic[n].fX - fLine[0].fX) * opp;
102 }
103 double A, B, C, D;
104 SkDCubic::Coefficients(&r[0].fX, &A, &B, &C, &D);
105 return SkDCubic::RootsValidT(A, B, C, D, roots);
106 }
107
108 int intersect() {
109 addExactEndPoints();
caryclark@google.com570863f2013-09-16 15:55:01 +0000110 if (fAllowNear) {
111 addNearEndPoints();
112 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000113 double rootVals[3];
114 int roots = intersectRay(rootVals);
115 for (int index = 0; index < roots; ++index) {
116 double cubicT = rootVals[index];
117 double lineT = findLineT(cubicT);
118 SkDPoint pt;
119 if (pinTs(&cubicT, &lineT, &pt, kPointUninitialized)) {
120 #if ONE_OFF_DEBUG
121 SkDPoint cPt = fCubic.ptAtT(cubicT);
122 SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY,
123 cPt.fX, cPt.fY);
124 #endif
125 fIntersections->insert(cubicT, lineT, pt);
126 }
127 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000128 return fIntersections->used();
129 }
130
131 int horizontalIntersect(double axisIntercept, double roots[3]) {
132 double A, B, C, D;
133 SkDCubic::Coefficients(&fCubic[0].fY, &A, &B, &C, &D);
134 D -= axisIntercept;
135 return SkDCubic::RootsValidT(A, B, C, D, roots);
136 }
137
138 int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
139 addExactHorizontalEndPoints(left, right, axisIntercept);
caryclark@google.com570863f2013-09-16 15:55:01 +0000140 if (fAllowNear) {
141 addNearHorizontalEndPoints(left, right, axisIntercept);
142 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000143 double rootVals[3];
144 int roots = horizontalIntersect(axisIntercept, rootVals);
145 for (int index = 0; index < roots; ++index) {
146 double cubicT = rootVals[index];
147 SkDPoint pt = fCubic.ptAtT(cubicT);
148 double lineT = (pt.fX - left) / (right - left);
149 if (pinTs(&cubicT, &lineT, &pt, kPointInitialized)) {
150 fIntersections->insert(cubicT, lineT, pt);
151 }
152 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000153 if (flipped) {
154 fIntersections->flip();
155 }
156 return fIntersections->used();
157 }
158
159 int verticalIntersect(double axisIntercept, double roots[3]) {
160 double A, B, C, D;
161 SkDCubic::Coefficients(&fCubic[0].fX, &A, &B, &C, &D);
162 D -= axisIntercept;
163 return SkDCubic::RootsValidT(A, B, C, D, roots);
164 }
165
166 int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
167 addExactVerticalEndPoints(top, bottom, axisIntercept);
caryclark@google.com570863f2013-09-16 15:55:01 +0000168 if (fAllowNear) {
169 addNearVerticalEndPoints(top, bottom, axisIntercept);
170 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000171 double rootVals[3];
172 int roots = verticalIntersect(axisIntercept, rootVals);
173 for (int index = 0; index < roots; ++index) {
174 double cubicT = rootVals[index];
175 SkDPoint pt = fCubic.ptAtT(cubicT);
176 double lineT = (pt.fY - top) / (bottom - top);
177 if (pinTs(&cubicT, &lineT, &pt, kPointInitialized)) {
178 fIntersections->insert(cubicT, lineT, pt);
179 }
180 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000181 if (flipped) {
182 fIntersections->flip();
183 }
184 return fIntersections->used();
185 }
186
187 protected:
188
189 void addExactEndPoints() {
190 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
191 double lineT = fLine.exactPoint(fCubic[cIndex]);
192 if (lineT < 0) {
193 continue;
194 }
195 double cubicT = (double) (cIndex >> 1);
196 fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000197 }
198 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000199
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000200 void addNearEndPoints() {
201 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
202 double cubicT = (double) (cIndex >> 1);
203 if (fIntersections->hasT(cubicT)) {
204 continue;
205 }
206 double lineT = fLine.nearPoint(fCubic[cIndex]);
207 if (lineT < 0) {
208 continue;
209 }
210 fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000211 }
212 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000213
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000214 void addExactHorizontalEndPoints(double left, double right, double y) {
215 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
216 double lineT = SkDLine::ExactPointH(fCubic[cIndex], left, right, y);
217 if (lineT < 0) {
218 continue;
219 }
220 double cubicT = (double) (cIndex >> 1);
221 fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000222 }
223 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000224
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000225 void addNearHorizontalEndPoints(double left, double right, double y) {
226 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
227 double cubicT = (double) (cIndex >> 1);
228 if (fIntersections->hasT(cubicT)) {
229 continue;
230 }
231 double lineT = SkDLine::NearPointH(fCubic[cIndex], left, right, y);
232 if (lineT < 0) {
233 continue;
234 }
235 fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
236 }
237 // FIXME: see if line end is nearly on cubic
238 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000239
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000240 void addExactVerticalEndPoints(double top, double bottom, double x) {
241 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
242 double lineT = SkDLine::ExactPointV(fCubic[cIndex], top, bottom, x);
243 if (lineT < 0) {
244 continue;
245 }
246 double cubicT = (double) (cIndex >> 1);
247 fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000248 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000249 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000250
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000251 void addNearVerticalEndPoints(double top, double bottom, double x) {
252 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
253 double cubicT = (double) (cIndex >> 1);
254 if (fIntersections->hasT(cubicT)) {
255 continue;
256 }
257 double lineT = SkDLine::NearPointV(fCubic[cIndex], top, bottom, x);
258 if (lineT < 0) {
259 continue;
260 }
261 fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000262 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000263 // FIXME: see if line end is nearly on cubic
caryclark@google.com07393ca2013-04-08 11:47:37 +0000264 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000265
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000266 double findLineT(double t) {
267 SkDPoint xy = fCubic.ptAtT(t);
268 double dx = fLine[1].fX - fLine[0].fX;
269 double dy = fLine[1].fY - fLine[0].fY;
270 if (fabs(dx) > fabs(dy)) {
271 return (xy.fX - fLine[0].fX) / dx;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000272 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000273 return (xy.fY - fLine[0].fY) / dy;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000274 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000275
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000276 bool pinTs(double* cubicT, double* lineT, SkDPoint* pt, PinTPoint ptSet) {
277 if (!approximately_one_or_less(*lineT)) {
278 return false;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000279 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000280 if (!approximately_zero_or_more(*lineT)) {
281 return false;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000282 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000283 double cT = *cubicT = SkPinT(*cubicT);
284 double lT = *lineT = SkPinT(*lineT);
285 if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && cT != 0 && cT != 1)) {
286 *pt = fLine.ptAtT(lT);
287 } else if (ptSet == kPointUninitialized) {
288 *pt = fCubic.ptAtT(cT);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000289 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000290 SkPoint gridPt = pt->asSkPoint();
291 if (gridPt == fLine[0].asSkPoint()) {
292 *lineT = 0;
293 } else if (gridPt == fLine[1].asSkPoint()) {
294 *lineT = 1;
295 }
296 if (gridPt == fCubic[0].asSkPoint() && approximately_equal(*cubicT, 0)) {
297 *cubicT = 0;
298 } else if (gridPt == fCubic[3].asSkPoint() && approximately_equal(*cubicT, 1)) {
299 *cubicT = 1;
300 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000301 return true;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000302 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000303
304private:
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000305 const SkDCubic& fCubic;
306 const SkDLine& fLine;
307 SkIntersections* fIntersections;
308 bool fAllowNear;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000309};
310
311int SkIntersections::horizontal(const SkDCubic& cubic, double left, double right, double y,
312 bool flipped) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000313 SkDLine line = {{{ left, y }, { right, y }}};
314 LineCubicIntersections c(cubic, line, this);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000315 return c.horizontalIntersect(y, left, right, flipped);
316}
317
318int SkIntersections::vertical(const SkDCubic& cubic, double top, double bottom, double x,
319 bool flipped) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000320 SkDLine line = {{{ x, top }, { x, bottom }}};
321 LineCubicIntersections c(cubic, line, this);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000322 return c.verticalIntersect(x, top, bottom, flipped);
323}
324
325int SkIntersections::intersect(const SkDCubic& cubic, const SkDLine& line) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000326 LineCubicIntersections c(cubic, line, this);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000327 c.allowNear(fAllowNear);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000328 return c.intersect();
329}
330
331int SkIntersections::intersectRay(const SkDCubic& cubic, const SkDLine& line) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000332 LineCubicIntersections c(cubic, line, this);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000333 fUsed = c.intersectRay(fT[0]);
334 for (int index = 0; index < fUsed; ++index) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000335 fPt[index] = cubic.ptAtT(fT[0][index]);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000336 }
337 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000338}