]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/beam.cc
* lily/beam.cc (least_squares): Add comment.
[lilypond.git] / lily / beam.cc
index 9d86ab4157465744370258bf1b9c7c9b16227cf2..5f19968e65f345140df834d6acbd2dda5989aab3 100644 (file)
@@ -11,9 +11,6 @@
 /*
 TODO:
 
-
-  * Junk stem_info.
-
   * Use Number_pair i.s.o Interval to represent (yl, yr).
   
   - Determine auto knees based on positions if it's set by the user.
@@ -70,29 +67,24 @@ Beam::add_stem (Grob *me, Grob *s)
 }
 
 
-/*
-  this returns the translation between 2 adjoining beams.
- */
+/* Return the translation between 2 adjoining beams. */
 Real
 Beam::get_beam_translation (Grob *me)
 {
   SCM func = me->get_grob_property ("space-function");
-  SCM s = gh_call2 (func, me->self_scm (), gh_int2scm (get_beam_count (me)));
+  SCM s = gh_call2 (func, me->self_scm (), scm_int2num (get_beam_count (me)));
   return gh_scm2double (s);
 }
 
-/*
-  Maximum beam_count.
- */
+/* Maximum beam_count. */
 int
 Beam::get_beam_count (Grob *me) 
 {
   int m = 0;
   for (SCM s = me->get_grob_property ("stems"); gh_pair_p (s); s = ly_cdr (s))
     {
-      Grob *sc = unsmob_grob (ly_car (s));
-      
-      m = m >? (Stem::beam_multiplicity (sc).length () + 1);
+      Grob *stem = unsmob_grob (ly_car (s));
+      m = m >? (Stem::beam_multiplicity (stem).length () + 1);
     }
   return m;
 }
@@ -146,7 +138,7 @@ Beam::before_line_breaking (SCM smob)
        {
          me->warning (_ ("Beam has less than two stems. Removing beam."));
 
-         unsmob_grob (gh_car (stems))->remove_grob_property ("beam");
+         unsmob_grob (gh_car (stems))->set_grob_property ("beam", SCM_EOL);
          me->suicide ();
 
          return SCM_UNSPECIFIED;
@@ -161,7 +153,7 @@ Beam::before_line_breaking (SCM smob)
     {
       Direction d = get_default_dir (me);
 
-      consider_auto_knees (me, d);
+      consider_auto_knees (me);
       set_stem_directions (me, d);
 
       connect_beams (me);
@@ -203,7 +195,7 @@ position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
       for ( SCM s = gh_car (right_beaming); gh_pair_p (s); s = gh_cdr (s))
        {
          int k = - right_dir * gh_scm2int (gh_car (s)) + i;
-         if (scm_memq (gh_int2scm (k), left_beaming) != SCM_BOOL_F)
+         if (scm_memq (scm_int2num (k), left_beaming) != SCM_BOOL_F)
            count ++;
        }
 
@@ -254,7 +246,7 @@ Beam::connect_beams (Grob *me)
                    start_point - this_dir * gh_scm2int (gh_car (s));
 
                  new_slice.add_point (new_beam_pos);
-                 gh_set_car_x (s, gh_int2scm (new_beam_pos));
+                 gh_set_car_x (s, scm_int2num (new_beam_pos));
                }
 
 
@@ -271,7 +263,7 @@ Beam::connect_beams (Grob *me)
          for (; gh_pair_p (s); s = gh_cdr (s))
            {
              int np = - this_dir * gh_scm2int (gh_car(s));
-             gh_set_car_x (s, gh_int2scm (np));
+             gh_set_car_x (s, scm_int2num (np));
              last_int.add_point (np);
            }
        }
@@ -417,7 +409,7 @@ Beam::brew_molecule (SCM grob)
                  int t = Stem::duration_log (st); 
 
                  SCM proc = me->get_grob_property ("flag-width-function");
-                 SCM result = gh_call1 (proc, gh_int2scm (t));
+                 SCM result = gh_call1 (proc, scm_int2num (t));
                  nw_f = gh_scm2double (result);
                }
              
@@ -494,8 +486,8 @@ Beam::get_default_dir (Grob *me)
   count[UP]  = count[DOWN] = 0;
   Direction d = DOWN;
 
-  Link_array<Item> stems=
-       Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
+  Link_array<Grob> stems=
+       Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems");
 
   for (int i=0; i <stems.size (); i++)
     do {
@@ -514,10 +506,10 @@ Beam::get_default_dir (Grob *me)
   
   SCM func = me->get_grob_property ("dir-function");
   SCM s = gh_call2 (func,
-                   gh_cons (gh_int2scm (count[UP]),
-                            gh_int2scm (count[DOWN])),
-                   gh_cons (gh_int2scm (total[UP]),
-                            gh_int2scm (total[DOWN])));
+                   gh_cons (scm_int2num (count[UP]),
+                            scm_int2num (count[DOWN])),
+                   gh_cons (scm_int2num (total[UP]),
+                            scm_int2num (total[DOWN])));
 
   if (gh_number_p (s) && gh_scm2int (s))
     return to_dir (s);
@@ -533,31 +525,18 @@ Beam::get_default_dir (Grob *me)
 void
 Beam::set_stem_directions (Grob *me, Direction d)
 {
-  Link_array<Item> stems
-    =Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
+  Link_array<Grob> stems
+    =Pointer_group_interface__extract_grobs (me, (Grob*) 0, "stems");
   
   for (int i=0; i <stems.size (); i++)
     {
       Grob *s = stems[i];
-      /* For knees, non-forced stems should probably have their
-        natural direction. In any case, when knee, beam direction is
-        foe.
-        
-        TODO: for x staff knees, set direction pointing to 'the
-        other' staff, rather than natural.
-      */
-      if (knee_b(me))
-       {
-         Stem::get_direction (s); // this actually sets it, if necessary
-       }
-      else
-       {
-         SCM force = s->remove_grob_property ("dir-forced");
-         if (!gh_boolean_p (force) || !gh_scm2bool (force))
-           Directional_element_interface::set (s, d);
-       }
+  
+      SCM forcedir = s->get_grob_property ("direction");
+      if (!to_dir (forcedir))
+       Directional_element_interface::set (s, d);
     }
-} 
+}
 
 /*
   A union of intervals in the real line.
@@ -599,13 +578,13 @@ struct Int_set
            before[RIGHT] = s[LEFT];
            after[LEFT] = s[RIGHT];
 
-           if (!before.empty_b())
+           if (!before.empty_b() && before.length () > 0.0)
              {
                allowed_regions_.insert (before, i);
                i++;
              }
            allowed_regions_.del (i);
-           if (!after.empty_b ())
+           if (!after.empty_b () && after.length () > 0.0)
              {
                allowed_regions_.insert (after, i);
                i++;
@@ -625,7 +604,7 @@ struct Int_set
   knee-beams, should set the directions manually.
  */
 void
-Beam::consider_auto_knees (Grob* me, Direction d)
+Beam::consider_auto_knees (Grob* me)
 {
   SCM scm = me->get_grob_property ("auto-knee-gap");
   if (!gh_number_p (scm))
@@ -636,7 +615,6 @@ Beam::consider_auto_knees (Grob* me, Direction d)
   Int_set gaps;
 
   gaps.set_full ();
-  
 
   Link_array<Grob> stems=
     Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems");
@@ -650,20 +628,25 @@ Beam::consider_auto_knees (Grob* me, Direction d)
       Grob* stem = stems[i];
       if (Stem::invisible_b (stem))
        continue;
-      
 
       Interval hps = Stem::head_positions (stem);
-
       if(!hps.empty_b())
        {
          hps[LEFT] += -1;
          hps[RIGHT] += 1; 
          hps *= staff_space * 0.5 ;
+
+         /*
+           We could subtract beam Y position, but this routine only
+           sets stem directions, a constant shift does not have an
+           influence.
+           
+          */
          hps += stem->relative_coordinate (common, Y_AXIS);
-      
-         if (to_boolean (stem->get_grob_property ("dir-forced")))
+
+         if (to_dir (stem->get_grob_property ("direction")))
            {
-             Direction stemdir =Directional_element_interface::get (stem);
+             Direction stemdir = to_dir (stem->get_grob_property ("direction"));
              hps[-stemdir] = - stemdir * infinity_f;
            }
        }
@@ -707,13 +690,7 @@ Beam::consider_auto_knees (Grob* me, Direction d)
          Direction d =  (hps.center () < max_gap.center()) ?
            UP : DOWN ;
          
-         stem->set_grob_property ("direction", gh_int2scm (d));
-
-         /*
-           UGH. Check why we still need dir-forced; I think we can
-           junk it.
-          */
-         stem->set_grob_property ("dir-forced", SCM_BOOL_T);
+         stem->set_grob_property ("direction", scm_int2num (d));
          
          hps.intersect (max_gap);
          assert (hps.empty_b () || hps.length () < 1e-6 );
@@ -742,19 +719,19 @@ Beam::set_stem_shorten (Grob *me)
   if (knee_b(me))
     return ;
   
-  Real forced_fraction = forced_stem_count (me) / visible_stem_count (me);
+  Real forced_fraction = 1.0 * forced_stem_count (me)
+    / visible_stem_count (me);
 
   int beam_count = get_beam_count (me);
 
-  SCM shorten = me->get_grob_property ("beamed-stem-shorten");
-  if (shorten == SCM_EOL)
+  SCM shorten_list = me->get_grob_property ("beamed-stem-shorten");
+  if (shorten_list == SCM_EOL)
     return;
 
-  int sz = scm_ilength (shorten);
-  
   Real staff_space = Staff_symbol_referencer::staff_space (me);
-  SCM shorten_elt = scm_list_ref (shorten,
-                                 gh_int2scm (beam_count <? (sz - 1)));
+  
+  SCM shorten_elt =
+    robust_list_ref (beam_count -1, shorten_list);
   Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
 
   /* your similar cute comment here */
@@ -818,9 +795,9 @@ Beam::least_squares (SCM smob)
   Grob *fvs  = first_visible_stem (me);
   Grob *lvs  = last_visible_stem (me);
   
-  Interval ideal (Stem::calc_stem_info (fvs).ideal_y_
+  Interval ideal (Stem::get_stem_info (fvs).ideal_y_
                  + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
-                 Stem::calc_stem_info (lvs).ideal_y_
+                 Stem::get_stem_info (lvs).ideal_y_
                  + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
   
   Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
@@ -842,23 +819,18 @@ Beam::least_squares (SCM smob)
       Interval chord (Stem::chord_start_y (first_visible_stem (me)),
                      Stem::chord_start_y (last_visible_stem (me)));
 
+      /* Simple beams (2 stems) on middle line should be allowed to be
+        slightly sloped.
+        
+        However, if both stems reach middle line,
+        ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
 
-      /*
-       TODO -- use scoring for this.
-
-       complicated, because we take stem-info.ideal for determining
-       beam slopes.
-       */
-      /* Make simple beam on middle line have small tilt */
+        For that case, we apply artificial slope */
       if (!ideal[LEFT] && chord.delta () && count == 2)
        {
-
-         /*
-           FIXME. -> UP
-         */
+         /* FIXME. -> UP */
          Direction d = (Direction) (sign (chord.delta ()) * UP);
          pos[d] = gh_scm2double (me->get_grob_property ("thickness")) / 2;
-         //                * dir;
          pos[-d] = - pos[d];
        }
       else
@@ -879,7 +851,7 @@ Beam::least_squares (SCM smob)
          if (Stem::invisible_b (s))
            continue;
          ideals.push (Offset (x_posns[i],
-                              Stem::calc_stem_info (s).ideal_y_
+                              Stem::get_stem_info (s).ideal_y_
                               + s->relative_coordinate (commony, Y_AXIS)
                               - my_y));
        }
@@ -955,7 +927,7 @@ Beam::shift_region_to_valid (SCM grob)
       Direction d = Stem::get_direction (s);
 
       Real left_y =
-       Stem::calc_stem_info (s).shortest_y_
+       Stem::get_stem_info (s).shortest_y_
        - dydx * x_posns [i];
 
       /*
@@ -1172,10 +1144,8 @@ where_are_the_whole_beams(SCM beaming)
   return l;
 }
 
-/*
-  Calculate the Y position of the stem-end, given the Y-left, Y-right
-  in POS for stem S. This Y position is relative to S.
- */
+/* Return the Y position of the stem-end, given the Y-left, Y-right
+   in POS for stem S.  This Y position is relative to S. */
 Real
 Beam::calc_stem_y (Grob *me, Grob* s, Grob ** common,
                   Real xl, Real xr,
@@ -1322,7 +1292,9 @@ Beam::forced_stem_count (Grob *me)
       if (Stem::invisible_b (s))
        continue;
 
-      if (((int)Stem::chord_start_y (s)) 
+      /* I can imagine counting those boundaries as a half forced stem,
+        but let's count them full for now. */
+      if (abs (Stem::chord_start_y (s)) > 0.1
         && (Stem::get_direction (s) != Stem::get_default_dir (s)))
         f++;
     }
@@ -1444,7 +1416,7 @@ Beam::rest_collision_callback (SCM element_smob, SCM axis)
 }
 
 bool
-Beam::knee_b (Grob*me)
+Beam::knee_b (Grob* me)
 {
   SCM k = me->get_grob_property ("knee");
   if (gh_boolean_p (k))
@@ -1469,6 +1441,26 @@ Beam::knee_b (Grob*me)
   return knee;
 }
 
+int
+Beam::get_direction_beam_count (Grob *me, Direction d )
+{
+  Link_array<Grob>stems = 
+    Pointer_group_interface__extract_grobs (me, (Grob*) 0, "stems");
+  int bc = 0;
+  
+  for (int i = stems.size (); i--;)
+    {
+      /*
+       Should we take invisible stems into account?
+       */
+      if (Stem::get_direction (stems[i]) == d)
+        bc = bc >? (Stem::beam_multiplicity (stems[i]).length () + 1);
+    }
+
+  return bc;
+}
+
+
 ADD_INTERFACE (Beam, "beam-interface",
   "A beam.