blob: b6a1761781e7b4516e9ef2911f9165cd7a9c29e0 [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
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) {
90 int end = oddMan ^ opp;
91 if (end == 3) {
92 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;
123 SkDPoint mid = q2.xyAtT(tMid);
124 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;
130 int roots = rootTs.intersect(q1, line);
131 if (roots == 0) {
132 if (subDivide) {
133 *subDivide = true;
134 }
135 return true;
136 }
137 if (roots == 2) {
138 return false;
139 }
140 SkDPoint pt2 = q1.xyAtT(rootTs[0][0]);
141 if (!pt2.approximatelyEqualHalf(mid)) {
142 return false;
143 }
144 i->insertSwap(rootTs[0][0], tMid, pt2);
145 return true;
146}
147
148static bool is_linear_inner(const SkDQuad& q1, double t1s, double t1e, const SkDQuad& q2,
149 double t2s, double t2e, SkIntersections* i, bool* subDivide) {
150 SkDQuad hull = q1.subDivide(t1s, t1e);
151 SkDLine line = {{hull[2], hull[0]}};
152 const SkDLine* testLines[] = { &line, (const SkDLine*) &hull[0], (const SkDLine*) &hull[1] };
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000153 size_t testCount = SK_ARRAY_COUNT(testLines);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000154 SkTDArray<double> tsFound;
155 for (size_t index = 0; index < testCount; ++index) {
156 SkIntersections rootTs;
157 int roots = rootTs.intersect(q2, *testLines[index]);
158 for (int idx2 = 0; idx2 < roots; ++idx2) {
159 double t = rootTs[0][idx2];
160#ifdef SK_DEBUG
161 SkDPoint qPt = q2.xyAtT(t);
162 SkDPoint lPt = testLines[index]->xyAtT(rootTs[1][idx2]);
163 SkASSERT(qPt.approximatelyEqual(lPt));
164#endif
165 if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
166 continue;
167 }
168 *tsFound.append() = rootTs[0][idx2];
169 }
170 }
171 int tCount = tsFound.count();
172 if (tCount <= 0) {
173 return true;
174 }
175 double tMin, tMax;
176 if (tCount == 1) {
177 tMin = tMax = tsFound[0];
178 } else if (tCount > 1) {
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000179 SkTQSort<double>(tsFound.begin(), tsFound.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000180 tMin = tsFound[0];
181 tMax = tsFound[tsFound.count() - 1];
182 }
183 SkDPoint end = q2.xyAtT(t2s);
184 bool startInTriangle = hull.pointInHull(end);
185 if (startInTriangle) {
186 tMin = t2s;
187 }
188 end = q2.xyAtT(t2e);
189 bool endInTriangle = hull.pointInHull(end);
190 if (endInTriangle) {
191 tMax = t2e;
192 }
193 int split = 0;
194 SkDVector dxy1, dxy2;
195 if (tMin != tMax || tCount > 2) {
196 dxy2 = q2.dxdyAtT(tMin);
197 for (int index = 1; index < tCount; ++index) {
198 dxy1 = dxy2;
199 dxy2 = q2.dxdyAtT(tsFound[index]);
200 double dot = dxy1.dot(dxy2);
201 if (dot < 0) {
202 split = index - 1;
203 break;
204 }
205 }
206 }
207 if (split == 0) { // there's one point
208 if (add_intercept(q1, q2, tMin, tMax, i, subDivide)) {
209 return true;
210 }
211 i->swap();
212 return is_linear_inner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide);
213 }
214 // At this point, we have two ranges of t values -- treat each separately at the split
215 bool result;
216 if (add_intercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) {
217 result = true;
218 } else {
219 i->swap();
220 result = is_linear_inner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide);
221 }
222 if (add_intercept(q1, q2, tsFound[split], tMax, i, subDivide)) {
223 result = true;
224 } else {
225 i->swap();
226 result |= is_linear_inner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide);
227 }
228 return result;
229}
230
231static double flat_measure(const SkDQuad& q) {
232 SkDVector mid = q[1] - q[0];
233 SkDVector dxy = q[2] - q[0];
234 double length = dxy.length(); // OPTIMIZE: get rid of sqrt
235 return fabs(mid.cross(dxy) / length);
236}
237
238// FIXME ? should this measure both and then use the quad that is the flattest as the line?
239static bool is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
240 double measure = flat_measure(q1);
241 // OPTIMIZE: (get rid of sqrt) use approximately_zero
242 if (!approximately_zero_sqrt(measure)) {
243 return false;
244 }
245 return is_linear_inner(q1, 0, 1, q2, 0, 1, i, NULL);
246}
247
248// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
249static void relaxed_is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
250 double m1 = flat_measure(q1);
251 double m2 = flat_measure(q2);
252#if DEBUG_FLAT_QUADS
caryclark@google.com3b97af52013-04-23 11:56:44 +0000253 double min = SkTMin(m1, m2);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000254 if (min > 5) {
255 SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min);
256 }
257#endif
258 i->reset();
259 const SkDQuad& rounder = m2 < m1 ? q1 : q2;
260 const SkDQuad& flatter = m2 < m1 ? q2 : q1;
261 bool subDivide = false;
262 is_linear_inner(flatter, 0, 1, rounder, 0, 1, i, &subDivide);
263 if (subDivide) {
264 SkDQuadPair pair = flatter.chopAt(0.5);
265 SkIntersections firstI, secondI;
266 relaxed_is_linear(pair.first(), rounder, &firstI);
267 for (int index = 0; index < firstI.used(); ++index) {
268 i->insert(firstI[0][index] * 0.5, firstI[1][index], firstI.pt(index));
269 }
270 relaxed_is_linear(pair.second(), rounder, &secondI);
271 for (int index = 0; index < secondI.used(); ++index) {
272 i->insert(0.5 + secondI[0][index] * 0.5, secondI[1][index], secondI.pt(index));
273 }
274 }
275 if (m2 < m1) {
276 i->swapPts();
277 }
278}
279
280// each time through the loop, this computes values it had from the last loop
281// if i == j == 1, the center values are still good
282// otherwise, for i != 1 or j != 1, four of the values are still good
283// and if i == 1 ^ j == 1, an additional value is good
284static bool binary_search(const SkDQuad& quad1, const SkDQuad& quad2, double* t1Seed,
285 double* t2Seed, SkDPoint* pt) {
286 double tStep = ROUGH_EPSILON;
287 SkDPoint t1[3], t2[3];
288 int calcMask = ~0;
289 do {
290 if (calcMask & (1 << 1)) t1[1] = quad1.xyAtT(*t1Seed);
291 if (calcMask & (1 << 4)) t2[1] = quad2.xyAtT(*t2Seed);
292 if (t1[1].approximatelyEqual(t2[1])) {
293 *pt = t1[1];
294 #if ONE_OFF_DEBUG
295 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) == (%1.9g,%1.9g)\n", __FUNCTION__,
296 t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY);
297 #endif
298 return true;
299 }
300 if (calcMask & (1 << 0)) t1[0] = quad1.xyAtT(*t1Seed - tStep);
301 if (calcMask & (1 << 2)) t1[2] = quad1.xyAtT(*t1Seed + tStep);
302 if (calcMask & (1 << 3)) t2[0] = quad2.xyAtT(*t2Seed - tStep);
303 if (calcMask & (1 << 5)) t2[2] = quad2.xyAtT(*t2Seed + tStep);
304 double dist[3][3];
305 // OPTIMIZE: using calcMask value permits skipping some distance calcuations
306 // if prior loop's results are moved to correct slot for reuse
307 dist[1][1] = t1[1].distanceSquared(t2[1]);
308 int best_i = 1, best_j = 1;
309 for (int i = 0; i < 3; ++i) {
310 for (int j = 0; j < 3; ++j) {
311 if (i == 1 && j == 1) {
312 continue;
313 }
314 dist[i][j] = t1[i].distanceSquared(t2[j]);
315 if (dist[best_i][best_j] > dist[i][j]) {
316 best_i = i;
317 best_j = j;
318 }
319 }
320 }
321 if (best_i == 1 && best_j == 1) {
322 tStep /= 2;
323 if (tStep < FLT_EPSILON_HALF) {
324 break;
325 }
326 calcMask = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 5);
327 continue;
328 }
329 if (best_i == 0) {
330 *t1Seed -= tStep;
331 t1[2] = t1[1];
332 t1[1] = t1[0];
333 calcMask = 1 << 0;
334 } else if (best_i == 2) {
335 *t1Seed += tStep;
336 t1[0] = t1[1];
337 t1[1] = t1[2];
338 calcMask = 1 << 2;
339 } else {
340 calcMask = 0;
341 }
342 if (best_j == 0) {
343 *t2Seed -= tStep;
344 t2[2] = t2[1];
345 t2[1] = t2[0];
346 calcMask |= 1 << 3;
347 } else if (best_j == 2) {
348 *t2Seed += tStep;
349 t2[0] = t2[1];
350 t2[1] = t2[2];
351 calcMask |= 1 << 5;
352 }
353 } while (true);
354#if ONE_OFF_DEBUG
355 SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) != (%1.9g,%1.9g) %s\n", __FUNCTION__,
356 t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY);
357#endif
358 return false;
359}
360
361int SkIntersections::intersect(const SkDQuad& q1, const SkDQuad& q2) {
362 // if the quads share an end point, check to see if they overlap
363
caryclark@google.comb3f09212013-04-17 15:49:16 +0000364 for (int i1 = 0; i1 < 3; i1 += 2) {
365 for (int i2 = 0; i2 < 3; i2 += 2) {
366 if (q1[i1].approximatelyEqualHalf(q2[i2])) {
367 insert(i1 >> 1, i2 >> 1, q1[i1]);
368 }
369 }
370 }
371 SkASSERT(fUsed < 3);
372 if (only_end_pts_in_common(q1, q2)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000373 return fUsed;
374 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000375 if (only_end_pts_in_common(q2, q1)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000376 return fUsed;
377 }
378 // see if either quad is really a line
379 if (is_linear(q1, q2, this)) {
380 return fUsed;
381 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000382 SkIntersections swapped;
383 if (is_linear(q2, q1, &swapped)) {
384 swapped.swapPts();
385 set(swapped);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000386 return fUsed;
387 }
388 SkDQuadImplicit i1(q1);
389 SkDQuadImplicit i2(q2);
390 if (i1.match(i2)) {
391 // FIXME: compute T values
392 // compute the intersections of the ends to find the coincident span
caryclark@google.comb3f09212013-04-17 15:49:16 +0000393 reset();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000394 bool useVertical = fabs(q1[0].fX - q1[2].fX) < fabs(q1[0].fY - q1[2].fY);
395 double t;
396 if ((t = SkIntersections::Axial(q1, q2[0], useVertical)) >= 0) {
397 insertCoincident(t, 0, q2[0]);
398 }
399 if ((t = SkIntersections::Axial(q1, q2[2], useVertical)) >= 0) {
400 insertCoincident(t, 1, q2[2]);
401 }
402 useVertical = fabs(q2[0].fX - q2[2].fX) < fabs(q2[0].fY - q2[2].fY);
403 if ((t = SkIntersections::Axial(q2, q1[0], useVertical)) >= 0) {
404 insertCoincident(0, t, q1[0]);
405 }
406 if ((t = SkIntersections::Axial(q2, q1[2], useVertical)) >= 0) {
407 insertCoincident(1, t, q1[2]);
408 }
409 SkASSERT(coincidentUsed() <= 2);
410 return fUsed;
411 }
412 int index;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000413 bool flip1 = q1[2] == q2[0];
414 bool flip2 = q1[0] == q2[2];
415 bool useCubic = q1[0] == q2[0];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000416 double roots1[4];
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000417 int rootCount = findRoots(i2, q1, roots1, useCubic, flip1, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000418 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
419 double roots1Copy[4];
420 int r1Count = addValidRoots(roots1, rootCount, roots1Copy);
421 SkDPoint pts1[4];
422 for (index = 0; index < r1Count; ++index) {
423 pts1[index] = q1.xyAtT(roots1Copy[index]);
424 }
425 double roots2[4];
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000426 int rootCount2 = findRoots(i1, q2, roots2, useCubic, flip2, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000427 double roots2Copy[4];
428 int r2Count = addValidRoots(roots2, rootCount2, roots2Copy);
429 SkDPoint pts2[4];
430 for (index = 0; index < r2Count; ++index) {
431 pts2[index] = q2.xyAtT(roots2Copy[index]);
432 }
433 if (r1Count == r2Count && r1Count <= 1) {
434 if (r1Count == 1) {
435 if (pts1[0].approximatelyEqualHalf(pts2[0])) {
436 insert(roots1Copy[0], roots2Copy[0], pts1[0]);
437 } else if (pts1[0].moreRoughlyEqual(pts2[0])) {
438 // experiment: try to find intersection by chasing t
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000439 rootCount = findRoots(i2, q1, roots1, useCubic, flip1, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000440 (void) addValidRoots(roots1, rootCount, roots1Copy);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000441 rootCount2 = findRoots(i1, q2, roots2, useCubic, flip2, 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000442 (void) addValidRoots(roots2, rootCount2, roots2Copy);
443 if (binary_search(q1, q2, roots1Copy, roots2Copy, pts1)) {
444 insert(roots1Copy[0], roots2Copy[0], pts1[0]);
445 }
446 }
447 }
448 return fUsed;
449 }
450 int closest[4];
451 double dist[4];
452 bool foundSomething = false;
453 for (index = 0; index < r1Count; ++index) {
454 dist[index] = DBL_MAX;
455 closest[index] = -1;
456 for (int ndex2 = 0; ndex2 < r2Count; ++ndex2) {
457 if (!pts2[ndex2].approximatelyEqualHalf(pts1[index])) {
458 continue;
459 }
460 double dx = pts2[ndex2].fX - pts1[index].fX;
461 double dy = pts2[ndex2].fY - pts1[index].fY;
462 double distance = dx * dx + dy * dy;
463 if (dist[index] <= distance) {
464 continue;
465 }
466 for (int outer = 0; outer < index; ++outer) {
467 if (closest[outer] != ndex2) {
468 continue;
469 }
470 if (dist[outer] < distance) {
471 goto next;
472 }
473 closest[outer] = -1;
474 }
475 dist[index] = distance;
476 closest[index] = ndex2;
477 foundSomething = true;
478 next:
479 ;
480 }
481 }
482 if (r1Count && r2Count && !foundSomething) {
483 relaxed_is_linear(q1, q2, this);
484 return fUsed;
485 }
486 int used = 0;
487 do {
488 double lowest = DBL_MAX;
489 int lowestIndex = -1;
490 for (index = 0; index < r1Count; ++index) {
491 if (closest[index] < 0) {
492 continue;
493 }
494 if (roots1Copy[index] < lowest) {
495 lowestIndex = index;
496 lowest = roots1Copy[index];
497 }
498 }
499 if (lowestIndex < 0) {
500 break;
501 }
502 insert(roots1Copy[lowestIndex], roots2Copy[closest[lowestIndex]],
503 pts1[lowestIndex]);
504 closest[lowestIndex] = -1;
505 } while (++used < r1Count);
506 return fUsed;
507}