2 bezier.cc -- implement Bezier and Bezier_bow
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
11 #include "libc-extension.hh"
13 Real binomial_coefficient_3[] = {
18 binomial_coefficient (Real over, int under)
24 x *= over / Real (under);
33 scale (vector<Offset> *array, Real x, Real y)
35 for (vsize i = 0; i < array->size (); i++)
37 (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
38 (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
43 rotate (vector<Offset> *array, Real phi)
45 Offset rot (complex_exp (Offset (0, phi)));
46 for (vsize i = 0; i < array->size (); i++)
47 (*array)[i] = complex_multiply (rot, (*array)[i]);
51 translate (vector<Offset> *array, Offset o)
53 for (vsize i = 0; i < array->size (); i++)
58 Formula of the bezier 3-spline
60 sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j) t^j
63 A is the axis of X coordinate.
67 Bezier::get_other_coordinate (Axis a, Real x) const
69 Axis other = Axis ((a +1) % NO_AXES);
70 vector<Real> ts = solve_point (a, x);
74 programming_error ("no solution found for Bezier intersection");
79 Offset c = curve_point (ts[0]);
80 if (fabs (c[a] - x) > 1e-8)
81 programming_error ("bezier intersection not correct?");
84 return curve_coordinate (ts[0], other);
88 Bezier::curve_coordinate (Real t, Axis a) const
93 for (int i = 1; i < 4; i++)
94 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
97 for (int j = 0; j < 4; j++)
99 r += control_[j][a] * binomial_coefficient_3[j]
100 * tj * one_min_tj[3 - j];
109 Bezier::curve_point (Real t) const
114 for (int i = 1; i < 4; i++)
115 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
118 for (int j = 0; j < 4; j++)
120 o += control_[j] * binomial_coefficient_3[j]
121 * tj * one_min_tj[3 - j];
127 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
128 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
135 Cache binom(3,j) t^j (1-t)^{3-j}
137 struct Polynomial_cache {
138 Polynomial terms_[4];
141 for (int j = 0; j <= 3; j++)
143 = binomial_coefficient_3[j]
144 * Polynomial::power (j, Polynomial (0, 1))
145 * Polynomial::power (3 - j, Polynomial (1, -1));
149 static Polynomial_cache poly_cache;
152 Bezier::polynomial (Axis a) const
156 for (int j = 0; j <= 3; j++)
158 q = poly_cache.terms_[j];
167 Remove all numbers outside [0, 1] from SOL
170 filter_solutions (vector<Real> sol)
172 for (vsize i = sol.size (); i--;)
173 if (sol[i] < 0 || sol[i] > 1)
174 sol.erase (sol.begin () + i);
179 find t such that derivative is proportional to DERIV
182 Bezier::solve_derivative (Offset deriv) const
184 Polynomial xp = polynomial (X_AXIS);
185 Polynomial yp = polynomial (Y_AXIS);
189 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
191 return filter_solutions (combine.solve ());
195 Find t such that curve_point (t)[AX] == COORDINATE
198 Bezier::solve_point (Axis ax, Real coordinate) const
200 Polynomial p (polynomial (ax));
201 p.coefs_[0] -= coordinate;
203 vector<Real> sol (p.solve ());
204 return filter_solutions (sol);
208 Compute the bounding box dimensions in direction of A.
211 Bezier::extent (Axis a) const
213 int o = (a + 1)%NO_AXES;
217 vector<Real> sols (solve_derivative (d));
218 sols.push_back (1.0);
219 sols.push_back (0.0);
220 for (vsize i = sols.size (); i--;)
222 Offset o (curve_point (sols[i]));
223 iv.unite (Interval (o[a], o[a]));
229 Bezier::control_point_extent (Axis a) const
232 for (int i = CONTROL_COUNT; i--;)
233 ext.add_point (control_[i][a]);
243 Bezier::scale (Real x, Real y)
245 for (int i = CONTROL_COUNT; i--;)
247 control_[i][X_AXIS] = x * control_[i][X_AXIS];
248 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
253 Bezier::rotate (Real phi)
255 Offset rot (complex_exp (Offset (0, phi)));
256 for (int i = 0; i < CONTROL_COUNT; i++)
257 control_[i] = complex_multiply (rot, control_[i]);
261 Bezier::translate (Offset o)
263 for (int i = 0; i < CONTROL_COUNT; i++)
268 Bezier::assert_sanity () const
270 for (int i = 0; i < CONTROL_COUNT; i++)
271 assert (!isnan (control_[i].length ())
272 && !isinf (control_[i].length ()));
279 for (int i = 0; i < CONTROL_COUNT; i++)
280 b2.control_[CONTROL_COUNT - i - 1] = control_[i];