caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame^] | 1 | /* |
| 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 8 | #include "Intersections.h" |
| 9 | #include "LineUtilities.h" |
| 10 | #include "QuadraticUtilities.h" |
| 11 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 12 | /* |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 13 | Find the interection of a line and quadratic by solving for valid t values. |
| 14 | |
| 15 | From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve |
| 16 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 17 | "A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three |
| 18 | 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] | 19 | A, B and C are points and t goes from zero to one. |
| 20 | |
| 21 | This 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 26 | 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] | 27 | with three equations and three unknowns (x, y and t)." |
| 28 | |
| 29 | Similar 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 |
| 32 | and the line as |
| 33 | y = g*x + h |
| 34 | |
| 35 | Using Mathematica, solve for the values of t where the quadratic intersects the |
| 36 | line: |
| 37 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 38 | (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] | 39 | 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] | 40 | (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] | 41 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 45 | 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] | 46 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 50 | 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] | 51 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 56 | Using 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 | |
| 61 | If g goes to infinity, we can rewrite the line in terms of x. |
| 62 | x = g'*y + h' |
| 63 | |
| 64 | And solve accordingly in Mathematica: |
| 65 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | (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] | 67 | 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] | 68 | (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] | 69 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | 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] | 74 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 78 | 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] | 79 | 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 | |
| 84 | Thus, 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 90 | |
| 91 | class LineQuadraticIntersections : public Intersections { |
| 92 | public: |
| 93 | |
| 94 | LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i) |
| 95 | : quad(q) |
| 96 | , line(l) |
| 97 | , intersections(i) { |
| 98 | } |
| 99 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 100 | int 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 125 | A += C - 2 * B; // A = a - 2*b + c |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 126 | 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.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 132 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 133 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 134 | return roots; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 135 | } |
| 136 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 137 | int 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.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 144 | return quadraticRoots(D, E, F, intersections.fT[0]); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 145 | } |
| 146 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 147 | int 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 160 | int 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.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 167 | return quadraticRoots(D, E, F, intersections.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 168 | } |
| 169 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 170 | int 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 183 | protected: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 184 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 185 | double 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.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 200 | bool 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 216 | private: |
| 217 | |
| 218 | const Quadratic& quad; |
| 219 | const _Line& line; |
| 220 | Intersections& intersections; |
| 221 | bool moreHorizontal; |
| 222 | |
| 223 | }; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 224 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 225 | // utility for pairs of coincident quads |
| 226 | static double horizontalIntersect(const Quadratic& quad, const _Point& pt) { |
| 227 | Intersections intersections; |
| 228 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
| 229 | int result = q.horizontalIntersect(pt.y); |
| 230 | if (result == 0) { |
| 231 | return -1; |
| 232 | } |
| 233 | assert(result == 1); |
| 234 | double x, y; |
| 235 | xy_at_t(quad, intersections.fT[0][0], x, y); |
| 236 | if (approximately_equal(x, pt.x)) { |
| 237 | return intersections.fT[0][0]; |
| 238 | } |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | static double verticalIntersect(const Quadratic& quad, const _Point& pt) { |
| 243 | Intersections intersections; |
| 244 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
| 245 | int result = q.horizontalIntersect(pt.x); |
| 246 | if (result == 0) { |
| 247 | return -1; |
| 248 | } |
| 249 | assert(result == 1); |
| 250 | double x, y; |
| 251 | xy_at_t(quad, intersections.fT[0][0], x, y); |
| 252 | if (approximately_equal(y, pt.y)) { |
| 253 | return intersections.fT[0][0]; |
| 254 | } |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) { |
| 259 | if (vertical) { |
| 260 | return verticalIntersect(q1, p); |
| 261 | } |
| 262 | return horizontalIntersect(q1, p); |
| 263 | } |
| 264 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 265 | int horizontalIntersect(const Quadratic& quad, double left, double right, |
| 266 | double y, double tRange[2]) { |
| 267 | Intersections i; |
| 268 | LineQuadraticIntersections q(quad, *((_Line*) 0), i); |
| 269 | int result = q.horizontalIntersect(y); |
| 270 | int tCount = 0; |
| 271 | for (int index = 0; index < result; ++index) { |
| 272 | double x, y; |
| 273 | xy_at_t(quad, i.fT[0][index], x, y); |
| 274 | if (x < left || x > right) { |
| 275 | continue; |
| 276 | } |
| 277 | tRange[tCount++] = i.fT[0][index]; |
| 278 | } |
| 279 | return tCount; |
| 280 | } |
| 281 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 282 | int horizontalIntersect(const Quadratic& quad, double left, double right, double y, |
| 283 | bool flipped, Intersections& intersections) { |
| 284 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 285 | int result = q.horizontalIntersect(y, left, right); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 286 | if (flipped) { |
| 287 | // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x |
| 288 | for (int index = 0; index < result; ++index) { |
| 289 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 290 | } |
| 291 | } |
| 292 | return result; |
| 293 | } |
| 294 | |
| 295 | int verticalIntersect(const Quadratic& quad, double top, double bottom, double x, |
| 296 | bool flipped, Intersections& intersections) { |
| 297 | LineQuadraticIntersections q(quad, *((_Line*) 0), intersections); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 298 | int result = q.verticalIntersect(x, top, bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 299 | if (flipped) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 300 | // 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] | 301 | for (int index = 0; index < result; ++index) { |
| 302 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 303 | } |
| 304 | } |
| 305 | return result; |
| 306 | } |
| 307 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 308 | int intersect(const Quadratic& quad, const _Line& line, Intersections& i) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 309 | LineQuadraticIntersections q(quad, line, i); |
| 310 | return q.intersect(); |
| 311 | } |