blob: 418e107e4170ffe9ad8e78b50d8435821bdbc223 [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)
83 , intersections(i) {
84}
85
86// see parallel routine in line quadratic intersections
87int intersectRay(double roots[3]) {
88 double adj = line[1].fX - line[0].fX;
89 double opp = line[1].fY - line[0].fY;
90 SkDCubic r;
91 for (int n = 0; n < 4; ++n) {
92 r[n].fX = (cubic[n].fY - line[0].fY) * adj - (cubic[n].fX - line[0].fX) * opp;
93 }
94 double A, B, C, D;
95 SkDCubic::Coefficients(&r[0].fX, &A, &B, &C, &D);
96 return SkDCubic::RootsValidT(A, B, C, D, roots);
97}
98
99int intersect() {
100 addEndPoints();
101 double rootVals[3];
102 int roots = intersectRay(rootVals);
103 for (int index = 0; index < roots; ++index) {
104 double cubicT = rootVals[index];
105 double lineT = findLineT(cubicT);
106 if (pinTs(&cubicT, &lineT)) {
107 SkDPoint pt = line.xyAtT(lineT);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000108#if ONE_OFF_DEBUG
109 SkDPoint cPt = cubic.xyAtT(cubicT);
110 SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY,
111 cPt.fX, cPt.fY);
112#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113 intersections.insert(cubicT, lineT, pt);
114 }
115 }
116 return intersections.used();
117}
118
119int horizontalIntersect(double axisIntercept, double roots[3]) {
120 double A, B, C, D;
121 SkDCubic::Coefficients(&cubic[0].fY, &A, &B, &C, &D);
122 D -= axisIntercept;
123 return SkDCubic::RootsValidT(A, B, C, D, roots);
124}
125
126int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
127 addHorizontalEndPoints(left, right, axisIntercept);
128 double rootVals[3];
129 int roots = horizontalIntersect(axisIntercept, rootVals);
130 for (int index = 0; index < roots; ++index) {
131 double cubicT = rootVals[index];
132 SkDPoint pt = cubic.xyAtT(cubicT);
133 double lineT = (pt.fX - left) / (right - left);
134 if (pinTs(&cubicT, &lineT)) {
135 intersections.insert(cubicT, lineT, pt);
136 }
137 }
138 if (flipped) {
139 intersections.flip();
140 }
141 return intersections.used();
142}
143
144int verticalIntersect(double axisIntercept, double roots[3]) {
145 double A, B, C, D;
146 SkDCubic::Coefficients(&cubic[0].fX, &A, &B, &C, &D);
147 D -= axisIntercept;
148 return SkDCubic::RootsValidT(A, B, C, D, roots);
149}
150
151int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
152 addVerticalEndPoints(top, bottom, axisIntercept);
153 double rootVals[3];
154 int roots = verticalIntersect(axisIntercept, rootVals);
155 for (int index = 0; index < roots; ++index) {
156 double cubicT = rootVals[index];
157 SkDPoint pt = cubic.xyAtT(cubicT);
158 double lineT = (pt.fY - top) / (bottom - top);
159 if (pinTs(&cubicT, &lineT)) {
160 intersections.insert(cubicT, lineT, pt);
161 }
162 }
163 if (flipped) {
164 intersections.flip();
165 }
166 return intersections.used();
167}
168
169protected:
170
171void addEndPoints() {
172 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000173 bool foundEnd = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000174 for (int lIndex = 0; lIndex < 2; lIndex++) {
175 if (cubic[cIndex] == line[lIndex]) {
176 intersections.insert(cIndex >> 1, lIndex, line[lIndex]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000177 foundEnd = true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000178 }
179 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000180 // for the test case this was written for, the dist / error ratio was 170.6667
181 // it looks like the cubic stops short of touching the line, but this fixed it.
182 if (foundEnd) {
183 continue;
184 }
185 // See if the cubic end touches the line.
186 double dist = line.isLeft(cubic[cIndex]); // this distance isn't cartesian
187 SkDVector lineLen = line[1] - line[0]; // the x/y magnitudes of the line
188 // compute the ULPS of the larger of the x/y deltas
189 double larger = SkTMax(SkTAbs(lineLen.fX), SkTAbs(lineLen.fY));
190 if (!RoughlyEqualUlps(larger, larger + dist)) { // is the dist within ULPS tolerance?
191 continue;
192 }
193 double lineT = findLineT(cIndex >> 1);
194 if (!between(0, lineT, 1)) {
195 continue;
196 }
197 SkDPoint linePt = line.xyAtT(lineT);
198 if (linePt.approximatelyEqual(cubic[cIndex])) {
199 intersections.insert(cIndex >> 1, lineT, cubic[cIndex]);
200 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000201 }
202}
203
204void addHorizontalEndPoints(double left, double right, double y) {
205 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
206 if (cubic[cIndex].fY != y) {
207 continue;
208 }
209 if (cubic[cIndex].fX == left) {
210 intersections.insert(cIndex >> 1, 0, cubic[cIndex]);
211 }
212 if (cubic[cIndex].fX == right) {
213 intersections.insert(cIndex >> 1, 1, cubic[cIndex]);
214 }
215 }
216}
217
218void addVerticalEndPoints(double top, double bottom, double x) {
219 for (int cIndex = 0; cIndex < 4; cIndex += 3) {
220 if (cubic[cIndex].fX != x) {
221 continue;
222 }
223 if (cubic[cIndex].fY == top) {
224 intersections.insert(cIndex >> 1, 0, cubic[cIndex]);
225 }
226 if (cubic[cIndex].fY == bottom) {
227 intersections.insert(cIndex >> 1, 1, cubic[cIndex]);
228 }
229 }
230}
231
232double findLineT(double t) {
233 SkDPoint xy = cubic.xyAtT(t);
234 double dx = line[1].fX - line[0].fX;
235 double dy = line[1].fY - line[0].fY;
236 if (fabs(dx) > fabs(dy)) {
237 return (xy.fX - line[0].fX) / dx;
238 }
239 return (xy.fY - line[0].fY) / dy;
240}
241
242static bool pinTs(double* cubicT, double* lineT) {
243 if (!approximately_one_or_less(*lineT)) {
244 return false;
245 }
246 if (!approximately_zero_or_more(*lineT)) {
247 return false;
248 }
249 if (precisely_less_than_zero(*cubicT)) {
250 *cubicT = 0;
251 } else if (precisely_greater_than_one(*cubicT)) {
252 *cubicT = 1;
253 }
254 if (precisely_less_than_zero(*lineT)) {
255 *lineT = 0;
256 } else if (precisely_greater_than_one(*lineT)) {
257 *lineT = 1;
258 }
259 return true;
260}
261
262private:
263
264const SkDCubic& cubic;
265const SkDLine& line;
266SkIntersections& intersections;
267};
268
269int SkIntersections::horizontal(const SkDCubic& cubic, double left, double right, double y,
270 bool flipped) {
271 LineCubicIntersections c(cubic, *(static_cast<SkDLine*>(0)), *this);
272 return c.horizontalIntersect(y, left, right, flipped);
273}
274
275int SkIntersections::vertical(const SkDCubic& cubic, double top, double bottom, double x,
276 bool flipped) {
277 LineCubicIntersections c(cubic, *(static_cast<SkDLine*>(0)), *this);
278 return c.verticalIntersect(x, top, bottom, flipped);
279}
280
281int SkIntersections::intersect(const SkDCubic& cubic, const SkDLine& line) {
282 LineCubicIntersections c(cubic, line, *this);
283 return c.intersect();
284}
285
286int SkIntersections::intersectRay(const SkDCubic& cubic, const SkDLine& line) {
287 LineCubicIntersections c(cubic, line, *this);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000288 fUsed = c.intersectRay(fT[0]);
289 for (int index = 0; index < fUsed; ++index) {
290 fPt[index] = cubic.xyAtT(fT[0][index]);
291 }
292 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000293}