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 | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 8 | #include "Intersections.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 9 | #include "LineIntersection.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 10 | #include <algorithm> // used for std::swap |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 11 | |
| 12 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 13 | /* |
| 14 | Determine the intersection point of two line segments |
| 15 | Return FALSE if the lines don't intersect |
| 16 | from: http://paulbourke.net/geometry/lineline2d/ |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 19 | int intersect(const _Line& a, const _Line& b, double aRange[2], double bRange[2]) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 20 | double axLen = a[1].x - a[0].x; |
| 21 | double ayLen = a[1].y - a[0].y; |
| 22 | double bxLen = b[1].x - b[0].x; |
| 23 | double byLen = b[1].y - b[0].y; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | /* Slopes match when denom goes to zero: |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 25 | axLen / ayLen == bxLen / byLen |
| 26 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 27 | byLen * axLen == ayLen * bxLen |
| 28 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 29 | */ |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 30 | double denom = byLen * axLen - ayLen * bxLen; |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame^] | 31 | if (approximately_zero(denom)) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 32 | /* See if the axis intercepts match: |
| 33 | ay - ax * ayLen / axLen == by - bx * ayLen / axLen |
| 34 | axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) |
| 35 | axLen * ay - ax * ayLen == axLen * by - bx * ayLen |
| 36 | */ |
| 37 | if (approximately_equal_squared(axLen * a[0].y - ayLen * a[0].x, |
| 38 | axLen * b[0].y - ayLen * b[0].x)) { |
| 39 | const double* aPtr; |
| 40 | const double* bPtr; |
| 41 | if (fabs(axLen) > fabs(ayLen) || fabs(bxLen) > fabs(byLen)) { |
| 42 | aPtr = &a[0].x; |
| 43 | bPtr = &b[0].x; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 44 | } else { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 45 | aPtr = &a[0].y; |
| 46 | bPtr = &b[0].y; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 47 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 48 | #if 0 // sorting edges fails to preserve original direction |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 49 | double aMin = aPtr[0]; |
| 50 | double aMax = aPtr[2]; |
| 51 | double bMin = bPtr[0]; |
| 52 | double bMax = bPtr[2]; |
| 53 | if (aMin > aMax) { |
| 54 | std::swap(aMin, aMax); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 55 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 56 | if (bMin > bMax) { |
| 57 | std::swap(bMin, bMax); |
| 58 | } |
| 59 | if (aMax < bMin || bMax < aMin) { |
| 60 | return 0; |
| 61 | } |
| 62 | if (aRange) { |
| 63 | aRange[0] = bMin <= aMin ? 0 : (bMin - aMin) / (aMax - aMin); |
| 64 | aRange[1] = bMax >= aMax ? 1 : (bMax - aMin) / (aMax - aMin); |
| 65 | } |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 66 | int bIn = (aPtr[0] - aPtr[2]) * (bPtr[0] - bPtr[2]) < 0; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 67 | if (bRange) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 68 | bRange[bIn] = aMin <= bMin ? 0 : (aMin - bMin) / (bMax - bMin); |
| 69 | bRange[!bIn] = aMax >= bMax ? 1 : (aMax - bMin) / (bMax - bMin); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 70 | } |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 71 | return 1 + ((aRange[0] != aRange[1]) || (bRange[0] != bRange[1])); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 72 | #else |
| 73 | assert(aRange); |
| 74 | assert(bRange); |
| 75 | double a0 = aPtr[0]; |
| 76 | double a1 = aPtr[2]; |
| 77 | double b0 = bPtr[0]; |
| 78 | double b1 = bPtr[2]; |
| 79 | double at0 = (a0 - b0) / (a0 - a1); |
| 80 | double at1 = (a0 - b1) / (a0 - a1); |
| 81 | if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) { |
| 82 | return 0; |
| 83 | } |
| 84 | aRange[0] = std::max(std::min(at0, 1.0), 0.0); |
| 85 | aRange[1] = std::max(std::min(at1, 1.0), 0.0); |
| 86 | int bIn = (a0 - a1) * (b0 - b1) < 0; |
| 87 | bRange[bIn] = std::max(std::min((b0 - a0) / (b0 - b1), 1.0), 0.0); |
| 88 | bRange[!bIn] = std::max(std::min((b0 - a1) / (b0 - b1), 1.0), 0.0); |
| 89 | bool second = fabs(aRange[0] - aRange[1]) > FLT_EPSILON; |
| 90 | assert((fabs(bRange[0] - bRange[1]) <= FLT_EPSILON) ^ second); |
| 91 | return 1 + second; |
| 92 | #endif |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 93 | } |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 94 | } |
| 95 | double ab0y = a[0].y - b[0].y; |
| 96 | double ab0x = a[0].x - b[0].x; |
| 97 | double numerA = ab0y * bxLen - byLen * ab0x; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 98 | if (numerA < 0 && denom > numerA || numerA > 0 && denom < numerA) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 99 | return 0; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 100 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 101 | double numerB = ab0y * axLen - ayLen * ab0x; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 102 | if (numerB < 0 && denom > numerB || numerB > 0 && denom < numerB) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 103 | return 0; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 104 | } |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 105 | /* Is the intersection along the the segments */ |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 106 | if (aRange) { |
| 107 | aRange[0] = numerA / denom; |
| 108 | } |
| 109 | if (bRange) { |
| 110 | bRange[0] = numerB / denom; |
| 111 | } |
| 112 | return 1; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 113 | } |
| 114 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 115 | int horizontalIntersect(const _Line& line, double y, double tRange[2]) { |
| 116 | double min = line[0].y; |
| 117 | double max = line[1].y; |
| 118 | if (min > max) { |
| 119 | std::swap(min, max); |
| 120 | } |
| 121 | if (min > y || max < y) { |
| 122 | return 0; |
| 123 | } |
| 124 | if (approximately_equal(min, max)) { |
| 125 | tRange[0] = 0; |
| 126 | tRange[1] = 1; |
| 127 | return 2; |
| 128 | } |
| 129 | tRange[0] = (y - line[0].y) / (line[1].y - line[0].y); |
| 130 | return 1; |
| 131 | } |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 132 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 133 | // OPTIMIZATION Given: dy = line[1].y - line[0].y |
| 134 | // and: xIntercept / (y - line[0].y) == (line[1].x - line[0].x) / dy |
| 135 | // then: xIntercept * dy == (line[1].x - line[0].x) * (y - line[0].y) |
| 136 | // Assuming that dy is always > 0, the line segment intercepts if: |
| 137 | // left * dy <= xIntercept * dy <= right * dy |
| 138 | // thus: left * dy <= (line[1].x - line[0].x) * (y - line[0].y) <= right * dy |
| 139 | // (clever as this is, it does not give us the t value, so may be useful only |
| 140 | // as a quick reject -- and maybe not then; it takes 3 muls, 3 adds, 2 cmps) |
| 141 | int horizontalLineIntersect(const _Line& line, double left, double right, |
| 142 | double y, double tRange[2]) { |
| 143 | int result = horizontalIntersect(line, y, tRange); |
| 144 | if (result != 1) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 145 | // FIXME: this is incorrect if result == 2 |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 146 | return result; |
| 147 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 148 | double xIntercept = line[0].x + tRange[0] * (line[1].x - line[0].x); |
| 149 | if (xIntercept > right || xIntercept < left) { |
| 150 | return 0; |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 155 | int horizontalIntersect(const _Line& line, double left, double right, |
| 156 | double y, bool flipped, Intersections& intersections) { |
| 157 | int result = horizontalIntersect(line, y, intersections.fT[0]); |
| 158 | switch (result) { |
| 159 | case 0: |
| 160 | break; |
| 161 | case 1: { |
| 162 | double xIntercept = line[0].x + intersections.fT[0][0] |
| 163 | * (line[1].x - line[0].x); |
| 164 | if (xIntercept > right || xIntercept < left) { |
| 165 | return 0; |
| 166 | } |
| 167 | intersections.fT[1][0] = (xIntercept - left) / (right - left); |
| 168 | break; |
| 169 | } |
| 170 | case 2: |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 171 | #if 0 // sorting edges fails to preserve original direction |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 172 | double lineL = line[0].x; |
| 173 | double lineR = line[1].x; |
| 174 | if (lineL > lineR) { |
| 175 | std::swap(lineL, lineR); |
| 176 | } |
| 177 | double overlapL = std::max(left, lineL); |
| 178 | double overlapR = std::min(right, lineR); |
| 179 | if (overlapL > overlapR) { |
| 180 | return 0; |
| 181 | } |
| 182 | if (overlapL == overlapR) { |
| 183 | result = 1; |
| 184 | } |
| 185 | intersections.fT[0][0] = (overlapL - line[0].x) / (line[1].x - line[0].x); |
| 186 | intersections.fT[1][0] = (overlapL - left) / (right - left); |
| 187 | if (result > 1) { |
| 188 | intersections.fT[0][1] = (overlapR - line[0].x) / (line[1].x - line[0].x); |
| 189 | intersections.fT[1][1] = (overlapR - left) / (right - left); |
| 190 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 191 | #else |
| 192 | double a0 = line[0].x; |
| 193 | double a1 = line[1].x; |
| 194 | double b0 = flipped ? right : left; |
| 195 | double b1 = flipped ? left : right; |
| 196 | // FIXME: share common code below |
| 197 | double at0 = (a0 - b0) / (a0 - a1); |
| 198 | double at1 = (a0 - b1) / (a0 - a1); |
| 199 | if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) { |
| 200 | return 0; |
| 201 | } |
| 202 | intersections.fT[0][0] = std::max(std::min(at0, 1.0), 0.0); |
| 203 | intersections.fT[0][1] = std::max(std::min(at1, 1.0), 0.0); |
| 204 | int bIn = (a0 - a1) * (b0 - b1) < 0; |
| 205 | intersections.fT[1][bIn] = std::max(std::min((b0 - a0) / (b0 - b1), |
| 206 | 1.0), 0.0); |
| 207 | intersections.fT[1][!bIn] = std::max(std::min((b0 - a1) / (b0 - b1), |
| 208 | 1.0), 0.0); |
| 209 | bool second = fabs(intersections.fT[0][0] - intersections.fT[0][1]) |
| 210 | > FLT_EPSILON; |
| 211 | assert((fabs(intersections.fT[1][0] - intersections.fT[1][1]) |
| 212 | <= FLT_EPSILON) ^ second); |
| 213 | return 1 + second; |
| 214 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | if (flipped) { |
| 218 | // OPTIMIZATION: instead of swapping, pass original line, use [1].x - [0].x |
| 219 | for (int index = 0; index < result; ++index) { |
| 220 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 221 | } |
| 222 | } |
| 223 | return result; |
| 224 | } |
| 225 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 226 | static int verticalIntersect(const _Line& line, double x, double tRange[2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 227 | double min = line[0].x; |
| 228 | double max = line[1].x; |
| 229 | if (min > max) { |
| 230 | std::swap(min, max); |
| 231 | } |
| 232 | if (min > x || max < x) { |
| 233 | return 0; |
| 234 | } |
| 235 | if (approximately_equal(min, max)) { |
| 236 | tRange[0] = 0; |
| 237 | tRange[1] = 1; |
| 238 | return 2; |
| 239 | } |
| 240 | tRange[0] = (x - line[0].x) / (line[1].x - line[0].x); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | int verticalIntersect(const _Line& line, double top, double bottom, |
| 245 | double x, bool flipped, Intersections& intersections) { |
| 246 | int result = verticalIntersect(line, x, intersections.fT[0]); |
| 247 | switch (result) { |
| 248 | case 0: |
| 249 | break; |
| 250 | case 1: { |
| 251 | double yIntercept = line[0].y + intersections.fT[0][0] |
| 252 | * (line[1].y - line[0].y); |
| 253 | if (yIntercept > bottom || yIntercept < top) { |
| 254 | return 0; |
| 255 | } |
| 256 | intersections.fT[1][0] = (yIntercept - top) / (bottom - top); |
| 257 | break; |
| 258 | } |
| 259 | case 2: |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 260 | #if 0 // sorting edges fails to preserve original direction |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 261 | double lineT = line[0].y; |
| 262 | double lineB = line[1].y; |
| 263 | if (lineT > lineB) { |
| 264 | std::swap(lineT, lineB); |
| 265 | } |
| 266 | double overlapT = std::max(top, lineT); |
| 267 | double overlapB = std::min(bottom, lineB); |
| 268 | if (overlapT > overlapB) { |
| 269 | return 0; |
| 270 | } |
| 271 | if (overlapT == overlapB) { |
| 272 | result = 1; |
| 273 | } |
| 274 | intersections.fT[0][0] = (overlapT - line[0].y) / (line[1].y - line[0].y); |
| 275 | intersections.fT[1][0] = (overlapT - top) / (bottom - top); |
| 276 | if (result > 1) { |
| 277 | intersections.fT[0][1] = (overlapB - line[0].y) / (line[1].y - line[0].y); |
| 278 | intersections.fT[1][1] = (overlapB - top) / (bottom - top); |
| 279 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 280 | #else |
| 281 | double a0 = line[0].y; |
| 282 | double a1 = line[1].y; |
| 283 | double b0 = flipped ? bottom : top; |
| 284 | double b1 = flipped ? top : bottom; |
| 285 | // FIXME: share common code above |
| 286 | double at0 = (a0 - b0) / (a0 - a1); |
| 287 | double at1 = (a0 - b1) / (a0 - a1); |
| 288 | if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) { |
| 289 | return 0; |
| 290 | } |
| 291 | intersections.fT[0][0] = std::max(std::min(at0, 1.0), 0.0); |
| 292 | intersections.fT[0][1] = std::max(std::min(at1, 1.0), 0.0); |
| 293 | int bIn = (a0 - a1) * (b0 - b1) < 0; |
| 294 | intersections.fT[1][bIn] = std::max(std::min((b0 - a0) / (b0 - b1), |
| 295 | 1.0), 0.0); |
| 296 | intersections.fT[1][!bIn] = std::max(std::min((b0 - a1) / (b0 - b1), |
| 297 | 1.0), 0.0); |
| 298 | bool second = fabs(intersections.fT[0][0] - intersections.fT[0][1]) |
| 299 | > FLT_EPSILON; |
| 300 | assert((fabs(intersections.fT[1][0] - intersections.fT[1][1]) |
| 301 | <= FLT_EPSILON) ^ second); |
| 302 | return 1 + second; |
| 303 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | if (flipped) { |
| 307 | // OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y |
| 308 | for (int index = 0; index < result; ++index) { |
| 309 | intersections.fT[1][index] = 1 - intersections.fT[1][index]; |
| 310 | } |
| 311 | } |
| 312 | return result; |
| 313 | } |
| 314 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 315 | // from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py |
| 316 | // 4 subs, 2 muls, 1 cmp |
| 317 | static bool ccw(const _Point& A, const _Point& B, const _Point& C) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 318 | return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // 16 subs, 8 muls, 6 cmps |
| 322 | bool testIntersect(const _Line& a, const _Line& b) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 323 | return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1]) |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 324 | && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]); |
| 325 | } |