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 "CubicUtilities.h" |
| 9 | #include "Intersections.h" |
| 10 | #include "LineUtilities.h" |
| 11 | |
| 12 | /* |
| 13 | Find the interection of a line and cubic by solving for valid t values. |
| 14 | |
| 15 | Analogous to line-quadratic intersection, solve line-cubic intersection by |
| 16 | representing the cubic as: |
| 17 | x = a(1-t)^3 + 2b(1-t)^2t + c(1-t)t^2 + dt^3 |
| 18 | y = e(1-t)^3 + 2f(1-t)^2t + g(1-t)t^2 + ht^3 |
| 19 | and the line as: |
| 20 | y = i*x + j (if the line is more horizontal) |
| 21 | or: |
| 22 | x = i*y + j (if the line is more vertical) |
| 23 | |
| 24 | Then using Mathematica, solve for the values of t where the cubic intersects the |
| 25 | line: |
| 26 | |
| 27 | (in) Resultant[ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - x, |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 29 | e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - i*x - j, x] |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 30 | (out) -e + j + |
| 31 | 3 e t - 3 f t - |
| 32 | 3 e t^2 + 6 f t^2 - 3 g t^2 + |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 33 | e t^3 - 3 f t^3 + 3 g t^3 - h t^3 + |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 34 | i ( a - |
| 35 | 3 a t + 3 b t + |
| 36 | 3 a t^2 - 6 b t^2 + 3 c t^2 - |
| 37 | a t^3 + 3 b t^3 - 3 c t^3 + d t^3 ) |
| 38 | |
| 39 | if i goes to infinity, we can rewrite the line in terms of x. Mathematica: |
| 40 | |
| 41 | (in) Resultant[ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 | a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - i*y - j, |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 43 | e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y] |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | (out) a - j - |
| 45 | 3 a t + 3 b t + |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 46 | 3 a t^2 - 6 b t^2 + 3 c t^2 - |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | a t^3 + 3 b t^3 - 3 c t^3 + d t^3 - |
| 48 | i ( e - |
| 49 | 3 e t + 3 f t + |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 50 | 3 e t^2 - 6 f t^2 + 3 g t^2 - |
| 51 | e t^3 + 3 f t^3 - 3 g t^3 + h t^3 ) |
| 52 | |
| 53 | Solving this with Mathematica produces an expression with hundreds of terms; |
| 54 | instead, use Numeric Solutions recipe to solve the cubic. |
| 55 | |
| 56 | The near-horizontal case, in terms of: Ax^3 + Bx^2 + Cx + D == 0 |
| 57 | A = (-(-e + 3*f - 3*g + h) + i*(-a + 3*b - 3*c + d) ) |
| 58 | B = 3*(-( e - 2*f + g ) + i*( a - 2*b + c ) ) |
| 59 | C = 3*(-(-e + f ) + i*(-a + b ) ) |
| 60 | D = (-( e ) + i*( a ) + j ) |
| 61 | |
| 62 | The near-vertical case, in terms of: Ax^3 + Bx^2 + Cx + D == 0 |
| 63 | A = ( (-a + 3*b - 3*c + d) - i*(-e + 3*f - 3*g + h) ) |
| 64 | B = 3*( ( a - 2*b + c ) - i*( e - 2*f + g ) ) |
| 65 | C = 3*( (-a + b ) - i*(-e + f ) ) |
| 66 | D = ( ( a ) - i*( e ) - j ) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 67 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 68 | For horizontal lines: |
| 69 | (in) Resultant[ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - j, |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 71 | e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y] |
| 72 | (out) e - j - |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | 3 e t + 3 f t + |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 74 | 3 e t^2 - 6 f t^2 + 3 g t^2 - |
| 75 | e t^3 + 3 f t^3 - 3 g t^3 + h t^3 |
| 76 | So the cubic coefficients are: |
| 77 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 78 | */ |
| 79 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 80 | class LineCubicIntersections { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 81 | public: |
| 82 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 83 | LineCubicIntersections(const Cubic& c, const _Line& l, Intersections& i) |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 84 | : cubic(c) |
| 85 | , line(l) |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 86 | , intersections(i) { |
| 87 | } |
| 88 | |
| 89 | // see parallel routine in line quadratic intersections |
| 90 | int intersectRay(double roots[3]) { |
| 91 | double adj = line[1].x - line[0].x; |
| 92 | double opp = line[1].y - line[0].y; |
| 93 | Cubic r; |
| 94 | for (int n = 0; n < 4; ++n) { |
| 95 | r[n].x = (cubic[n].y - line[0].y) * adj - (cubic[n].x - line[0].x) * opp; |
| 96 | } |
| 97 | double A, B, C, D; |
| 98 | coefficients(&r[0].x, A, B, C, D); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 99 | return cubicRootsValidT(A, B, C, D, roots); |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 100 | } |
| 101 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 102 | int intersect() { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 103 | addEndPoints(); |
| 104 | double rootVals[3]; |
| 105 | int roots = intersectRay(rootVals); |
| 106 | for (int index = 0; index < roots; ++index) { |
| 107 | double cubicT = rootVals[index]; |
| 108 | double lineT = findLineT(cubicT); |
| 109 | if (pinTs(cubicT, lineT)) { |
| 110 | intersections.insert(cubicT, lineT); |
| 111 | } |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 112 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 113 | return intersections.fUsed; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 114 | } |
| 115 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 116 | int horizontalIntersect(double axisIntercept, double roots[3]) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 117 | double A, B, C, D; |
| 118 | coefficients(&cubic[0].y, A, B, C, D); |
| 119 | D -= axisIntercept; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 120 | return cubicRootsValidT(A, B, C, D, roots); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 121 | } |
| 122 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 123 | int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) { |
| 124 | addHorizontalEndPoints(left, right, axisIntercept); |
| 125 | double rootVals[3]; |
| 126 | int roots = horizontalIntersect(axisIntercept, rootVals); |
| 127 | for (int index = 0; index < roots; ++index) { |
| 128 | double x; |
| 129 | double cubicT = rootVals[index]; |
| 130 | xy_at_t(cubic, cubicT, x, *(double*) NULL); |
| 131 | double lineT = (x - left) / (right - left); |
| 132 | if (pinTs(cubicT, lineT)) { |
| 133 | intersections.insert(cubicT, lineT); |
| 134 | } |
| 135 | } |
| 136 | if (flipped) { |
| 137 | flip(); |
| 138 | } |
| 139 | return intersections.fUsed; |
| 140 | } |
| 141 | |
| 142 | int verticalIntersect(double axisIntercept, double roots[3]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 143 | double A, B, C, D; |
| 144 | coefficients(&cubic[0].x, A, B, C, D); |
| 145 | D -= axisIntercept; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 146 | return cubicRootsValidT(A, B, C, D, roots); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) { |
| 150 | addVerticalEndPoints(top, bottom, axisIntercept); |
| 151 | double rootVals[3]; |
| 152 | int roots = verticalIntersect(axisIntercept, rootVals); |
| 153 | for (int index = 0; index < roots; ++index) { |
| 154 | double y; |
| 155 | double cubicT = rootVals[index]; |
| 156 | xy_at_t(cubic, cubicT, *(double*) NULL, y); |
| 157 | double lineT = (y - top) / (bottom - top); |
| 158 | if (pinTs(cubicT, lineT)) { |
| 159 | intersections.insert(cubicT, lineT); |
| 160 | } |
| 161 | } |
| 162 | if (flipped) { |
| 163 | flip(); |
| 164 | } |
| 165 | return intersections.fUsed; |
| 166 | } |
| 167 | |
| 168 | protected: |
| 169 | |
| 170 | void addEndPoints() |
| 171 | { |
| 172 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 173 | for (int lIndex = 0; lIndex < 2; lIndex++) { |
| 174 | if (cubic[cIndex] == line[lIndex]) { |
| 175 | intersections.insert(cIndex >> 1, lIndex); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void addHorizontalEndPoints(double left, double right, double y) |
| 182 | { |
| 183 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 184 | if (cubic[cIndex].y != y) { |
| 185 | continue; |
| 186 | } |
| 187 | if (cubic[cIndex].x == left) { |
| 188 | intersections.insert(cIndex >> 1, 0); |
| 189 | } |
| 190 | if (cubic[cIndex].x == right) { |
| 191 | intersections.insert(cIndex >> 1, 1); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void addVerticalEndPoints(double top, double bottom, double x) |
| 197 | { |
| 198 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 199 | if (cubic[cIndex].x != x) { |
| 200 | continue; |
| 201 | } |
| 202 | if (cubic[cIndex].y == top) { |
| 203 | intersections.insert(cIndex >> 1, 0); |
| 204 | } |
| 205 | if (cubic[cIndex].y == bottom) { |
| 206 | intersections.insert(cIndex >> 1, 1); |
| 207 | } |
| 208 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 209 | } |
| 210 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 211 | double findLineT(double t) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 212 | double x, y; |
| 213 | xy_at_t(cubic, t, x, y); |
| 214 | double dx = line[1].x - line[0].x; |
| 215 | double dy = line[1].y - line[0].y; |
| 216 | if (fabs(dx) > fabs(dy)) { |
| 217 | return (x - line[0].x) / dx; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 218 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 219 | return (y - line[0].y) / dy; |
| 220 | } |
| 221 | |
| 222 | void flip() { |
| 223 | // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y |
| 224 | int roots = intersections.fUsed; |
| 225 | for (int index = 0; index < roots; ++index) { |
| 226 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | bool pinTs(double& cubicT, double& lineT) { |
| 231 | if (!approximately_one_or_less(lineT)) { |
| 232 | return false; |
| 233 | } |
| 234 | if (!approximately_zero_or_more(lineT)) { |
| 235 | return false; |
| 236 | } |
| 237 | if (cubicT < 0) { |
| 238 | cubicT = 0; |
| 239 | } else if (cubicT > 1) { |
| 240 | cubicT = 1; |
| 241 | } |
| 242 | if (lineT < 0) { |
| 243 | lineT = 0; |
| 244 | } else if (lineT > 1) { |
| 245 | lineT = 1; |
| 246 | } |
| 247 | return true; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private: |
| 251 | |
| 252 | const Cubic& cubic; |
| 253 | const _Line& line; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 254 | Intersections& intersections; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 255 | }; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 256 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 257 | int horizontalIntersect(const Cubic& cubic, double left, double right, double y, |
| 258 | double tRange[3]) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 259 | LineCubicIntersections c(cubic, *((_Line*) 0), *((Intersections*) 0)); |
| 260 | double rootVals[3]; |
| 261 | int result = c.horizontalIntersect(y, rootVals); |
| 262 | int tCount = 0; |
| 263 | for (int index = 0; index < result; ++index) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 264 | double x, y; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 265 | xy_at_t(cubic, rootVals[index], x, y); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 266 | if (x < left || x > right) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 267 | continue; |
| 268 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 269 | tRange[tCount++] = rootVals[index]; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 270 | } |
| 271 | return result; |
| 272 | } |
| 273 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 274 | int horizontalIntersect(const Cubic& cubic, double left, double right, double y, |
| 275 | bool flipped, Intersections& intersections) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 276 | LineCubicIntersections c(cubic, *((_Line*) 0), intersections); |
| 277 | return c.horizontalIntersect(y, left, right, flipped); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | int verticalIntersect(const Cubic& cubic, double top, double bottom, double x, |
| 281 | bool flipped, Intersections& intersections) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 282 | LineCubicIntersections c(cubic, *((_Line*) 0), intersections); |
| 283 | return c.verticalIntersect(x, top, bottom, flipped); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 284 | } |
| 285 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 286 | int intersect(const Cubic& cubic, const _Line& line, Intersections& i) { |
| 287 | LineCubicIntersections c(cubic, line, i); |
| 288 | return c.intersect(); |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 289 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame^] | 290 | |
| 291 | int intersectRay(const Cubic& cubic, const _Line& line, Intersections& i) { |
| 292 | LineCubicIntersections c(cubic, line, i); |
| 293 | return c.intersectRay(i.fT[0]); |
| 294 | } |