]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/bezier.cc
release: 1.5.29
[lilypond.git] / lily / bezier.cc
index 66a7a55245381ab1c8ae5b32e6de1581067e0db8..6d22b1d6b68a441ba5e4356f9ade405209621bdf 100644 (file)
@@ -3,29 +3,75 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1998--1999 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1998--2002 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include <math.h>
+
+#include "config.h"
+#include "warn.hh"
+#include "libc-extension.hh"
 #include "bezier.hh"
 #include "polynomial.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;
+}
 
-Bezier::Bezier ()
+void
+scale (Array<Offset>* arr_p, Real x , Real y)
 {
+  for (int i = 0; i < arr_p->size (); i++)
+    {
+      (*arr_p)[i][X_AXIS] = x* (*arr_p)[i][X_AXIS];
+      (*arr_p)[i][Y_AXIS] = y* (*arr_p)[i][Y_AXIS];
+    }
 }
 
+void
+rotate (Array<Offset>* arr_p, 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]);
+}
+
+void
+translate (Array<Offset>* arr_p, Offset o)
+{
+  for (int i = 0; i < arr_p->size (); i++)
+    (*arr_p)[i] += o;
+}
+
+/*
+
+  Formula of the bezier 3-spline
+
+  sum_{j=0}^3 (3 over j) z_j (1-t)^ (3-j)  t^j
+ */
+
 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);
@@ -33,26 +79,12 @@ Bezier::get_other_coordinate (Axis a,  Real x) const
   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++)
@@ -65,9 +97,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;
 }
@@ -82,7 +115,7 @@ Bezier::polynomial (Axis a)const
       p += control_[j][a]
        * Polynomial::power (j , Polynomial (0,1))*
        Polynomial::power (3 - j, Polynomial (1,-1))*
-       binomial_coefficient(3, j);
+       binomial_coefficient (3, j);
     }
 
   return p;
@@ -123,13 +156,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
 {
@@ -148,11 +184,18 @@ 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,7 +214,7 @@ Bezier::translate (Offset o)
 }
 
 void
-Bezier::check_sanity () const
+Bezier::assert_sanity () const
 {
   for (int i=0; i < CONTROL_COUNT; i++)
     assert (!isnan (control_[i].length ())