blob: b6711a0df4338e85081972007957f0eebf5de895 [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"
halcanary4dbbd042016-06-07 17:21:10 -070011#include "SkMathPriv.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000012
Brian Osman25294d72017-05-09 16:36:41 -040013static const SkScalar gMinCurveTol = 0.0001f;
14
bsalomon@google.com81712882012-11-01 17:12:34 +000015SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000016 const SkMatrix& viewM,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000017 const SkRect& pathBounds) {
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000018 // 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 +000019 // scale when mapping to screen coordinates.
20 SkScalar stretch = viewM.getMaxScale();
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 }
Brian Osman25294d72017-05-09 16:36:41 -040033 SkScalar srcTol = devTol / stretch;
34 if (srcTol < gMinCurveTol) {
35 srcTol = gMinCurveTol;
36 }
37 return srcTol;
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000038}
39
Robert Phillipsb712a852017-04-18 16:56:06 -040040uint32_t GrPathUtils::quadraticPointCount(const SkPoint points[], SkScalar tol) {
Brian Osman25294d72017-05-09 16:36:41 -040041 // You should have called scaleToleranceToSrc, which guarantees this
42 SkASSERT(tol >= gMinCurveTol);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000043
bsalomon@google.com81712882012-11-01 17:12:34 +000044 SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]);
senorblancob6a40b82016-08-19 08:07:22 -070045 if (!SkScalarIsFinite(d)) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040046 return kMaxPointsPerCurve;
senorblancob6a40b82016-08-19 08:07:22 -070047 } else 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);
reed80ea19c2015-05-12 10:37:34 -070054 SkScalar divSqrt = SkScalarSqrt(d / tol);
egdaniel5a23a142015-02-25 06:41:47 -080055 if (((SkScalar)SK_MaxS32) <= divSqrt) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040056 return kMaxPointsPerCurve;
egdaniel5a23a142015-02-25 06:41:47 -080057 } else {
58 int temp = SkScalarCeilToInt(divSqrt);
59 int pow2 = GrNextPow2(temp);
60 // Because of NaNs & INFs we can wind up with a degenerate temp
61 // such that pow2 comes out negative. Also, our point generator
62 // will always output at least one pt.
63 if (pow2 < 1) {
64 pow2 = 1;
65 }
Brian Osman49b7b6f2017-06-20 14:43:58 -040066 return SkTMin(pow2, kMaxPointsPerCurve);
bsalomon@google.com61f3bde2011-06-17 20:06:49 +000067 }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000068 }
69}
70
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000071uint32_t GrPathUtils::generateQuadraticPoints(const SkPoint& p0,
72 const SkPoint& p1,
73 const SkPoint& p2,
bsalomon@google.com81712882012-11-01 17:12:34 +000074 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000075 SkPoint** points,
tomhudson@google.comc10a8882011-06-28 15:19:32 +000076 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000077 if (pointsLeft < 2 ||
78 (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) {
79 (*points)[0] = p2;
80 *points += 1;
81 return 1;
82 }
83
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000084 SkPoint q[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000085 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
86 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000087 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000088 SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000089
90 pointsLeft >>= 1;
91 uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft);
92 uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft);
93 return a + b;
94}
95
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000096uint32_t GrPathUtils::cubicPointCount(const SkPoint points[],
bsalomon@google.com81712882012-11-01 17:12:34 +000097 SkScalar tol) {
Brian Osman25294d72017-05-09 16:36:41 -040098 // You should have called scaleToleranceToSrc, which guarantees this
99 SkASSERT(tol >= gMinCurveTol);
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000100
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000101 SkScalar d = SkTMax(
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000102 points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]),
103 points[2].distanceToLineSegmentBetweenSqd(points[0], points[3]));
epoger@google.com2047f002011-05-17 17:36:59 +0000104 d = SkScalarSqrt(d);
senorblancob6a40b82016-08-19 08:07:22 -0700105 if (!SkScalarIsFinite(d)) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400106 return kMaxPointsPerCurve;
senorblancob6a40b82016-08-19 08:07:22 -0700107 } else if (d <= tol) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000108 return 1;
109 } else {
reed80ea19c2015-05-12 10:37:34 -0700110 SkScalar divSqrt = SkScalarSqrt(d / tol);
egdaniel5a23a142015-02-25 06:41:47 -0800111 if (((SkScalar)SK_MaxS32) <= divSqrt) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400112 return kMaxPointsPerCurve;
egdaniel5a23a142015-02-25 06:41:47 -0800113 } else {
reed80ea19c2015-05-12 10:37:34 -0700114 int temp = SkScalarCeilToInt(SkScalarSqrt(d / tol));
egdaniel5a23a142015-02-25 06:41:47 -0800115 int pow2 = GrNextPow2(temp);
116 // Because of NaNs & INFs we can wind up with a degenerate temp
117 // such that pow2 comes out negative. Also, our point generator
118 // will always output at least one pt.
119 if (pow2 < 1) {
120 pow2 = 1;
121 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400122 return SkTMin(pow2, kMaxPointsPerCurve);
bsalomon@google.com61f3bde2011-06-17 20:06:49 +0000123 }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000124 }
125}
126
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000127uint32_t GrPathUtils::generateCubicPoints(const SkPoint& p0,
128 const SkPoint& p1,
129 const SkPoint& p2,
130 const SkPoint& p3,
bsalomon@google.com81712882012-11-01 17:12:34 +0000131 SkScalar tolSqd,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000132 SkPoint** points,
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000133 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000134 if (pointsLeft < 2 ||
135 (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd &&
136 p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) {
robertphillipsf08ce6c2015-12-08 05:19:12 -0800137 (*points)[0] = p3;
138 *points += 1;
139 return 1;
140 }
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000141 SkPoint q[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000142 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
143 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
144 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000145 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000146 SkPoint r[] = {
bsalomon@google.com81712882012-11-01 17:12:34 +0000147 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
148 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000149 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000150 SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000151 pointsLeft >>= 1;
152 uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
153 uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
154 return a + b;
155}
156
Robert Phillipsb712a852017-04-18 16:56:06 -0400157int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, SkScalar tol) {
Brian Osman25294d72017-05-09 16:36:41 -0400158 // You should have called scaleToleranceToSrc, which guarantees this
159 SkASSERT(tol >= gMinCurveTol);
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000160
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000161 int pointCount = 0;
162 *subpaths = 1;
163
164 bool first = true;
165
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000166 SkPath::Iter iter(path, false);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000167 SkPath::Verb verb;
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000168
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000169 SkPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000170 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000171
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000172 switch (verb) {
173 case SkPath::kLine_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000174 pointCount += 1;
175 break;
egdanielaf18a092015-01-05 10:22:28 -0800176 case SkPath::kConic_Verb: {
177 SkScalar weight = iter.conicWeight();
178 SkAutoConicToQuads converter;
Robert Phillipsb712a852017-04-18 16:56:06 -0400179 const SkPoint* quadPts = converter.computeQuads(pts, weight, tol);
egdanielaf18a092015-01-05 10:22:28 -0800180 for (int i = 0; i < converter.countQuads(); ++i) {
181 pointCount += quadraticPointCount(quadPts + 2*i, tol);
182 }
183 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000184 case SkPath::kQuad_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000185 pointCount += quadraticPointCount(pts, tol);
186 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000187 case SkPath::kCubic_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000188 pointCount += cubicPointCount(pts, tol);
189 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000190 case SkPath::kMove_Verb:
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000191 pointCount += 1;
192 if (!first) {
193 ++(*subpaths);
194 }
195 break;
196 default:
197 break;
198 }
199 first = false;
200 }
201 return pointCount;
202}
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000203
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000204void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) {
bsalomon@google.com19713172012-03-15 13:51:08 +0000205 SkMatrix m;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000206 // We want M such that M * xy_pt = uv_pt
207 // We know M * control_pts = [0 1/2 1]
208 // [0 0 1]
209 // [1 1 1]
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000210 // And control_pts = [x0 x1 x2]
211 // [y0 y1 y2]
212 // [1 1 1 ]
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000213 // We invert the control pt matrix and post concat to both sides to get M.
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000214 // Using the known form of the control point matrix and the result, we can
215 // optimize and improve precision.
216
217 double x0 = qPts[0].fX;
218 double y0 = qPts[0].fY;
219 double x1 = qPts[1].fX;
220 double y1 = qPts[1].fY;
221 double x2 = qPts[2].fX;
222 double y2 = qPts[2].fY;
223 double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2;
224
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000225 if (!sk_float_isfinite(det)
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000226 || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) {
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000227 // The quad is degenerate. Hopefully this is rare. Find the pts that are
228 // farthest apart to compute a line (unless it is really a pt).
229 SkScalar maxD = qPts[0].distanceToSqd(qPts[1]);
230 int maxEdge = 0;
231 SkScalar d = qPts[1].distanceToSqd(qPts[2]);
232 if (d > maxD) {
233 maxD = d;
234 maxEdge = 1;
235 }
236 d = qPts[2].distanceToSqd(qPts[0]);
237 if (d > maxD) {
238 maxD = d;
239 maxEdge = 2;
240 }
241 // We could have a tolerance here, not sure if it would improve anything
242 if (maxD > 0) {
243 // Set the matrix to give (u = 0, v = distance_to_line)
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000244 SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge];
bsalomon@google.com20e542e2012-02-15 18:49:41 +0000245 // when looking from the point 0 down the line we want positive
246 // distances to be to the left. This matches the non-degenerate
247 // case.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000248 lineVec.setOrthog(lineVec, SkPoint::kLeft_Side);
bsalomon@google.com19713172012-03-15 13:51:08 +0000249 // first row
250 fM[0] = 0;
251 fM[1] = 0;
252 fM[2] = 0;
253 // second row
254 fM[3] = lineVec.fX;
255 fM[4] = lineVec.fY;
256 fM[5] = -lineVec.dot(qPts[maxEdge]);
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000257 } else {
258 // It's a point. It should cover zero area. Just set the matrix such
259 // that (u, v) will always be far away from the quad.
bsalomon@google.com19713172012-03-15 13:51:08 +0000260 fM[0] = 0; fM[1] = 0; fM[2] = 100.f;
261 fM[3] = 0; fM[4] = 0; fM[5] = 100.f;
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000262 }
263 } else {
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000264 double scale = 1.0/det;
265
266 // compute adjugate matrix
robertphillips87a22342016-03-01 14:49:44 -0800267 double a2, a3, a4, a5, a6, a7, a8;
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000268 a2 = x1*y2-x2*y1;
269
270 a3 = y2-y0;
271 a4 = x0-x2;
272 a5 = x2*y0-x0*y2;
273
274 a6 = y0-y1;
275 a7 = x1-x0;
276 a8 = x0*y1-x1*y0;
277
skia.committer@gmail.com8491d242013-12-05 07:02:16 +0000278 // this performs the uv_pts*adjugate(control_pts) multiply,
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000279 // then does the scale by 1/det afterwards to improve precision
280 m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale);
281 m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale);
282 m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale);
283
284 m[SkMatrix::kMSkewY] = (float)(a6*scale);
285 m[SkMatrix::kMScaleY] = (float)(a7*scale);
286 m[SkMatrix::kMTransY] = (float)(a8*scale);
287
robertphillips87a22342016-03-01 14:49:44 -0800288 // kMPersp0 & kMPersp1 should algebraically be zero
289 m[SkMatrix::kMPersp0] = 0.0f;
290 m[SkMatrix::kMPersp1] = 0.0f;
commit-bot@chromium.orgf543fd92013-12-04 21:33:08 +0000291 m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale);
bsalomon@google.com19713172012-03-15 13:51:08 +0000292
bsalomon@google.com19713172012-03-15 13:51:08 +0000293 // 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
Dean McNamee3b830a92017-01-13 12:17:09 +0000316// k = (y2 - y0, x0 - x2, x2*y0 - x0*y2)
317// l = (y1 - y0, x0 - x1, x1*y0 - x0*y1) * 2*w
318// m = (y2 - y1, x1 - x2, x2*y1 - x1*y2) * 2*w
csmartdaltoncc261272017-03-23 13:38:45 -0600319void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkMatrix* out) {
320 SkMatrix& klm = *out;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000321 const SkScalar w2 = 2.f * weight;
322 klm[0] = p[2].fY - p[0].fY;
323 klm[1] = p[0].fX - p[2].fX;
Dean McNamee3b830a92017-01-13 12:17:09 +0000324 klm[2] = p[2].fX * p[0].fY - p[0].fX * p[2].fY;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000325
326 klm[3] = w2 * (p[1].fY - p[0].fY);
327 klm[4] = w2 * (p[0].fX - p[1].fX);
328 klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY);
329
330 klm[6] = w2 * (p[2].fY - p[1].fY);
331 klm[7] = w2 * (p[1].fX - p[2].fX);
332 klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY);
333
334 // scale the max absolute value of coeffs to 10
335 SkScalar scale = 0.f;
336 for (int i = 0; i < 9; ++i) {
337 scale = SkMaxScalar(scale, SkScalarAbs(klm[i]));
338 }
339 SkASSERT(scale > 0.f);
340 scale = 10.f / scale;
341 for (int i = 0; i < 9; ++i) {
342 klm[i] *= scale;
343 }
344}
345
346////////////////////////////////////////////////////////////////////////////////
347
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000348namespace {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000349
350// a is the first control point of the cubic.
351// ab is the vector from a to the second control point.
352// dc is the vector from the fourth to the third control point.
353// d is the fourth control point.
354// p is the candidate quadratic control point.
355// this assumes that the cubic doesn't inflect and is simple
356bool is_point_within_cubic_tangents(const SkPoint& a,
357 const SkVector& ab,
358 const SkVector& dc,
359 const SkPoint& d,
reed026beb52015-06-10 14:23:15 -0700360 SkPathPriv::FirstDirection dir,
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000361 const SkPoint p) {
362 SkVector ap = p - a;
363 SkScalar apXab = ap.cross(ab);
reed026beb52015-06-10 14:23:15 -0700364 if (SkPathPriv::kCW_FirstDirection == dir) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000365 if (apXab > 0) {
366 return false;
367 }
368 } else {
reed026beb52015-06-10 14:23:15 -0700369 SkASSERT(SkPathPriv::kCCW_FirstDirection == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000370 if (apXab < 0) {
371 return false;
372 }
373 }
374
375 SkVector dp = p - d;
376 SkScalar dpXdc = dp.cross(dc);
reed026beb52015-06-10 14:23:15 -0700377 if (SkPathPriv::kCW_FirstDirection == dir) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000378 if (dpXdc < 0) {
379 return false;
380 }
381 } else {
reed026beb52015-06-10 14:23:15 -0700382 SkASSERT(SkPathPriv::kCCW_FirstDirection == dir);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000383 if (dpXdc > 0) {
384 return false;
385 }
386 }
387 return true;
388}
389
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000390void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000391 SkScalar toleranceSqd,
392 bool constrainWithinTangents,
reed026beb52015-06-10 14:23:15 -0700393 SkPathPriv::FirstDirection dir,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000394 SkTArray<SkPoint, true>* quads,
395 int sublevel = 0) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000396
397 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is
398 // 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].
399
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000400 SkVector ab = p[1] - p[0];
401 SkVector dc = p[2] - p[3];
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000402
robertphillipsf08ce6c2015-12-08 05:19:12 -0800403 if (ab.lengthSqd() < SK_ScalarNearlyZero) {
404 if (dc.lengthSqd() < SK_ScalarNearlyZero) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000405 SkPoint* degQuad = quads->push_back_n(3);
406 degQuad[0] = p[0];
407 degQuad[1] = p[0];
408 degQuad[2] = p[3];
409 return;
410 }
411 ab = p[2] - p[0];
412 }
robertphillipsf08ce6c2015-12-08 05:19:12 -0800413 if (dc.lengthSqd() < SK_ScalarNearlyZero) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000414 dc = p[1] - p[3];
415 }
416
bsalomon3935a7b2014-06-19 12:33:08 -0700417 // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the
418 // constraint that the quad point falls between the tangents becomes hard to enforce and we are
419 // likely to hit the max subdivision count. However, in this case the cubic is approaching a
420 // line and the accuracy of the quad point isn't so important. We check if the two middle cubic
421 // control points are very close to the baseline vector. If so then we just pick quadratic
422 // points on the control polygon.
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000423
424 if (constrainWithinTangents) {
425 SkVector da = p[0] - p[3];
bsalomon3935a7b2014-06-19 12:33:08 -0700426 bool doQuads = dc.lengthSqd() < SK_ScalarNearlyZero ||
427 ab.lengthSqd() < SK_ScalarNearlyZero;
428 if (!doQuads) {
429 SkScalar invDALengthSqd = da.lengthSqd();
430 if (invDALengthSqd > SK_ScalarNearlyZero) {
431 invDALengthSqd = SkScalarInvert(invDALengthSqd);
432 // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a.
433 // same goes for point c using vector cd.
434 SkScalar detABSqd = ab.cross(da);
435 detABSqd = SkScalarSquare(detABSqd);
436 SkScalar detDCSqd = dc.cross(da);
437 detDCSqd = SkScalarSquare(detDCSqd);
Mike Reed8be952a2017-02-13 20:44:33 -0500438 if (detABSqd * invDALengthSqd < toleranceSqd &&
439 detDCSqd * invDALengthSqd < toleranceSqd)
440 {
bsalomon3935a7b2014-06-19 12:33:08 -0700441 doQuads = true;
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000442 }
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000443 }
444 }
bsalomon3935a7b2014-06-19 12:33:08 -0700445 if (doQuads) {
446 SkPoint b = p[0] + ab;
447 SkPoint c = p[3] + dc;
448 SkPoint mid = b + c;
449 mid.scale(SK_ScalarHalf);
450 // Insert two quadratics to cover the case when ab points away from d and/or dc
451 // points away from a.
452 if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) {
453 SkPoint* qpts = quads->push_back_n(6);
454 qpts[0] = p[0];
455 qpts[1] = b;
456 qpts[2] = mid;
457 qpts[3] = mid;
458 qpts[4] = c;
459 qpts[5] = p[3];
460 } else {
461 SkPoint* qpts = quads->push_back_n(3);
462 qpts[0] = p[0];
463 qpts[1] = mid;
464 qpts[2] = p[3];
465 }
466 return;
467 }
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000468 }
469
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000470 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000471 static const int kMaxSubdivs = 10;
472
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000473 ab.scale(kLengthScale);
474 dc.scale(kLengthScale);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000475
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000476 // e0 and e1 are extrapolations along vectors ab and dc.
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000477 SkVector c0 = p[0];
478 c0 += ab;
479 SkVector c1 = p[3];
480 c1 += dc;
481
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000482 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000483 if (dSqd < toleranceSqd) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000484 SkPoint cAvg = c0;
485 cAvg += c1;
486 cAvg.scale(SK_ScalarHalf);
487
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000488 bool subdivide = false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000489
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000490 if (constrainWithinTangents &&
491 !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) {
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000492 // choose a new cAvg that is the intersection of the two tangent lines.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000493 ab.setOrthog(ab);
494 SkScalar z0 = -ab.dot(p[0]);
495 dc.setOrthog(dc);
496 SkScalar z1 = -dc.dot(p[3]);
Mike Reed8be952a2017-02-13 20:44:33 -0500497 cAvg.fX = ab.fY * z1 - z0 * dc.fY;
498 cAvg.fY = z0 * dc.fX - ab.fX * z1;
499 SkScalar z = ab.fX * dc.fY - ab.fY * dc.fX;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000500 z = SkScalarInvert(z);
501 cAvg.fX *= z;
502 cAvg.fY *= z;
503 if (sublevel <= kMaxSubdivs) {
504 SkScalar d0Sqd = c0.distanceToSqd(cAvg);
505 SkScalar d1Sqd = c1.distanceToSqd(cAvg);
bsalomon@google.com54ad8512012-08-02 14:55:45 +0000506 // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know
507 // the distances and tolerance can't be negative.
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000508 // (d0 + d1)^2 > toleranceSqd
509 // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd
Mike Reed8be952a2017-02-13 20:44:33 -0500510 SkScalar d0d1 = SkScalarSqrt(d0Sqd * d1Sqd);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000511 subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd;
512 }
513 }
514 if (!subdivide) {
515 SkPoint* pts = quads->push_back_n(3);
516 pts[0] = p[0];
517 pts[1] = cAvg;
518 pts[2] = p[3];
519 return;
520 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000521 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000522 SkPoint choppedPts[7];
523 SkChopCubicAtHalf(p, choppedPts);
524 convert_noninflect_cubic_to_quads(choppedPts + 0,
525 toleranceSqd,
526 constrainWithinTangents,
527 dir,
528 quads,
529 sublevel + 1);
530 convert_noninflect_cubic_to_quads(choppedPts + 3,
531 toleranceSqd,
532 constrainWithinTangents,
533 dir,
534 quads,
535 sublevel + 1);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000536}
537}
538
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000539void GrPathUtils::convertCubicToQuads(const SkPoint p[4],
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000540 SkScalar tolScale,
541 SkTArray<SkPoint, true>* quads) {
542 SkPoint chopped[10];
543 int count = SkChopCubicAtInflections(p, chopped);
544
bsalomon18fab302016-02-16 08:00:05 -0800545 const SkScalar tolSqd = SkScalarSquare(tolScale);
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000546
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000547 for (int i = 0; i < count; ++i) {
548 SkPoint* cubic = chopped + 3*i;
bsalomon18fab302016-02-16 08:00:05 -0800549 // The direction param is ignored if the third param is false.
550 convert_noninflect_cubic_to_quads(cubic, tolSqd, false,
551 SkPathPriv::kCCW_FirstDirection, quads);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000552 }
bsalomon18fab302016-02-16 08:00:05 -0800553}
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000554
bsalomon18fab302016-02-16 08:00:05 -0800555void GrPathUtils::convertCubicToQuadsConstrainToTangents(const SkPoint p[4],
556 SkScalar tolScale,
557 SkPathPriv::FirstDirection dir,
558 SkTArray<SkPoint, true>* quads) {
559 SkPoint chopped[10];
560 int count = SkChopCubicAtInflections(p, chopped);
561
562 const SkScalar tolSqd = SkScalarSquare(tolScale);
563
564 for (int i = 0; i < count; ++i) {
565 SkPoint* cubic = chopped + 3*i;
566 convert_noninflect_cubic_to_quads(cubic, tolSqd, true, dir, quads);
567 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000568}
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000569
570////////////////////////////////////////////////////////////////////////////////
571
csmartdaltoncc261272017-03-23 13:38:45 -0600572/**
573 * Computes an SkMatrix that can find the cubic KLM functionals as follows:
574 *
575 * | ..K.. | | ..kcoeffs.. |
576 * | ..L.. | = | ..lcoeffs.. | * inverse_transpose_power_basis_matrix
577 * | ..M.. | | ..mcoeffs.. |
578 *
579 * 'kcoeffs' are the power basis coefficients to a scalar valued cubic function that returns the
580 * signed distance to line K from a given point on the curve:
581 *
582 * k(t,s) = C(t,s) * K [C(t,s) is defined in the following comment]
583 *
584 * The same applies for lcoeffs and mcoeffs. These are found separately, depending on the type of
585 * curve. There are 4 coefficients but 3 rows in the matrix, so in order to do this calculation the
586 * caller must first remove a specific column of coefficients.
587 *
588 * @return which column of klm coefficients to exclude from the calculation.
589 */
590static int calc_inverse_transpose_power_basis_matrix(const SkPoint pts[4], SkMatrix* out) {
591 using SkScalar4 = SkNx<4, SkScalar>;
592
593 // First we convert the bezier coordinates 'pts' to power basis coefficients X,Y,W=[0 0 0 1].
594 // M3 is the matrix that does this conversion. The homogeneous equation for the cubic becomes:
595 //
596 // | X Y 0 |
597 // C(t,s) = [t^3 t^2*s t*s^2 s^3] * | . . 0 |
598 // | . . 0 |
599 // | . . 1 |
600 //
601 const SkScalar4 M3[3] = {SkScalar4(-1, 3, -3, 1),
602 SkScalar4(3, -6, 3, 0),
603 SkScalar4(-3, 3, 0, 0)};
604 // 4th column of M3 = SkScalar4(1, 0, 0, 0)};
605 SkScalar4 X(pts[3].x(), 0, 0, 0);
606 SkScalar4 Y(pts[3].y(), 0, 0, 0);
607 for (int i = 2; i >= 0; --i) {
608 X += M3[i] * pts[i].x();
609 Y += M3[i] * pts[i].y();
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000610 }
611
csmartdaltoncc261272017-03-23 13:38:45 -0600612 // 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 -0600613 // of the top three rows. We toss the row that leaves us with the largest absolute determinant.
614 // Since the right column will be [0 0 1], the determinant reduces to x0*y1 - y0*x1.
615 SkScalar absDet[4];
616 const SkScalar4 DETX1 = SkNx_shuffle<1,0,0,3>(X), DETY1 = SkNx_shuffle<1,0,0,3>(Y);
617 const SkScalar4 DETX2 = SkNx_shuffle<2,2,1,3>(X), DETY2 = SkNx_shuffle<2,2,1,3>(Y);
618 const SkScalar4 DET = DETX1 * DETY2 - DETY1 * DETX2;
619 DET.abs().store(absDet);
620 const int skipRow = absDet[0] > absDet[2] ? (absDet[0] > absDet[1] ? 0 : 1)
621 : (absDet[1] > absDet[2] ? 1 : 2);
622 const SkScalar rdet = 1 / DET[skipRow];
csmartdaltoncc261272017-03-23 13:38:45 -0600623 const int row0 = (0 != skipRow) ? 0 : 1;
624 const int row1 = (2 == skipRow) ? 1 : 2;
625
626 // Compute the inverse-transpose of the power basis matrix with the 'skipRow'th row removed.
627 // Since W=[0 0 0 1], it follows that our corresponding solution will be equal to:
628 //
629 // | y1 -x1 x1*y2 - y1*x2 |
630 // 1/det * | -y0 x0 -x0*y2 + y0*x2 |
631 // | 0 0 det |
632 //
633 const SkScalar4 R(rdet, rdet, rdet, 1);
634 X *= R;
635 Y *= R;
636
637 SkScalar x[4], y[4], z[4];
638 X.store(x);
639 Y.store(y);
640 (X * SkNx_shuffle<3,3,3,3>(Y) - Y * SkNx_shuffle<3,3,3,3>(X)).store(z);
641
642 out->setAll( y[row1], -x[row1], z[row1],
643 -y[row0], x[row0], -z[row0],
644 0, 0, 1);
645
646 return skipRow;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000647}
648
Christopher Dalton7f5af0c2017-06-06 14:26:27 -0600649static void calc_serp_klm(const SkPoint pts[4], SkScalar tl, SkScalar sl, SkScalar tm, SkScalar sm,
650 SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600651 SkMatrix CIT;
652 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000653
csmartdaltoncc261272017-03-23 13:38:45 -0600654 SkMatrix klmCoeffs;
655 int col = 0;
656 if (0 != skipCol) {
657 klmCoeffs[0] = 0;
658 klmCoeffs[3] = -sl * sl * sl;
659 klmCoeffs[6] = -sm * sm * sm;
660 ++col;
661 }
662 if (1 != skipCol) {
663 klmCoeffs[col + 0] = sl * sm;
664 klmCoeffs[col + 3] = 3 * sl * sl * tl;
665 klmCoeffs[col + 6] = 3 * sm * sm * tm;
666 ++col;
667 }
668 if (2 != skipCol) {
669 klmCoeffs[col + 0] = -tl * sm - tm * sl;
670 klmCoeffs[col + 3] = -3 * sl * tl * tl;
671 klmCoeffs[col + 6] = -3 * sm * tm * tm;
672 ++col;
673 }
674
675 SkASSERT(2 == col);
676 klmCoeffs[2] = tl * tm;
677 klmCoeffs[5] = tl * tl * tl;
678 klmCoeffs[8] = tm * tm * tm;
679
680 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000681}
682
Christopher Dalton7f5af0c2017-06-06 14:26:27 -0600683static void calc_loop_klm(const SkPoint pts[4], SkScalar td, SkScalar sd, SkScalar te, SkScalar se,
684 SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600685 SkMatrix CIT;
686 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000687
csmartdaltoncc261272017-03-23 13:38:45 -0600688 const SkScalar tdse = td * se;
Chris Daltonfebbffa2017-06-08 13:12:02 -0600689 const SkScalar tesd = te * sd;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000690
csmartdaltoncc261272017-03-23 13:38:45 -0600691 SkMatrix klmCoeffs;
692 int col = 0;
693 if (0 != skipCol) {
694 klmCoeffs[0] = 0;
695 klmCoeffs[3] = -sd * sd * se;
696 klmCoeffs[6] = -se * se * sd;
697 ++col;
698 }
699 if (1 != skipCol) {
700 klmCoeffs[col + 0] = sd * se;
701 klmCoeffs[col + 3] = sd * (2 * tdse + tesd);
702 klmCoeffs[col + 6] = se * (2 * tesd + tdse);
703 ++col;
704 }
705 if (2 != skipCol) {
706 klmCoeffs[col + 0] = -tdse - tesd;
707 klmCoeffs[col + 3] = -td * (tdse + 2 * tesd);
708 klmCoeffs[col + 6] = -te * (tesd + 2 * tdse);
709 ++col;
710 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000711
csmartdaltoncc261272017-03-23 13:38:45 -0600712 SkASSERT(2 == col);
713 klmCoeffs[2] = td * te;
714 klmCoeffs[5] = td * td * te;
715 klmCoeffs[8] = te * te * td;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000716
csmartdaltoncc261272017-03-23 13:38:45 -0600717 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000718}
719
csmartdaltoncc261272017-03-23 13:38:45 -0600720// For the case when we have a cusp at a parameter value of infinity (discr == 0, d1 == 0).
Christopher Dalton7f5af0c2017-06-06 14:26:27 -0600721static void calc_inf_cusp_klm(const SkPoint pts[4], SkScalar tn, SkScalar sn, SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600722 SkMatrix CIT;
723 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000724
csmartdaltoncc261272017-03-23 13:38:45 -0600725 SkMatrix klmCoeffs;
726 int col = 0;
727 if (0 != skipCol) {
728 klmCoeffs[0] = 0;
729 klmCoeffs[3] = -sn * sn * sn;
730 ++col;
731 }
732 if (1 != skipCol) {
733 klmCoeffs[col + 0] = 0;
734 klmCoeffs[col + 3] = 3 * sn * sn * tn;
735 ++col;
736 }
737 if (2 != skipCol) {
738 klmCoeffs[col + 0] = -sn;
739 klmCoeffs[col + 3] = -3 * sn * tn * tn;
740 ++col;
741 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000742
csmartdaltoncc261272017-03-23 13:38:45 -0600743 SkASSERT(2 == col);
744 klmCoeffs[2] = tn;
745 klmCoeffs[5] = tn * tn * tn;
746
747 klmCoeffs[6] = 0;
748 klmCoeffs[7] = 0;
749 klmCoeffs[8] = 1;
750
751 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000752}
753
csmartdaltoncc261272017-03-23 13:38:45 -0600754// For the case when a cubic bezier is actually a quadratic. We duplicate k in l so that the
755// implicit becomes:
756//
757// k^3 - l*m == k^3 - l*k == k * (k^2 - l)
758//
759// In the quadratic case we can simply assign fixed values at each control point:
760//
761// | ..K.. | | pts[0] pts[1] pts[2] pts[3] | | 0 1/3 2/3 1 |
762// | ..L.. | * | . . . . | == | 0 0 1/3 1 |
763// | ..K.. | | 1 1 1 1 | | 0 1/3 2/3 1 |
764//
Chris Dalton390f6cd2017-06-12 11:22:54 -0600765static void calc_quadratic_klm(const SkPoint pts[4], double d3, SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600766 SkMatrix klmAtPts;
767 klmAtPts.setAll(0, 1.f/3, 1,
768 0, 0, 1,
769 0, 1.f/3, 1);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000770
csmartdaltoncc261272017-03-23 13:38:45 -0600771 SkMatrix inversePts;
772 inversePts.setAll(pts[0].x(), pts[1].x(), pts[3].x(),
773 pts[0].y(), pts[1].y(), pts[3].y(),
774 1, 1, 1);
775 SkAssertResult(inversePts.invert(&inversePts));
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000776
csmartdaltoncc261272017-03-23 13:38:45 -0600777 klm->setConcat(klmAtPts, inversePts);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000778
csmartdaltoncc261272017-03-23 13:38:45 -0600779 // If d3 > 0 we need to flip the orientation of our curve
780 // This is done by negating the k and l values
781 if (d3 > 0) {
Christopher Dalton7f5af0c2017-06-06 14:26:27 -0600782 klm->postScale(-1, -1);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000783 }
784}
785
csmartdaltoncc261272017-03-23 13:38:45 -0600786// For the case when a cubic bezier is actually a line. We set K=0, L=1, M=-line, which results in
787// the following implicit:
788//
789// k^3 - l*m == 0^3 - 1*(-line) == -(-line) == line
790//
791static void calc_line_klm(const SkPoint pts[4], SkMatrix* klm) {
792 SkScalar ny = pts[0].x() - pts[3].x();
793 SkScalar nx = pts[3].y() - pts[0].y();
794 SkScalar k = nx * pts[0].x() + ny * pts[0].y();
795 klm->setAll( 0, 0, 0,
796 0, 0, 1,
797 -nx, -ny, k);
798}
799
Chris Dalton390f6cd2017-06-12 11:22:54 -0600800SkCubicType GrPathUtils::getCubicKLM(const SkPoint src[4], SkMatrix* klm, double t[2],
801 double s[2]) {
802 double d[4];
Chris Daltonfebbffa2017-06-08 13:12:02 -0600803 SkCubicType type = SkClassifyCubic(src, t, s, d);
Chris Dalton390f6cd2017-06-12 11:22:54 -0600804
805 const SkScalar tt[2] = {static_cast<SkScalar>(t[0]), static_cast<SkScalar>(t[1])};
806 const SkScalar ss[2] = {static_cast<SkScalar>(s[0]), static_cast<SkScalar>(s[1])};
807
Chris Daltonfebbffa2017-06-08 13:12:02 -0600808 switch (type) {
809 case SkCubicType::kSerpentine:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600810 calc_serp_klm(src, tt[0], ss[0], tt[1], ss[1], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600811 break;
812 case SkCubicType::kLoop:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600813 calc_loop_klm(src, tt[0], ss[0], tt[1], ss[1], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600814 break;
815 case SkCubicType::kLocalCusp:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600816 calc_serp_klm(src, tt[0], ss[0], tt[1], ss[1], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600817 break;
818 case SkCubicType::kCuspAtInfinity:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600819 calc_inf_cusp_klm(src, tt[0], ss[0], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600820 break;
821 case SkCubicType::kQuadratic:
822 calc_quadratic_klm(src, d[3], klm);
823 break;
824 case SkCubicType::kLineOrPoint:
825 calc_line_klm(src, klm);
826 break;
827 }
828
829 return type;
830}
831
csmartdaltoncc261272017-03-23 13:38:45 -0600832int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkMatrix* klm,
Greg Daniel8199d942017-03-14 10:20:24 -0400833 int* loopIndex) {
Chris Daltonfebbffa2017-06-08 13:12:02 -0600834 SkSTArray<2, SkScalar> chops;
835 *loopIndex = -1;
csmartdaltoncc261272017-03-23 13:38:45 -0600836
Chris Dalton390f6cd2017-06-12 11:22:54 -0600837 double t[2], s[2];
Chris Daltonfebbffa2017-06-08 13:12:02 -0600838 if (SkCubicType::kLoop == GrPathUtils::getCubicKLM(src, klm, t, s)) {
Chris Dalton390f6cd2017-06-12 11:22:54 -0600839 SkScalar t0 = static_cast<SkScalar>(t[0] / s[0]);
840 SkScalar t1 = static_cast<SkScalar>(t[1] / s[1]);
841 SkASSERT(t0 <= t1); // Technically t0 != t1 in a loop, but there may be FP error.
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000842
Chris Dalton390f6cd2017-06-12 11:22:54 -0600843 if (t0 < 1 && t1 > 0) {
Chris Daltonb58194a2017-06-09 12:51:56 -0600844 *loopIndex = 0;
Chris Dalton390f6cd2017-06-12 11:22:54 -0600845 if (t0 > 0) {
846 chops.push_back(t0);
Chris Daltonb58194a2017-06-09 12:51:56 -0600847 *loopIndex = 1;
848 }
Chris Dalton390f6cd2017-06-12 11:22:54 -0600849 if (t1 < 1) {
850 chops.push_back(t1);
Chris Daltonb58194a2017-06-09 12:51:56 -0600851 *loopIndex = chops.count() - 1;
852 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000853 }
Greg Daniel8199d942017-03-14 10:20:24 -0400854 }
csmartdaltoncc261272017-03-23 13:38:45 -0600855
Chris Daltonfebbffa2017-06-08 13:12:02 -0600856 SkChopCubicAt(src, dst, chops.begin(), chops.count());
857 return chops.count() + 1;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000858}