blob: 958cabb316eb90867a6fcd8dc7437308df8561ce [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
10void coefficients(const double* cubic, double& A, double& B, double& C, double& D) {
11 A = cubic[6]; // d
12 B = cubic[4] * 3; // 3*c
13 C = cubic[2] * 3; // 3*b
14 D = cubic[0]; // a
15 A -= D - C + B; // A = -a + 3*b - 3*c + d
16 B += 3 * D - 2 * C; // B = 3*a - 6*b + 3*c
17 C -= 3 * D; // C = -3*a + 3*b
18}
19
20// cubic roots
21
22const double PI = 4 * atan(1);
23
24static bool is_unit_interval(double x) {
25 return x > 0 && x < 1;
26}
27
28// from SkGeometry.cpp (and Numeric Solutions, 5.6)
29int cubicRoots(double A, double B, double C, double D, double t[3]) {
30 if (approximately_zero(A)) { // we're just a quadratic
31 return quadraticRoots(B, C, D, t);
32 }
33 double a, b, c;
34 {
35 double invA = 1 / A;
36 a = B * invA;
37 b = C * invA;
38 c = D * invA;
39 }
40 double a2 = a * a;
41 double Q = (a2 - b * 3) / 9;
42 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
43 double Q3 = Q * Q * Q;
44 double R2MinusQ3 = R * R - Q3;
45 double adiv3 = a / 3;
46 double* roots = t;
47 double r;
48
49 if (R2MinusQ3 < 0) // we have 3 real roots
50 {
51 double theta = acos(R / sqrt(Q3));
52 double neg2RootQ = -2 * sqrt(Q);
53
54 r = neg2RootQ * cos(theta / 3) - adiv3;
55 if (is_unit_interval(r))
56 *roots++ = r;
57
58 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
59 if (is_unit_interval(r))
60 *roots++ = r;
61
62 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
63 if (is_unit_interval(r))
64 *roots++ = r;
65 }
66 else // we have 1 real root
67 {
68 double A = fabs(R) + sqrt(R2MinusQ3);
69 A = cube_root(A);
70 if (R > 0) {
71 A = -A;
72 }
73 if (A != 0) {
74 A += Q / A;
75 }
76 r = A - adiv3;
77 if (is_unit_interval(r))
78 *roots++ = r;
79 }
80 return (int)(roots - t);
81}
caryclark@google.com8dcf1142012-07-02 20:27:02 +000082
83// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
84// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
85// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
86// = 3(b-a)(1-t)^2 + 6(c-b)t(1-t) + 3(d-c)t^2
87double derivativeAtT(const double* cubic, double t) {
88 double one_t = 1 - t;
89 double a = cubic[0];
90 double b = cubic[2];
91 double c = cubic[4];
92 double d = cubic[6];
93 return (b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t;
94}
95
96// same as derivativeAtT
97// which is more accurate? which is faster?
98double derivativeAtT_2(const double* cubic, double t) {
99 double a = cubic[2] - cubic[0];
100 double b = cubic[4] - 2 * cubic[2] + cubic[0];
101 double c = cubic[6] + 3 * (cubic[2] - cubic[4]) - cubic[0];
102 return c * c * t * t + 2 * b * t + a;
103}
104
105void dxdy_at_t(const Cubic& cubic, double t, double& dx, double& dy) {
106 if (&dx) {
107 dx = derivativeAtT(&cubic[0].x, t);
108 }
109 if (&dy) {
110 dy = derivativeAtT(&cubic[0].y, t);
111 }
112}
113
114bool rotate(const Cubic& cubic, int zero, int index, Cubic& rotPath) {
115 double dy = cubic[index].y - cubic[zero].y;
116 double dx = cubic[index].x - cubic[zero].x;
117 if (approximately_equal(dy, 0)) {
118 if (approximately_equal(dx, 0)) {
119 return false;
120 }
121 memcpy(rotPath, cubic, sizeof(Cubic));
122 return true;
123 }
124 for (int index = 0; index < 4; ++index) {
125 rotPath[index].x = cubic[index].x * dx + cubic[index].y * dy;
126 rotPath[index].y = cubic[index].y * dx - cubic[index].x * dy;
127 }
128 return true;
129}
130
131double secondDerivativeAtT(const double* cubic, double t) {
132 double a = cubic[0];
133 double b = cubic[2];
134 double c = cubic[4];
135 double d = cubic[6];
136 return (c - 2 * b + a) * (1 - t) + (d - 2 * c + b) * t;
137}
138
139void xy_at_t(const Cubic& cubic, double t, double& x, double& y) {
140 double one_t = 1 - t;
141 double one_t2 = one_t * one_t;
142 double a = one_t2 * one_t;
143 double b = 3 * one_t2 * t;
144 double t2 = t * t;
145 double c = 3 * one_t * t2;
146 double d = t2 * t;
147 if (&x) {
148 x = a * cubic[0].x + b * cubic[1].x + c * cubic[2].x + d * cubic[3].x;
149 }
150 if (&y) {
151 y = a * cubic[0].y + b * cubic[1].y + c * cubic[2].y + d * cubic[3].y;
152 }
153}