blob: 78be8df878ca60649e8771a959acccf9e460e377 [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
caryclark@google.combeda3892013-02-07 13:13:41 +0000115 if (approximately_zero_when_compared_to(A, B)
116 && approximately_zero_when_compared_to(A, C)
117 && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic
caryclark@google.com9f602912013-01-24 21:47:16 +0000118 return quadraticRootsReal(B, C, D, s);
119 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000120 if (approximately_zero_when_compared_to(D, A)
121 && approximately_zero_when_compared_to(D, B)
122 && approximately_zero_when_compared_to(D, C)) { // 0 is one root
caryclark@google.com9f602912013-01-24 21:47:16 +0000123 int num = quadraticRootsReal(A, B, C, s);
124 for (int i = 0; i < num; ++i) {
125 if (approximately_zero(s[i])) {
126 return num;
127 }
128 }
129 s[num++] = 0;
130 return num;
131 }
132 if (approximately_zero(A + B + C + D)) { // 1 is one root
133 int num = quadraticRootsReal(A, A + B, -D, s);
134 for (int i = 0; i < num; ++i) {
135 if (AlmostEqualUlps(s[i], 1)) {
136 return num;
137 }
138 }
139 s[num++] = 1;
140 return num;
141 }
142 double a, b, c;
143 {
144 double invA = 1 / A;
145 a = B * invA;
146 b = C * invA;
147 c = D * invA;
148 }
149 double a2 = a * a;
150 double Q = (a2 - b * 3) / 9;
151 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
152 double R2 = R * R;
153 double Q3 = Q * Q * Q;
154 double R2MinusQ3 = R2 - Q3;
155 double adiv3 = a / 3;
156 double r;
157 double* roots = s;
158#if 0
159 if (approximately_zero_squared(R2MinusQ3) && AlmostEqualUlps(R2, Q3)) {
160 if (approximately_zero_squared(R)) {/* one triple solution */
161 *roots++ = -adiv3;
162 } else { /* one single and one double solution */
163
164 double u = cube_root(-R);
165 *roots++ = 2 * u - adiv3;
166 *roots++ = -u - adiv3;
167 }
168 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000169 else
caryclark@google.com9f602912013-01-24 21:47:16 +0000170#endif
171 if (R2MinusQ3 < 0) // we have 3 real roots
172 {
173 double theta = acos(R / sqrt(Q3));
174 double neg2RootQ = -2 * sqrt(Q);
175
176 r = neg2RootQ * cos(theta / 3) - adiv3;
177 *roots++ = r;
178
179 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
180 if (!AlmostEqualUlps(s[0], r)) {
181 *roots++ = r;
182 }
183 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
184 if (!AlmostEqualUlps(s[0], r) && (roots - s == 1 || !AlmostEqualUlps(s[1], r))) {
185 *roots++ = r;
186 }
187 }
188 else // we have 1 real root
189 {
190 double sqrtR2MinusQ3 = sqrt(R2MinusQ3);
191 double A = fabs(R) + sqrtR2MinusQ3;
192 A = cube_root(A);
193 if (R > 0) {
194 A = -A;
195 }
196 if (A != 0) {
197 A += Q / A;
198 }
199 r = A - adiv3;
200 *roots++ = r;
201 if (AlmostEqualUlps(R2, Q3)) {
202 r = -A / 2 - adiv3;
203 if (!AlmostEqualUlps(s[0], r)) {
204 *roots++ = r;
205 }
206 }
207 }
208 return (int)(roots - s);
caryclark@google.comc6825902012-02-03 22:07:47 +0000209}
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000210
211// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
212// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
213// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
214// = 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 +0000215static double derivativeAtT(const double* cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000216 double one_t = 1 - t;
217 double a = cubic[0];
218 double b = cubic[2];
219 double c = cubic[4];
220 double d = cubic[6];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000221 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 +0000222}
223
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000224double dx_at_t(const Cubic& cubic, double t) {
225 return derivativeAtT(&cubic[0].x, t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000226}
227
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000228double dy_at_t(const Cubic& cubic, double t) {
229 return derivativeAtT(&cubic[0].y, t);
230}
231
232// OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t?
233void dxdy_at_t(const Cubic& cubic, double t, _Point& dxdy) {
234 dxdy.x = derivativeAtT(&cubic[0].x, t);
235 dxdy.y = derivativeAtT(&cubic[0].y, t);
236}
237
caryclark@google.com73ca6242013-01-17 21:02:47 +0000238int find_cubic_inflections(const Cubic& src, double tValues[])
239{
240 double Ax = src[1].x - src[0].x;
241 double Ay = src[1].y - src[0].y;
242 double Bx = src[2].x - 2 * src[1].x + src[0].x;
243 double By = src[2].y - 2 * src[1].y + src[0].y;
244 double Cx = src[3].x + 3 * (src[1].x - src[2].x) - src[0].x;
245 double Cy = src[3].y + 3 * (src[1].y - src[2].y) - src[0].y;
caryclark@google.com9f602912013-01-24 21:47:16 +0000246 return quadraticRootsValidT(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx), Ax * By - Ay * Bx, tValues);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000247}
248
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000249bool rotate(const Cubic& cubic, int zero, int index, Cubic& rotPath) {
250 double dy = cubic[index].y - cubic[zero].y;
251 double dx = cubic[index].x - cubic[zero].x;
caryclark@google.com9f602912013-01-24 21:47:16 +0000252 if (approximately_zero(dy)) {
253 if (approximately_zero(dx)) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000254 return false;
255 }
256 memcpy(rotPath, cubic, sizeof(Cubic));
257 return true;
258 }
259 for (int index = 0; index < 4; ++index) {
260 rotPath[index].x = cubic[index].x * dx + cubic[index].y * dy;
261 rotPath[index].y = cubic[index].y * dx - cubic[index].x * dy;
262 }
263 return true;
264}
265
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000266#if 0 // unused for now
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000267double secondDerivativeAtT(const double* cubic, double t) {
268 double a = cubic[0];
269 double b = cubic[2];
270 double c = cubic[4];
271 double d = cubic[6];
272 return (c - 2 * b + a) * (1 - t) + (d - 2 * c + b) * t;
273}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000274#endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000275
276void xy_at_t(const Cubic& cubic, double t, double& x, double& y) {
277 double one_t = 1 - t;
278 double one_t2 = one_t * one_t;
279 double a = one_t2 * one_t;
280 double b = 3 * one_t2 * t;
281 double t2 = t * t;
282 double c = 3 * one_t * t2;
283 double d = t2 * t;
284 if (&x) {
285 x = a * cubic[0].x + b * cubic[1].x + c * cubic[2].x + d * cubic[3].x;
286 }
287 if (&y) {
288 y = a * cubic[0].y + b * cubic[1].y + c * cubic[2].y + d * cubic[3].y;
289 }
290}