blob: 19f16c6aaafa49e420b7fcb7375ca8a1ca2ab47e [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;
24 sub_divide(cubic, SkMax32(0, t - scale), SkMin32(1, t + scale), part);
25 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
44static bool is_unit_interval(double x) {
45 return x > 0 && x < 1;
46}
47
48// from SkGeometry.cpp (and Numeric Solutions, 5.6)
49int cubicRoots(double A, double B, double C, double D, double t[3]) {
50 if (approximately_zero(A)) { // we're just a quadratic
51 return quadraticRoots(B, C, D, t);
52 }
53 double a, b, c;
54 {
55 double invA = 1 / A;
56 a = B * invA;
57 b = C * invA;
58 c = D * invA;
59 }
60 double a2 = a * a;
61 double Q = (a2 - b * 3) / 9;
62 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
63 double Q3 = Q * Q * Q;
64 double R2MinusQ3 = R * R - Q3;
65 double adiv3 = a / 3;
66 double* roots = t;
67 double r;
68
69 if (R2MinusQ3 < 0) // we have 3 real roots
70 {
71 double theta = acos(R / sqrt(Q3));
72 double neg2RootQ = -2 * sqrt(Q);
73
74 r = neg2RootQ * cos(theta / 3) - adiv3;
75 if (is_unit_interval(r))
76 *roots++ = r;
77
78 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
79 if (is_unit_interval(r))
80 *roots++ = r;
81
82 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
83 if (is_unit_interval(r))
84 *roots++ = r;
85 }
86 else // we have 1 real root
87 {
88 double A = fabs(R) + sqrt(R2MinusQ3);
89 A = cube_root(A);
90 if (R > 0) {
91 A = -A;
92 }
93 if (A != 0) {
94 A += Q / A;
95 }
96 r = A - adiv3;
97 if (is_unit_interval(r))
98 *roots++ = r;
99 }
100 return (int)(roots - t);
101}
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000102
103// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
104// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
105// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
106// = 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 +0000107static double derivativeAtT(const double* cubic, double t) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000108 double one_t = 1 - t;
109 double a = cubic[0];
110 double b = cubic[2];
111 double c = cubic[4];
112 double d = cubic[6];
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000113 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 +0000114}
115
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000116double dx_at_t(const Cubic& cubic, double t) {
117 return derivativeAtT(&cubic[0].x, t);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000118}
119
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000120double dy_at_t(const Cubic& cubic, double t) {
121 return derivativeAtT(&cubic[0].y, t);
122}
123
124// OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t?
125void dxdy_at_t(const Cubic& cubic, double t, _Point& dxdy) {
126 dxdy.x = derivativeAtT(&cubic[0].x, t);
127 dxdy.y = derivativeAtT(&cubic[0].y, t);
128}
129
130
caryclark@google.com73ca6242013-01-17 21:02:47 +0000131int find_cubic_inflections(const Cubic& src, double tValues[])
132{
133 double Ax = src[1].x - src[0].x;
134 double Ay = src[1].y - src[0].y;
135 double Bx = src[2].x - 2 * src[1].x + src[0].x;
136 double By = src[2].y - 2 * src[1].y + src[0].y;
137 double Cx = src[3].x + 3 * (src[1].x - src[2].x) - src[0].x;
138 double Cy = src[3].y + 3 * (src[1].y - src[2].y) - src[0].y;
139 return quadraticRoots(Bx * Cy - By * Cx, (Ax * Cy - Ay * Cx) / 2, Ax * By - Ay * Bx, tValues);
140}
141
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000142bool rotate(const Cubic& cubic, int zero, int index, Cubic& rotPath) {
143 double dy = cubic[index].y - cubic[zero].y;
144 double dx = cubic[index].x - cubic[zero].x;
145 if (approximately_equal(dy, 0)) {
146 if (approximately_equal(dx, 0)) {
147 return false;
148 }
149 memcpy(rotPath, cubic, sizeof(Cubic));
150 return true;
151 }
152 for (int index = 0; index < 4; ++index) {
153 rotPath[index].x = cubic[index].x * dx + cubic[index].y * dy;
154 rotPath[index].y = cubic[index].y * dx - cubic[index].x * dy;
155 }
156 return true;
157}
158
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000159#if 0 // unused for now
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000160double secondDerivativeAtT(const double* cubic, double t) {
161 double a = cubic[0];
162 double b = cubic[2];
163 double c = cubic[4];
164 double d = cubic[6];
165 return (c - 2 * b + a) * (1 - t) + (d - 2 * c + b) * t;
166}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000167#endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000168
169void xy_at_t(const Cubic& cubic, double t, double& x, double& y) {
170 double one_t = 1 - t;
171 double one_t2 = one_t * one_t;
172 double a = one_t2 * one_t;
173 double b = 3 * one_t2 * t;
174 double t2 = t * t;
175 double c = 3 * one_t * t2;
176 double d = t2 * t;
177 if (&x) {
178 x = a * cubic[0].x + b * cubic[1].x + c * cubic[2].x + d * cubic[3].x;
179 }
180 if (&y) {
181 y = a * cubic[0].y + b * cubic[1].y + c * cubic[2].y + d * cubic[3].y;
182 }
183}