blob: 5abbbdcd82a75f9ae83e2e1de1fb436c92929fdd [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"
12#include "SkTDArray.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
22static int findRoots(const SkDQuadImplicit& i, const SkDQuad& q2, double roots[4],
23 bool oneHint, int firstCubicRoot) {
24 double a, b, c;
25 SkDQuad::SetABC(&q2[0].fX, &a, &b, &c);
26 double d, e, f;
27 SkDQuad::SetABC(&q2[0].fY, &d, &e, &f);
28 const double t4 = i.x2() * a * a
29 + i.xy() * a * d
30 + i.y2() * d * d;
31 const double t3 = 2 * i.x2() * a * b
32 + i.xy() * (a * e + b * d)
33 + 2 * i.y2() * d * e;
34 const double t2 = i.x2() * (b * b + 2 * a * c)
35 + i.xy() * (c * d + b * e + a * f)
36 + i.y2() * (e * e + 2 * d * f)
37 + i.x() * a
38 + i.y() * d;
39 const double t1 = 2 * i.x2() * b * c
40 + i.xy() * (c * e + b * f)
41 + 2 * i.y2() * e * f
42 + i.x() * b
43 + i.y() * e;
44 const double t0 = i.x2() * c * c
45 + i.xy() * c * f
46 + i.y2() * f * f
47 + i.x() * c
48 + i.y() * f
49 + i.c();
50 int rootCount = SkReducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots);
51 if (rootCount >= 0) {
52 return rootCount;
53 }
54 return SkQuarticRootsReal(firstCubicRoot, t4, t3, t2, t1, t0, roots);
55}
56
57static int addValidRoots(const double roots[4], const int count, double valid[4]) {
58 int result = 0;
59 int index;
60 for (index = 0; index < count; ++index) {
61 if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) {
62 continue;
63 }
64 double t = 1 - roots[index];
65 if (approximately_less_than_zero(t)) {
66 t = 0;
67 } else if (approximately_greater_than_one(t)) {
68 t = 1;
69 }
70 valid[result++] = t;
71 }
72 return result;
73}
74
caryclark@google.comb3f09212013-04-17 15:49:16 +000075static bool only_end_pts_in_common(const SkDQuad& q1, const SkDQuad& q2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000076// the idea here is to see at minimum do a quick reject by rotating all points
77// to either side of the line formed by connecting the endpoints
78// if the opposite curves points are on the line or on the other side, the
79// curves at most intersect at the endpoints
80 for (int oddMan = 0; oddMan < 3; ++oddMan) {
81 const SkDPoint* endPt[2];
82 for (int opp = 1; opp < 3; ++opp) {
83 int end = oddMan ^ opp;
84 if (end == 3) {
85 end = opp;
86 }
87 endPt[opp - 1] = &q1[end];
88 }
89 double origX = endPt[0]->fX;
90 double origY = endPt[0]->fY;
91 double adj = endPt[1]->fX - origX;
92 double opp = endPt[1]->fY - origY;
93 double sign = (q1[oddMan].fY - origY) * adj - (q1[oddMan].fX - origX) * opp;
94 if (approximately_zero(sign)) {
95 goto tryNextHalfPlane;
96 }
97 for (int n = 0; n < 3; ++n) {
98 double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp;
caryclark@google.comb3f09212013-04-17 15:49:16 +000099 if (test * sign > 0 && !precisely_zero(test)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000100 goto tryNextHalfPlane;
101 }
102 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000103 return true;
104tryNextHalfPlane:
105 ;
106 }
107 return false;
108}
109
110// returns false if there's more than one intercept or the intercept doesn't match the point
111// returns true if the intercept was successfully added or if the
112// original quads need to be subdivided
113static bool add_intercept(const SkDQuad& q1, const SkDQuad& q2, double tMin, double tMax,
114 SkIntersections* i, bool* subDivide) {
115 double tMid = (tMin + tMax) / 2;
116 SkDPoint mid = q2.xyAtT(tMid);
117 SkDLine line;
118 line[0] = line[1] = mid;
119 SkDVector dxdy = q2.dxdyAtT(tMid);
120 line[0] -= dxdy;
121 line[1] += dxdy;
122 SkIntersections rootTs;
123 int roots = rootTs.intersect(q1, line);
124 if (roots == 0) {
125 if (subDivide) {
126 *subDivide = true;
127 }
128 return true;
129 }
130 if (roots == 2) {
131 return false;
132 }
133 SkDPoint pt2 = q1.xyAtT(rootTs[0][0]);
134 if (!pt2.approximatelyEqualHalf(mid)) {
135 return false;
136 }
137 i->insertSwap(rootTs[0][0], tMid, pt2);
138 return true;
139}
140
141static bool is_linear_inner(const SkDQuad& q1, double t1s, double t1e, const SkDQuad& q2,
142 double t2s, double t2e, SkIntersections* i, bool* subDivide) {
143 SkDQuad hull = q1.subDivide(t1s, t1e);
144 SkDLine line = {{hull[2], hull[0]}};
145 const SkDLine* testLines[] = { &line, (const SkDLine*) &hull[0], (const SkDLine*) &hull[1] };
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000146 size_t testCount = SK_ARRAY_COUNT(testLines);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000147 SkTDArray<double> tsFound;
148 for (size_t index = 0; index < testCount; ++index) {
149 SkIntersections rootTs;
150 int roots = rootTs.intersect(q2, *testLines[index]);
151 for (int idx2 = 0; idx2 < roots; ++idx2) {
152 double t = rootTs[0][idx2];
153#ifdef SK_DEBUG
154 SkDPoint qPt = q2.xyAtT(t);
155 SkDPoint lPt = testLines[index]->xyAtT(rootTs[1][idx2]);
156 SkASSERT(qPt.approximatelyEqual(lPt));
157#endif
158 if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
159 continue;
160 }
161 *tsFound.append() = rootTs[0][idx2];
162 }
163 }
164 int tCount = tsFound.count();
165 if (tCount <= 0) {
166 return true;
167 }
168 double tMin, tMax;
169 if (tCount == 1) {
170 tMin = tMax = tsFound[0];
171 } else if (tCount > 1) {
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000172 SkTQSort<double>(tsFound.begin(), tsFound.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000173 tMin = tsFound[0];
174 tMax = tsFound[tsFound.count() - 1];
175 }
176 SkDPoint end = q2.xyAtT(t2s);
177 bool startInTriangle = hull.pointInHull(end);
178 if (startInTriangle) {
179 tMin = t2s;
180 }
181 end = q2.xyAtT(t2e);
182 bool endInTriangle = hull.pointInHull(end);
183 if (endInTriangle) {
184 tMax = t2e;
185 }
186 int split = 0;
187 SkDVector dxy1, dxy2;
188 if (tMin != tMax || tCount > 2) {
189 dxy2 = q2.dxdyAtT(tMin);
190 for (int index = 1; index < tCount; ++index) {
191 dxy1 = dxy2;
192 dxy2 = q2.dxdyAtT(tsFound[index]);
193 double dot = dxy1.dot(dxy2);
194 if (dot < 0) {
195 split = index - 1;
196 break;
197 }
198 }
199 }
200 if (split == 0) { // there's one point
201 if (add_intercept(q1, q2, tMin, tMax, i, subDivide)) {
202 return true;
203 }
204 i->swap();
205 return is_linear_inner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide);
206 }
207 // At this point, we have two ranges of t values -- treat each separately at the split
208 bool result;
209 if (add_intercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) {
210 result = true;
211 } else {
212 i->swap();
213 result = is_linear_inner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide);
214 }
215 if (add_intercept(q1, q2, tsFound[split], tMax, i, subDivide)) {
216 result = true;
217 } else {
218 i->swap();
219 result |= is_linear_inner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide);
220 }
221 return result;
222}
223
224static double flat_measure(const SkDQuad& q) {
225 SkDVector mid = q[1] - q[0];
226 SkDVector dxy = q[2] - q[0];
227 double length = dxy.length(); // OPTIMIZE: get rid of sqrt
228 return fabs(mid.cross(dxy) / length);
229}
230
231// FIXME ? should this measure both and then use the quad that is the flattest as the line?
232static bool is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
233 double measure = flat_measure(q1);
234 // OPTIMIZE: (get rid of sqrt) use approximately_zero
235 if (!approximately_zero_sqrt(measure)) {
236 return false;
237 }
238 return is_linear_inner(q1, 0, 1, q2, 0, 1, i, NULL);
239}
240
241// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
242static void relaxed_is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
243 double m1 = flat_measure(q1);
244 double m2 = flat_measure(q2);
245#if DEBUG_FLAT_QUADS
246 double min = SkTMin<double>(m1, m2);
247 if (min > 5) {
248 SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min);
249 }
250#endif
251 i->reset();
252 const SkDQuad& rounder = m2 < m1 ? q1 : q2;
253 const SkDQuad& flatter = m2 < m1 ? q2 : q1;
254 bool subDivide = false;
255 is_linear_inner(flatter, 0, 1, rounder, 0, 1, i, &subDivide);
256 if (subDivide) {
257 SkDQuadPair pair = flatter.chopAt(0.5);
258 SkIntersections firstI, secondI;
259 relaxed_is_linear(pair.first(), rounder, &firstI);
260 for (int index = 0; index < firstI.used(); ++index) {
261 i->insert(firstI[0][index] * 0.5, firstI[1][index], firstI.pt(index));
262 }
263 relaxed_is_linear(pair.second(), rounder, &secondI);
264 for (int index = 0; index < secondI.used(); ++index) {
265 i->insert(0.5 + secondI[0][index] * 0.5, secondI[1][index], secondI.pt(index));
266 }
267 }
268 if (m2 < m1) {
269 i->swapPts();
270 }
271}
272
273// each time through the loop, this computes values it had from the last loop
274// if i == j == 1, the center values are still good
275// otherwise, for i != 1 or j != 1, four of the values are still good
276// and if i == 1 ^ j == 1, an additional value is good
277static bool binary_search(const SkDQuad& quad1, const SkDQuad& quad2, double* t1Seed,
278 double* t2Seed, SkDPoint* pt) {
279 double tStep = ROUGH_EPSILON;
280 SkDPoint t1[3], t2[3];
281 int calcMask = ~0;
282 do {
283 if (calcMask & (1 << 1)) t1[1] = quad1.xyAtT(*t1Seed);
284 if (calcMask & (1 << 4)) t2[1] = quad2.xyAtT(*t2Seed);
285 if (t1[1].approximatelyEqual(t2[1])) {
286 *pt = t1[1];
287 #if ONE_OFF_DEBUG
288 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) == (%1.9g,%1.9g)\n", __FUNCTION__,
289 t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY);
290 #endif
291 return true;
292 }
293 if (calcMask & (1 << 0)) t1[0] = quad1.xyAtT(*t1Seed - tStep);
294 if (calcMask & (1 << 2)) t1[2] = quad1.xyAtT(*t1Seed + tStep);
295 if (calcMask & (1 << 3)) t2[0] = quad2.xyAtT(*t2Seed - tStep);
296 if (calcMask & (1 << 5)) t2[2] = quad2.xyAtT(*t2Seed + tStep);
297 double dist[3][3];
298 // OPTIMIZE: using calcMask value permits skipping some distance calcuations
299 // if prior loop's results are moved to correct slot for reuse
300 dist[1][1] = t1[1].distanceSquared(t2[1]);
301 int best_i = 1, best_j = 1;
302 for (int i = 0; i < 3; ++i) {
303 for (int j = 0; j < 3; ++j) {
304 if (i == 1 && j == 1) {
305 continue;
306 }
307 dist[i][j] = t1[i].distanceSquared(t2[j]);
308 if (dist[best_i][best_j] > dist[i][j]) {
309 best_i = i;
310 best_j = j;
311 }
312 }
313 }
314 if (best_i == 1 && best_j == 1) {
315 tStep /= 2;
316 if (tStep < FLT_EPSILON_HALF) {
317 break;
318 }
319 calcMask = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 5);
320 continue;
321 }
322 if (best_i == 0) {
323 *t1Seed -= tStep;
324 t1[2] = t1[1];
325 t1[1] = t1[0];
326 calcMask = 1 << 0;
327 } else if (best_i == 2) {
328 *t1Seed += tStep;
329 t1[0] = t1[1];
330 t1[1] = t1[2];
331 calcMask = 1 << 2;
332 } else {
333 calcMask = 0;
334 }
335 if (best_j == 0) {
336 *t2Seed -= tStep;
337 t2[2] = t2[1];
338 t2[1] = t2[0];
339 calcMask |= 1 << 3;
340 } else if (best_j == 2) {
341 *t2Seed += tStep;
342 t2[0] = t2[1];
343 t2[1] = t2[2];
344 calcMask |= 1 << 5;
345 }
346 } while (true);
347#if ONE_OFF_DEBUG
348 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) != (%1.9g,%1.9g) %s\n", __FUNCTION__,
349 t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY);
350#endif
351 return false;
352}
353
354int SkIntersections::intersect(const SkDQuad& q1, const SkDQuad& q2) {
355 // if the quads share an end point, check to see if they overlap
356
caryclark@google.comb3f09212013-04-17 15:49:16 +0000357 for (int i1 = 0; i1 < 3; i1 += 2) {
358 for (int i2 = 0; i2 < 3; i2 += 2) {
359 if (q1[i1].approximatelyEqualHalf(q2[i2])) {
360 insert(i1 >> 1, i2 >> 1, q1[i1]);
361 }
362 }
363 }
364 SkASSERT(fUsed < 3);
365 if (only_end_pts_in_common(q1, q2)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000366 return fUsed;
367 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000368 if (only_end_pts_in_common(q2, q1)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000369 return fUsed;
370 }
371 // see if either quad is really a line
372 if (is_linear(q1, q2, this)) {
373 return fUsed;
374 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000375 SkIntersections swapped;
376 if (is_linear(q2, q1, &swapped)) {
377 swapped.swapPts();
378 set(swapped);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000379 return fUsed;
380 }
381 SkDQuadImplicit i1(q1);
382 SkDQuadImplicit i2(q2);
383 if (i1.match(i2)) {
384 // FIXME: compute T values
385 // compute the intersections of the ends to find the coincident span
caryclark@google.comb3f09212013-04-17 15:49:16 +0000386 reset();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000387 bool useVertical = fabs(q1[0].fX - q1[2].fX) < fabs(q1[0].fY - q1[2].fY);
388 double t;
389 if ((t = SkIntersections::Axial(q1, q2[0], useVertical)) >= 0) {
390 insertCoincident(t, 0, q2[0]);
391 }
392 if ((t = SkIntersections::Axial(q1, q2[2], useVertical)) >= 0) {
393 insertCoincident(t, 1, q2[2]);
394 }
395 useVertical = fabs(q2[0].fX - q2[2].fX) < fabs(q2[0].fY - q2[2].fY);
396 if ((t = SkIntersections::Axial(q2, q1[0], useVertical)) >= 0) {
397 insertCoincident(0, t, q1[0]);
398 }
399 if ((t = SkIntersections::Axial(q2, q1[2], useVertical)) >= 0) {
400 insertCoincident(1, t, q1[2]);
401 }
402 SkASSERT(coincidentUsed() <= 2);
403 return fUsed;
404 }
405 int index;
406 bool useCubic = q1[0] == q2[0] || q1[0] == q2[2] || q1[2] == q2[0];
407 double roots1[4];
408 int rootCount = findRoots(i2, q1, roots1, useCubic, 0);
409 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
410 double roots1Copy[4];
411 int r1Count = addValidRoots(roots1, rootCount, roots1Copy);
412 SkDPoint pts1[4];
413 for (index = 0; index < r1Count; ++index) {
414 pts1[index] = q1.xyAtT(roots1Copy[index]);
415 }
416 double roots2[4];
417 int rootCount2 = findRoots(i1, q2, roots2, useCubic, 0);
418 double roots2Copy[4];
419 int r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
420 SkDPoint pts2[4];
421 for (index = 0; index < r2Count; ++index) {
422 pts2[index] = q2.xyAtT(roots2Copy[index]);
423 }
424 if (r1Count == r2Count && r1Count <= 1) {
425 if (r1Count == 1) {
426 if (pts1[0].approximatelyEqualHalf(pts2[0])) {
427 insert(roots1Copy[0], roots2Copy[0], pts1[0]);
428 } else if (pts1[0].moreRoughlyEqual(pts2[0])) {
429 // experiment: try to find intersection by chasing t
430 rootCount = findRoots(i2, q1, roots1, useCubic, 0);
431 (void) addValidRoots(roots1, rootCount, roots1Copy);
432 rootCount2 = findRoots(i1, q2, roots2, useCubic, 0);
433 (void) addValidRoots(roots2, rootCount2, roots2Copy);
434 if (binary_search(q1, q2, roots1Copy, roots2Copy, pts1)) {
435 insert(roots1Copy[0], roots2Copy[0], pts1[0]);
436 }
437 }
438 }
439 return fUsed;
440 }
441 int closest[4];
442 double dist[4];
443 bool foundSomething = false;
444 for (index = 0; index < r1Count; ++index) {
445 dist[index] = DBL_MAX;
446 closest[index] = -1;
447 for (int ndex2 = 0; ndex2 < r2Count; ++ndex2) {
448 if (!pts2[ndex2].approximatelyEqualHalf(pts1[index])) {
449 continue;
450 }
451 double dx = pts2[ndex2].fX - pts1[index].fX;
452 double dy = pts2[ndex2].fY - pts1[index].fY;
453 double distance = dx * dx + dy * dy;
454 if (dist[index] <= distance) {
455 continue;
456 }
457 for (int outer = 0; outer < index; ++outer) {
458 if (closest[outer] != ndex2) {
459 continue;
460 }
461 if (dist[outer] < distance) {
462 goto next;
463 }
464 closest[outer] = -1;
465 }
466 dist[index] = distance;
467 closest[index] = ndex2;
468 foundSomething = true;
469 next:
470 ;
471 }
472 }
473 if (r1Count && r2Count && !foundSomething) {
474 relaxed_is_linear(q1, q2, this);
475 return fUsed;
476 }
477 int used = 0;
478 do {
479 double lowest = DBL_MAX;
480 int lowestIndex = -1;
481 for (index = 0; index < r1Count; ++index) {
482 if (closest[index] < 0) {
483 continue;
484 }
485 if (roots1Copy[index] < lowest) {
486 lowestIndex = index;
487 lowest = roots1Copy[index];
488 }
489 }
490 if (lowestIndex < 0) {
491 break;
492 }
493 insert(roots1Copy[lowestIndex], roots2Copy[closest[lowestIndex]],
494 pts1[lowestIndex]);
495 closest[lowestIndex] = -1;
496 } while (++used < r1Count);
497 return fUsed;
498}