senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "GrPathUtils.h" |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 9 | |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 10 | #include "GrTypes.h" |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 11 | #include "SkGeometry.h" |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 12 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 13 | SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 14 | const SkMatrix& viewM, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 15 | const SkRect& pathBounds) { |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 16 | // In order to tesselate the path we get a bound on how much the matrix can |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 17 | // scale when mapping to screen coordinates. |
| 18 | SkScalar stretch = viewM.getMaxScale(); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 19 | SkScalar srcTol = devTol; |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 20 | |
| 21 | if (stretch < 0) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 22 | // take worst case mapRadius amoung four corners. |
| 23 | // (less than perfect) |
| 24 | for (int i = 0; i < 4; ++i) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 25 | SkMatrix mat; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 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 | } |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 31 | } |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 32 | srcTol = SkScalarDiv(srcTol, stretch); |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 33 | return srcTol; |
| 34 | } |
| 35 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 36 | static const int MAX_POINTS_PER_CURVE = 1 << 10; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 37 | static const SkScalar gMinCurveTol = 0.0001f; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 39 | uint32_t GrPathUtils::quadraticPointCount(const SkPoint points[], |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 40 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 41 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 42 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 43 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 44 | SkASSERT(tol > 0); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 46 | SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 47 | if (d <= tol) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 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); |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 54 | int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol))); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 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 | } |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 62 | return SkTMin(pow2, MAX_POINTS_PER_CURVE); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 66 | uint32_t GrPathUtils::generateQuadraticPoints(const SkPoint& p0, |
| 67 | const SkPoint& p1, |
| 68 | const SkPoint& p2, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 69 | SkScalar tolSqd, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 70 | SkPoint** points, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 71 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 72 | if (pointsLeft < 2 || |
| 73 | (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) { |
| 74 | (*points)[0] = p2; |
| 75 | *points += 1; |
| 76 | return 1; |
| 77 | } |
| 78 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 79 | SkPoint q[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 80 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 81 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 82 | }; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 83 | SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 91 | uint32_t GrPathUtils::cubicPointCount(const SkPoint points[], |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 92 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 93 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 94 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 95 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 96 | SkASSERT(tol > 0); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 98 | SkScalar d = SkTMax( |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 99 | points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]), |
| 100 | points[2].distanceToLineSegmentBetweenSqd(points[0], points[3])); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 101 | d = SkScalarSqrt(d); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 102 | if (d <= tol) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 103 | return 1; |
| 104 | } else { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 105 | int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol))); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 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 | } |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 113 | return SkTMin(pow2, MAX_POINTS_PER_CURVE); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 117 | uint32_t GrPathUtils::generateCubicPoints(const SkPoint& p0, |
| 118 | const SkPoint& p1, |
| 119 | const SkPoint& p2, |
| 120 | const SkPoint& p3, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 121 | SkScalar tolSqd, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 122 | SkPoint** points, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 123 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 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 | } |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 131 | SkPoint q[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 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) } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 135 | }; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 136 | SkPoint r[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 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) } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 139 | }; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 140 | SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 147 | int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 148 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 149 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 150 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 151 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 152 | SkASSERT(tol > 0); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 153 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 154 | int pointCount = 0; |
| 155 | *subpaths = 1; |
| 156 | |
| 157 | bool first = true; |
| 158 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 159 | SkPath::Iter iter(path, false); |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 160 | SkPath::Verb verb; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 161 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 162 | SkPoint pts[4]; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 163 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 164 | |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 165 | switch (verb) { |
| 166 | case SkPath::kLine_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 167 | pointCount += 1; |
| 168 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 169 | case SkPath::kQuad_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 170 | pointCount += quadraticPointCount(pts, tol); |
| 171 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 172 | case SkPath::kCubic_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 173 | pointCount += cubicPointCount(pts, tol); |
| 174 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 175 | case SkPath::kMove_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 176 | pointCount += 1; |
| 177 | if (!first) { |
| 178 | ++(*subpaths); |
| 179 | } |
| 180 | break; |
| 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | first = false; |
| 185 | } |
| 186 | return pointCount; |
| 187 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 188 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 189 | void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) { |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 190 | SkMatrix m; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 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] |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 195 | // And control_pts = [x0 x1 x2] |
| 196 | // [y0 y1 y2] |
| 197 | // [1 1 1 ] |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 198 | // We invert the control pt matrix and post concat to both sides to get M. |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 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 | |
skia.committer@gmail.com | 8491d24 | 2013-12-05 07:02:16 +0000 | [diff] [blame] | 210 | if (!sk_float_isfinite(det) |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 211 | || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 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) |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 229 | SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge]; |
bsalomon@google.com | 20e542e | 2012-02-15 18:49:41 +0000 | [diff] [blame] | 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. |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 233 | lineVec.setOrthog(lineVec, SkPoint::kLeft_Side); |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 234 | lineVec.dot(qPts[0]); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 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]); |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 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. |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 246 | fM[0] = 0; fM[1] = 0; fM[2] = 100.f; |
| 247 | fM[3] = 0; fM[4] = 0; fM[5] = 100.f; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 248 | } |
| 249 | } else { |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 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 | |
skia.committer@gmail.com | 8491d24 | 2013-12-05 07:02:16 +0000 | [diff] [blame] | 266 | // this performs the uv_pts*adjugate(control_pts) multiply, |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 279 | |
| 280 | // The matrix should not have perspective. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 281 | SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 282 | SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL); |
| 283 | SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 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 | } |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 303 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 304 | } |
| 305 | |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 339 | namespace { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 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 { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 360 | SkASSERT(SkPath::kCCW_Direction == dir); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 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 { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 373 | SkASSERT(SkPath::kCCW_Direction == dir); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 374 | if (dpXdc > 0) { |
| 375 | return false; |
| 376 | } |
| 377 | } |
| 378 | return true; |
| 379 | } |
| 380 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 381 | void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 382 | SkScalar toleranceSqd, |
| 383 | bool constrainWithinTangents, |
| 384 | SkPath::Direction dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 385 | SkTArray<SkPoint, true>* quads, |
| 386 | int sublevel = 0) { |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 391 | SkVector ab = p[1] - p[0]; |
| 392 | SkVector dc = p[2] - p[3]; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 393 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 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 | |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 408 | // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the |
| 409 | // constraint that the quad point falls between the tangents becomes hard to enforce and we are |
| 410 | // likely to hit the max subdivision count. However, in this case the cubic is approaching a |
| 411 | // line and the accuracy of the quad point isn't so important. We check if the two middle cubic |
| 412 | // control points are very close to the baseline vector. If so then we just pick quadratic |
| 413 | // points on the control polygon. |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 414 | |
| 415 | if (constrainWithinTangents) { |
| 416 | SkVector da = p[0] - p[3]; |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 417 | bool doQuads = dc.lengthSqd() < SK_ScalarNearlyZero || |
| 418 | ab.lengthSqd() < SK_ScalarNearlyZero; |
| 419 | if (!doQuads) { |
| 420 | SkScalar invDALengthSqd = da.lengthSqd(); |
| 421 | if (invDALengthSqd > SK_ScalarNearlyZero) { |
| 422 | invDALengthSqd = SkScalarInvert(invDALengthSqd); |
| 423 | // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a. |
| 424 | // same goes for point c using vector cd. |
| 425 | SkScalar detABSqd = ab.cross(da); |
| 426 | detABSqd = SkScalarSquare(detABSqd); |
| 427 | SkScalar detDCSqd = dc.cross(da); |
| 428 | detDCSqd = SkScalarSquare(detDCSqd); |
| 429 | if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd && |
| 430 | SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) { |
| 431 | doQuads = true; |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 432 | } |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 435 | if (doQuads) { |
| 436 | SkPoint b = p[0] + ab; |
| 437 | SkPoint c = p[3] + dc; |
| 438 | SkPoint mid = b + c; |
| 439 | mid.scale(SK_ScalarHalf); |
| 440 | // Insert two quadratics to cover the case when ab points away from d and/or dc |
| 441 | // points away from a. |
| 442 | if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) { |
| 443 | SkPoint* qpts = quads->push_back_n(6); |
| 444 | qpts[0] = p[0]; |
| 445 | qpts[1] = b; |
| 446 | qpts[2] = mid; |
| 447 | qpts[3] = mid; |
| 448 | qpts[4] = c; |
| 449 | qpts[5] = p[3]; |
| 450 | } else { |
| 451 | SkPoint* qpts = quads->push_back_n(3); |
| 452 | qpts[0] = p[0]; |
| 453 | qpts[1] = mid; |
| 454 | qpts[2] = p[3]; |
| 455 | } |
| 456 | return; |
| 457 | } |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 458 | } |
| 459 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 460 | static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 461 | static const int kMaxSubdivs = 10; |
| 462 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 463 | ab.scale(kLengthScale); |
| 464 | dc.scale(kLengthScale); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 465 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 466 | // e0 and e1 are extrapolations along vectors ab and dc. |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 467 | SkVector c0 = p[0]; |
| 468 | c0 += ab; |
| 469 | SkVector c1 = p[3]; |
| 470 | c1 += dc; |
| 471 | |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 472 | SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 473 | if (dSqd < toleranceSqd) { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 474 | SkPoint cAvg = c0; |
| 475 | cAvg += c1; |
| 476 | cAvg.scale(SK_ScalarHalf); |
| 477 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 478 | bool subdivide = false; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 479 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 480 | if (constrainWithinTangents && |
| 481 | !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) { |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 482 | // choose a new cAvg that is the intersection of the two tangent lines. |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 483 | ab.setOrthog(ab); |
| 484 | SkScalar z0 = -ab.dot(p[0]); |
| 485 | dc.setOrthog(dc); |
| 486 | SkScalar z1 = -dc.dot(p[3]); |
| 487 | cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY); |
| 488 | cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1); |
| 489 | SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX); |
| 490 | z = SkScalarInvert(z); |
| 491 | cAvg.fX *= z; |
| 492 | cAvg.fY *= z; |
| 493 | if (sublevel <= kMaxSubdivs) { |
| 494 | SkScalar d0Sqd = c0.distanceToSqd(cAvg); |
| 495 | SkScalar d1Sqd = c1.distanceToSqd(cAvg); |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 496 | // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know |
| 497 | // the distances and tolerance can't be negative. |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 498 | // (d0 + d1)^2 > toleranceSqd |
| 499 | // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd |
| 500 | SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd)); |
| 501 | subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd; |
| 502 | } |
| 503 | } |
| 504 | if (!subdivide) { |
| 505 | SkPoint* pts = quads->push_back_n(3); |
| 506 | pts[0] = p[0]; |
| 507 | pts[1] = cAvg; |
| 508 | pts[2] = p[3]; |
| 509 | return; |
| 510 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 511 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 512 | SkPoint choppedPts[7]; |
| 513 | SkChopCubicAtHalf(p, choppedPts); |
| 514 | convert_noninflect_cubic_to_quads(choppedPts + 0, |
| 515 | toleranceSqd, |
| 516 | constrainWithinTangents, |
| 517 | dir, |
| 518 | quads, |
| 519 | sublevel + 1); |
| 520 | convert_noninflect_cubic_to_quads(choppedPts + 3, |
| 521 | toleranceSqd, |
| 522 | constrainWithinTangents, |
| 523 | dir, |
| 524 | quads, |
| 525 | sublevel + 1); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 529 | void GrPathUtils::convertCubicToQuads(const SkPoint p[4], |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 530 | SkScalar tolScale, |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 531 | bool constrainWithinTangents, |
| 532 | SkPath::Direction dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 533 | SkTArray<SkPoint, true>* quads) { |
| 534 | SkPoint chopped[10]; |
| 535 | int count = SkChopCubicAtInflections(p, chopped); |
| 536 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 537 | // base tolerance is 1 pixel. |
| 538 | static const SkScalar kTolerance = SK_Scalar1; |
| 539 | const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance)); |
| 540 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 541 | for (int i = 0; i < count; ++i) { |
| 542 | SkPoint* cubic = chopped + 3*i; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 543 | convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | } |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 547 | |
| 548 | //////////////////////////////////////////////////////////////////////////////// |
| 549 | |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 550 | // Solves linear system to extract klm |
| 551 | // P.K = k (similarly for l, m) |
| 552 | // Where P is matrix of control points |
| 553 | // K is coefficients for the line K |
| 554 | // k is vector of values of K evaluated at the control points |
| 555 | // Solving for K, thus K = P^(-1) . k |
| 556 | static void calc_cubic_klm(const SkPoint p[4], const SkScalar controlK[4], |
| 557 | const SkScalar controlL[4], const SkScalar controlM[4], |
| 558 | SkScalar k[3], SkScalar l[3], SkScalar m[3]) { |
| 559 | SkMatrix matrix; |
| 560 | matrix.setAll(p[0].fX, p[0].fY, 1.f, |
| 561 | p[1].fX, p[1].fY, 1.f, |
| 562 | p[2].fX, p[2].fY, 1.f); |
| 563 | SkMatrix inverse; |
| 564 | if (matrix.invert(&inverse)) { |
| 565 | inverse.mapHomogeneousPoints(k, controlK, 1); |
| 566 | inverse.mapHomogeneousPoints(l, controlL, 1); |
| 567 | inverse.mapHomogeneousPoints(m, controlM, 1); |
| 568 | } |
| 569 | |
| 570 | } |
| 571 | |
| 572 | static void set_serp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 573 | SkScalar tempSqrt = SkScalarSqrt(9.f * d[1] * d[1] - 12.f * d[0] * d[2]); |
| 574 | SkScalar ls = 3.f * d[1] - tempSqrt; |
| 575 | SkScalar lt = 6.f * d[0]; |
| 576 | SkScalar ms = 3.f * d[1] + tempSqrt; |
| 577 | SkScalar mt = 6.f * d[0]; |
| 578 | |
| 579 | k[0] = ls * ms; |
| 580 | k[1] = (3.f * ls * ms - ls * mt - lt * ms) / 3.f; |
| 581 | k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; |
| 582 | k[3] = (lt - ls) * (mt - ms); |
| 583 | |
| 584 | l[0] = ls * ls * ls; |
| 585 | const SkScalar lt_ls = lt - ls; |
| 586 | l[1] = ls * ls * lt_ls * -1.f; |
| 587 | l[2] = lt_ls * lt_ls * ls; |
| 588 | l[3] = -1.f * lt_ls * lt_ls * lt_ls; |
| 589 | |
| 590 | m[0] = ms * ms * ms; |
| 591 | const SkScalar mt_ms = mt - ms; |
| 592 | m[1] = ms * ms * mt_ms * -1.f; |
| 593 | m[2] = mt_ms * mt_ms * ms; |
| 594 | m[3] = -1.f * mt_ms * mt_ms * mt_ms; |
| 595 | |
| 596 | // If d0 < 0 we need to flip the orientation of our curve |
| 597 | // This is done by negating the k and l values |
| 598 | // We want negative distance values to be on the inside |
| 599 | if ( d[0] > 0) { |
| 600 | for (int i = 0; i < 4; ++i) { |
| 601 | k[i] = -k[i]; |
| 602 | l[i] = -l[i]; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 608 | SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); |
| 609 | SkScalar ls = d[1] - tempSqrt; |
| 610 | SkScalar lt = 2.f * d[0]; |
| 611 | SkScalar ms = d[1] + tempSqrt; |
| 612 | SkScalar mt = 2.f * d[0]; |
| 613 | |
| 614 | k[0] = ls * ms; |
| 615 | k[1] = (3.f * ls*ms - ls * mt - lt * ms) / 3.f; |
| 616 | k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; |
| 617 | k[3] = (lt - ls) * (mt - ms); |
| 618 | |
| 619 | l[0] = ls * ls * ms; |
| 620 | l[1] = (ls * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/-3.f; |
| 621 | l[2] = ((lt - ls) * (ls * (2.f * mt - 3.f * ms) + lt * ms))/3.f; |
| 622 | l[3] = -1.f * (lt - ls) * (lt - ls) * (mt - ms); |
| 623 | |
| 624 | m[0] = ls * ms * ms; |
| 625 | m[1] = (ms * (ls * (2.f * mt - 3.f * ms) + lt * ms))/-3.f; |
| 626 | m[2] = ((mt - ms) * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/3.f; |
| 627 | m[3] = -1.f * (lt - ls) * (mt - ms) * (mt - ms); |
| 628 | |
| 629 | |
| 630 | // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0), |
| 631 | // we need to flip the orientation of our curve. |
| 632 | // This is done by negating the k and l values |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 633 | if ( (d[0] < 0 && k[1] > 0) || (d[0] > 0 && k[1] < 0)) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 634 | for (int i = 0; i < 4; ++i) { |
| 635 | k[i] = -k[i]; |
| 636 | l[i] = -l[i]; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static void set_cusp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 642 | const SkScalar ls = d[2]; |
| 643 | const SkScalar lt = 3.f * d[1]; |
| 644 | |
| 645 | k[0] = ls; |
| 646 | k[1] = ls - lt / 3.f; |
| 647 | k[2] = ls - 2.f * lt / 3.f; |
| 648 | k[3] = ls - lt; |
| 649 | |
| 650 | l[0] = ls * ls * ls; |
| 651 | const SkScalar ls_lt = ls - lt; |
| 652 | l[1] = ls * ls * ls_lt; |
| 653 | l[2] = ls_lt * ls_lt * ls; |
| 654 | l[3] = ls_lt * ls_lt * ls_lt; |
| 655 | |
| 656 | m[0] = 1.f; |
| 657 | m[1] = 1.f; |
| 658 | m[2] = 1.f; |
| 659 | m[3] = 1.f; |
| 660 | } |
| 661 | |
| 662 | // For the case when a cubic is actually a quadratic |
| 663 | // M = |
| 664 | // 0 0 0 |
| 665 | // 1/3 0 1/3 |
| 666 | // 2/3 1/3 2/3 |
| 667 | // 1 1 1 |
| 668 | static void set_quadratic_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 669 | k[0] = 0.f; |
| 670 | k[1] = 1.f/3.f; |
| 671 | k[2] = 2.f/3.f; |
| 672 | k[3] = 1.f; |
| 673 | |
| 674 | l[0] = 0.f; |
| 675 | l[1] = 0.f; |
| 676 | l[2] = 1.f/3.f; |
| 677 | l[3] = 1.f; |
| 678 | |
| 679 | m[0] = 0.f; |
| 680 | m[1] = 1.f/3.f; |
| 681 | m[2] = 2.f/3.f; |
| 682 | m[3] = 1.f; |
| 683 | |
| 684 | // If d2 < 0 we need to flip the orientation of our curve |
| 685 | // This is done by negating the k and l values |
| 686 | if ( d[2] > 0) { |
| 687 | for (int i = 0; i < 4; ++i) { |
| 688 | k[i] = -k[i]; |
| 689 | l[i] = -l[i]; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 694 | int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkScalar klm[9], |
| 695 | SkScalar klm_rev[3]) { |
| 696 | // Variable to store the two parametric values at the loop double point |
| 697 | SkScalar smallS = 0.f; |
| 698 | SkScalar largeS = 0.f; |
| 699 | |
| 700 | SkScalar d[3]; |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 701 | SkCubicType cType = SkClassifyCubic(src, d); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 702 | |
| 703 | int chop_count = 0; |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 704 | if (kLoop_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 705 | SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); |
| 706 | SkScalar ls = d[1] - tempSqrt; |
| 707 | SkScalar lt = 2.f * d[0]; |
| 708 | SkScalar ms = d[1] + tempSqrt; |
| 709 | SkScalar mt = 2.f * d[0]; |
| 710 | ls = ls / lt; |
| 711 | ms = ms / mt; |
| 712 | // need to have t values sorted since this is what is expected by SkChopCubicAt |
| 713 | if (ls <= ms) { |
| 714 | smallS = ls; |
| 715 | largeS = ms; |
| 716 | } else { |
| 717 | smallS = ms; |
| 718 | largeS = ls; |
| 719 | } |
| 720 | |
| 721 | SkScalar chop_ts[2]; |
| 722 | if (smallS > 0.f && smallS < 1.f) { |
| 723 | chop_ts[chop_count++] = smallS; |
| 724 | } |
| 725 | if (largeS > 0.f && largeS < 1.f) { |
| 726 | chop_ts[chop_count++] = largeS; |
| 727 | } |
| 728 | if(dst) { |
| 729 | SkChopCubicAt(src, dst, chop_ts, chop_count); |
| 730 | } |
| 731 | } else { |
| 732 | if (dst) { |
| 733 | memcpy(dst, src, sizeof(SkPoint) * 4); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | if (klm && klm_rev) { |
| 738 | // Set klm_rev to to match the sub_section of cubic that needs to have its orientation |
| 739 | // flipped. This will always be the section that is the "loop" |
| 740 | if (2 == chop_count) { |
| 741 | klm_rev[0] = 1.f; |
| 742 | klm_rev[1] = -1.f; |
| 743 | klm_rev[2] = 1.f; |
| 744 | } else if (1 == chop_count) { |
| 745 | if (smallS < 0.f) { |
| 746 | klm_rev[0] = -1.f; |
| 747 | klm_rev[1] = 1.f; |
| 748 | } else { |
| 749 | klm_rev[0] = 1.f; |
| 750 | klm_rev[1] = -1.f; |
| 751 | } |
| 752 | } else { |
| 753 | if (smallS < 0.f && largeS > 1.f) { |
| 754 | klm_rev[0] = -1.f; |
| 755 | } else { |
| 756 | klm_rev[0] = 1.f; |
| 757 | } |
| 758 | } |
| 759 | SkScalar controlK[4]; |
| 760 | SkScalar controlL[4]; |
| 761 | SkScalar controlM[4]; |
| 762 | |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 763 | if (kSerpentine_SkCubicType == cType || (kCusp_SkCubicType == cType && 0.f != d[0])) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 764 | set_serp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 765 | } else if (kLoop_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 766 | set_loop_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 767 | } else if (kCusp_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 768 | SkASSERT(0.f == d[0]); |
| 769 | set_cusp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 770 | } else if (kQuadratic_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 771 | set_quadratic_klm(d, controlK, controlL, controlM); |
| 772 | } |
| 773 | |
| 774 | calc_cubic_klm(src, controlK, controlL, controlM, klm, &klm[3], &klm[6]); |
| 775 | } |
| 776 | return chop_count + 1; |
| 777 | } |
| 778 | |
| 779 | void GrPathUtils::getCubicKLM(const SkPoint p[4], SkScalar klm[9]) { |
| 780 | SkScalar d[3]; |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 781 | SkCubicType cType = SkClassifyCubic(p, d); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 782 | |
| 783 | SkScalar controlK[4]; |
| 784 | SkScalar controlL[4]; |
| 785 | SkScalar controlM[4]; |
| 786 | |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 787 | if (kSerpentine_SkCubicType == cType || (kCusp_SkCubicType == cType && 0.f != d[0])) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 788 | set_serp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 789 | } else if (kLoop_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 790 | set_loop_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 791 | } else if (kCusp_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 792 | SkASSERT(0.f == d[0]); |
| 793 | set_cusp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame^] | 794 | } else if (kQuadratic_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 795 | set_quadratic_klm(d, controlK, controlL, controlM); |
| 796 | } |
| 797 | |
| 798 | calc_cubic_klm(p, controlK, controlL, controlM, klm, &klm[3], &klm[6]); |
| 799 | } |