epoger@google.com | 685cfc0 | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkGeometry.h" |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkMatrix.h" |
| 10 | |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 11 | bool SkXRayCrossesLine(const SkXRay& pt, |
| 12 | const SkPoint pts[2], |
| 13 | bool* ambiguous) { |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 14 | if (ambiguous) { |
| 15 | *ambiguous = false; |
| 16 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 17 | // Determine quick discards. |
| 18 | // Consider query line going exactly through point 0 to not |
| 19 | // intersect, for symmetry with SkXRayCrossesMonotonicCubic. |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 20 | if (pt.fY == pts[0].fY) { |
| 21 | if (ambiguous) { |
| 22 | *ambiguous = true; |
| 23 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 24 | return false; |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 25 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 26 | if (pt.fY < pts[0].fY && pt.fY < pts[1].fY) |
| 27 | return false; |
| 28 | if (pt.fY > pts[0].fY && pt.fY > pts[1].fY) |
| 29 | return false; |
| 30 | if (pt.fX > pts[0].fX && pt.fX > pts[1].fX) |
| 31 | return false; |
| 32 | // Determine degenerate cases |
| 33 | if (SkScalarNearlyZero(pts[0].fY - pts[1].fY)) |
| 34 | return false; |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 35 | if (SkScalarNearlyZero(pts[0].fX - pts[1].fX)) { |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 36 | // We've already determined the query point lies within the |
| 37 | // vertical range of the line segment. |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 38 | if (pt.fX <= pts[0].fX) { |
| 39 | if (ambiguous) { |
| 40 | *ambiguous = (pt.fY == pts[1].fY); |
| 41 | } |
| 42 | return true; |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | // Ambiguity check |
| 47 | if (pt.fY == pts[1].fY) { |
| 48 | if (pt.fX <= pts[1].fX) { |
| 49 | if (ambiguous) { |
| 50 | *ambiguous = true; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 56 | // Full line segment evaluation |
| 57 | SkScalar delta_y = pts[1].fY - pts[0].fY; |
| 58 | SkScalar delta_x = pts[1].fX - pts[0].fX; |
| 59 | SkScalar slope = SkScalarDiv(delta_y, delta_x); |
| 60 | SkScalar b = pts[0].fY - SkScalarMul(slope, pts[0].fX); |
| 61 | // Solve for x coordinate at y = pt.fY |
| 62 | SkScalar x = SkScalarDiv(pt.fY - b, slope); |
| 63 | return pt.fX <= x; |
| 64 | } |
| 65 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | /** If defined, this makes eval_quad and eval_cubic do more setup (sometimes |
| 67 | involving integer multiplies by 2 or 3, but fewer calls to SkScalarMul. |
| 68 | May also introduce overflow of fixed when we compute our setup. |
| 69 | */ |
reed@google.com | 3c541f9 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 70 | // #define DIRECT_EVAL_OF_POLYNOMIALS |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
| 72 | //////////////////////////////////////////////////////////////////////// |
| 73 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 74 | static int is_not_monotonic(SkScalar a, SkScalar b, SkScalar c) { |
| 75 | SkScalar ab = a - b; |
| 76 | SkScalar bc = b - c; |
reed@google.com | 3c541f9 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 77 | if (ab < 0) { |
| 78 | bc = -bc; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
reed@google.com | 3c541f9 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 80 | return ab == 0 || bc < 0; |
| 81 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | |
| 83 | //////////////////////////////////////////////////////////////////////// |
| 84 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 85 | static bool is_unit_interval(SkScalar x) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | return x > 0 && x < SK_Scalar1; |
| 87 | } |
| 88 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 89 | static int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | SkASSERT(ratio); |
| 91 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 92 | if (numer < 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | numer = -numer; |
| 94 | denom = -denom; |
| 95 | } |
| 96 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 97 | if (denom == 0 || numer == 0 || numer >= denom) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | return 0; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 99 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | |
| 101 | SkScalar r = SkScalarDiv(numer, denom); |
reed@android.com | 7c83e1c | 2010-03-08 17:44:42 +0000 | [diff] [blame] | 102 | if (SkScalarIsNaN(r)) { |
| 103 | return 0; |
| 104 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | SkASSERT(r >= 0 && r < SK_Scalar1); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 106 | if (r == 0) { // catch underflow if numer <<<< denom |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | return 0; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 108 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | *ratio = r; |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | /** From Numerical Recipes in C. |
| 114 | |
| 115 | Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C]) |
| 116 | x1 = Q / A |
| 117 | x2 = C / Q |
| 118 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 119 | int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | SkASSERT(roots); |
| 121 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 122 | if (A == 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | return valid_unit_divide(-C, B, roots); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 124 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | |
| 126 | SkScalar* r = roots; |
| 127 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 128 | SkScalar R = B*B - 4*A*C; |
reed@android.com | 7c83e1c | 2010-03-08 17:44:42 +0000 | [diff] [blame] | 129 | if (R < 0 || SkScalarIsNaN(R)) { // complex roots |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | return 0; |
reed@android.com | 7c83e1c | 2010-03-08 17:44:42 +0000 | [diff] [blame] | 131 | } |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 132 | R = SkScalarSqrt(R); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | |
| 134 | SkScalar Q = (B < 0) ? -(B-R)/2 : -(B+R)/2; |
| 135 | r += valid_unit_divide(Q, A, r); |
| 136 | r += valid_unit_divide(C, Q, r); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 137 | if (r - roots == 2) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | if (roots[0] > roots[1]) |
| 139 | SkTSwap<SkScalar>(roots[0], roots[1]); |
| 140 | else if (roots[0] == roots[1]) // nearly-equal? |
| 141 | r -= 1; // skip the double root |
| 142 | } |
| 143 | return (int)(r - roots); |
| 144 | } |
| 145 | |
reed@google.com | 3c541f9 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 146 | /////////////////////////////////////////////////////////////////////////////// |
| 147 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 149 | static SkScalar eval_quad(const SkScalar src[], SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | SkASSERT(src); |
| 151 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
| 152 | |
| 153 | #ifdef DIRECT_EVAL_OF_POLYNOMIALS |
| 154 | SkScalar C = src[0]; |
| 155 | SkScalar A = src[4] - 2 * src[2] + C; |
| 156 | SkScalar B = 2 * (src[2] - C); |
| 157 | return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); |
| 158 | #else |
| 159 | SkScalar ab = SkScalarInterp(src[0], src[2], t); |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 160 | SkScalar bc = SkScalarInterp(src[2], src[4], t); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | return SkScalarInterp(ab, bc, t); |
| 162 | #endif |
| 163 | } |
| 164 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 165 | static SkScalar eval_quad_derivative(const SkScalar src[], SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | SkScalar A = src[4] - 2 * src[2] + src[0]; |
| 167 | SkScalar B = src[2] - src[0]; |
| 168 | |
| 169 | return 2 * SkScalarMulAdd(A, t, B); |
| 170 | } |
| 171 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 172 | static SkScalar eval_quad_derivative_at_half(const SkScalar src[]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | SkScalar A = src[4] - 2 * src[2] + src[0]; |
| 174 | SkScalar B = src[2] - src[0]; |
| 175 | return A + 2 * B; |
| 176 | } |
| 177 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 178 | void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, |
| 179 | SkVector* tangent) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | SkASSERT(src); |
| 181 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
| 182 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 183 | if (pt) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | pt->set(eval_quad(&src[0].fX, t), eval_quad(&src[0].fY, t)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 185 | } |
| 186 | if (tangent) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | tangent->set(eval_quad_derivative(&src[0].fX, t), |
| 188 | eval_quad_derivative(&src[0].fY, t)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 189 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 192 | void SkEvalQuadAtHalf(const SkPoint src[3], SkPoint* pt, SkVector* tangent) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | SkASSERT(src); |
| 194 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 195 | if (pt) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); |
| 197 | SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); |
| 198 | SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); |
| 199 | SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); |
| 200 | pt->set(SkScalarAve(x01, x12), SkScalarAve(y01, y12)); |
| 201 | } |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 202 | if (tangent) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | tangent->set(eval_quad_derivative_at_half(&src[0].fX), |
| 204 | eval_quad_derivative_at_half(&src[0].fY)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 205 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | } |
| 207 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 208 | static void interp_quad_coords(const SkScalar* src, SkScalar* dst, SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | SkScalar ab = SkScalarInterp(src[0], src[2], t); |
| 210 | SkScalar bc = SkScalarInterp(src[2], src[4], t); |
| 211 | |
| 212 | dst[0] = src[0]; |
| 213 | dst[2] = ab; |
| 214 | dst[4] = SkScalarInterp(ab, bc, t); |
| 215 | dst[6] = bc; |
| 216 | dst[8] = src[4]; |
| 217 | } |
| 218 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 219 | void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | SkASSERT(t > 0 && t < SK_Scalar1); |
| 221 | |
| 222 | interp_quad_coords(&src[0].fX, &dst[0].fX, t); |
| 223 | interp_quad_coords(&src[0].fY, &dst[0].fY, t); |
| 224 | } |
| 225 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 226 | void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); |
| 228 | SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); |
| 229 | SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); |
| 230 | SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); |
| 231 | |
| 232 | dst[0] = src[0]; |
| 233 | dst[1].set(x01, y01); |
| 234 | dst[2].set(SkScalarAve(x01, x12), SkScalarAve(y01, y12)); |
| 235 | dst[3].set(x12, y12); |
| 236 | dst[4] = src[2]; |
| 237 | } |
| 238 | |
| 239 | /** Quad'(t) = At + B, where |
| 240 | A = 2(a - 2b + c) |
| 241 | B = 2(b - a) |
| 242 | Solve for t, only if it fits between 0 < t < 1 |
| 243 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 244 | int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValue[1]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | /* At + B == 0 |
| 246 | t = -B / A |
| 247 | */ |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | return valid_unit_divide(a - b, a - b - b + c, tValue); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | } |
| 250 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 251 | static inline void flatten_double_quad_extrema(SkScalar coords[14]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | coords[2] = coords[6] = coords[4]; |
| 253 | } |
| 254 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | /* Returns 0 for 1 quad, and 1 for two quads, either way the answer is |
reed@android.com | 001bd97 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 256 | stored in dst[]. Guarantees that the 1/2 quads will be monotonic. |
| 257 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 258 | int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | SkASSERT(src); |
| 260 | SkASSERT(dst); |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 261 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 262 | SkScalar a = src[0].fY; |
| 263 | SkScalar b = src[1].fY; |
| 264 | SkScalar c = src[2].fY; |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 265 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 266 | if (is_not_monotonic(a, b, c)) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | SkScalar tValue; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 268 | if (valid_unit_divide(a - b, a - b - b + c, &tValue)) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | SkChopQuadAt(src, dst, tValue); |
| 270 | flatten_double_quad_extrema(&dst[0].fY); |
| 271 | return 1; |
| 272 | } |
| 273 | // if we get here, we need to force dst to be monotonic, even though |
| 274 | // we couldn't compute a unit_divide value (probably underflow). |
| 275 | b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; |
| 276 | } |
| 277 | dst[0].set(src[0].fX, a); |
| 278 | dst[1].set(src[1].fX, b); |
| 279 | dst[2].set(src[2].fX, c); |
| 280 | return 0; |
| 281 | } |
| 282 | |
reed@android.com | 001bd97 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 283 | /* Returns 0 for 1 quad, and 1 for two quads, either way the answer is |
| 284 | stored in dst[]. Guarantees that the 1/2 quads will be monotonic. |
| 285 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 286 | int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]) { |
reed@android.com | 001bd97 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 287 | SkASSERT(src); |
| 288 | SkASSERT(dst); |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 289 | |
reed@android.com | 001bd97 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 290 | SkScalar a = src[0].fX; |
| 291 | SkScalar b = src[1].fX; |
| 292 | SkScalar c = src[2].fX; |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 293 | |
reed@android.com | 001bd97 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 294 | if (is_not_monotonic(a, b, c)) { |
| 295 | SkScalar tValue; |
| 296 | if (valid_unit_divide(a - b, a - b - b + c, &tValue)) { |
| 297 | SkChopQuadAt(src, dst, tValue); |
| 298 | flatten_double_quad_extrema(&dst[0].fX); |
| 299 | return 1; |
| 300 | } |
| 301 | // if we get here, we need to force dst to be monotonic, even though |
| 302 | // we couldn't compute a unit_divide value (probably underflow). |
| 303 | b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; |
| 304 | } |
| 305 | dst[0].set(a, src[0].fY); |
| 306 | dst[1].set(b, src[1].fY); |
| 307 | dst[2].set(c, src[2].fY); |
| 308 | return 0; |
| 309 | } |
| 310 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | // F(t) = a (1 - t) ^ 2 + 2 b t (1 - t) + c t ^ 2 |
| 312 | // F'(t) = 2 (b - a) + 2 (a - 2b + c) t |
| 313 | // F''(t) = 2 (a - 2b + c) |
| 314 | // |
| 315 | // A = 2 (b - a) |
| 316 | // B = 2 (a - 2b + c) |
| 317 | // |
| 318 | // Maximum curvature for a quadratic means solving |
| 319 | // Fx' Fx'' + Fy' Fy'' = 0 |
| 320 | // |
| 321 | // t = - (Ax Bx + Ay By) / (Bx ^ 2 + By ^ 2) |
| 322 | // |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 323 | SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 324 | SkScalar Ax = src[1].fX - src[0].fX; |
| 325 | SkScalar Ay = src[1].fY - src[0].fY; |
| 326 | SkScalar Bx = src[0].fX - src[1].fX - src[1].fX + src[2].fX; |
| 327 | SkScalar By = src[0].fY - src[1].fY - src[1].fY + src[2].fY; |
| 328 | SkScalar t = 0; // 0 means don't chop |
| 329 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | (void)valid_unit_divide(-(Ax * Bx + Ay * By), Bx * Bx + By * By, &t); |
egdaniel@google.com | 63cc6e1 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 331 | return t; |
| 332 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 334 | int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]) { |
egdaniel@google.com | 63cc6e1 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 335 | SkScalar t = SkFindQuadMaxCurvature(src); |
| 336 | if (t == 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | memcpy(dst, src, 3 * sizeof(SkPoint)); |
| 338 | return 1; |
egdaniel@google.com | 63cc6e1 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 339 | } else { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | SkChopQuadAt(src, dst, t); |
| 341 | return 2; |
| 342 | } |
| 343 | } |
| 344 | |
reed@google.com | 3c541f9 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 345 | #define SK_ScalarTwoThirds (0.666666666f) |
reed@google.com | 007593e | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 346 | |
| 347 | void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]) { |
| 348 | const SkScalar scale = SK_ScalarTwoThirds; |
| 349 | dst[0] = src[0]; |
| 350 | dst[1].set(src[0].fX + SkScalarMul(src[1].fX - src[0].fX, scale), |
| 351 | src[0].fY + SkScalarMul(src[1].fY - src[0].fY, scale)); |
| 352 | dst[2].set(src[2].fX + SkScalarMul(src[1].fX - src[2].fX, scale), |
| 353 | src[2].fY + SkScalarMul(src[1].fY - src[2].fY, scale)); |
| 354 | dst[3] = src[2]; |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 355 | } |
| 356 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 357 | ////////////////////////////////////////////////////////////////////////////// |
| 358 | ///// CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS ///// |
| 359 | ////////////////////////////////////////////////////////////////////////////// |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 361 | static void get_cubic_coeff(const SkScalar pt[], SkScalar coeff[4]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | coeff[0] = pt[6] + 3*(pt[2] - pt[4]) - pt[0]; |
| 363 | coeff[1] = 3*(pt[4] - pt[2] - pt[2] + pt[0]); |
| 364 | coeff[2] = 3*(pt[2] - pt[0]); |
| 365 | coeff[3] = pt[0]; |
| 366 | } |
| 367 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 368 | void SkGetCubicCoeff(const SkPoint pts[4], SkScalar cx[4], SkScalar cy[4]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | SkASSERT(pts); |
| 370 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 371 | if (cx) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | get_cubic_coeff(&pts[0].fX, cx); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 373 | } |
| 374 | if (cy) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | get_cubic_coeff(&pts[0].fY, cy); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 376 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | } |
| 378 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 379 | static SkScalar eval_cubic(const SkScalar src[], SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | SkASSERT(src); |
| 381 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
| 382 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 383 | if (t == 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 384 | return src[0]; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 385 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 386 | |
| 387 | #ifdef DIRECT_EVAL_OF_POLYNOMIALS |
| 388 | SkScalar D = src[0]; |
| 389 | SkScalar A = src[6] + 3*(src[2] - src[4]) - D; |
| 390 | SkScalar B = 3*(src[4] - src[2] - src[2] + D); |
| 391 | SkScalar C = 3*(src[2] - D); |
| 392 | |
| 393 | return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); |
| 394 | #else |
| 395 | SkScalar ab = SkScalarInterp(src[0], src[2], t); |
| 396 | SkScalar bc = SkScalarInterp(src[2], src[4], t); |
| 397 | SkScalar cd = SkScalarInterp(src[4], src[6], t); |
| 398 | SkScalar abc = SkScalarInterp(ab, bc, t); |
| 399 | SkScalar bcd = SkScalarInterp(bc, cd, t); |
| 400 | return SkScalarInterp(abc, bcd, t); |
| 401 | #endif |
| 402 | } |
| 403 | |
| 404 | /** return At^2 + Bt + C |
| 405 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 406 | static SkScalar eval_quadratic(SkScalar A, SkScalar B, SkScalar C, SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 407 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
| 408 | |
| 409 | return SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); |
| 410 | } |
| 411 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 412 | static SkScalar eval_cubic_derivative(const SkScalar src[], SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 413 | SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0]; |
| 414 | SkScalar B = 2*(src[4] - 2 * src[2] + src[0]); |
| 415 | SkScalar C = src[2] - src[0]; |
| 416 | |
| 417 | return eval_quadratic(A, B, C, t); |
| 418 | } |
| 419 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 420 | static SkScalar eval_cubic_2ndDerivative(const SkScalar src[], SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 421 | SkScalar A = src[6] + 3*(src[2] - src[4]) - src[0]; |
| 422 | SkScalar B = src[4] - 2 * src[2] + src[0]; |
| 423 | |
| 424 | return SkScalarMulAdd(A, t, B); |
| 425 | } |
| 426 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 427 | void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* loc, |
| 428 | SkVector* tangent, SkVector* curvature) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 429 | SkASSERT(src); |
| 430 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
| 431 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 432 | if (loc) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | loc->set(eval_cubic(&src[0].fX, t), eval_cubic(&src[0].fY, t)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 434 | } |
| 435 | if (tangent) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 436 | tangent->set(eval_cubic_derivative(&src[0].fX, t), |
| 437 | eval_cubic_derivative(&src[0].fY, t)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 438 | } |
| 439 | if (curvature) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | curvature->set(eval_cubic_2ndDerivative(&src[0].fX, t), |
| 441 | eval_cubic_2ndDerivative(&src[0].fY, t)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 442 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** Cubic'(t) = At^2 + Bt + C, where |
| 446 | A = 3(-a + 3(b - c) + d) |
| 447 | B = 6(a - 2b + c) |
| 448 | C = 3(b - a) |
| 449 | Solve for t, keeping only those that fit betwee 0 < t < 1 |
| 450 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 451 | int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, |
| 452 | SkScalar tValues[2]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 453 | // we divide A,B,C by 3 to simplify |
| 454 | SkScalar A = d - a + 3*(b - c); |
| 455 | SkScalar B = 2*(a - b - b + c); |
| 456 | SkScalar C = b - a; |
| 457 | |
| 458 | return SkFindUnitQuadRoots(A, B, C, tValues); |
| 459 | } |
| 460 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 461 | static void interp_cubic_coords(const SkScalar* src, SkScalar* dst, |
| 462 | SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 463 | SkScalar ab = SkScalarInterp(src[0], src[2], t); |
| 464 | SkScalar bc = SkScalarInterp(src[2], src[4], t); |
| 465 | SkScalar cd = SkScalarInterp(src[4], src[6], t); |
| 466 | SkScalar abc = SkScalarInterp(ab, bc, t); |
| 467 | SkScalar bcd = SkScalarInterp(bc, cd, t); |
| 468 | SkScalar abcd = SkScalarInterp(abc, bcd, t); |
| 469 | |
| 470 | dst[0] = src[0]; |
| 471 | dst[2] = ab; |
| 472 | dst[4] = abc; |
| 473 | dst[6] = abcd; |
| 474 | dst[8] = bcd; |
| 475 | dst[10] = cd; |
| 476 | dst[12] = src[6]; |
| 477 | } |
| 478 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 479 | void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 480 | SkASSERT(t > 0 && t < SK_Scalar1); |
| 481 | |
| 482 | interp_cubic_coords(&src[0].fX, &dst[0].fX, t); |
| 483 | interp_cubic_coords(&src[0].fY, &dst[0].fY, t); |
| 484 | } |
| 485 | |
reed@android.com | 17bdc09 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 486 | /* http://code.google.com/p/skia/issues/detail?id=32 |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 487 | |
reed@android.com | 17bdc09 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 488 | This test code would fail when we didn't check the return result of |
| 489 | valid_unit_divide in SkChopCubicAt(... tValues[], int roots). The reason is |
| 490 | that after the first chop, the parameters to valid_unit_divide are equal |
| 491 | (thanks to finite float precision and rounding in the subtracts). Thus |
| 492 | even though the 2nd tValue looks < 1.0, after we renormalize it, we end |
| 493 | up with 1.0, hence the need to check and just return the last cubic as |
| 494 | a degenerate clump of 4 points in the sampe place. |
| 495 | |
| 496 | static void test_cubic() { |
| 497 | SkPoint src[4] = { |
| 498 | { 556.25000, 523.03003 }, |
| 499 | { 556.23999, 522.96002 }, |
| 500 | { 556.21997, 522.89001 }, |
| 501 | { 556.21997, 522.82001 } |
| 502 | }; |
| 503 | SkPoint dst[10]; |
| 504 | SkScalar tval[] = { 0.33333334f, 0.99999994f }; |
| 505 | SkChopCubicAt(src, dst, tval, 2); |
| 506 | } |
| 507 | */ |
| 508 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 509 | void SkChopCubicAt(const SkPoint src[4], SkPoint dst[], |
| 510 | const SkScalar tValues[], int roots) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 511 | #ifdef SK_DEBUG |
| 512 | { |
| 513 | for (int i = 0; i < roots - 1; i++) |
| 514 | { |
| 515 | SkASSERT(is_unit_interval(tValues[i])); |
| 516 | SkASSERT(is_unit_interval(tValues[i+1])); |
| 517 | SkASSERT(tValues[i] < tValues[i+1]); |
| 518 | } |
| 519 | } |
| 520 | #endif |
| 521 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 522 | if (dst) { |
| 523 | if (roots == 0) { // nothing to chop |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | memcpy(dst, src, 4*sizeof(SkPoint)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 525 | } else { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | SkScalar t = tValues[0]; |
| 527 | SkPoint tmp[4]; |
| 528 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 529 | for (int i = 0; i < roots; i++) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | SkChopCubicAt(src, dst, t); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 531 | if (i == roots - 1) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 532 | break; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 533 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 534 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | dst += 3; |
reed@android.com | 17bdc09 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 536 | // have src point to the remaining cubic (after the chop) |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | memcpy(tmp, dst, 4 * sizeof(SkPoint)); |
| 538 | src = tmp; |
reed@android.com | 17bdc09 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 539 | |
| 540 | // watch out in case the renormalized t isn't in range |
| 541 | if (!valid_unit_divide(tValues[i+1] - tValues[i], |
| 542 | SK_Scalar1 - tValues[i], &t)) { |
| 543 | // if we can't, just create a degenerate cubic |
| 544 | dst[4] = dst[5] = dst[6] = src[3]; |
| 545 | break; |
| 546 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 552 | void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 553 | SkScalar x01 = SkScalarAve(src[0].fX, src[1].fX); |
| 554 | SkScalar y01 = SkScalarAve(src[0].fY, src[1].fY); |
| 555 | SkScalar x12 = SkScalarAve(src[1].fX, src[2].fX); |
| 556 | SkScalar y12 = SkScalarAve(src[1].fY, src[2].fY); |
| 557 | SkScalar x23 = SkScalarAve(src[2].fX, src[3].fX); |
| 558 | SkScalar y23 = SkScalarAve(src[2].fY, src[3].fY); |
| 559 | |
| 560 | SkScalar x012 = SkScalarAve(x01, x12); |
| 561 | SkScalar y012 = SkScalarAve(y01, y12); |
| 562 | SkScalar x123 = SkScalarAve(x12, x23); |
| 563 | SkScalar y123 = SkScalarAve(y12, y23); |
| 564 | |
| 565 | dst[0] = src[0]; |
| 566 | dst[1].set(x01, y01); |
| 567 | dst[2].set(x012, y012); |
| 568 | dst[3].set(SkScalarAve(x012, x123), SkScalarAve(y012, y123)); |
| 569 | dst[4].set(x123, y123); |
| 570 | dst[5].set(x23, y23); |
| 571 | dst[6] = src[3]; |
| 572 | } |
| 573 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 574 | static void flatten_double_cubic_extrema(SkScalar coords[14]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 575 | coords[4] = coords[8] = coords[6]; |
| 576 | } |
| 577 | |
| 578 | /** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 579 | the resulting beziers are monotonic in Y. This is called by the scan |
| 580 | converter. Depending on what is returned, dst[] is treated as follows: |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 581 | 0 dst[0..3] is the original cubic |
| 582 | 1 dst[0..3] and dst[3..6] are the two new cubics |
| 583 | 2 dst[0..3], dst[3..6], dst[6..9] are the three new cubics |
| 584 | If dst == null, it is ignored and only the count is returned. |
| 585 | */ |
reed@android.com | 68779c3 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 586 | int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 587 | SkScalar tValues[2]; |
reed@android.com | 68779c3 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 588 | int roots = SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY, |
| 589 | src[3].fY, tValues); |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 590 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 591 | SkChopCubicAt(src, dst, tValues, roots); |
reed@android.com | 68779c3 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 592 | if (dst && roots > 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 593 | // we do some cleanup to ensure our Y extrema are flat |
| 594 | flatten_double_cubic_extrema(&dst[0].fY); |
reed@android.com | 68779c3 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 595 | if (roots == 2) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 596 | flatten_double_cubic_extrema(&dst[3].fY); |
reed@android.com | 68779c3 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | return roots; |
| 600 | } |
| 601 | |
| 602 | int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]) { |
| 603 | SkScalar tValues[2]; |
| 604 | int roots = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX, |
| 605 | src[3].fX, tValues); |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 606 | |
reed@android.com | 68779c3 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 607 | SkChopCubicAt(src, dst, tValues, roots); |
| 608 | if (dst && roots > 0) { |
| 609 | // we do some cleanup to ensure our Y extrema are flat |
| 610 | flatten_double_cubic_extrema(&dst[0].fX); |
| 611 | if (roots == 2) { |
| 612 | flatten_double_cubic_extrema(&dst[3].fX); |
| 613 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 614 | } |
| 615 | return roots; |
| 616 | } |
| 617 | |
| 618 | /** http://www.faculty.idc.ac.il/arik/quality/appendixA.html |
| 619 | |
| 620 | Inflection means that curvature is zero. |
| 621 | Curvature is [F' x F''] / [F'^3] |
| 622 | So we solve F'x X F''y - F'y X F''y == 0 |
| 623 | After some canceling of the cubic term, we get |
| 624 | A = b - a |
| 625 | B = c - 2b + a |
| 626 | C = d - 3c + 3b - a |
| 627 | (BxCy - ByCx)t^2 + (AxCy - AyCx)t + AxBy - AyBx == 0 |
| 628 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 629 | int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 630 | SkScalar Ax = src[1].fX - src[0].fX; |
| 631 | SkScalar Ay = src[1].fY - src[0].fY; |
| 632 | SkScalar Bx = src[2].fX - 2 * src[1].fX + src[0].fX; |
| 633 | SkScalar By = src[2].fY - 2 * src[1].fY + src[0].fY; |
| 634 | SkScalar Cx = src[3].fX + 3 * (src[1].fX - src[2].fX) - src[0].fX; |
| 635 | SkScalar Cy = src[3].fY + 3 * (src[1].fY - src[2].fY) - src[0].fY; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 636 | |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 637 | return SkFindUnitQuadRoots(Bx*Cy - By*Cx, |
| 638 | Ax*Cy - Ay*Cx, |
| 639 | Ax*By - Ay*Bx, |
| 640 | tValues); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 641 | } |
| 642 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 643 | int SkChopCubicAtInflections(const SkPoint src[], SkPoint dst[10]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 644 | SkScalar tValues[2]; |
| 645 | int count = SkFindCubicInflections(src, tValues); |
| 646 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 647 | if (dst) { |
| 648 | if (count == 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 649 | memcpy(dst, src, 4 * sizeof(SkPoint)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 650 | } else { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 651 | SkChopCubicAt(src, dst, tValues, count); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 652 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 653 | } |
| 654 | return count + 1; |
| 655 | } |
| 656 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 657 | template <typename T> void bubble_sort(T array[], int count) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 658 | for (int i = count - 1; i > 0; --i) |
| 659 | for (int j = i; j > 0; --j) |
| 660 | if (array[j] < array[j-1]) |
| 661 | { |
| 662 | T tmp(array[j]); |
| 663 | array[j] = array[j-1]; |
| 664 | array[j-1] = tmp; |
| 665 | } |
| 666 | } |
| 667 | |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 668 | /** |
| 669 | * Given an array and count, remove all pair-wise duplicates from the array, |
| 670 | * keeping the existing sorting, and return the new count |
| 671 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 672 | static int collaps_duplicates(SkScalar array[], int count) { |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 673 | for (int n = count; n > 1; --n) { |
| 674 | if (array[0] == array[1]) { |
| 675 | for (int i = 1; i < n; ++i) { |
| 676 | array[i - 1] = array[i]; |
| 677 | } |
| 678 | count -= 1; |
| 679 | } else { |
| 680 | array += 1; |
| 681 | } |
| 682 | } |
| 683 | return count; |
| 684 | } |
| 685 | |
| 686 | #ifdef SK_DEBUG |
| 687 | |
| 688 | #define TEST_COLLAPS_ENTRY(array) array, SK_ARRAY_COUNT(array) |
| 689 | |
| 690 | static void test_collaps_duplicates() { |
| 691 | static bool gOnce; |
| 692 | if (gOnce) { return; } |
| 693 | gOnce = true; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 694 | const SkScalar src0[] = { 0 }; |
| 695 | const SkScalar src1[] = { 0, 0 }; |
| 696 | const SkScalar src2[] = { 0, 1 }; |
| 697 | const SkScalar src3[] = { 0, 0, 0 }; |
| 698 | const SkScalar src4[] = { 0, 0, 1 }; |
| 699 | const SkScalar src5[] = { 0, 1, 1 }; |
| 700 | const SkScalar src6[] = { 0, 1, 2 }; |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 701 | const struct { |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 702 | const SkScalar* fData; |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 703 | int fCount; |
| 704 | int fCollapsedCount; |
| 705 | } data[] = { |
| 706 | { TEST_COLLAPS_ENTRY(src0), 1 }, |
| 707 | { TEST_COLLAPS_ENTRY(src1), 1 }, |
| 708 | { TEST_COLLAPS_ENTRY(src2), 2 }, |
| 709 | { TEST_COLLAPS_ENTRY(src3), 1 }, |
| 710 | { TEST_COLLAPS_ENTRY(src4), 2 }, |
| 711 | { TEST_COLLAPS_ENTRY(src5), 2 }, |
| 712 | { TEST_COLLAPS_ENTRY(src6), 3 }, |
| 713 | }; |
| 714 | for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 715 | SkScalar dst[3]; |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 716 | memcpy(dst, data[i].fData, data[i].fCount * sizeof(dst[0])); |
| 717 | int count = collaps_duplicates(dst, data[i].fCount); |
| 718 | SkASSERT(data[i].fCollapsedCount == count); |
| 719 | for (int j = 1; j < count; ++j) { |
| 720 | SkASSERT(dst[j-1] < dst[j]); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | #endif |
| 725 | |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 726 | static SkScalar SkScalarCubeRoot(SkScalar x) { |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 727 | return SkScalarPow(x, 0.3333333f); |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 728 | } |
| 729 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 730 | /* Solve coeff(t) == 0, returning the number of roots that |
| 731 | lie withing 0 < t < 1. |
| 732 | coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3] |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 733 | |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 734 | Eliminates repeated roots (so that all tValues are distinct, and are always |
| 735 | in increasing order. |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 736 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 737 | static int solve_cubic_poly(const SkScalar coeff[4], SkScalar tValues[3]) { |
| 738 | if (SkScalarNearlyZero(coeff[0])) { // we're just a quadratic |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 739 | return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues); |
| 740 | } |
| 741 | |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 742 | SkScalar a, b, c, Q, R; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 743 | |
| 744 | { |
| 745 | SkASSERT(coeff[0] != 0); |
| 746 | |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 747 | SkScalar inva = SkScalarInvert(coeff[0]); |
| 748 | a = coeff[1] * inva; |
| 749 | b = coeff[2] * inva; |
| 750 | c = coeff[3] * inva; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 751 | } |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 752 | Q = (a*a - b*3) / 9; |
| 753 | R = (2*a*a*a - 9*a*b + 27*c) / 54; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 754 | |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 755 | SkScalar Q3 = Q * Q * Q; |
| 756 | SkScalar R2MinusQ3 = R * R - Q3; |
| 757 | SkScalar adiv3 = a / 3; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 758 | |
| 759 | SkScalar* roots = tValues; |
| 760 | SkScalar r; |
| 761 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 762 | if (R2MinusQ3 < 0) { // we have 3 real roots |
| 763 | SkScalar theta = SkScalarACos(R / SkScalarSqrt(Q3)); |
| 764 | SkScalar neg2RootQ = -2 * SkScalarSqrt(Q); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 765 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 766 | r = neg2RootQ * SkScalarCos(theta/3) - adiv3; |
| 767 | if (is_unit_interval(r)) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 768 | *roots++ = r; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 769 | } |
| 770 | r = neg2RootQ * SkScalarCos((theta + 2*SK_ScalarPI)/3) - adiv3; |
| 771 | if (is_unit_interval(r)) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 772 | *roots++ = r; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 773 | } |
| 774 | r = neg2RootQ * SkScalarCos((theta - 2*SK_ScalarPI)/3) - adiv3; |
| 775 | if (is_unit_interval(r)) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 776 | *roots++ = r; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 777 | } |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 778 | SkDEBUGCODE(test_collaps_duplicates();) |
| 779 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 780 | // now sort the roots |
reed@google.com | 7de5093 | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 781 | int count = (int)(roots - tValues); |
| 782 | SkASSERT((unsigned)count <= 3); |
| 783 | bubble_sort(tValues, count); |
| 784 | count = collaps_duplicates(tValues, count); |
| 785 | roots = tValues + count; // so we compute the proper count below |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 786 | } else { // we have 1 real root |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 787 | SkScalar A = SkScalarAbs(R) + SkScalarSqrt(R2MinusQ3); |
| 788 | A = SkScalarCubeRoot(A); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 789 | if (R > 0) { |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 790 | A = -A; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 791 | } |
| 792 | if (A != 0) { |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 793 | A += Q / A; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 794 | } |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 795 | r = A - adiv3; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 796 | if (is_unit_interval(r)) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 797 | *roots++ = r; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 798 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | return (int)(roots - tValues); |
| 802 | } |
| 803 | |
| 804 | /* Looking for F' dot F'' == 0 |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 805 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 806 | A = b - a |
| 807 | B = c - 2b + a |
| 808 | C = d - 3c + 3b - a |
| 809 | |
| 810 | F' = 3Ct^2 + 6Bt + 3A |
| 811 | F'' = 6Ct + 6B |
| 812 | |
| 813 | F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB |
| 814 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 815 | static void formulate_F1DotF2(const SkScalar src[], SkScalar coeff[4]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 816 | SkScalar a = src[2] - src[0]; |
| 817 | SkScalar b = src[4] - 2 * src[2] + src[0]; |
| 818 | SkScalar c = src[6] + 3 * (src[2] - src[4]) - src[0]; |
| 819 | |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 820 | coeff[0] = c * c; |
| 821 | coeff[1] = 3 * b * c; |
| 822 | coeff[2] = 2 * b * b + c * a; |
| 823 | coeff[3] = a * b; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 824 | } |
| 825 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 826 | /* Looking for F' dot F'' == 0 |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 827 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 828 | A = b - a |
| 829 | B = c - 2b + a |
| 830 | C = d - 3c + 3b - a |
| 831 | |
| 832 | F' = 3Ct^2 + 6Bt + 3A |
| 833 | F'' = 6Ct + 6B |
| 834 | |
| 835 | F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB |
| 836 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 837 | int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]) { |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 838 | SkScalar coeffX[4], coeffY[4]; |
| 839 | int i; |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 840 | |
| 841 | formulate_F1DotF2(&src[0].fX, coeffX); |
| 842 | formulate_F1DotF2(&src[0].fY, coeffY); |
| 843 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 844 | for (i = 0; i < 4; i++) { |
reed@google.com | ceb7526 | 2013-12-16 14:17:40 +0000 | [diff] [blame] | 845 | coeffX[i] += coeffY[i]; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 846 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 847 | |
| 848 | SkScalar t[3]; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 849 | int count = solve_cubic_poly(coeffX, t); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 850 | int maxCount = 0; |
| 851 | |
| 852 | // now remove extrema where the curvature is zero (mins) |
| 853 | // !!!! need a test for this !!!! |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 854 | for (i = 0; i < count; i++) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 855 | // if (not_min_curvature()) |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 856 | if (t[i] > 0 && t[i] < SK_Scalar1) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 857 | tValues[maxCount++] = t[i]; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 858 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 859 | } |
| 860 | return maxCount; |
| 861 | } |
| 862 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 863 | int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], |
| 864 | SkScalar tValues[3]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 865 | SkScalar t_storage[3]; |
| 866 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 867 | if (tValues == NULL) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 868 | tValues = t_storage; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 869 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 870 | |
| 871 | int count = SkFindCubicMaxCurvature(src, tValues); |
| 872 | |
egdaniel@google.com | 63cc6e1 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 873 | if (dst) { |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 874 | if (count == 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 875 | memcpy(dst, src, 4 * sizeof(SkPoint)); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 876 | } else { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 877 | SkChopCubicAt(src, dst, tValues, count); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 878 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 879 | } |
| 880 | return count + 1; |
| 881 | } |
| 882 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 883 | bool SkXRayCrossesMonotonicCubic(const SkXRay& pt, const SkPoint cubic[4], |
| 884 | bool* ambiguous) { |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 885 | if (ambiguous) { |
| 886 | *ambiguous = false; |
| 887 | } |
| 888 | |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 889 | // Find the minimum and maximum y of the extrema, which are the |
| 890 | // first and last points since this cubic is monotonic |
| 891 | SkScalar min_y = SkMinScalar(cubic[0].fY, cubic[3].fY); |
| 892 | SkScalar max_y = SkMaxScalar(cubic[0].fY, cubic[3].fY); |
| 893 | |
| 894 | if (pt.fY == cubic[0].fY |
| 895 | || pt.fY < min_y |
| 896 | || pt.fY > max_y) { |
| 897 | // The query line definitely does not cross the curve |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 898 | if (ambiguous) { |
| 899 | *ambiguous = (pt.fY == cubic[0].fY); |
| 900 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 901 | return false; |
| 902 | } |
| 903 | |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 904 | bool pt_at_extremum = (pt.fY == cubic[3].fY); |
| 905 | |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 906 | SkScalar min_x = |
| 907 | SkMinScalar( |
| 908 | SkMinScalar( |
| 909 | SkMinScalar(cubic[0].fX, cubic[1].fX), |
| 910 | cubic[2].fX), |
| 911 | cubic[3].fX); |
| 912 | if (pt.fX < min_x) { |
| 913 | // The query line definitely crosses the curve |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 914 | if (ambiguous) { |
| 915 | *ambiguous = pt_at_extremum; |
| 916 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 917 | return true; |
| 918 | } |
| 919 | |
| 920 | SkScalar max_x = |
| 921 | SkMaxScalar( |
| 922 | SkMaxScalar( |
| 923 | SkMaxScalar(cubic[0].fX, cubic[1].fX), |
| 924 | cubic[2].fX), |
| 925 | cubic[3].fX); |
| 926 | if (pt.fX > max_x) { |
| 927 | // The query line definitely does not cross the curve |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | // Do a binary search to find the parameter value which makes y as |
| 932 | // close as possible to the query point. See whether the query |
| 933 | // line's origin is to the left of the associated x coordinate. |
| 934 | |
| 935 | // kMaxIter is chosen as the number of mantissa bits for a float, |
| 936 | // since there's no way we are going to get more precision by |
| 937 | // iterating more times than that. |
| 938 | const int kMaxIter = 23; |
| 939 | SkPoint eval; |
| 940 | int iter = 0; |
| 941 | SkScalar upper_t; |
| 942 | SkScalar lower_t; |
| 943 | // Need to invert direction of t parameter if cubic goes up |
| 944 | // instead of down |
| 945 | if (cubic[3].fY > cubic[0].fY) { |
| 946 | upper_t = SK_Scalar1; |
commit-bot@chromium.org | dfc928c | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 947 | lower_t = 0; |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 948 | } else { |
commit-bot@chromium.org | dfc928c | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 949 | upper_t = 0; |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 950 | lower_t = SK_Scalar1; |
| 951 | } |
| 952 | do { |
| 953 | SkScalar t = SkScalarAve(upper_t, lower_t); |
| 954 | SkEvalCubicAt(cubic, t, &eval, NULL, NULL); |
| 955 | if (pt.fY > eval.fY) { |
| 956 | lower_t = t; |
| 957 | } else { |
| 958 | upper_t = t; |
| 959 | } |
| 960 | } while (++iter < kMaxIter |
| 961 | && !SkScalarNearlyZero(eval.fY - pt.fY)); |
| 962 | if (pt.fX <= eval.fX) { |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 963 | if (ambiguous) { |
| 964 | *ambiguous = pt_at_extremum; |
| 965 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | return false; |
| 969 | } |
| 970 | |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 971 | int SkNumXRayCrossingsForCubic(const SkXRay& pt, |
| 972 | const SkPoint cubic[4], |
| 973 | bool* ambiguous) { |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 974 | int num_crossings = 0; |
| 975 | SkPoint monotonic_cubics[10]; |
| 976 | int num_monotonic_cubics = SkChopCubicAtYExtrema(cubic, monotonic_cubics); |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 977 | if (ambiguous) { |
| 978 | *ambiguous = false; |
| 979 | } |
| 980 | bool locally_ambiguous; |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 981 | if (SkXRayCrossesMonotonicCubic(pt, |
| 982 | &monotonic_cubics[0], |
| 983 | &locally_ambiguous)) |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 984 | ++num_crossings; |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 985 | if (ambiguous) { |
| 986 | *ambiguous |= locally_ambiguous; |
| 987 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 988 | if (num_monotonic_cubics > 0) |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 989 | if (SkXRayCrossesMonotonicCubic(pt, |
| 990 | &monotonic_cubics[3], |
| 991 | &locally_ambiguous)) |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 992 | ++num_crossings; |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 993 | if (ambiguous) { |
| 994 | *ambiguous |= locally_ambiguous; |
| 995 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 996 | if (num_monotonic_cubics > 1) |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 997 | if (SkXRayCrossesMonotonicCubic(pt, |
| 998 | &monotonic_cubics[6], |
| 999 | &locally_ambiguous)) |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 1000 | ++num_crossings; |
kbr@chromium.org | c1b5333 | 2010-07-07 22:20:35 +0000 | [diff] [blame] | 1001 | if (ambiguous) { |
| 1002 | *ambiguous |= locally_ambiguous; |
| 1003 | } |
reed@android.com | 5b4541e | 2010-02-05 20:41:02 +0000 | [diff] [blame] | 1004 | return num_crossings; |
| 1005 | } |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1006 | |
| 1007 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1008 | |
| 1009 | /* Find t value for quadratic [a, b, c] = d. |
| 1010 | Return 0 if there is no solution within [0, 1) |
| 1011 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1012 | static SkScalar quad_solve(SkScalar a, SkScalar b, SkScalar c, SkScalar d) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1013 | // At^2 + Bt + C = d |
| 1014 | SkScalar A = a - 2 * b + c; |
| 1015 | SkScalar B = 2 * (b - a); |
| 1016 | SkScalar C = a - d; |
| 1017 | |
| 1018 | SkScalar roots[2]; |
| 1019 | int count = SkFindUnitQuadRoots(A, B, C, roots); |
| 1020 | |
| 1021 | SkASSERT(count <= 1); |
| 1022 | return count == 1 ? roots[0] : 0; |
| 1023 | } |
| 1024 | |
robertphillips@google.com | dd4bd43 | 2013-07-09 15:03:59 +0000 | [diff] [blame] | 1025 | /* given a quad-curve and a point (x,y), chop the quad at that point and place |
skia.committer@gmail.com | 937bf44 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 1026 | the new off-curve point and endpoint into 'dest'. |
skia.committer@gmail.com | 6f1f159 | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 1027 | Should only return false if the computed pos is the start of the curve |
robertphillips@google.com | dd4bd43 | 2013-07-09 15:03:59 +0000 | [diff] [blame] | 1028 | (i.e. root == 0) |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1029 | */ |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1030 | static bool truncate_last_curve(const SkPoint quad[3], SkScalar x, SkScalar y, |
| 1031 | SkPoint* dest) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1032 | const SkScalar* base; |
| 1033 | SkScalar value; |
| 1034 | |
| 1035 | if (SkScalarAbs(x) < SkScalarAbs(y)) { |
| 1036 | base = &quad[0].fX; |
| 1037 | value = x; |
| 1038 | } else { |
| 1039 | base = &quad[0].fY; |
| 1040 | value = y; |
| 1041 | } |
| 1042 | |
| 1043 | // note: this returns 0 if it thinks value is out of range, meaning the |
| 1044 | // root might return something outside of [0, 1) |
| 1045 | SkScalar t = quad_solve(base[0], base[2], base[4], value); |
| 1046 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1047 | if (t > 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1048 | SkPoint tmp[5]; |
| 1049 | SkChopQuadAt(quad, tmp, t); |
robertphillips@google.com | dd4bd43 | 2013-07-09 15:03:59 +0000 | [diff] [blame] | 1050 | dest[0] = tmp[1]; |
robertphillips@google.com | a9531cd | 2013-09-27 17:05:59 +0000 | [diff] [blame] | 1051 | dest[1].set(x, y); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1052 | return true; |
| 1053 | } else { |
| 1054 | /* t == 0 means either the value triggered a root outside of [0, 1) |
| 1055 | For our purposes, we can ignore the <= 0 roots, but we want to |
| 1056 | catch the >= 1 roots (which given our caller, will basically mean |
| 1057 | a root of 1, give-or-take numerical instability). If we are in the |
| 1058 | >= 1 case, return the existing offCurve point. |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1059 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1060 | The test below checks to see if we are close to the "end" of the |
| 1061 | curve (near base[4]). Rather than specifying a tolerance, I just |
| 1062 | check to see if value is on to the right/left of the middle point |
| 1063 | (depending on the direction/sign of the end points). |
| 1064 | */ |
| 1065 | if ((base[0] < base[4] && value > base[2]) || |
| 1066 | (base[0] > base[4] && value < base[2])) // should root have been 1 |
| 1067 | { |
robertphillips@google.com | dd4bd43 | 2013-07-09 15:03:59 +0000 | [diff] [blame] | 1068 | dest[0] = quad[1]; |
| 1069 | dest[1].set(x, y); |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1070 | return true; |
| 1071 | } |
| 1072 | } |
| 1073 | return false; |
| 1074 | } |
| 1075 | |
| 1076 | static const SkPoint gQuadCirclePts[kSkBuildQuadArcStorage] = { |
commit-bot@chromium.org | 942ed4e | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 1077 | // The mid point of the quadratic arc approximation is half way between the two |
| 1078 | // control points. The float epsilon adjustment moves the on curve point out by |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 1079 | // two bits, distributing the convex test error between the round rect |
| 1080 | // approximation and the convex cross product sign equality test. |
| 1081 | #define SK_MID_RRECT_OFFSET \ |
| 1082 | (SK_Scalar1 + SK_ScalarTanPIOver8 + FLT_EPSILON * 4) / 2 |
commit-bot@chromium.org | 942ed4e | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 1083 | { SK_Scalar1, 0 }, |
| 1084 | { SK_Scalar1, SK_ScalarTanPIOver8 }, |
| 1085 | { SK_MID_RRECT_OFFSET, SK_MID_RRECT_OFFSET }, |
| 1086 | { SK_ScalarTanPIOver8, SK_Scalar1 }, |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1087 | |
commit-bot@chromium.org | 942ed4e | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 1088 | { 0, SK_Scalar1 }, |
| 1089 | { -SK_ScalarTanPIOver8, SK_Scalar1 }, |
| 1090 | { -SK_MID_RRECT_OFFSET, SK_MID_RRECT_OFFSET }, |
| 1091 | { -SK_Scalar1, SK_ScalarTanPIOver8 }, |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1092 | |
commit-bot@chromium.org | 942ed4e | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 1093 | { -SK_Scalar1, 0 }, |
| 1094 | { -SK_Scalar1, -SK_ScalarTanPIOver8 }, |
| 1095 | { -SK_MID_RRECT_OFFSET, -SK_MID_RRECT_OFFSET }, |
| 1096 | { -SK_ScalarTanPIOver8, -SK_Scalar1 }, |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1097 | |
commit-bot@chromium.org | 942ed4e | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 1098 | { 0, -SK_Scalar1 }, |
| 1099 | { SK_ScalarTanPIOver8, -SK_Scalar1 }, |
| 1100 | { SK_MID_RRECT_OFFSET, -SK_MID_RRECT_OFFSET }, |
| 1101 | { SK_Scalar1, -SK_ScalarTanPIOver8 }, |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1102 | |
commit-bot@chromium.org | 942ed4e | 2013-11-01 15:24:55 +0000 | [diff] [blame] | 1103 | { SK_Scalar1, 0 } |
| 1104 | #undef SK_MID_RRECT_OFFSET |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1105 | }; |
| 1106 | |
| 1107 | int SkBuildQuadArc(const SkVector& uStart, const SkVector& uStop, |
| 1108 | SkRotationDirection dir, const SkMatrix* userMatrix, |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1109 | SkPoint quadPoints[]) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1110 | // rotate by x,y so that uStart is (1.0) |
| 1111 | SkScalar x = SkPoint::DotProduct(uStart, uStop); |
| 1112 | SkScalar y = SkPoint::CrossProduct(uStart, uStop); |
| 1113 | |
| 1114 | SkScalar absX = SkScalarAbs(x); |
| 1115 | SkScalar absY = SkScalarAbs(y); |
| 1116 | |
| 1117 | int pointCount; |
| 1118 | |
| 1119 | // check for (effectively) coincident vectors |
| 1120 | // this can happen if our angle is nearly 0 or nearly 180 (y == 0) |
| 1121 | // ... we use the dot-prod to distinguish between 0 and 180 (x > 0) |
| 1122 | if (absY <= SK_ScalarNearlyZero && x > 0 && |
| 1123 | ((y >= 0 && kCW_SkRotationDirection == dir) || |
| 1124 | (y <= 0 && kCCW_SkRotationDirection == dir))) { |
rmistry@google.com | 935e9f4 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1125 | |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1126 | // just return the start-point |
| 1127 | quadPoints[0].set(SK_Scalar1, 0); |
| 1128 | pointCount = 1; |
| 1129 | } else { |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1130 | if (dir == kCCW_SkRotationDirection) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1131 | y = -y; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1132 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1133 | // what octant (quadratic curve) is [xy] in? |
| 1134 | int oct = 0; |
| 1135 | bool sameSign = true; |
| 1136 | |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1137 | if (0 == y) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1138 | oct = 4; // 180 |
| 1139 | SkASSERT(SkScalarAbs(x + SK_Scalar1) <= SK_ScalarNearlyZero); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1140 | } else if (0 == x) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1141 | SkASSERT(absY - SK_Scalar1 <= SK_ScalarNearlyZero); |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1142 | oct = y > 0 ? 2 : 6; // 90 : 270 |
| 1143 | } else { |
| 1144 | if (y < 0) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1145 | oct += 4; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1146 | } |
| 1147 | if ((x < 0) != (y < 0)) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1148 | oct += 2; |
| 1149 | sameSign = false; |
| 1150 | } |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1151 | if ((absX < absY) == sameSign) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1152 | oct += 1; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1153 | } |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | int wholeCount = oct << 1; |
| 1157 | memcpy(quadPoints, gQuadCirclePts, (wholeCount + 1) * sizeof(SkPoint)); |
| 1158 | |
| 1159 | const SkPoint* arc = &gQuadCirclePts[wholeCount]; |
commit-bot@chromium.org | 1e31455 | 2014-02-21 12:17:34 +0000 | [diff] [blame] | 1160 | if (truncate_last_curve(arc, x, y, &quadPoints[wholeCount + 1])) { |
reed@android.com | bcd4d5a | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1161 | wholeCount += 2; |
| 1162 | } |
| 1163 | pointCount = wholeCount + 1; |
| 1164 | } |
| 1165 | |
| 1166 | // now handle counter-clockwise and the initial unitStart rotation |
| 1167 | SkMatrix matrix; |
| 1168 | matrix.setSinCos(uStart.fY, uStart.fX); |
| 1169 | if (dir == kCCW_SkRotationDirection) { |
| 1170 | matrix.preScale(SK_Scalar1, -SK_Scalar1); |
| 1171 | } |
| 1172 | if (userMatrix) { |
| 1173 | matrix.postConcat(*userMatrix); |
| 1174 | } |
| 1175 | matrix.mapPoints(quadPoints, pointCount); |
| 1176 | return pointCount; |
| 1177 | } |
reed@google.com | 7997588 | 2013-04-12 19:11:10 +0000 | [diff] [blame] | 1178 | |
reed@google.com | 7997588 | 2013-04-12 19:11:10 +0000 | [diff] [blame] | 1179 | |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 1180 | /////////////////////////////////////////////////////////////////////////////// |
| 1181 | // |
| 1182 | // NURB representation for conics. Helpful explanations at: |
| 1183 | // |
| 1184 | // http://citeseerx.ist.psu.edu/viewdoc/ |
| 1185 | // download?doi=10.1.1.44.5740&rep=rep1&type=ps |
| 1186 | // and |
| 1187 | // http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html |
| 1188 | // |
reed@google.com | 256f310 | 2013-04-16 21:07:27 +0000 | [diff] [blame] | 1189 | // F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w) |
| 1190 | // ------------------------------------------ |
| 1191 | // ((1 - t)^2 + t^2 + 2 (1 - t) t w) |
| 1192 | // |
| 1193 | // = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0} |
| 1194 | // ------------------------------------------------ |
| 1195 | // {t^2 (2 - 2 w), t (-2 + 2 w), 1} |
| 1196 | // |
reed@google.com | 256f310 | 2013-04-16 21:07:27 +0000 | [diff] [blame] | 1197 | |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1198 | static SkScalar conic_eval_pos(const SkScalar src[], SkScalar w, SkScalar t) { |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1199 | SkASSERT(src); |
| 1200 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
skia.committer@gmail.com | cd48746 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 1201 | |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1202 | SkScalar src2w = SkScalarMul(src[2], w); |
| 1203 | SkScalar C = src[0]; |
| 1204 | SkScalar A = src[4] - 2 * src2w + C; |
| 1205 | SkScalar B = 2 * (src2w - C); |
| 1206 | SkScalar numer = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); |
skia.committer@gmail.com | cd48746 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 1207 | |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1208 | B = 2 * (w - SK_Scalar1); |
| 1209 | C = SK_Scalar1; |
| 1210 | A = -B; |
| 1211 | SkScalar denom = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); |
skia.committer@gmail.com | cd48746 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 1212 | |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1213 | return SkScalarDiv(numer, denom); |
| 1214 | } |
| 1215 | |
| 1216 | // F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w) |
| 1217 | // |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1218 | // t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w) |
| 1219 | // t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w) |
| 1220 | // t^0 : -2 P0 w + 2 P1 w |
| 1221 | // |
| 1222 | // We disregard magnitude, so we can freely ignore the denominator of F', and |
| 1223 | // divide the numerator by 2 |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1224 | // |
reed@google.com | 256f310 | 2013-04-16 21:07:27 +0000 | [diff] [blame] | 1225 | // coeff[0] for t^2 |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1226 | // coeff[1] for t^1 |
| 1227 | // coeff[2] for t^0 |
reed@google.com | 256f310 | 2013-04-16 21:07:27 +0000 | [diff] [blame] | 1228 | // |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 1229 | static void conic_deriv_coeff(const SkScalar src[], |
| 1230 | SkScalar w, |
| 1231 | SkScalar coeff[3]) { |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1232 | const SkScalar P20 = src[4] - src[0]; |
| 1233 | const SkScalar P10 = src[2] - src[0]; |
| 1234 | const SkScalar wP10 = w * P10; |
| 1235 | coeff[0] = w * P20 - P20; |
| 1236 | coeff[1] = P20 - 2 * wP10; |
| 1237 | coeff[2] = wP10; |
reed@google.com | 256f310 | 2013-04-16 21:07:27 +0000 | [diff] [blame] | 1238 | } |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1239 | |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1240 | static SkScalar conic_eval_tan(const SkScalar coord[], SkScalar w, SkScalar t) { |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1241 | SkScalar coeff[3]; |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1242 | conic_deriv_coeff(coord, w, coeff); |
| 1243 | return t * (t * coeff[0] + coeff[1]) + coeff[2]; |
| 1244 | } |
| 1245 | |
| 1246 | static bool conic_find_extrema(const SkScalar src[], SkScalar w, SkScalar* t) { |
| 1247 | SkScalar coeff[3]; |
| 1248 | conic_deriv_coeff(src, w, coeff); |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1249 | |
| 1250 | SkScalar tValues[2]; |
| 1251 | int roots = SkFindUnitQuadRoots(coeff[0], coeff[1], coeff[2], tValues); |
| 1252 | SkASSERT(0 == roots || 1 == roots); |
skia.committer@gmail.com | cd48746 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 1253 | |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1254 | if (1 == roots) { |
| 1255 | *t = tValues[0]; |
| 1256 | return true; |
| 1257 | } |
| 1258 | return false; |
| 1259 | } |
reed@google.com | 256f310 | 2013-04-16 21:07:27 +0000 | [diff] [blame] | 1260 | |
reed@google.com | 0fef2ef | 2013-04-12 21:55:26 +0000 | [diff] [blame] | 1261 | struct SkP3D { |
| 1262 | SkScalar fX, fY, fZ; |
skia.committer@gmail.com | a43230c | 2013-04-13 07:01:15 +0000 | [diff] [blame] | 1263 | |
reed@google.com | 0fef2ef | 2013-04-12 21:55:26 +0000 | [diff] [blame] | 1264 | void set(SkScalar x, SkScalar y, SkScalar z) { |
| 1265 | fX = x; fY = y; fZ = z; |
| 1266 | } |
skia.committer@gmail.com | a43230c | 2013-04-13 07:01:15 +0000 | [diff] [blame] | 1267 | |
reed@google.com | 0fef2ef | 2013-04-12 21:55:26 +0000 | [diff] [blame] | 1268 | void projectDown(SkPoint* dst) const { |
| 1269 | dst->set(fX / fZ, fY / fZ); |
| 1270 | } |
| 1271 | }; |
| 1272 | |
commit-bot@chromium.org | 15dd6a5 | 2014-01-28 14:36:52 +0000 | [diff] [blame] | 1273 | // We only interpolate one dimension at a time (the first, at +0, +3, +6). |
| 1274 | static void p3d_interp(const SkScalar src[7], SkScalar dst[7], SkScalar t) { |
reed@google.com | 0fef2ef | 2013-04-12 21:55:26 +0000 | [diff] [blame] | 1275 | SkScalar ab = SkScalarInterp(src[0], src[3], t); |
| 1276 | SkScalar bc = SkScalarInterp(src[3], src[6], t); |
| 1277 | dst[0] = ab; |
| 1278 | dst[3] = SkScalarInterp(ab, bc, t); |
| 1279 | dst[6] = bc; |
| 1280 | } |
| 1281 | |
| 1282 | static void ratquad_mapTo3D(const SkPoint src[3], SkScalar w, SkP3D dst[]) { |
| 1283 | dst[0].set(src[0].fX * 1, src[0].fY * 1, 1); |
| 1284 | dst[1].set(src[1].fX * w, src[1].fY * w, w); |
| 1285 | dst[2].set(src[2].fX * 1, src[2].fY * 1, 1); |
| 1286 | } |
| 1287 | |
reed@google.com | 4e1502a | 2013-05-07 20:42:35 +0000 | [diff] [blame] | 1288 | void SkConic::evalAt(SkScalar t, SkPoint* pt, SkVector* tangent) const { |
reed@google.com | 7997588 | 2013-04-12 19:11:10 +0000 | [diff] [blame] | 1289 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
skia.committer@gmail.com | a43230c | 2013-04-13 07:01:15 +0000 | [diff] [blame] | 1290 | |
reed@google.com | 7997588 | 2013-04-12 19:11:10 +0000 | [diff] [blame] | 1291 | if (pt) { |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1292 | pt->set(conic_eval_pos(&fPts[0].fX, fW, t), |
| 1293 | conic_eval_pos(&fPts[0].fY, fW, t)); |
reed@google.com | 7997588 | 2013-04-12 19:11:10 +0000 | [diff] [blame] | 1294 | } |
reed@google.com | 4e1502a | 2013-05-07 20:42:35 +0000 | [diff] [blame] | 1295 | if (tangent) { |
| 1296 | tangent->set(conic_eval_tan(&fPts[0].fX, fW, t), |
| 1297 | conic_eval_tan(&fPts[0].fY, fW, t)); |
| 1298 | } |
reed@google.com | 7997588 | 2013-04-12 19:11:10 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1301 | void SkConic::chopAt(SkScalar t, SkConic dst[2]) const { |
reed@google.com | 0fef2ef | 2013-04-12 21:55:26 +0000 | [diff] [blame] | 1302 | SkP3D tmp[3], tmp2[3]; |
| 1303 | |
| 1304 | ratquad_mapTo3D(fPts, fW, tmp); |
skia.committer@gmail.com | a43230c | 2013-04-13 07:01:15 +0000 | [diff] [blame] | 1305 | |
reed@google.com | 0fef2ef | 2013-04-12 21:55:26 +0000 | [diff] [blame] | 1306 | p3d_interp(&tmp[0].fX, &tmp2[0].fX, t); |
| 1307 | p3d_interp(&tmp[0].fY, &tmp2[0].fY, t); |
| 1308 | p3d_interp(&tmp[0].fZ, &tmp2[0].fZ, t); |
skia.committer@gmail.com | a43230c | 2013-04-13 07:01:15 +0000 | [diff] [blame] | 1309 | |
reed@google.com | 0fef2ef | 2013-04-12 21:55:26 +0000 | [diff] [blame] | 1310 | dst[0].fPts[0] = fPts[0]; |
| 1311 | tmp2[0].projectDown(&dst[0].fPts[1]); |
| 1312 | tmp2[1].projectDown(&dst[0].fPts[2]); dst[1].fPts[0] = dst[0].fPts[2]; |
| 1313 | tmp2[2].projectDown(&dst[1].fPts[1]); |
| 1314 | dst[1].fPts[2] = fPts[2]; |
| 1315 | |
mike@reedtribe.org | 464a153 | 2013-04-13 10:51:51 +0000 | [diff] [blame] | 1316 | // to put in "standard form", where w0 and w2 are both 1, we compute the |
| 1317 | // new w1 as sqrt(w1*w1/w0*w2) |
| 1318 | // or |
| 1319 | // w1 /= sqrt(w0*w2) |
| 1320 | // |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 1321 | // However, in our case, we know that for dst[0]: |
| 1322 | // w0 == 1, and for dst[1], w2 == 1 |
mike@reedtribe.org | 464a153 | 2013-04-13 10:51:51 +0000 | [diff] [blame] | 1323 | // |
| 1324 | SkScalar root = SkScalarSqrt(tmp2[1].fZ); |
| 1325 | dst[0].fW = tmp2[0].fZ / root; |
| 1326 | dst[1].fW = tmp2[2].fZ / root; |
reed@google.com | 7997588 | 2013-04-12 19:11:10 +0000 | [diff] [blame] | 1327 | } |
mike@reedtribe.org | 9106839 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 1328 | |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1329 | static SkScalar subdivide_w_value(SkScalar w) { |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1330 | return SkScalarSqrt(SK_ScalarHalf + w * SK_ScalarHalf); |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1333 | void SkConic::chop(SkConic dst[2]) const { |
mike@reedtribe.org | 9106839 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 1334 | SkScalar scale = SkScalarInvert(SK_Scalar1 + fW); |
| 1335 | SkScalar p1x = fW * fPts[1].fX; |
| 1336 | SkScalar p1y = fW * fPts[1].fY; |
| 1337 | SkScalar mx = (fPts[0].fX + 2 * p1x + fPts[2].fX) * scale * SK_ScalarHalf; |
| 1338 | SkScalar my = (fPts[0].fY + 2 * p1y + fPts[2].fY) * scale * SK_ScalarHalf; |
| 1339 | |
| 1340 | dst[0].fPts[0] = fPts[0]; |
| 1341 | dst[0].fPts[1].set((fPts[0].fX + p1x) * scale, |
| 1342 | (fPts[0].fY + p1y) * scale); |
| 1343 | dst[0].fPts[2].set(mx, my); |
| 1344 | |
| 1345 | dst[1].fPts[0].set(mx, my); |
| 1346 | dst[1].fPts[1].set((p1x + fPts[2].fX) * scale, |
| 1347 | (p1y + fPts[2].fY) * scale); |
| 1348 | dst[1].fPts[2] = fPts[2]; |
| 1349 | |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1350 | dst[0].fW = dst[1].fW = subdivide_w_value(fW); |
mike@reedtribe.org | 9106839 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 1351 | } |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1352 | |
mike@reedtribe.org | dc5176e | 2013-04-27 18:23:16 +0000 | [diff] [blame] | 1353 | /* |
| 1354 | * "High order approximation of conic sections by quadratic splines" |
| 1355 | * by Michael Floater, 1993 |
| 1356 | */ |
mike@reedtribe.org | 3661c44 | 2013-04-30 02:14:58 +0000 | [diff] [blame] | 1357 | #define AS_QUAD_ERROR_SETUP \ |
| 1358 | SkScalar a = fW - 1; \ |
| 1359 | SkScalar k = a / (4 * (2 + a)); \ |
| 1360 | SkScalar x = k * (fPts[0].fX - 2 * fPts[1].fX + fPts[2].fX); \ |
| 1361 | SkScalar y = k * (fPts[0].fY - 2 * fPts[1].fY + fPts[2].fY); |
| 1362 | |
| 1363 | void SkConic::computeAsQuadError(SkVector* err) const { |
| 1364 | AS_QUAD_ERROR_SETUP |
| 1365 | err->set(x, y); |
| 1366 | } |
| 1367 | |
| 1368 | bool SkConic::asQuadTol(SkScalar tol) const { |
| 1369 | AS_QUAD_ERROR_SETUP |
| 1370 | return (x * x + y * y) <= tol * tol; |
mike@reedtribe.org | dc5176e | 2013-04-27 18:23:16 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1373 | int SkConic::computeQuadPOW2(SkScalar tol) const { |
mike@reedtribe.org | 3661c44 | 2013-04-30 02:14:58 +0000 | [diff] [blame] | 1374 | AS_QUAD_ERROR_SETUP |
| 1375 | SkScalar error = SkScalarSqrt(x * x + y * y) - tol; |
| 1376 | |
| 1377 | if (error <= 0) { |
mike@reedtribe.org | dc5176e | 2013-04-27 18:23:16 +0000 | [diff] [blame] | 1378 | return 0; |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1379 | } |
mike@reedtribe.org | dc5176e | 2013-04-27 18:23:16 +0000 | [diff] [blame] | 1380 | uint32_t ierr = (uint32_t)error; |
mike@reedtribe.org | 3661c44 | 2013-04-30 02:14:58 +0000 | [diff] [blame] | 1381 | return (34 - SkCLZ(ierr)) >> 1; |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1384 | static SkPoint* subdivide(const SkConic& src, SkPoint pts[], int level) { |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1385 | SkASSERT(level >= 0); |
mike@reedtribe.org | 3661c44 | 2013-04-30 02:14:58 +0000 | [diff] [blame] | 1386 | |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1387 | if (0 == level) { |
| 1388 | memcpy(pts, &src.fPts[1], 2 * sizeof(SkPoint)); |
| 1389 | return pts + 2; |
| 1390 | } else { |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1391 | SkConic dst[2]; |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1392 | src.chop(dst); |
| 1393 | --level; |
| 1394 | pts = subdivide(dst[0], pts, level); |
| 1395 | return subdivide(dst[1], pts, level); |
| 1396 | } |
| 1397 | } |
| 1398 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1399 | int SkConic::chopIntoQuadsPOW2(SkPoint pts[], int pow2) const { |
mike@reedtribe.org | 3661c44 | 2013-04-30 02:14:58 +0000 | [diff] [blame] | 1400 | SkASSERT(pow2 >= 0); |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1401 | *pts = fPts[0]; |
reed@google.com | 901641b | 2013-04-15 15:23:38 +0000 | [diff] [blame] | 1402 | SkDEBUGCODE(SkPoint* endPts =) subdivide(*this, pts + 1, pow2); |
mike@reedtribe.org | 6c125bd | 2013-04-15 15:20:52 +0000 | [diff] [blame] | 1403 | SkASSERT(endPts - pts == (2 * (1 << pow2) + 1)); |
| 1404 | return 1 << pow2; |
| 1405 | } |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1406 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1407 | bool SkConic::findXExtrema(SkScalar* t) const { |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1408 | return conic_find_extrema(&fPts[0].fX, fW, t); |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1411 | bool SkConic::findYExtrema(SkScalar* t) const { |
mike@reedtribe.org | 3f0cf54 | 2013-05-08 01:55:49 +0000 | [diff] [blame] | 1412 | return conic_find_extrema(&fPts[0].fY, fW, t); |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1415 | bool SkConic::chopAtXExtrema(SkConic dst[2]) const { |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1416 | SkScalar t; |
| 1417 | if (this->findXExtrema(&t)) { |
| 1418 | this->chopAt(t, dst); |
| 1419 | // now clean-up the middle, since we know t was meant to be at |
| 1420 | // an X-extrema |
| 1421 | SkScalar value = dst[0].fPts[2].fX; |
| 1422 | dst[0].fPts[1].fX = value; |
| 1423 | dst[1].fPts[0].fX = value; |
| 1424 | dst[1].fPts[1].fX = value; |
| 1425 | return true; |
| 1426 | } |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1430 | bool SkConic::chopAtYExtrema(SkConic dst[2]) const { |
mike@reedtribe.org | 472a49c | 2013-04-17 01:21:01 +0000 | [diff] [blame] | 1431 | SkScalar t; |
| 1432 | if (this->findYExtrema(&t)) { |
| 1433 | this->chopAt(t, dst); |
| 1434 | // now clean-up the middle, since we know t was meant to be at |
| 1435 | // an Y-extrema |
| 1436 | SkScalar value = dst[0].fPts[2].fY; |
| 1437 | dst[0].fPts[1].fY = value; |
| 1438 | dst[1].fPts[0].fY = value; |
| 1439 | dst[1].fPts[1].fY = value; |
| 1440 | return true; |
| 1441 | } |
| 1442 | return false; |
| 1443 | } |
| 1444 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1445 | void SkConic::computeTightBounds(SkRect* bounds) const { |
mike@reedtribe.org | c732591 | 2013-04-17 02:25:33 +0000 | [diff] [blame] | 1446 | SkPoint pts[4]; |
| 1447 | pts[0] = fPts[0]; |
| 1448 | pts[1] = fPts[2]; |
| 1449 | int count = 2; |
| 1450 | |
| 1451 | SkScalar t; |
| 1452 | if (this->findXExtrema(&t)) { |
| 1453 | this->evalAt(t, &pts[count++]); |
| 1454 | } |
| 1455 | if (this->findYExtrema(&t)) { |
| 1456 | this->evalAt(t, &pts[count++]); |
| 1457 | } |
| 1458 | bounds->set(pts, count); |
| 1459 | } |
| 1460 | |
mike@reedtribe.org | 27a0a56 | 2013-04-26 00:58:29 +0000 | [diff] [blame] | 1461 | void SkConic::computeFastBounds(SkRect* bounds) const { |
mike@reedtribe.org | c732591 | 2013-04-17 02:25:33 +0000 | [diff] [blame] | 1462 | bounds->set(fPts, 3); |
| 1463 | } |
commit-bot@chromium.org | 4a0370d | 2014-02-21 19:49:10 +0000 | [diff] [blame^] | 1464 | |
| 1465 | bool SkConic::findMaxCurvature(SkScalar* t) const { |
| 1466 | // TODO: Implement me |
| 1467 | return false; |
| 1468 | } |