blob: b3303cf95e1b677a08faab21dfb3559486055671 [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
caryclark@google.comfb51afb2012-10-19 15:54:16 +000091class LineQuadraticIntersections {
caryclark@google.com27accef2012-01-25 18:57:23 +000092public:
93
94LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i)
95 : quad(q)
96 , line(l)
97 , intersections(i) {
98}
99
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000100int intersectRay(double roots[2]) {
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.comfb51afb2012-10-19 15:54:16 +0000127 return quadraticRoots(A, B, C, roots);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000128}
129
130int intersect() {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000131 addEndPoints();
132 double rootVals[2];
133 int roots = intersectRay(rootVals);
134 for (int index = 0; index < roots; ++index) {
135 double quadT = rootVals[index];
136 double lineT = findLineT(quadT);
137 if (pinTs(quadT, lineT)) {
138 intersections.insert(quadT, lineT);
caryclark@google.com24bec792012-08-20 12:43:57 +0000139 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000140 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000141 return intersections.fUsed;
caryclark@google.com27accef2012-01-25 18:57:23 +0000142}
143
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000144int horizontalIntersect(double axisIntercept, double roots[2]) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000145 double D = quad[2].y; // f
146 double E = quad[1].y; // e
147 double F = quad[0].y; // d
148 D += F - 2 * E; // D = d - 2*e + f
149 E -= F; // E = -(d - e)
150 F -= axisIntercept;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000151 return quadraticRoots(D, E, F, roots);
caryclark@google.com198e0542012-03-30 18:47:02 +0000152}
153
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000154int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
155 addHorizontalEndPoints(left, right, axisIntercept);
156 double rootVals[2];
157 int roots = horizontalIntersect(axisIntercept, rootVals);
158 for (int index = 0; index < roots; ++index) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000159 double x;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000160 double quadT = rootVals[index];
161 xy_at_t(quad, quadT, x, *(double*) NULL);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000162 double lineT = (x - left) / (right - left);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000163 if (pinTs(quadT, lineT)) {
164 intersections.insert(quadT, lineT);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000165 }
166 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000167 if (flipped) {
168 flip();
169 }
170 return intersections.fUsed;
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000171}
172
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000173int verticalIntersect(double axisIntercept, double roots[2]) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000174 double D = quad[2].x; // f
175 double E = quad[1].x; // e
176 double F = quad[0].x; // d
177 D += F - 2 * E; // D = d - 2*e + f
178 E -= F; // E = -(d - e)
179 F -= axisIntercept;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000180 return quadraticRoots(D, E, F, roots);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000181}
182
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000183int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
184 addVerticalEndPoints(top, bottom, axisIntercept);
185 double rootVals[2];
186 int roots = verticalIntersect(axisIntercept, rootVals);
187 for (int index = 0; index < roots; ++index) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000188 double y;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000189 double quadT = rootVals[index];
190 xy_at_t(quad, quadT, *(double*) NULL, y);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000191 double lineT = (y - top) / (bottom - top);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000192 if (pinTs(quadT, lineT)) {
193 intersections.insert(quadT, lineT);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000194 }
195 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000196 if (flipped) {
197 flip();
198 }
199 return intersections.fUsed;
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000200}
201
caryclark@google.com27accef2012-01-25 18:57:23 +0000202protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000203
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000204// add endpoints first to get zero and one t values exactly
205void addEndPoints()
206{
207 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
208 for (int lIndex = 0; lIndex < 2; lIndex++) {
209 if (quad[qIndex] == line[lIndex]) {
210 intersections.insert(qIndex >> 1, lIndex);
211 }
212 }
213 }
214}
215
216void addHorizontalEndPoints(double left, double right, double y)
217{
218 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
219 if (quad[qIndex].y != y) {
220 continue;
221 }
222 if (quad[qIndex].x == left) {
223 intersections.insert(qIndex >> 1, 0);
224 }
225 if (quad[qIndex].x == right) {
226 intersections.insert(qIndex >> 1, 1);
227 }
228 }
229}
230
231void addVerticalEndPoints(double top, double bottom, double x)
232{
233 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
234 if (quad[qIndex].x != x) {
235 continue;
236 }
237 if (quad[qIndex].y == top) {
238 intersections.insert(qIndex >> 1, 0);
239 }
240 if (quad[qIndex].y == bottom) {
241 intersections.insert(qIndex >> 1, 1);
242 }
243 }
244}
245
caryclark@google.com27accef2012-01-25 18:57:23 +0000246double findLineT(double t) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000247 double x, y;
248 xy_at_t(quad, t, x, y);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000249 double dx = line[1].x - line[0].x;
250 double dy = line[1].y - line[0].y;
251 if (fabs(dx) > fabs(dy)) {
252 return (x - line[0].x) / dx;
253 }
254 return (y - line[0].y) / dy;
caryclark@google.com27accef2012-01-25 18:57:23 +0000255}
256
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000257void flip() {
258 // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
259 int roots = intersections.fUsed;
260 for (int index = 0; index < roots; ++index) {
261 intersections.fT[1][index] = 1 - intersections.fT[1][index];
262 }
263}
264
265bool pinTs(double& quadT, double& lineT) {
266 if (!approximately_one_or_less(lineT)) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000267 return false;
268 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000269 if (!approximately_zero_or_more(lineT)) {
270 return false;
271 }
272 if (quadT < 0) {
273 quadT = 0;
274 } else if (quadT > 1) {
275 quadT = 1;
276 }
277 if (lineT < 0) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000278 lineT = 0;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000279 } else if (lineT > 1) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000280 lineT = 1;
281 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000282 return true;
283}
284
caryclark@google.com27accef2012-01-25 18:57:23 +0000285private:
286
287const Quadratic& quad;
288const _Line& line;
289Intersections& intersections;
caryclark@google.com27accef2012-01-25 18:57:23 +0000290};
caryclark@google.com198e0542012-03-30 18:47:02 +0000291
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000292// utility for pairs of coincident quads
293static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000294 LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
295 double rootVals[2];
296 int roots = q.horizontalIntersect(pt.y, rootVals);
caryclark@google.com32546db2012-08-31 20:55:07 +0000297 for (int index = 0; index < roots; ++index) {
298 double x;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000299 double t = rootVals[index];
caryclark@google.com32546db2012-08-31 20:55:07 +0000300 xy_at_t(quad, t, x, *(double*) 0);
301 if (approximately_equal(x, pt.x)) {
302 return t;
303 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000304 }
305 return -1;
306}
307
308static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000309 LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
310 double rootVals[2];
311 int roots = q.verticalIntersect(pt.x, rootVals);
caryclark@google.com32546db2012-08-31 20:55:07 +0000312 for (int index = 0; index < roots; ++index) {
313 double y;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000314 double t = rootVals[index];
caryclark@google.com32546db2012-08-31 20:55:07 +0000315 xy_at_t(quad, t, *(double*) 0, y);
316 if (approximately_equal(y, pt.y)) {
317 return t;
318 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000319 }
320 return -1;
321}
322
323double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
324 if (vertical) {
325 return verticalIntersect(q1, p);
326 }
327 return horizontalIntersect(q1, p);
328}
329
caryclark@google.com198e0542012-03-30 18:47:02 +0000330int horizontalIntersect(const Quadratic& quad, double left, double right,
331 double y, double tRange[2]) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000332 LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
333 double rootVals[2];
334 int result = q.horizontalIntersect(y, rootVals);
caryclark@google.com198e0542012-03-30 18:47:02 +0000335 int tCount = 0;
336 for (int index = 0; index < result; ++index) {
337 double x, y;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000338 xy_at_t(quad, rootVals[index], x, y);
caryclark@google.com198e0542012-03-30 18:47:02 +0000339 if (x < left || x > right) {
340 continue;
341 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000342 tRange[tCount++] = rootVals[index];
caryclark@google.com198e0542012-03-30 18:47:02 +0000343 }
344 return tCount;
345}
346
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000347int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
348 bool flipped, Intersections& intersections) {
349 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000350 return q.horizontalIntersect(y, left, right, flipped);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000351}
352
353int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
354 bool flipped, Intersections& intersections) {
355 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000356 return q.verticalIntersect(x, top, bottom, flipped);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000357}
358
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000359int intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000360 LineQuadraticIntersections q(quad, line, i);
361 return q.intersect();
362}
caryclark@google.com235f56a2012-09-14 14:19:30 +0000363
364int intersectRay(const Quadratic& quad, const _Line& line, Intersections& i) {
365 LineQuadraticIntersections q(quad, line, i);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000366 return q.intersectRay(i.fT[0]);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000367}