michael@0: /* michael@0: * Copyright 2006 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: #include "SkGeometry.h" michael@0: #include "SkMatrix.h" michael@0: michael@0: bool SkXRayCrossesLine(const SkXRay& pt, michael@0: const SkPoint pts[2], michael@0: bool* ambiguous) { michael@0: if (ambiguous) { michael@0: *ambiguous = false; michael@0: } michael@0: // Determine quick discards. michael@0: // Consider query line going exactly through point 0 to not michael@0: // intersect, for symmetry with SkXRayCrossesMonotonicCubic. michael@0: if (pt.fY == pts[0].fY) { michael@0: if (ambiguous) { michael@0: *ambiguous = true; michael@0: } michael@0: return false; michael@0: } michael@0: if (pt.fY < pts[0].fY && pt.fY < pts[1].fY) michael@0: return false; michael@0: if (pt.fY > pts[0].fY && pt.fY > pts[1].fY) michael@0: return false; michael@0: if (pt.fX > pts[0].fX && pt.fX > pts[1].fX) michael@0: return false; michael@0: // Determine degenerate cases michael@0: if (SkScalarNearlyZero(pts[0].fY - pts[1].fY)) michael@0: return false; michael@0: if (SkScalarNearlyZero(pts[0].fX - pts[1].fX)) { michael@0: // We've already determined the query point lies within the michael@0: // vertical range of the line segment. michael@0: if (pt.fX <= pts[0].fX) { michael@0: if (ambiguous) { michael@0: *ambiguous = (pt.fY == pts[1].fY); michael@0: } michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: // Ambiguity check michael@0: if (pt.fY == pts[1].fY) { michael@0: if (pt.fX <= pts[1].fX) { michael@0: if (ambiguous) { michael@0: *ambiguous = true; michael@0: } michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: // Full line segment evaluation michael@0: SkScalar delta_y = pts[1].fY - pts[0].fY; michael@0: SkScalar delta_x = pts[1].fX - pts[0].fX; michael@0: SkScalar slope = SkScalarDiv(delta_y, delta_x); michael@0: SkScalar b = pts[0].fY - SkScalarMul(slope, pts[0].fX); michael@0: // Solve for x coordinate at y = pt.fY michael@0: SkScalar x = SkScalarDiv(pt.fY - b, slope); michael@0: return pt.fX <= x; michael@0: } michael@0: michael@0: /** If defined, this makes eval_quad and eval_cubic do more setup (sometimes michael@0: involving integer multiplies by 2 or 3, but fewer calls to SkScalarMul. michael@0: May also introduce overflow of fixed when we compute our setup. michael@0: */ michael@0: // #define DIRECT_EVAL_OF_POLYNOMIALS michael@0: michael@0: //////////////////////////////////////////////////////////////////////// michael@0: michael@0: static int is_not_monotonic(SkScalar a, SkScalar b, SkScalar c) { michael@0: SkScalar ab = a - b; michael@0: SkScalar bc = b - c; michael@0: if (ab < 0) { michael@0: bc = -bc; michael@0: } michael@0: return ab == 0 || bc < 0; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool is_unit_interval(SkScalar x) { michael@0: return x > 0 && x < SK_Scalar1; michael@0: } michael@0: michael@0: static int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio) { michael@0: SkASSERT(ratio); michael@0: michael@0: if (numer < 0) { michael@0: numer = -numer; michael@0: denom = -denom; michael@0: } michael@0: michael@0: if (denom == 0 || numer == 0 || numer >= denom) { michael@0: return 0; michael@0: } michael@0: michael@0: SkScalar r = SkScalarDiv(numer, denom); michael@0: if (SkScalarIsNaN(r)) { michael@0: return 0; michael@0: } michael@0: SkASSERT(r >= 0 && r < SK_Scalar1); michael@0: if (r == 0) { // catch underflow if numer <<<< denom michael@0: return 0; michael@0: } michael@0: *ratio = r; michael@0: return 1; michael@0: } michael@0: michael@0: /** From Numerical Recipes in C. michael@0: michael@0: Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C]) michael@0: x1 = Q / A michael@0: x2 = C / Q michael@0: */ michael@0: int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]) { michael@0: SkASSERT(roots); michael@0: michael@0: if (A == 0) { michael@0: return valid_unit_divide(-C, B, roots); michael@0: } michael@0: michael@0: SkScalar* r = roots; michael@0: michael@0: SkScalar R = B*B - 4*A*C; michael@0: if (R < 0 || SkScalarIsNaN(R)) { // complex roots michael@0: return 0; michael@0: } michael@0: R = SkScalarSqrt(R); michael@0: michael@0: SkScalar Q = (B < 0) ? -(B-R)/2 : -(B+R)/2; michael@0: r += valid_unit_divide(Q, A, r); michael@0: r += valid_unit_divide(C, Q, r); michael@0: if (r - roots == 2) { michael@0: if (roots[0] > roots[1]) michael@0: SkTSwap(roots[0], roots[1]); michael@0: else if (roots[0] == roots[1]) // nearly-equal? michael@0: r -= 1; // skip the double root michael@0: } michael@0: return (int)(r - roots); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static SkScalar eval_quad(const SkScalar src[], SkScalar t) { michael@0: SkASSERT(src); michael@0: SkASSERT(t >= 0 && t <= SK_Scalar1); michael@0: michael@0: #ifdef DIRECT_EVAL_OF_POLYNOMIALS michael@0: SkScalar C = src[0]; michael@0: SkScalar A = src[4] - 2 * src[2] + C; michael@0: SkScalar B = 2 * (src[2] - C); michael@0: return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); michael@0: #else michael@0: SkScalar ab = SkScalarInterp(src[0], src[2], t); michael@0: SkScalar bc = SkScalarInterp(src[2], src[4], t); michael@0: return SkScalarInterp(ab, bc, t); michael@0: #endif michael@0: } michael@0: michael@0: static SkScalar eval_quad_derivative(const SkScalar src[], SkScalar t) { michael@0: SkScalar A = src[4] - 2 * src[2] + src[0]; michael@0: SkScalar B = src[2] - src[0]; michael@0: michael@0: return 2 * SkScalarMulAdd(A, t, B); michael@0: } michael@0: michael@0: static SkScalar eval_quad_derivative_at_half(const SkScalar src[]) { michael@0: SkScalar A = src[4] - 2 * src[2] + src[0]; michael@0: SkScalar B = src[2] - src[0]; michael@0: return A + 2 * B; michael@0: } michael@0: michael@0: void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, michael@0: SkVector* tangent) { michael@0: SkASSERT(src); michael@0: SkASSERT(t >= 0 && t <= SK_Scalar1); michael@0: michael@0: if (pt) { michael@0: pt->set(eval_quad(&src[0].fX, t), eval_quad(&src[0].fY, t)); michael@0: } michael@0: if (tangent) { michael@0: tangent->set(eval_quad_derivative(&src[0].fX, t), michael@0: eval_quad_derivative(&src[0].fY, t)); michael@0: } michael@0: } michael@0: michael@0: void SkEvalQuadAtHalf(const SkPoint src[3], SkPoint* pt, SkVector* tangent) { michael@0: SkASSERT(src); michael@0: michael@0: if (pt) { michael@0: SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); michael@0: SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); michael@0: SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); michael@0: SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); michael@0: pt->set(SkScalarAve(x01, x12), SkScalarAve(y01, y12)); michael@0: } michael@0: if (tangent) { michael@0: tangent->set(eval_quad_derivative_at_half(&src[0].fX), michael@0: eval_quad_derivative_at_half(&src[0].fY)); michael@0: } michael@0: } michael@0: michael@0: static void interp_quad_coords(const SkScalar* src, SkScalar* dst, SkScalar t) { michael@0: SkScalar ab = SkScalarInterp(src[0], src[2], t); michael@0: SkScalar bc = SkScalarInterp(src[2], src[4], t); michael@0: michael@0: dst[0] = src[0]; michael@0: dst[2] = ab; michael@0: dst[4] = SkScalarInterp(ab, bc, t); michael@0: dst[6] = bc; michael@0: dst[8] = src[4]; michael@0: } michael@0: michael@0: void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t) { michael@0: SkASSERT(t > 0 && t < SK_Scalar1); michael@0: michael@0: interp_quad_coords(&src[0].fX, &dst[0].fX, t); michael@0: interp_quad_coords(&src[0].fY, &dst[0].fY, t); michael@0: } michael@0: michael@0: void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]) { michael@0: SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); michael@0: SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); michael@0: SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); michael@0: SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); michael@0: michael@0: dst[0] = src[0]; michael@0: dst[1].set(x01, y01); michael@0: dst[2].set(SkScalarAve(x01, x12), SkScalarAve(y01, y12)); michael@0: dst[3].set(x12, y12); michael@0: dst[4] = src[2]; michael@0: } michael@0: michael@0: /** Quad'(t) = At + B, where michael@0: A = 2(a - 2b + c) michael@0: B = 2(b - a) michael@0: Solve for t, only if it fits between 0 < t < 1 michael@0: */ michael@0: int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValue[1]) { michael@0: /* At + B == 0 michael@0: t = -B / A michael@0: */ michael@0: return valid_unit_divide(a - b, a - b - b + c, tValue); michael@0: } michael@0: michael@0: static inline void flatten_double_quad_extrema(SkScalar coords[14]) { michael@0: coords[2] = coords[6] = coords[4]; michael@0: } michael@0: michael@0: /* Returns 0 for 1 quad, and 1 for two quads, either way the answer is michael@0: stored in dst[]. Guarantees that the 1/2 quads will be monotonic. michael@0: */ michael@0: int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]) { michael@0: SkASSERT(src); michael@0: SkASSERT(dst); michael@0: michael@0: SkScalar a = src[0].fY; michael@0: SkScalar b = src[1].fY; michael@0: SkScalar c = src[2].fY; michael@0: michael@0: if (is_not_monotonic(a, b, c)) { michael@0: SkScalar tValue; michael@0: if (valid_unit_divide(a - b, a - b - b + c, &tValue)) { michael@0: SkChopQuadAt(src, dst, tValue); michael@0: flatten_double_quad_extrema(&dst[0].fY); michael@0: return 1; michael@0: } michael@0: // if we get here, we need to force dst to be monotonic, even though michael@0: // we couldn't compute a unit_divide value (probably underflow). michael@0: b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; michael@0: } michael@0: dst[0].set(src[0].fX, a); michael@0: dst[1].set(src[1].fX, b); michael@0: dst[2].set(src[2].fX, c); michael@0: return 0; michael@0: } michael@0: michael@0: /* Returns 0 for 1 quad, and 1 for two quads, either way the answer is michael@0: stored in dst[]. Guarantees that the 1/2 quads will be monotonic. michael@0: */ michael@0: int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]) { michael@0: SkASSERT(src); michael@0: SkASSERT(dst); michael@0: michael@0: SkScalar a = src[0].fX; michael@0: SkScalar b = src[1].fX; michael@0: SkScalar c = src[2].fX; michael@0: michael@0: if (is_not_monotonic(a, b, c)) { michael@0: SkScalar tValue; michael@0: if (valid_unit_divide(a - b, a - b - b + c, &tValue)) { michael@0: SkChopQuadAt(src, dst, tValue); michael@0: flatten_double_quad_extrema(&dst[0].fX); michael@0: return 1; michael@0: } michael@0: // if we get here, we need to force dst to be monotonic, even though michael@0: // we couldn't compute a unit_divide value (probably underflow). michael@0: b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; michael@0: } michael@0: dst[0].set(a, src[0].fY); michael@0: dst[1].set(b, src[1].fY); michael@0: dst[2].set(c, src[2].fY); michael@0: return 0; michael@0: } michael@0: michael@0: // F(t) = a (1 - t) ^ 2 + 2 b t (1 - t) + c t ^ 2 michael@0: // F'(t) = 2 (b - a) + 2 (a - 2b + c) t michael@0: // F''(t) = 2 (a - 2b + c) michael@0: // michael@0: // A = 2 (b - a) michael@0: // B = 2 (a - 2b + c) michael@0: // michael@0: // Maximum curvature for a quadratic means solving michael@0: // Fx' Fx'' + Fy' Fy'' = 0 michael@0: // michael@0: // t = - (Ax Bx + Ay By) / (Bx ^ 2 + By ^ 2) michael@0: // michael@0: SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]) { michael@0: SkScalar Ax = src[1].fX - src[0].fX; michael@0: SkScalar Ay = src[1].fY - src[0].fY; michael@0: SkScalar Bx = src[0].fX - src[1].fX - src[1].fX + src[2].fX; michael@0: SkScalar By = src[0].fY - src[1].fY - src[1].fY + src[2].fY; michael@0: SkScalar t = 0; // 0 means don't chop michael@0: michael@0: (void)valid_unit_divide(-(Ax * Bx + Ay * By), Bx * Bx + By * By, &t); michael@0: return t; michael@0: } michael@0: michael@0: int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]) { michael@0: SkScalar t = SkFindQuadMaxCurvature(src); michael@0: if (t == 0) { michael@0: memcpy(dst, src, 3 * sizeof(SkPoint)); michael@0: return 1; michael@0: } else { michael@0: SkChopQuadAt(src, dst, t); michael@0: return 2; michael@0: } michael@0: } michael@0: michael@0: #define SK_ScalarTwoThirds (0.666666666f) michael@0: michael@0: void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]) { michael@0: const SkScalar scale = SK_ScalarTwoThirds; michael@0: dst[0] = src[0]; michael@0: dst[1].set(src[0].fX + SkScalarMul(src[1].fX - src[0].fX, scale), michael@0: src[0].fY + SkScalarMul(src[1].fY - src[0].fY, scale)); michael@0: dst[2].set(src[2].fX + SkScalarMul(src[1].fX - src[2].fX, scale), michael@0: src[2].fY + SkScalarMul(src[1].fY - src[2].fY, scale)); michael@0: dst[3] = src[2]; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: ///// CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS ///// michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static void get_cubic_coeff(const SkScalar pt[], SkScalar coeff[4]) { michael@0: coeff[0] = pt[6] + 3*(pt[2] - pt[4]) - pt[0]; michael@0: coeff[1] = 3*(pt[4] - pt[2] - pt[2] + pt[0]); michael@0: coeff[2] = 3*(pt[2] - pt[0]); michael@0: coeff[3] = pt[0]; michael@0: } michael@0: michael@0: void SkGetCubicCoeff(const SkPoint pts[4], SkScalar cx[4], SkScalar cy[4]) { michael@0: SkASSERT(pts); michael@0: michael@0: if (cx) { michael@0: get_cubic_coeff(&pts[0].fX, cx); michael@0: } michael@0: if (cy) { michael@0: get_cubic_coeff(&pts[0].fY, cy); michael@0: } michael@0: } michael@0: michael@0: static SkScalar eval_cubic(const SkScalar src[], SkScalar t) { michael@0: SkASSERT(src); michael@0: SkASSERT(t >= 0 && t <= SK_Scalar1); michael@0: michael@0: if (t == 0) { michael@0: return src[0]; michael@0: } michael@0: michael@0: #ifdef DIRECT_EVAL_OF_POLYNOMIALS michael@0: SkScalar D = src[0]; michael@0: SkScalar A = src[6] + 3*(src[2] - src[4]) - D; michael@0: SkScalar B = 3*(src[4] - src[2] - src[2] + D); michael@0: SkScalar C = 3*(src[2] - D); michael@0: michael@0: return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); michael@0: #else michael@0: SkScalar ab = SkScalarInterp(src[0], src[2], t); michael@0: SkScalar bc = SkScalarInterp(src[2], src[4], t); michael@0: SkScalar cd = SkScalarInterp(src[4], src[6], t); michael@0: SkScalar abc = SkScalarInterp(ab, bc, t); michael@0: SkScalar bcd = SkScalarInterp(bc, cd, t); michael@0: return SkScalarInterp(abc, bcd, t); michael@0: #endif michael@0: } michael@0: michael@0: /** return At^2 + Bt + C michael@0: */ michael@0: static SkScalar eval_quadratic(SkScalar A, SkScalar B, SkScalar C, SkScalar t) { michael@0: SkASSERT(t >= 0 && t <= SK_Scalar1); michael@0: michael@0: return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); michael@0: } michael@0: michael@0: static SkScalar eval_cubic_derivative(const SkScalar src[], SkScalar t) { michael@0: SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0]; michael@0: SkScalar B = 2*(src[4] - 2 * src[2] + src[0]); michael@0: SkScalar C = src[2] - src[0]; michael@0: michael@0: return eval_quadratic(A, B, C, t); michael@0: } michael@0: michael@0: static SkScalar eval_cubic_2ndDerivative(const SkScalar src[], SkScalar t) { michael@0: SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0]; michael@0: SkScalar B = src[4] - 2 * src[2] + src[0]; michael@0: michael@0: return SkScalarMulAdd(A, t, B); michael@0: } michael@0: michael@0: void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* loc, michael@0: SkVector* tangent, SkVector* curvature) { michael@0: SkASSERT(src); michael@0: SkASSERT(t >= 0 && t <= SK_Scalar1); michael@0: michael@0: if (loc) { michael@0: loc->set(eval_cubic(&src[0].fX, t), eval_cubic(&src[0].fY, t)); michael@0: } michael@0: if (tangent) { michael@0: tangent->set(eval_cubic_derivative(&src[0].fX, t), michael@0: eval_cubic_derivative(&src[0].fY, t)); michael@0: } michael@0: if (curvature) { michael@0: curvature->set(eval_cubic_2ndDerivative(&src[0].fX, t), michael@0: eval_cubic_2ndDerivative(&src[0].fY, t)); michael@0: } michael@0: } michael@0: michael@0: /** Cubic'(t) = At^2 + Bt + C, where michael@0: A = 3(-a + 3(b - c) + d) michael@0: B = 6(a - 2b + c) michael@0: C = 3(b - a) michael@0: Solve for t, keeping only those that fit betwee 0 < t < 1 michael@0: */ michael@0: int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, michael@0: SkScalar tValues[2]) { michael@0: // we divide A,B,C by 3 to simplify michael@0: SkScalar A = d - a + 3*(b - c); michael@0: SkScalar B = 2*(a - b - b + c); michael@0: SkScalar C = b - a; michael@0: michael@0: return SkFindUnitQuadRoots(A, B, C, tValues); michael@0: } michael@0: michael@0: static void interp_cubic_coords(const SkScalar* src, SkScalar* dst, michael@0: SkScalar t) { michael@0: SkScalar ab = SkScalarInterp(src[0], src[2], t); michael@0: SkScalar bc = SkScalarInterp(src[2], src[4], t); michael@0: SkScalar cd = SkScalarInterp(src[4], src[6], t); michael@0: SkScalar abc = SkScalarInterp(ab, bc, t); michael@0: SkScalar bcd = SkScalarInterp(bc, cd, t); michael@0: SkScalar abcd = SkScalarInterp(abc, bcd, t); michael@0: michael@0: dst[0] = src[0]; michael@0: dst[2] = ab; michael@0: dst[4] = abc; michael@0: dst[6] = abcd; michael@0: dst[8] = bcd; michael@0: dst[10] = cd; michael@0: dst[12] = src[6]; michael@0: } michael@0: michael@0: void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t) { michael@0: SkASSERT(t > 0 && t < SK_Scalar1); michael@0: michael@0: interp_cubic_coords(&src[0].fX, &dst[0].fX, t); michael@0: interp_cubic_coords(&src[0].fY, &dst[0].fY, t); michael@0: } michael@0: michael@0: /* http://code.google.com/p/skia/issues/detail?id=32 michael@0: michael@0: This test code would fail when we didn't check the return result of michael@0: valid_unit_divide in SkChopCubicAt(... tValues[], int roots). The reason is michael@0: that after the first chop, the parameters to valid_unit_divide are equal michael@0: (thanks to finite float precision and rounding in the subtracts). Thus michael@0: even though the 2nd tValue looks < 1.0, after we renormalize it, we end michael@0: up with 1.0, hence the need to check and just return the last cubic as michael@0: a degenerate clump of 4 points in the sampe place. michael@0: michael@0: static void test_cubic() { michael@0: SkPoint src[4] = { michael@0: { 556.25000, 523.03003 }, michael@0: { 556.23999, 522.96002 }, michael@0: { 556.21997, 522.89001 }, michael@0: { 556.21997, 522.82001 } michael@0: }; michael@0: SkPoint dst[10]; michael@0: SkScalar tval[] = { 0.33333334f, 0.99999994f }; michael@0: SkChopCubicAt(src, dst, tval, 2); michael@0: } michael@0: */ michael@0: michael@0: void SkChopCubicAt(const SkPoint src[4], SkPoint dst[], michael@0: const SkScalar tValues[], int roots) { michael@0: #ifdef SK_DEBUG michael@0: { michael@0: for (int i = 0; i < roots - 1; i++) michael@0: { michael@0: SkASSERT(is_unit_interval(tValues[i])); michael@0: SkASSERT(is_unit_interval(tValues[i+1])); michael@0: SkASSERT(tValues[i] < tValues[i+1]); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (dst) { michael@0: if (roots == 0) { // nothing to chop michael@0: memcpy(dst, src, 4*sizeof(SkPoint)); michael@0: } else { michael@0: SkScalar t = tValues[0]; michael@0: SkPoint tmp[4]; michael@0: michael@0: for (int i = 0; i < roots; i++) { michael@0: SkChopCubicAt(src, dst, t); michael@0: if (i == roots - 1) { michael@0: break; michael@0: } michael@0: michael@0: dst += 3; michael@0: // have src point to the remaining cubic (after the chop) michael@0: memcpy(tmp, dst, 4 * sizeof(SkPoint)); michael@0: src = tmp; michael@0: michael@0: // watch out in case the renormalized t isn't in range michael@0: if (!valid_unit_divide(tValues[i+1] - tValues[i], michael@0: SK_Scalar1 - tValues[i], &t)) { michael@0: // if we can't, just create a degenerate cubic michael@0: dst[4] = dst[5] = dst[6] = src[3]; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]) { michael@0: SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); michael@0: SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); michael@0: SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); michael@0: SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); michael@0: SkScalar x23 = SkScalarAve(src[2].fX, src[3].fX); michael@0: SkScalar y23 = SkScalarAve(src[2].fY, src[3].fY); michael@0: michael@0: SkScalar x012 = SkScalarAve(x01, x12); michael@0: SkScalar y012 = SkScalarAve(y01, y12); michael@0: SkScalar x123 = SkScalarAve(x12, x23); michael@0: SkScalar y123 = SkScalarAve(y12, y23); michael@0: michael@0: dst[0] = src[0]; michael@0: dst[1].set(x01, y01); michael@0: dst[2].set(x012, y012); michael@0: dst[3].set(SkScalarAve(x012, x123), SkScalarAve(y012, y123)); michael@0: dst[4].set(x123, y123); michael@0: dst[5].set(x23, y23); michael@0: dst[6] = src[3]; michael@0: } michael@0: michael@0: static void flatten_double_cubic_extrema(SkScalar coords[14]) { michael@0: coords[4] = coords[8] = coords[6]; michael@0: } michael@0: michael@0: /** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that michael@0: the resulting beziers are monotonic in Y. This is called by the scan michael@0: converter. Depending on what is returned, dst[] is treated as follows: michael@0: 0 dst[0..3] is the original cubic michael@0: 1 dst[0..3] and dst[3..6] are the two new cubics michael@0: 2 dst[0..3], dst[3..6], dst[6..9] are the three new cubics michael@0: If dst == null, it is ignored and only the count is returned. michael@0: */ michael@0: int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]) { michael@0: SkScalar tValues[2]; michael@0: int roots = SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY, michael@0: src[3].fY, tValues); michael@0: michael@0: SkChopCubicAt(src, dst, tValues, roots); michael@0: if (dst && roots > 0) { michael@0: // we do some cleanup to ensure our Y extrema are flat michael@0: flatten_double_cubic_extrema(&dst[0].fY); michael@0: if (roots == 2) { michael@0: flatten_double_cubic_extrema(&dst[3].fY); michael@0: } michael@0: } michael@0: return roots; michael@0: } michael@0: michael@0: int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]) { michael@0: SkScalar tValues[2]; michael@0: int roots = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX, michael@0: src[3].fX, tValues); michael@0: michael@0: SkChopCubicAt(src, dst, tValues, roots); michael@0: if (dst && roots > 0) { michael@0: // we do some cleanup to ensure our Y extrema are flat michael@0: flatten_double_cubic_extrema(&dst[0].fX); michael@0: if (roots == 2) { michael@0: flatten_double_cubic_extrema(&dst[3].fX); michael@0: } michael@0: } michael@0: return roots; michael@0: } michael@0: michael@0: /** http://www.faculty.idc.ac.il/arik/quality/appendixA.html michael@0: michael@0: Inflection means that curvature is zero. michael@0: Curvature is [F' x F''] / [F'^3] michael@0: So we solve F'x X F''y - F'y X F''y == 0 michael@0: After some canceling of the cubic term, we get michael@0: A = b - a michael@0: B = c - 2b + a michael@0: C = d - 3c + 3b - a michael@0: (BxCy - ByCx)t^2 + (AxCy - AyCx)t + AxBy - AyBx == 0 michael@0: */ michael@0: int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[]) { michael@0: SkScalar Ax = src[1].fX - src[0].fX; michael@0: SkScalar Ay = src[1].fY - src[0].fY; michael@0: SkScalar Bx = src[2].fX - 2 * src[1].fX + src[0].fX; michael@0: SkScalar By = src[2].fY - 2 * src[1].fY + src[0].fY; michael@0: SkScalar Cx = src[3].fX + 3 * (src[1].fX - src[2].fX) - src[0].fX; michael@0: SkScalar Cy = src[3].fY + 3 * (src[1].fY - src[2].fY) - src[0].fY; michael@0: michael@0: return SkFindUnitQuadRoots(Bx*Cy - By*Cx, michael@0: Ax*Cy - Ay*Cx, michael@0: Ax*By - Ay*Bx, michael@0: tValues); michael@0: } michael@0: michael@0: int SkChopCubicAtInflections(const SkPoint src[], SkPoint dst[10]) { michael@0: SkScalar tValues[2]; michael@0: int count = SkFindCubicInflections(src, tValues); michael@0: michael@0: if (dst) { michael@0: if (count == 0) { michael@0: memcpy(dst, src, 4 * sizeof(SkPoint)); michael@0: } else { michael@0: SkChopCubicAt(src, dst, tValues, count); michael@0: } michael@0: } michael@0: return count + 1; michael@0: } michael@0: michael@0: template void bubble_sort(T array[], int count) { michael@0: for (int i = count - 1; i > 0; --i) michael@0: for (int j = i; j > 0; --j) michael@0: if (array[j] < array[j-1]) michael@0: { michael@0: T tmp(array[j]); michael@0: array[j] = array[j-1]; michael@0: array[j-1] = tmp; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Given an array and count, remove all pair-wise duplicates from the array, michael@0: * keeping the existing sorting, and return the new count michael@0: */ michael@0: static int collaps_duplicates(SkScalar array[], int count) { michael@0: for (int n = count; n > 1; --n) { michael@0: if (array[0] == array[1]) { michael@0: for (int i = 1; i < n; ++i) { michael@0: array[i - 1] = array[i]; michael@0: } michael@0: count -= 1; michael@0: } else { michael@0: array += 1; michael@0: } michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: michael@0: #define TEST_COLLAPS_ENTRY(array) array, SK_ARRAY_COUNT(array) michael@0: michael@0: static void test_collaps_duplicates() { michael@0: static bool gOnce; michael@0: if (gOnce) { return; } michael@0: gOnce = true; michael@0: const SkScalar src0[] = { 0 }; michael@0: const SkScalar src1[] = { 0, 0 }; michael@0: const SkScalar src2[] = { 0, 1 }; michael@0: const SkScalar src3[] = { 0, 0, 0 }; michael@0: const SkScalar src4[] = { 0, 0, 1 }; michael@0: const SkScalar src5[] = { 0, 1, 1 }; michael@0: const SkScalar src6[] = { 0, 1, 2 }; michael@0: const struct { michael@0: const SkScalar* fData; michael@0: int fCount; michael@0: int fCollapsedCount; michael@0: } data[] = { michael@0: { TEST_COLLAPS_ENTRY(src0), 1 }, michael@0: { TEST_COLLAPS_ENTRY(src1), 1 }, michael@0: { TEST_COLLAPS_ENTRY(src2), 2 }, michael@0: { TEST_COLLAPS_ENTRY(src3), 1 }, michael@0: { TEST_COLLAPS_ENTRY(src4), 2 }, michael@0: { TEST_COLLAPS_ENTRY(src5), 2 }, michael@0: { TEST_COLLAPS_ENTRY(src6), 3 }, michael@0: }; michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { michael@0: SkScalar dst[3]; michael@0: memcpy(dst, data[i].fData, data[i].fCount * sizeof(dst[0])); michael@0: int count = collaps_duplicates(dst, data[i].fCount); michael@0: SkASSERT(data[i].fCollapsedCount == count); michael@0: for (int j = 1; j < count; ++j) { michael@0: SkASSERT(dst[j-1] < dst[j]); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: static SkScalar SkScalarCubeRoot(SkScalar x) { michael@0: return SkScalarPow(x, 0.3333333f); michael@0: } michael@0: michael@0: /* Solve coeff(t) == 0, returning the number of roots that michael@0: lie withing 0 < t < 1. michael@0: coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3] michael@0: michael@0: Eliminates repeated roots (so that all tValues are distinct, and are always michael@0: in increasing order. michael@0: */ michael@0: static int solve_cubic_poly(const SkScalar coeff[4], SkScalar tValues[3]) { michael@0: if (SkScalarNearlyZero(coeff[0])) { // we're just a quadratic michael@0: return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues); michael@0: } michael@0: michael@0: SkScalar a, b, c, Q, R; michael@0: michael@0: { michael@0: SkASSERT(coeff[0] != 0); michael@0: michael@0: SkScalar inva = SkScalarInvert(coeff[0]); michael@0: a = coeff[1] * inva; michael@0: b = coeff[2] * inva; michael@0: c = coeff[3] * inva; michael@0: } michael@0: Q = (a*a - b*3) / 9; michael@0: R = (2*a*a*a - 9*a*b + 27*c) / 54; michael@0: michael@0: SkScalar Q3 = Q * Q * Q; michael@0: SkScalar R2MinusQ3 = R * R - Q3; michael@0: SkScalar adiv3 = a / 3; michael@0: michael@0: SkScalar* roots = tValues; michael@0: SkScalar r; michael@0: michael@0: if (R2MinusQ3 < 0) { // we have 3 real roots michael@0: SkScalar theta = SkScalarACos(R / SkScalarSqrt(Q3)); michael@0: SkScalar neg2RootQ = -2 * SkScalarSqrt(Q); michael@0: michael@0: r = neg2RootQ * SkScalarCos(theta/3) - adiv3; michael@0: if (is_unit_interval(r)) { michael@0: *roots++ = r; michael@0: } michael@0: r = neg2RootQ * SkScalarCos((theta + 2*SK_ScalarPI)/3) - adiv3; michael@0: if (is_unit_interval(r)) { michael@0: *roots++ = r; michael@0: } michael@0: r = neg2RootQ * SkScalarCos((theta - 2*SK_ScalarPI)/3) - adiv3; michael@0: if (is_unit_interval(r)) { michael@0: *roots++ = r; michael@0: } michael@0: SkDEBUGCODE(test_collaps_duplicates();) michael@0: michael@0: // now sort the roots michael@0: int count = (int)(roots - tValues); michael@0: SkASSERT((unsigned)count <= 3); michael@0: bubble_sort(tValues, count); michael@0: count = collaps_duplicates(tValues, count); michael@0: roots = tValues + count; // so we compute the proper count below michael@0: } else { // we have 1 real root michael@0: SkScalar A = SkScalarAbs(R) + SkScalarSqrt(R2MinusQ3); michael@0: A = SkScalarCubeRoot(A); michael@0: if (R > 0) { michael@0: A = -A; michael@0: } michael@0: if (A != 0) { michael@0: A += Q / A; michael@0: } michael@0: r = A - adiv3; michael@0: if (is_unit_interval(r)) { michael@0: *roots++ = r; michael@0: } michael@0: } michael@0: michael@0: return (int)(roots - tValues); michael@0: } michael@0: michael@0: /* Looking for F' dot F'' == 0 michael@0: michael@0: A = b - a michael@0: B = c - 2b + a michael@0: C = d - 3c + 3b - a michael@0: michael@0: F' = 3Ct^2 + 6Bt + 3A michael@0: F'' = 6Ct + 6B michael@0: michael@0: F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB michael@0: */ michael@0: static void formulate_F1DotF2(const SkScalar src[], SkScalar coeff[4]) { michael@0: SkScalar a = src[2] - src[0]; michael@0: SkScalar b = src[4] - 2 * src[2] + src[0]; michael@0: SkScalar c = src[6] + 3 * (src[2] - src[4]) - src[0]; michael@0: michael@0: coeff[0] = c * c; michael@0: coeff[1] = 3 * b * c; michael@0: coeff[2] = 2 * b * b + c * a; michael@0: coeff[3] = a * b; michael@0: } michael@0: michael@0: /* Looking for F' dot F'' == 0 michael@0: michael@0: A = b - a michael@0: B = c - 2b + a michael@0: C = d - 3c + 3b - a michael@0: michael@0: F' = 3Ct^2 + 6Bt + 3A michael@0: F'' = 6Ct + 6B michael@0: michael@0: F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB michael@0: */ michael@0: int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]) { michael@0: SkScalar coeffX[4], coeffY[4]; michael@0: int i; michael@0: michael@0: formulate_F1DotF2(&src[0].fX, coeffX); michael@0: formulate_F1DotF2(&src[0].fY, coeffY); michael@0: michael@0: for (i = 0; i < 4; i++) { michael@0: coeffX[i] += coeffY[i]; michael@0: } michael@0: michael@0: SkScalar t[3]; michael@0: int count = solve_cubic_poly(coeffX, t); michael@0: int maxCount = 0; michael@0: michael@0: // now remove extrema where the curvature is zero (mins) michael@0: // !!!! need a test for this !!!! michael@0: for (i = 0; i < count; i++) { michael@0: // if (not_min_curvature()) michael@0: if (t[i] > 0 && t[i] < SK_Scalar1) { michael@0: tValues[maxCount++] = t[i]; michael@0: } michael@0: } michael@0: return maxCount; michael@0: } michael@0: michael@0: int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], michael@0: SkScalar tValues[3]) { michael@0: SkScalar t_storage[3]; michael@0: michael@0: if (tValues == NULL) { michael@0: tValues = t_storage; michael@0: } michael@0: michael@0: int count = SkFindCubicMaxCurvature(src, tValues); michael@0: michael@0: if (dst) { michael@0: if (count == 0) { michael@0: memcpy(dst, src, 4 * sizeof(SkPoint)); michael@0: } else { michael@0: SkChopCubicAt(src, dst, tValues, count); michael@0: } michael@0: } michael@0: return count + 1; michael@0: } michael@0: michael@0: bool SkXRayCrossesMonotonicCubic(const SkXRay& pt, const SkPoint cubic[4], michael@0: bool* ambiguous) { michael@0: if (ambiguous) { michael@0: *ambiguous = false; michael@0: } michael@0: michael@0: // Find the minimum and maximum y of the extrema, which are the michael@0: // first and last points since this cubic is monotonic michael@0: SkScalar min_y = SkMinScalar(cubic[0].fY, cubic[3].fY); michael@0: SkScalar max_y = SkMaxScalar(cubic[0].fY, cubic[3].fY); michael@0: michael@0: if (pt.fY == cubic[0].fY michael@0: || pt.fY < min_y michael@0: || pt.fY > max_y) { michael@0: // The query line definitely does not cross the curve michael@0: if (ambiguous) { michael@0: *ambiguous = (pt.fY == cubic[0].fY); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool pt_at_extremum = (pt.fY == cubic[3].fY); michael@0: michael@0: SkScalar min_x = michael@0: SkMinScalar( michael@0: SkMinScalar( michael@0: SkMinScalar(cubic[0].fX, cubic[1].fX), michael@0: cubic[2].fX), michael@0: cubic[3].fX); michael@0: if (pt.fX < min_x) { michael@0: // The query line definitely crosses the curve michael@0: if (ambiguous) { michael@0: *ambiguous = pt_at_extremum; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: SkScalar max_x = michael@0: SkMaxScalar( michael@0: SkMaxScalar( michael@0: SkMaxScalar(cubic[0].fX, cubic[1].fX), michael@0: cubic[2].fX), michael@0: cubic[3].fX); michael@0: if (pt.fX > max_x) { michael@0: // The query line definitely does not cross the curve michael@0: return false; michael@0: } michael@0: michael@0: // Do a binary search to find the parameter value which makes y as michael@0: // close as possible to the query point. See whether the query michael@0: // line's origin is to the left of the associated x coordinate. michael@0: michael@0: // kMaxIter is chosen as the number of mantissa bits for a float, michael@0: // since there's no way we are going to get more precision by michael@0: // iterating more times than that. michael@0: const int kMaxIter = 23; michael@0: SkPoint eval; michael@0: int iter = 0; michael@0: SkScalar upper_t; michael@0: SkScalar lower_t; michael@0: // Need to invert direction of t parameter if cubic goes up michael@0: // instead of down michael@0: if (cubic[3].fY > cubic[0].fY) { michael@0: upper_t = SK_Scalar1; michael@0: lower_t = 0; michael@0: } else { michael@0: upper_t = 0; michael@0: lower_t = SK_Scalar1; michael@0: } michael@0: do { michael@0: SkScalar t = SkScalarAve(upper_t, lower_t); michael@0: SkEvalCubicAt(cubic, t, &eval, NULL, NULL); michael@0: if (pt.fY > eval.fY) { michael@0: lower_t = t; michael@0: } else { michael@0: upper_t = t; michael@0: } michael@0: } while (++iter < kMaxIter michael@0: && !SkScalarNearlyZero(eval.fY - pt.fY)); michael@0: if (pt.fX <= eval.fX) { michael@0: if (ambiguous) { michael@0: *ambiguous = pt_at_extremum; michael@0: } michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: int SkNumXRayCrossingsForCubic(const SkXRay& pt, michael@0: const SkPoint cubic[4], michael@0: bool* ambiguous) { michael@0: int num_crossings = 0; michael@0: SkPoint monotonic_cubics[10]; michael@0: int num_monotonic_cubics = SkChopCubicAtYExtrema(cubic, monotonic_cubics); michael@0: if (ambiguous) { michael@0: *ambiguous = false; michael@0: } michael@0: bool locally_ambiguous; michael@0: if (SkXRayCrossesMonotonicCubic(pt, michael@0: &monotonic_cubics[0], michael@0: &locally_ambiguous)) michael@0: ++num_crossings; michael@0: if (ambiguous) { michael@0: *ambiguous |= locally_ambiguous; michael@0: } michael@0: if (num_monotonic_cubics > 0) michael@0: if (SkXRayCrossesMonotonicCubic(pt, michael@0: &monotonic_cubics[3], michael@0: &locally_ambiguous)) michael@0: ++num_crossings; michael@0: if (ambiguous) { michael@0: *ambiguous |= locally_ambiguous; michael@0: } michael@0: if (num_monotonic_cubics > 1) michael@0: if (SkXRayCrossesMonotonicCubic(pt, michael@0: &monotonic_cubics[6], michael@0: &locally_ambiguous)) michael@0: ++num_crossings; michael@0: if (ambiguous) { michael@0: *ambiguous |= locally_ambiguous; michael@0: } michael@0: return num_crossings; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* Find t value for quadratic [a, b, c] = d. michael@0: Return 0 if there is no solution within [0, 1) michael@0: */ michael@0: static SkScalar quad_solve(SkScalar a, SkScalar b, SkScalar c, SkScalar d) { michael@0: // At^2 + Bt + C = d michael@0: SkScalar A = a - 2 * b + c; michael@0: SkScalar B = 2 * (b - a); michael@0: SkScalar C = a - d; michael@0: michael@0: SkScalar roots[2]; michael@0: int count = SkFindUnitQuadRoots(A, B, C, roots); michael@0: michael@0: SkASSERT(count <= 1); michael@0: return count == 1 ? roots[0] : 0; michael@0: } michael@0: michael@0: /* given a quad-curve and a point (x,y), chop the quad at that point and place michael@0: the new off-curve point and endpoint into 'dest'. michael@0: Should only return false if the computed pos is the start of the curve michael@0: (i.e. root == 0) michael@0: */ michael@0: static bool truncate_last_curve(const SkPoint quad[3], SkScalar x, SkScalar y, michael@0: SkPoint* dest) { michael@0: const SkScalar* base; michael@0: SkScalar value; michael@0: michael@0: if (SkScalarAbs(x) < SkScalarAbs(y)) { michael@0: base = &quad[0].fX; michael@0: value = x; michael@0: } else { michael@0: base = &quad[0].fY; michael@0: value = y; michael@0: } michael@0: michael@0: // note: this returns 0 if it thinks value is out of range, meaning the michael@0: // root might return something outside of [0, 1) michael@0: SkScalar t = quad_solve(base[0], base[2], base[4], value); michael@0: michael@0: if (t > 0) { michael@0: SkPoint tmp[5]; michael@0: SkChopQuadAt(quad, tmp, t); michael@0: dest[0] = tmp[1]; michael@0: dest[1].set(x, y); michael@0: return true; michael@0: } else { michael@0: /* t == 0 means either the value triggered a root outside of [0, 1) michael@0: For our purposes, we can ignore the <= 0 roots, but we want to michael@0: catch the >= 1 roots (which given our caller, will basically mean michael@0: a root of 1, give-or-take numerical instability). If we are in the michael@0: >= 1 case, return the existing offCurve point. michael@0: michael@0: The test below checks to see if we are close to the "end" of the michael@0: curve (near base[4]). Rather than specifying a tolerance, I just michael@0: check to see if value is on to the right/left of the middle point michael@0: (depending on the direction/sign of the end points). michael@0: */ michael@0: if ((base[0] < base[4] && value > base[2]) || michael@0: (base[0] > base[4] && value < base[2])) // should root have been 1 michael@0: { michael@0: dest[0] = quad[1]; michael@0: dest[1].set(x, y); michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static const SkPoint gQuadCirclePts[kSkBuildQuadArcStorage] = { michael@0: // The mid point of the quadratic arc approximation is half way between the two michael@0: // control points. The float epsilon adjustment moves the on curve point out by michael@0: // two bits, distributing the convex test error between the round rect michael@0: // approximation and the convex cross product sign equality test. michael@0: #define SK_MID_RRECT_OFFSET \ michael@0: (SK_Scalar1 + SK_ScalarTanPIOver8 + FLT_EPSILON * 4) / 2 michael@0: { SK_Scalar1, 0 }, michael@0: { SK_Scalar1, SK_ScalarTanPIOver8 }, michael@0: { SK_MID_RRECT_OFFSET, SK_MID_RRECT_OFFSET }, michael@0: { SK_ScalarTanPIOver8, SK_Scalar1 }, michael@0: michael@0: { 0, SK_Scalar1 }, michael@0: { -SK_ScalarTanPIOver8, SK_Scalar1 }, michael@0: { -SK_MID_RRECT_OFFSET, SK_MID_RRECT_OFFSET }, michael@0: { -SK_Scalar1, SK_ScalarTanPIOver8 }, michael@0: michael@0: { -SK_Scalar1, 0 }, michael@0: { -SK_Scalar1, -SK_ScalarTanPIOver8 }, michael@0: { -SK_MID_RRECT_OFFSET, -SK_MID_RRECT_OFFSET }, michael@0: { -SK_ScalarTanPIOver8, -SK_Scalar1 }, michael@0: michael@0: { 0, -SK_Scalar1 }, michael@0: { SK_ScalarTanPIOver8, -SK_Scalar1 }, michael@0: { SK_MID_RRECT_OFFSET, -SK_MID_RRECT_OFFSET }, michael@0: { SK_Scalar1, -SK_ScalarTanPIOver8 }, michael@0: michael@0: { SK_Scalar1, 0 } michael@0: #undef SK_MID_RRECT_OFFSET michael@0: }; michael@0: michael@0: int SkBuildQuadArc(const SkVector& uStart, const SkVector& uStop, michael@0: SkRotationDirection dir, const SkMatrix* userMatrix, michael@0: SkPoint quadPoints[]) { michael@0: // rotate by x,y so that uStart is (1.0) michael@0: SkScalar x = SkPoint::DotProduct(uStart, uStop); michael@0: SkScalar y = SkPoint::CrossProduct(uStart, uStop); michael@0: michael@0: SkScalar absX = SkScalarAbs(x); michael@0: SkScalar absY = SkScalarAbs(y); michael@0: michael@0: int pointCount; michael@0: michael@0: // check for (effectively) coincident vectors michael@0: // this can happen if our angle is nearly 0 or nearly 180 (y == 0) michael@0: // ... we use the dot-prod to distinguish between 0 and 180 (x > 0) michael@0: if (absY <= SK_ScalarNearlyZero && x > 0 && michael@0: ((y >= 0 && kCW_SkRotationDirection == dir) || michael@0: (y <= 0 && kCCW_SkRotationDirection == dir))) { michael@0: michael@0: // just return the start-point michael@0: quadPoints[0].set(SK_Scalar1, 0); michael@0: pointCount = 1; michael@0: } else { michael@0: if (dir == kCCW_SkRotationDirection) { michael@0: y = -y; michael@0: } michael@0: // what octant (quadratic curve) is [xy] in? michael@0: int oct = 0; michael@0: bool sameSign = true; michael@0: michael@0: if (0 == y) { michael@0: oct = 4; // 180 michael@0: SkASSERT(SkScalarAbs(x + SK_Scalar1) <= SK_ScalarNearlyZero); michael@0: } else if (0 == x) { michael@0: SkASSERT(absY - SK_Scalar1 <= SK_ScalarNearlyZero); michael@0: oct = y > 0 ? 2 : 6; // 90 : 270 michael@0: } else { michael@0: if (y < 0) { michael@0: oct += 4; michael@0: } michael@0: if ((x < 0) != (y < 0)) { michael@0: oct += 2; michael@0: sameSign = false; michael@0: } michael@0: if ((absX < absY) == sameSign) { michael@0: oct += 1; michael@0: } michael@0: } michael@0: michael@0: int wholeCount = oct << 1; michael@0: memcpy(quadPoints, gQuadCirclePts, (wholeCount + 1) * sizeof(SkPoint)); michael@0: michael@0: const SkPoint* arc = &gQuadCirclePts[wholeCount]; michael@0: if (truncate_last_curve(arc, x, y, &quadPoints[wholeCount + 1])) { michael@0: wholeCount += 2; michael@0: } michael@0: pointCount = wholeCount + 1; michael@0: } michael@0: michael@0: // now handle counter-clockwise and the initial unitStart rotation michael@0: SkMatrix matrix; michael@0: matrix.setSinCos(uStart.fY, uStart.fX); michael@0: if (dir == kCCW_SkRotationDirection) { michael@0: matrix.preScale(SK_Scalar1, -SK_Scalar1); michael@0: } michael@0: if (userMatrix) { michael@0: matrix.postConcat(*userMatrix); michael@0: } michael@0: matrix.mapPoints(quadPoints, pointCount); michael@0: return pointCount; michael@0: } michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // NURB representation for conics. Helpful explanations at: michael@0: // michael@0: // http://citeseerx.ist.psu.edu/viewdoc/ michael@0: // download?doi=10.1.1.44.5740&rep=rep1&type=ps michael@0: // and michael@0: // http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html michael@0: // michael@0: // F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w) michael@0: // ------------------------------------------ michael@0: // ((1 - t)^2 + t^2 + 2 (1 - t) t w) michael@0: // michael@0: // = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0} michael@0: // ------------------------------------------------ michael@0: // {t^2 (2 - 2 w), t (-2 + 2 w), 1} michael@0: // michael@0: michael@0: static SkScalar conic_eval_pos(const SkScalar src[], SkScalar w, SkScalar t) { michael@0: SkASSERT(src); michael@0: SkASSERT(t >= 0 && t <= SK_Scalar1); michael@0: michael@0: SkScalar src2w = SkScalarMul(src[2], w); michael@0: SkScalar C = src[0]; michael@0: SkScalar A = src[4] - 2 * src2w + C; michael@0: SkScalar B = 2 * (src2w - C); michael@0: SkScalar numer = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); michael@0: michael@0: B = 2 * (w - SK_Scalar1); michael@0: C = SK_Scalar1; michael@0: A = -B; michael@0: SkScalar denom = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); michael@0: michael@0: return SkScalarDiv(numer, denom); michael@0: } michael@0: michael@0: // F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w) michael@0: // michael@0: // t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w) michael@0: // t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w) michael@0: // t^0 : -2 P0 w + 2 P1 w michael@0: // michael@0: // We disregard magnitude, so we can freely ignore the denominator of F', and michael@0: // divide the numerator by 2 michael@0: // michael@0: // coeff[0] for t^2 michael@0: // coeff[1] for t^1 michael@0: // coeff[2] for t^0 michael@0: // michael@0: static void conic_deriv_coeff(const SkScalar src[], michael@0: SkScalar w, michael@0: SkScalar coeff[3]) { michael@0: const SkScalar P20 = src[4] - src[0]; michael@0: const SkScalar P10 = src[2] - src[0]; michael@0: const SkScalar wP10 = w * P10; michael@0: coeff[0] = w * P20 - P20; michael@0: coeff[1] = P20 - 2 * wP10; michael@0: coeff[2] = wP10; michael@0: } michael@0: michael@0: static SkScalar conic_eval_tan(const SkScalar coord[], SkScalar w, SkScalar t) { michael@0: SkScalar coeff[3]; michael@0: conic_deriv_coeff(coord, w, coeff); michael@0: return t * (t * coeff[0] + coeff[1]) + coeff[2]; michael@0: } michael@0: michael@0: static bool conic_find_extrema(const SkScalar src[], SkScalar w, SkScalar* t) { michael@0: SkScalar coeff[3]; michael@0: conic_deriv_coeff(src, w, coeff); michael@0: michael@0: SkScalar tValues[2]; michael@0: int roots = SkFindUnitQuadRoots(coeff[0], coeff[1], coeff[2], tValues); michael@0: SkASSERT(0 == roots || 1 == roots); michael@0: michael@0: if (1 == roots) { michael@0: *t = tValues[0]; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: struct SkP3D { michael@0: SkScalar fX, fY, fZ; michael@0: michael@0: void set(SkScalar x, SkScalar y, SkScalar z) { michael@0: fX = x; fY = y; fZ = z; michael@0: } michael@0: michael@0: void projectDown(SkPoint* dst) const { michael@0: dst->set(fX / fZ, fY / fZ); michael@0: } michael@0: }; michael@0: michael@0: // We only interpolate one dimension at a time (the first, at +0, +3, +6). michael@0: static void p3d_interp(const SkScalar src[7], SkScalar dst[7], SkScalar t) { michael@0: SkScalar ab = SkScalarInterp(src[0], src[3], t); michael@0: SkScalar bc = SkScalarInterp(src[3], src[6], t); michael@0: dst[0] = ab; michael@0: dst[3] = SkScalarInterp(ab, bc, t); michael@0: dst[6] = bc; michael@0: } michael@0: michael@0: static void ratquad_mapTo3D(const SkPoint src[3], SkScalar w, SkP3D dst[]) { michael@0: dst[0].set(src[0].fX * 1, src[0].fY * 1, 1); michael@0: dst[1].set(src[1].fX * w, src[1].fY * w, w); michael@0: dst[2].set(src[2].fX * 1, src[2].fY * 1, 1); michael@0: } michael@0: michael@0: void SkConic::evalAt(SkScalar t, SkPoint* pt, SkVector* tangent) const { michael@0: SkASSERT(t >= 0 && t <= SK_Scalar1); michael@0: michael@0: if (pt) { michael@0: pt->set(conic_eval_pos(&fPts[0].fX, fW, t), michael@0: conic_eval_pos(&fPts[0].fY, fW, t)); michael@0: } michael@0: if (tangent) { michael@0: tangent->set(conic_eval_tan(&fPts[0].fX, fW, t), michael@0: conic_eval_tan(&fPts[0].fY, fW, t)); michael@0: } michael@0: } michael@0: michael@0: void SkConic::chopAt(SkScalar t, SkConic dst[2]) const { michael@0: SkP3D tmp[3], tmp2[3]; michael@0: michael@0: ratquad_mapTo3D(fPts, fW, tmp); michael@0: michael@0: p3d_interp(&tmp[0].fX, &tmp2[0].fX, t); michael@0: p3d_interp(&tmp[0].fY, &tmp2[0].fY, t); michael@0: p3d_interp(&tmp[0].fZ, &tmp2[0].fZ, t); michael@0: michael@0: dst[0].fPts[0] = fPts[0]; michael@0: tmp2[0].projectDown(&dst[0].fPts[1]); michael@0: tmp2[1].projectDown(&dst[0].fPts[2]); dst[1].fPts[0] = dst[0].fPts[2]; michael@0: tmp2[2].projectDown(&dst[1].fPts[1]); michael@0: dst[1].fPts[2] = fPts[2]; michael@0: michael@0: // to put in "standard form", where w0 and w2 are both 1, we compute the michael@0: // new w1 as sqrt(w1*w1/w0*w2) michael@0: // or michael@0: // w1 /= sqrt(w0*w2) michael@0: // michael@0: // However, in our case, we know that for dst[0]: michael@0: // w0 == 1, and for dst[1], w2 == 1 michael@0: // michael@0: SkScalar root = SkScalarSqrt(tmp2[1].fZ); michael@0: dst[0].fW = tmp2[0].fZ / root; michael@0: dst[1].fW = tmp2[2].fZ / root; michael@0: } michael@0: michael@0: static SkScalar subdivide_w_value(SkScalar w) { michael@0: return SkScalarSqrt(SK_ScalarHalf + w * SK_ScalarHalf); michael@0: } michael@0: michael@0: void SkConic::chop(SkConic dst[2]) const { michael@0: SkScalar scale = SkScalarInvert(SK_Scalar1 + fW); michael@0: SkScalar p1x = fW * fPts[1].fX; michael@0: SkScalar p1y = fW * fPts[1].fY; michael@0: SkScalar mx = (fPts[0].fX + 2 * p1x + fPts[2].fX) * scale * SK_ScalarHalf; michael@0: SkScalar my = (fPts[0].fY + 2 * p1y + fPts[2].fY) * scale * SK_ScalarHalf; michael@0: michael@0: dst[0].fPts[0] = fPts[0]; michael@0: dst[0].fPts[1].set((fPts[0].fX + p1x) * scale, michael@0: (fPts[0].fY + p1y) * scale); michael@0: dst[0].fPts[2].set(mx, my); michael@0: michael@0: dst[1].fPts[0].set(mx, my); michael@0: dst[1].fPts[1].set((p1x + fPts[2].fX) * scale, michael@0: (p1y + fPts[2].fY) * scale); michael@0: dst[1].fPts[2] = fPts[2]; michael@0: michael@0: dst[0].fW = dst[1].fW = subdivide_w_value(fW); michael@0: } michael@0: michael@0: /* michael@0: * "High order approximation of conic sections by quadratic splines" michael@0: * by Michael Floater, 1993 michael@0: */ michael@0: #define AS_QUAD_ERROR_SETUP \ michael@0: SkScalar a = fW - 1; \ michael@0: SkScalar k = a / (4 * (2 + a)); \ michael@0: SkScalar x = k * (fPts[0].fX - 2 * fPts[1].fX + fPts[2].fX); \ michael@0: SkScalar y = k * (fPts[0].fY - 2 * fPts[1].fY + fPts[2].fY); michael@0: michael@0: void SkConic::computeAsQuadError(SkVector* err) const { michael@0: AS_QUAD_ERROR_SETUP michael@0: err->set(x, y); michael@0: } michael@0: michael@0: bool SkConic::asQuadTol(SkScalar tol) const { michael@0: AS_QUAD_ERROR_SETUP michael@0: return (x * x + y * y) <= tol * tol; michael@0: } michael@0: michael@0: int SkConic::computeQuadPOW2(SkScalar tol) const { michael@0: AS_QUAD_ERROR_SETUP michael@0: SkScalar error = SkScalarSqrt(x * x + y * y) - tol; michael@0: michael@0: if (error <= 0) { michael@0: return 0; michael@0: } michael@0: uint32_t ierr = (uint32_t)error; michael@0: return (34 - SkCLZ(ierr)) >> 1; michael@0: } michael@0: michael@0: static SkPoint* subdivide(const SkConic& src, SkPoint pts[], int level) { michael@0: SkASSERT(level >= 0); michael@0: michael@0: if (0 == level) { michael@0: memcpy(pts, &src.fPts[1], 2 * sizeof(SkPoint)); michael@0: return pts + 2; michael@0: } else { michael@0: SkConic dst[2]; michael@0: src.chop(dst); michael@0: --level; michael@0: pts = subdivide(dst[0], pts, level); michael@0: return subdivide(dst[1], pts, level); michael@0: } michael@0: } michael@0: michael@0: int SkConic::chopIntoQuadsPOW2(SkPoint pts[], int pow2) const { michael@0: SkASSERT(pow2 >= 0); michael@0: *pts = fPts[0]; michael@0: SkDEBUGCODE(SkPoint* endPts =) subdivide(*this, pts + 1, pow2); michael@0: SkASSERT(endPts - pts == (2 * (1 << pow2) + 1)); michael@0: return 1 << pow2; michael@0: } michael@0: michael@0: bool SkConic::findXExtrema(SkScalar* t) const { michael@0: return conic_find_extrema(&fPts[0].fX, fW, t); michael@0: } michael@0: michael@0: bool SkConic::findYExtrema(SkScalar* t) const { michael@0: return conic_find_extrema(&fPts[0].fY, fW, t); michael@0: } michael@0: michael@0: bool SkConic::chopAtXExtrema(SkConic dst[2]) const { michael@0: SkScalar t; michael@0: if (this->findXExtrema(&t)) { michael@0: this->chopAt(t, dst); michael@0: // now clean-up the middle, since we know t was meant to be at michael@0: // an X-extrema michael@0: SkScalar value = dst[0].fPts[2].fX; michael@0: dst[0].fPts[1].fX = value; michael@0: dst[1].fPts[0].fX = value; michael@0: dst[1].fPts[1].fX = value; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool SkConic::chopAtYExtrema(SkConic dst[2]) const { michael@0: SkScalar t; michael@0: if (this->findYExtrema(&t)) { michael@0: this->chopAt(t, dst); michael@0: // now clean-up the middle, since we know t was meant to be at michael@0: // an Y-extrema michael@0: SkScalar value = dst[0].fPts[2].fY; michael@0: dst[0].fPts[1].fY = value; michael@0: dst[1].fPts[0].fY = value; michael@0: dst[1].fPts[1].fY = value; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void SkConic::computeTightBounds(SkRect* bounds) const { michael@0: SkPoint pts[4]; michael@0: pts[0] = fPts[0]; michael@0: pts[1] = fPts[2]; michael@0: int count = 2; michael@0: michael@0: SkScalar t; michael@0: if (this->findXExtrema(&t)) { michael@0: this->evalAt(t, &pts[count++]); michael@0: } michael@0: if (this->findYExtrema(&t)) { michael@0: this->evalAt(t, &pts[count++]); michael@0: } michael@0: bounds->set(pts, count); michael@0: } michael@0: michael@0: void SkConic::computeFastBounds(SkRect* bounds) const { michael@0: bounds->set(fPts, 3); michael@0: } michael@0: michael@0: bool SkConic::findMaxCurvature(SkScalar* t) const { michael@0: // TODO: Implement me michael@0: return false; michael@0: }