]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/collision.cc
release: 1.3.50
[lilypond.git] / lily / collision.cc
index 881c561e9592601a9c796b48cc557300d8758a08..32ee0d69f685e985e038cfd6fde8e51ba3f30d39 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 #include "debug.hh"
 #include "collision.hh"
 #include "note-column.hh"
 #include "note-head.hh"
 #include "paper-def.hh"
+#include "axis-group-interface.hh"
+
 
 Collision::Collision()
 {
+  Axis_group_interface (this).set_interface ();
+  Axis_group_interface (this).set_axes (X_AXIS, Y_AXIS);
 }
 
 void
-Collision::add (Note_column* ncol_l)
+Collision::add_column (Note_column* ncol_l)
 {
-  clash_l_arr_.push (ncol_l);
-  add_element (ncol_l);
+  Axis_group_interface (this).add_element (ncol_l);
   add_dependency (ncol_l);
 }
-/**
-  should derive of Array.
- */
-static 
-int idx (int dir, bool h_shift_b)
-{
-  assert (abs (dir) == 1);
-  int j = dir > 0 ? 0 : 3;
-  if (h_shift_b) 
-       j += dir;
-  return j;
-}
-
-/** This complicated routine moves note columns around horizontally
-  (and rests vertically) to ensure that notes don't clash. 
 
-  This should be done better, probably.  
-
-  This routine is dedicated to Stine Randmael :-)
-
-  */
 void
-Collision::do_pre_processing()
+Collision::before_line_breaking ()
 {
-  if (clash_l_arr_.size() <= 1)
-       return;
+  SCM autos (automatic_shift ());
+  SCM hand (forced_shift ());
+  Link_array<Score_element> done;
   
-  /*
-    [stem up, stem up shifted, stem down shifted, stem down]
-   */
-  Array<Note_column*> clash_group_arr_a[4];
-  
-  for (int i=0; i < clash_l_arr_.size(); i++) 
+  Real wid = paper_l ()->get_var ("collision_note_width");
+  for (; gh_pair_p (hand); hand =gh_cdr (hand))
     {
-       Note_column* c_l = clash_l_arr_[i];
-       if (! c_l->dir_) 
-         {
-           warning ("No stem direction set. Ignoring column in clash. ");
-           continue;
-         }
-       int d = (c_l->dir_);
-       
-       clash_group_arr_a[idx (d, c_l->h_shift_b_)].push (c_l);
+      Score_element * s = unsmob_element (gh_caar (hand));
+      Real amount = gh_scm2double (gh_cdar (hand));
+      
+      s->translate_axis (amount *wid, X_AXIS);
+      done.push (s);
     }
-  
-  
-  for (int j=0; j < 4; j++) 
+  for (; gh_pair_p (autos); autos =gh_cdr (autos))
     {
-       if (clash_group_arr_a[j].size() > 1) 
-         {
-           warning ("Too many clashing notecolumns. Ignoring them.");
-           return;
-         }
+      Score_element * s = unsmob_element (gh_caar (autos));
+      Real amount = gh_scm2double (gh_cdar (autos));
+      
+      if (!done.find_l (s))
+       s->translate_axis (amount * wid, X_AXIS);
     }
-  int d = 1;
-  do 
+}
+
+/** This complicated routine moves note columns around horizontally to
+  ensure that notes don't clash.
+
+  This should be done better, probably.
+
+  */
+SCM
+Collision::automatic_shift ()
+{
+  Drul_array<Link_array<Note_column> > clash_groups;
+  Drul_array<Array<int> > shifts;
+  SCM  tups = SCM_EOL;
+
+  SCM s = get_elt_property ("elements");
+  for (; gh_pair_p (s); s = gh_cdr (s))
     {
-       if (!clash_group_arr_a[idx (d, false)].size())
-         {
-           clash_group_arr_a[idx (d,  false)] = clash_group_arr_a[idx (d, true)];
-           clash_group_arr_a[idx (d, true)].clear();
-         }
-  } while ((d *= -1) != 1);
-                                 
+      SCM car = gh_car (s);
+
+      Score_element * se = unsmob_element (car);
+      if (Note_column * col = dynamic_cast<Note_column*> (se))
+       clash_groups[col->dir ()].push (col);
+    }
 
-  Interval_t<int> y_extent[4];
-  Note_column * col_l_a[4];
-  Real x_off [4];
-  int y_off[4];
   
-  for (int j =0 ; j < 4; j++) 
+  Direction d = UP;
+  do
     {
-       if (clash_group_arr_a[j].size()) 
-           col_l_a[j] = clash_group_arr_a[j][0];
-       else
-           col_l_a[j] = 0;
-       
-       if (col_l_a[j]) 
-         {
-           y_extent[j] = col_l_a[j]->head_positions_interval();
-         }
+      Array<int> & shift (shifts[d]);
+      Link_array<Note_column> & clashes (clash_groups[d]);
 
+      clashes.sort (Note_column::shift_compare);
 
-       x_off [j] = 0.0;
-       y_off[j] = 0;
+      for (int i=0; i < clashes.size (); i++)
+       {
+         SCM sh
+           = clashes[i]->remove_elt_property ("horizontal-shift");
+
+         if (gh_number_p (sh))
+           shift.push (gh_scm2int (sh));
+         else
+           shift.push (0);
+       }
+      
+      for (int i=1; i < shift.size (); i++)
+       {
+         if (shift[i-1] == shift[i])
+           {
+             warning (_ ("Too many clashing notecolumns.  Ignoring them."));
+             return tups;
+           }
+       }
     }
-  
-  do 
-    {
-       x_off[idx (d, true)] = d*0.5;
-  } while ((d *= -1) != 1);
+  while ((flip (&d))!= UP);
 
-  
-  // y_extent: smallest y-pos noteball interval containing all balls
-  // 4 (0..3) groups: stem up/down; shift on/off; 
-  Interval_t<int> middle (y_extent[idx (-1,0)].max(),
-                           y_extent[idx (1,0)].min());
-  Interval_t<int> open_middle (y_extent[idx (-1,0)].max()+1, y_extent[idx (1,0)].min ()-1);
+  Drul_array< Array < Slice > > extents;
+  Drul_array< Array < Real > > offsets;
+  d = UP;
   do
     {
-       if (!open_middle.contains_b (y_extent[idx (d,true)]))
-           x_off[idx (d, true)] = d *1.0 ;
-  } while ((d *= -1) != 1);
-   
-  if (!middle.empty_b() 
-       && middle.length() < 2 && col_l_a[idx (1,0)] && col_l_a[idx (-1,0)]) {  
-// reproduction of bugfix at 3am ?
-       Note_head * nu_l= col_l_a[idx (1,0)]->head_l_arr_[0];
-       Note_head * nd_l = col_l_a[idx (-1,0)]->head_l_arr_.top();
-       if (! (nu_l->balltype_i_ == nd_l->balltype_i_ 
-              && nu_l->dots_i_ == nd_l->dots_i_  && middle.length() == 0)) 
-                {
-           x_off[idx (1,0)] -= 0.5;
-           x_off[idx (1,1)] -= 0.5;
-           x_off[idx (-1,1)] += 0.5;
-           x_off[idx (-1,0)] += 0.5;
-         }
-       
+      for (int i=0; i < clash_groups[d].size (); i++)
+       {
+         Slice s(clash_groups[d][i]->head_positions_interval ());
+         s[LEFT] --;
+         s[RIGHT]++;
+         extents[d].push (s);
+         offsets[d].push (d * 0.5 * i);
+       }
     }
-  Real inter_f = paper()->internote_f ();
-  Real wid_f = paper()->note_width ();
-  for (int j=0; j < 4; j++) 
+  while ((flip (&d))!= UP);
+  
+  do
     {
-       if (col_l_a[j]) 
+      for (int i=1; i < clash_groups[d].size (); i++)
+       {
+         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))
+           for (int j = i; j <  clash_groups[d].size (); j++)
+             offsets[d][j] += d * 0.5;
+       }
+    }  
+  while ((flip (&d))!= UP);
+
+  /*
+    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 ())
+    {
+      Note_column *cu_l =clash_groups[UP][0];
+      Note_column *cd_l =clash_groups[DOWN][0];
+
+
+      /*
+       TODO.
+       */
+      Note_head * nu_l= cu_l->first_head();
+      Note_head * nd_l = cd_l->first_head();
+      
+      int downpos = cd_l->head_positions_interval ()[BIGGER];
+      int uppos = cu_l->head_positions_interval ()[SMALLER];      
+      
+      bool merge  =
+       downpos == uppos
+       && nu_l->balltype_i () == nd_l->balltype_i ();
+
+
+      if (!to_boolean (get_elt_property ("merge-differently-dotted")))
+       merge = merge && nu_l->dot_count () == nd_l->dot_count ();
+
+      /*
+       notes are close, but can not be merged.  Shift
+       */
+      if (abs(uppos - downpos) < 2 && !merge)
+         do
          {
-           /* collision.cc:138: request for method `translate' is ambiguous 
-              
-              (shaddup)
-            */
-           Offset o (x_off[j] * wid_f, y_off[j] * inter_f);
-           ((Score_elem*)col_l_a[j])->translate (o);
+           for (int i=0; i < clash_groups[d].size (); i++)
+             {
+               offsets[d][i] -= d * 0.5;
+             }
          }
+         while ((flip (&d))!= UP);
     }
-}
 
+  do
+    {
+      for (int i=0; i < clash_groups[d].size (); i++)
+       tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm_, gh_double2scm (offsets[d][i])),
+                                tups);
+    }
+  while (flip (&d) != UP);
+  return tups;
+}
 
-IMPLEMENT_IS_TYPE_B1(Collision, Item);
 
-void
-Collision::do_substitute_dependency (Score_elem*o_l,Score_elem*n_l)
+SCM
+Collision::forced_shift ()
 {
-  clash_l_arr_.substitute ((Note_column*)o_l->item(), 
-                           (Note_column*)(n_l?n_l->item():0));
+  SCM tups = SCM_EOL;
+  
+  SCM s = get_elt_property ("elements");
+  for (; gh_pair_p (s); s = gh_cdr (s))
+    {
+      Score_element * se = unsmob_element ( gh_car (s));
+
+      SCM force =  se->remove_elt_property ("force-hshift");
+      if (gh_number_p (force))
+       {
+         tups = gh_cons (gh_cons (se->self_scm_, force),
+                         tups);
+       }
+    }
+  return tups;
 }
+
+
+