blob: 5869d7db19c0ac927b1130e1173626df8589dd01 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +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
8#include "SkDQuadImplicit.h"
9#include "SkIntersections.h"
10#include "SkPathOpsLine.h"
11#include "SkQuarticRoot.h"
caryclark@google.comd892bd82013-06-17 14:10:36 +000012#include "SkTArray.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000013#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000014
15/* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F
16 * and given x = at^2 + bt + c (the parameterized form)
17 * y = dt^2 + et + f
18 * then
19 * 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
20 */
21
caryclark@google.comcffbcc32013-06-04 17:59:42 +000022static int findRoots(const SkDQuadImplicit& i, const SkDQuad& quad, double roots[4],
23 bool oneHint, bool flip, int firstCubicRoot) {
24 SkDQuad flipped;
25 const SkDQuad& q = flip ? (flipped = quad.flip()) : quad;
caryclark@google.com07393ca2013-04-08 11:47:37 +000026 double a, b, c;
caryclark@google.comcffbcc32013-06-04 17:59:42 +000027 SkDQuad::SetABC(&q[0].fX, &a, &b, &c);
caryclark@google.com07393ca2013-04-08 11:47:37 +000028 double d, e, f;
caryclark@google.comcffbcc32013-06-04 17:59:42 +000029 SkDQuad::SetABC(&q[0].fY, &d, &e, &f);
caryclark@google.com07393ca2013-04-08 11:47:37 +000030 const double t4 = i.x2() * a * a
31 + i.xy() * a * d
32 + i.y2() * d * d;
33 const double t3 = 2 * i.x2() * a * b
34 + i.xy() * (a * e + b * d)
35 + 2 * i.y2() * d * e;
36 const double t2 = i.x2() * (b * b + 2 * a * c)
37 + i.xy() * (c * d + b * e + a * f)
38 + i.y2() * (e * e + 2 * d * f)
39 + i.x() * a
40 + i.y() * d;
41 const double t1 = 2 * i.x2() * b * c
42 + i.xy() * (c * e + b * f)
43 + 2 * i.y2() * e * f
44 + i.x() * b
45 + i.y() * e;
46 const double t0 = i.x2() * c * c
47 + i.xy() * c * f
48 + i.y2() * f * f
49 + i.x() * c
50 + i.y() * f
51 + i.c();
52 int rootCount = SkReducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots);
caryclark@google.comcffbcc32013-06-04 17:59:42 +000053 if (rootCount < 0) {
54 rootCount = SkQuarticRootsReal(firstCubicRoot, t4, t3, t2, t1, t0, roots);
caryclark@google.com07393ca2013-04-08 11:47:37 +000055 }
caryclark@google.comcffbcc32013-06-04 17:59:42 +000056 if (flip) {
57 for (int index = 0; index < rootCount; ++index) {
58 roots[index] = 1 - roots[index];
59 }
60 }
61 return rootCount;
caryclark@google.com07393ca2013-04-08 11:47:37 +000062}
63
64static int addValidRoots(const double roots[4], const int count, double valid[4]) {
65 int result = 0;
66 int index;
67 for (index = 0; index < count; ++index) {
68 if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) {
69 continue;
70 }
71 double t = 1 - roots[index];
72 if (approximately_less_than_zero(t)) {
73 t = 0;
74 } else if (approximately_greater_than_one(t)) {
75 t = 1;
76 }
77 valid[result++] = t;
78 }
79 return result;
80}
81
caryclark@google.comb3f09212013-04-17 15:49:16 +000082static bool only_end_pts_in_common(const SkDQuad& q1, const SkDQuad& q2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000083// the idea here is to see at minimum do a quick reject by rotating all points
84// to either side of the line formed by connecting the endpoints
85// if the opposite curves points are on the line or on the other side, the
86// curves at most intersect at the endpoints
87 for (int oddMan = 0; oddMan < 3; ++oddMan) {
88 const SkDPoint* endPt[2];
89 for (int opp = 1; opp < 3; ++opp) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +000090 int end = oddMan ^ opp; // choose a value not equal to oddMan
91 if (3 == end) { // and correct so that largest value is 1 or 2
caryclark@google.com07393ca2013-04-08 11:47:37 +000092 end = opp;
93 }
94 endPt[opp - 1] = &q1[end];
95 }
96 double origX = endPt[0]->fX;
97 double origY = endPt[0]->fY;
98 double adj = endPt[1]->fX - origX;
99 double opp = endPt[1]->fY - origY;
100 double sign = (q1[oddMan].fY - origY) * adj - (q1[oddMan].fX - origX) * opp;
101 if (approximately_zero(sign)) {
102 goto tryNextHalfPlane;
103 }
104 for (int n = 0; n < 3; ++n) {
105 double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp;
caryclark@google.comb3f09212013-04-17 15:49:16 +0000106 if (test * sign > 0 && !precisely_zero(test)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000107 goto tryNextHalfPlane;
108 }
109 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000110 return true;
111tryNextHalfPlane:
112 ;
113 }
114 return false;
115}
116
117// returns false if there's more than one intercept or the intercept doesn't match the point
118// returns true if the intercept was successfully added or if the
119// original quads need to be subdivided
120static bool add_intercept(const SkDQuad& q1, const SkDQuad& q2, double tMin, double tMax,
121 SkIntersections* i, bool* subDivide) {
122 double tMid = (tMin + tMax) / 2;
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000123 SkDPoint mid = q2.ptAtT(tMid);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000124 SkDLine line;
125 line[0] = line[1] = mid;
126 SkDVector dxdy = q2.dxdyAtT(tMid);
127 line[0] -= dxdy;
128 line[1] += dxdy;
129 SkIntersections rootTs;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000130 rootTs.allowNear(false);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000131 int roots = rootTs.intersect(q1, line);
132 if (roots == 0) {
133 if (subDivide) {
134 *subDivide = true;
135 }
136 return true;
137 }
138 if (roots == 2) {
139 return false;
140 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000141 SkDPoint pt2 = q1.ptAtT(rootTs[0][0]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000142 if (!pt2.approximatelyEqualHalf(mid)) {
143 return false;
144 }
145 i->insertSwap(rootTs[0][0], tMid, pt2);
146 return true;
147}
148
149static bool is_linear_inner(const SkDQuad& q1, double t1s, double t1e, const SkDQuad& q2,
150 double t2s, double t2e, SkIntersections* i, bool* subDivide) {
151 SkDQuad hull = q1.subDivide(t1s, t1e);
152 SkDLine line = {{hull[2], hull[0]}};
153 const SkDLine* testLines[] = { &line, (const SkDLine*) &hull[0], (const SkDLine*) &hull[1] };
caryclark@google.comd892bd82013-06-17 14:10:36 +0000154 const size_t kTestCount = SK_ARRAY_COUNT(testLines);
155 SkSTArray<kTestCount * 2, double, true> tsFound;
156 for (size_t index = 0; index < kTestCount; ++index) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000157 SkIntersections rootTs;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000158 rootTs.allowNear(false);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000159 int roots = rootTs.intersect(q2, *testLines[index]);
160 for (int idx2 = 0; idx2 < roots; ++idx2) {
161 double t = rootTs[0][idx2];
162#ifdef SK_DEBUG
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000163 SkDPoint qPt = q2.ptAtT(t);
164 SkDPoint lPt = testLines[index]->ptAtT(rootTs[1][idx2]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000165 SkASSERT(qPt.approximatelyEqual(lPt));
166#endif
167 if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
168 continue;
169 }
caryclark@google.comd892bd82013-06-17 14:10:36 +0000170 tsFound.push_back(rootTs[0][idx2]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000171 }
172 }
173 int tCount = tsFound.count();
174 if (tCount <= 0) {
175 return true;
176 }
177 double tMin, tMax;
178 if (tCount == 1) {
179 tMin = tMax = tsFound[0];
reed@google.coma3e500c2013-07-02 14:44:27 +0000180 } else {
181 SkASSERT(tCount > 1);
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000182 SkTQSort<double>(tsFound.begin(), tsFound.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000183 tMin = tsFound[0];
184 tMax = tsFound[tsFound.count() - 1];
185 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000186 SkDPoint end = q2.ptAtT(t2s);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000187 bool startInTriangle = hull.pointInHull(end);
188 if (startInTriangle) {
189 tMin = t2s;
190 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000191 end = q2.ptAtT(t2e);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000192 bool endInTriangle = hull.pointInHull(end);
193 if (endInTriangle) {
194 tMax = t2e;
195 }
196 int split = 0;
197 SkDVector dxy1, dxy2;
198 if (tMin != tMax || tCount > 2) {
199 dxy2 = q2.dxdyAtT(tMin);
200 for (int index = 1; index < tCount; ++index) {
201 dxy1 = dxy2;
202 dxy2 = q2.dxdyAtT(tsFound[index]);
203 double dot = dxy1.dot(dxy2);
204 if (dot < 0) {
205 split = index - 1;
206 break;
207 }
208 }
209 }
210 if (split == 0) { // there's one point
211 if (add_intercept(q1, q2, tMin, tMax, i, subDivide)) {
212 return true;
213 }
214 i->swap();
215 return is_linear_inner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide);
216 }
217 // At this point, we have two ranges of t values -- treat each separately at the split
218 bool result;
219 if (add_intercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) {
220 result = true;
221 } else {
222 i->swap();
223 result = is_linear_inner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide);
224 }
225 if (add_intercept(q1, q2, tsFound[split], tMax, i, subDivide)) {
226 result = true;
227 } else {
228 i->swap();
229 result |= is_linear_inner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide);
230 }
231 return result;
232}
233
234static double flat_measure(const SkDQuad& q) {
235 SkDVector mid = q[1] - q[0];
236 SkDVector dxy = q[2] - q[0];
237 double length = dxy.length(); // OPTIMIZE: get rid of sqrt
238 return fabs(mid.cross(dxy) / length);
239}
240
241// FIXME ? should this measure both and then use the quad that is the flattest as the line?
242static bool is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
243 double measure = flat_measure(q1);
244 // OPTIMIZE: (get rid of sqrt) use approximately_zero
245 if (!approximately_zero_sqrt(measure)) {
246 return false;
247 }
248 return is_linear_inner(q1, 0, 1, q2, 0, 1, i, NULL);
249}
250
251// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
252static void relaxed_is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
253 double m1 = flat_measure(q1);
254 double m2 = flat_measure(q2);
255#if DEBUG_FLAT_QUADS
caryclark@google.com3b97af52013-04-23 11:56:44 +0000256 double min = SkTMin(m1, m2);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000257 if (min > 5) {
258 SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min);
259 }
260#endif
261 i->reset();
262 const SkDQuad& rounder = m2 < m1 ? q1 : q2;
263 const SkDQuad& flatter = m2 < m1 ? q2 : q1;
264 bool subDivide = false;
265 is_linear_inner(flatter, 0, 1, rounder, 0, 1, i, &subDivide);
266 if (subDivide) {
267 SkDQuadPair pair = flatter.chopAt(0.5);
268 SkIntersections firstI, secondI;
269 relaxed_is_linear(pair.first(), rounder, &firstI);
270 for (int index = 0; index < firstI.used(); ++index) {
271 i->insert(firstI[0][index] * 0.5, firstI[1][index], firstI.pt(index));
272 }
273 relaxed_is_linear(pair.second(), rounder, &secondI);
274 for (int index = 0; index < secondI.used(); ++index) {
275 i->insert(0.5 + secondI[0][index] * 0.5, secondI[1][index], secondI.pt(index));
276 }
277 }
278 if (m2 < m1) {
279 i->swapPts();
280 }
281}
282
283// each time through the loop, this computes values it had from the last loop
284// if i == j == 1, the center values are still good
285// otherwise, for i != 1 or j != 1, four of the values are still good
286// and if i == 1 ^ j == 1, an additional value is good
287static bool binary_search(const SkDQuad& quad1, const SkDQuad& quad2, double* t1Seed,
288 double* t2Seed, SkDPoint* pt) {
289 double tStep = ROUGH_EPSILON;
290 SkDPoint t1[3], t2[3];
291 int calcMask = ~0;
292 do {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000293 if (calcMask & (1 << 1)) t1[1] = quad1.ptAtT(*t1Seed);
294 if (calcMask & (1 << 4)) t2[1] = quad2.ptAtT(*t2Seed);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000295 if (t1[1].approximatelyEqual(t2[1])) {
296 *pt = t1[1];
297 #if ONE_OFF_DEBUG
298 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) == (%1.9g,%1.9g)\n", __FUNCTION__,
299 t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY);
300 #endif
301 return true;
302 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000303 if (calcMask & (1 << 0)) t1[0] = quad1.ptAtT(*t1Seed - tStep);
304 if (calcMask & (1 << 2)) t1[2] = quad1.ptAtT(*t1Seed + tStep);
305 if (calcMask & (1 << 3)) t2[0] = quad2.ptAtT(*t2Seed - tStep);
306 if (calcMask & (1 << 5)) t2[2] = quad2.ptAtT(*t2Seed + tStep);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000307 double dist[3][3];
308 // OPTIMIZE: using calcMask value permits skipping some distance calcuations
309 // if prior loop's results are moved to correct slot for reuse
310 dist[1][1] = t1[1].distanceSquared(t2[1]);
311 int best_i = 1, best_j = 1;
312 for (int i = 0; i < 3; ++i) {
313 for (int j = 0; j < 3; ++j) {
314 if (i == 1 && j == 1) {
315 continue;
316 }
317 dist[i][j] = t1[i].distanceSquared(t2[j]);
318 if (dist[best_i][best_j] > dist[i][j]) {
319 best_i = i;
320 best_j = j;
321 }
322 }
323 }
324 if (best_i == 1 && best_j == 1) {
325 tStep /= 2;
326 if (tStep < FLT_EPSILON_HALF) {
327 break;
328 }
329 calcMask = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 5);
330 continue;
331 }
332 if (best_i == 0) {
333 *t1Seed -= tStep;
334 t1[2] = t1[1];
335 t1[1] = t1[0];
336 calcMask = 1 << 0;
337 } else if (best_i == 2) {
338 *t1Seed += tStep;
339 t1[0] = t1[1];
340 t1[1] = t1[2];
341 calcMask = 1 << 2;
342 } else {
343 calcMask = 0;
344 }
345 if (best_j == 0) {
346 *t2Seed -= tStep;
347 t2[2] = t2[1];
348 t2[1] = t2[0];
349 calcMask |= 1 << 3;
350 } else if (best_j == 2) {
351 *t2Seed += tStep;
352 t2[0] = t2[1];
353 t2[1] = t2[2];
354 calcMask |= 1 << 5;
355 }
356 } while (true);
357#if ONE_OFF_DEBUG
358 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) != (%1.9g,%1.9g) %s\n", __FUNCTION__,
359 t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY);
360#endif
361 return false;
362}
363
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000364static void lookNearEnd(const SkDQuad& q1, const SkDQuad& q2, int testT,
365 const SkIntersections& orig, bool swap, SkIntersections* i) {
366 if (orig.used() == 1 && orig[!swap][0] == testT) {
367 return;
368 }
369 if (orig.used() == 2 && orig[!swap][1] == testT) {
370 return;
371 }
372 SkDLine tmpLine;
373 int testTIndex = testT << 1;
374 tmpLine[0] = tmpLine[1] = q2[testTIndex];
375 tmpLine[1].fX += q2[1].fY - q2[testTIndex].fY;
376 tmpLine[1].fY -= q2[1].fX - q2[testTIndex].fX;
377 SkIntersections impTs;
378 impTs.intersectRay(q1, tmpLine);
379 for (int index = 0; index < impTs.used(); ++index) {
380 SkDPoint realPt = impTs.pt(index);
381 if (!tmpLine[0].approximatelyEqualHalf(realPt)) {
382 continue;
383 }
384 if (swap) {
385 i->insert(testT, impTs[0][index], tmpLine[0]);
386 } else {
387 i->insert(impTs[0][index], testT, tmpLine[0]);
388 }
389 }
390}
391
caryclark@google.com07393ca2013-04-08 11:47:37 +0000392int SkIntersections::intersect(const SkDQuad& q1, const SkDQuad& q2) {
393 // if the quads share an end point, check to see if they overlap
caryclark@google.comb3f09212013-04-17 15:49:16 +0000394 for (int i1 = 0; i1 < 3; i1 += 2) {
395 for (int i2 = 0; i2 < 3; i2 += 2) {
caryclark@google.com570863f2013-09-16 15:55:01 +0000396 if (q1[i1] == q2[i2]) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000397 insert(i1 >> 1, i2 >> 1, q1[i1]);
398 }
399 }
400 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000401 if (fAllowNear || true) { // FIXME ? cubic/cubic intersection fails without (cubicOp67u)
402 for (int i1 = 0; i1 < 3; i1 += 2) {
403 for (int i2 = 0; i2 < 3; i2 += 2) {
404 if (q1[i1] != q2[i2] && q1[i1].approximatelyEqualHalf(q2[i2])) {
405 insertNear(i1 >> 1, i2 >> 1, q1[i1]);
406 }
407 }
408 }
409 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000410 SkASSERT(fUsed < 3);
411 if (only_end_pts_in_common(q1, q2)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000412 return fUsed;
413 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000414 if (only_end_pts_in_common(q2, q1)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000415 return fUsed;
416 }
417 // see if either quad is really a line
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000418 // FIXME: figure out why reduce step didn't find this earlier
caryclark@google.com07393ca2013-04-08 11:47:37 +0000419 if (is_linear(q1, q2, this)) {
420 return fUsed;
421 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000422 SkIntersections swapped;
423 if (is_linear(q2, q1, &swapped)) {
424 swapped.swapPts();
425 set(swapped);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000426 return fUsed;
427 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000428 SkIntersections copyI(*this);
429 lookNearEnd(q1, q2, 0, *this, false, &copyI);
430 lookNearEnd(q1, q2, 1, *this, false, &copyI);
431 lookNearEnd(q2, q1, 0, *this, true, &copyI);
432 lookNearEnd(q2, q1, 1, *this, true, &copyI);
433 int innerEqual = 0;
434 if (copyI.fUsed >= 2) {
435 SkASSERT(copyI.fUsed <= 4);
436 double width = copyI[0][1] - copyI[0][0];
437 int midEnd = 1;
438 for (int index = 2; index < copyI.fUsed; ++index) {
439 double testWidth = copyI[0][index] - copyI[0][index - 1];
440 if (testWidth <= width) {
441 continue;
442 }
443 midEnd = index;
444 }
445 for (int index = 0; index < 2; ++index) {
446 double testT = (copyI[0][midEnd] * (index + 1)
447 + copyI[0][midEnd - 1] * (2 - index)) / 3;
448 SkDPoint testPt1 = q1.ptAtT(testT);
449 testT = (copyI[1][midEnd] * (index + 1) + copyI[1][midEnd - 1] * (2 - index)) / 3;
450 SkDPoint testPt2 = q2.ptAtT(testT);
451 innerEqual += testPt1.approximatelyEqual(testPt2);
452 }
453 }
454 bool expectCoincident = copyI.fUsed >= 2 && innerEqual == 2;
455 if (expectCoincident) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000456 reset();
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000457 insertCoincident(copyI[0][0], copyI[1][0], copyI.fPt[0]);
458 int last = copyI.fUsed - 1;
459 insertCoincident(copyI[0][last], copyI[1][last], copyI.fPt[last]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000460 return fUsed;
461 }
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000462 SkDQuadImplicit i1(q1);
463 SkDQuadImplicit i2(q2);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000464 int index;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000465 bool flip1 = q1[2] == q2[0];
466 bool flip2 = q1[0] == q2[2];
467 bool useCubic = q1[0] == q2[0];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000468 double roots1[4];
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000469 int rootCount = findRoots(i2, q1, roots1, useCubic, flip1, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000470 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
471 double roots1Copy[4];
472 int r1Count = addValidRoots(roots1, rootCount, roots1Copy);
473 SkDPoint pts1[4];
474 for (index = 0; index < r1Count; ++index) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000475 pts1[index] = q1.ptAtT(roots1Copy[index]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000476 }
477 double roots2[4];
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000478 int rootCount2 = findRoots(i1, q2, roots2, useCubic, flip2, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000479 double roots2Copy[4];
480 int r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
481 SkDPoint pts2[4];
482 for (index = 0; index < r2Count; ++index) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000483 pts2[index] = q2.ptAtT(roots2Copy[index]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000484 }
485 if (r1Count == r2Count && r1Count <= 1) {
486 if (r1Count == 1) {
487 if (pts1[0].approximatelyEqualHalf(pts2[0])) {
488 insert(roots1Copy[0], roots2Copy[0], pts1[0]);
489 } else if (pts1[0].moreRoughlyEqual(pts2[0])) {
490 // experiment: try to find intersection by chasing t
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000491 rootCount = findRoots(i2, q1, roots1, useCubic, flip1, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000492 (void) addValidRoots(roots1, rootCount, roots1Copy);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000493 rootCount2 = findRoots(i1, q2, roots2, useCubic, flip2, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000494 (void) addValidRoots(roots2, rootCount2, roots2Copy);
495 if (binary_search(q1, q2, roots1Copy, roots2Copy, pts1)) {
496 insert(roots1Copy[0], roots2Copy[0], pts1[0]);
497 }
498 }
499 }
500 return fUsed;
501 }
502 int closest[4];
503 double dist[4];
504 bool foundSomething = false;
505 for (index = 0; index < r1Count; ++index) {
506 dist[index] = DBL_MAX;
507 closest[index] = -1;
508 for (int ndex2 = 0; ndex2 < r2Count; ++ndex2) {
509 if (!pts2[ndex2].approximatelyEqualHalf(pts1[index])) {
510 continue;
511 }
512 double dx = pts2[ndex2].fX - pts1[index].fX;
513 double dy = pts2[ndex2].fY - pts1[index].fY;
514 double distance = dx * dx + dy * dy;
515 if (dist[index] <= distance) {
516 continue;
517 }
518 for (int outer = 0; outer < index; ++outer) {
519 if (closest[outer] != ndex2) {
520 continue;
521 }
522 if (dist[outer] < distance) {
523 goto next;
524 }
525 closest[outer] = -1;
526 }
527 dist[index] = distance;
528 closest[index] = ndex2;
529 foundSomething = true;
530 next:
531 ;
532 }
533 }
534 if (r1Count && r2Count && !foundSomething) {
535 relaxed_is_linear(q1, q2, this);
536 return fUsed;
537 }
538 int used = 0;
539 do {
540 double lowest = DBL_MAX;
541 int lowestIndex = -1;
542 for (index = 0; index < r1Count; ++index) {
543 if (closest[index] < 0) {
544 continue;
545 }
546 if (roots1Copy[index] < lowest) {
547 lowestIndex = index;
548 lowest = roots1Copy[index];
549 }
550 }
551 if (lowestIndex < 0) {
552 break;
553 }
554 insert(roots1Copy[lowestIndex], roots2Copy[closest[lowestIndex]],
555 pts1[lowestIndex]);
556 closest[lowestIndex] = -1;
557 } while (++used < r1Count);
558 return fUsed;
559}