gfx/skia/trunk/src/pathops/SkDQuadLineIntersection.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial