blob: 0c7f2c1cb5d656d879e024f6b8eafc6466c964b4 [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.com05c4bad2013-01-19 13:22:39 +000011const int precisionUnit = 256; // FIXME: arbitrary -- should try different values in test framework
12
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.com05c4bad2013-01-19 13:22:39 +000019 return (width > height ? width : height) / precisionUnit;
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);
115#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000116 if (approximately_zero(A)
117 && approximately_zero_when_compared_to(A, B)
caryclark@google.combeda3892013-02-07 13:13:41 +0000118 && approximately_zero_when_compared_to(A, C)
119 && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic
caryclark@google.com9f602912013-01-24 21:47:16 +0000120 return quadraticRootsReal(B, C, D, s);
121 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000122 if (approximately_zero_when_compared_to(D, A)
123 && approximately_zero_when_compared_to(D, B)
124 && approximately_zero_when_compared_to(D, C)) { // 0 is one root
caryclark@google.com9f602912013-01-24 21:47:16 +0000125 int num = quadraticRootsReal(A, B, C, s);
126 for (int i = 0; i < num; ++i) {
127 if (approximately_zero(s[i])) {
128 return num;
129 }
130 }
131 s[num++] = 0;
132 return num;
133 }
134 if (approximately_zero(A + B + C + D)) { // 1 is one root
135 int num = quadraticRootsReal(A, A + B, -D, s);
136 for (int i = 0; i < num; ++i) {
137 if (AlmostEqualUlps(s[i], 1)) {
138 return num;
139 }
140 }
141 s[num++] = 1;
142 return num;
143 }
144 double a, b, c;
145 {
146 double invA = 1 / A;
147 a = B * invA;
148 b = C * invA;
149 c = D * invA;
150 }
151 double a2 = a * a;
152 double Q = (a2 - b * 3) / 9;
153 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
154 double R2 = R * R;
155 double Q3 = Q * Q * Q;
156 double R2MinusQ3 = R2 - Q3;
157 double adiv3 = a / 3;
158 double r;
159 double* roots = s;
160#if 0
161 if (approximately_zero_squared(R2MinusQ3) && AlmostEqualUlps(R2, Q3)) {
162 if (approximately_zero_squared(R)) {/* one triple solution */
163 *roots++ = -adiv3;
164 } else { /* one single and one double solution */
165
166 double u = cube_root(-R);
167 *roots++ = 2 * u - adiv3;
168 *roots++ = -u - adiv3;
169 }
170 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000171 else
caryclark@google.com9f602912013-01-24 21:47:16 +0000172#endif
173 if (R2MinusQ3 < 0) // we have 3 real roots
174 {
175 double theta = acos(R / sqrt(Q3));
176 double neg2RootQ = -2 * sqrt(Q);
177
178 r = neg2RootQ * cos(theta / 3) - adiv3;
179 *roots++ = r;
180
181 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
182 if (!AlmostEqualUlps(s[0], r)) {
183 *roots++ = r;
184 }
185 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
186 if (!AlmostEqualUlps(s[0], r) && (roots - s == 1 || !AlmostEqualUlps(s[1], r))) {
187 *roots++ = r;
188 }
189 }
190 else // we have 1 real root
191 {
192 double sqrtR2MinusQ3 = sqrt(R2MinusQ3);
193 double A = fabs(R) + sqrtR2MinusQ3;
194 A = cube_root(A);
195 if (R > 0) {
196 A = -A;
197 }
198 if (A != 0) {
199 A += Q / A;
200 }
201 r = A - adiv3;
202 *roots++ = r;
203 if (AlmostEqualUlps(R2, Q3)) {
204 r = -A / 2 - adiv3;
205 if (!AlmostEqualUlps(s[0], r)) {
206 *roots++ = r;
207 }
208 }
209 }
210 return (int)(roots - s);
caryclark@google.comc6825902012-02-03 22:07:47 +0000211}
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000212
213// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
214// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
215// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
216// = 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 +0000217static double derivativeAtT(const double* cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000218 double one_t = 1 - t;
219 double a = cubic[0];
220 double b = cubic[2];
221 double c = cubic[4];
222 double d = cubic[6];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000223 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 +0000224}
225
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000226double dx_at_t(const Cubic& cubic, double t) {
227 return derivativeAtT(&cubic[0].x, t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000228}
229
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000230double dy_at_t(const Cubic& cubic, double t) {
231 return derivativeAtT(&cubic[0].y, t);
232}
233
234// OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t?
235void dxdy_at_t(const Cubic& cubic, double t, _Point& dxdy) {
236 dxdy.x = derivativeAtT(&cubic[0].x, t);
237 dxdy.y = derivativeAtT(&cubic[0].y, t);
238}
239
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000240_Point dxdy_at_t(const Cubic& cubic, double t) {
241 _Point result = { derivativeAtT(&cubic[0].x, t), derivativeAtT(&cubic[0].y, t) };
242 return result;
243}
244
caryclark@google.com73ca6242013-01-17 21:02:47 +0000245int find_cubic_inflections(const Cubic& src, double tValues[])
246{
247 double Ax = src[1].x - src[0].x;
248 double Ay = src[1].y - src[0].y;
249 double Bx = src[2].x - 2 * src[1].x + src[0].x;
250 double By = src[2].y - 2 * src[1].y + src[0].y;
251 double Cx = src[3].x + 3 * (src[1].x - src[2].x) - src[0].x;
252 double Cy = src[3].y + 3 * (src[1].y - src[2].y) - src[0].y;
caryclark@google.com9f602912013-01-24 21:47:16 +0000253 return quadraticRootsValidT(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx), Ax * By - Ay * Bx, tValues);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000254}
255
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000256bool rotate(const Cubic& cubic, int zero, int index, Cubic& rotPath) {
257 double dy = cubic[index].y - cubic[zero].y;
258 double dx = cubic[index].x - cubic[zero].x;
caryclark@google.com9f602912013-01-24 21:47:16 +0000259 if (approximately_zero(dy)) {
260 if (approximately_zero(dx)) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000261 return false;
262 }
263 memcpy(rotPath, cubic, sizeof(Cubic));
264 return true;
265 }
266 for (int index = 0; index < 4; ++index) {
267 rotPath[index].x = cubic[index].x * dx + cubic[index].y * dy;
268 rotPath[index].y = cubic[index].y * dx - cubic[index].x * dy;
269 }
270 return true;
271}
272
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000273#if 0 // unused for now
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000274double secondDerivativeAtT(const double* cubic, double t) {
275 double a = cubic[0];
276 double b = cubic[2];
277 double c = cubic[4];
278 double d = cubic[6];
279 return (c - 2 * b + a) * (1 - t) + (d - 2 * c + b) * t;
280}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000281#endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000282
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000283_Point top(const Cubic& cubic, double startT, double endT) {
284 Cubic sub;
285 sub_divide(cubic, startT, endT, sub);
286 _Point topPt = sub[0];
287 if (topPt.y > sub[3].y || (topPt.y == sub[3].y && topPt.x > sub[3].x)) {
288 topPt = sub[3];
289 }
290 double extremeTs[2];
291 if (!between(sub[0].y, sub[1].y, sub[3].y) && !between(sub[0].y, sub[2].y, sub[3].y)) {
292 int roots = findExtrema(sub[0].y, sub[1].y, sub[2].y, sub[3].y, extremeTs);
293 for (int index = 0; index < roots; ++index) {
294 _Point mid;
295 double t = startT + (endT - startT) * extremeTs[index];
296 xy_at_t(cubic, t, mid.x, mid.y);
297 if (topPt.y > mid.y || (topPt.y == mid.y && topPt.x > mid.x)) {
298 topPt = mid;
299 }
300 }
301 }
302 return topPt;
303}
304
305_Point xy_at_t(const Cubic& cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000306 double one_t = 1 - t;
307 double one_t2 = one_t * one_t;
308 double a = one_t2 * one_t;
309 double b = 3 * one_t2 * t;
310 double t2 = t * t;
311 double c = 3 * one_t * t2;
312 double d = t2 * t;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000313 _Point result = {a * cubic[0].x + b * cubic[1].x + c * cubic[2].x + d * cubic[3].x,
314 a * cubic[0].y + b * cubic[1].y + c * cubic[2].y + d * cubic[3].y};
315 return result;
316}
317
318
319void xy_at_t(const Cubic& cubic, double t, double& x, double& y) {
320 _Point xy = xy_at_t(cubic, t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000321 if (&x) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000322 x = xy.x;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000323 }
324 if (&y) {
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000325 y = xy.y;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000326 }
327}
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000328