]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/beam-quanting.cc
* lily/scores.cc (Input_file_settings): Initalize global_header_.
[lilypond.git] / lily / beam-quanting.cc
index efe670738a40e2986bfd61e9de616f3050f6fd3f..a26b09c3701b7750fee2b8264a72751d03e28118 100644 (file)
@@ -1,3 +1,15 @@
+/*
+  beam-quanting.cc -- implement Beam quanting functions
+  
+  source file of the GNU LilyPond music typesetter
+  
+  (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  Jan Nieuwenhuizen <janneke@gnu.org>
+  
+*/
+
+
+
 #include <math.h>
 
 #include "grob.hh"
@@ -20,9 +32,9 @@ const int IDEAL_SLOPE_FACTOR = 10;
 
 
 static Real
-shrink_extra_weight (Real x)
+shrink_extra_weight (Real x, Real fac)
 {
-  return fabs (x) * ((x < 0) ? 1.5 : 1.0);
+  return fabs (x) * ((x < 0) ? fac : 1.0);
 }
 
 
@@ -74,7 +86,7 @@ Beam::quanting (SCM smob)
 
   Real ss = Staff_symbol_referencer::staff_space (me);
   Real thickness = gh_scm2double (me->get_grob_property ("thickness")) / ss;
-  Real slt = me->paper_l ()->get_var ("linethickness") / ss;
+  Real slt = me->get_paper ()->get_var ("linethickness") / ss;
 
 
   SCM sdy = me->get_grob_property ("least-squares-dy");
@@ -133,7 +145,7 @@ Beam::quanting (SCM smob)
   for (int i= 0; i < stems.size(); i++)
     {
       Grob*s = stems[i];
-      stem_infos.push (Stem::calc_stem_info (s));
+      stem_infos.push (Stem::get_stem_info (s));
       dirs_found[stem_infos.top ().dir_] = true;
 
       bool f = french && i > 0&& (i < stems.size  () -1);
@@ -181,37 +193,32 @@ Beam::quanting (SCM smob)
        qscores.push (qs);
       }
 
-  /*
-    This is a longish function, but we don't separate this out into
-    neat modular separate subfunctions, as the subfunctions would be
-    called for many values of YL, YR. By precomputing various
-    parameters outside of the loop, we can save a lot of time.
-  */
-
+  /* This is a longish function, but we don't separate this out into
+     neat modular separate subfunctions, as the subfunctions would be
+     called for many values of YL, YR. By precomputing various
+     parameters outside of the loop, we can save a lot of time. */
   for (int i = qscores.size (); i--;)
     {
       qscores[i].demerits
-       += score_slopes_dy (me, qscores[i].yl, qscores[i].yr,
+       += score_slopes_dy (qscores[i].yl, qscores[i].yr,
                            dy_mus, yr- yl, xstaff); 
     }
 
   Real rad = Staff_symbol_referencer::staff_radius (me);
   int beam_count = get_beam_count (me);
-  Real beam_translation = beam_count < 4
-    ? (2*ss + slt - thickness) / 2.0
-     : (3*ss + slt - thickness) / 3.0;
+  Real beam_translation = get_beam_translation (me);
 
   Real reasonable_score = (knee_b) ? 200000 : 100;
   for (int i = qscores.size (); i--;)
     if (qscores[i].demerits < reasonable_score)
       {
        qscores[i].demerits
-         += score_forbidden_quants (me, qscores[i].yl, qscores[i].yr,
+         += score_forbidden_quants (qscores[i].yl, qscores[i].yr,
                                     rad, slt, thickness, beam_translation,
                                     beam_count, ldir, rdir); 
       }
 
-
+  ; /* silly gdb thinks best_idx is inside for loop. */
   for (int i = qscores.size (); i--;)
     if (qscores[i].demerits < reasonable_score)
       {
@@ -220,10 +227,10 @@ Beam::quanting (SCM smob)
                                 base_lengths, stem_xposns,
                                 xl, xr,
                                 knee_b,
-                                me, qscores[i].yl, qscores[i].yr);
+                                qscores[i].yl, qscores[i].yr);
       }
 
-
+  ; /* silly gdb thinks best_idx is inside for loop. */
   int best_idx = best_quant_score_idx (qscores);
   me->set_grob_property ("positions",
                         gh_cons (gh_double2scm (qscores[best_idx].yl),
@@ -235,7 +242,7 @@ Beam::quanting (SCM smob)
   // debug quanting
   me->set_grob_property ("quant-score",
                         gh_double2scm (qscores[best_idx].demerits));
-  me->set_grob_property ("best-idx", gh_int2scm (best_idx));
+  me->set_grob_property ("best-idx", scm_int2num (best_idx));
 #endif
 
   return SCM_UNSPECIFIED;
@@ -248,13 +255,12 @@ Beam::score_stem_lengths (Link_array<Grob>stems,
                          Array<Real> stem_xs,
                          Real xl, Real xr, 
                          bool knee, 
-                         Grob*me,
                          Real yl, Real yr)
 {
-  Real pen = STEM_LENGTH_LIMIT_PENALTY;
-
+  Real limit_penalty = STEM_LENGTH_LIMIT_PENALTY;
   Drul_array<Real> score (0, 0);
   Drul_array<int> count (0, 0);
+  
   for (int i=0; i < stems.size (); i++)
     {
       Grob* s = stems[i];
@@ -265,15 +271,23 @@ Beam::score_stem_lengths (Link_array<Grob>stems,
       Real dx = xr-xl;
       Real beam_y = yr *(x - xl)/dx + yl * ( xr - x)/dx;
       Real current_y = beam_y + base_stem_ys[i];
+      Real length_pen = STEM_LENGTH_DEMERIT_FACTOR;
       
       Stem_info info = stem_infos[i];
       Direction d = info.dir_;
 
-      score[d] += pen
-       * (0 >? (info.dir_ * (info.shortest_y_ - current_y)));
+      score[d] += limit_penalty * (0 >? (d * (info.shortest_y_ - current_y)));
+      
+      Real ideal_diff = d * (current_y - info.ideal_y_);
+      Real ideal_score = shrink_extra_weight (ideal_diff, 1.5);
       
-      score[d] += STEM_LENGTH_DEMERIT_FACTOR
-       * shrink_extra_weight (d * current_y  - info.dir_ * info.ideal_y_);
+      /* We introduce a power, to make the scoring strictly
+         convex. Otherwise a symmetric knee beam (up/down/up/down)
+         does not have an optimum in the middle. */
+      if (knee)
+       ideal_score = pow (ideal_score, 1.1);
+      
+      score[d] += length_pen * ideal_score;
 
       count[d] ++;
     }
@@ -287,8 +301,7 @@ Beam::score_stem_lengths (Link_array<Grob>stems,
 }
 
 Real
-Beam::score_slopes_dy (Grob *me,
-                      Real yl, Real yr,
+Beam::score_slopes_dy (Real yl, Real yr,
                       Real dy_mus, Real dy_damp,
                       bool xstaff)
 {
@@ -305,14 +318,14 @@ Beam::score_slopes_dy (Grob *me,
 
    Real slope_penalty = IDEAL_SLOPE_FACTOR;
 
-   /*
-     Xstaff beams tend to use extreme slopes to get short stems. We
-     put in a penalty here.
-   */
+   /* Xstaff beams tend to use extreme slopes to get short stems. We
+      put in a penalty here. */
    if (xstaff)
      slope_penalty *= 10;
 
-   dem += shrink_extra_weight (fabs (dy_damp) - fabs (dy))* slope_penalty;
+   /* Huh, why would a too steep beam be better than a too flat one ? */
+   dem += shrink_extra_weight (fabs (dy_damp) - fabs (dy), 1.5)
+     * slope_penalty;
    return dem;
 }
 
@@ -323,8 +336,7 @@ my_modf (Real x)
 }
 
 Real
-Beam::score_forbidden_quants (Grob*me,
-                             Real yl, Real yr,
+Beam::score_forbidden_quants (Real yl, Real yr,
                              Real rad,
                              Real slt,
                              Real thickness, Real beam_translation,