blob: 10d4e71d03ecad7040ab2dd0124b9ae3502bce46 [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
8#include "SkIntersections.h"
9#include "SkPathOpsCubic.h"
10#include "SkPathOpsLine.h"
11#include "SkPathOpsPoint.h"
12#include "SkPathOpsQuad.h"
13#include "SkPathOpsRect.h"
14#include "SkReduceOrder.h"
15#include "SkTDArray.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000016#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000017
18#if ONE_OFF_DEBUG
19static const double tLimits1[2][2] = {{0.36, 0.37}, {0.63, 0.64}};
20static const double tLimits2[2][2] = {{-0.865211397, -0.865215212}, {-0.865207696, -0.865208078}};
21#endif
22
23#define DEBUG_QUAD_PART 0
24#define SWAP_TOP_DEBUG 0
25
26static int quadPart(const SkDCubic& cubic, double tStart, double tEnd, SkReduceOrder* reducer) {
27 SkDCubic part = cubic.subDivide(tStart, tEnd);
28 SkDQuad quad = part.toQuad();
29 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
30 // extremely shallow quadratic?
31 int order = reducer->reduce(quad, SkReduceOrder::kFill_Style);
32#if DEBUG_QUAD_PART
33 SkDebugf("%s cubic=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)"
34 " t=(%1.17g,%1.17g)\n", __FUNCTION__, cubic[0].fX, cubic[0].fY,
35 cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY,
36 cubic[3].fX, cubic[3].fY, tStart, tEnd);
37 SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)"
38 " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__,
39 part[0].fX, part[0].fY, part[1].fX, part[1].fY, part[2].fX, part[2].fY,
40 part[3].fX, part[3].fY, quad[0].fX, quad[0].fY,
41 quad[1].fX, quad[1].fY, quad[2].fX, quad[2].fY);
42 SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, reducer->fQuad[0].fX, reducer->fQuad[0].fY);
43 if (order > 1) {
44 SkDebugf(" %1.17g,%1.17g", reducer->fQuad[1].fX, reducer->fQuad[1].fY);
45 }
46 if (order > 2) {
47 SkDebugf(" %1.17g,%1.17g", reducer->fQuad[2].fX, reducer->fQuad[2].fY);
48 }
49 SkDebugf(")\n");
50 SkASSERT(order < 4 && order > 0);
51#endif
52 return order;
53}
54
55static void intersectWithOrder(const SkDQuad& simple1, int order1, const SkDQuad& simple2,
56 int order2, SkIntersections& i) {
57 if (order1 == 3 && order2 == 3) {
58 i.intersect(simple1, simple2);
59 } else if (order1 <= 2 && order2 <= 2) {
60 i.intersect((const SkDLine&) simple1, (const SkDLine&) simple2);
61 } else if (order1 == 3 && order2 <= 2) {
62 i.intersect(simple1, (const SkDLine&) simple2);
63 } else {
64 SkASSERT(order1 <= 2 && order2 == 3);
65 i.intersect(simple2, (const SkDLine&) simple1);
66 i.swapPts();
67 }
68}
69
70// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
71// chase intersections near quadratic ends, requiring odd hacks to find them.
72static void intersect(const SkDCubic& cubic1, double t1s, double t1e, const SkDCubic& cubic2,
73 double t2s, double t2e, double precisionScale, SkIntersections& i) {
74 i.upDepth();
75 SkDCubic c1 = cubic1.subDivide(t1s, t1e);
76 SkDCubic c2 = cubic2.subDivide(t2s, t2e);
77 SkTDArray<double> ts1;
78 // OPTIMIZE: if c1 == c2, call once (happens when detecting self-intersection)
79 c1.toQuadraticTs(c1.calcPrecision() * precisionScale, &ts1);
80 SkTDArray<double> ts2;
81 c2.toQuadraticTs(c2.calcPrecision() * precisionScale, &ts2);
82 double t1Start = t1s;
83 int ts1Count = ts1.count();
84 for (int i1 = 0; i1 <= ts1Count; ++i1) {
85 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
86 const double t1 = t1s + (t1e - t1s) * tEnd1;
87 SkReduceOrder s1;
88 int o1 = quadPart(cubic1, t1Start, t1, &s1);
89 double t2Start = t2s;
90 int ts2Count = ts2.count();
91 for (int i2 = 0; i2 <= ts2Count; ++i2) {
92 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
93 const double t2 = t2s + (t2e - t2s) * tEnd2;
94 if (&cubic1 == &cubic2 && t1Start >= t2Start) {
95 t2Start = t2;
96 continue;
97 }
98 SkReduceOrder s2;
99 int o2 = quadPart(cubic2, t2Start, t2, &s2);
100 #if ONE_OFF_DEBUG
101 char tab[] = " ";
102 if (tLimits1[0][0] >= t1Start && tLimits1[0][1] <= t1
103 && tLimits1[1][0] >= t2Start && tLimits1[1][1] <= t2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000104 SkDebugf("%.*s %s t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)", i.depth()*2, tab,
105 __FUNCTION__, t1Start, t1, t2Start, t2);
106 SkIntersections xlocals;
107 intersectWithOrder(s1.fQuad, o1, s2.fQuad, o2, xlocals);
108 SkDebugf(" xlocals.fUsed=%d\n", xlocals.used());
109 }
110 #endif
111 SkIntersections locals;
112 intersectWithOrder(s1.fQuad, o1, s2.fQuad, o2, locals);
113 double coStart[2] = { -1 };
114 SkDPoint coPoint;
115 int tCount = locals.used();
116 for (int tIdx = 0; tIdx < tCount; ++tIdx) {
117 double to1 = t1Start + (t1 - t1Start) * locals[0][tIdx];
118 double to2 = t2Start + (t2 - t2Start) * locals[1][tIdx];
119 // if the computed t is not sufficiently precise, iterate
120 SkDPoint p1 = cubic1.xyAtT(to1);
121 SkDPoint p2 = cubic2.xyAtT(to2);
122 if (p1.approximatelyEqual(p2)) {
123 if (locals.isCoincident(tIdx)) {
124 if (coStart[0] < 0) {
125 coStart[0] = to1;
126 coStart[1] = to2;
127 coPoint = p1;
128 } else {
129 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
130 coStart[0] = -1;
131 }
132 } else if (&cubic1 != &cubic2 || !approximately_equal(to1, to2)) {
133 if (i.swapped()) { // FIXME: insert should respect swap
134 i.insert(to2, to1, p1);
135 } else {
136 i.insert(to1, to2, p1);
137 }
138 }
139 } else {
140 double offset = precisionScale / 16; // FIME: const is arbitrary: test, refine
141#if 1
142 double c1Bottom = tIdx == 0 ? 0 :
143 (t1Start + (t1 - t1Start) * locals[0][tIdx - 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000144 double c1Min = SkTMax(c1Bottom, to1 - offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000145 double c1Top = tIdx == tCount - 1 ? 1 :
146 (t1Start + (t1 - t1Start) * locals[0][tIdx + 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000147 double c1Max = SkTMin(c1Top, to1 + offset);
148 double c2Min = SkTMax(0., to2 - offset);
149 double c2Max = SkTMin(1., to2 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000150 #if ONE_OFF_DEBUG
151 SkDebugf("%.*s %s 1 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
152 __FUNCTION__,
153 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
154 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
155 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
156 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
157 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
158 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
159 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
160 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
161 SkDebugf("%.*s %s 1 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
162 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
163 i.depth()*2, tab, __FUNCTION__, c1Bottom, c1Top, 0., 1.,
164 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
165 SkDebugf("%.*s %s 1 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
166 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
167 c1Max, c2Min, c2Max);
168 #endif
169 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
170 #if ONE_OFF_DEBUG
171 SkDebugf("%.*s %s 1 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
172 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
173 #endif
174 if (tCount > 1) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000175 c1Min = SkTMax(0., to1 - offset);
176 c1Max = SkTMin(1., to1 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000177 double c2Bottom = tIdx == 0 ? to2 :
178 (t2Start + (t2 - t2Start) * locals[1][tIdx - 1] + to2) / 2;
179 double c2Top = tIdx == tCount - 1 ? to2 :
180 (t2Start + (t2 - t2Start) * locals[1][tIdx + 1] + to2) / 2;
181 if (c2Bottom > c2Top) {
182 SkTSwap(c2Bottom, c2Top);
183 }
184 if (c2Bottom == to2) {
185 c2Bottom = 0;
186 }
187 if (c2Top == to2) {
188 c2Top = 1;
189 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000190 c2Min = SkTMax(c2Bottom, to2 - offset);
191 c2Max = SkTMin(c2Top, to2 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000192 #if ONE_OFF_DEBUG
193 SkDebugf("%.*s %s 2 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
194 __FUNCTION__,
195 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
196 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
197 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
198 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
199 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
200 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
201 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
202 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
203 SkDebugf("%.*s %s 2 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
204 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
205 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
206 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
207 SkDebugf("%.*s %s 2 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
208 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
209 c1Max, c2Min, c2Max);
210 #endif
211 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
212 #if ONE_OFF_DEBUG
213 SkDebugf("%.*s %s 2 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
214 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
215 #endif
caryclark@google.com3b97af52013-04-23 11:56:44 +0000216 c1Min = SkTMax(c1Bottom, to1 - offset);
217 c1Max = SkTMin(c1Top, to1 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000218 #if ONE_OFF_DEBUG
219 SkDebugf("%.*s %s 3 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
220 __FUNCTION__,
221 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
222 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
223 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
224 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
225 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
226 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
227 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
228 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
229 SkDebugf("%.*s %s 3 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
230 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
231 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
232 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
233 SkDebugf("%.*s %s 3 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
234 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
235 c1Max, c2Min, c2Max);
236 #endif
237 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
238 #if ONE_OFF_DEBUG
239 SkDebugf("%.*s %s 3 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
240 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
241 #endif
242 }
243#else
244 double c1Bottom = tIdx == 0 ? 0 :
245 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx - 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000246 double c1Min = SkTMax(c1Bottom, to1 - offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000247 double c1Top = tIdx == tCount - 1 ? 1 :
248 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx + 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000249 double c1Max = SkTMin(c1Top, to1 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000250 double c2Bottom = tIdx == 0 ? to2 :
251 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx - 1] + to2) / 2;
252 double c2Top = tIdx == tCount - 1 ? to2 :
253 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx + 1] + to2) / 2;
254 if (c2Bottom > c2Top) {
255 SkTSwap(c2Bottom, c2Top);
256 }
257 if (c2Bottom == to2) {
258 c2Bottom = 0;
259 }
260 if (c2Top == to2) {
261 c2Top = 1;
262 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000263 double c2Min = SkTMax(c2Bottom, to2 - offset);
264 double c2Max = SkTMin(c2Top, to2 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000265 #if ONE_OFF_DEBUG
266 SkDebugf("%s contains1=%d/%d contains2=%d/%d\n", __FUNCTION__,
267 c1Min <= 0.210357794 && 0.210357794 <= c1Max
268 && c2Min <= 0.223476406 && 0.223476406 <= c2Max,
269 to1 - offset <= 0.210357794 && 0.210357794 <= to1 + offset
270 && to2 - offset <= 0.223476406 && 0.223476406 <= to2 + offset,
271 c1Min <= 0.211324707 && 0.211324707 <= c1Max
272 && c2Min <= 0.211327209 && 0.211327209 <= c2Max,
273 to1 - offset <= 0.211324707 && 0.211324707 <= to1 + offset
274 && to2 - offset <= 0.211327209 && 0.211327209 <= to2 + offset);
275 SkDebugf("%s c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
276 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
277 __FUNCTION__, c1Bottom, c1Top, c2Bottom, c2Top,
278 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
279 SkDebugf("%s to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
280 " c2Max=%1.9g\n", __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
281 #endif
282#endif
283 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
284 // FIXME: if no intersection is found, either quadratics intersected where
285 // cubics did not, or the intersection was missed. In the former case, expect
286 // the quadratics to be nearly parallel at the point of intersection, and check
287 // for that.
288 }
289 }
290 SkASSERT(coStart[0] == -1);
291 t2Start = t2;
292 }
293 t1Start = t1;
294 }
295 i.downDepth();
296}
297
298#define LINE_FRACTION 0.1
299
300// intersect the end of the cubic with the other. Try lines from the end to control and opposite
301// end to determine range of t on opposite cubic.
302static void intersectEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2,
303 const SkDRect& bounds2, SkIntersections& i) {
304 SkDLine line;
305 int t1Index = start ? 0 : 3;
306 line[0] = cubic1[t1Index];
307 // don't bother if the two cubics are connnected
308 SkTDArray<double> tVals; // OPTIMIZE: replace with hard-sized array
309 for (int index = 0; index < 4; ++index) {
310 if (index == t1Index) {
311 continue;
312 }
313 SkDVector dxy1 = cubic1[index] - line[0];
314 dxy1 /= SkDCubic::gPrecisionUnit;
315 line[1] = line[0] + dxy1;
316 SkDRect lineBounds;
317 lineBounds.setBounds(line);
318 if (!bounds2.intersects(&lineBounds)) {
319 continue;
320 }
321 SkIntersections local;
322 if (!local.intersect(cubic2, line)) {
323 continue;
324 }
325 for (int idx2 = 0; idx2 < local.used(); ++idx2) {
326 double foundT = local[0][idx2];
327 if (approximately_less_than_zero(foundT)
328 || approximately_greater_than_one(foundT)) {
329 continue;
330 }
331 if (local.pt(idx2).approximatelyEqual(line[0])) {
332 if (i.swapped()) { // FIXME: insert should respect swap
333 i.insert(foundT, start ? 0 : 1, line[0]);
334 } else {
335 i.insert(start ? 0 : 1, foundT, line[0]);
336 }
337 } else {
338 *tVals.append() = local[0][idx2];
339 }
340 }
341 }
342 if (tVals.count() == 0) {
343 return;
344 }
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000345 SkTQSort<double>(tVals.begin(), tVals.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000346 double tMin1 = start ? 0 : 1 - LINE_FRACTION;
347 double tMax1 = start ? LINE_FRACTION : 1;
348 int tIdx = 0;
349 do {
350 int tLast = tIdx;
351 while (tLast + 1 < tVals.count() && roughly_equal(tVals[tLast + 1], tVals[tIdx])) {
352 ++tLast;
353 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000354 double tMin2 = SkTMax(tVals[tIdx] - LINE_FRACTION, 0.0);
355 double tMax2 = SkTMin(tVals[tLast] + LINE_FRACTION, 1.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000356 int lastUsed = i.used();
357 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
358 if (lastUsed == i.used()) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000359 tMin2 = SkTMax(tVals[tIdx] - (1.0 / SkDCubic::gPrecisionUnit), 0.0);
360 tMax2 = SkTMin(tVals[tLast] + (1.0 / SkDCubic::gPrecisionUnit), 1.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000361 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
362 }
363 tIdx = tLast + 1;
364 } while (tIdx < tVals.count());
365 return;
366}
367
368const double CLOSE_ENOUGH = 0.001;
369
370static bool closeStart(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) {
371 if (i[cubicIndex][0] != 0 || i[cubicIndex][1] > CLOSE_ENOUGH) {
372 return false;
373 }
374 pt = cubic.xyAtT((i[cubicIndex][0] + i[cubicIndex][1]) / 2);
375 return true;
376}
377
378static bool closeEnd(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) {
379 int last = i.used() - 1;
380 if (i[cubicIndex][last] != 1 || i[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
381 return false;
382 }
383 pt = cubic.xyAtT((i[cubicIndex][last] + i[cubicIndex][last - 1]) / 2);
384 return true;
385}
386
387int SkIntersections::intersect(const SkDCubic& c1, const SkDCubic& c2) {
388 ::intersect(c1, 0, 1, c2, 0, 1, 1, *this);
389 // FIXME: pass in cached bounds from caller
390 SkDRect c1Bounds, c2Bounds;
391 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
392 c2Bounds.setBounds(c2);
393 intersectEnd(c1, false, c2, c2Bounds, *this);
394 intersectEnd(c1, true, c2, c2Bounds, *this);
395 bool selfIntersect = &c1 == &c2;
396 if (!selfIntersect) {
397 swap();
398 intersectEnd(c2, false, c1, c1Bounds, *this);
399 intersectEnd(c2, true, c1, c1Bounds, *this);
400 swap();
401 }
402 // If an end point and a second point very close to the end is returned, the second
403 // point may have been detected because the approximate quads
404 // intersected at the end and close to it. Verify that the second point is valid.
405 if (fUsed <= 1 || coincidentUsed()) {
406 return fUsed;
407 }
408 SkDPoint pt[2];
409 if (closeStart(c1, 0, *this, pt[0]) && closeStart(c2, 1, *this, pt[1])
410 && pt[0].approximatelyEqual(pt[1])) {
411 removeOne(1);
412 }
413 if (closeEnd(c1, 0, *this, pt[0]) && closeEnd(c2, 1, *this, pt[1])
414 && pt[0].approximatelyEqual(pt[1])) {
415 removeOne(used() - 2);
416 }
417 return fUsed;
418}
419
420// Up promote the quad to a cubic.
421// OPTIMIZATION If this is a common use case, optimize by duplicating
422// the intersect 3 loop to avoid the promotion / demotion code
423int SkIntersections::intersect(const SkDCubic& cubic, const SkDQuad& quad) {
424 SkDCubic up = quad.toCubic();
425 (void) intersect(cubic, up);
426 return used();
427}
428
429/* http://www.ag.jku.at/compass/compasssample.pdf
430( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen
431Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no
432SINTEF Applied Mathematics http://www.sintef.no )
433describes a method to find the self intersection of a cubic by taking the gradient of the implicit
434form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/
435
436int SkIntersections::intersect(const SkDCubic& c) {
437 // check to see if x or y end points are the extrema. Are other quick rejects possible?
438 if (c.endsAreExtremaInXOrY()) {
439 return false;
440 }
441 (void) intersect(c, c);
442 if (used() > 0) {
443 SkASSERT(used() == 1);
444 if (fT[0][0] > fT[1][0]) {
445 swapPts();
446 }
447 }
448 return used();
449}