blob: bf8ea468c3dab173d01f411d12427116e7ce1f8f [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.com7ff5c842013-02-26 15:56:05 +000017static const double tLimits[2][2] = {{0.772784538, 0.77278492}, {0.999111748, 0.999112129}};
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);
caryclark@google.com7ff5c842013-02-26 15:56:05 +000077 double delta = 1.0 / gPrecisionUnit;
caryclark@google.combeda3892013-02-07 13:13:41 +000078 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
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000280// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000281// chase intersections near quadratic ends, requiring odd hacks to find them.
282static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
283 double t2s, double t2e, double precisionScale, Intersections& i) {
284 i.upDepth();
285 bool result = false;
286 Cubic c1, c2;
287 sub_divide(cubic1, t1s, t1e, c1);
288 sub_divide(cubic2, t2s, t2e, c2);
289 SkTDArray<double> ts1;
290 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
291 SkTDArray<double> ts2;
292 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
293 double t1Start = t1s;
294 int ts1Count = ts1.count();
295 for (int i1 = 0; i1 <= ts1Count; ++i1) {
296 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
297 const double t1 = t1s + (t1e - t1s) * tEnd1;
298 Quadratic s1;
299 int o1 = quadPart(cubic1, t1Start, t1, s1);
300 double t2Start = t2s;
301 int ts2Count = ts2.count();
302 for (int i2 = 0; i2 <= ts2Count; ++i2) {
303 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
304 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000305 if (cubic1 == cubic2 && t1Start >= t2Start) {
306 t2Start = t2;
307 continue;
308 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000309 Quadratic s2;
310 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000311 #if ONE_OFF_DEBUG
312 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
313 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
314 Cubic cSub1, cSub2;
315 sub_divide(cubic1, t1Start, tEnd1, cSub1);
316 sub_divide(cubic2, t2Start, tEnd2, cSub2);
317 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
318 t1Start, t1, t2Start, t2);
319 Intersections xlocals;
320 intersectWithOrder(s1, o1, s2, o2, xlocals);
321 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
322 }
323 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000324 Intersections locals;
325 intersectWithOrder(s1, o1, s2, o2, locals);
326 double coStart[2] = { -1 };
327 _Point coPoint;
328 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
329 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
330 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
331 // if the computed t is not sufficiently precise, iterate
332 _Point p1, p2;
333 xy_at_t(cubic1, to1, p1.x, p1.y);
334 xy_at_t(cubic2, to2, p2.x, p2.y);
335 if (p1.approximatelyEqual(p2)) {
336 if (locals.fIsCoincident[0] & 1 << tIdx) {
337 if (coStart[0] < 0) {
338 coStart[0] = to1;
339 coStart[1] = to2;
340 coPoint = p1;
341 } else {
342 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
343 coStart[0] = -1;
344 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000345 result = true;
346 } else if (cubic1 != cubic2 || !approximately_equal(to1, to2)) {
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000347 if (i.swapped()) { // FIXME: insert should respect swap
348 i.insert(to2, to1, p1);
349 } else {
350 i.insert(to1, to2, p1);
351 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000352 result = true;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000353 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000354 } else {
355 double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine
356 double c1Min = SkTMax(0., to1 - offset);
357 double c1Max = SkTMin(1., to1 + offset);
358 double c2Min = SkTMax(0., to2 - offset);
359 double c2Max = SkTMin(1., to2 + offset);
360 bool found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
361 if (false && !found) {
362 // either offset was overagressive or cubics didn't really intersect
363 // if they didn't intersect, then quad tangents ought to be nearly parallel
364 offset = precisionScale / 2; // try much less agressive offset
365 c1Min = SkTMax(0., to1 - offset);
366 c1Max = SkTMin(1., to1 + offset);
367 c2Min = SkTMax(0., to2 - offset);
368 c2Max = SkTMin(1., to2 + offset);
369 found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
370 if (found) {
371 SkDebugf("%s *** over-aggressive? offset=%1.9g depth=%d\n", __FUNCTION__,
372 offset, i.depth());
373 }
374 // try parallel measure
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000375 _Vector d1 = dxdy_at_t(cubic1, to1);
376 _Vector d2 = dxdy_at_t(cubic2, to2);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000377 double shallow = d1.cross(d2);
378 #if 1 || ONE_OFF_DEBUG // not sure this is worth debugging
379 if (!approximately_zero(shallow)) {
380 SkDebugf("%s *** near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
381 offset, i.depth());
382 }
383 #endif
384 if (i.depth() == 1 && shallow < 0.6) {
385 SkDebugf("%s !!! near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__,
386 offset, i.depth());
387 }
388 }
389 }
390 }
391 SkASSERT(coStart[0] == -1);
392 t2Start = t2;
393 }
394 t1Start = t1;
395 }
396 i.downDepth();
397 return result;
398}
399
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000400// intersect the end of the cubic with the other. Try lines from the end to control and opposite
401// end to determine range of t on opposite cubic.
402static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
403 Intersections& i) {
404 _Line line;
405 int t1Index = start ? 0 : 3;
406 line[0] = cubic1[t1Index];
407 // don't bother if the two cubics are connnected
408 if (line[0].approximatelyEqual(cubic2[0]) || line[0].approximatelyEqual(cubic2[3])) {
409 return false;
410 }
411 double tMin = 1, tMax = 0;
412 for (int index = 0; index < 4; ++index) {
413 if (index == t1Index) {
414 continue;
415 }
416 _Vector dxy1 = cubic1[index] - line[0];
417 dxy1 /= gPrecisionUnit;
418 line[1] = line[0] + dxy1;
419 _Rect lineBounds;
420 lineBounds.setBounds(line);
421 if (!bounds2.intersects(lineBounds)) {
422 continue;
423 }
424 Intersections local;
425 if (!intersect(cubic2, line, local)) {
426 continue;
427 }
428 for (int index = 0; index < local.fUsed; ++index) {
429 tMin = SkTMin(tMin, local.fT[0][index]);
430 tMax = SkTMax(tMax, local.fT[0][index]);
431 }
432 }
433 if (tMin > tMax) {
434 return false;
435 }
436 double tMin1 = start ? 0 : 1 - 1.0 / gPrecisionUnit;
437 double tMax1 = start ? 1.0 / gPrecisionUnit : 1;
438 double tMin2 = SkTMax(tMin - 1.0 / gPrecisionUnit, 0.0);
439 double tMax2 = SkTMin(tMax + 1.0 / gPrecisionUnit, 1.0);
440 return intersect3(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
441}
442
caryclark@google.comf9502d72013-02-04 14:06:49 +0000443// FIXME: add intersection of convex hull on cubics' ends with the opposite cubic. The hull line
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000444// segments can be constructed to be only as long as the calculated precision suggests. If the hull
445// line segments intersect the cubic, then use the intersections to construct a subdivision for
446// quadratic curve fitting.
447bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000448 bool result = intersect2(c1, 0, 1, c2, 0, 1, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000449 // 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);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000455 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000456 result |= intersectEnd(c2, false, c1, c1Bounds, i);
457 result |= intersectEnd(c2, true, c1, c1Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000458 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000459 return result;
460}
461
caryclark@google.com47d73da2013-02-17 01:41:25 +0000462const double CLOSE_ENOUGH = 0.001;
skia.committer@gmail.come7707c22013-02-17 07:02:20 +0000463
caryclark@google.com47d73da2013-02-17 01:41:25 +0000464static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
465 if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) {
466 return false;
467 }
468 pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2);
469 return true;
470}
471
472static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
473 int last = i.used() - 1;
474 if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
475 return false;
476 }
477 pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2);
478 return true;
479}
480
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000481bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) {
482 bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i);
483 // FIXME: pass in cached bounds from caller
484 _Rect c1Bounds, c2Bounds;
485 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
486 c2Bounds.setBounds(c2);
487 result |= intersectEnd(c1, false, c2, c2Bounds, i);
488 result |= intersectEnd(c1, true, c2, c2Bounds, i);
489 i.swap();
490 result |= intersectEnd(c2, false, c1, c1Bounds, i);
491 result |= intersectEnd(c2, true, c1, c1Bounds, i);
492 i.swap();
caryclark@google.com47d73da2013-02-17 01:41:25 +0000493 // If an end point and a second point very close to the end is returned, the second
494 // point may have been detected because the approximate quads
495 // intersected at the end and close to it. Verify that the second point is valid.
496 if (i.used() <= 1 || i.coincidentUsed()) {
497 return result;
498 }
499 _Point pt[2];
500 if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1])
501 && pt[0].approximatelyEqual(pt[1])) {
502 i.removeOne(1);
503 }
504 if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1])
505 && pt[0].approximatelyEqual(pt[1])) {
506 i.removeOne(i.used() - 2);
507 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000508 return result;
509}
510
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000511// Up promote the quad to a cubic.
512// OPTIMIZATION If this is a common use case, optimize by duplicating
513// the intersect 3 loop to avoid the promotion / demotion code
caryclark@google.com73ca6242013-01-17 21:02:47 +0000514int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000515 Cubic up;
516 toCubic(quad, up);
517 (void) intersect3(cubic, up, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000518 return i.used();
519}
520
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000521/* http://www.ag.jku.at/compass/compasssample.pdf
522( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen
523Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no
524SINTEF Applied Mathematics http://www.sintef.no )
525describes a method to find the self intersection of a cubic by taking the gradient of the implicit
526form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/
527
528int intersect(const Cubic& c, Intersections& i) {
529 // check to see if x or y end points are the extrema. Are other quick rejects possible?
530 if ((between(c[0].x, c[1].x, c[3].x) && between(c[0].x, c[2].x, c[3].x))
531 || (between(c[0].y, c[1].y, c[3].y) && between(c[0].y, c[2].y, c[3].y))) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000532 return false;
533 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000534 (void) intersect3(c, c, i);
535 return i.used();
caryclark@google.com73ca6242013-01-17 21:02:47 +0000536}