]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/beam-quanting.cc
* Documentation/user/music-glossary.tely: add @omf tags
[lilypond.git] / lily / beam-quanting.cc
index 890e7abe1fff2e181169693e408a67789fd07130..29a42034b37aa686686505e7f2f624e2641394c2 100644 (file)
@@ -1,3 +1,15 @@
+/*
+  beam-quanting.cc -- implement Beam quanting functions
+  
+  source file of the GNU LilyPond music typesetter
+  
+  (c)  1997--2003 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);
@@ -160,7 +172,10 @@ Beam::quanting (SCM smob)
    */
   if (knee_b)
     region_size += 2;
-  
+
+  /*
+    Asymetry ? should run to <= region_size ?
+   */
   for (int i = -region_size ; i < region_size; i++)
     for (int j = 0; j < num_quants; j++)
       {
@@ -181,13 +196,10 @@ 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
@@ -197,9 +209,7 @@ Beam::quanting (SCM smob)
 
   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--;)
@@ -211,7 +221,7 @@ Beam::quanting (SCM smob)
                                     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)
       {
@@ -223,7 +233,7 @@ Beam::quanting (SCM smob)
                                 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 +245,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;
@@ -250,10 +260,10 @@ Beam::score_stem_lengths (Link_array<Grob>stems,
                          bool knee, 
                          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];
@@ -264,25 +274,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 >? (d * (info.shortest_y_ - current_y)));
-
-      Real ideal_score = shrink_extra_weight (d * current_y  - d * info.ideal_y_);
+      score[d] += limit_penalty * (0 >? (d * (info.shortest_y_ - current_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.
-       
-       */
+      Real ideal_diff = d * (current_y - info.ideal_y_);
+      Real ideal_score = shrink_extra_weight (ideal_diff, 1.5);
+      
+      /* 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] += STEM_LENGTH_DEMERIT_FACTOR * ideal_score;
+      
+      score[d] += length_pen * ideal_score;
 
       count[d] ++;
     }
@@ -313,14 +321,14 @@ Beam::score_slopes_dy (Real yl, Real yr,
 
    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;
 }
 
@@ -368,8 +376,8 @@ Beam::score_forbidden_quants (Real yl, Real yr,
       /*
        Can't we simply compute the distance between the nearest
        staffline and the secondary beam? That would get rid of the
-       silly case analysis here (which is probably not when we have
-       different beam-thicknesses.)
+       silly case analysis here (which is probably not valid when we
+       have different beam-thicknesses.)
 
        --hwn
        */