blob: dc534e06c4d6ec328cf5de537045ce0eda7fe3a3 [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.com1304bb22013-03-13 20:29:41 +000015#include "TSearch.h"
caryclark@google.com639df892012-01-10 21:46:10 +000016
caryclark@google.com45a8fc62013-02-14 15:29:11 +000017#if ONE_OFF_DEBUG
caryclark@google.com1304bb22013-03-13 20:29:41 +000018static const double tLimits1[2][2] = {{0.328432751, 0.328433132}, {0.556114578, 0.55611496}};
19static const double tLimits2[2][2] = {{-0.83051487, -0.830515252}, {-0.860977985, -0.860978367}};
caryclark@google.com45a8fc62013-02-14 15:29:11 +000020#endif
21
caryclark@google.combeda3892013-02-07 13:13:41 +000022#define DEBUG_QUAD_PART 0
caryclark@google.com47d73da2013-02-17 01:41:25 +000023#define SWAP_TOP_DEBUG 0
caryclark@google.comf9502d72013-02-04 14:06:49 +000024
caryclark@google.comf9502d72013-02-04 14:06:49 +000025static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) {
26 Cubic part;
27 sub_divide(cubic, tStart, tEnd, part);
28 Quadratic quad;
29 demote_cubic_to_quad(part, quad);
30 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
31 // extremely shallow quadratic?
caryclark@google.com47d73da2013-02-17 01:41:25 +000032 int order = reduceOrder(quad, simple, kReduceOrder_TreatAsFill);
caryclark@google.combeda3892013-02-07 13:13:41 +000033#if DEBUG_QUAD_PART
34 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",
35 __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y,
36 cubic[3].x, cubic[3].y, 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__, part[0].x, part[0].y,
39 part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y,
40 quad[1].x, quad[1].y, quad[2].x, quad[2].y);
41 SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y);
42 if (order > 1) {
43 SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y);
44 }
45 if (order > 2) {
46 SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y);
47 }
48 SkDebugf(")\n");
49 SkASSERT(order < 4 && order > 0);
50#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +000051 return order;
52}
53
54static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2,
55 int order2, Intersections& i) {
56 if (order1 == 3 && order2 == 3) {
57 intersect2(simple1, simple2, i);
58 } else if (order1 <= 2 && order2 <= 2) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000059 intersect((const _Line&) simple1, (const _Line&) simple2, i);
caryclark@google.comf9502d72013-02-04 14:06:49 +000060 } else if (order1 == 3 && order2 <= 2) {
61 intersect(simple1, (const _Line&) simple2, i);
62 } else {
63 SkASSERT(order1 <= 2 && order2 == 3);
64 intersect(simple2, (const _Line&) simple1, i);
65 for (int s = 0; s < i.fUsed; ++s) {
66 SkTSwap(i.fT[0][s], i.fT[1][s]);
67 }
68 }
69}
70
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000071// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
caryclark@google.com45a8fc62013-02-14 15:29:11 +000072// chase intersections near quadratic ends, requiring odd hacks to find them.
73static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
74 double t2s, double t2e, double precisionScale, Intersections& i) {
75 i.upDepth();
76 bool result = false;
77 Cubic c1, c2;
78 sub_divide(cubic1, t1s, t1e, c1);
79 sub_divide(cubic2, t2s, t2e, c2);
80 SkTDArray<double> ts1;
caryclark@google.com1304bb22013-03-13 20:29:41 +000081 // OPTIMIZE: if c1 == c2, call once (happens when detecting self-intersection)
caryclark@google.com45a8fc62013-02-14 15:29:11 +000082 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
83 SkTDArray<double> ts2;
84 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
85 double t1Start = t1s;
86 int ts1Count = ts1.count();
87 for (int i1 = 0; i1 <= ts1Count; ++i1) {
88 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
89 const double t1 = t1s + (t1e - t1s) * tEnd1;
90 Quadratic s1;
91 int o1 = quadPart(cubic1, t1Start, t1, s1);
92 double t2Start = t2s;
93 int ts2Count = ts2.count();
94 for (int i2 = 0; i2 <= ts2Count; ++i2) {
95 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
96 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.comc83c70e2013-02-22 21:50:07 +000097 if (cubic1 == cubic2 && t1Start >= t2Start) {
98 t2Start = t2;
99 continue;
100 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000101 Quadratic s2;
102 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000103 #if ONE_OFF_DEBUG
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000104 char tab[] = " ";
105 if (tLimits1[0][0] >= t1Start && tLimits1[0][1] <= t1
106 && tLimits1[1][0] >= t2Start && tLimits1[1][1] <= t2) {
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000107 Cubic cSub1, cSub2;
caryclark@google.com1304bb22013-03-13 20:29:41 +0000108 sub_divide(cubic1, t1Start, t1, cSub1);
109 sub_divide(cubic2, t2Start, t2, cSub2);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000110 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 +0000111 t1Start, t1, t2Start, t2);
112 Intersections xlocals;
113 intersectWithOrder(s1, o1, s2, o2, xlocals);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000114 SkDebugf(" xlocals.fUsed=%d\n", xlocals.used());
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000115 }
116 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000117 Intersections locals;
118 intersectWithOrder(s1, o1, s2, o2, locals);
119 double coStart[2] = { -1 };
120 _Point coPoint;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000121 int tCount = locals.used();
122 for (int tIdx = 0; tIdx < tCount; ++tIdx) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000123 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
124 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
125 // if the computed t is not sufficiently precise, iterate
caryclark@google.com1304bb22013-03-13 20:29:41 +0000126 _Point p1 = xy_at_t(cubic1, to1);
127 _Point p2 = xy_at_t(cubic2, to2);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000128 if (p1.approximatelyEqual(p2)) {
129 if (locals.fIsCoincident[0] & 1 << tIdx) {
130 if (coStart[0] < 0) {
131 coStart[0] = to1;
132 coStart[1] = to2;
133 coPoint = p1;
134 } else {
135 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
136 coStart[0] = -1;
137 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000138 result = true;
139 } else if (cubic1 != cubic2 || !approximately_equal(to1, to2)) {
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000140 if (i.swapped()) { // FIXME: insert should respect swap
141 i.insert(to2, to1, p1);
142 } else {
143 i.insert(to1, to2, p1);
144 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000145 result = true;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000146 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000147 } else {
148 double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000149#if 1
150 double c1Bottom = tIdx == 0 ? 0 :
151 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx - 1] + to1) / 2;
152 double c1Min = SkTMax(c1Bottom, to1 - offset);
153 double c1Top = tIdx == tCount - 1 ? 1 :
154 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx + 1] + to1) / 2;
155 double c1Max = SkTMin(c1Top, to1 + offset);
156 double c2Min = SkTMax(0., to2 - offset);
157 double c2Max = SkTMin(1., to2 + offset);
158 #if ONE_OFF_DEBUG
159 SkDebugf("%.*s %s 1 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, __FUNCTION__,
160 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
161 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
162 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
163 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
164 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
165 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
166 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
167 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
168 SkDebugf("%.*s %s 1 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
169 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
skia.committer@gmail.com64334352013-03-06 07:01:46 +0000170 i.depth()*2, tab, __FUNCTION__, c1Bottom, c1Top, 0., 1.,
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000171 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
172 SkDebugf("%.*s %s 1 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
173 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
174 #endif
175 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
176 #if ONE_OFF_DEBUG
177 SkDebugf("%.*s %s 1 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, i.used(),
178 i.used() > 0 ? i.fT[0][i.used() - 1] : -1);
179 #endif
180 if (tCount > 1) {
181 c1Min = SkTMax(0., to1 - offset);
182 c1Max = SkTMin(1., to1 + offset);
183 double c2Bottom = tIdx == 0 ? to2 :
184 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx - 1] + to2) / 2;
185 double c2Top = tIdx == tCount - 1 ? to2 :
186 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx + 1] + to2) / 2;
187 if (c2Bottom > c2Top) {
188 SkTSwap(c2Bottom, c2Top);
189 }
190 if (c2Bottom == to2) {
191 c2Bottom = 0;
192 }
193 if (c2Top == to2) {
194 c2Top = 1;
195 }
196 c2Min = SkTMax(c2Bottom, to2 - offset);
197 c2Max = SkTMin(c2Top, to2 + offset);
198 #if ONE_OFF_DEBUG
199 SkDebugf("%.*s %s 2 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, __FUNCTION__,
200 c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max
201 && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max,
202 to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset
203 && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset,
204 c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max
205 && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max,
206 to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset
207 && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset);
208 SkDebugf("%.*s %s 2 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
209 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
skia.committer@gmail.com64334352013-03-06 07:01:46 +0000210 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000211 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
212 SkDebugf("%.*s %s 2 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
213 " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
214 #endif
215 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
216 #if ONE_OFF_DEBUG
217 SkDebugf("%.*s %s 2 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, i.used(),
218 i.used() > 0 ? i.fT[0][i.used() - 1] : -1);
219 #endif
220 c1Min = SkTMax(c1Bottom, to1 - offset);
221 c1Max = SkTMin(c1Top, to1 + offset);
222 #if ONE_OFF_DEBUG
223 SkDebugf("%.*s %s 3 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, __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",
skia.committer@gmail.com64334352013-03-06 07:01:46 +0000234 i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top,
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000235 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, c1Max, c2Min, c2Max);
238 #endif
239 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
240 #if ONE_OFF_DEBUG
241 SkDebugf("%.*s %s 3 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, i.used(),
242 i.used() > 0 ? i.fT[0][i.used() - 1] : -1);
243 #endif
244 }
skia.committer@gmail.com64334352013-03-06 07:01:46 +0000245#else
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000246 double c1Bottom = tIdx == 0 ? 0 :
247 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx - 1] + to1) / 2;
248 double c1Min = SkTMax(c1Bottom, to1 - offset);
249 double c1Top = tIdx == tCount - 1 ? 1 :
250 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx + 1] + to1) / 2;
251 double c1Max = SkTMin(c1Top, to1 + offset);
252 double c2Bottom = tIdx == 0 ? to2 :
253 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx - 1] + to2) / 2;
254 double c2Top = tIdx == tCount - 1 ? to2 :
255 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx + 1] + to2) / 2;
256 if (c2Bottom > c2Top) {
257 SkTSwap(c2Bottom, c2Top);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000258 }
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000259 if (c2Bottom == to2) {
260 c2Bottom = 0;
261 }
262 if (c2Top == to2) {
263 c2Top = 1;
264 }
265 double c2Min = SkTMax(c2Bottom, to2 - offset);
266 double c2Max = SkTMin(c2Top, to2 + offset);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000267 #if ONE_OFF_DEBUG
268 SkDebugf("%s contains1=%d/%d contains2=%d/%d\n", __FUNCTION__,
269 c1Min <= 0.210357794 && 0.210357794 <= c1Max
270 && c2Min <= 0.223476406 && 0.223476406 <= c2Max,
271 to1 - offset <= 0.210357794 && 0.210357794 <= to1 + offset
272 && to2 - offset <= 0.223476406 && 0.223476406 <= to2 + offset,
273 c1Min <= 0.211324707 && 0.211324707 <= c1Max
274 && c2Min <= 0.211327209 && 0.211327209 <= c2Max,
275 to1 - offset <= 0.211324707 && 0.211324707 <= to1 + offset
276 && to2 - offset <= 0.211327209 && 0.211327209 <= to2 + offset);
277 SkDebugf("%s c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g"
278 " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n",
skia.committer@gmail.com64334352013-03-06 07:01:46 +0000279 __FUNCTION__, c1Bottom, c1Top, c2Bottom, c2Top,
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000280 to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset);
281 SkDebugf("%s to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g"
282 " c2Max=%1.9g\n", __FUNCTION__, to1, to2, c1Min, c1Max, c2Min, c2Max);
283 #endif
284#endif
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000285 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
286 // TODO: if no intersection is found, either quadratics intersected where
287 // cubics did not, or the intersection was missed. In the former case, expect
288 // the quadratics to be nearly parallel at the point of intersection, and check
289 // for that.
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000290 }
291 }
292 SkASSERT(coStart[0] == -1);
293 t2Start = t2;
294 }
295 t1Start = t1;
296 }
297 i.downDepth();
298 return result;
299}
300
caryclark@google.com1304bb22013-03-13 20:29:41 +0000301#if 0
302#define LINE_FRACTION (1.0 / gPrecisionUnit)
303#else
304#define LINE_FRACTION 0.1
305#endif
306
skia.committer@gmail.com12eea2b2013-02-27 07:10:10 +0000307// 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 +0000308// end to determine range of t on opposite cubic.
309static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
310 Intersections& i) {
caryclark@google.com1304bb22013-03-13 20:29:41 +0000311 // bool selfIntersect = cubic1 == cubic2;
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000312 _Line line;
313 int t1Index = start ? 0 : 3;
314 line[0] = cubic1[t1Index];
315 // don't bother if the two cubics are connnected
caryclark@google.com1304bb22013-03-13 20:29:41 +0000316#if 0
317 if (!selfIntersect && (line[0].approximatelyEqual(cubic2[0])
318 || line[0].approximatelyEqual(cubic2[3]))) {
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000319 return false;
320 }
caryclark@google.com1304bb22013-03-13 20:29:41 +0000321#endif
322 bool result = false;
323 SkTDArray<double> tVals; // OPTIMIZE: replace with hard-sized array
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000324 for (int index = 0; index < 4; ++index) {
325 if (index == t1Index) {
326 continue;
327 }
328 _Vector dxy1 = cubic1[index] - line[0];
329 dxy1 /= gPrecisionUnit;
330 line[1] = line[0] + dxy1;
331 _Rect lineBounds;
332 lineBounds.setBounds(line);
333 if (!bounds2.intersects(lineBounds)) {
334 continue;
335 }
336 Intersections local;
337 if (!intersect(cubic2, line, local)) {
338 continue;
339 }
caryclark@google.com1304bb22013-03-13 20:29:41 +0000340 for (int idx2 = 0; idx2 < local.used(); ++idx2) {
341 double foundT = local.fT[0][idx2];
342 if (approximately_less_than_zero(foundT)
343 || approximately_greater_than_one(foundT)) {
344 continue;
345 }
346 if (local.fPt[idx2].approximatelyEqual(line[0])) {
347 if (i.swapped()) { // FIXME: insert should respect swap
348 i.insert(foundT, start ? 0 : 1, line[0]);
349 } else {
350 i.insert(start ? 0 : 1, foundT, line[0]);
351 }
352 result = true;
353 } else {
354 *tVals.append() = local.fT[0][idx2];
355 }
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000356 }
357 }
caryclark@google.com1304bb22013-03-13 20:29:41 +0000358 if (tVals.count() == 0) {
359 return result;
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000360 }
caryclark@google.com1304bb22013-03-13 20:29:41 +0000361 QSort<double>(tVals.begin(), tVals.end() - 1);
362 double tMin1 = start ? 0 : 1 - LINE_FRACTION;
363 double tMax1 = start ? LINE_FRACTION : 1;
364 int tIdx = 0;
365 do {
366 int tLast = tIdx;
367 while (tLast + 1 < tVals.count() && roughly_equal(tVals[tLast + 1], tVals[tIdx])) {
368 ++tLast;
369 }
370 double tMin2 = SkTMax(tVals[tIdx] - LINE_FRACTION, 0.0);
371 double tMax2 = SkTMin(tVals[tLast] + LINE_FRACTION, 1.0);
372 int lastUsed = i.used();
373 result |= intersect3(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
374 if (lastUsed == i.used()) {
375 tMin2 = SkTMax(tVals[tIdx] - (1.0 / gPrecisionUnit), 0.0);
376 tMax2 = SkTMin(tVals[tLast] + (1.0 / gPrecisionUnit), 1.0);
377 result |= intersect3(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
378 }
379 tIdx = tLast + 1;
380 } while (tIdx < tVals.count());
381 return result;
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000382}
383
caryclark@google.com47d73da2013-02-17 01:41:25 +0000384const double CLOSE_ENOUGH = 0.001;
skia.committer@gmail.come7707c22013-02-17 07:02:20 +0000385
caryclark@google.com47d73da2013-02-17 01:41:25 +0000386static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
387 if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) {
388 return false;
389 }
390 pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2);
391 return true;
392}
393
394static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
395 int last = i.used() - 1;
396 if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
397 return false;
398 }
399 pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2);
400 return true;
401}
402
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000403bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) {
404 bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i);
405 // FIXME: pass in cached bounds from caller
406 _Rect c1Bounds, c2Bounds;
407 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
408 c2Bounds.setBounds(c2);
409 result |= intersectEnd(c1, false, c2, c2Bounds, i);
410 result |= intersectEnd(c1, true, c2, c2Bounds, i);
caryclark@google.com1304bb22013-03-13 20:29:41 +0000411 bool selfIntersect = c1 == c2;
412 if (!selfIntersect) {
413 i.swap();
414 result |= intersectEnd(c2, false, c1, c1Bounds, i);
415 result |= intersectEnd(c2, true, c1, c1Bounds, i);
416 i.swap();
417 }
caryclark@google.com47d73da2013-02-17 01:41:25 +0000418 // If an end point and a second point very close to the end is returned, the second
419 // point may have been detected because the approximate quads
420 // intersected at the end and close to it. Verify that the second point is valid.
421 if (i.used() <= 1 || i.coincidentUsed()) {
422 return result;
423 }
424 _Point pt[2];
425 if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1])
426 && pt[0].approximatelyEqual(pt[1])) {
427 i.removeOne(1);
428 }
429 if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1])
430 && pt[0].approximatelyEqual(pt[1])) {
431 i.removeOne(i.used() - 2);
432 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000433 return result;
434}
435
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000436// Up promote the quad to a cubic.
437// OPTIMIZATION If this is a common use case, optimize by duplicating
438// the intersect 3 loop to avoid the promotion / demotion code
caryclark@google.com73ca6242013-01-17 21:02:47 +0000439int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000440 Cubic up;
441 toCubic(quad, up);
442 (void) intersect3(cubic, up, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000443 return i.used();
444}
445
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000446/* http://www.ag.jku.at/compass/compasssample.pdf
447( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen
448Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no
449SINTEF Applied Mathematics http://www.sintef.no )
450describes a method to find the self intersection of a cubic by taking the gradient of the implicit
451form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/
452
453int intersect(const Cubic& c, Intersections& i) {
454 // check to see if x or y end points are the extrema. Are other quick rejects possible?
caryclark@google.com1304bb22013-03-13 20:29:41 +0000455 if (ends_are_extrema_in_x_or_y(c)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000456 return false;
457 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000458 (void) intersect3(c, c, i);
caryclark@google.com1304bb22013-03-13 20:29:41 +0000459 if (i.used() > 0) {
460 SkASSERT(i.used() == 1);
461 if (i.fT[0][0] > i.fT[1][0]) {
462 SkTSwap(i.fT[0][0], i.fT[1][0]);
463 }
464 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000465 return i.used();
caryclark@google.com73ca6242013-01-17 21:02:47 +0000466}