blob: d92c8bfe14c36ed2c63abb41cc01fd308beebd40 [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.com639df892012-01-10 21:46:10 +000014
caryclark@google.com45a8fc62013-02-14 15:29:11 +000015#if ONE_OFF_DEBUG
16static const double tLimits[2][2] = {{0.516980827, 0.516981209}, {0.647714088, 0.64771447}};
17#endif
18
caryclark@google.combeda3892013-02-07 13:13:41 +000019#define DEBUG_QUAD_PART 0
caryclark@google.com47d73da2013-02-17 01:41:25 +000020#define SWAP_TOP_DEBUG 0
caryclark@google.comf9502d72013-02-04 14:06:49 +000021
caryclark@google.comf9502d72013-02-04 14:06:49 +000022static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) {
23 Cubic part;
24 sub_divide(cubic, tStart, tEnd, part);
25 Quadratic quad;
26 demote_cubic_to_quad(part, quad);
27 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
28 // extremely shallow quadratic?
caryclark@google.com47d73da2013-02-17 01:41:25 +000029 int order = reduceOrder(quad, simple, kReduceOrder_TreatAsFill);
caryclark@google.combeda3892013-02-07 13:13:41 +000030#if DEBUG_QUAD_PART
31 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",
32 __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y,
33 cubic[3].x, cubic[3].y, tStart, tEnd);
34 SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)"
35 " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, part[0].x, part[0].y,
36 part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y,
37 quad[1].x, quad[1].y, quad[2].x, quad[2].y);
38 SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y);
39 if (order > 1) {
40 SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y);
41 }
42 if (order > 2) {
43 SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y);
44 }
45 SkDebugf(")\n");
46 SkASSERT(order < 4 && order > 0);
47#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +000048 return order;
49}
50
51static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2,
52 int order2, Intersections& i) {
53 if (order1 == 3 && order2 == 3) {
54 intersect2(simple1, simple2, i);
55 } else if (order1 <= 2 && order2 <= 2) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000056 intersect((const _Line&) simple1, (const _Line&) simple2, i);
caryclark@google.comf9502d72013-02-04 14:06:49 +000057 } else if (order1 == 3 && order2 <= 2) {
58 intersect(simple1, (const _Line&) simple2, i);
59 } else {
60 SkASSERT(order1 <= 2 && order2 == 3);
61 intersect(simple2, (const _Line&) simple1, i);
62 for (int s = 0; s < i.fUsed; ++s) {
63 SkTSwap(i.fT[0][s], i.fT[1][s]);
64 }
65 }
66}
67
caryclark@google.combeda3892013-02-07 13:13:41 +000068static double distanceFromEnd(double t) {
69 return t > 0.5 ? 1 - t : t;
70}
71
72// OPTIMIZATION: this used to try to guess the value for delta, and that may still be worthwhile
73static void bumpForRetry(double t1, double t2, double& s1, double& e1, double& s2, double& e2) {
74 double dt1 = distanceFromEnd(t1);
75 double dt2 = distanceFromEnd(t2);
76 double delta = 1.0 / precisionUnit;
77 if (dt1 < dt2) {
78 if (t1 == dt1) {
79 s1 = SkTMax(s1 - delta, 0.);
80 } else {
81 e1 = SkTMin(e1 + delta, 1.);
82 }
83 } else {
84 if (t2 == dt2) {
85 s2 = SkTMax(s2 - delta, 0.);
86 } else {
87 e2 = SkTMin(e2 + delta, 1.);
88 }
89 }
90}
91
92static bool doIntersect(const Cubic& cubic1, double t1s, double t1m, double t1e,
caryclark@google.comf9502d72013-02-04 14:06:49 +000093 const Cubic& cubic2, double t2s, double t2m, double t2e, Intersections& i) {
caryclark@google.combeda3892013-02-07 13:13:41 +000094 bool result = false;
caryclark@google.comf9502d72013-02-04 14:06:49 +000095 i.upDepth();
96 // divide the quadratics at the new t value and try again
97 double p1s = t1s;
98 double p1e = t1m;
99 for (int p1 = 0; p1 < 2; ++p1) {
100 Quadratic s1a;
101 int o1a = quadPart(cubic1, p1s, p1e, s1a);
102 double p2s = t2s;
103 double p2e = t2m;
104 for (int p2 = 0; p2 < 2; ++p2) {
105 Quadratic s2a;
106 int o2a = quadPart(cubic2, p2s, p2e, s2a);
107 Intersections locals;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000108 #if ONE_OFF_DEBUG
109 if (tLimits[0][0] >= p1s && tLimits[0][1] <= p1e
110 && tLimits[1][0] >= p2s && tLimits[1][1] <= p2e) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000111 SkDebugf("t1=(%1.9g,%1.9g) o1=%d t2=(%1.9g,%1.9g) o2=%d\n",
112 p1s, p1e, o1a, p2s, p2e, o2a);
113 if (o1a == 2) {
114 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
115 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y);
116 } else {
117 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
118 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y, s1a[2].x, s1a[2].y);
119 }
120 if (o2a == 2) {
121 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
122 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y);
123 } else {
124 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
125 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y, s2a[2].x, s2a[2].y);
126 }
127 Intersections xlocals;
128 intersectWithOrder(s1a, o1a, s2a, o2a, xlocals);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000129 SkDebugf("xlocals.fUsed=%d depth=%d\n", xlocals.used(), i.depth());
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000130 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000131 #endif
132 intersectWithOrder(s1a, o1a, s2a, o2a, locals);
133 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
134 double to1 = p1s + (p1e - p1s) * locals.fT[0][tIdx];
135 double to2 = p2s + (p2e - p2s) * locals.fT[1][tIdx];
136 // if the computed t is not sufficiently precise, iterate
137 _Point p1, p2;
138 xy_at_t(cubic1, to1, p1.x, p1.y);
139 xy_at_t(cubic2, to2, p2.x, p2.y);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000140 #if ONE_OFF_DEBUG
caryclark@google.comf9502d72013-02-04 14:06:49 +0000141 SkDebugf("to1=%1.9g p1=(%1.9g,%1.9g) to2=%1.9g p2=(%1.9g,%1.9g) d=%1.9g\n",
142 to1, p1.x, p1.y, to2, p2.x, p2.y, p1.distance(p2));
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000143
caryclark@google.comf9502d72013-02-04 14:06:49 +0000144 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000145 if (p1.approximatelyEqualHalf(p2)) {
146 i.insertSwap(to1, to2, p1);
caryclark@google.combeda3892013-02-07 13:13:41 +0000147 result = true;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000148 } else {
caryclark@google.combeda3892013-02-07 13:13:41 +0000149 result = doIntersect(cubic1, p1s, to1, p1e, cubic2, p2s, to2, p2e, i);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000150 if (!result && p1.approximatelyEqual(p2)) {
151 i.insertSwap(to1, to2, p1);
caryclark@google.com47d73da2013-02-17 01:41:25 +0000152 #if SWAP_TOP_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000153 SkDebugf("!!!\n");
caryclark@google.com47d73da2013-02-17 01:41:25 +0000154 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000155 result = true;
156 } else
caryclark@google.combeda3892013-02-07 13:13:41 +0000157 // if both cubics curve in the same direction, the quadratic intersection
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000158 // may mark a range that does not contain the cubic intersection. If no
159 // intersection is found, look again including the t distance of the
caryclark@google.combeda3892013-02-07 13:13:41 +0000160 // of the quadratic intersection nearest a quadratic end (which in turn is
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000161 // nearest the actual cubic)
caryclark@google.combeda3892013-02-07 13:13:41 +0000162 if (!result) {
163 double b1s = p1s;
164 double b1e = p1e;
165 double b2s = p2s;
166 double b2e = p2e;
167 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
168 result = doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
169 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000170 }
171 }
172 p2s = p2e;
173 p2e = t2e;
174 }
175 p1s = p1e;
176 p1e = t1e;
177 }
178 i.downDepth();
caryclark@google.combeda3892013-02-07 13:13:41 +0000179 return result;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000180}
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000181
caryclark@google.com73ca6242013-01-17 21:02:47 +0000182// this flavor approximates the cubics with quads to find the intersecting ts
183// OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used
184// to create the approximations, could be stored in the cubic segment
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000185// FIXME: this strategy needs to intersect the convex hull on either end with the opposite to
186// account for inset quadratics that cause the endpoint intersection to avoid detection
187// the segments can be very short -- the length of the maximum quadratic error (precision)
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000188static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000189 double t2s, double t2e, double precisionScale, Intersections& i) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000190 Cubic c1, c2;
191 sub_divide(cubic1, t1s, t1e, c1);
192 sub_divide(cubic2, t2s, t2e, c2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000193 SkTDArray<double> ts1;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000194 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000195 SkTDArray<double> ts2;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000196 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000197 double t1Start = t1s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000198 int ts1Count = ts1.count();
199 for (int i1 = 0; i1 <= ts1Count; ++i1) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000200 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
201 const double t1 = t1s + (t1e - t1s) * tEnd1;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000202 Quadratic s1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000203 int o1 = quadPart(cubic1, t1Start, t1, s1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000204 double t2Start = t2s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000205 int ts2Count = ts2.count();
206 for (int i2 = 0; i2 <= ts2Count; ++i2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000207 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
208 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000209 Quadratic s2;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000210 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000211 #if ONE_OFF_DEBUG
212 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
213 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
caryclark@google.combeda3892013-02-07 13:13:41 +0000214 Cubic cSub1, cSub2;
215 sub_divide(cubic1, t1Start, tEnd1, cSub1);
216 sub_divide(cubic2, t2Start, tEnd2, cSub2);
217 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
218 t1Start, t1, t2Start, t2);
219 Intersections xlocals;
220 intersectWithOrder(s1, o1, s2, o2, xlocals);
221 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
222 }
223 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000224 Intersections locals;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000225 intersectWithOrder(s1, o1, s2, o2, locals);
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000226
caryclark@google.com73ca6242013-01-17 21:02:47 +0000227 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
228 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
229 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000230 // if the computed t is not sufficiently precise, iterate
231 _Point p1, p2;
232 xy_at_t(cubic1, to1, p1.x, p1.y);
233 xy_at_t(cubic2, to2, p2.x, p2.y);
234 if (p1.approximatelyEqual(p2)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000235 i.insert(to1, to2, p1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000236 } else {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000237 #if ONE_OFF_DEBUG
238 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
239 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
caryclark@google.combeda3892013-02-07 13:13:41 +0000240 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
241 t1Start, t1, t2Start, t2);
242 }
243 #endif
244 bool found = doIntersect(cubic1, t1Start, to1, t1, cubic2, t2Start, to2, t2, i);
245 if (!found) {
246 double b1s = t1Start;
247 double b1e = t1;
248 double b2s = t2Start;
249 double b2e = t2;
250 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
251 doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
252 }
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000253 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000254 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000255 int coincidentCount = locals.coincidentUsed();
256 if (coincidentCount) {
257 // FIXME: one day, we'll probably need to allow coincident + non-coincident pts
258 SkASSERT(coincidentCount == locals.used());
259 SkASSERT(coincidentCount == 2);
caryclark@google.combeda3892013-02-07 13:13:41 +0000260 double coTs[2][2];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000261 for (int tIdx = 0; tIdx < coincidentCount; ++tIdx) {
262 if (locals.fIsCoincident[0] & (1 << tIdx)) {
263 coTs[0][tIdx] = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
264 }
265 if (locals.fIsCoincident[1] & (1 << tIdx)) {
266 coTs[1][tIdx] = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
267 }
caryclark@google.combeda3892013-02-07 13:13:41 +0000268 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000269 i.insertCoincidentPair(coTs[0][0], coTs[0][1], coTs[1][0], coTs[1][1],
270 locals.fPt[0], locals.fPt[1]);
caryclark@google.combeda3892013-02-07 13:13:41 +0000271 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000272 t2Start = t2;
273 }
274 t1Start = t1;
275 }
276 return i.intersected();
277}
278
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000279static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
280 Intersections& i) {
281 _Line line1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000282 line1[1] = cubic1[start ? 0 : 3];
283 if (line1[1].approximatelyEqual(cubic2[0]) || line1[1].approximatelyEqual(cubic2[3])) {
284 return false;
285 }
286 line1[0] = line1[1];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000287 _Point dxy1 = line1[0] - cubic1[start ? 1 : 2];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000288 if (dxy1.approximatelyZero()) {
289 dxy1 = line1[0] - cubic1[start ? 2 : 1];
290 }
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000291 dxy1 /= precisionUnit;
292 line1[1] += dxy1;
293 _Rect line1Bounds;
294 line1Bounds.setBounds(line1);
295 if (!bounds2.intersects(line1Bounds)) {
296 return false;
297 }
298 _Line line2;
299 line2[0] = line2[1] = line1[0];
300 _Point dxy2 = line2[0] - cubic1[start ? 3 : 0];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000301 SkASSERT(!dxy2.approximatelyZero());
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000302 dxy2 /= precisionUnit;
303 line2[1] += dxy2;
304#if 0 // this is so close to the first bounds test it isn't worth the short circuit test
305 _Rect line2Bounds;
306 line2Bounds.setBounds(line2);
307 if (!bounds2.intersects(line2Bounds)) {
308 return false;
309 }
310#endif
311 Intersections local1;
312 if (!intersect(cubic2, line1, local1)) {
313 return false;
314 }
315 Intersections local2;
316 if (!intersect(cubic2, line2, local2)) {
317 return false;
318 }
319 double tMin, tMax;
320 tMin = tMax = local1.fT[0][0];
321 for (int index = 1; index < local1.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000322 tMin = SkTMin(tMin, local1.fT[0][index]);
323 tMax = SkTMax(tMax, local1.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000324 }
325 for (int index = 1; index < local2.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000326 tMin = SkTMin(tMin, local2.fT[0][index]);
327 tMax = SkTMax(tMax, local2.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000328 }
329 return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000330 cubic2, tMin, tMax, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000331}
332
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000333// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000334// chase intersections near quadratic ends, requiring odd hacks to find them.
335static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
336 double t2s, double t2e, double precisionScale, Intersections& i) {
337 i.upDepth();
338 bool result = false;
339 Cubic c1, c2;
340 sub_divide(cubic1, t1s, t1e, c1);
341 sub_divide(cubic2, t2s, t2e, c2);
342 SkTDArray<double> ts1;
343 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
344 SkTDArray<double> ts2;
345 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
346 double t1Start = t1s;
347 int ts1Count = ts1.count();
348 for (int i1 = 0; i1 <= ts1Count; ++i1) {
349 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
350 const double t1 = t1s + (t1e - t1s) * tEnd1;
351 Quadratic s1;
352 int o1 = quadPart(cubic1, t1Start, t1, s1);
353 double t2Start = t2s;
354 int ts2Count = ts2.count();
355 for (int i2 = 0; i2 <= ts2Count; ++i2) {
356 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
357 const double t2 = t2s + (t2e - t2s) * tEnd2;
358 Quadratic s2;
359 int o2 = quadPart(cubic2, t2Start, t2, s2);
360 Intersections locals;
361 intersectWithOrder(s1, o1, s2, o2, locals);
362 double coStart[2] = { -1 };
363 _Point coPoint;
364 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
365 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
366 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
367 // if the computed t is not sufficiently precise, iterate
368 _Point p1, p2;
369 xy_at_t(cubic1, to1, p1.x, p1.y);
370 xy_at_t(cubic2, to2, p2.x, p2.y);
371 if (p1.approximatelyEqual(p2)) {
372 if (locals.fIsCoincident[0] & 1 << tIdx) {
373 if (coStart[0] < 0) {
374 coStart[0] = to1;
375 coStart[1] = to2;
376 coPoint = p1;
377 } else {
378 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
379 coStart[0] = -1;
380 }
381 } else {
382 i.insert(to1, to2, p1);
383 }
384 result = true;
385 } else {
386 double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine
387 double c1Min = SkTMax(0., to1 - offset);
388 double c1Max = SkTMin(1., to1 + offset);
389 double c2Min = SkTMax(0., to2 - offset);
390 double c2Max = SkTMin(1., to2 + offset);
391 bool found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
392 if (false && !found) {
393 // either offset was overagressive or cubics didn't really intersect
394 // if they didn't intersect, then quad tangents ought to be nearly parallel
395 offset = precisionScale / 2; // try much less agressive offset
396 c1Min = SkTMax(0., to1 - offset);
397 c1Max = SkTMin(1., to1 + offset);
398 c2Min = SkTMax(0., to2 - offset);
399 c2Max = SkTMin(1., to2 + offset);
400 found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
401 if (found) {
402 SkDebugf("%s *** over-aggressive? offset=%1.9g depth=%d\n", __FUNCTION__,
403 offset, i.depth());
404 }
405 // try parallel measure
406 _Point d1 = dxdy_at_t(cubic1, to1);
407 _Point d2 = dxdy_at_t(cubic2, to2);
408 double shallow = d1.cross(d2);
409 #if 1 || ONE_OFF_DEBUG // not sure this is worth debugging
410 if (!approximately_zero(shallow)) {
411 SkDebugf("%s *** near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
412 offset, i.depth());
413 }
414 #endif
415 if (i.depth() == 1 && shallow < 0.6) {
416 SkDebugf("%s !!! near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
417 offset, i.depth());
418 }
419 }
420 }
421 }
422 SkASSERT(coStart[0] == -1);
423 t2Start = t2;
424 }
425 t1Start = t1;
426 }
427 i.downDepth();
428 return result;
429}
430
caryclark@google.comf9502d72013-02-04 14:06:49 +0000431// FIXME: add intersection of convex hull on cubics' ends with the opposite cubic. The hull line
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000432// segments can be constructed to be only as long as the calculated precision suggests. If the hull
433// line segments intersect the cubic, then use the intersections to construct a subdivision for
434// quadratic curve fitting.
435bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000436 bool result = intersect2(c1, 0, 1, c2, 0, 1, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000437 // FIXME: pass in cached bounds from caller
438 _Rect c1Bounds, c2Bounds;
439 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
440 c2Bounds.setBounds(c2);
441 result |= intersectEnd(c1, false, c2, c2Bounds, i);
442 result |= intersectEnd(c1, true, c2, c2Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000443 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000444 result |= intersectEnd(c2, false, c1, c1Bounds, i);
445 result |= intersectEnd(c2, true, c1, c1Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000446 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000447 return result;
448}
449
caryclark@google.com47d73da2013-02-17 01:41:25 +0000450const double CLOSE_ENOUGH = 0.001;
skia.committer@gmail.come7707c22013-02-17 07:02:20 +0000451
caryclark@google.com47d73da2013-02-17 01:41:25 +0000452static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
453 if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) {
454 return false;
455 }
456 pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2);
457 return true;
458}
459
460static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
461 int last = i.used() - 1;
462 if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
463 return false;
464 }
465 pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2);
466 return true;
467}
468
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000469bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) {
470 bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i);
471 // FIXME: pass in cached bounds from caller
472 _Rect c1Bounds, c2Bounds;
473 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
474 c2Bounds.setBounds(c2);
475 result |= intersectEnd(c1, false, c2, c2Bounds, i);
476 result |= intersectEnd(c1, true, c2, c2Bounds, i);
477 i.swap();
478 result |= intersectEnd(c2, false, c1, c1Bounds, i);
479 result |= intersectEnd(c2, true, c1, c1Bounds, i);
480 i.swap();
caryclark@google.com47d73da2013-02-17 01:41:25 +0000481 // If an end point and a second point very close to the end is returned, the second
482 // point may have been detected because the approximate quads
483 // intersected at the end and close to it. Verify that the second point is valid.
484 if (i.used() <= 1 || i.coincidentUsed()) {
485 return result;
486 }
487 _Point pt[2];
488 if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1])
489 && pt[0].approximatelyEqual(pt[1])) {
490 i.removeOne(1);
491 }
492 if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1])
493 && pt[0].approximatelyEqual(pt[1])) {
494 i.removeOne(i.used() - 2);
495 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000496 return result;
497}
498
caryclark@google.com73ca6242013-01-17 21:02:47 +0000499int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
500 SkTDArray<double> ts;
501 double precision = calcPrecision(cubic);
502 cubic_to_quadratics(cubic, precision, ts);
503 double tStart = 0;
504 Cubic part;
505 int tsCount = ts.count();
506 for (int idx = 0; idx <= tsCount; ++idx) {
507 double t = idx < tsCount ? ts[idx] : 1;
508 Quadratic q1;
509 sub_divide(cubic, tStart, t, part);
510 demote_cubic_to_quad(part, q1);
511 Intersections locals;
512 intersect2(q1, quad, locals);
513 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
514 double globalT = tStart + (t - tStart) * locals.fT[0][tIdx];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000515 i.insert(globalT, locals.fT[1][tIdx], locals.fPt[tIdx]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000516 }
517 tStart = t;
518 }
519 return i.used();
520}
521
522bool intersect(const Cubic& cubic, Intersections& i) {
523 SkTDArray<double> ts;
524 double precision = calcPrecision(cubic);
525 cubic_to_quadratics(cubic, precision, ts);
526 int tsCount = ts.count();
527 if (tsCount == 1) {
528 return false;
529 }
530 double t1Start = 0;
531 Cubic part;
532 for (int idx = 0; idx < tsCount; ++idx) {
533 double t1 = ts[idx];
534 Quadratic q1;
535 sub_divide(cubic, t1Start, t1, part);
536 demote_cubic_to_quad(part, q1);
537 double t2Start = t1;
538 for (int i2 = idx + 1; i2 <= tsCount; ++i2) {
539 const double t2 = i2 < tsCount ? ts[i2] : 1;
540 Quadratic q2;
541 sub_divide(cubic, t2Start, t2, part);
542 demote_cubic_to_quad(part, q2);
543 Intersections locals;
544 intersect2(q1, q2, locals);
545 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
546 // discard intersections at cusp? (maximum curvature)
547 double t1sect = locals.fT[0][tIdx];
548 double t2sect = locals.fT[1][tIdx];
549 if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) {
550 continue;
551 }
552 double to1 = t1Start + (t1 - t1Start) * t1sect;
553 double to2 = t2Start + (t2 - t2Start) * t2sect;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000554 i.insert(to1, to2, locals.fPt[tIdx]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000555 }
556 t2Start = t2;
557 }
558 t1Start = t1;
559 }
560 return i.intersected();
561}