2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 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[] =
30 scale (vector<Offset> *array, Real x, Real y)
32 for (vsize i = 0; i < array->size (); i++)
34 (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
35 (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
40 rotate (vector<Offset> *array, Real phi)
42 Offset rot (complex_exp (Offset (0, phi)));
43 for (vsize i = 0; i < array->size (); i++)
44 (*array)[i] = complex_multiply (rot, (*array)[i]);
48 translate (vector<Offset> *array, Offset o)
50 for (vsize i = 0; i < array->size (); i++)
55 Formula of the bezier 3-spline
57 sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j) t^j
60 A is the axis of X coordinate.
64 Bezier::get_other_coordinate (Axis a, Real x) const
66 Axis other = Axis ((a + 1) % NO_AXES);
67 vector<Real> ts = solve_point (a, x);
71 programming_error ("no solution found for Bezier intersection");
76 Offset c = curve_point (ts[0]);
77 if (fabs (c[a] - x) > 1e-8)
78 programming_error ("bezier intersection not correct?");
81 return curve_coordinate (ts[0], other);
85 Bezier::get_other_coordinates (Axis a, Real x) const
87 Axis other = other_axis (a);
88 vector<Real> ts = solve_point (a, x);
90 for (vsize i = 0; i < ts.size (); i++)
91 sols.push_back (curve_coordinate (ts[i], other));
96 Bezier::curve_coordinate (Real t, Axis a) const
101 for (int i = 1; i < 4; i++)
102 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
105 for (int j = 0; j < 4; j++)
107 r += control_[j][a] * binomial_coefficient_3[j]
108 * tj * one_min_tj[3 - j];
117 Bezier::curve_point (Real t) const
122 for (int i = 1; i < 4; i++)
123 one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
126 for (int j = 0; j < 4; j++)
128 o += control_[j] * binomial_coefficient_3[j]
129 * tj * one_min_tj[3 - j];
135 assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
136 assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
143 Cache binom (3, j) t^j (1-t)^{3-j}
145 struct Polynomial_cache
147 Polynomial terms_[4];
150 for (int j = 0; j <= 3; j++)
152 = binomial_coefficient_3[j]
153 * Polynomial::power (j, Polynomial (0, 1))
154 * Polynomial::power (3 - j, Polynomial (1, -1));
158 static Polynomial_cache poly_cache;
161 Bezier::polynomial (Axis a) const
165 for (int j = 0; j <= 3; j++)
167 q = poly_cache.terms_[j];
176 Remove all numbers outside [0, 1] from SOL
179 filter_solutions (vector<Real> sol)
181 for (vsize i = sol.size (); i--;)
182 if (sol[i] < 0 || sol[i] > 1)
183 sol.erase (sol.begin () + i);
188 find t such that derivative is proportional to DERIV
191 Bezier::solve_derivative (Offset deriv) const
193 Polynomial xp = polynomial (X_AXIS);
194 Polynomial yp = polynomial (Y_AXIS);
198 Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
200 return filter_solutions (combine.solve ());
204 Find t such that curve_point (t)[AX] == COORDINATE
207 Bezier::solve_point (Axis ax, Real coordinate) const
209 Polynomial p (polynomial (ax));
210 p.coefs_[0] -= coordinate;
212 vector<Real> sol (p.solve ());
213 return filter_solutions (sol);
217 Assuming AX is X_AXIS, and D is UP, finds the
218 maximum value of curve_coordinate(t, Y_AXIS) subject to
219 l <= curve_coordinate(t, X_AXIS) <= r.
222 Bezier::minmax (Axis ax, Real l, Real r, Direction d) const
224 Axis other = other_axis (ax);
226 vector<Real> solutions;
228 // Possible solutions are:
230 solutions.push_back (0);
231 solutions.push_back (1);
233 // t is a critical point for the other-axis polynomial, or...
234 Polynomial p_prime (polynomial (other));
235 p_prime.differentiate ();
236 vector<Real> criticals = p_prime.solve ();
237 solutions.insert (solutions.end (), criticals.begin (), criticals.end ());
239 // t solves curve_coordinate(t, X_AXIS) = l or r.
240 Direction dir = LEFT;
243 Polynomial p (polynomial (ax));
244 p.coefs_[0] -= lr[dir];
246 vector<Real> sol = p.solve ();
247 solutions.insert (solutions.end (), sol.begin (), sol.end ());
249 while (flip (&dir) != LEFT);
251 Polynomial p (polynomial (ax));
252 Polynomial other_p (polynomial (other));
254 for (vsize i = solutions.size (); i--;)
256 Real t = solutions[i];
257 if (t >= 0 && t <= 1 && p.eval (t) >= l && p.eval (t) <= r)
258 values.push_back (other_p.eval (t));
263 programming_error ("no solution found for Bezier intersection");
267 vector_sort (values, less<Real> ());
268 return (d == DOWN) ? values[0] : values.back ();
272 Compute the bounding box dimensions in direction of A.
275 Bezier::extent (Axis a) const
277 int o = (a + 1) % NO_AXES;
281 vector<Real> sols (solve_derivative (d));
282 sols.push_back (1.0);
283 sols.push_back (0.0);
284 for (vsize i = sols.size (); i--;)
286 Offset o (curve_point (sols[i]));
287 iv.unite (Interval (o[a], o[a]));
293 Bezier::control_point_extent (Axis a) const
296 for (int i = CONTROL_COUNT; i--;)
297 ext.add_point (control_[i][a]);
306 Bezier::scale (Real x, Real y)
308 for (int i = CONTROL_COUNT; i--;)
310 control_[i][X_AXIS] = x * control_[i][X_AXIS];
311 control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
316 Bezier::rotate (Real phi)
318 Offset rot (complex_exp (Offset (0, phi)));
319 for (int i = 0; i < CONTROL_COUNT; i++)
320 control_[i] = complex_multiply (rot, control_[i]);
324 Bezier::translate (Offset o)
326 for (int i = 0; i < CONTROL_COUNT; i++)
331 Bezier::assert_sanity () const
333 for (int i = 0; i < CONTROL_COUNT; i++)
334 assert (!isnan (control_[i].length ())
335 && !isinf (control_[i].length ()));
342 for (int i = 0; i < CONTROL_COUNT; i++)
343 b2.control_[CONTROL_COUNT - i - 1] = control_[i];
348 Subdivide a bezier at T into LEFT_PART and RIGHT_PART
349 using deCasteljau's algorithm.
352 Bezier::subdivide (Real t, Bezier *left_part, Bezier *right_part) const
354 Offset p[CONTROL_COUNT][CONTROL_COUNT];
356 for (int i = 0; i < CONTROL_COUNT; i++)
357 p[i][CONTROL_COUNT - 1 ] = control_[i];
358 for (int j = CONTROL_COUNT - 2; j >= 0; j--)
359 for (int i = 0; i < CONTROL_COUNT - 1; i++)
360 p[i][j] = p[i][j + 1] + t * (p[i + 1][j + 1] - p[i][j + 1]);
361 for (int i = 0; i < CONTROL_COUNT; i++)
363 left_part->control_[i] = p[0][CONTROL_COUNT - 1 - i];
364 right_part->control_[i] = p[i][i];
369 Extract a portion of a bezier from T_MIN to T_MAX
373 Bezier::extract (Real t_min, Real t_max) const
375 if ((t_min < 0) || (t_max) > 1)
377 ("bezier extract arguments outside of limits: curve may have bad shape");
380 ("lower bezier extract value not less than upper value: curve may have bad shape");
381 Bezier bez1, bez2, bez3, bez4;
385 subdivide (t_min, &bez1, &bez2);
390 bez2.subdivide ((t_max - t_min) / (1 - t_min), &bez3, &bez4);