blob: 8e0a9fc0ccf5506ed07c13ec7b005e5cfe6fd851 [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"
halcanary4dbbd042016-06-07 17:21:10 -070012#include "SkMathPriv.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000013
bsalomon@google.com81712882012-11-01 17:12:34 +000014SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000015 const SkMatrix& viewM,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000016 const SkRect& pathBounds) {
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000017 // 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 +000018 // scale when mapping to screen coordinates.
19 SkScalar stretch = viewM.getMaxScale();
bsalomon@google.com81712882012-11-01 17:12:34 +000020 SkScalar srcTol = devTol;
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000021
22 if (stretch < 0) {
bsalomon@google.com38396322011-09-09 19:32:04 +000023 // take worst case mapRadius amoung four corners.
24 // (less than perfect)
25 for (int i = 0; i < 4; ++i) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000026 SkMatrix mat;
bsalomon@google.com38396322011-09-09 19:32:04 +000027 mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight,
28 (i < 2) ? pathBounds.fTop : pathBounds.fBottom);
29 mat.postConcat(viewM);
30 stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1));
31 }
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000032 }
reed80ea19c2015-05-12 10:37:34 -070033 return srcTol / stretch;
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000034}
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
Robert Phillipsb712a852017-04-18 16:56:06 -040039uint32_t GrPathUtils::quadraticPointCount(const SkPoint points[], SkScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +000040 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +000041 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +000042 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000043 SkASSERT(tol > 0);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000044
bsalomon@google.com81712882012-11-01 17:12:34 +000045 SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]);
senorblancob6a40b82016-08-19 08:07:22 -070046 if (!SkScalarIsFinite(d)) {
47 return MAX_POINTS_PER_CURVE;
48 } else if (d <= tol) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000049 return 1;
50 } else {
51 // Each time we subdivide, d should be cut in 4. So we need to
52 // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x)
53 // points.
54 // 2^(log4(x)) = sqrt(x);
reed80ea19c2015-05-12 10:37:34 -070055 SkScalar divSqrt = SkScalarSqrt(d / tol);
egdaniel5a23a142015-02-25 06:41:47 -080056 if (((SkScalar)SK_MaxS32) <= divSqrt) {
57 return MAX_POINTS_PER_CURVE;
58 } else {
59 int temp = SkScalarCeilToInt(divSqrt);
60 int pow2 = GrNextPow2(temp);
61 // Because of NaNs & INFs we can wind up with a degenerate temp
62 // such that pow2 comes out negative. Also, our point generator
63 // will always output at least one pt.
64 if (pow2 < 1) {
65 pow2 = 1;
66 }
67 return SkTMin(pow2, MAX_POINTS_PER_CURVE);
bsalomon@google.com61f3bde2011-06-17 20:06:49 +000068 }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000069 }
70}
71
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000072uint32_t GrPathUtils::generateQuadraticPoints(const SkPoint& p0,
73 const SkPoint& p1,
74 const SkPoint& p2,
bsalomon@google.com81712882012-11-01 17:12:34 +000075 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000076 SkPoint** points,
tomhudson@google.comc10a8882011-06-28 15:19:32 +000077 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000078 if (pointsLeft < 2 ||
79 (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) {
80 (*points)[0] = p2;
81 *points += 1;
82 return 1;
83 }
84
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000085 SkPoint q[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000086 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
87 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000088 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000089 SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000090
91 pointsLeft >>= 1;
92 uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft);
93 uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft);
94 return a + b;
95}
96
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000097uint32_t GrPathUtils::cubicPointCount(const SkPoint points[],
bsalomon@google.com81712882012-11-01 17:12:34 +000098 SkScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +000099 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +0000100 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000101 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(tol > 0);
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000103
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000104 SkScalar d = SkTMax(
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000105 points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]),
106 points[2].distanceToLineSegmentBetweenSqd(points[0], points[3]));
epoger@google.com2047f002011-05-17 17:36:59 +0000107 d = SkScalarSqrt(d);
senorblancob6a40b82016-08-19 08:07:22 -0700108 if (!SkScalarIsFinite(d)) {
109 return MAX_POINTS_PER_CURVE;
110 } else if (d <= tol) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000111 return 1;
112 } else {
reed80ea19c2015-05-12 10:37:34 -0700113 SkScalar divSqrt = SkScalarSqrt(d / tol);
egdaniel5a23a142015-02-25 06:41:47 -0800114 if (((SkScalar)SK_MaxS32) <= divSqrt) {
115 return MAX_POINTS_PER_CURVE;
116 } else {
reed80ea19c2015-05-12 10:37:34 -0700117 int temp = SkScalarCeilToInt(SkScalarSqrt(d / tol));
egdaniel5a23a142015-02-25 06:41:47 -0800118 int pow2 = GrNextPow2(temp);
119 // Because of NaNs & INFs we can wind up with a degenerate temp
120 // such that pow2 comes out negative. Also, our point generator
121 // will always output at least one pt.
122 if (pow2 < 1) {
123 pow2 = 1;
124 }
125 return SkTMin(pow2, MAX_POINTS_PER_CURVE);
bsalomon@google.com61f3bde2011-06-17 20:06:49 +0000126 }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000127 }
128}
129
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000130uint32_t GrPathUtils::generateCubicPoints(const SkPoint& p0,
131 const SkPoint& p1,
132 const SkPoint& p2,
133 const SkPoint& p3,
bsalomon@google.com81712882012-11-01 17:12:34 +0000134 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000135 SkPoint** points,
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000136 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000137 if (pointsLeft < 2 ||
138 (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd &&
139 p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) {
robertphillipsf08ce6c2015-12-08 05:19:12 -0800140 (*points)[0] = p3;
141 *points += 1;
142 return 1;
143 }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000144 SkPoint q[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000145 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
146 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
147 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000148 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000149 SkPoint r[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000150 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
151 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000152 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000153 SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000154 pointsLeft >>= 1;
155 uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
156 uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
157 return a + b;
158}
159
Robert Phillipsb712a852017-04-18 16:56:06 -0400160int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, SkScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000161 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +0000162 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000163 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000164 SkASSERT(tol > 0);
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000165
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000166 int pointCount = 0;
167 *subpaths = 1;
168
169 bool first = true;
170
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000171 SkPath::Iter iter(path, false);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000172 SkPath::Verb verb;
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000173
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000174 SkPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000175 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000176
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000177 switch (verb) {
178 case SkPath::kLine_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000179 pointCount += 1;
180 break;
egdanielaf18a092015-01-05 10:22:28 -0800181 case SkPath::kConic_Verb: {
182 SkScalar weight = iter.conicWeight();
183 SkAutoConicToQuads converter;
Robert Phillipsb712a852017-04-18 16:56:06 -0400184 const SkPoint* quadPts = converter.computeQuads(pts, weight, tol);
egdanielaf18a092015-01-05 10:22:28 -0800185 for (int i = 0; i < converter.countQuads(); ++i) {
186 pointCount += quadraticPointCount(quadPts + 2*i, tol);
187 }
188 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000189 case SkPath::kQuad_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000190 pointCount += quadraticPointCount(pts, tol);
191 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000192 case SkPath::kCubic_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000193 pointCount += cubicPointCount(pts, tol);
194 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000195 case SkPath::kMove_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000196 pointCount += 1;
197 if (!first) {
198 ++(*subpaths);
199 }
200 break;
201 default:
202 break;
203 }
204 first = false;
205 }
206 return pointCount;
207}
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000208
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000209void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) {
bsalomon@google.com19713172012-03-15 13:51:08 +0000210 SkMatrix m;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000211 // We want M such that M * xy_pt = uv_pt
212 // We know M * control_pts = [0 1/2 1]
213 // [0 0 1]
214 // [1 1 1]
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000215 // And control_pts = [x0 x1 x2]
216 // [y0 y1 y2]
217 // [1 1 1 ]
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000218 // We invert the control pt matrix and post concat to both sides to get M.
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000219 // Using the known form of the control point matrix and the result, we can
220 // optimize and improve precision.
221
222 double x0 = qPts[0].fX;
223 double y0 = qPts[0].fY;
224 double x1 = qPts[1].fX;
225 double y1 = qPts[1].fY;
226 double x2 = qPts[2].fX;
227 double y2 = qPts[2].fY;
228 double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2;
229
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000230 if (!sk_float_isfinite(det)
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000231 || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) {
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000232 // The quad is degenerate. Hopefully this is rare. Find the pts that are
233 // farthest apart to compute a line (unless it is really a pt).
234 SkScalar maxD = qPts[0].distanceToSqd(qPts[1]);
235 int maxEdge = 0;
236 SkScalar d = qPts[1].distanceToSqd(qPts[2]);
237 if (d > maxD) {
238 maxD = d;
239 maxEdge = 1;
240 }
241 d = qPts[2].distanceToSqd(qPts[0]);
242 if (d > maxD) {
243 maxD = d;
244 maxEdge = 2;
245 }
246 // We could have a tolerance here, not sure if it would improve anything
247 if (maxD > 0) {
248 // Set the matrix to give (u = 0, v = distance_to_line)
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000249 SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge];
bsalomon@google.com20e542e2012-02-15 18:49:41 +0000250 // when looking from the point 0 down the line we want positive
251 // distances to be to the left. This matches the non-degenerate
252 // case.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000253 lineVec.setOrthog(lineVec, SkPoint::kLeft_Side);
bsalomon@google.com19713172012-03-15 13:51:08 +0000254 // first row
255 fM[0] = 0;
256 fM[1] = 0;
257 fM[2] = 0;
258 // second row
259 fM[3] = lineVec.fX;
260 fM[4] = lineVec.fY;
261 fM[5] = -lineVec.dot(qPts[maxEdge]);
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000262 } else {
263 // It's a point. It should cover zero area. Just set the matrix such
264 // that (u, v) will always be far away from the quad.
bsalomon@google.com19713172012-03-15 13:51:08 +0000265 fM[0] = 0; fM[1] = 0; fM[2] = 100.f;
266 fM[3] = 0; fM[4] = 0; fM[5] = 100.f;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000267 }
268 } else {
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000269 double scale = 1.0/det;
270
271 // compute adjugate matrix
robertphillips87a22342016-03-01 14:49:44 -0800272 double a2, a3, a4, a5, a6, a7, a8;
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000273 a2 = x1*y2-x2*y1;
274
275 a3 = y2-y0;
276 a4 = x0-x2;
277 a5 = x2*y0-x0*y2;
278
279 a6 = y0-y1;
280 a7 = x1-x0;
281 a8 = x0*y1-x1*y0;
282
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000283 // this performs the uv_pts*adjugate(control_pts) multiply,
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000284 // then does the scale by 1/det afterwards to improve precision
285 m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale);
286 m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale);
287 m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale);
288
289 m[SkMatrix::kMSkewY] = (float)(a6*scale);
290 m[SkMatrix::kMScaleY] = (float)(a7*scale);
291 m[SkMatrix::kMTransY] = (float)(a8*scale);
292
robertphillips87a22342016-03-01 14:49:44 -0800293 // kMPersp0 & kMPersp1 should algebraically be zero
294 m[SkMatrix::kMPersp0] = 0.0f;
295 m[SkMatrix::kMPersp1] = 0.0f;
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000296 m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale);
bsalomon@google.com19713172012-03-15 13:51:08 +0000297
bsalomon@google.com19713172012-03-15 13:51:08 +0000298 // It may not be normalized to have 1.0 in the bottom right
299 float m33 = m.get(SkMatrix::kMPersp2);
300 if (1.f != m33) {
301 m33 = 1.f / m33;
302 fM[0] = m33 * m.get(SkMatrix::kMScaleX);
303 fM[1] = m33 * m.get(SkMatrix::kMSkewX);
304 fM[2] = m33 * m.get(SkMatrix::kMTransX);
305 fM[3] = m33 * m.get(SkMatrix::kMSkewY);
306 fM[4] = m33 * m.get(SkMatrix::kMScaleY);
307 fM[5] = m33 * m.get(SkMatrix::kMTransY);
308 } else {
309 fM[0] = m.get(SkMatrix::kMScaleX);
310 fM[1] = m.get(SkMatrix::kMSkewX);
311 fM[2] = m.get(SkMatrix::kMTransX);
312 fM[3] = m.get(SkMatrix::kMSkewY);
313 fM[4] = m.get(SkMatrix::kMScaleY);
314 fM[5] = m.get(SkMatrix::kMTransY);
315 }
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000316 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000317}
318
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000319////////////////////////////////////////////////////////////////////////////////
320
Dean McNamee3b830a92017-01-13 12:17:09 +0000321// k = (y2 - y0, x0 - x2, x2*y0 - x0*y2)
322// l = (y1 - y0, x0 - x1, x1*y0 - x0*y1) * 2*w
323// m = (y2 - y1, x1 - x2, x2*y1 - x1*y2) * 2*w
csmartdaltoncc261272017-03-23 13:38:45 -0600324void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkMatrix* out) {
325 SkMatrix& klm = *out;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000326 const SkScalar w2 = 2.f * weight;
327 klm[0] = p[2].fY - p[0].fY;
328 klm[1] = p[0].fX - p[2].fX;
Dean McNamee3b830a92017-01-13 12:17:09 +0000329 klm[2] = p[2].fX * p[0].fY - p[0].fX * p[2].fY;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000330
331 klm[3] = w2 * (p[1].fY - p[0].fY);
332 klm[4] = w2 * (p[0].fX - p[1].fX);
333 klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY);
334
335 klm[6] = w2 * (p[2].fY - p[1].fY);
336 klm[7] = w2 * (p[1].fX - p[2].fX);
337 klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY);
338
339 // scale the max absolute value of coeffs to 10
340 SkScalar scale = 0.f;
341 for (int i = 0; i < 9; ++i) {
342 scale = SkMaxScalar(scale, SkScalarAbs(klm[i]));
343 }
344 SkASSERT(scale > 0.f);
345 scale = 10.f / scale;
346 for (int i = 0; i < 9; ++i) {
347 klm[i] *= scale;
348 }
349}
350
351////////////////////////////////////////////////////////////////////////////////
352
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000353namespace {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000354
355// a is the first control point of the cubic.
356// ab is the vector from a to the second control point.
357// dc is the vector from the fourth to the third control point.
358// d is the fourth control point.
359// p is the candidate quadratic control point.
360// this assumes that the cubic doesn't inflect and is simple
361bool is_point_within_cubic_tangents(const SkPoint& a,
362 const SkVector& ab,
363 const SkVector& dc,
364 const SkPoint& d,
reed026beb52015-06-10 14:23:15 -0700365 SkPathPriv::FirstDirection dir,
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000366 const SkPoint p) {
367 SkVector ap = p - a;
368 SkScalar apXab = ap.cross(ab);
reed026beb52015-06-10 14:23:15 -0700369 if (SkPathPriv::kCW_FirstDirection == dir) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000370 if (apXab > 0) {
371 return false;
372 }
373 } else {
reed026beb52015-06-10 14:23:15 -0700374 SkASSERT(SkPathPriv::kCCW_FirstDirection == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000375 if (apXab < 0) {
376 return false;
377 }
378 }
379
380 SkVector dp = p - d;
381 SkScalar dpXdc = dp.cross(dc);
reed026beb52015-06-10 14:23:15 -0700382 if (SkPathPriv::kCW_FirstDirection == dir) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000383 if (dpXdc < 0) {
384 return false;
385 }
386 } else {
reed026beb52015-06-10 14:23:15 -0700387 SkASSERT(SkPathPriv::kCCW_FirstDirection == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000388 if (dpXdc > 0) {
389 return false;
390 }
391 }
392 return true;
393}
394
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000395void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000396 SkScalar toleranceSqd,
397 bool constrainWithinTangents,
reed026beb52015-06-10 14:23:15 -0700398 SkPathPriv::FirstDirection dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000399 SkTArray<SkPoint, true>* quads,
400 int sublevel = 0) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000401
402 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is
403 // 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].
404
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000405 SkVector ab = p[1] - p[0];
406 SkVector dc = p[2] - p[3];
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000407
robertphillipsf08ce6c2015-12-08 05:19:12 -0800408 if (ab.lengthSqd() < SK_ScalarNearlyZero) {
409 if (dc.lengthSqd() < SK_ScalarNearlyZero) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000410 SkPoint* degQuad = quads->push_back_n(3);
411 degQuad[0] = p[0];
412 degQuad[1] = p[0];
413 degQuad[2] = p[3];
414 return;
415 }
416 ab = p[2] - p[0];
417 }
robertphillipsf08ce6c2015-12-08 05:19:12 -0800418 if (dc.lengthSqd() < SK_ScalarNearlyZero) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000419 dc = p[1] - p[3];
420 }
421
bsalomon3935a7b2014-06-19 12:33:08 -0700422 // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the
423 // constraint that the quad point falls between the tangents becomes hard to enforce and we are
424 // likely to hit the max subdivision count. However, in this case the cubic is approaching a
425 // line and the accuracy of the quad point isn't so important. We check if the two middle cubic
426 // control points are very close to the baseline vector. If so then we just pick quadratic
427 // points on the control polygon.
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000428
429 if (constrainWithinTangents) {
430 SkVector da = p[0] - p[3];
bsalomon3935a7b2014-06-19 12:33:08 -0700431 bool doQuads = dc.lengthSqd() < SK_ScalarNearlyZero ||
432 ab.lengthSqd() < SK_ScalarNearlyZero;
433 if (!doQuads) {
434 SkScalar invDALengthSqd = da.lengthSqd();
435 if (invDALengthSqd > SK_ScalarNearlyZero) {
436 invDALengthSqd = SkScalarInvert(invDALengthSqd);
437 // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a.
438 // same goes for point c using vector cd.
439 SkScalar detABSqd = ab.cross(da);
440 detABSqd = SkScalarSquare(detABSqd);
441 SkScalar detDCSqd = dc.cross(da);
442 detDCSqd = SkScalarSquare(detDCSqd);
Mike Reed8be952a2017-02-13 20:44:33 -0500443 if (detABSqd * invDALengthSqd < toleranceSqd &&
444 detDCSqd * invDALengthSqd < toleranceSqd)
445 {
bsalomon3935a7b2014-06-19 12:33:08 -0700446 doQuads = true;
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000447 }
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000448 }
449 }
bsalomon3935a7b2014-06-19 12:33:08 -0700450 if (doQuads) {
451 SkPoint b = p[0] + ab;
452 SkPoint c = p[3] + dc;
453 SkPoint mid = b + c;
454 mid.scale(SK_ScalarHalf);
455 // Insert two quadratics to cover the case when ab points away from d and/or dc
456 // points away from a.
457 if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) {
458 SkPoint* qpts = quads->push_back_n(6);
459 qpts[0] = p[0];
460 qpts[1] = b;
461 qpts[2] = mid;
462 qpts[3] = mid;
463 qpts[4] = c;
464 qpts[5] = p[3];
465 } else {
466 SkPoint* qpts = quads->push_back_n(3);
467 qpts[0] = p[0];
468 qpts[1] = mid;
469 qpts[2] = p[3];
470 }
471 return;
472 }
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000473 }
474
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000475 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000476 static const int kMaxSubdivs = 10;
477
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000478 ab.scale(kLengthScale);
479 dc.scale(kLengthScale);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000480
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000481 // e0 and e1 are extrapolations along vectors ab and dc.
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000482 SkVector c0 = p[0];
483 c0 += ab;
484 SkVector c1 = p[3];
485 c1 += dc;
486
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000487 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000488 if (dSqd < toleranceSqd) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000489 SkPoint cAvg = c0;
490 cAvg += c1;
491 cAvg.scale(SK_ScalarHalf);
492
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000493 bool subdivide = false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000494
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000495 if (constrainWithinTangents &&
496 !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000497 // choose a new cAvg that is the intersection of the two tangent lines.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000498 ab.setOrthog(ab);
499 SkScalar z0 = -ab.dot(p[0]);
500 dc.setOrthog(dc);
501 SkScalar z1 = -dc.dot(p[3]);
Mike Reed8be952a2017-02-13 20:44:33 -0500502 cAvg.fX = ab.fY * z1 - z0 * dc.fY;
503 cAvg.fY = z0 * dc.fX - ab.fX * z1;
504 SkScalar z = ab.fX * dc.fY - ab.fY * dc.fX;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000505 z = SkScalarInvert(z);
506 cAvg.fX *= z;
507 cAvg.fY *= z;
508 if (sublevel <= kMaxSubdivs) {
509 SkScalar d0Sqd = c0.distanceToSqd(cAvg);
510 SkScalar d1Sqd = c1.distanceToSqd(cAvg);
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000511 // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know
512 // the distances and tolerance can't be negative.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000513 // (d0 + d1)^2 > toleranceSqd
514 // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd
Mike Reed8be952a2017-02-13 20:44:33 -0500515 SkScalar d0d1 = SkScalarSqrt(d0Sqd * d1Sqd);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000516 subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd;
517 }
518 }
519 if (!subdivide) {
520 SkPoint* pts = quads->push_back_n(3);
521 pts[0] = p[0];
522 pts[1] = cAvg;
523 pts[2] = p[3];
524 return;
525 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000526 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000527 SkPoint choppedPts[7];
528 SkChopCubicAtHalf(p, choppedPts);
529 convert_noninflect_cubic_to_quads(choppedPts + 0,
530 toleranceSqd,
531 constrainWithinTangents,
532 dir,
533 quads,
534 sublevel + 1);
535 convert_noninflect_cubic_to_quads(choppedPts + 3,
536 toleranceSqd,
537 constrainWithinTangents,
538 dir,
539 quads,
540 sublevel + 1);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000541}
542}
543
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000544void GrPathUtils::convertCubicToQuads(const SkPoint p[4],
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000545 SkScalar tolScale,
546 SkTArray<SkPoint, true>* quads) {
547 SkPoint chopped[10];
548 int count = SkChopCubicAtInflections(p, chopped);
549
bsalomon18fab302016-02-16 08:00:05 -0800550 const SkScalar tolSqd = SkScalarSquare(tolScale);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000551
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000552 for (int i = 0; i < count; ++i) {
553 SkPoint* cubic = chopped + 3*i;
bsalomon18fab302016-02-16 08:00:05 -0800554 // The direction param is ignored if the third param is false.
555 convert_noninflect_cubic_to_quads(cubic, tolSqd, false,
556 SkPathPriv::kCCW_FirstDirection, quads);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000557 }
bsalomon18fab302016-02-16 08:00:05 -0800558}
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000559
bsalomon18fab302016-02-16 08:00:05 -0800560void GrPathUtils::convertCubicToQuadsConstrainToTangents(const SkPoint p[4],
561 SkScalar tolScale,
562 SkPathPriv::FirstDirection dir,
563 SkTArray<SkPoint, true>* quads) {
564 SkPoint chopped[10];
565 int count = SkChopCubicAtInflections(p, chopped);
566
567 const SkScalar tolSqd = SkScalarSquare(tolScale);
568
569 for (int i = 0; i < count; ++i) {
570 SkPoint* cubic = chopped + 3*i;
571 convert_noninflect_cubic_to_quads(cubic, tolSqd, true, dir, quads);
572 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000573}
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000574
575////////////////////////////////////////////////////////////////////////////////
576
csmartdaltoncc261272017-03-23 13:38:45 -0600577/**
578 * Computes an SkMatrix that can find the cubic KLM functionals as follows:
579 *
580 * | ..K.. | | ..kcoeffs.. |
581 * | ..L.. | = | ..lcoeffs.. | * inverse_transpose_power_basis_matrix
582 * | ..M.. | | ..mcoeffs.. |
583 *
584 * 'kcoeffs' are the power basis coefficients to a scalar valued cubic function that returns the
585 * signed distance to line K from a given point on the curve:
586 *
587 * k(t,s) = C(t,s) * K [C(t,s) is defined in the following comment]
588 *
589 * The same applies for lcoeffs and mcoeffs. These are found separately, depending on the type of
590 * curve. There are 4 coefficients but 3 rows in the matrix, so in order to do this calculation the
591 * caller must first remove a specific column of coefficients.
592 *
593 * @return which column of klm coefficients to exclude from the calculation.
594 */
595static int calc_inverse_transpose_power_basis_matrix(const SkPoint pts[4], SkMatrix* out) {
596 using SkScalar4 = SkNx<4, SkScalar>;
597
598 // First we convert the bezier coordinates 'pts' to power basis coefficients X,Y,W=[0 0 0 1].
599 // M3 is the matrix that does this conversion. The homogeneous equation for the cubic becomes:
600 //
601 // | X Y 0 |
602 // C(t,s) = [t^3 t^2*s t*s^2 s^3] * | . . 0 |
603 // | . . 0 |
604 // | . . 1 |
605 //
606 const SkScalar4 M3[3] = {SkScalar4(-1, 3, -3, 1),
607 SkScalar4(3, -6, 3, 0),
608 SkScalar4(-3, 3, 0, 0)};
609 // 4th column of M3 = SkScalar4(1, 0, 0, 0)};
610 SkScalar4 X(pts[3].x(), 0, 0, 0);
611 SkScalar4 Y(pts[3].y(), 0, 0, 0);
612 for (int i = 2; i >= 0; --i) {
613 X += M3[i] * pts[i].x();
614 Y += M3[i] * pts[i].y();
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000615 }
616
csmartdaltoncc261272017-03-23 13:38:45 -0600617 // The matrix is 3x4. In order to invert it, we first need to make it square by throwing out one
csmartdalton0f47a252017-04-10 11:58:17 -0600618 // of the top three rows. We toss the row that leaves us with the largest absolute determinant.
619 // Since the right column will be [0 0 1], the determinant reduces to x0*y1 - y0*x1.
620 SkScalar absDet[4];
621 const SkScalar4 DETX1 = SkNx_shuffle<1,0,0,3>(X), DETY1 = SkNx_shuffle<1,0,0,3>(Y);
622 const SkScalar4 DETX2 = SkNx_shuffle<2,2,1,3>(X), DETY2 = SkNx_shuffle<2,2,1,3>(Y);
623 const SkScalar4 DET = DETX1 * DETY2 - DETY1 * DETX2;
624 DET.abs().store(absDet);
625 const int skipRow = absDet[0] > absDet[2] ? (absDet[0] > absDet[1] ? 0 : 1)
626 : (absDet[1] > absDet[2] ? 1 : 2);
627 const SkScalar rdet = 1 / DET[skipRow];
csmartdaltoncc261272017-03-23 13:38:45 -0600628 const int row0 = (0 != skipRow) ? 0 : 1;
629 const int row1 = (2 == skipRow) ? 1 : 2;
630
631 // Compute the inverse-transpose of the power basis matrix with the 'skipRow'th row removed.
632 // Since W=[0 0 0 1], it follows that our corresponding solution will be equal to:
633 //
634 // | y1 -x1 x1*y2 - y1*x2 |
635 // 1/det * | -y0 x0 -x0*y2 + y0*x2 |
636 // | 0 0 det |
637 //
638 const SkScalar4 R(rdet, rdet, rdet, 1);
639 X *= R;
640 Y *= R;
641
642 SkScalar x[4], y[4], z[4];
643 X.store(x);
644 Y.store(y);
645 (X * SkNx_shuffle<3,3,3,3>(Y) - Y * SkNx_shuffle<3,3,3,3>(X)).store(z);
646
647 out->setAll( y[row1], -x[row1], z[row1],
648 -y[row0], x[row0], -z[row0],
649 0, 0, 1);
650
651 return skipRow;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000652}
653
csmartdaltoncc261272017-03-23 13:38:45 -0600654static void negate_kl(SkMatrix* klm) {
655 // We could use klm->postScale(-1, -1), but it ends up doing a full matrix multiply.
656 for (int i = 0; i < 6; ++i) {
657 (*klm)[i] = -(*klm)[i];
658 }
659}
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000660
Chris Dalton43436542017-04-13 14:26:00 -0600661static void calc_serp_klm(const SkPoint pts[4], const SkScalar d[4], SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600662 SkMatrix CIT;
663 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000664
Chris Dalton43436542017-04-13 14:26:00 -0600665 SkASSERT(d[0] >= 0);
666 const SkScalar root = SkScalarSqrt(3 * d[0]);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000667
Chris Dalton43436542017-04-13 14:26:00 -0600668 const SkScalar tl = 3 * d[2] + root;
669 const SkScalar sl = 6 * d[1];
670 const SkScalar tm = 3 * d[2] - root;
671 const SkScalar sm = 6 * d[1];
csmartdaltoncc261272017-03-23 13:38:45 -0600672
673 SkMatrix klmCoeffs;
674 int col = 0;
675 if (0 != skipCol) {
676 klmCoeffs[0] = 0;
677 klmCoeffs[3] = -sl * sl * sl;
678 klmCoeffs[6] = -sm * sm * sm;
679 ++col;
680 }
681 if (1 != skipCol) {
682 klmCoeffs[col + 0] = sl * sm;
683 klmCoeffs[col + 3] = 3 * sl * sl * tl;
684 klmCoeffs[col + 6] = 3 * sm * sm * tm;
685 ++col;
686 }
687 if (2 != skipCol) {
688 klmCoeffs[col + 0] = -tl * sm - tm * sl;
689 klmCoeffs[col + 3] = -3 * sl * tl * tl;
690 klmCoeffs[col + 6] = -3 * sm * tm * tm;
691 ++col;
692 }
693
694 SkASSERT(2 == col);
695 klmCoeffs[2] = tl * tm;
696 klmCoeffs[5] = tl * tl * tl;
697 klmCoeffs[8] = tm * tm * tm;
698
699 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000700
Chris Dalton43436542017-04-13 14:26:00 -0600701 // If d1 > 0 we need to flip the orientation of our curve
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000702 // This is done by negating the k and l values
703 // We want negative distance values to be on the inside
Chris Dalton43436542017-04-13 14:26:00 -0600704 if (d[1] > 0) {
csmartdaltoncc261272017-03-23 13:38:45 -0600705 negate_kl(klm);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000706 }
707}
708
csmartdaltoncc261272017-03-23 13:38:45 -0600709static void calc_loop_klm(const SkPoint pts[4], SkScalar d1, SkScalar td, SkScalar sd,
710 SkScalar te, SkScalar se, SkMatrix* klm) {
711 SkMatrix CIT;
712 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000713
csmartdaltoncc261272017-03-23 13:38:45 -0600714 const SkScalar tesd = te * sd;
715 const SkScalar tdse = td * se;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000716
csmartdaltoncc261272017-03-23 13:38:45 -0600717 SkMatrix klmCoeffs;
718 int col = 0;
719 if (0 != skipCol) {
720 klmCoeffs[0] = 0;
721 klmCoeffs[3] = -sd * sd * se;
722 klmCoeffs[6] = -se * se * sd;
723 ++col;
724 }
725 if (1 != skipCol) {
726 klmCoeffs[col + 0] = sd * se;
727 klmCoeffs[col + 3] = sd * (2 * tdse + tesd);
728 klmCoeffs[col + 6] = se * (2 * tesd + tdse);
729 ++col;
730 }
731 if (2 != skipCol) {
732 klmCoeffs[col + 0] = -tdse - tesd;
733 klmCoeffs[col + 3] = -td * (tdse + 2 * tesd);
734 klmCoeffs[col + 6] = -te * (tesd + 2 * tdse);
735 ++col;
736 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000737
csmartdaltoncc261272017-03-23 13:38:45 -0600738 SkASSERT(2 == col);
739 klmCoeffs[2] = td * te;
740 klmCoeffs[5] = td * td * te;
741 klmCoeffs[8] = te * te * td;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000742
csmartdaltoncc261272017-03-23 13:38:45 -0600743 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000744
Greg Daniel8199d942017-03-14 10:20:24 -0400745 // For the general loop curve, we flip the orientation in the same pattern as the serp case
csmartdaltoncc261272017-03-23 13:38:45 -0600746 // above. Thus we only check d1. Technically we should check the value of the hessian as well
747 // cause we care about the sign of d1*Hessian. However, the Hessian is always negative outside
Greg Daniel8199d942017-03-14 10:20:24 -0400748 // the loop section and positive inside. We take care of the flipping for the loop sections
749 // later on.
csmartdaltoncc261272017-03-23 13:38:45 -0600750 if (d1 > 0) {
751 negate_kl(klm);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000752 }
753}
754
csmartdaltoncc261272017-03-23 13:38:45 -0600755// For the case when we have a cusp at a parameter value of infinity (discr == 0, d1 == 0).
756static void calc_inf_cusp_klm(const SkPoint pts[4], SkScalar d2, SkScalar d3, SkMatrix* klm) {
757 SkMatrix CIT;
758 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000759
csmartdaltoncc261272017-03-23 13:38:45 -0600760 const SkScalar tn = d3;
761 const SkScalar sn = 3 * d2;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000762
csmartdaltoncc261272017-03-23 13:38:45 -0600763 SkMatrix klmCoeffs;
764 int col = 0;
765 if (0 != skipCol) {
766 klmCoeffs[0] = 0;
767 klmCoeffs[3] = -sn * sn * sn;
768 ++col;
769 }
770 if (1 != skipCol) {
771 klmCoeffs[col + 0] = 0;
772 klmCoeffs[col + 3] = 3 * sn * sn * tn;
773 ++col;
774 }
775 if (2 != skipCol) {
776 klmCoeffs[col + 0] = -sn;
777 klmCoeffs[col + 3] = -3 * sn * tn * tn;
778 ++col;
779 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000780
csmartdaltoncc261272017-03-23 13:38:45 -0600781 SkASSERT(2 == col);
782 klmCoeffs[2] = tn;
783 klmCoeffs[5] = tn * tn * tn;
784
785 klmCoeffs[6] = 0;
786 klmCoeffs[7] = 0;
787 klmCoeffs[8] = 1;
788
789 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000790}
791
csmartdaltoncc261272017-03-23 13:38:45 -0600792// For the case when a cubic bezier is actually a quadratic. We duplicate k in l so that the
793// implicit becomes:
794//
795// k^3 - l*m == k^3 - l*k == k * (k^2 - l)
796//
797// In the quadratic case we can simply assign fixed values at each control point:
798//
799// | ..K.. | | pts[0] pts[1] pts[2] pts[3] | | 0 1/3 2/3 1 |
800// | ..L.. | * | . . . . | == | 0 0 1/3 1 |
801// | ..K.. | | 1 1 1 1 | | 0 1/3 2/3 1 |
802//
803static void calc_quadratic_klm(const SkPoint pts[4], SkScalar d3, SkMatrix* klm) {
804 SkMatrix klmAtPts;
805 klmAtPts.setAll(0, 1.f/3, 1,
806 0, 0, 1,
807 0, 1.f/3, 1);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000808
csmartdaltoncc261272017-03-23 13:38:45 -0600809 SkMatrix inversePts;
810 inversePts.setAll(pts[0].x(), pts[1].x(), pts[3].x(),
811 pts[0].y(), pts[1].y(), pts[3].y(),
812 1, 1, 1);
813 SkAssertResult(inversePts.invert(&inversePts));
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000814
csmartdaltoncc261272017-03-23 13:38:45 -0600815 klm->setConcat(klmAtPts, inversePts);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000816
csmartdaltoncc261272017-03-23 13:38:45 -0600817 // If d3 > 0 we need to flip the orientation of our curve
818 // This is done by negating the k and l values
819 if (d3 > 0) {
820 negate_kl(klm);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000821 }
822}
823
csmartdaltoncc261272017-03-23 13:38:45 -0600824// For the case when a cubic bezier is actually a line. We set K=0, L=1, M=-line, which results in
825// the following implicit:
826//
827// k^3 - l*m == 0^3 - 1*(-line) == -(-line) == line
828//
829static void calc_line_klm(const SkPoint pts[4], SkMatrix* klm) {
830 SkScalar ny = pts[0].x() - pts[3].x();
831 SkScalar nx = pts[3].y() - pts[0].y();
832 SkScalar k = nx * pts[0].x() + ny * pts[0].y();
833 klm->setAll( 0, 0, 0,
834 0, 0, 1,
835 -nx, -ny, k);
836}
837
838int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkMatrix* klm,
Greg Daniel8199d942017-03-14 10:20:24 -0400839 int* loopIndex) {
csmartdaltoncc261272017-03-23 13:38:45 -0600840 // Variables to store the two parametric values at the loop double point.
841 SkScalar t1 = 0, t2 = 0;
842
843 // Homogeneous parametric values at the loop double point.
844 SkScalar td, sd, te, se;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000845
Chris Dalton43436542017-04-13 14:26:00 -0600846 SkScalar d[4];
caryclark8dd31cf2014-12-12 09:11:23 -0800847 SkCubicType cType = SkClassifyCubic(src, d);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000848
849 int chop_count = 0;
Chris Dalton43436542017-04-13 14:26:00 -0600850 if (SkCubicType::kLoop == cType) {
851 SkASSERT(d[0] < 0);
852 const SkScalar tempSqrt = SkScalarSqrt(-d[0]);
853 td = d[2] + tempSqrt;
854 sd = 2.f * d[1];
855 te = d[2] - tempSqrt;
856 se = 2.f * d[1];
csmartdaltoncc261272017-03-23 13:38:45 -0600857
858 t1 = td / sd;
859 t2 = te / se;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000860 // need to have t values sorted since this is what is expected by SkChopCubicAt
csmartdaltoncc261272017-03-23 13:38:45 -0600861 if (t1 > t2) {
862 SkTSwap(t1, t2);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000863 }
864
865 SkScalar chop_ts[2];
csmartdaltoncc261272017-03-23 13:38:45 -0600866 if (t1 > 0.f && t1 < 1.f) {
867 chop_ts[chop_count++] = t1;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000868 }
csmartdaltoncc261272017-03-23 13:38:45 -0600869 if (t2 > 0.f && t2 < 1.f) {
870 chop_ts[chop_count++] = t2;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000871 }
872 if(dst) {
873 SkChopCubicAt(src, dst, chop_ts, chop_count);
874 }
875 } else {
876 if (dst) {
877 memcpy(dst, src, sizeof(SkPoint) * 4);
878 }
879 }
880
Greg Daniel8199d942017-03-14 10:20:24 -0400881 if (loopIndex) {
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000882 if (2 == chop_count) {
Greg Daniel8199d942017-03-14 10:20:24 -0400883 *loopIndex = 1;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000884 } else if (1 == chop_count) {
csmartdaltoncc261272017-03-23 13:38:45 -0600885 if (t1 < 0.f) {
Greg Daniel8199d942017-03-14 10:20:24 -0400886 *loopIndex = 0;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000887 } else {
Greg Daniel8199d942017-03-14 10:20:24 -0400888 *loopIndex = 1;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000889 }
890 } else {
csmartdaltoncc261272017-03-23 13:38:45 -0600891 if (t1 < 0.f && t2 > 1.f) {
Greg Daniel8199d942017-03-14 10:20:24 -0400892 *loopIndex = 0;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000893 } else {
Greg Daniel8199d942017-03-14 10:20:24 -0400894 *loopIndex = -1;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000895 }
896 }
Greg Daniel8199d942017-03-14 10:20:24 -0400897 }
csmartdaltoncc261272017-03-23 13:38:45 -0600898
Greg Daniel8199d942017-03-14 10:20:24 -0400899 if (klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600900 switch (cType) {
Chris Dalton43436542017-04-13 14:26:00 -0600901 case SkCubicType::kSerpentine:
902 case SkCubicType::kLocalCusp:
csmartdaltoncc261272017-03-23 13:38:45 -0600903 calc_serp_klm(src, d, klm);
904 break;
Chris Dalton43436542017-04-13 14:26:00 -0600905 case SkCubicType::kLoop:
906 calc_loop_klm(src, d[1], td, sd, te, se, klm);
csmartdaltoncc261272017-03-23 13:38:45 -0600907 break;
Chris Dalton43436542017-04-13 14:26:00 -0600908 case SkCubicType::kInfiniteCusp:
909 calc_inf_cusp_klm(src, d[2], d[3], klm);
csmartdaltoncc261272017-03-23 13:38:45 -0600910 break;
Chris Dalton43436542017-04-13 14:26:00 -0600911 case SkCubicType::kQuadratic:
912 calc_quadratic_klm(src, d[3], klm);
csmartdaltoncc261272017-03-23 13:38:45 -0600913 break;
Chris Dalton43436542017-04-13 14:26:00 -0600914 case SkCubicType::kLineOrPoint:
csmartdaltoncc261272017-03-23 13:38:45 -0600915 calc_line_klm(src, klm);
916 break;
917 };
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000918 }
919 return chop_count + 1;
920}