Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
solve-bezier-one-d.cpp
Go to the documentation of this file.
1#include <2geom/solver.h>
2#include <2geom/choose.h>
3#include <2geom/bezier.h>
4#include <2geom/point.h>
5
6#include <cmath>
7#include <algorithm>
8
9/*** Find the zeros of the bernstein function. The code subdivides until it is happy with the
10 * linearity of the function. This requires an O(degree^2) subdivision for each step, even when
11 * there is only one solution.
12 */
13
14namespace Geom {
15namespace {
16
20struct Bernsteins
21{
22 static constexpr size_t MAX_DEPTH = 53;
23 size_t degree, N;
24 std::vector<double> &solutions;
25
26 Bernsteins(size_t _degree, std::vector<double> &sol)
27 : degree{_degree}
28 , N{degree + 1}
29 , solutions{sol}
30 {}
31
32 void
33 find_bernstein_roots(double const *w, /* The control points */
34 unsigned depth, /* The depth of the recursion */
35 double left_t, double right_t);
36};
37
38} // namespace
39
40/*
41 * find_bernstein_roots : Given an equation in Bernstein-Bernstein form, find all
42 * of the roots in the open interval (0, 1). Return the number of roots found.
43 */
44void
45find_bernstein_roots(double const *w, /* The control points */
46 unsigned degree, /* The degree of the polynomial */
47 std::vector<double> &solutions, /* RETURN candidate t-values */
48 unsigned depth, /* The depth of the recursion */
49 double left_t, double right_t, bool /*use_secant*/)
50{
51 Bernsteins B(degree, solutions);
52 B.find_bernstein_roots(w, depth, left_t, right_t);
53}
54
55void
56find_bernstein_roots(std::vector<double> &solutions, /* RETURN candidate t-values */
57 Geom::Bezier const &bz, /* The control points */
58 double left_t, double right_t)
59{
60 Bernsteins B(bz.degree(), solutions);
61 Geom::Bezier& bzl = const_cast<Geom::Bezier&>(bz);
62 double* w = &(bzl[0]);
63 B.find_bernstein_roots(w, 0, left_t, right_t);
64}
65
66void Bernsteins::find_bernstein_roots(double const *w, /* The control points */
67 unsigned depth, /* The depth of the recursion */
68 double left_t,
69 double right_t)
70{
71 size_t n_crossings = 0;
72
73 int old_sign = Geom::sgn(w[0]);
74 //std::cout << "w[0] = " << w[0] << std::endl;
75 for (size_t i = 1; i < N; i++)
76 {
77 //std::cout << "w[" << i << "] = " << w[i] << std::endl;
78 int sign = Geom::sgn(w[i]);
79 if (sign != 0)
80 {
81 if (sign != old_sign && old_sign != 0)
82 {
83 ++n_crossings;
84 }
85 old_sign = sign;
86 }
87 }
88 //std::cout << "n_crossings = " << n_crossings << std::endl;
89 if (n_crossings == 0) return; // no solutions here
90
91 if (n_crossings == 1) /* Unique solution */
92 {
93 //std::cout << "depth = " << depth << std::endl;
94 /* Stop recursion when the tree is deep enough */
95 /* if deep enough, return 1 solution at midpoint */
96 if (depth > MAX_DEPTH)
97 {
98 //printf("bottom out %d\n", depth);
99 const double Ax = right_t - left_t;
100 const double Ay = w[degree] - w[0];
101
102 solutions.push_back(left_t - Ax*w[0] / Ay);
103 return;
104 }
105
106
107 double s = 0, t = 1;
108 double e = 1e-10;
109 int side = 0;
110 double r, fs = w[0], ft = w[degree];
111
112 for (size_t n = 0; n < 100; ++n)
113 {
114 r = (fs*t - ft*s) / (fs - ft);
115 if (fabs(t-s) < e * fabs(t+s)) break;
116
117 double fr = bernstein_value_at(r, w, degree);
118
119 if (fr * ft > 0)
120 {
121 t = r; ft = fr;
122 if (side == -1) fs /= 2;
123 side = -1;
124 }
125 else if (fs * fr > 0)
126 {
127 s = r; fs = fr;
128 if (side == +1) ft /= 2;
129 side = +1;
130 }
131 else break;
132 }
133 solutions.push_back(r*right_t + (1-r)*left_t);
134 return;
135
136 }
137
138 /* Otherwise, solve recursively after subdividing control polygon */
139 double* LR = new double[2*N];
140 double* Left = LR;
141 double* Right = LR + N;
142
143 std::copy(w, w + N, Right);
144
145 Left[0] = Right[0];
146 for (size_t i = 1; i < N; ++i)
147 {
148 for (size_t j = 0; j < N-i; ++j)
149 {
150 Right[j] = (Right[j] + Right[j+1]) * 0.5;
151 }
152 Left[i] = Right[0];
153 }
154
155 double mid_t = (left_t + right_t) * 0.5;
156
157
158 find_bernstein_roots(Left, depth+1, left_t, mid_t);
159
160
161 /* Solution is exactly on the subdivision point. */
162 if (Right[0] == 0)
163 {
164 solutions.push_back(mid_t);
165 }
166
167 find_bernstein_roots(Right, depth+1, mid_t, right_t);
168 delete[] LR;
169}
170
171} // namespace Geom
172
173/*
174 Local Variables:
175 mode:c++
176 c-file-style:"stroustrup"
177 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
178 indent-tabs-mode:nil
179 fill-column:99
180 End:
181*/
182// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
Bernstein-Bezier polynomial.
Calculation of binomial cefficients.
Polynomial in Bernstein-Bezier basis.
Definition bezier.h:126
unsigned degree() const
Definition bezier.h:146
const double w
Definition conic-4.cpp:19
Various utility functions.
Definition affine.h:22
int sgn(const T &x)
Sign function - indicates the sign of a numeric type.
Definition math-utils.h:51
static float sign(double number)
Returns +1 for positive numbers, -1 for negative numbers, and 0 otherwise.
void find_bernstein_roots(double const *w, unsigned degree, std::vector< double > &solutions, unsigned depth, double left_t=0, double right_t=1, bool use_secant=true)
T bernstein_value_at(double t, T const *c_, unsigned n)
Compute the value of a Bernstein-Bezier polynomial.
Definition bezier.h:55
int n
Definition spiro.cpp:57
size_t degree
size_t N
std::vector< double > & solutions
static constexpr size_t MAX_DEPTH
Finding roots of Bernstein-Bezier polynomials.