1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2012 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkPath.h"
8*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkPoint.h"
9*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkTypes.h"
10*c8dee2aaSAndroid Build Coastguard Worker #include "include/private/base/SkDebug.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "src/pathops/SkIntersections.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "src/pathops/SkPathOpsCubic.h"
13*c8dee2aaSAndroid Build Coastguard Worker #include "src/pathops/SkPathOpsCurve.h"
14*c8dee2aaSAndroid Build Coastguard Worker #include "src/pathops/SkPathOpsDebug.h"
15*c8dee2aaSAndroid Build Coastguard Worker #include "src/pathops/SkPathOpsLine.h"
16*c8dee2aaSAndroid Build Coastguard Worker #include "src/pathops/SkPathOpsPoint.h"
17*c8dee2aaSAndroid Build Coastguard Worker #include "src/pathops/SkPathOpsTypes.h"
18*c8dee2aaSAndroid Build Coastguard Worker
19*c8dee2aaSAndroid Build Coastguard Worker #include <cmath>
20*c8dee2aaSAndroid Build Coastguard Worker
21*c8dee2aaSAndroid Build Coastguard Worker /*
22*c8dee2aaSAndroid Build Coastguard Worker Find the intersection of a line and cubic by solving for valid t values.
23*c8dee2aaSAndroid Build Coastguard Worker
24*c8dee2aaSAndroid Build Coastguard Worker Analogous to line-quadratic intersection, solve line-cubic intersection by
25*c8dee2aaSAndroid Build Coastguard Worker representing the cubic as:
26*c8dee2aaSAndroid Build Coastguard Worker x = a(1-t)^3 + 2b(1-t)^2t + c(1-t)t^2 + dt^3
27*c8dee2aaSAndroid Build Coastguard Worker y = e(1-t)^3 + 2f(1-t)^2t + g(1-t)t^2 + ht^3
28*c8dee2aaSAndroid Build Coastguard Worker and the line as:
29*c8dee2aaSAndroid Build Coastguard Worker y = i*x + j (if the line is more horizontal)
30*c8dee2aaSAndroid Build Coastguard Worker or:
31*c8dee2aaSAndroid Build Coastguard Worker x = i*y + j (if the line is more vertical)
32*c8dee2aaSAndroid Build Coastguard Worker
33*c8dee2aaSAndroid Build Coastguard Worker Then using Mathematica, solve for the values of t where the cubic intersects the
34*c8dee2aaSAndroid Build Coastguard Worker line:
35*c8dee2aaSAndroid Build Coastguard Worker
36*c8dee2aaSAndroid Build Coastguard Worker (in) Resultant[
37*c8dee2aaSAndroid Build Coastguard Worker a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - x,
38*c8dee2aaSAndroid Build Coastguard Worker e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - i*x - j, x]
39*c8dee2aaSAndroid Build Coastguard Worker (out) -e + j +
40*c8dee2aaSAndroid Build Coastguard Worker 3 e t - 3 f t -
41*c8dee2aaSAndroid Build Coastguard Worker 3 e t^2 + 6 f t^2 - 3 g t^2 +
42*c8dee2aaSAndroid Build Coastguard Worker e t^3 - 3 f t^3 + 3 g t^3 - h t^3 +
43*c8dee2aaSAndroid Build Coastguard Worker i ( a -
44*c8dee2aaSAndroid Build Coastguard Worker 3 a t + 3 b t +
45*c8dee2aaSAndroid Build Coastguard Worker 3 a t^2 - 6 b t^2 + 3 c t^2 -
46*c8dee2aaSAndroid Build Coastguard Worker a t^3 + 3 b t^3 - 3 c t^3 + d t^3 )
47*c8dee2aaSAndroid Build Coastguard Worker
48*c8dee2aaSAndroid Build Coastguard Worker if i goes to infinity, we can rewrite the line in terms of x. Mathematica:
49*c8dee2aaSAndroid Build Coastguard Worker
50*c8dee2aaSAndroid Build Coastguard Worker (in) Resultant[
51*c8dee2aaSAndroid Build Coastguard Worker a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - i*y - j,
52*c8dee2aaSAndroid Build Coastguard Worker e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y]
53*c8dee2aaSAndroid Build Coastguard Worker (out) a - j -
54*c8dee2aaSAndroid Build Coastguard Worker 3 a t + 3 b t +
55*c8dee2aaSAndroid Build Coastguard Worker 3 a t^2 - 6 b t^2 + 3 c t^2 -
56*c8dee2aaSAndroid Build Coastguard Worker a t^3 + 3 b t^3 - 3 c t^3 + d t^3 -
57*c8dee2aaSAndroid Build Coastguard Worker i ( e -
58*c8dee2aaSAndroid Build Coastguard Worker 3 e t + 3 f t +
59*c8dee2aaSAndroid Build Coastguard Worker 3 e t^2 - 6 f t^2 + 3 g t^2 -
60*c8dee2aaSAndroid Build Coastguard Worker e t^3 + 3 f t^3 - 3 g t^3 + h t^3 )
61*c8dee2aaSAndroid Build Coastguard Worker
62*c8dee2aaSAndroid Build Coastguard Worker Solving this with Mathematica produces an expression with hundreds of terms;
63*c8dee2aaSAndroid Build Coastguard Worker instead, use Numeric Solutions recipe to solve the cubic.
64*c8dee2aaSAndroid Build Coastguard Worker
65*c8dee2aaSAndroid Build Coastguard Worker The near-horizontal case, in terms of: Ax^3 + Bx^2 + Cx + D == 0
66*c8dee2aaSAndroid Build Coastguard Worker A = (-(-e + 3*f - 3*g + h) + i*(-a + 3*b - 3*c + d) )
67*c8dee2aaSAndroid Build Coastguard Worker B = 3*(-( e - 2*f + g ) + i*( a - 2*b + c ) )
68*c8dee2aaSAndroid Build Coastguard Worker C = 3*(-(-e + f ) + i*(-a + b ) )
69*c8dee2aaSAndroid Build Coastguard Worker D = (-( e ) + i*( a ) + j )
70*c8dee2aaSAndroid Build Coastguard Worker
71*c8dee2aaSAndroid Build Coastguard Worker The near-vertical case, in terms of: Ax^3 + Bx^2 + Cx + D == 0
72*c8dee2aaSAndroid Build Coastguard Worker A = ( (-a + 3*b - 3*c + d) - i*(-e + 3*f - 3*g + h) )
73*c8dee2aaSAndroid Build Coastguard Worker B = 3*( ( a - 2*b + c ) - i*( e - 2*f + g ) )
74*c8dee2aaSAndroid Build Coastguard Worker C = 3*( (-a + b ) - i*(-e + f ) )
75*c8dee2aaSAndroid Build Coastguard Worker D = ( ( a ) - i*( e ) - j )
76*c8dee2aaSAndroid Build Coastguard Worker
77*c8dee2aaSAndroid Build Coastguard Worker For horizontal lines:
78*c8dee2aaSAndroid Build Coastguard Worker (in) Resultant[
79*c8dee2aaSAndroid Build Coastguard Worker a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - j,
80*c8dee2aaSAndroid Build Coastguard Worker e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y]
81*c8dee2aaSAndroid Build Coastguard Worker (out) e - j -
82*c8dee2aaSAndroid Build Coastguard Worker 3 e t + 3 f t +
83*c8dee2aaSAndroid Build Coastguard Worker 3 e t^2 - 6 f t^2 + 3 g t^2 -
84*c8dee2aaSAndroid Build Coastguard Worker e t^3 + 3 f t^3 - 3 g t^3 + h t^3
85*c8dee2aaSAndroid Build Coastguard Worker */
86*c8dee2aaSAndroid Build Coastguard Worker
87*c8dee2aaSAndroid Build Coastguard Worker class LineCubicIntersections {
88*c8dee2aaSAndroid Build Coastguard Worker public:
89*c8dee2aaSAndroid Build Coastguard Worker enum PinTPoint {
90*c8dee2aaSAndroid Build Coastguard Worker kPointUninitialized,
91*c8dee2aaSAndroid Build Coastguard Worker kPointInitialized
92*c8dee2aaSAndroid Build Coastguard Worker };
93*c8dee2aaSAndroid Build Coastguard Worker
LineCubicIntersections(const SkDCubic & c,const SkDLine & l,SkIntersections * i)94*c8dee2aaSAndroid Build Coastguard Worker LineCubicIntersections(const SkDCubic& c, const SkDLine& l, SkIntersections* i)
95*c8dee2aaSAndroid Build Coastguard Worker : fCubic(c)
96*c8dee2aaSAndroid Build Coastguard Worker , fLine(l)
97*c8dee2aaSAndroid Build Coastguard Worker , fIntersections(i)
98*c8dee2aaSAndroid Build Coastguard Worker , fAllowNear(true) {
99*c8dee2aaSAndroid Build Coastguard Worker i->setMax(4);
100*c8dee2aaSAndroid Build Coastguard Worker }
101*c8dee2aaSAndroid Build Coastguard Worker
allowNear(bool allow)102*c8dee2aaSAndroid Build Coastguard Worker void allowNear(bool allow) {
103*c8dee2aaSAndroid Build Coastguard Worker fAllowNear = allow;
104*c8dee2aaSAndroid Build Coastguard Worker }
105*c8dee2aaSAndroid Build Coastguard Worker
checkCoincident()106*c8dee2aaSAndroid Build Coastguard Worker void checkCoincident() {
107*c8dee2aaSAndroid Build Coastguard Worker int last = fIntersections->used() - 1;
108*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < last; ) {
109*c8dee2aaSAndroid Build Coastguard Worker double cubicMidT = ((*fIntersections)[0][index] + (*fIntersections)[0][index + 1]) / 2;
110*c8dee2aaSAndroid Build Coastguard Worker SkDPoint cubicMidPt = fCubic.ptAtT(cubicMidT);
111*c8dee2aaSAndroid Build Coastguard Worker double t = fLine.nearPoint(cubicMidPt, nullptr);
112*c8dee2aaSAndroid Build Coastguard Worker if (t < 0) {
113*c8dee2aaSAndroid Build Coastguard Worker ++index;
114*c8dee2aaSAndroid Build Coastguard Worker continue;
115*c8dee2aaSAndroid Build Coastguard Worker }
116*c8dee2aaSAndroid Build Coastguard Worker if (fIntersections->isCoincident(index)) {
117*c8dee2aaSAndroid Build Coastguard Worker fIntersections->removeOne(index);
118*c8dee2aaSAndroid Build Coastguard Worker --last;
119*c8dee2aaSAndroid Build Coastguard Worker } else if (fIntersections->isCoincident(index + 1)) {
120*c8dee2aaSAndroid Build Coastguard Worker fIntersections->removeOne(index + 1);
121*c8dee2aaSAndroid Build Coastguard Worker --last;
122*c8dee2aaSAndroid Build Coastguard Worker } else {
123*c8dee2aaSAndroid Build Coastguard Worker fIntersections->setCoincident(index++);
124*c8dee2aaSAndroid Build Coastguard Worker }
125*c8dee2aaSAndroid Build Coastguard Worker fIntersections->setCoincident(index);
126*c8dee2aaSAndroid Build Coastguard Worker }
127*c8dee2aaSAndroid Build Coastguard Worker }
128*c8dee2aaSAndroid Build Coastguard Worker
129*c8dee2aaSAndroid Build Coastguard Worker // see parallel routine in line quadratic intersections
intersectRay(double roots[3])130*c8dee2aaSAndroid Build Coastguard Worker int intersectRay(double roots[3]) {
131*c8dee2aaSAndroid Build Coastguard Worker double adj = fLine[1].fX - fLine[0].fX;
132*c8dee2aaSAndroid Build Coastguard Worker double opp = fLine[1].fY - fLine[0].fY;
133*c8dee2aaSAndroid Build Coastguard Worker SkDCubic c;
134*c8dee2aaSAndroid Build Coastguard Worker SkDEBUGCODE(c.fDebugGlobalState = fIntersections->globalState());
135*c8dee2aaSAndroid Build Coastguard Worker for (int n = 0; n < 4; ++n) {
136*c8dee2aaSAndroid Build Coastguard Worker c[n].fX = (fCubic[n].fY - fLine[0].fY) * adj - (fCubic[n].fX - fLine[0].fX) * opp;
137*c8dee2aaSAndroid Build Coastguard Worker }
138*c8dee2aaSAndroid Build Coastguard Worker double A, B, C, D;
139*c8dee2aaSAndroid Build Coastguard Worker SkDCubic::Coefficients(&c[0].fX, &A, &B, &C, &D);
140*c8dee2aaSAndroid Build Coastguard Worker int count = SkDCubic::RootsValidT(A, B, C, D, roots);
141*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < count; ++index) {
142*c8dee2aaSAndroid Build Coastguard Worker SkDPoint calcPt = c.ptAtT(roots[index]);
143*c8dee2aaSAndroid Build Coastguard Worker if (!approximately_zero(calcPt.fX)) {
144*c8dee2aaSAndroid Build Coastguard Worker for (int n = 0; n < 4; ++n) {
145*c8dee2aaSAndroid Build Coastguard Worker c[n].fY = (fCubic[n].fY - fLine[0].fY) * opp
146*c8dee2aaSAndroid Build Coastguard Worker + (fCubic[n].fX - fLine[0].fX) * adj;
147*c8dee2aaSAndroid Build Coastguard Worker }
148*c8dee2aaSAndroid Build Coastguard Worker double extremeTs[6];
149*c8dee2aaSAndroid Build Coastguard Worker int extrema = SkDCubic::FindExtrema(&c[0].fX, extremeTs);
150*c8dee2aaSAndroid Build Coastguard Worker count = c.searchRoots(extremeTs, extrema, 0, SkDCubic::kXAxis, roots);
151*c8dee2aaSAndroid Build Coastguard Worker break;
152*c8dee2aaSAndroid Build Coastguard Worker }
153*c8dee2aaSAndroid Build Coastguard Worker }
154*c8dee2aaSAndroid Build Coastguard Worker return count;
155*c8dee2aaSAndroid Build Coastguard Worker }
156*c8dee2aaSAndroid Build Coastguard Worker
intersect()157*c8dee2aaSAndroid Build Coastguard Worker int intersect() {
158*c8dee2aaSAndroid Build Coastguard Worker addExactEndPoints();
159*c8dee2aaSAndroid Build Coastguard Worker if (fAllowNear) {
160*c8dee2aaSAndroid Build Coastguard Worker addNearEndPoints();
161*c8dee2aaSAndroid Build Coastguard Worker }
162*c8dee2aaSAndroid Build Coastguard Worker double rootVals[3];
163*c8dee2aaSAndroid Build Coastguard Worker int roots = intersectRay(rootVals);
164*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < roots; ++index) {
165*c8dee2aaSAndroid Build Coastguard Worker double cubicT = rootVals[index];
166*c8dee2aaSAndroid Build Coastguard Worker double lineT = findLineT(cubicT);
167*c8dee2aaSAndroid Build Coastguard Worker SkDPoint pt;
168*c8dee2aaSAndroid Build Coastguard Worker if (pinTs(&cubicT, &lineT, &pt, kPointUninitialized) && uniqueAnswer(cubicT, pt)) {
169*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, pt);
170*c8dee2aaSAndroid Build Coastguard Worker }
171*c8dee2aaSAndroid Build Coastguard Worker }
172*c8dee2aaSAndroid Build Coastguard Worker checkCoincident();
173*c8dee2aaSAndroid Build Coastguard Worker return fIntersections->used();
174*c8dee2aaSAndroid Build Coastguard Worker }
175*c8dee2aaSAndroid Build Coastguard Worker
HorizontalIntersect(const SkDCubic & c,double axisIntercept,double roots[3])176*c8dee2aaSAndroid Build Coastguard Worker static int HorizontalIntersect(const SkDCubic& c, double axisIntercept, double roots[3]) {
177*c8dee2aaSAndroid Build Coastguard Worker double A, B, C, D;
178*c8dee2aaSAndroid Build Coastguard Worker SkDCubic::Coefficients(&c[0].fY, &A, &B, &C, &D);
179*c8dee2aaSAndroid Build Coastguard Worker D -= axisIntercept;
180*c8dee2aaSAndroid Build Coastguard Worker int count = SkDCubic::RootsValidT(A, B, C, D, roots);
181*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < count; ++index) {
182*c8dee2aaSAndroid Build Coastguard Worker SkDPoint calcPt = c.ptAtT(roots[index]);
183*c8dee2aaSAndroid Build Coastguard Worker if (!approximately_equal(calcPt.fY, axisIntercept)) {
184*c8dee2aaSAndroid Build Coastguard Worker double extremeTs[6];
185*c8dee2aaSAndroid Build Coastguard Worker int extrema = SkDCubic::FindExtrema(&c[0].fY, extremeTs);
186*c8dee2aaSAndroid Build Coastguard Worker count = c.searchRoots(extremeTs, extrema, axisIntercept, SkDCubic::kYAxis, roots);
187*c8dee2aaSAndroid Build Coastguard Worker break;
188*c8dee2aaSAndroid Build Coastguard Worker }
189*c8dee2aaSAndroid Build Coastguard Worker }
190*c8dee2aaSAndroid Build Coastguard Worker return count;
191*c8dee2aaSAndroid Build Coastguard Worker }
192*c8dee2aaSAndroid Build Coastguard Worker
horizontalIntersect(double axisIntercept,double left,double right,bool flipped)193*c8dee2aaSAndroid Build Coastguard Worker int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
194*c8dee2aaSAndroid Build Coastguard Worker addExactHorizontalEndPoints(left, right, axisIntercept);
195*c8dee2aaSAndroid Build Coastguard Worker if (fAllowNear) {
196*c8dee2aaSAndroid Build Coastguard Worker addNearHorizontalEndPoints(left, right, axisIntercept);
197*c8dee2aaSAndroid Build Coastguard Worker }
198*c8dee2aaSAndroid Build Coastguard Worker double roots[3];
199*c8dee2aaSAndroid Build Coastguard Worker int count = HorizontalIntersect(fCubic, axisIntercept, roots);
200*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < count; ++index) {
201*c8dee2aaSAndroid Build Coastguard Worker double cubicT = roots[index];
202*c8dee2aaSAndroid Build Coastguard Worker SkDPoint pt = { fCubic.ptAtT(cubicT).fX, axisIntercept };
203*c8dee2aaSAndroid Build Coastguard Worker double lineT = (pt.fX - left) / (right - left);
204*c8dee2aaSAndroid Build Coastguard Worker if (pinTs(&cubicT, &lineT, &pt, kPointInitialized) && uniqueAnswer(cubicT, pt)) {
205*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, pt);
206*c8dee2aaSAndroid Build Coastguard Worker }
207*c8dee2aaSAndroid Build Coastguard Worker }
208*c8dee2aaSAndroid Build Coastguard Worker if (flipped) {
209*c8dee2aaSAndroid Build Coastguard Worker fIntersections->flip();
210*c8dee2aaSAndroid Build Coastguard Worker }
211*c8dee2aaSAndroid Build Coastguard Worker checkCoincident();
212*c8dee2aaSAndroid Build Coastguard Worker return fIntersections->used();
213*c8dee2aaSAndroid Build Coastguard Worker }
214*c8dee2aaSAndroid Build Coastguard Worker
uniqueAnswer(double cubicT,const SkDPoint & pt)215*c8dee2aaSAndroid Build Coastguard Worker bool uniqueAnswer(double cubicT, const SkDPoint& pt) {
216*c8dee2aaSAndroid Build Coastguard Worker for (int inner = 0; inner < fIntersections->used(); ++inner) {
217*c8dee2aaSAndroid Build Coastguard Worker if (fIntersections->pt(inner) != pt) {
218*c8dee2aaSAndroid Build Coastguard Worker continue;
219*c8dee2aaSAndroid Build Coastguard Worker }
220*c8dee2aaSAndroid Build Coastguard Worker double existingCubicT = (*fIntersections)[0][inner];
221*c8dee2aaSAndroid Build Coastguard Worker if (cubicT == existingCubicT) {
222*c8dee2aaSAndroid Build Coastguard Worker return false;
223*c8dee2aaSAndroid Build Coastguard Worker }
224*c8dee2aaSAndroid Build Coastguard Worker // check if midway on cubic is also same point. If so, discard this
225*c8dee2aaSAndroid Build Coastguard Worker double cubicMidT = (existingCubicT + cubicT) / 2;
226*c8dee2aaSAndroid Build Coastguard Worker SkDPoint cubicMidPt = fCubic.ptAtT(cubicMidT);
227*c8dee2aaSAndroid Build Coastguard Worker if (cubicMidPt.approximatelyEqual(pt)) {
228*c8dee2aaSAndroid Build Coastguard Worker return false;
229*c8dee2aaSAndroid Build Coastguard Worker }
230*c8dee2aaSAndroid Build Coastguard Worker }
231*c8dee2aaSAndroid Build Coastguard Worker #if ONE_OFF_DEBUG
232*c8dee2aaSAndroid Build Coastguard Worker SkDPoint cPt = fCubic.ptAtT(cubicT);
233*c8dee2aaSAndroid Build Coastguard Worker SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY,
234*c8dee2aaSAndroid Build Coastguard Worker cPt.fX, cPt.fY);
235*c8dee2aaSAndroid Build Coastguard Worker #endif
236*c8dee2aaSAndroid Build Coastguard Worker return true;
237*c8dee2aaSAndroid Build Coastguard Worker }
238*c8dee2aaSAndroid Build Coastguard Worker
VerticalIntersect(const SkDCubic & c,double axisIntercept,double roots[3])239*c8dee2aaSAndroid Build Coastguard Worker static int VerticalIntersect(const SkDCubic& c, double axisIntercept, double roots[3]) {
240*c8dee2aaSAndroid Build Coastguard Worker double A, B, C, D;
241*c8dee2aaSAndroid Build Coastguard Worker SkDCubic::Coefficients(&c[0].fX, &A, &B, &C, &D);
242*c8dee2aaSAndroid Build Coastguard Worker D -= axisIntercept;
243*c8dee2aaSAndroid Build Coastguard Worker int count = SkDCubic::RootsValidT(A, B, C, D, roots);
244*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < count; ++index) {
245*c8dee2aaSAndroid Build Coastguard Worker SkDPoint calcPt = c.ptAtT(roots[index]);
246*c8dee2aaSAndroid Build Coastguard Worker if (!approximately_equal(calcPt.fX, axisIntercept)) {
247*c8dee2aaSAndroid Build Coastguard Worker double extremeTs[6];
248*c8dee2aaSAndroid Build Coastguard Worker int extrema = SkDCubic::FindExtrema(&c[0].fX, extremeTs);
249*c8dee2aaSAndroid Build Coastguard Worker count = c.searchRoots(extremeTs, extrema, axisIntercept, SkDCubic::kXAxis, roots);
250*c8dee2aaSAndroid Build Coastguard Worker break;
251*c8dee2aaSAndroid Build Coastguard Worker }
252*c8dee2aaSAndroid Build Coastguard Worker }
253*c8dee2aaSAndroid Build Coastguard Worker return count;
254*c8dee2aaSAndroid Build Coastguard Worker }
255*c8dee2aaSAndroid Build Coastguard Worker
verticalIntersect(double axisIntercept,double top,double bottom,bool flipped)256*c8dee2aaSAndroid Build Coastguard Worker int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
257*c8dee2aaSAndroid Build Coastguard Worker addExactVerticalEndPoints(top, bottom, axisIntercept);
258*c8dee2aaSAndroid Build Coastguard Worker if (fAllowNear) {
259*c8dee2aaSAndroid Build Coastguard Worker addNearVerticalEndPoints(top, bottom, axisIntercept);
260*c8dee2aaSAndroid Build Coastguard Worker }
261*c8dee2aaSAndroid Build Coastguard Worker double roots[3];
262*c8dee2aaSAndroid Build Coastguard Worker int count = VerticalIntersect(fCubic, axisIntercept, roots);
263*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < count; ++index) {
264*c8dee2aaSAndroid Build Coastguard Worker double cubicT = roots[index];
265*c8dee2aaSAndroid Build Coastguard Worker SkDPoint pt = { axisIntercept, fCubic.ptAtT(cubicT).fY };
266*c8dee2aaSAndroid Build Coastguard Worker double lineT = (pt.fY - top) / (bottom - top);
267*c8dee2aaSAndroid Build Coastguard Worker if (pinTs(&cubicT, &lineT, &pt, kPointInitialized) && uniqueAnswer(cubicT, pt)) {
268*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, pt);
269*c8dee2aaSAndroid Build Coastguard Worker }
270*c8dee2aaSAndroid Build Coastguard Worker }
271*c8dee2aaSAndroid Build Coastguard Worker if (flipped) {
272*c8dee2aaSAndroid Build Coastguard Worker fIntersections->flip();
273*c8dee2aaSAndroid Build Coastguard Worker }
274*c8dee2aaSAndroid Build Coastguard Worker checkCoincident();
275*c8dee2aaSAndroid Build Coastguard Worker return fIntersections->used();
276*c8dee2aaSAndroid Build Coastguard Worker }
277*c8dee2aaSAndroid Build Coastguard Worker
278*c8dee2aaSAndroid Build Coastguard Worker protected:
279*c8dee2aaSAndroid Build Coastguard Worker
addExactEndPoints()280*c8dee2aaSAndroid Build Coastguard Worker void addExactEndPoints() {
281*c8dee2aaSAndroid Build Coastguard Worker for (int cIndex = 0; cIndex < 4; cIndex += 3) {
282*c8dee2aaSAndroid Build Coastguard Worker double lineT = fLine.exactPoint(fCubic[cIndex]);
283*c8dee2aaSAndroid Build Coastguard Worker if (lineT < 0) {
284*c8dee2aaSAndroid Build Coastguard Worker continue;
285*c8dee2aaSAndroid Build Coastguard Worker }
286*c8dee2aaSAndroid Build Coastguard Worker double cubicT = (double) (cIndex >> 1);
287*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
288*c8dee2aaSAndroid Build Coastguard Worker }
289*c8dee2aaSAndroid Build Coastguard Worker }
290*c8dee2aaSAndroid Build Coastguard Worker
291*c8dee2aaSAndroid Build Coastguard Worker /* Note that this does not look for endpoints of the line that are near the cubic.
292*c8dee2aaSAndroid Build Coastguard Worker These points are found later when check ends looks for missing points */
addNearEndPoints()293*c8dee2aaSAndroid Build Coastguard Worker void addNearEndPoints() {
294*c8dee2aaSAndroid Build Coastguard Worker for (int cIndex = 0; cIndex < 4; cIndex += 3) {
295*c8dee2aaSAndroid Build Coastguard Worker double cubicT = (double) (cIndex >> 1);
296*c8dee2aaSAndroid Build Coastguard Worker if (fIntersections->hasT(cubicT)) {
297*c8dee2aaSAndroid Build Coastguard Worker continue;
298*c8dee2aaSAndroid Build Coastguard Worker }
299*c8dee2aaSAndroid Build Coastguard Worker double lineT = fLine.nearPoint(fCubic[cIndex], nullptr);
300*c8dee2aaSAndroid Build Coastguard Worker if (lineT < 0) {
301*c8dee2aaSAndroid Build Coastguard Worker continue;
302*c8dee2aaSAndroid Build Coastguard Worker }
303*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
304*c8dee2aaSAndroid Build Coastguard Worker }
305*c8dee2aaSAndroid Build Coastguard Worker this->addLineNearEndPoints();
306*c8dee2aaSAndroid Build Coastguard Worker }
307*c8dee2aaSAndroid Build Coastguard Worker
addLineNearEndPoints()308*c8dee2aaSAndroid Build Coastguard Worker void addLineNearEndPoints() {
309*c8dee2aaSAndroid Build Coastguard Worker for (int lIndex = 0; lIndex < 2; ++lIndex) {
310*c8dee2aaSAndroid Build Coastguard Worker double lineT = (double) lIndex;
311*c8dee2aaSAndroid Build Coastguard Worker if (fIntersections->hasOppT(lineT)) {
312*c8dee2aaSAndroid Build Coastguard Worker continue;
313*c8dee2aaSAndroid Build Coastguard Worker }
314*c8dee2aaSAndroid Build Coastguard Worker double cubicT = ((const SkDCurve*)&fCubic)
315*c8dee2aaSAndroid Build Coastguard Worker ->nearPoint(SkPath::kCubic_Verb, fLine[lIndex], fLine[!lIndex]);
316*c8dee2aaSAndroid Build Coastguard Worker if (cubicT < 0) {
317*c8dee2aaSAndroid Build Coastguard Worker continue;
318*c8dee2aaSAndroid Build Coastguard Worker }
319*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, fLine[lIndex]);
320*c8dee2aaSAndroid Build Coastguard Worker }
321*c8dee2aaSAndroid Build Coastguard Worker }
322*c8dee2aaSAndroid Build Coastguard Worker
addExactHorizontalEndPoints(double left,double right,double y)323*c8dee2aaSAndroid Build Coastguard Worker void addExactHorizontalEndPoints(double left, double right, double y) {
324*c8dee2aaSAndroid Build Coastguard Worker for (int cIndex = 0; cIndex < 4; cIndex += 3) {
325*c8dee2aaSAndroid Build Coastguard Worker double lineT = SkDLine::ExactPointH(fCubic[cIndex], left, right, y);
326*c8dee2aaSAndroid Build Coastguard Worker if (lineT < 0) {
327*c8dee2aaSAndroid Build Coastguard Worker continue;
328*c8dee2aaSAndroid Build Coastguard Worker }
329*c8dee2aaSAndroid Build Coastguard Worker double cubicT = (double) (cIndex >> 1);
330*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
331*c8dee2aaSAndroid Build Coastguard Worker }
332*c8dee2aaSAndroid Build Coastguard Worker }
333*c8dee2aaSAndroid Build Coastguard Worker
addNearHorizontalEndPoints(double left,double right,double y)334*c8dee2aaSAndroid Build Coastguard Worker void addNearHorizontalEndPoints(double left, double right, double y) {
335*c8dee2aaSAndroid Build Coastguard Worker for (int cIndex = 0; cIndex < 4; cIndex += 3) {
336*c8dee2aaSAndroid Build Coastguard Worker double cubicT = (double) (cIndex >> 1);
337*c8dee2aaSAndroid Build Coastguard Worker if (fIntersections->hasT(cubicT)) {
338*c8dee2aaSAndroid Build Coastguard Worker continue;
339*c8dee2aaSAndroid Build Coastguard Worker }
340*c8dee2aaSAndroid Build Coastguard Worker double lineT = SkDLine::NearPointH(fCubic[cIndex], left, right, y);
341*c8dee2aaSAndroid Build Coastguard Worker if (lineT < 0) {
342*c8dee2aaSAndroid Build Coastguard Worker continue;
343*c8dee2aaSAndroid Build Coastguard Worker }
344*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
345*c8dee2aaSAndroid Build Coastguard Worker }
346*c8dee2aaSAndroid Build Coastguard Worker this->addLineNearEndPoints();
347*c8dee2aaSAndroid Build Coastguard Worker }
348*c8dee2aaSAndroid Build Coastguard Worker
addExactVerticalEndPoints(double top,double bottom,double x)349*c8dee2aaSAndroid Build Coastguard Worker void addExactVerticalEndPoints(double top, double bottom, double x) {
350*c8dee2aaSAndroid Build Coastguard Worker for (int cIndex = 0; cIndex < 4; cIndex += 3) {
351*c8dee2aaSAndroid Build Coastguard Worker double lineT = SkDLine::ExactPointV(fCubic[cIndex], top, bottom, x);
352*c8dee2aaSAndroid Build Coastguard Worker if (lineT < 0) {
353*c8dee2aaSAndroid Build Coastguard Worker continue;
354*c8dee2aaSAndroid Build Coastguard Worker }
355*c8dee2aaSAndroid Build Coastguard Worker double cubicT = (double) (cIndex >> 1);
356*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
357*c8dee2aaSAndroid Build Coastguard Worker }
358*c8dee2aaSAndroid Build Coastguard Worker }
359*c8dee2aaSAndroid Build Coastguard Worker
addNearVerticalEndPoints(double top,double bottom,double x)360*c8dee2aaSAndroid Build Coastguard Worker void addNearVerticalEndPoints(double top, double bottom, double x) {
361*c8dee2aaSAndroid Build Coastguard Worker for (int cIndex = 0; cIndex < 4; cIndex += 3) {
362*c8dee2aaSAndroid Build Coastguard Worker double cubicT = (double) (cIndex >> 1);
363*c8dee2aaSAndroid Build Coastguard Worker if (fIntersections->hasT(cubicT)) {
364*c8dee2aaSAndroid Build Coastguard Worker continue;
365*c8dee2aaSAndroid Build Coastguard Worker }
366*c8dee2aaSAndroid Build Coastguard Worker double lineT = SkDLine::NearPointV(fCubic[cIndex], top, bottom, x);
367*c8dee2aaSAndroid Build Coastguard Worker if (lineT < 0) {
368*c8dee2aaSAndroid Build Coastguard Worker continue;
369*c8dee2aaSAndroid Build Coastguard Worker }
370*c8dee2aaSAndroid Build Coastguard Worker fIntersections->insert(cubicT, lineT, fCubic[cIndex]);
371*c8dee2aaSAndroid Build Coastguard Worker }
372*c8dee2aaSAndroid Build Coastguard Worker this->addLineNearEndPoints();
373*c8dee2aaSAndroid Build Coastguard Worker }
374*c8dee2aaSAndroid Build Coastguard Worker
findLineT(double t)375*c8dee2aaSAndroid Build Coastguard Worker double findLineT(double t) {
376*c8dee2aaSAndroid Build Coastguard Worker SkDPoint xy = fCubic.ptAtT(t);
377*c8dee2aaSAndroid Build Coastguard Worker double dx = fLine[1].fX - fLine[0].fX;
378*c8dee2aaSAndroid Build Coastguard Worker double dy = fLine[1].fY - fLine[0].fY;
379*c8dee2aaSAndroid Build Coastguard Worker if (fabs(dx) > fabs(dy)) {
380*c8dee2aaSAndroid Build Coastguard Worker return (xy.fX - fLine[0].fX) / dx;
381*c8dee2aaSAndroid Build Coastguard Worker }
382*c8dee2aaSAndroid Build Coastguard Worker return (xy.fY - fLine[0].fY) / dy;
383*c8dee2aaSAndroid Build Coastguard Worker }
384*c8dee2aaSAndroid Build Coastguard Worker
pinTs(double * cubicT,double * lineT,SkDPoint * pt,PinTPoint ptSet)385*c8dee2aaSAndroid Build Coastguard Worker bool pinTs(double* cubicT, double* lineT, SkDPoint* pt, PinTPoint ptSet) {
386*c8dee2aaSAndroid Build Coastguard Worker if (!approximately_one_or_less(*lineT)) {
387*c8dee2aaSAndroid Build Coastguard Worker return false;
388*c8dee2aaSAndroid Build Coastguard Worker }
389*c8dee2aaSAndroid Build Coastguard Worker if (!approximately_zero_or_more(*lineT)) {
390*c8dee2aaSAndroid Build Coastguard Worker return false;
391*c8dee2aaSAndroid Build Coastguard Worker }
392*c8dee2aaSAndroid Build Coastguard Worker double cT = *cubicT = SkPinT(*cubicT);
393*c8dee2aaSAndroid Build Coastguard Worker double lT = *lineT = SkPinT(*lineT);
394*c8dee2aaSAndroid Build Coastguard Worker SkDPoint lPt = fLine.ptAtT(lT);
395*c8dee2aaSAndroid Build Coastguard Worker SkDPoint cPt = fCubic.ptAtT(cT);
396*c8dee2aaSAndroid Build Coastguard Worker if (!lPt.roughlyEqual(cPt)) {
397*c8dee2aaSAndroid Build Coastguard Worker return false;
398*c8dee2aaSAndroid Build Coastguard Worker }
399*c8dee2aaSAndroid Build Coastguard Worker // FIXME: if points are roughly equal but not approximately equal, need to do
400*c8dee2aaSAndroid Build Coastguard Worker // a binary search like quad/quad intersection to find more precise t values
401*c8dee2aaSAndroid Build Coastguard Worker if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && cT != 0 && cT != 1)) {
402*c8dee2aaSAndroid Build Coastguard Worker *pt = lPt;
403*c8dee2aaSAndroid Build Coastguard Worker } else if (ptSet == kPointUninitialized) {
404*c8dee2aaSAndroid Build Coastguard Worker *pt = cPt;
405*c8dee2aaSAndroid Build Coastguard Worker }
406*c8dee2aaSAndroid Build Coastguard Worker SkPoint gridPt = pt->asSkPoint();
407*c8dee2aaSAndroid Build Coastguard Worker if (gridPt == fLine[0].asSkPoint()) {
408*c8dee2aaSAndroid Build Coastguard Worker *lineT = 0;
409*c8dee2aaSAndroid Build Coastguard Worker } else if (gridPt == fLine[1].asSkPoint()) {
410*c8dee2aaSAndroid Build Coastguard Worker *lineT = 1;
411*c8dee2aaSAndroid Build Coastguard Worker }
412*c8dee2aaSAndroid Build Coastguard Worker if (gridPt == fCubic[0].asSkPoint() && approximately_equal(*cubicT, 0)) {
413*c8dee2aaSAndroid Build Coastguard Worker *cubicT = 0;
414*c8dee2aaSAndroid Build Coastguard Worker } else if (gridPt == fCubic[3].asSkPoint() && approximately_equal(*cubicT, 1)) {
415*c8dee2aaSAndroid Build Coastguard Worker *cubicT = 1;
416*c8dee2aaSAndroid Build Coastguard Worker }
417*c8dee2aaSAndroid Build Coastguard Worker return true;
418*c8dee2aaSAndroid Build Coastguard Worker }
419*c8dee2aaSAndroid Build Coastguard Worker
420*c8dee2aaSAndroid Build Coastguard Worker private:
421*c8dee2aaSAndroid Build Coastguard Worker const SkDCubic& fCubic;
422*c8dee2aaSAndroid Build Coastguard Worker const SkDLine& fLine;
423*c8dee2aaSAndroid Build Coastguard Worker SkIntersections* fIntersections;
424*c8dee2aaSAndroid Build Coastguard Worker bool fAllowNear;
425*c8dee2aaSAndroid Build Coastguard Worker };
426*c8dee2aaSAndroid Build Coastguard Worker
horizontal(const SkDCubic & cubic,double left,double right,double y,bool flipped)427*c8dee2aaSAndroid Build Coastguard Worker int SkIntersections::horizontal(const SkDCubic& cubic, double left, double right, double y,
428*c8dee2aaSAndroid Build Coastguard Worker bool flipped) {
429*c8dee2aaSAndroid Build Coastguard Worker SkDLine line = {{{ left, y }, { right, y }}};
430*c8dee2aaSAndroid Build Coastguard Worker LineCubicIntersections c(cubic, line, this);
431*c8dee2aaSAndroid Build Coastguard Worker return c.horizontalIntersect(y, left, right, flipped);
432*c8dee2aaSAndroid Build Coastguard Worker }
433*c8dee2aaSAndroid Build Coastguard Worker
vertical(const SkDCubic & cubic,double top,double bottom,double x,bool flipped)434*c8dee2aaSAndroid Build Coastguard Worker int SkIntersections::vertical(const SkDCubic& cubic, double top, double bottom, double x,
435*c8dee2aaSAndroid Build Coastguard Worker bool flipped) {
436*c8dee2aaSAndroid Build Coastguard Worker SkDLine line = {{{ x, top }, { x, bottom }}};
437*c8dee2aaSAndroid Build Coastguard Worker LineCubicIntersections c(cubic, line, this);
438*c8dee2aaSAndroid Build Coastguard Worker return c.verticalIntersect(x, top, bottom, flipped);
439*c8dee2aaSAndroid Build Coastguard Worker }
440*c8dee2aaSAndroid Build Coastguard Worker
intersect(const SkDCubic & cubic,const SkDLine & line)441*c8dee2aaSAndroid Build Coastguard Worker int SkIntersections::intersect(const SkDCubic& cubic, const SkDLine& line) {
442*c8dee2aaSAndroid Build Coastguard Worker LineCubicIntersections c(cubic, line, this);
443*c8dee2aaSAndroid Build Coastguard Worker c.allowNear(fAllowNear);
444*c8dee2aaSAndroid Build Coastguard Worker return c.intersect();
445*c8dee2aaSAndroid Build Coastguard Worker }
446*c8dee2aaSAndroid Build Coastguard Worker
intersectRay(const SkDCubic & cubic,const SkDLine & line)447*c8dee2aaSAndroid Build Coastguard Worker int SkIntersections::intersectRay(const SkDCubic& cubic, const SkDLine& line) {
448*c8dee2aaSAndroid Build Coastguard Worker LineCubicIntersections c(cubic, line, this);
449*c8dee2aaSAndroid Build Coastguard Worker fUsed = c.intersectRay(fT[0]);
450*c8dee2aaSAndroid Build Coastguard Worker for (int index = 0; index < fUsed; ++index) {
451*c8dee2aaSAndroid Build Coastguard Worker fPt[index] = cubic.ptAtT(fT[0][index]);
452*c8dee2aaSAndroid Build Coastguard Worker }
453*c8dee2aaSAndroid Build Coastguard Worker return fUsed;
454*c8dee2aaSAndroid Build Coastguard Worker }
455*c8dee2aaSAndroid Build Coastguard Worker
456*c8dee2aaSAndroid Build Coastguard Worker // SkDCubic accessors to Intersection utilities
457*c8dee2aaSAndroid Build Coastguard Worker
horizontalIntersect(double yIntercept,double roots[3]) const458*c8dee2aaSAndroid Build Coastguard Worker int SkDCubic::horizontalIntersect(double yIntercept, double roots[3]) const {
459*c8dee2aaSAndroid Build Coastguard Worker return LineCubicIntersections::HorizontalIntersect(*this, yIntercept, roots);
460*c8dee2aaSAndroid Build Coastguard Worker }
461*c8dee2aaSAndroid Build Coastguard Worker
verticalIntersect(double xIntercept,double roots[3]) const462*c8dee2aaSAndroid Build Coastguard Worker int SkDCubic::verticalIntersect(double xIntercept, double roots[3]) const {
463*c8dee2aaSAndroid Build Coastguard Worker return LineCubicIntersections::VerticalIntersect(*this, xIntercept, roots);
464*c8dee2aaSAndroid Build Coastguard Worker }
465