2 bezier.cc -- implement Bezier and Bezier_bow
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "libc-extension.hh"
15 Real binomial_coefficient_3[] = {
20 binomial_coefficient (Real over, int under)
26 x *= over / Real (under);
35 scale (Array<Offset> *array, Real x, Real y)
37 for (int i = 0; i < array->size (); i++)
39 (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
40 (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
45 rotate (Array<Offset> *array, Real phi)
47 Offset rot (complex_exp (Offset (0, phi)));
48 for (int i = 0; i < array->size (); i++)
49 (*array)[i] = complex_multiply (rot, (*array)[i]);
53 translate (Array<Offset> *array, Offset o)
55 for (int i = 0; i < array->size (); i++)
60 Formula of the bezier 3-spline
62 sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j) t^j
65 A is the axis of X coordinate.
69 Bezier::get_other_coordinate (Axis a, Real x) const
71 Axis other = Axis ((a +1)%NO_AXES);
72 Array<Real> ts = solve_point (a, x);
76 programming_error ("no solution found for Bezier intersection");
81 Offset c = curve_point (ts[0]);
82 if (fabs (c[a] - x) > 1e-8)
83 programming_error ("bezier intersection not correct?");
86 return curve_coordinate (ts[0], other);
90 Bezier::curve_coordinate (Real t, Axis a) const
95 for (int i = 1; i < 4; i++)
96 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
99 for (int j = 0; j < 4; j++)
101 r += control_[j][a] * binomial_coefficient_3[j]
102 * tj * one_min_tj[3 - j];
111 Bezier::curve_point (Real t) const
116 for (int i = 1; i < 4; i++)
117 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
120 for (int j = 0; j < 4; j++)
122 o += control_[j] * binomial_coefficient_3[j]
123 * tj * one_min_tj[3 - j];
129 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
130 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
137 Cache binom(3,j) t^j (1-t)^{3-j}
139 static struct Polynomial bezier_term_cache[4];
140 static bool done_cache_init;
143 init_polynomial_cache ()
145 for (int j = 0; j <= 3; j++)
147 = binomial_coefficient_3[j]
148 * Polynomial::power (j, Polynomial (0, 1))
149 * Polynomial::power (3 - j, Polynomial (1, -1));
150 done_cache_init = true;
154 Bezier::polynomial (Axis a) const
156 if (!done_cache_init)
157 init_polynomial_cache ();
161 for (int j = 0; j <= 3; j++)
163 q = bezier_term_cache[j];
172 Remove all numbers outside [0, 1] from SOL
175 filter_solutions (Array<Real> sol)
177 for (int i = sol.size (); i--;)
178 if (sol[i] < 0 || sol[i] > 1)
184 find t such that derivative is proportional to DERIV
187 Bezier::solve_derivative (Offset deriv) const
189 Polynomial xp = polynomial (X_AXIS);
190 Polynomial yp = polynomial (Y_AXIS);
194 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
196 return filter_solutions (combine.solve ());
200 Find t such that curve_point (t)[AX] == COORDINATE
203 Bezier::solve_point (Axis ax, Real coordinate) const
205 Polynomial p (polynomial (ax));
206 p.coefs_[0] -= coordinate;
208 Array<Real> sol (p.solve ());
209 return filter_solutions (sol);
213 Compute the bounding box dimensions in direction of A.
216 Bezier::extent (Axis a) const
218 int o = (a + 1)%NO_AXES;
222 Array<Real> sols (solve_derivative (d));
225 for (int i = sols.size (); i--;)
227 Offset o (curve_point (sols[i]));
228 iv.unite (Interval (o[a], o[a]));
237 Bezier::scale (Real x, Real y)
239 for (int i = CONTROL_COUNT; i--;)
241 control_[i][X_AXIS] = x * control_[i][X_AXIS];
242 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
247 Bezier::rotate (Real phi)
249 Offset rot (complex_exp (Offset (0, phi)));
250 for (int i = 0; i < CONTROL_COUNT; i++)
251 control_[i] = complex_multiply (rot, control_[i]);
255 Bezier::translate (Offset o)
257 for (int i = 0; i < CONTROL_COUNT; i++)
262 Bezier::assert_sanity () const
264 for (int i = 0; i < CONTROL_COUNT; i++)
265 assert (!isnan (control_[i].length ())
266 && !isinf (control_[i].length ()));
273 for (int i = 0; i < CONTROL_COUNT; i++)
274 b2.control_[CONTROL_COUNT - i - 1] = control_[i];