blob: c3e6d23e52071476796a233bd10f3e9305492818 [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
caryclark@google.com27accef2012-01-25 18:57:23 +000050Using the results above (when the line tends towards horizontal)
51 A = (-(d - 2*e + f) + g*(a - 2*b + c) )
52 B = 2*( (d - e ) - g*(a - b ) )
53 C = (-(d ) + g*(a ) + h )
54
55If g goes to infinity, we can rewrite the line in terms of x.
56 x = g'*y + h'
57
58And solve accordingly in Mathematica:
59
60 (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h',
61 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y]
62 (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 -
63 g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2)
64 (in) Solve[t2 == 0, t]
65 (out) {
66 {t -> (2 a - 2 b - 2 d g' + 2 e g' -
67 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
68 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) /
69 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
70 },
71 {t -> (2 a - 2 b - 2 d g' + 2 e g' +
72 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
73 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/
74 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
75 }
76 }
77
78Thus, if the slope of the line tends towards vertical, we use:
79 A = ( (a - 2*b + c) - g'*(d - 2*e + f) )
80 B = 2*(-(a - b ) + g'*(d - e ) )
81 C = ( (a ) - g'*(d ) - h' )
82 */
83
84
85class LineQuadraticIntersections : public Intersections {
86public:
87
88LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i)
89 : quad(q)
90 , line(l)
91 , intersections(i) {
92}
93
94bool intersect() {
95 double slope;
96 double axisIntercept;
97 moreHorizontal = implicitLine(line, slope, axisIntercept);
98 double A = quad[2].x; // c
99 double B = quad[1].x; // b
100 double C = quad[0].x; // a
101 A += C - 2 * B; // A = a - 2*b + c
102 B -= C; // B = -(a - b)
103 double D = quad[2].y; // f
104 double E = quad[1].y; // e
105 double F = quad[0].y; // d
106 D += F - 2 * E; // D = d - 2*e + f
107 E -= F; // E = -(d - e)
108 if (moreHorizontal) {
109 A = A * slope - D;
110 B = B * slope - E;
111 C = C * slope - F + axisIntercept;
112 } else {
113 A = A - D * slope;
114 B = B - E * slope;
115 C = C - F * slope - axisIntercept;
116 }
117 double t[2];
118 int roots = quadraticRoots(A, B, C, t);
caryclark@google.com03f97062012-08-21 13:13:52 +0000119 for (int x = 0; x < roots; ) {
120 double lineT = findLineT(t[x]);
121 if (lineT <= -FLT_EPSILON || lineT >= 1 + FLT_EPSILON) {
122 if (x < --roots) {
123 t[x] = t[roots];
124 }
125 continue;
caryclark@google.com24bec792012-08-20 12:43:57 +0000126 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000127 if (lineT < FLT_EPSILON) {
128 lineT = 0;
129 } else if (lineT > 1 - FLT_EPSILON) {
130 lineT = 1;
caryclark@google.com24bec792012-08-20 12:43:57 +0000131 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000132 intersections.add(t[x], lineT);
133 ++x;
caryclark@google.com24bec792012-08-20 12:43:57 +0000134 }
caryclark@google.com27accef2012-01-25 18:57:23 +0000135 return roots > 0;
136}
137
caryclark@google.com198e0542012-03-30 18:47:02 +0000138int horizontalIntersect(double axisIntercept) {
139 double D = quad[2].y; // f
140 double E = quad[1].y; // e
141 double F = quad[0].y; // d
142 D += F - 2 * E; // D = d - 2*e + f
143 E -= F; // E = -(d - e)
144 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000145 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.com198e0542012-03-30 18:47:02 +0000146}
147
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000148int verticalIntersect(double axisIntercept) {
149 double D = quad[2].x; // f
150 double E = quad[1].x; // e
151 double F = quad[0].x; // d
152 D += F - 2 * E; // D = d - 2*e + f
153 E -= F; // E = -(d - e)
154 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000155 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000156}
157
caryclark@google.com27accef2012-01-25 18:57:23 +0000158protected:
159
160double findLineT(double t) {
161 const double* qPtr;
162 const double* lPtr;
163 if (moreHorizontal) {
164 qPtr = &quad[0].x;
165 lPtr = &line[0].x;
166 } else {
167 qPtr = &quad[0].y;
168 lPtr = &line[0].y;
169 }
170 double s = 1 - t;
171 double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t;
172 return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]);
173}
174
175private:
176
177const Quadratic& quad;
178const _Line& line;
179Intersections& intersections;
180bool moreHorizontal;
181
182};
caryclark@google.com198e0542012-03-30 18:47:02 +0000183
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000184// utility for pairs of coincident quads
185static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
186 Intersections intersections;
187 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
188 int result = q.horizontalIntersect(pt.y);
189 if (result == 0) {
190 return -1;
191 }
192 assert(result == 1);
193 double x, y;
194 xy_at_t(quad, intersections.fT[0][0], x, y);
195 if (approximately_equal(x, pt.x)) {
196 return intersections.fT[0][0];
197 }
198 return -1;
199}
200
201static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
202 Intersections intersections;
203 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
204 int result = q.horizontalIntersect(pt.x);
205 if (result == 0) {
206 return -1;
207 }
208 assert(result == 1);
209 double x, y;
210 xy_at_t(quad, intersections.fT[0][0], x, y);
211 if (approximately_equal(y, pt.y)) {
212 return intersections.fT[0][0];
213 }
214 return -1;
215}
216
217double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
218 if (vertical) {
219 return verticalIntersect(q1, p);
220 }
221 return horizontalIntersect(q1, p);
222}
223
caryclark@google.com198e0542012-03-30 18:47:02 +0000224int horizontalIntersect(const Quadratic& quad, double left, double right,
225 double y, double tRange[2]) {
226 Intersections i;
227 LineQuadraticIntersections q(quad, *((_Line*) 0), i);
228 int result = q.horizontalIntersect(y);
229 int tCount = 0;
230 for (int index = 0; index < result; ++index) {
231 double x, y;
232 xy_at_t(quad, i.fT[0][index], x, y);
233 if (x < left || x > right) {
234 continue;
235 }
236 tRange[tCount++] = i.fT[0][index];
237 }
238 return tCount;
239}
240
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000241int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
242 bool flipped, Intersections& intersections) {
243 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
244 int result = q.horizontalIntersect(y);
245 for (int index = 0; index < result; ) {
246 double x, y;
247 xy_at_t(quad, intersections.fT[0][index], x, y);
caryclark@google.com03f97062012-08-21 13:13:52 +0000248 double lineT = (x - left) / (right - left);
249 if (lineT <= -FLT_EPSILON || lineT >= 1 + FLT_EPSILON) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000250 if (--result > index) {
251 intersections.fT[0][index] = intersections.fT[0][result];
252 }
253 continue;
254 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000255 if (lineT < FLT_EPSILON) {
256 lineT = 0;
257 } else if (lineT > 1 - FLT_EPSILON) {
258 lineT = 1;
259 }
260 intersections.fT[1][index] = lineT;
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000261 ++index;
262 }
263 if (flipped) {
264 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
265 for (int index = 0; index < result; ++index) {
266 intersections.fT[1][index] = 1 - intersections.fT[1][index];
267 }
268 }
269 return result;
270}
271
272int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
273 bool flipped, Intersections& intersections) {
274 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
275 int result = q.verticalIntersect(x);
276 for (int index = 0; index < result; ) {
277 double x, y;
278 xy_at_t(quad, intersections.fT[0][index], x, y);
caryclark@google.com03f97062012-08-21 13:13:52 +0000279 double lineT = (y - top) / (bottom - top);
280 if (lineT <= -FLT_EPSILON || lineT >= 1 + FLT_EPSILON) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000281 if (--result > index) {
282 intersections.fT[0][index] = intersections.fT[0][result];
283 }
284 continue;
285 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000286 if (lineT < FLT_EPSILON) {
287 lineT = 0;
288 } else if (lineT > 1 - FLT_EPSILON) {
289 lineT = 1;
290 }
291 intersections.fT[1][index] = lineT;
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000292 ++index;
293 }
294 if (flipped) {
295 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
296 for (int index = 0; index < result; ++index) {
297 intersections.fT[1][index] = 1 - intersections.fT[1][index];
298 }
299 }
300 return result;
301}
302
caryclark@google.comc6825902012-02-03 22:07:47 +0000303bool intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000304 LineQuadraticIntersections q(quad, line, i);
305 return q.intersect();
306}