]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/collision.cc
release: 1.5.9
[lilypond.git] / lily / collision.cc
index 728ffbb9330c54c303af115493c11dcf25752d01..39e906b6ab72eccab8fb334bfba016d58b4c5548 100644 (file)
@@ -3,8 +3,9 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
+
 #include "debug.hh"
 #include "collision.hh"
 #include "note-column.hh"
 #include "paper-def.hh"
 #include "axis-group-interface.hh"
 #include "item.hh"
+#include "stem.hh"
 
-void
-Collision::add_column (Score_element*me,Score_element* ncol_l)
-{
-  ncol_l->add_offset_callback (force_shift_callback, X_AXIS);
-  Axis_group_interface::add_element (me, ncol_l);
-  me->add_dependency (ncol_l);
-}
+MAKE_SCHEME_CALLBACK (Collision,force_shift_callback,2);
 
-Real
-Collision::force_shift_callback (Score_element * c, Axis a)
+SCM
+Collision::force_shift_callback (SCM element_smob, SCM axis)
 {
+  Grob *me = unsmob_grob (element_smob);
+  Axis a = (Axis) gh_scm2int (axis);
   assert (a == X_AXIS);
   
-  Score_element * me = c->parent_l (a);
+   me = me->parent_l (a);
   /*
     ugh. the way DONE is done is not clean
    */
-  if (!unsmob_element (me->get_elt_property ("done")))
+  if (!unsmob_grob (me->get_grob_property ("done")))
     {
-      me->set_elt_property ("done", me->self_scm ());
+      me->set_grob_property ("done", me->self_scm ());
       do_shifts (me);
     }
   
-  return 0.0;
+  return gh_double2scm (0.0);
 }
 
+
+void
+check_meshing_chords (Grob*me,
+                     Drul_array< Array < Real > > *offsets,
+                     Drul_array< Array < Slice > > const &extents,
+                     Drul_array<Link_array<Grob> > const &clash_groups)
+       
+{
+  if (!extents[UP].size () || ! extents[DOWN].size ())
+    return ;
+  
+  
+  Grob *cu =clash_groups[UP][0];
+  Grob *cd =clash_groups[DOWN][0];
+
+  Grob * nu_l= Note_column::first_head (cu);
+  Grob * nd_l = Note_column::first_head (cd);
+      
+     
+  
+  bool half_collide = false;
+  bool full_collide = false;  
+
+  /*
+    TODO:
+
+    filter out the 'o's in this configuration, since they're no part
+    in the collision.
+
+     |
+    x|o
+    x|o
+    x
+
+    
+   */
+  Array<int> ups = Stem::note_head_positions (Note_column::stem_l (cu));
+  Array<int> dps = Stem::note_head_positions (Note_column::stem_l (cd));
+
+  /*
+    they're too far apart to collide. 
+    
+   */
+
+  if (ups[0] > dps.top () + 1)
+    return ; 
+
+  bool touch = (ups[0] - dps.top () >= 0);
+  
+  bool merge_possible = (ups[0] >= dps[0]) && (ups.top () <= dps.top ());
+
+  merge_possible = merge_possible &&
+    Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
+    
+  if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
+    merge_possible = merge_possible && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
+  
+  int i = 0, j=0;
+  while (i < ups.size () && j < dps.size ())
+  {
+    if (abs (ups[i] - dps[j]) == 1)
+      {
+       merge_possible = false;
+       half_collide = true;
+      }
+    else if (ups[i]==dps[j])
+      full_collide = true;
+    else if (ups[i] >dps[0] && ups[i] < dps.top ())
+      merge_possible = false;
+    else if (dps[j] >ups[0] && dps[j] < ups.top ())
+      merge_possible = false;
+    
+    if (ups[i] < dps[j])
+      i++;
+    else if (ups[i] > dps[j])
+      j++;
+    else
+      {
+       i++;
+       j++;
+      }
+  }
+
+  Drul_array<Real> center_note_shifts;
+  center_note_shifts[LEFT] = 0.0;
+  center_note_shifts[RIGHT] = 0.0;
+
+  
+  Real shift_amount = 1;
+
+  if ( touch)
+    shift_amount *= -1;
+  else
+  /*
+    for full collisions, the right hand head may obscure dots, so
+    make sure the dotted heads go to the right.
+   */
+    if ((Rhythmic_head::dot_count (nu_l) < Rhythmic_head::dot_count (nd_l)
+          && full_collide))
+      shift_amount *= -1;
+
+  if (merge_possible)
+    shift_amount *= 0.0;
+  else if (half_collide || full_collide) 
+    shift_amount *= 0.5;
+  else
+    shift_amount *= 0.25;
+
+  Direction d = UP;
+  do
+    {
+      for (int i=0; i < clash_groups[d].size (); i++)
+       (*offsets)[d][i] += d * shift_amount;
+    }
+  while ((flip (&d))!= UP);
+}
+
+
 /*
   TODO: make callback of this.
+
+  TODO:
+
+  note-width is hardcoded, making it difficult to handle all note
+  heads sanely. We should really look at the widths of the colliding
+  columns, and have a separate setting for "align stems".
+
+  
  */
 void
-Collision::do_shifts(Score_element* me)
+Collision::do_shifts (Grob* me)
 {
   SCM autos (automatic_shift (me));
   SCM hand (forced_shift (me));
   
-  Link_array<Score_element> done;
-  
+  Link_array<Grob> done;
+
+
   Real wid
-    = gh_scm2double (me->get_elt_property ("note-width"))
-      * me->paper_l ()->get_var ("staffspace");
+    = gh_scm2double (me->get_grob_property ("note-width"));
   
   for (; gh_pair_p (hand); hand =gh_cdr (hand))
     {
-      Score_element * s = unsmob_element (gh_caar (hand));
+      Grob * s = unsmob_grob (gh_caar (hand));
       Real amount = gh_scm2double (gh_cdar (hand));
       
       s->translate_axis (amount *wid, X_AXIS);
@@ -64,7 +188,7 @@ Collision::do_shifts(Score_element* me)
     }
   for (; gh_pair_p (autos); autos =gh_cdr (autos))
     {
-      Score_element * s = unsmob_element (gh_caar (autos));
+      Grob * s = unsmob_grob (gh_caar (autos));
       Real amount = gh_scm2double (gh_cdar (autos));
       
       if (!done.find_l (s))
@@ -78,18 +202,18 @@ Collision::do_shifts(Score_element* me)
   This should be put into Scheme.  
   */
 SCM
-Collision::automatic_shift (Score_element *me)
+Collision::automatic_shift (Grob *me)
 {
-  Drul_array<Link_array<Score_element> > clash_groups;
+  Drul_array<Link_array<Grob> > clash_groups;
   Drul_array<Array<int> > shifts;
   SCM  tups = SCM_EOL;
 
-  SCM s = me->get_elt_property ("elements");
+  SCM s = me->get_grob_property ("elements");
   for (; gh_pair_p (s); s = gh_cdr (s))
     {
       SCM car = gh_car (s);
 
-      Score_element * se = unsmob_element (car);
+      Grob * se = unsmob_grob (car);
       if (Note_column::has_interface (se))
        clash_groups[Note_column::dir (se)].push (se);
     }
@@ -99,14 +223,14 @@ Collision::automatic_shift (Score_element *me)
   do
     {
       Array<int> & shift (shifts[d]);
-      Link_array<Score_element> & clashes (clash_groups[d]);
+      Link_array<Grob> & clashes (clash_groups[d]);
 
       clashes.sort (Note_column::shift_compare);
 
       for (int i=0; i < clashes.size (); i++)
        {
          SCM sh
-           = clashes[i]->get_elt_property ("horizontal-shift");
+           = clashes[i]->get_grob_property ("horizontal-shift");
 
          if (gh_number_p (sh))
            shift.push (gh_scm2int (sh));
@@ -132,7 +256,7 @@ Collision::automatic_shift (Score_element *me)
     {
       for (int i=0; i < clash_groups[d].size (); i++)
        {
-         Slice s(Note_column::head_positions_interval (clash_groups[d][i]));
+         Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
          s[LEFT] --;
          s[RIGHT]++;
          extents[d].push (s);
@@ -140,6 +264,15 @@ Collision::automatic_shift (Score_element *me)
        }
     }
   while ((flip (&d))!= UP);
+
+  /*
+    do horizontal shifts of each direction 
+
+       | 
+      x||
+       x||
+        x|
+   */
   
   do
     {
@@ -148,27 +281,35 @@ Collision::automatic_shift (Score_element *me)
          Slice prev =extents[d][i-1];
          prev.intersect (extents[d][i]);
          if (prev.length ()> 0 ||
            (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
+ (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
            for (int j = i; j <  clash_groups[d].size (); j++)
              offsets[d][j] += d * 0.5;
        }
     }  
   while ((flip (&d))!= UP);
 
+
+  /*
+    Check if chords are meshing
+   */
+
+  check_meshing_chords (me, &offsets, extents, clash_groups);
+  
+#if 0  
   /*
     if the up and down version are close, and can not be merged, move
     all of them again. */
   if (extents[UP].size () && extents[DOWN].size ())
     {
-      Score_element *cu_l =clash_groups[UP][0];
-      Score_element *cd_l =clash_groups[DOWN][0];
+      Grob *cu_l =clash_groups[UP][0];
+      Grob *cd_l =clash_groups[DOWN][0];
 
 
       /*
        TODO.
        */
-      Score_element * nu_l= Note_column::first_head(cu_l);
-      Score_element * nd_l = Note_column::first_head(cd_l);
+      Grob * nu_l= Note_column::first_head (cu_l);
+      Grob * nd_l = Note_column::first_head (cd_l);
       
       int downpos = Note_column::head_positions_interval (cd_l)[BIGGER];
       int uppos = Note_column::head_positions_interval (cu_l)[SMALLER];      
@@ -178,23 +319,27 @@ Collision::automatic_shift (Score_element *me)
        && Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
 
 
-      if (!to_boolean (me->get_elt_property ("merge-differently-dotted")))
+      if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
        merge = merge && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
 
       /*
        notes are close, but can not be merged.  Shift
        */
-      if (abs(uppos - downpos) < 2 && !merge)
+      if (abs (uppos - downpos) < 2 && !merge)
          do
          {
            for (int i=0; i < clash_groups[d].size (); i++)
              {
-               offsets[d][i] -= d * 0.5;
+               if(Rhythmic_head::dot_count (nu_l) > Rhythmic_head::dot_count (nd_l))
+                 offsets[d][i] += d * 0.5;
+               else 
+                 offsets[d][i] -= d * 0.5;
              }
          }
          while ((flip (&d))!= UP);
     }
-
+#endif
+  
   do
     {
       for (int i=0; i < clash_groups[d].size (); i++)
@@ -207,16 +352,16 @@ Collision::automatic_shift (Score_element *me)
 
 
 SCM
-Collision::forced_shift (Score_element *me)
+Collision::forced_shift (Grob *me)
 {
   SCM tups = SCM_EOL;
   
-  SCM s = me->get_elt_property ("elements");
+  SCM s = me->get_grob_property ("elements");
   for (; gh_pair_p (s); s = gh_cdr (s))
     {
-      Score_element * se = unsmob_element (gh_car (s));
+      Grob * se = unsmob_grob (gh_car (s));
 
-      SCM force =  se->remove_elt_property ("force-hshift");
+      SCM force =  se->remove_grob_property ("force-hshift");
       if (gh_number_p (force))
        {
          tups = gh_cons (gh_cons (se->self_scm (), force),
@@ -226,5 +371,10 @@ Collision::forced_shift (Score_element *me)
   return tups;
 }
 
-
-
+void
+Collision::add_column (Grob*me,Grob* ncol_l)
+{
+  ncol_l->add_offset_callback (Collision::force_shift_callback_proc, X_AXIS);
+  Axis_group_interface::add_element (me, ncol_l);
+  me->add_dependency (ncol_l);
+}