michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrPathUtils.h" michael@0: michael@0: #include "GrPoint.h" michael@0: #include "SkGeometry.h" michael@0: michael@0: SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol, michael@0: const SkMatrix& viewM, michael@0: const SkRect& pathBounds) { michael@0: // In order to tesselate the path we get a bound on how much the matrix can michael@0: // stretch when mapping to screen coordinates. michael@0: SkScalar stretch = viewM.getMaxStretch(); michael@0: SkScalar srcTol = devTol; michael@0: michael@0: if (stretch < 0) { michael@0: // take worst case mapRadius amoung four corners. michael@0: // (less than perfect) michael@0: for (int i = 0; i < 4; ++i) { michael@0: SkMatrix mat; michael@0: mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight, michael@0: (i < 2) ? pathBounds.fTop : pathBounds.fBottom); michael@0: mat.postConcat(viewM); michael@0: stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1)); michael@0: } michael@0: } michael@0: srcTol = SkScalarDiv(srcTol, stretch); michael@0: return srcTol; michael@0: } michael@0: michael@0: static const int MAX_POINTS_PER_CURVE = 1 << 10; michael@0: static const SkScalar gMinCurveTol = 0.0001f; michael@0: michael@0: uint32_t GrPathUtils::quadraticPointCount(const GrPoint points[], michael@0: SkScalar tol) { michael@0: if (tol < gMinCurveTol) { michael@0: tol = gMinCurveTol; michael@0: } michael@0: SkASSERT(tol > 0); michael@0: michael@0: SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]); michael@0: if (d <= tol) { michael@0: return 1; michael@0: } else { michael@0: // Each time we subdivide, d should be cut in 4. So we need to michael@0: // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x) michael@0: // points. michael@0: // 2^(log4(x)) = sqrt(x); michael@0: int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol))); michael@0: int pow2 = GrNextPow2(temp); michael@0: // Because of NaNs & INFs we can wind up with a degenerate temp michael@0: // such that pow2 comes out negative. Also, our point generator michael@0: // will always output at least one pt. michael@0: if (pow2 < 1) { michael@0: pow2 = 1; michael@0: } michael@0: return GrMin(pow2, MAX_POINTS_PER_CURVE); michael@0: } michael@0: } michael@0: michael@0: uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0, michael@0: const GrPoint& p1, michael@0: const GrPoint& p2, michael@0: SkScalar tolSqd, michael@0: GrPoint** points, michael@0: uint32_t pointsLeft) { michael@0: if (pointsLeft < 2 || michael@0: (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) { michael@0: (*points)[0] = p2; michael@0: *points += 1; michael@0: return 1; michael@0: } michael@0: michael@0: GrPoint q[] = { michael@0: { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, michael@0: { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, michael@0: }; michael@0: GrPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; michael@0: michael@0: pointsLeft >>= 1; michael@0: uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft); michael@0: uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft); michael@0: return a + b; michael@0: } michael@0: michael@0: uint32_t GrPathUtils::cubicPointCount(const GrPoint points[], michael@0: SkScalar tol) { michael@0: if (tol < gMinCurveTol) { michael@0: tol = gMinCurveTol; michael@0: } michael@0: SkASSERT(tol > 0); michael@0: michael@0: SkScalar d = GrMax( michael@0: points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]), michael@0: points[2].distanceToLineSegmentBetweenSqd(points[0], points[3])); michael@0: d = SkScalarSqrt(d); michael@0: if (d <= tol) { michael@0: return 1; michael@0: } else { michael@0: int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol))); michael@0: int pow2 = GrNextPow2(temp); michael@0: // Because of NaNs & INFs we can wind up with a degenerate temp michael@0: // such that pow2 comes out negative. Also, our point generator michael@0: // will always output at least one pt. michael@0: if (pow2 < 1) { michael@0: pow2 = 1; michael@0: } michael@0: return GrMin(pow2, MAX_POINTS_PER_CURVE); michael@0: } michael@0: } michael@0: michael@0: uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0, michael@0: const GrPoint& p1, michael@0: const GrPoint& p2, michael@0: const GrPoint& p3, michael@0: SkScalar tolSqd, michael@0: GrPoint** points, michael@0: uint32_t pointsLeft) { michael@0: if (pointsLeft < 2 || michael@0: (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd && michael@0: p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) { michael@0: (*points)[0] = p3; michael@0: *points += 1; michael@0: return 1; michael@0: } michael@0: GrPoint q[] = { michael@0: { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, michael@0: { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, michael@0: { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } michael@0: }; michael@0: GrPoint r[] = { michael@0: { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, michael@0: { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } michael@0: }; michael@0: GrPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; michael@0: pointsLeft >>= 1; michael@0: uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft); michael@0: uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft); michael@0: return a + b; michael@0: } michael@0: michael@0: int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, michael@0: SkScalar tol) { michael@0: if (tol < gMinCurveTol) { michael@0: tol = gMinCurveTol; michael@0: } michael@0: SkASSERT(tol > 0); michael@0: michael@0: int pointCount = 0; michael@0: *subpaths = 1; michael@0: michael@0: bool first = true; michael@0: michael@0: SkPath::Iter iter(path, false); michael@0: SkPath::Verb verb; michael@0: michael@0: GrPoint pts[4]; michael@0: while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { michael@0: michael@0: switch (verb) { michael@0: case SkPath::kLine_Verb: michael@0: pointCount += 1; michael@0: break; michael@0: case SkPath::kQuad_Verb: michael@0: pointCount += quadraticPointCount(pts, tol); michael@0: break; michael@0: case SkPath::kCubic_Verb: michael@0: pointCount += cubicPointCount(pts, tol); michael@0: break; michael@0: case SkPath::kMove_Verb: michael@0: pointCount += 1; michael@0: if (!first) { michael@0: ++(*subpaths); michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: first = false; michael@0: } michael@0: return pointCount; michael@0: } michael@0: michael@0: void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) { michael@0: SkMatrix m; michael@0: // We want M such that M * xy_pt = uv_pt michael@0: // We know M * control_pts = [0 1/2 1] michael@0: // [0 0 1] michael@0: // [1 1 1] michael@0: // And control_pts = [x0 x1 x2] michael@0: // [y0 y1 y2] michael@0: // [1 1 1 ] michael@0: // We invert the control pt matrix and post concat to both sides to get M. michael@0: // Using the known form of the control point matrix and the result, we can michael@0: // optimize and improve precision. michael@0: michael@0: double x0 = qPts[0].fX; michael@0: double y0 = qPts[0].fY; michael@0: double x1 = qPts[1].fX; michael@0: double y1 = qPts[1].fY; michael@0: double x2 = qPts[2].fX; michael@0: double y2 = qPts[2].fY; michael@0: double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2; michael@0: michael@0: if (!sk_float_isfinite(det) michael@0: || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { michael@0: // The quad is degenerate. Hopefully this is rare. Find the pts that are michael@0: // farthest apart to compute a line (unless it is really a pt). michael@0: SkScalar maxD = qPts[0].distanceToSqd(qPts[1]); michael@0: int maxEdge = 0; michael@0: SkScalar d = qPts[1].distanceToSqd(qPts[2]); michael@0: if (d > maxD) { michael@0: maxD = d; michael@0: maxEdge = 1; michael@0: } michael@0: d = qPts[2].distanceToSqd(qPts[0]); michael@0: if (d > maxD) { michael@0: maxD = d; michael@0: maxEdge = 2; michael@0: } michael@0: // We could have a tolerance here, not sure if it would improve anything michael@0: if (maxD > 0) { michael@0: // Set the matrix to give (u = 0, v = distance_to_line) michael@0: GrVec lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge]; michael@0: // when looking from the point 0 down the line we want positive michael@0: // distances to be to the left. This matches the non-degenerate michael@0: // case. michael@0: lineVec.setOrthog(lineVec, GrPoint::kLeft_Side); michael@0: lineVec.dot(qPts[0]); michael@0: // first row michael@0: fM[0] = 0; michael@0: fM[1] = 0; michael@0: fM[2] = 0; michael@0: // second row michael@0: fM[3] = lineVec.fX; michael@0: fM[4] = lineVec.fY; michael@0: fM[5] = -lineVec.dot(qPts[maxEdge]); michael@0: } else { michael@0: // It's a point. It should cover zero area. Just set the matrix such michael@0: // that (u, v) will always be far away from the quad. michael@0: fM[0] = 0; fM[1] = 0; fM[2] = 100.f; michael@0: fM[3] = 0; fM[4] = 0; fM[5] = 100.f; michael@0: } michael@0: } else { michael@0: double scale = 1.0/det; michael@0: michael@0: // compute adjugate matrix michael@0: double a0, a1, a2, a3, a4, a5, a6, a7, a8; michael@0: a0 = y1-y2; michael@0: a1 = x2-x1; michael@0: a2 = x1*y2-x2*y1; michael@0: michael@0: a3 = y2-y0; michael@0: a4 = x0-x2; michael@0: a5 = x2*y0-x0*y2; michael@0: michael@0: a6 = y0-y1; michael@0: a7 = x1-x0; michael@0: a8 = x0*y1-x1*y0; michael@0: michael@0: // this performs the uv_pts*adjugate(control_pts) multiply, michael@0: // then does the scale by 1/det afterwards to improve precision michael@0: m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale); michael@0: m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale); michael@0: m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale); michael@0: michael@0: m[SkMatrix::kMSkewY] = (float)(a6*scale); michael@0: m[SkMatrix::kMScaleY] = (float)(a7*scale); michael@0: m[SkMatrix::kMTransY] = (float)(a8*scale); michael@0: michael@0: m[SkMatrix::kMPersp0] = (float)((a0 + a3 + a6)*scale); michael@0: m[SkMatrix::kMPersp1] = (float)((a1 + a4 + a7)*scale); michael@0: m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale); michael@0: michael@0: // The matrix should not have perspective. michael@0: SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f); michael@0: SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL); michael@0: SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL); michael@0: michael@0: // It may not be normalized to have 1.0 in the bottom right michael@0: float m33 = m.get(SkMatrix::kMPersp2); michael@0: if (1.f != m33) { michael@0: m33 = 1.f / m33; michael@0: fM[0] = m33 * m.get(SkMatrix::kMScaleX); michael@0: fM[1] = m33 * m.get(SkMatrix::kMSkewX); michael@0: fM[2] = m33 * m.get(SkMatrix::kMTransX); michael@0: fM[3] = m33 * m.get(SkMatrix::kMSkewY); michael@0: fM[4] = m33 * m.get(SkMatrix::kMScaleY); michael@0: fM[5] = m33 * m.get(SkMatrix::kMTransY); michael@0: } else { michael@0: fM[0] = m.get(SkMatrix::kMScaleX); michael@0: fM[1] = m.get(SkMatrix::kMSkewX); michael@0: fM[2] = m.get(SkMatrix::kMTransX); michael@0: fM[3] = m.get(SkMatrix::kMSkewY); michael@0: fM[4] = m.get(SkMatrix::kMScaleY); michael@0: fM[5] = m.get(SkMatrix::kMTransY); michael@0: } michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // k = (y2 - y0, x0 - x2, (x2 - x0)*y0 - (y2 - y0)*x0 ) michael@0: // l = (2*w * (y1 - y0), 2*w * (x0 - x1), 2*w * (x1*y0 - x0*y1)) michael@0: // m = (2*w * (y2 - y1), 2*w * (x1 - x2), 2*w * (x2*y1 - x1*y2)) michael@0: void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkScalar klm[9]) { michael@0: const SkScalar w2 = 2.f * weight; michael@0: klm[0] = p[2].fY - p[0].fY; michael@0: klm[1] = p[0].fX - p[2].fX; michael@0: klm[2] = (p[2].fX - p[0].fX) * p[0].fY - (p[2].fY - p[0].fY) * p[0].fX; michael@0: michael@0: klm[3] = w2 * (p[1].fY - p[0].fY); michael@0: klm[4] = w2 * (p[0].fX - p[1].fX); michael@0: klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY); michael@0: michael@0: klm[6] = w2 * (p[2].fY - p[1].fY); michael@0: klm[7] = w2 * (p[1].fX - p[2].fX); michael@0: klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY); michael@0: michael@0: // scale the max absolute value of coeffs to 10 michael@0: SkScalar scale = 0.f; michael@0: for (int i = 0; i < 9; ++i) { michael@0: scale = SkMaxScalar(scale, SkScalarAbs(klm[i])); michael@0: } michael@0: SkASSERT(scale > 0.f); michael@0: scale = 10.f / scale; michael@0: for (int i = 0; i < 9; ++i) { michael@0: klm[i] *= scale; michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: namespace { michael@0: michael@0: // a is the first control point of the cubic. michael@0: // ab is the vector from a to the second control point. michael@0: // dc is the vector from the fourth to the third control point. michael@0: // d is the fourth control point. michael@0: // p is the candidate quadratic control point. michael@0: // this assumes that the cubic doesn't inflect and is simple michael@0: bool is_point_within_cubic_tangents(const SkPoint& a, michael@0: const SkVector& ab, michael@0: const SkVector& dc, michael@0: const SkPoint& d, michael@0: SkPath::Direction dir, michael@0: const SkPoint p) { michael@0: SkVector ap = p - a; michael@0: SkScalar apXab = ap.cross(ab); michael@0: if (SkPath::kCW_Direction == dir) { michael@0: if (apXab > 0) { michael@0: return false; michael@0: } michael@0: } else { michael@0: SkASSERT(SkPath::kCCW_Direction == dir); michael@0: if (apXab < 0) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: SkVector dp = p - d; michael@0: SkScalar dpXdc = dp.cross(dc); michael@0: if (SkPath::kCW_Direction == dir) { michael@0: if (dpXdc < 0) { michael@0: return false; michael@0: } michael@0: } else { michael@0: SkASSERT(SkPath::kCCW_Direction == dir); michael@0: if (dpXdc > 0) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void convert_noninflect_cubic_to_quads(const SkPoint p[4], michael@0: SkScalar toleranceSqd, michael@0: bool constrainWithinTangents, michael@0: SkPath::Direction dir, michael@0: SkTArray* quads, michael@0: int sublevel = 0) { michael@0: michael@0: // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is michael@0: // p[2]. Point d is always p[3]. Point c is p[2] unless p[2] == p[3], in which case it is p[1]. michael@0: michael@0: SkVector ab = p[1] - p[0]; michael@0: SkVector dc = p[2] - p[3]; michael@0: michael@0: if (ab.isZero()) { michael@0: if (dc.isZero()) { michael@0: SkPoint* degQuad = quads->push_back_n(3); michael@0: degQuad[0] = p[0]; michael@0: degQuad[1] = p[0]; michael@0: degQuad[2] = p[3]; michael@0: return; michael@0: } michael@0: ab = p[2] - p[0]; michael@0: } michael@0: if (dc.isZero()) { michael@0: dc = p[1] - p[3]; michael@0: } michael@0: michael@0: // When the ab and cd tangents are nearly parallel with vector from d to a the constraint that michael@0: // the quad point falls between the tangents becomes hard to enforce and we are likely to hit michael@0: // the max subdivision count. However, in this case the cubic is approaching a line and the michael@0: // accuracy of the quad point isn't so important. We check if the two middle cubic control michael@0: // points are very close to the baseline vector. If so then we just pick quadratic points on the michael@0: // control polygon. michael@0: michael@0: if (constrainWithinTangents) { michael@0: SkVector da = p[0] - p[3]; michael@0: SkScalar invDALengthSqd = da.lengthSqd(); michael@0: if (invDALengthSqd > SK_ScalarNearlyZero) { michael@0: invDALengthSqd = SkScalarInvert(invDALengthSqd); michael@0: // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a. michael@0: // same goed for point c using vector cd. michael@0: SkScalar detABSqd = ab.cross(da); michael@0: detABSqd = SkScalarSquare(detABSqd); michael@0: SkScalar detDCSqd = dc.cross(da); michael@0: detDCSqd = SkScalarSquare(detDCSqd); michael@0: if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd && michael@0: SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) { michael@0: SkPoint b = p[0] + ab; michael@0: SkPoint c = p[3] + dc; michael@0: SkPoint mid = b + c; michael@0: mid.scale(SK_ScalarHalf); michael@0: // Insert two quadratics to cover the case when ab points away from d and/or dc michael@0: // points away from a. michael@0: if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) { michael@0: SkPoint* qpts = quads->push_back_n(6); michael@0: qpts[0] = p[0]; michael@0: qpts[1] = b; michael@0: qpts[2] = mid; michael@0: qpts[3] = mid; michael@0: qpts[4] = c; michael@0: qpts[5] = p[3]; michael@0: } else { michael@0: SkPoint* qpts = quads->push_back_n(3); michael@0: qpts[0] = p[0]; michael@0: qpts[1] = mid; michael@0: qpts[2] = p[3]; michael@0: } michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; michael@0: static const int kMaxSubdivs = 10; michael@0: michael@0: ab.scale(kLengthScale); michael@0: dc.scale(kLengthScale); michael@0: michael@0: // e0 and e1 are extrapolations along vectors ab and dc. michael@0: SkVector c0 = p[0]; michael@0: c0 += ab; michael@0: SkVector c1 = p[3]; michael@0: c1 += dc; michael@0: michael@0: SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1); michael@0: if (dSqd < toleranceSqd) { michael@0: SkPoint cAvg = c0; michael@0: cAvg += c1; michael@0: cAvg.scale(SK_ScalarHalf); michael@0: michael@0: bool subdivide = false; michael@0: michael@0: if (constrainWithinTangents && michael@0: !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) { michael@0: // choose a new cAvg that is the intersection of the two tangent lines. michael@0: ab.setOrthog(ab); michael@0: SkScalar z0 = -ab.dot(p[0]); michael@0: dc.setOrthog(dc); michael@0: SkScalar z1 = -dc.dot(p[3]); michael@0: cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY); michael@0: cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1); michael@0: SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX); michael@0: z = SkScalarInvert(z); michael@0: cAvg.fX *= z; michael@0: cAvg.fY *= z; michael@0: if (sublevel <= kMaxSubdivs) { michael@0: SkScalar d0Sqd = c0.distanceToSqd(cAvg); michael@0: SkScalar d1Sqd = c1.distanceToSqd(cAvg); michael@0: // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know michael@0: // the distances and tolerance can't be negative. michael@0: // (d0 + d1)^2 > toleranceSqd michael@0: // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd michael@0: SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd)); michael@0: subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd; michael@0: } michael@0: } michael@0: if (!subdivide) { michael@0: SkPoint* pts = quads->push_back_n(3); michael@0: pts[0] = p[0]; michael@0: pts[1] = cAvg; michael@0: pts[2] = p[3]; michael@0: return; michael@0: } michael@0: } michael@0: SkPoint choppedPts[7]; michael@0: SkChopCubicAtHalf(p, choppedPts); michael@0: convert_noninflect_cubic_to_quads(choppedPts + 0, michael@0: toleranceSqd, michael@0: constrainWithinTangents, michael@0: dir, michael@0: quads, michael@0: sublevel + 1); michael@0: convert_noninflect_cubic_to_quads(choppedPts + 3, michael@0: toleranceSqd, michael@0: constrainWithinTangents, michael@0: dir, michael@0: quads, michael@0: sublevel + 1); michael@0: } michael@0: } michael@0: michael@0: void GrPathUtils::convertCubicToQuads(const GrPoint p[4], michael@0: SkScalar tolScale, michael@0: bool constrainWithinTangents, michael@0: SkPath::Direction dir, michael@0: SkTArray* quads) { michael@0: SkPoint chopped[10]; michael@0: int count = SkChopCubicAtInflections(p, chopped); michael@0: michael@0: // base tolerance is 1 pixel. michael@0: static const SkScalar kTolerance = SK_Scalar1; michael@0: const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance)); michael@0: michael@0: for (int i = 0; i < count; ++i) { michael@0: SkPoint* cubic = chopped + 3*i; michael@0: convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads); michael@0: } michael@0: michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: enum CubicType { michael@0: kSerpentine_CubicType, michael@0: kCusp_CubicType, michael@0: kLoop_CubicType, michael@0: kQuadratic_CubicType, michael@0: kLine_CubicType, michael@0: kPoint_CubicType michael@0: }; michael@0: michael@0: // discr(I) = d0^2 * (3*d1^2 - 4*d0*d2) michael@0: // Classification: michael@0: // discr(I) > 0 Serpentine michael@0: // discr(I) = 0 Cusp michael@0: // discr(I) < 0 Loop michael@0: // d0 = d1 = 0 Quadratic michael@0: // d0 = d1 = d2 = 0 Line michael@0: // p0 = p1 = p2 = p3 Point michael@0: static CubicType classify_cubic(const SkPoint p[4], const SkScalar d[3]) { michael@0: if (p[0] == p[1] && p[0] == p[2] && p[0] == p[3]) { michael@0: return kPoint_CubicType; michael@0: } michael@0: const SkScalar discr = d[0] * d[0] * (3.f * d[1] * d[1] - 4.f * d[0] * d[2]); michael@0: if (discr > SK_ScalarNearlyZero) { michael@0: return kSerpentine_CubicType; michael@0: } else if (discr < -SK_ScalarNearlyZero) { michael@0: return kLoop_CubicType; michael@0: } else { michael@0: if (0.f == d[0] && 0.f == d[1]) { michael@0: return (0.f == d[2] ? kLine_CubicType : kQuadratic_CubicType); michael@0: } else { michael@0: return kCusp_CubicType; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Assumes the third component of points is 1. michael@0: // Calcs p0 . (p1 x p2) michael@0: static SkScalar calc_dot_cross_cubic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { michael@0: const SkScalar xComp = p0.fX * (p1.fY - p2.fY); michael@0: const SkScalar yComp = p0.fY * (p2.fX - p1.fX); michael@0: const SkScalar wComp = p1.fX * p2.fY - p1.fY * p2.fX; michael@0: return (xComp + yComp + wComp); michael@0: } michael@0: michael@0: // Solves linear system to extract klm michael@0: // P.K = k (similarly for l, m) michael@0: // Where P is matrix of control points michael@0: // K is coefficients for the line K michael@0: // k is vector of values of K evaluated at the control points michael@0: // Solving for K, thus K = P^(-1) . k michael@0: static void calc_cubic_klm(const SkPoint p[4], const SkScalar controlK[4], michael@0: const SkScalar controlL[4], const SkScalar controlM[4], michael@0: SkScalar k[3], SkScalar l[3], SkScalar m[3]) { michael@0: SkMatrix matrix; michael@0: matrix.setAll(p[0].fX, p[0].fY, 1.f, michael@0: p[1].fX, p[1].fY, 1.f, michael@0: p[2].fX, p[2].fY, 1.f); michael@0: SkMatrix inverse; michael@0: if (matrix.invert(&inverse)) { michael@0: inverse.mapHomogeneousPoints(k, controlK, 1); michael@0: inverse.mapHomogeneousPoints(l, controlL, 1); michael@0: inverse.mapHomogeneousPoints(m, controlM, 1); michael@0: } michael@0: michael@0: } michael@0: michael@0: static void set_serp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { michael@0: SkScalar tempSqrt = SkScalarSqrt(9.f * d[1] * d[1] - 12.f * d[0] * d[2]); michael@0: SkScalar ls = 3.f * d[1] - tempSqrt; michael@0: SkScalar lt = 6.f * d[0]; michael@0: SkScalar ms = 3.f * d[1] + tempSqrt; michael@0: SkScalar mt = 6.f * d[0]; michael@0: michael@0: k[0] = ls * ms; michael@0: k[1] = (3.f * ls * ms - ls * mt - lt * ms) / 3.f; michael@0: k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; michael@0: k[3] = (lt - ls) * (mt - ms); michael@0: michael@0: l[0] = ls * ls * ls; michael@0: const SkScalar lt_ls = lt - ls; michael@0: l[1] = ls * ls * lt_ls * -1.f; michael@0: l[2] = lt_ls * lt_ls * ls; michael@0: l[3] = -1.f * lt_ls * lt_ls * lt_ls; michael@0: michael@0: m[0] = ms * ms * ms; michael@0: const SkScalar mt_ms = mt - ms; michael@0: m[1] = ms * ms * mt_ms * -1.f; michael@0: m[2] = mt_ms * mt_ms * ms; michael@0: m[3] = -1.f * mt_ms * mt_ms * mt_ms; michael@0: michael@0: // If d0 < 0 we need to flip the orientation of our curve michael@0: // This is done by negating the k and l values michael@0: // We want negative distance values to be on the inside michael@0: if ( d[0] > 0) { michael@0: for (int i = 0; i < 4; ++i) { michael@0: k[i] = -k[i]; michael@0: l[i] = -l[i]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { michael@0: SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); michael@0: SkScalar ls = d[1] - tempSqrt; michael@0: SkScalar lt = 2.f * d[0]; michael@0: SkScalar ms = d[1] + tempSqrt; michael@0: SkScalar mt = 2.f * d[0]; michael@0: michael@0: k[0] = ls * ms; michael@0: k[1] = (3.f * ls*ms - ls * mt - lt * ms) / 3.f; michael@0: k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; michael@0: k[3] = (lt - ls) * (mt - ms); michael@0: michael@0: l[0] = ls * ls * ms; michael@0: l[1] = (ls * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/-3.f; michael@0: l[2] = ((lt - ls) * (ls * (2.f * mt - 3.f * ms) + lt * ms))/3.f; michael@0: l[3] = -1.f * (lt - ls) * (lt - ls) * (mt - ms); michael@0: michael@0: m[0] = ls * ms * ms; michael@0: m[1] = (ms * (ls * (2.f * mt - 3.f * ms) + lt * ms))/-3.f; michael@0: m[2] = ((mt - ms) * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/3.f; michael@0: m[3] = -1.f * (lt - ls) * (mt - ms) * (mt - ms); michael@0: michael@0: michael@0: // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0), michael@0: // we need to flip the orientation of our curve. michael@0: // This is done by negating the k and l values michael@0: if ( (d[0] < 0 && k[1] > 0) || (d[0] > 0 && k[1] < 0)) { michael@0: for (int i = 0; i < 4; ++i) { michael@0: k[i] = -k[i]; michael@0: l[i] = -l[i]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void set_cusp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { michael@0: const SkScalar ls = d[2]; michael@0: const SkScalar lt = 3.f * d[1]; michael@0: michael@0: k[0] = ls; michael@0: k[1] = ls - lt / 3.f; michael@0: k[2] = ls - 2.f * lt / 3.f; michael@0: k[3] = ls - lt; michael@0: michael@0: l[0] = ls * ls * ls; michael@0: const SkScalar ls_lt = ls - lt; michael@0: l[1] = ls * ls * ls_lt; michael@0: l[2] = ls_lt * ls_lt * ls; michael@0: l[3] = ls_lt * ls_lt * ls_lt; michael@0: michael@0: m[0] = 1.f; michael@0: m[1] = 1.f; michael@0: m[2] = 1.f; michael@0: m[3] = 1.f; michael@0: } michael@0: michael@0: // For the case when a cubic is actually a quadratic michael@0: // M = michael@0: // 0 0 0 michael@0: // 1/3 0 1/3 michael@0: // 2/3 1/3 2/3 michael@0: // 1 1 1 michael@0: static void set_quadratic_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { michael@0: k[0] = 0.f; michael@0: k[1] = 1.f/3.f; michael@0: k[2] = 2.f/3.f; michael@0: k[3] = 1.f; michael@0: michael@0: l[0] = 0.f; michael@0: l[1] = 0.f; michael@0: l[2] = 1.f/3.f; michael@0: l[3] = 1.f; michael@0: michael@0: m[0] = 0.f; michael@0: m[1] = 1.f/3.f; michael@0: m[2] = 2.f/3.f; michael@0: m[3] = 1.f; michael@0: michael@0: // If d2 < 0 we need to flip the orientation of our curve michael@0: // This is done by negating the k and l values michael@0: if ( d[2] > 0) { michael@0: for (int i = 0; i < 4; ++i) { michael@0: k[i] = -k[i]; michael@0: l[i] = -l[i]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Calc coefficients of I(s,t) where roots of I are inflection points of curve michael@0: // I(s,t) = t*(3*d0*s^2 - 3*d1*s*t + d2*t^2) michael@0: // d0 = a1 - 2*a2+3*a3 michael@0: // d1 = -a2 + 3*a3 michael@0: // d2 = 3*a3 michael@0: // a1 = p0 . (p3 x p2) michael@0: // a2 = p1 . (p0 x p3) michael@0: // a3 = p2 . (p1 x p0) michael@0: // Places the values of d1, d2, d3 in array d passed in michael@0: static void calc_cubic_inflection_func(const SkPoint p[4], SkScalar d[3]) { michael@0: SkScalar a1 = calc_dot_cross_cubic(p[0], p[3], p[2]); michael@0: SkScalar a2 = calc_dot_cross_cubic(p[1], p[0], p[3]); michael@0: SkScalar a3 = calc_dot_cross_cubic(p[2], p[1], p[0]); michael@0: michael@0: // need to scale a's or values in later calculations will grow to high michael@0: SkScalar max = SkScalarAbs(a1); michael@0: max = SkMaxScalar(max, SkScalarAbs(a2)); michael@0: max = SkMaxScalar(max, SkScalarAbs(a3)); michael@0: max = 1.f/max; michael@0: a1 = a1 * max; michael@0: a2 = a2 * max; michael@0: a3 = a3 * max; michael@0: michael@0: d[2] = 3.f * a3; michael@0: d[1] = d[2] - a2; michael@0: d[0] = d[1] - a2 + a1; michael@0: } michael@0: michael@0: int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkScalar klm[9], michael@0: SkScalar klm_rev[3]) { michael@0: // Variable to store the two parametric values at the loop double point michael@0: SkScalar smallS = 0.f; michael@0: SkScalar largeS = 0.f; michael@0: michael@0: SkScalar d[3]; michael@0: calc_cubic_inflection_func(src, d); michael@0: michael@0: CubicType cType = classify_cubic(src, d); michael@0: michael@0: int chop_count = 0; michael@0: if (kLoop_CubicType == cType) { michael@0: SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); michael@0: SkScalar ls = d[1] - tempSqrt; michael@0: SkScalar lt = 2.f * d[0]; michael@0: SkScalar ms = d[1] + tempSqrt; michael@0: SkScalar mt = 2.f * d[0]; michael@0: ls = ls / lt; michael@0: ms = ms / mt; michael@0: // need to have t values sorted since this is what is expected by SkChopCubicAt michael@0: if (ls <= ms) { michael@0: smallS = ls; michael@0: largeS = ms; michael@0: } else { michael@0: smallS = ms; michael@0: largeS = ls; michael@0: } michael@0: michael@0: SkScalar chop_ts[2]; michael@0: if (smallS > 0.f && smallS < 1.f) { michael@0: chop_ts[chop_count++] = smallS; michael@0: } michael@0: if (largeS > 0.f && largeS < 1.f) { michael@0: chop_ts[chop_count++] = largeS; michael@0: } michael@0: if(dst) { michael@0: SkChopCubicAt(src, dst, chop_ts, chop_count); michael@0: } michael@0: } else { michael@0: if (dst) { michael@0: memcpy(dst, src, sizeof(SkPoint) * 4); michael@0: } michael@0: } michael@0: michael@0: if (klm && klm_rev) { michael@0: // Set klm_rev to to match the sub_section of cubic that needs to have its orientation michael@0: // flipped. This will always be the section that is the "loop" michael@0: if (2 == chop_count) { michael@0: klm_rev[0] = 1.f; michael@0: klm_rev[1] = -1.f; michael@0: klm_rev[2] = 1.f; michael@0: } else if (1 == chop_count) { michael@0: if (smallS < 0.f) { michael@0: klm_rev[0] = -1.f; michael@0: klm_rev[1] = 1.f; michael@0: } else { michael@0: klm_rev[0] = 1.f; michael@0: klm_rev[1] = -1.f; michael@0: } michael@0: } else { michael@0: if (smallS < 0.f && largeS > 1.f) { michael@0: klm_rev[0] = -1.f; michael@0: } else { michael@0: klm_rev[0] = 1.f; michael@0: } michael@0: } michael@0: SkScalar controlK[4]; michael@0: SkScalar controlL[4]; michael@0: SkScalar controlM[4]; michael@0: michael@0: if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) { michael@0: set_serp_klm(d, controlK, controlL, controlM); michael@0: } else if (kLoop_CubicType == cType) { michael@0: set_loop_klm(d, controlK, controlL, controlM); michael@0: } else if (kCusp_CubicType == cType) { michael@0: SkASSERT(0.f == d[0]); michael@0: set_cusp_klm(d, controlK, controlL, controlM); michael@0: } else if (kQuadratic_CubicType == cType) { michael@0: set_quadratic_klm(d, controlK, controlL, controlM); michael@0: } michael@0: michael@0: calc_cubic_klm(src, controlK, controlL, controlM, klm, &klm[3], &klm[6]); michael@0: } michael@0: return chop_count + 1; michael@0: } michael@0: michael@0: void GrPathUtils::getCubicKLM(const SkPoint p[4], SkScalar klm[9]) { michael@0: SkScalar d[3]; michael@0: calc_cubic_inflection_func(p, d); michael@0: michael@0: CubicType cType = classify_cubic(p, d); michael@0: michael@0: SkScalar controlK[4]; michael@0: SkScalar controlL[4]; michael@0: SkScalar controlM[4]; michael@0: michael@0: if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) { michael@0: set_serp_klm(d, controlK, controlL, controlM); michael@0: } else if (kLoop_CubicType == cType) { michael@0: set_loop_klm(d, controlK, controlL, controlM); michael@0: } else if (kCusp_CubicType == cType) { michael@0: SkASSERT(0.f == d[0]); michael@0: set_cusp_klm(d, controlK, controlL, controlM); michael@0: } else if (kQuadratic_CubicType == cType) { michael@0: set_quadratic_klm(d, controlK, controlL, controlM); michael@0: } michael@0: michael@0: calc_cubic_klm(p, controlK, controlL, controlM, klm, &klm[3], &klm[6]); michael@0: }