]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/beam-quanting.cc
* Documentation/topdocs/NEWS.tely (Top): Mention markup text feature.
[lilypond.git] / lily / beam-quanting.cc
index 0cdba71128ae172d091809500d56e5e6f47cbfdf..54cee073b983a1a1a78794b2759402c24c53d213 100644 (file)
@@ -3,19 +3,14 @@
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
   Jan Nieuwenhuizen <janneke@gnu.org>
   
-
-  
 */
 
-
-
 #include <math.h>
 
 #include "warn.hh"
-#include "grob.hh"
 #include "staff-symbol-referencer.hh"
 #include "beam.hh"
 #include "stem.hh"
@@ -38,7 +33,6 @@ const int DAMPING_DIRECTION_PENALTY = 800;
 const int MUSICAL_DIRECTION_FACTOR = 400;
 const int IDEAL_SLOPE_FACTOR = 10;
 const Real ROUND_TO_ZERO_SLOPE = 0.02;
-const int ROUND_TO_ZERO_POINTS = 4;
 
 static Real
 shrink_extra_weight (Real x, Real fac)
@@ -71,7 +65,8 @@ struct Quant_score
 
 */
 
-int best_quant_score_idx (Array<Quant_score>  const & qscores)
+int
+best_quant_score_idx (Array<Quant_score>  const & qscores)
 {
   Real best = 1e6;
   int best_idx = -1;
@@ -84,6 +79,13 @@ int best_quant_score_idx (Array<Quant_score>  const & qscores)
        }
     }
 
+  if (best_idx < 0)
+    {
+      programming_error ("Huh? No best beam quant score?");
+      best_idx = 0;
+    }
+  
+    
   return best_idx;
 }
   
@@ -94,8 +96,8 @@ Beam::quanting (SCM smob)
   Grob *me = unsmob_grob (smob);
 
   SCM s = me->get_property ("positions");
-  Real yl = ly_scm2double (ly_car (s));
-  Real yr = ly_scm2double (ly_cdr (s));
+  Real yl = scm_to_double (scm_car (s));
+  Real yr = scm_to_double (scm_cdr (s));
 
 
   /*
@@ -107,14 +109,14 @@ Beam::quanting (SCM smob)
   Real thickness = Beam::get_thickness (me) / ss ;
   Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
 
-  SCM sdy = me->get_property ("least-squares-dy");
-  Real dy_mus = ly_c_number_p (sdy) ? ly_scm2double (sdy) : 0.0;
-  
+  Real dy_mus = robust_scm2double (me->get_property ("least-squares-dy"), 0);
   Real straddle = 0.0;
   Real sit = (thickness - slt) / 2;
   Real inter = 0.5;
   Real hang = 1.0 - (thickness - slt) / 2;
   Real quants [] = {straddle, sit, inter, hang };
+
+
   
   int num_quants = int (sizeof (quants)/sizeof (Real));
   Array<Real> quantsl;
@@ -135,13 +137,13 @@ Beam::quanting (SCM smob)
     Do stem computations.  These depend on YL and YR linearly, so we can
     precompute for every stem 2 factors.
    */
-  Link_array<Grob> stems=
+  Link_array<Grob> stems =
     Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems");
   Array<Stem_info> stem_infos;
   Array<Real> base_lengths;
   Array<Real> stem_xposns;  
 
-  Drul_array<bool> dirs_found (0,0);
+  Drul_array<bool> dirs_found (0, 0);
   Grob *common[2];
   for (int a = 2; a--;)
     common[a] = common_refpoint_of_array (stems, me, Axis (a));
@@ -159,7 +161,7 @@ Beam::quanting (SCM smob)
     stem_y != 0.0, when we're cross staff.
     
    */
-  for (int i= 0; i < stems.size (); i++)
+  for (int i = 0; i < stems.size (); i++)
     {
       Grob*s = stems[i];
 
@@ -172,11 +174,11 @@ Beam::quanting (SCM smob)
         && s != lvs && s != fvs;
 
       base_lengths.push (calc_stem_y (me, s, common, xl, xr,
-                                     Interval (0,0), f) / ss);
+                                     Interval (0, 0), f) / ss);
       stem_xposns.push (s->relative_coordinate (common[X_AXIS], X_AXIS));
     }
 
-  bool xstaff= false;
+  bool xstaff = false;
   if (lvs && fvs)
     {
       Grob *commony = fvs->common_refpoint (lvs, Y_AXIS);
@@ -207,8 +209,8 @@ Beam::quanting (SCM smob)
 
   Array<Quant_score> qscores;
   
-  for (int l =0; l < quantsl.size (); l++)  
-    for (int r =0; r < quantsr.size (); r++)
+  for (int l = 0; l < quantsl.size (); l++)  
+    for (int r = 0; r < quantsr.size (); r++)
       {
        Quant_score qs;
        qs.yl = quantsl[l];
@@ -231,14 +233,11 @@ Beam::quanting (SCM smob)
       qscores[i].demerits += d;
 
 #if DEBUG_QUANTING
-      qscores[i].score_card_ += to_string ("S%.2f",d);
+      qscores[i].score_card_ += to_string ("S%.2f", d);
 #endif
     }
 
   Real rad = Staff_symbol_referencer::staff_radius (me);
-
-  
-  
   Drul_array<int> edge_beam_counts
     (Stem::beam_multiplicity (stems[0]).length  () + 1,
      Stem::beam_multiplicity (stems.top ()).length  () + 1);
@@ -250,8 +249,8 @@ Beam::quanting (SCM smob)
     if (qscores[i].demerits < reasonable_score)
       {
        Real d = score_forbidden_quants (qscores[i].yl, qscores[i].yr,
-                                    rad, slt, thickness, beam_translation,
-                                    edge_beam_counts, ldir, rdir); 
+                                        rad, slt, thickness, beam_translation,
+                                        edge_beam_counts, ldir, rdir); 
        qscores[i].demerits += d;
 
 #if DEBUG_QUANTING
@@ -262,7 +261,7 @@ Beam::quanting (SCM smob)
   for (int i = qscores.size (); i--;)
     if (qscores[i].demerits < reasonable_score)
       {
-       Real d=score_stem_lengths (stems, stem_infos,
+       Real d = score_stem_lengths (stems, stem_infos,
                                 base_lengths, stem_xposns,
                                 xl, xr,
                                 is_knee,
@@ -276,11 +275,10 @@ Beam::quanting (SCM smob)
 
   int best_idx = best_quant_score_idx (qscores);
 
-
 #if DEBUG_QUANTING
   SCM inspect_quants = me->get_property ("inspect-quants");
-  if (to_boolean (me->get_paper ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting")))
-      && ly_c_pair_p (inspect_quants))
+  if (to_boolean (me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting")))
+      && scm_is_pair (inspect_quants))
     {
       Drul_array<Real> ins = ly_scm2interval (inspect_quants);
 
@@ -289,11 +287,11 @@ Beam::quanting (SCM smob)
       Real mindist = 1e6;
       for (; i < qscores.size (); i ++)
        {
-         Real d =fabs (qscores[i].yl- ins[LEFT]) + fabs (qscores[i].yr - ins[RIGHT]);
+         Real d = fabs (qscores[i].yl- ins[LEFT]) + fabs (qscores[i].yr - ins[RIGHT]);
          if (d < mindist)
            {
              best_idx = i;
-             mindist= d;
+             mindist = d;
            }
        }
       if (mindist > 1e5)
@@ -305,7 +303,7 @@ Beam::quanting (SCM smob)
                    ly_interval2scm (Drul_array<Real> (qscores[best_idx].yl,
                                                       qscores[best_idx].yr)));
 #if DEBUG_QUANTING
-  if (to_boolean (me->get_paper ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"))))
+  if (to_boolean (me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"))))
     {
       qscores[best_idx].score_card_ += to_string ("i%d", best_idx);
       
@@ -331,7 +329,7 @@ Beam::score_stem_lengths (Link_array<Grob> const &stems,
   Drul_array<Real> score (0, 0);
   Drul_array<int> count (0, 0);
   
-  for (int i=0; i < stems.size (); i++)
+  for (int i = 0; i < stems.size (); i++)
     {
       Grob* s = stems[i];
       if (Stem::is_invisible (s))
@@ -408,23 +406,10 @@ Beam::score_slopes_dy (Real yl, Real yr,
    dem += shrink_extra_weight (fabs (dy_damp) - fabs (dy), 1.5)
      * slope_penalty;
 
-#if 0
-   /*
-     almost zero slopes look like errors in horizontal beams. 
-    */
-   /*
-     This causes too much problems, because horizontal depends on
-     horizontal spacing details.  These errors should be dealt with
-     through concaveness. --hwn.
-    */
-   if (fabs (dy) > 1e-3
-       && fabs (dy / dx) < ROUND_TO_ZERO_SLOPE)
-     dem += ROUND_TO_ZERO_POINTS;
-#endif
-   
    return dem;
 }
 
+
 static Real
 my_modf (Real x)
 {
@@ -446,14 +431,15 @@ Beam::score_forbidden_quants (Real yl, Real yr,
                              Direction ldir, Direction rdir)
 {
   Real dy = yr - yl;
-  Drul_array<Real> y (yl,yr);
-  Drul_array<Direction> dirs (ldir,rdir);
+  Drul_array<Real> y (yl, yr);
+  Drul_array<Direction> dirs (ldir, rdir);
   
   Real extra_demerit = SECONDARY_BEAM_DEMERIT / (beam_counts[LEFT] >? beam_counts[RIGHT]);
 
   Direction d = LEFT;
   Real dem = 0.0;
   
+
   do
     {
       for (int j = 1; j <= beam_counts[d]; j++)
@@ -463,10 +449,10 @@ Beam::score_forbidden_quants (Real yl, Real yr,
          /*
            The 2.2 factor is to provide a little leniency for
            borderline cases. If we do 2.0, then the upper outer line
-           will be in the gap of the (2,sit) quant, leading to a
+           will be in the gap of the (2, sit) quant, leading to a
            false demerit.
-          */
-         Real gap1 =  y[d] - stem_dir * ((j-1) * beam_translation + thickness / 2 - slt/2.2 );
+         */
+         Real gap1 = y[d] - stem_dir * ((j-1) * beam_translation + thickness / 2 - slt/2.2 );
          Real gap2 = y[d] - stem_dir * (j * beam_translation - thickness / 2 + slt/2.2);
 
          Interval gap;
@@ -476,7 +462,18 @@ Beam::score_forbidden_quants (Real yl, Real yr,
          for (Real k = - radius ;
               k <= radius + BEAM_EPS; k += 1.0) 
            if (gap.contains (k))
-             dem += extra_demerit;
+             {
+               Real dist = fabs (gap[UP]-k) <? fabs (gap[DOWN] - k);
+
+               /*
+                 this parameter is tuned to grace-stem-length.ly
+               */
+               Real fixed_demerit = 0.4;
+               
+               dem += extra_demerit
+                 * (fixed_demerit +
+                    (1-fixed_demerit) * (dist / gap.length())* 2);
+             }
        }
     }
   while ((flip (&d))!= LEFT);