michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkGeometry_DEFINED michael@0: #define SkGeometry_DEFINED michael@0: michael@0: #include "SkMatrix.h" michael@0: michael@0: /** An XRay is a half-line that runs from the specific point/origin to michael@0: +infinity in the X direction. e.g. XRay(3,5) is the half-line michael@0: (3,5)....(infinity, 5) michael@0: */ michael@0: typedef SkPoint SkXRay; michael@0: michael@0: /** Given a line segment from pts[0] to pts[1], and an xray, return true if michael@0: they intersect. Optional outgoing "ambiguous" argument indicates michael@0: whether the answer is ambiguous because the query occurred exactly at michael@0: one of the endpoints' y coordinates, indicating that another query y michael@0: coordinate is preferred for robustness. michael@0: */ michael@0: bool SkXRayCrossesLine(const SkXRay& pt, const SkPoint pts[2], michael@0: bool* ambiguous = NULL); michael@0: michael@0: /** Given a quadratic equation Ax^2 + Bx + C = 0, return 0, 1, 2 roots for the michael@0: equation. michael@0: */ michael@0: int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** Set pt to the point on the src quadratic specified by t. t must be michael@0: 0 <= t <= 1.0 michael@0: */ michael@0: void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, michael@0: SkVector* tangent = NULL); michael@0: void SkEvalQuadAtHalf(const SkPoint src[3], SkPoint* pt, michael@0: SkVector* tangent = NULL); michael@0: michael@0: /** Given a src quadratic bezier, chop it at the specified t value, michael@0: where 0 < t < 1, and return the two new quadratics in dst: michael@0: dst[0..2] and dst[2..4] michael@0: */ michael@0: void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t); michael@0: michael@0: /** Given a src quadratic bezier, chop it at the specified t == 1/2, michael@0: The new quads are returned in dst[0..2] and dst[2..4] michael@0: */ michael@0: void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]); michael@0: michael@0: /** Given the 3 coefficients for a quadratic bezier (either X or Y values), look michael@0: for extrema, and return the number of t-values that are found that represent michael@0: these extrema. If the quadratic has no extrema betwee (0..1) exclusive, the michael@0: function returns 0. michael@0: Returned count tValues[] michael@0: 0 ignored michael@0: 1 0 < tValues[0] < 1 michael@0: */ michael@0: int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValues[1]); michael@0: michael@0: /** Given 3 points on a quadratic bezier, chop it into 1, 2 beziers such that michael@0: the resulting beziers are monotonic in Y. This is called by the scan converter. michael@0: Depending on what is returned, dst[] is treated as follows michael@0: 0 dst[0..2] is the original quad michael@0: 1 dst[0..2] and dst[2..4] are the two new quads michael@0: */ michael@0: int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]); michael@0: int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]); michael@0: michael@0: /** Given 3 points on a quadratic bezier, if the point of maximum michael@0: curvature exists on the segment, returns the t value for this michael@0: point along the curve. Otherwise it will return a value of 0. michael@0: */ michael@0: float SkFindQuadMaxCurvature(const SkPoint src[3]); michael@0: michael@0: /** Given 3 points on a quadratic bezier, divide it into 2 quadratics michael@0: if the point of maximum curvature exists on the quad segment. michael@0: Depending on what is returned, dst[] is treated as follows michael@0: 1 dst[0..2] is the original quad michael@0: 2 dst[0..2] and dst[2..4] are the two new quads michael@0: If dst == null, it is ignored and only the count is returned. michael@0: */ michael@0: int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]); michael@0: michael@0: /** Given 3 points on a quadratic bezier, use degree elevation to michael@0: convert it into the cubic fitting the same curve. The new cubic michael@0: curve is returned in dst[0..3]. michael@0: */ michael@0: SK_API void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** Convert from parametric from (pts) to polynomial coefficients michael@0: coeff[0]*T^3 + coeff[1]*T^2 + coeff[2]*T + coeff[3] michael@0: */ michael@0: void SkGetCubicCoeff(const SkPoint pts[4], SkScalar cx[4], SkScalar cy[4]); michael@0: michael@0: /** Set pt to the point on the src cubic specified by t. t must be michael@0: 0 <= t <= 1.0 michael@0: */ michael@0: void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* locOrNull, michael@0: SkVector* tangentOrNull, SkVector* curvatureOrNull); michael@0: michael@0: /** Given a src cubic bezier, chop it at the specified t value, michael@0: where 0 < t < 1, and return the two new cubics in dst: michael@0: dst[0..3] and dst[3..6] michael@0: */ michael@0: void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t); michael@0: /** Given a src cubic bezier, chop it at the specified t values, michael@0: where 0 < t < 1, and return the new cubics in dst: michael@0: dst[0..3],dst[3..6],...,dst[3*t_count..3*(t_count+1)] michael@0: */ michael@0: void SkChopCubicAt(const SkPoint src[4], SkPoint dst[], const SkScalar t[], michael@0: int t_count); michael@0: michael@0: /** Given a src cubic bezier, chop it at the specified t == 1/2, michael@0: The new cubics are returned in dst[0..3] and dst[3..6] michael@0: */ michael@0: void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]); michael@0: michael@0: /** Given the 4 coefficients for a cubic bezier (either X or Y values), look michael@0: for extrema, and return the number of t-values that are found that represent michael@0: these extrema. If the cubic has no extrema betwee (0..1) exclusive, the michael@0: function returns 0. michael@0: Returned count tValues[] michael@0: 0 ignored michael@0: 1 0 < tValues[0] < 1 michael@0: 2 0 < tValues[0] < tValues[1] < 1 michael@0: */ michael@0: int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, michael@0: SkScalar tValues[2]); michael@0: michael@0: /** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that michael@0: the resulting beziers are monotonic in Y. This is called by the scan converter. michael@0: Depending on what is returned, dst[] is treated as follows michael@0: 0 dst[0..3] is the original cubic michael@0: 1 dst[0..3] and dst[3..6] are the two new cubics michael@0: 2 dst[0..3], dst[3..6], dst[6..9] are the three new cubics michael@0: If dst == null, it is ignored and only the count is returned. michael@0: */ michael@0: int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]); michael@0: int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]); michael@0: michael@0: /** Given a cubic bezier, return 0, 1, or 2 t-values that represent the michael@0: inflection points. michael@0: */ michael@0: int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[2]); michael@0: michael@0: /** Return 1 for no chop, 2 for having chopped the cubic at a single michael@0: inflection point, 3 for having chopped at 2 inflection points. michael@0: dst will hold the resulting 1, 2, or 3 cubics. michael@0: */ michael@0: int SkChopCubicAtInflections(const SkPoint src[4], SkPoint dst[10]); michael@0: michael@0: int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]); michael@0: int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], michael@0: SkScalar tValues[3] = NULL); michael@0: michael@0: /** Given a monotonic cubic bezier, determine whether an xray intersects the michael@0: cubic. michael@0: By definition the cubic is open at the starting point; in other michael@0: words, if pt.fY is equivalent to cubic[0].fY, and pt.fX is to the michael@0: left of the curve, the line is not considered to cross the curve, michael@0: but if it is equal to cubic[3].fY then it is considered to michael@0: cross. michael@0: Optional outgoing "ambiguous" argument indicates whether the answer is michael@0: ambiguous because the query occurred exactly at one of the endpoints' y michael@0: coordinates, indicating that another query y coordinate is preferred michael@0: for robustness. michael@0: */ michael@0: bool SkXRayCrossesMonotonicCubic(const SkXRay& pt, const SkPoint cubic[4], michael@0: bool* ambiguous = NULL); michael@0: michael@0: /** Given an arbitrary cubic bezier, return the number of times an xray crosses michael@0: the cubic. Valid return values are [0..3] michael@0: By definition the cubic is open at the starting point; in other michael@0: words, if pt.fY is equivalent to cubic[0].fY, and pt.fX is to the michael@0: left of the curve, the line is not considered to cross the curve, michael@0: but if it is equal to cubic[3].fY then it is considered to michael@0: cross. michael@0: Optional outgoing "ambiguous" argument indicates whether the answer is michael@0: ambiguous because the query occurred exactly at one of the endpoints' y michael@0: coordinates or at a tangent point, indicating that another query y michael@0: coordinate is preferred for robustness. michael@0: */ michael@0: int SkNumXRayCrossingsForCubic(const SkXRay& pt, const SkPoint cubic[4], michael@0: bool* ambiguous = NULL); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: enum SkRotationDirection { michael@0: kCW_SkRotationDirection, michael@0: kCCW_SkRotationDirection michael@0: }; michael@0: michael@0: /** Maximum number of points needed in the quadPoints[] parameter for michael@0: SkBuildQuadArc() michael@0: */ michael@0: #define kSkBuildQuadArcStorage 17 michael@0: michael@0: /** Given 2 unit vectors and a rotation direction, fill out the specified michael@0: array of points with quadratic segments. Return is the number of points michael@0: written to, which will be { 0, 3, 5, 7, ... kSkBuildQuadArcStorage } michael@0: michael@0: matrix, if not null, is appled to the points before they are returned. michael@0: */ michael@0: int SkBuildQuadArc(const SkVector& unitStart, const SkVector& unitStop, michael@0: SkRotationDirection, const SkMatrix*, SkPoint quadPoints[]); michael@0: michael@0: // experimental michael@0: struct SkConic { michael@0: SkPoint fPts[3]; michael@0: SkScalar fW; michael@0: michael@0: void set(const SkPoint pts[3], SkScalar w) { michael@0: memcpy(fPts, pts, 3 * sizeof(SkPoint)); michael@0: fW = w; michael@0: } michael@0: michael@0: /** michael@0: * Given a t-value [0...1] return its position and/or tangent. michael@0: * If pos is not null, return its position at the t-value. michael@0: * If tangent is not null, return its tangent at the t-value. NOTE the michael@0: * tangent value's length is arbitrary, and only its direction should michael@0: * be used. michael@0: */ michael@0: void evalAt(SkScalar t, SkPoint* pos, SkVector* tangent = NULL) const; michael@0: void chopAt(SkScalar t, SkConic dst[2]) const; michael@0: void chop(SkConic dst[2]) const; michael@0: michael@0: void computeAsQuadError(SkVector* err) const; michael@0: bool asQuadTol(SkScalar tol) const; michael@0: michael@0: /** michael@0: * return the power-of-2 number of quads needed to approximate this conic michael@0: * with a sequence of quads. Will be >= 0. michael@0: */ michael@0: int computeQuadPOW2(SkScalar tol) const; michael@0: michael@0: /** michael@0: * Chop this conic into N quads, stored continguously in pts[], where michael@0: * N = 1 << pow2. The amount of storage needed is (1 + 2 * N) michael@0: */ michael@0: int chopIntoQuadsPOW2(SkPoint pts[], int pow2) const; michael@0: michael@0: bool findXExtrema(SkScalar* t) const; michael@0: bool findYExtrema(SkScalar* t) const; michael@0: bool chopAtXExtrema(SkConic dst[2]) const; michael@0: bool chopAtYExtrema(SkConic dst[2]) const; michael@0: michael@0: void computeTightBounds(SkRect* bounds) const; michael@0: void computeFastBounds(SkRect* bounds) const; michael@0: michael@0: /** Find the parameter value where the conic takes on its maximum curvature. michael@0: * michael@0: * @param t output scalar for max curvature. Will be unchanged if michael@0: * max curvature outside 0..1 range. michael@0: * michael@0: * @return true if max curvature found inside 0..1 range, false otherwise michael@0: */ michael@0: bool findMaxCurvature(SkScalar* t) const; michael@0: }; michael@0: michael@0: #include "SkTemplates.h" michael@0: michael@0: /** michael@0: * Help class to allocate storage for approximating a conic with N quads. michael@0: */ michael@0: class SkAutoConicToQuads { michael@0: public: michael@0: SkAutoConicToQuads() : fQuadCount(0) {} michael@0: michael@0: /** michael@0: * Given a conic and a tolerance, return the array of points for the michael@0: * approximating quad(s). Call countQuads() to know the number of quads michael@0: * represented in these points. michael@0: * michael@0: * The quads are allocated to share end-points. e.g. if there are 4 quads, michael@0: * there will be 9 points allocated as follows michael@0: * quad[0] == pts[0..2] michael@0: * quad[1] == pts[2..4] michael@0: * quad[2] == pts[4..6] michael@0: * quad[3] == pts[6..8] michael@0: */ michael@0: const SkPoint* computeQuads(const SkConic& conic, SkScalar tol) { michael@0: int pow2 = conic.computeQuadPOW2(tol); michael@0: fQuadCount = 1 << pow2; michael@0: SkPoint* pts = fStorage.reset(1 + 2 * fQuadCount); michael@0: conic.chopIntoQuadsPOW2(pts, pow2); michael@0: return pts; michael@0: } michael@0: michael@0: const SkPoint* computeQuads(const SkPoint pts[3], SkScalar weight, michael@0: SkScalar tol) { michael@0: SkConic conic; michael@0: conic.set(pts, weight); michael@0: return computeQuads(conic, tol); michael@0: } michael@0: michael@0: int countQuads() const { return fQuadCount; } michael@0: michael@0: private: michael@0: enum { michael@0: kQuadCount = 8, // should handle most conics michael@0: kPointCount = 1 + 2 * kQuadCount, michael@0: }; michael@0: SkAutoSTMalloc fStorage; michael@0: int fQuadCount; // #quads for current usage michael@0: }; michael@0: michael@0: #endif