caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CubicUtilities.h" |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 8 | #include "Extrema.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 9 | #include "QuadraticUtilities.h" |
| 10 | |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame] | 11 | const int gPrecisionUnit = 256; // FIXME: arbitrary -- should try different values in test framework |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 12 | |
| 13 | // FIXME: cache keep the bounds and/or precision with the caller? |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 14 | double calcPrecision(const Cubic& cubic) { |
| 15 | _Rect dRect; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 16 | dRect.setBounds(cubic); // OPTIMIZATION: just use setRawBounds ? |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 17 | double width = dRect.right - dRect.left; |
| 18 | double height = dRect.bottom - dRect.top; |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame] | 19 | return (width > height ? width : height) / gPrecisionUnit; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 20 | } |
| 21 | |
caryclark@google.com | 9d5f99b | 2013-01-22 12:55:54 +0000 | [diff] [blame] | 22 | #if SK_DEBUG |
| 23 | double calcPrecision(const Cubic& cubic, double t, double scale) { |
| 24 | Cubic part; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 25 | sub_divide(cubic, SkTMax(0., t - scale), SkTMin(1., t + scale), part); |
caryclark@google.com | 9d5f99b | 2013-01-22 12:55:54 +0000 | [diff] [blame] | 26 | return calcPrecision(part); |
| 27 | } |
| 28 | #endif |
| 29 | |
| 30 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 31 | void coefficients(const double* cubic, double& A, double& B, double& C, double& D) { |
| 32 | A = cubic[6]; // d |
| 33 | B = cubic[4] * 3; // 3*c |
| 34 | C = cubic[2] * 3; // 3*b |
| 35 | D = cubic[0]; // a |
| 36 | A -= D - C + B; // A = -a + 3*b - 3*c + d |
| 37 | B += 3 * D - 2 * C; // B = 3*a - 6*b + 3*c |
| 38 | C -= 3 * D; // C = -3*a + 3*b |
| 39 | } |
| 40 | |
| 41 | // cubic roots |
| 42 | |
| 43 | const double PI = 4 * atan(1); |
| 44 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 45 | // from SkGeometry.cpp (and Numeric Solutions, 5.6) |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 46 | int cubicRootsValidT(double A, double B, double C, double D, double t[3]) { |
| 47 | #if 0 |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 48 | if (approximately_zero(A)) { // we're just a quadratic |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 49 | return quadraticRootsValidT(B, C, D, t); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 50 | } |
| 51 | double a, b, c; |
| 52 | { |
| 53 | double invA = 1 / A; |
| 54 | a = B * invA; |
| 55 | b = C * invA; |
| 56 | c = D * invA; |
| 57 | } |
| 58 | double a2 = a * a; |
| 59 | double Q = (a2 - b * 3) / 9; |
| 60 | double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54; |
| 61 | double Q3 = Q * Q * Q; |
| 62 | double R2MinusQ3 = R * R - Q3; |
| 63 | double adiv3 = a / 3; |
| 64 | double* roots = t; |
| 65 | double r; |
| 66 | |
| 67 | if (R2MinusQ3 < 0) // we have 3 real roots |
| 68 | { |
| 69 | double theta = acos(R / sqrt(Q3)); |
| 70 | double neg2RootQ = -2 * sqrt(Q); |
| 71 | |
| 72 | r = neg2RootQ * cos(theta / 3) - adiv3; |
| 73 | if (is_unit_interval(r)) |
| 74 | *roots++ = r; |
| 75 | |
| 76 | r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3; |
| 77 | if (is_unit_interval(r)) |
| 78 | *roots++ = r; |
| 79 | |
| 80 | r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3; |
| 81 | if (is_unit_interval(r)) |
| 82 | *roots++ = r; |
| 83 | } |
| 84 | else // we have 1 real root |
| 85 | { |
| 86 | double A = fabs(R) + sqrt(R2MinusQ3); |
| 87 | A = cube_root(A); |
| 88 | if (R > 0) { |
| 89 | A = -A; |
| 90 | } |
| 91 | if (A != 0) { |
| 92 | A += Q / A; |
| 93 | } |
| 94 | r = A - adiv3; |
| 95 | if (is_unit_interval(r)) |
| 96 | *roots++ = r; |
| 97 | } |
| 98 | return (int)(roots - t); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 99 | #else |
| 100 | double s[3]; |
| 101 | int realRoots = cubicRootsReal(A, B, C, D, s); |
| 102 | int foundRoots = add_valid_ts(s, realRoots, t); |
| 103 | return foundRoots; |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | int cubicRootsReal(double A, double B, double C, double D, double s[3]) { |
| 108 | #if SK_DEBUG |
| 109 | // create a string mathematica understands |
| 110 | // GDB set print repe 15 # if repeated digits is a bother |
| 111 | // set print elements 400 # if line doesn't fit |
| 112 | char str[1024]; |
| 113 | bzero(str, sizeof(str)); |
| 114 | sprintf(str, "Solve[%1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]", A, B, C, D); |
caryclark@google.com | 5e0500f | 2013-02-20 12:51:37 +0000 | [diff] [blame] | 115 | mathematica_ize(str, sizeof(str)); |
| 116 | #if ONE_OFF_DEBUG |
| 117 | SkDebugf("%s\n", str); |
| 118 | #endif |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 119 | #endif |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 120 | if (approximately_zero(A) |
| 121 | && approximately_zero_when_compared_to(A, B) |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 122 | && approximately_zero_when_compared_to(A, C) |
| 123 | && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 124 | return quadraticRootsReal(B, C, D, s); |
| 125 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 126 | if (approximately_zero_when_compared_to(D, A) |
| 127 | && approximately_zero_when_compared_to(D, B) |
| 128 | && approximately_zero_when_compared_to(D, C)) { // 0 is one root |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 129 | int num = quadraticRootsReal(A, B, C, s); |
| 130 | for (int i = 0; i < num; ++i) { |
| 131 | if (approximately_zero(s[i])) { |
| 132 | return num; |
| 133 | } |
| 134 | } |
| 135 | s[num++] = 0; |
| 136 | return num; |
| 137 | } |
| 138 | if (approximately_zero(A + B + C + D)) { // 1 is one root |
| 139 | int num = quadraticRootsReal(A, A + B, -D, s); |
| 140 | for (int i = 0; i < num; ++i) { |
| 141 | if (AlmostEqualUlps(s[i], 1)) { |
| 142 | return num; |
| 143 | } |
| 144 | } |
| 145 | s[num++] = 1; |
| 146 | return num; |
| 147 | } |
| 148 | double a, b, c; |
| 149 | { |
| 150 | double invA = 1 / A; |
| 151 | a = B * invA; |
| 152 | b = C * invA; |
| 153 | c = D * invA; |
| 154 | } |
| 155 | double a2 = a * a; |
| 156 | double Q = (a2 - b * 3) / 9; |
| 157 | double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54; |
| 158 | double R2 = R * R; |
| 159 | double Q3 = Q * Q * Q; |
| 160 | double R2MinusQ3 = R2 - Q3; |
| 161 | double adiv3 = a / 3; |
| 162 | double r; |
| 163 | double* roots = s; |
| 164 | #if 0 |
| 165 | if (approximately_zero_squared(R2MinusQ3) && AlmostEqualUlps(R2, Q3)) { |
| 166 | if (approximately_zero_squared(R)) {/* one triple solution */ |
| 167 | *roots++ = -adiv3; |
| 168 | } else { /* one single and one double solution */ |
| 169 | |
| 170 | double u = cube_root(-R); |
| 171 | *roots++ = 2 * u - adiv3; |
| 172 | *roots++ = -u - adiv3; |
| 173 | } |
| 174 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 175 | else |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 176 | #endif |
| 177 | if (R2MinusQ3 < 0) // we have 3 real roots |
| 178 | { |
| 179 | double theta = acos(R / sqrt(Q3)); |
| 180 | double neg2RootQ = -2 * sqrt(Q); |
| 181 | |
| 182 | r = neg2RootQ * cos(theta / 3) - adiv3; |
| 183 | *roots++ = r; |
| 184 | |
| 185 | r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3; |
| 186 | if (!AlmostEqualUlps(s[0], r)) { |
| 187 | *roots++ = r; |
| 188 | } |
| 189 | r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3; |
| 190 | if (!AlmostEqualUlps(s[0], r) && (roots - s == 1 || !AlmostEqualUlps(s[1], r))) { |
| 191 | *roots++ = r; |
| 192 | } |
| 193 | } |
| 194 | else // we have 1 real root |
| 195 | { |
| 196 | double sqrtR2MinusQ3 = sqrt(R2MinusQ3); |
| 197 | double A = fabs(R) + sqrtR2MinusQ3; |
| 198 | A = cube_root(A); |
| 199 | if (R > 0) { |
| 200 | A = -A; |
| 201 | } |
| 202 | if (A != 0) { |
| 203 | A += Q / A; |
| 204 | } |
| 205 | r = A - adiv3; |
| 206 | *roots++ = r; |
| 207 | if (AlmostEqualUlps(R2, Q3)) { |
| 208 | r = -A / 2 - adiv3; |
| 209 | if (!AlmostEqualUlps(s[0], r)) { |
| 210 | *roots++ = r; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | return (int)(roots - s); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 215 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 216 | |
| 217 | // from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf |
| 218 | // c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3 |
| 219 | // c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2 |
| 220 | // = 3(b-a)(1-t)^2 + 6(c-b)t(1-t) + 3(d-c)t^2 |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 221 | static double derivativeAtT(const double* cubic, double t) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 222 | double one_t = 1 - t; |
| 223 | double a = cubic[0]; |
| 224 | double b = cubic[2]; |
| 225 | double c = cubic[4]; |
| 226 | double d = cubic[6]; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 227 | return 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 228 | } |
| 229 | |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 230 | double dx_at_t(const Cubic& cubic, double t) { |
| 231 | return derivativeAtT(&cubic[0].x, t); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 232 | } |
| 233 | |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 234 | double dy_at_t(const Cubic& cubic, double t) { |
| 235 | return derivativeAtT(&cubic[0].y, t); |
| 236 | } |
| 237 | |
| 238 | // OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t? |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame] | 239 | _Vector dxdy_at_t(const Cubic& cubic, double t) { |
| 240 | _Vector result = { derivativeAtT(&cubic[0].x, t), derivativeAtT(&cubic[0].y, t) }; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 241 | return result; |
| 242 | } |
| 243 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 244 | int find_cubic_inflections(const Cubic& src, double tValues[]) |
| 245 | { |
| 246 | double Ax = src[1].x - src[0].x; |
| 247 | double Ay = src[1].y - src[0].y; |
| 248 | double Bx = src[2].x - 2 * src[1].x + src[0].x; |
| 249 | double By = src[2].y - 2 * src[1].y + src[0].y; |
| 250 | double Cx = src[3].x + 3 * (src[1].x - src[2].x) - src[0].x; |
| 251 | double Cy = src[3].y + 3 * (src[1].y - src[2].y) - src[0].y; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 252 | return quadraticRootsValidT(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx), Ax * By - Ay * Bx, tValues); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 253 | } |
| 254 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 255 | bool rotate(const Cubic& cubic, int zero, int index, Cubic& rotPath) { |
| 256 | double dy = cubic[index].y - cubic[zero].y; |
| 257 | double dx = cubic[index].x - cubic[zero].x; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 258 | if (approximately_zero(dy)) { |
| 259 | if (approximately_zero(dx)) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | memcpy(rotPath, cubic, sizeof(Cubic)); |
| 263 | return true; |
| 264 | } |
| 265 | for (int index = 0; index < 4; ++index) { |
| 266 | rotPath[index].x = cubic[index].x * dx + cubic[index].y * dy; |
| 267 | rotPath[index].y = cubic[index].y * dx - cubic[index].x * dy; |
| 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 272 | #if 0 // unused for now |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 273 | double secondDerivativeAtT(const double* cubic, double t) { |
| 274 | double a = cubic[0]; |
| 275 | double b = cubic[2]; |
| 276 | double c = cubic[4]; |
| 277 | double d = cubic[6]; |
| 278 | return (c - 2 * b + a) * (1 - t) + (d - 2 * c + b) * t; |
| 279 | } |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 280 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 281 | |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 282 | _Point top(const Cubic& cubic, double startT, double endT) { |
| 283 | Cubic sub; |
| 284 | sub_divide(cubic, startT, endT, sub); |
| 285 | _Point topPt = sub[0]; |
| 286 | if (topPt.y > sub[3].y || (topPt.y == sub[3].y && topPt.x > sub[3].x)) { |
| 287 | topPt = sub[3]; |
| 288 | } |
| 289 | double extremeTs[2]; |
| 290 | if (!between(sub[0].y, sub[1].y, sub[3].y) && !between(sub[0].y, sub[2].y, sub[3].y)) { |
| 291 | int roots = findExtrema(sub[0].y, sub[1].y, sub[2].y, sub[3].y, extremeTs); |
| 292 | for (int index = 0; index < roots; ++index) { |
| 293 | _Point mid; |
| 294 | double t = startT + (endT - startT) * extremeTs[index]; |
| 295 | xy_at_t(cubic, t, mid.x, mid.y); |
| 296 | if (topPt.y > mid.y || (topPt.y == mid.y && topPt.x > mid.x)) { |
| 297 | topPt = mid; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | return topPt; |
| 302 | } |
| 303 | |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame] | 304 | // OPTIMIZE: avoid computing the unused half |
| 305 | void xy_at_t(const Cubic& cubic, double t, double& x, double& y) { |
| 306 | _Point xy = xy_at_t(cubic, t); |
| 307 | if (&x) { |
| 308 | x = xy.x; |
| 309 | } |
| 310 | if (&y) { |
| 311 | y = xy.y; |
| 312 | } |
| 313 | } |
| 314 | |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 315 | _Point xy_at_t(const Cubic& cubic, double t) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 316 | double one_t = 1 - t; |
| 317 | double one_t2 = one_t * one_t; |
| 318 | double a = one_t2 * one_t; |
| 319 | double b = 3 * one_t2 * t; |
| 320 | double t2 = t * t; |
| 321 | double c = 3 * one_t * t2; |
| 322 | double d = t2 * t; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 323 | _Point result = {a * cubic[0].x + b * cubic[1].x + c * cubic[2].x + d * cubic[3].x, |
| 324 | a * cubic[0].y + b * cubic[1].y + c * cubic[2].y + d * cubic[3].y}; |
| 325 | return result; |
| 326 | } |