Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2009 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #include "SkCubicClipper.h"
     11 #include "SkGeometry.h"
     12 
     13 SkCubicClipper::SkCubicClipper() {
     14     fClip.setEmpty();
     15 }
     16 
     17 void SkCubicClipper::setClip(const SkIRect& clip) {
     18     // conver to scalars, since that's where we'll see the points
     19     fClip.set(clip);
     20 }
     21 
     22 
     23 static bool chopMonoCubicAtY(SkPoint pts[4], SkScalar y, SkScalar* t) {
     24     SkScalar ycrv[4];
     25     ycrv[0] = pts[0].fY - y;
     26     ycrv[1] = pts[1].fY - y;
     27     ycrv[2] = pts[2].fY - y;
     28     ycrv[3] = pts[3].fY - y;
     29 
     30 #ifdef NEWTON_RAPHSON    // Quadratic convergence, typically <= 3 iterations.
     31     // Initial guess.
     32     // TODO(turk): Check for zero denominator? Shouldn't happen unless the curve
     33     // is not only monotonic but degenerate.
     34 #ifdef SK_SCALAR_IS_FLOAT
     35     SkScalar t1 = ycrv[0] / (ycrv[0] - ycrv[3]);
     36 #else  // !SK_SCALAR_IS_FLOAT
     37     SkScalar t1 = SkDivBits(ycrv[0], ycrv[0] - ycrv[3], 16);
     38 #endif  // !SK_SCALAR_IS_FLOAT
     39 
     40     // Newton's iterations.
     41     const SkScalar tol = SK_Scalar1 / 16384;  // This leaves 2 fixed noise bits.
     42     SkScalar t0;
     43     const int maxiters = 5;
     44     int iters = 0;
     45     bool converged;
     46     do {
     47         t0 = t1;
     48         SkScalar y01   = SkScalarInterp(ycrv[0], ycrv[1], t0);
     49         SkScalar y12   = SkScalarInterp(ycrv[1], ycrv[2], t0);
     50         SkScalar y23   = SkScalarInterp(ycrv[2], ycrv[3], t0);
     51         SkScalar y012  = SkScalarInterp(y01,  y12,  t0);
     52         SkScalar y123  = SkScalarInterp(y12,  y23,  t0);
     53         SkScalar y0123 = SkScalarInterp(y012, y123, t0);
     54         SkScalar yder  = (y123 - y012) * 3;
     55         // TODO(turk): check for yder==0: horizontal.
     56 #ifdef SK_SCALAR_IS_FLOAT
     57         t1 -= y0123 / yder;
     58 #else  // !SK_SCALAR_IS_FLOAT
     59         t1 -= SkDivBits(y0123, yder, 16);
     60 #endif  // !SK_SCALAR_IS_FLOAT
     61         converged = SkScalarAbs(t1 - t0) <= tol;  // NaN-safe
     62         ++iters;
     63     } while (!converged && (iters < maxiters));
     64     *t = t1;                  // Return the result.
     65 
     66     // The result might be valid, even if outside of the range [0, 1], but
     67     // we never evaluate a Bezier outside this interval, so we return false.
     68     if (t1 < 0 || t1 > SK_Scalar1)
     69         return false;         // This shouldn't happen, but check anyway.
     70     return converged;
     71 
     72 #else  // BISECTION    // Linear convergence, typically 16 iterations.
     73 
     74     // Check that the endpoints straddle zero.
     75     SkScalar tNeg, tPos;    // Negative and positive function parameters.
     76     if (ycrv[0] < 0) {
     77         if (ycrv[3] < 0)
     78             return false;
     79         tNeg = 0;
     80         tPos = SK_Scalar1;
     81     } else if (ycrv[0] > 0) {
     82         if (ycrv[3] > 0)
     83             return false;
     84         tNeg = SK_Scalar1;
     85         tPos = 0;
     86     } else {
     87         *t = 0;
     88         return true;
     89     }
     90 
     91     const SkScalar tol = SK_Scalar1 / 65536;  // 1 for fixed, 1e-5 for float.
     92     int iters = 0;
     93     do {
     94         SkScalar tMid = (tPos + tNeg) / 2;
     95         SkScalar y01   = SkScalarInterp(ycrv[0], ycrv[1], tMid);
     96         SkScalar y12   = SkScalarInterp(ycrv[1], ycrv[2], tMid);
     97         SkScalar y23   = SkScalarInterp(ycrv[2], ycrv[3], tMid);
     98         SkScalar y012  = SkScalarInterp(y01,     y12,     tMid);
     99         SkScalar y123  = SkScalarInterp(y12,     y23,     tMid);
    100         SkScalar y0123 = SkScalarInterp(y012,    y123,    tMid);
    101         if (y0123 == 0) {
    102             *t = tMid;
    103             return true;
    104         }
    105         if (y0123 < 0)  tNeg = tMid;
    106         else            tPos = tMid;
    107         ++iters;
    108     } while (!(SkScalarAbs(tPos - tNeg) <= tol));   // Nan-safe
    109 
    110     *t = (tNeg + tPos) / 2;
    111     return true;
    112 #endif  // BISECTION
    113 }
    114 
    115 
    116 bool SkCubicClipper::clipCubic(const SkPoint srcPts[4], SkPoint dst[4]) {
    117     bool reverse;
    118 
    119     // we need the data to be monotonically descending in Y
    120     if (srcPts[0].fY > srcPts[3].fY) {
    121         dst[0] = srcPts[3];
    122         dst[1] = srcPts[2];
    123         dst[2] = srcPts[1];
    124         dst[3] = srcPts[0];
    125         reverse = true;
    126     } else {
    127         memcpy(dst, srcPts, 4 * sizeof(SkPoint));
    128         reverse = false;
    129     }
    130 
    131     // are we completely above or below
    132     const SkScalar ctop = fClip.fTop;
    133     const SkScalar cbot = fClip.fBottom;
    134     if (dst[3].fY <= ctop || dst[0].fY >= cbot) {
    135         return false;
    136     }
    137 
    138     SkScalar t;
    139     SkPoint tmp[7]; // for SkChopCubicAt
    140 
    141     // are we partially above
    142     if (dst[0].fY < ctop && chopMonoCubicAtY(dst, ctop, &t)) {
    143         SkChopCubicAt(dst, tmp, t);
    144         dst[0] = tmp[3];
    145         dst[1] = tmp[4];
    146         dst[2] = tmp[5];
    147     }
    148 
    149     // are we partially below
    150     if (dst[3].fY > cbot && chopMonoCubicAtY(dst, cbot, &t)) {
    151         SkChopCubicAt(dst, tmp, t);
    152         dst[1] = tmp[1];
    153         dst[2] = tmp[2];
    154         dst[3] = tmp[3];
    155     }
    156 
    157     if (reverse) {
    158         SkTSwap<SkPoint>(dst[0], dst[3]);
    159         SkTSwap<SkPoint>(dst[1], dst[2]);
    160     }
    161     return true;
    162 }
    163