blob: 3cc27e5e1a689dc3699d91a849ed4e2d9f08daec [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.com9f602912013-01-24 21:47:16 +0000127 return quadraticRootsValidT(A, 2 * 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)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000138 _Point pt;
139 xy_at_t(line, lineT, pt.x, pt.y);
140 intersections.insert(quadT, lineT, pt);
caryclark@google.com24bec792012-08-20 12:43:57 +0000141 }
caryclark@google.com24bec792012-08-20 12:43:57 +0000142 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000143 return intersections.fUsed;
caryclark@google.com27accef2012-01-25 18:57:23 +0000144}
145
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000146int horizontalIntersect(double axisIntercept, double roots[2]) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000147 double D = quad[2].y; // f
148 double E = quad[1].y; // e
149 double F = quad[0].y; // d
150 D += F - 2 * E; // D = d - 2*e + f
151 E -= F; // E = -(d - e)
152 F -= axisIntercept;
caryclark@google.com9f602912013-01-24 21:47:16 +0000153 return quadraticRootsValidT(D, 2 * E, F, roots);
caryclark@google.com198e0542012-03-30 18:47:02 +0000154}
155
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000156int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
157 addHorizontalEndPoints(left, right, axisIntercept);
158 double rootVals[2];
159 int roots = horizontalIntersect(axisIntercept, rootVals);
160 for (int index = 0; index < roots; ++index) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000161 _Point pt;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000162 double quadT = rootVals[index];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000163 xy_at_t(quad, quadT, pt.x, pt.y);
164 double lineT = (pt.x - left) / (right - left);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000165 if (pinTs(quadT, lineT)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000166 intersections.insert(quadT, lineT, pt);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000167 }
168 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000169 if (flipped) {
170 flip();
171 }
172 return intersections.fUsed;
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000173}
174
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000175int verticalIntersect(double axisIntercept, double roots[2]) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000176 double D = quad[2].x; // f
177 double E = quad[1].x; // e
178 double F = quad[0].x; // d
179 D += F - 2 * E; // D = d - 2*e + f
180 E -= F; // E = -(d - e)
181 F -= axisIntercept;
caryclark@google.com9f602912013-01-24 21:47:16 +0000182 return quadraticRootsValidT(D, 2 * E, F, roots);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000183}
184
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000185int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
186 addVerticalEndPoints(top, bottom, axisIntercept);
187 double rootVals[2];
188 int roots = verticalIntersect(axisIntercept, rootVals);
189 for (int index = 0; index < roots; ++index) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000190 _Point pt;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000191 double quadT = rootVals[index];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000192 xy_at_t(quad, quadT, pt.x, pt.y);
193 double lineT = (pt.y - top) / (bottom - top);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000194 if (pinTs(quadT, lineT)) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000195 intersections.insert(quadT, lineT, pt);
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000196 }
197 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000198 if (flipped) {
199 flip();
200 }
201 return intersections.fUsed;
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000202}
203
caryclark@google.com27accef2012-01-25 18:57:23 +0000204protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000205
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000206// add endpoints first to get zero and one t values exactly
207void addEndPoints()
208{
209 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
210 for (int lIndex = 0; lIndex < 2; lIndex++) {
211 if (quad[qIndex] == line[lIndex]) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000212 intersections.insert(qIndex >> 1, lIndex, line[lIndex]);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000213 }
214 }
215 }
216}
217
218void addHorizontalEndPoints(double left, double right, double y)
219{
220 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
221 if (quad[qIndex].y != y) {
222 continue;
223 }
224 if (quad[qIndex].x == left) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000225 intersections.insert(qIndex >> 1, 0, quad[qIndex]);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000226 }
227 if (quad[qIndex].x == right) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000228 intersections.insert(qIndex >> 1, 1, quad[qIndex]);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000229 }
230 }
231}
232
233void addVerticalEndPoints(double top, double bottom, double x)
234{
235 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
236 if (quad[qIndex].x != x) {
237 continue;
238 }
239 if (quad[qIndex].y == top) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000240 intersections.insert(qIndex >> 1, 0, quad[qIndex]);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000241 }
242 if (quad[qIndex].y == bottom) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000243 intersections.insert(qIndex >> 1, 1, quad[qIndex]);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000244 }
245 }
246}
247
caryclark@google.com27accef2012-01-25 18:57:23 +0000248double findLineT(double t) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000249 double x, y;
250 xy_at_t(quad, t, x, y);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000251 double dx = line[1].x - line[0].x;
252 double dy = line[1].y - line[0].y;
253 if (fabs(dx) > fabs(dy)) {
254 return (x - line[0].x) / dx;
255 }
256 return (y - line[0].y) / dy;
caryclark@google.com27accef2012-01-25 18:57:23 +0000257}
258
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000259void flip() {
260 // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
261 int roots = intersections.fUsed;
262 for (int index = 0; index < roots; ++index) {
263 intersections.fT[1][index] = 1 - intersections.fT[1][index];
264 }
265}
266
267bool pinTs(double& quadT, double& lineT) {
268 if (!approximately_one_or_less(lineT)) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000269 return false;
270 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000271 if (!approximately_zero_or_more(lineT)) {
272 return false;
273 }
274 if (quadT < 0) {
275 quadT = 0;
276 } else if (quadT > 1) {
277 quadT = 1;
278 }
279 if (lineT < 0) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000280 lineT = 0;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000281 } else if (lineT > 1) {
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000282 lineT = 1;
283 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000284 return true;
285}
286
caryclark@google.com27accef2012-01-25 18:57:23 +0000287private:
288
289const Quadratic& quad;
290const _Line& line;
291Intersections& intersections;
caryclark@google.com27accef2012-01-25 18:57:23 +0000292};
caryclark@google.com198e0542012-03-30 18:47:02 +0000293
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000294// utility for pairs of coincident quads
295static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000296 LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
297 double rootVals[2];
298 int roots = q.horizontalIntersect(pt.y, rootVals);
caryclark@google.com32546db2012-08-31 20:55:07 +0000299 for (int index = 0; index < roots; ++index) {
300 double x;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000301 double t = rootVals[index];
caryclark@google.com32546db2012-08-31 20:55:07 +0000302 xy_at_t(quad, t, x, *(double*) 0);
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000303 if (AlmostEqualUlps(x, pt.x)) {
caryclark@google.com32546db2012-08-31 20:55:07 +0000304 return t;
305 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000306 }
307 return -1;
308}
309
310static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000311 LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
312 double rootVals[2];
313 int roots = q.verticalIntersect(pt.x, rootVals);
caryclark@google.com32546db2012-08-31 20:55:07 +0000314 for (int index = 0; index < roots; ++index) {
315 double y;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000316 double t = rootVals[index];
caryclark@google.com32546db2012-08-31 20:55:07 +0000317 xy_at_t(quad, t, *(double*) 0, y);
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000318 if (AlmostEqualUlps(y, pt.y)) {
caryclark@google.com32546db2012-08-31 20:55:07 +0000319 return t;
320 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000321 }
322 return -1;
323}
324
325double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
326 if (vertical) {
327 return verticalIntersect(q1, p);
328 }
329 return horizontalIntersect(q1, p);
330}
331
caryclark@google.com198e0542012-03-30 18:47:02 +0000332int horizontalIntersect(const Quadratic& quad, double left, double right,
333 double y, double tRange[2]) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000334 LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
335 double rootVals[2];
336 int result = q.horizontalIntersect(y, rootVals);
caryclark@google.com198e0542012-03-30 18:47:02 +0000337 int tCount = 0;
338 for (int index = 0; index < result; ++index) {
339 double x, y;
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000340 xy_at_t(quad, rootVals[index], x, y);
caryclark@google.com198e0542012-03-30 18:47:02 +0000341 if (x < left || x > right) {
342 continue;
343 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000344 tRange[tCount++] = rootVals[index];
caryclark@google.com198e0542012-03-30 18:47:02 +0000345 }
346 return tCount;
347}
348
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000349int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
350 bool flipped, Intersections& intersections) {
351 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000352 return q.horizontalIntersect(y, left, right, flipped);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000353}
354
355int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
356 bool flipped, Intersections& intersections) {
357 LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000358 return q.verticalIntersect(x, top, bottom, flipped);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000359}
360
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000361int intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
caryclark@google.com27accef2012-01-25 18:57:23 +0000362 LineQuadraticIntersections q(quad, line, i);
363 return q.intersect();
364}
caryclark@google.com235f56a2012-09-14 14:19:30 +0000365
366int intersectRay(const Quadratic& quad, const _Line& line, Intersections& i) {
367 LineQuadraticIntersections q(quad, line, i);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000368 return q.intersectRay(i.fT[0]);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000369}