blob: 571159fd077eb592c909e620a90c96097b0717d4 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/sgl/SkGeometry.h
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef SkGeometry_DEFINED
19#define SkGeometry_DEFINED
20
21#include "SkMatrix.h"
22
23/** Given a quadratic equation Ax^2 + Bx + C = 0, return 0, 1, 2 roots for the
24 equation.
25*/
26int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]);
27
28///////////////////////////////////////////////////////////////////////////////
29
30/** Set pt to the point on the src quadratic specified by t. t must be
31 0 <= t <= 1.0
32*/
33void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent = NULL);
34void SkEvalQuadAtHalf(const SkPoint src[3], SkPoint* pt, SkVector* tangent = NULL);
35
36/** Given a src quadratic bezier, chop it at the specified t value,
37 where 0 < t < 1, and return the two new quadratics in dst:
38 dst[0..2] and dst[2..4]
39*/
40void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t);
41
42/** Given a src quadratic bezier, chop it at the specified t == 1/2,
43 The new quads are returned in dst[0..2] and dst[2..4]
44*/
45void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]);
46
47/** Given the 3 coefficients for a quadratic bezier (either X or Y values), look
48 for extrema, and return the number of t-values that are found that represent
49 these extrema. If the quadratic has no extrema betwee (0..1) exclusive, the
50 function returns 0.
51 Returned count tValues[]
52 0 ignored
53 1 0 < tValues[0] < 1
54*/
55int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValues[1]);
56
57/** Given 3 points on a quadratic bezier, chop it into 1, 2 beziers such that
58 the resulting beziers are monotonic in Y. This is called by the scan converter.
59 Depending on what is returned, dst[] is treated as follows
60 1 dst[0..2] is the original quad
61 2 dst[0..2] and dst[2..4] are the two new quads
62 If dst == null, it is ignored and only the count is returned.
63*/
64int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]);
65
66/** Given 3 points on a quadratic bezier, divide it into 2 quadratics
67 if the point of maximum curvature exists on the quad segment.
68 Depending on what is returned, dst[] is treated as follows
69 1 dst[0..2] is the original quad
70 2 dst[0..2] and dst[2..4] are the two new quads
71 If dst == null, it is ignored and only the count is returned.
72*/
73int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]);
74
75////////////////////////////////////////////////////////////////////////////////////////
76
77/** Convert from parametric from (pts) to polynomial coefficients
78 coeff[0]*T^3 + coeff[1]*T^2 + coeff[2]*T + coeff[3]
79*/
80void SkGetCubicCoeff(const SkPoint pts[4], SkScalar cx[4], SkScalar cy[4]);
81
82/** Set pt to the point on the src cubic specified by t. t must be
83 0 <= t <= 1.0
84*/
85void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* locOrNull, SkVector* tangentOrNull, SkVector* curvatureOrNull);
86
87/** Given a src cubic bezier, chop it at the specified t value,
88 where 0 < t < 1, and return the two new cubics in dst:
89 dst[0..3] and dst[3..6]
90*/
91void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t);
92void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], const SkScalar t[], int t_count);
93
94/** Given a src cubic bezier, chop it at the specified t == 1/2,
95 The new cubics are returned in dst[0..3] and dst[3..6]
96*/
97void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]);
98
99/** Given the 4 coefficients for a cubic bezier (either X or Y values), look
100 for extrema, and return the number of t-values that are found that represent
101 these extrema. If the cubic has no extrema betwee (0..1) exclusive, the
102 function returns 0.
103 Returned count tValues[]
104 0 ignored
105 1 0 < tValues[0] < 1
106 2 0 < tValues[0] < tValues[1] < 1
107*/
108int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, SkScalar tValues[2]);
109
110/** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that
111 the resulting beziers are monotonic in Y. This is called by the scan converter.
112 Depending on what is returned, dst[] is treated as follows
113 1 dst[0..3] is the original cubic
114 2 dst[0..3] and dst[3..6] are the two new cubics
115 3 dst[0..3], dst[3..6], dst[6..9] are the three new cubics
116 If dst == null, it is ignored and only the count is returned.
117*/
118int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]);
119
120/** Given a cubic bezier, return 0, 1, or 2 t-values that represent the
121 inflection points.
122*/
123int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[2]);
124
125/** Return 1 for no chop, or 2 for having chopped the cubic at its
126 inflection point.
127*/
128int SkChopCubicAtInflections(const SkPoint src[4], SkPoint dst[10]);
129
130int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]);
131int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], SkScalar tValues[3] = NULL);
132
133///////////////////////////////////////////////////////////////////////////////////////////
134
135enum SkRotationDirection {
136 kCW_SkRotationDirection,
137 kCCW_SkRotationDirection
138};
139
140/** Maximum number of points needed in the quadPoints[] parameter for
141 SkBuildQuadArc()
142*/
143#define kSkBuildQuadArcStorage 17
144
145/** Given 2 unit vectors and a rotation direction, fill out the specified
146 array of points with quadratic segments. Return is the number of points
147 written to, which will be { 0, 3, 5, 7, ... kSkBuildQuadArcStorage }
148
149 matrix, if not null, is appled to the points before they are returned.
150*/
151int SkBuildQuadArc(const SkVector& unitStart, const SkVector& unitStop, SkRotationDirection,
152 const SkMatrix* matrix, SkPoint quadPoints[]);
153
154//////////////////////////////////////////////////////////////////////////////
155
156#ifdef SK_DEBUG
157 class SkGeometry {
158 public:
159 static void UnitTest();
160 };
161#endif
162
163#endif