blob: fc9c67623f46f8a15f9cb5c8c7485eacb028d7a5 [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
rmistry@google.comd6176b02012-08-23 18:14:13 +00006/*
caryclark@google.com27accef2012-01-25 18:57:23 +00007Find 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
rmistry@google.comd6176b02012-08-23 18:14:13 +000011"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
caryclark@google.com27accef2012-01-25 18:57:23 +000013A, 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
rmistry@google.comd6176b02012-08-23 18:14:13 +000020If you add for instance the line equation (y = kx + m) to that, you'll end up
caryclark@google.com27accef2012-01-25 18:57:23 +000021with 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
rmistry@google.comd6176b02012-08-23 18:14:13 +000032 (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x,
caryclark@google.com27accef2012-01-25 18:57:23 +000033 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x]
rmistry@google.comd6176b02012-08-23 18:14:13 +000034 (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 +
caryclark@google.com27accef2012-01-25 18:57:23 +000035 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 -
rmistry@google.comd6176b02012-08-23 18:14:13 +000039 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000040 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 +
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000045 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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000049
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
rmistry@google.comd6176b02012-08-23 18:14:13 +000060 (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h',
caryclark@google.com27accef2012-01-25 18:57:23 +000061 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y]
rmistry@google.comd6176b02012-08-23 18:14:13 +000062 (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000063 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' -
rmistry@google.comd6176b02012-08-23 18:14:13 +000067 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000068 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' +
rmistry@google.comd6176b02012-08-23 18:14:13 +000072 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000073 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 */
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
caryclark@google.com27accef2012-01-25 18:57:23 +000084
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
caryclark@google.com3350c3c2012-08-24 15:24:36 +000094int intersect() {
95/*
96 solve by rotating line+quad so line is horizontal, then finding the roots
97 set up matrix to rotate quad to x-axis
98 |cos(a) -sin(a)|
99 |sin(a) cos(a)|
100 note that cos(a) = A(djacent) / Hypoteneuse
101 sin(a) = O(pposite) / Hypoteneuse
102 since we are computing Ts, we can ignore hypoteneuse, the scale factor:
103 | A -O |
104 | O A |
105 A = line[1].x - line[0].x (adjacent side of the right triangle)
106 O = line[1].y - line[0].y (opposite side of the right triangle)
107 for each of the three points (e.g. n = 0 to 2)
108 quad[n].y' = (quad[n].y - line[0].y) * A - (quad[n].x - line[0].x) * O
109*/
110 double adj = line[1].x - line[0].x;
111 double opp = line[1].y - line[0].y;
112 double r[3];
113 for (int n = 0; n < 3; ++n) {
114 r[n] = (quad[n].y - line[0].y) * adj - (quad[n].x - line[0].x) * opp;
115 }
116 double A = r[2];
117 double B = r[1];
118 double C = r[0];
caryclark@google.com27accef2012-01-25 18:57:23 +0000119 A += C - 2 * B; // A = a - 2*b + c
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000120 B -= C; // B = -(b - c)
121 int roots = quadraticRoots(A, B, C, intersections.fT[0]);
122 for (int index = 0; index < roots; ) {
123 double lineT = findLineT(intersections.fT[0][index]);
124 if (lineIntersects(lineT, index, roots)) {
125 ++index;
caryclark@google.com24bec792012-08-20 12:43:57 +0000126 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000127 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000128 return roots;
caryclark@google.com27accef2012-01-25 18:57:23 +0000129}
130
caryclark@google.com198e0542012-03-30 18:47:02 +0000131int horizontalIntersect(double axisIntercept) {
132 double D = quad[2].y; // f
133 double E = quad[1].y; // e
134 double F = quad[0].y; // d
135 D += F - 2 * E; // D = d - 2*e + f
136 E -= F; // E = -(d - e)
137 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000138 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.com198e0542012-03-30 18:47:02 +0000139}
140
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000141int horizontalIntersect(double axisIntercept, double left, double right) {
142 int roots = horizontalIntersect(axisIntercept);
143 for (int index = 0; index < roots; ) {
144 double x;
145 xy_at_t(quad, intersections.fT[0][index], x, *(double*) NULL);
146 double lineT = (x - left) / (right - left);
147 if (lineIntersects(lineT, index, roots)) {
148 ++index;
149 }
150 }
151 return roots;
152}
153
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000154int verticalIntersect(double axisIntercept) {
155 double D = quad[2].x; // f
156 double E = quad[1].x; // e
157 double F = quad[0].x; // d
158 D += F - 2 * E; // D = d - 2*e + f
159 E -= F; // E = -(d - e)
160 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000161 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000162}
163
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000164int verticalIntersect(double axisIntercept, double top, double bottom) {
165 int roots = verticalIntersect(axisIntercept);
166 for (int index = 0; index < roots; ) {
167 double y;
168 xy_at_t(quad, intersections.fT[0][index], *(double*) NULL, y);
169 double lineT = (y - top) / (bottom - top);
170 if (lineIntersects(lineT, index, roots)) {
171 ++index;
172 }
173 }
174 return roots;
175}
176
caryclark@google.com27accef2012-01-25 18:57:23 +0000177protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178
caryclark@google.com27accef2012-01-25 18:57:23 +0000179double findLineT(double t) {
180 const double* qPtr;
181 const double* lPtr;
182 if (moreHorizontal) {
183 qPtr = &quad[0].x;
184 lPtr = &line[0].x;
185 } else {
186 qPtr = &quad[0].y;
187 lPtr = &line[0].y;
188 }
189 double s = 1 - t;
190 double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t;
191 return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]);
192}
193
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000194bool lineIntersects(double lineT, const int x, int& roots) {
195 if (!approximately_zero_or_more(lineT) || !approximately_one_or_less(lineT)) {
196 if (x < --roots) {
197 intersections.fT[0][x] = intersections.fT[0][roots];
198 }
199 return false;
200 }
201 if (approximately_less_than_zero(lineT)) {
202 lineT = 0;
203 } else if (approximately_greater_than_one(lineT)) {
204 lineT = 1;
205 }
206 intersections.fT[1][x] = lineT;
207 return true;
208}
209
caryclark@google.com27accef2012-01-25 18:57:23 +0000210private:
211
212const Quadratic& quad;
213const _Line& line;
214Intersections& intersections;
215bool moreHorizontal;
216
217};
caryclark@google.com198e0542012-03-30 18:47:02 +0000218
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000219// utility for pairs of coincident quads
220static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
221 Intersections intersections;
222 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
223 int result = q.horizontalIntersect(pt.y);
224 if (result == 0) {
225 return -1;
226 }
227 assert(result == 1);
228 double x, y;
229 xy_at_t(quad, intersections.fT[0][0], x, y);
230 if (approximately_equal(x, pt.x)) {
231 return intersections.fT[0][0];
232 }
233 return -1;
234}
235
236static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
237 Intersections intersections;
238 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
239 int result = q.horizontalIntersect(pt.x);
240 if (result == 0) {
241 return -1;
242 }
243 assert(result == 1);
244 double x, y;
245 xy_at_t(quad, intersections.fT[0][0], x, y);
246 if (approximately_equal(y, pt.y)) {
247 return intersections.fT[0][0];
248 }
249 return -1;
250}
251
252double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
253 if (vertical) {
254 return verticalIntersect(q1, p);
255 }
256 return horizontalIntersect(q1, p);
257}
258
caryclark@google.com198e0542012-03-30 18:47:02 +0000259int horizontalIntersect(const Quadratic& quad, double left, double right,
260 double y, double tRange[2]) {
261 Intersections i;
262 LineQuadraticIntersections q(quad, *((_Line*) 0), i);
263 int result = q.horizontalIntersect(y);
264 int tCount = 0;
265 for (int index = 0; index < result; ++index) {
266 double x, y;
267 xy_at_t(quad, i.fT[0][index], x, y);
268 if (x < left || x > right) {
269 continue;
270 }
271 tRange[tCount++] = i.fT[0][index];
272 }
273 return tCount;
274}
275
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000276int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
277 bool flipped, Intersections& intersections) {
278 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000279 int result = q.horizontalIntersect(y, left, right);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000280 if (flipped) {
281 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
282 for (int index = 0; index < result; ++index) {
283 intersections.fT[1][index] = 1 - intersections.fT[1][index];
284 }
285 }
286 return result;
287}
288
289int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
290 bool flipped, Intersections& intersections) {
291 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000292 int result = q.verticalIntersect(x, top, bottom);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000293 if (flipped) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000294 // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000295 for (int index = 0; index < result; ++index) {
296 intersections.fT[1][index] = 1 - intersections.fT[1][index];
297 }
298 }
299 return result;
300}
301
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000302int intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000303 LineQuadraticIntersections q(quad, line, i);
304 return q.intersect();
305}