blob: fe146714a9eef81063bbbc98d5d7f098f0673791 [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.com5e0500f2013-02-20 12:51:37 +000017static const double tLimits[2][2] = {{0.599274754, 0.599275135}, {0.599274754, 0.599275135}};
caryclark@google.com45a8fc62013-02-14 15:29:11 +000018#endif
19
caryclark@google.combeda3892013-02-07 13:13:41 +000020#define DEBUG_QUAD_PART 0
caryclark@google.com47d73da2013-02-17 01:41:25 +000021#define SWAP_TOP_DEBUG 0
caryclark@google.comf9502d72013-02-04 14:06:49 +000022
caryclark@google.comf9502d72013-02-04 14:06:49 +000023static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) {
24 Cubic part;
25 sub_divide(cubic, tStart, tEnd, part);
26 Quadratic quad;
27 demote_cubic_to_quad(part, quad);
28 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
29 // extremely shallow quadratic?
caryclark@google.com47d73da2013-02-17 01:41:25 +000030 int order = reduceOrder(quad, simple, kReduceOrder_TreatAsFill);
caryclark@google.combeda3892013-02-07 13:13:41 +000031#if DEBUG_QUAD_PART
32 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",
33 __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y,
34 cubic[3].x, cubic[3].y, tStart, tEnd);
35 SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)"
36 " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, part[0].x, part[0].y,
37 part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y,
38 quad[1].x, quad[1].y, quad[2].x, quad[2].y);
39 SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y);
40 if (order > 1) {
41 SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y);
42 }
43 if (order > 2) {
44 SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y);
45 }
46 SkDebugf(")\n");
47 SkASSERT(order < 4 && order > 0);
48#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +000049 return order;
50}
51
52static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2,
53 int order2, Intersections& i) {
54 if (order1 == 3 && order2 == 3) {
55 intersect2(simple1, simple2, i);
56 } else if (order1 <= 2 && order2 <= 2) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000057 intersect((const _Line&) simple1, (const _Line&) simple2, i);
caryclark@google.comf9502d72013-02-04 14:06:49 +000058 } else if (order1 == 3 && order2 <= 2) {
59 intersect(simple1, (const _Line&) simple2, i);
60 } else {
61 SkASSERT(order1 <= 2 && order2 == 3);
62 intersect(simple2, (const _Line&) simple1, i);
63 for (int s = 0; s < i.fUsed; ++s) {
64 SkTSwap(i.fT[0][s], i.fT[1][s]);
65 }
66 }
67}
68
caryclark@google.combeda3892013-02-07 13:13:41 +000069static double distanceFromEnd(double t) {
70 return t > 0.5 ? 1 - t : t;
71}
72
73// OPTIMIZATION: this used to try to guess the value for delta, and that may still be worthwhile
74static void bumpForRetry(double t1, double t2, double& s1, double& e1, double& s2, double& e2) {
75 double dt1 = distanceFromEnd(t1);
76 double dt2 = distanceFromEnd(t2);
77 double delta = 1.0 / precisionUnit;
78 if (dt1 < dt2) {
79 if (t1 == dt1) {
80 s1 = SkTMax(s1 - delta, 0.);
81 } else {
82 e1 = SkTMin(e1 + delta, 1.);
83 }
84 } else {
85 if (t2 == dt2) {
86 s2 = SkTMax(s2 - delta, 0.);
87 } else {
88 e2 = SkTMin(e2 + delta, 1.);
89 }
90 }
91}
92
93static bool doIntersect(const Cubic& cubic1, double t1s, double t1m, double t1e,
caryclark@google.comf9502d72013-02-04 14:06:49 +000094 const Cubic& cubic2, double t2s, double t2m, double t2e, Intersections& i) {
caryclark@google.combeda3892013-02-07 13:13:41 +000095 bool result = false;
caryclark@google.comf9502d72013-02-04 14:06:49 +000096 i.upDepth();
97 // divide the quadratics at the new t value and try again
98 double p1s = t1s;
99 double p1e = t1m;
100 for (int p1 = 0; p1 < 2; ++p1) {
101 Quadratic s1a;
102 int o1a = quadPart(cubic1, p1s, p1e, s1a);
103 double p2s = t2s;
104 double p2e = t2m;
105 for (int p2 = 0; p2 < 2; ++p2) {
106 Quadratic s2a;
107 int o2a = quadPart(cubic2, p2s, p2e, s2a);
108 Intersections locals;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000109 #if ONE_OFF_DEBUG
110 if (tLimits[0][0] >= p1s && tLimits[0][1] <= p1e
111 && tLimits[1][0] >= p2s && tLimits[1][1] <= p2e) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000112 SkDebugf("t1=(%1.9g,%1.9g) o1=%d t2=(%1.9g,%1.9g) o2=%d\n",
113 p1s, p1e, o1a, p2s, p2e, o2a);
114 if (o1a == 2) {
115 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
116 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y);
117 } else {
118 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
119 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y, s1a[2].x, s1a[2].y);
120 }
121 if (o2a == 2) {
122 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
123 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y);
124 } else {
125 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
126 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y, s2a[2].x, s2a[2].y);
127 }
128 Intersections xlocals;
129 intersectWithOrder(s1a, o1a, s2a, o2a, xlocals);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000130 SkDebugf("xlocals.fUsed=%d depth=%d\n", xlocals.used(), i.depth());
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000131 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000132 #endif
133 intersectWithOrder(s1a, o1a, s2a, o2a, locals);
134 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
135 double to1 = p1s + (p1e - p1s) * locals.fT[0][tIdx];
136 double to2 = p2s + (p2e - p2s) * locals.fT[1][tIdx];
137 // if the computed t is not sufficiently precise, iterate
138 _Point p1, p2;
139 xy_at_t(cubic1, to1, p1.x, p1.y);
140 xy_at_t(cubic2, to2, p2.x, p2.y);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000141 #if ONE_OFF_DEBUG
caryclark@google.comf9502d72013-02-04 14:06:49 +0000142 SkDebugf("to1=%1.9g p1=(%1.9g,%1.9g) to2=%1.9g p2=(%1.9g,%1.9g) d=%1.9g\n",
143 to1, p1.x, p1.y, to2, p2.x, p2.y, p1.distance(p2));
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000144
caryclark@google.comf9502d72013-02-04 14:06:49 +0000145 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000146 if (p1.approximatelyEqualHalf(p2)) {
147 i.insertSwap(to1, to2, p1);
caryclark@google.combeda3892013-02-07 13:13:41 +0000148 result = true;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000149 } else {
caryclark@google.combeda3892013-02-07 13:13:41 +0000150 result = doIntersect(cubic1, p1s, to1, p1e, cubic2, p2s, to2, p2e, i);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000151 if (!result && p1.approximatelyEqual(p2)) {
152 i.insertSwap(to1, to2, p1);
caryclark@google.com47d73da2013-02-17 01:41:25 +0000153 #if SWAP_TOP_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000154 SkDebugf("!!!\n");
caryclark@google.com47d73da2013-02-17 01:41:25 +0000155 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000156 result = true;
157 } else
caryclark@google.combeda3892013-02-07 13:13:41 +0000158 // if both cubics curve in the same direction, the quadratic intersection
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000159 // may mark a range that does not contain the cubic intersection. If no
160 // intersection is found, look again including the t distance of the
caryclark@google.combeda3892013-02-07 13:13:41 +0000161 // of the quadratic intersection nearest a quadratic end (which in turn is
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000162 // nearest the actual cubic)
caryclark@google.combeda3892013-02-07 13:13:41 +0000163 if (!result) {
164 double b1s = p1s;
165 double b1e = p1e;
166 double b2s = p2s;
167 double b2e = p2e;
168 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
169 result = doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
170 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000171 }
172 }
173 p2s = p2e;
174 p2e = t2e;
175 }
176 p1s = p1e;
177 p1e = t1e;
178 }
179 i.downDepth();
caryclark@google.combeda3892013-02-07 13:13:41 +0000180 return result;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000181}
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000182
caryclark@google.com73ca6242013-01-17 21:02:47 +0000183// this flavor approximates the cubics with quads to find the intersecting ts
184// OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used
185// to create the approximations, could be stored in the cubic segment
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000186// FIXME: this strategy needs to intersect the convex hull on either end with the opposite to
187// account for inset quadratics that cause the endpoint intersection to avoid detection
188// the segments can be very short -- the length of the maximum quadratic error (precision)
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000189static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000190 double t2s, double t2e, double precisionScale, Intersections& i) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000191 Cubic c1, c2;
192 sub_divide(cubic1, t1s, t1e, c1);
193 sub_divide(cubic2, t2s, t2e, c2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000194 SkTDArray<double> ts1;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000195 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000196 SkTDArray<double> ts2;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000197 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000198 double t1Start = t1s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000199 int ts1Count = ts1.count();
200 for (int i1 = 0; i1 <= ts1Count; ++i1) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000201 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
202 const double t1 = t1s + (t1e - t1s) * tEnd1;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000203 Quadratic s1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000204 int o1 = quadPart(cubic1, t1Start, t1, s1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000205 double t2Start = t2s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000206 int ts2Count = ts2.count();
207 for (int i2 = 0; i2 <= ts2Count; ++i2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000208 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
209 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000210 Quadratic s2;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000211 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000212 #if ONE_OFF_DEBUG
213 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
214 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
caryclark@google.combeda3892013-02-07 13:13:41 +0000215 Cubic cSub1, cSub2;
216 sub_divide(cubic1, t1Start, tEnd1, cSub1);
217 sub_divide(cubic2, t2Start, tEnd2, cSub2);
218 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
219 t1Start, t1, t2Start, t2);
220 Intersections xlocals;
221 intersectWithOrder(s1, o1, s2, o2, xlocals);
222 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
223 }
224 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000225 Intersections locals;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000226 intersectWithOrder(s1, o1, s2, o2, locals);
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000227
caryclark@google.com73ca6242013-01-17 21:02:47 +0000228 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
229 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
230 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000231 // if the computed t is not sufficiently precise, iterate
232 _Point p1, p2;
233 xy_at_t(cubic1, to1, p1.x, p1.y);
234 xy_at_t(cubic2, to2, p2.x, p2.y);
235 if (p1.approximatelyEqual(p2)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000236 i.insert(to1, to2, p1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000237 } else {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000238 #if ONE_OFF_DEBUG
239 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
240 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
caryclark@google.combeda3892013-02-07 13:13:41 +0000241 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
242 t1Start, t1, t2Start, t2);
243 }
244 #endif
245 bool found = doIntersect(cubic1, t1Start, to1, t1, cubic2, t2Start, to2, t2, i);
246 if (!found) {
247 double b1s = t1Start;
248 double b1e = t1;
249 double b2s = t2Start;
250 double b2e = t2;
251 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
252 doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
253 }
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000254 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000255 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000256 int coincidentCount = locals.coincidentUsed();
257 if (coincidentCount) {
258 // FIXME: one day, we'll probably need to allow coincident + non-coincident pts
259 SkASSERT(coincidentCount == locals.used());
260 SkASSERT(coincidentCount == 2);
caryclark@google.combeda3892013-02-07 13:13:41 +0000261 double coTs[2][2];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000262 for (int tIdx = 0; tIdx < coincidentCount; ++tIdx) {
263 if (locals.fIsCoincident[0] & (1 << tIdx)) {
264 coTs[0][tIdx] = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
265 }
266 if (locals.fIsCoincident[1] & (1 << tIdx)) {
267 coTs[1][tIdx] = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
268 }
caryclark@google.combeda3892013-02-07 13:13:41 +0000269 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000270 i.insertCoincidentPair(coTs[0][0], coTs[0][1], coTs[1][0], coTs[1][1],
271 locals.fPt[0], locals.fPt[1]);
caryclark@google.combeda3892013-02-07 13:13:41 +0000272 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000273 t2Start = t2;
274 }
275 t1Start = t1;
276 }
277 return i.intersected();
278}
279
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000280static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
281 Intersections& i) {
282 _Line line1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000283 line1[1] = cubic1[start ? 0 : 3];
284 if (line1[1].approximatelyEqual(cubic2[0]) || line1[1].approximatelyEqual(cubic2[3])) {
285 return false;
286 }
287 line1[0] = line1[1];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000288 _Point dxy1 = line1[0] - cubic1[start ? 1 : 2];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000289 if (dxy1.approximatelyZero()) {
290 dxy1 = line1[0] - cubic1[start ? 2 : 1];
291 }
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000292 dxy1 /= precisionUnit;
293 line1[1] += dxy1;
294 _Rect line1Bounds;
295 line1Bounds.setBounds(line1);
296 if (!bounds2.intersects(line1Bounds)) {
297 return false;
298 }
299 _Line line2;
300 line2[0] = line2[1] = line1[0];
301 _Point dxy2 = line2[0] - cubic1[start ? 3 : 0];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000302 SkASSERT(!dxy2.approximatelyZero());
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000303 dxy2 /= precisionUnit;
304 line2[1] += dxy2;
305#if 0 // this is so close to the first bounds test it isn't worth the short circuit test
306 _Rect line2Bounds;
307 line2Bounds.setBounds(line2);
308 if (!bounds2.intersects(line2Bounds)) {
309 return false;
310 }
311#endif
312 Intersections local1;
313 if (!intersect(cubic2, line1, local1)) {
314 return false;
315 }
316 Intersections local2;
317 if (!intersect(cubic2, line2, local2)) {
318 return false;
319 }
320 double tMin, tMax;
321 tMin = tMax = local1.fT[0][0];
322 for (int index = 1; index < local1.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000323 tMin = SkTMin(tMin, local1.fT[0][index]);
324 tMax = SkTMax(tMax, local1.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000325 }
326 for (int index = 1; index < local2.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000327 tMin = SkTMin(tMin, local2.fT[0][index]);
328 tMax = SkTMax(tMax, local2.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000329 }
330 return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000331 cubic2, tMin, tMax, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000332}
333
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000334// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000335// chase intersections near quadratic ends, requiring odd hacks to find them.
336static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
337 double t2s, double t2e, double precisionScale, Intersections& i) {
338 i.upDepth();
339 bool result = false;
340 Cubic c1, c2;
341 sub_divide(cubic1, t1s, t1e, c1);
342 sub_divide(cubic2, t2s, t2e, c2);
343 SkTDArray<double> ts1;
344 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
345 SkTDArray<double> ts2;
346 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
347 double t1Start = t1s;
348 int ts1Count = ts1.count();
349 for (int i1 = 0; i1 <= ts1Count; ++i1) {
350 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
351 const double t1 = t1s + (t1e - t1s) * tEnd1;
352 Quadratic s1;
353 int o1 = quadPart(cubic1, t1Start, t1, s1);
354 double t2Start = t2s;
355 int ts2Count = ts2.count();
356 for (int i2 = 0; i2 <= ts2Count; ++i2) {
357 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
358 const double t2 = t2s + (t2e - t2s) * tEnd2;
359 Quadratic s2;
360 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000361 #if ONE_OFF_DEBUG
362 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
363 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
364 Cubic cSub1, cSub2;
365 sub_divide(cubic1, t1Start, tEnd1, cSub1);
366 sub_divide(cubic2, t2Start, tEnd2, cSub2);
367 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
368 t1Start, t1, t2Start, t2);
369 Intersections xlocals;
370 intersectWithOrder(s1, o1, s2, o2, xlocals);
371 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
372 }
373 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000374 Intersections locals;
375 intersectWithOrder(s1, o1, s2, o2, locals);
376 double coStart[2] = { -1 };
377 _Point coPoint;
378 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
379 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
380 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
381 // if the computed t is not sufficiently precise, iterate
382 _Point p1, p2;
383 xy_at_t(cubic1, to1, p1.x, p1.y);
384 xy_at_t(cubic2, to2, p2.x, p2.y);
385 if (p1.approximatelyEqual(p2)) {
386 if (locals.fIsCoincident[0] & 1 << tIdx) {
387 if (coStart[0] < 0) {
388 coStart[0] = to1;
389 coStart[1] = to2;
390 coPoint = p1;
391 } else {
392 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
393 coStart[0] = -1;
394 }
395 } else {
396 i.insert(to1, to2, p1);
397 }
398 result = true;
399 } else {
400 double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine
401 double c1Min = SkTMax(0., to1 - offset);
402 double c1Max = SkTMin(1., to1 + offset);
403 double c2Min = SkTMax(0., to2 - offset);
404 double c2Max = SkTMin(1., to2 + offset);
405 bool found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
406 if (false && !found) {
407 // either offset was overagressive or cubics didn't really intersect
408 // if they didn't intersect, then quad tangents ought to be nearly parallel
409 offset = precisionScale / 2; // try much less agressive offset
410 c1Min = SkTMax(0., to1 - offset);
411 c1Max = SkTMin(1., to1 + offset);
412 c2Min = SkTMax(0., to2 - offset);
413 c2Max = SkTMin(1., to2 + offset);
414 found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
415 if (found) {
416 SkDebugf("%s *** over-aggressive? offset=%1.9g depth=%d\n", __FUNCTION__,
417 offset, i.depth());
418 }
419 // try parallel measure
420 _Point d1 = dxdy_at_t(cubic1, to1);
421 _Point d2 = dxdy_at_t(cubic2, to2);
422 double shallow = d1.cross(d2);
423 #if 1 || ONE_OFF_DEBUG // not sure this is worth debugging
424 if (!approximately_zero(shallow)) {
425 SkDebugf("%s *** near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
426 offset, i.depth());
427 }
428 #endif
429 if (i.depth() == 1 && shallow < 0.6) {
430 SkDebugf("%s !!! near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
431 offset, i.depth());
432 }
433 }
434 }
435 }
436 SkASSERT(coStart[0] == -1);
437 t2Start = t2;
438 }
439 t1Start = t1;
440 }
441 i.downDepth();
442 return result;
443}
444
caryclark@google.comf9502d72013-02-04 14:06:49 +0000445// FIXME: add intersection of convex hull on cubics' ends with the opposite cubic. The hull line
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000446// segments can be constructed to be only as long as the calculated precision suggests. If the hull
447// line segments intersect the cubic, then use the intersections to construct a subdivision for
448// quadratic curve fitting.
449bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000450 bool result = intersect2(c1, 0, 1, c2, 0, 1, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000451 // FIXME: pass in cached bounds from caller
452 _Rect c1Bounds, c2Bounds;
453 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
454 c2Bounds.setBounds(c2);
455 result |= intersectEnd(c1, false, c2, c2Bounds, i);
456 result |= intersectEnd(c1, true, c2, c2Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000457 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000458 result |= intersectEnd(c2, false, c1, c1Bounds, i);
459 result |= intersectEnd(c2, true, c1, c1Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000460 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000461 return result;
462}
463
caryclark@google.com47d73da2013-02-17 01:41:25 +0000464const double CLOSE_ENOUGH = 0.001;
skia.committer@gmail.come7707c22013-02-17 07:02:20 +0000465
caryclark@google.com47d73da2013-02-17 01:41:25 +0000466static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
467 if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) {
468 return false;
469 }
470 pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2);
471 return true;
472}
473
474static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
475 int last = i.used() - 1;
476 if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
477 return false;
478 }
479 pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2);
480 return true;
481}
482
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000483bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) {
484 bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i);
485 // FIXME: pass in cached bounds from caller
486 _Rect c1Bounds, c2Bounds;
487 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
488 c2Bounds.setBounds(c2);
489 result |= intersectEnd(c1, false, c2, c2Bounds, i);
490 result |= intersectEnd(c1, true, c2, c2Bounds, i);
491 i.swap();
492 result |= intersectEnd(c2, false, c1, c1Bounds, i);
493 result |= intersectEnd(c2, true, c1, c1Bounds, i);
494 i.swap();
caryclark@google.com47d73da2013-02-17 01:41:25 +0000495 // If an end point and a second point very close to the end is returned, the second
496 // point may have been detected because the approximate quads
497 // intersected at the end and close to it. Verify that the second point is valid.
498 if (i.used() <= 1 || i.coincidentUsed()) {
499 return result;
500 }
501 _Point pt[2];
502 if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1])
503 && pt[0].approximatelyEqual(pt[1])) {
504 i.removeOne(1);
505 }
506 if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1])
507 && pt[0].approximatelyEqual(pt[1])) {
508 i.removeOne(i.used() - 2);
509 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000510 return result;
511}
512
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000513// Up promote the quad to a cubic.
514// OPTIMIZATION If this is a common use case, optimize by duplicating
515// the intersect 3 loop to avoid the promotion / demotion code
caryclark@google.com73ca6242013-01-17 21:02:47 +0000516int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000517 Cubic up;
518 toCubic(quad, up);
519 (void) intersect3(cubic, up, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000520 return i.used();
521}
522
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000523// FIXME: this needs to be recursive like intersect 3
caryclark@google.com73ca6242013-01-17 21:02:47 +0000524bool intersect(const Cubic& cubic, Intersections& i) {
525 SkTDArray<double> ts;
526 double precision = calcPrecision(cubic);
527 cubic_to_quadratics(cubic, precision, ts);
528 int tsCount = ts.count();
529 if (tsCount == 1) {
530 return false;
531 }
532 double t1Start = 0;
533 Cubic part;
534 for (int idx = 0; idx < tsCount; ++idx) {
535 double t1 = ts[idx];
536 Quadratic q1;
537 sub_divide(cubic, t1Start, t1, part);
538 demote_cubic_to_quad(part, q1);
539 double t2Start = t1;
540 for (int i2 = idx + 1; i2 <= tsCount; ++i2) {
541 const double t2 = i2 < tsCount ? ts[i2] : 1;
542 Quadratic q2;
543 sub_divide(cubic, t2Start, t2, part);
544 demote_cubic_to_quad(part, q2);
545 Intersections locals;
546 intersect2(q1, q2, locals);
547 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
548 // discard intersections at cusp? (maximum curvature)
549 double t1sect = locals.fT[0][tIdx];
550 double t2sect = locals.fT[1][tIdx];
551 if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) {
552 continue;
553 }
554 double to1 = t1Start + (t1 - t1Start) * t1sect;
555 double to2 = t2Start + (t2 - t2Start) * t2sect;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000556 i.insert(to1, to2, locals.fPt[tIdx]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000557 }
558 t2Start = t2;
559 }
560 t1Start = t1;
561 }
562 return i.intersected();
563}