]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/bezier.cc
release: 1.0.1
[lilypond.git] / lily / bezier.cc
index bb360581e24df1d18a3ab4e1fb3da95d952e98e6..e851e74f031b72e8274bbbcdf8c761dbb587d07f 100644 (file)
@@ -3,21 +3,22 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1998 Jan Nieuwenhuizen <jan@digicash.com>
+  (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include <math.h>
 #include "bezier.hh"
+#include "misc.hh"
 
 #ifndef STANDALONE
 #include "direction.hh"
-#include "dimen.hh"
+#include "dimension.hh"
 #include "paper-def.hh"
 #include "debug.hh"
 #include "main.hh"
-#define SLUR_DOUT if (check_debug && !monitor->silent_b ("Slur")) cout
+#define BEZIER_BOW_DOUT if (check_debug && !monitor->silent_b ("Bezier_bow")) cout
 #else
-#define SLUR_DOUT cerr
+#define BEZIER_BOW_DOUT cerr
 #endif
 
 void
@@ -94,21 +95,20 @@ Bezier::set (Array<Offset> points)
 Real
 Bezier::y (Real x)
 {
-  for (int i = 1; i < curve_.size (); i++ )
-    {
-      if (x < curve_[i].x () || (i == curve_.size () - 1))
-       {
-         Offset z1 = curve_[i-1];
-         Offset z2 = curve_[i];
-         Real multiplier = (x - z2.x ()) / (z1.x () - z2.x ());
-         Real y = z1.y () * multiplier + (1.0 - multiplier) * z2.y();
-
-         return y;
-        }
-    }
-  assert (false);
-  // silly c++
-  return 0;
+  // ugh
+  // bounds func should be templatised to take array of offsets too?
+  Array<Real> positions;
+  for (int i = 0; i < curve_.size (); i++)
+    positions.push (curve_[i].x ());
+
+  Slice slice = get_bounds_slice (positions, x);
+  // ugh
+  Offset z1 = curve_[0 >? slice.max () - 1];
+  Offset z2 = curve_[1 >? slice.max ()];
+  Real multiplier = (x - z2.x ()) / (z1.x () - z2.x ());
+  Real y = z1.y () * multiplier + (1.0 - multiplier) * z2.y();
+
+  return y;
 }
 
 
@@ -139,10 +139,12 @@ Bezier_bow::blow_fit ()
     return;
 
 #ifndef STANDALONE
-  Real epsilon = paper_l_->rule_thickness ();
+  Real internote_f = paper_l_->internote_f ();
 #else
-  Real epsilon = 1.5 * 0.4 PT;
+  Real internote_f = STAFFHEIGHT / 8;
 #endif
+
+  Real epsilon = internote_f / 4;
   if (abs (dy2 - dy1) < epsilon)
     return;
   
@@ -166,6 +168,12 @@ Bezier_bow::blow_fit ()
   Real b = dy1;
   Real h = -b / a;
 
+  if (sign (h) != sign (h1))
+    return;
+
+  if (sign (h) != sign (h1))
+    return;
+
   control_[1].y () += -h1 +h; 
   control_[2].y () += -h1 +h; 
   return_[1].y () += -h1 +h;
@@ -197,7 +205,7 @@ Bezier_bow::calc_f (Real height)
   Real dy = check_fit_f ();
   calc_return (0, 0);
 
-  transform_controls_back ();
+  transform_back ();
   return dy;
 }
 
@@ -208,7 +216,7 @@ Bezier_bow::calc ()
 
   calc_controls ();
 
-  transform_controls_back ();
+  transform_back ();
 }
 
 /*
@@ -223,7 +231,9 @@ Bezier_bow::calc ()
   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*.
-    * slurs shouldn't be too high ( <= 1.5 staffheight?)
+
+    * slurs shouldn't be too high 
+      let's try : h <= 1.2 b && h <= 3 staffheight?
 
   We could calculate the tangent of the bezier curve from
   both ends going inward, and clip the slur at the point
@@ -243,65 +253,61 @@ Bezier_bow::calc ()
 bool
 Bezier_bow::calc_clipping ()
 {
-  if (!experimental_features_global_b)
-    return false;
 #ifndef STANDALONE
-  Real staffsize_f = paper_l_->get_var ("barsize");
+  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");
 #else
   Real staffsize_f = STAFFHEIGHT;
+  Real clip_height = 3.0 * staffsize_f;
+  Real clip_ratio = 1.2;
+  Real clip_angle = 100;
 #endif
 
-  Real clip_h = staffsize_f;
+  Real b = control_[3].x () - control_[0].x ();
+  Real clip_h = clip_ratio * b <? clip_height;
   Real begin_h = control_[1].y () - control_[0].y ();
   Real end_h = control_[2].y () - control_[3].y ();
-  Real begin_dy = begin_h - clip_h;
-  Real end_dy = end_h - clip_h;
+  Real begin_dy = 0 >? begin_h - clip_h;
+  Real end_dy = 0 >? end_h - clip_h;
   
   Real pi = M_PI;
-  Real begin_alpha = (control_[1] - control_[0]).arg () + alpha_;
-  Real end_alpha = pi -  (control_[2] - control_[3]).arg () - alpha_;
+  Real begin_alpha = (control_[1] - control_[0]).arg () + dir_ * alpha_;
+  Real end_alpha = pi -  (control_[2] - control_[3]).arg () - dir_ * alpha_;
 
-  Real max_alpha = 1.1 * pi/2;
+  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;
 
-  encompass_.rotate (alpha_);
-  // ugh
-  origin_.y () *= dir_;
-  encompass_.translate (origin_);
+  transform_back ();
 
   bool again = true;
-  //ugh
+
   if ((begin_dy > 0) || (end_dy > 0))
     {
       Real dy = (begin_dy + end_dy) / 4;
       dy *= cos (alpha_);
-      encompass_[0].y () += dy;
-      encompass_[encompass_.size () - 1].y () += dy;
+      encompass_[0].y () += dir_ * dy;
+      encompass_[encompass_.size () - 1].y () += dir_ * dy;
     }
   else
     {
       //ugh
-      Real c = -0.4;
+      Real c = 0.4;
       if (begin_alpha >= max_alpha)
-       begin_dy = c * begin_alpha / max_alpha * begin_h;
+       begin_dy = 0 >? c * begin_alpha / max_alpha * begin_h;
       if (end_alpha >= max_alpha)
-       end_dy = c * end_alpha / max_alpha * end_h;
-      encompass_[0].y () += begin_dy;
-      encompass_[encompass_.size () - 1].y () += end_dy;
+       end_dy = 0 >? c * end_alpha / max_alpha * end_h;
+
+      encompass_[0].y () += dir_ * begin_dy;
+      encompass_[encompass_.size () - 1].y () += dir_ * end_dy;
 
       Offset delta = encompass_[encompass_.size () - 1] - encompass_[0];
       alpha_ = delta.arg ();
-      alpha_ *= dir_;
-//      again = false;
     }
 
-  origin_ = encompass_[0];
-  encompass_.translate (-origin_);
-  // ugh
-  origin_.y () *= dir_;
-  encompass_.rotate (-alpha_);
+  transform ();
 
   return again;
 }
@@ -311,6 +317,9 @@ Bezier_bow::calc_controls ()
 {
   for (int i = 0; i < 3; i++)
     {
+      if (i && !calc_clipping ())
+       return;
+
       calc_default (0);
       calc_bezier ();
       
@@ -320,13 +329,10 @@ Bezier_bow::calc_controls ()
          return;
        }
       calc_tangent_controls ();
-      blow_fit ();
 
+      blow_fit ();
       // ugh
       blow_fit ();
-
-      if (!calc_clipping ())
-       return;
     }
 }
 
@@ -334,7 +340,7 @@ void
 Bezier_bow::calc_return (Real begin_alpha, Real end_alpha)
 {
 #ifndef STANDALONE
-  Real thick = 1.8 * paper_l_->rule_thickness ();
+  Real thick = paper_l_->get_var ("slur_thickness");
 #else
   Real thick = 1.8 * 0.4 PT;
 #endif
@@ -354,7 +360,7 @@ void
 Bezier_bow::calc_tangent_controls ()
 {
   Offset ijk_p (control_[3].x () / 2, control_[1].y ());
-  SLUR_DOUT << "ijk: " << ijk_p.x () << ", " << ijk_p.y () << endl;
+  BEZIER_BOW_DOUT << "ijk: " << ijk_p.x () << ", " << ijk_p.y () << endl;
 
   Real default_rc = ijk_p.y () / ijk_p.x ();
 
@@ -390,8 +396,8 @@ Bezier_bow::calc_tangent_controls ()
       end_p = ijk_p;
       end_rc = default_rc;
     }
-  SLUR_DOUT << "begin " << begin_p.x () << ", " << begin_p.y () << endl;
-  SLUR_DOUT << "end " << end_p.x () << ", " << end_p.y () << endl;
+  BEZIER_BOW_DOUT << "begin " << begin_p.x () << ", " << begin_p.y () << endl;
+  BEZIER_BOW_DOUT << "end " << end_p.x () << ", " << end_p.y () << endl;
 
   Real height =control_[1].y (); 
   for (int i = 0; i < encompass_.size (); i++ )
@@ -399,7 +405,13 @@ Bezier_bow::calc_tangent_controls ()
 
   // emperic computer science:
   //   * tangents somewhat steeper than minimal line
+#ifndef STANDALONE
+  Real internote = paper_l_->internote_f ();
+  Real rc_correct = paper_l_->get_var ("slur_rc_factor");
+#else
+  Real internote = STAFFHEIGHT / 8;
   Real rc_correct = 2.4;
+#endif
 
   begin_rc *= rc_correct;
   end_rc *= rc_correct;
@@ -410,11 +422,6 @@ Bezier_bow::calc_tangent_controls ()
   Real end_alpha = atan (-end_rc);
   Real theta = (begin_alpha - end_alpha) / 2;
 
-#ifndef STANDALONE
-  Real internote = paper_l_->internote_f ();
-#else
-  Real internote = STAFFHEIGHT / 8;
-#endif
   Real epsilon = internote / 5;
 
   // if we have two disturbing points, have height line through those...
@@ -430,17 +437,17 @@ Bezier_bow::calc_tangent_controls ()
   Real c3 = begin_p.y () > end_p.y () ? begin_p.y () 
     - rc3 * begin_p.x () : end_p.y () - rc3 * end_p.x ();
 
-  SLUR_DOUT << "y1 = " << rc1 << " x + 0" << endl;
-  SLUR_DOUT << "y2 = " << rc2 << " x + " << c2 << endl;
-  SLUR_DOUT << "y3 = " << rc3 << " x + " << c3 << endl;
+  BEZIER_BOW_DOUT << "y1 = " << rc1 << " x + 0" << endl;
+  BEZIER_BOW_DOUT << "y2 = " << rc2 << " x + " << c2 << endl;
+  BEZIER_BOW_DOUT << "y3 = " << rc3 << " x + " << c3 << endl;
   control_[1].x () = c3 / (rc1 - rc3);
   control_[1].y () = rc1 * control_[1].x ();
   control_[2].x () = (c3 - c2) / (rc2 - rc3);
-  SLUR_DOUT << "c2.x () = " << control_[2].x () << endl;
-  SLUR_DOUT << "(c3 - c2) = " << (c3 - c2) << endl;
-  SLUR_DOUT << "(rc2 - rc3) = " << (rc2 - rc3) << endl;
+  BEZIER_BOW_DOUT << "c2.x () = " << control_[2].x () << endl;
+  BEZIER_BOW_DOUT << "(c3 - c2) = " << (c3 - c2) << endl;
+  BEZIER_BOW_DOUT << "(rc2 - rc3) = " << (rc2 - rc3) << endl;
   control_[2].y () = rc2 * control_[2].x () + c2;
-  SLUR_DOUT << "c2.y ()" << control_[2].y () << endl;
+  BEZIER_BOW_DOUT << "c2.y ()" << control_[2].y () << endl;
 
   calc_return (begin_alpha, end_alpha);
 }
@@ -449,8 +456,10 @@ bool
 Bezier_bow::check_fit_bo ()
 {
   for (int i = 1; i < encompass_.size () - 1; i++)
-    if (encompass_[i].y () > y (encompass_[i].x ()))
-      return false;
+    if ((encompass_[i].x () > encompass_[0].x ())
+      && (encompass_[i].x () < encompass_[encompass_.size () -1].x ()))
+      if (encompass_[i].y () > y (encompass_[i].x ()))
+       return false;
   return true;
 }
 
@@ -459,7 +468,9 @@ Bezier_bow::check_fit_f ()
 {
   Real dy = 0;
   for (int i = 1; i < encompass_.size () - 1; i++)
-    dy = dy >? (encompass_[i].y () - y (encompass_[i].x ()));
+    if ((encompass_[i].x () > encompass_[0].x ())
+      && (encompass_[i].x () < encompass_[encompass_.size () -1].x ()))
+      dy = dy >? (encompass_[i].y () - y (encompass_[i].x ()));
   return dy;
 }
 
@@ -486,11 +497,8 @@ Bezier_bow::transform ()
 }
 
 void
-Bezier_bow::transform_controls_back ()
+Bezier_bow::transform_back ()
 {
-  // silly name; let's transform encompass back too
-  // to allow recalculation without re-set()ting encompass array
-
   if (dir_ == DOWN)
     {
       control_.flipy ();
@@ -506,7 +514,6 @@ Bezier_bow::transform_controls_back ()
 
   encompass_.rotate (alpha_);
   encompass_.translate (origin_);
-
 }
 
 /*
@@ -517,13 +524,13 @@ Bezier_bow::calc_default (Real h)
 {
   Real pi = M_PI;
 #ifndef STANDALONE
-  Real staffsize_f = paper_l_->get_var ("barsize");
+  Real height_limit = paper_l_->get_var ("slur_height_limit");
+  Real ratio = paper_l_->get_var ("slur_ratio");
 #else
   Real staffsize_f = STAFFHEIGHT;
-#endif
-
   Real height_limit = staffsize_f;
   Real ratio = 1.0/3.0;
+#endif
 
   Real alpha = height_limit * 2.0 / pi;
   Real beta = pi * ratio / (2.0 * height_limit);