]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/beam.cc
release: 1.3.105
[lilypond.git] / lily / beam.cc
index 33584530760d4a1f8db774796890ec7117a2d71e..f94c018083f6e3c8617c5649e13a09bca4c45f08 100644 (file)
@@ -3,64 +3,62 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
     Jan Nieuwenhuizen <janneke@gnu.org>
 
 */
 
 /*
   [TODO]
+  * shorter! (now +- 1000 lines)
     * less hairy code
     * move paper vars to scm
 
+  remove *-hs variables, and do all y-position stuff in staff-space.
 */
 
+
+#include <math.h> // tanh.
+
+#include "molecule.hh" 
+#include "directional-element-interface.hh"
 #include "beaming.hh"
-#include "dimensions.hh"
 #include "beam.hh"
 #include "misc.hh"
-#include "debug.hh"
-#include "leastsquares.hh"
+#include "least-squares.hh"
 #include "stem.hh"
 #include "paper-def.hh"
 #include "lookup.hh"
 #include "group-interface.hh"
 #include "staff-symbol-referencer.hh"
 #include "cross-staff.hh"
-
-Beam::Beam ()
-{
-  Group_interface g (this, "stems");
-  g.set_interface ();
-}
+#include "item.hh"
+#include "spanner.hh"
+#include "warn.hh"
 
 void
-Beam::add_stem (Stem*s)
+Beam::add_stem (Score_element*me, Score_element*s)
 {
-  Group_interface gi (this, "stems");
-  gi.add_element (s);
+  Pointer_group_interface:: add_element(me, "stems", s);
   
-  s->add_dependency (this);
+  s->add_dependency (me);
 
-  assert (!s->beam_l ());
-  s->set_elt_property ("beam", self_scm_);
+  assert (!Stem::beam_l (s));
+  s->set_elt_property ("beam", me->self_scm ());
 
-  if (!spanned_drul_[LEFT])
-    set_bounds (LEFT,s);
-  else
-    set_bounds (RIGHT,s);
+  add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (s));
 }
 
 int
-Beam::get_multiplicity () const
+Beam::get_multiplicity (Score_element*me) 
 {
   int m = 0;
-  for (SCM s = get_elt_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
+  for (SCM s = me->get_elt_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
     {
       Score_element * sc = unsmob_element (gh_car (s));
 
-      if (Stem * st = dynamic_cast<Stem*> (sc))
-       m = m >? st->beam_count (LEFT) >? st->beam_count (RIGHT);
+      if (Stem::has_interface (sc))
+       m = m >? Stem::beam_count (sc,LEFT) >? Stem::beam_count (sc,RIGHT);
     }
   return m;
 }
@@ -73,30 +71,30 @@ Beam::get_multiplicity () const
   [Alternatively, stems could set its own directions, according to
    their beam, during 'final-pre-processing'.]
  */
-void
-Beam::do_pre_processing ()
+MAKE_SCHEME_CALLBACK(Beam,before_line_breaking,1);
+SCM
+Beam::before_line_breaking (SCM smob)
 {
+  Score_element * me =  unsmob_element (smob);
+
   // Why?
-  if (visible_stem_count () < 2)
+  if (visible_stem_count (me) < 2)
     {
       warning (_ ("beam has less than two stems"));
-      set_elt_property ("transparent", SCM_BOOL_T);
     }
 
-  if (!get_direction ())
-    set_direction (get_default_dir ());
+  if (!Directional_element_interface::get (me))
+    Directional_element_interface::set (me, get_default_dir (me));
 
-  auto_knees ();
-  set_stem_directions ();
+  auto_knees (me);
+  set_stem_directions (me);
+  set_stem_shorten (me);
 
-  set_stem_shorten (); 
+  return SCM_EOL;
 }
 
-/*
- FIXME
- */
 Direction
-Beam::get_default_dir () const
+Beam::get_default_dir (Score_element*me) 
 {
   Drul_array<int> total;
   total[UP]  = total[DOWN] = 0;
@@ -104,12 +102,15 @@ Beam::get_default_dir () const
   count[UP]  = count[DOWN] = 0;
   Direction d = DOWN;
 
-  for (int i=0; i <stem_count (); i++)
+  Link_array<Item> stems=
+       Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
+
+  for (int i=0; i <stems.size (); i++)
     do { // HUH -- waar slaat dit op?
-      Stem *s = stem (i);
-      int current = s->get_direction () 
-       ? (1 + d * s->get_direction ())/2
-       : s->get_center_distance ((Direction)-d);
+      Score_element *s = stems[i];
+      Direction sd = Directional_element_interface::get (s);
+      int current = sd ? (1 + d * sd)/2
+       : Stem::get_center_distance (s, (Direction)-d);
 
       if (current)
        {
@@ -119,46 +120,20 @@ Beam::get_default_dir () const
 
     } while (flip(&d) != DOWN);
   
-  /* 
-     [Ross] states that the majority of the notes dictates the
-     direction (and not the mean of "center distance")
-
-     But is that because it really looks better, or because he wants
-     to provide some real simple hands-on rules?
-     
-     We have our doubts, so we simply provide all sensible alternatives.
-
-     If dir is not determined: up (see stem::get_default_dir ()) */
-
-  Direction beam_dir = CENTER;
-  Direction neutral_dir = (Direction)(int)paper_l ()->get_var ("stem_default_neutral_direction");
-
-  SCM a = get_elt_property ("beam-dir-algorithm");
+  SCM func = me->get_elt_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])));
+
+  if (gh_number_p (s) && gh_scm2int (s))
+    return to_dir (s);
   
-  if (a == ly_symbol2scm ("majority")) // should get default from paper.
-    beam_dir = (count[UP] == count[DOWN]) ? neutral_dir 
-      : (count[UP] > count[DOWN]) ? UP : DOWN;
-  else if (a == ly_symbol2scm ("mean"))
-    // mean center distance
-    beam_dir = (total[UP] == total[DOWN]) ? neutral_dir
-      : (total[UP] > total[DOWN]) ? UP : DOWN;
-  else if (a == ly_symbol2scm ("median"))
-    {
-      // median center distance
-      if (count[DOWN] && count[UP])
-       {
-         beam_dir = (total[UP] / count[UP] == total[DOWN] / count[DOWN]) 
-           ? neutral_dir 
-           : (total[UP] / count[UP] > total[DOWN] / count[DOWN]) ? UP : DOWN;
-       }
-      else
-       {
-         beam_dir = (count[UP] == count[DOWN]) ? neutral_dir 
-           : (count[UP] > count[DOWN]) ? UP : DOWN;
-       }
-    }
-  
-  return beam_dir;
+  /*
+    If dir is not determined: get default
+  */
+  return to_dir (me->get_elt_property ("default-neutral-direction"));
 }
 
 
@@ -168,23 +143,26 @@ Beam::get_default_dir () const
        once stem gets cleaned-up.
  */
 void
-Beam::set_stem_directions ()
+Beam::set_stem_directions (Score_element*me)
 {
-  Direction d = get_direction ();
-  for (int i=0; i <stem_count (); i++)
+  Link_array<Item> stems
+    =Pointer_group_interface__extract_elements (me,  (Item*) 0, "stems");
+  Direction d = Directional_element_interface::get (me);
+  
+  for (int i=0; i <stems.size (); i++)
     {
-      Stem *s = stem (i);
+      Score_element *s = stems[i];
       SCM force = s->remove_elt_property ("dir-forced");
       if (!gh_boolean_p (force) || !gh_scm2bool (force))
-       s->set_direction (d);
+       Directional_element_interface ::set (s,d);
     }
 } 
 
 void
-Beam::auto_knees ()
+Beam::auto_knees (Score_element*me)
 {
-  if (!auto_knee ("auto-interstaff-knee-gap", true))
-    auto_knee ("auto-knee-gap", false);
+  if (!auto_knee (me,"auto-interstaff-knee-gap", true))
+    auto_knee (me, "auto-knee-gap", false);
 }
 
 /*
@@ -195,22 +173,28 @@ Beam::auto_knees ()
   don't set, or unset autoKneeGap/autoInterstaffKneeGap.
  */
 bool
-Beam::auto_knee (String gap_str, bool interstaff_b)
+Beam::auto_knee (Score_element*me, String gap_str, bool interstaff_b)
 {
   bool knee_b = false;
   int knee_y = 0;
-  SCM gap = get_elt_property (gap_str);
+  SCM gap = me->get_elt_property (gap_str.ch_C());
+
+  Direction d = Directional_element_interface::get (me);
+      Link_array<Item> stems=
+       Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
+  
   if (gh_number_p (gap))
     {
+      Spanner*sp = dynamic_cast<Spanner*> (me);
       int auto_gap_i = gh_scm2int (gap);
-      for (int i=1; i < stem_count (); i++)
+      for (int i=1; i < stems.size (); i++)
         {
-         bool is_b = (bool)(calc_interstaff_dist (stem (i), this
-           - calc_interstaff_dist (stem (i-1), this));
-         int l_y = (int)(stem (i-1)->chord_start_f ())
-           + (int)calc_interstaff_dist (stem (i-1), this);
-         int r_y = (int)(stem (i)->chord_start_f ())
-           + (int)calc_interstaff_dist (stem (i), this);
+         bool is_b = (bool)(calc_interstaff_dist (stems[i], sp
+           - calc_interstaff_dist (stems[i-1], sp));
+         int l_y = (int)(Stem::head_positions(stems[i-1])[d])
+           + (int)calc_interstaff_dist (stems[i-1], sp);
+         int r_y = (int)(Stem::head_positions(stems[i])[d])
+           + (int)calc_interstaff_dist (stems[i], sp);
          int gap_i = r_y - l_y;
 
          if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
@@ -223,12 +207,14 @@ Beam::auto_knee (String gap_str, bool interstaff_b)
     }
   if (knee_b)
     {
-      for (int i=0; i < stem_count (); i++)
+      for (int i=0; i < stems.size (); i++)
         {
-         int y = (int)(stem (i)->chord_start_f ())
-           + (int)calc_interstaff_dist (stem (i), this);
-         stem (i)->set_direction (y < knee_y ? UP : DOWN);
-         stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
+         Item *s = stems[i];     
+         int y = (int)(Stem::head_positions(s)[d])
+           + (int)calc_interstaff_dist (s, dynamic_cast<Spanner*> (me));
+
+         Directional_element_interface::set (s,y < knee_y ? UP : DOWN);
+         s->set_elt_property ("dir-forced", SCM_BOOL_T);
        }
     }
   return knee_b;
@@ -236,89 +222,111 @@ Beam::auto_knee (String gap_str, bool interstaff_b)
 
 /*
  Set stem's shorten property if unset.
- TODO: take some y-position (nearest?) into account
+ TODO:
+    take some y-position (chord/beam/nearest?) into account
+    scmify forced-fraction
  */
 void
-Beam::set_stem_shorten ()
+Beam::set_stem_shorten (Score_element*m)
 {
-  if (!visible_stem_count ())
+  Spanner*me = dynamic_cast<Spanner*> (m);
+  if (!visible_stem_count (me))
     return;
 
-  Real forced_fraction = forced_stem_count () / visible_stem_count ();
+  Real forced_fraction = forced_stem_count (me) / visible_stem_count (me);
   if (forced_fraction < 0.5)
     return;
 
-  int multiplicity = get_multiplicity ();
-  SCM shorten = scm_eval (scm_listify (
-    ly_symbol2scm ("beamed-stem-shorten"),
-    gh_int2scm (multiplicity), 
-    SCM_UNDEFINED));
-  Real shorten_f = gh_scm2double (shorten) 
-    * Staff_symbol_referencer_interface (this).staff_space ();
+  int multiplicity = get_multiplicity (me);
+
+  SCM shorten = me->get_elt_property ("beamed-stem-shorten");
+  if (shorten == SCM_EOL)
+    return;
 
-  /* cute, but who invented this -- how to customise ? */
+  int sz = scm_ilength (shorten);
+  
+  Real staff_space = Staff_symbol_referencer::staff_space (me);
+  SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
+  Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
+
+  /* cute, but who invented me -- how to customise ? */
   if (forced_fraction < 1)
     shorten_f /= 2;
 
-  for (int i=0; i < stem_count (); i++)
+  Link_array<Item> stems=
+    Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
+
+  for (int i=0; i < stems.size (); i++)
     {
-      Stem* s = stem (i);
-      if (s->invisible_b ())
+      Item* s = stems[i];
+      if (Stem::invisible_b (s))
         continue;
       if (gh_number_p (s->get_elt_property ("shorten")))
        s->set_elt_property ("shorten", gh_double2scm (shorten_f));
     }
 }
+
 /*
   Set elt properties height and y-position if not set.
   Adjust stem lengths to reach beam.
  */
-void
-Beam::do_post_processing ()
+MAKE_SCHEME_CALLBACK(Beam,after_line_breaking,1);
+SCM
+Beam::after_line_breaking (SCM smob)
 {
+  Score_element * me =  unsmob_element (smob);
+
   /* first, calculate y, dy */
   Real y, dy;
-  calc_position_and_height (&y, &dy);
-  if (suspect_slope_b (y, dy))
-    dy = 0;
+  calc_default_position_and_height (me, &y, &dy);
+  if (visible_stem_count (me))
+    {
+      if (suspect_slope_b (me, y, dy))
+       dy = 0;
 
-  Real damped_dy = calc_slope_damping_f (dy);
-  Real quantised_dy = quantise_dy_f (damped_dy);
+      Real damped_dy = calc_slope_damping_f (me, dy);
+      Real quantised_dy = quantise_dy_f (me, damped_dy);
 
-  y += (dy - quantised_dy) / 2;
-  dy = quantised_dy;
-  
+      y += (dy - quantised_dy) / 2;
+      dy = quantised_dy;
+    }
   /*
     until here, we used only stem_info, which acts as if dir=up
    */
-  y *= get_direction ();
-  dy *= get_direction ();
+  y *= Directional_element_interface::get (me);
+  dy *= Directional_element_interface::get (me);
 
-  /* set or read dy as necessary */
-  SCM s = get_elt_property ("height");
+
+  Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
+
+  /* weird: why do we do calc_position_and_height () ? regardless of
+     this setting?
+
+  */
+  /* check for user-override of dy */
+  SCM s = me->remove_elt_property ("height-hs");
   if (gh_number_p (s))
-    dy = gh_scm2double (s);
-  else
-    set_elt_property ("height", gh_double2scm (dy));
+    {
+      dy = gh_scm2double (s) * half_space;
+    }
+  me->set_elt_property ("height", gh_double2scm (dy));
 
-  /* set or read y as necessary */
-  s = get_elt_property ("y-position");
+  /* check for user-override of y */
+  s = me->remove_elt_property ("y-position-hs");
   if (gh_number_p (s))
     {
-      y = gh_scm2double (s);
-      set_stem_length (y, dy);
+      y = gh_scm2double (s) * half_space;
     }
   else
     { 
       /* we can modify y, so we should quantise y */
-      Real y_shift = check_stem_length_f (y, dy);
+      Real y_shift = check_stem_length_f (me, y, dy);
       y += y_shift;
-      y = quantise_y_f (y, dy, 0);
-      set_stem_length (y, dy);
-      y_shift = check_stem_length_f (y, dy);
+      y = quantise_y_f (me,y, dy, 0);
+      set_stem_length (me, y, dy);
+      y_shift = check_stem_length_f (me, y, dy);
 
-      Real internote_f = paper_l ()->get_var ("interline") / 2;
-      if (y_shift > internote_f / 4)
+      if (y_shift > half_space / 4)
        {
          y += y_shift;
 
@@ -327,64 +335,75 @@ Beam::do_post_processing ()
            request quanting the other way.
          */
          int quant_dir = 0;
-         if (abs (y_shift) > internote_f / 2)
-           quant_dir = sign (y_shift) * get_direction ();
-         y = quantise_y_f (y, dy, quant_dir);
-         set_stem_length (y, dy);
+         if (abs (y_shift) > half_space / 2)
+           quant_dir = sign (y_shift) * Directional_element_interface::get (me);
+         y = quantise_y_f (me, y, dy, quant_dir);
        }
-
-      set_elt_property ("y-position", gh_double2scm (y));
     }
+  // UGH. Y is not in staff position unit?
+  // Ik dacht datwe daar juist van weg wilden?
+  set_stem_length (me, y, dy);
+  me->set_elt_property ("y-position", gh_double2scm (y));
+
+  return SCM_UNSPECIFIED;
 }
 
 /*
   See Documentation/tex/fonts.doc
  */
 void
-Beam::calc_position_and_height (Real* y, Real* dy) const
+Beam::calc_default_position_and_height (Score_element*me,Real* y, Real* dy) 
 {
-  *y = *dy = 0;
-  if (visible_stem_count () <= 1)
+  *y = 0;
+  *dy = 0;  
+  if (visible_stem_count (me) <= 1)
     return;
 
-  Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
-  if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
+  Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
+  if (first_ideal == Stem::calc_stem_info (last_visible_stem (me)).idealy_f_)
     {
       *dy = 0;
       *y = first_ideal;
       return;
     }
 
-  Least_squares ls;
-  Real x0 = first_visible_stem ()->hpos_f ();
-  for (int i=0; i < stem_count (); i++)
+  Array<Offset> ideals;
+
+  // ugh -> use commonx
+  Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
+  Link_array<Item> stems=
+    Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
+
+  for (int i=0; i < stems.size (); i++)
     {
-      Stem* s = stem (i);
-      if (s->invisible_b ())
+      Item* s = stems[i];
+      if (Stem::invisible_b (s))
         continue;
-      ls.input.push (Offset (s->hpos_f () - x0, 
-        s->calc_stem_info ().idealy_f_));
+      ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0, 
+                          Stem::calc_stem_info (s).idealy_f_));
     }
   Real dydx;
-  ls.minimise (dydx, *y); // duh, takes references
+  minimise_least_squares (&dydx, y, ideals); // duh, takes references
 
-  Real dx = last_visible_stem ()->hpos_f () - x0;
+  Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
   *dy = dydx * dx;
 }
 
 bool
-Beam::suspect_slope_b (Real y, Real dy) const
+Beam::suspect_slope_b (Score_element*me, Real y, Real dy) 
 {
+  /* first, calculate y, dy */
   /*
     steep slope running against lengthened stem is suspect
   */
-  Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
-  Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
-  Real lengthened = paper_l ()->get_var ("beam_lengthened");
-  Real steep = paper_l ()->get_var ("beam_steep_slope");
+  Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
+  Real last_ideal = Stem::calc_stem_info (last_visible_stem (me)).idealy_f_;
+  Real lengthened = gh_scm2double (me->get_elt_property ("outer-stem-length-limit"));
+  Real steep = gh_scm2double (me->get_elt_property ("slope-limit"));
 
-  Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
-  Real dydx = dy/dx;
+  // ugh -> use commonx
+  Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
+  Real dydx = dy && dx ? dy/dx : 0;
 
   if (((y - first_ideal > lengthened) && (dydx > steep))
       || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
@@ -400,18 +419,17 @@ Beam::suspect_slope_b (Real y, Real dy) const
   corresponds with some tables in [Wanske]
 */
 Real
-Beam::calc_slope_damping_f (Real dy) const
+Beam::calc_slope_damping_f (Score_element*me,Real dy) 
 {
-  SCM damp = get_elt_property ("damping"); // remove?
-  int damping = 1;             // ugh.
-  if (gh_number_p (damp))
-    damping = gh_scm2int (damp);
+  SCM damp = me->get_elt_property ("damping"); 
+  int damping = gh_scm2int (damp);
 
   if (damping)
     {
-      Real dx = last_visible_stem ()->hpos_f ()
-       - first_visible_stem ()->hpos_f ();
-      Real dydx = dy/dx;
+  // ugh -> use commonx
+      Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS)
+       - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
+      Real dydx = dy && dx ? dy/dx : 0;
       dydx = 0.6 * tanh (dydx) / damping;
       return dydx * dx;
     }
@@ -419,119 +437,135 @@ Beam::calc_slope_damping_f (Real dy) const
 }
 
 Real
-Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
+Beam::calc_stem_y_f (Score_element*me,Item* s, Real y, Real dy) 
 {
-  Real beam_f = gh_scm2double (get_elt_property ("beam-thickness"));
-  int   multiplicity = get_multiplicity ();
+  int beam_multiplicity = get_multiplicity (me);
+  int stem_multiplicity = (Stem::flag_i (s) - 2) >? 0;
+
+  SCM space_proc = me->get_elt_property ("space-function");
+  SCM space = gh_call1 (space_proc, gh_int2scm (beam_multiplicity));
 
+  Real thick = gh_scm2double (me->get_elt_property ("thickness")) ;
+  Real interbeam_f = gh_scm2double (space) ;
 
-  Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
-  Real x0 = first_visible_stem ()->hpos_f ();
-  Real dx = last_visible_stem ()->hpos_f () - x0;
-  Real stem_y = (s->hpos_f () - x0) / dx * dy + y;
+  // ugh -> use commonx
+  Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
+  Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
+  Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
 
   /* knee */
-  if (get_direction () != s->get_direction ())
-    {
-      stem_y -= get_direction () * (beam_f / 2
-       + (multiplicity - 1) * interbeam_f);
+   Direction dir  = Directional_element_interface::get (me);
+   Direction sdir = Directional_element_interface::get (s);
+   
+    /* knee */
+   if (dir!= sdir)
+      {
+       stem_y -= dir 
+       * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
+
 
-      Staff_symbol_referencer_interface me (s);
-      Staff_symbol_referencer_interface last (last_visible_stem ());
       
-      if ((s != first_visible_stem ())
-         && me.staff_symbol_l () != last.staff_symbol_l ())
-       stem_y += get_direction () 
-                 * (multiplicity - (s->flag_i () - 2) >? 0) * interbeam_f;
-    }
+      // huh, why not for first visible?
+       if (Staff_symbol_referencer::staff_symbol_l (s)
+          != Staff_symbol_referencer::staff_symbol_l (last_visible_stem (me)))
+        stem_y += Directional_element_interface::get (me)
+          * (beam_multiplicity - stem_multiplicity) * interbeam_f;
+      }
+
   return stem_y;
 }
 
 Real
-Beam::check_stem_length_f (Real y, Real dy) const
+Beam::check_stem_length_f (Score_element*me,Real y, Real dy) 
 {
   Real shorten = 0;
   Real lengthen = 0;
-  for (int i=0; i < stem_count (); i++)
+  Direction dir = Directional_element_interface::get (me);
+
+  Link_array<Item> stems=
+    Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
+
+  for (int i=0; i < stems.size(); i++)
     {
-      Stem* s = stem (i);
-      if (s->invisible_b ())
+      Item* s = stems[i];
+      if (Stem::invisible_b (s))
        continue;
 
-      Real stem_y = calc_stem_y_f (s, y, dy);
+      Real stem_y = calc_stem_y_f (me, s, y, dy);
        
-      stem_y *= get_direction ();
-      Stem_info info = s->calc_stem_info ();
+      stem_y *= dir;
+      Stem_info info = Stem::calc_stem_info (s);
 
-      if (stem_y > info.maxy_f_)
-       shorten = shorten <? info.maxy_f_ - stem_y;
-
-      if (stem_y < info.miny_f_)
-        lengthen = lengthen >? info.miny_f_ - stem_y; 
+      // if (0 > info.maxy_f_ - stem_y)
+      shorten = shorten <? info.maxy_f_ - stem_y;
+      // if (0 < info.miny_f_ - stem_y)
+      lengthen = lengthen >? info.miny_f_ - stem_y; 
     }
 
   if (lengthen && shorten)
     warning (_ ("weird beam vertical offset"));
 
   /* when all stems are too short, normal stems win */
-  if (shorten)
-    return shorten * get_direction ();
-  else
-    return lengthen * get_direction ();
+  return dir * ((shorten) ?  shorten : lengthen);
 }
 
+/*
+  Hmm.  At this time, beam position and slope are determined.  Maybe,
+  stem directions and length should set to relative to the chord's
+  position of the beam.  */
 void
-Beam::set_stem_length (Real y, Real dy)
+Beam::set_stem_length (Score_element*me,Real y, Real dy)
 {
-  Real internote_f = paper_l ()->get_var ("interline") / 2;
-  for (int i=0; i < stem_count (); i++)
+  Real half_space = Staff_symbol_referencer::staff_space (me)/2;
+  Link_array<Item> stems=
+    Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
+
+
+  for (int i=0; i < stems.size (); i++)
     {
-      Stem* s = stem (i);
-      if (s->invisible_b ())
+      Item* s = stems[i];
+      if (Stem::invisible_b (s))
        continue;
 
-      Real stem_y = calc_stem_y_f (s, y, dy);
+      Real stem_y = calc_stem_y_f (me, s, y, dy);
 
       /* caution: stem measures in staff-positions */
-      s->set_stemend ((stem_y - calc_interstaff_dist (s, this)) / internote_f);
+      Stem::set_stemend (s,(stem_y + calc_interstaff_dist (s, dynamic_cast<Spanner*> (me))) / half_space);
     }
 }
 
 /*
   [Ross] (simplification of)
-  Try to set dy complying with:
+  Set dy complying with:
     - zero
-    - beam_f / 2 + staffline_f / 2
-    - beam_f + staffline_f
-  + n * interline
-
-  TODO: get allowed-positions as scm list (aarg: from paper block)
+    - thick / 2 + staffline_f / 2
+    - thick + staffline_f
+  + n * staff_space
 */
 Real
-Beam::quantise_dy_f (Real dy) const
+Beam::quantise_dy_f (Score_element*me,Real dy) 
 {
-  SCM s = get_elt_property ("slope-quantisation");
+  Array<Real> a;
+
+  SCM proc = me->get_elt_property ("height-quants");
+  SCM quants = gh_call2 (proc, me->self_scm (),
+                        gh_double2scm (me->paper_l ()->get_var ("stafflinethickness")
+                                       / 1.0));
+  
   
-  if (s == ly_symbol2scm ("none"))
+  for (SCM s = quants; gh_pair_p (s); s = gh_cdr (s))
+    a.push (gh_scm2double (gh_car (s)));
+  
+  if (a.size () <= 1)
     return dy;
 
-  Staff_symbol_referencer_interface st (this);
-  Real interline_f = st.staff_space ();
+  Real staff_space = Staff_symbol_referencer::staff_space (me);
   
-  Real staffline_f = paper_l ()->get_var ("stafflinethickness");
-  Real beam_f = gh_scm2double (get_elt_property ("beam-thickness"));;
-
-  Array<Real> allowed_fraction (3);
-  allowed_fraction[0] = 0;
-  allowed_fraction[1] = (beam_f / 2 + staffline_f / 2);
-  allowed_fraction[2] = (beam_f + staffline_f);
-
-  allowed_fraction.push (interline_f);
-  Interval iv = quantise_iv (allowed_fraction,  abs (dy));
+  Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
   Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
     ? iv[SMALLER]
     : iv[BIGGER];
-
+  
   return q * sign (dy);
 }
 
@@ -539,94 +573,59 @@ Beam::quantise_dy_f (Real dy) const
   Prevent interference from stafflines and beams.
   See Documentation/tex/fonts.doc
 
-  TODO: get allowed-positions as scm list (aarg: from paper block)
+  We only need to quantise the (left) y-position of the beam,
+  since dy is quantised too.
+  if extend_b then stems must *not* get shorter
  */
 Real
-Beam::quantise_y_f (Real y, Real dy, int quant_dir)
+Beam::quantise_y_f (Score_element*me,Real y, Real dy, int quant_dir)
 {
-   /*
-    We only need to quantise the (left) y-position of the beam,
-    since dy is quantised too.
-    if extend_b then stems must *not* get shorter
-   */
-  SCM s = get_elt_property ("slope-quantisation");
-  if (s == ly_symbol2scm ("none"))
-    return y;
+  int multiplicity = get_multiplicity (me);
 
-  /*
-    ----------------------------------------------------------
-                                                   ########
-                                       ########
-                             ########
-    --------------########------------------------------------
-       ########
-
-       hang       straddle   sit        inter      hang
-   */
+  Real staff_space = Staff_symbol_referencer::staff_space (me);
+  Real thick = me->paper_l ()->get_var ("stafflinethickness");
 
-  Staff_symbol_referencer_interface sinf (this);
-  Real space = sinf.staff_space ();
-  Real staffline_f = paper_l ()->get_var ("stafflinethickness");
-  Real beam_f = gh_scm2double (get_elt_property ("beam-thickness"));;
 
-  Real straddle = 0;
-  Real sit = beam_f / 2 - staffline_f / 2;
-  Real hang = space - beam_f / 2 + staffline_f / 2;
-
-  /*
-    Put all allowed positions into an array.
-    Whether a position is allowed or not depends on 
-    strictness of quantisation, multiplicity and direction.
-
-    For simplicity, we'll assume dir = UP and correct if 
-    dir = DOWN afterwards.
-   */
+  SCM proc = me->get_elt_property ("vertical-position-quant-function");
+  SCM quants = scm_apply (proc,
+                         me->self_scm (),
+                         gh_list (gh_int2scm (multiplicity),
+                                  gh_double2scm (dy/staff_space),
+                                  gh_double2scm (thick/staff_space),
+                                  SCM_EOL, SCM_UNDEFINED));
   
-  int multiplicity = get_multiplicity ();
+  Array<Real> a;
 
+  for (; gh_pair_p (quants); quants = gh_cdr (quants))
+    a.push (gh_scm2double (gh_car (quants)));
 
-  Array<Real> allowed_position;
-  if (s == ly_symbol2scm ("normal"))
-    {
-      if ((multiplicity <= 2) || (abs (dy) >= staffline_f / 2))
-       allowed_position.push (straddle);
-      if ((multiplicity <= 1) || (abs (dy) >= staffline_f / 2))
-       allowed_position.push (sit);
-      allowed_position.push (hang);
-    }
-  else if (s == ly_symbol2scm ("traditional"))
-    {
-      // TODO: check and fix TRADITIONAL
-      if ((multiplicity <= 2) || (abs (dy) >= staffline_f / 2))
-       allowed_position.push (straddle);
-      if ((multiplicity <= 1) && (dy <= staffline_f / 2))
-       allowed_position.push (sit);
-      if (dy >= -staffline_f / 2)
-       allowed_position.push (hang);
-    }
+  if (a.size () <= 1)
+    return y;
 
-  allowed_position.push (space);
-  Real up_y = get_direction () * y;
-  Interval iv = quantise_iv (allowed_position, up_y);
+  Real up_y = Directional_element_interface::get (me) * y;
+  Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
 
   Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y 
     ? iv[SMALLER] : iv[BIGGER];
   if (quant_dir)
     q = iv[(Direction)quant_dir];
 
-  return q * get_direction ();
+  return q * Directional_element_interface::get (me);
 }
 
 void
-Beam::set_beaming (Beaming_info_list *beaming)
+Beam::set_beaming (Score_element*me,Beaming_info_list *beaming)
 {
+  Link_array<Score_element> stems=
+    Pointer_group_interface__extract_elements (me, (Score_element*)0, "stems");
+  
   Direction d = LEFT;
-  for (int i=0; i  < stem_count (); i++)
+  for (int i=0; i  < stems.size(); i++)
     {
       do
        {
-         if (stem (i)->beam_count (d) == 0)
-           stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
+         if (Stem::beam_count (stems[i], d) == 0)
+           Stem::set_beaming ( stems[i], beaming->infos_.elem (i).beams_i_drul_[d],d);
        }
       while (flip (&d) != LEFT);
     }
@@ -637,78 +636,87 @@ Beam::set_beaming (Beaming_info_list *beaming)
 /*
   beams to go with one stem.
 
-  BURP
-  clean  me up.
+  FIXME: clean me up.
   */
 Molecule
-Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
+Beam::stem_beams (Score_element*me,Item *here, Item *next, Item *prev) 
 {
-  if ((next && !(next->hpos_f () > here->hpos_f ())) ||
-      (prev && !(prev->hpos_f () < here->hpos_f ())))
+  // ugh -> use commonx
+  if ((next && !(next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
+      (prev && !(prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
       programming_error ("Beams are not left-to-right");
 
-  Real staffline_f = paper_l ()->get_var ("stafflinethickness");
-  int   multiplicity = get_multiplicity ();
+  Real staffline_f = me->paper_l ()->get_var ("stafflinethickness");
+  int multiplicity = get_multiplicity (me);
 
+  SCM space_proc = me->get_elt_property ("space-function");
+  SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
 
-  Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
-  Real beam_f = gh_scm2double (get_elt_property ("beam-thickness"));;
-
-  Real dy = interbeam_f;
+  Real thick = gh_scm2double (me->get_elt_property ("thickness")) ;
+  Real interbeam_f = gh_scm2double (space) ;
+    
+  Real bdy = interbeam_f;
   Real stemdx = staffline_f;
 
-  Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
-  Real dydx = get_real ("height")/dx;
+    // ugh -> use commonx
+  Real dx = visible_stem_count (me) ?
+    last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS)
+    : 0.0;
+  Real dy = gh_scm2double (me->get_elt_property ("height"));
+  Real dydx = dy && dx ? dy/dx : 0;
 
   Molecule leftbeams;
   Molecule rightbeams;
 
-  // UGH
   Real nw_f;
-  if (!here->first_head ())
+  if (!Stem::first_head (here))
     nw_f = 0;
-  else if (here->type_i ()== 1)
-    nw_f = paper_l ()->get_var ("wholewidth");
-  else if (here->type_i () == 2)
-    nw_f = paper_l ()->get_var ("notewidth") * 0.8;
-  else
-    nw_f = paper_l ()->get_var ("quartwidth");
+  else {
+    int t = Stem::type_i (here); 
+
+    SCM proc = me->get_elt_property ("flag-width-function");
+    SCM result = gh_call1 (proc, gh_int2scm (t));
+    nw_f = gh_scm2double (result);
+  }
+
 
+  Direction dir = Directional_element_interface::get (me);
+  
   /* half beams extending to the left. */
   if (prev)
     {
-      int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
-      int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
+      int lhalfs= lhalfs = Stem::beam_count (here,LEFT) - Stem::beam_count (prev,RIGHT);
+      int lwholebeams= Stem::beam_count (here,LEFT) <? Stem::beam_count (prev,RIGHT) ;
       /*
        Half beam should be one note-width, 
        but let's make sure two half-beams never touch
        */
-      Real w = here->hpos_f () - prev->hpos_f ();
+      Real w = here->relative_coordinate (0, X_AXIS) - prev->relative_coordinate (0, X_AXIS);
       w = w/2 <? nw_f;
       Molecule a;
       if (lhalfs)              // generates warnings if not
-       a =  lookup_l ()->beam (dydx, w, beam_f);
+       a =  Lookup::beam (dydx, w, thick);
       a.translate (Offset (-w, -w * dydx));
       for (int j = 0; j  < lhalfs; j++)
        {
          Molecule b (a);
-         b.translate_axis (-get_direction () * dy * (lwholebeams+j), Y_AXIS);
+         b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
          leftbeams.add_molecule (b);
        }
     }
 
   if (next)
     {
-      int rhalfs  = here->beam_count (RIGHT) - next->beam_count (LEFT);
-      int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
+      int rhalfs  = Stem::beam_count (here,RIGHT) - Stem::beam_count (next,LEFT);
+      int rwholebeams= Stem::beam_count (here,RIGHT) <? Stem::beam_count (next,LEFT) ;
 
-      Real w = next->hpos_f () - here->hpos_f ();
-      Molecule a = lookup_l ()->beam (dydx, w + stemdx, beam_f);
+      Real w = next->relative_coordinate (0, X_AXIS) - here->relative_coordinate (0, X_AXIS);
+      Molecule a = Lookup::beam (dydx, w + stemdx, thick);
       a.translate_axis( - stemdx/2, X_AXIS);
       int j = 0;
       Real gap_f = 0;
 
-      SCM gap = get_elt_property ("beam-gap");
+      SCM gap = me->get_elt_property ("gap");
       if (gh_number_p (gap))
        {
          int gap_i = gh_scm2int ( (gap));
@@ -717,33 +725,30 @@ Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
          for (; j  < nogap; j++)
            {
              Molecule b (a);
-             b.translate_axis (-get_direction () * dy * j, Y_AXIS);
+             b.translate_axis (-dir  * bdy * j, Y_AXIS);
              rightbeams.add_molecule (b);
            }
          // TODO: notehead widths differ for different types
          gap_f = nw_f / 2;
          w -= 2 * gap_f;
-         a = lookup_l ()->beam (dydx, w + stemdx, beam_f);
+         a = Lookup::beam (dydx, w + stemdx, thick);
        }
 
       for (; j  < rwholebeams; j++)
        {
          Molecule b (a);
-         if (!here->invisible_b ())
-           b.translate (Offset (gap_f, -get_direction () * dy * j));
-         else
-           b.translate (Offset (0, -get_direction () * dy * j));
+         b.translate (Offset (Stem::invisible_b (here) ? 0 : gap_f, -dir * bdy * j));
          rightbeams.add_molecule (b);
        }
 
       w = w/2 <? nw_f;
       if (rhalfs)
-       a = lookup_l ()->beam (dydx, w, beam_f);
+       a = Lookup::beam (dydx, w, thick);
 
       for (; j  < rwholebeams + rhalfs; j++)
        {
          Molecule b (a);
-         b.translate_axis (-get_direction () * dy * j, Y_AXIS);
+         b.translate_axis (- dir * bdy * j, Y_AXIS);
          rightbeams.add_molecule (b);
        }
 
@@ -757,48 +762,68 @@ Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
   return leftbeams;
 }
 
-
-Molecule*
-Beam::do_brew_molecule_p () const
+MAKE_SCHEME_CALLBACK(Beam,brew_molecule,1);
+SCM
+Beam::brew_molecule (SCM smob)
 {
-  Molecule *mol_p = new Molecule;
-  if (!stem_count ())
-    return mol_p;
+  Score_element * me =unsmob_element (smob);
+
+  Molecule mol;
+  if (!gh_pair_p (me->get_elt_property ("stems")))
+    return SCM_EOL;
+  Real x0,dx;
+  Link_array<Item>stems = 
+    Pointer_group_interface__extract_elements (me, (Item*) 0, "stems");  
+  if (visible_stem_count (me))
+    {
+  // ugh -> use commonx
+      x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
+      dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
+    }
+  else
+    {
+      x0 = stems[0]->relative_coordinate (0, X_AXIS);
+      dx = stems.top()->relative_coordinate (0, X_AXIS) - x0;
+    }
+  
   
-  Real x0 = first_visible_stem ()->hpos_f ();
-  Real dx = last_visible_stem ()->hpos_f () - x0;
-  Real dydx = get_real ("height")/dx;
-  Real y = get_real ("y-position");
-  for (int j=0; j <stem_count (); j++)
+  Real dy = gh_scm2double (me->get_elt_property ("height"));
+  Real dydx = dy && dx ? dy/dx : 0;
+  Real y = gh_scm2double (me->get_elt_property ("y-position"));
+
+
+  for (int j=0; j <stems.size  (); j++)
     {
-      Stem *i = stem (j);
-      Stem * prev = (j > 0)? stem (j-1) : 0;
-      Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
+      Item *i = stems[j];
+      Item * prev = (j > 0)? stems[j-1] : 0;
+      Item * next = (j < stems.size()-1) ? stems[j+1] :0;
 
-      Molecule sb = stem_beams (i, next, prev);
-      Real x = i->hpos_f ()-x0;
+      Molecule sb = stem_beams (me, i, next, prev);
+      Real x = i->relative_coordinate (0, X_AXIS)-x0;
       sb.translate (Offset (x, x * dydx + y));
-      mol_p->add_molecule (sb);
+      mol.add_molecule (sb);
     }
-  mol_p->translate_axis (x0 
-    - spanned_drul_[LEFT]->relative_coordinate (0, X_AXIS), X_AXIS);
+  mol.translate_axis (x0 
+    - dynamic_cast<Spanner*> (me)->get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
 
-  return mol_p;
+  return mol.smobbed_copy ();
 }
 
 int
-Beam::forced_stem_count () const
+Beam::forced_stem_count (Score_element*me) 
 {
+  Link_array<Item>stems = 
+    Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
   int f = 0;
-  for (int i=0; i < stem_count (); i++)
+  for (int i=0; i < stems.size (); i++)
     {
-      Stem *s = stem (i);
+      Item *s = stems[i];
 
-      if (s->invisible_b ())
+      if (Stem::invisible_b (s))
        continue;
 
-      if (((int)s->chord_start_f ()) 
-        && (s->get_direction () != s->get_default_dir ()))
+      if (((int)Stem::chord_start_f (s)) 
+        && (Stem::get_direction (s ) != Stem::get_default_dir (s )))
         f++;
     }
   return f;
@@ -806,72 +831,136 @@ Beam::forced_stem_count () const
 
 
 
-/*
-  TODO: Fix this class. This is wildly inefficient.
-  And it sux.  Yet another array/list 'interface'.
- */
-Stem *
-Beam::stem (int i) const
-{
-  return Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
-}
 
+/* TODO:
+   use filter and standard list functions.
+ */
 int
-Beam::stem_count () const
+Beam::visible_stem_count (Score_element*me) 
 {
-  Group_interface gi (this, "stems");
-  return gi.count ();
+  Link_array<Item>stems = 
+    Pointer_group_interface__extract_elements (me, (Item*) 0, "stems");
+  int c = 0;
+  for (int i = stems.size (); i--;)
+    {
+      if (!Stem::invisible_b (stems[i]))
+        c++;
+    }
+  return c;
 }
 
-Stem*
-Beam::stem_top () const
+Item*
+Beam::first_visible_stem(Score_element*me) 
 {
-  SCM s = get_elt_property ("stems");
+  Link_array<Item>stems = 
+    Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
   
-  return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
-    
-  //Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[stem_count () - 1];
+  for (int i = 0; i < stems.size (); i++)
+    {
+      if (!Stem::invisible_b (stems[i]))
+        return stems[i];
+    }
+  return 0;
 }
 
-/* burp */
-int
-Beam::visible_stem_count () const
+Item*
+Beam::last_visible_stem(Score_element*me) 
 {
-  int c = 0;
-  for (int i = 0; i < stem_count (); i++)
+  Link_array<Item>stems = 
+    Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
+  for (int i = stems.size (); i--;)
     {
-      if (!stem (i)->invisible_b ())
-        c++;
+      if (!Stem::invisible_b (stems[i]))
+        return stems[i];
     }
-  return c;
+  return 0;
 }
 
-Stem*
-Beam::first_visible_stem () const
+
+/*
+  [TODO]
+  handle rest under beam (do_post: beams are calculated now)
+  what about combination of collisions and rest under beam.
+
+  Should lookup
+    
+    rest -> stem -> beam -> interpolate_y_position ()
+*/
+MAKE_SCHEME_CALLBACK(Beam,rest_collision_callback,2);
+SCM
+Beam::rest_collision_callback (SCM element_smob, SCM axis)
 {
-  for (int i = 0; i < stem_count (); i++)
-    {
-      Stem* s = stem (i);
-      if (!s->invisible_b ())
-        return s;
-    }
+  Score_element *rest = unsmob_element (element_smob);
+  Axis a = (Axis) gh_scm2int (axis);
+  
+  assert (a == Y_AXIS);
 
-  assert (0);
+  Score_element * st = unsmob_element (rest->get_elt_property ("stem"));
+  Score_element * stem = st;
+  if (!stem)
+    return gh_double2scm (0.0);
+  Score_element * beam = unsmob_element (stem->get_elt_property ("beam"));
+  if (!beam || !Beam::has_interface (beam) || !Beam::visible_stem_count (beam))
+    return gh_double2scm (0.0);
 
-  return 0;
+  // make callback for rest from this.
+  Real beam_dy = 0;
+  Real beam_y = 0;
+
+
+  // todo: make sure this calced already.
+  SCM s = beam->get_elt_property ("height");
+  if (gh_number_p (s))
+    beam_dy = gh_scm2double (s);
+  
+  s = beam->get_elt_property ("y-position");
+  if (gh_number_p (s))
+    beam_y = gh_scm2double (s);
+  
+  // ugh -> use commonx
+  Real x0 = first_visible_stem(beam)->relative_coordinate (0, X_AXIS);
+  Real dx = last_visible_stem(beam)->relative_coordinate (0, X_AXIS) - x0;
+  Real dydx = beam_dy && dx ? beam_dy/dx : 0;
+
+  Direction d = Stem::get_direction (stem);
+  Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + beam_y;
+
+  Real staff_space =   Staff_symbol_referencer::staff_space (rest);
+
+  
+  Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space ; // refp??
+
+  Real minimum_dist
+    = gh_scm2double (rest->get_elt_property ("minimum-beam-collision-distance"));
+  Real dist =
+    minimum_dist +  -d  * (beamy - rest_dim) >? 0;
+
+  int stafflines = Staff_symbol_referencer::line_count (rest);
+
+  // move discretely by half spaces.
+  int discrete_dist = int (ceil (dist));
+
+  // move by whole spaces inside the staff.
+  if (discrete_dist < stafflines+1)
+    discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
+
+  return gh_double2scm  (-d *  discrete_dist);
 }
 
-Stem*
-Beam::last_visible_stem () const
+
+bool
+Beam::has_interface (Score_element*me)
 {
-  for (int i = stem_count (); i > 0; i--)
-    {
-      Stem* s = stem (i - 1);
-      if (!s->invisible_b ())
-        return s;
-    }
+  return me->has_interface (ly_symbol2scm ("beam-interface"));
+}
 
-  assert (0);
-  // sigh
-  return 0;
+void
+Beam::set_interface (Score_element*me)
+{
+  /*
+    why the init? No way to tell difference between default and user
+    override.  */
+  me->set_elt_property ("height", gh_int2scm (0)); // ugh.
+  me->set_elt_property ("y-position" ,gh_int2scm (0));
+  me->set_interface (ly_symbol2scm("beam-interface"));
 }