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 | |
| 6 | /* |
| 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 | |
| 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 |
| 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 | |
| 20 | If you add for instance the line equation (y = kx + m) to that, you'll end up |
| 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 | |
| 32 | (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x, |
| 33 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x] |
| 34 | (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 + |
| 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 - |
| 39 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
| 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 + |
| 44 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
| 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 | } |
| 49 | |
| 50 | Numeric Solutions (5.6) suggests to solve the quadratic by computing |
| 51 | |
| 52 | Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C)) |
| 53 | |
| 54 | and using the roots |
| 55 | |
| 56 | t1 = Q / A |
| 57 | t2 = C / Q |
| 58 | |
| 59 | Using the results above (when the line tends towards horizontal) |
| 60 | A = (-(d - 2*e + f) + g*(a - 2*b + c) ) |
| 61 | B = 2*( (d - e ) - g*(a - b ) ) |
| 62 | C = (-(d ) + g*(a ) + h ) |
| 63 | |
| 64 | If g goes to infinity, we can rewrite the line in terms of x. |
| 65 | x = g'*y + h' |
| 66 | |
| 67 | And solve accordingly in Mathematica: |
| 68 | |
| 69 | (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h', |
| 70 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y] |
| 71 | (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 - |
| 72 | g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2) |
| 73 | (in) Solve[t2 == 0, t] |
| 74 | (out) { |
| 75 | {t -> (2 a - 2 b - 2 d g' + 2 e g' - |
| 76 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
| 77 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) / |
| 78 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 79 | }, |
| 80 | {t -> (2 a - 2 b - 2 d g' + 2 e g' + |
| 81 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
| 82 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/ |
| 83 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | Thus, if the slope of the line tends towards vertical, we use: |
| 88 | A = ( (a - 2*b + c) - g'*(d - 2*e + f) ) |
| 89 | B = 2*(-(a - b ) + g'*(d - e ) ) |
| 90 | C = ( (a ) - g'*(d ) - h' ) |
| 91 | */ |
| 92 | |
| 93 | |
| 94 | class LineQuadraticIntersections : public Intersections { |
| 95 | public: |
| 96 | |
| 97 | LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i) |
| 98 | : quad(q) |
| 99 | , line(l) |
| 100 | , intersections(i) { |
| 101 | } |
| 102 | |
| 103 | bool intersect() { |
| 104 | double slope; |
| 105 | double axisIntercept; |
| 106 | moreHorizontal = implicitLine(line, slope, axisIntercept); |
| 107 | double A = quad[2].x; // c |
| 108 | double B = quad[1].x; // b |
| 109 | double C = quad[0].x; // a |
| 110 | A += C - 2 * B; // A = a - 2*b + c |
| 111 | B -= C; // B = -(a - b) |
| 112 | double D = quad[2].y; // f |
| 113 | double E = quad[1].y; // e |
| 114 | double F = quad[0].y; // d |
| 115 | D += F - 2 * E; // D = d - 2*e + f |
| 116 | E -= F; // E = -(d - e) |
| 117 | if (moreHorizontal) { |
| 118 | A = A * slope - D; |
| 119 | B = B * slope - E; |
| 120 | C = C * slope - F + axisIntercept; |
| 121 | } else { |
| 122 | A = A - D * slope; |
| 123 | B = B - E * slope; |
| 124 | C = C - F * slope - axisIntercept; |
| 125 | } |
| 126 | double t[2]; |
| 127 | int roots = quadraticRoots(A, B, C, t); |
| 128 | for (int x = 0; x < roots; ++x) { |
| 129 | intersections.add(t[x], findLineT(t[x])); |
| 130 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame^] | 131 | // FIXME: quadratic root doesn't find t=0 or t=1, necessitating the hack below |
| 132 | if (roots == 0 || (roots == 1 && intersections.fT[0][0] >= FLT_EPSILON)) { |
| 133 | if (quad[0] == line[0]) { |
| 134 | intersections.fT[0][roots] = 0; |
| 135 | intersections.fT[1][roots++] = 0; |
| 136 | intersections.fUsed++; |
| 137 | } else if (quad[0] == line[1]) { |
| 138 | intersections.fT[0][roots] = 0; |
| 139 | intersections.fT[1][roots++] = 1; |
| 140 | intersections.fUsed++; |
| 141 | } |
| 142 | } |
| 143 | if (roots == 0 || (roots == 1 && intersections.fT[0][0] <= 1 - FLT_EPSILON)) { |
| 144 | if (quad[2] == line[1]) { |
| 145 | intersections.fT[0][roots] = 1; |
| 146 | intersections.fT[1][roots++] = 1; |
| 147 | intersections.fUsed++; |
| 148 | } else if (quad[2] == line[0]) { |
| 149 | intersections.fT[0][roots] = 1; |
| 150 | intersections.fT[1][roots++] = 0; |
| 151 | intersections.fUsed++; |
| 152 | } |
| 153 | } |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 154 | return roots > 0; |
| 155 | } |
| 156 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 157 | int horizontalIntersect(double axisIntercept) { |
| 158 | double D = quad[2].y; // f |
| 159 | double E = quad[1].y; // e |
| 160 | double F = quad[0].y; // d |
| 161 | D += F - 2 * E; // D = d - 2*e + f |
| 162 | E -= F; // E = -(d - e) |
| 163 | F -= axisIntercept; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame^] | 164 | int roots = quadraticRoots(D, E, F, intersections.fT[0]); |
| 165 | // FIXME: ? quadraticRoots doesn't pick up intersections at 0, 1 |
| 166 | if (roots < 2 && fabs(F) < FLT_EPSILON |
| 167 | && (roots == 0 || intersections.fT[0][0] >= FLT_EPSILON)) { |
| 168 | intersections.fT[0][roots++] = 0; |
| 169 | } |
| 170 | if (roots < 2 && fabs(quad[2].y - axisIntercept) < FLT_EPSILON |
| 171 | && (roots == 0 || intersections.fT[0][0] <= 1 - FLT_EPSILON)) { |
| 172 | intersections.fT[0][roots++] = 1; |
| 173 | } |
| 174 | return roots; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 175 | } |
| 176 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 177 | int verticalIntersect(double axisIntercept) { |
| 178 | double D = quad[2].x; // f |
| 179 | double E = quad[1].x; // e |
| 180 | double F = quad[0].x; // d |
| 181 | D += F - 2 * E; // D = d - 2*e + f |
| 182 | E -= F; // E = -(d - e) |
| 183 | F -= axisIntercept; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame^] | 184 | int roots = quadraticRoots(D, E, F, intersections.fT[0]); |
| 185 | // FIXME: ? quadraticRoots doesn't pick up intersections at 0, 1 |
| 186 | if (roots < 2 && fabs(F) < FLT_EPSILON |
| 187 | && (roots == 0 || intersections.fT[0][0] >= FLT_EPSILON)) { |
| 188 | intersections.fT[0][roots++] = 0; |
| 189 | } |
| 190 | if (roots < 2 && fabs(quad[2].x - axisIntercept) < FLT_EPSILON |
| 191 | && (roots == 0 || intersections.fT[0][0] <= 1 - FLT_EPSILON)) { |
| 192 | intersections.fT[0][roots++] = 1; |
| 193 | } |
| 194 | return roots; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 195 | } |
| 196 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 197 | protected: |
| 198 | |
| 199 | double findLineT(double t) { |
| 200 | const double* qPtr; |
| 201 | const double* lPtr; |
| 202 | if (moreHorizontal) { |
| 203 | qPtr = &quad[0].x; |
| 204 | lPtr = &line[0].x; |
| 205 | } else { |
| 206 | qPtr = &quad[0].y; |
| 207 | lPtr = &line[0].y; |
| 208 | } |
| 209 | double s = 1 - t; |
| 210 | double quadVal = qPtr[0] * s * s + 2 * qPtr[2] * s * t + qPtr[4] * t * t; |
| 211 | return (quadVal - lPtr[0]) / (lPtr[2] - lPtr[0]); |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | |
| 216 | const Quadratic& quad; |
| 217 | const _Line& line; |
| 218 | Intersections& intersections; |
| 219 | bool moreHorizontal; |
| 220 | |
| 221 | }; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 222 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 223 | // utility for pairs of coincident quads |
| 224 | static double horizontalIntersect(const Quadratic& quad, const _Point& pt) { |
| 225 | Intersections intersections; |
| 226 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
| 227 | int result = q.horizontalIntersect(pt.y); |
| 228 | if (result == 0) { |
| 229 | return -1; |
| 230 | } |
| 231 | assert(result == 1); |
| 232 | double x, y; |
| 233 | xy_at_t(quad, intersections.fT[0][0], x, y); |
| 234 | if (approximately_equal(x, pt.x)) { |
| 235 | return intersections.fT[0][0]; |
| 236 | } |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | static double verticalIntersect(const Quadratic& quad, const _Point& pt) { |
| 241 | Intersections intersections; |
| 242 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
| 243 | int result = q.horizontalIntersect(pt.x); |
| 244 | if (result == 0) { |
| 245 | return -1; |
| 246 | } |
| 247 | assert(result == 1); |
| 248 | double x, y; |
| 249 | xy_at_t(quad, intersections.fT[0][0], x, y); |
| 250 | if (approximately_equal(y, pt.y)) { |
| 251 | return intersections.fT[0][0]; |
| 252 | } |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | double 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.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 263 | int 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 280 | int horizontalIntersect(const Quadratic& quad, double left, double right, double y, |
| 281 | bool flipped, Intersections& intersections) { |
| 282 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
| 283 | int result = q.horizontalIntersect(y); |
| 284 | for (int index = 0; index < result; ) { |
| 285 | double x, y; |
| 286 | xy_at_t(quad, intersections.fT[0][index], x, y); |
| 287 | if (x < left || x > right) { |
| 288 | if (--result > index) { |
| 289 | intersections.fT[0][index] = intersections.fT[0][result]; |
| 290 | } |
| 291 | continue; |
| 292 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame^] | 293 | intersections.fT[1][index] = (x - left) / (right - left); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 294 | ++index; |
| 295 | } |
| 296 | if (flipped) { |
| 297 | // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x |
| 298 | for (int index = 0; index < result; ++index) { |
| 299 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 300 | } |
| 301 | } |
| 302 | return result; |
| 303 | } |
| 304 | |
| 305 | int verticalIntersect(const Quadratic& quad, double top, double bottom, double x, |
| 306 | bool flipped, Intersections& intersections) { |
| 307 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
| 308 | int result = q.verticalIntersect(x); |
| 309 | for (int index = 0; index < result; ) { |
| 310 | double x, y; |
| 311 | xy_at_t(quad, intersections.fT[0][index], x, y); |
| 312 | if (y < top || y > bottom) { |
| 313 | if (--result > index) { |
| 314 | intersections.fT[0][index] = intersections.fT[0][result]; |
| 315 | } |
| 316 | continue; |
| 317 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame^] | 318 | intersections.fT[1][index] = (y - top) / (bottom - top); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 319 | ++index; |
| 320 | } |
| 321 | if (flipped) { |
| 322 | // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x |
| 323 | for (int index = 0; index < result; ++index) { |
| 324 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 325 | } |
| 326 | } |
| 327 | return result; |
| 328 | } |
| 329 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 330 | bool intersect(const Quadratic& quad, const _Line& line, Intersections& i) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 331 | LineQuadraticIntersections q(quad, line, i); |
| 332 | return q.intersect(); |
| 333 | } |