| 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 |  | 
|---|
| 8 | #include "include/core/SkMatrix.h" | 
|---|
| 9 | #include "include/core/SkPoint3.h" | 
|---|
| 10 | #include "include/private/SkNx.h" | 
|---|
| 11 | #include "src/core/SkGeometry.h" | 
|---|
| 12 | #include "src/core/SkPointPriv.h" | 
|---|
| 13 |  | 
|---|
| 14 | #include <utility> | 
|---|
| 15 |  | 
|---|
| 16 | static SkVector to_vector(const Sk2s& x) { | 
|---|
| 17 | SkVector vector; | 
|---|
| 18 | x.store(&vector); | 
|---|
| 19 | return vector; | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 23 |  | 
|---|
| 24 | static int is_not_monotonic(SkScalar a, SkScalar b, SkScalar c) { | 
|---|
| 25 | SkScalar ab = a - b; | 
|---|
| 26 | SkScalar bc = b - c; | 
|---|
| 27 | if (ab < 0) { | 
|---|
| 28 | bc = -bc; | 
|---|
| 29 | } | 
|---|
| 30 | return ab == 0 || bc < 0; | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | //////////////////////////////////////////////////////////////////////// | 
|---|
| 34 |  | 
|---|
| 35 | static int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio) { | 
|---|
| 36 | SkASSERT(ratio); | 
|---|
| 37 |  | 
|---|
| 38 | if (numer < 0) { | 
|---|
| 39 | numer = -numer; | 
|---|
| 40 | denom = -denom; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | if (denom == 0 || numer == 0 || numer >= denom) { | 
|---|
| 44 | return 0; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | SkScalar r = numer / denom; | 
|---|
| 48 | if (SkScalarIsNaN(r)) { | 
|---|
| 49 | return 0; | 
|---|
| 50 | } | 
|---|
| 51 | SkASSERTF(r >= 0 && r < SK_Scalar1, "numer %f, denom %f, r %f", numer, denom, r); | 
|---|
| 52 | if (r == 0) { // catch underflow if numer <<<< denom | 
|---|
| 53 | return 0; | 
|---|
| 54 | } | 
|---|
| 55 | *ratio = r; | 
|---|
| 56 | return 1; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | // Just returns its argument, but makes it easy to set a break-point to know when | 
|---|
| 60 | // SkFindUnitQuadRoots is going to return 0 (an error). | 
|---|
| 61 | static int return_check_zero(int value) { | 
|---|
| 62 | if (value == 0) { | 
|---|
| 63 | return 0; | 
|---|
| 64 | } | 
|---|
| 65 | return value; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | /** From Numerical Recipes in C. | 
|---|
| 69 |  | 
|---|
| 70 | Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C]) | 
|---|
| 71 | x1 = Q / A | 
|---|
| 72 | x2 = C / Q | 
|---|
| 73 | */ | 
|---|
| 74 | int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]) { | 
|---|
| 75 | SkASSERT(roots); | 
|---|
| 76 |  | 
|---|
| 77 | if (A == 0) { | 
|---|
| 78 | return return_check_zero(valid_unit_divide(-C, B, roots)); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | SkScalar* r = roots; | 
|---|
| 82 |  | 
|---|
| 83 | // use doubles so we don't overflow temporarily trying to compute R | 
|---|
| 84 | double dr = (double)B * B - 4 * (double)A * C; | 
|---|
| 85 | if (dr < 0) { | 
|---|
| 86 | return return_check_zero(0); | 
|---|
| 87 | } | 
|---|
| 88 | dr = sqrt(dr); | 
|---|
| 89 | SkScalar R = SkDoubleToScalar(dr); | 
|---|
| 90 | if (!SkScalarIsFinite(R)) { | 
|---|
| 91 | return return_check_zero(0); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | SkScalar Q = (B < 0) ? -(B-R)/2 : -(B+R)/2; | 
|---|
| 95 | r += valid_unit_divide(Q, A, r); | 
|---|
| 96 | r += valid_unit_divide(C, Q, r); | 
|---|
| 97 | if (r - roots == 2) { | 
|---|
| 98 | if (roots[0] > roots[1]) { | 
|---|
| 99 | using std::swap; | 
|---|
| 100 | swap(roots[0], roots[1]); | 
|---|
| 101 | } else if (roots[0] == roots[1]) { // nearly-equal? | 
|---|
| 102 | r -= 1; // skip the double root | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 | return return_check_zero((int)(r - roots)); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 109 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 110 |  | 
|---|
| 111 | void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent) { | 
|---|
| 112 | SkASSERT(src); | 
|---|
| 113 | SkASSERT(t >= 0 && t <= SK_Scalar1); | 
|---|
| 114 |  | 
|---|
| 115 | if (pt) { | 
|---|
| 116 | *pt = SkEvalQuadAt(src, t); | 
|---|
| 117 | } | 
|---|
| 118 | if (tangent) { | 
|---|
| 119 | *tangent = SkEvalQuadTangentAt(src, t); | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | SkPoint SkEvalQuadAt(const SkPoint src[3], SkScalar t) { | 
|---|
| 124 | return to_point(SkQuadCoeff(src).eval(t)); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | SkVector SkEvalQuadTangentAt(const SkPoint src[3], SkScalar t) { | 
|---|
| 128 | // The derivative equation is 2(b - a +(a - 2b +c)t). This returns a | 
|---|
| 129 | // zero tangent vector when t is 0 or 1, and the control point is equal | 
|---|
| 130 | // to the end point. In this case, use the quad end points to compute the tangent. | 
|---|
| 131 | if ((t == 0 && src[0] == src[1]) || (t == 1 && src[1] == src[2])) { | 
|---|
| 132 | return src[2] - src[0]; | 
|---|
| 133 | } | 
|---|
| 134 | SkASSERT(src); | 
|---|
| 135 | SkASSERT(t >= 0 && t <= SK_Scalar1); | 
|---|
| 136 |  | 
|---|
| 137 | Sk2s P0 = from_point(src[0]); | 
|---|
| 138 | Sk2s P1 = from_point(src[1]); | 
|---|
| 139 | Sk2s P2 = from_point(src[2]); | 
|---|
| 140 |  | 
|---|
| 141 | Sk2s B = P1 - P0; | 
|---|
| 142 | Sk2s A = P2 - P1 - B; | 
|---|
| 143 | Sk2s T = A * Sk2s(t) + B; | 
|---|
| 144 |  | 
|---|
| 145 | return to_vector(T + T); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | static inline Sk2s interp(const Sk2s& v0, const Sk2s& v1, const Sk2s& t) { | 
|---|
| 149 | return v0 + (v1 - v0) * t; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t) { | 
|---|
| 153 | SkASSERT(t > 0 && t < SK_Scalar1); | 
|---|
| 154 |  | 
|---|
| 155 | Sk2s p0 = from_point(src[0]); | 
|---|
| 156 | Sk2s p1 = from_point(src[1]); | 
|---|
| 157 | Sk2s p2 = from_point(src[2]); | 
|---|
| 158 | Sk2s tt(t); | 
|---|
| 159 |  | 
|---|
| 160 | Sk2s p01 = interp(p0, p1, tt); | 
|---|
| 161 | Sk2s p12 = interp(p1, p2, tt); | 
|---|
| 162 |  | 
|---|
| 163 | dst[0] = to_point(p0); | 
|---|
| 164 | dst[1] = to_point(p01); | 
|---|
| 165 | dst[2] = to_point(interp(p01, p12, tt)); | 
|---|
| 166 | dst[3] = to_point(p12); | 
|---|
| 167 | dst[4] = to_point(p2); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]) { | 
|---|
| 171 | SkChopQuadAt(src, dst, 0.5f); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | /** Quad'(t) = At + B, where | 
|---|
| 175 | A = 2(a - 2b + c) | 
|---|
| 176 | B = 2(b - a) | 
|---|
| 177 | Solve for t, only if it fits between 0 < t < 1 | 
|---|
| 178 | */ | 
|---|
| 179 | int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValue[1]) { | 
|---|
| 180 | /*  At + B == 0 | 
|---|
| 181 | t = -B / A | 
|---|
| 182 | */ | 
|---|
| 183 | return valid_unit_divide(a - b, a - b - b + c, tValue); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | static inline void flatten_double_quad_extrema(SkScalar coords[14]) { | 
|---|
| 187 | coords[2] = coords[6] = coords[4]; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | /*  Returns 0 for 1 quad, and 1 for two quads, either way the answer is | 
|---|
| 191 | stored in dst[]. Guarantees that the 1/2 quads will be monotonic. | 
|---|
| 192 | */ | 
|---|
| 193 | int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]) { | 
|---|
| 194 | SkASSERT(src); | 
|---|
| 195 | SkASSERT(dst); | 
|---|
| 196 |  | 
|---|
| 197 | SkScalar a = src[0].fY; | 
|---|
| 198 | SkScalar b = src[1].fY; | 
|---|
| 199 | SkScalar c = src[2].fY; | 
|---|
| 200 |  | 
|---|
| 201 | if (is_not_monotonic(a, b, c)) { | 
|---|
| 202 | SkScalar    tValue; | 
|---|
| 203 | if (valid_unit_divide(a - b, a - b - b + c, &tValue)) { | 
|---|
| 204 | SkChopQuadAt(src, dst, tValue); | 
|---|
| 205 | flatten_double_quad_extrema(&dst[0].fY); | 
|---|
| 206 | return 1; | 
|---|
| 207 | } | 
|---|
| 208 | // if we get here, we need to force dst to be monotonic, even though | 
|---|
| 209 | // we couldn't compute a unit_divide value (probably underflow). | 
|---|
| 210 | b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; | 
|---|
| 211 | } | 
|---|
| 212 | dst[0].set(src[0].fX, a); | 
|---|
| 213 | dst[1].set(src[1].fX, b); | 
|---|
| 214 | dst[2].set(src[2].fX, c); | 
|---|
| 215 | return 0; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | /*  Returns 0 for 1 quad, and 1 for two quads, either way the answer is | 
|---|
| 219 | stored in dst[]. Guarantees that the 1/2 quads will be monotonic. | 
|---|
| 220 | */ | 
|---|
| 221 | int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]) { | 
|---|
| 222 | SkASSERT(src); | 
|---|
| 223 | SkASSERT(dst); | 
|---|
| 224 |  | 
|---|
| 225 | SkScalar a = src[0].fX; | 
|---|
| 226 | SkScalar b = src[1].fX; | 
|---|
| 227 | SkScalar c = src[2].fX; | 
|---|
| 228 |  | 
|---|
| 229 | if (is_not_monotonic(a, b, c)) { | 
|---|
| 230 | SkScalar tValue; | 
|---|
| 231 | if (valid_unit_divide(a - b, a - b - b + c, &tValue)) { | 
|---|
| 232 | SkChopQuadAt(src, dst, tValue); | 
|---|
| 233 | flatten_double_quad_extrema(&dst[0].fX); | 
|---|
| 234 | return 1; | 
|---|
| 235 | } | 
|---|
| 236 | // if we get here, we need to force dst to be monotonic, even though | 
|---|
| 237 | // we couldn't compute a unit_divide value (probably underflow). | 
|---|
| 238 | b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c; | 
|---|
| 239 | } | 
|---|
| 240 | dst[0].set(a, src[0].fY); | 
|---|
| 241 | dst[1].set(b, src[1].fY); | 
|---|
| 242 | dst[2].set(c, src[2].fY); | 
|---|
| 243 | return 0; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | //  F(t)    = a (1 - t) ^ 2 + 2 b t (1 - t) + c t ^ 2 | 
|---|
| 247 | //  F'(t)   = 2 (b - a) + 2 (a - 2b + c) t | 
|---|
| 248 | //  F''(t)  = 2 (a - 2b + c) | 
|---|
| 249 | // | 
|---|
| 250 | //  A = 2 (b - a) | 
|---|
| 251 | //  B = 2 (a - 2b + c) | 
|---|
| 252 | // | 
|---|
| 253 | //  Maximum curvature for a quadratic means solving | 
|---|
| 254 | //  Fx' Fx'' + Fy' Fy'' = 0 | 
|---|
| 255 | // | 
|---|
| 256 | //  t = - (Ax Bx + Ay By) / (Bx ^ 2 + By ^ 2) | 
|---|
| 257 | // | 
|---|
| 258 | SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]) { | 
|---|
| 259 | SkScalar    Ax = src[1].fX - src[0].fX; | 
|---|
| 260 | SkScalar    Ay = src[1].fY - src[0].fY; | 
|---|
| 261 | SkScalar    Bx = src[0].fX - src[1].fX - src[1].fX + src[2].fX; | 
|---|
| 262 | SkScalar    By = src[0].fY - src[1].fY - src[1].fY + src[2].fY; | 
|---|
| 263 |  | 
|---|
| 264 | SkScalar numer = -(Ax * Bx + Ay * By); | 
|---|
| 265 | SkScalar denom = Bx * Bx + By * By; | 
|---|
| 266 | if (denom < 0) { | 
|---|
| 267 | numer = -numer; | 
|---|
| 268 | denom = -denom; | 
|---|
| 269 | } | 
|---|
| 270 | if (numer <= 0) { | 
|---|
| 271 | return 0; | 
|---|
| 272 | } | 
|---|
| 273 | if (numer >= denom) {  // Also catches denom=0. | 
|---|
| 274 | return 1; | 
|---|
| 275 | } | 
|---|
| 276 | SkScalar t = numer / denom; | 
|---|
| 277 | SkASSERT((0 <= t && t < 1) || SkScalarIsNaN(t)); | 
|---|
| 278 | return t; | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]) { | 
|---|
| 282 | SkScalar t = SkFindQuadMaxCurvature(src); | 
|---|
| 283 | if (t == 0 || t == 1) { | 
|---|
| 284 | memcpy(dst, src, 3 * sizeof(SkPoint)); | 
|---|
| 285 | return 1; | 
|---|
| 286 | } else { | 
|---|
| 287 | SkChopQuadAt(src, dst, t); | 
|---|
| 288 | return 2; | 
|---|
| 289 | } | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]) { | 
|---|
| 293 | Sk2s scale(SkDoubleToScalar(2.0 / 3.0)); | 
|---|
| 294 | Sk2s s0 = from_point(src[0]); | 
|---|
| 295 | Sk2s s1 = from_point(src[1]); | 
|---|
| 296 | Sk2s s2 = from_point(src[2]); | 
|---|
| 297 |  | 
|---|
| 298 | dst[0] = to_point(s0); | 
|---|
| 299 | dst[1] = to_point(s0 + (s1 - s0) * scale); | 
|---|
| 300 | dst[2] = to_point(s2 + (s1 - s2) * scale); | 
|---|
| 301 | dst[3] = to_point(s2); | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 305 | ///// CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS ///// | 
|---|
| 306 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 307 |  | 
|---|
| 308 | static SkVector eval_cubic_derivative(const SkPoint src[4], SkScalar t) { | 
|---|
| 309 | SkQuadCoeff coeff; | 
|---|
| 310 | Sk2s P0 = from_point(src[0]); | 
|---|
| 311 | Sk2s P1 = from_point(src[1]); | 
|---|
| 312 | Sk2s P2 = from_point(src[2]); | 
|---|
| 313 | Sk2s P3 = from_point(src[3]); | 
|---|
| 314 |  | 
|---|
| 315 | coeff.fA = P3 + Sk2s(3) * (P1 - P2) - P0; | 
|---|
| 316 | coeff.fB = times_2(P2 - times_2(P1) + P0); | 
|---|
| 317 | coeff.fC = P1 - P0; | 
|---|
| 318 | return to_vector(coeff.eval(t)); | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | static SkVector eval_cubic_2ndDerivative(const SkPoint src[4], SkScalar t) { | 
|---|
| 322 | Sk2s P0 = from_point(src[0]); | 
|---|
| 323 | Sk2s P1 = from_point(src[1]); | 
|---|
| 324 | Sk2s P2 = from_point(src[2]); | 
|---|
| 325 | Sk2s P3 = from_point(src[3]); | 
|---|
| 326 | Sk2s A = P3 + Sk2s(3) * (P1 - P2) - P0; | 
|---|
| 327 | Sk2s B = P2 - times_2(P1) + P0; | 
|---|
| 328 |  | 
|---|
| 329 | return to_vector(A * Sk2s(t) + B); | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* loc, | 
|---|
| 333 | SkVector* tangent, SkVector* curvature) { | 
|---|
| 334 | SkASSERT(src); | 
|---|
| 335 | SkASSERT(t >= 0 && t <= SK_Scalar1); | 
|---|
| 336 |  | 
|---|
| 337 | if (loc) { | 
|---|
| 338 | *loc = to_point(SkCubicCoeff(src).eval(t)); | 
|---|
| 339 | } | 
|---|
| 340 | if (tangent) { | 
|---|
| 341 | // The derivative equation returns a zero tangent vector when t is 0 or 1, and the | 
|---|
| 342 | // adjacent control point is equal to the end point. In this case, use the | 
|---|
| 343 | // next control point or the end points to compute the tangent. | 
|---|
| 344 | if ((t == 0 && src[0] == src[1]) || (t == 1 && src[2] == src[3])) { | 
|---|
| 345 | if (t == 0) { | 
|---|
| 346 | *tangent = src[2] - src[0]; | 
|---|
| 347 | } else { | 
|---|
| 348 | *tangent = src[3] - src[1]; | 
|---|
| 349 | } | 
|---|
| 350 | if (!tangent->fX && !tangent->fY) { | 
|---|
| 351 | *tangent = src[3] - src[0]; | 
|---|
| 352 | } | 
|---|
| 353 | } else { | 
|---|
| 354 | *tangent = eval_cubic_derivative(src, t); | 
|---|
| 355 | } | 
|---|
| 356 | } | 
|---|
| 357 | if (curvature) { | 
|---|
| 358 | *curvature = eval_cubic_2ndDerivative(src, t); | 
|---|
| 359 | } | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | /** Cubic'(t) = At^2 + Bt + C, where | 
|---|
| 363 | A = 3(-a + 3(b - c) + d) | 
|---|
| 364 | B = 6(a - 2b + c) | 
|---|
| 365 | C = 3(b - a) | 
|---|
| 366 | Solve for t, keeping only those that fit betwee 0 < t < 1 | 
|---|
| 367 | */ | 
|---|
| 368 | int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d, | 
|---|
| 369 | SkScalar tValues[2]) { | 
|---|
| 370 | // we divide A,B,C by 3 to simplify | 
|---|
| 371 | SkScalar A = d - a + 3*(b - c); | 
|---|
| 372 | SkScalar B = 2*(a - b - b + c); | 
|---|
| 373 | SkScalar C = b - a; | 
|---|
| 374 |  | 
|---|
| 375 | return SkFindUnitQuadRoots(A, B, C, tValues); | 
|---|
| 376 | } | 
|---|
| 377 |  | 
|---|
| 378 | void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t) { | 
|---|
| 379 | SkASSERT(t > 0 && t < SK_Scalar1); | 
|---|
| 380 |  | 
|---|
| 381 | Sk2s    p0 = from_point(src[0]); | 
|---|
| 382 | Sk2s    p1 = from_point(src[1]); | 
|---|
| 383 | Sk2s    p2 = from_point(src[2]); | 
|---|
| 384 | Sk2s    p3 = from_point(src[3]); | 
|---|
| 385 | Sk2s    tt(t); | 
|---|
| 386 |  | 
|---|
| 387 | Sk2s    ab = interp(p0, p1, tt); | 
|---|
| 388 | Sk2s    bc = interp(p1, p2, tt); | 
|---|
| 389 | Sk2s    cd = interp(p2, p3, tt); | 
|---|
| 390 | Sk2s    abc = interp(ab, bc, tt); | 
|---|
| 391 | Sk2s    bcd = interp(bc, cd, tt); | 
|---|
| 392 | Sk2s    abcd = interp(abc, bcd, tt); | 
|---|
| 393 |  | 
|---|
| 394 | dst[0] = to_point(p0); | 
|---|
| 395 | dst[1] = to_point(ab); | 
|---|
| 396 | dst[2] = to_point(abc); | 
|---|
| 397 | dst[3] = to_point(abcd); | 
|---|
| 398 | dst[4] = to_point(bcd); | 
|---|
| 399 | dst[5] = to_point(cd); | 
|---|
| 400 | dst[6] = to_point(p3); | 
|---|
| 401 | } | 
|---|
| 402 |  | 
|---|
| 403 | /*  http://code.google.com/p/skia/issues/detail?id=32 | 
|---|
| 404 |  | 
|---|
| 405 | This test code would fail when we didn't check the return result of | 
|---|
| 406 | valid_unit_divide in SkChopCubicAt(... tValues[], int roots). The reason is | 
|---|
| 407 | that after the first chop, the parameters to valid_unit_divide are equal | 
|---|
| 408 | (thanks to finite float precision and rounding in the subtracts). Thus | 
|---|
| 409 | even though the 2nd tValue looks < 1.0, after we renormalize it, we end | 
|---|
| 410 | up with 1.0, hence the need to check and just return the last cubic as | 
|---|
| 411 | a degenerate clump of 4 points in the sampe place. | 
|---|
| 412 |  | 
|---|
| 413 | static void test_cubic() { | 
|---|
| 414 | SkPoint src[4] = { | 
|---|
| 415 | { 556.25000, 523.03003 }, | 
|---|
| 416 | { 556.23999, 522.96002 }, | 
|---|
| 417 | { 556.21997, 522.89001 }, | 
|---|
| 418 | { 556.21997, 522.82001 } | 
|---|
| 419 | }; | 
|---|
| 420 | SkPoint dst[10]; | 
|---|
| 421 | SkScalar tval[] = { 0.33333334f, 0.99999994f }; | 
|---|
| 422 | SkChopCubicAt(src, dst, tval, 2); | 
|---|
| 423 | } | 
|---|
| 424 | */ | 
|---|
| 425 |  | 
|---|
| 426 | void SkChopCubicAt(const SkPoint src[4], SkPoint dst[], | 
|---|
| 427 | const SkScalar tValues[], int roots) { | 
|---|
| 428 | #ifdef SK_DEBUG | 
|---|
| 429 | { | 
|---|
| 430 | for (int i = 0; i < roots - 1; i++) | 
|---|
| 431 | { | 
|---|
| 432 | SkASSERT(0 < tValues[i] && tValues[i] < 1); | 
|---|
| 433 | SkASSERT(0 < tValues[i+1] && tValues[i+1] < 1); | 
|---|
| 434 | SkASSERT(tValues[i] < tValues[i+1]); | 
|---|
| 435 | } | 
|---|
| 436 | } | 
|---|
| 437 | #endif | 
|---|
| 438 |  | 
|---|
| 439 | if (dst) { | 
|---|
| 440 | if (roots == 0) { // nothing to chop | 
|---|
| 441 | memcpy(dst, src, 4*sizeof(SkPoint)); | 
|---|
| 442 | } else { | 
|---|
| 443 | SkScalar    t = tValues[0]; | 
|---|
| 444 | SkPoint     tmp[4]; | 
|---|
| 445 |  | 
|---|
| 446 | for (int i = 0; i < roots; i++) { | 
|---|
| 447 | SkChopCubicAt(src, dst, t); | 
|---|
| 448 | if (i == roots - 1) { | 
|---|
| 449 | break; | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 | dst += 3; | 
|---|
| 453 | // have src point to the remaining cubic (after the chop) | 
|---|
| 454 | memcpy(tmp, dst, 4 * sizeof(SkPoint)); | 
|---|
| 455 | src = tmp; | 
|---|
| 456 |  | 
|---|
| 457 | // watch out in case the renormalized t isn't in range | 
|---|
| 458 | if (!valid_unit_divide(tValues[i+1] - tValues[i], | 
|---|
| 459 | SK_Scalar1 - tValues[i], &t)) { | 
|---|
| 460 | // if we can't, just create a degenerate cubic | 
|---|
| 461 | dst[4] = dst[5] = dst[6] = src[3]; | 
|---|
| 462 | break; | 
|---|
| 463 | } | 
|---|
| 464 | } | 
|---|
| 465 | } | 
|---|
| 466 | } | 
|---|
| 467 | } | 
|---|
| 468 |  | 
|---|
| 469 | void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]) { | 
|---|
| 470 | SkChopCubicAt(src, dst, 0.5f); | 
|---|
| 471 | } | 
|---|
| 472 |  | 
|---|
| 473 | static void flatten_double_cubic_extrema(SkScalar coords[14]) { | 
|---|
| 474 | coords[4] = coords[8] = coords[6]; | 
|---|
| 475 | } | 
|---|
| 476 |  | 
|---|
| 477 | /** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that | 
|---|
| 478 | the resulting beziers are monotonic in Y. This is called by the scan | 
|---|
| 479 | converter.  Depending on what is returned, dst[] is treated as follows: | 
|---|
| 480 | 0   dst[0..3] is the original cubic | 
|---|
| 481 | 1   dst[0..3] and dst[3..6] are the two new cubics | 
|---|
| 482 | 2   dst[0..3], dst[3..6], dst[6..9] are the three new cubics | 
|---|
| 483 | If dst == null, it is ignored and only the count is returned. | 
|---|
| 484 | */ | 
|---|
| 485 | int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]) { | 
|---|
| 486 | SkScalar    tValues[2]; | 
|---|
| 487 | int         roots = SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY, | 
|---|
| 488 | src[3].fY, tValues); | 
|---|
| 489 |  | 
|---|
| 490 | SkChopCubicAt(src, dst, tValues, roots); | 
|---|
| 491 | if (dst && roots > 0) { | 
|---|
| 492 | // we do some cleanup to ensure our Y extrema are flat | 
|---|
| 493 | flatten_double_cubic_extrema(&dst[0].fY); | 
|---|
| 494 | if (roots == 2) { | 
|---|
| 495 | flatten_double_cubic_extrema(&dst[3].fY); | 
|---|
| 496 | } | 
|---|
| 497 | } | 
|---|
| 498 | return roots; | 
|---|
| 499 | } | 
|---|
| 500 |  | 
|---|
| 501 | int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]) { | 
|---|
| 502 | SkScalar    tValues[2]; | 
|---|
| 503 | int         roots = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX, | 
|---|
| 504 | src[3].fX, tValues); | 
|---|
| 505 |  | 
|---|
| 506 | SkChopCubicAt(src, dst, tValues, roots); | 
|---|
| 507 | if (dst && roots > 0) { | 
|---|
| 508 | // we do some cleanup to ensure our Y extrema are flat | 
|---|
| 509 | flatten_double_cubic_extrema(&dst[0].fX); | 
|---|
| 510 | if (roots == 2) { | 
|---|
| 511 | flatten_double_cubic_extrema(&dst[3].fX); | 
|---|
| 512 | } | 
|---|
| 513 | } | 
|---|
| 514 | return roots; | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 | /** http://www.faculty.idc.ac.il/arik/quality/appendixA.html | 
|---|
| 518 |  | 
|---|
| 519 | Inflection means that curvature is zero. | 
|---|
| 520 | Curvature is [F' x F''] / [F'^3] | 
|---|
| 521 | So we solve F'x X F''y - F'y X F''y == 0 | 
|---|
| 522 | After some canceling of the cubic term, we get | 
|---|
| 523 | A = b - a | 
|---|
| 524 | B = c - 2b + a | 
|---|
| 525 | C = d - 3c + 3b - a | 
|---|
| 526 | (BxCy - ByCx)t^2 + (AxCy - AyCx)t + AxBy - AyBx == 0 | 
|---|
| 527 | */ | 
|---|
| 528 | int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[]) { | 
|---|
| 529 | SkScalar    Ax = src[1].fX - src[0].fX; | 
|---|
| 530 | SkScalar    Ay = src[1].fY - src[0].fY; | 
|---|
| 531 | SkScalar    Bx = src[2].fX - 2 * src[1].fX + src[0].fX; | 
|---|
| 532 | SkScalar    By = src[2].fY - 2 * src[1].fY + src[0].fY; | 
|---|
| 533 | SkScalar    Cx = src[3].fX + 3 * (src[1].fX - src[2].fX) - src[0].fX; | 
|---|
| 534 | SkScalar    Cy = src[3].fY + 3 * (src[1].fY - src[2].fY) - src[0].fY; | 
|---|
| 535 |  | 
|---|
| 536 | return SkFindUnitQuadRoots(Bx*Cy - By*Cx, | 
|---|
| 537 | Ax*Cy - Ay*Cx, | 
|---|
| 538 | Ax*By - Ay*Bx, | 
|---|
| 539 | tValues); | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | int SkChopCubicAtInflections(const SkPoint src[], SkPoint dst[10]) { | 
|---|
| 543 | SkScalar    tValues[2]; | 
|---|
| 544 | int         count = SkFindCubicInflections(src, tValues); | 
|---|
| 545 |  | 
|---|
| 546 | if (dst) { | 
|---|
| 547 | if (count == 0) { | 
|---|
| 548 | memcpy(dst, src, 4 * sizeof(SkPoint)); | 
|---|
| 549 | } else { | 
|---|
| 550 | SkChopCubicAt(src, dst, tValues, count); | 
|---|
| 551 | } | 
|---|
| 552 | } | 
|---|
| 553 | return count + 1; | 
|---|
| 554 | } | 
|---|
| 555 |  | 
|---|
| 556 | // Assumes the third component of points is 1. | 
|---|
| 557 | // Calcs p0 . (p1 x p2) | 
|---|
| 558 | static double calc_dot_cross_cubic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { | 
|---|
| 559 | const double xComp = (double) p0.fX * ((double) p1.fY - (double) p2.fY); | 
|---|
| 560 | const double yComp = (double) p0.fY * ((double) p2.fX - (double) p1.fX); | 
|---|
| 561 | const double wComp = (double) p1.fX * (double) p2.fY - (double) p1.fY * (double) p2.fX; | 
|---|
| 562 | return (xComp + yComp + wComp); | 
|---|
| 563 | } | 
|---|
| 564 |  | 
|---|
| 565 | // Returns a positive power of 2 that, when multiplied by n, and excepting the two edge cases listed | 
|---|
| 566 | // below, shifts the exponent of n to yield a magnitude somewhere inside [1..2). | 
|---|
| 567 | // Returns 2^1023 if abs(n) < 2^-1022 (including 0). | 
|---|
| 568 | // Returns NaN if n is Inf or NaN. | 
|---|
| 569 | inline static double previous_inverse_pow2(double n) { | 
|---|
| 570 | uint64_t bits; | 
|---|
| 571 | memcpy(&bits, &n, sizeof(double)); | 
|---|
| 572 | bits = ((1023llu*2 << 52) + ((1llu << 52) - 1)) - bits; // exp=-exp | 
|---|
| 573 | bits &= (0x7ffllu) << 52; // mantissa=1.0, sign=0 | 
|---|
| 574 | memcpy(&n, &bits, sizeof(double)); | 
|---|
| 575 | return n; | 
|---|
| 576 | } | 
|---|
| 577 |  | 
|---|
| 578 | inline static void write_cubic_inflection_roots(double t0, double s0, double t1, double s1, | 
|---|
| 579 | double* t, double* s) { | 
|---|
| 580 | t[0] = t0; | 
|---|
| 581 | s[0] = s0; | 
|---|
| 582 |  | 
|---|
| 583 | // This copysign/abs business orients the implicit function so positive values are always on the | 
|---|
| 584 | // "left" side of the curve. | 
|---|
| 585 | t[1] = -copysign(t1, t1 * s1); | 
|---|
| 586 | s[1] = -fabs(s1); | 
|---|
| 587 |  | 
|---|
| 588 | // Ensure t[0]/s[0] <= t[1]/s[1] (s[1] is negative from above). | 
|---|
| 589 | if (copysign(s[1], s[0]) * t[0] > -fabs(s[0]) * t[1]) { | 
|---|
| 590 | using std::swap; | 
|---|
| 591 | swap(t[0], t[1]); | 
|---|
| 592 | swap(s[0], s[1]); | 
|---|
| 593 | } | 
|---|
| 594 | } | 
|---|
| 595 |  | 
|---|
| 596 | SkCubicType SkClassifyCubic(const SkPoint P[4], double t[2], double s[2], double d[4]) { | 
|---|
| 597 | // Find the cubic's inflection function, I = [T^3  -3T^2  3T  -1] dot D. (D0 will always be 0 | 
|---|
| 598 | // for integral cubics.) | 
|---|
| 599 | // | 
|---|
| 600 | // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware", | 
|---|
| 601 | // 4.2 Curve Categorization: | 
|---|
| 602 | // | 
|---|
| 603 | // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf | 
|---|
| 604 | double A1 = calc_dot_cross_cubic(P[0], P[3], P[2]); | 
|---|
| 605 | double A2 = calc_dot_cross_cubic(P[1], P[0], P[3]); | 
|---|
| 606 | double A3 = calc_dot_cross_cubic(P[2], P[1], P[0]); | 
|---|
| 607 |  | 
|---|
| 608 | double D3 = 3 * A3; | 
|---|
| 609 | double D2 = D3 - A2; | 
|---|
| 610 | double D1 = D2 - A2 + A1; | 
|---|
| 611 |  | 
|---|
| 612 | // Shift the exponents in D so the largest magnitude falls somewhere in 1..2. This protects us | 
|---|
| 613 | // from overflow down the road while solving for roots and KLM functionals. | 
|---|
| 614 | double Dmax = std::max(std::max(fabs(D1), fabs(D2)), fabs(D3)); | 
|---|
| 615 | double norm = previous_inverse_pow2(Dmax); | 
|---|
| 616 | D1 *= norm; | 
|---|
| 617 | D2 *= norm; | 
|---|
| 618 | D3 *= norm; | 
|---|
| 619 |  | 
|---|
| 620 | if (d) { | 
|---|
| 621 | d[3] = D3; | 
|---|
| 622 | d[2] = D2; | 
|---|
| 623 | d[1] = D1; | 
|---|
| 624 | d[0] = 0; | 
|---|
| 625 | } | 
|---|
| 626 |  | 
|---|
| 627 | // Now use the inflection function to classify the cubic. | 
|---|
| 628 | // | 
|---|
| 629 | // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware", | 
|---|
| 630 | // 4.4 Integral Cubics: | 
|---|
| 631 | // | 
|---|
| 632 | // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf | 
|---|
| 633 | if (0 != D1) { | 
|---|
| 634 | double discr = 3*D2*D2 - 4*D1*D3; | 
|---|
| 635 | if (discr > 0) { // Serpentine. | 
|---|
| 636 | if (t && s) { | 
|---|
| 637 | double q = 3*D2 + copysign(sqrt(3*discr), D2); | 
|---|
| 638 | write_cubic_inflection_roots(q, 6*D1, 2*D3, q, t, s); | 
|---|
| 639 | } | 
|---|
| 640 | return SkCubicType::kSerpentine; | 
|---|
| 641 | } else if (discr < 0) { // Loop. | 
|---|
| 642 | if (t && s) { | 
|---|
| 643 | double q = D2 + copysign(sqrt(-discr), D2); | 
|---|
| 644 | write_cubic_inflection_roots(q, 2*D1, 2*(D2*D2 - D3*D1), D1*q, t, s); | 
|---|
| 645 | } | 
|---|
| 646 | return SkCubicType::kLoop; | 
|---|
| 647 | } else { // Cusp. | 
|---|
| 648 | if (t && s) { | 
|---|
| 649 | write_cubic_inflection_roots(D2, 2*D1, D2, 2*D1, t, s); | 
|---|
| 650 | } | 
|---|
| 651 | return SkCubicType::kLocalCusp; | 
|---|
| 652 | } | 
|---|
| 653 | } else { | 
|---|
| 654 | if (0 != D2) { // Cusp at T=infinity. | 
|---|
| 655 | if (t && s) { | 
|---|
| 656 | write_cubic_inflection_roots(D3, 3*D2, 1, 0, t, s); // T1=infinity. | 
|---|
| 657 | } | 
|---|
| 658 | return SkCubicType::kCuspAtInfinity; | 
|---|
| 659 | } else { // Degenerate. | 
|---|
| 660 | if (t && s) { | 
|---|
| 661 | write_cubic_inflection_roots(1, 0, 1, 0, t, s); // T0=T1=infinity. | 
|---|
| 662 | } | 
|---|
| 663 | return 0 != D3 ? SkCubicType::kQuadratic : SkCubicType::kLineOrPoint; | 
|---|
| 664 | } | 
|---|
| 665 | } | 
|---|
| 666 | } | 
|---|
| 667 |  | 
|---|
| 668 | template <typename T> void bubble_sort(T array[], int count) { | 
|---|
| 669 | for (int i = count - 1; i > 0; --i) | 
|---|
| 670 | for (int j = i; j > 0; --j) | 
|---|
| 671 | if (array[j] < array[j-1]) | 
|---|
| 672 | { | 
|---|
| 673 | T   tmp(array[j]); | 
|---|
| 674 | array[j] = array[j-1]; | 
|---|
| 675 | array[j-1] = tmp; | 
|---|
| 676 | } | 
|---|
| 677 | } | 
|---|
| 678 |  | 
|---|
| 679 | /** | 
|---|
| 680 | *  Given an array and count, remove all pair-wise duplicates from the array, | 
|---|
| 681 | *  keeping the existing sorting, and return the new count | 
|---|
| 682 | */ | 
|---|
| 683 | static int collaps_duplicates(SkScalar array[], int count) { | 
|---|
| 684 | for (int n = count; n > 1; --n) { | 
|---|
| 685 | if (array[0] == array[1]) { | 
|---|
| 686 | for (int i = 1; i < n; ++i) { | 
|---|
| 687 | array[i - 1] = array[i]; | 
|---|
| 688 | } | 
|---|
| 689 | count -= 1; | 
|---|
| 690 | } else { | 
|---|
| 691 | array += 1; | 
|---|
| 692 | } | 
|---|
| 693 | } | 
|---|
| 694 | return count; | 
|---|
| 695 | } | 
|---|
| 696 |  | 
|---|
| 697 | #ifdef SK_DEBUG | 
|---|
| 698 |  | 
|---|
| 699 | #define TEST_COLLAPS_ENTRY(array)   array, SK_ARRAY_COUNT(array) | 
|---|
| 700 |  | 
|---|
| 701 | static void test_collaps_duplicates() { | 
|---|
| 702 | static bool gOnce; | 
|---|
| 703 | if (gOnce) { return; } | 
|---|
| 704 | gOnce = true; | 
|---|
| 705 | const SkScalar src0[] = { 0 }; | 
|---|
| 706 | const SkScalar src1[] = { 0, 0 }; | 
|---|
| 707 | const SkScalar src2[] = { 0, 1 }; | 
|---|
| 708 | const SkScalar src3[] = { 0, 0, 0 }; | 
|---|
| 709 | const SkScalar src4[] = { 0, 0, 1 }; | 
|---|
| 710 | const SkScalar src5[] = { 0, 1, 1 }; | 
|---|
| 711 | const SkScalar src6[] = { 0, 1, 2 }; | 
|---|
| 712 | const struct { | 
|---|
| 713 | const SkScalar* fData; | 
|---|
| 714 | int fCount; | 
|---|
| 715 | int fCollapsedCount; | 
|---|
| 716 | } data[] = { | 
|---|
| 717 | { TEST_COLLAPS_ENTRY(src0), 1 }, | 
|---|
| 718 | { TEST_COLLAPS_ENTRY(src1), 1 }, | 
|---|
| 719 | { TEST_COLLAPS_ENTRY(src2), 2 }, | 
|---|
| 720 | { TEST_COLLAPS_ENTRY(src3), 1 }, | 
|---|
| 721 | { TEST_COLLAPS_ENTRY(src4), 2 }, | 
|---|
| 722 | { TEST_COLLAPS_ENTRY(src5), 2 }, | 
|---|
| 723 | { TEST_COLLAPS_ENTRY(src6), 3 }, | 
|---|
| 724 | }; | 
|---|
| 725 | for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { | 
|---|
| 726 | SkScalar dst[3]; | 
|---|
| 727 | memcpy(dst, data[i].fData, data[i].fCount * sizeof(dst[0])); | 
|---|
| 728 | int count = collaps_duplicates(dst, data[i].fCount); | 
|---|
| 729 | SkASSERT(data[i].fCollapsedCount == count); | 
|---|
| 730 | for (int j = 1; j < count; ++j) { | 
|---|
| 731 | SkASSERT(dst[j-1] < dst[j]); | 
|---|
| 732 | } | 
|---|
| 733 | } | 
|---|
| 734 | } | 
|---|
| 735 | #endif | 
|---|
| 736 |  | 
|---|
| 737 | static SkScalar SkScalarCubeRoot(SkScalar x) { | 
|---|
| 738 | return SkScalarPow(x, 0.3333333f); | 
|---|
| 739 | } | 
|---|
| 740 |  | 
|---|
| 741 | /*  Solve coeff(t) == 0, returning the number of roots that | 
|---|
| 742 | lie withing 0 < t < 1. | 
|---|
| 743 | coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3] | 
|---|
| 744 |  | 
|---|
| 745 | Eliminates repeated roots (so that all tValues are distinct, and are always | 
|---|
| 746 | in increasing order. | 
|---|
| 747 | */ | 
|---|
| 748 | static int solve_cubic_poly(const SkScalar coeff[4], SkScalar tValues[3]) { | 
|---|
| 749 | if (SkScalarNearlyZero(coeff[0])) {  // we're just a quadratic | 
|---|
| 750 | return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues); | 
|---|
| 751 | } | 
|---|
| 752 |  | 
|---|
| 753 | SkScalar a, b, c, Q, R; | 
|---|
| 754 |  | 
|---|
| 755 | { | 
|---|
| 756 | SkASSERT(coeff[0] != 0); | 
|---|
| 757 |  | 
|---|
| 758 | SkScalar inva = SkScalarInvert(coeff[0]); | 
|---|
| 759 | a = coeff[1] * inva; | 
|---|
| 760 | b = coeff[2] * inva; | 
|---|
| 761 | c = coeff[3] * inva; | 
|---|
| 762 | } | 
|---|
| 763 | Q = (a*a - b*3) / 9; | 
|---|
| 764 | R = (2*a*a*a - 9*a*b + 27*c) / 54; | 
|---|
| 765 |  | 
|---|
| 766 | SkScalar Q3 = Q * Q * Q; | 
|---|
| 767 | SkScalar R2MinusQ3 = R * R - Q3; | 
|---|
| 768 | SkScalar adiv3 = a / 3; | 
|---|
| 769 |  | 
|---|
| 770 | if (R2MinusQ3 < 0) { // we have 3 real roots | 
|---|
| 771 | // the divide/root can, due to finite precisions, be slightly outside of -1...1 | 
|---|
| 772 | SkScalar theta = SkScalarACos(SkTPin(R / SkScalarSqrt(Q3), -1.0f, 1.0f)); | 
|---|
| 773 | SkScalar neg2RootQ = -2 * SkScalarSqrt(Q); | 
|---|
| 774 |  | 
|---|
| 775 | tValues[0] = SkTPin(neg2RootQ * SkScalarCos(theta/3) - adiv3, 0.0f, 1.0f); | 
|---|
| 776 | tValues[1] = SkTPin(neg2RootQ * SkScalarCos((theta + 2*SK_ScalarPI)/3) - adiv3, 0.0f, 1.0f); | 
|---|
| 777 | tValues[2] = SkTPin(neg2RootQ * SkScalarCos((theta - 2*SK_ScalarPI)/3) - adiv3, 0.0f, 1.0f); | 
|---|
| 778 | SkDEBUGCODE(test_collaps_duplicates();) | 
|---|
| 779 |  | 
|---|
| 780 | // now sort the roots | 
|---|
| 781 | bubble_sort(tValues, 3); | 
|---|
| 782 | return collaps_duplicates(tValues, 3); | 
|---|
| 783 | } else {              // we have 1 real root | 
|---|
| 784 | SkScalar A = SkScalarAbs(R) + SkScalarSqrt(R2MinusQ3); | 
|---|
| 785 | A = SkScalarCubeRoot(A); | 
|---|
| 786 | if (R > 0) { | 
|---|
| 787 | A = -A; | 
|---|
| 788 | } | 
|---|
| 789 | if (A != 0) { | 
|---|
| 790 | A += Q / A; | 
|---|
| 791 | } | 
|---|
| 792 | tValues[0] = SkTPin(A - adiv3, 0.0f, 1.0f); | 
|---|
| 793 | return 1; | 
|---|
| 794 | } | 
|---|
| 795 | } | 
|---|
| 796 |  | 
|---|
| 797 | /*  Looking for F' dot F'' == 0 | 
|---|
| 798 |  | 
|---|
| 799 | A = b - a | 
|---|
| 800 | B = c - 2b + a | 
|---|
| 801 | C = d - 3c + 3b - a | 
|---|
| 802 |  | 
|---|
| 803 | F' = 3Ct^2 + 6Bt + 3A | 
|---|
| 804 | F'' = 6Ct + 6B | 
|---|
| 805 |  | 
|---|
| 806 | F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB | 
|---|
| 807 | */ | 
|---|
| 808 | static void formulate_F1DotF2(const SkScalar src[], SkScalar coeff[4]) { | 
|---|
| 809 | SkScalar    a = src[2] - src[0]; | 
|---|
| 810 | SkScalar    b = src[4] - 2 * src[2] + src[0]; | 
|---|
| 811 | SkScalar    c = src[6] + 3 * (src[2] - src[4]) - src[0]; | 
|---|
| 812 |  | 
|---|
| 813 | coeff[0] = c * c; | 
|---|
| 814 | coeff[1] = 3 * b * c; | 
|---|
| 815 | coeff[2] = 2 * b * b + c * a; | 
|---|
| 816 | coeff[3] = a * b; | 
|---|
| 817 | } | 
|---|
| 818 |  | 
|---|
| 819 | /*  Looking for F' dot F'' == 0 | 
|---|
| 820 |  | 
|---|
| 821 | A = b - a | 
|---|
| 822 | B = c - 2b + a | 
|---|
| 823 | C = d - 3c + 3b - a | 
|---|
| 824 |  | 
|---|
| 825 | F' = 3Ct^2 + 6Bt + 3A | 
|---|
| 826 | F'' = 6Ct + 6B | 
|---|
| 827 |  | 
|---|
| 828 | F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB | 
|---|
| 829 | */ | 
|---|
| 830 | int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]) { | 
|---|
| 831 | SkScalar coeffX[4], coeffY[4]; | 
|---|
| 832 | int      i; | 
|---|
| 833 |  | 
|---|
| 834 | formulate_F1DotF2(&src[0].fX, coeffX); | 
|---|
| 835 | formulate_F1DotF2(&src[0].fY, coeffY); | 
|---|
| 836 |  | 
|---|
| 837 | for (i = 0; i < 4; i++) { | 
|---|
| 838 | coeffX[i] += coeffY[i]; | 
|---|
| 839 | } | 
|---|
| 840 |  | 
|---|
| 841 | int numRoots = solve_cubic_poly(coeffX, tValues); | 
|---|
| 842 | // now remove extrema where the curvature is zero (mins) | 
|---|
| 843 | // !!!! need a test for this !!!! | 
|---|
| 844 | return numRoots; | 
|---|
| 845 | } | 
|---|
| 846 |  | 
|---|
| 847 | int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13], | 
|---|
| 848 | SkScalar tValues[3]) { | 
|---|
| 849 | SkScalar    t_storage[3]; | 
|---|
| 850 |  | 
|---|
| 851 | if (tValues == nullptr) { | 
|---|
| 852 | tValues = t_storage; | 
|---|
| 853 | } | 
|---|
| 854 |  | 
|---|
| 855 | SkScalar roots[3]; | 
|---|
| 856 | int rootCount = SkFindCubicMaxCurvature(src, roots); | 
|---|
| 857 |  | 
|---|
| 858 | // Throw out values not inside 0..1. | 
|---|
| 859 | int count = 0; | 
|---|
| 860 | for (int i = 0; i < rootCount; ++i) { | 
|---|
| 861 | if (0 < roots[i] && roots[i] < 1) { | 
|---|
| 862 | tValues[count++] = roots[i]; | 
|---|
| 863 | } | 
|---|
| 864 | } | 
|---|
| 865 |  | 
|---|
| 866 | if (dst) { | 
|---|
| 867 | if (count == 0) { | 
|---|
| 868 | memcpy(dst, src, 4 * sizeof(SkPoint)); | 
|---|
| 869 | } else { | 
|---|
| 870 | SkChopCubicAt(src, dst, tValues, count); | 
|---|
| 871 | } | 
|---|
| 872 | } | 
|---|
| 873 | return count + 1; | 
|---|
| 874 | } | 
|---|
| 875 |  | 
|---|
| 876 | // Returns a constant proportional to the dimensions of the cubic. | 
|---|
| 877 | // Constant found through experimentation -- maybe there's a better way.... | 
|---|
| 878 | static SkScalar calc_cubic_precision(const SkPoint src[4]) { | 
|---|
| 879 | return (SkPointPriv::DistanceToSqd(src[1], src[0]) + SkPointPriv::DistanceToSqd(src[2], src[1]) | 
|---|
| 880 | + SkPointPriv::DistanceToSqd(src[3], src[2])) * 1e-8f; | 
|---|
| 881 | } | 
|---|
| 882 |  | 
|---|
| 883 | // Returns true if both points src[testIndex], src[testIndex+1] are in the same half plane defined | 
|---|
| 884 | // by the line segment src[lineIndex], src[lineIndex+1]. | 
|---|
| 885 | static bool on_same_side(const SkPoint src[4], int testIndex, int lineIndex) { | 
|---|
| 886 | SkPoint origin = src[lineIndex]; | 
|---|
| 887 | SkVector line = src[lineIndex + 1] - origin; | 
|---|
| 888 | SkScalar crosses[2]; | 
|---|
| 889 | for (int index = 0; index < 2; ++index) { | 
|---|
| 890 | SkVector testLine = src[testIndex + index] - origin; | 
|---|
| 891 | crosses[index] = line.cross(testLine); | 
|---|
| 892 | } | 
|---|
| 893 | return crosses[0] * crosses[1] >= 0; | 
|---|
| 894 | } | 
|---|
| 895 |  | 
|---|
| 896 | // Return location (in t) of cubic cusp, if there is one. | 
|---|
| 897 | // Note that classify cubic code does not reliably return all cusp'd cubics, so | 
|---|
| 898 | // it is not called here. | 
|---|
| 899 | SkScalar SkFindCubicCusp(const SkPoint src[4]) { | 
|---|
| 900 | // When the adjacent control point matches the end point, it behaves as if | 
|---|
| 901 | // the cubic has a cusp: there's a point of max curvature where the derivative | 
|---|
| 902 | // goes to zero. Ideally, this would be where t is zero or one, but math | 
|---|
| 903 | // error makes not so. It is not uncommon to create cubics this way; skip them. | 
|---|
| 904 | if (src[0] == src[1]) { | 
|---|
| 905 | return -1; | 
|---|
| 906 | } | 
|---|
| 907 | if (src[2] == src[3]) { | 
|---|
| 908 | return -1; | 
|---|
| 909 | } | 
|---|
| 910 | // Cubics only have a cusp if the line segments formed by the control and end points cross. | 
|---|
| 911 | // Detect crossing if line ends are on opposite sides of plane formed by the other line. | 
|---|
| 912 | if (on_same_side(src, 0, 2) || on_same_side(src, 2, 0)) { | 
|---|
| 913 | return -1; | 
|---|
| 914 | } | 
|---|
| 915 | // Cubics may have multiple points of maximum curvature, although at most only | 
|---|
| 916 | // one is a cusp. | 
|---|
| 917 | SkScalar maxCurvature[3]; | 
|---|
| 918 | int roots = SkFindCubicMaxCurvature(src, maxCurvature); | 
|---|
| 919 | for (int index = 0; index < roots; ++index) { | 
|---|
| 920 | SkScalar testT = maxCurvature[index]; | 
|---|
| 921 | if (0 >= testT || testT >= 1) {  // no need to consider max curvature on the end | 
|---|
| 922 | continue; | 
|---|
| 923 | } | 
|---|
| 924 | // A cusp is at the max curvature, and also has a derivative close to zero. | 
|---|
| 925 | // Choose the 'close to zero' meaning by comparing the derivative length | 
|---|
| 926 | // with the overall cubic size. | 
|---|
| 927 | SkVector dPt = eval_cubic_derivative(src, testT); | 
|---|
| 928 | SkScalar dPtMagnitude = SkPointPriv::LengthSqd(dPt); | 
|---|
| 929 | SkScalar precision = calc_cubic_precision(src); | 
|---|
| 930 | if (dPtMagnitude < precision) { | 
|---|
| 931 | // All three max curvature t values may be close to the cusp; | 
|---|
| 932 | // return the first one. | 
|---|
| 933 | return testT; | 
|---|
| 934 | } | 
|---|
| 935 | } | 
|---|
| 936 | return -1; | 
|---|
| 937 | } | 
|---|
| 938 |  | 
|---|
| 939 | #include "src/pathops/SkPathOpsCubic.h" | 
|---|
| 940 |  | 
|---|
| 941 | typedef int (SkDCubic::*InterceptProc)(double intercept, double roots[3]) const; | 
|---|
| 942 |  | 
|---|
| 943 | static bool cubic_dchop_at_intercept(const SkPoint src[4], SkScalar intercept, SkPoint dst[7], | 
|---|
| 944 | InterceptProc method) { | 
|---|
| 945 | SkDCubic cubic; | 
|---|
| 946 | double roots[3]; | 
|---|
| 947 | int count = (cubic.set(src).*method)(intercept, roots); | 
|---|
| 948 | if (count > 0) { | 
|---|
| 949 | SkDCubicPair pair = cubic.chopAt(roots[0]); | 
|---|
| 950 | for (int i = 0; i < 7; ++i) { | 
|---|
| 951 | dst[i] = pair.pts[i].asSkPoint(); | 
|---|
| 952 | } | 
|---|
| 953 | return true; | 
|---|
| 954 | } | 
|---|
| 955 | return false; | 
|---|
| 956 | } | 
|---|
| 957 |  | 
|---|
| 958 | bool SkChopMonoCubicAtY(SkPoint src[4], SkScalar y, SkPoint dst[7]) { | 
|---|
| 959 | return cubic_dchop_at_intercept(src, y, dst, &SkDCubic::horizontalIntersect); | 
|---|
| 960 | } | 
|---|
| 961 |  | 
|---|
| 962 | bool SkChopMonoCubicAtX(SkPoint src[4], SkScalar x, SkPoint dst[7]) { | 
|---|
| 963 | return cubic_dchop_at_intercept(src, x, dst, &SkDCubic::verticalIntersect); | 
|---|
| 964 | } | 
|---|
| 965 |  | 
|---|
| 966 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 967 | // | 
|---|
| 968 | // NURB representation for conics.  Helpful explanations at: | 
|---|
| 969 | // | 
|---|
| 970 | // http://citeseerx.ist.psu.edu/viewdoc/ | 
|---|
| 971 | //   download?doi=10.1.1.44.5740&rep=rep1&type=ps | 
|---|
| 972 | // and | 
|---|
| 973 | // http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html | 
|---|
| 974 | // | 
|---|
| 975 | // F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w) | 
|---|
| 976 | //     ------------------------------------------ | 
|---|
| 977 | //         ((1 - t)^2 + t^2 + 2 (1 - t) t w) | 
|---|
| 978 | // | 
|---|
| 979 | //   = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0} | 
|---|
| 980 | //     ------------------------------------------------ | 
|---|
| 981 | //             {t^2 (2 - 2 w), t (-2 + 2 w), 1} | 
|---|
| 982 | // | 
|---|
| 983 |  | 
|---|
| 984 | // F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w) | 
|---|
| 985 | // | 
|---|
| 986 | //  t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w) | 
|---|
| 987 | //  t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w) | 
|---|
| 988 | //  t^0 : -2 P0 w + 2 P1 w | 
|---|
| 989 | // | 
|---|
| 990 | //  We disregard magnitude, so we can freely ignore the denominator of F', and | 
|---|
| 991 | //  divide the numerator by 2 | 
|---|
| 992 | // | 
|---|
| 993 | //    coeff[0] for t^2 | 
|---|
| 994 | //    coeff[1] for t^1 | 
|---|
| 995 | //    coeff[2] for t^0 | 
|---|
| 996 | // | 
|---|
| 997 | static void conic_deriv_coeff(const SkScalar src[], | 
|---|
| 998 | SkScalar w, | 
|---|
| 999 | SkScalar coeff[3]) { | 
|---|
| 1000 | const SkScalar P20 = src[4] - src[0]; | 
|---|
| 1001 | const SkScalar P10 = src[2] - src[0]; | 
|---|
| 1002 | const SkScalar wP10 = w * P10; | 
|---|
| 1003 | coeff[0] = w * P20 - P20; | 
|---|
| 1004 | coeff[1] = P20 - 2 * wP10; | 
|---|
| 1005 | coeff[2] = wP10; | 
|---|
| 1006 | } | 
|---|
| 1007 |  | 
|---|
| 1008 | static bool conic_find_extrema(const SkScalar src[], SkScalar w, SkScalar* t) { | 
|---|
| 1009 | SkScalar coeff[3]; | 
|---|
| 1010 | conic_deriv_coeff(src, w, coeff); | 
|---|
| 1011 |  | 
|---|
| 1012 | SkScalar tValues[2]; | 
|---|
| 1013 | int roots = SkFindUnitQuadRoots(coeff[0], coeff[1], coeff[2], tValues); | 
|---|
| 1014 | SkASSERT(0 == roots || 1 == roots); | 
|---|
| 1015 |  | 
|---|
| 1016 | if (1 == roots) { | 
|---|
| 1017 | *t = tValues[0]; | 
|---|
| 1018 | return true; | 
|---|
| 1019 | } | 
|---|
| 1020 | return false; | 
|---|
| 1021 | } | 
|---|
| 1022 |  | 
|---|
| 1023 | // We only interpolate one dimension at a time (the first, at +0, +3, +6). | 
|---|
| 1024 | static void p3d_interp(const SkScalar src[7], SkScalar dst[7], SkScalar t) { | 
|---|
| 1025 | SkScalar ab = SkScalarInterp(src[0], src[3], t); | 
|---|
| 1026 | SkScalar bc = SkScalarInterp(src[3], src[6], t); | 
|---|
| 1027 | dst[0] = ab; | 
|---|
| 1028 | dst[3] = SkScalarInterp(ab, bc, t); | 
|---|
| 1029 | dst[6] = bc; | 
|---|
| 1030 | } | 
|---|
| 1031 |  | 
|---|
| 1032 | static void ratquad_mapTo3D(const SkPoint src[3], SkScalar w, SkPoint3 dst[3]) { | 
|---|
| 1033 | dst[0].set(src[0].fX * 1, src[0].fY * 1, 1); | 
|---|
| 1034 | dst[1].set(src[1].fX * w, src[1].fY * w, w); | 
|---|
| 1035 | dst[2].set(src[2].fX * 1, src[2].fY * 1, 1); | 
|---|
| 1036 | } | 
|---|
| 1037 |  | 
|---|
| 1038 | static SkPoint project_down(const SkPoint3& src) { | 
|---|
| 1039 | return {src.fX / src.fZ, src.fY / src.fZ}; | 
|---|
| 1040 | } | 
|---|
| 1041 |  | 
|---|
| 1042 | // return false if infinity or NaN is generated; caller must check | 
|---|
| 1043 | bool SkConic::chopAt(SkScalar t, SkConic dst[2]) const { | 
|---|
| 1044 | SkPoint3 tmp[3], tmp2[3]; | 
|---|
| 1045 |  | 
|---|
| 1046 | ratquad_mapTo3D(fPts, fW, tmp); | 
|---|
| 1047 |  | 
|---|
| 1048 | p3d_interp(&tmp[0].fX, &tmp2[0].fX, t); | 
|---|
| 1049 | p3d_interp(&tmp[0].fY, &tmp2[0].fY, t); | 
|---|
| 1050 | p3d_interp(&tmp[0].fZ, &tmp2[0].fZ, t); | 
|---|
| 1051 |  | 
|---|
| 1052 | dst[0].fPts[0] = fPts[0]; | 
|---|
| 1053 | dst[0].fPts[1] = project_down(tmp2[0]); | 
|---|
| 1054 | dst[0].fPts[2] = project_down(tmp2[1]); dst[1].fPts[0] = dst[0].fPts[2]; | 
|---|
| 1055 | dst[1].fPts[1] = project_down(tmp2[2]); | 
|---|
| 1056 | dst[1].fPts[2] = fPts[2]; | 
|---|
| 1057 |  | 
|---|
| 1058 | // to put in "standard form", where w0 and w2 are both 1, we compute the | 
|---|
| 1059 | // new w1 as sqrt(w1*w1/w0*w2) | 
|---|
| 1060 | // or | 
|---|
| 1061 | // w1 /= sqrt(w0*w2) | 
|---|
| 1062 | // | 
|---|
| 1063 | // However, in our case, we know that for dst[0]: | 
|---|
| 1064 | //     w0 == 1, and for dst[1], w2 == 1 | 
|---|
| 1065 | // | 
|---|
| 1066 | SkScalar root = SkScalarSqrt(tmp2[1].fZ); | 
|---|
| 1067 | dst[0].fW = tmp2[0].fZ / root; | 
|---|
| 1068 | dst[1].fW = tmp2[2].fZ / root; | 
|---|
| 1069 | SkASSERT(sizeof(dst[0]) == sizeof(SkScalar) * 7); | 
|---|
| 1070 | SkASSERT(0 == offsetof(SkConic, fPts[0].fX)); | 
|---|
| 1071 | return SkScalarsAreFinite(&dst[0].fPts[0].fX, 7 * 2); | 
|---|
| 1072 | } | 
|---|
| 1073 |  | 
|---|
| 1074 | void SkConic::chopAt(SkScalar t1, SkScalar t2, SkConic* dst) const { | 
|---|
| 1075 | if (0 == t1 || 1 == t2) { | 
|---|
| 1076 | if (0 == t1 && 1 == t2) { | 
|---|
| 1077 | *dst = *this; | 
|---|
| 1078 | return; | 
|---|
| 1079 | } else { | 
|---|
| 1080 | SkConic pair[2]; | 
|---|
| 1081 | if (this->chopAt(t1 ? t1 : t2, pair)) { | 
|---|
| 1082 | *dst = pair[SkToBool(t1)]; | 
|---|
| 1083 | return; | 
|---|
| 1084 | } | 
|---|
| 1085 | } | 
|---|
| 1086 | } | 
|---|
| 1087 | SkConicCoeff coeff(*this); | 
|---|
| 1088 | Sk2s tt1(t1); | 
|---|
| 1089 | Sk2s aXY = coeff.fNumer.eval(tt1); | 
|---|
| 1090 | Sk2s aZZ = coeff.fDenom.eval(tt1); | 
|---|
| 1091 | Sk2s midTT((t1 + t2) / 2); | 
|---|
| 1092 | Sk2s dXY = coeff.fNumer.eval(midTT); | 
|---|
| 1093 | Sk2s dZZ = coeff.fDenom.eval(midTT); | 
|---|
| 1094 | Sk2s tt2(t2); | 
|---|
| 1095 | Sk2s cXY = coeff.fNumer.eval(tt2); | 
|---|
| 1096 | Sk2s cZZ = coeff.fDenom.eval(tt2); | 
|---|
| 1097 | Sk2s bXY = times_2(dXY) - (aXY + cXY) * Sk2s(0.5f); | 
|---|
| 1098 | Sk2s bZZ = times_2(dZZ) - (aZZ + cZZ) * Sk2s(0.5f); | 
|---|
| 1099 | dst->fPts[0] = to_point(aXY / aZZ); | 
|---|
| 1100 | dst->fPts[1] = to_point(bXY / bZZ); | 
|---|
| 1101 | dst->fPts[2] = to_point(cXY / cZZ); | 
|---|
| 1102 | Sk2s ww = bZZ / (aZZ * cZZ).sqrt(); | 
|---|
| 1103 | dst->fW = ww[0]; | 
|---|
| 1104 | } | 
|---|
| 1105 |  | 
|---|
| 1106 | SkPoint SkConic::evalAt(SkScalar t) const { | 
|---|
| 1107 | return to_point(SkConicCoeff(*this).eval(t)); | 
|---|
| 1108 | } | 
|---|
| 1109 |  | 
|---|
| 1110 | SkVector SkConic::evalTangentAt(SkScalar t) const { | 
|---|
| 1111 | // The derivative equation returns a zero tangent vector when t is 0 or 1, | 
|---|
| 1112 | // and the control point is equal to the end point. | 
|---|
| 1113 | // In this case, use the conic endpoints to compute the tangent. | 
|---|
| 1114 | if ((t == 0 && fPts[0] == fPts[1]) || (t == 1 && fPts[1] == fPts[2])) { | 
|---|
| 1115 | return fPts[2] - fPts[0]; | 
|---|
| 1116 | } | 
|---|
| 1117 | Sk2s p0 = from_point(fPts[0]); | 
|---|
| 1118 | Sk2s p1 = from_point(fPts[1]); | 
|---|
| 1119 | Sk2s p2 = from_point(fPts[2]); | 
|---|
| 1120 | Sk2s ww(fW); | 
|---|
| 1121 |  | 
|---|
| 1122 | Sk2s p20 = p2 - p0; | 
|---|
| 1123 | Sk2s p10 = p1 - p0; | 
|---|
| 1124 |  | 
|---|
| 1125 | Sk2s C = ww * p10; | 
|---|
| 1126 | Sk2s A = ww * p20 - p20; | 
|---|
| 1127 | Sk2s B = p20 - C - C; | 
|---|
| 1128 |  | 
|---|
| 1129 | return to_vector(SkQuadCoeff(A, B, C).eval(t)); | 
|---|
| 1130 | } | 
|---|
| 1131 |  | 
|---|
| 1132 | void SkConic::evalAt(SkScalar t, SkPoint* pt, SkVector* tangent) const { | 
|---|
| 1133 | SkASSERT(t >= 0 && t <= SK_Scalar1); | 
|---|
| 1134 |  | 
|---|
| 1135 | if (pt) { | 
|---|
| 1136 | *pt = this->evalAt(t); | 
|---|
| 1137 | } | 
|---|
| 1138 | if (tangent) { | 
|---|
| 1139 | *tangent = this->evalTangentAt(t); | 
|---|
| 1140 | } | 
|---|
| 1141 | } | 
|---|
| 1142 |  | 
|---|
| 1143 | static SkScalar subdivide_w_value(SkScalar w) { | 
|---|
| 1144 | return SkScalarSqrt(SK_ScalarHalf + w * SK_ScalarHalf); | 
|---|
| 1145 | } | 
|---|
| 1146 |  | 
|---|
| 1147 | void SkConic::chop(SkConic * SK_RESTRICT dst) const { | 
|---|
| 1148 | Sk2s scale = Sk2s(SkScalarInvert(SK_Scalar1 + fW)); | 
|---|
| 1149 | SkScalar newW = subdivide_w_value(fW); | 
|---|
| 1150 |  | 
|---|
| 1151 | Sk2s p0 = from_point(fPts[0]); | 
|---|
| 1152 | Sk2s p1 = from_point(fPts[1]); | 
|---|
| 1153 | Sk2s p2 = from_point(fPts[2]); | 
|---|
| 1154 | Sk2s ww(fW); | 
|---|
| 1155 |  | 
|---|
| 1156 | Sk2s wp1 = ww * p1; | 
|---|
| 1157 | Sk2s m = (p0 + times_2(wp1) + p2) * scale * Sk2s(0.5f); | 
|---|
| 1158 | SkPoint mPt = to_point(m); | 
|---|
| 1159 | if (!mPt.isFinite()) { | 
|---|
| 1160 | double w_d = fW; | 
|---|
| 1161 | double w_2 = w_d * 2; | 
|---|
| 1162 | double scale_half = 1 / (1 + w_d) * 0.5; | 
|---|
| 1163 | mPt.fX = SkDoubleToScalar((fPts[0].fX + w_2 * fPts[1].fX + fPts[2].fX) * scale_half); | 
|---|
| 1164 | mPt.fY = SkDoubleToScalar((fPts[0].fY + w_2 * fPts[1].fY + fPts[2].fY) * scale_half); | 
|---|
| 1165 | } | 
|---|
| 1166 | dst[0].fPts[0] = fPts[0]; | 
|---|
| 1167 | dst[0].fPts[1] = to_point((p0 + wp1) * scale); | 
|---|
| 1168 | dst[0].fPts[2] = dst[1].fPts[0] = mPt; | 
|---|
| 1169 | dst[1].fPts[1] = to_point((wp1 + p2) * scale); | 
|---|
| 1170 | dst[1].fPts[2] = fPts[2]; | 
|---|
| 1171 |  | 
|---|
| 1172 | dst[0].fW = dst[1].fW = newW; | 
|---|
| 1173 | } | 
|---|
| 1174 |  | 
|---|
| 1175 | /* | 
|---|
| 1176 | *  "High order approximation of conic sections by quadratic splines" | 
|---|
| 1177 | *      by Michael Floater, 1993 | 
|---|
| 1178 | */ | 
|---|
| 1179 | #define AS_QUAD_ERROR_SETUP                                         \ | 
|---|
| 1180 | SkScalar a = fW - 1;                                            \ | 
|---|
| 1181 | SkScalar k = a / (4 * (2 + a));                                 \ | 
|---|
| 1182 | SkScalar x = k * (fPts[0].fX - 2 * fPts[1].fX + fPts[2].fX);    \ | 
|---|
| 1183 | SkScalar y = k * (fPts[0].fY - 2 * fPts[1].fY + fPts[2].fY); | 
|---|
| 1184 |  | 
|---|
| 1185 | void SkConic::computeAsQuadError(SkVector* err) const { | 
|---|
| 1186 | AS_QUAD_ERROR_SETUP | 
|---|
| 1187 | err->set(x, y); | 
|---|
| 1188 | } | 
|---|
| 1189 |  | 
|---|
| 1190 | bool SkConic::asQuadTol(SkScalar tol) const { | 
|---|
| 1191 | AS_QUAD_ERROR_SETUP | 
|---|
| 1192 | return (x * x + y * y) <= tol * tol; | 
|---|
| 1193 | } | 
|---|
| 1194 |  | 
|---|
| 1195 | // Limit the number of suggested quads to approximate a conic | 
|---|
| 1196 | #define kMaxConicToQuadPOW2     5 | 
|---|
| 1197 |  | 
|---|
| 1198 | int SkConic::computeQuadPOW2(SkScalar tol) const { | 
|---|
| 1199 | if (tol < 0 || !SkScalarIsFinite(tol) || !SkPointPriv::AreFinite(fPts, 3)) { | 
|---|
| 1200 | return 0; | 
|---|
| 1201 | } | 
|---|
| 1202 |  | 
|---|
| 1203 | AS_QUAD_ERROR_SETUP | 
|---|
| 1204 |  | 
|---|
| 1205 | SkScalar error = SkScalarSqrt(x * x + y * y); | 
|---|
| 1206 | int pow2; | 
|---|
| 1207 | for (pow2 = 0; pow2 < kMaxConicToQuadPOW2; ++pow2) { | 
|---|
| 1208 | if (error <= tol) { | 
|---|
| 1209 | break; | 
|---|
| 1210 | } | 
|---|
| 1211 | error *= 0.25f; | 
|---|
| 1212 | } | 
|---|
| 1213 | // float version -- using ceil gives the same results as the above. | 
|---|
| 1214 | if (false) { | 
|---|
| 1215 | SkScalar err = SkScalarSqrt(x * x + y * y); | 
|---|
| 1216 | if (err <= tol) { | 
|---|
| 1217 | return 0; | 
|---|
| 1218 | } | 
|---|
| 1219 | SkScalar tol2 = tol * tol; | 
|---|
| 1220 | if (tol2 == 0) { | 
|---|
| 1221 | return kMaxConicToQuadPOW2; | 
|---|
| 1222 | } | 
|---|
| 1223 | SkScalar fpow2 = SkScalarLog2((x * x + y * y) / tol2) * 0.25f; | 
|---|
| 1224 | int altPow2 = SkScalarCeilToInt(fpow2); | 
|---|
| 1225 | if (altPow2 != pow2) { | 
|---|
| 1226 | SkDebugf( "pow2 %d altPow2 %d fbits %g err %g tol %g\n", pow2, altPow2, fpow2, err, tol); | 
|---|
| 1227 | } | 
|---|
| 1228 | pow2 = altPow2; | 
|---|
| 1229 | } | 
|---|
| 1230 | return pow2; | 
|---|
| 1231 | } | 
|---|
| 1232 |  | 
|---|
| 1233 | // This was originally developed and tested for pathops: see SkOpTypes.h | 
|---|
| 1234 | // returns true if (a <= b <= c) || (a >= b >= c) | 
|---|
| 1235 | static bool between(SkScalar a, SkScalar b, SkScalar c) { | 
|---|
| 1236 | return (a - b) * (c - b) <= 0; | 
|---|
| 1237 | } | 
|---|
| 1238 |  | 
|---|
| 1239 | static SkPoint* subdivide(const SkConic& src, SkPoint pts[], int level) { | 
|---|
| 1240 | SkASSERT(level >= 0); | 
|---|
| 1241 |  | 
|---|
| 1242 | if (0 == level) { | 
|---|
| 1243 | memcpy(pts, &src.fPts[1], 2 * sizeof(SkPoint)); | 
|---|
| 1244 | return pts + 2; | 
|---|
| 1245 | } else { | 
|---|
| 1246 | SkConic dst[2]; | 
|---|
| 1247 | src.chop(dst); | 
|---|
| 1248 | const SkScalar startY = src.fPts[0].fY; | 
|---|
| 1249 | SkScalar endY = src.fPts[2].fY; | 
|---|
| 1250 | if (between(startY, src.fPts[1].fY, endY)) { | 
|---|
| 1251 | // If the input is monotonic and the output is not, the scan converter hangs. | 
|---|
| 1252 | // Ensure that the chopped conics maintain their y-order. | 
|---|
| 1253 | SkScalar midY = dst[0].fPts[2].fY; | 
|---|
| 1254 | if (!between(startY, midY, endY)) { | 
|---|
| 1255 | // If the computed midpoint is outside the ends, move it to the closer one. | 
|---|
| 1256 | SkScalar closerY = SkTAbs(midY - startY) < SkTAbs(midY - endY) ? startY : endY; | 
|---|
| 1257 | dst[0].fPts[2].fY = dst[1].fPts[0].fY = closerY; | 
|---|
| 1258 | } | 
|---|
| 1259 | if (!between(startY, dst[0].fPts[1].fY, dst[0].fPts[2].fY)) { | 
|---|
| 1260 | // If the 1st control is not between the start and end, put it at the start. | 
|---|
| 1261 | // This also reduces the quad to a line. | 
|---|
| 1262 | dst[0].fPts[1].fY = startY; | 
|---|
| 1263 | } | 
|---|
| 1264 | if (!between(dst[1].fPts[0].fY, dst[1].fPts[1].fY, endY)) { | 
|---|
| 1265 | // If the 2nd control is not between the start and end, put it at the end. | 
|---|
| 1266 | // This also reduces the quad to a line. | 
|---|
| 1267 | dst[1].fPts[1].fY = endY; | 
|---|
| 1268 | } | 
|---|
| 1269 | // Verify that all five points are in order. | 
|---|
| 1270 | SkASSERT(between(startY, dst[0].fPts[1].fY, dst[0].fPts[2].fY)); | 
|---|
| 1271 | SkASSERT(between(dst[0].fPts[1].fY, dst[0].fPts[2].fY, dst[1].fPts[1].fY)); | 
|---|
| 1272 | SkASSERT(between(dst[0].fPts[2].fY, dst[1].fPts[1].fY, endY)); | 
|---|
| 1273 | } | 
|---|
| 1274 | --level; | 
|---|
| 1275 | pts = subdivide(dst[0], pts, level); | 
|---|
| 1276 | return subdivide(dst[1], pts, level); | 
|---|
| 1277 | } | 
|---|
| 1278 | } | 
|---|
| 1279 |  | 
|---|
| 1280 | int SkConic::chopIntoQuadsPOW2(SkPoint pts[], int pow2) const { | 
|---|
| 1281 | SkASSERT(pow2 >= 0); | 
|---|
| 1282 | *pts = fPts[0]; | 
|---|
| 1283 | SkDEBUGCODE(SkPoint* endPts); | 
|---|
| 1284 | if (pow2 == kMaxConicToQuadPOW2) {  // If an extreme weight generates many quads ... | 
|---|
| 1285 | SkConic dst[2]; | 
|---|
| 1286 | this->chop(dst); | 
|---|
| 1287 | // check to see if the first chop generates a pair of lines | 
|---|
| 1288 | if (SkPointPriv::EqualsWithinTolerance(dst[0].fPts[1], dst[0].fPts[2]) && | 
|---|
| 1289 | SkPointPriv::EqualsWithinTolerance(dst[1].fPts[0], dst[1].fPts[1])) { | 
|---|
| 1290 | pts[1] = pts[2] = pts[3] = dst[0].fPts[1];  // set ctrl == end to make lines | 
|---|
| 1291 | pts[4] = dst[1].fPts[2]; | 
|---|
| 1292 | pow2 = 1; | 
|---|
| 1293 | SkDEBUGCODE(endPts = &pts[5]); | 
|---|
| 1294 | goto commonFinitePtCheck; | 
|---|
| 1295 | } | 
|---|
| 1296 | } | 
|---|
| 1297 | SkDEBUGCODE(endPts = ) subdivide(*this, pts + 1, pow2); | 
|---|
| 1298 | commonFinitePtCheck: | 
|---|
| 1299 | const int quadCount = 1 << pow2; | 
|---|
| 1300 | const int ptCount = 2 * quadCount + 1; | 
|---|
| 1301 | SkASSERT(endPts - pts == ptCount); | 
|---|
| 1302 | if (!SkPointPriv::AreFinite(pts, ptCount)) { | 
|---|
| 1303 | // if we generated a non-finite, pin ourselves to the middle of the hull, | 
|---|
| 1304 | // as our first and last are already on the first/last pts of the hull. | 
|---|
| 1305 | for (int i = 1; i < ptCount - 1; ++i) { | 
|---|
| 1306 | pts[i] = fPts[1]; | 
|---|
| 1307 | } | 
|---|
| 1308 | } | 
|---|
| 1309 | return 1 << pow2; | 
|---|
| 1310 | } | 
|---|
| 1311 |  | 
|---|
| 1312 | bool SkConic::findXExtrema(SkScalar* t) const { | 
|---|
| 1313 | return conic_find_extrema(&fPts[0].fX, fW, t); | 
|---|
| 1314 | } | 
|---|
| 1315 |  | 
|---|
| 1316 | bool SkConic::findYExtrema(SkScalar* t) const { | 
|---|
| 1317 | return conic_find_extrema(&fPts[0].fY, fW, t); | 
|---|
| 1318 | } | 
|---|
| 1319 |  | 
|---|
| 1320 | bool SkConic::chopAtXExtrema(SkConic dst[2]) const { | 
|---|
| 1321 | SkScalar t; | 
|---|
| 1322 | if (this->findXExtrema(&t)) { | 
|---|
| 1323 | if (!this->chopAt(t, dst)) { | 
|---|
| 1324 | // if chop can't return finite values, don't chop | 
|---|
| 1325 | return false; | 
|---|
| 1326 | } | 
|---|
| 1327 | // now clean-up the middle, since we know t was meant to be at | 
|---|
| 1328 | // an X-extrema | 
|---|
| 1329 | SkScalar value = dst[0].fPts[2].fX; | 
|---|
| 1330 | dst[0].fPts[1].fX = value; | 
|---|
| 1331 | dst[1].fPts[0].fX = value; | 
|---|
| 1332 | dst[1].fPts[1].fX = value; | 
|---|
| 1333 | return true; | 
|---|
| 1334 | } | 
|---|
| 1335 | return false; | 
|---|
| 1336 | } | 
|---|
| 1337 |  | 
|---|
| 1338 | bool SkConic::chopAtYExtrema(SkConic dst[2]) const { | 
|---|
| 1339 | SkScalar t; | 
|---|
| 1340 | if (this->findYExtrema(&t)) { | 
|---|
| 1341 | if (!this->chopAt(t, dst)) { | 
|---|
| 1342 | // if chop can't return finite values, don't chop | 
|---|
| 1343 | return false; | 
|---|
| 1344 | } | 
|---|
| 1345 | // now clean-up the middle, since we know t was meant to be at | 
|---|
| 1346 | // an Y-extrema | 
|---|
| 1347 | SkScalar value = dst[0].fPts[2].fY; | 
|---|
| 1348 | dst[0].fPts[1].fY = value; | 
|---|
| 1349 | dst[1].fPts[0].fY = value; | 
|---|
| 1350 | dst[1].fPts[1].fY = value; | 
|---|
| 1351 | return true; | 
|---|
| 1352 | } | 
|---|
| 1353 | return false; | 
|---|
| 1354 | } | 
|---|
| 1355 |  | 
|---|
| 1356 | void SkConic::computeTightBounds(SkRect* bounds) const { | 
|---|
| 1357 | SkPoint pts[4]; | 
|---|
| 1358 | pts[0] = fPts[0]; | 
|---|
| 1359 | pts[1] = fPts[2]; | 
|---|
| 1360 | int count = 2; | 
|---|
| 1361 |  | 
|---|
| 1362 | SkScalar t; | 
|---|
| 1363 | if (this->findXExtrema(&t)) { | 
|---|
| 1364 | this->evalAt(t, &pts[count++]); | 
|---|
| 1365 | } | 
|---|
| 1366 | if (this->findYExtrema(&t)) { | 
|---|
| 1367 | this->evalAt(t, &pts[count++]); | 
|---|
| 1368 | } | 
|---|
| 1369 | bounds->setBounds(pts, count); | 
|---|
| 1370 | } | 
|---|
| 1371 |  | 
|---|
| 1372 | void SkConic::computeFastBounds(SkRect* bounds) const { | 
|---|
| 1373 | bounds->setBounds(fPts, 3); | 
|---|
| 1374 | } | 
|---|
| 1375 |  | 
|---|
| 1376 | #if 0  // unimplemented | 
|---|
| 1377 | bool SkConic::findMaxCurvature(SkScalar* t) const { | 
|---|
| 1378 | // TODO: Implement me | 
|---|
| 1379 | return false; | 
|---|
| 1380 | } | 
|---|
| 1381 | #endif | 
|---|
| 1382 |  | 
|---|
| 1383 | SkScalar SkConic::TransformW(const SkPoint pts[], SkScalar w, const SkMatrix& matrix) { | 
|---|
| 1384 | if (!matrix.hasPerspective()) { | 
|---|
| 1385 | return w; | 
|---|
| 1386 | } | 
|---|
| 1387 |  | 
|---|
| 1388 | SkPoint3 src[3], dst[3]; | 
|---|
| 1389 |  | 
|---|
| 1390 | ratquad_mapTo3D(pts, w, src); | 
|---|
| 1391 |  | 
|---|
| 1392 | matrix.mapHomogeneousPoints(dst, src, 3); | 
|---|
| 1393 |  | 
|---|
| 1394 | // w' = sqrt(w1*w1/w0*w2) | 
|---|
| 1395 | // use doubles temporarily, to handle small numer/denom | 
|---|
| 1396 | double w0 = dst[0].fZ; | 
|---|
| 1397 | double w1 = dst[1].fZ; | 
|---|
| 1398 | double w2 = dst[2].fZ; | 
|---|
| 1399 | return sk_double_to_float(sqrt(sk_ieee_double_divide(w1 * w1, w0 * w2))); | 
|---|
| 1400 | } | 
|---|
| 1401 |  | 
|---|
| 1402 | int SkConic::BuildUnitArc(const SkVector& uStart, const SkVector& uStop, SkRotationDirection dir, | 
|---|
| 1403 | const SkMatrix* userMatrix, SkConic dst[kMaxConicsForArc]) { | 
|---|
| 1404 | // rotate by x,y so that uStart is (1.0) | 
|---|
| 1405 | SkScalar x = SkPoint::DotProduct(uStart, uStop); | 
|---|
| 1406 | SkScalar y = SkPoint::CrossProduct(uStart, uStop); | 
|---|
| 1407 |  | 
|---|
| 1408 | SkScalar absY = SkScalarAbs(y); | 
|---|
| 1409 |  | 
|---|
| 1410 | // check for (effectively) coincident vectors | 
|---|
| 1411 | // this can happen if our angle is nearly 0 or nearly 180 (y == 0) | 
|---|
| 1412 | // ... we use the dot-prod to distinguish between 0 and 180 (x > 0) | 
|---|
| 1413 | if (absY <= SK_ScalarNearlyZero && x > 0 && ((y >= 0 && kCW_SkRotationDirection == dir) || | 
|---|
| 1414 | (y <= 0 && kCCW_SkRotationDirection == dir))) { | 
|---|
| 1415 | return 0; | 
|---|
| 1416 | } | 
|---|
| 1417 |  | 
|---|
| 1418 | if (dir == kCCW_SkRotationDirection) { | 
|---|
| 1419 | y = -y; | 
|---|
| 1420 | } | 
|---|
| 1421 |  | 
|---|
| 1422 | // We decide to use 1-conic per quadrant of a circle. What quadrant does [xy] lie in? | 
|---|
| 1423 | //      0 == [0  .. 90) | 
|---|
| 1424 | //      1 == [90 ..180) | 
|---|
| 1425 | //      2 == [180..270) | 
|---|
| 1426 | //      3 == [270..360) | 
|---|
| 1427 | // | 
|---|
| 1428 | int quadrant = 0; | 
|---|
| 1429 | if (0 == y) { | 
|---|
| 1430 | quadrant = 2;        // 180 | 
|---|
| 1431 | SkASSERT(SkScalarAbs(x + SK_Scalar1) <= SK_ScalarNearlyZero); | 
|---|
| 1432 | } else if (0 == x) { | 
|---|
| 1433 | SkASSERT(absY - SK_Scalar1 <= SK_ScalarNearlyZero); | 
|---|
| 1434 | quadrant = y > 0 ? 1 : 3; // 90 : 270 | 
|---|
| 1435 | } else { | 
|---|
| 1436 | if (y < 0) { | 
|---|
| 1437 | quadrant += 2; | 
|---|
| 1438 | } | 
|---|
| 1439 | if ((x < 0) != (y < 0)) { | 
|---|
| 1440 | quadrant += 1; | 
|---|
| 1441 | } | 
|---|
| 1442 | } | 
|---|
| 1443 |  | 
|---|
| 1444 | const SkPoint quadrantPts[] = { | 
|---|
| 1445 | { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 } | 
|---|
| 1446 | }; | 
|---|
| 1447 | const SkScalar quadrantWeight = SK_ScalarRoot2Over2; | 
|---|
| 1448 |  | 
|---|
| 1449 | int conicCount = quadrant; | 
|---|
| 1450 | for (int i = 0; i < conicCount; ++i) { | 
|---|
| 1451 | dst[i].set(&quadrantPts[i * 2], quadrantWeight); | 
|---|
| 1452 | } | 
|---|
| 1453 |  | 
|---|
| 1454 | // Now compute any remaing (sub-90-degree) arc for the last conic | 
|---|
| 1455 | const SkPoint finalP = { x, y }; | 
|---|
| 1456 | const SkPoint& lastQ = quadrantPts[quadrant * 2];  // will already be a unit-vector | 
|---|
| 1457 | const SkScalar dot = SkVector::DotProduct(lastQ, finalP); | 
|---|
| 1458 | SkASSERT(0 <= dot && dot <= SK_Scalar1 + SK_ScalarNearlyZero); | 
|---|
| 1459 |  | 
|---|
| 1460 | if (dot < 1) { | 
|---|
| 1461 | SkVector offCurve = { lastQ.x() + x, lastQ.y() + y }; | 
|---|
| 1462 | // compute the bisector vector, and then rescale to be the off-curve point. | 
|---|
| 1463 | // we compute its length from cos(theta/2) = length / 1, using half-angle identity we get | 
|---|
| 1464 | // length = sqrt(2 / (1 + cos(theta)). We already have cos() when to computed the dot. | 
|---|
| 1465 | // This is nice, since our computed weight is cos(theta/2) as well! | 
|---|
| 1466 | // | 
|---|
| 1467 | const SkScalar cosThetaOver2 = SkScalarSqrt((1 + dot) / 2); | 
|---|
| 1468 | offCurve.setLength(SkScalarInvert(cosThetaOver2)); | 
|---|
| 1469 | if (!SkPointPriv::EqualsWithinTolerance(lastQ, offCurve)) { | 
|---|
| 1470 | dst[conicCount].set(lastQ, offCurve, finalP, cosThetaOver2); | 
|---|
| 1471 | conicCount += 1; | 
|---|
| 1472 | } | 
|---|
| 1473 | } | 
|---|
| 1474 |  | 
|---|
| 1475 | // now handle counter-clockwise and the initial unitStart rotation | 
|---|
| 1476 | SkMatrix    matrix; | 
|---|
| 1477 | matrix.setSinCos(uStart.fY, uStart.fX); | 
|---|
| 1478 | if (dir == kCCW_SkRotationDirection) { | 
|---|
| 1479 | matrix.preScale(SK_Scalar1, -SK_Scalar1); | 
|---|
| 1480 | } | 
|---|
| 1481 | if (userMatrix) { | 
|---|
| 1482 | matrix.postConcat(*userMatrix); | 
|---|
| 1483 | } | 
|---|
| 1484 | for (int i = 0; i < conicCount; ++i) { | 
|---|
| 1485 | matrix.mapPoints(dst[i].fPts, 3); | 
|---|
| 1486 | } | 
|---|
| 1487 | return conicCount; | 
|---|
| 1488 | } | 
|---|
| 1489 |  | 
|---|