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