blob: 02310a3be78ebd9d6dfa4fe90615f12bd31c0976 [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.comf9502d72013-02-04 14:06:49 +000020
caryclark@google.comf9502d72013-02-04 14:06:49 +000021static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) {
22 Cubic part;
23 sub_divide(cubic, tStart, tEnd, part);
24 Quadratic quad;
25 demote_cubic_to_quad(part, quad);
26 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
27 // extremely shallow quadratic?
28 int order = reduceOrder(quad, simple);
caryclark@google.combeda3892013-02-07 13:13:41 +000029#if DEBUG_QUAD_PART
30 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",
31 __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y,
32 cubic[3].x, cubic[3].y, tStart, tEnd);
33 SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)"
34 " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, part[0].x, part[0].y,
35 part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y,
36 quad[1].x, quad[1].y, quad[2].x, quad[2].y);
37 SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y);
38 if (order > 1) {
39 SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y);
40 }
41 if (order > 2) {
42 SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y);
43 }
44 SkDebugf(")\n");
45 SkASSERT(order < 4 && order > 0);
46#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +000047 return order;
48}
49
50static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2,
51 int order2, Intersections& i) {
52 if (order1 == 3 && order2 == 3) {
53 intersect2(simple1, simple2, i);
54 } else if (order1 <= 2 && order2 <= 2) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000055 intersect((const _Line&) simple1, (const _Line&) simple2, i);
caryclark@google.comf9502d72013-02-04 14:06:49 +000056 } else if (order1 == 3 && order2 <= 2) {
57 intersect(simple1, (const _Line&) simple2, i);
58 } else {
59 SkASSERT(order1 <= 2 && order2 == 3);
60 intersect(simple2, (const _Line&) simple1, i);
61 for (int s = 0; s < i.fUsed; ++s) {
62 SkTSwap(i.fT[0][s], i.fT[1][s]);
63 }
64 }
65}
66
caryclark@google.combeda3892013-02-07 13:13:41 +000067static double distanceFromEnd(double t) {
68 return t > 0.5 ? 1 - t : t;
69}
70
71// OPTIMIZATION: this used to try to guess the value for delta, and that may still be worthwhile
72static void bumpForRetry(double t1, double t2, double& s1, double& e1, double& s2, double& e2) {
73 double dt1 = distanceFromEnd(t1);
74 double dt2 = distanceFromEnd(t2);
75 double delta = 1.0 / precisionUnit;
76 if (dt1 < dt2) {
77 if (t1 == dt1) {
78 s1 = SkTMax(s1 - delta, 0.);
79 } else {
80 e1 = SkTMin(e1 + delta, 1.);
81 }
82 } else {
83 if (t2 == dt2) {
84 s2 = SkTMax(s2 - delta, 0.);
85 } else {
86 e2 = SkTMin(e2 + delta, 1.);
87 }
88 }
89}
90
91static bool doIntersect(const Cubic& cubic1, double t1s, double t1m, double t1e,
caryclark@google.comf9502d72013-02-04 14:06:49 +000092 const Cubic& cubic2, double t2s, double t2m, double t2e, Intersections& i) {
caryclark@google.combeda3892013-02-07 13:13:41 +000093 bool result = false;
caryclark@google.comf9502d72013-02-04 14:06:49 +000094 i.upDepth();
95 // divide the quadratics at the new t value and try again
96 double p1s = t1s;
97 double p1e = t1m;
98 for (int p1 = 0; p1 < 2; ++p1) {
99 Quadratic s1a;
100 int o1a = quadPart(cubic1, p1s, p1e, s1a);
101 double p2s = t2s;
102 double p2e = t2m;
103 for (int p2 = 0; p2 < 2; ++p2) {
104 Quadratic s2a;
105 int o2a = quadPart(cubic2, p2s, p2e, s2a);
106 Intersections locals;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000107 #if ONE_OFF_DEBUG
108 if (tLimits[0][0] >= p1s && tLimits[0][1] <= p1e
109 && tLimits[1][0] >= p2s && tLimits[1][1] <= p2e) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000110 SkDebugf("t1=(%1.9g,%1.9g) o1=%d t2=(%1.9g,%1.9g) o2=%d\n",
111 p1s, p1e, o1a, p2s, p2e, o2a);
112 if (o1a == 2) {
113 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
114 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y);
115 } else {
116 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
117 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y, s1a[2].x, s1a[2].y);
118 }
119 if (o2a == 2) {
120 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
121 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y);
122 } else {
123 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
124 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y, s2a[2].x, s2a[2].y);
125 }
126 Intersections xlocals;
127 intersectWithOrder(s1a, o1a, s2a, o2a, xlocals);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000128 SkDebugf("xlocals.fUsed=%d depth=%d\n", xlocals.used(), i.depth());
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000129 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000130 #endif
131 intersectWithOrder(s1a, o1a, s2a, o2a, locals);
132 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
133 double to1 = p1s + (p1e - p1s) * locals.fT[0][tIdx];
134 double to2 = p2s + (p2e - p2s) * locals.fT[1][tIdx];
135 // if the computed t is not sufficiently precise, iterate
136 _Point p1, p2;
137 xy_at_t(cubic1, to1, p1.x, p1.y);
138 xy_at_t(cubic2, to2, p2.x, p2.y);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000139 #if ONE_OFF_DEBUG
caryclark@google.comf9502d72013-02-04 14:06:49 +0000140 SkDebugf("to1=%1.9g p1=(%1.9g,%1.9g) to2=%1.9g p2=(%1.9g,%1.9g) d=%1.9g\n",
141 to1, p1.x, p1.y, to2, p2.x, p2.y, p1.distance(p2));
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000142
caryclark@google.comf9502d72013-02-04 14:06:49 +0000143 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000144 if (p1.approximatelyEqualHalf(p2)) {
145 i.insertSwap(to1, to2, p1);
caryclark@google.combeda3892013-02-07 13:13:41 +0000146 result = true;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000147 } else {
caryclark@google.combeda3892013-02-07 13:13:41 +0000148 result = doIntersect(cubic1, p1s, to1, p1e, cubic2, p2s, to2, p2e, i);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000149 if (!result && p1.approximatelyEqual(p2)) {
150 i.insertSwap(to1, to2, p1);
151 SkDebugf("!!!\n");
152 result = true;
153 } else
caryclark@google.combeda3892013-02-07 13:13:41 +0000154 // if both cubics curve in the same direction, the quadratic intersection
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000155 // may mark a range that does not contain the cubic intersection. If no
156 // intersection is found, look again including the t distance of the
caryclark@google.combeda3892013-02-07 13:13:41 +0000157 // of the quadratic intersection nearest a quadratic end (which in turn is
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000158 // nearest the actual cubic)
caryclark@google.combeda3892013-02-07 13:13:41 +0000159 if (!result) {
160 double b1s = p1s;
161 double b1e = p1e;
162 double b2s = p2s;
163 double b2e = p2e;
164 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
165 result = doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
166 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000167 }
168 }
169 p2s = p2e;
170 p2e = t2e;
171 }
172 p1s = p1e;
173 p1e = t1e;
174 }
175 i.downDepth();
caryclark@google.combeda3892013-02-07 13:13:41 +0000176 return result;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000177}
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000178
caryclark@google.com73ca6242013-01-17 21:02:47 +0000179// this flavor approximates the cubics with quads to find the intersecting ts
180// OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used
181// to create the approximations, could be stored in the cubic segment
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000182// FIXME: this strategy needs to intersect the convex hull on either end with the opposite to
183// account for inset quadratics that cause the endpoint intersection to avoid detection
184// the segments can be very short -- the length of the maximum quadratic error (precision)
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000185static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000186 double t2s, double t2e, double precisionScale, Intersections& i) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000187 Cubic c1, c2;
188 sub_divide(cubic1, t1s, t1e, c1);
189 sub_divide(cubic2, t2s, t2e, c2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000190 SkTDArray<double> ts1;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000191 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000192 SkTDArray<double> ts2;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000193 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000194 double t1Start = t1s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000195 int ts1Count = ts1.count();
196 for (int i1 = 0; i1 <= ts1Count; ++i1) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000197 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
198 const double t1 = t1s + (t1e - t1s) * tEnd1;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000199 Quadratic s1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000200 int o1 = quadPart(cubic1, t1Start, t1, s1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000201 double t2Start = t2s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000202 int ts2Count = ts2.count();
203 for (int i2 = 0; i2 <= ts2Count; ++i2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000204 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
205 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000206 Quadratic s2;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000207 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000208 #if ONE_OFF_DEBUG
209 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
210 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
caryclark@google.combeda3892013-02-07 13:13:41 +0000211 Cubic cSub1, cSub2;
212 sub_divide(cubic1, t1Start, tEnd1, cSub1);
213 sub_divide(cubic2, t2Start, tEnd2, cSub2);
214 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
215 t1Start, t1, t2Start, t2);
216 Intersections xlocals;
217 intersectWithOrder(s1, o1, s2, o2, xlocals);
218 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
219 }
220 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000221 Intersections locals;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000222 intersectWithOrder(s1, o1, s2, o2, locals);
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000223
caryclark@google.com73ca6242013-01-17 21:02:47 +0000224 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
225 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
226 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000227 // if the computed t is not sufficiently precise, iterate
228 _Point p1, p2;
229 xy_at_t(cubic1, to1, p1.x, p1.y);
230 xy_at_t(cubic2, to2, p2.x, p2.y);
231 if (p1.approximatelyEqual(p2)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000232 i.insert(to1, to2, p1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000233 } else {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000234 #if ONE_OFF_DEBUG
235 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
236 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
caryclark@google.combeda3892013-02-07 13:13:41 +0000237 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
238 t1Start, t1, t2Start, t2);
239 }
240 #endif
241 bool found = doIntersect(cubic1, t1Start, to1, t1, cubic2, t2Start, to2, t2, i);
242 if (!found) {
243 double b1s = t1Start;
244 double b1e = t1;
245 double b2s = t2Start;
246 double b2e = t2;
247 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
248 doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
249 }
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000250 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000251 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000252 int coincidentCount = locals.coincidentUsed();
253 if (coincidentCount) {
254 // FIXME: one day, we'll probably need to allow coincident + non-coincident pts
255 SkASSERT(coincidentCount == locals.used());
256 SkASSERT(coincidentCount == 2);
caryclark@google.combeda3892013-02-07 13:13:41 +0000257 double coTs[2][2];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000258 for (int tIdx = 0; tIdx < coincidentCount; ++tIdx) {
259 if (locals.fIsCoincident[0] & (1 << tIdx)) {
260 coTs[0][tIdx] = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
261 }
262 if (locals.fIsCoincident[1] & (1 << tIdx)) {
263 coTs[1][tIdx] = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
264 }
caryclark@google.combeda3892013-02-07 13:13:41 +0000265 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000266 i.insertCoincidentPair(coTs[0][0], coTs[0][1], coTs[1][0], coTs[1][1],
267 locals.fPt[0], locals.fPt[1]);
caryclark@google.combeda3892013-02-07 13:13:41 +0000268 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000269 t2Start = t2;
270 }
271 t1Start = t1;
272 }
273 return i.intersected();
274}
275
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000276static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
277 Intersections& i) {
278 _Line line1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000279 line1[1] = cubic1[start ? 0 : 3];
280 if (line1[1].approximatelyEqual(cubic2[0]) || line1[1].approximatelyEqual(cubic2[3])) {
281 return false;
282 }
283 line1[0] = line1[1];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000284 _Point dxy1 = line1[0] - cubic1[start ? 1 : 2];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000285 if (dxy1.approximatelyZero()) {
286 dxy1 = line1[0] - cubic1[start ? 2 : 1];
287 }
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000288 dxy1 /= precisionUnit;
289 line1[1] += dxy1;
290 _Rect line1Bounds;
291 line1Bounds.setBounds(line1);
292 if (!bounds2.intersects(line1Bounds)) {
293 return false;
294 }
295 _Line line2;
296 line2[0] = line2[1] = line1[0];
297 _Point dxy2 = line2[0] - cubic1[start ? 3 : 0];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000298 SkASSERT(!dxy2.approximatelyZero());
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000299 dxy2 /= precisionUnit;
300 line2[1] += dxy2;
301#if 0 // this is so close to the first bounds test it isn't worth the short circuit test
302 _Rect line2Bounds;
303 line2Bounds.setBounds(line2);
304 if (!bounds2.intersects(line2Bounds)) {
305 return false;
306 }
307#endif
308 Intersections local1;
309 if (!intersect(cubic2, line1, local1)) {
310 return false;
311 }
312 Intersections local2;
313 if (!intersect(cubic2, line2, local2)) {
314 return false;
315 }
316 double tMin, tMax;
317 tMin = tMax = local1.fT[0][0];
318 for (int index = 1; index < local1.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000319 tMin = SkTMin(tMin, local1.fT[0][index]);
320 tMax = SkTMax(tMax, local1.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000321 }
322 for (int index = 1; index < local2.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000323 tMin = SkTMin(tMin, local2.fT[0][index]);
324 tMax = SkTMax(tMax, local2.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000325 }
326 return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000327 cubic2, tMin, tMax, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000328}
329
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000330// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
331// chase intersections near quadratic ends, requiring odd hacks to find them.
332static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
333 double t2s, double t2e, double precisionScale, Intersections& i) {
334 i.upDepth();
335 bool result = false;
336 Cubic c1, c2;
337 sub_divide(cubic1, t1s, t1e, c1);
338 sub_divide(cubic2, t2s, t2e, c2);
339 SkTDArray<double> ts1;
340 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
341 SkTDArray<double> ts2;
342 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
343 double t1Start = t1s;
344 int ts1Count = ts1.count();
345 for (int i1 = 0; i1 <= ts1Count; ++i1) {
346 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
347 const double t1 = t1s + (t1e - t1s) * tEnd1;
348 Quadratic s1;
349 int o1 = quadPart(cubic1, t1Start, t1, s1);
350 double t2Start = t2s;
351 int ts2Count = ts2.count();
352 for (int i2 = 0; i2 <= ts2Count; ++i2) {
353 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
354 const double t2 = t2s + (t2e - t2s) * tEnd2;
355 Quadratic s2;
356 int o2 = quadPart(cubic2, t2Start, t2, s2);
357 Intersections locals;
358 intersectWithOrder(s1, o1, s2, o2, locals);
359 double coStart[2] = { -1 };
360 _Point coPoint;
361 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
362 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
363 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
364 // if the computed t is not sufficiently precise, iterate
365 _Point p1, p2;
366 xy_at_t(cubic1, to1, p1.x, p1.y);
367 xy_at_t(cubic2, to2, p2.x, p2.y);
368 if (p1.approximatelyEqual(p2)) {
369 if (locals.fIsCoincident[0] & 1 << tIdx) {
370 if (coStart[0] < 0) {
371 coStart[0] = to1;
372 coStart[1] = to2;
373 coPoint = p1;
374 } else {
375 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
376 coStart[0] = -1;
377 }
378 } else {
379 i.insert(to1, to2, p1);
380 }
381 result = true;
382 } else {
383 double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine
384 double c1Min = SkTMax(0., to1 - offset);
385 double c1Max = SkTMin(1., to1 + offset);
386 double c2Min = SkTMax(0., to2 - offset);
387 double c2Max = SkTMin(1., to2 + offset);
388 bool found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
389 if (false && !found) {
390 // either offset was overagressive or cubics didn't really intersect
391 // if they didn't intersect, then quad tangents ought to be nearly parallel
392 offset = precisionScale / 2; // try much less agressive offset
393 c1Min = SkTMax(0., to1 - offset);
394 c1Max = SkTMin(1., to1 + offset);
395 c2Min = SkTMax(0., to2 - offset);
396 c2Max = SkTMin(1., to2 + offset);
397 found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
398 if (found) {
399 SkDebugf("%s *** over-aggressive? offset=%1.9g depth=%d\n", __FUNCTION__,
400 offset, i.depth());
401 }
402 // try parallel measure
403 _Point d1 = dxdy_at_t(cubic1, to1);
404 _Point d2 = dxdy_at_t(cubic2, to2);
405 double shallow = d1.cross(d2);
406 #if 1 || ONE_OFF_DEBUG // not sure this is worth debugging
407 if (!approximately_zero(shallow)) {
408 SkDebugf("%s *** near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
409 offset, i.depth());
410 }
411 #endif
412 if (i.depth() == 1 && shallow < 0.6) {
413 SkDebugf("%s !!! near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
414 offset, i.depth());
415 }
416 }
417 }
418 }
419 SkASSERT(coStart[0] == -1);
420 t2Start = t2;
421 }
422 t1Start = t1;
423 }
424 i.downDepth();
425 return result;
426}
427
caryclark@google.comf9502d72013-02-04 14:06:49 +0000428// FIXME: add intersection of convex hull on cubics' ends with the opposite cubic. The hull line
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000429// segments can be constructed to be only as long as the calculated precision suggests. If the hull
430// line segments intersect the cubic, then use the intersections to construct a subdivision for
431// quadratic curve fitting.
432bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000433 bool result = intersect2(c1, 0, 1, c2, 0, 1, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000434 // FIXME: pass in cached bounds from caller
435 _Rect c1Bounds, c2Bounds;
436 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
437 c2Bounds.setBounds(c2);
438 result |= intersectEnd(c1, false, c2, c2Bounds, i);
439 result |= intersectEnd(c1, true, c2, c2Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000440 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000441 result |= intersectEnd(c2, false, c1, c1Bounds, i);
442 result |= intersectEnd(c2, true, c1, c1Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000443 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000444 return result;
445}
446
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000447bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) {
448 bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i);
449 // FIXME: pass in cached bounds from caller
450 _Rect c1Bounds, c2Bounds;
451 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
452 c2Bounds.setBounds(c2);
453 result |= intersectEnd(c1, false, c2, c2Bounds, i);
454 result |= intersectEnd(c1, true, c2, c2Bounds, i);
455 i.swap();
456 result |= intersectEnd(c2, false, c1, c1Bounds, i);
457 result |= intersectEnd(c2, true, c1, c1Bounds, i);
458 i.swap();
459 return result;
460}
461
caryclark@google.com73ca6242013-01-17 21:02:47 +0000462int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
463 SkTDArray<double> ts;
464 double precision = calcPrecision(cubic);
465 cubic_to_quadratics(cubic, precision, ts);
466 double tStart = 0;
467 Cubic part;
468 int tsCount = ts.count();
469 for (int idx = 0; idx <= tsCount; ++idx) {
470 double t = idx < tsCount ? ts[idx] : 1;
471 Quadratic q1;
472 sub_divide(cubic, tStart, t, part);
473 demote_cubic_to_quad(part, q1);
474 Intersections locals;
475 intersect2(q1, quad, locals);
476 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
477 double globalT = tStart + (t - tStart) * locals.fT[0][tIdx];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000478 i.insert(globalT, locals.fT[1][tIdx], locals.fPt[tIdx]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000479 }
480 tStart = t;
481 }
482 return i.used();
483}
484
485bool intersect(const Cubic& cubic, Intersections& i) {
486 SkTDArray<double> ts;
487 double precision = calcPrecision(cubic);
488 cubic_to_quadratics(cubic, precision, ts);
489 int tsCount = ts.count();
490 if (tsCount == 1) {
491 return false;
492 }
493 double t1Start = 0;
494 Cubic part;
495 for (int idx = 0; idx < tsCount; ++idx) {
496 double t1 = ts[idx];
497 Quadratic q1;
498 sub_divide(cubic, t1Start, t1, part);
499 demote_cubic_to_quad(part, q1);
500 double t2Start = t1;
501 for (int i2 = idx + 1; i2 <= tsCount; ++i2) {
502 const double t2 = i2 < tsCount ? ts[i2] : 1;
503 Quadratic q2;
504 sub_divide(cubic, t2Start, t2, part);
505 demote_cubic_to_quad(part, q2);
506 Intersections locals;
507 intersect2(q1, q2, locals);
508 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
509 // discard intersections at cusp? (maximum curvature)
510 double t1sect = locals.fT[0][tIdx];
511 double t2sect = locals.fT[1][tIdx];
512 if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) {
513 continue;
514 }
515 double to1 = t1Start + (t1 - t1Start) * t1sect;
516 double to2 = t2Start + (t2 - t2Start) * t2sect;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000517 i.insert(to1, to2, locals.fPt[tIdx]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000518 }
519 t2Start = t2;
520 }
521 t1Start = t1;
522 }
523 return i.intersected();
524}