blob: a5b4261dfe12d8a9a0ee024c28229fdfe3572588 [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.com0d3d09e2012-12-10 14:50:04 +000015static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections
16
caryclark@google.comc6825902012-02-03 22:07:47 +000017class CubicIntersections : public Intersections {
18public:
19
rmistry@google.comd6176b02012-08-23 18:14:13 +000020CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i)
caryclark@google.comc6825902012-02-03 22:07:47 +000021 : cubic1(c1)
22 , cubic2(c2)
23 , intersections(i)
rmistry@google.comd6176b02012-08-23 18:14:13 +000024 , depth(0)
caryclark@google.comc6825902012-02-03 22:07:47 +000025 , splits(0) {
caryclark@google.com639df892012-01-10 21:46:10 +000026}
27
caryclark@google.comc6825902012-02-03 22:07:47 +000028bool intersect() {
caryclark@google.com639df892012-01-10 21:46:10 +000029 double minT1, minT2, maxT1, maxT2;
30 if (!bezier_clip(cubic2, cubic1, minT1, maxT1)) {
31 return false;
32 }
33 if (!bezier_clip(cubic1, cubic2, minT2, maxT2)) {
34 return false;
35 }
caryclark@google.comc6825902012-02-03 22:07:47 +000036 int split;
caryclark@google.com639df892012-01-10 21:46:10 +000037 if (maxT1 - minT1 < maxT2 - minT2) {
38 intersections.swap();
caryclark@google.comc6825902012-02-03 22:07:47 +000039 minT2 = 0;
40 maxT2 = 1;
41 split = maxT1 - minT1 > tClipLimit;
42 } else {
43 minT1 = 0;
44 maxT1 = 1;
45 split = (maxT2 - minT2 > tClipLimit) << 1;
46 }
47 return chop(minT1, maxT1, minT2, maxT2, split);
caryclark@google.com639df892012-01-10 21:46:10 +000048}
caryclark@google.comc6825902012-02-03 22:07:47 +000049
50protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
caryclark@google.comc6825902012-02-03 22:07:47 +000052bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
53 Cubic smaller, larger;
rmistry@google.comd6176b02012-08-23 18:14:13 +000054 // FIXME: carry last subdivide and reduceOrder result with cubic
caryclark@google.comc6825902012-02-03 22:07:47 +000055 sub_divide(cubic1, minT1, maxT1, intersections.swapped() ? larger : smaller);
56 sub_divide(cubic2, minT2, maxT2, intersections.swapped() ? smaller : larger);
57 Cubic smallResult;
58 if (reduceOrder(smaller, smallResult,
59 kReduceOrder_NoQuadraticsAllowed) <= 2) {
60 Cubic largeResult;
61 if (reduceOrder(larger, largeResult,
62 kReduceOrder_NoQuadraticsAllowed) <= 2) {
63 const _Line& smallLine = (const _Line&) smallResult;
64 const _Line& largeLine = (const _Line&) largeResult;
65 double smallT[2];
66 double largeT[2];
67 // FIXME: this doesn't detect or deal with coincident lines
68 if (!::intersect(smallLine, largeLine, smallT, largeT)) {
69 return false;
70 }
71 if (intersections.swapped()) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000072 smallT[0] = interp(minT2, maxT2, smallT[0]);
73 largeT[0] = interp(minT1, maxT1, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000074 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +000075 smallT[0] = interp(minT1, maxT1, smallT[0]);
76 largeT[0] = interp(minT2, maxT2, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000077 }
78 intersections.add(smallT[0], largeT[0]);
79 return true;
80 }
81 }
82 double minT, maxT;
83 if (!bezier_clip(smaller, larger, minT, maxT)) {
84 if (minT == maxT) {
85 if (intersections.swapped()) {
86 minT1 = (minT1 + maxT1) / 2;
87 minT2 = interp(minT2, maxT2, minT);
88 } else {
89 minT1 = interp(minT1, maxT1, minT);
90 minT2 = (minT2 + maxT2) / 2;
91 }
92 intersections.add(minT1, minT2);
93 return true;
94 }
95 return false;
96 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
caryclark@google.comc6825902012-02-03 22:07:47 +000098 int split;
99 if (intersections.swapped()) {
100 double newMinT1 = interp(minT1, maxT1, minT);
101 double newMaxT1 = interp(minT1, maxT1, maxT);
102 split = (newMaxT1 - newMinT1 > (maxT1 - minT1) * tClipLimit) << 1;
103#define VERBOSE 0
104#if VERBOSE
105 printf("%s d=%d s=%d new1=(%g,%g) old1=(%g,%g) split=%d\n",
106 __FUNCTION__, depth, splits, newMinT1, newMaxT1, minT1, maxT1,
107 split);
108#endif
109 minT1 = newMinT1;
110 maxT1 = newMaxT1;
111 } else {
112 double newMinT2 = interp(minT2, maxT2, minT);
113 double newMaxT2 = interp(minT2, maxT2, maxT);
114 split = newMaxT2 - newMinT2 > (maxT2 - minT2) * tClipLimit;
115#if VERBOSE
116 printf("%s d=%d s=%d new2=(%g,%g) old2=(%g,%g) split=%d\n",
117 __FUNCTION__, depth, splits, newMinT2, newMaxT2, minT2, maxT2,
118 split);
119#endif
120 minT2 = newMinT2;
121 maxT2 = newMaxT2;
122 }
123 return chop(minT1, maxT1, minT2, maxT2, split);
124}
125
126bool chop(double minT1, double maxT1, double minT2, double maxT2, int split) {
127 ++depth;
128 intersections.swap();
129 if (split) {
130 ++splits;
131 if (split & 2) {
132 double middle1 = (maxT1 + minT1) / 2;
133 intersect(minT1, middle1, minT2, maxT2);
134 intersect(middle1, maxT1, minT2, maxT2);
135 } else {
136 double middle2 = (maxT2 + minT2) / 2;
137 intersect(minT1, maxT1, minT2, middle2);
138 intersect(minT1, maxT1, middle2, maxT2);
139 }
140 --splits;
141 intersections.swap();
142 --depth;
143 return intersections.intersected();
144 }
145 bool result = intersect(minT1, maxT1, minT2, maxT2);
146 intersections.swap();
147 --depth;
148 return result;
149}
150
151private:
152
caryclark@google.comc6825902012-02-03 22:07:47 +0000153const Cubic& cubic1;
154const Cubic& cubic2;
155Intersections& intersections;
156int depth;
157int splits;
158};
159
160bool intersect(const Cubic& c1, const Cubic& c2, Intersections& i) {
161 CubicIntersections c(c1, c2, i);
162 return c.intersect();
163}
164
caryclark@google.com73ca6242013-01-17 21:02:47 +0000165#include "CubicUtilities.h"
166
caryclark@google.com9f602912013-01-24 21:47:16 +0000167static void cubicTangent(const Cubic& cubic, double t, _Line& tangent, _Point& pt, _Point& dxy) {
168 xy_at_t(cubic, t, tangent[0].x, tangent[0].y);
169 pt = tangent[1] = tangent[0];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000170 dxdy_at_t(cubic, t, dxy);
caryclark@google.com9f602912013-01-24 21:47:16 +0000171 tangent[0] -= dxy;
172 tangent[1] += dxy;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000173}
174
caryclark@google.com9f602912013-01-24 21:47:16 +0000175static double cubicDelta(const _Point& dxy, _Line& tangent, double scale) {
176 double tangentLen = dxy.length();
177 tangent[0] -= tangent[1];
178 double intersectLen = tangent[0].length();
179 double result = intersectLen / tangentLen + scale;
180 return result;
181}
182
183// FIXME: after testing, make this static
184void computeDelta(const Cubic& c1, double t1, double scale1, const Cubic& c2, double t2,
185 double scale2, double& delta1, double& delta2) {
186 _Line tangent1, tangent2, line1, line2;
187 _Point dxy1, dxy2;
188 cubicTangent(c1, t1, line1, tangent1[0], dxy1);
189 cubicTangent(c2, t2, line2, tangent2[0], dxy2);
190 double range1[2], range2[2];
191 int found = intersect(line1, line2, range1, range2);
192 if (found == 0) {
193 range1[0] = 0.5;
194 } else {
195 SkASSERT(found == 1);
196 }
197 xy_at_t(line1, range1[0], tangent1[1].x, tangent1[1].y);
198#if SK_DEBUG
199 if (found == 1) {
200 xy_at_t(line2, range2[0], tangent2[1].x, tangent2[1].y);
201 SkASSERT(tangent2[1].approximatelyEqual(tangent1[1]));
202 }
203#endif
204 tangent2[1] = tangent1[1];
205 delta1 = cubicDelta(dxy1, tangent1, scale1 / precisionUnit);
206 delta2 = cubicDelta(dxy2, tangent2, scale2 / precisionUnit);
207}
208
209int debugDepth;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000210// this flavor approximates the cubics with quads to find the intersecting ts
211// OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used
212// to create the approximations, could be stored in the cubic segment
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000213// FIXME: this strategy needs to intersect the convex hull on either end with the opposite to
214// account for inset quadratics that cause the endpoint intersection to avoid detection
215// the segments can be very short -- the length of the maximum quadratic error (precision)
216// FIXME: this needs to recurse on itself, taking a range of T values and computing the new
217// t range ala is linear inner. The range can be figured by taking the dx/dy and determining
218// the fraction that matches the precision. That fraction is the change in t for the smaller cubic.
219static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
220 double t2s, double t2e, Intersections& i) {
221 Cubic c1, c2;
222 sub_divide(cubic1, t1s, t1e, c1);
223 sub_divide(cubic2, t2s, t2e, c2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000224 SkTDArray<double> ts1;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000225 cubic_to_quadratics(c1, calcPrecision(c1), ts1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000226 SkTDArray<double> ts2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000227 cubic_to_quadratics(c2, calcPrecision(c2), ts2);
228 double t1Start = t1s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000229 int ts1Count = ts1.count();
230 for (int i1 = 0; i1 <= ts1Count; ++i1) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000231 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
232 const double t1 = t1s + (t1e - t1s) * tEnd1;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000233 Cubic part1;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000234 sub_divide(cubic1, t1Start, t1, part1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000235 Quadratic q1;
236 demote_cubic_to_quad(part1, q1);
237 // start here;
238 // should reduceOrder be looser in this use case if quartic is going to blow up on an
239 // extremely shallow quadratic?
caryclark@google.com73ca6242013-01-17 21:02:47 +0000240 Quadratic s1;
241 int o1 = reduceOrder(q1, s1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000242 double t2Start = t2s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000243 int ts2Count = ts2.count();
244 for (int i2 = 0; i2 <= ts2Count; ++i2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000245 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
246 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000247 Cubic part2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000248 sub_divide(cubic2, t2Start, t2, part2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000249 Quadratic q2;
250 demote_cubic_to_quad(part2, q2);
251 Quadratic s2;
252 double o2 = reduceOrder(q2, s2);
253 Intersections locals;
254 if (o1 == 3 && o2 == 3) {
255 intersect2(q1, q2, locals);
256 } else if (o1 <= 2 && o2 <= 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000257 locals.fUsed = intersect((const _Line&) s1, (const _Line&) s2, locals.fT[0],
258 locals.fT[1]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000259 } else if (o1 == 3 && o2 <= 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000260 intersect(q1, (const _Line&) s2, locals);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000261 } else {
262 SkASSERT(o1 <= 2 && o2 == 3);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000263 intersect(q2, (const _Line&) s1, locals);
264 for (int s = 0; s < locals.fUsed; ++s) {
265 SkTSwap(locals.fT[0][s], locals.fT[1][s]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000266 }
267 }
268 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
269 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
270 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000271 // if the computed t is not sufficiently precise, iterate
272 _Point p1, p2;
273 xy_at_t(cubic1, to1, p1.x, p1.y);
274 xy_at_t(cubic2, to2, p2.x, p2.y);
275 if (p1.approximatelyEqual(p2)) {
276 i.insert(i.swapped() ? to2 : to1, i.swapped() ? to1 : to2);
277 } else {
caryclark@google.com9f602912013-01-24 21:47:16 +0000278 double dt1, dt2;
279 computeDelta(cubic1, to1, (t1e - t1s), cubic2, to2, (t2e - t2s), dt1, dt2);
280 ++debugDepth;
281 assert(debugDepth < 10);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000282 i.swap();
caryclark@google.com9f602912013-01-24 21:47:16 +0000283 intersect2(cubic2, SkTMax(to2 - dt2, 0.), SkTMin(to2 + dt2, 1.),
284 cubic1, SkTMax(to1 - dt1, 0.), SkTMin(to1 + dt1, 1.), i);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000285 i.swap();
caryclark@google.com9f602912013-01-24 21:47:16 +0000286 --debugDepth;
287
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000288 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000289 }
290 t2Start = t2;
291 }
292 t1Start = t1;
293 }
294 return i.intersected();
295}
296
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000297static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
298 Intersections& i) {
299 _Line line1;
300 line1[0] = line1[1] = cubic1[start ? 0 : 3];
301 _Point dxy1 = line1[0] - cubic1[start ? 1 : 2];
302 dxy1 /= precisionUnit;
303 line1[1] += dxy1;
304 _Rect line1Bounds;
305 line1Bounds.setBounds(line1);
306 if (!bounds2.intersects(line1Bounds)) {
307 return false;
308 }
309 _Line line2;
310 line2[0] = line2[1] = line1[0];
311 _Point dxy2 = line2[0] - cubic1[start ? 3 : 0];
312 dxy2 /= precisionUnit;
313 line2[1] += dxy2;
314#if 0 // this is so close to the first bounds test it isn't worth the short circuit test
315 _Rect line2Bounds;
316 line2Bounds.setBounds(line2);
317 if (!bounds2.intersects(line2Bounds)) {
318 return false;
319 }
320#endif
321 Intersections local1;
322 if (!intersect(cubic2, line1, local1)) {
323 return false;
324 }
325 Intersections local2;
326 if (!intersect(cubic2, line2, local2)) {
327 return false;
328 }
329 double tMin, tMax;
330 tMin = tMax = local1.fT[0][0];
331 for (int index = 1; index < local1.fUsed; ++index) {
332 tMin = std::min(tMin, local1.fT[0][index]);
333 tMax = std::max(tMax, local1.fT[0][index]);
334 }
335 for (int index = 1; index < local2.fUsed; ++index) {
336 tMin = std::min(tMin, local2.fT[0][index]);
337 tMax = std::max(tMax, local2.fT[0][index]);
338 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000339 debugDepth = 0;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000340 return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit,
341 cubic2, tMin, tMax, i);
342}
343
344// FIXME: add intersection of convex null on cubics' ends with the opposite cubic. The hull line
345// segments can be constructed to be only as long as the calculated precision suggests. If the hull
346// line segments intersect the cubic, then use the intersections to construct a subdivision for
347// quadratic curve fitting.
348bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000349 debugDepth = 0;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000350 bool result = intersect2(c1, 0, 1, c2, 0, 1, i);
351 // FIXME: pass in cached bounds from caller
352 _Rect c1Bounds, c2Bounds;
353 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
354 c2Bounds.setBounds(c2);
355 result |= intersectEnd(c1, false, c2, c2Bounds, i);
356 result |= intersectEnd(c1, true, c2, c2Bounds, i);
357 result |= intersectEnd(c2, false, c1, c1Bounds, i);
358 result |= intersectEnd(c2, true, c1, c1Bounds, i);
359 return result;
360}
361
caryclark@google.com73ca6242013-01-17 21:02:47 +0000362int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
363 SkTDArray<double> ts;
364 double precision = calcPrecision(cubic);
365 cubic_to_quadratics(cubic, precision, ts);
366 double tStart = 0;
367 Cubic part;
368 int tsCount = ts.count();
369 for (int idx = 0; idx <= tsCount; ++idx) {
370 double t = idx < tsCount ? ts[idx] : 1;
371 Quadratic q1;
372 sub_divide(cubic, tStart, t, part);
373 demote_cubic_to_quad(part, q1);
374 Intersections locals;
375 intersect2(q1, quad, locals);
376 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
377 double globalT = tStart + (t - tStart) * locals.fT[0][tIdx];
378 i.insertOne(globalT, 0);
379 globalT = locals.fT[1][tIdx];
380 i.insertOne(globalT, 1);
381 }
382 tStart = t;
383 }
384 return i.used();
385}
386
387bool intersect(const Cubic& cubic, Intersections& i) {
388 SkTDArray<double> ts;
389 double precision = calcPrecision(cubic);
390 cubic_to_quadratics(cubic, precision, ts);
391 int tsCount = ts.count();
392 if (tsCount == 1) {
393 return false;
394 }
395 double t1Start = 0;
396 Cubic part;
397 for (int idx = 0; idx < tsCount; ++idx) {
398 double t1 = ts[idx];
399 Quadratic q1;
400 sub_divide(cubic, t1Start, t1, part);
401 demote_cubic_to_quad(part, q1);
402 double t2Start = t1;
403 for (int i2 = idx + 1; i2 <= tsCount; ++i2) {
404 const double t2 = i2 < tsCount ? ts[i2] : 1;
405 Quadratic q2;
406 sub_divide(cubic, t2Start, t2, part);
407 demote_cubic_to_quad(part, q2);
408 Intersections locals;
409 intersect2(q1, q2, locals);
410 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
411 // discard intersections at cusp? (maximum curvature)
412 double t1sect = locals.fT[0][tIdx];
413 double t2sect = locals.fT[1][tIdx];
414 if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) {
415 continue;
416 }
417 double to1 = t1Start + (t1 - t1Start) * t1sect;
418 double to2 = t2Start + (t2 - t2Start) * t2sect;
419 i.insert(to1, to2);
420 }
421 t2Start = t2;
422 }
423 t1Start = t1;
424 }
425 return i.intersected();
426}