blob: 2d9d9b9a881225914b6c52e56a29f9d2e3f95e47 [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;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000191 _Point dxdy;
192 dxdy_at_t(q2, tMid, dxdy);
193 line[0].x -= dxdy.x;
194 line[0].y -= dxdy.y;
195 line[1].x += dxdy.x;
196 line[1].y += dxdy.y;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000197 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) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000254 dxdy_at_t(q2, tMin, dxy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000255 for (int index = 1; index < tCount; ++index) {
256 dxy1 = dxy2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000257 dxdy_at_t(q2, tsFound[index], dxy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000258 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);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000312 i.reset();
caryclark@google.com73ca6242013-01-17 21:02:47 +0000313 if (fabs(m1) < fabs(m2)) {
314 isLinearInner(q1, 0, 1, q2, 0, 1, i);
315 return false;
316 } else {
317 isLinearInner(q2, 0, 1, q1, 0, 1, i);
318 return true;
319 }
320}
321
322#if 0
323static void unsortableExpanse(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
324 const Quadratic* qs[2] = { &q1, &q2 };
325 // need t values for start and end of unsortable expanse on both curves
326 // try projecting lines parallel to the end points
327 i.fT[0][0] = 0;
328 i.fT[0][1] = 1;
329 int flip = -1; // undecided
330 for (int qIdx = 0; qIdx < 2; qIdx++) {
331 for (int t = 0; t < 2; t++) {
332 _Point dxdy;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000333 dxdy_at_t(*qs[qIdx], t, dxdy);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000334 _Line perp;
335 perp[0] = perp[1] = (*qs[qIdx])[t == 0 ? 0 : 2];
336 perp[0].x += dxdy.y;
337 perp[0].y -= dxdy.x;
338 perp[1].x -= dxdy.y;
339 perp[1].y += dxdy.x;
340 Intersections hitData;
341 int hits = intersectRay(*qs[qIdx ^ 1], perp, hitData);
342 assert(hits <= 1);
343 if (hits) {
344 if (flip < 0) {
345 _Point dxdy2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000346 dxdy_at_t(*qs[qIdx ^ 1], hitData.fT[0][0], dxdy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000347 double dot = dxdy.dot(dxdy2);
348 flip = dot < 0;
349 i.fT[1][0] = flip;
350 i.fT[1][1] = !flip;
351 }
352 i.fT[qIdx ^ 1][t ^ flip] = hitData.fT[0][0];
353 }
354 }
355 }
356 i.fUnsortable = true; // failed, probably coincident or near-coincident
357 i.fUsed = 2;
358}
359#endif
360
caryclark@google.com235f56a2012-09-14 14:19:30 +0000361bool intersect2(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000362 // if the quads share an end point, check to see if they overlap
363
364 if (onlyEndPtsInCommon(q1, q2, i)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000365 return i.intersected();
366 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000367 if (onlyEndPtsInCommon(q2, q1, i)) {
368 i.swapPts();
369 return i.intersected();
370 }
371 // see if either quad is really a line
372 if (isLinear(q1, q2, i)) {
373 return i.intersected();
374 }
375 if (isLinear(q2, q1, i)) {
376 i.swapPts();
377 return i.intersected();
378 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000379 QuadImplicitForm i1(q1);
380 QuadImplicitForm i2(q2);
381 if (i1.implicit_match(i2)) {
382 // FIXME: compute T values
383 // compute the intersections of the ends to find the coincident span
384 bool useVertical = fabs(q1[0].x - q1[2].x) < fabs(q1[0].y - q1[2].y);
385 double t;
386 if ((t = axialIntersect(q1, q2[0], useVertical)) >= 0) {
387 i.addCoincident(t, 0);
388 }
389 if ((t = axialIntersect(q1, q2[2], useVertical)) >= 0) {
390 i.addCoincident(t, 1);
391 }
392 useVertical = fabs(q2[0].x - q2[2].x) < fabs(q2[0].y - q2[2].y);
393 if ((t = axialIntersect(q2, q1[0], useVertical)) >= 0) {
394 i.addCoincident(0, t);
395 }
396 if ((t = axialIntersect(q2, q1[2], useVertical)) >= 0) {
397 i.addCoincident(1, t);
398 }
399 assert(i.fCoincidentUsed <= 2);
400 return i.fCoincidentUsed > 0;
401 }
402 double roots1[4], roots2[4];
caryclark@google.com73ca6242013-01-17 21:02:47 +0000403 bool disregardCount1 = false;
404 bool disregardCount2 = false;
405 bool useCubic = q1[0] == q2[0] || q1[0] == q2[2] || q1[2] == q2[0];
406 int rootCount = findRoots(i2, q1, roots1, useCubic, disregardCount1);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000407 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
caryclark@google.com73ca6242013-01-17 21:02:47 +0000408 int rootCount2 = findRoots(i1, q2, roots2, useCubic, disregardCount2);
409 #if 0
410 if (rootCount != rootCount2 && !disregardCount1 && !disregardCount2) {
411 unsortableExpanse(q1, q2, i);
412 return false;
413 }
414 #endif
caryclark@google.com235f56a2012-09-14 14:19:30 +0000415 addValidRoots(roots1, rootCount, 0, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000416 addValidRoots(roots2, rootCount2, 1, i);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000417 if (i.insertBalanced() && i.fUsed <= 1) {
418 if (i.fUsed == 1) {
419 _Point xy1, xy2;
420 xy_at_t(q1, i.fT[0][0], xy1.x, xy1.y);
421 xy_at_t(q2, i.fT[1][0], xy2.x, xy2.y);
422 if (!xy1.approximatelyEqual(xy2)) {
423 --i.fUsed;
424 --i.fUsed2;
425 }
426 }
427 return i.intersected();
428 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000429 _Point pts[4];
caryclark@google.com0b7da432012-10-31 19:00:20 +0000430 int closest[4];
431 double dist[4];
caryclark@google.comd1688742012-09-18 20:08:37 +0000432 int index, ndex2;
433 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
434 xy_at_t(q2, i.fT[1][ndex2], pts[ndex2].x, pts[ndex2].y);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000435 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000436 bool foundSomething = false;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000437 for (index = 0; index < i.fUsed; ++index) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000438 _Point xy;
439 xy_at_t(q1, i.fT[0][index], xy.x, xy.y);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000440 dist[index] = DBL_MAX;
441 closest[index] = -1;
caryclark@google.comd1688742012-09-18 20:08:37 +0000442 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
caryclark@google.com0b7da432012-10-31 19:00:20 +0000443 if (!pts[ndex2].approximatelyEqual(xy)) {
444 continue;
445 }
446 double dx = pts[ndex2].x - xy.x;
447 double dy = pts[ndex2].y - xy.y;
448 double distance = dx * dx + dy * dy;
449 if (dist[index] <= distance) {
450 continue;
451 }
452 for (int outer = 0; outer < index; ++outer) {
453 if (closest[outer] != ndex2) {
454 continue;
455 }
456 if (dist[outer] < distance) {
457 goto next;
458 }
459 closest[outer] = -1;
460 }
461 dist[index] = distance;
462 closest[index] = ndex2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000463 foundSomething = true;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000464 next:
465 ;
466 }
467 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000468 if (i.fUsed && i.fUsed2 && !foundSomething) {
469 if (relaxedIsLinear(q1, q2, i)) {
470 i.swapPts();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000471 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000472 return i.intersected();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000473 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000474 double roots1Copy[4], roots2Copy[4];
475 memcpy(roots1Copy, i.fT[0], i.fUsed * sizeof(double));
476 memcpy(roots2Copy, i.fT[1], i.fUsed2 * sizeof(double));
477 int used = 0;
478 do {
479 double lowest = DBL_MAX;
480 int lowestIndex = -1;
481 for (index = 0; index < i.fUsed; ++index) {
482 if (closest[index] < 0) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000483 continue;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000484 }
485 if (roots1Copy[index] < lowest) {
486 lowestIndex = index;
487 lowest = roots1Copy[index];
488 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000489 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000490 if (lowestIndex < 0) {
491 break;
492 }
493 i.fT[0][used] = roots1Copy[lowestIndex];
494 i.fT[1][used] = roots2Copy[closest[lowestIndex]];
495 closest[lowestIndex] = -1;
496 } while (++used < i.fUsed);
497 i.fUsed = i.fUsed2 = used;
498 i.fFlip = false;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000499 return i.intersected();
500}