michael@0: michael@0: /* michael@0: * Copyright 2009 The Android Open Source Project 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: michael@0: michael@0: #include "SkCubicClipper.h" michael@0: #include "SkGeometry.h" michael@0: michael@0: SkCubicClipper::SkCubicClipper() { michael@0: fClip.setEmpty(); michael@0: } michael@0: michael@0: void SkCubicClipper::setClip(const SkIRect& clip) { michael@0: // conver to scalars, since that's where we'll see the points michael@0: fClip.set(clip); michael@0: } michael@0: michael@0: michael@0: static bool chopMonoCubicAtY(SkPoint pts[4], SkScalar y, SkScalar* t) { michael@0: SkScalar ycrv[4]; michael@0: ycrv[0] = pts[0].fY - y; michael@0: ycrv[1] = pts[1].fY - y; michael@0: ycrv[2] = pts[2].fY - y; michael@0: ycrv[3] = pts[3].fY - y; michael@0: michael@0: #ifdef NEWTON_RAPHSON // Quadratic convergence, typically <= 3 iterations. michael@0: // Initial guess. michael@0: // TODO(turk): Check for zero denominator? Shouldn't happen unless the curve michael@0: // is not only monotonic but degenerate. michael@0: SkScalar t1 = ycrv[0] / (ycrv[0] - ycrv[3]); michael@0: michael@0: // Newton's iterations. michael@0: const SkScalar tol = SK_Scalar1 / 16384; // This leaves 2 fixed noise bits. michael@0: SkScalar t0; michael@0: const int maxiters = 5; michael@0: int iters = 0; michael@0: bool converged; michael@0: do { michael@0: t0 = t1; michael@0: SkScalar y01 = SkScalarInterp(ycrv[0], ycrv[1], t0); michael@0: SkScalar y12 = SkScalarInterp(ycrv[1], ycrv[2], t0); michael@0: SkScalar y23 = SkScalarInterp(ycrv[2], ycrv[3], t0); michael@0: SkScalar y012 = SkScalarInterp(y01, y12, t0); michael@0: SkScalar y123 = SkScalarInterp(y12, y23, t0); michael@0: SkScalar y0123 = SkScalarInterp(y012, y123, t0); michael@0: SkScalar yder = (y123 - y012) * 3; michael@0: // TODO(turk): check for yder==0: horizontal. michael@0: t1 -= y0123 / yder; michael@0: converged = SkScalarAbs(t1 - t0) <= tol; // NaN-safe michael@0: ++iters; michael@0: } while (!converged && (iters < maxiters)); michael@0: *t = t1; // Return the result. michael@0: michael@0: // The result might be valid, even if outside of the range [0, 1], but michael@0: // we never evaluate a Bezier outside this interval, so we return false. michael@0: if (t1 < 0 || t1 > SK_Scalar1) michael@0: return false; // This shouldn't happen, but check anyway. michael@0: return converged; michael@0: michael@0: #else // BISECTION // Linear convergence, typically 16 iterations. michael@0: michael@0: // Check that the endpoints straddle zero. michael@0: SkScalar tNeg, tPos; // Negative and positive function parameters. michael@0: if (ycrv[0] < 0) { michael@0: if (ycrv[3] < 0) michael@0: return false; michael@0: tNeg = 0; michael@0: tPos = SK_Scalar1; michael@0: } else if (ycrv[0] > 0) { michael@0: if (ycrv[3] > 0) michael@0: return false; michael@0: tNeg = SK_Scalar1; michael@0: tPos = 0; michael@0: } else { michael@0: *t = 0; michael@0: return true; michael@0: } michael@0: michael@0: const SkScalar tol = SK_Scalar1 / 65536; // 1 for fixed, 1e-5 for float. michael@0: int iters = 0; michael@0: do { michael@0: SkScalar tMid = (tPos + tNeg) / 2; michael@0: SkScalar y01 = SkScalarInterp(ycrv[0], ycrv[1], tMid); michael@0: SkScalar y12 = SkScalarInterp(ycrv[1], ycrv[2], tMid); michael@0: SkScalar y23 = SkScalarInterp(ycrv[2], ycrv[3], tMid); michael@0: SkScalar y012 = SkScalarInterp(y01, y12, tMid); michael@0: SkScalar y123 = SkScalarInterp(y12, y23, tMid); michael@0: SkScalar y0123 = SkScalarInterp(y012, y123, tMid); michael@0: if (y0123 == 0) { michael@0: *t = tMid; michael@0: return true; michael@0: } michael@0: if (y0123 < 0) tNeg = tMid; michael@0: else tPos = tMid; michael@0: ++iters; michael@0: } while (!(SkScalarAbs(tPos - tNeg) <= tol)); // Nan-safe michael@0: michael@0: *t = (tNeg + tPos) / 2; michael@0: return true; michael@0: #endif // BISECTION michael@0: } michael@0: michael@0: michael@0: bool SkCubicClipper::clipCubic(const SkPoint srcPts[4], SkPoint dst[4]) { michael@0: bool reverse; michael@0: michael@0: // we need the data to be monotonically descending in Y michael@0: if (srcPts[0].fY > srcPts[3].fY) { michael@0: dst[0] = srcPts[3]; michael@0: dst[1] = srcPts[2]; michael@0: dst[2] = srcPts[1]; michael@0: dst[3] = srcPts[0]; michael@0: reverse = true; michael@0: } else { michael@0: memcpy(dst, srcPts, 4 * sizeof(SkPoint)); michael@0: reverse = false; michael@0: } michael@0: michael@0: // are we completely above or below michael@0: const SkScalar ctop = fClip.fTop; michael@0: const SkScalar cbot = fClip.fBottom; michael@0: if (dst[3].fY <= ctop || dst[0].fY >= cbot) { michael@0: return false; michael@0: } michael@0: michael@0: SkScalar t; michael@0: SkPoint tmp[7]; // for SkChopCubicAt michael@0: michael@0: // are we partially above michael@0: if (dst[0].fY < ctop && chopMonoCubicAtY(dst, ctop, &t)) { michael@0: SkChopCubicAt(dst, tmp, t); michael@0: dst[0] = tmp[3]; michael@0: dst[1] = tmp[4]; michael@0: dst[2] = tmp[5]; michael@0: } michael@0: michael@0: // are we partially below michael@0: if (dst[3].fY > cbot && chopMonoCubicAtY(dst, cbot, &t)) { michael@0: SkChopCubicAt(dst, tmp, t); michael@0: dst[1] = tmp[1]; michael@0: dst[2] = tmp[2]; michael@0: dst[3] = tmp[3]; michael@0: } michael@0: michael@0: if (reverse) { michael@0: SkTSwap(dst[0], dst[3]); michael@0: SkTSwap(dst[1], dst[2]); michael@0: } michael@0: return true; michael@0: }