blob: 1ad37353d60e3ad3129b0ae10fe68323fb4df960 [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.com3350c3c2012-08-24 15:24:36 +0000100int intersect() {
101/*
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)
127 int roots = quadraticRoots(A, B, C, intersections.fT[0]);
128 for (int index = 0; index < roots; ) {
129 double lineT = findLineT(intersections.fT[0][index]);
130 if (lineIntersects(lineT, index, roots)) {
131 ++index;
caryclark@google.com24bec792012-08-20 12:43:57 +0000132 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000133 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000134 return roots;
caryclark@google.com27accef2012-01-25 18:57:23 +0000135}
136
caryclark@google.com198e0542012-03-30 18:47:02 +0000137int horizontalIntersect(double axisIntercept) {
138 double D = quad[2].y; // f
139 double E = quad[1].y; // e
140 double F = quad[0].y; // d
141 D += F - 2 * E; // D = d - 2*e + f
142 E -= F; // E = -(d - e)
143 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000144 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.com198e0542012-03-30 18:47:02 +0000145}
146
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000147int horizontalIntersect(double axisIntercept, double left, double right) {
148 int roots = horizontalIntersect(axisIntercept);
149 for (int index = 0; index < roots; ) {
150 double x;
151 xy_at_t(quad, intersections.fT[0][index], x, *(double*) NULL);
152 double lineT = (x - left) / (right - left);
153 if (lineIntersects(lineT, index, roots)) {
154 ++index;
155 }
156 }
157 return roots;
158}
159
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000160int verticalIntersect(double axisIntercept) {
161 double D = quad[2].x; // f
162 double E = quad[1].x; // e
163 double F = quad[0].x; // d
164 D += F - 2 * E; // D = d - 2*e + f
165 E -= F; // E = -(d - e)
166 F -= axisIntercept;
caryclark@google.com03f97062012-08-21 13:13:52 +0000167 return quadraticRoots(D, E, F, intersections.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000168}
169
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000170int verticalIntersect(double axisIntercept, double top, double bottom) {
171 int roots = verticalIntersect(axisIntercept);
172 for (int index = 0; index < roots; ) {
173 double y;
174 xy_at_t(quad, intersections.fT[0][index], *(double*) NULL, y);
175 double lineT = (y - top) / (bottom - top);
176 if (lineIntersects(lineT, index, roots)) {
177 ++index;
178 }
179 }
180 return roots;
181}
182
caryclark@google.com27accef2012-01-25 18:57:23 +0000183protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184
caryclark@google.com27accef2012-01-25 18:57:23 +0000185double findLineT(double t) {
186 const double* qPtr;
187 const double* lPtr;
188 if (moreHorizontal) {
189 qPtr = &quad[0].x;
190 lPtr = &line[0].x;
191 } else {
192 qPtr = &quad[0].y;
193 lPtr = &line[0].y;
194 }
195 double s = 1 - t;
196 double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t;
197 return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]);
198}
199
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000200bool lineIntersects(double lineT, const int x, int& roots) {
201 if (!approximately_zero_or_more(lineT) || !approximately_one_or_less(lineT)) {
202 if (x < --roots) {
203 intersections.fT[0][x] = intersections.fT[0][roots];
204 }
205 return false;
206 }
207 if (approximately_less_than_zero(lineT)) {
208 lineT = 0;
209 } else if (approximately_greater_than_one(lineT)) {
210 lineT = 1;
211 }
212 intersections.fT[1][x] = lineT;
213 return true;
214}
215
caryclark@google.com27accef2012-01-25 18:57:23 +0000216private:
217
218const Quadratic& quad;
219const _Line& line;
220Intersections& intersections;
221bool moreHorizontal;
222
223};
caryclark@google.com198e0542012-03-30 18:47:02 +0000224
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000225// utility for pairs of coincident quads
226static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
227 Intersections intersections;
228 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com32546db2012-08-31 20:55:07 +0000229 int roots = q.horizontalIntersect(pt.y);
230 for (int index = 0; index < roots; ++index) {
231 double x;
232 double t = intersections.fT[0][index];
233 xy_at_t(quad, t, x, *(double*) 0);
234 if (approximately_equal(x, pt.x)) {
235 return t;
236 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000237 }
238 return -1;
239}
240
241static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
242 Intersections intersections;
243 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com32546db2012-08-31 20:55:07 +0000244 int roots = q.verticalIntersect(pt.x);
245 for (int index = 0; index < roots; ++index) {
246 double y;
247 double t = intersections.fT[0][index];
248 xy_at_t(quad, t, *(double*) 0, y);
249 if (approximately_equal(y, pt.y)) {
250 return t;
251 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000252 }
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);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000283 int result = q.horizontalIntersect(y, left, right);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000284 if (flipped) {
285 // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x
286 for (int index = 0; index < result; ++index) {
287 intersections.fT[1][index] = 1 - intersections.fT[1][index];
288 }
289 }
290 return result;
291}
292
293int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
294 bool flipped, Intersections& intersections) {
295 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000296 int result = q.verticalIntersect(x, top, bottom);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000297 if (flipped) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000298 // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000299 for (int index = 0; index < result; ++index) {
300 intersections.fT[1][index] = 1 - intersections.fT[1][index];
301 }
302 }
303 return result;
304}
305
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000306int intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000307 LineQuadraticIntersections q(quad, line, i);
308 return q.intersect();
309}