Debian Codegraph @

src/core/SkGeometry.h

raw
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 "include/core/SkPathTypes.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkSpan.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/base/SkFloatingPoint.h"
17 #include "src/base/SkVx.h"
18
19 #include <cstring>
20
21 class SkMatrix;
22 struct SkRect;
23
24 static inline skvx::float2 from_point(const SkPoint& point) {
25 return skvx::float2::Load(&point);
26 }
27
28 static inline SkPoint to_point(const skvx::float2& x) {
29 SkPoint point;
30 x.store(&point);
31 return point;
32 }
33
34 static skvx::float2 times_2(const skvx::float2& value) {
35 return value + value;
36 }
37
38 /** Given a quadratic equation Ax^2 + Bx + C = 0, return 0, 1, 2 roots for the
39 equation.
40 */
41 int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]);
42
43 /** Measures the angle between two vectors, in the range [0, pi].
44 */
45 float SkMeasureAngleBetweenVectors(SkVector, SkVector);
46
47 /** Returns a new, arbitrarily scaled vector that bisects the given vectors. The returned bisector
48 will always point toward the interior of the provided vectors.
49 */
50 SkVector SkFindBisector(SkVector, SkVector);
51
52 ///////////////////////////////////////////////////////////////////////////////
53
54 SkPoint SkEvalQuadAt(const SkPoint src[3], SkScalar t);
55 SkPoint SkEvalQuadTangentAt(const SkPoint src[3], SkScalar t);
56
57 /** Set pt to the point on the src quadratic specified by t. t must be
58 0 <= t <= 1.0
59 */
60 void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent = nullptr);
61
62 /** Given a src quadratic bezier, chop it at the specified t value,
63 where 0 < t < 1, and return the two new quadratics in dst:
64 dst[0..2] and dst[2..4]
65 */
66 void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t);
67
68 /** Given a src quadratic bezier, chop it at the specified t == 1/2,
69 The new quads are returned in dst[0..2] and dst[2..4]
70 */
71 void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]);
72
73 /** Measures the rotation of the given quadratic curve in radians.
74
75 Rotation is perhaps easiest described via a driving analogy: If you drive your car along the
76 curve from p0 to p2, then by the time you arrive at p2, how many radians will your car have
77 rotated? For a quadratic this is the same as the vector inside the tangents at the endpoints.
78
79 Quadratics can have rotations in the range [0, pi].
80 */
81 inline float SkMeasureQuadRotation(const SkPoint pts[3]) {
82 return SkMeasureAngleBetweenVectors(pts[1] - pts[0], pts[2] - pts[1]);
83 }
84
85 /** Given a src quadratic bezier, returns the T value whose tangent angle is halfway between the
86 tangents at p0 and p3.
87 */
88 float SkFindQuadMidTangent(const SkPoint src[3]);
89
90 /** Given a src quadratic bezier, chop it at the tangent whose angle is halfway between the
91 tangents at p0 and p2. The new quads are returned in dst[0..2] and dst[2..4].
92 */
93 inline void SkChopQuadAtMidTangent(const SkPoint src[3], SkPoint dst[5]) {
94 SkChopQuadAt(src, dst, SkFindQuadMidTangent(src));
95 }
96
97 /** Given the 3 coefficients for a quadratic bezier (either X or Y values), look
98 for extrema, and return the number of t-values that are found that represent
99 these extrema. If the quadratic has no extrema betwee (0..1) exclusive, the
100 function returns 0.
101 Returned count tValues[]
102 0 ignored
103 1 0 < tValues[0] < 1
104 */
105 int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValues[1]);
106
107 /** Given 3 points on a quadratic bezier, chop it into 1, 2 beziers such that
108 the resulting beziers are monotonic in Y. This is called by the scan converter.
109 Depending on what is returned, dst[] is treated as follows
110 0 dst[0..2] is the original quad
111 1 dst[0..2] and dst[2..4] are the two new quads
112 */
113 int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]);
114 int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]);
115
116 /** Given 3 points on a quadratic bezier, if the point of maximum
117 curvature exists on the segment, returns the t value for this
118 point along the curve. Otherwise it will return a value of 0.
119 */
120 SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]);
121
122 /** Given 3 points on a quadratic bezier, divide it into 2 quadratics
123 if the point of maximum curvature exists on the quad segment.
124 Depending on what is returned, dst[] is treated as follows
125 1 dst[0..2] is the original quad
126 2 dst[0..2] and dst[2..4] are the two new quads
127 If dst == null, it is ignored and only the count is returned.
128 */
129 int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]);
130
131 /** Given 3 points on a quadratic bezier, use degree elevation to
132 convert it into the cubic fitting the same curve. The new cubic
133 curve is returned in dst[0..3].
134 */
135 void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]);
136
137 ///////////////////////////////////////////////////////////////////////////////
138
139 /** Set pt to the point on the src cubic specified by t. t must be
140 0 <= t <= 1.0
141 */
142 void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* locOrNull,
143 SkVector* tangentOrNull, SkVector* curvatureOrNull);
144
145 /** Given a src cubic bezier, chop it at the specified t value,
146 where 0 <= t <= 1, and return the two new cubics in dst:
147 dst[0..3] and dst[3..6]
148 */
149 void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t);
150
151 /** Given a src cubic bezier, chop it at the specified t0 and t1 values,
152 where 0 <= t0 <= t1 <= 1, and return the three new cubics in dst:
153 dst[0..3], dst[3..6], and dst[6..9]
154 */
155 void SkChopCubicAt(const SkPoint src[4], SkPoint dst[10], float t0, float t1);
156
157 /** Given a src cubic bezier, chop it at the specified t values,
158 where 0 <= t0 <= t1 <= ... <= 1, and return the new cubics in dst:
159 dst[0..3],dst[3..6],...,dst[3*t_count..3*(t_count+1)]
160 */
161 void SkChopCubicAt(const SkPoint src[4], SkPoint dst[], const SkScalar t[],
162 int t_count);
163
164 /** Given a src cubic bezier, chop it at the specified t == 1/2,
165 The new cubics are returned in dst[0..3] and dst[3..6]
166 */
167 void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]);
168
169 /** Given a cubic curve with no inflection points, this method measures the rotation in radians.
170
171 Rotation is perhaps easiest described via a driving analogy: If you drive your car along the
172 curve from p0 to p3, then by the time you arrive at p3, how many radians will your car have
173 rotated? This is not quite the same as the vector inside the tangents at the endpoints, even
174 without inflection, because the curve might rotate around the outside of the
175 tangents (>= 180 degrees) or the inside (<= 180 degrees).
176
177 Cubics can have rotations in the range [0, 2*pi].
178
179 NOTE: The caller must either call SkChopCubicAtInflections or otherwise prove that the provided
180 cubic has no inflection points prior to calling this method.
181 */
182 float SkMeasureNonInflectCubicRotation(const SkPoint[4]);
183
184 /** Given a src cubic bezier, returns the T value whose tangent angle is halfway between the
185 tangents at p0 and p3.
186 */
187 float SkFindCubicMidTangent(const SkPoint src[4]);
188
189 /** Given a src cubic bezier, chop it at the tangent whose angle is halfway between the
190 tangents at p0 and p3. The new cubics are returned in dst[0..3] and dst[3..6].
191
192 NOTE: 0- and 360-degree flat lines don't have single points of midtangent.
193 (tangent == midtangent at every point on these curves except the cusp points.)
194 If this is the case then we simply chop at a point which guarantees neither side rotates more
195 than 180 degrees.
196 */
197 inline void SkChopCubicAtMidTangent(const SkPoint src[4], SkPoint dst[7]) {
198 SkChopCubicAt(src, dst, SkFindCubicMidTangent(src));
199 }
200
201 /** Given the 4 coefficients for a cubic bezier (either X or Y values), look
202 for extrema, and return the number of t-values that are found that represent
203 these extrema. If the cubic has no extrema betwee (0..1) exclusive, the
204 function returns 0.
205 Returned count tValues[]
206 0 ignored
207 1 0 < tValues[0] < 1
208 2 0 < tValues[0] < tValues[1] < 1
209 */
210 int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d,
211 SkScalar tValues[2]);
212
213 /** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that
214 the resulting beziers are monotonic in Y. This is called by the scan converter.
215 Depending on what is returned, dst[] is treated as follows
216 0 dst[0..3] is the original cubic
217 1 dst[0..3] and dst[3..6] are the two new cubics
218 2 dst[0..3], dst[3..6], dst[6..9] are the three new cubics
219 If dst == null, it is ignored and only the count is returned.
220 */
221 int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]);
222 int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]);
223
224 /** Given a cubic bezier, return 0, 1, or 2 t-values that represent the
225 inflection points.
226 */
227 int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[2]);
228
229 /** Return 1 for no chop, 2 for having chopped the cubic at a single
230 inflection point, 3 for having chopped at 2 inflection points.
231 dst will hold the resulting 1, 2, or 3 cubics.
232 */
233 int SkChopCubicAtInflections(const SkPoint src[4], SkPoint dst[10]);
234
235 int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]);
236 int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13],
237 SkScalar tValues[3] = nullptr);
238 /** Returns t value of cusp if cubic has one; returns -1 otherwise.
239 */
240 SkScalar SkFindCubicCusp(const SkPoint src[4]);
241
242 /** Given a monotonically increasing or decreasing cubic bezier src, chop it
243 * where the X value is the specified value. The returned cubics will be in
244 * dst, sharing the middle point. That is, the first cubic is dst[0..3] and
245 * the second dst[3..6].
246 *
247 * If the cubic provided is *not* monotone, it will be chopped at the first
248 * time the curve has the specified X value.
249 *
250 * If the cubic never reaches the specified value, the function returns false.
251 */
252 bool SkChopMonoCubicAtX(const SkPoint src[4], SkScalar x, SkPoint dst[7]);
253
254 /** Given a monotonically increasing or decreasing cubic bezier src, chop it
255 * where the Y value is the specified value. The returned cubics will be in
256 * dst, sharing the middle point. That is, the first cubic is dst[0..3] and
257 * the second dst[3..6].
258 *
259 * If the cubic provided is *not* monotone, it will be chopped at the first
260 * time the curve has the specified Y value.
261 *
262 * If the cubic never reaches the specified value, the function returns false.
263 */
264 bool SkChopMonoCubicAtY(const SkPoint src[4], SkScalar y, SkPoint dst[7]);
265
266 enum class SkCubicType {
267 kSerpentine,
268 kLoop,
269 kLocalCusp, // Cusp at a non-infinite parameter value with an inflection at t=infinity.
270 kCuspAtInfinity, // Cusp with a cusp at t=infinity and a local inflection.
271 kQuadratic,
272 kLineOrPoint
273 };
274
275 static inline bool SkCubicIsDegenerate(SkCubicType type) {
276 switch (type) {
277 case SkCubicType::kSerpentine:
278 case SkCubicType::kLoop:
279 case SkCubicType::kLocalCusp:
280 case SkCubicType::kCuspAtInfinity:
281 return false;
282 case SkCubicType::kQuadratic:
283 case SkCubicType::kLineOrPoint:
284 return true;
285 }
286 SK_ABORT("Invalid SkCubicType");
287 }
288
289 static inline const char* SkCubicTypeName(SkCubicType type) {
290 switch (type) {
291 case SkCubicType::kSerpentine: return "kSerpentine";
292 case SkCubicType::kLoop: return "kLoop";
293 case SkCubicType::kLocalCusp: return "kLocalCusp";
294 case SkCubicType::kCuspAtInfinity: return "kCuspAtInfinity";
295 case SkCubicType::kQuadratic: return "kQuadratic";
296 case SkCubicType::kLineOrPoint: return "kLineOrPoint";
297 }
298 SK_ABORT("Invalid SkCubicType");
299 }
300
301 /** Returns the cubic classification.
302
303 t[],s[] are set to the two homogeneous parameter values at which points the lines L & M
304 intersect with K, sorted from smallest to largest and oriented so positive values of the
305 implicit are on the "left" side. For a serpentine curve they are the inflection points. For a
306 loop they are the double point. For a local cusp, they are both equal and denote the cusp point.
307 For a cusp at an infinite parameter value, one will be the local inflection point and the other
308 +inf (t,s = 1,0). If the curve is degenerate (i.e. quadratic or linear) they are both set to a
309 parameter value of +inf (t,s = 1,0).
310
311 d[] is filled with the cubic inflection function coefficients. See "Resolution Independent
312 Curve Rendering using Programmable Graphics Hardware", 4.2 Curve Categorization:
313
314 If the input points contain infinities or NaN, the return values are undefined.
315
316 https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
317 */
318 SkCubicType SkClassifyCubic(const SkPoint p[4], double t[2] = nullptr, double s[2] = nullptr,
319 double d[4] = nullptr);
320
321 ///////////////////////////////////////////////////////////////////////////////
322
323 struct SkConic {
324 SkConic() {}
325 SkConic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, SkScalar w) {
326 this->set(p0, p1, p2, w);
327 }
328
329 SkConic(const SkPoint pts[3], SkScalar w) {
330 this->set(pts, w);
331 }
332
333 SkPoint fPts[3];
334 SkScalar fW;
335
336 void set(const SkPoint pts[3], SkScalar w) {
337 memcpy(fPts, pts, 3 * sizeof(SkPoint));
338 this->setW(w);
339 }
340
341 void set(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, SkScalar w) {
342 fPts[0] = p0;
343 fPts[1] = p1;
344 fPts[2] = p2;
345 this->setW(w);
346 }
347
348 void setW(SkScalar w) {
349 if (SkIsFinite(w)) {
350 SkASSERT(w > 0);
351 }
352
353 // Guard against bad weights by forcing them to 1.
354 fW = w > 0 && SkIsFinite(w) ? w : 1;
355 }
356
357 /**
358 * Given a t-value [0...1] return its position and/or tangent.
359 * If pos is not null, return its position at the t-value.
360 * If tangent is not null, return its tangent at the t-value. NOTE the
361 * tangent value's length is arbitrary, and only its direction should
362 * be used.
363 */
364 void evalAt(SkScalar t, SkPoint* pos, SkVector* tangent = nullptr) const;
365 [[nodiscard]] bool chopAt(SkScalar t, SkConic dst[2]) const;
366 void chopAt(SkScalar t1, SkScalar t2, SkConic* dst) const;
367 void chop(SkConic dst[2]) const;
368
369 SkPoint evalAt(SkScalar t) const;
370 SkVector evalTangentAt(SkScalar t) const;
371
372 void computeAsQuadError(SkVector* err) const;
373 bool asQuadTol(SkScalar tol) const;
374
375 /**
376 * return the power-of-2 number of quads needed to approximate this conic
377 * with a sequence of quads. Will be >= 0.
378 */
379 int SK_SPI computeQuadPOW2(SkScalar tol) const;
380
381 /**
382 * Chop this conic into N quads, stored continguously in pts[], where
383 * N = 1 << pow2. The amount of storage needed is (1 + 2 * N)
384 */
385 [[nodiscard]] int SK_SPI chopIntoQuadsPOW2(SkPoint pts[], int pow2) const;
386
387 float findMidTangent() const;
388 bool findXExtrema(SkScalar* t) const;
389 bool findYExtrema(SkScalar* t) const;
390 bool chopAtXExtrema(SkConic dst[2]) const;
391 bool chopAtYExtrema(SkConic dst[2]) const;
392
393 void computeTightBounds(SkRect* bounds) const;
394 void computeFastBounds(SkRect* bounds) const;
395
396 /** Find the parameter value where the conic takes on its maximum curvature.
397 *
398 * @param t output scalar for max curvature. Will be unchanged if
399 * max curvature outside 0..1 range.
400 *
401 * @return true if max curvature found inside 0..1 range, false otherwise
402 */
403 // bool findMaxCurvature(SkScalar* t) const; // unimplemented
404
405 static SkScalar TransformW(const SkPoint[3], SkScalar w, const SkMatrix&);
406
407 enum {
408 kMaxConicsForArc = 5
409 };
410 static int BuildUnitArc(const SkVector& start, const SkVector& stop, SkPathDirection,
411 const SkMatrix*, SkConic conics[kMaxConicsForArc]);
412 };
413
414 // inline helpers are contained in a namespace to avoid external leakage to fragile SkVx members
415 namespace { // NOLINT(google-build-namespaces)
416
417 /**
418 * use for : eval(t) == A * t^2 + B * t + C
419 */
420 struct SkQuadCoeff {
421 SkQuadCoeff() {}
422
423 SkQuadCoeff(const skvx::float2& A, const skvx::float2& B, const skvx::float2& C)
424 : fA(A)
425 , fB(B)
426 , fC(C)
427 {
428 }
429
430 SkQuadCoeff(const SkPoint src[3]) {
431 fC = from_point(src[0]);
432 auto P1 = from_point(src[1]);
433 auto P2 = from_point(src[2]);
434 fB = times_2(P1 - fC);
435 fA = P2 - times_2(P1) + fC;
436 }
437
438 skvx::float2 eval(const skvx::float2& tt) const {
439 return (fA * tt + fB) * tt + fC;
440 }
441
442 skvx::float2 fA;
443 skvx::float2 fB;
444 skvx::float2 fC;
445 };
446
447 struct SkConicCoeff {
448 SkConicCoeff(const SkConic& conic) {
449 skvx::float2 p0 = from_point(conic.fPts[0]);
450 skvx::float2 p1 = from_point(conic.fPts[1]);
451 skvx::float2 p2 = from_point(conic.fPts[2]);
452 skvx::float2 ww(conic.fW);
453
454 auto p1w = p1 * ww;
455 fNumer.fC = p0;
456 fNumer.fA = p2 - times_2(p1w) + p0;
457 fNumer.fB = times_2(p1w - p0);
458
459 fDenom.fC = 1;
460 fDenom.fB = times_2(ww - fDenom.fC);
461 fDenom.fA = 0 - fDenom.fB;
462 }
463
464 skvx::float2 eval(SkScalar t) const {
465 skvx::float2 tt(t);
466 skvx::float2 numer = fNumer.eval(tt);
467 skvx::float2 denom = fDenom.eval(tt);
468 return numer / denom;
469 }
470
471 SkQuadCoeff fNumer;
472 SkQuadCoeff fDenom;
473 };
474
475 struct SkCubicCoeff {
476 SkCubicCoeff(const SkPoint src[4]) {
477 skvx::float2 P0 = from_point(src[0]);
478 skvx::float2 P1 = from_point(src[1]);
479 skvx::float2 P2 = from_point(src[2]);
480 skvx::float2 P3 = from_point(src[3]);
481 skvx::float2 three(3);
482 fA = P3 + three * (P1 - P2) - P0;
483 fB = three * (P2 - times_2(P1) + P0);
484 fC = three * (P1 - P0);
485 fD = P0;
486 }
487
488 skvx::float2 eval(const skvx::float2& t) const {
489 return ((fA * t + fB) * t + fC) * t + fD;
490 }
491
492 skvx::float2 fA;
493 skvx::float2 fB;
494 skvx::float2 fC;
495 skvx::float2 fD;
496 };
497
498 } // namespace
499
500 #include "include/private/base/SkTemplates.h"
501
502 /**
503 * Help class to allocate storage for approximating a conic with N quads.
504 */
505 class [[nodiscard]] SkAutoConicToQuads {
506 public:
507 SkAutoConicToQuads() : fQuadCount(0) {}
508
509 /**
510 * Given a conic and a tolerance, return the array of points for the
511 * approximating quad(s). Call countQuads() to know the number of quads
512 * represented in these points.
513 *
514 * The quads are allocated to share end-points. e.g. if there are 4 quads,
515 * there will be 9 points allocated as follows
516 * quad[0] == pts[0..2]
517 * quad[1] == pts[2..4]
518 * quad[2] == pts[4..6]
519 * quad[3] == pts[6..8]
520 */
521 const SkPoint* computeQuads(const SkConic& conic, SkScalar tol) {
522 int pow2 = conic.computeQuadPOW2(tol);
523 fQuadCount = 1 << pow2;
524 SkPoint* pts = fStorage.reset(1 + 2 * fQuadCount);
525 fQuadCount = conic.chopIntoQuadsPOW2(pts, pow2);
526 return pts;
527 }
528
529 const SkPoint* computeQuads(SkSpan<const SkPoint> pts, SkScalar weight, SkScalar tol) {
530 SkConic conic;
531 conic.set(pts.data(), weight);
532 return computeQuads(conic, tol);
533 }
534
535 const SkPoint* computeQuads(const SkPoint pts[3], SkScalar weight, SkScalar tol) {
536 return this->computeQuads({pts, 3}, weight, tol);
537 }
538
539 int countQuads() const { return fQuadCount; }
540
541 private:
542 enum {
543 kQuadCount = 8, // should handle most conics
544 kPointCount = 1 + 2 * kQuadCount,
545 };
546 skia_private::AutoSTMalloc<kPointCount, SkPoint> fStorage;
547 int fQuadCount; // #quads for current usage
548 };
549
550 #endif