source file of the GNU LilyPond music typesetter
- (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
+ (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
*/
#include "bezier.hh"
1, 3, 3, 1
};
-Real
-binomial_coefficient (Real over, int under)
-{
- Real x = 1.0;
-
- while (under)
- {
- x *= over / Real (under);
-
- over -= 1.0;
- under--;
- }
- return x;
-}
-
void
-scale (std::vector<Offset> *array, Real x, Real y)
+scale (vector<Offset> *array, Real x, Real y)
{
for (vsize i = 0; i < array->size (); i++)
{
}
void
-rotate (std::vector<Offset> *array, Real phi)
+rotate (vector<Offset> *array, Real phi)
{
Offset rot (complex_exp (Offset (0, phi)));
for (vsize i = 0; i < array->size (); i++)
}
void
-translate (std::vector<Offset> *array, Offset o)
+translate (vector<Offset> *array, Offset o)
{
for (vsize i = 0; i < array->size (); i++)
(*array)[i] += o;
Bezier::get_other_coordinate (Axis a, Real x) const
{
Axis other = Axis ((a +1) % NO_AXES);
- std::vector<Real> ts = solve_point (a, x);
+ vector<Real> ts = solve_point (a, x);
if (ts.size () == 0)
{
/*
Cache binom(3,j) t^j (1-t)^{3-j}
*/
-static struct Polynomial bezier_term_cache[4];
-static bool done_cache_init;
+struct Polynomial_cache {
+ Polynomial terms_[4];
+ Polynomial_cache ()
+ {
+ for (int j = 0; j <= 3; j++)
+ terms_[j]
+ = binomial_coefficient_3[j]
+ * Polynomial::power (j, Polynomial (0, 1))
+ * Polynomial::power (3 - j, Polynomial (1, -1));
+ }
+};
-void
-init_polynomial_cache ()
-{
- for (int j = 0; j <= 3; j++)
- bezier_term_cache[j]
- = binomial_coefficient_3[j]
- * Polynomial::power (j, Polynomial (0, 1))
- * Polynomial::power (3 - j, Polynomial (1, -1));
- done_cache_init = true;
-}
+static Polynomial_cache poly_cache;
Polynomial
Bezier::polynomial (Axis a) const
{
- if (!done_cache_init)
- init_polynomial_cache ();
-
Polynomial p (0.0);
Polynomial q;
for (int j = 0; j <= 3; j++)
{
- q = bezier_term_cache[j];
+ q = poly_cache.terms_[j];
q *= control_[j][a];
p += q;
}
/**
Remove all numbers outside [0, 1] from SOL
*/
-std::vector<Real>
-filter_solutions (std::vector<Real> sol)
+vector<Real>
+filter_solutions (vector<Real> sol)
{
for (vsize i = sol.size (); i--;)
if (sol[i] < 0 || sol[i] > 1)
- sol.del (i);
+ sol.erase (sol.begin () + i);
return sol;
}
/**
find t such that derivative is proportional to DERIV
*/
-std::vector<Real>
+vector<Real>
Bezier::solve_derivative (Offset deriv) const
{
Polynomial xp = polynomial (X_AXIS);
/*
Find t such that curve_point (t)[AX] == COORDINATE
*/
-std::vector<Real>
+vector<Real>
Bezier::solve_point (Axis ax, Real coordinate) const
{
Polynomial p (polynomial (ax));
p.coefs_[0] -= coordinate;
- std::vector<Real> sol (p.solve ());
+ vector<Real> sol (p.solve ());
return filter_solutions (sol);
}
Offset d;
d[Axis (o)] = 1.0;
Interval iv;
- std::vector<Real> sols (solve_derivative (d));
+ vector<Real> sols (solve_derivative (d));
sols.push_back (1.0);
sols.push_back (0.0);
for (vsize i = sols.size (); i--;)