blob: 6e465571460ac827b1f4742f1e78ae9099834489 [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
16#include <algorithm> // for std::min, max
caryclark@google.com235f56a2012-09-14 14:19:30 +000017
18/* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F
19 * and given x = at^2 + bt + c (the parameterized form)
20 * y = dt^2 + et + f
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000021 * then
caryclark@google.com235f56a2012-09-14 14:19:30 +000022 * 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
23 */
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000024
caryclark@google.com73ca6242013-01-17 21:02:47 +000025#if SK_DEBUG
26#define QUARTIC_DEBUG 1
27#else
28#define QUARTIC_DEBUG 0
29#endif
caryclark@google.com235f56a2012-09-14 14:19:30 +000030
caryclark@google.com73ca6242013-01-17 21:02:47 +000031static int findRoots(const QuadImplicitForm& i, const Quadratic& q2, double roots[4],
32 bool useCubic, bool& disregardCount) {
caryclark@google.com235f56a2012-09-14 14:19:30 +000033 double a, b, c;
34 set_abc(&q2[0].x, a, b, c);
35 double d, e, f;
36 set_abc(&q2[0].y, d, e, f);
37 const double t4 = i.x2() * a * a
38 + i.xy() * a * d
39 + i.y2() * d * d;
40 const double t3 = 2 * i.x2() * a * b
41 + i.xy() * (a * e + b * d)
42 + 2 * i.y2() * d * e;
43 const double t2 = i.x2() * (b * b + 2 * a * c)
44 + i.xy() * (c * d + b * e + a * f)
45 + i.y2() * (e * e + 2 * d * f)
46 + i.x() * a
47 + i.y() * d;
48 const double t1 = 2 * i.x2() * b * c
49 + i.xy() * (c * e + b * f)
50 + 2 * i.y2() * e * f
51 + i.x() * b
52 + i.y() * e;
53 const double t0 = i.x2() * c * c
54 + i.xy() * c * f
55 + i.y2() * f * f
56 + i.x() * c
57 + i.y() * f
58 + i.c();
caryclark@google.com73ca6242013-01-17 21:02:47 +000059#if QUARTIC_DEBUG
60 // create a string mathematica understands
61 char str[1024];
62 bzero(str, sizeof(str));
63 sprintf(str, "Solve[%1.19g x^4 + %1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]",
64 t4, t3, t2, t1, t0);
65#endif
66 if (approximately_zero(t4)) {
67 disregardCount = true;
68 if (approximately_zero(t3)) {
69 return quadraticRootsX(t2, t1, t0, roots);
70 }
71 return cubicRootsX(t3, t2, t1, t0, roots);
72 }
73 if (approximately_zero(t0)) { // 0 is one root
74 disregardCount = true;
75 int num = cubicRootsX(t4, t3, t2, t1, roots);
76 for (int i = 0; i < num; ++i) {
77 if (approximately_zero(roots[i])) {
78 return num;
79 }
80 }
81 roots[num++] = 0;
82 return num;
83 }
84 if (useCubic) {
85 assert(approximately_zero(t4 + t3 + t2 + t1 + t0)); // 1 is one root
86 int num = cubicRootsX(t4, t4 + t3, -(t1 + t0), -t0, roots); // note that -C==A+B+D+E
87 for (int i = 0; i < num; ++i) {
88 if (approximately_equal(roots[i], 1)) {
89 return num;
90 }
91 }
92 roots[num++] = 1;
93 return num;
94 }
caryclark@google.com235f56a2012-09-14 14:19:30 +000095 return quarticRoots(t4, t3, t2, t1, t0, roots);
96}
97
98static void addValidRoots(const double roots[4], const int count, const int side, Intersections& i) {
99 int index;
100 for (index = 0; index < count; ++index) {
101 if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) {
102 continue;
103 }
104 double t = 1 - roots[index];
105 if (approximately_less_than_zero(t)) {
106 t = 0;
107 } else if (approximately_greater_than_one(t)) {
108 t = 1;
109 }
110 i.insertOne(t, side);
111 }
112}
113
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000114static bool onlyEndPtsInCommon(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
115// the idea here is to see at minimum do a quick reject by rotating all points
116// to either side of the line formed by connecting the endpoints
117// if the opposite curves points are on the line or on the other side, the
118// curves at most intersect at the endpoints
119 for (int oddMan = 0; oddMan < 3; ++oddMan) {
120 const _Point* endPt[2];
121 for (int opp = 1; opp < 3; ++opp) {
122 int end = oddMan ^ opp;
123 if (end == 3) {
124 end = opp;
125 }
126 endPt[opp - 1] = &q1[end];
127 }
128 double origX = endPt[0]->x;
129 double origY = endPt[0]->y;
130 double adj = endPt[1]->x - origX;
131 double opp = endPt[1]->y - origY;
132 double sign = (q1[oddMan].y - origY) * adj - (q1[oddMan].x - origX) * opp;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000133 if (approximately_zero(sign)) {
134 goto tryNextHalfPlane;
135 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000136 for (int n = 0; n < 3; ++n) {
137 double test = (q2[n].y - origY) * adj - (q2[n].x - origX) * opp;
138 if (test * sign > 0) {
139 goto tryNextHalfPlane;
140 }
141 }
142 for (int i1 = 0; i1 < 3; i1 += 2) {
143 for (int i2 = 0; i2 < 3; i2 += 2) {
144 if (q1[i1] == q2[i2]) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000145 i.insert(i1 >> 1, i2 >> 1);
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000146 }
147 }
148 }
149 assert(i.fUsed < 3);
150 return true;
151tryNextHalfPlane:
152 ;
153 }
154 return false;
155}
156
caryclark@google.com73ca6242013-01-17 21:02:47 +0000157// http://www.blackpawn.com/texts/pointinpoly/default.html
158static bool pointInTriangle(const _Point& pt, const _Line* testLines[]) {
159 const _Point& A = (*testLines[0])[0];
160 const _Point& B = (*testLines[1])[0];
161 const _Point& C = (*testLines[2])[0];
162
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000163// Compute vectors
caryclark@google.com73ca6242013-01-17 21:02:47 +0000164 _Point v0 = C - A;
165 _Point v1 = B - A;
166 _Point v2 = pt - A;
167
168// Compute dot products
169 double dot00 = v0.dot(v0);
170 double dot01 = v0.dot(v1);
171 double dot02 = v0.dot(v2);
172 double dot11 = v1.dot(v1);
173 double dot12 = v1.dot(v2);
174
175// Compute barycentric coordinates
176 double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
177 double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
178 double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
179
180// Check if point is in triangle
181 return (u >= 0) && (v >= 0) && (u + v < 1);
182}
183
184static bool addIntercept(const Quadratic& q1, const Quadratic& q2, double tMin, double tMax,
185 Intersections& i) {
186 double tMid = (tMin + tMax) / 2;
187 _Point mid;
188 xy_at_t(q2, tMid, mid.x, mid.y);
189 _Line line;
190 line[0] = line[1] = mid;
191 double dx, dy;
192 dxdy_at_t(q2, tMid, dx, dy);
193 line[0].x -= dx;
194 line[0].y -= dy;
195 line[1].x += dx;
196 line[1].y += dy;
197 Intersections rootTs;
198 int roots = intersect(q1, line, rootTs);
199 assert(roots == 1);
200 _Point pt2;
201 xy_at_t(q1, rootTs.fT[0][0], pt2.x, pt2.y);
202 if (!pt2.approximatelyEqual(mid)) {
203 return false;
204 }
205 i.add(rootTs.fT[0][0], tMid);
206 return true;
207}
208
209static bool isLinearInner(const Quadratic& q1, double t1s, double t1e, const Quadratic& q2,
210 double t2s, double t2e, Intersections& i) {
211 Quadratic hull;
212 sub_divide(q1, t1s, t1e, hull);
213 _Line line = {hull[2], hull[0]};
214 const _Line* testLines[] = { &line, (const _Line*) &hull[0], (const _Line*) &hull[1] };
215 size_t testCount = sizeof(testLines) / sizeof(testLines[0]);
216 SkTDArray<double> tsFound;
217 for (size_t index = 0; index < testCount; ++index) {
218 Intersections rootTs;
219 int roots = intersect(q2, *testLines[index], rootTs);
220 for (int idx2 = 0; idx2 < roots; ++idx2) {
221 double t = rootTs.fT[0][idx2];
222 if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
223 continue;
224 }
225 *tsFound.append() = rootTs.fT[0][idx2];
226 }
227 }
228 int tCount = tsFound.count();
229 if (!tCount) {
230 return true;
231 }
232 double tMin, tMax;
233 _Point dxy1, dxy2;
234 if (tCount == 1) {
235 tMin = tMax = tsFound[0];
236 } else if (tCount > 1) {
237 QSort<double>(tsFound.begin(), tsFound.end() - 1);
238 tMin = tsFound[0];
239 tMax = tsFound[1];
240 }
241 _Point end;
242 xy_at_t(q2, t2s, end.x, end.y);
243 bool startInTriangle = pointInTriangle(end, testLines);
244 if (startInTriangle) {
245 tMin = t2s;
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000246 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000247 xy_at_t(q2, t2e, end.x, end.y);
248 bool endInTriangle = pointInTriangle(end, testLines);
249 if (endInTriangle) {
250 tMax = t2e;
251 }
252 int split = 0;
253 if (tMin != tMax || tCount > 2) {
254 dxdy_at_t(q2, tMin, dxy2.x, dxy2.y);
255 for (int index = 1; index < tCount; ++index) {
256 dxy1 = dxy2;
257 dxdy_at_t(q2, tsFound[index], dxy2.x, dxy2.y);
258 double dot = dxy1.dot(dxy2);
259 if (dot < 0) {
260 split = index - 1;
261 break;
262 }
263 }
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000264
caryclark@google.com73ca6242013-01-17 21:02:47 +0000265 }
266 if (split == 0) { // there's one point
267 if (addIntercept(q1, q2, tMin, tMax, i)) {
268 return true;
269 }
270 i.swap();
271 return isLinearInner(q2, tMin, tMax, q1, t1s, t1e, i);
272 }
273 // At this point, we have two ranges of t values -- treat each separately at the split
274 bool result;
275 if (addIntercept(q1, q2, tMin, tsFound[split - 1], i)) {
276 result = true;
277 } else {
278 i.swap();
279 result = isLinearInner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i);
280 }
281 if (addIntercept(q1, q2, tsFound[split], tMax, i)) {
282 result = true;
283 } else {
284 i.swap();
285 result |= isLinearInner(q2, tsFound[split], tMax, q1, t1s, t1e, i);
286 }
287 return result;
288}
289
290static double flatMeasure(const Quadratic& q) {
291 _Point mid;
292 xy_at_t(q, 0.5, mid.x, mid.y);
293 double dx = q[2].x - q[0].x;
294 double dy = q[2].y - q[0].y;
295 double length = sqrt(dx * dx + dy * dy); // OPTIMIZE: get rid of sqrt
296 return ((mid.x - q[0].x) * dy - (mid.y - q[0].y) * dx) / length;
297}
298
299// FIXME ? should this measure both and then use the quad that is the flattest as the line?
300static bool isLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
301 double measure = flatMeasure(q1);
302 // OPTIMIZE: (get rid of sqrt) use approximately_zero
303 if (!approximately_zero_sqrt(measure)) {
304 return false;
305 }
306 return isLinearInner(q1, 0, 1, q2, 0, 1, i);
307}
308
309static bool relaxedIsLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
310 double m1 = flatMeasure(q1);
311 double m2 = flatMeasure(q2);
312 if (fabs(m1) < fabs(m2)) {
313 isLinearInner(q1, 0, 1, q2, 0, 1, i);
314 return false;
315 } else {
316 isLinearInner(q2, 0, 1, q1, 0, 1, i);
317 return true;
318 }
319}
320
321#if 0
322static void unsortableExpanse(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
323 const Quadratic* qs[2] = { &q1, &q2 };
324 // need t values for start and end of unsortable expanse on both curves
325 // try projecting lines parallel to the end points
326 i.fT[0][0] = 0;
327 i.fT[0][1] = 1;
328 int flip = -1; // undecided
329 for (int qIdx = 0; qIdx < 2; qIdx++) {
330 for (int t = 0; t < 2; t++) {
331 _Point dxdy;
332 dxdy_at_t(*qs[qIdx], t, dxdy.x, dxdy.y);
333 _Line perp;
334 perp[0] = perp[1] = (*qs[qIdx])[t == 0 ? 0 : 2];
335 perp[0].x += dxdy.y;
336 perp[0].y -= dxdy.x;
337 perp[1].x -= dxdy.y;
338 perp[1].y += dxdy.x;
339 Intersections hitData;
340 int hits = intersectRay(*qs[qIdx ^ 1], perp, hitData);
341 assert(hits <= 1);
342 if (hits) {
343 if (flip < 0) {
344 _Point dxdy2;
345 dxdy_at_t(*qs[qIdx ^ 1], hitData.fT[0][0], dxdy2.x, dxdy2.y);
346 double dot = dxdy.dot(dxdy2);
347 flip = dot < 0;
348 i.fT[1][0] = flip;
349 i.fT[1][1] = !flip;
350 }
351 i.fT[qIdx ^ 1][t ^ flip] = hitData.fT[0][0];
352 }
353 }
354 }
355 i.fUnsortable = true; // failed, probably coincident or near-coincident
356 i.fUsed = 2;
357}
358#endif
359
caryclark@google.com235f56a2012-09-14 14:19:30 +0000360bool intersect2(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000361 // if the quads share an end point, check to see if they overlap
362
363 if (onlyEndPtsInCommon(q1, q2, i)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000364 return i.intersected();
365 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000366 if (onlyEndPtsInCommon(q2, q1, i)) {
367 i.swapPts();
368 return i.intersected();
369 }
370 // see if either quad is really a line
371 if (isLinear(q1, q2, i)) {
372 return i.intersected();
373 }
374 if (isLinear(q2, q1, i)) {
375 i.swapPts();
376 return i.intersected();
377 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000378 QuadImplicitForm i1(q1);
379 QuadImplicitForm i2(q2);
380 if (i1.implicit_match(i2)) {
381 // FIXME: compute T values
382 // compute the intersections of the ends to find the coincident span
383 bool useVertical = fabs(q1[0].x - q1[2].x) < fabs(q1[0].y - q1[2].y);
384 double t;
385 if ((t = axialIntersect(q1, q2[0], useVertical)) >= 0) {
386 i.addCoincident(t, 0);
387 }
388 if ((t = axialIntersect(q1, q2[2], useVertical)) >= 0) {
389 i.addCoincident(t, 1);
390 }
391 useVertical = fabs(q2[0].x - q2[2].x) < fabs(q2[0].y - q2[2].y);
392 if ((t = axialIntersect(q2, q1[0], useVertical)) >= 0) {
393 i.addCoincident(0, t);
394 }
395 if ((t = axialIntersect(q2, q1[2], useVertical)) >= 0) {
396 i.addCoincident(1, t);
397 }
398 assert(i.fCoincidentUsed <= 2);
399 return i.fCoincidentUsed > 0;
400 }
401 double roots1[4], roots2[4];
caryclark@google.com73ca6242013-01-17 21:02:47 +0000402 bool disregardCount1 = false;
403 bool disregardCount2 = false;
404 bool useCubic = q1[0] == q2[0] || q1[0] == q2[2] || q1[2] == q2[0];
405 int rootCount = findRoots(i2, q1, roots1, useCubic, disregardCount1);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000406 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
caryclark@google.com73ca6242013-01-17 21:02:47 +0000407 int rootCount2 = findRoots(i1, q2, roots2, useCubic, disregardCount2);
408 #if 0
409 if (rootCount != rootCount2 && !disregardCount1 && !disregardCount2) {
410 unsortableExpanse(q1, q2, i);
411 return false;
412 }
413 #endif
caryclark@google.com235f56a2012-09-14 14:19:30 +0000414 addValidRoots(roots1, rootCount, 0, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000415 addValidRoots(roots2, rootCount2, 1, i);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000416 if (i.insertBalanced() && i.fUsed <= 1) {
417 if (i.fUsed == 1) {
418 _Point xy1, xy2;
419 xy_at_t(q1, i.fT[0][0], xy1.x, xy1.y);
420 xy_at_t(q2, i.fT[1][0], xy2.x, xy2.y);
421 if (!xy1.approximatelyEqual(xy2)) {
422 --i.fUsed;
423 --i.fUsed2;
424 }
425 }
426 return i.intersected();
427 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000428 _Point pts[4];
caryclark@google.com0b7da432012-10-31 19:00:20 +0000429 int closest[4];
430 double dist[4];
caryclark@google.comd1688742012-09-18 20:08:37 +0000431 int index, ndex2;
432 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
433 xy_at_t(q2, i.fT[1][ndex2], pts[ndex2].x, pts[ndex2].y);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000434 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000435 bool foundSomething = false;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000436 for (index = 0; index < i.fUsed; ++index) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000437 _Point xy;
438 xy_at_t(q1, i.fT[0][index], xy.x, xy.y);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000439 dist[index] = DBL_MAX;
440 closest[index] = -1;
caryclark@google.comd1688742012-09-18 20:08:37 +0000441 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
caryclark@google.com0b7da432012-10-31 19:00:20 +0000442 if (!pts[ndex2].approximatelyEqual(xy)) {
443 continue;
444 }
445 double dx = pts[ndex2].x - xy.x;
446 double dy = pts[ndex2].y - xy.y;
447 double distance = dx * dx + dy * dy;
448 if (dist[index] <= distance) {
449 continue;
450 }
451 for (int outer = 0; outer < index; ++outer) {
452 if (closest[outer] != ndex2) {
453 continue;
454 }
455 if (dist[outer] < distance) {
456 goto next;
457 }
458 closest[outer] = -1;
459 }
460 dist[index] = distance;
461 closest[index] = ndex2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000462 foundSomething = true;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000463 next:
464 ;
465 }
466 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000467 if (i.fUsed && i.fUsed2 && !foundSomething) {
468 if (relaxedIsLinear(q1, q2, i)) {
469 i.swapPts();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000470 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000471 return i.intersected();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000472 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000473 double roots1Copy[4], roots2Copy[4];
474 memcpy(roots1Copy, i.fT[0], i.fUsed * sizeof(double));
475 memcpy(roots2Copy, i.fT[1], i.fUsed2 * sizeof(double));
476 int used = 0;
477 do {
478 double lowest = DBL_MAX;
479 int lowestIndex = -1;
480 for (index = 0; index < i.fUsed; ++index) {
481 if (closest[index] < 0) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000482 continue;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000483 }
484 if (roots1Copy[index] < lowest) {
485 lowestIndex = index;
486 lowest = roots1Copy[index];
487 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000488 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000489 if (lowestIndex < 0) {
490 break;
491 }
492 i.fT[0][used] = roots1Copy[lowestIndex];
493 i.fT[1][used] = roots2Copy[closest[lowestIndex]];
494 closest[lowestIndex] = -1;
495 } while (++used < i.fUsed);
496 i.fUsed = i.fUsed2 = used;
497 i.fFlip = false;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000498 return i.intersected();
499}