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 2012 Google Inc. |
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 | #include "SkLineParameters.h" |
michael@0 | 8 | #include "SkPathOpsCubic.h" |
michael@0 | 9 | #include "SkPathOpsLine.h" |
michael@0 | 10 | #include "SkPathOpsQuad.h" |
michael@0 | 11 | #include "SkPathOpsRect.h" |
michael@0 | 12 | |
michael@0 | 13 | const int SkDCubic::gPrecisionUnit = 256; // FIXME: test different values in test framework |
michael@0 | 14 | |
michael@0 | 15 | // FIXME: cache keep the bounds and/or precision with the caller? |
michael@0 | 16 | double SkDCubic::calcPrecision() const { |
michael@0 | 17 | SkDRect dRect; |
michael@0 | 18 | dRect.setBounds(*this); // OPTIMIZATION: just use setRawBounds ? |
michael@0 | 19 | double width = dRect.fRight - dRect.fLeft; |
michael@0 | 20 | double height = dRect.fBottom - dRect.fTop; |
michael@0 | 21 | return (width > height ? width : height) / gPrecisionUnit; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | bool SkDCubic::clockwise() const { |
michael@0 | 25 | double sum = (fPts[0].fX - fPts[3].fX) * (fPts[0].fY + fPts[3].fY); |
michael@0 | 26 | for (int idx = 0; idx < 3; ++idx) { |
michael@0 | 27 | sum += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY); |
michael@0 | 28 | } |
michael@0 | 29 | return sum <= 0; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | void SkDCubic::Coefficients(const double* src, double* A, double* B, double* C, double* D) { |
michael@0 | 33 | *A = src[6]; // d |
michael@0 | 34 | *B = src[4] * 3; // 3*c |
michael@0 | 35 | *C = src[2] * 3; // 3*b |
michael@0 | 36 | *D = src[0]; // a |
michael@0 | 37 | *A -= *D - *C + *B; // A = -a + 3*b - 3*c + d |
michael@0 | 38 | *B += 3 * *D - 2 * *C; // B = 3*a - 6*b + 3*c |
michael@0 | 39 | *C -= 3 * *D; // C = -3*a + 3*b |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | bool SkDCubic::controlsContainedByEnds() const { |
michael@0 | 43 | SkDVector startTan = fPts[1] - fPts[0]; |
michael@0 | 44 | if (startTan.fX == 0 && startTan.fY == 0) { |
michael@0 | 45 | startTan = fPts[2] - fPts[0]; |
michael@0 | 46 | } |
michael@0 | 47 | SkDVector endTan = fPts[2] - fPts[3]; |
michael@0 | 48 | if (endTan.fX == 0 && endTan.fY == 0) { |
michael@0 | 49 | endTan = fPts[1] - fPts[3]; |
michael@0 | 50 | } |
michael@0 | 51 | if (startTan.dot(endTan) >= 0) { |
michael@0 | 52 | return false; |
michael@0 | 53 | } |
michael@0 | 54 | SkDLine startEdge = {{fPts[0], fPts[0]}}; |
michael@0 | 55 | startEdge[1].fX -= startTan.fY; |
michael@0 | 56 | startEdge[1].fY += startTan.fX; |
michael@0 | 57 | SkDLine endEdge = {{fPts[3], fPts[3]}}; |
michael@0 | 58 | endEdge[1].fX -= endTan.fY; |
michael@0 | 59 | endEdge[1].fY += endTan.fX; |
michael@0 | 60 | double leftStart1 = startEdge.isLeft(fPts[1]); |
michael@0 | 61 | if (leftStart1 * startEdge.isLeft(fPts[2]) < 0) { |
michael@0 | 62 | return false; |
michael@0 | 63 | } |
michael@0 | 64 | double leftEnd1 = endEdge.isLeft(fPts[1]); |
michael@0 | 65 | if (leftEnd1 * endEdge.isLeft(fPts[2]) < 0) { |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | return leftStart1 * leftEnd1 >= 0; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | bool SkDCubic::endsAreExtremaInXOrY() const { |
michael@0 | 72 | return (between(fPts[0].fX, fPts[1].fX, fPts[3].fX) |
michael@0 | 73 | && between(fPts[0].fX, fPts[2].fX, fPts[3].fX)) |
michael@0 | 74 | || (between(fPts[0].fY, fPts[1].fY, fPts[3].fY) |
michael@0 | 75 | && between(fPts[0].fY, fPts[2].fY, fPts[3].fY)); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | bool SkDCubic::isLinear(int startIndex, int endIndex) const { |
michael@0 | 79 | SkLineParameters lineParameters; |
michael@0 | 80 | lineParameters.cubicEndPoints(*this, startIndex, endIndex); |
michael@0 | 81 | // FIXME: maybe it's possible to avoid this and compare non-normalized |
michael@0 | 82 | lineParameters.normalize(); |
michael@0 | 83 | double distance = lineParameters.controlPtDistance(*this, 1); |
michael@0 | 84 | if (!approximately_zero(distance)) { |
michael@0 | 85 | return false; |
michael@0 | 86 | } |
michael@0 | 87 | distance = lineParameters.controlPtDistance(*this, 2); |
michael@0 | 88 | return approximately_zero(distance); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | bool SkDCubic::monotonicInY() const { |
michael@0 | 92 | return between(fPts[0].fY, fPts[1].fY, fPts[3].fY) |
michael@0 | 93 | && between(fPts[0].fY, fPts[2].fY, fPts[3].fY); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | bool SkDCubic::serpentine() const { |
michael@0 | 97 | if (!controlsContainedByEnds()) { |
michael@0 | 98 | return false; |
michael@0 | 99 | } |
michael@0 | 100 | double wiggle = (fPts[0].fX - fPts[2].fX) * (fPts[0].fY + fPts[2].fY); |
michael@0 | 101 | for (int idx = 0; idx < 2; ++idx) { |
michael@0 | 102 | wiggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY); |
michael@0 | 103 | } |
michael@0 | 104 | double waggle = (fPts[1].fX - fPts[3].fX) * (fPts[1].fY + fPts[3].fY); |
michael@0 | 105 | for (int idx = 1; idx < 3; ++idx) { |
michael@0 | 106 | waggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY); |
michael@0 | 107 | } |
michael@0 | 108 | return wiggle * waggle < 0; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // cubic roots |
michael@0 | 112 | |
michael@0 | 113 | static const double PI = 3.141592653589793; |
michael@0 | 114 | |
michael@0 | 115 | // from SkGeometry.cpp (and Numeric Solutions, 5.6) |
michael@0 | 116 | int SkDCubic::RootsValidT(double A, double B, double C, double D, double t[3]) { |
michael@0 | 117 | double s[3]; |
michael@0 | 118 | int realRoots = RootsReal(A, B, C, D, s); |
michael@0 | 119 | int foundRoots = SkDQuad::AddValidTs(s, realRoots, t); |
michael@0 | 120 | return foundRoots; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | int SkDCubic::RootsReal(double A, double B, double C, double D, double s[3]) { |
michael@0 | 124 | #ifdef SK_DEBUG |
michael@0 | 125 | // create a string mathematica understands |
michael@0 | 126 | // GDB set print repe 15 # if repeated digits is a bother |
michael@0 | 127 | // set print elements 400 # if line doesn't fit |
michael@0 | 128 | char str[1024]; |
michael@0 | 129 | sk_bzero(str, sizeof(str)); |
michael@0 | 130 | SK_SNPRINTF(str, sizeof(str), "Solve[%1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]", |
michael@0 | 131 | A, B, C, D); |
michael@0 | 132 | SkPathOpsDebug::MathematicaIze(str, sizeof(str)); |
michael@0 | 133 | #if ONE_OFF_DEBUG && ONE_OFF_DEBUG_MATHEMATICA |
michael@0 | 134 | SkDebugf("%s\n", str); |
michael@0 | 135 | #endif |
michael@0 | 136 | #endif |
michael@0 | 137 | if (approximately_zero(A) |
michael@0 | 138 | && approximately_zero_when_compared_to(A, B) |
michael@0 | 139 | && approximately_zero_when_compared_to(A, C) |
michael@0 | 140 | && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic |
michael@0 | 141 | return SkDQuad::RootsReal(B, C, D, s); |
michael@0 | 142 | } |
michael@0 | 143 | if (approximately_zero_when_compared_to(D, A) |
michael@0 | 144 | && approximately_zero_when_compared_to(D, B) |
michael@0 | 145 | && approximately_zero_when_compared_to(D, C)) { // 0 is one root |
michael@0 | 146 | int num = SkDQuad::RootsReal(A, B, C, s); |
michael@0 | 147 | for (int i = 0; i < num; ++i) { |
michael@0 | 148 | if (approximately_zero(s[i])) { |
michael@0 | 149 | return num; |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | s[num++] = 0; |
michael@0 | 153 | return num; |
michael@0 | 154 | } |
michael@0 | 155 | if (approximately_zero(A + B + C + D)) { // 1 is one root |
michael@0 | 156 | int num = SkDQuad::RootsReal(A, A + B, -D, s); |
michael@0 | 157 | for (int i = 0; i < num; ++i) { |
michael@0 | 158 | if (AlmostDequalUlps(s[i], 1)) { |
michael@0 | 159 | return num; |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | s[num++] = 1; |
michael@0 | 163 | return num; |
michael@0 | 164 | } |
michael@0 | 165 | double a, b, c; |
michael@0 | 166 | { |
michael@0 | 167 | double invA = 1 / A; |
michael@0 | 168 | a = B * invA; |
michael@0 | 169 | b = C * invA; |
michael@0 | 170 | c = D * invA; |
michael@0 | 171 | } |
michael@0 | 172 | double a2 = a * a; |
michael@0 | 173 | double Q = (a2 - b * 3) / 9; |
michael@0 | 174 | double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54; |
michael@0 | 175 | double R2 = R * R; |
michael@0 | 176 | double Q3 = Q * Q * Q; |
michael@0 | 177 | double R2MinusQ3 = R2 - Q3; |
michael@0 | 178 | double adiv3 = a / 3; |
michael@0 | 179 | double r; |
michael@0 | 180 | double* roots = s; |
michael@0 | 181 | if (R2MinusQ3 < 0) { // we have 3 real roots |
michael@0 | 182 | double theta = acos(R / sqrt(Q3)); |
michael@0 | 183 | double neg2RootQ = -2 * sqrt(Q); |
michael@0 | 184 | |
michael@0 | 185 | r = neg2RootQ * cos(theta / 3) - adiv3; |
michael@0 | 186 | *roots++ = r; |
michael@0 | 187 | |
michael@0 | 188 | r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3; |
michael@0 | 189 | if (!AlmostDequalUlps(s[0], r)) { |
michael@0 | 190 | *roots++ = r; |
michael@0 | 191 | } |
michael@0 | 192 | r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3; |
michael@0 | 193 | if (!AlmostDequalUlps(s[0], r) && (roots - s == 1 || !AlmostDequalUlps(s[1], r))) { |
michael@0 | 194 | *roots++ = r; |
michael@0 | 195 | } |
michael@0 | 196 | } else { // we have 1 real root |
michael@0 | 197 | double sqrtR2MinusQ3 = sqrt(R2MinusQ3); |
michael@0 | 198 | double A = fabs(R) + sqrtR2MinusQ3; |
michael@0 | 199 | A = SkDCubeRoot(A); |
michael@0 | 200 | if (R > 0) { |
michael@0 | 201 | A = -A; |
michael@0 | 202 | } |
michael@0 | 203 | if (A != 0) { |
michael@0 | 204 | A += Q / A; |
michael@0 | 205 | } |
michael@0 | 206 | r = A - adiv3; |
michael@0 | 207 | *roots++ = r; |
michael@0 | 208 | if (AlmostDequalUlps(R2, Q3)) { |
michael@0 | 209 | r = -A / 2 - adiv3; |
michael@0 | 210 | if (!AlmostDequalUlps(s[0], r)) { |
michael@0 | 211 | *roots++ = r; |
michael@0 | 212 | } |
michael@0 | 213 | } |
michael@0 | 214 | } |
michael@0 | 215 | return static_cast<int>(roots - s); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | // from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf |
michael@0 | 219 | // c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3 |
michael@0 | 220 | // c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2 |
michael@0 | 221 | // = 3(b-a)(1-t)^2 + 6(c-b)t(1-t) + 3(d-c)t^2 |
michael@0 | 222 | static double derivative_at_t(const double* src, double t) { |
michael@0 | 223 | double one_t = 1 - t; |
michael@0 | 224 | double a = src[0]; |
michael@0 | 225 | double b = src[2]; |
michael@0 | 226 | double c = src[4]; |
michael@0 | 227 | double d = src[6]; |
michael@0 | 228 | return 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | // OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t? |
michael@0 | 232 | SkDVector SkDCubic::dxdyAtT(double t) const { |
michael@0 | 233 | SkDVector result = { derivative_at_t(&fPts[0].fX, t), derivative_at_t(&fPts[0].fY, t) }; |
michael@0 | 234 | return result; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | // OPTIMIZE? share code with formulate_F1DotF2 |
michael@0 | 238 | int SkDCubic::findInflections(double tValues[]) const { |
michael@0 | 239 | double Ax = fPts[1].fX - fPts[0].fX; |
michael@0 | 240 | double Ay = fPts[1].fY - fPts[0].fY; |
michael@0 | 241 | double Bx = fPts[2].fX - 2 * fPts[1].fX + fPts[0].fX; |
michael@0 | 242 | double By = fPts[2].fY - 2 * fPts[1].fY + fPts[0].fY; |
michael@0 | 243 | double Cx = fPts[3].fX + 3 * (fPts[1].fX - fPts[2].fX) - fPts[0].fX; |
michael@0 | 244 | double Cy = fPts[3].fY + 3 * (fPts[1].fY - fPts[2].fY) - fPts[0].fY; |
michael@0 | 245 | return SkDQuad::RootsValidT(Bx * Cy - By * Cx, Ax * Cy - Ay * Cx, Ax * By - Ay * Bx, tValues); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | static void formulate_F1DotF2(const double src[], double coeff[4]) { |
michael@0 | 249 | double a = src[2] - src[0]; |
michael@0 | 250 | double b = src[4] - 2 * src[2] + src[0]; |
michael@0 | 251 | double c = src[6] + 3 * (src[2] - src[4]) - src[0]; |
michael@0 | 252 | coeff[0] = c * c; |
michael@0 | 253 | coeff[1] = 3 * b * c; |
michael@0 | 254 | coeff[2] = 2 * b * b + c * a; |
michael@0 | 255 | coeff[3] = a * b; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | /** SkDCubic'(t) = At^2 + Bt + C, where |
michael@0 | 259 | A = 3(-a + 3(b - c) + d) |
michael@0 | 260 | B = 6(a - 2b + c) |
michael@0 | 261 | C = 3(b - a) |
michael@0 | 262 | Solve for t, keeping only those that fit between 0 < t < 1 |
michael@0 | 263 | */ |
michael@0 | 264 | int SkDCubic::FindExtrema(double a, double b, double c, double d, double tValues[2]) { |
michael@0 | 265 | // we divide A,B,C by 3 to simplify |
michael@0 | 266 | double A = d - a + 3*(b - c); |
michael@0 | 267 | double B = 2*(a - b - b + c); |
michael@0 | 268 | double C = b - a; |
michael@0 | 269 | |
michael@0 | 270 | return SkDQuad::RootsValidT(A, B, C, tValues); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | /* from SkGeometry.cpp |
michael@0 | 274 | Looking for F' dot F'' == 0 |
michael@0 | 275 | |
michael@0 | 276 | A = b - a |
michael@0 | 277 | B = c - 2b + a |
michael@0 | 278 | C = d - 3c + 3b - a |
michael@0 | 279 | |
michael@0 | 280 | F' = 3Ct^2 + 6Bt + 3A |
michael@0 | 281 | F'' = 6Ct + 6B |
michael@0 | 282 | |
michael@0 | 283 | F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB |
michael@0 | 284 | */ |
michael@0 | 285 | int SkDCubic::findMaxCurvature(double tValues[]) const { |
michael@0 | 286 | double coeffX[4], coeffY[4]; |
michael@0 | 287 | int i; |
michael@0 | 288 | formulate_F1DotF2(&fPts[0].fX, coeffX); |
michael@0 | 289 | formulate_F1DotF2(&fPts[0].fY, coeffY); |
michael@0 | 290 | for (i = 0; i < 4; i++) { |
michael@0 | 291 | coeffX[i] = coeffX[i] + coeffY[i]; |
michael@0 | 292 | } |
michael@0 | 293 | return RootsValidT(coeffX[0], coeffX[1], coeffX[2], coeffX[3], tValues); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | SkDPoint SkDCubic::top(double startT, double endT) const { |
michael@0 | 297 | SkDCubic sub = subDivide(startT, endT); |
michael@0 | 298 | SkDPoint topPt = sub[0]; |
michael@0 | 299 | if (topPt.fY > sub[3].fY || (topPt.fY == sub[3].fY && topPt.fX > sub[3].fX)) { |
michael@0 | 300 | topPt = sub[3]; |
michael@0 | 301 | } |
michael@0 | 302 | double extremeTs[2]; |
michael@0 | 303 | if (!sub.monotonicInY()) { |
michael@0 | 304 | int roots = FindExtrema(sub[0].fY, sub[1].fY, sub[2].fY, sub[3].fY, extremeTs); |
michael@0 | 305 | for (int index = 0; index < roots; ++index) { |
michael@0 | 306 | double t = startT + (endT - startT) * extremeTs[index]; |
michael@0 | 307 | SkDPoint mid = ptAtT(t); |
michael@0 | 308 | if (topPt.fY > mid.fY || (topPt.fY == mid.fY && topPt.fX > mid.fX)) { |
michael@0 | 309 | topPt = mid; |
michael@0 | 310 | } |
michael@0 | 311 | } |
michael@0 | 312 | } |
michael@0 | 313 | return topPt; |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | SkDPoint SkDCubic::ptAtT(double t) const { |
michael@0 | 317 | if (0 == t) { |
michael@0 | 318 | return fPts[0]; |
michael@0 | 319 | } |
michael@0 | 320 | if (1 == t) { |
michael@0 | 321 | return fPts[3]; |
michael@0 | 322 | } |
michael@0 | 323 | double one_t = 1 - t; |
michael@0 | 324 | double one_t2 = one_t * one_t; |
michael@0 | 325 | double a = one_t2 * one_t; |
michael@0 | 326 | double b = 3 * one_t2 * t; |
michael@0 | 327 | double t2 = t * t; |
michael@0 | 328 | double c = 3 * one_t * t2; |
michael@0 | 329 | double d = t2 * t; |
michael@0 | 330 | SkDPoint result = {a * fPts[0].fX + b * fPts[1].fX + c * fPts[2].fX + d * fPts[3].fX, |
michael@0 | 331 | a * fPts[0].fY + b * fPts[1].fY + c * fPts[2].fY + d * fPts[3].fY}; |
michael@0 | 332 | return result; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | /* |
michael@0 | 336 | Given a cubic c, t1, and t2, find a small cubic segment. |
michael@0 | 337 | |
michael@0 | 338 | The new cubic is defined as points A, B, C, and D, where |
michael@0 | 339 | s1 = 1 - t1 |
michael@0 | 340 | s2 = 1 - t2 |
michael@0 | 341 | A = c[0]*s1*s1*s1 + 3*c[1]*s1*s1*t1 + 3*c[2]*s1*t1*t1 + c[3]*t1*t1*t1 |
michael@0 | 342 | D = c[0]*s2*s2*s2 + 3*c[1]*s2*s2*t2 + 3*c[2]*s2*t2*t2 + c[3]*t2*t2*t2 |
michael@0 | 343 | |
michael@0 | 344 | We don't have B or C. So We define two equations to isolate them. |
michael@0 | 345 | First, compute two reference T values 1/3 and 2/3 from t1 to t2: |
michael@0 | 346 | |
michael@0 | 347 | c(at (2*t1 + t2)/3) == E |
michael@0 | 348 | c(at (t1 + 2*t2)/3) == F |
michael@0 | 349 | |
michael@0 | 350 | Next, compute where those values must be if we know the values of B and C: |
michael@0 | 351 | |
michael@0 | 352 | _12 = A*2/3 + B*1/3 |
michael@0 | 353 | 12_ = A*1/3 + B*2/3 |
michael@0 | 354 | _23 = B*2/3 + C*1/3 |
michael@0 | 355 | 23_ = B*1/3 + C*2/3 |
michael@0 | 356 | _34 = C*2/3 + D*1/3 |
michael@0 | 357 | 34_ = C*1/3 + D*2/3 |
michael@0 | 358 | _123 = (A*2/3 + B*1/3)*2/3 + (B*2/3 + C*1/3)*1/3 = A*4/9 + B*4/9 + C*1/9 |
michael@0 | 359 | 123_ = (A*1/3 + B*2/3)*1/3 + (B*1/3 + C*2/3)*2/3 = A*1/9 + B*4/9 + C*4/9 |
michael@0 | 360 | _234 = (B*2/3 + C*1/3)*2/3 + (C*2/3 + D*1/3)*1/3 = B*4/9 + C*4/9 + D*1/9 |
michael@0 | 361 | 234_ = (B*1/3 + C*2/3)*1/3 + (C*1/3 + D*2/3)*2/3 = B*1/9 + C*4/9 + D*4/9 |
michael@0 | 362 | _1234 = (A*4/9 + B*4/9 + C*1/9)*2/3 + (B*4/9 + C*4/9 + D*1/9)*1/3 |
michael@0 | 363 | = A*8/27 + B*12/27 + C*6/27 + D*1/27 |
michael@0 | 364 | = E |
michael@0 | 365 | 1234_ = (A*1/9 + B*4/9 + C*4/9)*1/3 + (B*1/9 + C*4/9 + D*4/9)*2/3 |
michael@0 | 366 | = A*1/27 + B*6/27 + C*12/27 + D*8/27 |
michael@0 | 367 | = F |
michael@0 | 368 | E*27 = A*8 + B*12 + C*6 + D |
michael@0 | 369 | F*27 = A + B*6 + C*12 + D*8 |
michael@0 | 370 | |
michael@0 | 371 | Group the known values on one side: |
michael@0 | 372 | |
michael@0 | 373 | M = E*27 - A*8 - D = B*12 + C* 6 |
michael@0 | 374 | N = F*27 - A - D*8 = B* 6 + C*12 |
michael@0 | 375 | M*2 - N = B*18 |
michael@0 | 376 | N*2 - M = C*18 |
michael@0 | 377 | B = (M*2 - N)/18 |
michael@0 | 378 | C = (N*2 - M)/18 |
michael@0 | 379 | */ |
michael@0 | 380 | |
michael@0 | 381 | static double interp_cubic_coords(const double* src, double t) { |
michael@0 | 382 | double ab = SkDInterp(src[0], src[2], t); |
michael@0 | 383 | double bc = SkDInterp(src[2], src[4], t); |
michael@0 | 384 | double cd = SkDInterp(src[4], src[6], t); |
michael@0 | 385 | double abc = SkDInterp(ab, bc, t); |
michael@0 | 386 | double bcd = SkDInterp(bc, cd, t); |
michael@0 | 387 | double abcd = SkDInterp(abc, bcd, t); |
michael@0 | 388 | return abcd; |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | SkDCubic SkDCubic::subDivide(double t1, double t2) const { |
michael@0 | 392 | if (t1 == 0 || t2 == 1) { |
michael@0 | 393 | if (t1 == 0 && t2 == 1) { |
michael@0 | 394 | return *this; |
michael@0 | 395 | } |
michael@0 | 396 | SkDCubicPair pair = chopAt(t1 == 0 ? t2 : t1); |
michael@0 | 397 | SkDCubic dst = t1 == 0 ? pair.first() : pair.second(); |
michael@0 | 398 | return dst; |
michael@0 | 399 | } |
michael@0 | 400 | SkDCubic dst; |
michael@0 | 401 | double ax = dst[0].fX = interp_cubic_coords(&fPts[0].fX, t1); |
michael@0 | 402 | double ay = dst[0].fY = interp_cubic_coords(&fPts[0].fY, t1); |
michael@0 | 403 | double ex = interp_cubic_coords(&fPts[0].fX, (t1*2+t2)/3); |
michael@0 | 404 | double ey = interp_cubic_coords(&fPts[0].fY, (t1*2+t2)/3); |
michael@0 | 405 | double fx = interp_cubic_coords(&fPts[0].fX, (t1+t2*2)/3); |
michael@0 | 406 | double fy = interp_cubic_coords(&fPts[0].fY, (t1+t2*2)/3); |
michael@0 | 407 | double dx = dst[3].fX = interp_cubic_coords(&fPts[0].fX, t2); |
michael@0 | 408 | double dy = dst[3].fY = interp_cubic_coords(&fPts[0].fY, t2); |
michael@0 | 409 | double mx = ex * 27 - ax * 8 - dx; |
michael@0 | 410 | double my = ey * 27 - ay * 8 - dy; |
michael@0 | 411 | double nx = fx * 27 - ax - dx * 8; |
michael@0 | 412 | double ny = fy * 27 - ay - dy * 8; |
michael@0 | 413 | /* bx = */ dst[1].fX = (mx * 2 - nx) / 18; |
michael@0 | 414 | /* by = */ dst[1].fY = (my * 2 - ny) / 18; |
michael@0 | 415 | /* cx = */ dst[2].fX = (nx * 2 - mx) / 18; |
michael@0 | 416 | /* cy = */ dst[2].fY = (ny * 2 - my) / 18; |
michael@0 | 417 | // FIXME: call align() ? |
michael@0 | 418 | return dst; |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | void SkDCubic::align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const { |
michael@0 | 422 | if (fPts[endIndex].fX == fPts[ctrlIndex].fX) { |
michael@0 | 423 | dstPt->fX = fPts[endIndex].fX; |
michael@0 | 424 | } |
michael@0 | 425 | if (fPts[endIndex].fY == fPts[ctrlIndex].fY) { |
michael@0 | 426 | dstPt->fY = fPts[endIndex].fY; |
michael@0 | 427 | } |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | void SkDCubic::subDivide(const SkDPoint& a, const SkDPoint& d, |
michael@0 | 431 | double t1, double t2, SkDPoint dst[2]) const { |
michael@0 | 432 | SkASSERT(t1 != t2); |
michael@0 | 433 | #if 0 |
michael@0 | 434 | double ex = interp_cubic_coords(&fPts[0].fX, (t1 * 2 + t2) / 3); |
michael@0 | 435 | double ey = interp_cubic_coords(&fPts[0].fY, (t1 * 2 + t2) / 3); |
michael@0 | 436 | double fx = interp_cubic_coords(&fPts[0].fX, (t1 + t2 * 2) / 3); |
michael@0 | 437 | double fy = interp_cubic_coords(&fPts[0].fY, (t1 + t2 * 2) / 3); |
michael@0 | 438 | double mx = ex * 27 - a.fX * 8 - d.fX; |
michael@0 | 439 | double my = ey * 27 - a.fY * 8 - d.fY; |
michael@0 | 440 | double nx = fx * 27 - a.fX - d.fX * 8; |
michael@0 | 441 | double ny = fy * 27 - a.fY - d.fY * 8; |
michael@0 | 442 | /* bx = */ dst[0].fX = (mx * 2 - nx) / 18; |
michael@0 | 443 | /* by = */ dst[0].fY = (my * 2 - ny) / 18; |
michael@0 | 444 | /* cx = */ dst[1].fX = (nx * 2 - mx) / 18; |
michael@0 | 445 | /* cy = */ dst[1].fY = (ny * 2 - my) / 18; |
michael@0 | 446 | #else |
michael@0 | 447 | // this approach assumes that the control points computed directly are accurate enough |
michael@0 | 448 | SkDCubic sub = subDivide(t1, t2); |
michael@0 | 449 | dst[0] = sub[1] + (a - sub[0]); |
michael@0 | 450 | dst[1] = sub[2] + (d - sub[3]); |
michael@0 | 451 | #endif |
michael@0 | 452 | if (t1 == 0 || t2 == 0) { |
michael@0 | 453 | align(0, 1, t1 == 0 ? &dst[0] : &dst[1]); |
michael@0 | 454 | } |
michael@0 | 455 | if (t1 == 1 || t2 == 1) { |
michael@0 | 456 | align(3, 2, t1 == 1 ? &dst[0] : &dst[1]); |
michael@0 | 457 | } |
michael@0 | 458 | if (precisely_subdivide_equal(dst[0].fX, a.fX)) { |
michael@0 | 459 | dst[0].fX = a.fX; |
michael@0 | 460 | } |
michael@0 | 461 | if (precisely_subdivide_equal(dst[0].fY, a.fY)) { |
michael@0 | 462 | dst[0].fY = a.fY; |
michael@0 | 463 | } |
michael@0 | 464 | if (precisely_subdivide_equal(dst[1].fX, d.fX)) { |
michael@0 | 465 | dst[1].fX = d.fX; |
michael@0 | 466 | } |
michael@0 | 467 | if (precisely_subdivide_equal(dst[1].fY, d.fY)) { |
michael@0 | 468 | dst[1].fY = d.fY; |
michael@0 | 469 | } |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | /* classic one t subdivision */ |
michael@0 | 473 | static void interp_cubic_coords(const double* src, double* dst, double t) { |
michael@0 | 474 | double ab = SkDInterp(src[0], src[2], t); |
michael@0 | 475 | double bc = SkDInterp(src[2], src[4], t); |
michael@0 | 476 | double cd = SkDInterp(src[4], src[6], t); |
michael@0 | 477 | double abc = SkDInterp(ab, bc, t); |
michael@0 | 478 | double bcd = SkDInterp(bc, cd, t); |
michael@0 | 479 | double abcd = SkDInterp(abc, bcd, t); |
michael@0 | 480 | |
michael@0 | 481 | dst[0] = src[0]; |
michael@0 | 482 | dst[2] = ab; |
michael@0 | 483 | dst[4] = abc; |
michael@0 | 484 | dst[6] = abcd; |
michael@0 | 485 | dst[8] = bcd; |
michael@0 | 486 | dst[10] = cd; |
michael@0 | 487 | dst[12] = src[6]; |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | SkDCubicPair SkDCubic::chopAt(double t) const { |
michael@0 | 491 | SkDCubicPair dst; |
michael@0 | 492 | if (t == 0.5) { |
michael@0 | 493 | dst.pts[0] = fPts[0]; |
michael@0 | 494 | dst.pts[1].fX = (fPts[0].fX + fPts[1].fX) / 2; |
michael@0 | 495 | dst.pts[1].fY = (fPts[0].fY + fPts[1].fY) / 2; |
michael@0 | 496 | dst.pts[2].fX = (fPts[0].fX + 2 * fPts[1].fX + fPts[2].fX) / 4; |
michael@0 | 497 | dst.pts[2].fY = (fPts[0].fY + 2 * fPts[1].fY + fPts[2].fY) / 4; |
michael@0 | 498 | dst.pts[3].fX = (fPts[0].fX + 3 * (fPts[1].fX + fPts[2].fX) + fPts[3].fX) / 8; |
michael@0 | 499 | dst.pts[3].fY = (fPts[0].fY + 3 * (fPts[1].fY + fPts[2].fY) + fPts[3].fY) / 8; |
michael@0 | 500 | dst.pts[4].fX = (fPts[1].fX + 2 * fPts[2].fX + fPts[3].fX) / 4; |
michael@0 | 501 | dst.pts[4].fY = (fPts[1].fY + 2 * fPts[2].fY + fPts[3].fY) / 4; |
michael@0 | 502 | dst.pts[5].fX = (fPts[2].fX + fPts[3].fX) / 2; |
michael@0 | 503 | dst.pts[5].fY = (fPts[2].fY + fPts[3].fY) / 2; |
michael@0 | 504 | dst.pts[6] = fPts[3]; |
michael@0 | 505 | return dst; |
michael@0 | 506 | } |
michael@0 | 507 | interp_cubic_coords(&fPts[0].fX, &dst.pts[0].fX, t); |
michael@0 | 508 | interp_cubic_coords(&fPts[0].fY, &dst.pts[0].fY, t); |
michael@0 | 509 | return dst; |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | #ifdef SK_DEBUG |
michael@0 | 513 | void SkDCubic::dump() { |
michael@0 | 514 | SkDebugf("{{"); |
michael@0 | 515 | int index = 0; |
michael@0 | 516 | do { |
michael@0 | 517 | fPts[index].dump(); |
michael@0 | 518 | SkDebugf(", "); |
michael@0 | 519 | } while (++index < 3); |
michael@0 | 520 | fPts[index].dump(); |
michael@0 | 521 | SkDebugf("}}\n"); |
michael@0 | 522 | } |
michael@0 | 523 | #endif |