]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/bezier.cc
*** empty log message ***
[lilypond.git] / lily / bezier.cc
index 28634b473c683b283e95cd27d465814a62d15ab4..c51ba3706d1603846c679ed0088d6aac78443e50 100644 (file)
@@ -3,59 +3,95 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
+
 #include <math.h>
+
 #include "bezier.hh"
-#include "polynomial.hh"
+#include "warn.hh"
+#include "libc-extension.hh"
 
-/*
+Real
+binomial_coefficient (Real over , int under)
+{
+  Real x = 1.0;
 
-  Formula of the bezier 3-spline
+  while (under)
+    {
+      x *= over / Real (under);
 
-  sum_{j=0}^3  (3 over j) z_j (1-t)^(3-j)  t^j
- */
+      over  -= 1.0;
+      under --;
+    }
+  return x;
+}
+
+void
+scale (Array<Offset>* array, Real x , Real y)
+{
+  for (int i = 0; i < array->size (); i++)
+    {
+      (*array)[i][X_AXIS] = x* (*array)[i][X_AXIS];
+      (*array)[i][Y_AXIS] = y* (*array)[i][Y_AXIS];
+    }
+}
+
+void
+rotate (Array<Offset>* array, Real phi)
+{
+  Offset rot (complex_exp (Offset (0, phi)));
+  for (int i = 0; i < array->size (); i++)
+    (*array)[i] = complex_multiply (rot, (*array)[i]);
+}
 
-Bezier::Bezier ()
+void
+translate (Array<Offset>* array, Offset 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
+
+
+  A is the axis of X coordinate.
+ */
+
 Real
 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.");
+      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];
 }
 
-Real
-binomial_coefficient (Real over , int under)
-{
-  Real x = 1.0;
-
-  while (under)
-    {
-      x *= over / Real (under);
-
-      over  -= 1.0;
-      under --;
-    }
-  return x;
-}
 
 Offset
 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);
@@ -65,9 +101,10 @@ Bezier::curve_point (Real t)const
        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);
-
+#endif
   
   return o;
 }
@@ -77,12 +114,12 @@ Polynomial
 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;
@@ -106,8 +143,8 @@ filter_solutions (Array<Real> sol)
 Array<Real>
 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 ();
   
@@ -123,13 +160,16 @@ Bezier::solve_derivative (Offset deriv)const
 Array<Real> 
 Bezier::solve_point (Axis ax, Real coordinate) const
 {
-  Polynomial p(polynomial (ax));
+  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
 {
@@ -140,7 +180,7 @@ Bezier::extent (Axis a)const
   Array<Real> sols (solve_derivative (d));
   sols.push (1.0);
   sols.push (0.0);  
-  for (int i= sols.size (); i--;)
+  for (int i = sols.size (); i--;)
     {
       Offset o (curve_point (sols[i]));
       iv.unite (Interval (o[a],o[a]));
@@ -148,11 +188,17 @@ Bezier::extent (Axis a)const
   return iv;
 }
 
+/**
+   Flip around axis A
+ */
 void
-Bezier::flip (Axis a)
+Bezier::scale (Real x, Real y)
 {
   for (int i = CONTROL_COUNT; i--;)
-    control_[i][a] = - control_[i][a];
+    {
+      control_[i][X_AXIS] = x * control_[i][X_AXIS];
+      control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
+    }
 }
 
 void
@@ -171,9 +217,9 @@ Bezier::translate (Offset o)
 }
 
 void
-Bezier::check_sanity () const
+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 ()));
 }
@@ -182,7 +228,7 @@ void
 Bezier::reverse ()
 {
   Bezier b2;
-  for (int i =0; i < CONTROL_COUNT; i++)
+  for (int i = 0; i < CONTROL_COUNT; i++)
     b2.control_[CONTROL_COUNT-i-1] = control_[i];
   *this = b2;
 }