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 "SkLineParameters.h" michael@0: #include "SkPathOpsCubic.h" michael@0: #include "SkPathOpsQuad.h" michael@0: #include "SkPathOpsTriangle.h" michael@0: michael@0: // from http://blog.gludion.com/2009/08/distance-to-quadratic-bezier-curve.html michael@0: // (currently only used by testing) michael@0: double SkDQuad::nearestT(const SkDPoint& pt) const { michael@0: SkDVector pos = fPts[0] - pt; michael@0: // search points P of bezier curve with PM.(dP / dt) = 0 michael@0: // a calculus leads to a 3d degree equation : michael@0: SkDVector A = fPts[1] - fPts[0]; michael@0: SkDVector B = fPts[2] - fPts[1]; michael@0: B -= A; michael@0: double a = B.dot(B); michael@0: double b = 3 * A.dot(B); michael@0: double c = 2 * A.dot(A) + pos.dot(B); michael@0: double d = pos.dot(A); michael@0: double ts[3]; michael@0: int roots = SkDCubic::RootsValidT(a, b, c, d, ts); michael@0: double d0 = pt.distanceSquared(fPts[0]); michael@0: double d2 = pt.distanceSquared(fPts[2]); michael@0: double distMin = SkTMin(d0, d2); michael@0: int bestIndex = -1; michael@0: for (int index = 0; index < roots; ++index) { michael@0: SkDPoint onQuad = ptAtT(ts[index]); michael@0: double dist = pt.distanceSquared(onQuad); michael@0: if (distMin > dist) { michael@0: distMin = dist; michael@0: bestIndex = index; michael@0: } michael@0: } michael@0: if (bestIndex >= 0) { michael@0: return ts[bestIndex]; michael@0: } michael@0: return d0 < d2 ? 0 : 1; michael@0: } michael@0: michael@0: bool SkDQuad::pointInHull(const SkDPoint& pt) const { michael@0: return ((const SkDTriangle&) fPts).contains(pt); michael@0: } michael@0: michael@0: SkDPoint SkDQuad::top(double startT, double endT) const { michael@0: SkDQuad sub = subDivide(startT, endT); michael@0: SkDPoint topPt = sub[0]; michael@0: if (topPt.fY > sub[2].fY || (topPt.fY == sub[2].fY && topPt.fX > sub[2].fX)) { michael@0: topPt = sub[2]; michael@0: } michael@0: if (!between(sub[0].fY, sub[1].fY, sub[2].fY)) { michael@0: double extremeT; michael@0: if (FindExtrema(sub[0].fY, sub[1].fY, sub[2].fY, &extremeT)) { michael@0: extremeT = startT + (endT - startT) * extremeT; michael@0: SkDPoint test = ptAtT(extremeT); michael@0: if (topPt.fY > test.fY || (topPt.fY == test.fY && topPt.fX > test.fX)) { michael@0: topPt = test; michael@0: } michael@0: } michael@0: } michael@0: return topPt; michael@0: } michael@0: michael@0: int SkDQuad::AddValidTs(double s[], int realRoots, double* t) { michael@0: int foundRoots = 0; michael@0: for (int index = 0; index < realRoots; ++index) { michael@0: double tValue = s[index]; michael@0: if (approximately_zero_or_more(tValue) && approximately_one_or_less(tValue)) { michael@0: if (approximately_less_than_zero(tValue)) { michael@0: tValue = 0; michael@0: } else if (approximately_greater_than_one(tValue)) { michael@0: tValue = 1; michael@0: } michael@0: for (int idx2 = 0; idx2 < foundRoots; ++idx2) { michael@0: if (approximately_equal(t[idx2], tValue)) { michael@0: goto nextRoot; michael@0: } michael@0: } michael@0: t[foundRoots++] = tValue; michael@0: } michael@0: nextRoot: michael@0: {} michael@0: } michael@0: return foundRoots; michael@0: } michael@0: michael@0: // note: caller expects multiple results to be sorted smaller first michael@0: // note: http://en.wikipedia.org/wiki/Loss_of_significance has an interesting michael@0: // analysis of the quadratic equation, suggesting why the following looks at michael@0: // the sign of B -- and further suggesting that the greatest loss of precision michael@0: // is in b squared less two a c michael@0: int SkDQuad::RootsValidT(double A, double B, double C, double t[2]) { michael@0: double s[2]; michael@0: int realRoots = RootsReal(A, B, C, s); michael@0: int foundRoots = AddValidTs(s, realRoots, t); michael@0: return foundRoots; michael@0: } michael@0: michael@0: /* michael@0: Numeric Solutions (5.6) suggests to solve the quadratic by computing michael@0: Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C)) michael@0: and using the roots michael@0: t1 = Q / A michael@0: t2 = C / Q michael@0: */ michael@0: // this does not discard real roots <= 0 or >= 1 michael@0: int SkDQuad::RootsReal(const double A, const double B, const double C, double s[2]) { michael@0: const double p = B / (2 * A); michael@0: const double q = C / A; michael@0: if (approximately_zero(A) && (approximately_zero_inverse(p) || approximately_zero_inverse(q))) { michael@0: if (approximately_zero(B)) { michael@0: s[0] = 0; michael@0: return C == 0; michael@0: } michael@0: s[0] = -C / B; michael@0: return 1; michael@0: } michael@0: /* normal form: x^2 + px + q = 0 */ michael@0: const double p2 = p * p; michael@0: if (!AlmostDequalUlps(p2, q) && p2 < q) { michael@0: return 0; michael@0: } michael@0: double sqrt_D = 0; michael@0: if (p2 > q) { michael@0: sqrt_D = sqrt(p2 - q); michael@0: } michael@0: s[0] = sqrt_D - p; michael@0: s[1] = -sqrt_D - p; michael@0: return 1 + !AlmostDequalUlps(s[0], s[1]); michael@0: } michael@0: michael@0: bool SkDQuad::isLinear(int startIndex, int endIndex) const { michael@0: SkLineParameters lineParameters; michael@0: lineParameters.quadEndPoints(*this, startIndex, endIndex); michael@0: // FIXME: maybe it's possible to avoid this and compare non-normalized michael@0: lineParameters.normalize(); michael@0: double distance = lineParameters.controlPtDistance(*this); michael@0: return approximately_zero(distance); michael@0: } michael@0: michael@0: SkDCubic SkDQuad::toCubic() const { michael@0: SkDCubic cubic; michael@0: cubic[0] = fPts[0]; michael@0: cubic[2] = fPts[1]; michael@0: cubic[3] = fPts[2]; michael@0: cubic[1].fX = (cubic[0].fX + cubic[2].fX * 2) / 3; michael@0: cubic[1].fY = (cubic[0].fY + cubic[2].fY * 2) / 3; michael@0: cubic[2].fX = (cubic[3].fX + cubic[2].fX * 2) / 3; michael@0: cubic[2].fY = (cubic[3].fY + cubic[2].fY * 2) / 3; michael@0: return cubic; michael@0: } michael@0: michael@0: SkDVector SkDQuad::dxdyAtT(double t) const { michael@0: double a = t - 1; michael@0: double b = 1 - 2 * t; michael@0: double c = t; michael@0: SkDVector result = { a * fPts[0].fX + b * fPts[1].fX + c * fPts[2].fX, michael@0: a * fPts[0].fY + b * fPts[1].fY + c * fPts[2].fY }; michael@0: return result; michael@0: } michael@0: michael@0: // OPTIMIZE: assert if caller passes in t == 0 / t == 1 ? michael@0: SkDPoint SkDQuad::ptAtT(double t) const { michael@0: if (0 == t) { michael@0: return fPts[0]; michael@0: } michael@0: if (1 == t) { michael@0: return fPts[2]; michael@0: } michael@0: double one_t = 1 - t; michael@0: double a = one_t * one_t; michael@0: double b = 2 * one_t * t; michael@0: double c = t * t; michael@0: SkDPoint result = { a * fPts[0].fX + b * fPts[1].fX + c * fPts[2].fX, michael@0: a * fPts[0].fY + b * fPts[1].fY + c * fPts[2].fY }; michael@0: return result; michael@0: } michael@0: michael@0: /* michael@0: Given a quadratic q, t1, and t2, find a small quadratic segment. michael@0: michael@0: The new quadratic is defined by A, B, and C, where michael@0: A = c[0]*(1 - t1)*(1 - t1) + 2*c[1]*t1*(1 - t1) + c[2]*t1*t1 michael@0: C = c[3]*(1 - t1)*(1 - t1) + 2*c[2]*t1*(1 - t1) + c[1]*t1*t1 michael@0: michael@0: To find B, compute the point halfway between t1 and t2: michael@0: michael@0: q(at (t1 + t2)/2) == D michael@0: michael@0: Next, compute where D must be if we know the value of B: michael@0: michael@0: _12 = A/2 + B/2 michael@0: 12_ = B/2 + C/2 michael@0: 123 = A/4 + B/2 + C/4 michael@0: = D michael@0: michael@0: Group the known values on one side: michael@0: michael@0: B = D*2 - A/2 - C/2 michael@0: */ michael@0: michael@0: static double interp_quad_coords(const double* src, double t) { michael@0: double ab = SkDInterp(src[0], src[2], t); michael@0: double bc = SkDInterp(src[2], src[4], t); michael@0: double abc = SkDInterp(ab, bc, t); michael@0: return abc; michael@0: } michael@0: michael@0: bool SkDQuad::monotonicInY() const { michael@0: return between(fPts[0].fY, fPts[1].fY, fPts[2].fY); michael@0: } michael@0: michael@0: SkDQuad SkDQuad::subDivide(double t1, double t2) const { michael@0: SkDQuad dst; michael@0: double ax = dst[0].fX = interp_quad_coords(&fPts[0].fX, t1); michael@0: double ay = dst[0].fY = interp_quad_coords(&fPts[0].fY, t1); michael@0: double dx = interp_quad_coords(&fPts[0].fX, (t1 + t2) / 2); michael@0: double dy = interp_quad_coords(&fPts[0].fY, (t1 + t2) / 2); michael@0: double cx = dst[2].fX = interp_quad_coords(&fPts[0].fX, t2); michael@0: double cy = dst[2].fY = interp_quad_coords(&fPts[0].fY, t2); michael@0: /* bx = */ dst[1].fX = 2*dx - (ax + cx)/2; michael@0: /* by = */ dst[1].fY = 2*dy - (ay + cy)/2; michael@0: return dst; michael@0: } michael@0: michael@0: void SkDQuad::align(int endIndex, SkDPoint* dstPt) const { michael@0: if (fPts[endIndex].fX == fPts[1].fX) { michael@0: dstPt->fX = fPts[endIndex].fX; michael@0: } michael@0: if (fPts[endIndex].fY == fPts[1].fY) { michael@0: dstPt->fY = fPts[endIndex].fY; michael@0: } michael@0: } michael@0: michael@0: SkDPoint SkDQuad::subDivide(const SkDPoint& a, const SkDPoint& c, double t1, double t2) const { michael@0: SkASSERT(t1 != t2); michael@0: SkDPoint b; michael@0: #if 0 michael@0: // this approach assumes that the control point computed directly is accurate enough michael@0: double dx = interp_quad_coords(&fPts[0].fX, (t1 + t2) / 2); michael@0: double dy = interp_quad_coords(&fPts[0].fY, (t1 + t2) / 2); michael@0: b.fX = 2 * dx - (a.fX + c.fX) / 2; michael@0: b.fY = 2 * dy - (a.fY + c.fY) / 2; michael@0: #else michael@0: SkDQuad sub = subDivide(t1, t2); michael@0: SkDLine b0 = {{a, sub[1] + (a - sub[0])}}; michael@0: SkDLine b1 = {{c, sub[1] + (c - sub[2])}}; michael@0: SkIntersections i; michael@0: i.intersectRay(b0, b1); michael@0: if (i.used() == 1) { michael@0: b = i.pt(0); michael@0: } else { michael@0: SkASSERT(i.used() == 2 || i.used() == 0); michael@0: b = SkDPoint::Mid(b0[1], b1[1]); michael@0: } michael@0: #endif michael@0: if (t1 == 0 || t2 == 0) { michael@0: align(0, &b); michael@0: } michael@0: if (t1 == 1 || t2 == 1) { michael@0: align(2, &b); michael@0: } michael@0: if (precisely_subdivide_equal(b.fX, a.fX)) { michael@0: b.fX = a.fX; michael@0: } else if (precisely_subdivide_equal(b.fX, c.fX)) { michael@0: b.fX = c.fX; michael@0: } michael@0: if (precisely_subdivide_equal(b.fY, a.fY)) { michael@0: b.fY = a.fY; michael@0: } else if (precisely_subdivide_equal(b.fY, c.fY)) { michael@0: b.fY = c.fY; michael@0: } michael@0: return b; michael@0: } michael@0: michael@0: /* classic one t subdivision */ michael@0: static void interp_quad_coords(const double* src, double* dst, double t) { michael@0: double ab = SkDInterp(src[0], src[2], t); michael@0: double bc = SkDInterp(src[2], src[4], t); michael@0: dst[0] = src[0]; michael@0: dst[2] = ab; michael@0: dst[4] = SkDInterp(ab, bc, t); michael@0: dst[6] = bc; michael@0: dst[8] = src[4]; michael@0: } michael@0: michael@0: SkDQuadPair SkDQuad::chopAt(double t) const michael@0: { michael@0: SkDQuadPair dst; michael@0: interp_quad_coords(&fPts[0].fX, &dst.pts[0].fX, t); michael@0: interp_quad_coords(&fPts[0].fY, &dst.pts[0].fY, t); michael@0: return dst; michael@0: } michael@0: michael@0: static int valid_unit_divide(double numer, double denom, double* ratio) michael@0: { michael@0: if (numer < 0) { michael@0: numer = -numer; michael@0: denom = -denom; michael@0: } michael@0: if (denom == 0 || numer == 0 || numer >= denom) { michael@0: return 0; michael@0: } michael@0: double r = numer / denom; michael@0: if (r == 0) { // catch underflow if numer <<<< denom michael@0: return 0; michael@0: } michael@0: *ratio = r; michael@0: return 1; michael@0: } michael@0: michael@0: /** Quad'(t) = At + B, where michael@0: A = 2(a - 2b + c) michael@0: B = 2(b - a) michael@0: Solve for t, only if it fits between 0 < t < 1 michael@0: */ michael@0: int SkDQuad::FindExtrema(double a, double b, double c, double tValue[1]) { michael@0: /* At + B == 0 michael@0: t = -B / A michael@0: */ michael@0: return valid_unit_divide(a - b, a - b - b + c, tValue); michael@0: } michael@0: michael@0: /* Parameterization form, given A*t*t + 2*B*t*(1-t) + C*(1-t)*(1-t) michael@0: * michael@0: * a = A - 2*B + C michael@0: * b = 2*B - 2*C michael@0: * c = C michael@0: */ michael@0: void SkDQuad::SetABC(const double* quad, double* a, double* b, double* c) { michael@0: *a = quad[0]; // a = A michael@0: *b = 2 * quad[2]; // b = 2*B michael@0: *c = quad[4]; // c = C michael@0: *b -= *c; // b = 2*B - C michael@0: *a -= *b; // a = A - 2*B + C michael@0: *b -= *c; // b = 2*B - 2*C michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkDQuad::dump() { michael@0: SkDebugf("{{"); michael@0: int index = 0; michael@0: do { michael@0: fPts[index].dump(); michael@0: SkDebugf(", "); michael@0: } while (++index < 2); michael@0: fPts[index].dump(); michael@0: SkDebugf("}}\n"); michael@0: } michael@0: #endif