blob: f7b66d540efe2cabe9e0ac0b2bd9eafa847fcee8 [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.com4aaaaea2013-02-28 16:12:39 +000017static const double tLimits[2][2] = {{0.134, 0.145}, {0.134, 0.136}};
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
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000069// this flavor centers potential intersections recursively. In contrast, '2' may inadvertently
caryclark@google.com45a8fc62013-02-14 15:29:11 +000070// chase intersections near quadratic ends, requiring odd hacks to find them.
71static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2,
72 double t2s, double t2e, double precisionScale, Intersections& i) {
73 i.upDepth();
74 bool result = false;
75 Cubic c1, c2;
76 sub_divide(cubic1, t1s, t1e, c1);
77 sub_divide(cubic2, t2s, t2e, c2);
78 SkTDArray<double> ts1;
79 cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1);
80 SkTDArray<double> ts2;
81 cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2);
82 double t1Start = t1s;
83 int ts1Count = ts1.count();
84 for (int i1 = 0; i1 <= ts1Count; ++i1) {
85 const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1;
86 const double t1 = t1s + (t1e - t1s) * tEnd1;
87 Quadratic s1;
88 int o1 = quadPart(cubic1, t1Start, t1, s1);
89 double t2Start = t2s;
90 int ts2Count = ts2.count();
91 for (int i2 = 0; i2 <= ts2Count; ++i2) {
92 const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1;
93 const double t2 = t2s + (t2e - t2s) * tEnd2;
caryclark@google.comc83c70e2013-02-22 21:50:07 +000094 if (cubic1 == cubic2 && t1Start >= t2Start) {
95 t2Start = t2;
96 continue;
97 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +000098 Quadratic s2;
99 int o2 = quadPart(cubic2, t2Start, t2, s2);
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000100 #if ONE_OFF_DEBUG
101 if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1
102 && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) {
103 Cubic cSub1, cSub2;
104 sub_divide(cubic1, t1Start, tEnd1, cSub1);
105 sub_divide(cubic2, t2Start, tEnd2, cSub2);
106 SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n",
107 t1Start, t1, t2Start, t2);
108 Intersections xlocals;
109 intersectWithOrder(s1, o1, s2, o2, xlocals);
110 SkDebugf("xlocals.fUsed=%d\n", xlocals.used());
111 }
112 #endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000113 Intersections locals;
114 intersectWithOrder(s1, o1, s2, o2, locals);
115 double coStart[2] = { -1 };
116 _Point coPoint;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000117 int tCount = locals.used();
118 for (int tIdx = 0; tIdx < tCount; ++tIdx) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000119 double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx];
120 double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx];
121 // if the computed t is not sufficiently precise, iterate
122 _Point p1, p2;
123 xy_at_t(cubic1, to1, p1.x, p1.y);
124 xy_at_t(cubic2, to2, p2.x, p2.y);
125 if (p1.approximatelyEqual(p2)) {
126 if (locals.fIsCoincident[0] & 1 << tIdx) {
127 if (coStart[0] < 0) {
128 coStart[0] = to1;
129 coStart[1] = to2;
130 coPoint = p1;
131 } else {
132 i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1);
133 coStart[0] = -1;
134 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000135 result = true;
136 } else if (cubic1 != cubic2 || !approximately_equal(to1, to2)) {
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000137 if (i.swapped()) { // FIXME: insert should respect swap
138 i.insert(to2, to1, p1);
139 } else {
140 i.insert(to1, to2, p1);
141 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000142 result = true;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000143 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000144 } else {
145 double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000146 double c1Bottom = tIdx == 0 ? 0 :
147 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx - 1] + to1) / 2;
148 double c1Min = SkTMax(c1Bottom, to1 - offset);
149 double c1Top = tIdx == tCount - 1 ? 1 :
150 (t1Start + (t1 - t1Start) * locals.fT[0][tIdx + 1] + to1) / 2;
151 double c1Max = SkTMin(c1Top, to1 + offset);
152 double c2Bottom = tIdx == 0 ? to2 :
153 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx - 1] + to2) / 2;
154 double c2Top = tIdx == tCount - 1 ? to2 :
155 (t2Start + (t2 - t2Start) * locals.fT[1][tIdx + 1] + to2) / 2;
156 if (c2Bottom > c2Top) {
157 SkTSwap(c2Bottom, c2Top);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000158 }
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000159 if (c2Bottom == to2) {
160 c2Bottom = 0;
161 }
162 if (c2Top == to2) {
163 c2Top = 1;
164 }
165 double c2Min = SkTMax(c2Bottom, to2 - offset);
166 double c2Max = SkTMin(c2Top, to2 + offset);
167 intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i);
168 // TODO: if no intersection is found, either quadratics intersected where
169 // cubics did not, or the intersection was missed. In the former case, expect
170 // the quadratics to be nearly parallel at the point of intersection, and check
171 // for that.
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000172 }
173 }
174 SkASSERT(coStart[0] == -1);
175 t2Start = t2;
176 }
177 t1Start = t1;
178 }
179 i.downDepth();
180 return result;
181}
182
skia.committer@gmail.com12eea2b2013-02-27 07:10:10 +0000183// intersect the end of the cubic with the other. Try lines from the end to control and opposite
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000184// end to determine range of t on opposite cubic.
185static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2,
186 Intersections& i) {
187 _Line line;
188 int t1Index = start ? 0 : 3;
189 line[0] = cubic1[t1Index];
190 // don't bother if the two cubics are connnected
191 if (line[0].approximatelyEqual(cubic2[0]) || line[0].approximatelyEqual(cubic2[3])) {
192 return false;
193 }
194 double tMin = 1, tMax = 0;
195 for (int index = 0; index < 4; ++index) {
196 if (index == t1Index) {
197 continue;
198 }
199 _Vector dxy1 = cubic1[index] - line[0];
200 dxy1 /= gPrecisionUnit;
201 line[1] = line[0] + dxy1;
202 _Rect lineBounds;
203 lineBounds.setBounds(line);
204 if (!bounds2.intersects(lineBounds)) {
205 continue;
206 }
207 Intersections local;
208 if (!intersect(cubic2, line, local)) {
209 continue;
210 }
211 for (int index = 0; index < local.fUsed; ++index) {
212 tMin = SkTMin(tMin, local.fT[0][index]);
213 tMax = SkTMax(tMax, local.fT[0][index]);
214 }
215 }
216 if (tMin > tMax) {
217 return false;
218 }
219 double tMin1 = start ? 0 : 1 - 1.0 / gPrecisionUnit;
220 double tMax1 = start ? 1.0 / gPrecisionUnit : 1;
221 double tMin2 = SkTMax(tMin - 1.0 / gPrecisionUnit, 0.0);
222 double tMax2 = SkTMin(tMax + 1.0 / gPrecisionUnit, 1.0);
223 return intersect3(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i);
224}
225
caryclark@google.com47d73da2013-02-17 01:41:25 +0000226const double CLOSE_ENOUGH = 0.001;
skia.committer@gmail.come7707c22013-02-17 07:02:20 +0000227
caryclark@google.com47d73da2013-02-17 01:41:25 +0000228static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
229 if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) {
230 return false;
231 }
232 pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2);
233 return true;
234}
235
236static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) {
237 int last = i.used() - 1;
238 if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) {
239 return false;
240 }
241 pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2);
242 return true;
243}
244
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000245bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) {
246 bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i);
247 // FIXME: pass in cached bounds from caller
248 _Rect c1Bounds, c2Bounds;
249 c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ?
250 c2Bounds.setBounds(c2);
251 result |= intersectEnd(c1, false, c2, c2Bounds, i);
252 result |= intersectEnd(c1, true, c2, c2Bounds, i);
253 i.swap();
254 result |= intersectEnd(c2, false, c1, c1Bounds, i);
255 result |= intersectEnd(c2, true, c1, c1Bounds, i);
256 i.swap();
caryclark@google.com47d73da2013-02-17 01:41:25 +0000257 // If an end point and a second point very close to the end is returned, the second
258 // point may have been detected because the approximate quads
259 // intersected at the end and close to it. Verify that the second point is valid.
260 if (i.used() <= 1 || i.coincidentUsed()) {
261 return result;
262 }
263 _Point pt[2];
264 if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1])
265 && pt[0].approximatelyEqual(pt[1])) {
266 i.removeOne(1);
267 }
268 if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1])
269 && pt[0].approximatelyEqual(pt[1])) {
270 i.removeOne(i.used() - 2);
271 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000272 return result;
273}
274
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000275// Up promote the quad to a cubic.
276// OPTIMIZATION If this is a common use case, optimize by duplicating
277// the intersect 3 loop to avoid the promotion / demotion code
caryclark@google.com73ca6242013-01-17 21:02:47 +0000278int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) {
caryclark@google.comd0a19eb2013-02-19 12:49:33 +0000279 Cubic up;
280 toCubic(quad, up);
281 (void) intersect3(cubic, up, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000282 return i.used();
283}
284
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000285/* http://www.ag.jku.at/compass/compasssample.pdf
286( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen
287Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no
288SINTEF Applied Mathematics http://www.sintef.no )
289describes a method to find the self intersection of a cubic by taking the gradient of the implicit
290form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/
291
292int intersect(const Cubic& c, Intersections& i) {
293 // check to see if x or y end points are the extrema. Are other quick rejects possible?
294 if ((between(c[0].x, c[1].x, c[3].x) && between(c[0].x, c[2].x, c[3].x))
295 || (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 +0000296 return false;
297 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000298 (void) intersect3(c, c, i);
299 return i.used();
caryclark@google.com73ca6242013-01-17 21:02:47 +0000300}