]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/bezier.cc
* scm/lily.scm (PLATFORM): Export.
[lilypond.git] / lily / bezier.cc
index fbbbc6c867020c2a3a1817013f311a2ad40fe86a..f9b7e84142c0f99b0b7902c64f8a71e2eecc01b1 100644 (file)
@@ -3,19 +3,17 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include <math.h>
-#include "config.h"
-#include "warn.hh"
 
-#include "libc-extension.hh"
 #include "bezier.hh"
-#include "polynomial.hh"
+#include "warn.hh"
+#include "libc-extension.hh"
 
 Real
-binomial_coefficient (Real over , int under)
+binomial_coefficient (Real over, int under)
 {
   Real x = 1.0;
 
@@ -23,171 +21,171 @@ binomial_coefficient (Real over , int under)
     {
       x *= over / Real (under);
 
-      over  -= 1.0;
-      under --;
+      over -= 1.0;
+      under--;
     }
   return x;
 }
 
 void
-scale (Array<Offset>* arr_p, Real x , Real y)
+scale (Array<Offset> *array, Real x, Real y)
 {
-  for (int i = 0; i < arr_p->size (); i++)
+  for (int i = 0; i < array->size (); i++)
     {
(*arr_p)[i][X_AXIS] = x* (*arr_p)[i][X_AXIS];
(*arr_p)[i][Y_AXIS] = y* (*arr_p)[i][Y_AXIS];
     (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
     (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
     }
 }
 
 void
-rotate (Array<Offset>* arr_p, Real phi)
+rotate (Array<Offset> *array, Real phi)
 {
   Offset rot (complex_exp (Offset (0, phi)));
-  for (int i = 0; i < arr_p->size (); i++)
(*arr_p)[i] = complex_multiply (rot, (*arr_p)[i]);
+  for (int i = 0; i < array->size (); i++)
   (*array)[i] = complex_multiply (rot, (*array)[i]);
 }
 
 void
-translate (Array<Offset>* arr_p, Offset o)
+translate (Array<Offset> *array, Offset o)
 {
-  for (int i = 0; i < arr_p->size (); i++)
(*arr_p)[i] += o;
+  for (int i = 0; i < array->size (); i++)
   (*array)[i] += o;
 }
 
 /*
-
   Formula of the bezier 3-spline
 
-  sum_{j=0}^3 (3 over j) z_j (1-t)^ (3-j)  t^j
- */
+  sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j)  t^j
+
+
+  A is the axis of X coordinate.
+*/
 
 Real
-Bezier::get_other_coordinate (Axis a,  Real x) const
+Bezier::get_other_coordinate (Axis a, Real x) const
 {
   Axis other = Axis ((a +1)%NO_AXES);
   Array<Real> ts = solve_point (a, x);
 
   if (ts.size () == 0)
     {
-      programming_error ("No solution found for Bezier intersection.");
+      programming_error ("no solution found for Bezier intersection");
       return 0.0;
     }
-  
+
   Offset c = curve_point (ts[0]);
-  assert (fabs (c[a] - x) < 1e-8);
-  
+
+  if (fabs (c[a] - x) > 1e-8)
+    programming_error ("bezier intersection not correct?");
+
   return c[other];
 }
 
-
 Offset
-Bezier::curve_point (Real t)const
+Bezier::curve_point (Real t) const
 {
   Real tj = 1;
-  Real one_min_tj = (1-t)* (1-t)* (1-t);
+  Real one_min_tj = (1 - t) * (1 - t) * (1 - t);
 
   Offset o;
-  for (int j=0 ; j < 4; j++)
+  for (int j = 0; j < 4; j++)
     {
       o += control_[j] * binomial_coefficient (3, j)
-       * pow (t,j) * pow (1-t, 3-j);
+       * pow (t, j) * pow (1 - t, 3 - j);
 
       tj *= t;
-      if (1-t)
-       one_min_tj /= (1-t);
+      if (1 - t)
+       one_min_tj /= (1 - t);
     }
 
 #ifdef PARANOID
-  assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t))< 1e-8);
-  assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t))< 1e-8);
+  assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
+  assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
 #endif
-  
+
   return o;
 }
 
-
 Polynomial
-Bezier::polynomial (Axis a)const
+Bezier::polynomial (Axis a) const
 {
   Polynomial p (0.0);
-  for (int j=0; j <= 3; j++)
+  for (int j = 0; j <= 3; j++)
     {
-      p += control_[j][a]
-       * Polynomial::power (j , Polynomial (0,1))*
-       Polynomial::power (3 - j, Polynomial (1,-1))*
-       binomial_coefficient (3, j);
+      p
+       += (control_[j][a] * binomial_coefficient (3, j))
+       * Polynomial::power (j, Polynomial (0, 1))
+       * Polynomial::power (3 - j, Polynomial (1, -1));
     }
 
   return p;
 }
 
 /**
-   Remove all numbers outside [0,1] from SOL
- */
+   Remove all numbers outside [0, 1] from SOL
+*/
 Array<Real>
 filter_solutions (Array<Real> sol)
 {
   for (int i = sol.size (); i--;)
-    if (sol[i] < 0 || sol[i] >1)
+    if (sol[i] < 0 || sol[i] > 1)
       sol.del (i);
   return sol;
 }
 
 /**
    find t such that derivative is proportional to DERIV
- */
+*/
 Array<Real>
-Bezier::solve_derivative (Offset deriv)const
+Bezier::solve_derivative (Offset deriv) const
 {
-  Polynomial xp=polynomial (X_AXIS);
-  Polynomial yp=polynomial (Y_AXIS);
+  Polynomial xp = polynomial (X_AXIS);
+  Polynomial yp = polynomial (Y_AXIS);
   xp.differentiate ();
   yp.differentiate ();
-  
+
   Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
 
   return filter_solutions (combine.solve ());
 }
-  
 
 /*
   Find t such that curve_point (t)[AX] == COORDINATE
 */
-Array<Real> 
+Array<Real>
 Bezier::solve_point (Axis ax, Real coordinate) const
 {
   Polynomial p (polynomial (ax));
   p.coefs_[0] -= coordinate;
-  
+
   Array<Real> sol (p.solve ());
   return filter_solutions (sol);
 }
 
 /**
    Compute the bounding box dimensions in direction of A.
- */
+*/
 Interval
-Bezier::extent (Axis a)const
+Bezier::extent (Axis a) const
 {
-  int o = (a+1)%NO_AXES;
+  int o = (a + 1)%NO_AXES;
   Offset d;
-  d[Axis (o)] =1.0;
+  d[Axis (o)] = 1.0;
   Interval iv;
   Array<Real> sols (solve_derivative (d));
   sols.push (1.0);
-  sols.push (0.0);  
-  for (int i= sols.size (); i--;)
+  sols.push (0.0);
+  for (int i = sols.size (); i--;)
     {
       Offset o (curve_point (sols[i]));
-      iv.unite (Interval (o[a],o[a]));
+      iv.unite (Interval (o[a], o[a]));
     }
   return iv;
 }
 
 /**
    Flip around axis A
- */
-
+*/
 void
 Bezier::scale (Real x, Real y)
 {
@@ -216,7 +214,7 @@ Bezier::translate (Offset o)
 void
 Bezier::assert_sanity () const
 {
-  for (int i=0; i < CONTROL_COUNT; i++)
+  for (int i = 0; i < CONTROL_COUNT; i++)
     assert (!isnan (control_[i].length ())
            && !isinf (control_[i].length ()));
 }
@@ -225,7 +223,7 @@ void
 Bezier::reverse ()
 {
   Bezier b2;
-  for (int i =0; i < CONTROL_COUNT; i++)
-    b2.control_[CONTROL_COUNT-i-1] = control_[i];
+  for (int i = 0; i < CONTROL_COUNT; i++)
+    b2.control_[CONTROL_COUNT - i - 1] = control_[i];
   *this = b2;
 }