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[] = {1,3 ,3, 1};
18 binomial_coefficient (Real over, int under)
24 x *= over / Real (under);
33 scale (Array<Offset> *array, Real x, Real y)
35 for (int 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 (Array<Offset> *array, Real phi)
45 Offset rot (complex_exp (Offset (0, phi)));
46 for (int i = 0; i < array->size (); i++)
47 (*array)[i] = complex_multiply (rot, (*array)[i]);
51 translate (Array<Offset> *array, Offset o)
53 for (int 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 Array<Real> ts = solve_point (a, x);
74 programming_error ("no solution found for Bezier intersection");
78 Offset c = curve_point (ts[0]);
81 if (fabs (c[a] - x) > 1e-8)
82 programming_error ("bezier intersection not correct?");
89 Bezier::curve_point (Real t) const
94 for (int i = 1; i < 4; i++)
96 one_min_tj[i] = one_min_tj[i-1] * (1-t);
100 for (int j = 0; j < 4; j++)
102 o += control_[j] * binomial_coefficient_3[j]
103 * tj * one_min_tj[3-j];
109 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
110 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
117 Cache binom(3,j) t^j (1-t)^{3-j}
119 static struct Polynomial bezier_term_cache[4];
120 static bool done_cache_init;
123 init_polynomial_cache ()
125 for (int j = 0; j <= 3; j++)
126 bezier_term_cache[j] =
127 binomial_coefficient_3[j]
128 * Polynomial::power (j, Polynomial (0, 1))
129 * Polynomial::power (3 - j, Polynomial (1, -1));
130 done_cache_init = true;
134 Bezier::polynomial (Axis a) const
136 if (!done_cache_init)
137 init_polynomial_cache ();
141 for (int j = 0; j <= 3; j++)
143 q = bezier_term_cache[j];
152 Remove all numbers outside [0, 1] from SOL
155 filter_solutions (Array<Real> sol)
157 for (int i = sol.size (); i--;)
158 if (sol[i] < 0 || sol[i] > 1)
164 find t such that derivative is proportional to DERIV
167 Bezier::solve_derivative (Offset deriv) const
169 Polynomial xp = polynomial (X_AXIS);
170 Polynomial yp = polynomial (Y_AXIS);
174 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
176 return filter_solutions (combine.solve ());
180 Find t such that curve_point (t)[AX] == COORDINATE
183 Bezier::solve_point (Axis ax, Real coordinate) const
185 Polynomial p (polynomial (ax));
186 p.coefs_[0] -= coordinate;
188 Array<Real> sol (p.solve ());
189 return filter_solutions (sol);
193 Compute the bounding box dimensions in direction of A.
196 Bezier::extent (Axis a) const
198 int o = (a + 1)%NO_AXES;
202 Array<Real> sols (solve_derivative (d));
205 for (int i = sols.size (); i--;)
207 Offset o (curve_point (sols[i]));
208 iv.unite (Interval (o[a], o[a]));
217 Bezier::scale (Real x, Real y)
219 for (int i = CONTROL_COUNT; i--;)
221 control_[i][X_AXIS] = x * control_[i][X_AXIS];
222 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
227 Bezier::rotate (Real phi)
229 Offset rot (complex_exp (Offset (0, phi)));
230 for (int i = 0; i < CONTROL_COUNT; i++)
231 control_[i] = complex_multiply (rot, control_[i]);
235 Bezier::translate (Offset o)
237 for (int i = 0; i < CONTROL_COUNT; i++)
242 Bezier::assert_sanity () const
244 for (int i = 0; i < CONTROL_COUNT; i++)
245 assert (!isnan (control_[i].length ())
246 && !isinf (control_[i].length ()));
253 for (int i = 0; i < CONTROL_COUNT; i++)
254 b2.control_[CONTROL_COUNT - i - 1] = control_[i];