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