caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +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 | */ |
| 7 | #include "SkIntersections.h" |
| 8 | #include "SkPathOpsLine.h" |
| 9 | |
| 10 | /* Determine the intersection point of two lines. This assumes the lines are not parallel, |
| 11 | and that that the lines are infinite. |
| 12 | From http://en.wikipedia.org/wiki/Line-line_intersection |
| 13 | */ |
| 14 | SkDPoint SkIntersections::Line(const SkDLine& a, const SkDLine& b) { |
| 15 | double axLen = a[1].fX - a[0].fX; |
| 16 | double ayLen = a[1].fY - a[0].fY; |
| 17 | double bxLen = b[1].fX - b[0].fX; |
| 18 | double byLen = b[1].fY - b[0].fY; |
| 19 | double denom = byLen * axLen - ayLen * bxLen; |
| 20 | SkASSERT(denom); |
| 21 | double term1 = a[1].fX * a[0].fY - a[1].fY * a[0].fX; |
| 22 | double term2 = b[1].fX * b[0].fY - b[1].fY * b[0].fX; |
| 23 | SkDPoint p; |
| 24 | p.fX = (term1 * bxLen - axLen * term2) / denom; |
| 25 | p.fY = (term1 * byLen - ayLen * term2) / denom; |
| 26 | return p; |
| 27 | } |
| 28 | |
| 29 | int SkIntersections::computePoints(const SkDLine& line, int used) { |
| 30 | fPt[0] = line.xyAtT(fT[0][0]); |
| 31 | if ((fUsed = used) == 2) { |
| 32 | fPt[1] = line.xyAtT(fT[0][1]); |
| 33 | } |
| 34 | return fUsed; |
| 35 | } |
| 36 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame^] | 37 | int SkIntersections::intersectRay(const SkDLine& a, const SkDLine& b) { |
| 38 | double axLen = a[1].fX - a[0].fX; |
| 39 | double ayLen = a[1].fY - a[0].fY; |
| 40 | double bxLen = b[1].fX - b[0].fX; |
| 41 | double byLen = b[1].fY - b[0].fY; |
| 42 | /* Slopes match when denom goes to zero: |
| 43 | axLen / ayLen == bxLen / byLen |
| 44 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 45 | byLen * axLen == ayLen * bxLen |
| 46 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 47 | */ |
| 48 | double denom = byLen * axLen - ayLen * bxLen; |
| 49 | double ab0y = a[0].fY - b[0].fY; |
| 50 | double ab0x = a[0].fX - b[0].fX; |
| 51 | double numerA = ab0y * bxLen - byLen * ab0x; |
| 52 | double numerB = ab0y * axLen - ayLen * ab0x; |
| 53 | numerA /= denom; |
| 54 | numerB /= denom; |
| 55 | int used; |
| 56 | if (!approximately_zero(denom)) { |
| 57 | fT[0][0] = numerA; |
| 58 | fT[1][0] = numerB; |
| 59 | used = 1; |
| 60 | } else { |
| 61 | /* See if the axis intercepts match: |
| 62 | ay - ax * ayLen / axLen == by - bx * ayLen / axLen |
| 63 | axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) |
| 64 | axLen * ay - ax * ayLen == axLen * by - bx * ayLen |
| 65 | */ |
| 66 | if (!AlmostEqualUlps(axLen * a[0].fY - ayLen * a[0].fX, |
| 67 | axLen * b[0].fY - ayLen * b[0].fX)) { |
| 68 | return fUsed = 0; |
| 69 | } |
| 70 | // there's no great answer for intersection points for coincident rays, but return something |
| 71 | fT[0][0] = fT[1][0] = 0; |
| 72 | fT[1][0] = fT[1][1] = 1; |
| 73 | used = 2; |
| 74 | } |
| 75 | return computePoints(a, used); |
| 76 | } |
| 77 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 78 | /* |
| 79 | Determine the intersection point of two line segments |
| 80 | Return FALSE if the lines don't intersect |
| 81 | from: http://paulbourke.net/geometry/lineline2d/ |
| 82 | */ |
| 83 | |
| 84 | int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) { |
| 85 | double axLen = a[1].fX - a[0].fX; |
| 86 | double ayLen = a[1].fY - a[0].fY; |
| 87 | double bxLen = b[1].fX - b[0].fX; |
| 88 | double byLen = b[1].fY - b[0].fY; |
| 89 | /* Slopes match when denom goes to zero: |
| 90 | axLen / ayLen == bxLen / byLen |
| 91 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 92 | byLen * axLen == ayLen * bxLen |
| 93 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 94 | */ |
| 95 | double denom = byLen * axLen - ayLen * bxLen; |
| 96 | double ab0y = a[0].fY - b[0].fY; |
| 97 | double ab0x = a[0].fX - b[0].fX; |
| 98 | double numerA = ab0y * bxLen - byLen * ab0x; |
| 99 | double numerB = ab0y * axLen - ayLen * ab0x; |
| 100 | bool mayNotOverlap = (numerA < 0 && denom > numerA) || (numerA > 0 && denom < numerA) |
| 101 | || (numerB < 0 && denom > numerB) || (numerB > 0 && denom < numerB); |
| 102 | numerA /= denom; |
| 103 | numerB /= denom; |
| 104 | if ((!approximately_zero(denom) || (!approximately_zero_inverse(numerA) |
| 105 | && !approximately_zero_inverse(numerB))) && !sk_double_isnan(numerA) |
| 106 | && !sk_double_isnan(numerB)) { |
| 107 | if (mayNotOverlap) { |
| 108 | return fUsed = 0; |
| 109 | } |
| 110 | fT[0][0] = numerA; |
| 111 | fT[1][0] = numerB; |
| 112 | fPt[0] = a.xyAtT(numerA); |
| 113 | return computePoints(a, 1); |
| 114 | } |
| 115 | /* See if the axis intercepts match: |
| 116 | ay - ax * ayLen / axLen == by - bx * ayLen / axLen |
| 117 | axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) |
| 118 | axLen * ay - ax * ayLen == axLen * by - bx * ayLen |
| 119 | */ |
caryclark@google.com | c3f6357 | 2013-04-23 12:04:05 +0000 | [diff] [blame] | 120 | if (!AlmostEqualUlps(axLen * a[0].fY - ayLen * a[0].fX, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 121 | axLen * b[0].fY - ayLen * b[0].fX)) { |
| 122 | return fUsed = 0; |
| 123 | } |
| 124 | const double* aPtr; |
| 125 | const double* bPtr; |
| 126 | if (fabs(axLen) > fabs(ayLen) || fabs(bxLen) > fabs(byLen)) { |
| 127 | aPtr = &a[0].fX; |
| 128 | bPtr = &b[0].fX; |
| 129 | } else { |
| 130 | aPtr = &a[0].fY; |
| 131 | bPtr = &b[0].fY; |
| 132 | } |
| 133 | double a0 = aPtr[0]; |
| 134 | double a1 = aPtr[2]; |
| 135 | double b0 = bPtr[0]; |
| 136 | double b1 = bPtr[2]; |
| 137 | // OPTIMIZATION: restructure to reject before the divide |
| 138 | // e.g., if ((a0 - b0) * (a0 - a1) < 0 || abs(a0 - b0) > abs(a0 - a1)) |
| 139 | // (except efficient) |
| 140 | double aDenom = a0 - a1; |
| 141 | if (approximately_zero(aDenom)) { |
| 142 | if (!between(b0, a0, b1)) { |
| 143 | return fUsed = 0; |
| 144 | } |
| 145 | fT[0][0] = fT[0][1] = 0; |
| 146 | } else { |
| 147 | double at0 = (a0 - b0) / aDenom; |
| 148 | double at1 = (a0 - b1) / aDenom; |
| 149 | if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) { |
| 150 | return fUsed = 0; |
| 151 | } |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 152 | fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0); |
| 153 | fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 154 | } |
| 155 | double bDenom = b0 - b1; |
| 156 | if (approximately_zero(bDenom)) { |
| 157 | fT[1][0] = fT[1][1] = 0; |
| 158 | } else { |
| 159 | int bIn = aDenom * bDenom < 0; |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 160 | fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / bDenom, 1.0), 0.0); |
| 161 | fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / bDenom, 1.0), 0.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 162 | } |
| 163 | bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON; |
| 164 | SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second); |
| 165 | return computePoints(a, 1 + second); |
| 166 | } |
| 167 | |
| 168 | int SkIntersections::horizontal(const SkDLine& line, double y) { |
| 169 | double min = line[0].fY; |
| 170 | double max = line[1].fY; |
| 171 | if (min > max) { |
| 172 | SkTSwap(min, max); |
| 173 | } |
| 174 | if (min > y || max < y) { |
| 175 | return fUsed = 0; |
| 176 | } |
| 177 | if (AlmostEqualUlps(min, max)) { |
| 178 | fT[0][0] = 0; |
| 179 | fT[0][1] = 1; |
| 180 | return fUsed = 2; |
| 181 | } |
| 182 | fT[0][0] = (y - line[0].fY) / (line[1].fY - line[0].fY); |
| 183 | return fUsed = 1; |
| 184 | } |
| 185 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 186 | int SkIntersections::horizontal(const SkDLine& line, double left, double right, |
| 187 | double y, bool flipped) { |
| 188 | int result = horizontal(line, y); |
| 189 | switch (result) { |
| 190 | case 0: |
| 191 | break; |
| 192 | case 1: { |
| 193 | double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX); |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 194 | if (!precisely_between(left, xIntercept, right)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 195 | return fUsed = 0; |
| 196 | } |
| 197 | fT[1][0] = (xIntercept - left) / (right - left); |
| 198 | break; |
| 199 | } |
| 200 | case 2: |
| 201 | double a0 = line[0].fX; |
| 202 | double a1 = line[1].fX; |
| 203 | double b0 = flipped ? right : left; |
| 204 | double b1 = flipped ? left : right; |
| 205 | // FIXME: share common code below |
| 206 | double at0 = (a0 - b0) / (a0 - a1); |
| 207 | double at1 = (a0 - b1) / (a0 - a1); |
| 208 | if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) { |
| 209 | return fUsed = 0; |
| 210 | } |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 211 | fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0); |
| 212 | fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 213 | int bIn = (a0 - a1) * (b0 - b1) < 0; |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 214 | fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1), 1.0), 0.0); |
| 215 | fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1), 1.0), 0.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 216 | bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON; |
| 217 | SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second); |
| 218 | return computePoints(line, 1 + second); |
| 219 | } |
| 220 | if (flipped) { |
| 221 | // OPTIMIZATION: instead of swapping, pass original line, use [1].fX - [0].fX |
| 222 | for (int index = 0; index < result; ++index) { |
| 223 | fT[1][index] = 1 - fT[1][index]; |
| 224 | } |
| 225 | } |
| 226 | return computePoints(line, result); |
| 227 | } |
| 228 | |
| 229 | int SkIntersections::vertical(const SkDLine& line, double x) { |
| 230 | double min = line[0].fX; |
| 231 | double max = line[1].fX; |
| 232 | if (min > max) { |
| 233 | SkTSwap(min, max); |
| 234 | } |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 235 | if (!precisely_between(min, x, max)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 236 | return fUsed = 0; |
| 237 | } |
| 238 | if (AlmostEqualUlps(min, max)) { |
| 239 | fT[0][0] = 0; |
| 240 | fT[0][1] = 1; |
| 241 | return fUsed = 2; |
| 242 | } |
| 243 | fT[0][0] = (x - line[0].fX) / (line[1].fX - line[0].fX); |
| 244 | return fUsed = 1; |
| 245 | } |
| 246 | |
| 247 | int SkIntersections::vertical(const SkDLine& line, double top, double bottom, |
| 248 | double x, bool flipped) { |
| 249 | int result = vertical(line, x); |
| 250 | switch (result) { |
| 251 | case 0: |
| 252 | break; |
| 253 | case 1: { |
| 254 | double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY); |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 255 | if (!precisely_between(top, yIntercept, bottom)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 256 | return fUsed = 0; |
| 257 | } |
| 258 | fT[1][0] = (yIntercept - top) / (bottom - top); |
| 259 | break; |
| 260 | } |
| 261 | case 2: |
| 262 | double a0 = line[0].fY; |
| 263 | double a1 = line[1].fY; |
| 264 | double b0 = flipped ? bottom : top; |
| 265 | double b1 = flipped ? top : bottom; |
| 266 | // FIXME: share common code above |
| 267 | double at0 = (a0 - b0) / (a0 - a1); |
| 268 | double at1 = (a0 - b1) / (a0 - a1); |
| 269 | if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) { |
| 270 | return fUsed = 0; |
| 271 | } |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 272 | fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0); |
| 273 | fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 274 | int bIn = (a0 - a1) * (b0 - b1) < 0; |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 275 | fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1), 1.0), 0.0); |
| 276 | fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1), 1.0), 0.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 277 | bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON; |
| 278 | SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second); |
| 279 | return computePoints(line, 1 + second); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 280 | } |
| 281 | if (flipped) { |
| 282 | // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY |
| 283 | for (int index = 0; index < result; ++index) { |
| 284 | fT[1][index] = 1 - fT[1][index]; |
| 285 | } |
| 286 | } |
| 287 | return computePoints(line, result); |
| 288 | } |
| 289 | |
| 290 | // from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py |
| 291 | // 4 subs, 2 muls, 1 cmp |
| 292 | static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) { |
| 293 | return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX); |
| 294 | } |
| 295 | |
| 296 | // 16 subs, 8 muls, 6 cmps |
| 297 | bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) { |
| 298 | return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1]) |
| 299 | && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]); |
| 300 | } |