blob: 84692bc67f0fc0078b6b83591ced2b466f943e6c [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +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 */
caryclark@google.com9d5f99b2013-01-22 12:55:54 +00007
8#include "CubicUtilities.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00009#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +000010#include "Intersections.h"
caryclark@google.comc6825902012-02-03 22:07:47 +000011#include "IntersectionUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +000012#include "LineIntersection.h"
caryclark@google.com9f602912013-01-24 21:47:16 +000013#include "LineUtilities.h"
caryclark@google.comd0a19eb2013-02-19 12:49:33 +000014#include "QuadraticUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +000015
caryclark@google.com45a8fc62013-02-14 15:29:11 +000016#if ONE_OFF_DEBUG
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +000017static const double tLimits1[2][2] = {{0.86731567, 0.867316052}, {0.912837526, 0.912837908}};
18static const double tLimits2[2][2] = {{0.83051487, 0.830515252}, {0.860977985, 0.860978367}};
caryclark@google.com45a8fc62013-02-14 15:29:11 +000019#endif
20
caryclark@google.combeda3892013-02-07 13:13:41 +000021#define DEBUG_QUAD_PART 0
caryclark@google.com47d73da2013-02-17 01:41:25 +000022#define SWAP_TOP_DEBUG 0
caryclark@google.comf9502d72013-02-04 14:06:49 +000023
caryclark@google.comf9502d72013-02-04 14:06:49 +000024static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) {
25 Cubic part;
26 sub_divide(cubic, tStart, tEnd, part);
27 Quadratic quad;
28 demote_cubic_to_quad(part, quad);
29 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
30 // extremely shallow quadratic?
caryclark@google.com47d73da2013-02-17 01:41:25 +000031 int order = reduceOrder(quad, simple, kReduceOrder_TreatAsFill);
caryclark@google.combeda3892013-02-07 13:13:41 +000032#if DEBUG_QUAD_PART
33 SkDebugf("%s cubic=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g) t=(%1.17g,%1.17g)\n",
34 __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y,
35 cubic[3].x, cubic[3].y, tStart, tEnd);
36 SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)"
37 " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, part[0].x, part[0].y,
38 part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y,
39 quad[1].x, quad[1].y, quad[2].x, quad[2].y);
40 SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y);
41 if (order > 1) {
42 SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y);
43 }
44 if (order > 2) {
45 SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y);
46 }
47 SkDebugf(")\n");
48 SkASSERT(order < 4 && order > 0);
49#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +000050 return order;
51}
52
53static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2,
54 int order2, Intersections& i) {
55 if (order1 == 3 && order2 == 3) {
56 intersect2(simple1, simple2, i);
57 } else if (order1 <= 2 && order2 <= 2) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000058 intersect((const _Line&) simple1, (const _Line&) simple2, i);
caryclark@google.comf9502d72013-02-04 14:06:49 +000059 } else if (order1 == 3 && order2 <= 2) {
60 intersect(simple1, (const _Line&) simple2, i);
61 } else {
62 SkASSERT(order1 <= 2 && order2 == 3);
63 intersect(simple2, (const _Line&) simple1, i);
64 for (int s = 0; s < i.fUsed; ++s) {
65 SkTSwap(i.fT[0][s], i.fT[1][s]);
66 }
67 }
68}
69
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000070// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
caryclark@google.com45a8fc62013-02-14 15:29:11 +000071// chase intersections near quadratic ends, requiring odd hacks to find them.
72static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
73 double t2s, double t2e, double precisionScale, Intersections& i) {
74 i.upDepth();
75 bool result = false;
76 Cubic c1, c2;
77 sub_divide(cubic1, t1s, t1e, c1);
78 sub_divide(cubic2, t2s, t2e, c2);
79 SkTDArray<double> ts1;
80 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
81 SkTDArray<double> ts2;
82 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
83 double t1Start = t1s;
84 int ts1Count = ts1.count();
85 for (int i1 = 0; i1 <= ts1Count; ++i1) {
86 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
87 const double t1 = t1s + (t1e - t1s) * tEnd1;
88 Quadratic s1;
89 int o1 = quadPart(cubic1, t1Start, t1, s1);
90 double t2Start = t2s;
91 int ts2Count = ts2.count();
92 for (int i2 = 0; i2 <= ts2Count; ++i2) {
93 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
94 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.comc83c70e2013-02-22 21:50:07 +000095 if (cubic1 == cubic2 && t1Start >= t2Start) {
96 t2Start = t2;
97 continue;
98 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +000099 Quadratic s2;
100 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000101 #if ONE_OFF_DEBUG
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000102 char tab[] = " ";
103 if (tLimits1[0][0] >= t1Start && tLimits1[0][1] <= t1
104 && tLimits1[1][0] >= t2Start && tLimits1[1][1] <= t2) {
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000105 Cubic cSub1, cSub2;
106 sub_divide(cubic1, t1Start, tEnd1, cSub1);
107 sub_divide(cubic2, t2Start, tEnd2, cSub2);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000108 SkDebugf("%.*s %s t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)", i.depth()*2, tab, __FUNCTION__,
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000109 t1Start, t1, t2Start, t2);
110 Intersections xlocals;
111 intersectWithOrder(s1, o1, s2, o2, xlocals);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000112 SkDebugf(" xlocals.fUsed=%d\n", xlocals.used());
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000113 }
114 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000115 Intersections locals;
116 intersectWithOrder(s1, o1, s2, o2, locals);
117 double coStart[2] = { -1 };
118 _Point coPoint;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000119 int tCount = locals.used();
120 for (int tIdx = 0; tIdx < tCount; ++tIdx) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000121 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
122 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
123 // if the computed t is not sufficiently precise, iterate
124 _Point p1, p2;
125 xy_at_t(cubic1, to1, p1.x, p1.y);
126 xy_at_t(cubic2, to2, p2.x, p2.y);
127 if (p1.approximatelyEqual(p2)) {
128 if (locals.fIsCoincident[0] & 1 << tIdx) {
129 if (coStart[0] < 0) {
130 coStart[0] = to1;
131 coStart[1] = to2;
132 coPoint = p1;
133 } else {
134 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
135 coStart[0] = -1;
136 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000137 result = true;
138 } else if (cubic1 != cubic2 || !approximately_equal(to1, to2)) {
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000139 if (i.swapped()) { // FIXME: insert should respect swap
140 i.insert(to2, to1, p1);
141 } else {
142 i.insert(to1, to2, p1);
143 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000144 result = true;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000145 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000146 } else {
147 double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000148#if 1
149 double c1Bottom = tIdx == 0 ? 0 :
150 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx - 1] + to1) / 2;
151 double c1Min = SkTMax(c1Bottom, to1 - offset);
152 double c1Top = tIdx == tCount - 1 ? 1 :
153 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx + 1] + to1) / 2;
154 double c1Max = SkTMin(c1Top, to1 + offset);
155 double c2Min = SkTMax(0., to2 - offset);
156 double c2Max = SkTMin(1., to2 + offset);
157 #if ONE_OFF_DEBUG
158 SkDebugf("%.*s %s 1 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, __FUNCTION__,
159 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
160 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
161 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
162 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
163 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
164 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
165 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
166 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
167 SkDebugf("%.*s %s 1 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
168 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
169 i.depth()*2, tab, __FUNCTION__, c1Bottom, c1Top, 0., 1.,
170 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
171 SkDebugf("%.*s %s 1 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
172 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
173 #endif
174 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
175 #if ONE_OFF_DEBUG
176 SkDebugf("%.*s %s 1 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, i.used(),
177 i.used() > 0 ? i.fT[0][i.used() - 1] : -1);
178 #endif
179 if (tCount > 1) {
180 c1Min = SkTMax(0., to1 - offset);
181 c1Max = SkTMin(1., to1 + offset);
182 double c2Bottom = tIdx == 0 ? to2 :
183 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx - 1] + to2) / 2;
184 double c2Top = tIdx == tCount - 1 ? to2 :
185 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx + 1] + to2) / 2;
186 if (c2Bottom > c2Top) {
187 SkTSwap(c2Bottom, c2Top);
188 }
189 if (c2Bottom == to2) {
190 c2Bottom = 0;
191 }
192 if (c2Top == to2) {
193 c2Top = 1;
194 }
195 c2Min = SkTMax(c2Bottom, to2 - offset);
196 c2Max = SkTMin(c2Top, to2 + offset);
197 #if ONE_OFF_DEBUG
198 SkDebugf("%.*s %s 2 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, __FUNCTION__,
199 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
200 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
201 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
202 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
203 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
204 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
205 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
206 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
207 SkDebugf("%.*s %s 2 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
208 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
209 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
210 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
211 SkDebugf("%.*s %s 2 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
212 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
213 #endif
214 intersect3(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__, i.used(),
217 i.used() > 0 ? i.fT[0][i.used() - 1] : -1);
218 #endif
219 c1Min = SkTMax(c1Bottom, to1 - offset);
220 c1Max = SkTMin(c1Top, to1 + offset);
221 #if ONE_OFF_DEBUG
222 SkDebugf("%.*s %s 3 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, __FUNCTION__,
223 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
224 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
225 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
226 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
227 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
228 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
229 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
230 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
231 SkDebugf("%.*s %s 3 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
232 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
233 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
234 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
235 SkDebugf("%.*s %s 3 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
236 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
237 #endif
238 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
239 #if ONE_OFF_DEBUG
240 SkDebugf("%.*s %s 3 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, i.used(),
241 i.used() > 0 ? i.fT[0][i.used() - 1] : -1);
242 #endif
243 }
244#else
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000245 double c1Bottom = tIdx == 0 ? 0 :
246 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx - 1] + to1) / 2;
247 double c1Min = SkTMax(c1Bottom, to1 - offset);
248 double c1Top = tIdx == tCount - 1 ? 1 :
249 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx + 1] + to1) / 2;
250 double c1Max = SkTMin(c1Top, to1 + offset);
251 double c2Bottom = tIdx == 0 ? to2 :
252 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx - 1] + to2) / 2;
253 double c2Top = tIdx == tCount - 1 ? to2 :
254 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx + 1] + to2) / 2;
255 if (c2Bottom > c2Top) {
256 SkTSwap(c2Bottom, c2Top);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000257 }
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000258 if (c2Bottom == to2) {
259 c2Bottom = 0;
260 }
261 if (c2Top == to2) {
262 c2Top = 1;
263 }
264 double c2Min = SkTMax(c2Bottom, to2 - offset);
265 double c2Max = SkTMin(c2Top, to2 + offset);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000266 #if ONE_OFF_DEBUG
267 SkDebugf("%s contains1=%d/%d contains2=%d/%d\n", __FUNCTION__,
268 c1Min <= 0.210357794 && 0.210357794 <= c1Max
269 && c2Min <= 0.223476406 && 0.223476406 <= c2Max,
270 to1 - offset <= 0.210357794 && 0.210357794 <= to1 + offset
271 && to2 - offset <= 0.223476406 && 0.223476406 <= to2 + offset,
272 c1Min <= 0.211324707 && 0.211324707 <= c1Max
273 && c2Min <= 0.211327209 && 0.211327209 <= c2Max,
274 to1 - offset <= 0.211324707 && 0.211324707 <= to1 + offset
275 && to2 - offset <= 0.211327209 && 0.211327209 <= to2 + offset);
276 SkDebugf("%s c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
277 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
278 __FUNCTION__, c1Bottom, c1Top, c2Bottom, c2Top,
279 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
280 SkDebugf("%s to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
281 " c2Max=%1.9g\n", __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
282 #endif
283#endif
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000284 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
285 // TODO: if no intersection is found, either quadratics intersected where
286 // cubics did not, or the intersection was missed. In the former case, expect
287 // the quadratics to be nearly parallel at the point of intersection, and check
288 // for that.
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000289 }
290 }
291 SkASSERT(coStart[0] == -1);
292 t2Start = t2;
293 }
294 t1Start = t1;
295 }
296 i.downDepth();
297 return result;
298}
299
skia.committer@gmail.com12eea2b2013-02-27 07:10:10 +0000300// intersect the end of the cubic with the other. Try lines from the end to control and opposite
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000301// end to determine range of t on opposite cubic.
302static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
303 Intersections& i) {
304 _Line line;
305 int t1Index = start ? 0 : 3;
306 line[0] = cubic1[t1Index];
307 // don't bother if the two cubics are connnected
308 if (line[0].approximatelyEqual(cubic2[0]) || line[0].approximatelyEqual(cubic2[3])) {
309 return false;
310 }
311 double tMin = 1, tMax = 0;
312 for (int index = 0; index < 4; ++index) {
313 if (index == t1Index) {
314 continue;
315 }
316 _Vector dxy1 = cubic1[index] - line[0];
317 dxy1 /= gPrecisionUnit;
318 line[1] = line[0] + dxy1;
319 _Rect lineBounds;
320 lineBounds.setBounds(line);
321 if (!bounds2.intersects(lineBounds)) {
322 continue;
323 }
324 Intersections local;
325 if (!intersect(cubic2, line, local)) {
326 continue;
327 }
328 for (int index = 0; index < local.fUsed; ++index) {
329 tMin = SkTMin(tMin, local.fT[0][index]);
330 tMax = SkTMax(tMax, local.fT[0][index]);
331 }
332 }
333 if (tMin > tMax) {
334 return false;
335 }
336 double tMin1 = start ? 0 : 1 - 1.0 / gPrecisionUnit;
337 double tMax1 = start ? 1.0 / gPrecisionUnit : 1;
338 double tMin2 = SkTMax(tMin - 1.0 / gPrecisionUnit, 0.0);
339 double tMax2 = SkTMin(tMax + 1.0 / gPrecisionUnit, 1.0);
340 return intersect3(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
341}
342
caryclark@google.com47d73da2013-02-17 01:41:25 +0000343const double CLOSE_ENOUGH = 0.001;
skia.committer@gmail.come7707c22013-02-17 07:02:20 +0000344
caryclark@google.com47d73da2013-02-17 01:41:25 +0000345static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
346 if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) {
347 return false;
348 }
349 pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2);
350 return true;
351}
352
353static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
354 int last = i.used() - 1;
355 if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
356 return false;
357 }
358 pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2);
359 return true;
360}
361
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000362bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) {
363 bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i);
364 // FIXME: pass in cached bounds from caller
365 _Rect c1Bounds, c2Bounds;
366 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
367 c2Bounds.setBounds(c2);
368 result |= intersectEnd(c1, false, c2, c2Bounds, i);
369 result |= intersectEnd(c1, true, c2, c2Bounds, i);
370 i.swap();
371 result |= intersectEnd(c2, false, c1, c1Bounds, i);
372 result |= intersectEnd(c2, true, c1, c1Bounds, i);
373 i.swap();
caryclark@google.com47d73da2013-02-17 01:41:25 +0000374 // If an end point and a second point very close to the end is returned, the second
375 // point may have been detected because the approximate quads
376 // intersected at the end and close to it. Verify that the second point is valid.
377 if (i.used() <= 1 || i.coincidentUsed()) {
378 return result;
379 }
380 _Point pt[2];
381 if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1])
382 && pt[0].approximatelyEqual(pt[1])) {
383 i.removeOne(1);
384 }
385 if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1])
386 && pt[0].approximatelyEqual(pt[1])) {
387 i.removeOne(i.used() - 2);
388 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000389 return result;
390}
391
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000392// Up promote the quad to a cubic.
393// OPTIMIZATION If this is a common use case, optimize by duplicating
394// the intersect 3 loop to avoid the promotion / demotion code
caryclark@google.com73ca6242013-01-17 21:02:47 +0000395int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000396 Cubic up;
397 toCubic(quad, up);
398 (void) intersect3(cubic, up, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000399 return i.used();
400}
401
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000402/* http://www.ag.jku.at/compass/compasssample.pdf
403( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen
404Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no
405SINTEF Applied Mathematics http://www.sintef.no )
406describes a method to find the self intersection of a cubic by taking the gradient of the implicit
407form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/
408
409int intersect(const Cubic& c, Intersections& i) {
410 // check to see if x or y end points are the extrema. Are other quick rejects possible?
411 if ((between(c[0].x, c[1].x, c[3].x) && between(c[0].x, c[2].x, c[3].x))
412 || (between(c[0].y, c[1].y, c[3].y) && between(c[0].y, c[2].y, c[3].y))) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000413 return false;
414 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000415 (void) intersect3(c, c, i);
416 return i.used();
caryclark@google.com73ca6242013-01-17 21:02:47 +0000417}