blob: 2c6cc3f5771a9466f1e41530b20f1bd8686a59a4 [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.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +00008#include "Intersections.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00009#include "IntersectionUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +000010#include "LineIntersection.h"
11
caryclark@google.com0d3d09e2012-12-10 14:50:04 +000012static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections
13
caryclark@google.comc6825902012-02-03 22:07:47 +000014class CubicIntersections : public Intersections {
15public:
16
rmistry@google.comd6176b02012-08-23 18:14:13 +000017CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i)
caryclark@google.comc6825902012-02-03 22:07:47 +000018 : cubic1(c1)
19 , cubic2(c2)
20 , intersections(i)
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 , depth(0)
caryclark@google.comc6825902012-02-03 22:07:47 +000022 , splits(0) {
caryclark@google.com639df892012-01-10 21:46:10 +000023}
24
caryclark@google.comc6825902012-02-03 22:07:47 +000025bool intersect() {
caryclark@google.com639df892012-01-10 21:46:10 +000026 double minT1, minT2, maxT1, maxT2;
27 if (!bezier_clip(cubic2, cubic1, minT1, maxT1)) {
28 return false;
29 }
30 if (!bezier_clip(cubic1, cubic2, minT2, maxT2)) {
31 return false;
32 }
caryclark@google.comc6825902012-02-03 22:07:47 +000033 int split;
caryclark@google.com639df892012-01-10 21:46:10 +000034 if (maxT1 - minT1 < maxT2 - minT2) {
35 intersections.swap();
caryclark@google.comc6825902012-02-03 22:07:47 +000036 minT2 = 0;
37 maxT2 = 1;
38 split = maxT1 - minT1 > tClipLimit;
39 } else {
40 minT1 = 0;
41 maxT1 = 1;
42 split = (maxT2 - minT2 > tClipLimit) << 1;
43 }
44 return chop(minT1, maxT1, minT2, maxT2, split);
caryclark@google.com639df892012-01-10 21:46:10 +000045}
caryclark@google.comc6825902012-02-03 22:07:47 +000046
47protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +000048
caryclark@google.comc6825902012-02-03 22:07:47 +000049bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
50 Cubic smaller, larger;
rmistry@google.comd6176b02012-08-23 18:14:13 +000051 // FIXME: carry last subdivide and reduceOrder result with cubic
caryclark@google.comc6825902012-02-03 22:07:47 +000052 sub_divide(cubic1, minT1, maxT1, intersections.swapped() ? larger : smaller);
53 sub_divide(cubic2, minT2, maxT2, intersections.swapped() ? smaller : larger);
54 Cubic smallResult;
55 if (reduceOrder(smaller, smallResult,
56 kReduceOrder_NoQuadraticsAllowed) <= 2) {
57 Cubic largeResult;
58 if (reduceOrder(larger, largeResult,
59 kReduceOrder_NoQuadraticsAllowed) <= 2) {
60 const _Line& smallLine = (const _Line&) smallResult;
61 const _Line& largeLine = (const _Line&) largeResult;
62 double smallT[2];
63 double largeT[2];
64 // FIXME: this doesn't detect or deal with coincident lines
65 if (!::intersect(smallLine, largeLine, smallT, largeT)) {
66 return false;
67 }
68 if (intersections.swapped()) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000069 smallT[0] = interp(minT2, maxT2, smallT[0]);
70 largeT[0] = interp(minT1, maxT1, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000071 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +000072 smallT[0] = interp(minT1, maxT1, smallT[0]);
73 largeT[0] = interp(minT2, maxT2, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000074 }
75 intersections.add(smallT[0], largeT[0]);
76 return true;
77 }
78 }
79 double minT, maxT;
80 if (!bezier_clip(smaller, larger, minT, maxT)) {
81 if (minT == maxT) {
82 if (intersections.swapped()) {
83 minT1 = (minT1 + maxT1) / 2;
84 minT2 = interp(minT2, maxT2, minT);
85 } else {
86 minT1 = interp(minT1, maxT1, minT);
87 minT2 = (minT2 + maxT2) / 2;
88 }
89 intersections.add(minT1, minT2);
90 return true;
91 }
92 return false;
93 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000094
caryclark@google.comc6825902012-02-03 22:07:47 +000095 int split;
96 if (intersections.swapped()) {
97 double newMinT1 = interp(minT1, maxT1, minT);
98 double newMaxT1 = interp(minT1, maxT1, maxT);
99 split = (newMaxT1 - newMinT1 > (maxT1 - minT1) * tClipLimit) << 1;
100#define VERBOSE 0
101#if VERBOSE
102 printf("%s d=%d s=%d new1=(%g,%g) old1=(%g,%g) split=%d\n",
103 __FUNCTION__, depth, splits, newMinT1, newMaxT1, minT1, maxT1,
104 split);
105#endif
106 minT1 = newMinT1;
107 maxT1 = newMaxT1;
108 } else {
109 double newMinT2 = interp(minT2, maxT2, minT);
110 double newMaxT2 = interp(minT2, maxT2, maxT);
111 split = newMaxT2 - newMinT2 > (maxT2 - minT2) * tClipLimit;
112#if VERBOSE
113 printf("%s d=%d s=%d new2=(%g,%g) old2=(%g,%g) split=%d\n",
114 __FUNCTION__, depth, splits, newMinT2, newMaxT2, minT2, maxT2,
115 split);
116#endif
117 minT2 = newMinT2;
118 maxT2 = newMaxT2;
119 }
120 return chop(minT1, maxT1, minT2, maxT2, split);
121}
122
123bool chop(double minT1, double maxT1, double minT2, double maxT2, int split) {
124 ++depth;
125 intersections.swap();
126 if (split) {
127 ++splits;
128 if (split & 2) {
129 double middle1 = (maxT1 + minT1) / 2;
130 intersect(minT1, middle1, minT2, maxT2);
131 intersect(middle1, maxT1, minT2, maxT2);
132 } else {
133 double middle2 = (maxT2 + minT2) / 2;
134 intersect(minT1, maxT1, minT2, middle2);
135 intersect(minT1, maxT1, middle2, maxT2);
136 }
137 --splits;
138 intersections.swap();
139 --depth;
140 return intersections.intersected();
141 }
142 bool result = intersect(minT1, maxT1, minT2, maxT2);
143 intersections.swap();
144 --depth;
145 return result;
146}
147
148private:
149
caryclark@google.comc6825902012-02-03 22:07:47 +0000150const Cubic& cubic1;
151const Cubic& cubic2;
152Intersections& intersections;
153int depth;
154int splits;
155};
156
157bool intersect(const Cubic& c1, const Cubic& c2, Intersections& i) {
158 CubicIntersections c(c1, c2, i);
159 return c.intersect();
160}
161
caryclark@google.com73ca6242013-01-17 21:02:47 +0000162#include "CubicUtilities.h"
163
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000164// FIXME: ? if needed, compute the error term from the tangent length
165static double computeDelta(const Cubic& cubic, double t, double scale) {
166 double attempt = scale / precisionUnit;
167#if SK_DEBUG
168 double precision = calcPrecision(cubic);
169 _Point dxy;
170 dxdy_at_t(cubic, t, dxy);
171 _Point p1, p2;
172 xy_at_t(cubic, std::max(t - attempt, 0.), p1.x, p1.y);
173 xy_at_t(cubic, std::min(t + attempt, 1.), p2.x, p2.y);
174 double dx = p1.x - p2.x;
175 double dy = p1.y - p2.y;
176 double distSq = dx * dx + dy * dy;
177 double dist = sqrt(distSq);
178 assert(dist > precision);
179#endif
180 return attempt;
181}
182
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)
189// FIXME: this needs to recurse on itself, taking a range of T values and computing the new
190// t range ala is linear inner. The range can be figured by taking the dx/dy and determining
191// the fraction that matches the precision. That fraction is the change in t for the smaller cubic.
192static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
193 double t2s, double t2e, Intersections& i) {
194 Cubic c1, c2;
195 sub_divide(cubic1, t1s, t1e, c1);
196 sub_divide(cubic2, t2s, t2e, c2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000197 SkTDArray<double> ts1;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000198 cubic_to_quadratics(c1, calcPrecision(c1), ts1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000199 SkTDArray<double> ts2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000200 cubic_to_quadratics(c2, calcPrecision(c2), ts2);
201 double t1Start = t1s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000202 int ts1Count = ts1.count();
203 for (int i1 = 0; i1 <= ts1Count; ++i1) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000204 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
205 const double t1 = t1s + (t1e - t1s) * tEnd1;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000206 Cubic part1;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000207 sub_divide(cubic1, t1Start, t1, part1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000208 Quadratic q1;
209 demote_cubic_to_quad(part1, q1);
210 // start here;
211 // should reduceOrder be looser in this use case if quartic is going to blow up on an
212 // extremely shallow quadratic?
caryclark@google.com73ca6242013-01-17 21:02:47 +0000213 Quadratic s1;
214 int o1 = reduceOrder(q1, s1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000215 double t2Start = t2s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000216 int ts2Count = ts2.count();
217 for (int i2 = 0; i2 <= ts2Count; ++i2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000218 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
219 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000220 Cubic part2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000221 sub_divide(cubic2, t2Start, t2, part2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000222 Quadratic q2;
223 demote_cubic_to_quad(part2, q2);
224 Quadratic s2;
225 double o2 = reduceOrder(q2, s2);
226 Intersections locals;
227 if (o1 == 3 && o2 == 3) {
228 intersect2(q1, q2, locals);
229 } else if (o1 <= 2 && o2 <= 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000230 locals.fUsed = intersect((const _Line&) s1, (const _Line&) s2, locals.fT[0],
231 locals.fT[1]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000232 } else if (o1 == 3 && o2 <= 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000233 intersect(q1, (const _Line&) s2, locals);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000234 } else {
235 SkASSERT(o1 <= 2 && o2 == 3);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000236 intersect(q2, (const _Line&) s1, locals);
237 for (int s = 0; s < locals.fUsed; ++s) {
238 SkTSwap(locals.fT[0][s], locals.fT[1][s]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000239 }
240 }
241 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
242 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
243 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000244 // if the computed t is not sufficiently precise, iterate
245 _Point p1, p2;
246 xy_at_t(cubic1, to1, p1.x, p1.y);
247 xy_at_t(cubic2, to2, p2.x, p2.y);
248 if (p1.approximatelyEqual(p2)) {
249 i.insert(i.swapped() ? to2 : to1, i.swapped() ? to1 : to2);
250 } else {
251 double dt1 = computeDelta(cubic1, to1, t1e - t1s);
252 double dt2 = computeDelta(cubic2, to2, t2e - t2s);
253 i.swap();
254 intersect2(cubic2, std::max(to2 - dt2, 0.), std::min(to2 + dt2, 1.),
255 cubic1, std::max(to1 - dt1, 0.), std::min(to1 + dt1, 1.), i);
256 i.swap();
257 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000258 }
259 t2Start = t2;
260 }
261 t1Start = t1;
262 }
263 return i.intersected();
264}
265
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000266static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
267 Intersections& i) {
268 _Line line1;
269 line1[0] = line1[1] = cubic1[start ? 0 : 3];
270 _Point dxy1 = line1[0] - cubic1[start ? 1 : 2];
271 dxy1 /= precisionUnit;
272 line1[1] += dxy1;
273 _Rect line1Bounds;
274 line1Bounds.setBounds(line1);
275 if (!bounds2.intersects(line1Bounds)) {
276 return false;
277 }
278 _Line line2;
279 line2[0] = line2[1] = line1[0];
280 _Point dxy2 = line2[0] - cubic1[start ? 3 : 0];
281 dxy2 /= precisionUnit;
282 line2[1] += dxy2;
283#if 0 // this is so close to the first bounds test it isn't worth the short circuit test
284 _Rect line2Bounds;
285 line2Bounds.setBounds(line2);
286 if (!bounds2.intersects(line2Bounds)) {
287 return false;
288 }
289#endif
290 Intersections local1;
291 if (!intersect(cubic2, line1, local1)) {
292 return false;
293 }
294 Intersections local2;
295 if (!intersect(cubic2, line2, local2)) {
296 return false;
297 }
298 double tMin, tMax;
299 tMin = tMax = local1.fT[0][0];
300 for (int index = 1; index < local1.fUsed; ++index) {
301 tMin = std::min(tMin, local1.fT[0][index]);
302 tMax = std::max(tMax, local1.fT[0][index]);
303 }
304 for (int index = 1; index < local2.fUsed; ++index) {
305 tMin = std::min(tMin, local2.fT[0][index]);
306 tMax = std::max(tMax, local2.fT[0][index]);
307 }
308 return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit,
309 cubic2, tMin, tMax, i);
310}
311
312// FIXME: add intersection of convex null on cubics' ends with the opposite cubic. The hull line
313// segments can be constructed to be only as long as the calculated precision suggests. If the hull
314// line segments intersect the cubic, then use the intersections to construct a subdivision for
315// quadratic curve fitting.
316bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
317 bool result = intersect2(c1, 0, 1, c2, 0, 1, i);
318 // FIXME: pass in cached bounds from caller
319 _Rect c1Bounds, c2Bounds;
320 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
321 c2Bounds.setBounds(c2);
322 result |= intersectEnd(c1, false, c2, c2Bounds, i);
323 result |= intersectEnd(c1, true, c2, c2Bounds, i);
324 result |= intersectEnd(c2, false, c1, c1Bounds, i);
325 result |= intersectEnd(c2, true, c1, c1Bounds, i);
326 return result;
327}
328
caryclark@google.com73ca6242013-01-17 21:02:47 +0000329int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
330 SkTDArray<double> ts;
331 double precision = calcPrecision(cubic);
332 cubic_to_quadratics(cubic, precision, ts);
333 double tStart = 0;
334 Cubic part;
335 int tsCount = ts.count();
336 for (int idx = 0; idx <= tsCount; ++idx) {
337 double t = idx < tsCount ? ts[idx] : 1;
338 Quadratic q1;
339 sub_divide(cubic, tStart, t, part);
340 demote_cubic_to_quad(part, q1);
341 Intersections locals;
342 intersect2(q1, quad, locals);
343 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
344 double globalT = tStart + (t - tStart) * locals.fT[0][tIdx];
345 i.insertOne(globalT, 0);
346 globalT = locals.fT[1][tIdx];
347 i.insertOne(globalT, 1);
348 }
349 tStart = t;
350 }
351 return i.used();
352}
353
354bool intersect(const Cubic& cubic, Intersections& i) {
355 SkTDArray<double> ts;
356 double precision = calcPrecision(cubic);
357 cubic_to_quadratics(cubic, precision, ts);
358 int tsCount = ts.count();
359 if (tsCount == 1) {
360 return false;
361 }
362 double t1Start = 0;
363 Cubic part;
364 for (int idx = 0; idx < tsCount; ++idx) {
365 double t1 = ts[idx];
366 Quadratic q1;
367 sub_divide(cubic, t1Start, t1, part);
368 demote_cubic_to_quad(part, q1);
369 double t2Start = t1;
370 for (int i2 = idx + 1; i2 <= tsCount; ++i2) {
371 const double t2 = i2 < tsCount ? ts[i2] : 1;
372 Quadratic q2;
373 sub_divide(cubic, t2Start, t2, part);
374 demote_cubic_to_quad(part, q2);
375 Intersections locals;
376 intersect2(q1, q2, locals);
377 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
378 // discard intersections at cusp? (maximum curvature)
379 double t1sect = locals.fT[0][tIdx];
380 double t2sect = locals.fT[1][tIdx];
381 if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) {
382 continue;
383 }
384 double to1 = t1Start + (t1 - t1Start) * t1sect;
385 double to2 = t2Start + (t2 - t2Start) * t2sect;
386 i.insert(to1, to2);
387 }
388 t2Start = t2;
389 }
390 t1Start = t1;
391 }
392 return i.intersected();
393}