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 | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 78 | static bool checkEndPoint(double x, double y, const SkDLine& l, double* tPtr, int useX) { |
| 79 | if (!between(l[0].fX, x, l[1].fX) || !between(l[0].fY, y, l[1].fY)) { |
| 80 | return false; |
| 81 | } |
| 82 | double xLen = l[1].fX - l[0].fX; |
| 83 | double yLen = l[1].fY - l[0].fY; |
| 84 | if (useX < 0) { |
| 85 | useX = SkTAbs(xLen) > SkTAbs(yLen); |
| 86 | } |
| 87 | // OPTIMIZATION: do between test before divide |
| 88 | double t = useX ? (x - l[0].fX) / xLen : (y - l[0].fY) / yLen; |
| 89 | if (!between(0, t, 1)) { |
| 90 | return false; |
| 91 | } |
| 92 | double opp = useX ? (1 - t) * l[0].fY + t * l[1].fY : (1 - t) * l[0].fX + t * l[1].fX; |
| 93 | if (!AlmostEqualUlps(opp, useX ? y : x)) { |
| 94 | return false; |
| 95 | } |
| 96 | *tPtr = t; |
| 97 | return true; |
| 98 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 99 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 100 | // note that this only works if both lines are neither horizontal nor vertical |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 101 | int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 102 | // see if end points intersect the opposite line |
| 103 | double t; |
| 104 | for (int iA = 0; iA < 2; ++iA) { |
| 105 | if (!checkEndPoint(a[iA].fX, a[iA].fY, b, &t, -1)) { |
| 106 | continue; |
| 107 | } |
| 108 | insert(iA, t, a[iA]); |
| 109 | } |
| 110 | for (int iB = 0; iB < 2; ++iB) { |
| 111 | if (!checkEndPoint(b[iB].fX, b[iB].fY, a, &t, -1)) { |
| 112 | continue; |
| 113 | } |
| 114 | insert(t, iB, b[iB]); |
| 115 | } |
| 116 | if (used() > 0) { |
| 117 | SkASSERT(fUsed <= 2); |
| 118 | return used(); // coincident lines are returned here |
| 119 | } |
| 120 | /* Determine the intersection point of two line segments |
| 121 | Return FALSE if the lines don't intersect |
| 122 | from: http://paulbourke.net/geometry/lineline2d/ */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 123 | double axLen = a[1].fX - a[0].fX; |
| 124 | double ayLen = a[1].fY - a[0].fY; |
| 125 | double bxLen = b[1].fX - b[0].fX; |
| 126 | double byLen = b[1].fY - b[0].fY; |
| 127 | /* Slopes match when denom goes to zero: |
| 128 | axLen / ayLen == bxLen / byLen |
| 129 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 130 | byLen * axLen == ayLen * bxLen |
| 131 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 132 | */ |
| 133 | double denom = byLen * axLen - ayLen * bxLen; |
| 134 | double ab0y = a[0].fY - b[0].fY; |
| 135 | double ab0x = a[0].fX - b[0].fX; |
| 136 | double numerA = ab0y * bxLen - byLen * ab0x; |
| 137 | double numerB = ab0y * axLen - ayLen * ab0x; |
| 138 | bool mayNotOverlap = (numerA < 0 && denom > numerA) || (numerA > 0 && denom < numerA) |
| 139 | || (numerB < 0 && denom > numerB) || (numerB > 0 && denom < numerB); |
| 140 | numerA /= denom; |
| 141 | numerB /= denom; |
| 142 | if ((!approximately_zero(denom) || (!approximately_zero_inverse(numerA) |
| 143 | && !approximately_zero_inverse(numerB))) && !sk_double_isnan(numerA) |
| 144 | && !sk_double_isnan(numerB)) { |
| 145 | if (mayNotOverlap) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 146 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 147 | } |
| 148 | fT[0][0] = numerA; |
| 149 | fT[1][0] = numerB; |
| 150 | fPt[0] = a.xyAtT(numerA); |
| 151 | return computePoints(a, 1); |
| 152 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 153 | return 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int SkIntersections::horizontal(const SkDLine& line, double y) { |
| 157 | double min = line[0].fY; |
| 158 | double max = line[1].fY; |
| 159 | if (min > max) { |
| 160 | SkTSwap(min, max); |
| 161 | } |
| 162 | if (min > y || max < y) { |
| 163 | return fUsed = 0; |
| 164 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 165 | if (AlmostEqualUlps(min, max) && max - min < fabs(line[0].fX - line[1].fX)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 166 | fT[0][0] = 0; |
| 167 | fT[0][1] = 1; |
| 168 | return fUsed = 2; |
| 169 | } |
| 170 | fT[0][0] = (y - line[0].fY) / (line[1].fY - line[0].fY); |
| 171 | return fUsed = 1; |
| 172 | } |
| 173 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 174 | static bool checkEndPointH(const SkDPoint& end, double left, double right, |
| 175 | double y, bool flipped, double* tPtr) { |
| 176 | if (!between(left, end.fX, right) || !AlmostEqualUlps(y, end.fY)) { |
| 177 | return false; |
| 178 | } |
| 179 | double t = (end.fX - left) / (right - left); |
| 180 | SkASSERT(between(0, t, 1)); |
| 181 | *tPtr = flipped ? 1 - t : t; |
| 182 | return true; |
| 183 | } |
| 184 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 185 | int SkIntersections::horizontal(const SkDLine& line, double left, double right, |
| 186 | double y, bool flipped) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 187 | // see if end points intersect the opposite line |
| 188 | double t; |
| 189 | if (checkEndPoint(left, y, line, &t, true)) { |
| 190 | insert(t, flipped, left, y); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 191 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 192 | if (left != right) { |
| 193 | if (checkEndPoint(right, y, line, &t, true)) { |
| 194 | insert(t, !flipped, right, y); |
| 195 | } |
| 196 | for (int index = 0; index < 2; ++index) { |
| 197 | if (!checkEndPointH(line[index], left, right, y, flipped, &t)) { |
| 198 | continue; |
| 199 | } |
| 200 | insert(index, t, line[index]); |
| 201 | } |
| 202 | } |
| 203 | if (used() > 0) { |
| 204 | SkASSERT(fUsed <= 2); |
| 205 | return used(); // coincident lines are returned here |
| 206 | } |
| 207 | int result = horizontal(line, y); |
| 208 | if (!result) { |
| 209 | return 0; |
| 210 | } |
| 211 | SkASSERT(result == 1); |
| 212 | double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX); |
| 213 | if (!precisely_between(left, xIntercept, right)) { |
| 214 | return fUsed = 0; |
| 215 | } |
| 216 | fT[1][0] = (xIntercept - left) / (right - left); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 217 | if (flipped) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 218 | // OPTIMIZATION: ? instead of swapping, pass original line, use [1].fX - [0].fX |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 219 | for (int index = 0; index < result; ++index) { |
| 220 | fT[1][index] = 1 - fT[1][index]; |
| 221 | } |
| 222 | } |
| 223 | return computePoints(line, result); |
| 224 | } |
| 225 | |
| 226 | int SkIntersections::vertical(const SkDLine& line, double x) { |
| 227 | double min = line[0].fX; |
| 228 | double max = line[1].fX; |
| 229 | if (min > max) { |
| 230 | SkTSwap(min, max); |
| 231 | } |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 232 | if (!precisely_between(min, x, max)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 233 | return fUsed = 0; |
| 234 | } |
| 235 | if (AlmostEqualUlps(min, max)) { |
| 236 | fT[0][0] = 0; |
| 237 | fT[0][1] = 1; |
| 238 | return fUsed = 2; |
| 239 | } |
| 240 | fT[0][0] = (x - line[0].fX) / (line[1].fX - line[0].fX); |
| 241 | return fUsed = 1; |
| 242 | } |
| 243 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 244 | static bool checkEndPointV(const SkDPoint& end, double top, double bottom, |
| 245 | double x, bool flipped, double* tPtr) { |
| 246 | if (!between(top, end.fY, bottom) || !AlmostEqualUlps(x, end.fX)) { |
| 247 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 248 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame^] | 249 | double t = (end.fY - top) / (bottom - top); |
| 250 | SkASSERT(between(0, t, 1)); |
| 251 | *tPtr = flipped ? 1 - t : t; |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | int SkIntersections::vertical(const SkDLine& line, double top, double bottom, |
| 256 | double x, bool flipped) { |
| 257 | // see if end points intersect the opposite line |
| 258 | double t; |
| 259 | if (checkEndPoint(x, top, line, &t, false)) { |
| 260 | insert(t, flipped, x, top); |
| 261 | } |
| 262 | if (top != bottom) { |
| 263 | if (checkEndPoint(x, bottom,line, &t, false)) { |
| 264 | insert(t, !flipped, x, bottom); |
| 265 | } |
| 266 | for (int index = 0; index < 2; ++index) { |
| 267 | if (!checkEndPointV(line[index], top, bottom, x, flipped, &t)) { |
| 268 | continue; |
| 269 | } |
| 270 | insert( index, t, line[index]); |
| 271 | } |
| 272 | } |
| 273 | if (used() > 0) { |
| 274 | SkASSERT(fUsed <= 2); |
| 275 | return used(); // coincident lines are returned here |
| 276 | } |
| 277 | int result = vertical(line, x); |
| 278 | if (!result) { |
| 279 | return 0; |
| 280 | } |
| 281 | SkASSERT(result == 1); |
| 282 | double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY); |
| 283 | if (!precisely_between(top, yIntercept, bottom)) { |
| 284 | return fUsed = 0; |
| 285 | } |
| 286 | fT[1][0] = (yIntercept - top) / (bottom - top); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 287 | if (flipped) { |
| 288 | // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY |
| 289 | for (int index = 0; index < result; ++index) { |
| 290 | fT[1][index] = 1 - fT[1][index]; |
| 291 | } |
| 292 | } |
| 293 | return computePoints(line, result); |
| 294 | } |
| 295 | |
| 296 | // from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py |
| 297 | // 4 subs, 2 muls, 1 cmp |
| 298 | static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) { |
| 299 | return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX); |
| 300 | } |
| 301 | |
| 302 | // 16 subs, 8 muls, 6 cmps |
| 303 | bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) { |
| 304 | return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1]) |
| 305 | && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]); |
| 306 | } |