2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "libc-extension.hh"
24 Real binomial_coefficient_3[]
31 scale (vector<Offset> *array, Real x, Real y)
33 for (vsize i = 0; i < array->size (); i++)
35 (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
36 (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
41 rotate (vector<Offset> *array, Real phi)
43 Offset rot (complex_exp (Offset (0, phi)));
44 for (vsize i = 0; i < array->size (); i++)
45 (*array)[i] = complex_multiply (rot, (*array)[i]);
49 translate (vector<Offset> *array, Offset o)
51 for (vsize i = 0; i < array->size (); i++)
56 Formula of the bezier 3-spline
58 sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j) t^j
61 A is the axis of X coordinate.
65 Bezier::get_other_coordinate (Axis a, Real x) const
67 Axis other = Axis ((a + 1) % NO_AXES);
68 vector<Real> ts = solve_point (a, x);
72 programming_error ("no solution found for Bezier intersection");
77 Offset c = curve_point (ts[0]);
78 if (fabs (c[a] - x) > 1e-8)
79 programming_error ("bezier intersection not correct?");
82 return curve_coordinate (ts[0], other);
86 Bezier::get_other_coordinates (Axis a, Real x) const
88 Axis other = other_axis (a);
89 vector<Real> ts = solve_point (a, x);
91 for (vsize i = 0; i < ts.size (); i++)
92 sols.push_back (curve_coordinate (ts[i], other));
97 Bezier::curve_coordinate (Real t, Axis a) const
102 for (int i = 1; i < 4; i++)
103 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
106 for (int j = 0; j < 4; j++)
108 r += control_[j][a] * binomial_coefficient_3[j]
109 * tj * one_min_tj[3 - j];
118 Bezier::curve_point (Real t) const
123 for (int i = 1; i < 4; i++)
124 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
127 for (int j = 0; j < 4; j++)
129 o += control_[j] * binomial_coefficient_3[j]
130 * tj * one_min_tj[3 - j];
136 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
137 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
144 Cache binom (3, j) t^j (1-t)^{3-j}
146 struct Polynomial_cache
148 Polynomial terms_[4];
151 for (int j = 0; j <= 3; j++)
153 = binomial_coefficient_3[j]
154 * Polynomial::power (j, Polynomial (0, 1))
155 * Polynomial::power (3 - j, Polynomial (1, -1));
159 static Polynomial_cache poly_cache;
162 Bezier::polynomial (Axis a) const
166 for (int j = 0; j <= 3; j++)
168 q = poly_cache.terms_[j];
177 Remove all numbers outside [0, 1] from SOL
180 filter_solutions (vector<Real> sol)
182 for (vsize i = sol.size (); i--;)
183 if (sol[i] < 0 || sol[i] > 1)
184 sol.erase (sol.begin () + i);
189 find t such that derivative is proportional to DERIV
192 Bezier::solve_derivative (Offset deriv) const
194 Polynomial xp = polynomial (X_AXIS);
195 Polynomial yp = polynomial (Y_AXIS);
199 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
201 return filter_solutions (combine.solve ());
205 Find t such that curve_point (t)[AX] == COORDINATE
208 Bezier::solve_point (Axis ax, Real coordinate) const
210 Polynomial p (polynomial (ax));
211 p.coefs_[0] -= coordinate;
213 vector<Real> sol (p.solve ());
214 return filter_solutions (sol);
218 For the portion of the curve between L and R along axis AX,
219 return the bounding box limit in direction D along the cross axis to AX.
220 If there is no portion between L and R, return 0.0 and report error.
223 Bezier::minmax (Axis ax, Real l, Real r, Direction d) const
225 Axis bx = other_axis (ax);
227 // The curve could hit its bounding box limit along BX at:
228 // points where the curve is parallel to AX,
229 Offset vec (0.0, 0.0);
231 vector<Real> sols (solve_derivative (vec));
232 // or endpoints of the curve,
233 sols.push_back (0.999);
234 sols.push_back (0.001);
235 // (using points just inside the ends, so that an endpoint is evaulated
236 // if it falls within rounding error of L or R and the curve lies inside)
239 for (vsize i = sols.size (); i--;)
241 Offset p (curve_point (sols[i]));
242 if (p[ax] >= l && p[ax] <= r)
243 iv.add_point (p[bx]);
246 // or intersections of the curve with the bounding lines at L and R.
248 for (LEFT_and_RIGHT (dir))
250 vector<Real> v = get_other_coordinates (ax, lr[dir]);
251 for (vsize i = v.size (); i--;)
257 programming_error ("Bezier curve does not cross region of concern");
265 Compute the bounding box dimensions in direction of A.
268 Bezier::extent (Axis a) const
270 int o = (a + 1) % NO_AXES;
274 vector<Real> sols (solve_derivative (d));
275 sols.push_back (1.0);
276 sols.push_back (0.0);
277 for (vsize i = sols.size (); i--;)
279 Offset o (curve_point (sols[i]));
280 iv.unite (Interval (o[a], o[a]));
286 Bezier::control_point_extent (Axis a) const
289 for (int i = CONTROL_COUNT; i--;)
290 ext.add_point (control_[i][a]);
299 Bezier::scale (Real x, Real y)
301 for (int i = CONTROL_COUNT; i--;)
303 control_[i][X_AXIS] = x * control_[i][X_AXIS];
304 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
309 Bezier::rotate (Real phi)
311 Offset rot (complex_exp (Offset (0, phi)));
312 for (int i = 0; i < CONTROL_COUNT; i++)
313 control_[i] = complex_multiply (rot, control_[i]);
317 Bezier::translate (Offset o)
319 for (int i = 0; i < CONTROL_COUNT; i++)
324 Bezier::assert_sanity () const
326 for (int i = 0; i < CONTROL_COUNT; i++)
327 assert (!isnan (control_[i].length ())
328 && !isinf (control_[i].length ()));
335 for (int i = 0; i < CONTROL_COUNT; i++)
336 b2.control_[CONTROL_COUNT - i - 1] = control_[i];
341 Subdivide a bezier at T into LEFT_PART and RIGHT_PART
342 using deCasteljau's algorithm.
345 Bezier::subdivide (Real t, Bezier *left_part, Bezier *right_part) const
347 Offset p[CONTROL_COUNT][CONTROL_COUNT];
349 for (int i = 0; i < CONTROL_COUNT; i++)
350 p[i][CONTROL_COUNT - 1 ] = control_[i];
351 for (int j = CONTROL_COUNT - 2; j >= 0; j--)
352 for (int i = 0; i < CONTROL_COUNT - 1; i++)
353 p[i][j] = p[i][j + 1] + t * (p[i + 1][j + 1] - p[i][j + 1]);
354 for (int i = 0; i < CONTROL_COUNT; i++)
356 left_part->control_[i] = p[0][CONTROL_COUNT - 1 - i];
357 right_part->control_[i] = p[i][i];
362 Extract a portion of a bezier from T_MIN to T_MAX
366 Bezier::extract (Real t_min, Real t_max) const
368 if ((t_min < 0) || (t_max) > 1)
370 ("bezier extract arguments outside of limits: curve may have bad shape");
373 ("lower bezier extract value not less than upper value: curve may have bad shape");
374 Bezier bez1, bez2, bez3, bez4;
378 subdivide (t_min, &bez1, &bez2);
383 bez2.subdivide ((t_max - t_min) / (1 - t_min), &bez3, &bez4);