blob: 3e2f474d7c448b1593e306f030b8d9e69054807c [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.comc6825902012-02-03 22:07:47 +00008#include "QuadraticUtilities.h"
9
caryclark@google.com05c4bad2013-01-19 13:22:39 +000010const 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.com73ca6242013-01-17 21:02:47 +000013double calcPrecision(const Cubic& cubic) {
14 _Rect dRect;
caryclark@google.com05c4bad2013-01-19 13:22:39 +000015 dRect.setBounds(cubic); // OPTIMIZATION: just use setRawBounds ?
caryclark@google.com73ca6242013-01-17 21:02:47 +000016 double width = dRect.right - dRect.left;
17 double height = dRect.bottom - dRect.top;
caryclark@google.com05c4bad2013-01-19 13:22:39 +000018 return (width > height ? width : height) / precisionUnit;
caryclark@google.com73ca6242013-01-17 21:02:47 +000019}
20
caryclark@google.com9d5f99b2013-01-22 12:55:54 +000021#if SK_DEBUG
22double calcPrecision(const Cubic& cubic, double t, double scale) {
23 Cubic part;
caryclark@google.com9f602912013-01-24 21:47:16 +000024 sub_divide(cubic, SkTMax(0., t - scale), SkTMin(1., t + scale), part);
caryclark@google.com9d5f99b2013-01-22 12:55:54 +000025 return calcPrecision(part);
26}
27#endif
28
29
caryclark@google.comc6825902012-02-03 22:07:47 +000030void 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
42const double PI = 4 * atan(1);
43
caryclark@google.comc6825902012-02-03 22:07:47 +000044// from SkGeometry.cpp (and Numeric Solutions, 5.6)
caryclark@google.com9f602912013-01-24 21:47:16 +000045int cubicRootsValidT(double A, double B, double C, double D, double t[3]) {
46#if 0
caryclark@google.comc6825902012-02-03 22:07:47 +000047 if (approximately_zero(A)) { // we're just a quadratic
caryclark@google.com9f602912013-01-24 21:47:16 +000048 return quadraticRootsValidT(B, C, D, t);
caryclark@google.comc6825902012-02-03 22:07:47 +000049 }
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.com9f602912013-01-24 21:47:16 +000098#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
106int 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.comc6825902012-02-03 22:07:47 +0000205}
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000206
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.com05c4bad2013-01-19 13:22:39 +0000211static double derivativeAtT(const double* cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000212 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.com05c4bad2013-01-19 13:22:39 +0000217 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 +0000218}
219
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000220double dx_at_t(const Cubic& cubic, double t) {
221 return derivativeAtT(&cubic[0].x, t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000222}
223
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000224double 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?
229void 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.com73ca6242013-01-17 21:02:47 +0000235int 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.com9f602912013-01-24 21:47:16 +0000243 return quadraticRootsValidT(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx), Ax * By - Ay * Bx, tValues);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000244}
245
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000246bool 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.com9f602912013-01-24 21:47:16 +0000249 if (approximately_zero(dy)) {
250 if (approximately_zero(dx)) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000251 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.com05c4bad2013-01-19 13:22:39 +0000263#if 0 // unused for now
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000264double 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.com05c4bad2013-01-19 13:22:39 +0000271#endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000272
273void 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}