blob: 9a79f2061ecd639384781006ac1afbcdeb690fba [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
Chris Daltonb072bb62017-08-07 09:00:46 -0600570static inline Sk2f normalize(const Sk2f& n) {
571 Sk2f nn = n*n;
572 return n * (nn + SkNx_shuffle<1,0>(nn)).rsqrt();
573}
574
575bool GrPathUtils::chopMonotonicQuads(const SkPoint p[3], SkPoint dst[5]) {
576 GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT);
577 GR_STATIC_ASSERT(2 * sizeof(float) == sizeof(SkPoint));
578 GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX));
579
580 Sk2f p0 = Sk2f::Load(&p[0]);
581 Sk2f p1 = Sk2f::Load(&p[1]);
582 Sk2f p2 = Sk2f::Load(&p[2]);
583
584 Sk2f tan0 = p1 - p0;
585 Sk2f tan1 = p2 - p1;
586 Sk2f v = p2 - p0;
587
588 // Check if the curve is already monotonic (i.e. (tan0 dot v) >= 0 and (tan1 dot v) >= 0).
589 // This should almost always be this case for well-behaved curves in the real world.
590 float dot0[2], dot1[2];
591 (tan0 * v).store(dot0);
592 (tan1 * v).store(dot1);
593 if (dot0[0] + dot0[1] >= 0 && dot1[0] + dot1[1] >= 0) {
594 return false;
595 }
596
597 // Chop the curve into two segments with equal curvature. To do this we find the T value whose
598 // tangent is perpendicular to the vector that bisects tan0 and -tan1.
599 Sk2f n = normalize(tan0) - normalize(tan1);
600
601 // This tangent can be found where (dQ(t) dot n) = 0:
602 //
603 // 0 = (dQ(t) dot n) = | 2*t 1 | * | p0 - 2*p1 + p2 | * | n |
604 // | -2*p0 + 2*p1 | | . |
605 //
606 // = | 2*t 1 | * | tan1 - tan0 | * | n |
607 // | 2*tan0 | | . |
608 //
609 // = 2*t * ((tan1 - tan0) dot n) + (2*tan0 dot n)
610 //
611 // t = (tan0 dot n) / ((tan0 - tan1) dot n)
612 Sk2f dQ1n = (tan0 - tan1) * n;
613 Sk2f dQ0n = tan0 * n;
614 Sk2f t = (dQ0n + SkNx_shuffle<1,0>(dQ0n)) / (dQ1n + SkNx_shuffle<1,0>(dQ1n));
615 t = Sk2f::Min(Sk2f::Max(t, 0), 1); // Clamp for FP error.
616
617 Sk2f p01 = SkNx_fma(t, tan0, p0);
618 Sk2f p12 = SkNx_fma(t, tan1, p1);
619 Sk2f p012 = SkNx_fma(t, p12 - p01, p01);
620
621 p0.store(&dst[0]);
622 p01.store(&dst[1]);
623 p012.store(&dst[2]);
624 p12.store(&dst[3]);
625 p2.store(&dst[4]);
626
627 return true;
628}
629
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000630////////////////////////////////////////////////////////////////////////////////
631
csmartdaltoncc261272017-03-23 13:38:45 -0600632/**
633 * Computes an SkMatrix that can find the cubic KLM functionals as follows:
634 *
635 * | ..K.. | | ..kcoeffs.. |
636 * | ..L.. | = | ..lcoeffs.. | * inverse_transpose_power_basis_matrix
637 * | ..M.. | | ..mcoeffs.. |
638 *
639 * 'kcoeffs' are the power basis coefficients to a scalar valued cubic function that returns the
640 * signed distance to line K from a given point on the curve:
641 *
642 * k(t,s) = C(t,s) * K [C(t,s) is defined in the following comment]
643 *
644 * The same applies for lcoeffs and mcoeffs. These are found separately, depending on the type of
645 * curve. There are 4 coefficients but 3 rows in the matrix, so in order to do this calculation the
646 * caller must first remove a specific column of coefficients.
647 *
648 * @return which column of klm coefficients to exclude from the calculation.
649 */
650static int calc_inverse_transpose_power_basis_matrix(const SkPoint pts[4], SkMatrix* out) {
651 using SkScalar4 = SkNx<4, SkScalar>;
652
653 // First we convert the bezier coordinates 'pts' to power basis coefficients X,Y,W=[0 0 0 1].
654 // M3 is the matrix that does this conversion. The homogeneous equation for the cubic becomes:
655 //
656 // | X Y 0 |
657 // C(t,s) = [t^3 t^2*s t*s^2 s^3] * | . . 0 |
658 // | . . 0 |
659 // | . . 1 |
660 //
661 const SkScalar4 M3[3] = {SkScalar4(-1, 3, -3, 1),
662 SkScalar4(3, -6, 3, 0),
663 SkScalar4(-3, 3, 0, 0)};
664 // 4th column of M3 = SkScalar4(1, 0, 0, 0)};
665 SkScalar4 X(pts[3].x(), 0, 0, 0);
666 SkScalar4 Y(pts[3].y(), 0, 0, 0);
667 for (int i = 2; i >= 0; --i) {
668 X += M3[i] * pts[i].x();
669 Y += M3[i] * pts[i].y();
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000670 }
671
csmartdaltoncc261272017-03-23 13:38:45 -0600672 // 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 -0600673 // of the top three rows. We toss the row that leaves us with the largest absolute determinant.
674 // Since the right column will be [0 0 1], the determinant reduces to x0*y1 - y0*x1.
675 SkScalar absDet[4];
676 const SkScalar4 DETX1 = SkNx_shuffle<1,0,0,3>(X), DETY1 = SkNx_shuffle<1,0,0,3>(Y);
677 const SkScalar4 DETX2 = SkNx_shuffle<2,2,1,3>(X), DETY2 = SkNx_shuffle<2,2,1,3>(Y);
678 const SkScalar4 DET = DETX1 * DETY2 - DETY1 * DETX2;
679 DET.abs().store(absDet);
680 const int skipRow = absDet[0] > absDet[2] ? (absDet[0] > absDet[1] ? 0 : 1)
681 : (absDet[1] > absDet[2] ? 1 : 2);
682 const SkScalar rdet = 1 / DET[skipRow];
csmartdaltoncc261272017-03-23 13:38:45 -0600683 const int row0 = (0 != skipRow) ? 0 : 1;
684 const int row1 = (2 == skipRow) ? 1 : 2;
685
686 // Compute the inverse-transpose of the power basis matrix with the 'skipRow'th row removed.
687 // Since W=[0 0 0 1], it follows that our corresponding solution will be equal to:
688 //
689 // | y1 -x1 x1*y2 - y1*x2 |
690 // 1/det * | -y0 x0 -x0*y2 + y0*x2 |
691 // | 0 0 det |
692 //
693 const SkScalar4 R(rdet, rdet, rdet, 1);
694 X *= R;
695 Y *= R;
696
697 SkScalar x[4], y[4], z[4];
698 X.store(x);
699 Y.store(y);
700 (X * SkNx_shuffle<3,3,3,3>(Y) - Y * SkNx_shuffle<3,3,3,3>(X)).store(z);
701
702 out->setAll( y[row1], -x[row1], z[row1],
703 -y[row0], x[row0], -z[row0],
704 0, 0, 1);
705
706 return skipRow;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000707}
708
Christopher Dalton7f5af0c2017-06-06 14:26:27 -0600709static void calc_serp_klm(const SkPoint pts[4], SkScalar tl, SkScalar sl, SkScalar tm, SkScalar sm,
710 SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600711 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 SkMatrix klmCoeffs;
715 int col = 0;
716 if (0 != skipCol) {
717 klmCoeffs[0] = 0;
718 klmCoeffs[3] = -sl * sl * sl;
719 klmCoeffs[6] = -sm * sm * sm;
720 ++col;
721 }
722 if (1 != skipCol) {
723 klmCoeffs[col + 0] = sl * sm;
724 klmCoeffs[col + 3] = 3 * sl * sl * tl;
725 klmCoeffs[col + 6] = 3 * sm * sm * tm;
726 ++col;
727 }
728 if (2 != skipCol) {
729 klmCoeffs[col + 0] = -tl * sm - tm * sl;
730 klmCoeffs[col + 3] = -3 * sl * tl * tl;
731 klmCoeffs[col + 6] = -3 * sm * tm * tm;
732 ++col;
733 }
734
735 SkASSERT(2 == col);
736 klmCoeffs[2] = tl * tm;
737 klmCoeffs[5] = tl * tl * tl;
738 klmCoeffs[8] = tm * tm * tm;
739
740 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000741}
742
Christopher Dalton7f5af0c2017-06-06 14:26:27 -0600743static void calc_loop_klm(const SkPoint pts[4], SkScalar td, SkScalar sd, SkScalar te, SkScalar se,
744 SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600745 SkMatrix CIT;
746 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000747
csmartdaltoncc261272017-03-23 13:38:45 -0600748 const SkScalar tdse = td * se;
Chris Daltonfebbffa2017-06-08 13:12:02 -0600749 const SkScalar tesd = te * sd;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000750
csmartdaltoncc261272017-03-23 13:38:45 -0600751 SkMatrix klmCoeffs;
752 int col = 0;
753 if (0 != skipCol) {
754 klmCoeffs[0] = 0;
755 klmCoeffs[3] = -sd * sd * se;
756 klmCoeffs[6] = -se * se * sd;
757 ++col;
758 }
759 if (1 != skipCol) {
760 klmCoeffs[col + 0] = sd * se;
761 klmCoeffs[col + 3] = sd * (2 * tdse + tesd);
762 klmCoeffs[col + 6] = se * (2 * tesd + tdse);
763 ++col;
764 }
765 if (2 != skipCol) {
766 klmCoeffs[col + 0] = -tdse - tesd;
767 klmCoeffs[col + 3] = -td * (tdse + 2 * tesd);
768 klmCoeffs[col + 6] = -te * (tesd + 2 * tdse);
769 ++col;
770 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000771
csmartdaltoncc261272017-03-23 13:38:45 -0600772 SkASSERT(2 == col);
773 klmCoeffs[2] = td * te;
774 klmCoeffs[5] = td * td * te;
775 klmCoeffs[8] = te * te * td;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000776
csmartdaltoncc261272017-03-23 13:38:45 -0600777 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000778}
779
csmartdaltoncc261272017-03-23 13:38:45 -0600780// 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 -0600781static void calc_inf_cusp_klm(const SkPoint pts[4], SkScalar tn, SkScalar sn, SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600782 SkMatrix CIT;
783 int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000784
csmartdaltoncc261272017-03-23 13:38:45 -0600785 SkMatrix klmCoeffs;
786 int col = 0;
787 if (0 != skipCol) {
788 klmCoeffs[0] = 0;
789 klmCoeffs[3] = -sn * sn * sn;
790 ++col;
791 }
792 if (1 != skipCol) {
793 klmCoeffs[col + 0] = 0;
794 klmCoeffs[col + 3] = 3 * sn * sn * tn;
795 ++col;
796 }
797 if (2 != skipCol) {
798 klmCoeffs[col + 0] = -sn;
799 klmCoeffs[col + 3] = -3 * sn * tn * tn;
800 ++col;
801 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000802
csmartdaltoncc261272017-03-23 13:38:45 -0600803 SkASSERT(2 == col);
804 klmCoeffs[2] = tn;
805 klmCoeffs[5] = tn * tn * tn;
806
807 klmCoeffs[6] = 0;
808 klmCoeffs[7] = 0;
809 klmCoeffs[8] = 1;
810
811 klm->setConcat(klmCoeffs, CIT);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000812}
813
csmartdaltoncc261272017-03-23 13:38:45 -0600814// For the case when a cubic bezier is actually a quadratic. We duplicate k in l so that the
815// implicit becomes:
816//
817// k^3 - l*m == k^3 - l*k == k * (k^2 - l)
818//
819// In the quadratic case we can simply assign fixed values at each control point:
820//
821// | ..K.. | | pts[0] pts[1] pts[2] pts[3] | | 0 1/3 2/3 1 |
822// | ..L.. | * | . . . . | == | 0 0 1/3 1 |
823// | ..K.. | | 1 1 1 1 | | 0 1/3 2/3 1 |
824//
Chris Dalton390f6cd2017-06-12 11:22:54 -0600825static void calc_quadratic_klm(const SkPoint pts[4], double d3, SkMatrix* klm) {
csmartdaltoncc261272017-03-23 13:38:45 -0600826 SkMatrix klmAtPts;
827 klmAtPts.setAll(0, 1.f/3, 1,
828 0, 0, 1,
829 0, 1.f/3, 1);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000830
csmartdaltoncc261272017-03-23 13:38:45 -0600831 SkMatrix inversePts;
832 inversePts.setAll(pts[0].x(), pts[1].x(), pts[3].x(),
833 pts[0].y(), pts[1].y(), pts[3].y(),
834 1, 1, 1);
835 SkAssertResult(inversePts.invert(&inversePts));
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000836
csmartdaltoncc261272017-03-23 13:38:45 -0600837 klm->setConcat(klmAtPts, inversePts);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000838
csmartdaltoncc261272017-03-23 13:38:45 -0600839 // If d3 > 0 we need to flip the orientation of our curve
840 // This is done by negating the k and l values
841 if (d3 > 0) {
Christopher Dalton7f5af0c2017-06-06 14:26:27 -0600842 klm->postScale(-1, -1);
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000843 }
844}
845
csmartdaltoncc261272017-03-23 13:38:45 -0600846// For the case when a cubic bezier is actually a line. We set K=0, L=1, M=-line, which results in
847// the following implicit:
848//
849// k^3 - l*m == 0^3 - 1*(-line) == -(-line) == line
850//
851static void calc_line_klm(const SkPoint pts[4], SkMatrix* klm) {
852 SkScalar ny = pts[0].x() - pts[3].x();
853 SkScalar nx = pts[3].y() - pts[0].y();
854 SkScalar k = nx * pts[0].x() + ny * pts[0].y();
855 klm->setAll( 0, 0, 0,
856 0, 0, 1,
857 -nx, -ny, k);
858}
859
Chris Dalton390f6cd2017-06-12 11:22:54 -0600860SkCubicType GrPathUtils::getCubicKLM(const SkPoint src[4], SkMatrix* klm, double t[2],
861 double s[2]) {
862 double d[4];
Chris Daltonfebbffa2017-06-08 13:12:02 -0600863 SkCubicType type = SkClassifyCubic(src, t, s, d);
Chris Dalton390f6cd2017-06-12 11:22:54 -0600864
865 const SkScalar tt[2] = {static_cast<SkScalar>(t[0]), static_cast<SkScalar>(t[1])};
866 const SkScalar ss[2] = {static_cast<SkScalar>(s[0]), static_cast<SkScalar>(s[1])};
867
Chris Daltonfebbffa2017-06-08 13:12:02 -0600868 switch (type) {
869 case SkCubicType::kSerpentine:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600870 calc_serp_klm(src, tt[0], ss[0], tt[1], ss[1], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600871 break;
872 case SkCubicType::kLoop:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600873 calc_loop_klm(src, tt[0], ss[0], tt[1], ss[1], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600874 break;
875 case SkCubicType::kLocalCusp:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600876 calc_serp_klm(src, tt[0], ss[0], tt[1], ss[1], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600877 break;
878 case SkCubicType::kCuspAtInfinity:
Chris Dalton390f6cd2017-06-12 11:22:54 -0600879 calc_inf_cusp_klm(src, tt[0], ss[0], klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600880 break;
881 case SkCubicType::kQuadratic:
882 calc_quadratic_klm(src, d[3], klm);
883 break;
884 case SkCubicType::kLineOrPoint:
885 calc_line_klm(src, klm);
886 break;
887 }
888
889 return type;
890}
891
csmartdaltoncc261272017-03-23 13:38:45 -0600892int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkMatrix* klm,
Greg Daniel8199d942017-03-14 10:20:24 -0400893 int* loopIndex) {
Chris Daltonfebbffa2017-06-08 13:12:02 -0600894 SkSTArray<2, SkScalar> chops;
895 *loopIndex = -1;
csmartdaltoncc261272017-03-23 13:38:45 -0600896
Chris Dalton390f6cd2017-06-12 11:22:54 -0600897 double t[2], s[2];
Chris Daltonfebbffa2017-06-08 13:12:02 -0600898 if (SkCubicType::kLoop == GrPathUtils::getCubicKLM(src, klm, t, s)) {
Chris Dalton390f6cd2017-06-12 11:22:54 -0600899 SkScalar t0 = static_cast<SkScalar>(t[0] / s[0]);
900 SkScalar t1 = static_cast<SkScalar>(t[1] / s[1]);
901 SkASSERT(t0 <= t1); // Technically t0 != t1 in a loop, but there may be FP error.
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000902
Chris Dalton390f6cd2017-06-12 11:22:54 -0600903 if (t0 < 1 && t1 > 0) {
Chris Daltonb58194a2017-06-09 12:51:56 -0600904 *loopIndex = 0;
Chris Dalton390f6cd2017-06-12 11:22:54 -0600905 if (t0 > 0) {
906 chops.push_back(t0);
Chris Daltonb58194a2017-06-09 12:51:56 -0600907 *loopIndex = 1;
908 }
Chris Dalton390f6cd2017-06-12 11:22:54 -0600909 if (t1 < 1) {
910 chops.push_back(t1);
Chris Daltonb58194a2017-06-09 12:51:56 -0600911 *loopIndex = chops.count() - 1;
912 }
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000913 }
Greg Daniel8199d942017-03-14 10:20:24 -0400914 }
csmartdaltoncc261272017-03-23 13:38:45 -0600915
Chris Daltonfebbffa2017-06-08 13:12:02 -0600916 SkChopCubicAt(src, dst, chops.begin(), chops.count());
917 return chops.count() + 1;
commit-bot@chromium.org858638d2013-08-20 14:45:45 +0000918}