blob: d892ae97e02c6a751a66e80e24a3a80bed54e7a6 [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
8#include "CurveIntersection.h"
9#include "Intersections.h"
10#include "QuadraticParameterization.h"
11#include "QuarticRoot.h"
12#include "QuadraticUtilities.h"
13
14/* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F
15 * and given x = at^2 + bt + c (the parameterized form)
16 * y = dt^2 + et + f
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000017 * then
caryclark@google.com235f56a2012-09-14 14:19:30 +000018 * 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
19 */
20
21static int findRoots(const QuadImplicitForm& i, const Quadratic& q2, double roots[4]) {
22 double a, b, c;
23 set_abc(&q2[0].x, a, b, c);
24 double d, e, f;
25 set_abc(&q2[0].y, d, e, f);
26 const double t4 = i.x2() * a * a
27 + i.xy() * a * d
28 + i.y2() * d * d;
29 const double t3 = 2 * i.x2() * a * b
30 + i.xy() * (a * e + b * d)
31 + 2 * i.y2() * d * e;
32 const double t2 = i.x2() * (b * b + 2 * a * c)
33 + i.xy() * (c * d + b * e + a * f)
34 + i.y2() * (e * e + 2 * d * f)
35 + i.x() * a
36 + i.y() * d;
37 const double t1 = 2 * i.x2() * b * c
38 + i.xy() * (c * e + b * f)
39 + 2 * i.y2() * e * f
40 + i.x() * b
41 + i.y() * e;
42 const double t0 = i.x2() * c * c
43 + i.xy() * c * f
44 + i.y2() * f * f
45 + i.x() * c
46 + i.y() * f
47 + i.c();
48 return quarticRoots(t4, t3, t2, t1, t0, roots);
49}
50
51static void addValidRoots(const double roots[4], const int count, const int side, Intersections& i) {
52 int index;
53 for (index = 0; index < count; ++index) {
54 if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) {
55 continue;
56 }
57 double t = 1 - roots[index];
58 if (approximately_less_than_zero(t)) {
59 t = 0;
60 } else if (approximately_greater_than_one(t)) {
61 t = 1;
62 }
63 i.insertOne(t, side);
64 }
65}
66
caryclark@google.com6aea33f2012-10-09 14:11:58 +000067static bool onlyEndPtsInCommon(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
68// the idea here is to see at minimum do a quick reject by rotating all points
69// to either side of the line formed by connecting the endpoints
70// if the opposite curves points are on the line or on the other side, the
71// curves at most intersect at the endpoints
72 for (int oddMan = 0; oddMan < 3; ++oddMan) {
73 const _Point* endPt[2];
74 for (int opp = 1; opp < 3; ++opp) {
75 int end = oddMan ^ opp;
76 if (end == 3) {
77 end = opp;
78 }
79 endPt[opp - 1] = &q1[end];
80 }
81 double origX = endPt[0]->x;
82 double origY = endPt[0]->y;
83 double adj = endPt[1]->x - origX;
84 double opp = endPt[1]->y - origY;
85 double sign = (q1[oddMan].y - origY) * adj - (q1[oddMan].x - origX) * opp;
86 assert(!approximately_zero(sign));
87 for (int n = 0; n < 3; ++n) {
88 double test = (q2[n].y - origY) * adj - (q2[n].x - origX) * opp;
89 if (test * sign > 0) {
90 goto tryNextHalfPlane;
91 }
92 }
93 for (int i1 = 0; i1 < 3; i1 += 2) {
94 for (int i2 = 0; i2 < 3; i2 += 2) {
95 if (q1[i1] == q2[i2]) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +000096 i.insert(i1 >> 1, i2 >> 1);
caryclark@google.com6aea33f2012-10-09 14:11:58 +000097 }
98 }
99 }
100 assert(i.fUsed < 3);
101 return true;
102tryNextHalfPlane:
103 ;
104 }
105 return false;
106}
107
caryclark@google.com235f56a2012-09-14 14:19:30 +0000108bool intersect2(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000109 // if the quads share an end point, check to see if they overlap
110
111 if (onlyEndPtsInCommon(q1, q2, i)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000112 return i.intersected();
113 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000114 QuadImplicitForm i1(q1);
115 QuadImplicitForm i2(q2);
116 if (i1.implicit_match(i2)) {
117 // FIXME: compute T values
118 // compute the intersections of the ends to find the coincident span
119 bool useVertical = fabs(q1[0].x - q1[2].x) < fabs(q1[0].y - q1[2].y);
120 double t;
121 if ((t = axialIntersect(q1, q2[0], useVertical)) >= 0) {
122 i.addCoincident(t, 0);
123 }
124 if ((t = axialIntersect(q1, q2[2], useVertical)) >= 0) {
125 i.addCoincident(t, 1);
126 }
127 useVertical = fabs(q2[0].x - q2[2].x) < fabs(q2[0].y - q2[2].y);
128 if ((t = axialIntersect(q2, q1[0], useVertical)) >= 0) {
129 i.addCoincident(0, t);
130 }
131 if ((t = axialIntersect(q2, q1[2], useVertical)) >= 0) {
132 i.addCoincident(1, t);
133 }
134 assert(i.fCoincidentUsed <= 2);
135 return i.fCoincidentUsed > 0;
136 }
137 double roots1[4], roots2[4];
138 int rootCount = findRoots(i2, q1, roots1);
139 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
caryclark@google.comd1688742012-09-18 20:08:37 +0000140#ifndef NDEBUG
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000141 int rootCount2 =
caryclark@google.comd1688742012-09-18 20:08:37 +0000142#endif
143 findRoots(i1, q2, roots2);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000144 assert(rootCount == rootCount2);
145 addValidRoots(roots1, rootCount, 0, i);
146 addValidRoots(roots2, rootCount, 1, i);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000147 if (i.insertBalanced() && i.fUsed <= 1) {
148 if (i.fUsed == 1) {
149 _Point xy1, xy2;
150 xy_at_t(q1, i.fT[0][0], xy1.x, xy1.y);
151 xy_at_t(q2, i.fT[1][0], xy2.x, xy2.y);
152 if (!xy1.approximatelyEqual(xy2)) {
153 --i.fUsed;
154 --i.fUsed2;
155 }
156 }
157 return i.intersected();
158 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000159 _Point pts[4];
160 bool matches[4];
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000161 int flipCheck[4];
caryclark@google.com0b7da432012-10-31 19:00:20 +0000162 int closest[4];
163 double dist[4];
caryclark@google.comd1688742012-09-18 20:08:37 +0000164 int index, ndex2;
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000165 int flipIndex = 0;
caryclark@google.comd1688742012-09-18 20:08:37 +0000166 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
167 xy_at_t(q2, i.fT[1][ndex2], pts[ndex2].x, pts[ndex2].y);
168 matches[ndex2] = false;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000169 }
caryclark@google.com0b7da432012-10-31 19:00:20 +0000170 for (index = 0; index < i.fUsed; ++index) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000171 _Point xy;
172 xy_at_t(q1, i.fT[0][index], xy.x, xy.y);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000173 dist[index] = DBL_MAX;
174 closest[index] = -1;
caryclark@google.comd1688742012-09-18 20:08:37 +0000175 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
caryclark@google.com0b7da432012-10-31 19:00:20 +0000176 if (!pts[ndex2].approximatelyEqual(xy)) {
177 continue;
178 }
179 double dx = pts[ndex2].x - xy.x;
180 double dy = pts[ndex2].y - xy.y;
181 double distance = dx * dx + dy * dy;
182 if (dist[index] <= distance) {
183 continue;
184 }
185 for (int outer = 0; outer < index; ++outer) {
186 if (closest[outer] != ndex2) {
187 continue;
188 }
189 if (dist[outer] < distance) {
190 goto next;
191 }
192 closest[outer] = -1;
193 }
194 dist[index] = distance;
195 closest[index] = ndex2;
196 next:
197 ;
198 }
199 }
200 for (index = 0; index < i.fUsed; ) {
201 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
202 if (closest[index] == ndex2) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000203 assert(flipIndex < 4);
204 flipCheck[flipIndex++] = ndex2;
caryclark@google.comd1688742012-09-18 20:08:37 +0000205 matches[ndex2] = true;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000206 goto next2;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000207 }
208 }
209 if (--i.fUsed > index) {
210 memmove(&i.fT[0][index], &i.fT[0][index + 1], (i.fUsed - index) * sizeof(i.fT[0][0]));
caryclark@google.com0b7da432012-10-31 19:00:20 +0000211 memmove(&closest[index], &closest[index + 1], (i.fUsed - index) * sizeof(closest[0]));
caryclark@google.com235f56a2012-09-14 14:19:30 +0000212 continue;
213 }
caryclark@google.com0b7da432012-10-31 19:00:20 +0000214 next2:
caryclark@google.com235f56a2012-09-14 14:19:30 +0000215 ++index;
216 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000217 for (ndex2 = 0; ndex2 < i.fUsed2; ) {
218 if (!matches[ndex2]) {
219 if (--i.fUsed2 > ndex2) {
220 memmove(&i.fT[1][ndex2], &i.fT[1][ndex2 + 1], (i.fUsed2 - ndex2) * sizeof(i.fT[1][0]));
221 memmove(&matches[ndex2], &matches[ndex2 + 1], (i.fUsed2 - ndex2) * sizeof(matches[0]));
caryclark@google.com235f56a2012-09-14 14:19:30 +0000222 continue;
223 }
224 }
caryclark@google.comd1688742012-09-18 20:08:37 +0000225 ++ndex2;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000226 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000227 i.fFlip = i.fUsed >= 2 && flipCheck[0] > flipCheck[1];
caryclark@google.com235f56a2012-09-14 14:19:30 +0000228 assert(i.insertBalanced());
229 return i.intersected();
230}