blob: 6e049708ac1a046e32bc2a02864447b57e0d11af [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"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000015#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000016
17#if ONE_OFF_DEBUG
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000018static const double tLimits1[2][2] = {{0.388600450, 0.388600452}, {0.245852802, 0.245852804}};
caryclark@google.com07393ca2013-04-08 11:47:37 +000019static const double tLimits2[2][2] = {{-0.865211397, -0.865215212}, {-0.865207696, -0.865208078}};
20#endif
21
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000022#define DEBUG_QUAD_PART ONE_OFF_DEBUG && 1
23#define DEBUG_QUAD_PART_SHOW_SIMPLE DEBUG_QUAD_PART && 0
caryclark@google.com07393ca2013-04-08 11:47:37 +000024#define SWAP_TOP_DEBUG 0
25
caryclark@google.comd892bd82013-06-17 14:10:36 +000026static const int kCubicToQuadSubdivisionDepth = 8; // slots reserved for cubic to quads subdivision
27
caryclark@google.com07393ca2013-04-08 11:47:37 +000028static int quadPart(const SkDCubic& cubic, double tStart, double tEnd, SkReduceOrder* reducer) {
29 SkDCubic part = cubic.subDivide(tStart, tEnd);
30 SkDQuad quad = part.toQuad();
31 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
32 // extremely shallow quadratic?
33 int order = reducer->reduce(quad, SkReduceOrder::kFill_Style);
34#if DEBUG_QUAD_PART
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000035 SkDebugf("%s cubic=(%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g)"
36 " t=(%1.9g,%1.9g)\n", __FUNCTION__, cubic[0].fX, cubic[0].fY,
caryclark@google.com07393ca2013-04-08 11:47:37 +000037 cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY,
38 cubic[3].fX, cubic[3].fY, tStart, tEnd);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000039 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n"
40 " {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com07393ca2013-04-08 11:47:37 +000041 part[0].fX, part[0].fY, part[1].fX, part[1].fY, part[2].fX, part[2].fY,
42 part[3].fX, part[3].fY, quad[0].fX, quad[0].fY,
43 quad[1].fX, quad[1].fY, quad[2].fX, quad[2].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000044#if DEBUG_QUAD_PART_SHOW_SIMPLE
45 SkDebugf("%s simple=(%1.9g,%1.9g", __FUNCTION__, reducer->fQuad[0].fX, reducer->fQuad[0].fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000046 if (order > 1) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000047 SkDebugf(" %1.9g,%1.9g", reducer->fQuad[1].fX, reducer->fQuad[1].fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000048 }
49 if (order > 2) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000050 SkDebugf(" %1.9g,%1.9g", reducer->fQuad[2].fX, reducer->fQuad[2].fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000051 }
52 SkDebugf(")\n");
53 SkASSERT(order < 4 && order > 0);
54#endif
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000055#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +000056 return order;
57}
58
59static void intersectWithOrder(const SkDQuad& simple1, int order1, const SkDQuad& simple2,
60 int order2, SkIntersections& i) {
61 if (order1 == 3 && order2 == 3) {
62 i.intersect(simple1, simple2);
63 } else if (order1 <= 2 && order2 <= 2) {
64 i.intersect((const SkDLine&) simple1, (const SkDLine&) simple2);
65 } else if (order1 == 3 && order2 <= 2) {
66 i.intersect(simple1, (const SkDLine&) simple2);
67 } else {
68 SkASSERT(order1 <= 2 && order2 == 3);
69 i.intersect(simple2, (const SkDLine&) simple1);
70 i.swapPts();
71 }
72}
73
74// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
75// chase intersections near quadratic ends, requiring odd hacks to find them.
76static void intersect(const SkDCubic& cubic1, double t1s, double t1e, const SkDCubic& cubic2,
77 double t2s, double t2e, double precisionScale, SkIntersections& i) {
78 i.upDepth();
79 SkDCubic c1 = cubic1.subDivide(t1s, t1e);
80 SkDCubic c2 = cubic2.subDivide(t2s, t2e);
caryclark@google.comd892bd82013-06-17 14:10:36 +000081 SkSTArray<kCubicToQuadSubdivisionDepth, double, true> ts1;
caryclark@google.com07393ca2013-04-08 11:47:37 +000082 // OPTIMIZE: if c1 == c2, call once (happens when detecting self-intersection)
83 c1.toQuadraticTs(c1.calcPrecision() * precisionScale, &ts1);
caryclark@google.comd892bd82013-06-17 14:10:36 +000084 SkSTArray<kCubicToQuadSubdivisionDepth, double, true> ts2;
caryclark@google.com07393ca2013-04-08 11:47:37 +000085 c2.toQuadraticTs(c2.calcPrecision() * precisionScale, &ts2);
86 double t1Start = t1s;
87 int ts1Count = ts1.count();
88 for (int i1 = 0; i1 <= ts1Count; ++i1) {
89 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
90 const double t1 = t1s + (t1e - t1s) * tEnd1;
91 SkReduceOrder s1;
92 int o1 = quadPart(cubic1, t1Start, t1, &s1);
93 double t2Start = t2s;
94 int ts2Count = ts2.count();
95 for (int i2 = 0; i2 <= ts2Count; ++i2) {
96 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
97 const double t2 = t2s + (t2e - t2s) * tEnd2;
98 if (&cubic1 == &cubic2 && t1Start >= t2Start) {
99 t2Start = t2;
100 continue;
101 }
102 SkReduceOrder s2;
103 int o2 = quadPart(cubic2, t2Start, t2, &s2);
104 #if ONE_OFF_DEBUG
105 char tab[] = " ";
106 if (tLimits1[0][0] >= t1Start && tLimits1[0][1] <= t1
107 && tLimits1[1][0] >= t2Start && tLimits1[1][1] <= t2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000108 SkDebugf("%.*s %s t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)", i.depth()*2, tab,
109 __FUNCTION__, t1Start, t1, t2Start, t2);
110 SkIntersections xlocals;
111 intersectWithOrder(s1.fQuad, o1, s2.fQuad, o2, xlocals);
112 SkDebugf(" xlocals.fUsed=%d\n", xlocals.used());
113 }
114 #endif
115 SkIntersections locals;
116 intersectWithOrder(s1.fQuad, o1, s2.fQuad, o2, locals);
117 double coStart[2] = { -1 };
118 SkDPoint coPoint;
119 int tCount = locals.used();
120 for (int tIdx = 0; tIdx < tCount; ++tIdx) {
121 double to1 = t1Start + (t1 - t1Start) * locals[0][tIdx];
122 double to2 = t2Start + (t2 - t2Start) * locals[1][tIdx];
123 // if the computed t is not sufficiently precise, iterate
124 SkDPoint p1 = cubic1.xyAtT(to1);
125 SkDPoint p2 = cubic2.xyAtT(to2);
126 if (p1.approximatelyEqual(p2)) {
127 if (locals.isCoincident(tIdx)) {
128 if (coStart[0] < 0) {
129 coStart[0] = to1;
130 coStart[1] = to2;
131 coPoint = p1;
132 } else {
133 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
134 coStart[0] = -1;
135 }
136 } else if (&cubic1 != &cubic2 || !approximately_equal(to1, to2)) {
137 if (i.swapped()) { // FIXME: insert should respect swap
138 i.insert(to2, to1, p1);
139 } else {
140 i.insert(to1, to2, p1);
141 }
142 }
143 } else {
144 double offset = precisionScale / 16; // FIME: const is arbitrary: test, refine
caryclark@google.com07393ca2013-04-08 11:47:37 +0000145 double c1Bottom = tIdx == 0 ? 0 :
146 (t1Start + (t1 - t1Start) * locals[0][tIdx - 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000147 double c1Min = SkTMax(c1Bottom, to1 - offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000148 double c1Top = tIdx == tCount - 1 ? 1 :
149 (t1Start + (t1 - t1Start) * locals[0][tIdx + 1] + to1) / 2;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000150 double c1Max = SkTMin(c1Top, to1 + offset);
151 double c2Min = SkTMax(0., to2 - offset);
152 double c2Max = SkTMin(1., to2 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000153 #if ONE_OFF_DEBUG
154 SkDebugf("%.*s %s 1 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
155 __FUNCTION__,
156 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
157 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
158 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
159 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
160 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
161 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
162 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
163 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
164 SkDebugf("%.*s %s 1 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
165 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
166 i.depth()*2, tab, __FUNCTION__, c1Bottom, c1Top, 0., 1.,
167 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
168 SkDebugf("%.*s %s 1 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
169 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
170 c1Max, c2Min, c2Max);
171 #endif
172 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
173 #if ONE_OFF_DEBUG
174 SkDebugf("%.*s %s 1 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
175 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
176 #endif
177 if (tCount > 1) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000178 c1Min = SkTMax(0., to1 - offset);
179 c1Max = SkTMin(1., to1 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000180 double c2Bottom = tIdx == 0 ? to2 :
181 (t2Start + (t2 - t2Start) * locals[1][tIdx - 1] + to2) / 2;
182 double c2Top = tIdx == tCount - 1 ? to2 :
183 (t2Start + (t2 - t2Start) * locals[1][tIdx + 1] + to2) / 2;
184 if (c2Bottom > c2Top) {
185 SkTSwap(c2Bottom, c2Top);
186 }
187 if (c2Bottom == to2) {
188 c2Bottom = 0;
189 }
190 if (c2Top == to2) {
191 c2Top = 1;
192 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000193 c2Min = SkTMax(c2Bottom, to2 - offset);
194 c2Max = SkTMin(c2Top, to2 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000195 #if ONE_OFF_DEBUG
196 SkDebugf("%.*s %s 2 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
197 __FUNCTION__,
198 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
199 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
200 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
201 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
202 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
203 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
204 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
205 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
206 SkDebugf("%.*s %s 2 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
207 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
208 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
209 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
210 SkDebugf("%.*s %s 2 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
211 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
212 c1Max, c2Min, c2Max);
213 #endif
214 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
215 #if ONE_OFF_DEBUG
216 SkDebugf("%.*s %s 2 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
217 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
218 #endif
caryclark@google.com3b97af52013-04-23 11:56:44 +0000219 c1Min = SkTMax(c1Bottom, to1 - offset);
220 c1Max = SkTMin(c1Top, to1 + offset);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000221 #if ONE_OFF_DEBUG
222 SkDebugf("%.*s %s 3 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab,
223 __FUNCTION__,
224 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
225 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
226 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
227 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
228 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
229 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
230 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
231 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
232 SkDebugf("%.*s %s 3 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
233 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
234 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
235 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
236 SkDebugf("%.*s %s 3 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
237 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min,
238 c1Max, c2Min, c2Max);
239 #endif
240 intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
241 #if ONE_OFF_DEBUG
242 SkDebugf("%.*s %s 3 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__,
243 i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1);
244 #endif
245 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000246 // intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000247 // FIXME: if no intersection is found, either quadratics intersected where
248 // cubics did not, or the intersection was missed. In the former case, expect
249 // the quadratics to be nearly parallel at the point of intersection, and check
250 // for that.
251 }
252 }
253 SkASSERT(coStart[0] == -1);
254 t2Start = t2;
255 }
256 t1Start = t1;
257 }
258 i.downDepth();
259}
260
261#define LINE_FRACTION 0.1
262
263// intersect the end of the cubic with the other. Try lines from the end to control and opposite
264// end to determine range of t on opposite cubic.
265static void intersectEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2,
266 const SkDRect& bounds2, SkIntersections& i) {
267 SkDLine line;
268 int t1Index = start ? 0 : 3;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000269 // don't bother if the two cubics are connnected
caryclark@google.coma5e55922013-05-07 18:51:31 +0000270#if 1
caryclark@google.comd892bd82013-06-17 14:10:36 +0000271 static const int kPointsInCubic = 4; // FIXME: move to DCubic, replace '4' with this
272 static const int kMaxLineCubicIntersections = 3;
273 SkSTArray<(kMaxLineCubicIntersections - 1) * kMaxLineCubicIntersections, double, true> tVals;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000274 line[0] = cubic1[t1Index];
275 // this variant looks for intersections with the end point and lines parallel to other points
caryclark@google.comd892bd82013-06-17 14:10:36 +0000276 for (int index = 0; index < kPointsInCubic; ++index) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000277 if (index == t1Index) {
278 continue;
279 }
280 SkDVector dxy1 = cubic1[index] - line[0];
281 dxy1 /= SkDCubic::gPrecisionUnit;
282 line[1] = line[0] + dxy1;
283 SkDRect lineBounds;
284 lineBounds.setBounds(line);
285 if (!bounds2.intersects(&lineBounds)) {
286 continue;
287 }
288 SkIntersections local;
289 if (!local.intersect(cubic2, line)) {
290 continue;
291 }
292 for (int idx2 = 0; idx2 < local.used(); ++idx2) {
293 double foundT = local[0][idx2];
294 if (approximately_less_than_zero(foundT)
295 || approximately_greater_than_one(foundT)) {
296 continue;
297 }
298 if (local.pt(idx2).approximatelyEqual(line[0])) {
299 if (i.swapped()) { // FIXME: insert should respect swap
300 i.insert(foundT, start ? 0 : 1, line[0]);
301 } else {
302 i.insert(start ? 0 : 1, foundT, line[0]);
303 }
304 } else {
caryclark@google.comd892bd82013-06-17 14:10:36 +0000305 tVals.push_back(foundT);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000306 }
307 }
308 }
309 if (tVals.count() == 0) {
310 return;
311 }
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000312 SkTQSort<double>(tVals.begin(), tVals.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000313 double tMin1 = start ? 0 : 1 - LINE_FRACTION;
314 double tMax1 = start ? LINE_FRACTION : 1;
315 int tIdx = 0;
316 do {
317 int tLast = tIdx;
318 while (tLast + 1 < tVals.count() && roughly_equal(tVals[tLast + 1], tVals[tIdx])) {
319 ++tLast;
320 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000321 double tMin2 = SkTMax(tVals[tIdx] - LINE_FRACTION, 0.0);
322 double tMax2 = SkTMin(tVals[tLast] + LINE_FRACTION, 1.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000323 int lastUsed = i.used();
324 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
325 if (lastUsed == i.used()) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000326 tMin2 = SkTMax(tVals[tIdx] - (1.0 / SkDCubic::gPrecisionUnit), 0.0);
327 tMax2 = SkTMin(tVals[tLast] + (1.0 / SkDCubic::gPrecisionUnit), 1.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000328 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
329 }
330 tIdx = tLast + 1;
331 } while (tIdx < tVals.count());
caryclark@google.coma5e55922013-05-07 18:51:31 +0000332#else
333 const SkDPoint& endPt = cubic1[t1Index];
334 if (!bounds2.contains(endPt)) {
335 return;
336 }
337 // this variant looks for intersections within an 'x' of the endpoint
338 double delta = SkTMax(bounds2.width(), bounds2.height());
339 for (int index = 0; index < 2; ++index) {
340 if (index == 0) {
341 line[0].fY = line[1].fY = endPt.fY;
342 line[0].fX = endPt.fX - delta;
343 line[1].fX = endPt.fX + delta;
344 } else {
345 line[0].fX = line[1].fX = cubic1[t1Index].fX;
346 line[0].fY = endPt.fY - delta;
347 line[1].fY = endPt.fY + delta;
348 }
349 SkIntersections local;
350 local.intersectRay(cubic2, line); // OPTIMIZE: special for horizontal/vertical lines
351 int used = local.used();
352 for (int index = 0; index < used; ++index) {
353 double foundT = local[0][index];
354 if (approximately_less_than_zero(foundT) || approximately_greater_than_one(foundT)) {
355 continue;
356 }
357 if (!local.pt(index).approximatelyEqual(endPt)) {
358 continue;
359 }
360 if (i.swapped()) { // FIXME: insert should respect swap
361 i.insert(foundT, start ? 0 : 1, endPt);
362 } else {
363 i.insert(start ? 0 : 1, foundT, endPt);
364 }
365 return;
366 }
367 }
368// the above doesn't catch when the end of the cubic missed the other cubic because the quad
369// approximation moved too far away, so something like the below is still needed. The enabled
370// code above tries to avoid this heavy lifting unless the convex hull intersected the cubic.
371 double tMin1 = start ? 0 : 1 - LINE_FRACTION;
372 double tMax1 = start ? LINE_FRACTION : 1;
373 double tMin2 = SkTMax(foundT - LINE_FRACTION, 0.0);
374 double tMax2 = SkTMin(foundT + LINE_FRACTION, 1.0);
375 int lastUsed = i.used();
376 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
377 if (lastUsed == i.used()) {
378 tMin2 = SkTMax(foundT - (1.0 / SkDCubic::gPrecisionUnit), 0.0);
379 tMax2 = SkTMin(foundT + (1.0 / SkDCubic::gPrecisionUnit), 1.0);
380 intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
381 }
382#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000383 return;
384}
385
386const double CLOSE_ENOUGH = 0.001;
387
388static bool closeStart(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) {
389 if (i[cubicIndex][0] != 0 || i[cubicIndex][1] > CLOSE_ENOUGH) {
390 return false;
391 }
392 pt = cubic.xyAtT((i[cubicIndex][0] + i[cubicIndex][1]) / 2);
393 return true;
394}
395
396static bool closeEnd(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) {
397 int last = i.used() - 1;
398 if (i[cubicIndex][last] != 1 || i[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
399 return false;
400 }
401 pt = cubic.xyAtT((i[cubicIndex][last] + i[cubicIndex][last - 1]) / 2);
402 return true;
403}
404
405int SkIntersections::intersect(const SkDCubic& c1, const SkDCubic& c2) {
406 ::intersect(c1, 0, 1, c2, 0, 1, 1, *this);
407 // FIXME: pass in cached bounds from caller
408 SkDRect c1Bounds, c2Bounds;
409 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
410 c2Bounds.setBounds(c2);
411 intersectEnd(c1, false, c2, c2Bounds, *this);
412 intersectEnd(c1, true, c2, c2Bounds, *this);
413 bool selfIntersect = &c1 == &c2;
414 if (!selfIntersect) {
415 swap();
416 intersectEnd(c2, false, c1, c1Bounds, *this);
417 intersectEnd(c2, true, c1, c1Bounds, *this);
418 swap();
419 }
420 // If an end point and a second point very close to the end is returned, the second
421 // point may have been detected because the approximate quads
422 // intersected at the end and close to it. Verify that the second point is valid.
423 if (fUsed <= 1 || coincidentUsed()) {
424 return fUsed;
425 }
426 SkDPoint pt[2];
427 if (closeStart(c1, 0, *this, pt[0]) && closeStart(c2, 1, *this, pt[1])
428 && pt[0].approximatelyEqual(pt[1])) {
429 removeOne(1);
430 }
431 if (closeEnd(c1, 0, *this, pt[0]) && closeEnd(c2, 1, *this, pt[1])
432 && pt[0].approximatelyEqual(pt[1])) {
433 removeOne(used() - 2);
434 }
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000435 // vet the pairs of t values to see if the mid value is also on the curve. If so, mark
436 // the span as coincident
437 if (fUsed >= 2 && !coincidentUsed()) {
438 int last = fUsed - 1;
439 int match = 0;
440 for (int index = 0; index < last; ++index) {
441 double mid1 = (fT[0][index] + fT[0][index + 1]) / 2;
442 double mid2 = (fT[1][index] + fT[1][index + 1]) / 2;
443 pt[0] = c1.xyAtT(mid1);
444 pt[1] = c2.xyAtT(mid2);
445 if (pt[0].approximatelyEqual(pt[1])) {
446 match |= 1 << index;
447 }
448 }
449 if (match) {
450 if (((match + 1) & match) != 0) {
451 SkDebugf("%s coincident hole\n", __FUNCTION__);
452 }
453 // for now, assume that everything from start to finish is coincident
454 if (fUsed > 2) {
455 fPt[1] = fPt[last];
456 fT[0][1] = fT[0][last];
457 fT[1][1] = fT[1][last];
458 fIsCoincident[0] = 0x03;
459 fIsCoincident[1] = 0x03;
460 fUsed = 2;
461 }
462 }
463 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000464 return fUsed;
465}
466
467// Up promote the quad to a cubic.
468// OPTIMIZATION If this is a common use case, optimize by duplicating
469// the intersect 3 loop to avoid the promotion / demotion code
470int SkIntersections::intersect(const SkDCubic& cubic, const SkDQuad& quad) {
471 SkDCubic up = quad.toCubic();
472 (void) intersect(cubic, up);
473 return used();
474}
475
476/* http://www.ag.jku.at/compass/compasssample.pdf
477( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen
478Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no
479SINTEF Applied Mathematics http://www.sintef.no )
480describes a method to find the self intersection of a cubic by taking the gradient of the implicit
481form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/
482
483int SkIntersections::intersect(const SkDCubic& c) {
484 // check to see if x or y end points are the extrema. Are other quick rejects possible?
485 if (c.endsAreExtremaInXOrY()) {
486 return false;
487 }
488 (void) intersect(c, c);
489 if (used() > 0) {
490 SkASSERT(used() == 1);
491 if (fT[0][0] > fT[1][0]) {
492 swapPts();
493 }
494 }
495 return used();
496}