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