• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "SkNx.h"
13 
from_point(const SkPoint & point)14 static inline Sk2s from_point(const SkPoint& point) {
15     return Sk2s::Load(&point);
16 }
17 
to_point(const Sk2s & x)18 static inline SkPoint to_point(const Sk2s& x) {
19     SkPoint point;
20     x.store(&point);
21     return point;
22 }
23 
times_2(const Sk2s & value)24 static Sk2s times_2(const Sk2s& value) {
25     return value + value;
26 }
27 
28 /** Given a quadratic equation Ax^2 + Bx + C = 0, return 0, 1, 2 roots for the
29     equation.
30 */
31 int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]);
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 SkPoint SkEvalQuadAt(const SkPoint src[3], SkScalar t);
36 SkPoint SkEvalQuadTangentAt(const SkPoint src[3], SkScalar t);
37 
38 /** Set pt to the point on the src quadratic specified by t. t must be
39     0 <= t <= 1.0
40 */
41 void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent = nullptr);
42 
43 /** Given a src quadratic bezier, chop it at the specified t value,
44     where 0 < t < 1, and return the two new quadratics in dst:
45     dst[0..2] and dst[2..4]
46 */
47 void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t);
48 
49 /** Given a src quadratic bezier, chop it at the specified t == 1/2,
50     The new quads are returned in dst[0..2] and dst[2..4]
51 */
52 void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]);
53 
54 /** Given the 3 coefficients for a quadratic bezier (either X or Y values), look
55     for extrema, and return the number of t-values that are found that represent
56     these extrema. If the quadratic has no extrema betwee (0..1) exclusive, the
57     function returns 0.
58     Returned count      tValues[]
59     0                   ignored
60     1                   0 < tValues[0] < 1
61 */
62 int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValues[1]);
63 
64 /** Given 3 points on a quadratic bezier, chop it into 1, 2 beziers such that
65     the resulting beziers are monotonic in Y. This is called by the scan converter.
66     Depending on what is returned, dst[] is treated as follows
67     0   dst[0..2] is the original quad
68     1   dst[0..2] and dst[2..4] are the two new quads
69 */
70 int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]);
71 int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]);
72 
73 /** Given 3 points on a quadratic bezier, if the point of maximum
74     curvature exists on the segment, returns the t value for this
75     point along the curve. Otherwise it will return a value of 0.
76 */
77 SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]);
78 
79 /** Given 3 points on a quadratic bezier, divide it into 2 quadratics
80     if the point of maximum curvature exists on the quad segment.
81     Depending on what is returned, dst[] is treated as follows
82     1   dst[0..2] is the original quad
83     2   dst[0..2] and dst[2..4] are the two new quads
84     If dst == null, it is ignored and only the count is returned.
85 */
86 int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]);
87 
88 /** Given 3 points on a quadratic bezier, use degree elevation to
89     convert it into the cubic fitting the same curve. The new cubic
90     curve is returned in dst[0..3].
91 */
92 SK_API void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]);
93 
94 ///////////////////////////////////////////////////////////////////////////////
95 
96 /** Set pt to the point on the src cubic specified by t. t must be
97     0 <= t <= 1.0
98 */
99 void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* locOrNull,
100                    SkVector* tangentOrNull, SkVector* curvatureOrNull);
101 
102 /** Given a src cubic bezier, chop it at the specified t value,
103     where 0 < t < 1, and return the two new cubics in dst:
104     dst[0..3] and dst[3..6]
105 */
106 void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t);
107 
108 /** Given a src cubic bezier, chop it at the specified t values,
109     where 0 < t < 1, and return the new cubics in dst:
110     dst[0..3],dst[3..6],...,dst[3*t_count..3*(t_count+1)]
111 */
112 void SkChopCubicAt(const SkPoint src[4], SkPoint dst[], const SkScalar t[],
113                    int t_count);
114 
115 /** Given a src cubic bezier, chop it at the specified t == 1/2,
116     The new cubics are returned in dst[0..3] and dst[3..6]
117 */
118 void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]);
119 
120 /** Given the 4 coefficients for a cubic bezier (either X or Y values), look
121     for extrema, and return the number of t-values that are found that represent
122     these extrema. If the cubic has no extrema betwee (0..1) exclusive, the
123     function returns 0.
124     Returned count      tValues[]
125     0                   ignored
126     1                   0 < tValues[0] < 1
127     2                   0 < tValues[0] < tValues[1] < 1
128 */
129 int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d,
130                        SkScalar tValues[2]);
131 
132 /** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that
133     the resulting beziers are monotonic in Y. This is called by the scan converter.
134     Depending on what is returned, dst[] is treated as follows
135     0   dst[0..3] is the original cubic
136     1   dst[0..3] and dst[3..6] are the two new cubics
137     2   dst[0..3], dst[3..6], dst[6..9] are the three new cubics
138     If dst == null, it is ignored and only the count is returned.
139 */
140 int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]);
141 int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]);
142 
143 /** Given a cubic bezier, return 0, 1, or 2 t-values that represent the
144     inflection points.
145 */
146 int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[2]);
147 
148 /** Return 1 for no chop, 2 for having chopped the cubic at a single
149     inflection point, 3 for having chopped at 2 inflection points.
150     dst will hold the resulting 1, 2, or 3 cubics.
151 */
152 int SkChopCubicAtInflections(const SkPoint src[4], SkPoint dst[10]);
153 
154 int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]);
155 int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13],
156                               SkScalar tValues[3] = nullptr);
157 /** Returns t value of cusp if cubic has one; returns -1 otherwise.
158  */
159 SkScalar SkFindCubicCusp(const SkPoint src[4]);
160 
161 bool SkChopMonoCubicAtX(SkPoint src[4], SkScalar y, SkPoint dst[7]);
162 bool SkChopMonoCubicAtY(SkPoint src[4], SkScalar x, SkPoint dst[7]);
163 
164 enum class SkCubicType {
165     kSerpentine,
166     kLoop,
167     kLocalCusp,       // Cusp at a non-infinite parameter value with an inflection at t=infinity.
168     kCuspAtInfinity,  // Cusp with a cusp at t=infinity and a local inflection.
169     kQuadratic,
170     kLineOrPoint
171 };
172 
SkCubicIsDegenerate(SkCubicType type)173 static inline bool SkCubicIsDegenerate(SkCubicType type) {
174     switch (type) {
175         case SkCubicType::kSerpentine:
176         case SkCubicType::kLoop:
177         case SkCubicType::kLocalCusp:
178         case SkCubicType::kCuspAtInfinity:
179             return false;
180         case SkCubicType::kQuadratic:
181         case SkCubicType::kLineOrPoint:
182             return true;
183     }
184     SK_ABORT("Invalid SkCubicType");
185     return true;
186 }
187 
SkCubicTypeName(SkCubicType type)188 static inline const char* SkCubicTypeName(SkCubicType type) {
189     switch (type) {
190         case SkCubicType::kSerpentine: return "kSerpentine";
191         case SkCubicType::kLoop: return "kLoop";
192         case SkCubicType::kLocalCusp: return "kLocalCusp";
193         case SkCubicType::kCuspAtInfinity: return "kCuspAtInfinity";
194         case SkCubicType::kQuadratic: return "kQuadratic";
195         case SkCubicType::kLineOrPoint: return "kLineOrPoint";
196     }
197     SK_ABORT("Invalid SkCubicType");
198     return "";
199 }
200 
201 /** Returns the cubic classification.
202 
203     t[],s[] are set to the two homogeneous parameter values at which points the lines L & M
204     intersect with K, sorted from smallest to largest and oriented so positive values of the
205     implicit are on the "left" side. For a serpentine curve they are the inflection points. For a
206     loop they are the double point. For a local cusp, they are both equal and denote the cusp point.
207     For a cusp at an infinite parameter value, one will be the local inflection point and the other
208     +inf (t,s = 1,0). If the curve is degenerate (i.e. quadratic or linear) they are both set to a
209     parameter value of +inf (t,s = 1,0).
210 
211     d[] is filled with the cubic inflection function coefficients. See "Resolution Independent
212     Curve Rendering using Programmable Graphics Hardware", 4.2 Curve Categorization:
213 
214     If the input points contain infinities or NaN, the return values are undefined.
215 
216     https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
217 */
218 SkCubicType SkClassifyCubic(const SkPoint p[4], double t[2] = nullptr, double s[2] = nullptr,
219                             double d[4] = nullptr);
220 
221 ///////////////////////////////////////////////////////////////////////////////
222 
223 enum SkRotationDirection {
224     kCW_SkRotationDirection,
225     kCCW_SkRotationDirection
226 };
227 
228 struct SkConic {
SkConicSkConic229     SkConic() {}
SkConicSkConic230     SkConic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, SkScalar w) {
231         fPts[0] = p0;
232         fPts[1] = p1;
233         fPts[2] = p2;
234         fW = w;
235     }
SkConicSkConic236     SkConic(const SkPoint pts[3], SkScalar w) {
237         memcpy(fPts, pts, sizeof(fPts));
238         fW = w;
239     }
240 
241     SkPoint  fPts[3];
242     SkScalar fW;
243 
setSkConic244     void set(const SkPoint pts[3], SkScalar w) {
245         memcpy(fPts, pts, 3 * sizeof(SkPoint));
246         fW = w;
247     }
248 
setSkConic249     void set(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, SkScalar w) {
250         fPts[0] = p0;
251         fPts[1] = p1;
252         fPts[2] = p2;
253         fW = w;
254     }
255 
256     /**
257      *  Given a t-value [0...1] return its position and/or tangent.
258      *  If pos is not null, return its position at the t-value.
259      *  If tangent is not null, return its tangent at the t-value. NOTE the
260      *  tangent value's length is arbitrary, and only its direction should
261      *  be used.
262      */
263     void evalAt(SkScalar t, SkPoint* pos, SkVector* tangent = nullptr) const;
264     bool SK_WARN_UNUSED_RESULT chopAt(SkScalar t, SkConic dst[2]) const;
265     void chopAt(SkScalar t1, SkScalar t2, SkConic* dst) const;
266     void chop(SkConic dst[2]) const;
267 
268     SkPoint evalAt(SkScalar t) const;
269     SkVector evalTangentAt(SkScalar t) const;
270 
271     void computeAsQuadError(SkVector* err) const;
272     bool asQuadTol(SkScalar tol) const;
273 
274     /**
275      *  return the power-of-2 number of quads needed to approximate this conic
276      *  with a sequence of quads. Will be >= 0.
277      */
278     int SK_API computeQuadPOW2(SkScalar tol) const;
279 
280     /**
281      *  Chop this conic into N quads, stored continguously in pts[], where
282      *  N = 1 << pow2. The amount of storage needed is (1 + 2 * N)
283      */
284     int SK_API SK_WARN_UNUSED_RESULT chopIntoQuadsPOW2(SkPoint pts[], int pow2) const;
285 
286     bool findXExtrema(SkScalar* t) const;
287     bool findYExtrema(SkScalar* t) const;
288     bool chopAtXExtrema(SkConic dst[2]) const;
289     bool chopAtYExtrema(SkConic dst[2]) const;
290 
291     void computeTightBounds(SkRect* bounds) const;
292     void computeFastBounds(SkRect* bounds) const;
293 
294     /** Find the parameter value where the conic takes on its maximum curvature.
295      *
296      *  @param t   output scalar for max curvature.  Will be unchanged if
297      *             max curvature outside 0..1 range.
298      *
299      *  @return  true if max curvature found inside 0..1 range, false otherwise
300      */
301 //    bool findMaxCurvature(SkScalar* t) const;  // unimplemented
302 
303     static SkScalar TransformW(const SkPoint[3], SkScalar w, const SkMatrix&);
304 
305     enum {
306         kMaxConicsForArc = 5
307     };
308     static int BuildUnitArc(const SkVector& start, const SkVector& stop, SkRotationDirection,
309                             const SkMatrix*, SkConic conics[kMaxConicsForArc]);
310 };
311 
312 // inline helpers are contained in a namespace to avoid external leakage to fragile SkNx members
313 namespace {  // NOLINT(google-build-namespaces)
314 
315 /**
316  *  use for : eval(t) == A * t^2 + B * t + C
317  */
318 struct SkQuadCoeff {
SkQuadCoeffSkQuadCoeff319     SkQuadCoeff() {}
320 
SkQuadCoeffSkQuadCoeff321     SkQuadCoeff(const Sk2s& A, const Sk2s& B, const Sk2s& C)
322         : fA(A)
323         , fB(B)
324         , fC(C)
325     {
326     }
327 
SkQuadCoeffSkQuadCoeff328     SkQuadCoeff(const SkPoint src[3]) {
329         fC = from_point(src[0]);
330         Sk2s P1 = from_point(src[1]);
331         Sk2s P2 = from_point(src[2]);
332         fB = times_2(P1 - fC);
333         fA = P2 - times_2(P1) + fC;
334     }
335 
evalSkQuadCoeff336     Sk2s eval(SkScalar t) {
337         Sk2s tt(t);
338         return eval(tt);
339     }
340 
evalSkQuadCoeff341     Sk2s eval(const Sk2s& tt) {
342         return (fA * tt + fB) * tt + fC;
343     }
344 
345     Sk2s fA;
346     Sk2s fB;
347     Sk2s fC;
348 };
349 
350 struct SkConicCoeff {
SkConicCoeffSkConicCoeff351     SkConicCoeff(const SkConic& conic) {
352         Sk2s p0 = from_point(conic.fPts[0]);
353         Sk2s p1 = from_point(conic.fPts[1]);
354         Sk2s p2 = from_point(conic.fPts[2]);
355         Sk2s ww(conic.fW);
356 
357         Sk2s p1w = p1 * ww;
358         fNumer.fC = p0;
359         fNumer.fA = p2 - times_2(p1w) + p0;
360         fNumer.fB = times_2(p1w - p0);
361 
362         fDenom.fC = Sk2s(1);
363         fDenom.fB = times_2(ww - fDenom.fC);
364         fDenom.fA = Sk2s(0) - fDenom.fB;
365     }
366 
evalSkConicCoeff367     Sk2s eval(SkScalar t) {
368         Sk2s tt(t);
369         Sk2s numer = fNumer.eval(tt);
370         Sk2s denom = fDenom.eval(tt);
371         return numer / denom;
372     }
373 
374     SkQuadCoeff fNumer;
375     SkQuadCoeff fDenom;
376 };
377 
378 struct SkCubicCoeff {
SkCubicCoeffSkCubicCoeff379     SkCubicCoeff(const SkPoint src[4]) {
380         Sk2s P0 = from_point(src[0]);
381         Sk2s P1 = from_point(src[1]);
382         Sk2s P2 = from_point(src[2]);
383         Sk2s P3 = from_point(src[3]);
384         Sk2s three(3);
385         fA = P3 + three * (P1 - P2) - P0;
386         fB = three * (P2 - times_2(P1) + P0);
387         fC = three * (P1 - P0);
388         fD = P0;
389     }
390 
evalSkCubicCoeff391     Sk2s eval(SkScalar t) {
392         Sk2s tt(t);
393         return eval(tt);
394     }
395 
evalSkCubicCoeff396     Sk2s eval(const Sk2s& t) {
397         return ((fA * t + fB) * t + fC) * t + fD;
398     }
399 
400     Sk2s fA;
401     Sk2s fB;
402     Sk2s fC;
403     Sk2s fD;
404 };
405 
406 }
407 
408 #include "SkTemplates.h"
409 
410 /**
411  *  Help class to allocate storage for approximating a conic with N quads.
412  */
413 class SkAutoConicToQuads {
414 public:
SkAutoConicToQuads()415     SkAutoConicToQuads() : fQuadCount(0) {}
416 
417     /**
418      *  Given a conic and a tolerance, return the array of points for the
419      *  approximating quad(s). Call countQuads() to know the number of quads
420      *  represented in these points.
421      *
422      *  The quads are allocated to share end-points. e.g. if there are 4 quads,
423      *  there will be 9 points allocated as follows
424      *      quad[0] == pts[0..2]
425      *      quad[1] == pts[2..4]
426      *      quad[2] == pts[4..6]
427      *      quad[3] == pts[6..8]
428      */
computeQuads(const SkConic & conic,SkScalar tol)429     const SkPoint* computeQuads(const SkConic& conic, SkScalar tol) {
430         int pow2 = conic.computeQuadPOW2(tol);
431         fQuadCount = 1 << pow2;
432         SkPoint* pts = fStorage.reset(1 + 2 * fQuadCount);
433         fQuadCount = conic.chopIntoQuadsPOW2(pts, pow2);
434         return pts;
435     }
436 
computeQuads(const SkPoint pts[3],SkScalar weight,SkScalar tol)437     const SkPoint* computeQuads(const SkPoint pts[3], SkScalar weight,
438                                 SkScalar tol) {
439         SkConic conic;
440         conic.set(pts, weight);
441         return computeQuads(conic, tol);
442     }
443 
countQuads()444     int countQuads() const { return fQuadCount; }
445 
446 private:
447     enum {
448         kQuadCount = 8, // should handle most conics
449         kPointCount = 1 + 2 * kQuadCount,
450     };
451     SkAutoSTMalloc<kPointCount, SkPoint> fStorage;
452     int fQuadCount; // #quads for current usage
453 };
454 
455 #endif
456