blob: 3db2feb34c68a8b8df5454ea61dc78ee67703b1c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
turk@google.com4896f9e2009-03-02 20:00:00 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2009 The Android Open Source Project
turk@google.com4896f9e2009-03-02 20:00:00 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
turk@google.com4896f9e2009-03-02 20:00:00 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
turk@google.com4896f9e2009-03-02 20:00:00 +000010#include "SkCubicClipper.h"
11#include "SkGeometry.h"
12
vandebo@chromium.orga728e352012-03-28 20:29:38 +000013SkCubicClipper::SkCubicClipper() {
14 fClip.setEmpty();
15}
turk@google.com4896f9e2009-03-02 20:00:00 +000016
17void 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
23static 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.
turk@google.com7d3a58a2009-03-04 01:33:35 +000075 SkScalar tNeg, tPos; // Negative and positive function parameters.
turk@google.com4896f9e2009-03-02 20:00:00 +000076 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 }
turk@google.com7d3a58a2009-03-04 01:33:35 +000090
turk@google.com4896f9e2009-03-02 20:00:00 +000091 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
116bool SkCubicClipper::clipCubic(const SkPoint srcPts[4], SkPoint dst[4]) {
117 bool reverse;
118
119 // we need the data to be monotonically descending in Y
turk@google.com5755a2a2009-03-03 02:56:05 +0000120 if (srcPts[0].fY > srcPts[3].fY) {
turk@google.com4896f9e2009-03-02 20:00:00 +0000121 dst[0] = srcPts[3];
122 dst[1] = srcPts[2];
123 dst[2] = srcPts[1];
124 dst[3] = srcPts[0];
125 reverse = true;
126 } else {
turk@google.com5755a2a2009-03-03 02:56:05 +0000127 memcpy(dst, srcPts, 4 * sizeof(SkPoint));
turk@google.com4896f9e2009-03-02 20:00:00 +0000128 reverse = false;
129 }
130
131 // are we completely above or below
132 const SkScalar ctop = fClip.fTop;
133 const SkScalar cbot = fClip.fBottom;
turk@google.com5755a2a2009-03-03 02:56:05 +0000134 if (dst[3].fY <= ctop || dst[0].fY >= cbot) {
turk@google.com4896f9e2009-03-02 20:00:00 +0000135 return false;
136 }
turk@google.com7d3a58a2009-03-04 01:33:35 +0000137
turk@google.com4896f9e2009-03-02 20:00:00 +0000138 SkScalar t;
turk@google.com5755a2a2009-03-03 02:56:05 +0000139 SkPoint tmp[7]; // for SkChopCubicAt
turk@google.com7d3a58a2009-03-04 01:33:35 +0000140
turk@google.com4896f9e2009-03-02 20:00:00 +0000141 // are we partially above
142 if (dst[0].fY < ctop && chopMonoCubicAtY(dst, ctop, &t)) {
143 SkChopCubicAt(dst, tmp, t);
turk@google.com5755a2a2009-03-03 02:56:05 +0000144 dst[0] = tmp[3];
145 dst[1] = tmp[4];
146 dst[2] = tmp[5];
turk@google.com4896f9e2009-03-02 20:00:00 +0000147 }
turk@google.com7d3a58a2009-03-04 01:33:35 +0000148
turk@google.com4896f9e2009-03-02 20:00:00 +0000149 // are we partially below
turk@google.com5755a2a2009-03-03 02:56:05 +0000150 if (dst[3].fY > cbot && chopMonoCubicAtY(dst, cbot, &t)) {
turk@google.com4896f9e2009-03-02 20:00:00 +0000151 SkChopCubicAt(dst, tmp, t);
152 dst[1] = tmp[1];
153 dst[2] = tmp[2];
turk@google.com5755a2a2009-03-03 02:56:05 +0000154 dst[3] = tmp[3];
turk@google.com4896f9e2009-03-02 20:00:00 +0000155 }
turk@google.com7d3a58a2009-03-04 01:33:35 +0000156
turk@google.com4896f9e2009-03-02 20:00:00 +0000157 if (reverse) {
158 SkTSwap<SkPoint>(dst[0], dst[3]);
159 SkTSwap<SkPoint>(dst[1], dst[2]);
160 }
161 return true;
162}
163