blob: a13e39b2c1bb1735601b11d0a60f4c46f33a8b3f [file] [log] [blame]
caryclark@google.com235f56a2012-09-14 14:19:30 +00001// Another approach is to start with the implicit form of one curve and solve
2// (seek implicit coefficients in QuadraticParameter.cpp
3// by substituting in the parametric form of the other.
4// The downside of this approach is that early rejects are difficult to come by.
5// http://planetmath.org/encyclopedia/GaloisTheoreticDerivationOfTheQuarticFormula.html#step
6
7
caryclark@google.com73ca6242013-01-17 21:02:47 +00008#include "CubicUtilities.h"
caryclark@google.com235f56a2012-09-14 14:19:30 +00009#include "CurveIntersection.h"
10#include "Intersections.h"
11#include "QuadraticParameterization.h"
12#include "QuarticRoot.h"
13#include "QuadraticUtilities.h"
caryclark@google.com73ca6242013-01-17 21:02:47 +000014#include "TSearch.h"
15
caryclark@google.com85ec74c2013-01-28 19:25:51 +000016#if SK_DEBUG
17#include "LineUtilities.h"
18#endif
19
caryclark@google.com235f56a2012-09-14 14:19:30 +000020/* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F
21 * and given x = at^2 + bt + c (the parameterized form)
22 * y = dt^2 + et + f
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000023 * then
caryclark@google.com235f56a2012-09-14 14:19:30 +000024 * 0 = A(at^2+bt+c)(at^2+bt+c)+B(at^2+bt+c)(dt^2+et+f)+C(dt^2+et+f)(dt^2+et+f)+D(at^2+bt+c)+E(dt^2+et+f)+F
25 */
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000026
caryclark@google.com73ca6242013-01-17 21:02:47 +000027static int findRoots(const QuadImplicitForm& i, const Quadratic& q2, double roots[4],
caryclark@google.com5e0500f2013-02-20 12:51:37 +000028 bool oneHint, int firstCubicRoot) {
caryclark@google.com235f56a2012-09-14 14:19:30 +000029 double a, b, c;
30 set_abc(&q2[0].x, a, b, c);
31 double d, e, f;
32 set_abc(&q2[0].y, d, e, f);
33 const double t4 = i.x2() * a * a
34 + i.xy() * a * d
35 + i.y2() * d * d;
36 const double t3 = 2 * i.x2() * a * b
37 + i.xy() * (a * e + b * d)
38 + 2 * i.y2() * d * e;
39 const double t2 = i.x2() * (b * b + 2 * a * c)
40 + i.xy() * (c * d + b * e + a * f)
41 + i.y2() * (e * e + 2 * d * f)
42 + i.x() * a
43 + i.y() * d;
44 const double t1 = 2 * i.x2() * b * c
45 + i.xy() * (c * e + b * f)
46 + 2 * i.y2() * e * f
47 + i.x() * b
48 + i.y() * e;
49 const double t0 = i.x2() * c * c
50 + i.xy() * c * f
51 + i.y2() * f * f
52 + i.x() * c
53 + i.y() * f
54 + i.c();
caryclark@google.com9f602912013-01-24 21:47:16 +000055 int rootCount = reducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots);
56 if (rootCount >= 0) {
57 return rootCount;
caryclark@google.com73ca6242013-01-17 21:02:47 +000058 }
caryclark@google.com5e0500f2013-02-20 12:51:37 +000059 return quarticRootsReal(firstCubicRoot, t4, t3, t2, t1, t0, roots);
caryclark@google.com235f56a2012-09-14 14:19:30 +000060}
61
caryclark@google.com45a8fc62013-02-14 15:29:11 +000062static int addValidRoots(const double roots[4], const int count, double valid[4]) {
63 int result = 0;
caryclark@google.com235f56a2012-09-14 14:19:30 +000064 int index;
65 for (index = 0; index < count; ++index) {
66 if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) {
67 continue;
68 }
69 double t = 1 - roots[index];
70 if (approximately_less_than_zero(t)) {
71 t = 0;
72 } else if (approximately_greater_than_one(t)) {
73 t = 1;
74 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +000075 valid[result++] = t;
caryclark@google.com235f56a2012-09-14 14:19:30 +000076 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +000077 return result;
caryclark@google.com235f56a2012-09-14 14:19:30 +000078}
79
caryclark@google.com6aea33f2012-10-09 14:11:58 +000080static bool onlyEndPtsInCommon(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
81// the idea here is to see at minimum do a quick reject by rotating all points
82// to either side of the line formed by connecting the endpoints
83// if the opposite curves points are on the line or on the other side, the
84// curves at most intersect at the endpoints
85 for (int oddMan = 0; oddMan < 3; ++oddMan) {
86 const _Point* endPt[2];
87 for (int opp = 1; opp < 3; ++opp) {
88 int end = oddMan ^ opp;
89 if (end == 3) {
90 end = opp;
91 }
92 endPt[opp - 1] = &q1[end];
93 }
94 double origX = endPt[0]->x;
95 double origY = endPt[0]->y;
96 double adj = endPt[1]->x - origX;
97 double opp = endPt[1]->y - origY;
98 double sign = (q1[oddMan].y - origY) * adj - (q1[oddMan].x - origX) * opp;
caryclark@google.com73ca6242013-01-17 21:02:47 +000099 if (approximately_zero(sign)) {
100 goto tryNextHalfPlane;
101 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000102 for (int n = 0; n < 3; ++n) {
103 double test = (q2[n].y - origY) * adj - (q2[n].x - origX) * opp;
104 if (test * sign > 0) {
105 goto tryNextHalfPlane;
106 }
107 }
108 for (int i1 = 0; i1 < 3; i1 += 2) {
109 for (int i2 = 0; i2 < 3; i2 += 2) {
110 if (q1[i1] == q2[i2]) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000111 i.insert(i1 >> 1, i2 >> 1, q1[i1]);
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000112 }
113 }
114 }
caryclark@google.comaa358312013-01-29 20:28:49 +0000115 SkASSERT(i.fUsed < 3);
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000116 return true;
117tryNextHalfPlane:
118 ;
119 }
120 return false;
121}
122
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000123// returns false if there's more than one intercept or the intercept doesn't match the point
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000124// returns true if the intercept was successfully added or if the
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000125// original quads need to be subdivided
caryclark@google.com73ca6242013-01-17 21:02:47 +0000126static bool addIntercept(const Quadratic& q1, const Quadratic& q2, double tMin, double tMax,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000127 Intersections& i, bool* subDivide) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000128 double tMid = (tMin + tMax) / 2;
129 _Point mid;
130 xy_at_t(q2, tMid, mid.x, mid.y);
131 _Line line;
132 line[0] = line[1] = mid;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000133 _Point dxdy;
134 dxdy_at_t(q2, tMid, dxdy);
135 line[0].x -= dxdy.x;
136 line[0].y -= dxdy.y;
137 line[1].x += dxdy.x;
138 line[1].y += dxdy.y;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000139 Intersections rootTs;
140 int roots = intersect(q1, line, rootTs);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000141 if (roots == 0) {
142 if (subDivide) {
143 *subDivide = true;
144 }
145 return true;
146 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000147 if (roots == 2) {
148 return false;
149 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000150 _Point pt2;
151 xy_at_t(q1, rootTs.fT[0][0], pt2.x, pt2.y);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000152 if (!pt2.approximatelyEqualHalf(mid)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000153 return false;
154 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000155 i.insertSwap(rootTs.fT[0][0], tMid, pt2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000156 return true;
157}
158
159static bool isLinearInner(const Quadratic& q1, double t1s, double t1e, const Quadratic& q2,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000160 double t2s, double t2e, Intersections& i, bool* subDivide) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000161 Quadratic hull;
162 sub_divide(q1, t1s, t1e, hull);
163 _Line line = {hull[2], hull[0]};
164 const _Line* testLines[] = { &line, (const _Line*) &hull[0], (const _Line*) &hull[1] };
165 size_t testCount = sizeof(testLines) / sizeof(testLines[0]);
166 SkTDArray<double> tsFound;
167 for (size_t index = 0; index < testCount; ++index) {
168 Intersections rootTs;
169 int roots = intersect(q2, *testLines[index], rootTs);
170 for (int idx2 = 0; idx2 < roots; ++idx2) {
171 double t = rootTs.fT[0][idx2];
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000172#if SK_DEBUG
173 _Point qPt, lPt;
174 xy_at_t(q2, t, qPt.x, qPt.y);
175 xy_at_t(*testLines[index], rootTs.fT[1][idx2], lPt.x, lPt.y);
176 SkASSERT(qPt.approximatelyEqual(lPt));
177#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000178 if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
179 continue;
180 }
181 *tsFound.append() = rootTs.fT[0][idx2];
182 }
183 }
184 int tCount = tsFound.count();
185 if (!tCount) {
186 return true;
187 }
188 double tMin, tMax;
189 _Point dxy1, dxy2;
190 if (tCount == 1) {
191 tMin = tMax = tsFound[0];
192 } else if (tCount > 1) {
193 QSort<double>(tsFound.begin(), tsFound.end() - 1);
194 tMin = tsFound[0];
195 tMax = tsFound[1];
196 }
197 _Point end;
198 xy_at_t(q2, t2s, end.x, end.y);
caryclark@google.combeda3892013-02-07 13:13:41 +0000199 bool startInTriangle = point_in_hull(hull, end);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000200 if (startInTriangle) {
201 tMin = t2s;
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000202 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000203 xy_at_t(q2, t2e, end.x, end.y);
caryclark@google.combeda3892013-02-07 13:13:41 +0000204 bool endInTriangle = point_in_hull(hull, end);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000205 if (endInTriangle) {
206 tMax = t2e;
207 }
208 int split = 0;
209 if (tMin != tMax || tCount > 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000210 dxdy_at_t(q2, tMin, dxy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000211 for (int index = 1; index < tCount; ++index) {
212 dxy1 = dxy2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000213 dxdy_at_t(q2, tsFound[index], dxy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000214 double dot = dxy1.dot(dxy2);
215 if (dot < 0) {
216 split = index - 1;
217 break;
218 }
219 }
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000220
caryclark@google.com73ca6242013-01-17 21:02:47 +0000221 }
222 if (split == 0) { // there's one point
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000223 if (addIntercept(q1, q2, tMin, tMax, i, subDivide)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000224 return true;
225 }
226 i.swap();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000227 return isLinearInner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000228 }
229 // At this point, we have two ranges of t values -- treat each separately at the split
230 bool result;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000231 if (addIntercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000232 result = true;
233 } else {
234 i.swap();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000235 result = isLinearInner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000236 }
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000237 if (addIntercept(q1, q2, tsFound[split], tMax, i, subDivide)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000238 result = true;
239 } else {
240 i.swap();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000241 result |= isLinearInner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000242 }
243 return result;
244}
245
246static double flatMeasure(const Quadratic& q) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000247 _Point mid = q[1];
248 mid -= q[0];
249 _Point dxy = q[2];
250 dxy -= q[0];
251 double length = dxy.length(); // OPTIMIZE: get rid of sqrt
252 return fabs(mid.cross(dxy) / length);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000253}
254
255// FIXME ? should this measure both and then use the quad that is the flattest as the line?
256static bool isLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
257 double measure = flatMeasure(q1);
258 // OPTIMIZE: (get rid of sqrt) use approximately_zero
259 if (!approximately_zero_sqrt(measure)) {
260 return false;
261 }
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000262 return isLinearInner(q1, 0, 1, q2, 0, 1, i, NULL);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000263}
264
caryclark@google.com9f602912013-01-24 21:47:16 +0000265// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000266static void relaxedIsLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000267 double m1 = flatMeasure(q1);
268 double m2 = flatMeasure(q2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000269#if SK_DEBUG
270 double min = SkTMin(m1, m2);
271 if (min > 5) {
272 SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min);
273 }
274#endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000275 i.reset();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000276 const Quadratic& rounder = m2 < m1 ? q1 : q2;
277 const Quadratic& flatter = m2 < m1 ? q2 : q1;
278 bool subDivide = false;
279 isLinearInner(flatter, 0, 1, rounder, 0, 1, i, &subDivide);
280 if (subDivide) {
281 QuadraticPair pair;
282 chop_at(flatter, pair, 0.5);
283 Intersections firstI, secondI;
284 relaxedIsLinear(pair.first(), rounder, firstI);
285 for (int index = 0; index < firstI.used(); ++index) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000286 i.insert(firstI.fT[0][index] * 0.5, firstI.fT[1][index], firstI.fPt[index]);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000287 }
288 relaxedIsLinear(pair.second(), rounder, secondI);
289 for (int index = 0; index < secondI.used(); ++index) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000290 i.insert(0.5 + secondI.fT[0][index] * 0.5, secondI.fT[1][index], secondI.fPt[index]);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000291 }
292 }
293 if (m2 < m1) {
294 i.swapPts();
caryclark@google.com73ca6242013-01-17 21:02:47 +0000295 }
296}
297
298#if 0
299static void unsortableExpanse(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
300 const Quadratic* qs[2] = { &q1, &q2 };
301 // need t values for start and end of unsortable expanse on both curves
302 // try projecting lines parallel to the end points
303 i.fT[0][0] = 0;
304 i.fT[0][1] = 1;
305 int flip = -1; // undecided
306 for (int qIdx = 0; qIdx < 2; qIdx++) {
307 for (int t = 0; t < 2; t++) {
308 _Point dxdy;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000309 dxdy_at_t(*qs[qIdx], t, dxdy);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000310 _Line perp;
311 perp[0] = perp[1] = (*qs[qIdx])[t == 0 ? 0 : 2];
312 perp[0].x += dxdy.y;
313 perp[0].y -= dxdy.x;
314 perp[1].x -= dxdy.y;
315 perp[1].y += dxdy.x;
316 Intersections hitData;
317 int hits = intersectRay(*qs[qIdx ^ 1], perp, hitData);
caryclark@google.comaa358312013-01-29 20:28:49 +0000318 SkASSERT(hits <= 1);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000319 if (hits) {
320 if (flip < 0) {
321 _Point dxdy2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000322 dxdy_at_t(*qs[qIdx ^ 1], hitData.fT[0][0], dxdy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000323 double dot = dxdy.dot(dxdy2);
324 flip = dot < 0;
325 i.fT[1][0] = flip;
326 i.fT[1][1] = !flip;
327 }
328 i.fT[qIdx ^ 1][t ^ flip] = hitData.fT[0][0];
329 }
330 }
331 }
332 i.fUnsortable = true; // failed, probably coincident or near-coincident
333 i.fUsed = 2;
334}
335#endif
336
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000337// each time through the loop, this computes values it had from the last loop
338// if i == j == 1, the center values are still good
339// otherwise, for i != 1 or j != 1, four of the values are still good
340// and if i == 1 ^ j == 1, an additional value is good
341static bool binarySearch(const Quadratic& quad1, const Quadratic& quad2, double& t1Seed,
342 double& t2Seed, _Point& pt) {
343 double tStep = ROUGH_EPSILON;
344 _Point t1[3], t2[3];
345 int calcMask = ~0;
346 do {
347 if (calcMask & (1 << 1)) t1[1] = xy_at_t(quad1, t1Seed);
348 if (calcMask & (1 << 4)) t2[1] = xy_at_t(quad2, t2Seed);
349 if (t1[1].approximatelyEqual(t2[1])) {
350 pt = t1[1];
351 #if ONE_OFF_DEBUG
352 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) == (%1.9g,%1.9g)\n", __FUNCTION__,
353 t1Seed, t2Seed, t1[1].x, t1[1].y, t1[2].x, t1[2].y);
354 #endif
355 return true;
356 }
357 if (calcMask & (1 << 0)) t1[0] = xy_at_t(quad1, t1Seed - tStep);
358 if (calcMask & (1 << 2)) t1[2] = xy_at_t(quad1, t1Seed + tStep);
359 if (calcMask & (1 << 3)) t2[0] = xy_at_t(quad2, t2Seed - tStep);
360 if (calcMask & (1 << 5)) t2[2] = xy_at_t(quad2, t2Seed + tStep);
361 double dist[3][3];
362 // OPTIMIZE: using calcMask value permits skipping some distance calcuations
363 // if prior loop's results are moved to correct slot for reuse
364 dist[1][1] = t1[1].distanceSquared(t2[1]);
365 int best_i = 1, best_j = 1;
366 for (int i = 0; i < 3; ++i) {
367 for (int j = 0; j < 3; ++j) {
368 if (i == 1 && j == 1) {
369 continue;
370 }
371 dist[i][j] = t1[i].distanceSquared(t2[j]);
372 if (dist[best_i][best_j] > dist[i][j]) {
373 best_i = i;
374 best_j = j;
375 }
376 }
377 }
378 if (best_i == 1 && best_j == 1) {
379 tStep /= 2;
380 if (tStep < FLT_EPSILON_HALF) {
381 break;
382 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000383 calcMask = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 5);
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000384 continue;
385 }
386 if (best_i == 0) {
387 t1Seed -= tStep;
388 t1[2] = t1[1];
389 t1[1] = t1[0];
390 calcMask = 1 << 0;
391 } else if (best_i == 2) {
392 t1Seed += tStep;
393 t1[0] = t1[1];
394 t1[1] = t1[2];
395 calcMask = 1 << 2;
396 } else {
397 calcMask = 0;
398 }
399 if (best_j == 0) {
400 t2Seed -= tStep;
401 t2[2] = t2[1];
402 t2[1] = t2[0];
403 calcMask |= 1 << 3;
404 } else if (best_j == 2) {
405 t2Seed += tStep;
406 t2[0] = t2[1];
407 t2[1] = t2[2];
408 calcMask |= 1 << 5;
409 }
410 } while (true);
411#if ONE_OFF_DEBUG
412 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) != (%1.9g,%1.9g) %s\n", __FUNCTION__,
413 t1Seed, t2Seed, t1[1].x, t1[1].y, t1[2].x, t1[2].y);
414#endif
415 return false;
416}
417
caryclark@google.com235f56a2012-09-14 14:19:30 +0000418bool intersect2(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000419 // if the quads share an end point, check to see if they overlap
420
421 if (onlyEndPtsInCommon(q1, q2, i)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000422 return i.intersected();
423 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000424 if (onlyEndPtsInCommon(q2, q1, i)) {
425 i.swapPts();
426 return i.intersected();
427 }
428 // see if either quad is really a line
429 if (isLinear(q1, q2, i)) {
430 return i.intersected();
431 }
432 if (isLinear(q2, q1, i)) {
433 i.swapPts();
434 return i.intersected();
435 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000436 QuadImplicitForm i1(q1);
437 QuadImplicitForm i2(q2);
438 if (i1.implicit_match(i2)) {
439 // FIXME: compute T values
440 // compute the intersections of the ends to find the coincident span
441 bool useVertical = fabs(q1[0].x - q1[2].x) < fabs(q1[0].y - q1[2].y);
442 double t;
443 if ((t = axialIntersect(q1, q2[0], useVertical)) >= 0) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000444 i.insertCoincident(t, 0, q2[0]);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000445 }
446 if ((t = axialIntersect(q1, q2[2], useVertical)) >= 0) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000447 i.insertCoincident(t, 1, q2[2]);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000448 }
449 useVertical = fabs(q2[0].x - q2[2].x) < fabs(q2[0].y - q2[2].y);
450 if ((t = axialIntersect(q2, q1[0], useVertical)) >= 0) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000451 i.insertCoincident(0, t, q1[0]);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000452 }
453 if ((t = axialIntersect(q2, q1[2], useVertical)) >= 0) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000454 i.insertCoincident(1, t, q1[2]);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000455 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000456 SkASSERT(i.coincidentUsed() <= 2);
457 return i.coincidentUsed() > 0;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000458 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000459 int index;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000460 bool useCubic = q1[0] == q2[0] || q1[0] == q2[2] || q1[2] == q2[0];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000461 double roots1[4];
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000462 int rootCount = findRoots(i2, q1, roots1, useCubic, 0);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000463 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000464 double roots1Copy[4];
465 int r1Count = addValidRoots(roots1, rootCount, roots1Copy);
466 _Point pts1[4];
467 for (index = 0; index < r1Count; ++index) {
468 xy_at_t(q1, roots1Copy[index], pts1[index].x, pts1[index].y);
469 }
470 double roots2[4];
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000471 int rootCount2 = findRoots(i1, q2, roots2, useCubic, 0);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000472 double roots2Copy[4];
473 int r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
474 _Point pts2[4];
475 for (index = 0; index < r2Count; ++index) {
476 xy_at_t(q2, roots2Copy[index], pts2[index].x, pts2[index].y);
477 }
478 if (r1Count == r2Count && r1Count <= 1) {
479 if (r1Count == 1) {
480 if (pts1[0].approximatelyEqualHalf(pts2[0])) {
481 i.insert(roots1Copy[0], roots2Copy[0], pts1[0]);
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000482 } else if (pts1[0].roughlyEqual(pts2[0])) {
483 // experiment: see if a different cubic solution provides the correct quartic answer
484 #if 0
485 for (int cu1 = 0; cu1 < 3; ++cu1) {
486 rootCount = findRoots(i2, q1, roots1, useCubic, cu1);
487 r1Count = addValidRoots(roots1, rootCount, roots1Copy);
488 if (r1Count == 0) {
489 continue;
490 }
491 for (int cu2 = 0; cu2 < 3; ++cu2) {
492 if (cu1 == 0 && cu2 == 0) {
493 continue;
494 }
495 rootCount2 = findRoots(i1, q2, roots2, useCubic, cu2);
496 r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
497 if (r2Count == 0) {
498 continue;
499 }
500 SkASSERT(r1Count == 1 && r2Count == 1);
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000501 SkDebugf("*** [%d,%d] (%1.9g,%1.9g) %s (%1.9g,%1.9g)\n", cu1, cu2,
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000502 pts1[0].x, pts1[0].y, pts1[0].approximatelyEqualHalf(pts2[0])
503 ? "==" : "!=", pts2[0].x, pts2[0].y);
504 }
505 }
506 #endif
507 // experiment: try to find intersection by chasing t
508 rootCount = findRoots(i2, q1, roots1, useCubic, 0);
509 r1Count = addValidRoots(roots1, rootCount, roots1Copy);
510 rootCount2 = findRoots(i1, q2, roots2, useCubic, 0);
511 r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
512 if (binarySearch(q1, q2, roots1Copy[0], roots2Copy[0], pts1[0])) {
513 i.insert(roots1Copy[0], roots2Copy[0], pts1[0]);
514 }
caryclark@google.com0b7da432012-10-31 19:00:20 +0000515 }
516 }
517 return i.intersected();
518 }
caryclark@google.com0b7da432012-10-31 19:00:20 +0000519 int closest[4];
520 double dist[4];
caryclark@google.com73ca6242013-01-17 21:02:47 +0000521 bool foundSomething = false;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000522 for (index = 0; index < r1Count; ++index) {
caryclark@google.com0b7da432012-10-31 19:00:20 +0000523 dist[index] = DBL_MAX;
524 closest[index] = -1;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000525 for (int ndex2 = 0; ndex2 < r2Count; ++ndex2) {
526 if (!pts2[ndex2].approximatelyEqualHalf(pts1[index])) {
caryclark@google.com0b7da432012-10-31 19:00:20 +0000527 continue;
528 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000529 double dx = pts2[ndex2].x - pts1[index].x;
530 double dy = pts2[ndex2].y - pts1[index].y;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000531 double distance = dx * dx + dy * dy;
532 if (dist[index] <= distance) {
533 continue;
534 }
535 for (int outer = 0; outer < index; ++outer) {
536 if (closest[outer] != ndex2) {
537 continue;
538 }
539 if (dist[outer] < distance) {
540 goto next;
541 }
542 closest[outer] = -1;
543 }
544 dist[index] = distance;
545 closest[index] = ndex2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000546 foundSomething = true;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000547 next:
548 ;
549 }
550 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000551 if (r1Count && r2Count && !foundSomething) {
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000552 relaxedIsLinear(q1, q2, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000553 return i.intersected();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000554 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000555 int used = 0;
556 do {
557 double lowest = DBL_MAX;
558 int lowestIndex = -1;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000559 for (index = 0; index < r1Count; ++index) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000560 if (closest[index] < 0) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000561 continue;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000562 }
563 if (roots1Copy[index] < lowest) {
564 lowestIndex = index;
565 lowest = roots1Copy[index];
566 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000567 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000568 if (lowestIndex < 0) {
569 break;
570 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000571 i.insert(roots1Copy[lowestIndex], roots2Copy[closest[lowestIndex]],
572 pts1[lowestIndex]);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000573 closest[lowestIndex] = -1;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000574 } while (++used < r1Count);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000575 i.fFlip = false;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000576 return i.intersected();
577}