blob: e2b1ac76343a74747cb75c5982a9ca480c22f933 [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
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000010#include "GrPoint.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
17 // stretch when mapping to screen coordinates.
bsalomon@google.com81712882012-11-01 17:12:34 +000018 SkScalar stretch = viewM.getMaxStretch();
19 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
39uint32_t GrPathUtils::quadraticPointCount(const GrPoint 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);
epoger@google.com2047f002011-05-17 17:36:59 +000054 int temp = SkScalarCeil(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 }
62 return GrMin(pow2, MAX_POINTS_PER_CURVE);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000063 }
64}
65
66uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0,
tomhudson@google.comc10a8882011-06-28 15:19:32 +000067 const GrPoint& p1,
68 const GrPoint& p2,
bsalomon@google.com81712882012-11-01 17:12:34 +000069 SkScalar tolSqd,
tomhudson@google.comc10a8882011-06-28 15:19:32 +000070 GrPoint** points,
71 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
79 GrPoint 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 };
bsalomon@google.com81712882012-11-01 17:12:34 +000083 GrPoint 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
91uint32_t GrPathUtils::cubicPointCount(const GrPoint 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
bsalomon@google.com81712882012-11-01 17:12:34 +000098 SkScalar d = GrMax(
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 {
epoger@google.com2047f002011-05-17 17:36:59 +0000105 int temp = SkScalarCeil(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 }
113 return GrMin(pow2, MAX_POINTS_PER_CURVE);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000114 }
115}
116
117uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0,
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000118 const GrPoint& p1,
119 const GrPoint& p2,
120 const GrPoint& p3,
bsalomon@google.com81712882012-11-01 17:12:34 +0000121 SkScalar tolSqd,
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000122 GrPoint** points,
123 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 }
131 GrPoint 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 };
136 GrPoint 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 };
bsalomon@google.com81712882012-11-01 17:12:34 +0000140 GrPoint 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
162 GrPoint 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;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000169 case SkPath::kQuad_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000170 pointCount += quadraticPointCount(pts, tol);
171 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000172 case SkPath::kCubic_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000173 pointCount += cubicPointCount(pts, tol);
174 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000175 case SkPath::kMove_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000176 pointCount += 1;
177 if (!first) {
178 ++(*subpaths);
179 }
180 break;
181 default:
182 break;
183 }
184 first = false;
185 }
186 return pointCount;
187}
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000188
bsalomon@google.com19713172012-03-15 13:51:08 +0000189void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) {
bsalomon@google.com5e9bf822012-01-17 14:39:21 +0000190#ifndef SK_SCALAR_IS_FLOAT
191 GrCrash("Expected scalar is float.");
192#endif
bsalomon@google.com19713172012-03-15 13:51:08 +0000193 SkMatrix m;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000194 // We want M such that M * xy_pt = uv_pt
195 // We know M * control_pts = [0 1/2 1]
196 // [0 0 1]
197 // [1 1 1]
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000198 // And control_pts = [x0 x1 x2]
199 // [y0 y1 y2]
200 // [1 1 1 ]
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000201 // We invert the control pt matrix and post concat to both sides to get M.
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000202 // Using the known form of the control point matrix and the result, we can
203 // optimize and improve precision.
204
205 double x0 = qPts[0].fX;
206 double y0 = qPts[0].fY;
207 double x1 = qPts[1].fX;
208 double y1 = qPts[1].fY;
209 double x2 = qPts[2].fX;
210 double y2 = qPts[2].fY;
211 double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2;
212
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000213 if (!sk_float_isfinite(det)
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000214 || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) {
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000215 // The quad is degenerate. Hopefully this is rare. Find the pts that are
216 // farthest apart to compute a line (unless it is really a pt).
217 SkScalar maxD = qPts[0].distanceToSqd(qPts[1]);
218 int maxEdge = 0;
219 SkScalar d = qPts[1].distanceToSqd(qPts[2]);
220 if (d > maxD) {
221 maxD = d;
222 maxEdge = 1;
223 }
224 d = qPts[2].distanceToSqd(qPts[0]);
225 if (d > maxD) {
226 maxD = d;
227 maxEdge = 2;
228 }
229 // We could have a tolerance here, not sure if it would improve anything
230 if (maxD > 0) {
231 // Set the matrix to give (u = 0, v = distance_to_line)
bsalomon@google.com20e542e2012-02-15 18:49:41 +0000232 GrVec lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge];
233 // when looking from the point 0 down the line we want positive
234 // distances to be to the left. This matches the non-degenerate
235 // case.
236 lineVec.setOrthog(lineVec, GrPoint::kLeft_Side);
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000237 lineVec.dot(qPts[0]);
bsalomon@google.com19713172012-03-15 13:51:08 +0000238 // first row
239 fM[0] = 0;
240 fM[1] = 0;
241 fM[2] = 0;
242 // second row
243 fM[3] = lineVec.fX;
244 fM[4] = lineVec.fY;
245 fM[5] = -lineVec.dot(qPts[maxEdge]);
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000246 } else {
247 // It's a point. It should cover zero area. Just set the matrix such
248 // that (u, v) will always be far away from the quad.
bsalomon@google.com19713172012-03-15 13:51:08 +0000249 fM[0] = 0; fM[1] = 0; fM[2] = 100.f;
250 fM[3] = 0; fM[4] = 0; fM[5] = 100.f;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000251 }
252 } else {
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000253 double scale = 1.0/det;
254
255 // compute adjugate matrix
256 double a0, a1, a2, a3, a4, a5, a6, a7, a8;
257 a0 = y1-y2;
258 a1 = x2-x1;
259 a2 = x1*y2-x2*y1;
260
261 a3 = y2-y0;
262 a4 = x0-x2;
263 a5 = x2*y0-x0*y2;
264
265 a6 = y0-y1;
266 a7 = x1-x0;
267 a8 = x0*y1-x1*y0;
268
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000269 // this performs the uv_pts*adjugate(control_pts) multiply,
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000270 // then does the scale by 1/det afterwards to improve precision
271 m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale);
272 m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale);
273 m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale);
274
275 m[SkMatrix::kMSkewY] = (float)(a6*scale);
276 m[SkMatrix::kMScaleY] = (float)(a7*scale);
277 m[SkMatrix::kMTransY] = (float)(a8*scale);
278
279 m[SkMatrix::kMPersp0] = (float)((a0 + a3 + a6)*scale);
280 m[SkMatrix::kMPersp1] = (float)((a1 + a4 + a7)*scale);
281 m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale);
bsalomon@google.com19713172012-03-15 13:51:08 +0000282
283 // The matrix should not have perspective.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000284 SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000285 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL);
286 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL);
bsalomon@google.com19713172012-03-15 13:51:08 +0000287
288 // It may not be normalized to have 1.0 in the bottom right
289 float m33 = m.get(SkMatrix::kMPersp2);
290 if (1.f != m33) {
291 m33 = 1.f / m33;
292 fM[0] = m33 * m.get(SkMatrix::kMScaleX);
293 fM[1] = m33 * m.get(SkMatrix::kMSkewX);
294 fM[2] = m33 * m.get(SkMatrix::kMTransX);
295 fM[3] = m33 * m.get(SkMatrix::kMSkewY);
296 fM[4] = m33 * m.get(SkMatrix::kMScaleY);
297 fM[5] = m33 * m.get(SkMatrix::kMTransY);
298 } else {
299 fM[0] = m.get(SkMatrix::kMScaleX);
300 fM[1] = m.get(SkMatrix::kMSkewX);
301 fM[2] = m.get(SkMatrix::kMTransX);
302 fM[3] = m.get(SkMatrix::kMSkewY);
303 fM[4] = m.get(SkMatrix::kMScaleY);
304 fM[5] = m.get(SkMatrix::kMTransY);
305 }
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000306 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000307}
308
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000309////////////////////////////////////////////////////////////////////////////////
310
311// k = (y2 - y0, x0 - x2, (x2 - x0)*y0 - (y2 - y0)*x0 )
312// l = (2*w * (y1 - y0), 2*w * (x0 - x1), 2*w * (x1*y0 - x0*y1))
313// m = (2*w * (y2 - y1), 2*w * (x1 - x2), 2*w * (x2*y1 - x1*y2))
314void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkScalar klm[9]) {
315 const SkScalar w2 = 2.f * weight;
316 klm[0] = p[2].fY - p[0].fY;
317 klm[1] = p[0].fX - p[2].fX;
318 klm[2] = (p[2].fX - p[0].fX) * p[0].fY - (p[2].fY - p[0].fY) * p[0].fX;
319
320 klm[3] = w2 * (p[1].fY - p[0].fY);
321 klm[4] = w2 * (p[0].fX - p[1].fX);
322 klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY);
323
324 klm[6] = w2 * (p[2].fY - p[1].fY);
325 klm[7] = w2 * (p[1].fX - p[2].fX);
326 klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY);
327
328 // scale the max absolute value of coeffs to 10
329 SkScalar scale = 0.f;
330 for (int i = 0; i < 9; ++i) {
331 scale = SkMaxScalar(scale, SkScalarAbs(klm[i]));
332 }
333 SkASSERT(scale > 0.f);
334 scale = 10.f / scale;
335 for (int i = 0; i < 9; ++i) {
336 klm[i] *= scale;
337 }
338}
339
340////////////////////////////////////////////////////////////////////////////////
341
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000342namespace {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000343
344// a is the first control point of the cubic.
345// ab is the vector from a to the second control point.
346// dc is the vector from the fourth to the third control point.
347// d is the fourth control point.
348// p is the candidate quadratic control point.
349// this assumes that the cubic doesn't inflect and is simple
350bool is_point_within_cubic_tangents(const SkPoint& a,
351 const SkVector& ab,
352 const SkVector& dc,
353 const SkPoint& d,
354 SkPath::Direction dir,
355 const SkPoint p) {
356 SkVector ap = p - a;
357 SkScalar apXab = ap.cross(ab);
358 if (SkPath::kCW_Direction == dir) {
359 if (apXab > 0) {
360 return false;
361 }
362 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000363 SkASSERT(SkPath::kCCW_Direction == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000364 if (apXab < 0) {
365 return false;
366 }
367 }
368
369 SkVector dp = p - d;
370 SkScalar dpXdc = dp.cross(dc);
371 if (SkPath::kCW_Direction == dir) {
372 if (dpXdc < 0) {
373 return false;
374 }
375 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000376 SkASSERT(SkPath::kCCW_Direction == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000377 if (dpXdc > 0) {
378 return false;
379 }
380 }
381 return true;
382}
383
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000384void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000385 SkScalar toleranceSqd,
386 bool constrainWithinTangents,
387 SkPath::Direction dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000388 SkTArray<SkPoint, true>* quads,
389 int sublevel = 0) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000390
391 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is
392 // 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].
393
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000394 SkVector ab = p[1] - p[0];
395 SkVector dc = p[2] - p[3];
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000396
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000397 if (ab.isZero()) {
398 if (dc.isZero()) {
399 SkPoint* degQuad = quads->push_back_n(3);
400 degQuad[0] = p[0];
401 degQuad[1] = p[0];
402 degQuad[2] = p[3];
403 return;
404 }
405 ab = p[2] - p[0];
406 }
407 if (dc.isZero()) {
408 dc = p[1] - p[3];
409 }
410
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000411 // When the ab and cd tangents are nearly parallel with vector from d to a the constraint that
412 // the quad point falls between the tangents becomes hard to enforce and we are likely to hit
413 // the max subdivision count. However, in this case the cubic is approaching a line and the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000414 // accuracy of the quad point isn't so important. We check if the two middle cubic control
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000415 // points are very close to the baseline vector. If so then we just pick quadratic points on the
416 // control polygon.
417
418 if (constrainWithinTangents) {
419 SkVector da = p[0] - p[3];
420 SkScalar invDALengthSqd = da.lengthSqd();
421 if (invDALengthSqd > SK_ScalarNearlyZero) {
422 invDALengthSqd = SkScalarInvert(invDALengthSqd);
423 // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a.
424 // same goed for point c using vector cd.
425 SkScalar detABSqd = ab.cross(da);
426 detABSqd = SkScalarSquare(detABSqd);
427 SkScalar detDCSqd = dc.cross(da);
428 detDCSqd = SkScalarSquare(detDCSqd);
429 if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd &&
430 SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) {
431 SkPoint b = p[0] + ab;
432 SkPoint c = p[3] + dc;
433 SkPoint mid = b + c;
434 mid.scale(SK_ScalarHalf);
435 // Insert two quadratics to cover the case when ab points away from d and/or dc
436 // points away from a.
437 if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) {
438 SkPoint* qpts = quads->push_back_n(6);
439 qpts[0] = p[0];
440 qpts[1] = b;
441 qpts[2] = mid;
442 qpts[3] = mid;
443 qpts[4] = c;
444 qpts[5] = p[3];
445 } else {
446 SkPoint* qpts = quads->push_back_n(3);
447 qpts[0] = p[0];
448 qpts[1] = mid;
449 qpts[2] = p[3];
450 }
451 return;
452 }
453 }
454 }
455
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000456 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000457 static const int kMaxSubdivs = 10;
458
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000459 ab.scale(kLengthScale);
460 dc.scale(kLengthScale);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000461
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000462 // e0 and e1 are extrapolations along vectors ab and dc.
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000463 SkVector c0 = p[0];
464 c0 += ab;
465 SkVector c1 = p[3];
466 c1 += dc;
467
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000468 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000469 if (dSqd < toleranceSqd) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000470 SkPoint cAvg = c0;
471 cAvg += c1;
472 cAvg.scale(SK_ScalarHalf);
473
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000474 bool subdivide = false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000475
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000476 if (constrainWithinTangents &&
477 !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000478 // choose a new cAvg that is the intersection of the two tangent lines.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000479 ab.setOrthog(ab);
480 SkScalar z0 = -ab.dot(p[0]);
481 dc.setOrthog(dc);
482 SkScalar z1 = -dc.dot(p[3]);
483 cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY);
484 cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1);
485 SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX);
486 z = SkScalarInvert(z);
487 cAvg.fX *= z;
488 cAvg.fY *= z;
489 if (sublevel <= kMaxSubdivs) {
490 SkScalar d0Sqd = c0.distanceToSqd(cAvg);
491 SkScalar d1Sqd = c1.distanceToSqd(cAvg);
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000492 // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know
493 // the distances and tolerance can't be negative.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000494 // (d0 + d1)^2 > toleranceSqd
495 // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd
496 SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd));
497 subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd;
498 }
499 }
500 if (!subdivide) {
501 SkPoint* pts = quads->push_back_n(3);
502 pts[0] = p[0];
503 pts[1] = cAvg;
504 pts[2] = p[3];
505 return;
506 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000507 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000508 SkPoint choppedPts[7];
509 SkChopCubicAtHalf(p, choppedPts);
510 convert_noninflect_cubic_to_quads(choppedPts + 0,
511 toleranceSqd,
512 constrainWithinTangents,
513 dir,
514 quads,
515 sublevel + 1);
516 convert_noninflect_cubic_to_quads(choppedPts + 3,
517 toleranceSqd,
518 constrainWithinTangents,
519 dir,
520 quads,
521 sublevel + 1);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000522}
523}
524
525void GrPathUtils::convertCubicToQuads(const GrPoint p[4],
526 SkScalar tolScale,
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000527 bool constrainWithinTangents,
528 SkPath::Direction dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000529 SkTArray<SkPoint, true>* quads) {
530 SkPoint chopped[10];
531 int count = SkChopCubicAtInflections(p, chopped);
532
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000533 // base tolerance is 1 pixel.
534 static const SkScalar kTolerance = SK_Scalar1;
535 const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance));
536
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000537 for (int i = 0; i < count; ++i) {
538 SkPoint* cubic = chopped + 3*i;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000539 convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000540 }
541
542}
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000543
544////////////////////////////////////////////////////////////////////////////////
545
546enum CubicType {
547 kSerpentine_CubicType,
548 kCusp_CubicType,
549 kLoop_CubicType,
550 kQuadratic_CubicType,
551 kLine_CubicType,
552 kPoint_CubicType
553};
554
555// discr(I) = d0^2 * (3*d1^2 - 4*d0*d2)
556// Classification:
557// discr(I) > 0 Serpentine
558// discr(I) = 0 Cusp
559// discr(I) < 0 Loop
560// d0 = d1 = 0 Quadratic
561// d0 = d1 = d2 = 0 Line
562// p0 = p1 = p2 = p3 Point
563static CubicType classify_cubic(const SkPoint p[4], const SkScalar d[3]) {
564 if (p[0] == p[1] && p[0] == p[2] && p[0] == p[3]) {
565 return kPoint_CubicType;
566 }
567 const SkScalar discr = d[0] * d[0] * (3.f * d[1] * d[1] - 4.f * d[0] * d[2]);
568 if (discr > SK_ScalarNearlyZero) {
569 return kSerpentine_CubicType;
570 } else if (discr < -SK_ScalarNearlyZero) {
571 return kLoop_CubicType;
572 } else {
573 if (0.f == d[0] && 0.f == d[1]) {
574 return (0.f == d[2] ? kLine_CubicType : kQuadratic_CubicType);
575 } else {
576 return kCusp_CubicType;
577 }
578 }
579}
580
581// Assumes the third component of points is 1.
582// Calcs p0 . (p1 x p2)
583static SkScalar calc_dot_cross_cubic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
584 const SkScalar xComp = p0.fX * (p1.fY - p2.fY);
585 const SkScalar yComp = p0.fY * (p2.fX - p1.fX);
586 const SkScalar wComp = p1.fX * p2.fY - p1.fY * p2.fX;
587 return (xComp + yComp + wComp);
588}
589
590// Solves linear system to extract klm
591// P.K = k (similarly for l, m)
592// Where P is matrix of control points
593// K is coefficients for the line K
594// k is vector of values of K evaluated at the control points
595// Solving for K, thus K = P^(-1) . k
596static void calc_cubic_klm(const SkPoint p[4], const SkScalar controlK[4],
597 const SkScalar controlL[4], const SkScalar controlM[4],
598 SkScalar k[3], SkScalar l[3], SkScalar m[3]) {
599 SkMatrix matrix;
600 matrix.setAll(p[0].fX, p[0].fY, 1.f,
601 p[1].fX, p[1].fY, 1.f,
602 p[2].fX, p[2].fY, 1.f);
603 SkMatrix inverse;
604 if (matrix.invert(&inverse)) {
605 inverse.mapHomogeneousPoints(k, controlK, 1);
606 inverse.mapHomogeneousPoints(l, controlL, 1);
607 inverse.mapHomogeneousPoints(m, controlM, 1);
608 }
609
610}
611
612static void set_serp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
613 SkScalar tempSqrt = SkScalarSqrt(9.f * d[1] * d[1] - 12.f * d[0] * d[2]);
614 SkScalar ls = 3.f * d[1] - tempSqrt;
615 SkScalar lt = 6.f * d[0];
616 SkScalar ms = 3.f * d[1] + tempSqrt;
617 SkScalar mt = 6.f * d[0];
618
619 k[0] = ls * ms;
620 k[1] = (3.f * ls * ms - ls * mt - lt * ms) / 3.f;
621 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f;
622 k[3] = (lt - ls) * (mt - ms);
623
624 l[0] = ls * ls * ls;
625 const SkScalar lt_ls = lt - ls;
626 l[1] = ls * ls * lt_ls * -1.f;
627 l[2] = lt_ls * lt_ls * ls;
628 l[3] = -1.f * lt_ls * lt_ls * lt_ls;
629
630 m[0] = ms * ms * ms;
631 const SkScalar mt_ms = mt - ms;
632 m[1] = ms * ms * mt_ms * -1.f;
633 m[2] = mt_ms * mt_ms * ms;
634 m[3] = -1.f * mt_ms * mt_ms * mt_ms;
635
636 // If d0 < 0 we need to flip the orientation of our curve
637 // This is done by negating the k and l values
638 // We want negative distance values to be on the inside
639 if ( d[0] > 0) {
640 for (int i = 0; i < 4; ++i) {
641 k[i] = -k[i];
642 l[i] = -l[i];
643 }
644 }
645}
646
647static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
648 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]);
649 SkScalar ls = d[1] - tempSqrt;
650 SkScalar lt = 2.f * d[0];
651 SkScalar ms = d[1] + tempSqrt;
652 SkScalar mt = 2.f * d[0];
653
654 k[0] = ls * ms;
655 k[1] = (3.f * ls*ms - ls * mt - lt * ms) / 3.f;
656 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f;
657 k[3] = (lt - ls) * (mt - ms);
658
659 l[0] = ls * ls * ms;
660 l[1] = (ls * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/-3.f;
661 l[2] = ((lt - ls) * (ls * (2.f * mt - 3.f * ms) + lt * ms))/3.f;
662 l[3] = -1.f * (lt - ls) * (lt - ls) * (mt - ms);
663
664 m[0] = ls * ms * ms;
665 m[1] = (ms * (ls * (2.f * mt - 3.f * ms) + lt * ms))/-3.f;
666 m[2] = ((mt - ms) * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/3.f;
667 m[3] = -1.f * (lt - ls) * (mt - ms) * (mt - ms);
668
669
670 // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0),
671 // we need to flip the orientation of our curve.
672 // This is done by negating the k and l values
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +0000673 if ( (d[0] < 0 && k[1] > 0) || (d[0] > 0 && k[1] < 0)) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000674 for (int i = 0; i < 4; ++i) {
675 k[i] = -k[i];
676 l[i] = -l[i];
677 }
678 }
679}
680
681static void set_cusp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
682 const SkScalar ls = d[2];
683 const SkScalar lt = 3.f * d[1];
684
685 k[0] = ls;
686 k[1] = ls - lt / 3.f;
687 k[2] = ls - 2.f * lt / 3.f;
688 k[3] = ls - lt;
689
690 l[0] = ls * ls * ls;
691 const SkScalar ls_lt = ls - lt;
692 l[1] = ls * ls * ls_lt;
693 l[2] = ls_lt * ls_lt * ls;
694 l[3] = ls_lt * ls_lt * ls_lt;
695
696 m[0] = 1.f;
697 m[1] = 1.f;
698 m[2] = 1.f;
699 m[3] = 1.f;
700}
701
702// For the case when a cubic is actually a quadratic
703// M =
704// 0 0 0
705// 1/3 0 1/3
706// 2/3 1/3 2/3
707// 1 1 1
708static void set_quadratic_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
709 k[0] = 0.f;
710 k[1] = 1.f/3.f;
711 k[2] = 2.f/3.f;
712 k[3] = 1.f;
713
714 l[0] = 0.f;
715 l[1] = 0.f;
716 l[2] = 1.f/3.f;
717 l[3] = 1.f;
718
719 m[0] = 0.f;
720 m[1] = 1.f/3.f;
721 m[2] = 2.f/3.f;
722 m[3] = 1.f;
723
724 // If d2 < 0 we need to flip the orientation of our curve
725 // This is done by negating the k and l values
726 if ( d[2] > 0) {
727 for (int i = 0; i < 4; ++i) {
728 k[i] = -k[i];
729 l[i] = -l[i];
730 }
731 }
732}
733
734// Calc coefficients of I(s,t) where roots of I are inflection points of curve
735// I(s,t) = t*(3*d0*s^2 - 3*d1*s*t + d2*t^2)
736// d0 = a1 - 2*a2+3*a3
737// d1 = -a2 + 3*a3
738// d2 = 3*a3
739// a1 = p0 . (p3 x p2)
740// a2 = p1 . (p0 x p3)
741// a3 = p2 . (p1 x p0)
742// Places the values of d1, d2, d3 in array d passed in
743static void calc_cubic_inflection_func(const SkPoint p[4], SkScalar d[3]) {
744 SkScalar a1 = calc_dot_cross_cubic(p[0], p[3], p[2]);
745 SkScalar a2 = calc_dot_cross_cubic(p[1], p[0], p[3]);
746 SkScalar a3 = calc_dot_cross_cubic(p[2], p[1], p[0]);
747
748 // need to scale a's or values in later calculations will grow to high
749 SkScalar max = SkScalarAbs(a1);
750 max = SkMaxScalar(max, SkScalarAbs(a2));
751 max = SkMaxScalar(max, SkScalarAbs(a3));
752 max = 1.f/max;
753 a1 = a1 * max;
754 a2 = a2 * max;
755 a3 = a3 * max;
756
757 d[2] = 3.f * a3;
758 d[1] = d[2] - a2;
759 d[0] = d[1] - a2 + a1;
760}
761
762int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkScalar klm[9],
763 SkScalar klm_rev[3]) {
764 // Variable to store the two parametric values at the loop double point
765 SkScalar smallS = 0.f;
766 SkScalar largeS = 0.f;
767
768 SkScalar d[3];
769 calc_cubic_inflection_func(src, d);
770
771 CubicType cType = classify_cubic(src, d);
772
773 int chop_count = 0;
774 if (kLoop_CubicType == cType) {
775 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]);
776 SkScalar ls = d[1] - tempSqrt;
777 SkScalar lt = 2.f * d[0];
778 SkScalar ms = d[1] + tempSqrt;
779 SkScalar mt = 2.f * d[0];
780 ls = ls / lt;
781 ms = ms / mt;
782 // need to have t values sorted since this is what is expected by SkChopCubicAt
783 if (ls <= ms) {
784 smallS = ls;
785 largeS = ms;
786 } else {
787 smallS = ms;
788 largeS = ls;
789 }
790
791 SkScalar chop_ts[2];
792 if (smallS > 0.f && smallS < 1.f) {
793 chop_ts[chop_count++] = smallS;
794 }
795 if (largeS > 0.f && largeS < 1.f) {
796 chop_ts[chop_count++] = largeS;
797 }
798 if(dst) {
799 SkChopCubicAt(src, dst, chop_ts, chop_count);
800 }
801 } else {
802 if (dst) {
803 memcpy(dst, src, sizeof(SkPoint) * 4);
804 }
805 }
806
807 if (klm && klm_rev) {
808 // Set klm_rev to to match the sub_section of cubic that needs to have its orientation
809 // flipped. This will always be the section that is the "loop"
810 if (2 == chop_count) {
811 klm_rev[0] = 1.f;
812 klm_rev[1] = -1.f;
813 klm_rev[2] = 1.f;
814 } else if (1 == chop_count) {
815 if (smallS < 0.f) {
816 klm_rev[0] = -1.f;
817 klm_rev[1] = 1.f;
818 } else {
819 klm_rev[0] = 1.f;
820 klm_rev[1] = -1.f;
821 }
822 } else {
823 if (smallS < 0.f && largeS > 1.f) {
824 klm_rev[0] = -1.f;
825 } else {
826 klm_rev[0] = 1.f;
827 }
828 }
829 SkScalar controlK[4];
830 SkScalar controlL[4];
831 SkScalar controlM[4];
832
833 if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) {
834 set_serp_klm(d, controlK, controlL, controlM);
835 } else if (kLoop_CubicType == cType) {
836 set_loop_klm(d, controlK, controlL, controlM);
837 } else if (kCusp_CubicType == cType) {
838 SkASSERT(0.f == d[0]);
839 set_cusp_klm(d, controlK, controlL, controlM);
840 } else if (kQuadratic_CubicType == cType) {
841 set_quadratic_klm(d, controlK, controlL, controlM);
842 }
843
844 calc_cubic_klm(src, controlK, controlL, controlM, klm, &klm[3], &klm[6]);
845 }
846 return chop_count + 1;
847}
848
849void GrPathUtils::getCubicKLM(const SkPoint p[4], SkScalar klm[9]) {
850 SkScalar d[3];
851 calc_cubic_inflection_func(p, d);
852
853 CubicType cType = classify_cubic(p, d);
854
855 SkScalar controlK[4];
856 SkScalar controlL[4];
857 SkScalar controlM[4];
858
859 if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) {
860 set_serp_klm(d, controlK, controlL, controlM);
861 } else if (kLoop_CubicType == cType) {
862 set_loop_klm(d, controlK, controlL, controlM);
863 } else if (kCusp_CubicType == cType) {
864 SkASSERT(0.f == d[0]);
865 set_cusp_klm(d, controlK, controlL, controlM);
866 } else if (kQuadratic_CubicType == cType) {
867 set_quadratic_klm(d, controlK, controlL, controlM);
868 }
869
870 calc_cubic_klm(p, controlK, controlL, controlM, klm, &klm[3], &klm[6]);
871}