SkGeometry.cpp revision 8f4d2306fa866a26f9448048ff63f692b2ba43aa
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGeometry.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "Sk64.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMatrix.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
142e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.orgbool SkXRayCrossesLine(const SkXRay& pt, const SkPoint pts[2], bool* ambiguous) {
152e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (ambiguous) {
162e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        *ambiguous = false;
172e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
18945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Determine quick discards.
19945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Consider query line going exactly through point 0 to not
20945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // intersect, for symmetry with SkXRayCrossesMonotonicCubic.
212e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (pt.fY == pts[0].fY) {
222e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (ambiguous) {
232e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            *ambiguous = true;
242e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        }
25945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return false;
262e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
27945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (pt.fY < pts[0].fY && pt.fY < pts[1].fY)
28945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return false;
29945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (pt.fY > pts[0].fY && pt.fY > pts[1].fY)
30945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return false;
31945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (pt.fX > pts[0].fX && pt.fX > pts[1].fX)
32945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return false;
33945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Determine degenerate cases
34945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (SkScalarNearlyZero(pts[0].fY - pts[1].fY))
35945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return false;
362e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (SkScalarNearlyZero(pts[0].fX - pts[1].fX)) {
37945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        // We've already determined the query point lies within the
38945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        // vertical range of the line segment.
392e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (pt.fX <= pts[0].fX) {
402e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            if (ambiguous) {
412e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org                *ambiguous = (pt.fY == pts[1].fY);
422e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            }
432e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            return true;
442e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        }
452e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        return false;
462e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
472e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    // Ambiguity check
482e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (pt.fY == pts[1].fY) {
492e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (pt.fX <= pts[1].fX) {
502e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            if (ambiguous) {
512e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org                *ambiguous = true;
522e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            }
532e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            return true;
542e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        }
552e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        return false;
562e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
57945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Full line segment evaluation
58945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar delta_y = pts[1].fY - pts[0].fY;
59945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar delta_x = pts[1].fX - pts[0].fX;
60945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar slope = SkScalarDiv(delta_y, delta_x);
61945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar b = pts[0].fY - SkScalarMul(slope, pts[0].fX);
62945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Solve for x coordinate at y = pt.fY
63945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar x = SkScalarDiv(pt.fY - b, slope);
64945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    return pt.fX <= x;
65945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com}
66945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** If defined, this makes eval_quad and eval_cubic do more setup (sometimes
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    involving integer multiplies by 2 or 3, but fewer calls to SkScalarMul.
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    May also introduce overflow of fixed when we compute our setup.
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
718f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com//    #define DIRECT_EVAL_OF_POLYNOMIALS
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
758f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.comstatic int is_not_monotonic(float a, float b, float c) {
768f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com    float ab = a - b;
778f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com    float bc = b - c;
788f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com    if (ab < 0) {
798f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com        bc = -bc;
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
818f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com    return ab == 0 || bc < 0;
828f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com}
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool is_unit_interval(SkScalar x)
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return x > 0 && x < SK_Scalar1;
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio)
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(ratio);
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (numer < 0)
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        numer = -numer;
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        denom = -denom;
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (denom == 0 || numer == 0 || numer >= denom)
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return 0;
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar r = SkScalarDiv(numer, denom);
10515161620bee33efcb706685486c9ce0fb51a72bbreed@android.com    if (SkScalarIsNaN(r)) {
10615161620bee33efcb706685486c9ce0fb51a72bbreed@android.com        return 0;
10715161620bee33efcb706685486c9ce0fb51a72bbreed@android.com    }
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(r >= 0 && r < SK_Scalar1);
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (r == 0) // catch underflow if numer <<<< denom
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return 0;
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    *ratio = r;
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return 1;
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** From Numerical Recipes in C.
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C])
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    x1 = Q / A
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    x2 = C / Q
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2])
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(roots);
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (A == 0)
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return valid_unit_divide(-C, B, roots);
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar* r = roots;
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    float R = B*B - 4*A*C;
13115161620bee33efcb706685486c9ce0fb51a72bbreed@android.com    if (R < 0 || SkScalarIsNaN(R)) {  // complex roots
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return 0;
13315161620bee33efcb706685486c9ce0fb51a72bbreed@android.com    }
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    R = sk_float_sqrt(R);
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar Q = (B < 0) ? -(B-R)/2 : -(B+R)/2;
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r += valid_unit_divide(Q, A, r);
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    r += valid_unit_divide(C, Q, r);
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (r - roots == 2)
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (roots[0] > roots[1])
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkTSwap<SkScalar>(roots[0], roots[1]);
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        else if (roots[0] == roots[1])  // nearly-equal?
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            r -= 1; // skip the double root
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (int)(r - roots);
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1498f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com///////////////////////////////////////////////////////////////////////////////
1508f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com///////////////////////////////////////////////////////////////////////////////
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quad(const SkScalar src[], SkScalar t)
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(src);
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef DIRECT_EVAL_OF_POLYNOMIALS
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    C = src[0];
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    A = src[4] - 2 * src[2] + C;
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    B = 2 * (src[2] - C);
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C);
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    ab = SkScalarInterp(src[0], src[2], t);
164fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkScalar    bc = SkScalarInterp(src[2], src[4], t);
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarInterp(ab, bc, t);
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quad_derivative(const SkScalar src[], SkScalar t)
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar A = src[4] - 2 * src[2] + src[0];
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar B = src[2] - src[0];
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return 2 * SkScalarMulAdd(A, t, B);
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quad_derivative_at_half(const SkScalar src[])
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar A = src[4] - 2 * src[2] + src[0];
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar B = src[2] - src[0];
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return A + 2 * B;
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent)
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(src);
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (pt)
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pt->set(eval_quad(&src[0].fX, t), eval_quad(&src[0].fY, t));
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (tangent)
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tangent->set(eval_quad_derivative(&src[0].fX, t),
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     eval_quad_derivative(&src[0].fY, t));
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkEvalQuadAtHalf(const SkPoint src[3], SkPoint* pt, SkVector* tangent)
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(src);
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (pt)
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX);
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY);
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX);
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY);
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pt->set(SkScalarAve(x01, x12), SkScalarAve(y01, y12));
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (tangent)
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tangent->set(eval_quad_derivative_at_half(&src[0].fX),
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     eval_quad_derivative_at_half(&src[0].fY));
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void interp_quad_coords(const SkScalar* src, SkScalar* dst, SkScalar t)
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    ab = SkScalarInterp(src[0], src[2], t);
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    bc = SkScalarInterp(src[2], src[4], t);
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = src[0];
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2] = ab;
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[4] = SkScalarInterp(ab, bc, t);
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[6] = bc;
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[8] = src[4];
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t)
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t > 0 && t < SK_Scalar1);
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    interp_quad_coords(&src[0].fX, &dst[0].fX, t);
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    interp_quad_coords(&src[0].fY, &dst[0].fY, t);
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5])
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX);
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY);
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX);
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY);
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = src[0];
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[1].set(x01, y01);
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2].set(SkScalarAve(x01, x12), SkScalarAve(y01, y12));
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[3].set(x12, y12);
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[4] = src[2];
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Quad'(t) = At + B, where
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    A = 2(a - 2b + c)
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    B = 2(b - a)
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Solve for t, only if it fits between 0 < t < 1
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValue[1])
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  At + B == 0
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        t = -B / A
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return valid_unit_divide(a - b, a - b - b + c, tValue);
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
260fc25abdabff76f913fb9d4f373418c10a1eca92breed@android.comstatic inline void flatten_double_quad_extrema(SkScalar coords[14])
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    coords[2] = coords[6] = coords[4];
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  Returns 0 for 1 quad, and 1 for two quads, either way the answer is
26677f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com stored in dst[]. Guarantees that the 1/2 quads will be monotonic.
26777f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com */
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5])
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(src);
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(dst);
272fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool once = true;
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (once)
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        once = false;
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint s[3] = { 0, 26398, 0, 26331, 0, 20621428 };
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint d[6];
280fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int n = SkChopQuadAtYExtrema(s, d);
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDebugf("chop=%d, Y=[%x %x %x %x %x %x]\n", n, d[0].fY, d[1].fY, d[2].fY, d[3].fY, d[4].fY, d[5].fY);
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
285fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar a = src[0].fY;
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar b = src[1].fY;
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar c = src[2].fY;
289fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (is_not_monotonic(a, b, c))
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    tValue;
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (valid_unit_divide(a - b, a - b - b + c, &tValue))
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkChopQuadAt(src, dst, tValue);
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            flatten_double_quad_extrema(&dst[0].fY);
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return 1;
2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if we get here, we need to force dst to be monotonic, even though
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // we couldn't compute a unit_divide value (probably underflow).
3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c;
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0].set(src[0].fX, a);
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[1].set(src[1].fX, b);
3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2].set(src[2].fX, c);
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return 0;
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
30977f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com/*  Returns 0 for 1 quad, and 1 for two quads, either way the answer is
31077f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    stored in dst[]. Guarantees that the 1/2 quads will be monotonic.
31177f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com */
31277f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.comint SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5])
31377f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com{
31477f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    SkASSERT(src);
31577f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    SkASSERT(dst);
316fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
31777f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    SkScalar a = src[0].fX;
31877f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    SkScalar b = src[1].fX;
31977f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    SkScalar c = src[2].fX;
320fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
32177f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    if (is_not_monotonic(a, b, c)) {
32277f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com        SkScalar tValue;
32377f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com        if (valid_unit_divide(a - b, a - b - b + c, &tValue)) {
32477f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com            SkChopQuadAt(src, dst, tValue);
32577f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com            flatten_double_quad_extrema(&dst[0].fX);
32677f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com            return 1;
32777f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com        }
32877f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com        // if we get here, we need to force dst to be monotonic, even though
32977f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com        // we couldn't compute a unit_divide value (probably underflow).
33077f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com        b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c;
33177f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    }
33277f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    dst[0].set(a, src[0].fY);
33377f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    dst[1].set(b, src[1].fY);
33477f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    dst[2].set(c, src[2].fY);
33577f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com    return 0;
33677f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com}
33777f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  F(t)    = a (1 - t) ^ 2 + 2 b t (1 - t) + c t ^ 2
3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  F'(t)   = 2 (b - a) + 2 (a - 2b + c) t
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  F''(t)  = 2 (a - 2b + c)
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  A = 2 (b - a)
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  B = 2 (a - 2b + c)
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  Maximum curvature for a quadratic means solving
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  Fx' Fx'' + Fy' Fy'' = 0
3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//  t = - (Ax Bx + Ay By) / (Bx ^ 2 + By ^ 2)
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//
3505383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.comfloat SkFindQuadMaxCurvature(const SkPoint src[3]) {
3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Ax = src[1].fX - src[0].fX;
3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Ay = src[1].fY - src[0].fY;
3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Bx = src[0].fX - src[1].fX - src[1].fX + src[2].fX;
3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    By = src[0].fY - src[1].fY - src[1].fY + src[2].fY;
3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    t = 0;  // 0 means don't chop
3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    (void)valid_unit_divide(-(Ax * Bx + Ay * By), Bx * Bx + By * By, &t);
3585383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com    return t;
3595383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com}
3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3615383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.comint SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5])
3625383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com{
3635383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com    SkScalar t = SkFindQuadMaxCurvature(src);
3645383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com    if (t == 0) {
3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memcpy(dst, src, 3 * sizeof(SkPoint));
3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return 1;
3675383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com    } else {
3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkChopQuadAt(src, dst, t);
3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return 2;
3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3738f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com#define SK_ScalarTwoThirds  (0.666666666f)
3746fc321a18acc8c6437735007240eefe7054e83b0reed@google.com
3756fc321a18acc8c6437735007240eefe7054e83b0reed@google.comvoid SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]) {
3766fc321a18acc8c6437735007240eefe7054e83b0reed@google.com    const SkScalar scale = SK_ScalarTwoThirds;
3776fc321a18acc8c6437735007240eefe7054e83b0reed@google.com    dst[0] = src[0];
3786fc321a18acc8c6437735007240eefe7054e83b0reed@google.com    dst[1].set(src[0].fX + SkScalarMul(src[1].fX - src[0].fX, scale),
3796fc321a18acc8c6437735007240eefe7054e83b0reed@google.com               src[0].fY + SkScalarMul(src[1].fY - src[0].fY, scale));
3806fc321a18acc8c6437735007240eefe7054e83b0reed@google.com    dst[2].set(src[2].fX + SkScalarMul(src[1].fX - src[2].fX, scale),
3816fc321a18acc8c6437735007240eefe7054e83b0reed@google.com               src[2].fY + SkScalarMul(src[1].fY - src[2].fY, scale));
3826fc321a18acc8c6437735007240eefe7054e83b0reed@google.com    dst[3] = src[2];
383945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com}
384945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////////////////
3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///// CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS /////
3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////////////////
3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void get_cubic_coeff(const SkScalar pt[], SkScalar coeff[4])
3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    coeff[0] = pt[6] + 3*(pt[2] - pt[4]) - pt[0];
3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    coeff[1] = 3*(pt[4] - pt[2] - pt[2] + pt[0]);
3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    coeff[2] = 3*(pt[2] - pt[0]);
3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    coeff[3] = pt[0];
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkGetCubicCoeff(const SkPoint pts[4], SkScalar cx[4], SkScalar cy[4])
3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(pts);
4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (cx)
4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        get_cubic_coeff(&pts[0].fX, cx);
4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (cy)
4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        get_cubic_coeff(&pts[0].fY, cy);
4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_cubic(const SkScalar src[], SkScalar t)
4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(src);
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (t == 0)
4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return src[0];
4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef DIRECT_EVAL_OF_POLYNOMIALS
4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar D = src[0];
4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar A = src[6] + 3*(src[2] - src[4]) - D;
4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar B = 3*(src[4] - src[2] - src[2] + D);
4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar C = 3*(src[2] - D);
4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D);
4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    ab = SkScalarInterp(src[0], src[2], t);
4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    bc = SkScalarInterp(src[2], src[4], t);
4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    cd = SkScalarInterp(src[4], src[6], t);
4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    abc = SkScalarInterp(ab, bc, t);
4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    bcd = SkScalarInterp(bc, cd, t);
4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarInterp(abc, bcd, t);
4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** return At^2 + Bt + C
4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quadratic(SkScalar A, SkScalar B, SkScalar C, SkScalar t)
4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C);
4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_cubic_derivative(const SkScalar src[], SkScalar t)
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0];
4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar B = 2*(src[4] - 2 * src[2] + src[0]);
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar C = src[2] - src[0];
4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return eval_quadratic(A, B, C, t);
4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_cubic_2ndDerivative(const SkScalar src[], SkScalar t)
4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0];
4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar B = src[4] - 2 * src[2] + src[0];
4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarMulAdd(A, t, B);
4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* loc, SkVector* tangent, SkVector* curvature)
4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(src);
4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (loc)
4648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        loc->set(eval_cubic(&src[0].fX, t), eval_cubic(&src[0].fY, t));
4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (tangent)
4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tangent->set(eval_cubic_derivative(&src[0].fX, t),
4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                     eval_cubic_derivative(&src[0].fY, t));
4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (curvature)
4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        curvature->set(eval_cubic_2ndDerivative(&src[0].fX, t),
4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                       eval_cubic_2ndDerivative(&src[0].fY, t));
4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Cubic'(t) = At^2 + Bt + C, where
4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    A = 3(-a + 3(b - c) + d)
4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    B = 6(a - 2b + c)
4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    C = 3(b - a)
4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Solve for t, keeping only those that fit betwee 0 < t < 1
4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, SkScalar tValues[2])
4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // we divide A,B,C by 3 to simplify
4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar A = d - a + 3*(b - c);
4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar B = 2*(a - b - b + c);
4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar C = b - a;
4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkFindUnitQuadRoots(A, B, C, tValues);
4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void interp_cubic_coords(const SkScalar* src, SkScalar* dst, SkScalar t)
4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    ab = SkScalarInterp(src[0], src[2], t);
4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    bc = SkScalarInterp(src[2], src[4], t);
4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    cd = SkScalarInterp(src[4], src[6], t);
4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    abc = SkScalarInterp(ab, bc, t);
4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    bcd = SkScalarInterp(bc, cd, t);
4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    abcd = SkScalarInterp(abc, bcd, t);
4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = src[0];
4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2] = ab;
5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[4] = abc;
5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[6] = abcd;
5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[8] = bcd;
5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[10] = cd;
5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[12] = src[6];
5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t)
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t > 0 && t < SK_Scalar1);
5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    interp_cubic_coords(&src[0].fX, &dst[0].fX, t);
5128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    interp_cubic_coords(&src[0].fY, &dst[0].fY, t);
5138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
515a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com/*  http://code.google.com/p/skia/issues/detail?id=32
516fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
517a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    This test code would fail when we didn't check the return result of
518a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    valid_unit_divide in SkChopCubicAt(... tValues[], int roots). The reason is
519a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    that after the first chop, the parameters to valid_unit_divide are equal
520a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    (thanks to finite float precision and rounding in the subtracts). Thus
521a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    even though the 2nd tValue looks < 1.0, after we renormalize it, we end
522a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    up with 1.0, hence the need to check and just return the last cubic as
523a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    a degenerate clump of 4 points in the sampe place.
524a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com
525a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    static void test_cubic() {
526a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com        SkPoint src[4] = {
527a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com            { 556.25000, 523.03003 },
528a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com            { 556.23999, 522.96002 },
529a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com            { 556.21997, 522.89001 },
530a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com            { 556.21997, 522.82001 }
531a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com        };
532a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com        SkPoint dst[10];
533a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com        SkScalar tval[] = { 0.33333334f, 0.99999994f };
534a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com        SkChopCubicAt(src, dst, tval, 2);
535a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com    }
536a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com */
537a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com
5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopCubicAt(const SkPoint src[4], SkPoint dst[], const SkScalar tValues[], int roots)
5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (int i = 0; i < roots - 1; i++)
5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(is_unit_interval(tValues[i]));
5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(is_unit_interval(tValues[i+1]));
5468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(tValues[i] < tValues[i+1]);
5478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (dst)
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (roots == 0) // nothing to chop
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            memcpy(dst, src, 4*sizeof(SkPoint));
5558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        else
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
5578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkScalar    t = tValues[0];
5588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkPoint     tmp[4];
5598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            for (int i = 0; i < roots; i++)
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            {
5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkChopCubicAt(src, dst, t);
5638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (i == roots - 1)
5648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    break;
5658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                dst += 3;
567a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                // have src point to the remaining cubic (after the chop)
5688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                memcpy(tmp, dst, 4 * sizeof(SkPoint));
5698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                src = tmp;
570a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com
571a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                // watch out in case the renormalized t isn't in range
572a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                if (!valid_unit_divide(tValues[i+1] - tValues[i],
573a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                                       SK_Scalar1 - tValues[i], &t)) {
574a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                    // if we can't, just create a degenerate cubic
575a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                    dst[4] = dst[5] = dst[6] = src[3];
576a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                    break;
577a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com                }
5788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
5798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
5808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
5818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7])
5848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
5858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX);
5868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY);
5878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX);
5888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY);
5898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x23 = SkScalarAve(src[2].fX, src[3].fX);
5908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y23 = SkScalarAve(src[2].fY, src[3].fY);
5918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x012 = SkScalarAve(x01, x12);
5938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y012 = SkScalarAve(y01, y12);
5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x123 = SkScalarAve(x12, x23);
5958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y123 = SkScalarAve(y12, y23);
5968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[0] = src[0];
5988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[1].set(x01, y01);
5998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[2].set(x012, y012);
6008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[3].set(SkScalarAve(x012, x123), SkScalarAve(y012, y123));
6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[4].set(x123, y123);
6028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[5].set(x23, y23);
6038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dst[6] = src[3];
6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void flatten_double_cubic_extrema(SkScalar coords[14])
6078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    coords[4] = coords[8] = coords[6];
6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that
6128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    the resulting beziers are monotonic in Y. This is called by the scan converter.
6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Depending on what is returned, dst[] is treated as follows
6148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    0   dst[0..3] is the original cubic
6158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    1   dst[0..3] and dst[3..6] are the two new cubics
6168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    2   dst[0..3], dst[3..6], dst[6..9] are the three new cubics
6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    If dst == null, it is ignored and only the count is returned.
6188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
619bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.comint SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]) {
6208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    tValues[2];
621bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    int         roots = SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY,
622bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com                                           src[3].fY, tValues);
623fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
6248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkChopCubicAt(src, dst, tValues, roots);
625bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    if (dst && roots > 0) {
6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // we do some cleanup to ensure our Y extrema are flat
6278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        flatten_double_cubic_extrema(&dst[0].fY);
628bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com        if (roots == 2) {
6298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            flatten_double_cubic_extrema(&dst[3].fY);
630bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com        }
631bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    }
632bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    return roots;
633bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com}
634bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com
635bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.comint SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]) {
636bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    SkScalar    tValues[2];
637bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    int         roots = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX,
638bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com                                           src[3].fX, tValues);
639fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
640bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    SkChopCubicAt(src, dst, tValues, roots);
641bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com    if (dst && roots > 0) {
642bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com        // we do some cleanup to ensure our Y extrema are flat
643bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com        flatten_double_cubic_extrema(&dst[0].fX);
644bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com        if (roots == 2) {
645bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com            flatten_double_cubic_extrema(&dst[3].fX);
646bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com        }
6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return roots;
6498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** http://www.faculty.idc.ac.il/arik/quality/appendixA.html
6528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Inflection means that curvature is zero.
6548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Curvature is [F' x F''] / [F'^3]
6558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    So we solve F'x X F''y - F'y X F''y == 0
6568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    After some canceling of the cubic term, we get
6578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    A = b - a
6588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    B = c - 2b + a
6598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    C = d - 3c + 3b - a
6608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    (BxCy - ByCx)t^2 + (AxCy - AyCx)t + AxBy - AyBx == 0
6618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
6628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[])
6638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
6648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Ax = src[1].fX - src[0].fX;
6658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Ay = src[1].fY - src[0].fY;
6668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Bx = src[2].fX - 2 * src[1].fX + src[0].fX;
6678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    By = src[2].fY - 2 * src[1].fY + src[0].fY;
6688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Cx = src[3].fX + 3 * (src[1].fX - src[2].fX) - src[0].fX;
6698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    Cy = src[3].fY + 3 * (src[1].fY - src[2].fY) - src[0].fY;
6708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6718f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com    return SkFindUnitQuadRoots(Bx*Cy - By*Cx, Ax*Cy - Ay*Cx, Ax*By - Ay*Bx, tValues);
6728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkChopCubicAtInflections(const SkPoint src[], SkPoint dst[10])
6758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
6768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    tValues[2];
6778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int         count = SkFindCubicInflections(src, tValues);
6788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (dst)
6808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
6818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (count == 0)
6828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            memcpy(dst, src, 4 * sizeof(SkPoint));
6838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        else
6848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkChopCubicAt(src, dst, tValues, count);
6858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
6868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return count + 1;
6878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
6888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <typename T> void bubble_sort(T array[], int count)
6908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
6918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = count - 1; i > 0; --i)
6928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (int j = i; j > 0; --j)
6938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (array[j] < array[j-1])
6948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            {
6958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                T   tmp(array[j]);
6968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                array[j] = array[j-1];
6978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                array[j-1] = tmp;
6988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
6998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// newton refinement
7028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
7038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar refine_cubic_root(const SkFP coeff[4], SkScalar root)
7048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
7058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //  x1 = x0 - f(t) / f'(t)
7068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFP    T = SkScalarToFloat(root);
7088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkFP    N, D;
7098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // f' = 3*coeff[0]*T^2 + 2*coeff[1]*T + coeff[2]
7118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    D = SkFPMul(SkFPMul(coeff[0], SkFPMul(T,T)), 3);
7128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    D = SkFPAdd(D, SkFPMulInt(SkFPMul(coeff[1], T), 2));
7138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    D = SkFPAdd(D, coeff[2]);
7148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (D == 0)
7168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return root;
7178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // f = coeff[0]*T^3 + coeff[1]*T^2 + coeff[2]*T + coeff[3]
7198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    N = SkFPMul(SkFPMul(SkFPMul(T, T), T), coeff[0]);
7208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    N = SkFPAdd(N, SkFPMul(SkFPMul(T, T), coeff[1]));
7218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    N = SkFPAdd(N, SkFPMul(T, coeff[2]));
7228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    N = SkFPAdd(N, coeff[3]);
7238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (N)
7258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
7268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar delta = SkFPToScalar(SkFPDiv(N, D));
7278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (delta)
7298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            root -= delta;
7308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
7318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return root;
7328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
7348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
735087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com/**
736087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com *  Given an array and count, remove all pair-wise duplicates from the array,
737087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com *  keeping the existing sorting, and return the new count
738087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com */
739087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.comstatic int collaps_duplicates(float array[], int count) {
740087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    for (int n = count; n > 1; --n) {
741087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        if (array[0] == array[1]) {
742087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com            for (int i = 1; i < n; ++i) {
743087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com                array[i - 1] = array[i];
744087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com            }
745087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com            count -= 1;
746087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        } else {
747087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com            array += 1;
748087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        }
749087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    }
750087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    return count;
751087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com}
752087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com
753087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com#ifdef SK_DEBUG
754087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com
755087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com#define TEST_COLLAPS_ENTRY(array)   array, SK_ARRAY_COUNT(array)
756087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com
757087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.comstatic void test_collaps_duplicates() {
758087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    static bool gOnce;
759087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    if (gOnce) { return; }
760087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    gOnce = true;
761087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const float src0[] = { 0 };
762087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const float src1[] = { 0, 0 };
763087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const float src2[] = { 0, 1 };
764087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const float src3[] = { 0, 0, 0 };
765087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const float src4[] = { 0, 0, 1 };
766087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const float src5[] = { 0, 1, 1 };
767087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const float src6[] = { 0, 1, 2 };
768087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    const struct {
769087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        const float* fData;
770087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        int fCount;
771087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        int fCollapsedCount;
772087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    } data[] = {
773087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        { TEST_COLLAPS_ENTRY(src0), 1 },
774087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        { TEST_COLLAPS_ENTRY(src1), 1 },
775087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        { TEST_COLLAPS_ENTRY(src2), 2 },
776087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        { TEST_COLLAPS_ENTRY(src3), 1 },
777087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        { TEST_COLLAPS_ENTRY(src4), 2 },
778087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        { TEST_COLLAPS_ENTRY(src5), 2 },
779087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        { TEST_COLLAPS_ENTRY(src6), 3 },
780087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    };
781087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
782087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        float dst[3];
783087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        memcpy(dst, data[i].fData, data[i].fCount * sizeof(dst[0]));
784087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        int count = collaps_duplicates(dst, data[i].fCount);
785087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        SkASSERT(data[i].fCollapsedCount == count);
786087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        for (int j = 1; j < count; ++j) {
787087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com            SkASSERT(dst[j-1] < dst[j]);
788087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        }
789087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    }
790087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com}
791087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com#endif
792087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com
7933c12840b234e614faf569e80f311a77ce65d9fe0reed@google.comstatic SkScalar SkScalarCubeRoot(SkScalar x) {
7943c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    return sk_float_pow(x, 0.3333333f);
7953c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com}
7963c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com
7978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  Solve coeff(t) == 0, returning the number of roots that
7988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    lie withing 0 < t < 1.
7998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3]
800fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
801087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    Eliminates repeated roots (so that all tValues are distinct, and are always
802087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com    in increasing order.
8038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
8043c12840b234e614faf569e80f311a77ce65d9fe0reed@google.comstatic int solve_cubic_polynomial(const SkScalar coeff[4], SkScalar tValues[3])
8058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
8068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkScalarNearlyZero(coeff[0]))   // we're just a quadratic
8078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
8088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues);
8098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8113c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    SkScalar a, b, c, Q, R;
8128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
8148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(coeff[0] != 0);
8158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8163c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        SkScalar inva = SkScalarInvert(coeff[0]);
8173c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        a = coeff[1] * inva;
8183c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        b = coeff[2] * inva;
8193c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        c = coeff[3] * inva;
8208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8213c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    Q = (a*a - b*3) / 9;
8223c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    R = (2*a*a*a - 9*a*b + 27*c) / 54;
8238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8243c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    SkScalar Q3 = Q * Q * Q;
8253c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    SkScalar R2MinusQ3 = R * R - Q3;
8263c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    SkScalar adiv3 = a / 3;
8278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar*   roots = tValues;
8298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    r;
8308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8313c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    if (R2MinusQ3 < 0)   // we have 3 real roots
8328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
8338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        float theta = sk_float_acos(R / sk_float_sqrt(Q3));
8348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        float neg2RootQ = -2 * sk_float_sqrt(Q);
8358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r = neg2RootQ * sk_float_cos(theta/3) - adiv3;
8378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (is_unit_interval(r))
8388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *roots++ = r;
8398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r = neg2RootQ * sk_float_cos((theta + 2*SK_ScalarPI)/3) - adiv3;
8418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (is_unit_interval(r))
8428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *roots++ = r;
8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r = neg2RootQ * sk_float_cos((theta - 2*SK_ScalarPI)/3) - adiv3;
8458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (is_unit_interval(r))
8468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *roots++ = r;
8478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
848087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        SkDEBUGCODE(test_collaps_duplicates();)
849087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com
8508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now sort the roots
851087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        int count = (int)(roots - tValues);
852087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        SkASSERT((unsigned)count <= 3);
853087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        bubble_sort(tValues, count);
854087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        count = collaps_duplicates(tValues, count);
855087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com        roots = tValues + count;    // so we compute the proper count below
8568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    else                // we have 1 real root
8588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
8593c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        SkScalar A = SkScalarAbs(R) + SkScalarSqrt(R2MinusQ3);
8603c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        A = SkScalarCubeRoot(A);
8613c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        if (R > 0)
8623c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com            A = -A;
8638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (A != 0)
8653c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com            A += Q / A;
8663c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        r = A - adiv3;
8678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (is_unit_interval(r))
8688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *roots++ = r;
8698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
8708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (int)(roots - tValues);
8728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  Looking for F' dot F'' == 0
875fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
8768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    A = b - a
8778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    B = c - 2b + a
8788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    C = d - 3c + 3b - a
8798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    F' = 3Ct^2 + 6Bt + 3A
8818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    F'' = 6Ct + 6B
8828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
8848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
8853c12840b234e614faf569e80f311a77ce65d9fe0reed@google.comstatic void formulate_F1DotF2(const SkScalar src[], SkScalar coeff[4])
8868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
8878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    a = src[2] - src[0];
8888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    b = src[4] - 2 * src[2] + src[0];
8898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    c = src[6] + 3 * (src[2] - src[4]) - src[0];
8908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8913c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    coeff[0] = c * c;
8923c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    coeff[1] = 3 * b * c;
8933c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    coeff[2] = 2 * b * b + c * a;
8943c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    coeff[3] = a * b;
8958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
8968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// EXPERIMENTAL: can set this to zero to accept all t-values 0 < t < 1
8988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//#define kMinTValueForChopping (SK_Scalar1 / 256)
8998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define kMinTValueForChopping   0
9008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  Looking for F' dot F'' == 0
902fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
9038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    A = b - a
9048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    B = c - 2b + a
9058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    C = d - 3c + 3b - a
9068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    F' = 3Ct^2 + 6Bt + 3A
9088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    F'' = 6Ct + 6B
9098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
9118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
9128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3])
9138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
9143c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    SkScalar coeffX[4], coeffY[4];
9153c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com    int      i;
9168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    formulate_F1DotF2(&src[0].fX, coeffX);
9188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    formulate_F1DotF2(&src[0].fY, coeffY);
9198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (i = 0; i < 4; i++)
9213c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com        coeffX[i] += coeffY[i];
9228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    t[3];
9248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int         count = solve_cubic_polynomial(coeffX, t);
9258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int         maxCount = 0;
9268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now remove extrema where the curvature is zero (mins)
9288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // !!!! need a test for this !!!!
9298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (i = 0; i < count; i++)
9308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
9318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // if (not_min_curvature())
9328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (t[i] > kMinTValueForChopping && t[i] < SK_Scalar1 - kMinTValueForChopping)
9338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            tValues[maxCount++] = t[i];
9348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return maxCount;
9368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], SkScalar tValues[3])
9398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
9408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    t_storage[3];
9418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (tValues == NULL)
9438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        tValues = t_storage;
9448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int count = SkFindCubicMaxCurvature(src, tValues);
9468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9475383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com    if (dst) {
9488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (count == 0)
9498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            memcpy(dst, src, 4 * sizeof(SkPoint));
9508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        else
9518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkChopCubicAt(src, dst, tValues, count);
9528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
9538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return count + 1;
9548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9562e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.orgbool SkXRayCrossesMonotonicCubic(const SkXRay& pt, const SkPoint cubic[4], bool* ambiguous) {
9572e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (ambiguous) {
9582e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        *ambiguous = false;
9592e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
9602e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org
961945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Find the minimum and maximum y of the extrema, which are the
962945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // first and last points since this cubic is monotonic
963945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar min_y = SkMinScalar(cubic[0].fY, cubic[3].fY);
964945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar max_y = SkMaxScalar(cubic[0].fY, cubic[3].fY);
965945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
966945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (pt.fY == cubic[0].fY
967945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        || pt.fY < min_y
968945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        || pt.fY > max_y) {
969945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        // The query line definitely does not cross the curve
9702e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (ambiguous) {
9712e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            *ambiguous = (pt.fY == cubic[0].fY);
9722e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        }
973945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return false;
974945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    }
975945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
9762e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    bool pt_at_extremum = (pt.fY == cubic[3].fY);
9772e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org
978945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar min_x =
979945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        SkMinScalar(
980945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            SkMinScalar(
981945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com                SkMinScalar(cubic[0].fX, cubic[1].fX),
982945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com                cubic[2].fX),
983945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            cubic[3].fX);
984945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (pt.fX < min_x) {
985945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        // The query line definitely crosses the curve
9862e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (ambiguous) {
9872e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            *ambiguous = pt_at_extremum;
9882e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        }
989945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return true;
990945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    }
991945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
992945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar max_x =
993945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        SkMaxScalar(
994945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            SkMaxScalar(
995945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com                SkMaxScalar(cubic[0].fX, cubic[1].fX),
996945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com                cubic[2].fX),
997945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            cubic[3].fX);
998945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (pt.fX > max_x) {
999945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        // The query line definitely does not cross the curve
1000945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return false;
1001945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    }
1002945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
1003945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Do a binary search to find the parameter value which makes y as
1004945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // close as possible to the query point. See whether the query
1005945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // line's origin is to the left of the associated x coordinate.
1006945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
1007945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // kMaxIter is chosen as the number of mantissa bits for a float,
1008945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // since there's no way we are going to get more precision by
1009945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // iterating more times than that.
1010945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    const int kMaxIter = 23;
1011945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkPoint eval;
1012945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    int iter = 0;
1013945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar upper_t;
1014945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkScalar lower_t;
1015945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // Need to invert direction of t parameter if cubic goes up
1016945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    // instead of down
1017945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (cubic[3].fY > cubic[0].fY) {
1018945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        upper_t = SK_Scalar1;
10194b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org        lower_t = 0;
1020945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    } else {
10214b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org        upper_t = 0;
1022945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        lower_t = SK_Scalar1;
1023945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    }
1024945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    do {
1025945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        SkScalar t = SkScalarAve(upper_t, lower_t);
1026945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        SkEvalCubicAt(cubic, t, &eval, NULL, NULL);
1027945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        if (pt.fY > eval.fY) {
1028945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            lower_t = t;
1029945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        } else {
1030945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            upper_t = t;
1031945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        }
1032945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    } while (++iter < kMaxIter
1033945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com             && !SkScalarNearlyZero(eval.fY - pt.fY));
1034945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (pt.fX <= eval.fX) {
10352e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (ambiguous) {
10362e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org            *ambiguous = pt_at_extremum;
10372e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        }
1038945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        return true;
1039945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    }
1040945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    return false;
1041945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com}
1042945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com
10432e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.orgint SkNumXRayCrossingsForCubic(const SkXRay& pt, const SkPoint cubic[4], bool* ambiguous) {
1044945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    int num_crossings = 0;
1045945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    SkPoint monotonic_cubics[10];
1046945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    int num_monotonic_cubics = SkChopCubicAtYExtrema(cubic, monotonic_cubics);
10472e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (ambiguous) {
10482e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        *ambiguous = false;
10492e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
10502e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    bool locally_ambiguous;
10512e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (SkXRayCrossesMonotonicCubic(pt, &monotonic_cubics[0], &locally_ambiguous))
1052945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com        ++num_crossings;
10532e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (ambiguous) {
10542e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        *ambiguous |= locally_ambiguous;
10552e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
1056945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (num_monotonic_cubics > 0)
10572e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (SkXRayCrossesMonotonicCubic(pt, &monotonic_cubics[3], &locally_ambiguous))
1058945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            ++num_crossings;
10592e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (ambiguous) {
10602e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        *ambiguous |= locally_ambiguous;
10612e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
1062945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    if (num_monotonic_cubics > 1)
10632e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        if (SkXRayCrossesMonotonicCubic(pt, &monotonic_cubics[6], &locally_ambiguous))
1064945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com            ++num_crossings;
10652e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    if (ambiguous) {
10662e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org        *ambiguous |= locally_ambiguous;
10672e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org    }
1068945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com    return num_crossings;
1069945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com}
10708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////////
10718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  Find t value for quadratic [a, b, c] = d.
10738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Return 0 if there is no solution within [0, 1)
10748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
10758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar quad_solve(SkScalar a, SkScalar b, SkScalar c, SkScalar d)
10768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
10778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // At^2 + Bt + C = d
10788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar A = a - 2 * b + c;
10798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar B = 2 * (b - a);
10808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar C = a - d;
10818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar    roots[2];
10838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int         count = SkFindUnitQuadRoots(A, B, C, roots);
10848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(count <= 1);
10868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return count == 1 ? roots[0] : 0;
10878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
10888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1089e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com/*  given a quad-curve and a point (x,y), chop the quad at that point and place
109050df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com    the new off-curve point and endpoint into 'dest'.
10919e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com    Should only return false if the computed pos is the start of the curve
1092e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com    (i.e. root == 0)
10938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
1094e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.comstatic bool truncate_last_curve(const SkPoint quad[3], SkScalar x, SkScalar y, SkPoint* dest)
10958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
10968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const SkScalar* base;
10978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar        value;
10988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (SkScalarAbs(x) < SkScalarAbs(y)) {
11008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        base = &quad[0].fX;
11018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = x;
11028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
11038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        base = &quad[0].fY;
11048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        value = y;
11058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // note: this returns 0 if it thinks value is out of range, meaning the
11088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // root might return something outside of [0, 1)
11098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar t = quad_solve(base[0], base[2], base[4], value);
11108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (t > 0)
11128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
11138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint tmp[5];
11148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkChopQuadAt(quad, tmp, t);
1115e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com        dest[0] = tmp[1];
1116b0889a5aa610552bf306edc8d9a35d2d601acdb9robertphillips@google.com        dest[1].set(x, y);
11178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
11188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
11198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /*  t == 0 means either the value triggered a root outside of [0, 1)
11208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            For our purposes, we can ignore the <= 0 roots, but we want to
11218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            catch the >= 1 roots (which given our caller, will basically mean
11228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            a root of 1, give-or-take numerical instability). If we are in the
11238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            >= 1 case, return the existing offCurve point.
1124fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
11258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            The test below checks to see if we are close to the "end" of the
11268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            curve (near base[4]). Rather than specifying a tolerance, I just
11278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            check to see if value is on to the right/left of the middle point
11288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            (depending on the direction/sign of the end points).
11298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
11308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if ((base[0] < base[4] && value > base[2]) ||
11318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            (base[0] > base[4] && value < base[2]))   // should root have been 1
11328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
1133e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com            dest[0] = quad[1];
1134e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com            dest[1].set(x, y);
11358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
11368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
11378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
11388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
11398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
11408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const SkPoint gQuadCirclePts[kSkBuildQuadArcStorage] = {
1142f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// The mid point of the quadratic arc approximation is half way between the two
1143f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// control points. The float epsilon adjustment moves the on curve point out by
1144f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// two bits, distributing the convex test error between the round rect approximation
1145f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// and the convex cross product sign equality test.
1146f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org#define SK_MID_RRECT_OFFSET (SK_Scalar1 + SK_ScalarTanPIOver8 + FLT_EPSILON * 4) / 2
1147f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_Scalar1,            0                      },
1148f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_Scalar1,            SK_ScalarTanPIOver8    },
1149f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_MID_RRECT_OFFSET,   SK_MID_RRECT_OFFSET    },
1150f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_ScalarTanPIOver8,   SK_Scalar1             },
1151f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org
1152f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { 0,                     SK_Scalar1             },
1153f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { -SK_ScalarTanPIOver8,  SK_Scalar1             },
1154f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { -SK_MID_RRECT_OFFSET,  SK_MID_RRECT_OFFSET    },
1155f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { -SK_Scalar1,           SK_ScalarTanPIOver8    },
1156f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org
1157f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { -SK_Scalar1,           0                      },
1158f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { -SK_Scalar1,           -SK_ScalarTanPIOver8   },
1159f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { -SK_MID_RRECT_OFFSET,  -SK_MID_RRECT_OFFSET   },
1160f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { -SK_ScalarTanPIOver8,  -SK_Scalar1            },
1161f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org
1162f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { 0,                     -SK_Scalar1            },
1163f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_ScalarTanPIOver8,   -SK_Scalar1            },
1164f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_MID_RRECT_OFFSET,   -SK_MID_RRECT_OFFSET   },
1165f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_Scalar1,            -SK_ScalarTanPIOver8   },
1166f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org
1167f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org    { SK_Scalar1,            0                      }
1168f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org#undef SK_MID_RRECT_OFFSET
11698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
11708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkBuildQuadArc(const SkVector& uStart, const SkVector& uStop,
11728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                   SkRotationDirection dir, const SkMatrix* userMatrix,
11738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                   SkPoint quadPoints[])
11748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
11758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // rotate by x,y so that uStart is (1.0)
11768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar x = SkPoint::DotProduct(uStart, uStop);
11778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar y = SkPoint::CrossProduct(uStart, uStop);
11788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar absX = SkScalarAbs(x);
11808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar absY = SkScalarAbs(y);
11818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int pointCount;
11838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // check for (effectively) coincident vectors
11858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // this can happen if our angle is nearly 0 or nearly 180 (y == 0)
11868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // ... we use the dot-prod to distinguish between 0 and 180 (x > 0)
11878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (absY <= SK_ScalarNearlyZero && x > 0 &&
11888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        ((y >= 0 && kCW_SkRotationDirection == dir) ||
11898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com         (y <= 0 && kCCW_SkRotationDirection == dir))) {
1190fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
11918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // just return the start-point
11928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        quadPoints[0].set(SK_Scalar1, 0);
11938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pointCount = 1;
11948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
11958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (dir == kCCW_SkRotationDirection)
11968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            y = -y;
11978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // what octant (quadratic curve) is [xy] in?
11998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int oct = 0;
12008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bool sameSign = true;
12018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (0 == y)
12038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
12048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            oct = 4;        // 180
12058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(SkScalarAbs(x + SK_Scalar1) <= SK_ScalarNearlyZero);
12068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        else if (0 == x)
12088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
12098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(absY - SK_Scalar1 <= SK_ScalarNearlyZero);
12108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (y > 0)
12118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                oct = 2;    // 90
12128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            else
12138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                oct = 6;    // 270
12148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        else
12168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
12178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (y < 0)
12188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                oct += 4;
12198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if ((x < 0) != (y < 0))
12208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            {
12218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                oct += 2;
12228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                sameSign = false;
12238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
12248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if ((absX < absY) == sameSign)
12258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                oct += 1;
12268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int wholeCount = oct << 1;
12298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        memcpy(quadPoints, gQuadCirclePts, (wholeCount + 1) * sizeof(SkPoint));
12308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkPoint* arc = &gQuadCirclePts[wholeCount];
1232e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com        if (truncate_last_curve(arc, x, y, &quadPoints[wholeCount + 1]))
12338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
12348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            wholeCount += 2;
12358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
12368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pointCount = wholeCount + 1;
12378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // now handle counter-clockwise and the initial unitStart rotation
12408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMatrix    matrix;
12418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.setSinCos(uStart.fY, uStart.fX);
12428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (dir == kCCW_SkRotationDirection) {
12438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.preScale(SK_Scalar1, -SK_Scalar1);
12448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (userMatrix) {
12468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        matrix.postConcat(*userMatrix);
12478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
12488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    matrix.mapPoints(quadPoints, pointCount);
12498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return pointCount;
12508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1251c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com
1252c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com///////////////////////////////////////////////////////////////////////////////
1253c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com
125417a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w)
125517a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//     ------------------------------------------
125617a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//         ((1 - t)^2 + t^2 + 2 (1 - t) t w)
125717a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//
125817a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//   = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0}
125917a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//     ------------------------------------------------
126017a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//             {t^2 (2 - 2 w), t (-2 + 2 w), 1}
126117a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//
126217a2c919d095797c364d407a5dbdb4d60533b953reed@google.com
126317a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// Take the parametric specification for the conic (either X or Y) and return
126417a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// in coeff[] the coefficients for the simple quadratic polynomial
126517a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//    coeff[0] for t^2
126617a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//    coeff[1] for t
126717a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//    coeff[2] for constant term
126817a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//
12696862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic SkScalar conic_eval_pos(const SkScalar src[], SkScalar w, SkScalar t) {
12700c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkASSERT(src);
12710c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkASSERT(t >= 0 && t <= SK_Scalar1);
127245fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com
12730c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar    src2w = SkScalarMul(src[2], w);
12740c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar    C = src[0];
12750c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar    A = src[4] - 2 * src2w + C;
12760c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar    B = 2 * (src2w - C);
12770c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar numer = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C);
127845fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com
12790c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    B = 2 * (w - SK_Scalar1);
12800c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    C = SK_Scalar1;
12810c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    A = -B;
12820c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar denom = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C);
128345fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com
12840c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    return SkScalarDiv(numer, denom);
12850c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org}
128617a2c919d095797c364d407a5dbdb4d60533b953reed@google.com
12870c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org// F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w)
12880c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org//
12896862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//  t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w)
12906862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//  t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w)
12916862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//  t^0 : -2 P0 w + 2 P1 w
12926862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//
12936862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//  We disregard magnitude, so we can freely ignore the denominator of F', and
12946862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//  divide the numerator by 2
12950c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org//
129617a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//    coeff[0] for t^2
12976862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//    coeff[1] for t^1
12986862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org//    coeff[2] for t^0
129917a2c919d095797c364d407a5dbdb4d60533b953reed@google.com//
13006862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic void conic_deriv_coeff(const SkScalar src[], SkScalar w, SkScalar coeff[3]) {
13016862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    const SkScalar P20 = src[4] - src[0];
13026862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    const SkScalar P10 = src[2] - src[0];
13036862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    const SkScalar wP10 = w * P10;
13046862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    coeff[0] = w * P20 - P20;
13056862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    coeff[1] = P20 - 2 * wP10;
13066862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    coeff[2] = wP10;
13076862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org}
13086862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org
13096862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic SkScalar conic_eval_tan(const SkScalar coord[], SkScalar w, SkScalar t) {
13106862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    SkScalar coeff[3];
13116862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    conic_deriv_coeff(coord, w, coeff);
13126862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    return t * (t * coeff[0] + coeff[1]) + coeff[2];
13130c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org}
13140c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org
13156862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic bool conic_find_extrema(const SkScalar src[], SkScalar w, SkScalar* t) {
13160c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar coeff[3];
13176862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    conic_deriv_coeff(src, w, coeff);
13180c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org
13190c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar tValues[2];
13200c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    int roots = SkFindUnitQuadRoots(coeff[0], coeff[1], coeff[2], tValues);
13210c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkASSERT(0 == roots || 1 == roots);
132245fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com
13230c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    if (1 == roots) {
13240c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        *t = tValues[0];
13250c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        return true;
13260c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    }
13270c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    return false;
132817a2c919d095797c364d407a5dbdb4d60533b953reed@google.com}
132917a2c919d095797c364d407a5dbdb4d60533b953reed@google.com
13300d099557feb99707c8f601746f46f5a295eb33b7reed@google.comstruct SkP3D {
13310d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    SkScalar fX, fY, fZ;
13324bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com
13330d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    void set(SkScalar x, SkScalar y, SkScalar z) {
13340d099557feb99707c8f601746f46f5a295eb33b7reed@google.com        fX = x; fY = y; fZ = z;
13350d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    }
13364bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com
13370d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    void projectDown(SkPoint* dst) const {
13380d099557feb99707c8f601746f46f5a295eb33b7reed@google.com        dst->set(fX / fZ, fY / fZ);
13390d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    }
13400d099557feb99707c8f601746f46f5a295eb33b7reed@google.com};
13410d099557feb99707c8f601746f46f5a295eb33b7reed@google.com
13420d099557feb99707c8f601746f46f5a295eb33b7reed@google.com// we just return the middle 3 points, since the first and last are dups of src
13430d099557feb99707c8f601746f46f5a295eb33b7reed@google.com//
13440d099557feb99707c8f601746f46f5a295eb33b7reed@google.comstatic void p3d_interp(const SkScalar src[3], SkScalar dst[3], SkScalar t) {
13450d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    SkScalar ab = SkScalarInterp(src[0], src[3], t);
13460d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    SkScalar bc = SkScalarInterp(src[3], src[6], t);
13470d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[0] = ab;
13480d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[3] = SkScalarInterp(ab, bc, t);
13490d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[6] = bc;
13500d099557feb99707c8f601746f46f5a295eb33b7reed@google.com}
13510d099557feb99707c8f601746f46f5a295eb33b7reed@google.com
13520d099557feb99707c8f601746f46f5a295eb33b7reed@google.comstatic void ratquad_mapTo3D(const SkPoint src[3], SkScalar w, SkP3D dst[]) {
13530d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[0].set(src[0].fX * 1, src[0].fY * 1, 1);
13540d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[1].set(src[1].fX * w, src[1].fY * w, w);
13550d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[2].set(src[2].fX * 1, src[2].fY * 1, 1);
13560d099557feb99707c8f601746f46f5a295eb33b7reed@google.com}
13570d099557feb99707c8f601746f46f5a295eb33b7reed@google.com
135824bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.comvoid SkConic::evalAt(SkScalar t, SkPoint* pt, SkVector* tangent) const {
1359c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
13604bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com
1361c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com    if (pt) {
13626862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org        pt->set(conic_eval_pos(&fPts[0].fX, fW, t),
13636862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org                conic_eval_pos(&fPts[0].fY, fW, t));
1364c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com    }
136524bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com    if (tangent) {
136624bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com        tangent->set(conic_eval_tan(&fPts[0].fX, fW, t),
136724bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com                     conic_eval_tan(&fPts[0].fY, fW, t));
136824bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com    }
1369c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com}
1370c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com
137128552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::chopAt(SkScalar t, SkConic dst[2]) const {
13720d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    SkP3D tmp[3], tmp2[3];
13730d099557feb99707c8f601746f46f5a295eb33b7reed@google.com
13740d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    ratquad_mapTo3D(fPts, fW, tmp);
13754bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com
13760d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    p3d_interp(&tmp[0].fX, &tmp2[0].fX, t);
13770d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    p3d_interp(&tmp[0].fY, &tmp2[0].fY, t);
13780d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    p3d_interp(&tmp[0].fZ, &tmp2[0].fZ, t);
13794bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com
13800d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[0].fPts[0] = fPts[0];
13810d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    tmp2[0].projectDown(&dst[0].fPts[1]);
13820d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    tmp2[1].projectDown(&dst[0].fPts[2]); dst[1].fPts[0] = dst[0].fPts[2];
13830d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    tmp2[2].projectDown(&dst[1].fPts[1]);
13840d099557feb99707c8f601746f46f5a295eb33b7reed@google.com    dst[1].fPts[2] = fPts[2];
13850d099557feb99707c8f601746f46f5a295eb33b7reed@google.com
13864af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    // to put in "standard form", where w0 and w2 are both 1, we compute the
13874af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    // new w1 as sqrt(w1*w1/w0*w2)
13884af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    // or
13894af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    // w1 /= sqrt(w0*w2)
13904af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    //
13914af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    // However, in our case, we know that for dst[0], w0 == 1, and for dst[1], w2 == 1
13924af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    //
13934af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    SkScalar root = SkScalarSqrt(tmp2[1].fZ);
13944af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    dst[0].fW = tmp2[0].fZ / root;
13954af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org    dst[1].fW = tmp2[2].fZ / root;
1396c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com}
13978d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org
13983df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.orgstatic SkScalar subdivide_w_value(SkScalar w) {
13996862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    return SkScalarSqrt(SK_ScalarHalf + w * SK_ScalarHalf);
14003df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org}
14013df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org
140228552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::chop(SkConic dst[2]) const {
14038d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    SkScalar scale = SkScalarInvert(SK_Scalar1 + fW);
14048d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    SkScalar p1x = fW * fPts[1].fX;
14058d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    SkScalar p1y = fW * fPts[1].fY;
14068d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    SkScalar mx = (fPts[0].fX + 2 * p1x + fPts[2].fX) * scale * SK_ScalarHalf;
14078d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    SkScalar my = (fPts[0].fY + 2 * p1y + fPts[2].fY) * scale * SK_ScalarHalf;
14088d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org
14098d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    dst[0].fPts[0] = fPts[0];
14108d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    dst[0].fPts[1].set((fPts[0].fX + p1x) * scale,
14118d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org                       (fPts[0].fY + p1y) * scale);
14128d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    dst[0].fPts[2].set(mx, my);
14138d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org
14148d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    dst[1].fPts[0].set(mx, my);
14158d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    dst[1].fPts[1].set((p1x + fPts[2].fX) * scale,
14168d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org                       (p1y + fPts[2].fY) * scale);
14178d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org    dst[1].fPts[2] = fPts[2];
14188d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org
14193df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    dst[0].fW = dst[1].fW = subdivide_w_value(fW);
14203df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org}
14213df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org
142297514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org/*
142397514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org *  "High order approximation of conic sections by quadratic splines"
142497514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org *      by Michael Floater, 1993
142597514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org */
1426af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org#define AS_QUAD_ERROR_SETUP                                         \
1427af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    SkScalar a = fW - 1;                                            \
1428af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    SkScalar k = a / (4 * (2 + a));                                 \
1429af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    SkScalar x = k * (fPts[0].fX - 2 * fPts[1].fX + fPts[2].fX);    \
1430af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    SkScalar y = k * (fPts[0].fY - 2 * fPts[1].fY + fPts[2].fY);
1431af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org
1432af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.orgvoid SkConic::computeAsQuadError(SkVector* err) const {
1433af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    AS_QUAD_ERROR_SETUP
1434af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    err->set(x, y);
1435af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org}
1436af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org
1437af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.orgbool SkConic::asQuadTol(SkScalar tol) const {
1438af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    AS_QUAD_ERROR_SETUP
1439af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    return (x * x + y * y) <= tol * tol;
144097514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org}
14417841c63136e8aa2d3aadbeab8432405abcd73c32skia.committer@gmail.com
144297514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.orgint SkConic::computeQuadPOW2(SkScalar tol) const {
1443af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    AS_QUAD_ERROR_SETUP
1444af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    SkScalar error = SkScalarSqrt(x * x + y * y) - tol;
1445af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org
1446af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    if (error <= 0) {
144797514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org        return 0;
14483df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    }
144997514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org    uint32_t ierr = (uint32_t)error;
1450af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    return (34 - SkCLZ(ierr)) >> 1;
14513df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org}
14523df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org
145328552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgstatic SkPoint* subdivide(const SkConic& src, SkPoint pts[], int level) {
14543df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    SkASSERT(level >= 0);
1455af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org
14563df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    if (0 == level) {
14573df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org        memcpy(pts, &src.fPts[1], 2 * sizeof(SkPoint));
14583df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org        return pts + 2;
14593df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    } else {
146028552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.org        SkConic dst[2];
14613df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org        src.chop(dst);
14623df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org        --level;
14633df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org        pts = subdivide(dst[0], pts, level);
14643df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org        return subdivide(dst[1], pts, level);
14653df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    }
14668d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org}
14673df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org
146828552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgint SkConic::chopIntoQuadsPOW2(SkPoint pts[], int pow2) const {
1469af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org    SkASSERT(pow2 >= 0);
14703df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    *pts = fPts[0];
1471aebfa7e1b1e94f693f3e7beb6ad43cfcb0f69e98reed@google.com    SkDEBUGCODE(SkPoint* endPts =) subdivide(*this, pts + 1, pow2);
14723df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    SkASSERT(endPts - pts == (2 * (1 << pow2) + 1));
14733df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org    return 1 << pow2;
14743df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org}
14750c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org
147628552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::findXExtrema(SkScalar* t) const {
14776862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    return conic_find_extrema(&fPts[0].fX, fW, t);
14780c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org}
14790c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org
148028552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::findYExtrema(SkScalar* t) const {
14816862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org    return conic_find_extrema(&fPts[0].fY, fW, t);
14820c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org}
14830c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org
148428552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::chopAtXExtrema(SkConic dst[2]) const {
14850c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar t;
14860c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    if (this->findXExtrema(&t)) {
14870c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        this->chopAt(t, dst);
14880c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        // now clean-up the middle, since we know t was meant to be at
14890c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        // an X-extrema
14900c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        SkScalar value = dst[0].fPts[2].fX;
14910c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        dst[0].fPts[1].fX = value;
14920c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        dst[1].fPts[0].fX = value;
14930c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        dst[1].fPts[1].fX = value;
14940c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        return true;
14950c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    }
14960c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    return false;
14970c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org}
14980c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org
149928552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::chopAtYExtrema(SkConic dst[2]) const {
15000c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    SkScalar t;
15010c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    if (this->findYExtrema(&t)) {
15020c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        this->chopAt(t, dst);
15030c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        // now clean-up the middle, since we know t was meant to be at
15040c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        // an Y-extrema
15050c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        SkScalar value = dst[0].fPts[2].fY;
15060c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        dst[0].fPts[1].fY = value;
15070c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        dst[1].fPts[0].fY = value;
15080c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        dst[1].fPts[1].fY = value;
15090c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org        return true;
15100c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    }
15110c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org    return false;
15120c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org}
15130c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org
151428552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::computeTightBounds(SkRect* bounds) const {
15155c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    SkPoint pts[4];
15165c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    pts[0] = fPts[0];
15175c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    pts[1] = fPts[2];
15185c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    int count = 2;
15195c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org
15205c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    SkScalar t;
15215c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    if (this->findXExtrema(&t)) {
15225c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org        this->evalAt(t, &pts[count++]);
15235c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    }
15245c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    if (this->findYExtrema(&t)) {
15255c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org        this->evalAt(t, &pts[count++]);
15265c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    }
15275c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    bounds->set(pts, count);
15285c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org}
15295c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org
153028552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::computeFastBounds(SkRect* bounds) const {
15315c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org    bounds->set(fPts, 3);
15325c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org}
1533