blob: 5106bbba98541472cb3f90a84f33dee2c090721d [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
caryclark@google.com07393ca2013-04-08 11:47:37 +0000141 double c1Bottom = tIdx == 0 ? 0 :
142 (t1Start + (t1 - t1Start) * locals[0][tIdx - 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000143 double c1Min = SkTMax(c1Bottom, to1 - offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000144 double c1Top = tIdx == tCount - 1 ? 1 :
145 (t1Start + (t1 - t1Start) * locals[0][tIdx + 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000146 double c1Max = SkTMin(c1Top, to1 + offset);
147 double c2Min = SkTMax(0., to2 - offset);
148 double c2Max = SkTMin(1., to2 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000149 #if ONE_OFF_DEBUG
150 SkDebugf("%.*s %s 1 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
151 __FUNCTION__,
152 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
153 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
154 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
155 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
156 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
157 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
158 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
159 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
160 SkDebugf("%.*s %s 1 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
161 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
162 i.depth()*2, tab, __FUNCTION__, c1Bottom, c1Top, 0., 1.,
163 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
164 SkDebugf("%.*s %s 1 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
165 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
166 c1Max, c2Min, c2Max);
167 #endif
168 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
169 #if ONE_OFF_DEBUG
170 SkDebugf("%.*s %s 1 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
171 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
172 #endif
173 if (tCount > 1) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000174 c1Min = SkTMax(0., to1 - offset);
175 c1Max = SkTMin(1., to1 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000176 double c2Bottom = tIdx == 0 ? to2 :
177 (t2Start + (t2 - t2Start) * locals[1][tIdx - 1] + to2) / 2;
178 double c2Top = tIdx == tCount - 1 ? to2 :
179 (t2Start + (t2 - t2Start) * locals[1][tIdx + 1] + to2) / 2;
180 if (c2Bottom > c2Top) {
181 SkTSwap(c2Bottom, c2Top);
182 }
183 if (c2Bottom == to2) {
184 c2Bottom = 0;
185 }
186 if (c2Top == to2) {
187 c2Top = 1;
188 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000189 c2Min = SkTMax(c2Bottom, to2 - offset);
190 c2Max = SkTMin(c2Top, to2 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000191 #if ONE_OFF_DEBUG
192 SkDebugf("%.*s %s 2 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
193 __FUNCTION__,
194 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
195 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
196 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
197 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
198 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
199 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
200 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
201 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
202 SkDebugf("%.*s %s 2 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
203 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
204 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
205 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
206 SkDebugf("%.*s %s 2 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
207 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
208 c1Max, c2Min, c2Max);
209 #endif
210 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
211 #if ONE_OFF_DEBUG
212 SkDebugf("%.*s %s 2 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
213 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
214 #endif
caryclark@google.com3b97af52013-04-23 11:56:44 +0000215 c1Min = SkTMax(c1Bottom, to1 - offset);
216 c1Max = SkTMin(c1Top, to1 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000217 #if ONE_OFF_DEBUG
218 SkDebugf("%.*s %s 3 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
219 __FUNCTION__,
220 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
221 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
222 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
223 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
224 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
225 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
226 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
227 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
228 SkDebugf("%.*s %s 3 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
229 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
230 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
231 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
232 SkDebugf("%.*s %s 3 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
233 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
234 c1Max, c2Min, c2Max);
235 #endif
236 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
237 #if ONE_OFF_DEBUG
238 SkDebugf("%.*s %s 3 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
239 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
240 #endif
241 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000242 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
243 // FIXME: if no intersection is found, either quadratics intersected where
244 // cubics did not, or the intersection was missed. In the former case, expect
245 // the quadratics to be nearly parallel at the point of intersection, and check
246 // for that.
247 }
248 }
249 SkASSERT(coStart[0] == -1);
250 t2Start = t2;
251 }
252 t1Start = t1;
253 }
254 i.downDepth();
255}
256
257#define LINE_FRACTION 0.1
258
259// intersect the end of the cubic with the other. Try lines from the end to control and opposite
260// end to determine range of t on opposite cubic.
261static void intersectEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2,
262 const SkDRect& bounds2, SkIntersections& i) {
263 SkDLine line;
264 int t1Index = start ? 0 : 3;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000265 // don't bother if the two cubics are connnected
caryclark@google.coma5e55922013-05-07 18:51:31 +0000266#if 1
caryclark@google.com07393ca2013-04-08 11:47:37 +0000267 SkTDArray<double> tVals; // OPTIMIZE: replace with hard-sized array
caryclark@google.coma5e55922013-05-07 18:51:31 +0000268 line[0] = cubic1[t1Index];
269 // this variant looks for intersections with the end point and lines parallel to other points
caryclark@google.com07393ca2013-04-08 11:47:37 +0000270 for (int index = 0; index < 4; ++index) {
271 if (index == t1Index) {
272 continue;
273 }
274 SkDVector dxy1 = cubic1[index] - line[0];
275 dxy1 /= SkDCubic::gPrecisionUnit;
276 line[1] = line[0] + dxy1;
277 SkDRect lineBounds;
278 lineBounds.setBounds(line);
279 if (!bounds2.intersects(&lineBounds)) {
280 continue;
281 }
282 SkIntersections local;
283 if (!local.intersect(cubic2, line)) {
284 continue;
285 }
286 for (int idx2 = 0; idx2 < local.used(); ++idx2) {
287 double foundT = local[0][idx2];
288 if (approximately_less_than_zero(foundT)
289 || approximately_greater_than_one(foundT)) {
290 continue;
291 }
292 if (local.pt(idx2).approximatelyEqual(line[0])) {
293 if (i.swapped()) { // FIXME: insert should respect swap
294 i.insert(foundT, start ? 0 : 1, line[0]);
295 } else {
296 i.insert(start ? 0 : 1, foundT, line[0]);
297 }
298 } else {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000299 *tVals.append() = foundT;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000300 }
301 }
302 }
303 if (tVals.count() == 0) {
304 return;
305 }
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000306 SkTQSort<double>(tVals.begin(), tVals.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000307 double tMin1 = start ? 0 : 1 - LINE_FRACTION;
308 double tMax1 = start ? LINE_FRACTION : 1;
309 int tIdx = 0;
310 do {
311 int tLast = tIdx;
312 while (tLast + 1 < tVals.count() && roughly_equal(tVals[tLast + 1], tVals[tIdx])) {
313 ++tLast;
314 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000315 double tMin2 = SkTMax(tVals[tIdx] - LINE_FRACTION, 0.0);
316 double tMax2 = SkTMin(tVals[tLast] + LINE_FRACTION, 1.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000317 int lastUsed = i.used();
318 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
319 if (lastUsed == i.used()) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000320 tMin2 = SkTMax(tVals[tIdx] - (1.0 / SkDCubic::gPrecisionUnit), 0.0);
321 tMax2 = SkTMin(tVals[tLast] + (1.0 / SkDCubic::gPrecisionUnit), 1.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000322 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
323 }
324 tIdx = tLast + 1;
325 } while (tIdx < tVals.count());
caryclark@google.coma5e55922013-05-07 18:51:31 +0000326#else
327 const SkDPoint& endPt = cubic1[t1Index];
328 if (!bounds2.contains(endPt)) {
329 return;
330 }
331 // this variant looks for intersections within an 'x' of the endpoint
332 double delta = SkTMax(bounds2.width(), bounds2.height());
333 for (int index = 0; index < 2; ++index) {
334 if (index == 0) {
335 line[0].fY = line[1].fY = endPt.fY;
336 line[0].fX = endPt.fX - delta;
337 line[1].fX = endPt.fX + delta;
338 } else {
339 line[0].fX = line[1].fX = cubic1[t1Index].fX;
340 line[0].fY = endPt.fY - delta;
341 line[1].fY = endPt.fY + delta;
342 }
343 SkIntersections local;
344 local.intersectRay(cubic2, line); // OPTIMIZE: special for horizontal/vertical lines
345 int used = local.used();
346 for (int index = 0; index < used; ++index) {
347 double foundT = local[0][index];
348 if (approximately_less_than_zero(foundT) || approximately_greater_than_one(foundT)) {
349 continue;
350 }
351 if (!local.pt(index).approximatelyEqual(endPt)) {
352 continue;
353 }
354 if (i.swapped()) { // FIXME: insert should respect swap
355 i.insert(foundT, start ? 0 : 1, endPt);
356 } else {
357 i.insert(start ? 0 : 1, foundT, endPt);
358 }
359 return;
360 }
361 }
362// the above doesn't catch when the end of the cubic missed the other cubic because the quad
363// approximation moved too far away, so something like the below is still needed. The enabled
364// code above tries to avoid this heavy lifting unless the convex hull intersected the cubic.
365 double tMin1 = start ? 0 : 1 - LINE_FRACTION;
366 double tMax1 = start ? LINE_FRACTION : 1;
367 double tMin2 = SkTMax(foundT - LINE_FRACTION, 0.0);
368 double tMax2 = SkTMin(foundT + LINE_FRACTION, 1.0);
369 int lastUsed = i.used();
370 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
371 if (lastUsed == i.used()) {
372 tMin2 = SkTMax(foundT - (1.0 / SkDCubic::gPrecisionUnit), 0.0);
373 tMax2 = SkTMin(foundT + (1.0 / SkDCubic::gPrecisionUnit), 1.0);
374 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
375 }
376#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000377 return;
378}
379
380const double CLOSE_ENOUGH = 0.001;
381
382static bool closeStart(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) {
383 if (i[cubicIndex][0] != 0 || i[cubicIndex][1] > CLOSE_ENOUGH) {
384 return false;
385 }
386 pt = cubic.xyAtT((i[cubicIndex][0] + i[cubicIndex][1]) / 2);
387 return true;
388}
389
390static bool closeEnd(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) {
391 int last = i.used() - 1;
392 if (i[cubicIndex][last] != 1 || i[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
393 return false;
394 }
395 pt = cubic.xyAtT((i[cubicIndex][last] + i[cubicIndex][last - 1]) / 2);
396 return true;
397}
398
399int SkIntersections::intersect(const SkDCubic& c1, const SkDCubic& c2) {
400 ::intersect(c1, 0, 1, c2, 0, 1, 1, *this);
401 // FIXME: pass in cached bounds from caller
402 SkDRect c1Bounds, c2Bounds;
403 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
404 c2Bounds.setBounds(c2);
405 intersectEnd(c1, false, c2, c2Bounds, *this);
406 intersectEnd(c1, true, c2, c2Bounds, *this);
407 bool selfIntersect = &c1 == &c2;
408 if (!selfIntersect) {
409 swap();
410 intersectEnd(c2, false, c1, c1Bounds, *this);
411 intersectEnd(c2, true, c1, c1Bounds, *this);
412 swap();
413 }
414 // If an end point and a second point very close to the end is returned, the second
415 // point may have been detected because the approximate quads
416 // intersected at the end and close to it. Verify that the second point is valid.
417 if (fUsed <= 1 || coincidentUsed()) {
418 return fUsed;
419 }
420 SkDPoint pt[2];
421 if (closeStart(c1, 0, *this, pt[0]) && closeStart(c2, 1, *this, pt[1])
422 && pt[0].approximatelyEqual(pt[1])) {
423 removeOne(1);
424 }
425 if (closeEnd(c1, 0, *this, pt[0]) && closeEnd(c2, 1, *this, pt[1])
426 && pt[0].approximatelyEqual(pt[1])) {
427 removeOne(used() - 2);
428 }
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000429 // vet the pairs of t values to see if the mid value is also on the curve. If so, mark
430 // the span as coincident
431 if (fUsed >= 2 && !coincidentUsed()) {
432 int last = fUsed - 1;
433 int match = 0;
434 for (int index = 0; index < last; ++index) {
435 double mid1 = (fT[0][index] + fT[0][index + 1]) / 2;
436 double mid2 = (fT[1][index] + fT[1][index + 1]) / 2;
437 pt[0] = c1.xyAtT(mid1);
438 pt[1] = c2.xyAtT(mid2);
439 if (pt[0].approximatelyEqual(pt[1])) {
440 match |= 1 << index;
441 }
442 }
443 if (match) {
444 if (((match + 1) & match) != 0) {
445 SkDebugf("%s coincident hole\n", __FUNCTION__);
446 }
447 // for now, assume that everything from start to finish is coincident
448 if (fUsed > 2) {
449 fPt[1] = fPt[last];
450 fT[0][1] = fT[0][last];
451 fT[1][1] = fT[1][last];
452 fIsCoincident[0] = 0x03;
453 fIsCoincident[1] = 0x03;
454 fUsed = 2;
455 }
456 }
457 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000458 return fUsed;
459}
460
461// Up promote the quad to a cubic.
462// OPTIMIZATION If this is a common use case, optimize by duplicating
463// the intersect 3 loop to avoid the promotion / demotion code
464int SkIntersections::intersect(const SkDCubic& cubic, const SkDQuad& quad) {
465 SkDCubic up = quad.toCubic();
466 (void) intersect(cubic, up);
467 return used();
468}
469
470/* http://www.ag.jku.at/compass/compasssample.pdf
471( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen
472Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no
473SINTEF Applied Mathematics http://www.sintef.no )
474describes a method to find the self intersection of a cubic by taking the gradient of the implicit
475form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/
476
477int SkIntersections::intersect(const SkDCubic& c) {
478 // check to see if x or y end points are the extrema. Are other quick rejects possible?
479 if (c.endsAreExtremaInXOrY()) {
480 return false;
481 }
482 (void) intersect(c, c);
483 if (used() > 0) {
484 SkASSERT(used() == 1);
485 if (fT[0][0] > fT[1][0]) {
486 swapPts();
487 }
488 }
489 return used();
490}