blob: 56f1e4548d636de4e202443424f36d3ceab81ae5 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
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.comc6825902012-02-03 22:07:47 +00007#include "CubicUtilities.h"
caryclark@google.com45a8fc62013-02-14 15:29:11 +00008#include "Extrema.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00009#include "QuadraticUtilities.h"
10
caryclark@google.com7ff5c842013-02-26 15:56:05 +000011const int gPrecisionUnit = 256; // FIXME: arbitrary -- should try different values in test framework
caryclark@google.com05c4bad2013-01-19 13:22:39 +000012
13// FIXME: cache keep the bounds and/or precision with the caller?
caryclark@google.com73ca6242013-01-17 21:02:47 +000014double calcPrecision(const Cubic& cubic) {
15 _Rect dRect;
caryclark@google.com05c4bad2013-01-19 13:22:39 +000016 dRect.setBounds(cubic); // OPTIMIZATION: just use setRawBounds ?
caryclark@google.com73ca6242013-01-17 21:02:47 +000017 double width = dRect.right - dRect.left;
18 double height = dRect.bottom - dRect.top;
caryclark@google.com7ff5c842013-02-26 15:56:05 +000019 return (width > height ? width : height) / gPrecisionUnit;
caryclark@google.com73ca6242013-01-17 21:02:47 +000020}
21
caryclark@google.com9d5f99b2013-01-22 12:55:54 +000022#if SK_DEBUG
23double calcPrecision(const Cubic& cubic, double t, double scale) {
24 Cubic part;
caryclark@google.com9f602912013-01-24 21:47:16 +000025 sub_divide(cubic, SkTMax(0., t - scale), SkTMin(1., t + scale), part);
caryclark@google.com9d5f99b2013-01-22 12:55:54 +000026 return calcPrecision(part);
27}
28#endif
29
30
caryclark@google.comc6825902012-02-03 22:07:47 +000031void 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
43const double PI = 4 * atan(1);
44
caryclark@google.comc6825902012-02-03 22:07:47 +000045// from SkGeometry.cpp (and Numeric Solutions, 5.6)
caryclark@google.com9f602912013-01-24 21:47:16 +000046int cubicRootsValidT(double A, double B, double C, double D, double t[3]) {
47#if 0
caryclark@google.comc6825902012-02-03 22:07:47 +000048 if (approximately_zero(A)) { // we're just a quadratic
caryclark@google.com9f602912013-01-24 21:47:16 +000049 return quadraticRootsValidT(B, C, D, t);
caryclark@google.comc6825902012-02-03 22:07:47 +000050 }
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.com9f602912013-01-24 21:47:16 +000099#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
107int 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.com5e0500f2013-02-20 12:51:37 +0000115 mathematica_ize(str, sizeof(str));
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000116#if ONE_OFF_DEBUG && ONE_OFF_DEBUG_MATHEMATICA
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000117 SkDebugf("%s\n", str);
118#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000119#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000120 if (approximately_zero(A)
121 && approximately_zero_when_compared_to(A, B)
caryclark@google.combeda3892013-02-07 13:13:41 +0000122 && approximately_zero_when_compared_to(A, C)
123 && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic
caryclark@google.com9f602912013-01-24 21:47:16 +0000124 return quadraticRootsReal(B, C, D, s);
125 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000126 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.com9f602912013-01-24 21:47:16 +0000129 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.com4024f322013-01-25 07:06:46 +0000175 else
caryclark@google.com9f602912013-01-24 21:47:16 +0000176#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.comc6825902012-02-03 22:07:47 +0000215}
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000216
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.com05c4bad2013-01-19 13:22:39 +0000221static double derivativeAtT(const double* cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000222 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.com05c4bad2013-01-19 13:22:39 +0000227 return 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000228}
229
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000230double dx_at_t(const Cubic& cubic, double t) {
231 return derivativeAtT(&cubic[0].x, t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000232}
233
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000234double 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.com7ff5c842013-02-26 15:56:05 +0000239_Vector dxdy_at_t(const Cubic& cubic, double t) {
240 _Vector result = { derivativeAtT(&cubic[0].x, t), derivativeAtT(&cubic[0].y, t) };
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000241 return result;
242}
243
caryclark@google.com73ca6242013-01-17 21:02:47 +0000244int 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.com9f602912013-01-24 21:47:16 +0000252 return quadraticRootsValidT(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx), Ax * By - Ay * Bx, tValues);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000253}
254
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000255bool 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.com9f602912013-01-24 21:47:16 +0000258 if (approximately_zero(dy)) {
259 if (approximately_zero(dx)) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000260 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.com05c4bad2013-01-19 13:22:39 +0000272#if 0 // unused for now
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000273double 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.com05c4bad2013-01-19 13:22:39 +0000280#endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000281
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000282_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.com7ff5c842013-02-26 15:56:05 +0000304// OPTIMIZE: avoid computing the unused half
305void 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.com45a8fc62013-02-14 15:29:11 +0000315_Point xy_at_t(const Cubic& cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000316 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.com45a8fc62013-02-14 15:29:11 +0000323 _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}