blob: 6e88effa180ff32880260f1a57ecdcf593dd0ea4 [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.comf9502d72013-02-04 14:06:49 +000015#define DEBUG_COMPUTE_DELTA 1
16#define COMPUTE_DELTA 0
caryclark@google.combeda3892013-02-07 13:13:41 +000017#define DEBUG_QUAD_PART 0
caryclark@google.comf9502d72013-02-04 14:06:49 +000018
caryclark@google.com0d3d09e2012-12-10 14:50:04 +000019static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections
20
caryclark@google.comc6825902012-02-03 22:07:47 +000021class CubicIntersections : public Intersections {
22public:
23
rmistry@google.comd6176b02012-08-23 18:14:13 +000024CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i)
caryclark@google.comc6825902012-02-03 22:07:47 +000025 : cubic1(c1)
26 , cubic2(c2)
27 , intersections(i)
rmistry@google.comd6176b02012-08-23 18:14:13 +000028 , depth(0)
caryclark@google.comc6825902012-02-03 22:07:47 +000029 , splits(0) {
caryclark@google.com639df892012-01-10 21:46:10 +000030}
31
caryclark@google.comc6825902012-02-03 22:07:47 +000032bool intersect() {
caryclark@google.com639df892012-01-10 21:46:10 +000033 double minT1, minT2, maxT1, maxT2;
34 if (!bezier_clip(cubic2, cubic1, minT1, maxT1)) {
35 return false;
36 }
37 if (!bezier_clip(cubic1, cubic2, minT2, maxT2)) {
38 return false;
39 }
caryclark@google.comc6825902012-02-03 22:07:47 +000040 int split;
caryclark@google.com639df892012-01-10 21:46:10 +000041 if (maxT1 - minT1 < maxT2 - minT2) {
42 intersections.swap();
caryclark@google.comc6825902012-02-03 22:07:47 +000043 minT2 = 0;
44 maxT2 = 1;
45 split = maxT1 - minT1 > tClipLimit;
46 } else {
47 minT1 = 0;
48 maxT1 = 1;
49 split = (maxT2 - minT2 > tClipLimit) << 1;
50 }
51 return chop(minT1, maxT1, minT2, maxT2, split);
caryclark@google.com639df892012-01-10 21:46:10 +000052}
caryclark@google.comc6825902012-02-03 22:07:47 +000053
54protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
caryclark@google.comc6825902012-02-03 22:07:47 +000056bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
57 Cubic smaller, larger;
rmistry@google.comd6176b02012-08-23 18:14:13 +000058 // FIXME: carry last subdivide and reduceOrder result with cubic
caryclark@google.comc6825902012-02-03 22:07:47 +000059 sub_divide(cubic1, minT1, maxT1, intersections.swapped() ? larger : smaller);
60 sub_divide(cubic2, minT2, maxT2, intersections.swapped() ? smaller : larger);
61 Cubic smallResult;
62 if (reduceOrder(smaller, smallResult,
63 kReduceOrder_NoQuadraticsAllowed) <= 2) {
64 Cubic largeResult;
65 if (reduceOrder(larger, largeResult,
66 kReduceOrder_NoQuadraticsAllowed) <= 2) {
67 const _Line& smallLine = (const _Line&) smallResult;
68 const _Line& largeLine = (const _Line&) largeResult;
69 double smallT[2];
70 double largeT[2];
71 // FIXME: this doesn't detect or deal with coincident lines
72 if (!::intersect(smallLine, largeLine, smallT, largeT)) {
73 return false;
74 }
75 if (intersections.swapped()) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000076 smallT[0] = interp(minT2, maxT2, smallT[0]);
77 largeT[0] = interp(minT1, maxT1, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000078 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +000079 smallT[0] = interp(minT1, maxT1, smallT[0]);
80 largeT[0] = interp(minT2, maxT2, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000081 }
82 intersections.add(smallT[0], largeT[0]);
83 return true;
84 }
85 }
86 double minT, maxT;
87 if (!bezier_clip(smaller, larger, minT, maxT)) {
88 if (minT == maxT) {
89 if (intersections.swapped()) {
90 minT1 = (minT1 + maxT1) / 2;
91 minT2 = interp(minT2, maxT2, minT);
92 } else {
93 minT1 = interp(minT1, maxT1, minT);
94 minT2 = (minT2 + maxT2) / 2;
95 }
96 intersections.add(minT1, minT2);
97 return true;
98 }
99 return false;
100 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
caryclark@google.comc6825902012-02-03 22:07:47 +0000102 int split;
103 if (intersections.swapped()) {
104 double newMinT1 = interp(minT1, maxT1, minT);
105 double newMaxT1 = interp(minT1, maxT1, maxT);
106 split = (newMaxT1 - newMinT1 > (maxT1 - minT1) * tClipLimit) << 1;
107#define VERBOSE 0
108#if VERBOSE
109 printf("%s d=%d s=%d new1=(%g,%g) old1=(%g,%g) split=%d\n",
110 __FUNCTION__, depth, splits, newMinT1, newMaxT1, minT1, maxT1,
111 split);
112#endif
113 minT1 = newMinT1;
114 maxT1 = newMaxT1;
115 } else {
116 double newMinT2 = interp(minT2, maxT2, minT);
117 double newMaxT2 = interp(minT2, maxT2, maxT);
118 split = newMaxT2 - newMinT2 > (maxT2 - minT2) * tClipLimit;
119#if VERBOSE
120 printf("%s d=%d s=%d new2=(%g,%g) old2=(%g,%g) split=%d\n",
121 __FUNCTION__, depth, splits, newMinT2, newMaxT2, minT2, maxT2,
122 split);
123#endif
124 minT2 = newMinT2;
125 maxT2 = newMaxT2;
126 }
127 return chop(minT1, maxT1, minT2, maxT2, split);
128}
129
130bool chop(double minT1, double maxT1, double minT2, double maxT2, int split) {
131 ++depth;
132 intersections.swap();
133 if (split) {
134 ++splits;
135 if (split & 2) {
136 double middle1 = (maxT1 + minT1) / 2;
137 intersect(minT1, middle1, minT2, maxT2);
138 intersect(middle1, maxT1, minT2, maxT2);
139 } else {
140 double middle2 = (maxT2 + minT2) / 2;
141 intersect(minT1, maxT1, minT2, middle2);
142 intersect(minT1, maxT1, middle2, maxT2);
143 }
144 --splits;
145 intersections.swap();
146 --depth;
147 return intersections.intersected();
148 }
149 bool result = intersect(minT1, maxT1, minT2, maxT2);
150 intersections.swap();
151 --depth;
152 return result;
153}
154
155private:
156
caryclark@google.comc6825902012-02-03 22:07:47 +0000157const Cubic& cubic1;
158const Cubic& cubic2;
159Intersections& intersections;
160int depth;
161int splits;
162};
163
164bool intersect(const Cubic& c1, const Cubic& c2, Intersections& i) {
165 CubicIntersections c(c1, c2, i);
166 return c.intersect();
167}
168
caryclark@google.comf9502d72013-02-04 14:06:49 +0000169#if COMPUTE_DELTA
caryclark@google.com9f602912013-01-24 21:47:16 +0000170static void cubicTangent(const Cubic& cubic, double t, _Line& tangent, _Point& pt, _Point& dxy) {
171 xy_at_t(cubic, t, tangent[0].x, tangent[0].y);
172 pt = tangent[1] = tangent[0];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000173 dxdy_at_t(cubic, t, dxy);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000174 if (dxy.approximatelyZero()) {
175 if (approximately_zero(t)) {
176 SkASSERT(cubic[0].approximatelyEqual(cubic[1]));
177 dxy = cubic[2];
178 dxy -= cubic[0];
179 } else {
180 SkASSERT(approximately_equal(t, 1));
181 SkASSERT(cubic[3].approximatelyEqual(cubic[2]));
182 dxy = cubic[3];
183 dxy -= cubic[1];
184 }
185 SkASSERT(!dxy.approximatelyZero());
186 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000187 tangent[0] -= dxy;
188 tangent[1] += dxy;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000189#if DEBUG_COMPUTE_DELTA
190 SkDebugf("%s t=%1.9g tangent=(%1.9g,%1.9g %1.9g,%1.9g)"
191 " pt=(%1.9g %1.9g) dxy=(%1.9g %1.9g)\n", __FUNCTION__, t,
192 tangent[0].x, tangent[0].y, tangent[1].x, tangent[1].y, pt.x, pt.y,
193 dxy.x, dxy.y);
194#endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000195}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000196#endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000197
caryclark@google.comf9502d72013-02-04 14:06:49 +0000198#if COMPUTE_DELTA
caryclark@google.com9f602912013-01-24 21:47:16 +0000199static double cubicDelta(const _Point& dxy, _Line& tangent, double scale) {
200 double tangentLen = dxy.length();
201 tangent[0] -= tangent[1];
202 double intersectLen = tangent[0].length();
203 double result = intersectLen / tangentLen + scale;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000204#if DEBUG_COMPUTE_DELTA
205 SkDebugf("%s tangent=(%1.9g,%1.9g %1.9g,%1.9g) intersectLen=%1.9g tangentLen=%1.9g scale=%1.9g"
206 " result=%1.9g\n", __FUNCTION__, tangent[0].x, tangent[0].y, tangent[1].x, tangent[1].y,
207 intersectLen, tangentLen, scale, result);
208#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000209 return result;
210}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000211#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000212
caryclark@google.comf9502d72013-02-04 14:06:49 +0000213#if COMPUTE_DELTA
caryclark@google.com9f602912013-01-24 21:47:16 +0000214// FIXME: after testing, make this static
caryclark@google.comf9502d72013-02-04 14:06:49 +0000215static void computeDelta(const Cubic& c1, double t1, double scale1, const Cubic& c2, double t2,
caryclark@google.com9f602912013-01-24 21:47:16 +0000216 double scale2, double& delta1, double& delta2) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000217#if DEBUG_COMPUTE_DELTA
218 SkDebugf("%s c1=(%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g) t1=%1.9g scale1=%1.9g"
219 " c2=(%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g) t2=%1.9g scale2=%1.9g\n",
220 __FUNCTION__,
221 c1[0].x, c1[0].y, c1[1].x, c1[1].y, c1[2].x, c1[2].y, c1[3].x, c1[3].y, t1, scale1,
222 c2[0].x, c2[0].y, c2[1].x, c2[1].y, c2[2].x, c2[2].y, c2[3].x, c2[3].y, t2, scale2);
223#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000224 _Line tangent1, tangent2, line1, line2;
225 _Point dxy1, dxy2;
226 cubicTangent(c1, t1, line1, tangent1[0], dxy1);
227 cubicTangent(c2, t2, line2, tangent2[0], dxy2);
228 double range1[2], range2[2];
229 int found = intersect(line1, line2, range1, range2);
230 if (found == 0) {
231 range1[0] = 0.5;
232 } else {
233 SkASSERT(found == 1);
234 }
235 xy_at_t(line1, range1[0], tangent1[1].x, tangent1[1].y);
236#if SK_DEBUG
237 if (found == 1) {
238 xy_at_t(line2, range2[0], tangent2[1].x, tangent2[1].y);
239 SkASSERT(tangent2[1].approximatelyEqual(tangent1[1]));
240 }
241#endif
242 tangent2[1] = tangent1[1];
243 delta1 = cubicDelta(dxy1, tangent1, scale1 / precisionUnit);
244 delta2 = cubicDelta(dxy2, tangent2, scale2 / precisionUnit);
245}
246
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000247#if SK_DEBUG
caryclark@google.com9f602912013-01-24 21:47:16 +0000248int debugDepth;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000249#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +0000250#endif
251
252static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) {
253 Cubic part;
254 sub_divide(cubic, tStart, tEnd, part);
255 Quadratic quad;
256 demote_cubic_to_quad(part, quad);
257 // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an
258 // extremely shallow quadratic?
259 int order = reduceOrder(quad, simple);
caryclark@google.combeda3892013-02-07 13:13:41 +0000260#if DEBUG_QUAD_PART
261 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",
262 __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y,
263 cubic[3].x, cubic[3].y, tStart, tEnd);
264 SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)"
265 " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, part[0].x, part[0].y,
266 part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y,
267 quad[1].x, quad[1].y, quad[2].x, quad[2].y);
268 SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y);
269 if (order > 1) {
270 SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y);
271 }
272 if (order > 2) {
273 SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y);
274 }
275 SkDebugf(")\n");
276 SkASSERT(order < 4 && order > 0);
277#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +0000278 return order;
279}
280
281static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2,
282 int order2, Intersections& i) {
283 if (order1 == 3 && order2 == 3) {
284 intersect2(simple1, simple2, i);
285 } else if (order1 <= 2 && order2 <= 2) {
286 i.fUsed = intersect((const _Line&) simple1, (const _Line&) simple2, i.fT[0], i.fT[1]);
287 } else if (order1 == 3 && order2 <= 2) {
288 intersect(simple1, (const _Line&) simple2, i);
289 } else {
290 SkASSERT(order1 <= 2 && order2 == 3);
291 intersect(simple2, (const _Line&) simple1, i);
292 for (int s = 0; s < i.fUsed; ++s) {
293 SkTSwap(i.fT[0][s], i.fT[1][s]);
294 }
295 }
296}
297
caryclark@google.combeda3892013-02-07 13:13:41 +0000298static double distanceFromEnd(double t) {
299 return t > 0.5 ? 1 - t : t;
300}
301
302// OPTIMIZATION: this used to try to guess the value for delta, and that may still be worthwhile
303static void bumpForRetry(double t1, double t2, double& s1, double& e1, double& s2, double& e2) {
304 double dt1 = distanceFromEnd(t1);
305 double dt2 = distanceFromEnd(t2);
306 double delta = 1.0 / precisionUnit;
307 if (dt1 < dt2) {
308 if (t1 == dt1) {
309 s1 = SkTMax(s1 - delta, 0.);
310 } else {
311 e1 = SkTMin(e1 + delta, 1.);
312 }
313 } else {
314 if (t2 == dt2) {
315 s2 = SkTMax(s2 - delta, 0.);
316 } else {
317 e2 = SkTMin(e2 + delta, 1.);
318 }
319 }
320}
321
322static bool doIntersect(const Cubic& cubic1, double t1s, double t1m, double t1e,
caryclark@google.comf9502d72013-02-04 14:06:49 +0000323 const Cubic& cubic2, double t2s, double t2m, double t2e, Intersections& i) {
caryclark@google.combeda3892013-02-07 13:13:41 +0000324 bool result = false;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000325 i.upDepth();
326 // divide the quadratics at the new t value and try again
327 double p1s = t1s;
328 double p1e = t1m;
329 for (int p1 = 0; p1 < 2; ++p1) {
330 Quadratic s1a;
331 int o1a = quadPart(cubic1, p1s, p1e, s1a);
332 double p2s = t2s;
333 double p2e = t2m;
334 for (int p2 = 0; p2 < 2; ++p2) {
335 Quadratic s2a;
336 int o2a = quadPart(cubic2, p2s, p2e, s2a);
337 Intersections locals;
338 #if 0 && SK_DEBUG
caryclark@google.combeda3892013-02-07 13:13:41 +0000339 if (0.497026154 >= p1s && 0.497026535 <= p1e
340 && 0.710440575 >= p2s && 0.710440956 <= p2e) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000341 SkDebugf("t1=(%1.9g,%1.9g) o1=%d t2=(%1.9g,%1.9g) o2=%d\n",
342 p1s, p1e, o1a, p2s, p2e, o2a);
343 if (o1a == 2) {
344 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
345 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y);
346 } else {
347 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
348 s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y, s1a[2].x, s1a[2].y);
349 }
350 if (o2a == 2) {
351 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
352 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y);
353 } else {
354 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
355 s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y, s2a[2].x, s2a[2].y);
356 }
357 Intersections xlocals;
358 intersectWithOrder(s1a, o1a, s2a, o2a, xlocals);
caryclark@google.combeda3892013-02-07 13:13:41 +0000359 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000360 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000361 #endif
362 intersectWithOrder(s1a, o1a, s2a, o2a, locals);
363 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
364 double to1 = p1s + (p1e - p1s) * locals.fT[0][tIdx];
365 double to2 = p2s + (p2e - p2s) * locals.fT[1][tIdx];
366 // if the computed t is not sufficiently precise, iterate
367 _Point p1, p2;
368 xy_at_t(cubic1, to1, p1.x, p1.y);
369 xy_at_t(cubic2, to2, p2.x, p2.y);
370 #if 0 && SK_DEBUG
371 SkDebugf("to1=%1.9g p1=(%1.9g,%1.9g) to2=%1.9g p2=(%1.9g,%1.9g) d=%1.9g\n",
372 to1, p1.x, p1.y, to2, p2.x, p2.y, p1.distance(p2));
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000373
caryclark@google.comf9502d72013-02-04 14:06:49 +0000374 #endif
375 if (p1.approximatelyEqual(p2)) {
376 i.insert(i.swapped() ? to2 : to1, i.swapped() ? to1 : to2);
caryclark@google.combeda3892013-02-07 13:13:41 +0000377 result = true;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000378 } else {
caryclark@google.combeda3892013-02-07 13:13:41 +0000379 result = doIntersect(cubic1, p1s, to1, p1e, cubic2, p2s, to2, p2e, i);
380 // if both cubics curve in the same direction, the quadratic intersection
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000381 // may mark a range that does not contain the cubic intersection. If no
382 // intersection is found, look again including the t distance of the
caryclark@google.combeda3892013-02-07 13:13:41 +0000383 // of the quadratic intersection nearest a quadratic end (which in turn is
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000384 // nearest the actual cubic)
caryclark@google.combeda3892013-02-07 13:13:41 +0000385 if (!result) {
386 double b1s = p1s;
387 double b1e = p1e;
388 double b2s = p2s;
389 double b2e = p2e;
390 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
391 result = doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
392 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000393 }
394 }
395 p2s = p2e;
396 p2e = t2e;
397 }
398 p1s = p1e;
399 p1e = t1e;
400 }
401 i.downDepth();
caryclark@google.combeda3892013-02-07 13:13:41 +0000402 return result;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000403}
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000404
caryclark@google.com73ca6242013-01-17 21:02:47 +0000405// this flavor approximates the cubics with quads to find the intersecting ts
406// OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used
407// to create the approximations, could be stored in the cubic segment
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000408// FIXME: this strategy needs to intersect the convex hull on either end with the opposite to
409// account for inset quadratics that cause the endpoint intersection to avoid detection
410// the segments can be very short -- the length of the maximum quadratic error (precision)
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000411static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000412 double t2s, double t2e, double precisionScale, Intersections& i) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000413 Cubic c1, c2;
414 sub_divide(cubic1, t1s, t1e, c1);
415 sub_divide(cubic2, t2s, t2e, c2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000416 SkTDArray<double> ts1;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000417 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000418 SkTDArray<double> ts2;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000419 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000420 double t1Start = t1s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000421 int ts1Count = ts1.count();
422 for (int i1 = 0; i1 <= ts1Count; ++i1) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000423 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
424 const double t1 = t1s + (t1e - t1s) * tEnd1;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000425 Quadratic s1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000426 int o1 = quadPart(cubic1, t1Start, t1, s1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000427 double t2Start = t2s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000428 int ts2Count = ts2.count();
429 for (int i2 = 0; i2 <= ts2Count; ++i2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000430 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
431 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000432 Quadratic s2;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000433 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.combeda3892013-02-07 13:13:41 +0000434 #if 0 && SK_DEBUG
435 if (0.497026154 >= t1Start && 0.497026535 <= t1
436 && 0.710440575 + 0.0004 >= t2Start && 0.710440956 <= t2) {
437 Cubic cSub1, cSub2;
438 sub_divide(cubic1, t1Start, tEnd1, cSub1);
439 sub_divide(cubic2, t2Start, tEnd2, cSub2);
440 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
441 t1Start, t1, t2Start, t2);
442 Intersections xlocals;
443 intersectWithOrder(s1, o1, s2, o2, xlocals);
444 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
445 }
446 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000447 Intersections locals;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000448 intersectWithOrder(s1, o1, s2, o2, locals);
skia.committer@gmail.comee235f92013-02-08 07:16:45 +0000449
caryclark@google.com73ca6242013-01-17 21:02:47 +0000450 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
451 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
452 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000453 // if the computed t is not sufficiently precise, iterate
454 _Point p1, p2;
455 xy_at_t(cubic1, to1, p1.x, p1.y);
456 xy_at_t(cubic2, to2, p2.x, p2.y);
457 if (p1.approximatelyEqual(p2)) {
458 i.insert(i.swapped() ? to2 : to1, i.swapped() ? to1 : to2);
459 } else {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000460 #if COMPUTE_DELTA
caryclark@google.com9f602912013-01-24 21:47:16 +0000461 double dt1, dt2;
462 computeDelta(cubic1, to1, (t1e - t1s), cubic2, to2, (t2e - t2s), dt1, dt2);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000463 double scale = precisionScale;
464 if (dt1 > 0.125 || dt2 > 0.125) {
465 scale /= 2;
466 SkDebugf("%s scale=%1.9g\n", __FUNCTION__, scale);
467 }
468#if SK_DEBUG
caryclark@google.com9f602912013-01-24 21:47:16 +0000469 ++debugDepth;
caryclark@google.comaa358312013-01-29 20:28:49 +0000470 SkASSERT(debugDepth < 10);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000471#endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000472 i.swap();
caryclark@google.com9f602912013-01-24 21:47:16 +0000473 intersect2(cubic2, SkTMax(to2 - dt2, 0.), SkTMin(to2 + dt2, 1.),
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000474 cubic1, SkTMax(to1 - dt1, 0.), SkTMin(to1 + dt1, 1.), scale, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000475 i.swap();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000476#if SK_DEBUG
caryclark@google.com9f602912013-01-24 21:47:16 +0000477 --debugDepth;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000478#endif
caryclark@google.comf9502d72013-02-04 14:06:49 +0000479 #else
caryclark@google.combeda3892013-02-07 13:13:41 +0000480 #if 0 && SK_DEBUG
481 if (0.497026154 >= t1Start && 0.497026535 <= t1
482 && 0.710440575 >= t2Start && 0.710440956 <= t2) {
483 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
484 t1Start, t1, t2Start, t2);
485 }
486 #endif
487 bool found = doIntersect(cubic1, t1Start, to1, t1, cubic2, t2Start, to2, t2, i);
488 if (!found) {
489 double b1s = t1Start;
490 double b1e = t1;
491 double b2s = t2Start;
492 double b2e = t2;
493 bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e);
494 doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i);
495 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000496 #endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000497 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000498 }
caryclark@google.combeda3892013-02-07 13:13:41 +0000499 if (locals.coincidentUsed()) {
500 SkASSERT(locals.coincidentUsed() == 2);
501 double coTs[2][2];
502 for (int tIdx = 0; tIdx < locals.coincidentUsed(); ++tIdx) {
503 coTs[0][tIdx] = t1Start + (t1 - t1Start) * locals.fCoincidentT[0][tIdx];
504 coTs[1][tIdx] = t2Start + (t2 - t2Start) * locals.fCoincidentT[1][tIdx];
505 }
506 i.addCoincident(coTs[0][0], coTs[0][1], coTs[1][0], coTs[1][1]);
507 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000508 t2Start = t2;
509 }
510 t1Start = t1;
511 }
512 return i.intersected();
513}
514
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000515static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
516 Intersections& i) {
517 _Line line1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000518 line1[1] = cubic1[start ? 0 : 3];
519 if (line1[1].approximatelyEqual(cubic2[0]) || line1[1].approximatelyEqual(cubic2[3])) {
520 return false;
521 }
522 line1[0] = line1[1];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000523 _Point dxy1 = line1[0] - cubic1[start ? 1 : 2];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000524 if (dxy1.approximatelyZero()) {
525 dxy1 = line1[0] - cubic1[start ? 2 : 1];
526 }
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000527 dxy1 /= precisionUnit;
528 line1[1] += dxy1;
529 _Rect line1Bounds;
530 line1Bounds.setBounds(line1);
531 if (!bounds2.intersects(line1Bounds)) {
532 return false;
533 }
534 _Line line2;
535 line2[0] = line2[1] = line1[0];
536 _Point dxy2 = line2[0] - cubic1[start ? 3 : 0];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000537 SkASSERT(!dxy2.approximatelyZero());
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000538 dxy2 /= precisionUnit;
539 line2[1] += dxy2;
540#if 0 // this is so close to the first bounds test it isn't worth the short circuit test
541 _Rect line2Bounds;
542 line2Bounds.setBounds(line2);
543 if (!bounds2.intersects(line2Bounds)) {
544 return false;
545 }
546#endif
547 Intersections local1;
548 if (!intersect(cubic2, line1, local1)) {
549 return false;
550 }
551 Intersections local2;
552 if (!intersect(cubic2, line2, local2)) {
553 return false;
554 }
555 double tMin, tMax;
556 tMin = tMax = local1.fT[0][0];
557 for (int index = 1; index < local1.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000558 tMin = SkTMin(tMin, local1.fT[0][index]);
559 tMax = SkTMax(tMax, local1.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000560 }
561 for (int index = 1; index < local2.fUsed; ++index) {
caryclark@google.comaa358312013-01-29 20:28:49 +0000562 tMin = SkTMin(tMin, local2.fT[0][index]);
563 tMax = SkTMax(tMax, local2.fT[0][index]);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000564 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000565#if SK_DEBUG && COMPUTE_DELTA
caryclark@google.com9f602912013-01-24 21:47:16 +0000566 debugDepth = 0;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000567#endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000568 return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000569 cubic2, tMin, tMax, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000570}
571
caryclark@google.comf9502d72013-02-04 14:06:49 +0000572// FIXME: add intersection of convex hull on cubics' ends with the opposite cubic. The hull line
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000573// segments can be constructed to be only as long as the calculated precision suggests. If the hull
574// line segments intersect the cubic, then use the intersections to construct a subdivision for
575// quadratic curve fitting.
576bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000577#if SK_DEBUG && COMPUTE_DELTA
caryclark@google.com9f602912013-01-24 21:47:16 +0000578 debugDepth = 0;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000579#endif
580 bool result = intersect2(c1, 0, 1, c2, 0, 1, 1, i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000581 // FIXME: pass in cached bounds from caller
582 _Rect c1Bounds, c2Bounds;
583 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
584 c2Bounds.setBounds(c2);
585 result |= intersectEnd(c1, false, c2, c2Bounds, i);
586 result |= intersectEnd(c1, true, c2, c2Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000587 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000588 result |= intersectEnd(c2, false, c1, c1Bounds, i);
589 result |= intersectEnd(c2, true, c1, c1Bounds, i);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000590 i.swap();
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000591 return result;
592}
593
caryclark@google.com73ca6242013-01-17 21:02:47 +0000594int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
595 SkTDArray<double> ts;
596 double precision = calcPrecision(cubic);
597 cubic_to_quadratics(cubic, precision, ts);
598 double tStart = 0;
599 Cubic part;
600 int tsCount = ts.count();
601 for (int idx = 0; idx <= tsCount; ++idx) {
602 double t = idx < tsCount ? ts[idx] : 1;
603 Quadratic q1;
604 sub_divide(cubic, tStart, t, part);
605 demote_cubic_to_quad(part, q1);
606 Intersections locals;
607 intersect2(q1, quad, locals);
608 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
609 double globalT = tStart + (t - tStart) * locals.fT[0][tIdx];
610 i.insertOne(globalT, 0);
611 globalT = locals.fT[1][tIdx];
612 i.insertOne(globalT, 1);
613 }
614 tStart = t;
615 }
616 return i.used();
617}
618
619bool intersect(const Cubic& cubic, Intersections& i) {
620 SkTDArray<double> ts;
621 double precision = calcPrecision(cubic);
622 cubic_to_quadratics(cubic, precision, ts);
623 int tsCount = ts.count();
624 if (tsCount == 1) {
625 return false;
626 }
627 double t1Start = 0;
628 Cubic part;
629 for (int idx = 0; idx < tsCount; ++idx) {
630 double t1 = ts[idx];
631 Quadratic q1;
632 sub_divide(cubic, t1Start, t1, part);
633 demote_cubic_to_quad(part, q1);
634 double t2Start = t1;
635 for (int i2 = idx + 1; i2 <= tsCount; ++i2) {
636 const double t2 = i2 < tsCount ? ts[i2] : 1;
637 Quadratic q2;
638 sub_divide(cubic, t2Start, t2, part);
639 demote_cubic_to_quad(part, q2);
640 Intersections locals;
641 intersect2(q1, q2, locals);
642 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
643 // discard intersections at cusp? (maximum curvature)
644 double t1sect = locals.fT[0][tIdx];
645 double t2sect = locals.fT[1][tIdx];
646 if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) {
647 continue;
648 }
649 double to1 = t1Start + (t1 - t1Start) * t1sect;
650 double to2 = t2Start + (t2 - t2Start) * t2sect;
651 i.insert(to1, to2);
652 }
653 t2Start = t2;
654 }
655 t1Start = t1;
656 }
657 return i.intersected();
658}