|
1 /* |
|
2 * 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. |
|
6 */ |
|
7 |
|
8 #include "GrPathUtils.h" |
|
9 |
|
10 #include "GrPoint.h" |
|
11 #include "SkGeometry.h" |
|
12 |
|
13 SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol, |
|
14 const SkMatrix& viewM, |
|
15 const SkRect& pathBounds) { |
|
16 // In order to tesselate the path we get a bound on how much the matrix can |
|
17 // stretch when mapping to screen coordinates. |
|
18 SkScalar stretch = viewM.getMaxStretch(); |
|
19 SkScalar srcTol = devTol; |
|
20 |
|
21 if (stretch < 0) { |
|
22 // take worst case mapRadius amoung four corners. |
|
23 // (less than perfect) |
|
24 for (int i = 0; i < 4; ++i) { |
|
25 SkMatrix mat; |
|
26 mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight, |
|
27 (i < 2) ? pathBounds.fTop : pathBounds.fBottom); |
|
28 mat.postConcat(viewM); |
|
29 stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1)); |
|
30 } |
|
31 } |
|
32 srcTol = SkScalarDiv(srcTol, stretch); |
|
33 return srcTol; |
|
34 } |
|
35 |
|
36 static const int MAX_POINTS_PER_CURVE = 1 << 10; |
|
37 static const SkScalar gMinCurveTol = 0.0001f; |
|
38 |
|
39 uint32_t GrPathUtils::quadraticPointCount(const GrPoint points[], |
|
40 SkScalar tol) { |
|
41 if (tol < gMinCurveTol) { |
|
42 tol = gMinCurveTol; |
|
43 } |
|
44 SkASSERT(tol > 0); |
|
45 |
|
46 SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]); |
|
47 if (d <= tol) { |
|
48 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); |
|
54 int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol))); |
|
55 int pow2 = GrNextPow2(temp); |
|
56 // Because of NaNs & INFs we can wind up with a degenerate temp |
|
57 // such that pow2 comes out negative. Also, our point generator |
|
58 // will always output at least one pt. |
|
59 if (pow2 < 1) { |
|
60 pow2 = 1; |
|
61 } |
|
62 return GrMin(pow2, MAX_POINTS_PER_CURVE); |
|
63 } |
|
64 } |
|
65 |
|
66 uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0, |
|
67 const GrPoint& p1, |
|
68 const GrPoint& p2, |
|
69 SkScalar tolSqd, |
|
70 GrPoint** points, |
|
71 uint32_t pointsLeft) { |
|
72 if (pointsLeft < 2 || |
|
73 (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) { |
|
74 (*points)[0] = p2; |
|
75 *points += 1; |
|
76 return 1; |
|
77 } |
|
78 |
|
79 GrPoint q[] = { |
|
80 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
|
81 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
|
82 }; |
|
83 GrPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; |
|
84 |
|
85 pointsLeft >>= 1; |
|
86 uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft); |
|
87 uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft); |
|
88 return a + b; |
|
89 } |
|
90 |
|
91 uint32_t GrPathUtils::cubicPointCount(const GrPoint points[], |
|
92 SkScalar tol) { |
|
93 if (tol < gMinCurveTol) { |
|
94 tol = gMinCurveTol; |
|
95 } |
|
96 SkASSERT(tol > 0); |
|
97 |
|
98 SkScalar d = GrMax( |
|
99 points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]), |
|
100 points[2].distanceToLineSegmentBetweenSqd(points[0], points[3])); |
|
101 d = SkScalarSqrt(d); |
|
102 if (d <= tol) { |
|
103 return 1; |
|
104 } else { |
|
105 int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol))); |
|
106 int pow2 = GrNextPow2(temp); |
|
107 // Because of NaNs & INFs we can wind up with a degenerate temp |
|
108 // such that pow2 comes out negative. Also, our point generator |
|
109 // will always output at least one pt. |
|
110 if (pow2 < 1) { |
|
111 pow2 = 1; |
|
112 } |
|
113 return GrMin(pow2, MAX_POINTS_PER_CURVE); |
|
114 } |
|
115 } |
|
116 |
|
117 uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0, |
|
118 const GrPoint& p1, |
|
119 const GrPoint& p2, |
|
120 const GrPoint& p3, |
|
121 SkScalar tolSqd, |
|
122 GrPoint** points, |
|
123 uint32_t pointsLeft) { |
|
124 if (pointsLeft < 2 || |
|
125 (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd && |
|
126 p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) { |
|
127 (*points)[0] = p3; |
|
128 *points += 1; |
|
129 return 1; |
|
130 } |
|
131 GrPoint q[] = { |
|
132 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
|
133 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
|
134 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
|
135 }; |
|
136 GrPoint r[] = { |
|
137 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
|
138 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } |
|
139 }; |
|
140 GrPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
|
141 pointsLeft >>= 1; |
|
142 uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft); |
|
143 uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft); |
|
144 return a + b; |
|
145 } |
|
146 |
|
147 int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, |
|
148 SkScalar tol) { |
|
149 if (tol < gMinCurveTol) { |
|
150 tol = gMinCurveTol; |
|
151 } |
|
152 SkASSERT(tol > 0); |
|
153 |
|
154 int pointCount = 0; |
|
155 *subpaths = 1; |
|
156 |
|
157 bool first = true; |
|
158 |
|
159 SkPath::Iter iter(path, false); |
|
160 SkPath::Verb verb; |
|
161 |
|
162 GrPoint pts[4]; |
|
163 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
|
164 |
|
165 switch (verb) { |
|
166 case SkPath::kLine_Verb: |
|
167 pointCount += 1; |
|
168 break; |
|
169 case SkPath::kQuad_Verb: |
|
170 pointCount += quadraticPointCount(pts, tol); |
|
171 break; |
|
172 case SkPath::kCubic_Verb: |
|
173 pointCount += cubicPointCount(pts, tol); |
|
174 break; |
|
175 case SkPath::kMove_Verb: |
|
176 pointCount += 1; |
|
177 if (!first) { |
|
178 ++(*subpaths); |
|
179 } |
|
180 break; |
|
181 default: |
|
182 break; |
|
183 } |
|
184 first = false; |
|
185 } |
|
186 return pointCount; |
|
187 } |
|
188 |
|
189 void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) { |
|
190 SkMatrix m; |
|
191 // We want M such that M * xy_pt = uv_pt |
|
192 // We know M * control_pts = [0 1/2 1] |
|
193 // [0 0 1] |
|
194 // [1 1 1] |
|
195 // And control_pts = [x0 x1 x2] |
|
196 // [y0 y1 y2] |
|
197 // [1 1 1 ] |
|
198 // We invert the control pt matrix and post concat to both sides to get M. |
|
199 // Using the known form of the control point matrix and the result, we can |
|
200 // optimize and improve precision. |
|
201 |
|
202 double x0 = qPts[0].fX; |
|
203 double y0 = qPts[0].fY; |
|
204 double x1 = qPts[1].fX; |
|
205 double y1 = qPts[1].fY; |
|
206 double x2 = qPts[2].fX; |
|
207 double y2 = qPts[2].fY; |
|
208 double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2; |
|
209 |
|
210 if (!sk_float_isfinite(det) |
|
211 || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { |
|
212 // The quad is degenerate. Hopefully this is rare. Find the pts that are |
|
213 // farthest apart to compute a line (unless it is really a pt). |
|
214 SkScalar maxD = qPts[0].distanceToSqd(qPts[1]); |
|
215 int maxEdge = 0; |
|
216 SkScalar d = qPts[1].distanceToSqd(qPts[2]); |
|
217 if (d > maxD) { |
|
218 maxD = d; |
|
219 maxEdge = 1; |
|
220 } |
|
221 d = qPts[2].distanceToSqd(qPts[0]); |
|
222 if (d > maxD) { |
|
223 maxD = d; |
|
224 maxEdge = 2; |
|
225 } |
|
226 // We could have a tolerance here, not sure if it would improve anything |
|
227 if (maxD > 0) { |
|
228 // Set the matrix to give (u = 0, v = distance_to_line) |
|
229 GrVec lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge]; |
|
230 // when looking from the point 0 down the line we want positive |
|
231 // distances to be to the left. This matches the non-degenerate |
|
232 // case. |
|
233 lineVec.setOrthog(lineVec, GrPoint::kLeft_Side); |
|
234 lineVec.dot(qPts[0]); |
|
235 // first row |
|
236 fM[0] = 0; |
|
237 fM[1] = 0; |
|
238 fM[2] = 0; |
|
239 // second row |
|
240 fM[3] = lineVec.fX; |
|
241 fM[4] = lineVec.fY; |
|
242 fM[5] = -lineVec.dot(qPts[maxEdge]); |
|
243 } else { |
|
244 // It's a point. It should cover zero area. Just set the matrix such |
|
245 // that (u, v) will always be far away from the quad. |
|
246 fM[0] = 0; fM[1] = 0; fM[2] = 100.f; |
|
247 fM[3] = 0; fM[4] = 0; fM[5] = 100.f; |
|
248 } |
|
249 } else { |
|
250 double scale = 1.0/det; |
|
251 |
|
252 // compute adjugate matrix |
|
253 double a0, a1, a2, a3, a4, a5, a6, a7, a8; |
|
254 a0 = y1-y2; |
|
255 a1 = x2-x1; |
|
256 a2 = x1*y2-x2*y1; |
|
257 |
|
258 a3 = y2-y0; |
|
259 a4 = x0-x2; |
|
260 a5 = x2*y0-x0*y2; |
|
261 |
|
262 a6 = y0-y1; |
|
263 a7 = x1-x0; |
|
264 a8 = x0*y1-x1*y0; |
|
265 |
|
266 // this performs the uv_pts*adjugate(control_pts) multiply, |
|
267 // then does the scale by 1/det afterwards to improve precision |
|
268 m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale); |
|
269 m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale); |
|
270 m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale); |
|
271 |
|
272 m[SkMatrix::kMSkewY] = (float)(a6*scale); |
|
273 m[SkMatrix::kMScaleY] = (float)(a7*scale); |
|
274 m[SkMatrix::kMTransY] = (float)(a8*scale); |
|
275 |
|
276 m[SkMatrix::kMPersp0] = (float)((a0 + a3 + a6)*scale); |
|
277 m[SkMatrix::kMPersp1] = (float)((a1 + a4 + a7)*scale); |
|
278 m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale); |
|
279 |
|
280 // The matrix should not have perspective. |
|
281 SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f); |
|
282 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL); |
|
283 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL); |
|
284 |
|
285 // It may not be normalized to have 1.0 in the bottom right |
|
286 float m33 = m.get(SkMatrix::kMPersp2); |
|
287 if (1.f != m33) { |
|
288 m33 = 1.f / m33; |
|
289 fM[0] = m33 * m.get(SkMatrix::kMScaleX); |
|
290 fM[1] = m33 * m.get(SkMatrix::kMSkewX); |
|
291 fM[2] = m33 * m.get(SkMatrix::kMTransX); |
|
292 fM[3] = m33 * m.get(SkMatrix::kMSkewY); |
|
293 fM[4] = m33 * m.get(SkMatrix::kMScaleY); |
|
294 fM[5] = m33 * m.get(SkMatrix::kMTransY); |
|
295 } else { |
|
296 fM[0] = m.get(SkMatrix::kMScaleX); |
|
297 fM[1] = m.get(SkMatrix::kMSkewX); |
|
298 fM[2] = m.get(SkMatrix::kMTransX); |
|
299 fM[3] = m.get(SkMatrix::kMSkewY); |
|
300 fM[4] = m.get(SkMatrix::kMScaleY); |
|
301 fM[5] = m.get(SkMatrix::kMTransY); |
|
302 } |
|
303 } |
|
304 } |
|
305 |
|
306 //////////////////////////////////////////////////////////////////////////////// |
|
307 |
|
308 // k = (y2 - y0, x0 - x2, (x2 - x0)*y0 - (y2 - y0)*x0 ) |
|
309 // l = (2*w * (y1 - y0), 2*w * (x0 - x1), 2*w * (x1*y0 - x0*y1)) |
|
310 // m = (2*w * (y2 - y1), 2*w * (x1 - x2), 2*w * (x2*y1 - x1*y2)) |
|
311 void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkScalar klm[9]) { |
|
312 const SkScalar w2 = 2.f * weight; |
|
313 klm[0] = p[2].fY - p[0].fY; |
|
314 klm[1] = p[0].fX - p[2].fX; |
|
315 klm[2] = (p[2].fX - p[0].fX) * p[0].fY - (p[2].fY - p[0].fY) * p[0].fX; |
|
316 |
|
317 klm[3] = w2 * (p[1].fY - p[0].fY); |
|
318 klm[4] = w2 * (p[0].fX - p[1].fX); |
|
319 klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY); |
|
320 |
|
321 klm[6] = w2 * (p[2].fY - p[1].fY); |
|
322 klm[7] = w2 * (p[1].fX - p[2].fX); |
|
323 klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY); |
|
324 |
|
325 // scale the max absolute value of coeffs to 10 |
|
326 SkScalar scale = 0.f; |
|
327 for (int i = 0; i < 9; ++i) { |
|
328 scale = SkMaxScalar(scale, SkScalarAbs(klm[i])); |
|
329 } |
|
330 SkASSERT(scale > 0.f); |
|
331 scale = 10.f / scale; |
|
332 for (int i = 0; i < 9; ++i) { |
|
333 klm[i] *= scale; |
|
334 } |
|
335 } |
|
336 |
|
337 //////////////////////////////////////////////////////////////////////////////// |
|
338 |
|
339 namespace { |
|
340 |
|
341 // a is the first control point of the cubic. |
|
342 // ab is the vector from a to the second control point. |
|
343 // dc is the vector from the fourth to the third control point. |
|
344 // d is the fourth control point. |
|
345 // p is the candidate quadratic control point. |
|
346 // this assumes that the cubic doesn't inflect and is simple |
|
347 bool is_point_within_cubic_tangents(const SkPoint& a, |
|
348 const SkVector& ab, |
|
349 const SkVector& dc, |
|
350 const SkPoint& d, |
|
351 SkPath::Direction dir, |
|
352 const SkPoint p) { |
|
353 SkVector ap = p - a; |
|
354 SkScalar apXab = ap.cross(ab); |
|
355 if (SkPath::kCW_Direction == dir) { |
|
356 if (apXab > 0) { |
|
357 return false; |
|
358 } |
|
359 } else { |
|
360 SkASSERT(SkPath::kCCW_Direction == dir); |
|
361 if (apXab < 0) { |
|
362 return false; |
|
363 } |
|
364 } |
|
365 |
|
366 SkVector dp = p - d; |
|
367 SkScalar dpXdc = dp.cross(dc); |
|
368 if (SkPath::kCW_Direction == dir) { |
|
369 if (dpXdc < 0) { |
|
370 return false; |
|
371 } |
|
372 } else { |
|
373 SkASSERT(SkPath::kCCW_Direction == dir); |
|
374 if (dpXdc > 0) { |
|
375 return false; |
|
376 } |
|
377 } |
|
378 return true; |
|
379 } |
|
380 |
|
381 void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
|
382 SkScalar toleranceSqd, |
|
383 bool constrainWithinTangents, |
|
384 SkPath::Direction dir, |
|
385 SkTArray<SkPoint, true>* quads, |
|
386 int sublevel = 0) { |
|
387 |
|
388 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is |
|
389 // 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]. |
|
390 |
|
391 SkVector ab = p[1] - p[0]; |
|
392 SkVector dc = p[2] - p[3]; |
|
393 |
|
394 if (ab.isZero()) { |
|
395 if (dc.isZero()) { |
|
396 SkPoint* degQuad = quads->push_back_n(3); |
|
397 degQuad[0] = p[0]; |
|
398 degQuad[1] = p[0]; |
|
399 degQuad[2] = p[3]; |
|
400 return; |
|
401 } |
|
402 ab = p[2] - p[0]; |
|
403 } |
|
404 if (dc.isZero()) { |
|
405 dc = p[1] - p[3]; |
|
406 } |
|
407 |
|
408 // When the ab and cd tangents are nearly parallel with vector from d to a the constraint that |
|
409 // the quad point falls between the tangents becomes hard to enforce and we are likely to hit |
|
410 // the max subdivision count. However, in this case the cubic is approaching a line and the |
|
411 // accuracy of the quad point isn't so important. We check if the two middle cubic control |
|
412 // points are very close to the baseline vector. If so then we just pick quadratic points on the |
|
413 // control polygon. |
|
414 |
|
415 if (constrainWithinTangents) { |
|
416 SkVector da = p[0] - p[3]; |
|
417 SkScalar invDALengthSqd = da.lengthSqd(); |
|
418 if (invDALengthSqd > SK_ScalarNearlyZero) { |
|
419 invDALengthSqd = SkScalarInvert(invDALengthSqd); |
|
420 // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a. |
|
421 // same goed for point c using vector cd. |
|
422 SkScalar detABSqd = ab.cross(da); |
|
423 detABSqd = SkScalarSquare(detABSqd); |
|
424 SkScalar detDCSqd = dc.cross(da); |
|
425 detDCSqd = SkScalarSquare(detDCSqd); |
|
426 if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd && |
|
427 SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) { |
|
428 SkPoint b = p[0] + ab; |
|
429 SkPoint c = p[3] + dc; |
|
430 SkPoint mid = b + c; |
|
431 mid.scale(SK_ScalarHalf); |
|
432 // Insert two quadratics to cover the case when ab points away from d and/or dc |
|
433 // points away from a. |
|
434 if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) { |
|
435 SkPoint* qpts = quads->push_back_n(6); |
|
436 qpts[0] = p[0]; |
|
437 qpts[1] = b; |
|
438 qpts[2] = mid; |
|
439 qpts[3] = mid; |
|
440 qpts[4] = c; |
|
441 qpts[5] = p[3]; |
|
442 } else { |
|
443 SkPoint* qpts = quads->push_back_n(3); |
|
444 qpts[0] = p[0]; |
|
445 qpts[1] = mid; |
|
446 qpts[2] = p[3]; |
|
447 } |
|
448 return; |
|
449 } |
|
450 } |
|
451 } |
|
452 |
|
453 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; |
|
454 static const int kMaxSubdivs = 10; |
|
455 |
|
456 ab.scale(kLengthScale); |
|
457 dc.scale(kLengthScale); |
|
458 |
|
459 // e0 and e1 are extrapolations along vectors ab and dc. |
|
460 SkVector c0 = p[0]; |
|
461 c0 += ab; |
|
462 SkVector c1 = p[3]; |
|
463 c1 += dc; |
|
464 |
|
465 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1); |
|
466 if (dSqd < toleranceSqd) { |
|
467 SkPoint cAvg = c0; |
|
468 cAvg += c1; |
|
469 cAvg.scale(SK_ScalarHalf); |
|
470 |
|
471 bool subdivide = false; |
|
472 |
|
473 if (constrainWithinTangents && |
|
474 !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) { |
|
475 // choose a new cAvg that is the intersection of the two tangent lines. |
|
476 ab.setOrthog(ab); |
|
477 SkScalar z0 = -ab.dot(p[0]); |
|
478 dc.setOrthog(dc); |
|
479 SkScalar z1 = -dc.dot(p[3]); |
|
480 cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY); |
|
481 cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1); |
|
482 SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX); |
|
483 z = SkScalarInvert(z); |
|
484 cAvg.fX *= z; |
|
485 cAvg.fY *= z; |
|
486 if (sublevel <= kMaxSubdivs) { |
|
487 SkScalar d0Sqd = c0.distanceToSqd(cAvg); |
|
488 SkScalar d1Sqd = c1.distanceToSqd(cAvg); |
|
489 // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know |
|
490 // the distances and tolerance can't be negative. |
|
491 // (d0 + d1)^2 > toleranceSqd |
|
492 // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd |
|
493 SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd)); |
|
494 subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd; |
|
495 } |
|
496 } |
|
497 if (!subdivide) { |
|
498 SkPoint* pts = quads->push_back_n(3); |
|
499 pts[0] = p[0]; |
|
500 pts[1] = cAvg; |
|
501 pts[2] = p[3]; |
|
502 return; |
|
503 } |
|
504 } |
|
505 SkPoint choppedPts[7]; |
|
506 SkChopCubicAtHalf(p, choppedPts); |
|
507 convert_noninflect_cubic_to_quads(choppedPts + 0, |
|
508 toleranceSqd, |
|
509 constrainWithinTangents, |
|
510 dir, |
|
511 quads, |
|
512 sublevel + 1); |
|
513 convert_noninflect_cubic_to_quads(choppedPts + 3, |
|
514 toleranceSqd, |
|
515 constrainWithinTangents, |
|
516 dir, |
|
517 quads, |
|
518 sublevel + 1); |
|
519 } |
|
520 } |
|
521 |
|
522 void GrPathUtils::convertCubicToQuads(const GrPoint p[4], |
|
523 SkScalar tolScale, |
|
524 bool constrainWithinTangents, |
|
525 SkPath::Direction dir, |
|
526 SkTArray<SkPoint, true>* quads) { |
|
527 SkPoint chopped[10]; |
|
528 int count = SkChopCubicAtInflections(p, chopped); |
|
529 |
|
530 // base tolerance is 1 pixel. |
|
531 static const SkScalar kTolerance = SK_Scalar1; |
|
532 const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance)); |
|
533 |
|
534 for (int i = 0; i < count; ++i) { |
|
535 SkPoint* cubic = chopped + 3*i; |
|
536 convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads); |
|
537 } |
|
538 |
|
539 } |
|
540 |
|
541 //////////////////////////////////////////////////////////////////////////////// |
|
542 |
|
543 enum CubicType { |
|
544 kSerpentine_CubicType, |
|
545 kCusp_CubicType, |
|
546 kLoop_CubicType, |
|
547 kQuadratic_CubicType, |
|
548 kLine_CubicType, |
|
549 kPoint_CubicType |
|
550 }; |
|
551 |
|
552 // discr(I) = d0^2 * (3*d1^2 - 4*d0*d2) |
|
553 // Classification: |
|
554 // discr(I) > 0 Serpentine |
|
555 // discr(I) = 0 Cusp |
|
556 // discr(I) < 0 Loop |
|
557 // d0 = d1 = 0 Quadratic |
|
558 // d0 = d1 = d2 = 0 Line |
|
559 // p0 = p1 = p2 = p3 Point |
|
560 static CubicType classify_cubic(const SkPoint p[4], const SkScalar d[3]) { |
|
561 if (p[0] == p[1] && p[0] == p[2] && p[0] == p[3]) { |
|
562 return kPoint_CubicType; |
|
563 } |
|
564 const SkScalar discr = d[0] * d[0] * (3.f * d[1] * d[1] - 4.f * d[0] * d[2]); |
|
565 if (discr > SK_ScalarNearlyZero) { |
|
566 return kSerpentine_CubicType; |
|
567 } else if (discr < -SK_ScalarNearlyZero) { |
|
568 return kLoop_CubicType; |
|
569 } else { |
|
570 if (0.f == d[0] && 0.f == d[1]) { |
|
571 return (0.f == d[2] ? kLine_CubicType : kQuadratic_CubicType); |
|
572 } else { |
|
573 return kCusp_CubicType; |
|
574 } |
|
575 } |
|
576 } |
|
577 |
|
578 // Assumes the third component of points is 1. |
|
579 // Calcs p0 . (p1 x p2) |
|
580 static SkScalar calc_dot_cross_cubic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
|
581 const SkScalar xComp = p0.fX * (p1.fY - p2.fY); |
|
582 const SkScalar yComp = p0.fY * (p2.fX - p1.fX); |
|
583 const SkScalar wComp = p1.fX * p2.fY - p1.fY * p2.fX; |
|
584 return (xComp + yComp + wComp); |
|
585 } |
|
586 |
|
587 // Solves linear system to extract klm |
|
588 // P.K = k (similarly for l, m) |
|
589 // Where P is matrix of control points |
|
590 // K is coefficients for the line K |
|
591 // k is vector of values of K evaluated at the control points |
|
592 // Solving for K, thus K = P^(-1) . k |
|
593 static void calc_cubic_klm(const SkPoint p[4], const SkScalar controlK[4], |
|
594 const SkScalar controlL[4], const SkScalar controlM[4], |
|
595 SkScalar k[3], SkScalar l[3], SkScalar m[3]) { |
|
596 SkMatrix matrix; |
|
597 matrix.setAll(p[0].fX, p[0].fY, 1.f, |
|
598 p[1].fX, p[1].fY, 1.f, |
|
599 p[2].fX, p[2].fY, 1.f); |
|
600 SkMatrix inverse; |
|
601 if (matrix.invert(&inverse)) { |
|
602 inverse.mapHomogeneousPoints(k, controlK, 1); |
|
603 inverse.mapHomogeneousPoints(l, controlL, 1); |
|
604 inverse.mapHomogeneousPoints(m, controlM, 1); |
|
605 } |
|
606 |
|
607 } |
|
608 |
|
609 static void set_serp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
|
610 SkScalar tempSqrt = SkScalarSqrt(9.f * d[1] * d[1] - 12.f * d[0] * d[2]); |
|
611 SkScalar ls = 3.f * d[1] - tempSqrt; |
|
612 SkScalar lt = 6.f * d[0]; |
|
613 SkScalar ms = 3.f * d[1] + tempSqrt; |
|
614 SkScalar mt = 6.f * d[0]; |
|
615 |
|
616 k[0] = ls * ms; |
|
617 k[1] = (3.f * ls * ms - ls * mt - lt * ms) / 3.f; |
|
618 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; |
|
619 k[3] = (lt - ls) * (mt - ms); |
|
620 |
|
621 l[0] = ls * ls * ls; |
|
622 const SkScalar lt_ls = lt - ls; |
|
623 l[1] = ls * ls * lt_ls * -1.f; |
|
624 l[2] = lt_ls * lt_ls * ls; |
|
625 l[3] = -1.f * lt_ls * lt_ls * lt_ls; |
|
626 |
|
627 m[0] = ms * ms * ms; |
|
628 const SkScalar mt_ms = mt - ms; |
|
629 m[1] = ms * ms * mt_ms * -1.f; |
|
630 m[2] = mt_ms * mt_ms * ms; |
|
631 m[3] = -1.f * mt_ms * mt_ms * mt_ms; |
|
632 |
|
633 // If d0 < 0 we need to flip the orientation of our curve |
|
634 // This is done by negating the k and l values |
|
635 // We want negative distance values to be on the inside |
|
636 if ( d[0] > 0) { |
|
637 for (int i = 0; i < 4; ++i) { |
|
638 k[i] = -k[i]; |
|
639 l[i] = -l[i]; |
|
640 } |
|
641 } |
|
642 } |
|
643 |
|
644 static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
|
645 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); |
|
646 SkScalar ls = d[1] - tempSqrt; |
|
647 SkScalar lt = 2.f * d[0]; |
|
648 SkScalar ms = d[1] + tempSqrt; |
|
649 SkScalar mt = 2.f * d[0]; |
|
650 |
|
651 k[0] = ls * ms; |
|
652 k[1] = (3.f * ls*ms - ls * mt - lt * ms) / 3.f; |
|
653 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; |
|
654 k[3] = (lt - ls) * (mt - ms); |
|
655 |
|
656 l[0] = ls * ls * ms; |
|
657 l[1] = (ls * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/-3.f; |
|
658 l[2] = ((lt - ls) * (ls * (2.f * mt - 3.f * ms) + lt * ms))/3.f; |
|
659 l[3] = -1.f * (lt - ls) * (lt - ls) * (mt - ms); |
|
660 |
|
661 m[0] = ls * ms * ms; |
|
662 m[1] = (ms * (ls * (2.f * mt - 3.f * ms) + lt * ms))/-3.f; |
|
663 m[2] = ((mt - ms) * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/3.f; |
|
664 m[3] = -1.f * (lt - ls) * (mt - ms) * (mt - ms); |
|
665 |
|
666 |
|
667 // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0), |
|
668 // we need to flip the orientation of our curve. |
|
669 // This is done by negating the k and l values |
|
670 if ( (d[0] < 0 && k[1] > 0) || (d[0] > 0 && k[1] < 0)) { |
|
671 for (int i = 0; i < 4; ++i) { |
|
672 k[i] = -k[i]; |
|
673 l[i] = -l[i]; |
|
674 } |
|
675 } |
|
676 } |
|
677 |
|
678 static void set_cusp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
|
679 const SkScalar ls = d[2]; |
|
680 const SkScalar lt = 3.f * d[1]; |
|
681 |
|
682 k[0] = ls; |
|
683 k[1] = ls - lt / 3.f; |
|
684 k[2] = ls - 2.f * lt / 3.f; |
|
685 k[3] = ls - lt; |
|
686 |
|
687 l[0] = ls * ls * ls; |
|
688 const SkScalar ls_lt = ls - lt; |
|
689 l[1] = ls * ls * ls_lt; |
|
690 l[2] = ls_lt * ls_lt * ls; |
|
691 l[3] = ls_lt * ls_lt * ls_lt; |
|
692 |
|
693 m[0] = 1.f; |
|
694 m[1] = 1.f; |
|
695 m[2] = 1.f; |
|
696 m[3] = 1.f; |
|
697 } |
|
698 |
|
699 // For the case when a cubic is actually a quadratic |
|
700 // M = |
|
701 // 0 0 0 |
|
702 // 1/3 0 1/3 |
|
703 // 2/3 1/3 2/3 |
|
704 // 1 1 1 |
|
705 static void set_quadratic_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
|
706 k[0] = 0.f; |
|
707 k[1] = 1.f/3.f; |
|
708 k[2] = 2.f/3.f; |
|
709 k[3] = 1.f; |
|
710 |
|
711 l[0] = 0.f; |
|
712 l[1] = 0.f; |
|
713 l[2] = 1.f/3.f; |
|
714 l[3] = 1.f; |
|
715 |
|
716 m[0] = 0.f; |
|
717 m[1] = 1.f/3.f; |
|
718 m[2] = 2.f/3.f; |
|
719 m[3] = 1.f; |
|
720 |
|
721 // If d2 < 0 we need to flip the orientation of our curve |
|
722 // This is done by negating the k and l values |
|
723 if ( d[2] > 0) { |
|
724 for (int i = 0; i < 4; ++i) { |
|
725 k[i] = -k[i]; |
|
726 l[i] = -l[i]; |
|
727 } |
|
728 } |
|
729 } |
|
730 |
|
731 // Calc coefficients of I(s,t) where roots of I are inflection points of curve |
|
732 // I(s,t) = t*(3*d0*s^2 - 3*d1*s*t + d2*t^2) |
|
733 // d0 = a1 - 2*a2+3*a3 |
|
734 // d1 = -a2 + 3*a3 |
|
735 // d2 = 3*a3 |
|
736 // a1 = p0 . (p3 x p2) |
|
737 // a2 = p1 . (p0 x p3) |
|
738 // a3 = p2 . (p1 x p0) |
|
739 // Places the values of d1, d2, d3 in array d passed in |
|
740 static void calc_cubic_inflection_func(const SkPoint p[4], SkScalar d[3]) { |
|
741 SkScalar a1 = calc_dot_cross_cubic(p[0], p[3], p[2]); |
|
742 SkScalar a2 = calc_dot_cross_cubic(p[1], p[0], p[3]); |
|
743 SkScalar a3 = calc_dot_cross_cubic(p[2], p[1], p[0]); |
|
744 |
|
745 // need to scale a's or values in later calculations will grow to high |
|
746 SkScalar max = SkScalarAbs(a1); |
|
747 max = SkMaxScalar(max, SkScalarAbs(a2)); |
|
748 max = SkMaxScalar(max, SkScalarAbs(a3)); |
|
749 max = 1.f/max; |
|
750 a1 = a1 * max; |
|
751 a2 = a2 * max; |
|
752 a3 = a3 * max; |
|
753 |
|
754 d[2] = 3.f * a3; |
|
755 d[1] = d[2] - a2; |
|
756 d[0] = d[1] - a2 + a1; |
|
757 } |
|
758 |
|
759 int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkScalar klm[9], |
|
760 SkScalar klm_rev[3]) { |
|
761 // Variable to store the two parametric values at the loop double point |
|
762 SkScalar smallS = 0.f; |
|
763 SkScalar largeS = 0.f; |
|
764 |
|
765 SkScalar d[3]; |
|
766 calc_cubic_inflection_func(src, d); |
|
767 |
|
768 CubicType cType = classify_cubic(src, d); |
|
769 |
|
770 int chop_count = 0; |
|
771 if (kLoop_CubicType == cType) { |
|
772 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); |
|
773 SkScalar ls = d[1] - tempSqrt; |
|
774 SkScalar lt = 2.f * d[0]; |
|
775 SkScalar ms = d[1] + tempSqrt; |
|
776 SkScalar mt = 2.f * d[0]; |
|
777 ls = ls / lt; |
|
778 ms = ms / mt; |
|
779 // need to have t values sorted since this is what is expected by SkChopCubicAt |
|
780 if (ls <= ms) { |
|
781 smallS = ls; |
|
782 largeS = ms; |
|
783 } else { |
|
784 smallS = ms; |
|
785 largeS = ls; |
|
786 } |
|
787 |
|
788 SkScalar chop_ts[2]; |
|
789 if (smallS > 0.f && smallS < 1.f) { |
|
790 chop_ts[chop_count++] = smallS; |
|
791 } |
|
792 if (largeS > 0.f && largeS < 1.f) { |
|
793 chop_ts[chop_count++] = largeS; |
|
794 } |
|
795 if(dst) { |
|
796 SkChopCubicAt(src, dst, chop_ts, chop_count); |
|
797 } |
|
798 } else { |
|
799 if (dst) { |
|
800 memcpy(dst, src, sizeof(SkPoint) * 4); |
|
801 } |
|
802 } |
|
803 |
|
804 if (klm && klm_rev) { |
|
805 // Set klm_rev to to match the sub_section of cubic that needs to have its orientation |
|
806 // flipped. This will always be the section that is the "loop" |
|
807 if (2 == chop_count) { |
|
808 klm_rev[0] = 1.f; |
|
809 klm_rev[1] = -1.f; |
|
810 klm_rev[2] = 1.f; |
|
811 } else if (1 == chop_count) { |
|
812 if (smallS < 0.f) { |
|
813 klm_rev[0] = -1.f; |
|
814 klm_rev[1] = 1.f; |
|
815 } else { |
|
816 klm_rev[0] = 1.f; |
|
817 klm_rev[1] = -1.f; |
|
818 } |
|
819 } else { |
|
820 if (smallS < 0.f && largeS > 1.f) { |
|
821 klm_rev[0] = -1.f; |
|
822 } else { |
|
823 klm_rev[0] = 1.f; |
|
824 } |
|
825 } |
|
826 SkScalar controlK[4]; |
|
827 SkScalar controlL[4]; |
|
828 SkScalar controlM[4]; |
|
829 |
|
830 if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) { |
|
831 set_serp_klm(d, controlK, controlL, controlM); |
|
832 } else if (kLoop_CubicType == cType) { |
|
833 set_loop_klm(d, controlK, controlL, controlM); |
|
834 } else if (kCusp_CubicType == cType) { |
|
835 SkASSERT(0.f == d[0]); |
|
836 set_cusp_klm(d, controlK, controlL, controlM); |
|
837 } else if (kQuadratic_CubicType == cType) { |
|
838 set_quadratic_klm(d, controlK, controlL, controlM); |
|
839 } |
|
840 |
|
841 calc_cubic_klm(src, controlK, controlL, controlM, klm, &klm[3], &klm[6]); |
|
842 } |
|
843 return chop_count + 1; |
|
844 } |
|
845 |
|
846 void GrPathUtils::getCubicKLM(const SkPoint p[4], SkScalar klm[9]) { |
|
847 SkScalar d[3]; |
|
848 calc_cubic_inflection_func(p, d); |
|
849 |
|
850 CubicType cType = classify_cubic(p, d); |
|
851 |
|
852 SkScalar controlK[4]; |
|
853 SkScalar controlL[4]; |
|
854 SkScalar controlM[4]; |
|
855 |
|
856 if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) { |
|
857 set_serp_klm(d, controlK, controlL, controlM); |
|
858 } else if (kLoop_CubicType == cType) { |
|
859 set_loop_klm(d, controlK, controlL, controlM); |
|
860 } else if (kCusp_CubicType == cType) { |
|
861 SkASSERT(0.f == d[0]); |
|
862 set_cusp_klm(d, controlK, controlL, controlM); |
|
863 } else if (kQuadratic_CubicType == cType) { |
|
864 set_quadratic_klm(d, controlK, controlL, controlM); |
|
865 } |
|
866 |
|
867 calc_cubic_klm(p, controlK, controlL, controlM, klm, &klm[3], &klm[6]); |
|
868 } |