michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkIntersections.h" michael@0: #include "SkPathOpsLine.h" michael@0: #include "SkPathOpsQuad.h" michael@0: michael@0: /* michael@0: Find the interection of a line and quadratic by solving for valid t values. michael@0: michael@0: From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve michael@0: michael@0: "A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three michael@0: control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where michael@0: A, B and C are points and t goes from zero to one. michael@0: michael@0: This will give you two equations: michael@0: michael@0: x = a(1 - t)^2 + b(1 - t)t + ct^2 michael@0: y = d(1 - t)^2 + e(1 - t)t + ft^2 michael@0: michael@0: If you add for instance the line equation (y = kx + m) to that, you'll end up michael@0: with three equations and three unknowns (x, y and t)." michael@0: michael@0: Similar to above, the quadratic is represented as michael@0: x = a(1-t)^2 + 2b(1-t)t + ct^2 michael@0: y = d(1-t)^2 + 2e(1-t)t + ft^2 michael@0: and the line as michael@0: y = g*x + h michael@0: michael@0: Using Mathematica, solve for the values of t where the quadratic intersects the michael@0: line: michael@0: michael@0: (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x, michael@0: d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x] michael@0: (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 + michael@0: g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2) michael@0: (in) Solve[t1 == 0, t] michael@0: (out) { michael@0: {t -> (-2 d + 2 e + 2 a g - 2 b g - michael@0: Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - michael@0: 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / michael@0: (2 (-d + 2 e - f + a g - 2 b g + c g)) michael@0: }, michael@0: {t -> (-2 d + 2 e + 2 a g - 2 b g + michael@0: Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 - michael@0: 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) / michael@0: (2 (-d + 2 e - f + a g - 2 b g + c g)) michael@0: } michael@0: } michael@0: michael@0: Using the results above (when the line tends towards horizontal) michael@0: A = (-(d - 2*e + f) + g*(a - 2*b + c) ) michael@0: B = 2*( (d - e ) - g*(a - b ) ) michael@0: C = (-(d ) + g*(a ) + h ) michael@0: michael@0: If g goes to infinity, we can rewrite the line in terms of x. michael@0: x = g'*y + h' michael@0: michael@0: And solve accordingly in Mathematica: michael@0: michael@0: (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h', michael@0: d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y] michael@0: (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 - michael@0: g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2) michael@0: (in) Solve[t2 == 0, t] michael@0: (out) { michael@0: {t -> (2 a - 2 b - 2 d g' + 2 e g' - michael@0: Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - michael@0: 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) / michael@0: (2 (a - 2 b + c - d g' + 2 e g' - f g')) michael@0: }, michael@0: {t -> (2 a - 2 b - 2 d g' + 2 e g' + michael@0: Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 - michael@0: 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/ michael@0: (2 (a - 2 b + c - d g' + 2 e g' - f g')) michael@0: } michael@0: } michael@0: michael@0: Thus, if the slope of the line tends towards vertical, we use: michael@0: A = ( (a - 2*b + c) - g'*(d - 2*e + f) ) michael@0: B = 2*(-(a - b ) + g'*(d - e ) ) michael@0: C = ( (a ) - g'*(d ) - h' ) michael@0: */ michael@0: michael@0: michael@0: class LineQuadraticIntersections { michael@0: public: michael@0: enum PinTPoint { michael@0: kPointUninitialized, michael@0: kPointInitialized michael@0: }; michael@0: michael@0: LineQuadraticIntersections(const SkDQuad& q, const SkDLine& l, SkIntersections* i) michael@0: : fQuad(q) michael@0: , fLine(l) michael@0: , fIntersections(i) michael@0: , fAllowNear(true) { michael@0: i->setMax(2); michael@0: } michael@0: michael@0: void allowNear(bool allow) { michael@0: fAllowNear = allow; michael@0: } michael@0: michael@0: int intersectRay(double roots[2]) { michael@0: /* michael@0: solve by rotating line+quad so line is horizontal, then finding the roots michael@0: set up matrix to rotate quad to x-axis michael@0: |cos(a) -sin(a)| michael@0: |sin(a) cos(a)| michael@0: note that cos(a) = A(djacent) / Hypoteneuse michael@0: sin(a) = O(pposite) / Hypoteneuse michael@0: since we are computing Ts, we can ignore hypoteneuse, the scale factor: michael@0: | A -O | michael@0: | O A | michael@0: A = line[1].fX - line[0].fX (adjacent side of the right triangle) michael@0: O = line[1].fY - line[0].fY (opposite side of the right triangle) michael@0: for each of the three points (e.g. n = 0 to 2) michael@0: quad[n].fY' = (quad[n].fY - line[0].fY) * A - (quad[n].fX - line[0].fX) * O michael@0: */ michael@0: double adj = fLine[1].fX - fLine[0].fX; michael@0: double opp = fLine[1].fY - fLine[0].fY; michael@0: double r[3]; michael@0: for (int n = 0; n < 3; ++n) { michael@0: r[n] = (fQuad[n].fY - fLine[0].fY) * adj - (fQuad[n].fX - fLine[0].fX) * opp; michael@0: } michael@0: double A = r[2]; michael@0: double B = r[1]; michael@0: double C = r[0]; michael@0: A += C - 2 * B; // A = a - 2*b + c michael@0: B -= C; // B = -(b - c) michael@0: return SkDQuad::RootsValidT(A, 2 * B, C, roots); michael@0: } michael@0: michael@0: int intersect() { michael@0: addExactEndPoints(); michael@0: if (fAllowNear) { michael@0: addNearEndPoints(); michael@0: } michael@0: if (fIntersections->used() == 2) { michael@0: // FIXME : need sharable code that turns spans into coincident if middle point is on michael@0: } else { michael@0: double rootVals[2]; michael@0: int roots = intersectRay(rootVals); michael@0: for (int index = 0; index < roots; ++index) { michael@0: double quadT = rootVals[index]; michael@0: double lineT = findLineT(quadT); michael@0: SkDPoint pt; michael@0: if (pinTs(&quadT, &lineT, &pt, kPointUninitialized)) { michael@0: fIntersections->insert(quadT, lineT, pt); michael@0: } michael@0: } michael@0: } michael@0: return fIntersections->used(); michael@0: } michael@0: michael@0: int horizontalIntersect(double axisIntercept, double roots[2]) { michael@0: double D = fQuad[2].fY; // f michael@0: double E = fQuad[1].fY; // e michael@0: double F = fQuad[0].fY; // d michael@0: D += F - 2 * E; // D = d - 2*e + f michael@0: E -= F; // E = -(d - e) michael@0: F -= axisIntercept; michael@0: return SkDQuad::RootsValidT(D, 2 * E, F, roots); michael@0: } michael@0: michael@0: int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) { michael@0: addExactHorizontalEndPoints(left, right, axisIntercept); michael@0: if (fAllowNear) { michael@0: addNearHorizontalEndPoints(left, right, axisIntercept); michael@0: } michael@0: double rootVals[2]; michael@0: int roots = horizontalIntersect(axisIntercept, rootVals); michael@0: for (int index = 0; index < roots; ++index) { michael@0: double quadT = rootVals[index]; michael@0: SkDPoint pt = fQuad.ptAtT(quadT); michael@0: double lineT = (pt.fX - left) / (right - left); michael@0: if (pinTs(&quadT, &lineT, &pt, kPointInitialized)) { michael@0: fIntersections->insert(quadT, lineT, pt); michael@0: } michael@0: } michael@0: if (flipped) { michael@0: fIntersections->flip(); michael@0: } michael@0: return fIntersections->used(); michael@0: } michael@0: michael@0: int verticalIntersect(double axisIntercept, double roots[2]) { michael@0: double D = fQuad[2].fX; // f michael@0: double E = fQuad[1].fX; // e michael@0: double F = fQuad[0].fX; // d michael@0: D += F - 2 * E; // D = d - 2*e + f michael@0: E -= F; // E = -(d - e) michael@0: F -= axisIntercept; michael@0: return SkDQuad::RootsValidT(D, 2 * E, F, roots); michael@0: } michael@0: michael@0: int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) { michael@0: addExactVerticalEndPoints(top, bottom, axisIntercept); michael@0: if (fAllowNear) { michael@0: addNearVerticalEndPoints(top, bottom, axisIntercept); michael@0: } michael@0: double rootVals[2]; michael@0: int roots = verticalIntersect(axisIntercept, rootVals); michael@0: for (int index = 0; index < roots; ++index) { michael@0: double quadT = rootVals[index]; michael@0: SkDPoint pt = fQuad.ptAtT(quadT); michael@0: double lineT = (pt.fY - top) / (bottom - top); michael@0: if (pinTs(&quadT, &lineT, &pt, kPointInitialized)) { michael@0: fIntersections->insert(quadT, lineT, pt); michael@0: } michael@0: } michael@0: if (flipped) { michael@0: fIntersections->flip(); michael@0: } michael@0: return fIntersections->used(); michael@0: } michael@0: michael@0: protected: michael@0: // add endpoints first to get zero and one t values exactly michael@0: void addExactEndPoints() { michael@0: for (int qIndex = 0; qIndex < 3; qIndex += 2) { michael@0: double lineT = fLine.exactPoint(fQuad[qIndex]); michael@0: if (lineT < 0) { michael@0: continue; michael@0: } michael@0: double quadT = (double) (qIndex >> 1); michael@0: fIntersections->insert(quadT, lineT, fQuad[qIndex]); michael@0: } michael@0: } michael@0: michael@0: void addNearEndPoints() { michael@0: for (int qIndex = 0; qIndex < 3; qIndex += 2) { michael@0: double quadT = (double) (qIndex >> 1); michael@0: if (fIntersections->hasT(quadT)) { michael@0: continue; michael@0: } michael@0: double lineT = fLine.nearPoint(fQuad[qIndex]); michael@0: if (lineT < 0) { michael@0: continue; michael@0: } michael@0: fIntersections->insert(quadT, lineT, fQuad[qIndex]); michael@0: } michael@0: // FIXME: see if line end is nearly on quad michael@0: } michael@0: michael@0: void addExactHorizontalEndPoints(double left, double right, double y) { michael@0: for (int qIndex = 0; qIndex < 3; qIndex += 2) { michael@0: double lineT = SkDLine::ExactPointH(fQuad[qIndex], left, right, y); michael@0: if (lineT < 0) { michael@0: continue; michael@0: } michael@0: double quadT = (double) (qIndex >> 1); michael@0: fIntersections->insert(quadT, lineT, fQuad[qIndex]); michael@0: } michael@0: } michael@0: michael@0: void addNearHorizontalEndPoints(double left, double right, double y) { michael@0: for (int qIndex = 0; qIndex < 3; qIndex += 2) { michael@0: double quadT = (double) (qIndex >> 1); michael@0: if (fIntersections->hasT(quadT)) { michael@0: continue; michael@0: } michael@0: double lineT = SkDLine::NearPointH(fQuad[qIndex], left, right, y); michael@0: if (lineT < 0) { michael@0: continue; michael@0: } michael@0: fIntersections->insert(quadT, lineT, fQuad[qIndex]); michael@0: } michael@0: // FIXME: see if line end is nearly on quad michael@0: } michael@0: michael@0: void addExactVerticalEndPoints(double top, double bottom, double x) { michael@0: for (int qIndex = 0; qIndex < 3; qIndex += 2) { michael@0: double lineT = SkDLine::ExactPointV(fQuad[qIndex], top, bottom, x); michael@0: if (lineT < 0) { michael@0: continue; michael@0: } michael@0: double quadT = (double) (qIndex >> 1); michael@0: fIntersections->insert(quadT, lineT, fQuad[qIndex]); michael@0: } michael@0: } michael@0: michael@0: void addNearVerticalEndPoints(double top, double bottom, double x) { michael@0: for (int qIndex = 0; qIndex < 3; qIndex += 2) { michael@0: double quadT = (double) (qIndex >> 1); michael@0: if (fIntersections->hasT(quadT)) { michael@0: continue; michael@0: } michael@0: double lineT = SkDLine::NearPointV(fQuad[qIndex], top, bottom, x); michael@0: if (lineT < 0) { michael@0: continue; michael@0: } michael@0: fIntersections->insert(quadT, lineT, fQuad[qIndex]); michael@0: } michael@0: // FIXME: see if line end is nearly on quad michael@0: } michael@0: michael@0: double findLineT(double t) { michael@0: SkDPoint xy = fQuad.ptAtT(t); michael@0: double dx = fLine[1].fX - fLine[0].fX; michael@0: double dy = fLine[1].fY - fLine[0].fY; michael@0: if (fabs(dx) > fabs(dy)) { michael@0: return (xy.fX - fLine[0].fX) / dx; michael@0: } michael@0: return (xy.fY - fLine[0].fY) / dy; michael@0: } michael@0: michael@0: bool pinTs(double* quadT, double* lineT, SkDPoint* pt, PinTPoint ptSet) { michael@0: if (!approximately_one_or_less(*lineT)) { michael@0: return false; michael@0: } michael@0: if (!approximately_zero_or_more(*lineT)) { michael@0: return false; michael@0: } michael@0: double qT = *quadT = SkPinT(*quadT); michael@0: double lT = *lineT = SkPinT(*lineT); michael@0: if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && qT != 0 && qT != 1)) { michael@0: *pt = fLine.ptAtT(lT); michael@0: } else if (ptSet == kPointUninitialized) { michael@0: *pt = fQuad.ptAtT(qT); michael@0: } michael@0: SkPoint gridPt = pt->asSkPoint(); michael@0: if (gridPt == fLine[0].asSkPoint()) { michael@0: *lineT = 0; michael@0: } else if (gridPt == fLine[1].asSkPoint()) { michael@0: *lineT = 1; michael@0: } michael@0: if (gridPt == fQuad[0].asSkPoint()) { michael@0: *quadT = 0; michael@0: } else if (gridPt == fQuad[2].asSkPoint()) { michael@0: *quadT = 1; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: private: michael@0: const SkDQuad& fQuad; michael@0: const SkDLine& fLine; michael@0: SkIntersections* fIntersections; michael@0: bool fAllowNear; michael@0: }; michael@0: michael@0: // utility for pairs of coincident quads michael@0: static double horizontalIntersect(const SkDQuad& quad, const SkDPoint& pt) { michael@0: LineQuadraticIntersections q(quad, *(static_cast(0)), michael@0: static_cast(0)); michael@0: double rootVals[2]; michael@0: int roots = q.horizontalIntersect(pt.fY, rootVals); michael@0: for (int index = 0; index < roots; ++index) { michael@0: double t = rootVals[index]; michael@0: SkDPoint qPt = quad.ptAtT(t); michael@0: if (AlmostEqualUlps(qPt.fX, pt.fX)) { michael@0: return t; michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: static double verticalIntersect(const SkDQuad& quad, const SkDPoint& pt) { michael@0: LineQuadraticIntersections q(quad, *(static_cast(0)), michael@0: static_cast(0)); michael@0: double rootVals[2]; michael@0: int roots = q.verticalIntersect(pt.fX, rootVals); michael@0: for (int index = 0; index < roots; ++index) { michael@0: double t = rootVals[index]; michael@0: SkDPoint qPt = quad.ptAtT(t); michael@0: if (AlmostEqualUlps(qPt.fY, pt.fY)) { michael@0: return t; michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: double SkIntersections::Axial(const SkDQuad& q1, const SkDPoint& p, bool vertical) { michael@0: if (vertical) { michael@0: return verticalIntersect(q1, p); michael@0: } michael@0: return horizontalIntersect(q1, p); michael@0: } michael@0: michael@0: int SkIntersections::horizontal(const SkDQuad& quad, double left, double right, double y, michael@0: bool flipped) { michael@0: SkDLine line = {{{ left, y }, { right, y }}}; michael@0: LineQuadraticIntersections q(quad, line, this); michael@0: return q.horizontalIntersect(y, left, right, flipped); michael@0: } michael@0: michael@0: int SkIntersections::vertical(const SkDQuad& quad, double top, double bottom, double x, michael@0: bool flipped) { michael@0: SkDLine line = {{{ x, top }, { x, bottom }}}; michael@0: LineQuadraticIntersections q(quad, line, this); michael@0: return q.verticalIntersect(x, top, bottom, flipped); michael@0: } michael@0: michael@0: int SkIntersections::intersect(const SkDQuad& quad, const SkDLine& line) { michael@0: LineQuadraticIntersections q(quad, line, this); michael@0: q.allowNear(fAllowNear); michael@0: return q.intersect(); michael@0: } michael@0: michael@0: int SkIntersections::intersectRay(const SkDQuad& quad, const SkDLine& line) { michael@0: LineQuadraticIntersections q(quad, line, this); michael@0: fUsed = q.intersectRay(fT[0]); michael@0: for (int index = 0; index < fUsed; ++index) { michael@0: fPt[index] = quad.ptAtT(fT[0][index]); michael@0: } michael@0: return fUsed; michael@0: }