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