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; |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 169 | case SkPath::kConic_Verb: { |
| 170 | SkScalar weight = iter.conicWeight(); |
| 171 | SkAutoConicToQuads converter; |
| 172 | const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f); |
| 173 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 174 | pointCount += quadraticPointCount(quadPts + 2*i, tol); |
| 175 | } |
| 176 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 177 | case SkPath::kQuad_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 178 | pointCount += quadraticPointCount(pts, tol); |
| 179 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 180 | case SkPath::kCubic_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 181 | pointCount += cubicPointCount(pts, tol); |
| 182 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 183 | case SkPath::kMove_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 184 | pointCount += 1; |
| 185 | if (!first) { |
| 186 | ++(*subpaths); |
| 187 | } |
| 188 | break; |
| 189 | default: |
| 190 | break; |
| 191 | } |
| 192 | first = false; |
| 193 | } |
| 194 | return pointCount; |
| 195 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 196 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 197 | void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) { |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 198 | SkMatrix m; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 199 | // We want M such that M * xy_pt = uv_pt |
| 200 | // We know M * control_pts = [0 1/2 1] |
| 201 | // [0 0 1] |
| 202 | // [1 1 1] |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 203 | // And control_pts = [x0 x1 x2] |
| 204 | // [y0 y1 y2] |
| 205 | // [1 1 1 ] |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 206 | // 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] | 207 | // Using the known form of the control point matrix and the result, we can |
| 208 | // optimize and improve precision. |
| 209 | |
| 210 | double x0 = qPts[0].fX; |
| 211 | double y0 = qPts[0].fY; |
| 212 | double x1 = qPts[1].fX; |
| 213 | double y1 = qPts[1].fY; |
| 214 | double x2 = qPts[2].fX; |
| 215 | double y2 = qPts[2].fY; |
| 216 | double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2; |
| 217 | |
skia.committer@gmail.com | 8491d24 | 2013-12-05 07:02:16 +0000 | [diff] [blame] | 218 | if (!sk_float_isfinite(det) |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 219 | || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 220 | // The quad is degenerate. Hopefully this is rare. Find the pts that are |
| 221 | // farthest apart to compute a line (unless it is really a pt). |
| 222 | SkScalar maxD = qPts[0].distanceToSqd(qPts[1]); |
| 223 | int maxEdge = 0; |
| 224 | SkScalar d = qPts[1].distanceToSqd(qPts[2]); |
| 225 | if (d > maxD) { |
| 226 | maxD = d; |
| 227 | maxEdge = 1; |
| 228 | } |
| 229 | d = qPts[2].distanceToSqd(qPts[0]); |
| 230 | if (d > maxD) { |
| 231 | maxD = d; |
| 232 | maxEdge = 2; |
| 233 | } |
| 234 | // We could have a tolerance here, not sure if it would improve anything |
| 235 | if (maxD > 0) { |
| 236 | // 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] | 237 | SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge]; |
bsalomon@google.com | 20e542e | 2012-02-15 18:49:41 +0000 | [diff] [blame] | 238 | // when looking from the point 0 down the line we want positive |
| 239 | // distances to be to the left. This matches the non-degenerate |
| 240 | // case. |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 241 | lineVec.setOrthog(lineVec, SkPoint::kLeft_Side); |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 242 | lineVec.dot(qPts[0]); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 243 | // first row |
| 244 | fM[0] = 0; |
| 245 | fM[1] = 0; |
| 246 | fM[2] = 0; |
| 247 | // second row |
| 248 | fM[3] = lineVec.fX; |
| 249 | fM[4] = lineVec.fY; |
| 250 | fM[5] = -lineVec.dot(qPts[maxEdge]); |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 251 | } else { |
| 252 | // It's a point. It should cover zero area. Just set the matrix such |
| 253 | // that (u, v) will always be far away from the quad. |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 254 | fM[0] = 0; fM[1] = 0; fM[2] = 100.f; |
| 255 | fM[3] = 0; fM[4] = 0; fM[5] = 100.f; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 256 | } |
| 257 | } else { |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 258 | double scale = 1.0/det; |
| 259 | |
| 260 | // compute adjugate matrix |
| 261 | double a0, a1, a2, a3, a4, a5, a6, a7, a8; |
| 262 | a0 = y1-y2; |
| 263 | a1 = x2-x1; |
| 264 | a2 = x1*y2-x2*y1; |
| 265 | |
| 266 | a3 = y2-y0; |
| 267 | a4 = x0-x2; |
| 268 | a5 = x2*y0-x0*y2; |
| 269 | |
| 270 | a6 = y0-y1; |
| 271 | a7 = x1-x0; |
| 272 | a8 = x0*y1-x1*y0; |
| 273 | |
skia.committer@gmail.com | 8491d24 | 2013-12-05 07:02:16 +0000 | [diff] [blame] | 274 | // this performs the uv_pts*adjugate(control_pts) multiply, |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 275 | // then does the scale by 1/det afterwards to improve precision |
| 276 | m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale); |
| 277 | m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale); |
| 278 | m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale); |
| 279 | |
| 280 | m[SkMatrix::kMSkewY] = (float)(a6*scale); |
| 281 | m[SkMatrix::kMScaleY] = (float)(a7*scale); |
| 282 | m[SkMatrix::kMTransY] = (float)(a8*scale); |
| 283 | |
| 284 | m[SkMatrix::kMPersp0] = (float)((a0 + a3 + a6)*scale); |
| 285 | m[SkMatrix::kMPersp1] = (float)((a1 + a4 + a7)*scale); |
| 286 | m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 287 | |
| 288 | // The matrix should not have perspective. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 289 | SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 290 | SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL); |
| 291 | SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 292 | |
| 293 | // 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.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 311 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 312 | } |
| 313 | |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 314 | //////////////////////////////////////////////////////////////////////////////// |
| 315 | |
| 316 | // k = (y2 - y0, x0 - x2, (x2 - x0)*y0 - (y2 - y0)*x0 ) |
| 317 | // l = (2*w * (y1 - y0), 2*w * (x0 - x1), 2*w * (x1*y0 - x0*y1)) |
| 318 | // m = (2*w * (y2 - y1), 2*w * (x1 - x2), 2*w * (x2*y1 - x1*y2)) |
| 319 | void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkScalar klm[9]) { |
| 320 | const SkScalar w2 = 2.f * weight; |
| 321 | klm[0] = p[2].fY - p[0].fY; |
| 322 | klm[1] = p[0].fX - p[2].fX; |
| 323 | klm[2] = (p[2].fX - p[0].fX) * p[0].fY - (p[2].fY - p[0].fY) * p[0].fX; |
| 324 | |
| 325 | klm[3] = w2 * (p[1].fY - p[0].fY); |
| 326 | klm[4] = w2 * (p[0].fX - p[1].fX); |
| 327 | klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY); |
| 328 | |
| 329 | klm[6] = w2 * (p[2].fY - p[1].fY); |
| 330 | klm[7] = w2 * (p[1].fX - p[2].fX); |
| 331 | klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY); |
| 332 | |
| 333 | // scale the max absolute value of coeffs to 10 |
| 334 | SkScalar scale = 0.f; |
| 335 | for (int i = 0; i < 9; ++i) { |
| 336 | scale = SkMaxScalar(scale, SkScalarAbs(klm[i])); |
| 337 | } |
| 338 | SkASSERT(scale > 0.f); |
| 339 | scale = 10.f / scale; |
| 340 | for (int i = 0; i < 9; ++i) { |
| 341 | klm[i] *= scale; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | //////////////////////////////////////////////////////////////////////////////// |
| 346 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 347 | namespace { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 348 | |
| 349 | // a is the first control point of the cubic. |
| 350 | // ab is the vector from a to the second control point. |
| 351 | // dc is the vector from the fourth to the third control point. |
| 352 | // d is the fourth control point. |
| 353 | // p is the candidate quadratic control point. |
| 354 | // this assumes that the cubic doesn't inflect and is simple |
| 355 | bool is_point_within_cubic_tangents(const SkPoint& a, |
| 356 | const SkVector& ab, |
| 357 | const SkVector& dc, |
| 358 | const SkPoint& d, |
| 359 | SkPath::Direction dir, |
| 360 | const SkPoint p) { |
| 361 | SkVector ap = p - a; |
| 362 | SkScalar apXab = ap.cross(ab); |
| 363 | if (SkPath::kCW_Direction == dir) { |
| 364 | if (apXab > 0) { |
| 365 | return false; |
| 366 | } |
| 367 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 368 | SkASSERT(SkPath::kCCW_Direction == dir); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 369 | if (apXab < 0) { |
| 370 | return false; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | SkVector dp = p - d; |
| 375 | SkScalar dpXdc = dp.cross(dc); |
| 376 | if (SkPath::kCW_Direction == dir) { |
| 377 | if (dpXdc < 0) { |
| 378 | return false; |
| 379 | } |
| 380 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 381 | SkASSERT(SkPath::kCCW_Direction == dir); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 382 | if (dpXdc > 0) { |
| 383 | return false; |
| 384 | } |
| 385 | } |
| 386 | return true; |
| 387 | } |
| 388 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 389 | void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 390 | SkScalar toleranceSqd, |
| 391 | bool constrainWithinTangents, |
| 392 | SkPath::Direction dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 393 | SkTArray<SkPoint, true>* quads, |
| 394 | int sublevel = 0) { |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 395 | |
| 396 | // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is |
| 397 | // 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]. |
| 398 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 399 | SkVector ab = p[1] - p[0]; |
| 400 | SkVector dc = p[2] - p[3]; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 401 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 402 | if (ab.isZero()) { |
| 403 | if (dc.isZero()) { |
| 404 | SkPoint* degQuad = quads->push_back_n(3); |
| 405 | degQuad[0] = p[0]; |
| 406 | degQuad[1] = p[0]; |
| 407 | degQuad[2] = p[3]; |
| 408 | return; |
| 409 | } |
| 410 | ab = p[2] - p[0]; |
| 411 | } |
| 412 | if (dc.isZero()) { |
| 413 | dc = p[1] - p[3]; |
| 414 | } |
| 415 | |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 416 | // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the |
| 417 | // constraint that the quad point falls between the tangents becomes hard to enforce and we are |
| 418 | // likely to hit the max subdivision count. However, in this case the cubic is approaching a |
| 419 | // line and the accuracy of the quad point isn't so important. We check if the two middle cubic |
| 420 | // control points are very close to the baseline vector. If so then we just pick quadratic |
| 421 | // points on the control polygon. |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 422 | |
| 423 | if (constrainWithinTangents) { |
| 424 | SkVector da = p[0] - p[3]; |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 425 | bool doQuads = dc.lengthSqd() < SK_ScalarNearlyZero || |
| 426 | ab.lengthSqd() < SK_ScalarNearlyZero; |
| 427 | if (!doQuads) { |
| 428 | SkScalar invDALengthSqd = da.lengthSqd(); |
| 429 | if (invDALengthSqd > SK_ScalarNearlyZero) { |
| 430 | invDALengthSqd = SkScalarInvert(invDALengthSqd); |
| 431 | // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a. |
| 432 | // same goes for point c using vector cd. |
| 433 | SkScalar detABSqd = ab.cross(da); |
| 434 | detABSqd = SkScalarSquare(detABSqd); |
| 435 | SkScalar detDCSqd = dc.cross(da); |
| 436 | detDCSqd = SkScalarSquare(detDCSqd); |
| 437 | if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd && |
| 438 | SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) { |
| 439 | doQuads = true; |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 440 | } |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 443 | if (doQuads) { |
| 444 | SkPoint b = p[0] + ab; |
| 445 | SkPoint c = p[3] + dc; |
| 446 | SkPoint mid = b + c; |
| 447 | mid.scale(SK_ScalarHalf); |
| 448 | // Insert two quadratics to cover the case when ab points away from d and/or dc |
| 449 | // points away from a. |
| 450 | if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) { |
| 451 | SkPoint* qpts = quads->push_back_n(6); |
| 452 | qpts[0] = p[0]; |
| 453 | qpts[1] = b; |
| 454 | qpts[2] = mid; |
| 455 | qpts[3] = mid; |
| 456 | qpts[4] = c; |
| 457 | qpts[5] = p[3]; |
| 458 | } else { |
| 459 | SkPoint* qpts = quads->push_back_n(3); |
| 460 | qpts[0] = p[0]; |
| 461 | qpts[1] = mid; |
| 462 | qpts[2] = p[3]; |
| 463 | } |
| 464 | return; |
| 465 | } |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 466 | } |
| 467 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 468 | static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 469 | static const int kMaxSubdivs = 10; |
| 470 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 471 | ab.scale(kLengthScale); |
| 472 | dc.scale(kLengthScale); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 473 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 474 | // e0 and e1 are extrapolations along vectors ab and dc. |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 475 | SkVector c0 = p[0]; |
| 476 | c0 += ab; |
| 477 | SkVector c1 = p[3]; |
| 478 | c1 += dc; |
| 479 | |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 480 | SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 481 | if (dSqd < toleranceSqd) { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 482 | SkPoint cAvg = c0; |
| 483 | cAvg += c1; |
| 484 | cAvg.scale(SK_ScalarHalf); |
| 485 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 486 | bool subdivide = false; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 487 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 488 | if (constrainWithinTangents && |
| 489 | !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] | 490 | // 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] | 491 | ab.setOrthog(ab); |
| 492 | SkScalar z0 = -ab.dot(p[0]); |
| 493 | dc.setOrthog(dc); |
| 494 | SkScalar z1 = -dc.dot(p[3]); |
| 495 | cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY); |
| 496 | cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1); |
| 497 | SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX); |
| 498 | z = SkScalarInvert(z); |
| 499 | cAvg.fX *= z; |
| 500 | cAvg.fY *= z; |
| 501 | if (sublevel <= kMaxSubdivs) { |
| 502 | SkScalar d0Sqd = c0.distanceToSqd(cAvg); |
| 503 | SkScalar d1Sqd = c1.distanceToSqd(cAvg); |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 504 | // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know |
| 505 | // the distances and tolerance can't be negative. |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 506 | // (d0 + d1)^2 > toleranceSqd |
| 507 | // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd |
| 508 | SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd)); |
| 509 | subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd; |
| 510 | } |
| 511 | } |
| 512 | if (!subdivide) { |
| 513 | SkPoint* pts = quads->push_back_n(3); |
| 514 | pts[0] = p[0]; |
| 515 | pts[1] = cAvg; |
| 516 | pts[2] = p[3]; |
| 517 | return; |
| 518 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 519 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 520 | SkPoint choppedPts[7]; |
| 521 | SkChopCubicAtHalf(p, choppedPts); |
| 522 | convert_noninflect_cubic_to_quads(choppedPts + 0, |
| 523 | toleranceSqd, |
| 524 | constrainWithinTangents, |
| 525 | dir, |
| 526 | quads, |
| 527 | sublevel + 1); |
| 528 | convert_noninflect_cubic_to_quads(choppedPts + 3, |
| 529 | toleranceSqd, |
| 530 | constrainWithinTangents, |
| 531 | dir, |
| 532 | quads, |
| 533 | sublevel + 1); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 537 | void GrPathUtils::convertCubicToQuads(const SkPoint p[4], |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 538 | SkScalar tolScale, |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 539 | bool constrainWithinTangents, |
| 540 | SkPath::Direction dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 541 | SkTArray<SkPoint, true>* quads) { |
| 542 | SkPoint chopped[10]; |
| 543 | int count = SkChopCubicAtInflections(p, chopped); |
| 544 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 545 | // base tolerance is 1 pixel. |
| 546 | static const SkScalar kTolerance = SK_Scalar1; |
| 547 | const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance)); |
| 548 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 549 | for (int i = 0; i < count; ++i) { |
| 550 | SkPoint* cubic = chopped + 3*i; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 551 | convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | } |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 555 | |
| 556 | //////////////////////////////////////////////////////////////////////////////// |
| 557 | |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 558 | // Solves linear system to extract klm |
| 559 | // P.K = k (similarly for l, m) |
| 560 | // Where P is matrix of control points |
| 561 | // K is coefficients for the line K |
| 562 | // k is vector of values of K evaluated at the control points |
| 563 | // Solving for K, thus K = P^(-1) . k |
| 564 | static void calc_cubic_klm(const SkPoint p[4], const SkScalar controlK[4], |
| 565 | const SkScalar controlL[4], const SkScalar controlM[4], |
| 566 | SkScalar k[3], SkScalar l[3], SkScalar m[3]) { |
| 567 | SkMatrix matrix; |
| 568 | matrix.setAll(p[0].fX, p[0].fY, 1.f, |
| 569 | p[1].fX, p[1].fY, 1.f, |
| 570 | p[2].fX, p[2].fY, 1.f); |
| 571 | SkMatrix inverse; |
| 572 | if (matrix.invert(&inverse)) { |
| 573 | inverse.mapHomogeneousPoints(k, controlK, 1); |
| 574 | inverse.mapHomogeneousPoints(l, controlL, 1); |
| 575 | inverse.mapHomogeneousPoints(m, controlM, 1); |
| 576 | } |
| 577 | |
| 578 | } |
| 579 | |
| 580 | static void set_serp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 581 | SkScalar tempSqrt = SkScalarSqrt(9.f * d[1] * d[1] - 12.f * d[0] * d[2]); |
| 582 | SkScalar ls = 3.f * d[1] - tempSqrt; |
| 583 | SkScalar lt = 6.f * d[0]; |
| 584 | SkScalar ms = 3.f * d[1] + tempSqrt; |
| 585 | SkScalar mt = 6.f * d[0]; |
| 586 | |
| 587 | k[0] = ls * ms; |
| 588 | k[1] = (3.f * ls * ms - ls * mt - lt * ms) / 3.f; |
| 589 | k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; |
| 590 | k[3] = (lt - ls) * (mt - ms); |
| 591 | |
| 592 | l[0] = ls * ls * ls; |
| 593 | const SkScalar lt_ls = lt - ls; |
| 594 | l[1] = ls * ls * lt_ls * -1.f; |
| 595 | l[2] = lt_ls * lt_ls * ls; |
| 596 | l[3] = -1.f * lt_ls * lt_ls * lt_ls; |
| 597 | |
| 598 | m[0] = ms * ms * ms; |
| 599 | const SkScalar mt_ms = mt - ms; |
| 600 | m[1] = ms * ms * mt_ms * -1.f; |
| 601 | m[2] = mt_ms * mt_ms * ms; |
| 602 | m[3] = -1.f * mt_ms * mt_ms * mt_ms; |
| 603 | |
| 604 | // If d0 < 0 we need to flip the orientation of our curve |
| 605 | // This is done by negating the k and l values |
| 606 | // We want negative distance values to be on the inside |
| 607 | if ( d[0] > 0) { |
| 608 | for (int i = 0; i < 4; ++i) { |
| 609 | k[i] = -k[i]; |
| 610 | l[i] = -l[i]; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 616 | SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); |
| 617 | SkScalar ls = d[1] - tempSqrt; |
| 618 | SkScalar lt = 2.f * d[0]; |
| 619 | SkScalar ms = d[1] + tempSqrt; |
| 620 | SkScalar mt = 2.f * d[0]; |
| 621 | |
| 622 | k[0] = ls * ms; |
| 623 | k[1] = (3.f * ls*ms - ls * mt - lt * ms) / 3.f; |
| 624 | k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f; |
| 625 | k[3] = (lt - ls) * (mt - ms); |
| 626 | |
| 627 | l[0] = ls * ls * ms; |
| 628 | l[1] = (ls * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/-3.f; |
| 629 | l[2] = ((lt - ls) * (ls * (2.f * mt - 3.f * ms) + lt * ms))/3.f; |
| 630 | l[3] = -1.f * (lt - ls) * (lt - ls) * (mt - ms); |
| 631 | |
| 632 | m[0] = ls * ms * ms; |
| 633 | m[1] = (ms * (ls * (2.f * mt - 3.f * ms) + lt * ms))/-3.f; |
| 634 | m[2] = ((mt - ms) * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/3.f; |
| 635 | m[3] = -1.f * (lt - ls) * (mt - ms) * (mt - ms); |
| 636 | |
| 637 | |
| 638 | // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0), |
| 639 | // we need to flip the orientation of our curve. |
| 640 | // This is done by negating the k and l values |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 641 | 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] | 642 | for (int i = 0; i < 4; ++i) { |
| 643 | k[i] = -k[i]; |
| 644 | l[i] = -l[i]; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | static void set_cusp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 650 | const SkScalar ls = d[2]; |
| 651 | const SkScalar lt = 3.f * d[1]; |
| 652 | |
| 653 | k[0] = ls; |
| 654 | k[1] = ls - lt / 3.f; |
| 655 | k[2] = ls - 2.f * lt / 3.f; |
| 656 | k[3] = ls - lt; |
| 657 | |
| 658 | l[0] = ls * ls * ls; |
| 659 | const SkScalar ls_lt = ls - lt; |
| 660 | l[1] = ls * ls * ls_lt; |
| 661 | l[2] = ls_lt * ls_lt * ls; |
| 662 | l[3] = ls_lt * ls_lt * ls_lt; |
| 663 | |
| 664 | m[0] = 1.f; |
| 665 | m[1] = 1.f; |
| 666 | m[2] = 1.f; |
| 667 | m[3] = 1.f; |
| 668 | } |
| 669 | |
| 670 | // For the case when a cubic is actually a quadratic |
| 671 | // M = |
| 672 | // 0 0 0 |
| 673 | // 1/3 0 1/3 |
| 674 | // 2/3 1/3 2/3 |
| 675 | // 1 1 1 |
| 676 | static void set_quadratic_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) { |
| 677 | k[0] = 0.f; |
| 678 | k[1] = 1.f/3.f; |
| 679 | k[2] = 2.f/3.f; |
| 680 | k[3] = 1.f; |
| 681 | |
| 682 | l[0] = 0.f; |
| 683 | l[1] = 0.f; |
| 684 | l[2] = 1.f/3.f; |
| 685 | l[3] = 1.f; |
| 686 | |
| 687 | m[0] = 0.f; |
| 688 | m[1] = 1.f/3.f; |
| 689 | m[2] = 2.f/3.f; |
| 690 | m[3] = 1.f; |
| 691 | |
| 692 | // If d2 < 0 we need to flip the orientation of our curve |
| 693 | // This is done by negating the k and l values |
| 694 | if ( d[2] > 0) { |
| 695 | for (int i = 0; i < 4; ++i) { |
| 696 | k[i] = -k[i]; |
| 697 | l[i] = -l[i]; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 702 | int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkScalar klm[9], |
| 703 | SkScalar klm_rev[3]) { |
| 704 | // Variable to store the two parametric values at the loop double point |
| 705 | SkScalar smallS = 0.f; |
| 706 | SkScalar largeS = 0.f; |
| 707 | |
| 708 | SkScalar d[3]; |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 709 | SkCubicType cType = SkClassifyCubic(src, d); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 710 | |
| 711 | int chop_count = 0; |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 712 | if (kLoop_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 713 | SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]); |
| 714 | SkScalar ls = d[1] - tempSqrt; |
| 715 | SkScalar lt = 2.f * d[0]; |
| 716 | SkScalar ms = d[1] + tempSqrt; |
| 717 | SkScalar mt = 2.f * d[0]; |
| 718 | ls = ls / lt; |
| 719 | ms = ms / mt; |
| 720 | // need to have t values sorted since this is what is expected by SkChopCubicAt |
| 721 | if (ls <= ms) { |
| 722 | smallS = ls; |
| 723 | largeS = ms; |
| 724 | } else { |
| 725 | smallS = ms; |
| 726 | largeS = ls; |
| 727 | } |
| 728 | |
| 729 | SkScalar chop_ts[2]; |
| 730 | if (smallS > 0.f && smallS < 1.f) { |
| 731 | chop_ts[chop_count++] = smallS; |
| 732 | } |
| 733 | if (largeS > 0.f && largeS < 1.f) { |
| 734 | chop_ts[chop_count++] = largeS; |
| 735 | } |
| 736 | if(dst) { |
| 737 | SkChopCubicAt(src, dst, chop_ts, chop_count); |
| 738 | } |
| 739 | } else { |
| 740 | if (dst) { |
| 741 | memcpy(dst, src, sizeof(SkPoint) * 4); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | if (klm && klm_rev) { |
| 746 | // Set klm_rev to to match the sub_section of cubic that needs to have its orientation |
| 747 | // flipped. This will always be the section that is the "loop" |
| 748 | if (2 == chop_count) { |
| 749 | klm_rev[0] = 1.f; |
| 750 | klm_rev[1] = -1.f; |
| 751 | klm_rev[2] = 1.f; |
| 752 | } else if (1 == chop_count) { |
| 753 | if (smallS < 0.f) { |
| 754 | klm_rev[0] = -1.f; |
| 755 | klm_rev[1] = 1.f; |
| 756 | } else { |
| 757 | klm_rev[0] = 1.f; |
| 758 | klm_rev[1] = -1.f; |
| 759 | } |
| 760 | } else { |
| 761 | if (smallS < 0.f && largeS > 1.f) { |
| 762 | klm_rev[0] = -1.f; |
| 763 | } else { |
| 764 | klm_rev[0] = 1.f; |
| 765 | } |
| 766 | } |
| 767 | SkScalar controlK[4]; |
| 768 | SkScalar controlL[4]; |
| 769 | SkScalar controlM[4]; |
| 770 | |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 771 | 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] | 772 | set_serp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 773 | } else if (kLoop_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 774 | set_loop_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 775 | } else if (kCusp_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 776 | SkASSERT(0.f == d[0]); |
| 777 | set_cusp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 778 | } else if (kQuadratic_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 779 | set_quadratic_klm(d, controlK, controlL, controlM); |
| 780 | } |
| 781 | |
| 782 | calc_cubic_klm(src, controlK, controlL, controlM, klm, &klm[3], &klm[6]); |
| 783 | } |
| 784 | return chop_count + 1; |
| 785 | } |
| 786 | |
| 787 | void GrPathUtils::getCubicKLM(const SkPoint p[4], SkScalar klm[9]) { |
| 788 | SkScalar d[3]; |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 789 | SkCubicType cType = SkClassifyCubic(p, d); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 790 | |
| 791 | SkScalar controlK[4]; |
| 792 | SkScalar controlL[4]; |
| 793 | SkScalar controlM[4]; |
| 794 | |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 795 | 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] | 796 | set_serp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 797 | } else if (kLoop_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 798 | set_loop_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 799 | } else if (kCusp_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 800 | SkASSERT(0.f == d[0]); |
| 801 | set_cusp_klm(d, controlK, controlL, controlM); |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 802 | } else if (kQuadratic_SkCubicType == cType) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 803 | set_quadratic_klm(d, controlK, controlL, controlM); |
| 804 | } |
| 805 | |
| 806 | calc_cubic_klm(p, controlK, controlL, controlM, klm, &klm[3], &klm[6]); |
| 807 | } |