blob: 8e7a01d0915aa74a039c8d8e05abf3540fd4e4c3 [file] [log] [blame]
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +00006 */
7
8#include "GrPathUtils.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00009
robertphillipsd5373412014-06-02 10:20:14 -070010#include "GrTypes.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000011#include "SkGeometry.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000012
bsalomon@google.com81712882012-11-01 17:12:34 +000013SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000014 const SkMatrix& viewM,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000015 const SkRect& pathBounds) {
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000016 // In order to tesselate the path we get a bound on how much the matrix can
commit-bot@chromium.org18786512014-05-20 14:53:45 +000017 // scale when mapping to screen coordinates.
18 SkScalar stretch = viewM.getMaxScale();
bsalomon@google.com81712882012-11-01 17:12:34 +000019 SkScalar srcTol = devTol;
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000020
21 if (stretch < 0) {
bsalomon@google.com38396322011-09-09 19:32:04 +000022 // take worst case mapRadius amoung four corners.
23 // (less than perfect)
24 for (int i = 0; i < 4; ++i) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000025 SkMatrix mat;
bsalomon@google.com38396322011-09-09 19:32:04 +000026 mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight,
27 (i < 2) ? pathBounds.fTop : pathBounds.fBottom);
28 mat.postConcat(viewM);
29 stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1));
30 }
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000031 }
bsalomon@google.com81712882012-11-01 17:12:34 +000032 srcTol = SkScalarDiv(srcTol, stretch);
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000033 return srcTol;
34}
35
bsalomon@google.comb5b31682011-06-16 18:05:35 +000036static const int MAX_POINTS_PER_CURVE = 1 << 10;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000037static const SkScalar gMinCurveTol = 0.0001f;
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000038
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000039uint32_t GrPathUtils::quadraticPointCount(const SkPoint points[],
bsalomon@google.com81712882012-11-01 17:12:34 +000040 SkScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +000041 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +000042 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +000043 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000044 SkASSERT(tol > 0);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000045
bsalomon@google.com81712882012-11-01 17:12:34 +000046 SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000047 if (d <= tol) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000048 return 1;
49 } else {
50 // Each time we subdivide, d should be cut in 4. So we need to
51 // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x)
52 // points.
53 // 2^(log4(x)) = sqrt(x);
reed@google.come1ca7052013-12-17 19:22:07 +000054 int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol)));
bsalomon@google.com61f3bde2011-06-17 20:06:49 +000055 int pow2 = GrNextPow2(temp);
56 // Because of NaNs & INFs we can wind up with a degenerate temp
57 // such that pow2 comes out negative. Also, our point generator
58 // will always output at least one pt.
59 if (pow2 < 1) {
60 pow2 = 1;
61 }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000062 return SkTMin(pow2, MAX_POINTS_PER_CURVE);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000063 }
64}
65
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000066uint32_t GrPathUtils::generateQuadraticPoints(const SkPoint& p0,
67 const SkPoint& p1,
68 const SkPoint& p2,
bsalomon@google.com81712882012-11-01 17:12:34 +000069 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000070 SkPoint** points,
tomhudson@google.comc10a8882011-06-28 15:19:32 +000071 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000072 if (pointsLeft < 2 ||
73 (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) {
74 (*points)[0] = p2;
75 *points += 1;
76 return 1;
77 }
78
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000079 SkPoint q[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000080 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
81 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000082 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000083 SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000084
85 pointsLeft >>= 1;
86 uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft);
87 uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft);
88 return a + b;
89}
90
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000091uint32_t GrPathUtils::cubicPointCount(const SkPoint points[],
bsalomon@google.com81712882012-11-01 17:12:34 +000092 SkScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +000093 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +000094 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +000095 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000096 SkASSERT(tol > 0);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000097
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000098 SkScalar d = SkTMax(
tomhudson@google.comc10a8882011-06-28 15:19:32 +000099 points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]),
100 points[2].distanceToLineSegmentBetweenSqd(points[0], points[3]));
epoger@google.com2047f002011-05-17 17:36:59 +0000101 d = SkScalarSqrt(d);
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000102 if (d <= tol) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000103 return 1;
104 } else {
reed@google.come1ca7052013-12-17 19:22:07 +0000105 int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol)));
bsalomon@google.com61f3bde2011-06-17 20:06:49 +0000106 int pow2 = GrNextPow2(temp);
107 // Because of NaNs & INFs we can wind up with a degenerate temp
108 // such that pow2 comes out negative. Also, our point generator
109 // will always output at least one pt.
110 if (pow2 < 1) {
111 pow2 = 1;
112 }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000113 return SkTMin(pow2, MAX_POINTS_PER_CURVE);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000114 }
115}
116
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000117uint32_t GrPathUtils::generateCubicPoints(const SkPoint& p0,
118 const SkPoint& p1,
119 const SkPoint& p2,
120 const SkPoint& p3,
bsalomon@google.com81712882012-11-01 17:12:34 +0000121 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000122 SkPoint** points,
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000123 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000124 if (pointsLeft < 2 ||
125 (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd &&
126 p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) {
127 (*points)[0] = p3;
128 *points += 1;
129 return 1;
130 }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000131 SkPoint q[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000132 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
133 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
134 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000135 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000136 SkPoint r[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000137 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
138 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000139 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000140 SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000141 pointsLeft >>= 1;
142 uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
143 uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
144 return a + b;
145}
146
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000147int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths,
bsalomon@google.com81712882012-11-01 17:12:34 +0000148 SkScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000149 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +0000150 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000151 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000152 SkASSERT(tol > 0);
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000153
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000154 int pointCount = 0;
155 *subpaths = 1;
156
157 bool first = true;
158
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000159 SkPath::Iter iter(path, false);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000160 SkPath::Verb verb;
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000161
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000162 SkPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000163 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000164
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000165 switch (verb) {
166 case SkPath::kLine_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000167 pointCount += 1;
168 break;
egdanielaf18a092015-01-05 10:22:28 -0800169 case SkPath::kConic_Verb: {
170 SkScalar weight = iter.conicWeight();
171 SkAutoConicToQuads converter;
172 const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f);
173 for (int i = 0; i < converter.countQuads(); ++i) {
174 pointCount += quadraticPointCount(quadPts + 2*i, tol);
175 }
176 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000177 case SkPath::kQuad_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000178 pointCount += quadraticPointCount(pts, tol);
179 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000180 case SkPath::kCubic_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000181 pointCount += cubicPointCount(pts, tol);
182 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000183 case SkPath::kMove_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000184 pointCount += 1;
185 if (!first) {
186 ++(*subpaths);
187 }
188 break;
189 default:
190 break;
191 }
192 first = false;
193 }
194 return pointCount;
195}
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000196
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000197void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) {
bsalomon@google.com19713172012-03-15 13:51:08 +0000198 SkMatrix m;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000199 // We want M such that M * xy_pt = uv_pt
200 // We know M * control_pts = [0 1/2 1]
201 // [0 0 1]
202 // [1 1 1]
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000203 // And control_pts = [x0 x1 x2]
204 // [y0 y1 y2]
205 // [1 1 1 ]
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000206 // We invert the control pt matrix and post concat to both sides to get M.
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000207 // Using the known form of the control point matrix and the result, we can
208 // optimize and improve precision.
209
210 double x0 = qPts[0].fX;
211 double y0 = qPts[0].fY;
212 double x1 = qPts[1].fX;
213 double y1 = qPts[1].fY;
214 double x2 = qPts[2].fX;
215 double y2 = qPts[2].fY;
216 double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2;
217
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000218 if (!sk_float_isfinite(det)
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000219 || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) {
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000220 // The quad is degenerate. Hopefully this is rare. Find the pts that are
221 // farthest apart to compute a line (unless it is really a pt).
222 SkScalar maxD = qPts[0].distanceToSqd(qPts[1]);
223 int maxEdge = 0;
224 SkScalar d = qPts[1].distanceToSqd(qPts[2]);
225 if (d > maxD) {
226 maxD = d;
227 maxEdge = 1;
228 }
229 d = qPts[2].distanceToSqd(qPts[0]);
230 if (d > maxD) {
231 maxD = d;
232 maxEdge = 2;
233 }
234 // We could have a tolerance here, not sure if it would improve anything
235 if (maxD > 0) {
236 // Set the matrix to give (u = 0, v = distance_to_line)
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000237 SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge];
bsalomon@google.com20e542e2012-02-15 18:49:41 +0000238 // when looking from the point 0 down the line we want positive
239 // distances to be to the left. This matches the non-degenerate
240 // case.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000241 lineVec.setOrthog(lineVec, SkPoint::kLeft_Side);
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000242 lineVec.dot(qPts[0]);
bsalomon@google.com19713172012-03-15 13:51:08 +0000243 // first row
244 fM[0] = 0;
245 fM[1] = 0;
246 fM[2] = 0;
247 // second row
248 fM[3] = lineVec.fX;
249 fM[4] = lineVec.fY;
250 fM[5] = -lineVec.dot(qPts[maxEdge]);
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000251 } else {
252 // It's a point. It should cover zero area. Just set the matrix such
253 // that (u, v) will always be far away from the quad.
bsalomon@google.com19713172012-03-15 13:51:08 +0000254 fM[0] = 0; fM[1] = 0; fM[2] = 100.f;
255 fM[3] = 0; fM[4] = 0; fM[5] = 100.f;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000256 }
257 } else {
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000258 double scale = 1.0/det;
259
260 // compute adjugate matrix
261 double a0, a1, a2, a3, a4, a5, a6, a7, a8;
262 a0 = y1-y2;
263 a1 = x2-x1;
264 a2 = x1*y2-x2*y1;
265
266 a3 = y2-y0;
267 a4 = x0-x2;
268 a5 = x2*y0-x0*y2;
269
270 a6 = y0-y1;
271 a7 = x1-x0;
272 a8 = x0*y1-x1*y0;
273
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000274 // this performs the uv_pts*adjugate(control_pts) multiply,
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000275 // then does the scale by 1/det afterwards to improve precision
276 m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale);
277 m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale);
278 m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale);
279
280 m[SkMatrix::kMSkewY] = (float)(a6*scale);
281 m[SkMatrix::kMScaleY] = (float)(a7*scale);
282 m[SkMatrix::kMTransY] = (float)(a8*scale);
283
284 m[SkMatrix::kMPersp0] = (float)((a0 + a3 + a6)*scale);
285 m[SkMatrix::kMPersp1] = (float)((a1 + a4 + a7)*scale);
286 m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale);
bsalomon@google.com19713172012-03-15 13:51:08 +0000287
288 // The matrix should not have perspective.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000289 SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000290 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL);
291 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL);
bsalomon@google.com19713172012-03-15 13:51:08 +0000292
293 // It may not be normalized to have 1.0 in the bottom right
294 float m33 = m.get(SkMatrix::kMPersp2);
295 if (1.f != m33) {
296 m33 = 1.f / m33;
297 fM[0] = m33 * m.get(SkMatrix::kMScaleX);
298 fM[1] = m33 * m.get(SkMatrix::kMSkewX);
299 fM[2] = m33 * m.get(SkMatrix::kMTransX);
300 fM[3] = m33 * m.get(SkMatrix::kMSkewY);
301 fM[4] = m33 * m.get(SkMatrix::kMScaleY);
302 fM[5] = m33 * m.get(SkMatrix::kMTransY);
303 } else {
304 fM[0] = m.get(SkMatrix::kMScaleX);
305 fM[1] = m.get(SkMatrix::kMSkewX);
306 fM[2] = m.get(SkMatrix::kMTransX);
307 fM[3] = m.get(SkMatrix::kMSkewY);
308 fM[4] = m.get(SkMatrix::kMScaleY);
309 fM[5] = m.get(SkMatrix::kMTransY);
310 }
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000311 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000312}
313
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000314////////////////////////////////////////////////////////////////////////////////
315
316// k = (y2 - y0, x0 - x2, (x2 - x0)*y0 - (y2 - y0)*x0 )
317// l = (2*w * (y1 - y0), 2*w * (x0 - x1), 2*w * (x1*y0 - x0*y1))
318// m = (2*w * (y2 - y1), 2*w * (x1 - x2), 2*w * (x2*y1 - x1*y2))
319void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkScalar klm[9]) {
320 const SkScalar w2 = 2.f * weight;
321 klm[0] = p[2].fY - p[0].fY;
322 klm[1] = p[0].fX - p[2].fX;
323 klm[2] = (p[2].fX - p[0].fX) * p[0].fY - (p[2].fY - p[0].fY) * p[0].fX;
324
325 klm[3] = w2 * (p[1].fY - p[0].fY);
326 klm[4] = w2 * (p[0].fX - p[1].fX);
327 klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY);
328
329 klm[6] = w2 * (p[2].fY - p[1].fY);
330 klm[7] = w2 * (p[1].fX - p[2].fX);
331 klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY);
332
333 // scale the max absolute value of coeffs to 10
334 SkScalar scale = 0.f;
335 for (int i = 0; i < 9; ++i) {
336 scale = SkMaxScalar(scale, SkScalarAbs(klm[i]));
337 }
338 SkASSERT(scale > 0.f);
339 scale = 10.f / scale;
340 for (int i = 0; i < 9; ++i) {
341 klm[i] *= scale;
342 }
343}
344
345////////////////////////////////////////////////////////////////////////////////
346
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000347namespace {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000348
349// a is the first control point of the cubic.
350// ab is the vector from a to the second control point.
351// dc is the vector from the fourth to the third control point.
352// d is the fourth control point.
353// p is the candidate quadratic control point.
354// this assumes that the cubic doesn't inflect and is simple
355bool is_point_within_cubic_tangents(const SkPoint& a,
356 const SkVector& ab,
357 const SkVector& dc,
358 const SkPoint& d,
359 SkPath::Direction dir,
360 const SkPoint p) {
361 SkVector ap = p - a;
362 SkScalar apXab = ap.cross(ab);
363 if (SkPath::kCW_Direction == dir) {
364 if (apXab > 0) {
365 return false;
366 }
367 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000368 SkASSERT(SkPath::kCCW_Direction == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000369 if (apXab < 0) {
370 return false;
371 }
372 }
373
374 SkVector dp = p - d;
375 SkScalar dpXdc = dp.cross(dc);
376 if (SkPath::kCW_Direction == dir) {
377 if (dpXdc < 0) {
378 return false;
379 }
380 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000381 SkASSERT(SkPath::kCCW_Direction == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000382 if (dpXdc > 0) {
383 return false;
384 }
385 }
386 return true;
387}
388
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000389void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000390 SkScalar toleranceSqd,
391 bool constrainWithinTangents,
392 SkPath::Direction dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000393 SkTArray<SkPoint, true>* quads,
394 int sublevel = 0) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000395
396 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is
397 // p[2]. Point d is always p[3]. Point c is p[2] unless p[2] == p[3], in which case it is p[1].
398
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000399 SkVector ab = p[1] - p[0];
400 SkVector dc = p[2] - p[3];
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000401
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000402 if (ab.isZero()) {
403 if (dc.isZero()) {
404 SkPoint* degQuad = quads->push_back_n(3);
405 degQuad[0] = p[0];
406 degQuad[1] = p[0];
407 degQuad[2] = p[3];
408 return;
409 }
410 ab = p[2] - p[0];
411 }
412 if (dc.isZero()) {
413 dc = p[1] - p[3];
414 }
415
bsalomon3935a7b2014-06-19 12:33:08 -0700416 // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the
417 // constraint that the quad point falls between the tangents becomes hard to enforce and we are
418 // likely to hit the max subdivision count. However, in this case the cubic is approaching a
419 // line and the accuracy of the quad point isn't so important. We check if the two middle cubic
420 // control points are very close to the baseline vector. If so then we just pick quadratic
421 // points on the control polygon.
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000422
423 if (constrainWithinTangents) {
424 SkVector da = p[0] - p[3];
bsalomon3935a7b2014-06-19 12:33:08 -0700425 bool doQuads = dc.lengthSqd() < SK_ScalarNearlyZero ||
426 ab.lengthSqd() < SK_ScalarNearlyZero;
427 if (!doQuads) {
428 SkScalar invDALengthSqd = da.lengthSqd();
429 if (invDALengthSqd > SK_ScalarNearlyZero) {
430 invDALengthSqd = SkScalarInvert(invDALengthSqd);
431 // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a.
432 // same goes for point c using vector cd.
433 SkScalar detABSqd = ab.cross(da);
434 detABSqd = SkScalarSquare(detABSqd);
435 SkScalar detDCSqd = dc.cross(da);
436 detDCSqd = SkScalarSquare(detDCSqd);
437 if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd &&
438 SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) {
439 doQuads = true;
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000440 }
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000441 }
442 }
bsalomon3935a7b2014-06-19 12:33:08 -0700443 if (doQuads) {
444 SkPoint b = p[0] + ab;
445 SkPoint c = p[3] + dc;
446 SkPoint mid = b + c;
447 mid.scale(SK_ScalarHalf);
448 // Insert two quadratics to cover the case when ab points away from d and/or dc
449 // points away from a.
450 if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) {
451 SkPoint* qpts = quads->push_back_n(6);
452 qpts[0] = p[0];
453 qpts[1] = b;
454 qpts[2] = mid;
455 qpts[3] = mid;
456 qpts[4] = c;
457 qpts[5] = p[3];
458 } else {
459 SkPoint* qpts = quads->push_back_n(3);
460 qpts[0] = p[0];
461 qpts[1] = mid;
462 qpts[2] = p[3];
463 }
464 return;
465 }
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000466 }
467
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000468 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000469 static const int kMaxSubdivs = 10;
470
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000471 ab.scale(kLengthScale);
472 dc.scale(kLengthScale);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000473
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000474 // e0 and e1 are extrapolations along vectors ab and dc.
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000475 SkVector c0 = p[0];
476 c0 += ab;
477 SkVector c1 = p[3];
478 c1 += dc;
479
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000480 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000481 if (dSqd < toleranceSqd) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000482 SkPoint cAvg = c0;
483 cAvg += c1;
484 cAvg.scale(SK_ScalarHalf);
485
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000486 bool subdivide = false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000487
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000488 if (constrainWithinTangents &&
489 !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000490 // choose a new cAvg that is the intersection of the two tangent lines.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000491 ab.setOrthog(ab);
492 SkScalar z0 = -ab.dot(p[0]);
493 dc.setOrthog(dc);
494 SkScalar z1 = -dc.dot(p[3]);
495 cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY);
496 cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1);
497 SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX);
498 z = SkScalarInvert(z);
499 cAvg.fX *= z;
500 cAvg.fY *= z;
501 if (sublevel <= kMaxSubdivs) {
502 SkScalar d0Sqd = c0.distanceToSqd(cAvg);
503 SkScalar d1Sqd = c1.distanceToSqd(cAvg);
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000504 // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know
505 // the distances and tolerance can't be negative.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000506 // (d0 + d1)^2 > toleranceSqd
507 // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd
508 SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd));
509 subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd;
510 }
511 }
512 if (!subdivide) {
513 SkPoint* pts = quads->push_back_n(3);
514 pts[0] = p[0];
515 pts[1] = cAvg;
516 pts[2] = p[3];
517 return;
518 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000519 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000520 SkPoint choppedPts[7];
521 SkChopCubicAtHalf(p, choppedPts);
522 convert_noninflect_cubic_to_quads(choppedPts + 0,
523 toleranceSqd,
524 constrainWithinTangents,
525 dir,
526 quads,
527 sublevel + 1);
528 convert_noninflect_cubic_to_quads(choppedPts + 3,
529 toleranceSqd,
530 constrainWithinTangents,
531 dir,
532 quads,
533 sublevel + 1);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000534}
535}
536
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000537void GrPathUtils::convertCubicToQuads(const SkPoint p[4],
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000538 SkScalar tolScale,
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000539 bool constrainWithinTangents,
540 SkPath::Direction dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000541 SkTArray<SkPoint, true>* quads) {
542 SkPoint chopped[10];
543 int count = SkChopCubicAtInflections(p, chopped);
544
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000545 // base tolerance is 1 pixel.
546 static const SkScalar kTolerance = SK_Scalar1;
547 const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance));
548
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000549 for (int i = 0; i < count; ++i) {
550 SkPoint* cubic = chopped + 3*i;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000551 convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000552 }
553
554}
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000555
556////////////////////////////////////////////////////////////////////////////////
557
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000558// Solves linear system to extract klm
559// P.K = k (similarly for l, m)
560// Where P is matrix of control points
561// K is coefficients for the line K
562// k is vector of values of K evaluated at the control points
563// Solving for K, thus K = P^(-1) . k
564static void calc_cubic_klm(const SkPoint p[4], const SkScalar controlK[4],
565 const SkScalar controlL[4], const SkScalar controlM[4],
566 SkScalar k[3], SkScalar l[3], SkScalar m[3]) {
567 SkMatrix matrix;
568 matrix.setAll(p[0].fX, p[0].fY, 1.f,
569 p[1].fX, p[1].fY, 1.f,
570 p[2].fX, p[2].fY, 1.f);
571 SkMatrix inverse;
572 if (matrix.invert(&inverse)) {
573 inverse.mapHomogeneousPoints(k, controlK, 1);
574 inverse.mapHomogeneousPoints(l, controlL, 1);
575 inverse.mapHomogeneousPoints(m, controlM, 1);
576 }
577
578}
579
580static void set_serp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
581 SkScalar tempSqrt = SkScalarSqrt(9.f * d[1] * d[1] - 12.f * d[0] * d[2]);
582 SkScalar ls = 3.f * d[1] - tempSqrt;
583 SkScalar lt = 6.f * d[0];
584 SkScalar ms = 3.f * d[1] + tempSqrt;
585 SkScalar mt = 6.f * d[0];
586
587 k[0] = ls * ms;
588 k[1] = (3.f * ls * ms - ls * mt - lt * ms) / 3.f;
589 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f;
590 k[3] = (lt - ls) * (mt - ms);
591
592 l[0] = ls * ls * ls;
593 const SkScalar lt_ls = lt - ls;
594 l[1] = ls * ls * lt_ls * -1.f;
595 l[2] = lt_ls * lt_ls * ls;
596 l[3] = -1.f * lt_ls * lt_ls * lt_ls;
597
598 m[0] = ms * ms * ms;
599 const SkScalar mt_ms = mt - ms;
600 m[1] = ms * ms * mt_ms * -1.f;
601 m[2] = mt_ms * mt_ms * ms;
602 m[3] = -1.f * mt_ms * mt_ms * mt_ms;
603
604 // If d0 < 0 we need to flip the orientation of our curve
605 // This is done by negating the k and l values
606 // We want negative distance values to be on the inside
607 if ( d[0] > 0) {
608 for (int i = 0; i < 4; ++i) {
609 k[i] = -k[i];
610 l[i] = -l[i];
611 }
612 }
613}
614
615static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
616 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]);
617 SkScalar ls = d[1] - tempSqrt;
618 SkScalar lt = 2.f * d[0];
619 SkScalar ms = d[1] + tempSqrt;
620 SkScalar mt = 2.f * d[0];
621
622 k[0] = ls * ms;
623 k[1] = (3.f * ls*ms - ls * mt - lt * ms) / 3.f;
624 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f;
625 k[3] = (lt - ls) * (mt - ms);
626
627 l[0] = ls * ls * ms;
628 l[1] = (ls * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/-3.f;
629 l[2] = ((lt - ls) * (ls * (2.f * mt - 3.f * ms) + lt * ms))/3.f;
630 l[3] = -1.f * (lt - ls) * (lt - ls) * (mt - ms);
631
632 m[0] = ls * ms * ms;
633 m[1] = (ms * (ls * (2.f * mt - 3.f * ms) + lt * ms))/-3.f;
634 m[2] = ((mt - ms) * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/3.f;
635 m[3] = -1.f * (lt - ls) * (mt - ms) * (mt - ms);
636
637
638 // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0),
639 // we need to flip the orientation of our curve.
640 // This is done by negating the k and l values
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000641 if ( (d[0] < 0 && k[1] > 0) || (d[0] > 0 && k[1] < 0)) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000642 for (int i = 0; i < 4; ++i) {
643 k[i] = -k[i];
644 l[i] = -l[i];
645 }
646 }
647}
648
649static void set_cusp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
650 const SkScalar ls = d[2];
651 const SkScalar lt = 3.f * d[1];
652
653 k[0] = ls;
654 k[1] = ls - lt / 3.f;
655 k[2] = ls - 2.f * lt / 3.f;
656 k[3] = ls - lt;
657
658 l[0] = ls * ls * ls;
659 const SkScalar ls_lt = ls - lt;
660 l[1] = ls * ls * ls_lt;
661 l[2] = ls_lt * ls_lt * ls;
662 l[3] = ls_lt * ls_lt * ls_lt;
663
664 m[0] = 1.f;
665 m[1] = 1.f;
666 m[2] = 1.f;
667 m[3] = 1.f;
668}
669
670// For the case when a cubic is actually a quadratic
671// M =
672// 0 0 0
673// 1/3 0 1/3
674// 2/3 1/3 2/3
675// 1 1 1
676static void set_quadratic_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
677 k[0] = 0.f;
678 k[1] = 1.f/3.f;
679 k[2] = 2.f/3.f;
680 k[3] = 1.f;
681
682 l[0] = 0.f;
683 l[1] = 0.f;
684 l[2] = 1.f/3.f;
685 l[3] = 1.f;
686
687 m[0] = 0.f;
688 m[1] = 1.f/3.f;
689 m[2] = 2.f/3.f;
690 m[3] = 1.f;
691
692 // If d2 < 0 we need to flip the orientation of our curve
693 // This is done by negating the k and l values
694 if ( d[2] > 0) {
695 for (int i = 0; i < 4; ++i) {
696 k[i] = -k[i];
697 l[i] = -l[i];
698 }
699 }
700}
701
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000702int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkScalar klm[9],
703 SkScalar klm_rev[3]) {
704 // Variable to store the two parametric values at the loop double point
705 SkScalar smallS = 0.f;
706 SkScalar largeS = 0.f;
707
708 SkScalar d[3];
caryclark8dd31cf2014-12-12 09:11:23 -0800709 SkCubicType cType = SkClassifyCubic(src, d);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000710
711 int chop_count = 0;
caryclark8dd31cf2014-12-12 09:11:23 -0800712 if (kLoop_SkCubicType == cType) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000713 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]);
714 SkScalar ls = d[1] - tempSqrt;
715 SkScalar lt = 2.f * d[0];
716 SkScalar ms = d[1] + tempSqrt;
717 SkScalar mt = 2.f * d[0];
718 ls = ls / lt;
719 ms = ms / mt;
720 // need to have t values sorted since this is what is expected by SkChopCubicAt
721 if (ls <= ms) {
722 smallS = ls;
723 largeS = ms;
724 } else {
725 smallS = ms;
726 largeS = ls;
727 }
728
729 SkScalar chop_ts[2];
730 if (smallS > 0.f && smallS < 1.f) {
731 chop_ts[chop_count++] = smallS;
732 }
733 if (largeS > 0.f && largeS < 1.f) {
734 chop_ts[chop_count++] = largeS;
735 }
736 if(dst) {
737 SkChopCubicAt(src, dst, chop_ts, chop_count);
738 }
739 } else {
740 if (dst) {
741 memcpy(dst, src, sizeof(SkPoint) * 4);
742 }
743 }
744
745 if (klm && klm_rev) {
746 // Set klm_rev to to match the sub_section of cubic that needs to have its orientation
747 // flipped. This will always be the section that is the "loop"
748 if (2 == chop_count) {
749 klm_rev[0] = 1.f;
750 klm_rev[1] = -1.f;
751 klm_rev[2] = 1.f;
752 } else if (1 == chop_count) {
753 if (smallS < 0.f) {
754 klm_rev[0] = -1.f;
755 klm_rev[1] = 1.f;
756 } else {
757 klm_rev[0] = 1.f;
758 klm_rev[1] = -1.f;
759 }
760 } else {
761 if (smallS < 0.f && largeS > 1.f) {
762 klm_rev[0] = -1.f;
763 } else {
764 klm_rev[0] = 1.f;
765 }
766 }
767 SkScalar controlK[4];
768 SkScalar controlL[4];
769 SkScalar controlM[4];
770
caryclark8dd31cf2014-12-12 09:11:23 -0800771 if (kSerpentine_SkCubicType == cType || (kCusp_SkCubicType == cType && 0.f != d[0])) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000772 set_serp_klm(d, controlK, controlL, controlM);
caryclark8dd31cf2014-12-12 09:11:23 -0800773 } else if (kLoop_SkCubicType == cType) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000774 set_loop_klm(d, controlK, controlL, controlM);
caryclark8dd31cf2014-12-12 09:11:23 -0800775 } else if (kCusp_SkCubicType == cType) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000776 SkASSERT(0.f == d[0]);
777 set_cusp_klm(d, controlK, controlL, controlM);
caryclark8dd31cf2014-12-12 09:11:23 -0800778 } else if (kQuadratic_SkCubicType == cType) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000779 set_quadratic_klm(d, controlK, controlL, controlM);
780 }
781
782 calc_cubic_klm(src, controlK, controlL, controlM, klm, &klm[3], &klm[6]);
783 }
784 return chop_count + 1;
785}
786
787void GrPathUtils::getCubicKLM(const SkPoint p[4], SkScalar klm[9]) {
788 SkScalar d[3];
caryclark8dd31cf2014-12-12 09:11:23 -0800789 SkCubicType cType = SkClassifyCubic(p, d);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000790
791 SkScalar controlK[4];
792 SkScalar controlL[4];
793 SkScalar controlM[4];
794
caryclark8dd31cf2014-12-12 09:11:23 -0800795 if (kSerpentine_SkCubicType == cType || (kCusp_SkCubicType == cType && 0.f != d[0])) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000796 set_serp_klm(d, controlK, controlL, controlM);
caryclark8dd31cf2014-12-12 09:11:23 -0800797 } else if (kLoop_SkCubicType == cType) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000798 set_loop_klm(d, controlK, controlL, controlM);
caryclark8dd31cf2014-12-12 09:11:23 -0800799 } else if (kCusp_SkCubicType == cType) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000800 SkASSERT(0.f == d[0]);
801 set_cusp_klm(d, controlK, controlL, controlM);
caryclark8dd31cf2014-12-12 09:11:23 -0800802 } else if (kQuadratic_SkCubicType == cType) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000803 set_quadratic_klm(d, controlK, controlL, controlM);
804 }
805
806 calc_cubic_klm(p, controlK, controlL, controlM, klm, &klm[3], &klm[6]);
807}