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 | #include "SkPathOpsQuad.h" |
| 10 | |
| 11 | /* |
| 12 | Find the interection of a line and quadratic by solving for valid t values. |
| 13 | |
| 14 | From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve |
| 15 | |
| 16 | "A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three |
| 17 | control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where |
| 18 | A, B and C are points and t goes from zero to one. |
| 19 | |
| 20 | This will give you two equations: |
| 21 | |
| 22 | x = a(1 - t)^2 + b(1 - t)t + ct^2 |
| 23 | y = d(1 - t)^2 + e(1 - t)t + ft^2 |
| 24 | |
| 25 | If you add for instance the line equation (y = kx + m) to that, you'll end up |
| 26 | with three equations and three unknowns (x, y and t)." |
| 27 | |
| 28 | Similar to above, the quadratic is represented as |
| 29 | x = a(1-t)^2 + 2b(1-t)t + ct^2 |
| 30 | y = d(1-t)^2 + 2e(1-t)t + ft^2 |
| 31 | and the line as |
| 32 | y = g*x + h |
| 33 | |
| 34 | Using Mathematica, solve for the values of t where the quadratic intersects the |
| 35 | line: |
| 36 | |
| 37 | (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x, |
| 38 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x] |
| 39 | (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 + |
| 40 | g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2) |
| 41 | (in) Solve[t1 == 0, t] |
| 42 | (out) { |
| 43 | {t -> (-2 d + 2 e + 2 a g - 2 b g - |
| 44 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
| 45 | 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / |
| 46 | (2 (-d + 2 e - f + a g - 2 b g + c g)) |
| 47 | }, |
| 48 | {t -> (-2 d + 2 e + 2 a g - 2 b g + |
| 49 | Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - |
| 50 | 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / |
| 51 | (2 (-d + 2 e - f + a g - 2 b g + c g)) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | Using the results above (when the line tends towards horizontal) |
| 56 | A = (-(d - 2*e + f) + g*(a - 2*b + c) ) |
| 57 | B = 2*( (d - e ) - g*(a - b ) ) |
| 58 | C = (-(d ) + g*(a ) + h ) |
| 59 | |
| 60 | If g goes to infinity, we can rewrite the line in terms of x. |
| 61 | x = g'*y + h' |
| 62 | |
| 63 | And solve accordingly in Mathematica: |
| 64 | |
| 65 | (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h', |
| 66 | d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y] |
| 67 | (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 - |
| 68 | g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2) |
| 69 | (in) Solve[t2 == 0, t] |
| 70 | (out) { |
| 71 | {t -> (2 a - 2 b - 2 d g' + 2 e g' - |
| 72 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
| 73 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) / |
| 74 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 75 | }, |
| 76 | {t -> (2 a - 2 b - 2 d g' + 2 e g' + |
| 77 | Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - |
| 78 | 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/ |
| 79 | (2 (a - 2 b + c - d g' + 2 e g' - f g')) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Thus, if the slope of the line tends towards vertical, we use: |
| 84 | A = ( (a - 2*b + c) - g'*(d - 2*e + f) ) |
| 85 | B = 2*(-(a - b ) + g'*(d - e ) ) |
| 86 | C = ( (a ) - g'*(d ) - h' ) |
| 87 | */ |
| 88 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 89 | class LineQuadraticIntersections { |
| 90 | public: |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 91 | enum PinTPoint { |
| 92 | kPointUninitialized, |
| 93 | kPointInitialized |
| 94 | }; |
| 95 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 96 | LineQuadraticIntersections(const SkDQuad& q, const SkDLine& l, SkIntersections* i) |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 97 | : fQuad(q) |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 98 | , fLine(&l) |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 99 | , fIntersections(i) |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 100 | , fAllowNear(true) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 101 | i->setMax(3); // allow short partial coincidence plus discrete intersection |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 104 | LineQuadraticIntersections(const SkDQuad& q) |
| 105 | : fQuad(q) |
| 106 | SkDEBUGPARAMS(fLine(NULL)) |
| 107 | SkDEBUGPARAMS(fIntersections(NULL)) |
| 108 | SkDEBUGPARAMS(fAllowNear(false)) { |
| 109 | } |
| 110 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 111 | void allowNear(bool allow) { |
| 112 | fAllowNear = allow; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 113 | } |
| 114 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 115 | void checkCoincident() { |
| 116 | int last = fIntersections->used() - 1; |
| 117 | for (int index = 0; index < last; ) { |
| 118 | double quadMidT = ((*fIntersections)[0][index] + (*fIntersections)[0][index + 1]) / 2; |
| 119 | SkDPoint quadMidPt = fQuad.ptAtT(quadMidT); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 120 | double t = fLine->nearPoint(quadMidPt, NULL); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 121 | if (t < 0) { |
| 122 | ++index; |
| 123 | continue; |
| 124 | } |
| 125 | if (fIntersections->isCoincident(index)) { |
| 126 | fIntersections->removeOne(index); |
| 127 | --last; |
| 128 | } else if (fIntersections->isCoincident(index + 1)) { |
| 129 | fIntersections->removeOne(index + 1); |
| 130 | --last; |
| 131 | } else { |
| 132 | fIntersections->setCoincident(index++); |
| 133 | } |
| 134 | fIntersections->setCoincident(index); |
| 135 | } |
| 136 | } |
| 137 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 138 | int intersectRay(double roots[2]) { |
| 139 | /* |
| 140 | solve by rotating line+quad so line is horizontal, then finding the roots |
| 141 | set up matrix to rotate quad to x-axis |
| 142 | |cos(a) -sin(a)| |
| 143 | |sin(a) cos(a)| |
| 144 | note that cos(a) = A(djacent) / Hypoteneuse |
| 145 | sin(a) = O(pposite) / Hypoteneuse |
| 146 | since we are computing Ts, we can ignore hypoteneuse, the scale factor: |
| 147 | | A -O | |
| 148 | | O A | |
| 149 | A = line[1].fX - line[0].fX (adjacent side of the right triangle) |
| 150 | O = line[1].fY - line[0].fY (opposite side of the right triangle) |
| 151 | for each of the three points (e.g. n = 0 to 2) |
| 152 | quad[n].fY' = (quad[n].fY - line[0].fY) * A - (quad[n].fX - line[0].fX) * O |
| 153 | */ |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 154 | double adj = (*fLine)[1].fX - (*fLine)[0].fX; |
| 155 | double opp = (*fLine)[1].fY - (*fLine)[0].fY; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 156 | double r[3]; |
| 157 | for (int n = 0; n < 3; ++n) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 158 | r[n] = (fQuad[n].fY - (*fLine)[0].fY) * adj - (fQuad[n].fX - (*fLine)[0].fX) * opp; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 159 | } |
| 160 | double A = r[2]; |
| 161 | double B = r[1]; |
| 162 | double C = r[0]; |
| 163 | A += C - 2 * B; // A = a - 2*b + c |
| 164 | B -= C; // B = -(b - c) |
| 165 | return SkDQuad::RootsValidT(A, 2 * B, C, roots); |
| 166 | } |
| 167 | |
| 168 | int intersect() { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 169 | addExactEndPoints(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 170 | if (fAllowNear) { |
| 171 | addNearEndPoints(); |
| 172 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 173 | double rootVals[2]; |
| 174 | int roots = intersectRay(rootVals); |
| 175 | for (int index = 0; index < roots; ++index) { |
| 176 | double quadT = rootVals[index]; |
| 177 | double lineT = findLineT(quadT); |
| 178 | SkDPoint pt; |
| 179 | if (pinTs(&quadT, &lineT, &pt, kPointUninitialized) && uniqueAnswer(quadT, pt)) { |
| 180 | fIntersections->insert(quadT, lineT, pt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 183 | checkCoincident(); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 184 | return fIntersections->used(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | int horizontalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 188 | double D = fQuad[2].fY; // f |
| 189 | double E = fQuad[1].fY; // e |
| 190 | double F = fQuad[0].fY; // d |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 191 | D += F - 2 * E; // D = d - 2*e + f |
| 192 | E -= F; // E = -(d - e) |
| 193 | F -= axisIntercept; |
| 194 | return SkDQuad::RootsValidT(D, 2 * E, F, roots); |
| 195 | } |
| 196 | |
| 197 | int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 198 | addExactHorizontalEndPoints(left, right, axisIntercept); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 199 | if (fAllowNear) { |
| 200 | addNearHorizontalEndPoints(left, right, axisIntercept); |
| 201 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 202 | double rootVals[2]; |
| 203 | int roots = horizontalIntersect(axisIntercept, rootVals); |
| 204 | for (int index = 0; index < roots; ++index) { |
| 205 | double quadT = rootVals[index]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 206 | SkDPoint pt = fQuad.ptAtT(quadT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 207 | double lineT = (pt.fX - left) / (right - left); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 208 | if (pinTs(&quadT, &lineT, &pt, kPointInitialized) && uniqueAnswer(quadT, pt)) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 209 | fIntersections->insert(quadT, lineT, pt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | if (flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 213 | fIntersections->flip(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 214 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 215 | checkCoincident(); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 216 | return fIntersections->used(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 217 | } |
| 218 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 219 | bool uniqueAnswer(double quadT, const SkDPoint& pt) { |
| 220 | for (int inner = 0; inner < fIntersections->used(); ++inner) { |
| 221 | if (fIntersections->pt(inner) != pt) { |
| 222 | continue; |
| 223 | } |
| 224 | double existingQuadT = (*fIntersections)[0][inner]; |
| 225 | if (quadT == existingQuadT) { |
| 226 | return false; |
| 227 | } |
| 228 | // check if midway on quad is also same point. If so, discard this |
| 229 | double quadMidT = (existingQuadT + quadT) / 2; |
| 230 | SkDPoint quadMidPt = fQuad.ptAtT(quadMidT); |
| 231 | if (quadMidPt.approximatelyEqual(pt)) { |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | #if ONE_OFF_DEBUG |
| 236 | SkDPoint qPt = fQuad.ptAtT(quadT); |
| 237 | SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY, |
| 238 | qPt.fX, qPt.fY); |
| 239 | #endif |
| 240 | return true; |
| 241 | } |
| 242 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 243 | int verticalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 244 | double D = fQuad[2].fX; // f |
| 245 | double E = fQuad[1].fX; // e |
| 246 | double F = fQuad[0].fX; // d |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 247 | D += F - 2 * E; // D = d - 2*e + f |
| 248 | E -= F; // E = -(d - e) |
| 249 | F -= axisIntercept; |
| 250 | return SkDQuad::RootsValidT(D, 2 * E, F, roots); |
| 251 | } |
| 252 | |
| 253 | int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 254 | addExactVerticalEndPoints(top, bottom, axisIntercept); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 255 | if (fAllowNear) { |
| 256 | addNearVerticalEndPoints(top, bottom, axisIntercept); |
| 257 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 258 | double rootVals[2]; |
| 259 | int roots = verticalIntersect(axisIntercept, rootVals); |
| 260 | for (int index = 0; index < roots; ++index) { |
| 261 | double quadT = rootVals[index]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 262 | SkDPoint pt = fQuad.ptAtT(quadT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 263 | double lineT = (pt.fY - top) / (bottom - top); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 264 | if (pinTs(&quadT, &lineT, &pt, kPointInitialized) && uniqueAnswer(quadT, pt)) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 265 | fIntersections->insert(quadT, lineT, pt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | if (flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 269 | fIntersections->flip(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 270 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 271 | checkCoincident(); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 272 | return fIntersections->used(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | protected: |
| 276 | // add endpoints first to get zero and one t values exactly |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 277 | void addExactEndPoints() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 278 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 279 | double lineT = fLine->exactPoint(fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 280 | if (lineT < 0) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 281 | continue; |
| 282 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 283 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 284 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 288 | void addNearEndPoints() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 289 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 290 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 291 | if (fIntersections->hasT(quadT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 292 | continue; |
| 293 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 294 | double lineT = fLine->nearPoint(fQuad[qIndex], NULL); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 295 | if (lineT < 0) { |
| 296 | continue; |
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 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 299 | } |
| 300 | // FIXME: see if line end is nearly on quad |
| 301 | } |
| 302 | |
| 303 | void addExactHorizontalEndPoints(double left, double right, double y) { |
| 304 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 305 | double lineT = SkDLine::ExactPointH(fQuad[qIndex], left, right, y); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 306 | if (lineT < 0) { |
| 307 | continue; |
| 308 | } |
| 309 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 310 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 314 | void addNearHorizontalEndPoints(double left, double right, double y) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 315 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 316 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 317 | if (fIntersections->hasT(quadT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 318 | continue; |
| 319 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 320 | double lineT = SkDLine::NearPointH(fQuad[qIndex], left, right, y); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 321 | if (lineT < 0) { |
| 322 | continue; |
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 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 325 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 326 | // FIXME: see if line end is nearly on quad |
| 327 | } |
| 328 | |
| 329 | void addExactVerticalEndPoints(double top, double bottom, double x) { |
| 330 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 331 | double lineT = SkDLine::ExactPointV(fQuad[qIndex], top, bottom, x); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 332 | if (lineT < 0) { |
| 333 | continue; |
| 334 | } |
| 335 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 336 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | void addNearVerticalEndPoints(double top, double bottom, double x) { |
| 341 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 342 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 343 | if (fIntersections->hasT(quadT)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 344 | continue; |
| 345 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 346 | double lineT = SkDLine::NearPointV(fQuad[qIndex], top, bottom, x); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 347 | if (lineT < 0) { |
| 348 | continue; |
| 349 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 350 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 351 | } |
| 352 | // FIXME: see if line end is nearly on quad |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | double findLineT(double t) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 356 | SkDPoint xy = fQuad.ptAtT(t); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 357 | double dx = (*fLine)[1].fX - (*fLine)[0].fX; |
| 358 | double dy = (*fLine)[1].fY - (*fLine)[0].fY; |
caryclark@google.com | 28d219c | 2013-11-25 13:39:12 +0000 | [diff] [blame] | 359 | if (fabs(dx) > fabs(dy)) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 360 | return (xy.fX - (*fLine)[0].fX) / dx; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 361 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 362 | return (xy.fY - (*fLine)[0].fY) / dy; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 363 | } |
| 364 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 365 | bool pinTs(double* quadT, double* lineT, SkDPoint* pt, PinTPoint ptSet) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 366 | if (!approximately_one_or_less_double(*lineT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 367 | return false; |
| 368 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 369 | if (!approximately_zero_or_more_double(*lineT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 370 | return false; |
| 371 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 372 | double qT = *quadT = SkPinT(*quadT); |
| 373 | double lT = *lineT = SkPinT(*lineT); |
| 374 | if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && qT != 0 && qT != 1)) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 375 | *pt = (*fLine).ptAtT(lT); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 376 | } else if (ptSet == kPointUninitialized) { |
| 377 | *pt = fQuad.ptAtT(qT); |
| 378 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 379 | SkPoint gridPt = pt->asSkPoint(); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 380 | if (SkDPoint::ApproximatelyEqual(gridPt, (*fLine)[0].asSkPoint())) { |
| 381 | *pt = (*fLine)[0]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 382 | *lineT = 0; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 383 | } else if (SkDPoint::ApproximatelyEqual(gridPt, (*fLine)[1].asSkPoint())) { |
| 384 | *pt = (*fLine)[1]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 385 | *lineT = 1; |
| 386 | } |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 387 | if (fIntersections->used() > 0 && approximately_equal((*fIntersections)[1][0], *lineT)) { |
| 388 | return false; |
| 389 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 390 | if (gridPt == fQuad[0].asSkPoint()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 391 | *pt = fQuad[0]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 392 | *quadT = 0; |
| 393 | } else if (gridPt == fQuad[2].asSkPoint()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 394 | *pt = fQuad[2]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 395 | *quadT = 1; |
| 396 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
| 400 | private: |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 401 | const SkDQuad& fQuad; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 402 | const SkDLine* fLine; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 403 | SkIntersections* fIntersections; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 404 | bool fAllowNear; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 405 | }; |
| 406 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 407 | int SkIntersections::horizontal(const SkDQuad& quad, double left, double right, double y, |
| 408 | bool flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 409 | SkDLine line = {{{ left, y }, { right, y }}}; |
| 410 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 411 | return q.horizontalIntersect(y, left, right, flipped); |
| 412 | } |
| 413 | |
| 414 | int SkIntersections::vertical(const SkDQuad& quad, double top, double bottom, double x, |
| 415 | bool flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 416 | SkDLine line = {{{ x, top }, { x, bottom }}}; |
| 417 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 418 | return q.verticalIntersect(x, top, bottom, flipped); |
| 419 | } |
| 420 | |
| 421 | int SkIntersections::intersect(const SkDQuad& quad, const SkDLine& line) { |
| 422 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 423 | q.allowNear(fAllowNear); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 424 | return q.intersect(); |
| 425 | } |
| 426 | |
| 427 | int SkIntersections::intersectRay(const SkDQuad& quad, const SkDLine& line) { |
| 428 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 429 | fUsed = q.intersectRay(fT[0]); |
| 430 | for (int index = 0; index < fUsed; ++index) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 431 | fPt[index] = quad.ptAtT(fT[0][index]); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 432 | } |
| 433 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 434 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 435 | |
| 436 | int SkIntersections::HorizontalIntercept(const SkDQuad& quad, SkScalar y, double* roots) { |
| 437 | LineQuadraticIntersections q(quad); |
| 438 | return q.horizontalIntersect(y, roots); |
| 439 | } |
| 440 | |
| 441 | int SkIntersections::VerticalIntercept(const SkDQuad& quad, SkScalar x, double* roots) { |
| 442 | LineQuadraticIntersections q(quad); |
| 443 | return q.verticalIntersect(x, roots); |
| 444 | } |