SkGeometry.h revision 562d0e1cd2286945cb73fca0233560071b052129
1/* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#ifndef SkGeometry_DEFINED 9#define SkGeometry_DEFINED 10 11#include "SkMatrix.h" 12 13/** Given a quadratic equation Ax^2 + Bx + C = 0, return 0, 1, 2 roots for the 14 equation. 15*/ 16int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]); 17 18/////////////////////////////////////////////////////////////////////////////// 19 20/** Set pt to the point on the src quadratic specified by t. t must be 21 0 <= t <= 1.0 22*/ 23void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, 24 SkVector* tangent = NULL); 25void SkEvalQuadAtHalf(const SkPoint src[3], SkPoint* pt, 26 SkVector* tangent = NULL); 27 28/** Given a src quadratic bezier, chop it at the specified t value, 29 where 0 < t < 1, and return the two new quadratics in dst: 30 dst[0..2] and dst[2..4] 31*/ 32void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t); 33 34/** Given a src quadratic bezier, chop it at the specified t == 1/2, 35 The new quads are returned in dst[0..2] and dst[2..4] 36*/ 37void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]); 38 39/** Given the 3 coefficients for a quadratic bezier (either X or Y values), look 40 for extrema, and return the number of t-values that are found that represent 41 these extrema. If the quadratic has no extrema betwee (0..1) exclusive, the 42 function returns 0. 43 Returned count tValues[] 44 0 ignored 45 1 0 < tValues[0] < 1 46*/ 47int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValues[1]); 48 49/** Given 3 points on a quadratic bezier, chop it into 1, 2 beziers such that 50 the resulting beziers are monotonic in Y. This is called by the scan converter. 51 Depending on what is returned, dst[] is treated as follows 52 0 dst[0..2] is the original quad 53 1 dst[0..2] and dst[2..4] are the two new quads 54*/ 55int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]); 56int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]); 57 58/** Given 3 points on a quadratic bezier, if the point of maximum 59 curvature exists on the segment, returns the t value for this 60 point along the curve. Otherwise it will return a value of 0. 61*/ 62SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]); 63 64/** Given 3 points on a quadratic bezier, divide it into 2 quadratics 65 if the point of maximum curvature exists on the quad segment. 66 Depending on what is returned, dst[] is treated as follows 67 1 dst[0..2] is the original quad 68 2 dst[0..2] and dst[2..4] are the two new quads 69 If dst == null, it is ignored and only the count is returned. 70*/ 71int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]); 72 73/** Given 3 points on a quadratic bezier, use degree elevation to 74 convert it into the cubic fitting the same curve. The new cubic 75 curve is returned in dst[0..3]. 76*/ 77SK_API void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]); 78 79/////////////////////////////////////////////////////////////////////////////// 80 81/** Convert from parametric from (pts) to polynomial coefficients 82 coeff[0]*T^3 + coeff[1]*T^2 + coeff[2]*T + coeff[3] 83*/ 84void SkGetCubicCoeff(const SkPoint pts[4], SkScalar cx[4], SkScalar cy[4]); 85 86/** Set pt to the point on the src cubic specified by t. t must be 87 0 <= t <= 1.0 88*/ 89void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* locOrNull, 90 SkVector* tangentOrNull, SkVector* curvatureOrNull); 91 92/** Given a src cubic bezier, chop it at the specified t value, 93 where 0 < t < 1, and return the two new cubics in dst: 94 dst[0..3] and dst[3..6] 95*/ 96void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t); 97/** Given a src cubic bezier, chop it at the specified t values, 98 where 0 < t < 1, and return the new cubics in dst: 99 dst[0..3],dst[3..6],...,dst[3*t_count..3*(t_count+1)] 100*/ 101void SkChopCubicAt(const SkPoint src[4], SkPoint dst[], const SkScalar t[], 102 int t_count); 103 104/** Given a src cubic bezier, chop it at the specified t == 1/2, 105 The new cubics are returned in dst[0..3] and dst[3..6] 106*/ 107void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]); 108 109/** Given the 4 coefficients for a cubic bezier (either X or Y values), look 110 for extrema, and return the number of t-values that are found that represent 111 these extrema. If the cubic has no extrema betwee (0..1) exclusive, the 112 function returns 0. 113 Returned count tValues[] 114 0 ignored 115 1 0 < tValues[0] < 1 116 2 0 < tValues[0] < tValues[1] < 1 117*/ 118int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, 119 SkScalar tValues[2]); 120 121/** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that 122 the resulting beziers are monotonic in Y. This is called by the scan converter. 123 Depending on what is returned, dst[] is treated as follows 124 0 dst[0..3] is the original cubic 125 1 dst[0..3] and dst[3..6] are the two new cubics 126 2 dst[0..3], dst[3..6], dst[6..9] are the three new cubics 127 If dst == null, it is ignored and only the count is returned. 128*/ 129int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]); 130int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]); 131 132/** Given a cubic bezier, return 0, 1, or 2 t-values that represent the 133 inflection points. 134*/ 135int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[2]); 136 137/** Return 1 for no chop, 2 for having chopped the cubic at a single 138 inflection point, 3 for having chopped at 2 inflection points. 139 dst will hold the resulting 1, 2, or 3 cubics. 140*/ 141int SkChopCubicAtInflections(const SkPoint src[4], SkPoint dst[10]); 142 143int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]); 144int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], 145 SkScalar tValues[3] = NULL); 146 147enum SkCubicType { 148 kSerpentine_SkCubicType, 149 kCusp_SkCubicType, 150 kLoop_SkCubicType, 151 kQuadratic_SkCubicType, 152 kLine_SkCubicType, 153 kPoint_SkCubicType 154}; 155 156/** Returns the cubic classification. Pass scratch storage for computing inflection data, 157 which can be used with additional work to find the loop intersections and so on. 158*/ 159SkCubicType SkClassifyCubic(const SkPoint p[4], SkScalar inflection[3]); 160 161/////////////////////////////////////////////////////////////////////////////// 162 163enum SkRotationDirection { 164 kCW_SkRotationDirection, 165 kCCW_SkRotationDirection 166}; 167 168/** Maximum number of points needed in the quadPoints[] parameter for 169 SkBuildQuadArc() 170*/ 171#define kSkBuildQuadArcStorage 17 172 173/** Given 2 unit vectors and a rotation direction, fill out the specified 174 array of points with quadratic segments. Return is the number of points 175 written to, which will be { 0, 3, 5, 7, ... kSkBuildQuadArcStorage } 176 177 matrix, if not null, is appled to the points before they are returned. 178*/ 179int SkBuildQuadArc(const SkVector& unitStart, const SkVector& unitStop, 180 SkRotationDirection, const SkMatrix*, SkPoint quadPoints[]); 181 182struct SkConic { 183 SkConic() {} 184 SkConic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, SkScalar w) { 185 fPts[0] = p0; 186 fPts[1] = p1; 187 fPts[2] = p2; 188 fW = w; 189 } 190 SkConic(const SkPoint pts[3], SkScalar w) { 191 memcpy(fPts, pts, sizeof(fPts)); 192 fW = w; 193 } 194 195 SkPoint fPts[3]; 196 SkScalar fW; 197 198 void set(const SkPoint pts[3], SkScalar w) { 199 memcpy(fPts, pts, 3 * sizeof(SkPoint)); 200 fW = w; 201 } 202 203 void set(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, SkScalar w) { 204 fPts[0] = p0; 205 fPts[1] = p1; 206 fPts[2] = p2; 207 fW = w; 208 } 209 210 /** 211 * Given a t-value [0...1] return its position and/or tangent. 212 * If pos is not null, return its position at the t-value. 213 * If tangent is not null, return its tangent at the t-value. NOTE the 214 * tangent value's length is arbitrary, and only its direction should 215 * be used. 216 */ 217 void evalAt(SkScalar t, SkPoint* pos, SkVector* tangent = NULL) const; 218 void chopAt(SkScalar t, SkConic dst[2]) const; 219 void chop(SkConic dst[2]) const; 220 221 void computeAsQuadError(SkVector* err) const; 222 bool asQuadTol(SkScalar tol) const; 223 224 /** 225 * return the power-of-2 number of quads needed to approximate this conic 226 * with a sequence of quads. Will be >= 0. 227 */ 228 int computeQuadPOW2(SkScalar tol) const; 229 230 /** 231 * Chop this conic into N quads, stored continguously in pts[], where 232 * N = 1 << pow2. The amount of storage needed is (1 + 2 * N) 233 */ 234 int chopIntoQuadsPOW2(SkPoint pts[], int pow2) const; 235 236 bool findXExtrema(SkScalar* t) const; 237 bool findYExtrema(SkScalar* t) const; 238 bool chopAtXExtrema(SkConic dst[2]) const; 239 bool chopAtYExtrema(SkConic dst[2]) const; 240 241 void computeTightBounds(SkRect* bounds) const; 242 void computeFastBounds(SkRect* bounds) const; 243 244 /** Find the parameter value where the conic takes on its maximum curvature. 245 * 246 * @param t output scalar for max curvature. Will be unchanged if 247 * max curvature outside 0..1 range. 248 * 249 * @return true if max curvature found inside 0..1 range, false otherwise 250 */ 251 bool findMaxCurvature(SkScalar* t) const; 252 253 static SkScalar TransformW(const SkPoint[3], SkScalar w, const SkMatrix&); 254 255 enum { 256 kMaxConicsForArc = 5 257 }; 258 static int BuildUnitArc(const SkVector& start, const SkVector& stop, SkRotationDirection, 259 const SkMatrix*, SkConic conics[kMaxConicsForArc]); 260}; 261 262#include "SkTemplates.h" 263 264/** 265 * Help class to allocate storage for approximating a conic with N quads. 266 */ 267class SkAutoConicToQuads { 268public: 269 SkAutoConicToQuads() : fQuadCount(0) {} 270 271 /** 272 * Given a conic and a tolerance, return the array of points for the 273 * approximating quad(s). Call countQuads() to know the number of quads 274 * represented in these points. 275 * 276 * The quads are allocated to share end-points. e.g. if there are 4 quads, 277 * there will be 9 points allocated as follows 278 * quad[0] == pts[0..2] 279 * quad[1] == pts[2..4] 280 * quad[2] == pts[4..6] 281 * quad[3] == pts[6..8] 282 */ 283 const SkPoint* computeQuads(const SkConic& conic, SkScalar tol) { 284 int pow2 = conic.computeQuadPOW2(tol); 285 fQuadCount = 1 << pow2; 286 SkPoint* pts = fStorage.reset(1 + 2 * fQuadCount); 287 conic.chopIntoQuadsPOW2(pts, pow2); 288 return pts; 289 } 290 291 const SkPoint* computeQuads(const SkPoint pts[3], SkScalar weight, 292 SkScalar tol) { 293 SkConic conic; 294 conic.set(pts, weight); 295 return computeQuads(conic, tol); 296 } 297 298 int countQuads() const { return fQuadCount; } 299 300private: 301 enum { 302 kQuadCount = 8, // should handle most conics 303 kPointCount = 1 + 2 * kQuadCount, 304 }; 305 SkAutoSTMalloc<kPointCount, SkPoint> fStorage; 306 int fQuadCount; // #quads for current usage 307}; 308 309#endif 310