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 "SkEdgeClipper.h" michael@0: #include "SkGeometry.h" michael@0: michael@0: static bool quick_reject(const SkRect& bounds, const SkRect& clip) { michael@0: return bounds.fTop >= clip.fBottom || bounds.fBottom <= clip.fTop; michael@0: } michael@0: michael@0: static inline void clamp_le(SkScalar& value, SkScalar max) { michael@0: if (value > max) { michael@0: value = max; michael@0: } michael@0: } michael@0: michael@0: static inline void clamp_ge(SkScalar& value, SkScalar min) { michael@0: if (value < min) { michael@0: value = min; michael@0: } michael@0: } michael@0: michael@0: /* src[] must be monotonic in Y. This routine copies src into dst, and sorts michael@0: it to be increasing in Y. If it had to reverse the order of the points, michael@0: it returns true, otherwise it returns false michael@0: */ michael@0: static bool sort_increasing_Y(SkPoint dst[], const SkPoint src[], int count) { michael@0: // we need the data to be monotonically increasing in Y michael@0: if (src[0].fY > src[count - 1].fY) { michael@0: for (int i = 0; i < count; i++) { michael@0: dst[i] = src[count - i - 1]; michael@0: } michael@0: return true; michael@0: } else { michael@0: memcpy(dst, src, count * sizeof(SkPoint)); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool chopMonoQuadAt(SkScalar c0, SkScalar c1, SkScalar c2, michael@0: SkScalar target, SkScalar* t) { michael@0: /* Solve F(t) = y where F(t) := [0](1-t)^2 + 2[1]t(1-t) + [2]t^2 michael@0: * We solve for t, using quadratic equation, hence we have to rearrange michael@0: * our cooefficents to look like At^2 + Bt + C michael@0: */ michael@0: SkScalar A = c0 - c1 - c1 + c2; michael@0: SkScalar B = 2*(c1 - c0); michael@0: SkScalar C = c0 - target; michael@0: michael@0: SkScalar roots[2]; // we only expect one, but make room for 2 for safety michael@0: int count = SkFindUnitQuadRoots(A, B, C, roots); michael@0: if (count) { michael@0: *t = roots[0]; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool chopMonoQuadAtY(SkPoint pts[3], SkScalar y, SkScalar* t) { michael@0: return chopMonoQuadAt(pts[0].fY, pts[1].fY, pts[2].fY, y, t); michael@0: } michael@0: michael@0: static bool chopMonoQuadAtX(SkPoint pts[3], SkScalar x, SkScalar* t) { michael@0: return chopMonoQuadAt(pts[0].fX, pts[1].fX, pts[2].fX, x, t); michael@0: } michael@0: michael@0: // Modify pts[] in place so that it is clipped in Y to the clip rect michael@0: static void chop_quad_in_Y(SkPoint pts[3], const SkRect& clip) { michael@0: SkScalar t; michael@0: SkPoint tmp[5]; // for SkChopQuadAt michael@0: michael@0: // are we partially above michael@0: if (pts[0].fY < clip.fTop) { michael@0: if (chopMonoQuadAtY(pts, clip.fTop, &t)) { michael@0: // take the 2nd chopped quad michael@0: SkChopQuadAt(pts, tmp, t); michael@0: // clamp to clean up imprecise numerics in the chop michael@0: tmp[2].fY = clip.fTop; michael@0: clamp_ge(tmp[3].fY, clip.fTop); michael@0: michael@0: pts[0] = tmp[2]; michael@0: pts[1] = tmp[3]; michael@0: } else { michael@0: // if chopMonoQuadAtY failed, then we may have hit inexact numerics michael@0: // so we just clamp against the top michael@0: for (int i = 0; i < 3; i++) { michael@0: if (pts[i].fY < clip.fTop) { michael@0: pts[i].fY = clip.fTop; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // are we partially below michael@0: if (pts[2].fY > clip.fBottom) { michael@0: if (chopMonoQuadAtY(pts, clip.fBottom, &t)) { michael@0: SkChopQuadAt(pts, tmp, t); michael@0: // clamp to clean up imprecise numerics in the chop michael@0: clamp_le(tmp[1].fY, clip.fBottom); michael@0: tmp[2].fY = clip.fBottom; michael@0: michael@0: pts[1] = tmp[1]; michael@0: pts[2] = tmp[2]; michael@0: } else { michael@0: // if chopMonoQuadAtY failed, then we may have hit inexact numerics michael@0: // so we just clamp against the bottom michael@0: for (int i = 0; i < 3; i++) { michael@0: if (pts[i].fY > clip.fBottom) { michael@0: pts[i].fY = clip.fBottom; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // srcPts[] must be monotonic in X and Y michael@0: void SkEdgeClipper::clipMonoQuad(const SkPoint srcPts[3], const SkRect& clip) { michael@0: SkPoint pts[3]; michael@0: bool reverse = sort_increasing_Y(pts, srcPts, 3); michael@0: michael@0: // are we completely above or below michael@0: if (pts[2].fY <= clip.fTop || pts[0].fY >= clip.fBottom) { michael@0: return; michael@0: } michael@0: michael@0: // Now chop so that pts is contained within clip in Y michael@0: chop_quad_in_Y(pts, clip); michael@0: michael@0: if (pts[0].fX > pts[2].fX) { michael@0: SkTSwap(pts[0], pts[2]); michael@0: reverse = !reverse; michael@0: } michael@0: SkASSERT(pts[0].fX <= pts[1].fX); michael@0: SkASSERT(pts[1].fX <= pts[2].fX); michael@0: michael@0: // Now chop in X has needed, and record the segments michael@0: michael@0: if (pts[2].fX <= clip.fLeft) { // wholly to the left michael@0: this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse); michael@0: return; michael@0: } michael@0: if (pts[0].fX >= clip.fRight) { // wholly to the right michael@0: this->appendVLine(clip.fRight, pts[0].fY, pts[2].fY, reverse); michael@0: return; michael@0: } michael@0: michael@0: SkScalar t; michael@0: SkPoint tmp[5]; // for SkChopQuadAt michael@0: michael@0: // are we partially to the left michael@0: if (pts[0].fX < clip.fLeft) { michael@0: if (chopMonoQuadAtX(pts, clip.fLeft, &t)) { michael@0: SkChopQuadAt(pts, tmp, t); michael@0: this->appendVLine(clip.fLeft, tmp[0].fY, tmp[2].fY, reverse); michael@0: // clamp to clean up imprecise numerics in the chop michael@0: tmp[2].fX = clip.fLeft; michael@0: clamp_ge(tmp[3].fX, clip.fLeft); michael@0: michael@0: pts[0] = tmp[2]; michael@0: pts[1] = tmp[3]; michael@0: } else { michael@0: // if chopMonoQuadAtY failed, then we may have hit inexact numerics michael@0: // so we just clamp against the left michael@0: this->appendVLine(clip.fLeft, pts[0].fY, pts[2].fY, reverse); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // are we partially to the right michael@0: if (pts[2].fX > clip.fRight) { michael@0: if (chopMonoQuadAtX(pts, clip.fRight, &t)) { michael@0: SkChopQuadAt(pts, tmp, t); michael@0: // clamp to clean up imprecise numerics in the chop michael@0: clamp_le(tmp[1].fX, clip.fRight); michael@0: tmp[2].fX = clip.fRight; michael@0: michael@0: this->appendQuad(tmp, reverse); michael@0: this->appendVLine(clip.fRight, tmp[2].fY, tmp[4].fY, reverse); michael@0: } else { michael@0: // if chopMonoQuadAtY failed, then we may have hit inexact numerics michael@0: // so we just clamp against the right michael@0: this->appendVLine(clip.fRight, pts[0].fY, pts[2].fY, reverse); michael@0: } michael@0: } else { // wholly inside the clip michael@0: this->appendQuad(pts, reverse); michael@0: } michael@0: } michael@0: michael@0: bool SkEdgeClipper::clipQuad(const SkPoint srcPts[3], const SkRect& clip) { michael@0: fCurrPoint = fPoints; michael@0: fCurrVerb = fVerbs; michael@0: michael@0: SkRect bounds; michael@0: bounds.set(srcPts, 3); michael@0: michael@0: if (!quick_reject(bounds, clip)) { michael@0: SkPoint monoY[5]; michael@0: int countY = SkChopQuadAtYExtrema(srcPts, monoY); michael@0: for (int y = 0; y <= countY; y++) { michael@0: SkPoint monoX[5]; michael@0: int countX = SkChopQuadAtXExtrema(&monoY[y * 2], monoX); michael@0: for (int x = 0; x <= countX; x++) { michael@0: this->clipMonoQuad(&monoX[x * 2], clip); michael@0: SkASSERT(fCurrVerb - fVerbs < kMaxVerbs); michael@0: SkASSERT(fCurrPoint - fPoints <= kMaxPoints); michael@0: } michael@0: } michael@0: } michael@0: michael@0: *fCurrVerb = SkPath::kDone_Verb; michael@0: fCurrPoint = fPoints; michael@0: fCurrVerb = fVerbs; michael@0: return SkPath::kDone_Verb != fVerbs[0]; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static SkScalar eval_cubic_coeff(SkScalar A, SkScalar B, SkScalar C, michael@0: SkScalar D, SkScalar t) { michael@0: return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); michael@0: } michael@0: michael@0: /* Given 4 cubic points (either Xs or Ys), and a target X or Y, compute the michael@0: t value such that cubic(t) = target michael@0: */ michael@0: static bool chopMonoCubicAt(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3, michael@0: SkScalar target, SkScalar* t) { michael@0: // SkASSERT(c0 <= c1 && c1 <= c2 && c2 <= c3); michael@0: SkASSERT(c0 < target && target < c3); michael@0: michael@0: SkScalar D = c0 - target; michael@0: SkScalar A = c3 + 3*(c1 - c2) - c0; michael@0: SkScalar B = 3*(c2 - c1 - c1 + c0); michael@0: SkScalar C = 3*(c1 - c0); michael@0: michael@0: const SkScalar TOLERANCE = SK_Scalar1 / 4096; michael@0: SkScalar minT = 0; michael@0: SkScalar maxT = SK_Scalar1; michael@0: SkScalar mid; michael@0: michael@0: // This is a lot of iterations. Is there a faster way? michael@0: for (int i = 0; i < 24; i++) { michael@0: mid = SkScalarAve(minT, maxT); michael@0: SkScalar delta = eval_cubic_coeff(A, B, C, D, mid); michael@0: if (delta < 0) { michael@0: minT = mid; michael@0: delta = -delta; michael@0: } else { michael@0: maxT = mid; michael@0: } michael@0: if (delta < TOLERANCE) { michael@0: break; michael@0: } michael@0: } michael@0: *t = mid; michael@0: // SkDebugf("-- evalCubicAt %d delta %g\n", i, eval_cubic_coeff(A, B, C, D, *t)); michael@0: return true; michael@0: } michael@0: michael@0: static bool chopMonoCubicAtY(SkPoint pts[4], SkScalar y, SkScalar* t) { michael@0: return chopMonoCubicAt(pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY, y, t); michael@0: } michael@0: michael@0: static bool chopMonoCubicAtX(SkPoint pts[4], SkScalar x, SkScalar* t) { michael@0: return chopMonoCubicAt(pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX, x, t); michael@0: } michael@0: michael@0: // Modify pts[] in place so that it is clipped in Y to the clip rect michael@0: static void chop_cubic_in_Y(SkPoint pts[4], const SkRect& clip) { michael@0: michael@0: // are we partially above michael@0: if (pts[0].fY < clip.fTop) { michael@0: SkScalar t; michael@0: if (chopMonoCubicAtY(pts, clip.fTop, &t)) { michael@0: SkPoint tmp[7]; michael@0: SkChopCubicAt(pts, tmp, t); michael@0: michael@0: // tmp[3, 4, 5].fY should all be to the below clip.fTop. michael@0: // Since we can't trust the numerics of michael@0: // the chopper, we force those conditions now michael@0: tmp[3].fY = clip.fTop; michael@0: clamp_ge(tmp[4].fY, clip.fTop); michael@0: clamp_ge(tmp[5].fY, clip.fTop); michael@0: michael@0: pts[0] = tmp[3]; michael@0: pts[1] = tmp[4]; michael@0: pts[2] = tmp[5]; michael@0: } else { michael@0: // if chopMonoCubicAtY failed, then we may have hit inexact numerics michael@0: // so we just clamp against the top michael@0: for (int i = 0; i < 4; i++) { michael@0: clamp_ge(pts[i].fY, clip.fTop); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // are we partially below michael@0: if (pts[3].fY > clip.fBottom) { michael@0: SkScalar t; michael@0: if (chopMonoCubicAtY(pts, clip.fBottom, &t)) { michael@0: SkPoint tmp[7]; michael@0: SkChopCubicAt(pts, tmp, t); michael@0: tmp[3].fY = clip.fBottom; michael@0: clamp_le(tmp[2].fY, clip.fBottom); michael@0: michael@0: pts[1] = tmp[1]; michael@0: pts[2] = tmp[2]; michael@0: pts[3] = tmp[3]; michael@0: } else { michael@0: // if chopMonoCubicAtY failed, then we may have hit inexact numerics michael@0: // so we just clamp against the bottom michael@0: for (int i = 0; i < 4; i++) { michael@0: clamp_le(pts[i].fY, clip.fBottom); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // srcPts[] must be monotonic in X and Y michael@0: void SkEdgeClipper::clipMonoCubic(const SkPoint src[4], const SkRect& clip) { michael@0: SkPoint pts[4]; michael@0: bool reverse = sort_increasing_Y(pts, src, 4); michael@0: michael@0: // are we completely above or below michael@0: if (pts[3].fY <= clip.fTop || pts[0].fY >= clip.fBottom) { michael@0: return; michael@0: } michael@0: michael@0: // Now chop so that pts is contained within clip in Y michael@0: chop_cubic_in_Y(pts, clip); michael@0: michael@0: if (pts[0].fX > pts[3].fX) { michael@0: SkTSwap(pts[0], pts[3]); michael@0: SkTSwap(pts[1], pts[2]); michael@0: reverse = !reverse; michael@0: } michael@0: michael@0: // Now chop in X has needed, and record the segments michael@0: michael@0: if (pts[3].fX <= clip.fLeft) { // wholly to the left michael@0: this->appendVLine(clip.fLeft, pts[0].fY, pts[3].fY, reverse); michael@0: return; michael@0: } michael@0: if (pts[0].fX >= clip.fRight) { // wholly to the right michael@0: this->appendVLine(clip.fRight, pts[0].fY, pts[3].fY, reverse); michael@0: return; michael@0: } michael@0: michael@0: // are we partially to the left michael@0: if (pts[0].fX < clip.fLeft) { michael@0: SkScalar t; michael@0: if (chopMonoCubicAtX(pts, clip.fLeft, &t)) { michael@0: SkPoint tmp[7]; michael@0: SkChopCubicAt(pts, tmp, t); michael@0: this->appendVLine(clip.fLeft, tmp[0].fY, tmp[3].fY, reverse); michael@0: michael@0: // tmp[3, 4, 5].fX should all be to the right of clip.fLeft. michael@0: // Since we can't trust the numerics of michael@0: // the chopper, we force those conditions now michael@0: tmp[3].fX = clip.fLeft; michael@0: clamp_ge(tmp[4].fX, clip.fLeft); michael@0: clamp_ge(tmp[5].fX, clip.fLeft); michael@0: michael@0: pts[0] = tmp[3]; michael@0: pts[1] = tmp[4]; michael@0: pts[2] = tmp[5]; michael@0: } else { michael@0: // if chopMonocubicAtY failed, then we may have hit inexact numerics michael@0: // so we just clamp against the left michael@0: this->appendVLine(clip.fLeft, pts[0].fY, pts[3].fY, reverse); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // are we partially to the right michael@0: if (pts[3].fX > clip.fRight) { michael@0: SkScalar t; michael@0: if (chopMonoCubicAtX(pts, clip.fRight, &t)) { michael@0: SkPoint tmp[7]; michael@0: SkChopCubicAt(pts, tmp, t); michael@0: tmp[3].fX = clip.fRight; michael@0: clamp_le(tmp[2].fX, clip.fRight); michael@0: clamp_le(tmp[1].fX, clip.fRight); michael@0: michael@0: this->appendCubic(tmp, reverse); michael@0: this->appendVLine(clip.fRight, tmp[3].fY, tmp[6].fY, reverse); michael@0: } else { michael@0: // if chopMonoCubicAtX failed, then we may have hit inexact numerics michael@0: // so we just clamp against the right michael@0: this->appendVLine(clip.fRight, pts[0].fY, pts[3].fY, reverse); michael@0: } michael@0: } else { // wholly inside the clip michael@0: this->appendCubic(pts, reverse); michael@0: } michael@0: } michael@0: michael@0: bool SkEdgeClipper::clipCubic(const SkPoint srcPts[4], const SkRect& clip) { michael@0: fCurrPoint = fPoints; michael@0: fCurrVerb = fVerbs; michael@0: michael@0: SkRect bounds; michael@0: bounds.set(srcPts, 4); michael@0: michael@0: if (!quick_reject(bounds, clip)) { michael@0: SkPoint monoY[10]; michael@0: int countY = SkChopCubicAtYExtrema(srcPts, monoY); michael@0: for (int y = 0; y <= countY; y++) { michael@0: SkPoint monoX[10]; michael@0: int countX = SkChopCubicAtXExtrema(&monoY[y * 3], monoX); michael@0: for (int x = 0; x <= countX; x++) { michael@0: this->clipMonoCubic(&monoX[x * 3], clip); michael@0: SkASSERT(fCurrVerb - fVerbs < kMaxVerbs); michael@0: SkASSERT(fCurrPoint - fPoints <= kMaxPoints); michael@0: } michael@0: } michael@0: } michael@0: michael@0: *fCurrVerb = SkPath::kDone_Verb; michael@0: fCurrPoint = fPoints; michael@0: fCurrVerb = fVerbs; michael@0: return SkPath::kDone_Verb != fVerbs[0]; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkEdgeClipper::appendVLine(SkScalar x, SkScalar y0, SkScalar y1, michael@0: bool reverse) { michael@0: *fCurrVerb++ = SkPath::kLine_Verb; michael@0: michael@0: if (reverse) { michael@0: SkTSwap(y0, y1); michael@0: } michael@0: fCurrPoint[0].set(x, y0); michael@0: fCurrPoint[1].set(x, y1); michael@0: fCurrPoint += 2; michael@0: } michael@0: michael@0: void SkEdgeClipper::appendQuad(const SkPoint pts[3], bool reverse) { michael@0: *fCurrVerb++ = SkPath::kQuad_Verb; michael@0: michael@0: if (reverse) { michael@0: fCurrPoint[0] = pts[2]; michael@0: fCurrPoint[2] = pts[0]; michael@0: } else { michael@0: fCurrPoint[0] = pts[0]; michael@0: fCurrPoint[2] = pts[2]; michael@0: } michael@0: fCurrPoint[1] = pts[1]; michael@0: fCurrPoint += 3; michael@0: } michael@0: michael@0: void SkEdgeClipper::appendCubic(const SkPoint pts[4], bool reverse) { michael@0: *fCurrVerb++ = SkPath::kCubic_Verb; michael@0: michael@0: if (reverse) { michael@0: for (int i = 0; i < 4; i++) { michael@0: fCurrPoint[i] = pts[3 - i]; michael@0: } michael@0: } else { michael@0: memcpy(fCurrPoint, pts, 4 * sizeof(SkPoint)); michael@0: } michael@0: fCurrPoint += 4; michael@0: } michael@0: michael@0: SkPath::Verb SkEdgeClipper::next(SkPoint pts[]) { michael@0: SkPath::Verb verb = *fCurrVerb; michael@0: michael@0: switch (verb) { michael@0: case SkPath::kLine_Verb: michael@0: memcpy(pts, fCurrPoint, 2 * sizeof(SkPoint)); michael@0: fCurrPoint += 2; michael@0: fCurrVerb += 1; michael@0: break; michael@0: case SkPath::kQuad_Verb: michael@0: memcpy(pts, fCurrPoint, 3 * sizeof(SkPoint)); michael@0: fCurrPoint += 3; michael@0: fCurrVerb += 1; michael@0: break; michael@0: case SkPath::kCubic_Verb: michael@0: memcpy(pts, fCurrPoint, 4 * sizeof(SkPoint)); michael@0: fCurrPoint += 4; michael@0: fCurrVerb += 1; michael@0: break; michael@0: case SkPath::kDone_Verb: michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("unexpected verb in quadclippper2 iter"); michael@0: break; michael@0: } michael@0: return verb; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: static void assert_monotonic(const SkScalar coord[], int count) { michael@0: if (coord[0] > coord[(count - 1) * 2]) { michael@0: for (int i = 1; i < count; i++) { michael@0: SkASSERT(coord[2 * (i - 1)] >= coord[i * 2]); michael@0: } michael@0: } else if (coord[0] < coord[(count - 1) * 2]) { michael@0: for (int i = 1; i < count; i++) { michael@0: SkASSERT(coord[2 * (i - 1)] <= coord[i * 2]); michael@0: } michael@0: } else { michael@0: for (int i = 1; i < count; i++) { michael@0: SkASSERT(coord[2 * (i - 1)] == coord[i * 2]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void sk_assert_monotonic_y(const SkPoint pts[], int count) { michael@0: if (count > 1) { michael@0: assert_monotonic(&pts[0].fY, count); michael@0: } michael@0: } michael@0: michael@0: void sk_assert_monotonic_x(const SkPoint pts[], int count) { michael@0: if (count > 1) { michael@0: assert_monotonic(&pts[0].fX, count); michael@0: } michael@0: } michael@0: #endif