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