]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/rest-collision.cc
release: 1.1.39
[lilypond.git] / lily / rest-collision.cc
index 54cfe814c1458152123cef4c40f81416c84092e8..0103302408e7d727a8e4ecf64026782ccedb4727 100644 (file)
@@ -3,9 +3,9 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
-
+#include "beam.hh"
 #include "debug.hh"
 #include "rest-collision.hh"
 #include "note-column.hh"
@@ -13,9 +13,7 @@
 #include "note-head.hh"
 #include "collision.hh"
 #include "paper-def.hh"
-
-
-IMPLEMENT_IS_TYPE_B1(Rest_collision,Item);
+#include "rest.hh"
 
 void
 Rest_collision::add_column (Note_column *nc_l)
@@ -27,45 +25,6 @@ Rest_collision::add_column (Note_column *nc_l)
     ncol_l_arr_.push (nc_l);
 }
 
-void
-Rest_collision::do_post_processing()
-{
-  /*
-    handle rest under beam (do_post: beams are calculated now)
-
-    [todo]
-    i-d like to have access to the beam itself, 
-    iso only the (half-initialised?) stem
-
-    what about combination of collisions and rest under beam
-   */
-
-  // no rests to collide
-  if (!rest_l_arr_.size())
-       return;
-  // can this happen?
-  Stem* stem_l = rest_l_arr_[0]->stem_l_;
-  if (!stem_l)
-    return;
-  // no beam
-  if (!(stem_l->beams_left_i_ || stem_l->beams_right_i_))
-    return;
-
-  int dir_i = rest_l_arr_[0]->dir_;
-  int midpos = 4;
-  // ugh
-  int stem_length_i = 7 - 2;
-  // ugh, Stem::stem_start vs Stem::stem_end
-  int pos = (int)(stem_l->stem_end_f() - midpos) - dir_i * stem_length_i;
-  /*
-    nogo: stem_start not set for rests?
-  int pos = (stem_l->stem_begin_f() - midpos) + dir_i * 2;
-
-  WHY IS THIS STILL HERE? --hwn
-  */
-  rest_l_arr_[0]->translate_rests (pos);       
-}
-
 void
 Rest_collision::do_pre_processing()
 {
@@ -73,7 +32,9 @@ Rest_collision::do_pre_processing()
      handle rest-rest and rest-note collisions
 
      [todo]
-     decide not to print rest if too crowded?
+     * decide not to print rest if too crowded?
+
+     * ignore rests under beams.
    */
 
   // no rests to collide
@@ -87,37 +48,70 @@ Rest_collision::do_pre_processing()
   // meisjes met meisjes
   if (!ncol_l_arr_.size()) 
     {
+      /*
+       UGH.  Should get dims from table.  Should have minimum dist.
+       */
       int dy = rest_l_arr_.size() > 2 ? 6 : 4;
        
-      rest_l_arr_[0]->translate_rests (rest_l_arr_[0]->dir_ *dy);      
-      // top is last element...
-      rest_l_arr_.top()->translate_rests (rest_l_arr_.top ()->dir_* dy);       
+      rest_l_arr_[0]->translate_rests (rest_l_arr_[0]->dir () *dy);    
+      rest_l_arr_.top()->translate_rests (rest_l_arr_.top ()->dir ()* dy);
     }
   // meisjes met jongetjes
   else 
     {
-      // int dir_i = - ncol_l_arr_[0]->dir_;
-      int dir_i = rest_l_arr_[0]->dir_;
-      // hope it's 4: if it works->doco
-      int midpos = 0;
-       
-      // minimum move
-      int minpos = 4;
-       
-      // quart rest height
-      // UGH Should get dims from table!
-      int size_i = 6;
-       
-      int sep_i = 3 + size_i / 2;
+      if (rest_l_arr_.size () > 1)
+       {
+         warning (_("Too many colliding rests."));
+       }
+      if (ncol_l_arr_.size () > 1)
+       {
+         warning (_("Too many notes for rest collision."));
+       }
+      Note_column * rcol = rest_l_arr_[0];
+
+      // try to be opposite of noteheads. 
+      Direction dir = - ncol_l_arr_[0]->dir();
+
+      Interval restdim;
+      for (int i=0; i < rcol->rest_l_arr_.size(); i++)
+       restdim.unite (rcol->rest_l_arr_[i]->extent (Y_AXIS));
+
+      if (restdim.empty_b ())
+       return;
+      
+      // staff ref'd?
+      Real staff_space = rcol->rest_l_arr_[0]->staff_line_leading_f ();      
+      Real internote_f = staff_space/2;
+      Real minimum_dist = paper_l ()->get_var ("restcollision_minimum_dist")
+       * internote_f;
+      
+      /*
+       assumption: ref points are the same. 
+       */
+      Interval notedim;
       for (int i = 0; i < ncol_l_arr_.size(); i++) 
        {
-         // how to know whether to sort?
-         ncol_l_arr_[i]->sort();
-         for (int j = 0; j < ncol_l_arr_[i]->head_l_arr_.size(); j++)
-           minpos = minpos >? dir_i * 
-             (ncol_l_arr_[i]->head_l_arr_[j]->position_i_ -midpos) + sep_i;
+         notedim.unite (ncol_l_arr_[i]->extent (Y_AXIS));
        }
-      rest_l_arr_[0]->translate_rests (dir_i * minpos);        
+
+      Interval inter (notedim);
+      inter.intersect (restdim);
+
+      Real dist =
+       minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
+
+
+      int stafflines = rcol->rest_l_arr_[0]->lines_i ();
+
+      
+      // move discretely by half spaces.
+      int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
+
+      // move by whole spaces inside the staff.
+      if (discrete_dist < stafflines+1)
+       discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
+      
+      rcol->translate_rests (dir * discrete_dist);
     }
 }
 
@@ -131,21 +125,18 @@ Rest_collision::do_print() const
 }
 
 void
-Rest_collision::do_substitute_dependency (Score_element*o,Score_element*n)
+Rest_collision::do_substitute_element_pointer (Score_element*o,Score_element*n)
 {
-  Item*o_l = dynamic_cast <Item *> (o);
-  
-
-  if (o_l&&o_l->is_type_b (Note_column::static_name ()))
+  if (Note_column *onl = dynamic_cast<Note_column *> (o))
     {
-      Note_column *n_l = n?(Note_column*)dynamic_cast <Item *> (n):0;
-      rest_l_arr_.substitute ((Note_column*)o_l, n_l);
-      ncol_l_arr_.substitute ((Note_column*)o_l, n_l);
+      Note_column *n_l = n?dynamic_cast<Note_column *> (n):0;
+      rest_l_arr_.substitute (onl, n_l);
+      ncol_l_arr_.substitute (onl, n_l);
     }
 }
 
 Rest_collision::Rest_collision()
 {
-  transparent_b_ = true;
+  set_elt_property (transparent_scm_sym, SCM_BOOL_T);
   set_empty (true);
 }