blob: f7302508088b581beceaf196a9f8f5c40f2af73b [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00008#include "Intersections.h"
9#include "LineUtilities.h"
10#include "QuadraticUtilities.h"
11
rmistry@google.comd6176b02012-08-23 18:14:13 +000012/*
caryclark@google.com27accef2012-01-25 18:57:23 +000013Find the interection of a line and quadratic by solving for valid t values.
14
15From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve
16
rmistry@google.comd6176b02012-08-23 18:14:13 +000017"A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three
18control 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 +000019A, B and C are points and t goes from zero to one.
20
21This will give you two equations:
22
23 x = a(1 - t)^2 + b(1 - t)t + ct^2
24 y = d(1 - t)^2 + e(1 - t)t + ft^2
25
rmistry@google.comd6176b02012-08-23 18:14:13 +000026If you add for instance the line equation (y = kx + m) to that, you'll end up
caryclark@google.com27accef2012-01-25 18:57:23 +000027with three equations and three unknowns (x, y and t)."
28
29Similar to above, the quadratic is represented as
30 x = a(1-t)^2 + 2b(1-t)t + ct^2
31 y = d(1-t)^2 + 2e(1-t)t + ft^2
32and the line as
33 y = g*x + h
34
35Using Mathematica, solve for the values of t where the quadratic intersects the
36line:
37
rmistry@google.comd6176b02012-08-23 18:14:13 +000038 (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x,
caryclark@google.com27accef2012-01-25 18:57:23 +000039 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x]
rmistry@google.comd6176b02012-08-23 18:14:13 +000040 (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 +000041 g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2)
42 (in) Solve[t1 == 0, t]
43 (out) {
44 {t -> (-2 d + 2 e + 2 a g - 2 b g -
rmistry@google.comd6176b02012-08-23 18:14:13 +000045 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000046 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
47 (2 (-d + 2 e - f + a g - 2 b g + c g))
48 },
49 {t -> (-2 d + 2 e + 2 a g - 2 b g +
rmistry@google.comd6176b02012-08-23 18:14:13 +000050 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000051 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
52 (2 (-d + 2 e - f + a g - 2 b g + c g))
53 }
54 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
caryclark@google.com27accef2012-01-25 18:57:23 +000056Using the results above (when the line tends towards horizontal)
57 A = (-(d - 2*e + f) + g*(a - 2*b + c) )
58 B = 2*( (d - e ) - g*(a - b ) )
59 C = (-(d ) + g*(a ) + h )
60
61If g goes to infinity, we can rewrite the line in terms of x.
62 x = g'*y + h'
63
64And solve accordingly in Mathematica:
65
rmistry@google.comd6176b02012-08-23 18:14:13 +000066 (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 +000067 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y]
rmistry@google.comd6176b02012-08-23 18:14:13 +000068 (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 +000069 g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2)
70 (in) Solve[t2 == 0, t]
71 (out) {
72 {t -> (2 a - 2 b - 2 d g' + 2 e g' -
rmistry@google.comd6176b02012-08-23 18:14:13 +000073 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000074 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) /
75 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
76 },
77 {t -> (2 a - 2 b - 2 d g' + 2 e g' +
rmistry@google.comd6176b02012-08-23 18:14:13 +000078 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
caryclark@google.com27accef2012-01-25 18:57:23 +000079 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/
80 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
81 }
82 }
83
84Thus, if the slope of the line tends towards vertical, we use:
85 A = ( (a - 2*b + c) - g'*(d - 2*e + f) )
86 B = 2*(-(a - b ) + g'*(d - e ) )
87 C = ( (a ) - g'*(d ) - h' )
88 */
rmistry@google.comd6176b02012-08-23 18:14:13 +000089
caryclark@google.com27accef2012-01-25 18:57:23 +000090
91class LineQuadraticIntersections : public Intersections {
92public:
93
94LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i)
95 : quad(q)
96 , line(l)
97 , intersections(i) {
98}
99
caryclark@google.com235f56a2012-09-14 14:19:30 +0000100int intersectRay() {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000101/*
102 solve by rotating line+quad so line is horizontal, then finding the roots
103 set up matrix to rotate quad to x-axis
104 |cos(a) -sin(a)|
105 |sin(a) cos(a)|
106 note that cos(a) = A(djacent) / Hypoteneuse
107 sin(a) = O(pposite) / Hypoteneuse
108 since we are computing Ts, we can ignore hypoteneuse, the scale factor:
109 | A -O |
110 | O A |
111 A = line[1].x - line[0].x (adjacent side of the right triangle)
112 O = line[1].y - line[0].y (opposite side of the right triangle)
113 for each of the three points (e.g. n = 0 to 2)
114 quad[n].y' = (quad[n].y - line[0].y) * A - (quad[n].x - line[0].x) * O
115*/
116 double adj = line[1].x - line[0].x;
117 double opp = line[1].y - line[0].y;
118 double r[3];
119 for (int n = 0; n < 3; ++n) {
120 r[n] = (quad[n].y - line[0].y) * adj - (quad[n].x - line[0].x) * opp;
121 }
122 double A = r[2];
123 double B = r[1];
124 double C = r[0];
caryclark@google.com27accef2012-01-25 18:57:23 +0000125 A += C - 2 * B; // A = a - 2*b + c
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000126 B -= C; // B = -(b - c)
caryclark@google.com235f56a2012-09-14 14:19:30 +0000127 return quadraticRoots(A, B, C, intersections.fT[0]);
128}
129
130int intersect() {
131 int roots = intersectRay();
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000132 for (int index = 0; index < roots; ) {
133 double lineT = findLineT(intersections.fT[0][index]);
134 if (lineIntersects(lineT, index, roots)) {
135 ++index;
caryclark@google.com24bec792012-08-20 12:43:57 +0000136 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000137 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000138 return roots;
caryclark@google.com27accef2012-01-25 18:57:23 +0000139}
140
caryclark@google.com198e0542012-03-30 18:47:02 +0000141int horizontalIntersect(double axisIntercept) {
142 double D = quad[2].y; // f
143 double E = quad[1].y; // e
144 double F = quad[0].y; // d
145 D += F - 2 * E; // D = d - 2*e + f
146 E -= F; // E = -(d - e)
147 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000148 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.com198e0542012-03-30 18:47:02 +0000149}
150
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000151int horizontalIntersect(double axisIntercept, double left, double right) {
152 int roots = horizontalIntersect(axisIntercept);
153 for (int index = 0; index < roots; ) {
154 double x;
155 xy_at_t(quad, intersections.fT[0][index], x, *(double*) NULL);
156 double lineT = (x - left) / (right - left);
157 if (lineIntersects(lineT, index, roots)) {
158 ++index;
159 }
160 }
161 return roots;
162}
163
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000164int verticalIntersect(double axisIntercept) {
165 double D = quad[2].x; // f
166 double E = quad[1].x; // e
167 double F = quad[0].x; // d
168 D += F - 2 * E; // D = d - 2*e + f
169 E -= F; // E = -(d - e)
170 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000171 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000172}
173
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000174int verticalIntersect(double axisIntercept, double top, double bottom) {
175 int roots = verticalIntersect(axisIntercept);
176 for (int index = 0; index < roots; ) {
177 double y;
178 xy_at_t(quad, intersections.fT[0][index], *(double*) NULL, y);
179 double lineT = (y - top) / (bottom - top);
180 if (lineIntersects(lineT, index, roots)) {
181 ++index;
182 }
183 }
184 return roots;
185}
186
caryclark@google.com27accef2012-01-25 18:57:23 +0000187protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
caryclark@google.com27accef2012-01-25 18:57:23 +0000189double findLineT(double t) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000190 double x, y;
191 xy_at_t(quad, t, x, y);
192 if (approximately_equal(x, line[0].x) && approximately_equal(y, line[0].y)) {
193 return 0;
caryclark@google.com27accef2012-01-25 18:57:23 +0000194 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000195 if (approximately_equal(x, line[1].x) && approximately_equal(y, line[1].y)) {
196 return 1;
197 }
198 double dx = line[1].x - line[0].x;
199 double dy = line[1].y - line[0].y;
200 if (fabs(dx) > fabs(dy)) {
201 return (x - line[0].x) / dx;
202 }
203 return (y - line[0].y) / dy;
caryclark@google.com27accef2012-01-25 18:57:23 +0000204}
205
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000206bool lineIntersects(double lineT, const int x, int& roots) {
207 if (!approximately_zero_or_more(lineT) || !approximately_one_or_less(lineT)) {
208 if (x < --roots) {
209 intersections.fT[0][x] = intersections.fT[0][roots];
210 }
211 return false;
212 }
213 if (approximately_less_than_zero(lineT)) {
214 lineT = 0;
215 } else if (approximately_greater_than_one(lineT)) {
216 lineT = 1;
217 }
218 intersections.fT[1][x] = lineT;
219 return true;
220}
221
caryclark@google.com27accef2012-01-25 18:57:23 +0000222private:
223
224const Quadratic& quad;
225const _Line& line;
226Intersections& intersections;
caryclark@google.com27accef2012-01-25 18:57:23 +0000227};
caryclark@google.com198e0542012-03-30 18:47:02 +0000228
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000229// utility for pairs of coincident quads
230static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
231 Intersections intersections;
232 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com32546db2012-08-31 20:55:07 +0000233 int roots = q.horizontalIntersect(pt.y);
234 for (int index = 0; index < roots; ++index) {
235 double x;
236 double t = intersections.fT[0][index];
237 xy_at_t(quad, t, x, *(double*) 0);
238 if (approximately_equal(x, pt.x)) {
239 return t;
240 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000241 }
242 return -1;
243}
244
245static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
246 Intersections intersections;
247 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com32546db2012-08-31 20:55:07 +0000248 int roots = q.verticalIntersect(pt.x);
249 for (int index = 0; index < roots; ++index) {
250 double y;
251 double t = intersections.fT[0][index];
252 xy_at_t(quad, t, *(double*) 0, y);
253 if (approximately_equal(y, pt.y)) {
254 return t;
255 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000256 }
257 return -1;
258}
259
260double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
261 if (vertical) {
262 return verticalIntersect(q1, p);
263 }
264 return horizontalIntersect(q1, p);
265}
266
caryclark@google.com198e0542012-03-30 18:47:02 +0000267int horizontalIntersect(const Quadratic& quad, double left, double right,
268 double y, double tRange[2]) {
269 Intersections i;
270 LineQuadraticIntersections q(quad, *((_Line*) 0), i);
271 int result = q.horizontalIntersect(y);
272 int tCount = 0;
273 for (int index = 0; index < result; ++index) {
274 double x, y;
275 xy_at_t(quad, i.fT[0][index], x, y);
276 if (x < left || x > right) {
277 continue;
278 }
279 tRange[tCount++] = i.fT[0][index];
280 }
281 return tCount;
282}
283
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000284int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
285 bool flipped, Intersections& intersections) {
286 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000287 int result = q.horizontalIntersect(y, left, right);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000288 if (flipped) {
289 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
290 for (int index = 0; index < result; ++index) {
291 intersections.fT[1][index] = 1 - intersections.fT[1][index];
292 }
293 }
294 return result;
295}
296
297int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
298 bool flipped, Intersections& intersections) {
299 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000300 int result = q.verticalIntersect(x, top, bottom);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000301 if (flipped) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000302 // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000303 for (int index = 0; index < result; ++index) {
304 intersections.fT[1][index] = 1 - intersections.fT[1][index];
305 }
306 }
307 return result;
308}
309
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000310int intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000311 LineQuadraticIntersections q(quad, line, i);
312 return q.intersect();
313}
caryclark@google.com235f56a2012-09-14 14:19:30 +0000314
315int intersectRay(const Quadratic& quad, const _Line& line, Intersections& i) {
316 LineQuadraticIntersections q(quad, line, i);
317 return q.intersectRay();
318}