]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/bezier-bow.cc
release: 1.3.28
[lilypond.git] / lily / bezier-bow.cc
index a029f36e661b8f3b85db7cab39ef1ad363450f93..3565cddb31e60d7a46d058411da3fcc40f75a07e 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1998--1999 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include <math.h>
@@ -12,9 +12,9 @@
 #include "bezier.hh"
 #include "dimensions.hh"
 #include "direction.hh"
-#include "paper-def.hh"
 #include "debug.hh"
 #include "main.hh"
+#include "lily-guile.hh"
 
 void
 flipy (Array<Offset>  &c)
@@ -39,186 +39,246 @@ translate (Array<Offset> &c, Offset o)
 }
 
 
-Bezier_bow::Bezier_bow (Paper_def* paper_l)
+Bezier_bow::Bezier_bow (Array<Offset> points, Direction dir)
 {
-  paper_l_ = paper_l;
+  dir_ = dir;
+  encompass_ = points;
+  to_canonic_form ();
+
+  rc_factor_ = 1.0;
+  height_limit_ = 1.0;
+  ratio_ = 1.0;
+}
+
+static Real
+default_height (Real len)
+{
+  // assume 20pt staff
+  // see fonts.doc
+  Real staff_space = 5.0;
+  Real h_inf = 2.0* staff_space;
+  Real r_0 = 0.33;
+  return h_inf * 2.0 / M_PI * atan ( M_PI * r_0 / (2.0 * h_inf) * len);
 }
 
 void
 Bezier_bow::blow_fit ()
 {
-  Real x1 = encompass_[0][X_AXIS];
-  Real x2 = encompass_.top ()[X_AXIS];
+  Real len = curve_.control_[3][X_AXIS]; 
+  Real h = curve_.control_[1][Y_AXIS] * fit_factor () / len;
+  curve_.control_[1][Y_AXIS] = h * len;
+  curve_.control_[2][Y_AXIS] = h * len;  
+  curve_.check_sanity ();
+}
 
-  Real factor = 1.0;
-  for (int i=1; i < encompass_.size ()-1; i++)
+void
+Bezier_bow::de_uglyfy ()
+{
+  Real len = curve_.control_[3][X_AXIS] ; 
+  Real ff = fit_factor ();
+  for (int i = 1; i < 3; i++)
     {
-      if (encompass_[i][X_AXIS] > x1 && encompass_[i][X_AXIS] < x2)
+      Real ind = abs (curve_.control_[(i-1)*3][X_AXIS]
+                     - curve_.control_[i][X_AXIS]) / len;
+      Real h = curve_.control_[i][Y_AXIS] * ff / len;
+
+      // ugh. Unhardcode this
+#if 0
+      // Too crude.
+      if (h > 4 * ind)
        {
-        Real y = curve_.get_other_coordinate (X_AXIS, encompass_[i][X_AXIS]);
-        if (y>0)
-          {
-            Real f = encompass_[i][Y_AXIS] / y;
-            factor = factor >? f;
-          }
+         h = 4* ind; 
        }
-    }
-
-  curve_.control_[1][Y_AXIS] *= factor;
-  curve_.control_[2][Y_AXIS] *= factor;  
-  return_.control_[1][Y_AXIS] *= factor;
-  return_.control_[2][Y_AXIS] *= factor;
+#else
+      Real f = default_height (len) / len;
+      if (h > 2.0 * f)
+       {
+         h = 2.0 * f; 
+       }
+#endif
+      
+      if (h > 0.8 + -2 * ind)
+       {
+         h = 0.8 - 2  *ind; 
+       }
+      
+      curve_.control_[i][Y_AXIS] = h * len;  
+    } 
 
   curve_.check_sanity ();
 }
 
 Real
-Bezier_bow::calc_f (Real height)
+Bezier_bow::calc_enclosed_area_f () const
 {
-  transform ();
-  calc_default (height);
-
-  Real dy = check_fit_f ();
-  calc_return (0, 0);
+  Real a = 0;
+  for (int i=0; i < encompass_.size (); i++)
+    {
+      Interval x;
+      Interval y;
+      if (i == 0)
+       {
+         x = Interval (0, encompass_[1][X_AXIS] / 2);
+         y = Interval (0,
+                       curve_.get_other_coordinate (X_AXIS,
+                                                    encompass_[1][X_AXIS]
+                                                    / 2));
+       }
+      else if (i == encompass_.size () - 1)
+       {
+         x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS])/2, 
+                       encompass_[i][X_AXIS]);
+         y = Interval (0,
+                       (curve_.get_other_coordinate (X_AXIS,
+                                                     (x[MIN] + x[MAX]) / 2)));
+       }
+      else
+       {
+         x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS]) / 2, 
+                       (encompass_[i][X_AXIS] + encompass_[i+1][X_AXIS]) / 2);
+         y = Interval (encompass_[i][Y_AXIS],
+                       (curve_.get_other_coordinate (X_AXIS, x[MIN])
+                        + curve_.get_other_coordinate (X_AXIS,
+                                                       (x[MIN] + x[MAX]) / 2)
+                        + curve_.get_other_coordinate (X_AXIS, x[MAX])) / 3);
+       }
+      
+      Real da = x.length () * y.length ();
+      a += da;
+    }
+  return a;
+}
 
-  transform_back ();
-  return dy;
+Array<Offset>
+Bezier_bow::area_gradient_offset_arr ()
+{
+  Real len = curve_.control_[3][X_AXIS]; 
+  Real area = calc_enclosed_area_f ();
+  
+  Real grow = len / 10.0;
+  Array<Offset> da (2);
+  for (int i=1; i < 3; i++)
+    {
+      for (Axis a=X_AXIS; a < NO_AXES; incr (a)) 
+       {
+         Real r = curve_.control_[i][a];
+         curve_.control_[i][a] += grow;
+         da[i-1][a] = (calc_enclosed_area_f () - area) / grow;
+         
+         curve_.control_[i][a] = r;
+       }
+    }
+  return da;
 }
 
 void
-Bezier_bow::calc ()
+Bezier_bow::minimise_enclosed_area ()
 {
-  transform ();
-  calc_controls ();
-  transform_back ();
-}
+  Real len = curve_.control_[3][X_AXIS]; 
+  Real beautiful = len * default_height (len) / 2.0;
 
+  DEBUG_OUT << to_str ("Beautiful: %f\n", beautiful);
+  DEBUG_OUT << to_str ("Length: %f\n", len);
+  int steps=2;
+  for (int i=0; i < steps; i++)
+    {
+      Real ff = fit_factor ();
+      if (!ff)
+       break;
 
+      DEBUG_OUT << to_str ("FitFac: %f\n", ff);
 
-/*
-  [TODO]
-    * see if it works
-    * document in Documentation/fonts.tex
- */
+      // slur must be higher at every point
+      if (ff > 1.01)
+       {
+         blow_fit ();
+         DEBUG_OUT << to_str ("Blown area: %f\n", calc_enclosed_area_f ());
+       }
+      else
+         DEBUG_OUT << to_str ("Init area: %f\n", calc_enclosed_area_f ());
 
-/*
-  Clipping
+      Real area = calc_enclosed_area_f ();
+      
 
-  This function tries to address two issues:
-    * the tangents of the slur should always point inwards 
-      in the actual slur, i.e.  *after rotating back*.
+      if (area <= beautiful)
+       break;
 
-    * slurs shouldn't be too high 
-      let's try : h <= 1.2 b && h <= 3 staffheight?
+      Array<Offset> da = area_gradient_offset_arr ();
 
-  We could calculate the tangent of the bezier curve from
-  both ends going inward, and clip the slur at the point
-  where the tangent (after rotation) points up (or inward
-  with a certain maximum angle).
-  
-  However, we assume that real clipping is not the best
-  answer.  We expect that moving the outer control point up 
-  if the slur becomes too high will result in a nicer slur 
-  after recalculation.
-
-  Knowing that the tangent is the line through the first
-  two control points, we'll clip (move the outer control
-  point upwards) too if the tangent points outwards.
- */
+      /*
+       Urg: empiric cs
+            Small slurs are easily too asymmetric,
+            while big slurs are too symmetric
 
-bool
-Bezier_bow::calc_clipping ()
-{
-  Real clip_height = paper_l_->get_var ("slur_clip_height");
-  Real clip_ratio = paper_l_->get_var ("slur_clip_ratio");
-  Real clip_angle = paper_l_->get_var ("slur_clip_angle");
-
-  Real b = curve_.control_[3][X_AXIS] - curve_.control_[0][X_AXIS];
-  Real clip_h = clip_ratio * b <? clip_height;
-  Real begin_h = curve_.control_[1][Y_AXIS] - curve_.control_[0][Y_AXIS];
-  Real end_h = curve_.control_[2][Y_AXIS] - curve_.control_[3][Y_AXIS];
-  Real begin_dy = 0 >? begin_h - clip_h;
-  Real end_dy = 0 >? end_h - clip_h;
+            This makes short slurs strictly x-bound,
+            long slurs become y-bound.
+       */
+      Real ypct = 0.50;
+      //Real xpct = (0.07 * len * len / 1000.0) <? 0.80;
+      Real xpct = (0.1 * len * len * len / 100000.0) <? 0.80;
+
+      Real yu = (abs (curve_.control_[1][Y_AXIS] / da[0][Y_AXIS])
+                <? abs (curve_.control_[2][Y_AXIS] / da[1][Y_AXIS]))
+       * ypct;
+      Real xu = (abs (curve_.control_[1][X_AXIS] / da[0][X_AXIS])
+                <? abs ((curve_.control_[3][X_AXIS]
+                            - curve_.control_[2][X_AXIS]) / da[1][X_AXIS]))
+       * xpct;
+      Real u = yu <? xu;
+      DEBUG_OUT << to_str ("u (xu, yu): %f (%f, %f)\n", u, xu, yu);
+      DEBUG_OUT << to_str ("pct (x, y): (%f, %f)\n", xpct, ypct);
+
+      DEBUG_OUT << to_str ("da1: (%f, %f)\n", da[0][X_AXIS], da[0][Y_AXIS]);
+      DEBUG_OUT << to_str ("da2: (%f, %f)\n", da[1][X_AXIS], da[1][Y_AXIS]);
+
+      curve_.control_[1] -= da[0] * u;
+      curve_.control_[2] -= da[1] * u;
+    }
   
-  Real pi = M_PI;
-  Real begin_alpha = (curve_.control_[1] - curve_.control_[0]).arg () + dir_ * alpha_;
-  Real end_alpha = pi -  (curve_.control_[2] - curve_.control_[3]).arg () - dir_  * alpha_;
-
-  Real max_alpha = clip_angle / 90 * pi / 2;
-  if ((begin_dy < 0) && (end_dy < 0)
-    && (begin_alpha < max_alpha) && (end_alpha < max_alpha))
-    return false;
-
-  transform_back ();
 
-  bool again = true;
+  if (fit_factor () > 1.5)
+    blow_fit ();
 
-  if ((begin_dy > 0) || (end_dy > 0))
-    {
-      Real dy = (begin_dy + end_dy) / 4;
-      dy *= cos (alpha_);
-      encompass_[0][Y_AXIS] += dir_ * dy;
-      encompass_.top ()[Y_AXIS] += dir_ * dy;
-    }
-  else
+  DEBUG_OUT << to_str ("Exarea: %f\n", calc_enclosed_area_f ());
+  Real area = calc_enclosed_area_f ();
+  /*
+    Slurs that fit beautifully are not ugly
+   */
+  if (area > beautiful)
     {
-      //ugh
-      Real c = 0.4;
-      if (begin_alpha >= max_alpha)
-       begin_dy = 0 >? c * begin_alpha / max_alpha * begin_h;
-      if (end_alpha >= max_alpha)
-       end_dy = 0 >? c * end_alpha / max_alpha * end_h;
-
-      encompass_[0][Y_AXIS] += dir_ * begin_dy;
-      encompass_.top ()[Y_AXIS] += dir_ * end_dy;
-
-      Offset delta = encompass_.top () - encompass_[0];
-      alpha_ = delta.arg ();
+      DEBUG_OUT << "DE-UGLYFY\n";
+      de_uglyfy ();
     }
 
-  transform ();
-
-  return again;
 }
 
 void
-Bezier_bow::calc_controls ()
+Bezier_bow::calculate ()
 {
-  for (int i = 0; i < 3; i++)
+  calc_default ();
+  if (fit_factor () > 1.0)
     {
-      
-      if (i && !calc_clipping ())
-       return;
-
-      /*
-       why do we always recalc from 0?
-       shouldn't calc_f () be used (too), rather than blow_fit () (only)?
-       */
-      calc_default (0);
-      curve_.check_sanity ();
-      if (check_fit_f () > 0)
-        {
-         calc_tangent_controls ();
-         blow_fit ();
-       }
-      else
-       {
-         calc_return (0, 0);
-         return;
-       }
+      //    calc_tangent_controls ();
+      // blow_fit ();
+      minimise_enclosed_area ();
     }
 }
 
-void
-Bezier_bow::calc_return (Real begin_alpha, Real end_alpha)
-{
-  Real thick = paper_l_->get_var ("slur_thickness");
 
-  return_.control_[0] = curve_.control_[3];
-  return_.control_[3] = curve_.control_[0];
+  
+Bezier
+Bezier_bow::get_curve ()const
+{
+  Bezier rv = curve_;
+  if (dir_ == DOWN)
+    {
+      rv.flip (Y_AXIS);
+    }
 
-  return_.control_[1] = curve_.control_[2] - thick * complex_exp (Offset (0, 90 + end_alpha));
-  return_.control_[2] = curve_.control_[1] - thick * complex_exp (Offset (0, 90 - begin_alpha));  
+  rv.rotate (alpha_);
+  rv.translate (origin_);
+  
+  return rv;
 }
 
 static Real const FUDGE = 1e-8;
@@ -273,13 +333,13 @@ Bezier_bow::calc_tangent_controls ()
     The curve will always be under line between curve_.control_0 -> curve_.control_1, so
     make it extra steep by slur_rc_factor
   */
-  Real rc_correct = paper_l_->get_var ("slur_rc_factor");
-  
+
+
   Drul_array<Real> angles;
   Direction d = LEFT;
   do
     {
-      maxtan[d] *= -d * rc_correct;
+      maxtan[d] *= -d * rc_factor_;
       angles[d] = atan (maxtan[d]);
     }
   while (flip(&d) != LEFT);
@@ -299,7 +359,7 @@ Bezier_bow::calc_tangent_controls ()
 
 
   // ugh: be less steep
-  rc3 /= 2*rc_correct;
+  rc3 /= 2*rc_factor_;
   
 
   Real c2 = -maxtan[RIGHT] * curve_.control_[3][X_AXIS];
@@ -318,38 +378,41 @@ Bezier_bow::calc_tangent_controls ()
 
 
   curve_.check_sanity();
-  
-  calc_return (angles[LEFT], angles[RIGHT]);
 }
 
 /*
-  The maximum amount that the encompass points stick out above the bezier curve.
+  max ( encompass.y / curve.y )
+  
  */
 Real
-Bezier_bow::check_fit_f () const
+Bezier_bow::fit_factor () const
 {
-  Real dy = 0;
   Real x1 = encompass_[0][X_AXIS];
   Real x2 = encompass_.top ()[X_AXIS];
-  for (int i = 1; i < encompass_.size () - 1; i++)
+
+  Real factor = 0.0;
+  for (int i=1; i < encompass_.size ()-1; i++)
     {
-      Real x = encompass_[i][X_AXIS];
-      if (x1< x&& x < x2)
-       dy = dy >? (encompass_[i][Y_AXIS] - curve_.get_other_coordinate (X_AXIS, x));
+      if (encompass_[i][X_AXIS] > x1 && encompass_[i][X_AXIS] < x2)
+       {
+        Real y = curve_.get_other_coordinate (X_AXIS, encompass_[i][X_AXIS]);
+        if (y>0)
+          {
+            Real f = encompass_[i][Y_AXIS] / y;
+            factor = factor >? f;
+          }
+       }
     }
-  return dy;
-}
 
 
-void
-Bezier_bow::set (Array<Offset> points, Direction dir)
-{
-  dir_ = dir;
-  encompass_ = points;
+  return factor;
 }
 
+
+
+
 void
-Bezier_bow::transform ()
+Bezier_bow::to_canonic_form ()
 {
   origin_ = encompass_[0];
   translate (encompass_,-origin_);
@@ -358,51 +421,44 @@ Bezier_bow::transform ()
   alpha_ = delta.arg ();
 
   rotate (encompass_, -alpha_);
-
-  if (dir_ == DOWN)
-    flipy (encompass_);
-}
-
-void
-Bezier_bow::transform_back ()
-{
   if (dir_ == DOWN)
     {
-      curve_.flip (Y_AXIS);
-      return_.flip (Y_AXIS);
       flipy (encompass_);
     }
 
-  curve_.rotate (alpha_);
-  curve_.translate (origin_);
-  return_.rotate (alpha_);
-  return_.translate (origin_);
-  rotate (encompass_,alpha_);
-  translate (encompass_,origin_);
+  while (encompass_.size () > 1 && encompass_[1][X_AXIS] <= 0.0)
+    {
+      programming_error ("Degenerate slur: infinite steepness reqd");
+      encompass_.del (1);
+    }
+
+  Real l = encompass_.top ()[X_AXIS];
+  while (encompass_.size () > 1 && encompass_.top (1)[X_AXIS] >= l)
+    {
+      programming_error ("Degenerate slur: infinite steepness reqd");
+      encompass_.del (encompass_.size ()-2);
+    }
 }
 
+
+
 /*
  See Documentation/fonts.tex
  */
 void
-Bezier_bow::calc_default (Real h)
+Bezier_bow::calc_default ()
 {
   Real pi = M_PI;
 
-  Real height_limit = paper_l_->get_var ("slur_height_limit");
-  Real ratio = paper_l_->get_var ("slur_ratio");
-
-  Real alpha = height_limit * 2.0 / pi;
-  Real beta = pi * ratio / (2.0 * height_limit);
+  Real alpha = height_limit_ * 2.0 / pi;
+  Real beta = pi * ratio_ / (2.0 * height_limit_);
 
   Offset delta (encompass_.top ()[X_AXIS] 
     - encompass_[0][X_AXIS], 0);
 
   Real b = delta.length ();
   Real indent = alpha * atan (beta * b);
-  Real height = indent + h;
+  Real height = indent;
  
   curve_.control_ [0] = Offset (0, 0);
   curve_.control_ [1] = Offset (indent, height);
@@ -411,3 +467,5 @@ Bezier_bow::calc_default (Real h)
 }
 
 
+
+