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 "SkPathOpsCubic.h" |
| 9 | #include "SkPathOpsLine.h" |
| 10 | |
| 11 | /* |
| 12 | Find the interection of a line and cubic by solving for valid t values. |
| 13 | |
| 14 | Analogous to line-quadratic intersection, solve line-cubic intersection by |
| 15 | representing the cubic as: |
| 16 | x = a(1-t)^3 + 2b(1-t)^2t + c(1-t)t^2 + dt^3 |
| 17 | y = e(1-t)^3 + 2f(1-t)^2t + g(1-t)t^2 + ht^3 |
| 18 | and the line as: |
| 19 | y = i*x + j (if the line is more horizontal) |
| 20 | or: |
| 21 | x = i*y + j (if the line is more vertical) |
| 22 | |
| 23 | Then using Mathematica, solve for the values of t where the cubic intersects the |
| 24 | line: |
| 25 | |
| 26 | (in) Resultant[ |
| 27 | a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - x, |
| 28 | e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - i*x - j, x] |
| 29 | (out) -e + j + |
| 30 | 3 e t - 3 f t - |
| 31 | 3 e t^2 + 6 f t^2 - 3 g t^2 + |
| 32 | e t^3 - 3 f t^3 + 3 g t^3 - h t^3 + |
| 33 | i ( a - |
| 34 | 3 a t + 3 b t + |
| 35 | 3 a t^2 - 6 b t^2 + 3 c t^2 - |
| 36 | a t^3 + 3 b t^3 - 3 c t^3 + d t^3 ) |
| 37 | |
| 38 | if i goes to infinity, we can rewrite the line in terms of x. Mathematica: |
| 39 | |
| 40 | (in) Resultant[ |
| 41 | a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - i*y - j, |
| 42 | e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y] |
| 43 | (out) a - j - |
| 44 | 3 a t + 3 b t + |
| 45 | 3 a t^2 - 6 b t^2 + 3 c t^2 - |
| 46 | a t^3 + 3 b t^3 - 3 c t^3 + d t^3 - |
| 47 | i ( e - |
| 48 | 3 e t + 3 f t + |
| 49 | 3 e t^2 - 6 f t^2 + 3 g t^2 - |
| 50 | e t^3 + 3 f t^3 - 3 g t^3 + h t^3 ) |
| 51 | |
| 52 | Solving this with Mathematica produces an expression with hundreds of terms; |
| 53 | instead, use Numeric Solutions recipe to solve the cubic. |
| 54 | |
| 55 | The near-horizontal case, in terms of: Ax^3 + Bx^2 + Cx + D == 0 |
| 56 | A = (-(-e + 3*f - 3*g + h) + i*(-a + 3*b - 3*c + d) ) |
| 57 | B = 3*(-( e - 2*f + g ) + i*( a - 2*b + c ) ) |
| 58 | C = 3*(-(-e + f ) + i*(-a + b ) ) |
| 59 | D = (-( e ) + i*( a ) + j ) |
| 60 | |
| 61 | The near-vertical case, in terms of: Ax^3 + Bx^2 + Cx + D == 0 |
| 62 | A = ( (-a + 3*b - 3*c + d) - i*(-e + 3*f - 3*g + h) ) |
| 63 | B = 3*( ( a - 2*b + c ) - i*( e - 2*f + g ) ) |
| 64 | C = 3*( (-a + b ) - i*(-e + f ) ) |
| 65 | D = ( ( a ) - i*( e ) - j ) |
| 66 | |
| 67 | For horizontal lines: |
| 68 | (in) Resultant[ |
| 69 | a*(1 - t)^3 + 3*b*(1 - t)^2*t + 3*c*(1 - t)*t^2 + d*t^3 - j, |
| 70 | e*(1 - t)^3 + 3*f*(1 - t)^2*t + 3*g*(1 - t)*t^2 + h*t^3 - y, y] |
| 71 | (out) e - j - |
| 72 | 3 e t + 3 f t + |
| 73 | 3 e t^2 - 6 f t^2 + 3 g t^2 - |
| 74 | e t^3 + 3 f t^3 - 3 g t^3 + h t^3 |
| 75 | */ |
| 76 | |
| 77 | class LineCubicIntersections { |
| 78 | public: |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 79 | enum PinTPoint { |
| 80 | kPointUninitialized, |
| 81 | kPointInitialized |
| 82 | }; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 83 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 84 | LineCubicIntersections(const SkDCubic& c, const SkDLine& l, SkIntersections* i) |
| 85 | : fCubic(c) |
| 86 | , fLine(l) |
| 87 | , fIntersections(i) |
| 88 | , fAllowNear(true) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 89 | i->setMax(3); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 90 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 91 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 92 | void allowNear(bool allow) { |
| 93 | fAllowNear = allow; |
| 94 | } |
| 95 | |
| 96 | // see parallel routine in line quadratic intersections |
| 97 | int intersectRay(double roots[3]) { |
| 98 | double adj = fLine[1].fX - fLine[0].fX; |
| 99 | double opp = fLine[1].fY - fLine[0].fY; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 100 | SkDCubic c; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 101 | for (int n = 0; n < 4; ++n) { |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 102 | c[n].fX = (fCubic[n].fY - fLine[0].fY) * adj - (fCubic[n].fX - fLine[0].fX) * opp; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 103 | } |
| 104 | double A, B, C, D; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 105 | SkDCubic::Coefficients(&c[0].fX, &A, &B, &C, &D); |
| 106 | int count = SkDCubic::RootsValidT(A, B, C, D, roots); |
| 107 | for (int index = 0; index < count; ++index) { |
| 108 | SkDPoint calcPt = c.ptAtT(roots[index]); |
| 109 | if (!approximately_zero(calcPt.fX)) { |
| 110 | for (int n = 0; n < 4; ++n) { |
| 111 | c[n].fY = (fCubic[n].fY - fLine[0].fY) * opp |
| 112 | + (fCubic[n].fX - fLine[0].fX) * adj; |
| 113 | } |
| 114 | double extremeTs[6]; |
| 115 | int extrema = SkDCubic::FindExtrema(c[0].fX, c[1].fX, c[2].fX, c[3].fX, extremeTs); |
| 116 | count = c.searchRoots(extremeTs, extrema, 0, SkDCubic::kXAxis, roots); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | return count; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int intersect() { |
| 124 | addExactEndPoints(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 125 | if (fAllowNear) { |
| 126 | addNearEndPoints(); |
| 127 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 128 | double rootVals[3]; |
| 129 | int roots = intersectRay(rootVals); |
| 130 | for (int index = 0; index < roots; ++index) { |
| 131 | double cubicT = rootVals[index]; |
| 132 | double lineT = findLineT(cubicT); |
| 133 | SkDPoint pt; |
| 134 | if (pinTs(&cubicT, &lineT, &pt, kPointUninitialized)) { |
| 135 | #if ONE_OFF_DEBUG |
| 136 | SkDPoint cPt = fCubic.ptAtT(cubicT); |
| 137 | SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY, |
| 138 | cPt.fX, cPt.fY); |
| 139 | #endif |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 140 | for (int inner = 0; inner < fIntersections->used(); ++inner) { |
| 141 | if (fIntersections->pt(inner) != pt) { |
| 142 | continue; |
| 143 | } |
| 144 | double existingCubicT = (*fIntersections)[0][inner]; |
| 145 | if (cubicT == existingCubicT) { |
| 146 | goto skipInsert; |
| 147 | } |
| 148 | // check if midway on cubic is also same point. If so, discard this |
| 149 | double cubicMidT = (existingCubicT + cubicT) / 2; |
| 150 | SkDPoint cubicMidPt = fCubic.ptAtT(cubicMidT); |
| 151 | if (cubicMidPt.approximatelyEqual(pt)) { |
| 152 | goto skipInsert; |
| 153 | } |
| 154 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 155 | fIntersections->insert(cubicT, lineT, pt); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 156 | skipInsert: |
| 157 | ; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 160 | return fIntersections->used(); |
| 161 | } |
| 162 | |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 163 | static int HorizontalIntersect(const SkDCubic& c, double axisIntercept, double roots[3]) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 164 | double A, B, C, D; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 165 | SkDCubic::Coefficients(&c[0].fY, &A, &B, &C, &D); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 166 | D -= axisIntercept; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 167 | int count = SkDCubic::RootsValidT(A, B, C, D, roots); |
| 168 | for (int index = 0; index < count; ++index) { |
| 169 | SkDPoint calcPt = c.ptAtT(roots[index]); |
| 170 | if (!approximately_equal(calcPt.fY, axisIntercept)) { |
| 171 | double extremeTs[6]; |
| 172 | int extrema = SkDCubic::FindExtrema(c[0].fY, c[1].fY, c[2].fY, c[3].fY, extremeTs); |
| 173 | count = c.searchRoots(extremeTs, extrema, axisIntercept, SkDCubic::kYAxis, roots); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | return count; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) { |
| 181 | addExactHorizontalEndPoints(left, right, axisIntercept); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 182 | if (fAllowNear) { |
| 183 | addNearHorizontalEndPoints(left, right, axisIntercept); |
| 184 | } |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 185 | double roots[3]; |
| 186 | int count = HorizontalIntersect(fCubic, axisIntercept, roots); |
| 187 | for (int index = 0; index < count; ++index) { |
| 188 | double cubicT = roots[index]; |
| 189 | SkDPoint pt; |
| 190 | pt.fX = fCubic.ptAtT(cubicT).fX; |
| 191 | pt.fY = axisIntercept; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 192 | double lineT = (pt.fX - left) / (right - left); |
| 193 | if (pinTs(&cubicT, &lineT, &pt, kPointInitialized)) { |
| 194 | fIntersections->insert(cubicT, lineT, pt); |
| 195 | } |
| 196 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 197 | if (flipped) { |
| 198 | fIntersections->flip(); |
| 199 | } |
| 200 | return fIntersections->used(); |
| 201 | } |
| 202 | |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 203 | static int VerticalIntersect(const SkDCubic& c, double axisIntercept, double roots[3]) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 204 | double A, B, C, D; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 205 | SkDCubic::Coefficients(&c[0].fX, &A, &B, &C, &D); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 206 | D -= axisIntercept; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 207 | int count = SkDCubic::RootsValidT(A, B, C, D, roots); |
| 208 | for (int index = 0; index < count; ++index) { |
| 209 | SkDPoint calcPt = c.ptAtT(roots[index]); |
| 210 | if (!approximately_equal(calcPt.fX, axisIntercept)) { |
| 211 | double extremeTs[6]; |
| 212 | int extrema = SkDCubic::FindExtrema(c[0].fX, c[1].fX, c[2].fX, c[3].fX, extremeTs); |
| 213 | count = c.searchRoots(extremeTs, extrema, axisIntercept, SkDCubic::kXAxis, roots); |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | return count; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) { |
| 221 | addExactVerticalEndPoints(top, bottom, axisIntercept); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 222 | if (fAllowNear) { |
| 223 | addNearVerticalEndPoints(top, bottom, axisIntercept); |
| 224 | } |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 225 | double roots[3]; |
| 226 | int count = VerticalIntersect(fCubic, axisIntercept, roots); |
| 227 | for (int index = 0; index < count; ++index) { |
| 228 | double cubicT = roots[index]; |
| 229 | SkDPoint pt; |
| 230 | pt.fX = axisIntercept; |
| 231 | pt.fY = fCubic.ptAtT(cubicT).fY; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 232 | double lineT = (pt.fY - top) / (bottom - top); |
| 233 | if (pinTs(&cubicT, &lineT, &pt, kPointInitialized)) { |
| 234 | fIntersections->insert(cubicT, lineT, pt); |
| 235 | } |
| 236 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 237 | if (flipped) { |
| 238 | fIntersections->flip(); |
| 239 | } |
| 240 | return fIntersections->used(); |
| 241 | } |
| 242 | |
| 243 | protected: |
| 244 | |
| 245 | void addExactEndPoints() { |
| 246 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 247 | double lineT = fLine.exactPoint(fCubic[cIndex]); |
| 248 | if (lineT < 0) { |
| 249 | continue; |
| 250 | } |
| 251 | double cubicT = (double) (cIndex >> 1); |
| 252 | fIntersections->insert(cubicT, lineT, fCubic[cIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 255 | |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 256 | /* Note that this does not look for endpoints of the line that are near the cubic. |
| 257 | These points are found later when check ends looks for missing points */ |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 258 | void addNearEndPoints() { |
| 259 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 260 | double cubicT = (double) (cIndex >> 1); |
| 261 | if (fIntersections->hasT(cubicT)) { |
| 262 | continue; |
| 263 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 264 | double lineT = fLine.nearPoint(fCubic[cIndex], NULL); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 265 | if (lineT < 0) { |
| 266 | continue; |
| 267 | } |
| 268 | fIntersections->insert(cubicT, lineT, fCubic[cIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 271 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 272 | void addExactHorizontalEndPoints(double left, double right, double y) { |
| 273 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 274 | double lineT = SkDLine::ExactPointH(fCubic[cIndex], left, right, y); |
| 275 | if (lineT < 0) { |
| 276 | continue; |
| 277 | } |
| 278 | double cubicT = (double) (cIndex >> 1); |
| 279 | fIntersections->insert(cubicT, lineT, fCubic[cIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 282 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 283 | void addNearHorizontalEndPoints(double left, double right, double y) { |
| 284 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 285 | double cubicT = (double) (cIndex >> 1); |
| 286 | if (fIntersections->hasT(cubicT)) { |
| 287 | continue; |
| 288 | } |
| 289 | double lineT = SkDLine::NearPointH(fCubic[cIndex], left, right, y); |
| 290 | if (lineT < 0) { |
| 291 | continue; |
| 292 | } |
| 293 | fIntersections->insert(cubicT, lineT, fCubic[cIndex]); |
| 294 | } |
| 295 | // FIXME: see if line end is nearly on cubic |
| 296 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 297 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 298 | void addExactVerticalEndPoints(double top, double bottom, double x) { |
| 299 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 300 | double lineT = SkDLine::ExactPointV(fCubic[cIndex], top, bottom, x); |
| 301 | if (lineT < 0) { |
| 302 | continue; |
| 303 | } |
| 304 | double cubicT = (double) (cIndex >> 1); |
| 305 | fIntersections->insert(cubicT, lineT, fCubic[cIndex]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 306 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 307 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 308 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 309 | void addNearVerticalEndPoints(double top, double bottom, double x) { |
| 310 | for (int cIndex = 0; cIndex < 4; cIndex += 3) { |
| 311 | double cubicT = (double) (cIndex >> 1); |
| 312 | if (fIntersections->hasT(cubicT)) { |
| 313 | continue; |
| 314 | } |
| 315 | double lineT = SkDLine::NearPointV(fCubic[cIndex], top, bottom, x); |
| 316 | if (lineT < 0) { |
| 317 | continue; |
| 318 | } |
| 319 | fIntersections->insert(cubicT, lineT, fCubic[cIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 320 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 321 | // FIXME: see if line end is nearly on cubic |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 322 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 323 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 324 | double findLineT(double t) { |
| 325 | SkDPoint xy = fCubic.ptAtT(t); |
| 326 | double dx = fLine[1].fX - fLine[0].fX; |
| 327 | double dy = fLine[1].fY - fLine[0].fY; |
| 328 | if (fabs(dx) > fabs(dy)) { |
| 329 | return (xy.fX - fLine[0].fX) / dx; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 330 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 331 | return (xy.fY - fLine[0].fY) / dy; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 332 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 333 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 334 | bool pinTs(double* cubicT, double* lineT, SkDPoint* pt, PinTPoint ptSet) { |
| 335 | if (!approximately_one_or_less(*lineT)) { |
| 336 | return false; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 337 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 338 | if (!approximately_zero_or_more(*lineT)) { |
| 339 | return false; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 340 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 341 | double cT = *cubicT = SkPinT(*cubicT); |
| 342 | double lT = *lineT = SkPinT(*lineT); |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 343 | SkDPoint lPt = fLine.ptAtT(lT); |
| 344 | SkDPoint cPt = fCubic.ptAtT(cT); |
| 345 | if (!lPt.moreRoughlyEqual(cPt)) { |
| 346 | return false; |
| 347 | } |
skia.committer@gmail.com | 24f6e29 | 2014-05-01 03:04:54 +0000 | [diff] [blame] | 348 | // FIXME: if points are roughly equal but not approximately equal, need to do |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 349 | // a binary search like quad/quad intersection to find more precise t values |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 350 | if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && cT != 0 && cT != 1)) { |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 351 | *pt = lPt; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 352 | } else if (ptSet == kPointUninitialized) { |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 353 | *pt = cPt; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 354 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 355 | SkPoint gridPt = pt->asSkPoint(); |
| 356 | if (gridPt == fLine[0].asSkPoint()) { |
| 357 | *lineT = 0; |
| 358 | } else if (gridPt == fLine[1].asSkPoint()) { |
| 359 | *lineT = 1; |
| 360 | } |
| 361 | if (gridPt == fCubic[0].asSkPoint() && approximately_equal(*cubicT, 0)) { |
| 362 | *cubicT = 0; |
| 363 | } else if (gridPt == fCubic[3].asSkPoint() && approximately_equal(*cubicT, 1)) { |
| 364 | *cubicT = 1; |
| 365 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 366 | return true; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 367 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 368 | |
| 369 | private: |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 370 | const SkDCubic& fCubic; |
| 371 | const SkDLine& fLine; |
| 372 | SkIntersections* fIntersections; |
| 373 | bool fAllowNear; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | int SkIntersections::horizontal(const SkDCubic& cubic, double left, double right, double y, |
| 377 | bool flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 378 | SkDLine line = {{{ left, y }, { right, y }}}; |
| 379 | LineCubicIntersections c(cubic, line, this); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 380 | return c.horizontalIntersect(y, left, right, flipped); |
| 381 | } |
| 382 | |
| 383 | int SkIntersections::vertical(const SkDCubic& cubic, double top, double bottom, double x, |
| 384 | bool flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 385 | SkDLine line = {{{ x, top }, { x, bottom }}}; |
| 386 | LineCubicIntersections c(cubic, line, this); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 387 | return c.verticalIntersect(x, top, bottom, flipped); |
| 388 | } |
| 389 | |
| 390 | int SkIntersections::intersect(const SkDCubic& cubic, const SkDLine& line) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 391 | LineCubicIntersections c(cubic, line, this); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 392 | c.allowNear(fAllowNear); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 393 | return c.intersect(); |
| 394 | } |
| 395 | |
| 396 | int SkIntersections::intersectRay(const SkDCubic& cubic, const SkDLine& line) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 397 | LineCubicIntersections c(cubic, line, this); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 398 | fUsed = c.intersectRay(fT[0]); |
| 399 | for (int index = 0; index < fUsed; ++index) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 400 | fPt[index] = cubic.ptAtT(fT[0][index]); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 401 | } |
| 402 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 403 | } |