caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1 | #include "CurveIntersection.h" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 2 | #include "Intersections.h" |
| 3 | #include "LineUtilities.h" |
| 4 | #include "QuadraticUtilities.h" |
| 5 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 6 | /* |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 7 | Find the interection of a line and quadratic by solving for valid t values. |
| 8 | |
| 9 | From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve |
| 10 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 11 | "A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three |
| 12 | control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 13 | A, B and C are points and t goes from zero to one. |
| 14 | |
| 15 | This 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | If you add for instance the line equation (y = kx + m) to that, you'll end up |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 21 | with three equations and three unknowns (x, y and t)." |
| 22 | |
| 23 | Similar 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 |
| 26 | and the line as |
| 27 | y = g*x + h |
| 28 | |
| 29 | Using Mathematica, solve for the values of t where the quadratic intersects the |
| 30 | line: |
| 31 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 32 | (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x, |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 33 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x] |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 34 | (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 + |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 35 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 39 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 40 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 45 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 50 | Using 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 | |
| 55 | If g goes to infinity, we can rewrite the line in terms of x. |
| 56 | x = g'*y + h' |
| 57 | |
| 58 | And solve accordingly in Mathematica: |
| 59 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 60 | (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h', |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 61 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y] |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 62 | (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 63 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 67 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 68 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 72 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 73 | 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 | |
| 78 | Thus, 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 84 | |
| 85 | class LineQuadraticIntersections : public Intersections { |
| 86 | public: |
| 87 | |
| 88 | LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i) |
| 89 | : quad(q) |
| 90 | , line(l) |
| 91 | , intersections(i) { |
| 92 | } |
| 93 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 94 | int 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 119 | A += C - 2 * B; // A = a - 2*b + c |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 120 | 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.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 126 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 127 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 128 | return roots; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 129 | } |
| 130 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 131 | int 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.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 138 | return quadraticRoots(D, E, F, intersections.fT[0]); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 139 | } |
| 140 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 141 | int 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 154 | int 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.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 161 | return quadraticRoots(D, E, F, intersections.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 162 | } |
| 163 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 164 | int 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 177 | protected: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 178 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 179 | double 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.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 194 | bool 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 210 | private: |
| 211 | |
| 212 | const Quadratic& quad; |
| 213 | const _Line& line; |
| 214 | Intersections& intersections; |
| 215 | bool moreHorizontal; |
| 216 | |
| 217 | }; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 218 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 219 | // utility for pairs of coincident quads |
| 220 | static 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 | |
| 236 | static 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 | |
| 252 | double 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.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 259 | int 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 276 | int horizontalIntersect(const Quadratic& quad, double left, double right, double y, |
| 277 | bool flipped, Intersections& intersections) { |
| 278 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 279 | int result = q.horizontalIntersect(y, left, right); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 280 | 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 | |
| 289 | int verticalIntersect(const Quadratic& quad, double top, double bottom, double x, |
| 290 | bool flipped, Intersections& intersections) { |
| 291 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 292 | int result = q.verticalIntersect(x, top, bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 293 | if (flipped) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 294 | // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 295 | 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.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame^] | 302 | int intersect(const Quadratic& quad, const _Line& line, Intersections& i) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 303 | LineQuadraticIntersections q(quad, line, i); |
| 304 | return q.intersect(); |
| 305 | } |