blob: e2e712f1b3cb51a857a45e4f2724e0565bf6119d [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00002#include "Intersections.h"
3#include "LineUtilities.h"
4#include "QuadraticUtilities.h"
5
6/*
7Find the interection of a line and quadratic by solving for valid t values.
8
9From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve
10
11"A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three
12control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where
13A, B and C are points and t goes from zero to one.
14
15This will give you two equations:
16
17 x = a(1 - t)^2 + b(1 - t)t + ct^2
18 y = d(1 - t)^2 + e(1 - t)t + ft^2
19
20If you add for instance the line equation (y = kx + m) to that, you'll end up
21with three equations and three unknowns (x, y and t)."
22
23Similar to above, the quadratic is represented as
24 x = a(1-t)^2 + 2b(1-t)t + ct^2
25 y = d(1-t)^2 + 2e(1-t)t + ft^2
26and the line as
27 y = g*x + h
28
29Using Mathematica, solve for the values of t where the quadratic intersects the
30line:
31
32 (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x,
33 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x]
34 (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 +
35 g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2)
36 (in) Solve[t1 == 0, t]
37 (out) {
38 {t -> (-2 d + 2 e + 2 a g - 2 b g -
39 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
40 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
41 (2 (-d + 2 e - f + a g - 2 b g + c g))
42 },
43 {t -> (-2 d + 2 e + 2 a g - 2 b g +
44 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
45 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
46 (2 (-d + 2 e - f + a g - 2 b g + c g))
47 }
48 }
49
50Numeric Solutions (5.6) suggests to solve the quadratic by computing
51
52 Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C))
53
54and using the roots
55
56 t1 = Q / A
57 t2 = C / Q
58
59Using the results above (when the line tends towards horizontal)
60 A = (-(d - 2*e + f) + g*(a - 2*b + c) )
61 B = 2*( (d - e ) - g*(a - b ) )
62 C = (-(d ) + g*(a ) + h )
63
64If g goes to infinity, we can rewrite the line in terms of x.
65 x = g'*y + h'
66
67And solve accordingly in Mathematica:
68
69 (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h',
70 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y]
71 (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 -
72 g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2)
73 (in) Solve[t2 == 0, t]
74 (out) {
75 {t -> (2 a - 2 b - 2 d g' + 2 e g' -
76 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
77 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) /
78 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
79 },
80 {t -> (2 a - 2 b - 2 d g' + 2 e g' +
81 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
82 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/
83 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
84 }
85 }
86
87Thus, if the slope of the line tends towards vertical, we use:
88 A = ( (a - 2*b + c) - g'*(d - 2*e + f) )
89 B = 2*(-(a - b ) + g'*(d - e ) )
90 C = ( (a ) - g'*(d ) - h' )
91 */
92
93
94class LineQuadraticIntersections : public Intersections {
95public:
96
97LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i)
98 : quad(q)
99 , line(l)
100 , intersections(i) {
101}
102
103bool intersect() {
104 double slope;
105 double axisIntercept;
106 moreHorizontal = implicitLine(line, slope, axisIntercept);
107 double A = quad[2].x; // c
108 double B = quad[1].x; // b
109 double C = quad[0].x; // a
110 A += C - 2 * B; // A = a - 2*b + c
111 B -= C; // B = -(a - b)
112 double D = quad[2].y; // f
113 double E = quad[1].y; // e
114 double F = quad[0].y; // d
115 D += F - 2 * E; // D = d - 2*e + f
116 E -= F; // E = -(d - e)
117 if (moreHorizontal) {
118 A = A * slope - D;
119 B = B * slope - E;
120 C = C * slope - F + axisIntercept;
121 } else {
122 A = A - D * slope;
123 B = B - E * slope;
124 C = C - F * slope - axisIntercept;
125 }
126 double t[2];
127 int roots = quadraticRoots(A, B, C, t);
128 for (int x = 0; x < roots; ++x) {
129 intersections.add(t[x], findLineT(t[x]));
130 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000131 // FIXME: quadratic root doesn't find t=0 or t=1, necessitating the hack below
132 if (roots == 0 || (roots == 1 && intersections.fT[0][0] >= FLT_EPSILON)) {
133 if (quad[0] == line[0]) {
134 intersections.fT[0][roots] = 0;
135 intersections.fT[1][roots++] = 0;
136 intersections.fUsed++;
137 } else if (quad[0] == line[1]) {
138 intersections.fT[0][roots] = 0;
139 intersections.fT[1][roots++] = 1;
140 intersections.fUsed++;
141 }
142 }
143 if (roots == 0 || (roots == 1 && intersections.fT[0][0] <= 1 - FLT_EPSILON)) {
144 if (quad[2] == line[1]) {
145 intersections.fT[0][roots] = 1;
146 intersections.fT[1][roots++] = 1;
147 intersections.fUsed++;
148 } else if (quad[2] == line[0]) {
149 intersections.fT[0][roots] = 1;
150 intersections.fT[1][roots++] = 0;
151 intersections.fUsed++;
152 }
153 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000154 return roots > 0;
155}
156
caryclark@google.com198e0542012-03-30 18:47:02 +0000157int horizontalIntersect(double axisIntercept) {
158 double D = quad[2].y; // f
159 double E = quad[1].y; // e
160 double F = quad[0].y; // d
161 D += F - 2 * E; // D = d - 2*e + f
162 E -= F; // E = -(d - e)
163 F -= axisIntercept;
caryclark@google.com24bec792012-08-20 12:43:57 +0000164 int roots = quadraticRoots(D, E, F, intersections.fT[0]);
165 // FIXME: ? quadraticRoots doesn't pick up intersections at 0, 1
166 if (roots < 2 && fabs(F) < FLT_EPSILON
167 && (roots == 0 || intersections.fT[0][0] >= FLT_EPSILON)) {
168 intersections.fT[0][roots++] = 0;
169 }
170 if (roots < 2 && fabs(quad[2].y - axisIntercept) < FLT_EPSILON
171 && (roots == 0 || intersections.fT[0][0] <= 1 - FLT_EPSILON)) {
172 intersections.fT[0][roots++] = 1;
173 }
174 return roots;
caryclark@google.com198e0542012-03-30 18:47:02 +0000175}
176
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000177int verticalIntersect(double axisIntercept) {
178 double D = quad[2].x; // f
179 double E = quad[1].x; // e
180 double F = quad[0].x; // d
181 D += F - 2 * E; // D = d - 2*e + f
182 E -= F; // E = -(d - e)
183 F -= axisIntercept;
caryclark@google.com24bec792012-08-20 12:43:57 +0000184 int roots = quadraticRoots(D, E, F, intersections.fT[0]);
185 // FIXME: ? quadraticRoots doesn't pick up intersections at 0, 1
186 if (roots < 2 && fabs(F) < FLT_EPSILON
187 && (roots == 0 || intersections.fT[0][0] >= FLT_EPSILON)) {
188 intersections.fT[0][roots++] = 0;
189 }
190 if (roots < 2 && fabs(quad[2].x - axisIntercept) < FLT_EPSILON
191 && (roots == 0 || intersections.fT[0][0] <= 1 - FLT_EPSILON)) {
192 intersections.fT[0][roots++] = 1;
193 }
194 return roots;
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000195}
196
caryclark@google.com27accef2012-01-25 18:57:23 +0000197protected:
198
199double findLineT(double t) {
200 const double* qPtr;
201 const double* lPtr;
202 if (moreHorizontal) {
203 qPtr = &quad[0].x;
204 lPtr = &line[0].x;
205 } else {
206 qPtr = &quad[0].y;
207 lPtr = &line[0].y;
208 }
209 double s = 1 - t;
210 double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t;
211 return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]);
212}
213
214private:
215
216const Quadratic& quad;
217const _Line& line;
218Intersections& intersections;
219bool moreHorizontal;
220
221};
caryclark@google.com198e0542012-03-30 18:47:02 +0000222
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000223// utility for pairs of coincident quads
224static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
225 Intersections intersections;
226 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
227 int result = q.horizontalIntersect(pt.y);
228 if (result == 0) {
229 return -1;
230 }
231 assert(result == 1);
232 double x, y;
233 xy_at_t(quad, intersections.fT[0][0], x, y);
234 if (approximately_equal(x, pt.x)) {
235 return intersections.fT[0][0];
236 }
237 return -1;
238}
239
240static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
241 Intersections intersections;
242 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
243 int result = q.horizontalIntersect(pt.x);
244 if (result == 0) {
245 return -1;
246 }
247 assert(result == 1);
248 double x, y;
249 xy_at_t(quad, intersections.fT[0][0], x, y);
250 if (approximately_equal(y, pt.y)) {
251 return intersections.fT[0][0];
252 }
253 return -1;
254}
255
256double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
257 if (vertical) {
258 return verticalIntersect(q1, p);
259 }
260 return horizontalIntersect(q1, p);
261}
262
caryclark@google.com198e0542012-03-30 18:47:02 +0000263int horizontalIntersect(const Quadratic& quad, double left, double right,
264 double y, double tRange[2]) {
265 Intersections i;
266 LineQuadraticIntersections q(quad, *((_Line*) 0), i);
267 int result = q.horizontalIntersect(y);
268 int tCount = 0;
269 for (int index = 0; index < result; ++index) {
270 double x, y;
271 xy_at_t(quad, i.fT[0][index], x, y);
272 if (x < left || x > right) {
273 continue;
274 }
275 tRange[tCount++] = i.fT[0][index];
276 }
277 return tCount;
278}
279
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000280int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
281 bool flipped, Intersections& intersections) {
282 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
283 int result = q.horizontalIntersect(y);
284 for (int index = 0; index < result; ) {
285 double x, y;
286 xy_at_t(quad, intersections.fT[0][index], x, y);
287 if (x < left || x > right) {
288 if (--result > index) {
289 intersections.fT[0][index] = intersections.fT[0][result];
290 }
291 continue;
292 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000293 intersections.fT[1][index] = (x - left) / (right - left);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000294 ++index;
295 }
296 if (flipped) {
297 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
298 for (int index = 0; index < result; ++index) {
299 intersections.fT[1][index] = 1 - intersections.fT[1][index];
300 }
301 }
302 return result;
303}
304
305int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
306 bool flipped, Intersections& intersections) {
307 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
308 int result = q.verticalIntersect(x);
309 for (int index = 0; index < result; ) {
310 double x, y;
311 xy_at_t(quad, intersections.fT[0][index], x, y);
312 if (y < top || y > bottom) {
313 if (--result > index) {
314 intersections.fT[0][index] = intersections.fT[0][result];
315 }
316 continue;
317 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000318 intersections.fT[1][index] = (y - top) / (bottom - top);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000319 ++index;
320 }
321 if (flipped) {
322 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
323 for (int index = 0; index < result; ++index) {
324 intersections.fT[1][index] = 1 - intersections.fT[1][index];
325 }
326 }
327 return result;
328}
329
caryclark@google.comc6825902012-02-03 22:07:47 +0000330bool intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000331 LineQuadraticIntersections q(quad, line, i);
332 return q.intersect();
333}