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 static struct Polynomial bezier_term_cache[4];
138 static bool done_cache_init;
141 init_polynomial_cache ()
143 for (int j = 0; j <= 3; j++)
145 = binomial_coefficient_3[j]
146 * Polynomial::power (j, Polynomial (0, 1))
147 * Polynomial::power (3 - j, Polynomial (1, -1));
148 done_cache_init = true;
152 Bezier::polynomial (Axis a) const
154 if (!done_cache_init)
155 init_polynomial_cache ();
159 for (int j = 0; j <= 3; j++)
161 q = bezier_term_cache[j];
170 Remove all numbers outside [0, 1] from SOL
173 filter_solutions (vector<Real> sol)
175 for (vsize i = sol.size (); i--;)
176 if (sol[i] < 0 || sol[i] > 1)
177 sol.erase (sol.begin () + i);
182 find t such that derivative is proportional to DERIV
185 Bezier::solve_derivative (Offset deriv) const
187 Polynomial xp = polynomial (X_AXIS);
188 Polynomial yp = polynomial (Y_AXIS);
192 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
194 return filter_solutions (combine.solve ());
198 Find t such that curve_point (t)[AX] == COORDINATE
201 Bezier::solve_point (Axis ax, Real coordinate) const
203 Polynomial p (polynomial (ax));
204 p.coefs_[0] -= coordinate;
206 vector<Real> sol (p.solve ());
207 return filter_solutions (sol);
211 Compute the bounding box dimensions in direction of A.
214 Bezier::extent (Axis a) const
216 int o = (a + 1)%NO_AXES;
220 vector<Real> sols (solve_derivative (d));
221 sols.push_back (1.0);
222 sols.push_back (0.0);
223 for (vsize i = sols.size (); i--;)
225 Offset o (curve_point (sols[i]));
226 iv.unite (Interval (o[a], o[a]));
232 Bezier::control_point_extent (Axis a) const
235 for (int i = CONTROL_COUNT; i--;)
236 ext.add_point (control_[i][a]);
246 Bezier::scale (Real x, Real y)
248 for (int i = CONTROL_COUNT; i--;)
250 control_[i][X_AXIS] = x * control_[i][X_AXIS];
251 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
256 Bezier::rotate (Real phi)
258 Offset rot (complex_exp (Offset (0, phi)));
259 for (int i = 0; i < CONTROL_COUNT; i++)
260 control_[i] = complex_multiply (rot, control_[i]);
264 Bezier::translate (Offset o)
266 for (int i = 0; i < CONTROL_COUNT; i++)
271 Bezier::assert_sanity () const
273 for (int i = 0; i < CONTROL_COUNT; i++)
274 assert (!isnan (control_[i].length ())
275 && !isinf (control_[i].length ()));
282 for (int i = 0; i < CONTROL_COUNT; i++)
283 b2.control_[CONTROL_COUNT - i - 1] = control_[i];