SkGeometry.cpp revision d50d87a185d26a38d7100bbbb119be17947aa5e4
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */ 7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGeometry.h" 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMatrix.h" 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 112e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.orgbool SkXRayCrossesLine(const SkXRay& pt, const SkPoint pts[2], bool* ambiguous) { 122e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 132e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = false; 142e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 15945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Determine quick discards. 16945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Consider query line going exactly through point 0 to not 17945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // intersect, for symmetry with SkXRayCrossesMonotonicCubic. 182e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (pt.fY == pts[0].fY) { 192e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 202e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = true; 212e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 22945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 232e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 24945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fY < pts[0].fY && pt.fY < pts[1].fY) 25945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 26945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fY > pts[0].fY && pt.fY > pts[1].fY) 27945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 28945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fX > pts[0].fX && pt.fX > pts[1].fX) 29945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 30945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Determine degenerate cases 31945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (SkScalarNearlyZero(pts[0].fY - pts[1].fY)) 32945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 332e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (SkScalarNearlyZero(pts[0].fX - pts[1].fX)) { 34945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // We've already determined the query point lies within the 35945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // vertical range of the line segment. 362e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (pt.fX <= pts[0].fX) { 372e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 382e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = (pt.fY == pts[1].fY); 392e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 402e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org return true; 412e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 422e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org return false; 432e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 442e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org // Ambiguity check 452e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (pt.fY == pts[1].fY) { 462e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (pt.fX <= pts[1].fX) { 472e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 482e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = true; 492e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 502e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org return true; 512e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 522e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org return false; 532e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 54945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Full line segment evaluation 55945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar delta_y = pts[1].fY - pts[0].fY; 56945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar delta_x = pts[1].fX - pts[0].fX; 57945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar slope = SkScalarDiv(delta_y, delta_x); 58945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar b = pts[0].fY - SkScalarMul(slope, pts[0].fX); 59945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Solve for x coordinate at y = pt.fY 60945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar x = SkScalarDiv(pt.fY - b, slope); 61945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return pt.fX <= x; 62945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com} 63945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** If defined, this makes eval_quad and eval_cubic do more setup (sometimes 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com involving integer multiplies by 2 or 3, but fewer calls to SkScalarMul. 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com May also introduce overflow of fixed when we compute our setup. 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 688f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com// #define DIRECT_EVAL_OF_POLYNOMIALS 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////// 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 728f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.comstatic int is_not_monotonic(float a, float b, float c) { 738f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com float ab = a - b; 748f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com float bc = b - c; 758f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com if (ab < 0) { 768f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com bc = -bc; 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 788f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com return ab == 0 || bc < 0; 798f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com} 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////// 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic bool is_unit_interval(SkScalar x) 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return x > 0 && x < SK_Scalar1; 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio) 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(ratio); 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (numer < 0) 938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com numer = -numer; 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com denom = -denom; 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (denom == 0 || numer == 0 || numer >= denom) 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 0; 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar r = SkScalarDiv(numer, denom); 10215161620bee33efcb706685486c9ce0fb51a72bbreed@android.com if (SkScalarIsNaN(r)) { 10315161620bee33efcb706685486c9ce0fb51a72bbreed@android.com return 0; 10415161620bee33efcb706685486c9ce0fb51a72bbreed@android.com } 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(r >= 0 && r < SK_Scalar1); 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (r == 0) // catch underflow if numer <<<< denom 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 0; 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *ratio = r; 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 1; 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** From Numerical Recipes in C. 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C]) 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com x1 = Q / A 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com x2 = C / Q 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]) 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(roots); 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (A == 0) 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return valid_unit_divide(-C, B, roots); 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar* r = roots; 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com float R = B*B - 4*A*C; 12815161620bee33efcb706685486c9ce0fb51a72bbreed@android.com if (R < 0 || SkScalarIsNaN(R)) { // complex roots 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 0; 13015161620bee33efcb706685486c9ce0fb51a72bbreed@android.com } 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com R = sk_float_sqrt(R); 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Q = (B < 0) ? -(B-R)/2 : -(B+R)/2; 1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r += valid_unit_divide(Q, A, r); 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r += valid_unit_divide(C, Q, r); 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (r - roots == 2) 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (roots[0] > roots[1]) 1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkTSwap<SkScalar>(roots[0], roots[1]); 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else if (roots[0] == roots[1]) // nearly-equal? 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r -= 1; // skip the double root 1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (int)(r - roots); 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1468f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com/////////////////////////////////////////////////////////////////////////////// 1478f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com/////////////////////////////////////////////////////////////////////////////// 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quad(const SkScalar src[], SkScalar t) 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(src); 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t >= 0 && t <= SK_Scalar1); 1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef DIRECT_EVAL_OF_POLYNOMIALS 1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar C = src[0]; 1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = src[4] - 2 * src[2] + C; 1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = 2 * (src[2] - C); 1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); 1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar ab = SkScalarInterp(src[0], src[2], t); 161fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com SkScalar bc = SkScalarInterp(src[2], src[4], t); 1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkScalarInterp(ab, bc, t); 1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quad_derivative(const SkScalar src[], SkScalar t) 1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = src[4] - 2 * src[2] + src[0]; 1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = src[2] - src[0]; 1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 2 * SkScalarMulAdd(A, t, B); 1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quad_derivative_at_half(const SkScalar src[]) 1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = src[4] - 2 * src[2] + src[0]; 1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = src[2] - src[0]; 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return A + 2 * B; 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent) 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(src); 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t >= 0 && t <= SK_Scalar1); 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (pt) 1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pt->set(eval_quad(&src[0].fX, t), eval_quad(&src[0].fY, t)); 1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (tangent) 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com tangent->set(eval_quad_derivative(&src[0].fX, t), 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com eval_quad_derivative(&src[0].fY, t)); 1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkEvalQuadAtHalf(const SkPoint src[3], SkPoint* pt, SkVector* tangent) 1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(src); 1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (pt) 1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); 2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); 2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); 2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); 2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pt->set(SkScalarAve(x01, x12), SkScalarAve(y01, y12)); 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (tangent) 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com tangent->set(eval_quad_derivative_at_half(&src[0].fX), 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com eval_quad_derivative_at_half(&src[0].fY)); 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void interp_quad_coords(const SkScalar* src, SkScalar* dst, SkScalar t) 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar ab = SkScalarInterp(src[0], src[2], t); 2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar bc = SkScalarInterp(src[2], src[4], t); 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[0] = src[0]; 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[2] = ab; 2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[4] = SkScalarInterp(ab, bc, t); 2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[6] = bc; 2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[8] = src[4]; 2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t) 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t > 0 && t < SK_Scalar1); 2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com interp_quad_coords(&src[0].fX, &dst[0].fX, t); 2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com interp_quad_coords(&src[0].fY, &dst[0].fY, t); 2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]) 2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); 2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); 2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[0] = src[0]; 2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[1].set(x01, y01); 2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[2].set(SkScalarAve(x01, x12), SkScalarAve(y01, y12)); 2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[3].set(x12, y12); 2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[4] = src[2]; 2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Quad'(t) = At + B, where 2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com A = 2(a - 2b + c) 2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com B = 2(b - a) 2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Solve for t, only if it fits between 0 < t < 1 2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValue[1]) 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /* At + B == 0 2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com t = -B / A 2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return valid_unit_divide(a - b, a - b - b + c, tValue); 2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 257fc25abdabff76f913fb9d4f373418c10a1eca92breed@android.comstatic inline void flatten_double_quad_extrema(SkScalar coords[14]) 2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com coords[2] = coords[6] = coords[4]; 2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* Returns 0 for 1 quad, and 1 for two quads, either way the answer is 26377f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com stored in dst[]. Guarantees that the 1/2 quads will be monotonic. 26477f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com */ 2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]) 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(src); 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(dst); 269fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static bool once = true; 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (once) 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com once = false; 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint s[3] = { 0, 26398, 0, 26331, 0, 20621428 }; 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint d[6]; 277fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int n = SkChopQuadAtYExtrema(s, d); 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@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); 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 282fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar a = src[0].fY; 2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar b = src[1].fY; 2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar c = src[2].fY; 286fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (is_not_monotonic(a, b, c)) 2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar tValue; 2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (valid_unit_divide(a - b, a - b - b + c, &tValue)) 2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChopQuadAt(src, dst, tValue); 2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com flatten_double_quad_extrema(&dst[0].fY); 2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 1; 2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // if we get here, we need to force dst to be monotonic, even though 2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // we couldn't compute a unit_divide value (probably underflow). 2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; 2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[0].set(src[0].fX, a); 3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[1].set(src[1].fX, b); 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[2].set(src[2].fX, c); 3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 0; 3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 30677f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com/* Returns 0 for 1 quad, and 1 for two quads, either way the answer is 30777f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com stored in dst[]. Guarantees that the 1/2 quads will be monotonic. 30877f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com */ 30977f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.comint SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]) 31077f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com{ 31177f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com SkASSERT(src); 31277f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com SkASSERT(dst); 313fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 31477f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com SkScalar a = src[0].fX; 31577f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com SkScalar b = src[1].fX; 31677f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com SkScalar c = src[2].fX; 317fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 31877f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com if (is_not_monotonic(a, b, c)) { 31977f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com SkScalar tValue; 32077f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com if (valid_unit_divide(a - b, a - b - b + c, &tValue)) { 32177f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com SkChopQuadAt(src, dst, tValue); 32277f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com flatten_double_quad_extrema(&dst[0].fX); 32377f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com return 1; 32477f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com } 32577f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com // if we get here, we need to force dst to be monotonic, even though 32677f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com // we couldn't compute a unit_divide value (probably underflow). 32777f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; 32877f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com } 32977f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com dst[0].set(a, src[0].fY); 33077f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com dst[1].set(b, src[1].fY); 33177f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com dst[2].set(c, src[2].fY); 33277f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com return 0; 33377f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com} 33477f0ef726f1f8b6769ed2509171afce8bac00b23reed@android.com 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// F(t) = a (1 - t) ^ 2 + 2 b t (1 - t) + c t ^ 2 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// F'(t) = 2 (b - a) + 2 (a - 2b + c) t 3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// F''(t) = 2 (a - 2b + c) 3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// 3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// A = 2 (b - a) 3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// B = 2 (a - 2b + c) 3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// 3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// Maximum curvature for a quadratic means solving 3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// Fx' Fx'' + Fy' Fy'' = 0 3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// 3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// t = - (Ax Bx + Ay By) / (Bx ^ 2 + By ^ 2) 3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// 3475383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.comfloat SkFindQuadMaxCurvature(const SkPoint src[3]) { 3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Ax = src[1].fX - src[0].fX; 3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Ay = src[1].fY - src[0].fY; 3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Bx = src[0].fX - src[1].fX - src[1].fX + src[2].fX; 3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar By = src[0].fY - src[1].fY - src[1].fY + src[2].fY; 3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar t = 0; // 0 means don't chop 3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (void)valid_unit_divide(-(Ax * Bx + Ay * By), Bx * Bx + By * By, &t); 3555383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com return t; 3565383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com} 3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3585383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.comint SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]) 3595383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com{ 3605383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com SkScalar t = SkFindQuadMaxCurvature(src); 3615383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com if (t == 0) { 3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memcpy(dst, src, 3 * sizeof(SkPoint)); 3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 1; 3645383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com } else { 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChopQuadAt(src, dst, t); 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return 2; 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3708f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com#define SK_ScalarTwoThirds (0.666666666f) 3716fc321a18acc8c6437735007240eefe7054e83b0reed@google.com 3726fc321a18acc8c6437735007240eefe7054e83b0reed@google.comvoid SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]) { 3736fc321a18acc8c6437735007240eefe7054e83b0reed@google.com const SkScalar scale = SK_ScalarTwoThirds; 3746fc321a18acc8c6437735007240eefe7054e83b0reed@google.com dst[0] = src[0]; 3756fc321a18acc8c6437735007240eefe7054e83b0reed@google.com dst[1].set(src[0].fX + SkScalarMul(src[1].fX - src[0].fX, scale), 3766fc321a18acc8c6437735007240eefe7054e83b0reed@google.com src[0].fY + SkScalarMul(src[1].fY - src[0].fY, scale)); 3776fc321a18acc8c6437735007240eefe7054e83b0reed@google.com dst[2].set(src[2].fX + SkScalarMul(src[1].fX - src[2].fX, scale), 3786fc321a18acc8c6437735007240eefe7054e83b0reed@google.com src[2].fY + SkScalarMul(src[1].fY - src[2].fY, scale)); 3796fc321a18acc8c6437735007240eefe7054e83b0reed@google.com dst[3] = src[2]; 380945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com} 381945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////////////// 3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///// CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS ///// 3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////////////// 3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void get_cubic_coeff(const SkScalar pt[], SkScalar coeff[4]) 3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com coeff[0] = pt[6] + 3*(pt[2] - pt[4]) - pt[0]; 3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com coeff[1] = 3*(pt[4] - pt[2] - pt[2] + pt[0]); 3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com coeff[2] = 3*(pt[2] - pt[0]); 3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com coeff[3] = pt[0]; 3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkGetCubicCoeff(const SkPoint pts[4], SkScalar cx[4], SkScalar cy[4]) 3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(pts); 3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (cx) 3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com get_cubic_coeff(&pts[0].fX, cx); 4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (cy) 4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com get_cubic_coeff(&pts[0].fY, cy); 4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_cubic(const SkScalar src[], SkScalar t) 4058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 4068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(src); 4078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t >= 0 && t <= SK_Scalar1); 4088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (t == 0) 4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return src[0]; 4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef DIRECT_EVAL_OF_POLYNOMIALS 4138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar D = src[0]; 4148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = src[6] + 3*(src[2] - src[4]) - D; 4158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = 3*(src[4] - src[2] - src[2] + D); 4168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar C = 3*(src[2] - D); 4178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); 4198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 4208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar ab = SkScalarInterp(src[0], src[2], t); 4218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar bc = SkScalarInterp(src[2], src[4], t); 4228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar cd = SkScalarInterp(src[4], src[6], t); 4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar abc = SkScalarInterp(ab, bc, t); 4248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar bcd = SkScalarInterp(bc, cd, t); 4258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkScalarInterp(abc, bcd, t); 4268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 4278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** return At^2 + Bt + C 4308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 4318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_quadratic(SkScalar A, SkScalar B, SkScalar C, SkScalar t) 4328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t >= 0 && t <= SK_Scalar1); 4348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); 4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_cubic_derivative(const SkScalar src[], SkScalar t) 4398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0]; 4418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = 2*(src[4] - 2 * src[2] + src[0]); 4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar C = src[2] - src[0]; 4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return eval_quadratic(A, B, C, t); 4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar eval_cubic_2ndDerivative(const SkScalar src[], SkScalar t) 4488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 4498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0]; 4508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = src[4] - 2 * src[2] + src[0]; 4518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkScalarMulAdd(A, t, B); 4538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* loc, SkVector* tangent, SkVector* curvature) 4568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 4578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(src); 4588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t >= 0 && t <= SK_Scalar1); 4598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (loc) 4618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com loc->set(eval_cubic(&src[0].fX, t), eval_cubic(&src[0].fY, t)); 4628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (tangent) 4638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com tangent->set(eval_cubic_derivative(&src[0].fX, t), 4648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com eval_cubic_derivative(&src[0].fY, t)); 4658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (curvature) 4668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com curvature->set(eval_cubic_2ndDerivative(&src[0].fX, t), 4678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com eval_cubic_2ndDerivative(&src[0].fY, t)); 4688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Cubic'(t) = At^2 + Bt + C, where 4718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com A = 3(-a + 3(b - c) + d) 4728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com B = 6(a - 2b + c) 4738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com C = 3(b - a) 4748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Solve for t, keeping only those that fit betwee 0 < t < 1 4758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 4768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, SkScalar tValues[2]) 4778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 4788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // we divide A,B,C by 3 to simplify 4798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = d - a + 3*(b - c); 4808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = 2*(a - b - b + c); 4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar C = b - a; 4828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkFindUnitQuadRoots(A, B, C, tValues); 4848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 4858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void interp_cubic_coords(const SkScalar* src, SkScalar* dst, SkScalar t) 4878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 4888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar ab = SkScalarInterp(src[0], src[2], t); 4898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar bc = SkScalarInterp(src[2], src[4], t); 4908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar cd = SkScalarInterp(src[4], src[6], t); 4918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar abc = SkScalarInterp(ab, bc, t); 4928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar bcd = SkScalarInterp(bc, cd, t); 4938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar abcd = SkScalarInterp(abc, bcd, t); 4948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[0] = src[0]; 4968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[2] = ab; 4978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[4] = abc; 4988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[6] = abcd; 4998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[8] = bcd; 5008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[10] = cd; 5018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[12] = src[6]; 5028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 5038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t) 5058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t > 0 && t < SK_Scalar1); 5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com interp_cubic_coords(&src[0].fX, &dst[0].fX, t); 5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com interp_cubic_coords(&src[0].fY, &dst[0].fY, t); 5108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 5118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 512a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com/* http://code.google.com/p/skia/issues/detail?id=32 513fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 514a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com This test code would fail when we didn't check the return result of 515a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com valid_unit_divide in SkChopCubicAt(... tValues[], int roots). The reason is 516a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com that after the first chop, the parameters to valid_unit_divide are equal 517a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com (thanks to finite float precision and rounding in the subtracts). Thus 518a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com even though the 2nd tValue looks < 1.0, after we renormalize it, we end 519a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com up with 1.0, hence the need to check and just return the last cubic as 520a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com a degenerate clump of 4 points in the sampe place. 521a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com 522a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com static void test_cubic() { 523a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com SkPoint src[4] = { 524a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com { 556.25000, 523.03003 }, 525a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com { 556.23999, 522.96002 }, 526a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com { 556.21997, 522.89001 }, 527a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com { 556.21997, 522.82001 } 528a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com }; 529a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com SkPoint dst[10]; 530a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com SkScalar tval[] = { 0.33333334f, 0.99999994f }; 531a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com SkChopCubicAt(src, dst, tval, 2); 532a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com } 533a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com */ 534a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com 5358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopCubicAt(const SkPoint src[4], SkPoint dst[], const SkScalar tValues[], int roots) 5368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 5378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 5388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 5398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int i = 0; i < roots - 1; i++) 5408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 5418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(is_unit_interval(tValues[i])); 5428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(is_unit_interval(tValues[i+1])); 5438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(tValues[i] < tValues[i+1]); 5448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 5478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (dst) 5498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 5508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (roots == 0) // nothing to chop 5518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memcpy(dst, src, 4*sizeof(SkPoint)); 5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar t = tValues[0]; 5558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint tmp[4]; 5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int i = 0; i < roots; i++) 5588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 5598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChopCubicAt(src, dst, t); 5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (i == roots - 1) 5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com break; 5628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst += 3; 564a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com // have src point to the remaining cubic (after the chop) 5658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memcpy(tmp, dst, 4 * sizeof(SkPoint)); 5668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com src = tmp; 567a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com 568a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com // watch out in case the renormalized t isn't in range 569a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com if (!valid_unit_divide(tValues[i+1] - tValues[i], 570a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com SK_Scalar1 - tValues[i], &t)) { 571a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com // if we can't, just create a degenerate cubic 572a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com dst[4] = dst[5] = dst[6] = src[3]; 573a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com break; 574a964028843ec3f69b3b2e556426ff881d1fcb4b2reed@android.com } 5758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 5788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 5798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]) 5818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 5828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); 5838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); 5848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); 5858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); 5868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x23 = SkScalarAve(src[2].fX, src[3].fX); 5878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y23 = SkScalarAve(src[2].fY, src[3].fY); 5888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x012 = SkScalarAve(x01, x12); 5908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y012 = SkScalarAve(y01, y12); 5918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x123 = SkScalarAve(x12, x23); 5928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y123 = SkScalarAve(y12, y23); 5938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[0] = src[0]; 5958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[1].set(x01, y01); 5968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[2].set(x012, y012); 5978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[3].set(SkScalarAve(x012, x123), SkScalarAve(y012, y123)); 5988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[4].set(x123, y123); 5998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[5].set(x23, y23); 6008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[6] = src[3]; 6018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 6028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void flatten_double_cubic_extrema(SkScalar coords[14]) 6048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 6058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com coords[4] = coords[8] = coords[6]; 6068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 6078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that 6098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com the resulting beziers are monotonic in Y. This is called by the scan converter. 6108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Depending on what is returned, dst[] is treated as follows 6118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 0 dst[0..3] is the original cubic 6128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1 dst[0..3] and dst[3..6] are the two new cubics 6138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2 dst[0..3], dst[3..6], dst[6..9] are the three new cubics 6148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com If dst == null, it is ignored and only the count is returned. 6158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 616bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.comint SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]) { 6178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar tValues[2]; 618bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com int roots = SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY, 619bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com src[3].fY, tValues); 620fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 6218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChopCubicAt(src, dst, tValues, roots); 622bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com if (dst && roots > 0) { 6238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // we do some cleanup to ensure our Y extrema are flat 6248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com flatten_double_cubic_extrema(&dst[0].fY); 625bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com if (roots == 2) { 6268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com flatten_double_cubic_extrema(&dst[3].fY); 627bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com } 628bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com } 629bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com return roots; 630bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com} 631bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com 632bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.comint SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]) { 633bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com SkScalar tValues[2]; 634bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com int roots = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX, 635bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com src[3].fX, tValues); 636fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 637bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com SkChopCubicAt(src, dst, tValues, roots); 638bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com if (dst && roots > 0) { 639bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com // we do some cleanup to ensure our Y extrema are flat 640bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com flatten_double_cubic_extrema(&dst[0].fX); 641bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com if (roots == 2) { 642bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com flatten_double_cubic_extrema(&dst[3].fX); 643bb13586591bd412a0372aeb85d44159d2fa3f947reed@android.com } 6448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 6458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return roots; 6468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 6478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** http://www.faculty.idc.ac.il/arik/quality/appendixA.html 6498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Inflection means that curvature is zero. 6518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Curvature is [F' x F''] / [F'^3] 6528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com So we solve F'x X F''y - F'y X F''y == 0 6538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com After some canceling of the cubic term, we get 6548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com A = b - a 6558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com B = c - 2b + a 6568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com C = d - 3c + 3b - a 6578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (BxCy - ByCx)t^2 + (AxCy - AyCx)t + AxBy - AyBx == 0 6588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 6598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[]) 6608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 6618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Ax = src[1].fX - src[0].fX; 6628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Ay = src[1].fY - src[0].fY; 6638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Bx = src[2].fX - 2 * src[1].fX + src[0].fX; 6648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar By = src[2].fY - 2 * src[1].fY + src[0].fY; 6658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Cx = src[3].fX + 3 * (src[1].fX - src[2].fX) - src[0].fX; 6668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar Cy = src[3].fY + 3 * (src[1].fY - src[2].fY) - src[0].fY; 6678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6688f4d2306fa866a26f9448048ff63f692b2ba43aareed@google.com return SkFindUnitQuadRoots(Bx*Cy - By*Cx, Ax*Cy - Ay*Cx, Ax*By - Ay*Bx, tValues); 6698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 6708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkChopCubicAtInflections(const SkPoint src[], SkPoint dst[10]) 6728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 6738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar tValues[2]; 6748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int count = SkFindCubicInflections(src, tValues); 6758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (dst) 6778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 6788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (count == 0) 6798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memcpy(dst, src, 4 * sizeof(SkPoint)); 6808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 6818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChopCubicAt(src, dst, tValues, count); 6828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 6838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return count + 1; 6848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 6858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <typename T> void bubble_sort(T array[], int count) 6878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 6888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int i = count - 1; i > 0; --i) 6898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int j = i; j > 0; --j) 6908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (array[j] < array[j-1]) 6918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 6928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com T tmp(array[j]); 6938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com array[j] = array[j-1]; 6948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com array[j-1] = tmp; 6958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 6968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 6978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// newton refinement 6998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0 7008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar refine_cubic_root(const SkFP coeff[4], SkScalar root) 7018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 7028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // x1 = x0 - f(t) / f'(t) 7038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkFP T = SkScalarToFloat(root); 7058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkFP N, D; 7068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // f' = 3*coeff[0]*T^2 + 2*coeff[1]*T + coeff[2] 7088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com D = SkFPMul(SkFPMul(coeff[0], SkFPMul(T,T)), 3); 7098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com D = SkFPAdd(D, SkFPMulInt(SkFPMul(coeff[1], T), 2)); 7108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com D = SkFPAdd(D, coeff[2]); 7118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (D == 0) 7138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return root; 7148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // f = coeff[0]*T^3 + coeff[1]*T^2 + coeff[2]*T + coeff[3] 7168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com N = SkFPMul(SkFPMul(SkFPMul(T, T), T), coeff[0]); 7178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com N = SkFPAdd(N, SkFPMul(SkFPMul(T, T), coeff[1])); 7188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com N = SkFPAdd(N, SkFPMul(T, coeff[2])); 7198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com N = SkFPAdd(N, coeff[3]); 7208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (N) 7228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 7238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar delta = SkFPToScalar(SkFPDiv(N, D)); 7248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 7258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (delta) 7268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com root -= delta; 7278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 7288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return root; 7298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 7308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 7318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 732087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com/** 733087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com * Given an array and count, remove all pair-wise duplicates from the array, 734087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com * keeping the existing sorting, and return the new count 735087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com */ 736087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.comstatic int collaps_duplicates(float array[], int count) { 737087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com for (int n = count; n > 1; --n) { 738087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com if (array[0] == array[1]) { 739087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com for (int i = 1; i < n; ++i) { 740087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com array[i - 1] = array[i]; 741087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com } 742087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com count -= 1; 743087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com } else { 744087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com array += 1; 745087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com } 746087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com } 747087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com return count; 748087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com} 749087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com 750087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com#ifdef SK_DEBUG 751087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com 752087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com#define TEST_COLLAPS_ENTRY(array) array, SK_ARRAY_COUNT(array) 753087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com 754087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.comstatic void test_collaps_duplicates() { 755087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com static bool gOnce; 756087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com if (gOnce) { return; } 757087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com gOnce = true; 758087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float src0[] = { 0 }; 759087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float src1[] = { 0, 0 }; 760087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float src2[] = { 0, 1 }; 761087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float src3[] = { 0, 0, 0 }; 762087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float src4[] = { 0, 0, 1 }; 763087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float src5[] = { 0, 1, 1 }; 764087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float src6[] = { 0, 1, 2 }; 765087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const struct { 766087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com const float* fData; 767087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com int fCount; 768087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com int fCollapsedCount; 769087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com } data[] = { 770087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com { TEST_COLLAPS_ENTRY(src0), 1 }, 771087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com { TEST_COLLAPS_ENTRY(src1), 1 }, 772087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com { TEST_COLLAPS_ENTRY(src2), 2 }, 773087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com { TEST_COLLAPS_ENTRY(src3), 1 }, 774087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com { TEST_COLLAPS_ENTRY(src4), 2 }, 775087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com { TEST_COLLAPS_ENTRY(src5), 2 }, 776087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com { TEST_COLLAPS_ENTRY(src6), 3 }, 777087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com }; 778087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { 779087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com float dst[3]; 780087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com memcpy(dst, data[i].fData, data[i].fCount * sizeof(dst[0])); 781087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com int count = collaps_duplicates(dst, data[i].fCount); 782087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com SkASSERT(data[i].fCollapsedCount == count); 783087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com for (int j = 1; j < count; ++j) { 784087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com SkASSERT(dst[j-1] < dst[j]); 785087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com } 786087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com } 787087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com} 788087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com#endif 789087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com 7903c12840b234e614faf569e80f311a77ce65d9fe0reed@google.comstatic SkScalar SkScalarCubeRoot(SkScalar x) { 7913c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com return sk_float_pow(x, 0.3333333f); 7923c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com} 7933c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com 7948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* Solve coeff(t) == 0, returning the number of roots that 7958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com lie withing 0 < t < 1. 7968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3] 797fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 798087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com Eliminates repeated roots (so that all tValues are distinct, and are always 799087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com in increasing order. 8008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 8013c12840b234e614faf569e80f311a77ce65d9fe0reed@google.comstatic int solve_cubic_polynomial(const SkScalar coeff[4], SkScalar tValues[3]) 8028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 8038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (SkScalarNearlyZero(coeff[0])) // we're just a quadratic 8048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 8058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues); 8068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 8078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8083c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com SkScalar a, b, c, Q, R; 8098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 8118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(coeff[0] != 0); 8128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8133c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com SkScalar inva = SkScalarInvert(coeff[0]); 8143c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com a = coeff[1] * inva; 8153c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com b = coeff[2] * inva; 8163c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com c = coeff[3] * inva; 8178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 8183c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com Q = (a*a - b*3) / 9; 8193c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com R = (2*a*a*a - 9*a*b + 27*c) / 54; 8208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8213c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com SkScalar Q3 = Q * Q * Q; 8223c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com SkScalar R2MinusQ3 = R * R - Q3; 8233c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com SkScalar adiv3 = a / 3; 8248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar* roots = tValues; 8268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar r; 8278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8283c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com if (R2MinusQ3 < 0) // we have 3 real roots 8298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 8308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com float theta = sk_float_acos(R / sk_float_sqrt(Q3)); 8318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com float neg2RootQ = -2 * sk_float_sqrt(Q); 8328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r = neg2RootQ * sk_float_cos(theta/3) - adiv3; 8348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (is_unit_interval(r)) 8358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *roots++ = r; 8368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r = neg2RootQ * sk_float_cos((theta + 2*SK_ScalarPI)/3) - adiv3; 8388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (is_unit_interval(r)) 8398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *roots++ = r; 8408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r = neg2RootQ * sk_float_cos((theta - 2*SK_ScalarPI)/3) - adiv3; 8428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (is_unit_interval(r)) 8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *roots++ = r; 8448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 845087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com SkDEBUGCODE(test_collaps_duplicates();) 846087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com 8478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // now sort the roots 848087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com int count = (int)(roots - tValues); 849087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com SkASSERT((unsigned)count <= 3); 850087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com bubble_sort(tValues, count); 851087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com count = collaps_duplicates(tValues, count); 852087d5aafb18b88dfc6c6a5dbf59160c8be914e62reed@google.com roots = tValues + count; // so we compute the proper count below 8538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 8548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else // we have 1 real root 8558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 8563c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com SkScalar A = SkScalarAbs(R) + SkScalarSqrt(R2MinusQ3); 8573c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com A = SkScalarCubeRoot(A); 8583c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com if (R > 0) 8593c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com A = -A; 8608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (A != 0) 8623c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com A += Q / A; 8633c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com r = A - adiv3; 8648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (is_unit_interval(r)) 8658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *roots++ = r; 8668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 8678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (int)(roots - tValues); 8698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 8708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* Looking for F' dot F'' == 0 872fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 8738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com A = b - a 8748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com B = c - 2b + a 8758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com C = d - 3c + 3b - a 8768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com F' = 3Ct^2 + 6Bt + 3A 8788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com F'' = 6Ct + 6B 8798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB 8818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 8823c12840b234e614faf569e80f311a77ce65d9fe0reed@google.comstatic void formulate_F1DotF2(const SkScalar src[], SkScalar coeff[4]) 8838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 8848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar a = src[2] - src[0]; 8858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar b = src[4] - 2 * src[2] + src[0]; 8868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar c = src[6] + 3 * (src[2] - src[4]) - src[0]; 8878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8883c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com coeff[0] = c * c; 8893c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com coeff[1] = 3 * b * c; 8903c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com coeff[2] = 2 * b * b + c * a; 8913c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com coeff[3] = a * b; 8928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 8938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// EXPERIMENTAL: can set this to zero to accept all t-values 0 < t < 1 8958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//#define kMinTValueForChopping (SK_Scalar1 / 256) 8968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define kMinTValueForChopping 0 8978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* Looking for F' dot F'' == 0 899fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 9008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com A = b - a 9018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com B = c - 2b + a 9028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com C = d - 3c + 3b - a 9038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com F' = 3Ct^2 + 6Bt + 3A 9058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com F'' = 6Ct + 6B 9068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB 9088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 9098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]) 9108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 9113c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com SkScalar coeffX[4], coeffY[4]; 9123c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com int i; 9138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com formulate_F1DotF2(&src[0].fX, coeffX); 9158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com formulate_F1DotF2(&src[0].fY, coeffY); 9168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (i = 0; i < 4; i++) 9183c12840b234e614faf569e80f311a77ce65d9fe0reed@google.com coeffX[i] += coeffY[i]; 9198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar t[3]; 9218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int count = solve_cubic_polynomial(coeffX, t); 9228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int maxCount = 0; 9238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // now remove extrema where the curvature is zero (mins) 9258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // !!!! need a test for this !!!! 9268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (i = 0; i < count; i++) 9278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 9288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // if (not_min_curvature()) 9298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (t[i] > kMinTValueForChopping && t[i] < SK_Scalar1 - kMinTValueForChopping) 9308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com tValues[maxCount++] = t[i]; 9318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 9328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return maxCount; 9338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 9348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], SkScalar tValues[3]) 9368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 9378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar t_storage[3]; 9388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (tValues == NULL) 9408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com tValues = t_storage; 9418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int count = SkFindCubicMaxCurvature(src, tValues); 9438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9445383a7525355dec72efa2083aeadffdd09a962b9egdaniel@google.com if (dst) { 9458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (count == 0) 9468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memcpy(dst, src, 4 * sizeof(SkPoint)); 9478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 9488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChopCubicAt(src, dst, tValues, count); 9498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 9508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return count + 1; 9518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 9528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9532e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.orgbool SkXRayCrossesMonotonicCubic(const SkXRay& pt, const SkPoint cubic[4], bool* ambiguous) { 9542e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 9552e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = false; 9562e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 9572e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org 958945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Find the minimum and maximum y of the extrema, which are the 959945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // first and last points since this cubic is monotonic 960945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar min_y = SkMinScalar(cubic[0].fY, cubic[3].fY); 961945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar max_y = SkMaxScalar(cubic[0].fY, cubic[3].fY); 962945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 963945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fY == cubic[0].fY 964945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com || pt.fY < min_y 965945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com || pt.fY > max_y) { 966945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // The query line definitely does not cross the curve 9672e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 9682e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = (pt.fY == cubic[0].fY); 9692e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 970945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 971945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } 972945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 9732e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org bool pt_at_extremum = (pt.fY == cubic[3].fY); 9742e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org 975945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar min_x = 976945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkMinScalar( 977945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkMinScalar( 978945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkMinScalar(cubic[0].fX, cubic[1].fX), 979945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com cubic[2].fX), 980945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com cubic[3].fX); 981945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fX < min_x) { 982945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // The query line definitely crosses the curve 9832e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 9842e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = pt_at_extremum; 9852e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 986945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return true; 987945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } 988945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 989945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar max_x = 990945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkMaxScalar( 991945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkMaxScalar( 992945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkMaxScalar(cubic[0].fX, cubic[1].fX), 993945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com cubic[2].fX), 994945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com cubic[3].fX); 995945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fX > max_x) { 996945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // The query line definitely does not cross the curve 997945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 998945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } 999945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 1000945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Do a binary search to find the parameter value which makes y as 1001945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // close as possible to the query point. See whether the query 1002945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // line's origin is to the left of the associated x coordinate. 1003945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 1004945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // kMaxIter is chosen as the number of mantissa bits for a float, 1005945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // since there's no way we are going to get more precision by 1006945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // iterating more times than that. 1007945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com const int kMaxIter = 23; 1008945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkPoint eval; 1009945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com int iter = 0; 1010945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar upper_t; 1011945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar lower_t; 1012945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // Need to invert direction of t parameter if cubic goes up 1013945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com // instead of down 1014945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (cubic[3].fY > cubic[0].fY) { 1015945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com upper_t = SK_Scalar1; 10164b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org lower_t = 0; 1017945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } else { 10184b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org upper_t = 0; 1019945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com lower_t = SK_Scalar1; 1020945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } 1021945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com do { 1022945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkScalar t = SkScalarAve(upper_t, lower_t); 1023945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkEvalCubicAt(cubic, t, &eval, NULL, NULL); 1024945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fY > eval.fY) { 1025945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com lower_t = t; 1026945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } else { 1027945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com upper_t = t; 1028945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } 1029945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } while (++iter < kMaxIter 1030945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com && !SkScalarNearlyZero(eval.fY - pt.fY)); 1031945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (pt.fX <= eval.fX) { 10322e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 10332e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = pt_at_extremum; 10342e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 1035945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return true; 1036945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com } 1037945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return false; 1038945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com} 1039945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com 10402e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.orgint SkNumXRayCrossingsForCubic(const SkXRay& pt, const SkPoint cubic[4], bool* ambiguous) { 1041945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com int num_crossings = 0; 1042945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com SkPoint monotonic_cubics[10]; 1043945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com int num_monotonic_cubics = SkChopCubicAtYExtrema(cubic, monotonic_cubics); 10442e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 10452e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous = false; 10462e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 10472e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org bool locally_ambiguous; 10482e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (SkXRayCrossesMonotonicCubic(pt, &monotonic_cubics[0], &locally_ambiguous)) 1049945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com ++num_crossings; 10502e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 10512e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous |= locally_ambiguous; 10522e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 1053945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (num_monotonic_cubics > 0) 10542e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (SkXRayCrossesMonotonicCubic(pt, &monotonic_cubics[3], &locally_ambiguous)) 1055945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com ++num_crossings; 10562e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 10572e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous |= locally_ambiguous; 10582e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 1059945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com if (num_monotonic_cubics > 1) 10602e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (SkXRayCrossesMonotonicCubic(pt, &monotonic_cubics[6], &locally_ambiguous)) 1061945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com ++num_crossings; 10622e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org if (ambiguous) { 10632e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org *ambiguous |= locally_ambiguous; 10642e086190e55a01dc2f7b74df6f2828e8cac2b9abkbr@chromium.org } 1065945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com return num_crossings; 1066945a139553a9c9da03766213661d7f5fd6ed3042reed@android.com} 10678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////// 10688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 10698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* Find t value for quadratic [a, b, c] = d. 10708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Return 0 if there is no solution within [0, 1) 10718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 10728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar quad_solve(SkScalar a, SkScalar b, SkScalar c, SkScalar d) 10738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 10748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // At^2 + Bt + C = d 10758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar A = a - 2 * b + c; 10768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar B = 2 * (b - a); 10778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar C = a - d; 10788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 10798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar roots[2]; 10808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int count = SkFindUnitQuadRoots(A, B, C, roots); 10818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 10828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(count <= 1); 10838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return count == 1 ? roots[0] : 0; 10848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 10858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1086e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com/* given a quad-curve and a point (x,y), chop the quad at that point and place 108750df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com the new off-curve point and endpoint into 'dest'. 10889e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com Should only return false if the computed pos is the start of the curve 1089e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com (i.e. root == 0) 10908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1091e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.comstatic bool truncate_last_curve(const SkPoint quad[3], SkScalar x, SkScalar y, SkPoint* dest) 10928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 10938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const SkScalar* base; 10948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar value; 10958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 10968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (SkScalarAbs(x) < SkScalarAbs(y)) { 10978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com base = &quad[0].fX; 10988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = x; 10998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } else { 11008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com base = &quad[0].fY; 11018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = y; 11028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 11038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // note: this returns 0 if it thinks value is out of range, meaning the 11058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // root might return something outside of [0, 1) 11068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar t = quad_solve(base[0], base[2], base[4], value); 11078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (t > 0) 11098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 11108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint tmp[5]; 11118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChopQuadAt(quad, tmp, t); 1112e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com dest[0] = tmp[1]; 1113b0889a5aa610552bf306edc8d9a35d2d601acdb9robertphillips@google.com dest[1].set(x, y); 11148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return true; 11158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } else { 11168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /* t == 0 means either the value triggered a root outside of [0, 1) 11178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com For our purposes, we can ignore the <= 0 roots, but we want to 11188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com catch the >= 1 roots (which given our caller, will basically mean 11198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a root of 1, give-or-take numerical instability). If we are in the 11208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com >= 1 case, return the existing offCurve point. 1121fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 11228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The test below checks to see if we are close to the "end" of the 11238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com curve (near base[4]). Rather than specifying a tolerance, I just 11248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com check to see if value is on to the right/left of the middle point 11258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (depending on the direction/sign of the end points). 11268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 11278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if ((base[0] < base[4] && value > base[2]) || 11288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (base[0] > base[4] && value < base[2])) // should root have been 1 11298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 1130e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com dest[0] = quad[1]; 1131e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com dest[1].set(x, y); 11328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return true; 11338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 11348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 11358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return false; 11368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 11378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const SkPoint gQuadCirclePts[kSkBuildQuadArcStorage] = { 1139f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// The mid point of the quadratic arc approximation is half way between the two 1140f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// control points. The float epsilon adjustment moves the on curve point out by 1141f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// two bits, distributing the convex test error between the round rect approximation 1142f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org// and the convex cross product sign equality test. 1143f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org#define SK_MID_RRECT_OFFSET (SK_Scalar1 + SK_ScalarTanPIOver8 + FLT_EPSILON * 4) / 2 1144f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_Scalar1, 0 }, 1145f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_Scalar1, SK_ScalarTanPIOver8 }, 1146f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_MID_RRECT_OFFSET, SK_MID_RRECT_OFFSET }, 1147f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_ScalarTanPIOver8, SK_Scalar1 }, 1148f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org 1149f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { 0, SK_Scalar1 }, 1150f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { -SK_ScalarTanPIOver8, SK_Scalar1 }, 1151f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { -SK_MID_RRECT_OFFSET, SK_MID_RRECT_OFFSET }, 1152f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { -SK_Scalar1, SK_ScalarTanPIOver8 }, 1153f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org 1154f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { -SK_Scalar1, 0 }, 1155f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { -SK_Scalar1, -SK_ScalarTanPIOver8 }, 1156f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { -SK_MID_RRECT_OFFSET, -SK_MID_RRECT_OFFSET }, 1157f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { -SK_ScalarTanPIOver8, -SK_Scalar1 }, 1158f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org 1159f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { 0, -SK_Scalar1 }, 1160f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_ScalarTanPIOver8, -SK_Scalar1 }, 1161f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_MID_RRECT_OFFSET, -SK_MID_RRECT_OFFSET }, 1162f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_Scalar1, -SK_ScalarTanPIOver8 }, 1163f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org 1164f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org { SK_Scalar1, 0 } 1165f91aaecbe9d3630123d803d80f7b29f06c8976c7commit-bot@chromium.org#undef SK_MID_RRECT_OFFSET 11668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 11678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkBuildQuadArc(const SkVector& uStart, const SkVector& uStop, 11698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRotationDirection dir, const SkMatrix* userMatrix, 11708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint quadPoints[]) 11718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 11728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // rotate by x,y so that uStart is (1.0) 11738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x = SkPoint::DotProduct(uStart, uStop); 11748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar y = SkPoint::CrossProduct(uStart, uStop); 11758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar absX = SkScalarAbs(x); 11778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar absY = SkScalarAbs(y); 11788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int pointCount; 11808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // check for (effectively) coincident vectors 11828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // this can happen if our angle is nearly 0 or nearly 180 (y == 0) 11838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // ... we use the dot-prod to distinguish between 0 and 180 (x > 0) 11848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (absY <= SK_ScalarNearlyZero && x > 0 && 11858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ((y >= 0 && kCW_SkRotationDirection == dir) || 11868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (y <= 0 && kCCW_SkRotationDirection == dir))) { 1187fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com 11888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // just return the start-point 11898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com quadPoints[0].set(SK_Scalar1, 0); 11908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pointCount = 1; 11918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } else { 11928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (dir == kCCW_SkRotationDirection) 11938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com y = -y; 11948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // what octant (quadratic curve) is [xy] in? 11968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int oct = 0; 11978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bool sameSign = true; 11988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (0 == y) 12008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 12018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com oct = 4; // 180 12028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(SkScalarAbs(x + SK_Scalar1) <= SK_ScalarNearlyZero); 12038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else if (0 == x) 12058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 12068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(absY - SK_Scalar1 <= SK_ScalarNearlyZero); 12078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (y > 0) 12088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com oct = 2; // 90 12098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 12108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com oct = 6; // 270 12118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 12138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 12148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (y < 0) 12158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com oct += 4; 12168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if ((x < 0) != (y < 0)) 12178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 12188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com oct += 2; 12198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sameSign = false; 12208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if ((absX < absY) == sameSign) 12228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com oct += 1; 12238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 12258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int wholeCount = oct << 1; 12268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memcpy(quadPoints, gQuadCirclePts, (wholeCount + 1) * sizeof(SkPoint)); 12278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 12288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const SkPoint* arc = &gQuadCirclePts[wholeCount]; 1229e1b75b4096c8ba9a569ae33d580806edd3c4a97arobertphillips@google.com if (truncate_last_curve(arc, x, y, &quadPoints[wholeCount + 1])) 12308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 12318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com wholeCount += 2; 12328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pointCount = wholeCount + 1; 12348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 12368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // now handle counter-clockwise and the initial unitStart rotation 12378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkMatrix matrix; 12388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix.setSinCos(uStart.fY, uStart.fX); 12398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (dir == kCCW_SkRotationDirection) { 12408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix.preScale(SK_Scalar1, -SK_Scalar1); 12418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (userMatrix) { 12438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix.postConcat(*userMatrix); 12448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com matrix.mapPoints(quadPoints, pointCount); 12468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return pointCount; 12478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1248c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com 1249c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com/////////////////////////////////////////////////////////////////////////////// 1250c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com 125117a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w) 125217a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// ------------------------------------------ 125317a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// ((1 - t)^2 + t^2 + 2 (1 - t) t w) 125417a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// 125517a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0} 125617a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// ------------------------------------------------ 125717a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// {t^2 (2 - 2 w), t (-2 + 2 w), 1} 125817a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// 125917a2c919d095797c364d407a5dbdb4d60533b953reed@google.com 126017a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// Take the parametric specification for the conic (either X or Y) and return 126117a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// in coeff[] the coefficients for the simple quadratic polynomial 126217a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// coeff[0] for t^2 126317a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// coeff[1] for t 126417a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// coeff[2] for constant term 126517a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// 12666862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic SkScalar conic_eval_pos(const SkScalar src[], SkScalar w, SkScalar t) { 12670c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkASSERT(src); 12680c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkASSERT(t >= 0 && t <= SK_Scalar1); 126945fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com 12700c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar src2w = SkScalarMul(src[2], w); 12710c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar C = src[0]; 12720c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar A = src[4] - 2 * src2w + C; 12730c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar B = 2 * (src2w - C); 12740c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar numer = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); 127545fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com 12760c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org B = 2 * (w - SK_Scalar1); 12770c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org C = SK_Scalar1; 12780c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org A = -B; 12790c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar denom = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); 128045fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com 12810c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org return SkScalarDiv(numer, denom); 12820c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org} 128317a2c919d095797c364d407a5dbdb4d60533b953reed@google.com 12840c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org// F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w) 12850c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org// 12866862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w) 12876862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w) 12886862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// t^0 : -2 P0 w + 2 P1 w 12896862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// 12906862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// We disregard magnitude, so we can freely ignore the denominator of F', and 12916862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// divide the numerator by 2 12920c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org// 129317a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// coeff[0] for t^2 12946862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// coeff[1] for t^1 12956862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org// coeff[2] for t^0 129617a2c919d095797c364d407a5dbdb4d60533b953reed@google.com// 12976862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic void conic_deriv_coeff(const SkScalar src[], SkScalar w, SkScalar coeff[3]) { 12986862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org const SkScalar P20 = src[4] - src[0]; 12996862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org const SkScalar P10 = src[2] - src[0]; 13006862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org const SkScalar wP10 = w * P10; 13016862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org coeff[0] = w * P20 - P20; 13026862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org coeff[1] = P20 - 2 * wP10; 13036862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org coeff[2] = wP10; 13046862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org} 13056862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org 13066862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic SkScalar conic_eval_tan(const SkScalar coord[], SkScalar w, SkScalar t) { 13076862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org SkScalar coeff[3]; 13086862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org conic_deriv_coeff(coord, w, coeff); 13096862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org return t * (t * coeff[0] + coeff[1]) + coeff[2]; 13100c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org} 13110c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org 13126862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.orgstatic bool conic_find_extrema(const SkScalar src[], SkScalar w, SkScalar* t) { 13130c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar coeff[3]; 13146862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org conic_deriv_coeff(src, w, coeff); 13150c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org 13160c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar tValues[2]; 13170c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org int roots = SkFindUnitQuadRoots(coeff[0], coeff[1], coeff[2], tValues); 13180c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkASSERT(0 == roots || 1 == roots); 131945fb8b60137c65106b7903285672d0f8a8041f97skia.committer@gmail.com 13200c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org if (1 == roots) { 13210c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org *t = tValues[0]; 13220c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org return true; 13230c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org } 13240c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org return false; 132517a2c919d095797c364d407a5dbdb4d60533b953reed@google.com} 132617a2c919d095797c364d407a5dbdb4d60533b953reed@google.com 13270d099557feb99707c8f601746f46f5a295eb33b7reed@google.comstruct SkP3D { 13280d099557feb99707c8f601746f46f5a295eb33b7reed@google.com SkScalar fX, fY, fZ; 13294bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com 13300d099557feb99707c8f601746f46f5a295eb33b7reed@google.com void set(SkScalar x, SkScalar y, SkScalar z) { 13310d099557feb99707c8f601746f46f5a295eb33b7reed@google.com fX = x; fY = y; fZ = z; 13320d099557feb99707c8f601746f46f5a295eb33b7reed@google.com } 13334bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com 13340d099557feb99707c8f601746f46f5a295eb33b7reed@google.com void projectDown(SkPoint* dst) const { 13350d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst->set(fX / fZ, fY / fZ); 13360d099557feb99707c8f601746f46f5a295eb33b7reed@google.com } 13370d099557feb99707c8f601746f46f5a295eb33b7reed@google.com}; 13380d099557feb99707c8f601746f46f5a295eb33b7reed@google.com 1339d50d87a185d26a38d7100bbbb119be17947aa5e4commit-bot@chromium.org// We only interpolate one dimension at a time (the first, at +0, +3, +6). 1340d50d87a185d26a38d7100bbbb119be17947aa5e4commit-bot@chromium.orgstatic void p3d_interp(const SkScalar src[7], SkScalar dst[7], SkScalar t) { 13410d099557feb99707c8f601746f46f5a295eb33b7reed@google.com SkScalar ab = SkScalarInterp(src[0], src[3], t); 13420d099557feb99707c8f601746f46f5a295eb33b7reed@google.com SkScalar bc = SkScalarInterp(src[3], src[6], t); 13430d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[0] = ab; 13440d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[3] = SkScalarInterp(ab, bc, t); 13450d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[6] = bc; 13460d099557feb99707c8f601746f46f5a295eb33b7reed@google.com} 13470d099557feb99707c8f601746f46f5a295eb33b7reed@google.com 13480d099557feb99707c8f601746f46f5a295eb33b7reed@google.comstatic void ratquad_mapTo3D(const SkPoint src[3], SkScalar w, SkP3D dst[]) { 13490d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[0].set(src[0].fX * 1, src[0].fY * 1, 1); 13500d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[1].set(src[1].fX * w, src[1].fY * w, w); 13510d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[2].set(src[2].fX * 1, src[2].fY * 1, 1); 13520d099557feb99707c8f601746f46f5a295eb33b7reed@google.com} 13530d099557feb99707c8f601746f46f5a295eb33b7reed@google.com 135424bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.comvoid SkConic::evalAt(SkScalar t, SkPoint* pt, SkVector* tangent) const { 1355c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com SkASSERT(t >= 0 && t <= SK_Scalar1); 13564bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com 1357c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com if (pt) { 13586862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org pt->set(conic_eval_pos(&fPts[0].fX, fW, t), 13596862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org conic_eval_pos(&fPts[0].fY, fW, t)); 1360c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com } 136124bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com if (tangent) { 136224bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com tangent->set(conic_eval_tan(&fPts[0].fX, fW, t), 136324bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com conic_eval_tan(&fPts[0].fY, fW, t)); 136424bd210f2e2cf66f356b4e98f7801631089b8aa3reed@google.com } 1365c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com} 1366c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com 136728552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::chopAt(SkScalar t, SkConic dst[2]) const { 13680d099557feb99707c8f601746f46f5a295eb33b7reed@google.com SkP3D tmp[3], tmp2[3]; 13690d099557feb99707c8f601746f46f5a295eb33b7reed@google.com 13700d099557feb99707c8f601746f46f5a295eb33b7reed@google.com ratquad_mapTo3D(fPts, fW, tmp); 13714bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com 13720d099557feb99707c8f601746f46f5a295eb33b7reed@google.com p3d_interp(&tmp[0].fX, &tmp2[0].fX, t); 13730d099557feb99707c8f601746f46f5a295eb33b7reed@google.com p3d_interp(&tmp[0].fY, &tmp2[0].fY, t); 13740d099557feb99707c8f601746f46f5a295eb33b7reed@google.com p3d_interp(&tmp[0].fZ, &tmp2[0].fZ, t); 13754bb50b22fce5c4031549976cf7b04d63cc22e624skia.committer@gmail.com 13760d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[0].fPts[0] = fPts[0]; 13770d099557feb99707c8f601746f46f5a295eb33b7reed@google.com tmp2[0].projectDown(&dst[0].fPts[1]); 13780d099557feb99707c8f601746f46f5a295eb33b7reed@google.com tmp2[1].projectDown(&dst[0].fPts[2]); dst[1].fPts[0] = dst[0].fPts[2]; 13790d099557feb99707c8f601746f46f5a295eb33b7reed@google.com tmp2[2].projectDown(&dst[1].fPts[1]); 13800d099557feb99707c8f601746f46f5a295eb33b7reed@google.com dst[1].fPts[2] = fPts[2]; 13810d099557feb99707c8f601746f46f5a295eb33b7reed@google.com 13824af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org // to put in "standard form", where w0 and w2 are both 1, we compute the 13834af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org // new w1 as sqrt(w1*w1/w0*w2) 13844af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org // or 13854af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org // w1 /= sqrt(w0*w2) 13864af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org // 13874af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org // However, in our case, we know that for dst[0], w0 == 1, and for dst[1], w2 == 1 13884af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org // 13894af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org SkScalar root = SkScalarSqrt(tmp2[1].fZ); 13904af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org dst[0].fW = tmp2[0].fZ / root; 13914af6280aa366a02540f34c48f89ea73ce3d27974mike@reedtribe.org dst[1].fW = tmp2[2].fZ / root; 1392c518710d9a99c4d0adf759a102f4a1cb582f5939reed@google.com} 13938d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org 13943df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.orgstatic SkScalar subdivide_w_value(SkScalar w) { 13956862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org return SkScalarSqrt(SK_ScalarHalf + w * SK_ScalarHalf); 13963df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org} 13973df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org 139828552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::chop(SkConic dst[2]) const { 13998d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org SkScalar scale = SkScalarInvert(SK_Scalar1 + fW); 14008d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org SkScalar p1x = fW * fPts[1].fX; 14018d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org SkScalar p1y = fW * fPts[1].fY; 14028d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org SkScalar mx = (fPts[0].fX + 2 * p1x + fPts[2].fX) * scale * SK_ScalarHalf; 14038d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org SkScalar my = (fPts[0].fY + 2 * p1y + fPts[2].fY) * scale * SK_ScalarHalf; 14048d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org 14058d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org dst[0].fPts[0] = fPts[0]; 14068d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org dst[0].fPts[1].set((fPts[0].fX + p1x) * scale, 14078d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org (fPts[0].fY + p1y) * scale); 14088d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org dst[0].fPts[2].set(mx, my); 14098d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org 14108d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org dst[1].fPts[0].set(mx, my); 14118d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org dst[1].fPts[1].set((p1x + fPts[2].fX) * scale, 14128d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org (p1y + fPts[2].fY) * scale); 14138d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org dst[1].fPts[2] = fPts[2]; 14148d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org 14153df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org dst[0].fW = dst[1].fW = subdivide_w_value(fW); 14163df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org} 14173df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org 141897514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org/* 141997514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org * "High order approximation of conic sections by quadratic splines" 142097514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org * by Michael Floater, 1993 142197514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org */ 1422af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org#define AS_QUAD_ERROR_SETUP \ 1423af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org SkScalar a = fW - 1; \ 1424af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org SkScalar k = a / (4 * (2 + a)); \ 1425af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org SkScalar x = k * (fPts[0].fX - 2 * fPts[1].fX + fPts[2].fX); \ 1426af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org SkScalar y = k * (fPts[0].fY - 2 * fPts[1].fY + fPts[2].fY); 1427af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org 1428af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.orgvoid SkConic::computeAsQuadError(SkVector* err) const { 1429af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org AS_QUAD_ERROR_SETUP 1430af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org err->set(x, y); 1431af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org} 1432af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org 1433af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.orgbool SkConic::asQuadTol(SkScalar tol) const { 1434af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org AS_QUAD_ERROR_SETUP 1435af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org return (x * x + y * y) <= tol * tol; 143697514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org} 14377841c63136e8aa2d3aadbeab8432405abcd73c32skia.committer@gmail.com 143897514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.orgint SkConic::computeQuadPOW2(SkScalar tol) const { 1439af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org AS_QUAD_ERROR_SETUP 1440af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org SkScalar error = SkScalarSqrt(x * x + y * y) - tol; 1441af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org 1442af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org if (error <= 0) { 144397514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org return 0; 14443df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org } 144597514f22e4cb85a0d79b089a1eecd54d4a952291mike@reedtribe.org uint32_t ierr = (uint32_t)error; 1446af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org return (34 - SkCLZ(ierr)) >> 1; 14473df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org} 14483df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org 144928552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgstatic SkPoint* subdivide(const SkConic& src, SkPoint pts[], int level) { 14503df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org SkASSERT(level >= 0); 1451af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org 14523df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org if (0 == level) { 14533df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org memcpy(pts, &src.fPts[1], 2 * sizeof(SkPoint)); 14543df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org return pts + 2; 14553df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org } else { 145628552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.org SkConic dst[2]; 14573df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org src.chop(dst); 14583df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org --level; 14593df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org pts = subdivide(dst[0], pts, level); 14603df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org return subdivide(dst[1], pts, level); 14613df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org } 14628d551011966a1bc14a654dbde704f343c0e222b6mike@reedtribe.org} 14633df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org 146428552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgint SkConic::chopIntoQuadsPOW2(SkPoint pts[], int pow2) const { 1465af5c506cd6b63f43a0ebee2fb171ea55ba98e09fmike@reedtribe.org SkASSERT(pow2 >= 0); 14663df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org *pts = fPts[0]; 1467aebfa7e1b1e94f693f3e7beb6ad43cfcb0f69e98reed@google.com SkDEBUGCODE(SkPoint* endPts =) subdivide(*this, pts + 1, pow2); 14683df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org SkASSERT(endPts - pts == (2 * (1 << pow2) + 1)); 14693df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org return 1 << pow2; 14703df87cb36e9f9d2e04d2f81ac64cf3d778c33847mike@reedtribe.org} 14710c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org 147228552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::findXExtrema(SkScalar* t) const { 14736862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org return conic_find_extrema(&fPts[0].fX, fW, t); 14740c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org} 14750c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org 147628552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::findYExtrema(SkScalar* t) const { 14776862cbad085729acb77825392c8c2a4f888a99cfmike@reedtribe.org return conic_find_extrema(&fPts[0].fY, fW, t); 14780c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org} 14790c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org 148028552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::chopAtXExtrema(SkConic dst[2]) const { 14810c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar t; 14820c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org if (this->findXExtrema(&t)) { 14830c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org this->chopAt(t, dst); 14840c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org // now clean-up the middle, since we know t was meant to be at 14850c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org // an X-extrema 14860c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar value = dst[0].fPts[2].fX; 14870c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org dst[0].fPts[1].fX = value; 14880c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org dst[1].fPts[0].fX = value; 14890c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org dst[1].fPts[1].fX = value; 14900c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org return true; 14910c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org } 14920c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org return false; 14930c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org} 14940c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org 149528552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgbool SkConic::chopAtYExtrema(SkConic dst[2]) const { 14960c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar t; 14970c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org if (this->findYExtrema(&t)) { 14980c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org this->chopAt(t, dst); 14990c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org // now clean-up the middle, since we know t was meant to be at 15000c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org // an Y-extrema 15010c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org SkScalar value = dst[0].fPts[2].fY; 15020c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org dst[0].fPts[1].fY = value; 15030c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org dst[1].fPts[0].fY = value; 15040c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org dst[1].fPts[1].fY = value; 15050c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org return true; 15060c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org } 15070c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org return false; 15080c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org} 15090c5c3867bdbde1005a7bbb9de9df93cc10e27782mike@reedtribe.org 151028552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::computeTightBounds(SkRect* bounds) const { 15115c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org SkPoint pts[4]; 15125c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org pts[0] = fPts[0]; 15135c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org pts[1] = fPts[2]; 15145c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org int count = 2; 15155c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org 15165c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org SkScalar t; 15175c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org if (this->findXExtrema(&t)) { 15185c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org this->evalAt(t, &pts[count++]); 15195c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org } 15205c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org if (this->findYExtrema(&t)) { 15215c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org this->evalAt(t, &pts[count++]); 15225c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org } 15235c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org bounds->set(pts, count); 15245c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org} 15255c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org 152628552e12a019bf5ae55c9e8602bbe216562d7a3emike@reedtribe.orgvoid SkConic::computeFastBounds(SkRect* bounds) const { 15275c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org bounds->set(fPts, 3); 15285c082a14acbb70eec2fd6dc5a4c134799f3d8535mike@reedtribe.org} 1529