blob: 4ae0d84e5faef28eaf1808b1aa33d58b1bddc29f [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"
13
caryclark@google.com0d3d09e2012-12-10 14:50:04 +000014static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections
15
caryclark@google.comc6825902012-02-03 22:07:47 +000016class CubicIntersections : public Intersections {
17public:
18
rmistry@google.comd6176b02012-08-23 18:14:13 +000019CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i)
caryclark@google.comc6825902012-02-03 22:07:47 +000020 : cubic1(c1)
21 , cubic2(c2)
22 , intersections(i)
rmistry@google.comd6176b02012-08-23 18:14:13 +000023 , depth(0)
caryclark@google.comc6825902012-02-03 22:07:47 +000024 , splits(0) {
caryclark@google.com639df892012-01-10 21:46:10 +000025}
26
caryclark@google.comc6825902012-02-03 22:07:47 +000027bool intersect() {
caryclark@google.com639df892012-01-10 21:46:10 +000028 double minT1, minT2, maxT1, maxT2;
29 if (!bezier_clip(cubic2, cubic1, minT1, maxT1)) {
30 return false;
31 }
32 if (!bezier_clip(cubic1, cubic2, minT2, maxT2)) {
33 return false;
34 }
caryclark@google.comc6825902012-02-03 22:07:47 +000035 int split;
caryclark@google.com639df892012-01-10 21:46:10 +000036 if (maxT1 - minT1 < maxT2 - minT2) {
37 intersections.swap();
caryclark@google.comc6825902012-02-03 22:07:47 +000038 minT2 = 0;
39 maxT2 = 1;
40 split = maxT1 - minT1 > tClipLimit;
41 } else {
42 minT1 = 0;
43 maxT1 = 1;
44 split = (maxT2 - minT2 > tClipLimit) << 1;
45 }
46 return chop(minT1, maxT1, minT2, maxT2, split);
caryclark@google.com639df892012-01-10 21:46:10 +000047}
caryclark@google.comc6825902012-02-03 22:07:47 +000048
49protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +000050
caryclark@google.comc6825902012-02-03 22:07:47 +000051bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
52 Cubic smaller, larger;
rmistry@google.comd6176b02012-08-23 18:14:13 +000053 // FIXME: carry last subdivide and reduceOrder result with cubic
caryclark@google.comc6825902012-02-03 22:07:47 +000054 sub_divide(cubic1, minT1, maxT1, intersections.swapped() ? larger : smaller);
55 sub_divide(cubic2, minT2, maxT2, intersections.swapped() ? smaller : larger);
56 Cubic smallResult;
57 if (reduceOrder(smaller, smallResult,
58 kReduceOrder_NoQuadraticsAllowed) <= 2) {
59 Cubic largeResult;
60 if (reduceOrder(larger, largeResult,
61 kReduceOrder_NoQuadraticsAllowed) <= 2) {
62 const _Line& smallLine = (const _Line&) smallResult;
63 const _Line& largeLine = (const _Line&) largeResult;
64 double smallT[2];
65 double largeT[2];
66 // FIXME: this doesn't detect or deal with coincident lines
67 if (!::intersect(smallLine, largeLine, smallT, largeT)) {
68 return false;
69 }
70 if (intersections.swapped()) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000071 smallT[0] = interp(minT2, maxT2, smallT[0]);
72 largeT[0] = interp(minT1, maxT1, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000073 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +000074 smallT[0] = interp(minT1, maxT1, smallT[0]);
75 largeT[0] = interp(minT2, maxT2, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000076 }
77 intersections.add(smallT[0], largeT[0]);
78 return true;
79 }
80 }
81 double minT, maxT;
82 if (!bezier_clip(smaller, larger, minT, maxT)) {
83 if (minT == maxT) {
84 if (intersections.swapped()) {
85 minT1 = (minT1 + maxT1) / 2;
86 minT2 = interp(minT2, maxT2, minT);
87 } else {
88 minT1 = interp(minT1, maxT1, minT);
89 minT2 = (minT2 + maxT2) / 2;
90 }
91 intersections.add(minT1, minT2);
92 return true;
93 }
94 return false;
95 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000096
caryclark@google.comc6825902012-02-03 22:07:47 +000097 int split;
98 if (intersections.swapped()) {
99 double newMinT1 = interp(minT1, maxT1, minT);
100 double newMaxT1 = interp(minT1, maxT1, maxT);
101 split = (newMaxT1 - newMinT1 > (maxT1 - minT1) * tClipLimit) << 1;
102#define VERBOSE 0
103#if VERBOSE
104 printf("%s d=%d s=%d new1=(%g,%g) old1=(%g,%g) split=%d\n",
105 __FUNCTION__, depth, splits, newMinT1, newMaxT1, minT1, maxT1,
106 split);
107#endif
108 minT1 = newMinT1;
109 maxT1 = newMaxT1;
110 } else {
111 double newMinT2 = interp(minT2, maxT2, minT);
112 double newMaxT2 = interp(minT2, maxT2, maxT);
113 split = newMaxT2 - newMinT2 > (maxT2 - minT2) * tClipLimit;
114#if VERBOSE
115 printf("%s d=%d s=%d new2=(%g,%g) old2=(%g,%g) split=%d\n",
116 __FUNCTION__, depth, splits, newMinT2, newMaxT2, minT2, maxT2,
117 split);
118#endif
119 minT2 = newMinT2;
120 maxT2 = newMaxT2;
121 }
122 return chop(minT1, maxT1, minT2, maxT2, split);
123}
124
125bool chop(double minT1, double maxT1, double minT2, double maxT2, int split) {
126 ++depth;
127 intersections.swap();
128 if (split) {
129 ++splits;
130 if (split & 2) {
131 double middle1 = (maxT1 + minT1) / 2;
132 intersect(minT1, middle1, minT2, maxT2);
133 intersect(middle1, maxT1, minT2, maxT2);
134 } else {
135 double middle2 = (maxT2 + minT2) / 2;
136 intersect(minT1, maxT1, minT2, middle2);
137 intersect(minT1, maxT1, middle2, maxT2);
138 }
139 --splits;
140 intersections.swap();
141 --depth;
142 return intersections.intersected();
143 }
144 bool result = intersect(minT1, maxT1, minT2, maxT2);
145 intersections.swap();
146 --depth;
147 return result;
148}
149
150private:
151
caryclark@google.comc6825902012-02-03 22:07:47 +0000152const Cubic& cubic1;
153const Cubic& cubic2;
154Intersections& intersections;
155int depth;
156int splits;
157};
158
159bool intersect(const Cubic& c1, const Cubic& c2, Intersections& i) {
160 CubicIntersections c(c1, c2, i);
161 return c.intersect();
162}
163
caryclark@google.com73ca6242013-01-17 21:02:47 +0000164#include "CubicUtilities.h"
165
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000166// FIXME: ? if needed, compute the error term from the tangent length
caryclark@google.com9d5f99b2013-01-22 12:55:54 +0000167start here;
168// need better delta computation -- assert fails
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000169static double computeDelta(const Cubic& cubic, double t, double scale) {
caryclark@google.com9d5f99b2013-01-22 12:55:54 +0000170 double attempt = scale / precisionUnit * 2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000171#if SK_DEBUG
caryclark@google.com9d5f99b2013-01-22 12:55:54 +0000172 double precision = calcPrecision(cubic, t, scale);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000173 _Point dxy;
174 dxdy_at_t(cubic, t, dxy);
175 _Point p1, p2;
176 xy_at_t(cubic, std::max(t - attempt, 0.), p1.x, p1.y);
177 xy_at_t(cubic, std::min(t + attempt, 1.), p2.x, p2.y);
178 double dx = p1.x - p2.x;
179 double dy = p1.y - p2.y;
180 double distSq = dx * dx + dy * dy;
181 double dist = sqrt(distSq);
182 assert(dist > precision);
183#endif
184 return attempt;
185}
186
caryclark@google.com73ca6242013-01-17 21:02:47 +0000187// this flavor approximates the cubics with quads to find the intersecting ts
188// OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used
189// to create the approximations, could be stored in the cubic segment
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000190// FIXME: this strategy needs to intersect the convex hull on either end with the opposite to
191// account for inset quadratics that cause the endpoint intersection to avoid detection
192// the segments can be very short -- the length of the maximum quadratic error (precision)
193// FIXME: this needs to recurse on itself, taking a range of T values and computing the new
194// t range ala is linear inner. The range can be figured by taking the dx/dy and determining
195// the fraction that matches the precision. That fraction is the change in t for the smaller cubic.
196static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
197 double t2s, double t2e, Intersections& i) {
198 Cubic c1, c2;
199 sub_divide(cubic1, t1s, t1e, c1);
200 sub_divide(cubic2, t2s, t2e, c2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000201 SkTDArray<double> ts1;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000202 cubic_to_quadratics(c1, calcPrecision(c1), ts1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000203 SkTDArray<double> ts2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000204 cubic_to_quadratics(c2, calcPrecision(c2), ts2);
205 double t1Start = t1s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000206 int ts1Count = ts1.count();
207 for (int i1 = 0; i1 <= ts1Count; ++i1) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000208 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
209 const double t1 = t1s + (t1e - t1s) * tEnd1;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000210 Cubic part1;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000211 sub_divide(cubic1, t1Start, t1, part1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000212 Quadratic q1;
213 demote_cubic_to_quad(part1, q1);
214 // start here;
215 // should reduceOrder be looser in this use case if quartic is going to blow up on an
216 // extremely shallow quadratic?
caryclark@google.com73ca6242013-01-17 21:02:47 +0000217 Quadratic s1;
218 int o1 = reduceOrder(q1, s1);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000219 double t2Start = t2s;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000220 int ts2Count = ts2.count();
221 for (int i2 = 0; i2 <= ts2Count; ++i2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000222 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
223 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000224 Cubic part2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000225 sub_divide(cubic2, t2Start, t2, part2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000226 Quadratic q2;
227 demote_cubic_to_quad(part2, q2);
228 Quadratic s2;
229 double o2 = reduceOrder(q2, s2);
230 Intersections locals;
231 if (o1 == 3 && o2 == 3) {
232 intersect2(q1, q2, locals);
233 } else if (o1 <= 2 && o2 <= 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000234 locals.fUsed = intersect((const _Line&) s1, (const _Line&) s2, locals.fT[0],
235 locals.fT[1]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000236 } else if (o1 == 3 && o2 <= 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000237 intersect(q1, (const _Line&) s2, locals);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000238 } else {
239 SkASSERT(o1 <= 2 && o2 == 3);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000240 intersect(q2, (const _Line&) s1, locals);
241 for (int s = 0; s < locals.fUsed; ++s) {
242 SkTSwap(locals.fT[0][s], locals.fT[1][s]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000243 }
244 }
245 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
246 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
247 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000248 // if the computed t is not sufficiently precise, iterate
249 _Point p1, p2;
250 xy_at_t(cubic1, to1, p1.x, p1.y);
251 xy_at_t(cubic2, to2, p2.x, p2.y);
252 if (p1.approximatelyEqual(p2)) {
253 i.insert(i.swapped() ? to2 : to1, i.swapped() ? to1 : to2);
254 } else {
255 double dt1 = computeDelta(cubic1, to1, t1e - t1s);
256 double dt2 = computeDelta(cubic2, to2, t2e - t2s);
257 i.swap();
258 intersect2(cubic2, std::max(to2 - dt2, 0.), std::min(to2 + dt2, 1.),
259 cubic1, std::max(to1 - dt1, 0.), std::min(to1 + dt1, 1.), i);
260 i.swap();
261 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000262 }
263 t2Start = t2;
264 }
265 t1Start = t1;
266 }
267 return i.intersected();
268}
269
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000270static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
271 Intersections& i) {
272 _Line line1;
273 line1[0] = line1[1] = cubic1[start ? 0 : 3];
274 _Point dxy1 = line1[0] - cubic1[start ? 1 : 2];
275 dxy1 /= precisionUnit;
276 line1[1] += dxy1;
277 _Rect line1Bounds;
278 line1Bounds.setBounds(line1);
279 if (!bounds2.intersects(line1Bounds)) {
280 return false;
281 }
282 _Line line2;
283 line2[0] = line2[1] = line1[0];
284 _Point dxy2 = line2[0] - cubic1[start ? 3 : 0];
285 dxy2 /= precisionUnit;
286 line2[1] += dxy2;
287#if 0 // this is so close to the first bounds test it isn't worth the short circuit test
288 _Rect line2Bounds;
289 line2Bounds.setBounds(line2);
290 if (!bounds2.intersects(line2Bounds)) {
291 return false;
292 }
293#endif
294 Intersections local1;
295 if (!intersect(cubic2, line1, local1)) {
296 return false;
297 }
298 Intersections local2;
299 if (!intersect(cubic2, line2, local2)) {
300 return false;
301 }
302 double tMin, tMax;
303 tMin = tMax = local1.fT[0][0];
304 for (int index = 1; index < local1.fUsed; ++index) {
305 tMin = std::min(tMin, local1.fT[0][index]);
306 tMax = std::max(tMax, local1.fT[0][index]);
307 }
308 for (int index = 1; index < local2.fUsed; ++index) {
309 tMin = std::min(tMin, local2.fT[0][index]);
310 tMax = std::max(tMax, local2.fT[0][index]);
311 }
312 return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit,
313 cubic2, tMin, tMax, i);
314}
315
316// FIXME: add intersection of convex null on cubics' ends with the opposite cubic. The hull line
317// segments can be constructed to be only as long as the calculated precision suggests. If the hull
318// line segments intersect the cubic, then use the intersections to construct a subdivision for
319// quadratic curve fitting.
320bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) {
321 bool result = intersect2(c1, 0, 1, c2, 0, 1, i);
322 // FIXME: pass in cached bounds from caller
323 _Rect c1Bounds, c2Bounds;
324 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
325 c2Bounds.setBounds(c2);
326 result |= intersectEnd(c1, false, c2, c2Bounds, i);
327 result |= intersectEnd(c1, true, c2, c2Bounds, i);
328 result |= intersectEnd(c2, false, c1, c1Bounds, i);
329 result |= intersectEnd(c2, true, c1, c1Bounds, i);
330 return result;
331}
332
caryclark@google.com73ca6242013-01-17 21:02:47 +0000333int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
334 SkTDArray<double> ts;
335 double precision = calcPrecision(cubic);
336 cubic_to_quadratics(cubic, precision, ts);
337 double tStart = 0;
338 Cubic part;
339 int tsCount = ts.count();
340 for (int idx = 0; idx <= tsCount; ++idx) {
341 double t = idx < tsCount ? ts[idx] : 1;
342 Quadratic q1;
343 sub_divide(cubic, tStart, t, part);
344 demote_cubic_to_quad(part, q1);
345 Intersections locals;
346 intersect2(q1, quad, locals);
347 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
348 double globalT = tStart + (t - tStart) * locals.fT[0][tIdx];
349 i.insertOne(globalT, 0);
350 globalT = locals.fT[1][tIdx];
351 i.insertOne(globalT, 1);
352 }
353 tStart = t;
354 }
355 return i.used();
356}
357
358bool intersect(const Cubic& cubic, Intersections& i) {
359 SkTDArray<double> ts;
360 double precision = calcPrecision(cubic);
361 cubic_to_quadratics(cubic, precision, ts);
362 int tsCount = ts.count();
363 if (tsCount == 1) {
364 return false;
365 }
366 double t1Start = 0;
367 Cubic part;
368 for (int idx = 0; idx < tsCount; ++idx) {
369 double t1 = ts[idx];
370 Quadratic q1;
371 sub_divide(cubic, t1Start, t1, part);
372 demote_cubic_to_quad(part, q1);
373 double t2Start = t1;
374 for (int i2 = idx + 1; i2 <= tsCount; ++i2) {
375 const double t2 = i2 < tsCount ? ts[i2] : 1;
376 Quadratic q2;
377 sub_divide(cubic, t2Start, t2, part);
378 demote_cubic_to_quad(part, q2);
379 Intersections locals;
380 intersect2(q1, q2, locals);
381 for (int tIdx = 0; tIdx < locals.used(); ++tIdx) {
382 // discard intersections at cusp? (maximum curvature)
383 double t1sect = locals.fT[0][tIdx];
384 double t2sect = locals.fT[1][tIdx];
385 if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) {
386 continue;
387 }
388 double to1 = t1Start + (t1 - t1Start) * t1sect;
389 double to2 = t2Start + (t2 - t2Start) * t2sect;
390 i.insert(to1, to2);
391 }
392 t2Start = t2;
393 }
394 t1Start = t1;
395 }
396 return i.intersected();
397}