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) |
| 98 | , fLine(l) |
| 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 | |
| 104 | void allowNear(bool allow) { |
| 105 | fAllowNear = allow; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | int intersectRay(double roots[2]) { |
| 109 | /* |
| 110 | solve by rotating line+quad so line is horizontal, then finding the roots |
| 111 | set up matrix to rotate quad to x-axis |
| 112 | |cos(a) -sin(a)| |
| 113 | |sin(a) cos(a)| |
| 114 | note that cos(a) = A(djacent) / Hypoteneuse |
| 115 | sin(a) = O(pposite) / Hypoteneuse |
| 116 | since we are computing Ts, we can ignore hypoteneuse, the scale factor: |
| 117 | | A -O | |
| 118 | | O A | |
| 119 | A = line[1].fX - line[0].fX (adjacent side of the right triangle) |
| 120 | O = line[1].fY - line[0].fY (opposite side of the right triangle) |
| 121 | for each of the three points (e.g. n = 0 to 2) |
| 122 | quad[n].fY' = (quad[n].fY - line[0].fY) * A - (quad[n].fX - line[0].fX) * O |
| 123 | */ |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 124 | double adj = fLine[1].fX - fLine[0].fX; |
| 125 | double opp = fLine[1].fY - fLine[0].fY; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 126 | double r[3]; |
| 127 | for (int n = 0; n < 3; ++n) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 128 | 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] | 129 | } |
| 130 | double A = r[2]; |
| 131 | double B = r[1]; |
| 132 | double C = r[0]; |
| 133 | A += C - 2 * B; // A = a - 2*b + c |
| 134 | B -= C; // B = -(b - c) |
| 135 | return SkDQuad::RootsValidT(A, 2 * B, C, roots); |
| 136 | } |
| 137 | |
| 138 | int intersect() { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 139 | addExactEndPoints(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 140 | if (fAllowNear) { |
| 141 | addNearEndPoints(); |
| 142 | } |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 143 | if (fIntersections->used() == 2) { |
| 144 | // FIXME : need sharable code that turns spans into coincident if middle point is on |
| 145 | } else { |
| 146 | double rootVals[2]; |
| 147 | int roots = intersectRay(rootVals); |
| 148 | for (int index = 0; index < roots; ++index) { |
| 149 | double quadT = rootVals[index]; |
| 150 | double lineT = findLineT(quadT); |
| 151 | SkDPoint pt; |
| 152 | if (pinTs(&quadT, &lineT, &pt, kPointUninitialized)) { |
| 153 | fIntersections->insert(quadT, lineT, pt); |
| 154 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 157 | return fIntersections->used(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int horizontalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 161 | double D = fQuad[2].fY; // f |
| 162 | double E = fQuad[1].fY; // e |
| 163 | double F = fQuad[0].fY; // d |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 164 | D += F - 2 * E; // D = d - 2*e + f |
| 165 | E -= F; // E = -(d - e) |
| 166 | F -= axisIntercept; |
| 167 | return SkDQuad::RootsValidT(D, 2 * E, F, roots); |
| 168 | } |
| 169 | |
| 170 | int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 171 | addExactHorizontalEndPoints(left, right, axisIntercept); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 172 | if (fAllowNear) { |
| 173 | addNearHorizontalEndPoints(left, right, axisIntercept); |
| 174 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 175 | double rootVals[2]; |
| 176 | int roots = horizontalIntersect(axisIntercept, rootVals); |
| 177 | for (int index = 0; index < roots; ++index) { |
| 178 | double quadT = rootVals[index]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 179 | SkDPoint pt = fQuad.ptAtT(quadT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 180 | double lineT = (pt.fX - left) / (right - left); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 181 | if (pinTs(&quadT, &lineT, &pt, kPointInitialized)) { |
| 182 | fIntersections->insert(quadT, lineT, pt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | if (flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 186 | fIntersections->flip(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 187 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 188 | return fIntersections->used(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int verticalIntersect(double axisIntercept, double roots[2]) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 192 | double D = fQuad[2].fX; // f |
| 193 | double E = fQuad[1].fX; // e |
| 194 | double F = fQuad[0].fX; // d |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 195 | D += F - 2 * E; // D = d - 2*e + f |
| 196 | E -= F; // E = -(d - e) |
| 197 | F -= axisIntercept; |
| 198 | return SkDQuad::RootsValidT(D, 2 * E, F, roots); |
| 199 | } |
| 200 | |
| 201 | int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 202 | addExactVerticalEndPoints(top, bottom, axisIntercept); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 203 | if (fAllowNear) { |
| 204 | addNearVerticalEndPoints(top, bottom, axisIntercept); |
| 205 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 206 | double rootVals[2]; |
| 207 | int roots = verticalIntersect(axisIntercept, rootVals); |
| 208 | for (int index = 0; index < roots; ++index) { |
| 209 | double quadT = rootVals[index]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 210 | SkDPoint pt = fQuad.ptAtT(quadT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 211 | double lineT = (pt.fY - top) / (bottom - top); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 212 | if (pinTs(&quadT, &lineT, &pt, kPointInitialized)) { |
| 213 | fIntersections->insert(quadT, lineT, pt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | if (flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 217 | fIntersections->flip(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 218 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 219 | return fIntersections->used(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | protected: |
| 223 | // add endpoints first to get zero and one t values exactly |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 224 | void addExactEndPoints() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 225 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 226 | double lineT = fLine.exactPoint(fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 227 | if (lineT < 0) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 228 | continue; |
| 229 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 230 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 231 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 235 | void addNearEndPoints() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 236 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 237 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 238 | if (fIntersections->hasT(quadT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 239 | continue; |
| 240 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 241 | double lineT = fLine.nearPoint(fQuad[qIndex], NULL); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 242 | if (lineT < 0) { |
| 243 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 244 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 245 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 246 | } |
| 247 | // FIXME: see if line end is nearly on quad |
| 248 | } |
| 249 | |
| 250 | void addExactHorizontalEndPoints(double left, double right, double y) { |
| 251 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 252 | double lineT = SkDLine::ExactPointH(fQuad[qIndex], left, right, y); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 253 | if (lineT < 0) { |
| 254 | continue; |
| 255 | } |
| 256 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 257 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 261 | void addNearHorizontalEndPoints(double left, double right, double y) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 262 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 263 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 264 | if (fIntersections->hasT(quadT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 265 | continue; |
| 266 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 267 | double lineT = SkDLine::NearPointH(fQuad[qIndex], left, right, y); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 268 | if (lineT < 0) { |
| 269 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 270 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 271 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 272 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 273 | // FIXME: see if line end is nearly on quad |
| 274 | } |
| 275 | |
| 276 | void addExactVerticalEndPoints(double top, double bottom, double x) { |
| 277 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 278 | double lineT = SkDLine::ExactPointV(fQuad[qIndex], top, bottom, x); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 279 | if (lineT < 0) { |
| 280 | continue; |
| 281 | } |
| 282 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 283 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | void addNearVerticalEndPoints(double top, double bottom, double x) { |
| 288 | for (int qIndex = 0; qIndex < 3; qIndex += 2) { |
| 289 | double quadT = (double) (qIndex >> 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 290 | if (fIntersections->hasT(quadT)) { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 291 | continue; |
| 292 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 293 | double lineT = SkDLine::NearPointV(fQuad[qIndex], top, bottom, x); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 294 | if (lineT < 0) { |
| 295 | continue; |
| 296 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 297 | fIntersections->insert(quadT, lineT, fQuad[qIndex]); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 298 | } |
| 299 | // FIXME: see if line end is nearly on quad |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | double findLineT(double t) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 303 | SkDPoint xy = fQuad.ptAtT(t); |
| 304 | double dx = fLine[1].fX - fLine[0].fX; |
| 305 | double dy = fLine[1].fY - fLine[0].fY; |
caryclark@google.com | 28d219c | 2013-11-25 13:39:12 +0000 | [diff] [blame] | 306 | if (fabs(dx) > fabs(dy)) { |
| 307 | return (xy.fX - fLine[0].fX) / dx; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 308 | } |
caryclark@google.com | 28d219c | 2013-11-25 13:39:12 +0000 | [diff] [blame] | 309 | return (xy.fY - fLine[0].fY) / dy; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 310 | } |
| 311 | |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 312 | bool pinTs(double* quadT, double* lineT, SkDPoint* pt, PinTPoint ptSet) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 313 | if (!approximately_one_or_less_double(*lineT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 314 | return false; |
| 315 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 316 | if (!approximately_zero_or_more_double(*lineT)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 319 | double qT = *quadT = SkPinT(*quadT); |
| 320 | double lT = *lineT = SkPinT(*lineT); |
| 321 | if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && qT != 0 && qT != 1)) { |
| 322 | *pt = fLine.ptAtT(lT); |
| 323 | } else if (ptSet == kPointUninitialized) { |
| 324 | *pt = fQuad.ptAtT(qT); |
| 325 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 326 | SkPoint gridPt = pt->asSkPoint(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 327 | if (SkDPoint::ApproximatelyEqual(gridPt, fLine[0].asSkPoint())) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 328 | *pt = fLine[0]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 329 | *lineT = 0; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 330 | } else if (SkDPoint::ApproximatelyEqual(gridPt, fLine[1].asSkPoint())) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 331 | *pt = fLine[1]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 332 | *lineT = 1; |
| 333 | } |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 334 | if (fIntersections->used() > 0 && approximately_equal((*fIntersections)[1][0], *lineT)) { |
| 335 | return false; |
| 336 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 337 | if (gridPt == fQuad[0].asSkPoint()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 338 | *pt = fQuad[0]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 339 | *quadT = 0; |
| 340 | } else if (gridPt == fQuad[2].asSkPoint()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 341 | *pt = fQuad[2]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 342 | *quadT = 1; |
| 343 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | |
| 347 | private: |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 348 | const SkDQuad& fQuad; |
| 349 | const SkDLine& fLine; |
| 350 | SkIntersections* fIntersections; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 351 | bool fAllowNear; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 352 | }; |
| 353 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 354 | int SkIntersections::horizontal(const SkDQuad& quad, double left, double right, double y, |
| 355 | bool flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 356 | SkDLine line = {{{ left, y }, { right, y }}}; |
| 357 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 358 | return q.horizontalIntersect(y, left, right, flipped); |
| 359 | } |
| 360 | |
| 361 | int SkIntersections::vertical(const SkDQuad& quad, double top, double bottom, double x, |
| 362 | bool flipped) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 363 | SkDLine line = {{{ x, top }, { x, bottom }}}; |
| 364 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 365 | return q.verticalIntersect(x, top, bottom, flipped); |
| 366 | } |
| 367 | |
| 368 | int SkIntersections::intersect(const SkDQuad& quad, const SkDLine& line) { |
| 369 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 370 | q.allowNear(fAllowNear); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 371 | return q.intersect(); |
| 372 | } |
| 373 | |
| 374 | int SkIntersections::intersectRay(const SkDQuad& quad, const SkDLine& line) { |
| 375 | LineQuadraticIntersections q(quad, line, this); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 376 | fUsed = q.intersectRay(fT[0]); |
| 377 | for (int index = 0; index < fUsed; ++index) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 378 | fPt[index] = quad.ptAtT(fT[0][index]); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 379 | } |
| 380 | return fUsed; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 381 | } |