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