blob: a5174c95a92c1712e3717bc96536650fdbdff077 [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 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000118 if (approximately_zero_when_compared_to(D, A)
119 && approximately_zero_when_compared_to(D, B)
120 && approximately_zero_when_compared_to(D, C)) { // 0 is one root
caryclark@google.com9f602912013-01-24 21:47:16 +0000121 int num = quadraticRootsReal(A, B, C, s);
122 for (int i = 0; i < num; ++i) {
123 if (approximately_zero(s[i])) {
124 return num;
125 }
126 }
127 s[num++] = 0;
128 return num;
129 }
130 if (approximately_zero(A + B + C + D)) { // 1 is one root
131 int num = quadraticRootsReal(A, A + B, -D, s);
132 for (int i = 0; i < num; ++i) {
133 if (AlmostEqualUlps(s[i], 1)) {
134 return num;
135 }
136 }
137 s[num++] = 1;
138 return num;
139 }
140 double a, b, c;
141 {
142 double invA = 1 / A;
143 a = B * invA;
144 b = C * invA;
145 c = D * invA;
146 }
147 double a2 = a * a;
148 double Q = (a2 - b * 3) / 9;
149 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
150 double R2 = R * R;
151 double Q3 = Q * Q * Q;
152 double R2MinusQ3 = R2 - Q3;
153 double adiv3 = a / 3;
154 double r;
155 double* roots = s;
156#if 0
157 if (approximately_zero_squared(R2MinusQ3) && AlmostEqualUlps(R2, Q3)) {
158 if (approximately_zero_squared(R)) {/* one triple solution */
159 *roots++ = -adiv3;
160 } else { /* one single and one double solution */
161
162 double u = cube_root(-R);
163 *roots++ = 2 * u - adiv3;
164 *roots++ = -u - adiv3;
165 }
166 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000167 else
caryclark@google.com9f602912013-01-24 21:47:16 +0000168#endif
169 if (R2MinusQ3 < 0) // we have 3 real roots
170 {
171 double theta = acos(R / sqrt(Q3));
172 double neg2RootQ = -2 * sqrt(Q);
173
174 r = neg2RootQ * cos(theta / 3) - adiv3;
175 *roots++ = r;
176
177 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
178 if (!AlmostEqualUlps(s[0], r)) {
179 *roots++ = r;
180 }
181 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
182 if (!AlmostEqualUlps(s[0], r) && (roots - s == 1 || !AlmostEqualUlps(s[1], r))) {
183 *roots++ = r;
184 }
185 }
186 else // we have 1 real root
187 {
188 double sqrtR2MinusQ3 = sqrt(R2MinusQ3);
189 double A = fabs(R) + sqrtR2MinusQ3;
190 A = cube_root(A);
191 if (R > 0) {
192 A = -A;
193 }
194 if (A != 0) {
195 A += Q / A;
196 }
197 r = A - adiv3;
198 *roots++ = r;
199 if (AlmostEqualUlps(R2, Q3)) {
200 r = -A / 2 - adiv3;
201 if (!AlmostEqualUlps(s[0], r)) {
202 *roots++ = r;
203 }
204 }
205 }
206 return (int)(roots - s);
caryclark@google.comc6825902012-02-03 22:07:47 +0000207}
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000208
209// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
210// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
211// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
212// = 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 +0000213static double derivativeAtT(const double* cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000214 double one_t = 1 - t;
215 double a = cubic[0];
216 double b = cubic[2];
217 double c = cubic[4];
218 double d = cubic[6];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000219 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 +0000220}
221
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000222double dx_at_t(const Cubic& cubic, double t) {
223 return derivativeAtT(&cubic[0].x, t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000224}
225
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000226double dy_at_t(const Cubic& cubic, double t) {
227 return derivativeAtT(&cubic[0].y, t);
228}
229
230// OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t?
231void dxdy_at_t(const Cubic& cubic, double t, _Point& dxdy) {
232 dxdy.x = derivativeAtT(&cubic[0].x, t);
233 dxdy.y = derivativeAtT(&cubic[0].y, t);
234}
235
caryclark@google.com73ca6242013-01-17 21:02:47 +0000236int find_cubic_inflections(const Cubic& src, double tValues[])
237{
238 double Ax = src[1].x - src[0].x;
239 double Ay = src[1].y - src[0].y;
240 double Bx = src[2].x - 2 * src[1].x + src[0].x;
241 double By = src[2].y - 2 * src[1].y + src[0].y;
242 double Cx = src[3].x + 3 * (src[1].x - src[2].x) - src[0].x;
243 double Cy = src[3].y + 3 * (src[1].y - src[2].y) - src[0].y;
caryclark@google.com9f602912013-01-24 21:47:16 +0000244 return quadraticRootsValidT(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx), Ax * By - Ay * Bx, tValues);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000245}
246
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000247bool rotate(const Cubic& cubic, int zero, int index, Cubic& rotPath) {
248 double dy = cubic[index].y - cubic[zero].y;
249 double dx = cubic[index].x - cubic[zero].x;
caryclark@google.com9f602912013-01-24 21:47:16 +0000250 if (approximately_zero(dy)) {
251 if (approximately_zero(dx)) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000252 return false;
253 }
254 memcpy(rotPath, cubic, sizeof(Cubic));
255 return true;
256 }
257 for (int index = 0; index < 4; ++index) {
258 rotPath[index].x = cubic[index].x * dx + cubic[index].y * dy;
259 rotPath[index].y = cubic[index].y * dx - cubic[index].x * dy;
260 }
261 return true;
262}
263
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000264#if 0 // unused for now
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000265double secondDerivativeAtT(const double* cubic, double t) {
266 double a = cubic[0];
267 double b = cubic[2];
268 double c = cubic[4];
269 double d = cubic[6];
270 return (c - 2 * b + a) * (1 - t) + (d - 2 * c + b) * t;
271}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000272#endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000273
274void xy_at_t(const Cubic& cubic, double t, double& x, double& y) {
275 double one_t = 1 - t;
276 double one_t2 = one_t * one_t;
277 double a = one_t2 * one_t;
278 double b = 3 * one_t2 * t;
279 double t2 = t * t;
280 double c = 3 * one_t * t2;
281 double d = t2 * t;
282 if (&x) {
283 x = a * cubic[0].x + b * cubic[1].x + c * cubic[2].x + d * cubic[3].x;
284 }
285 if (&y) {
286 y = a * cubic[0].y + b * cubic[1].y + c * cubic[2].y + d * cubic[3].y;
287 }
288}