]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/spring-spacer.cc
release: 1.1.57
[lilypond.git] / lily / spring-spacer.cc
index 65a7c1336d352478733c7d8d3002ffd0b4035201..aee2bba8bce4b42e1b43ad84ddb154950d04297c 100644 (file)
@@ -3,12 +3,13 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1996,  1997--1998, 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1996--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
 
 #include <math.h>
 #include <limits.h>
+#include "killing-cons.tcc"
 #include "spring-spacer.hh"
 #include "p-col.hh"
 #include "debug.hh"
 Vector
 Spring_spacer::default_solution() const
 {
-  return try_initial_solution() ;
+  return try_initial_solution();
 }
 
 Score_column*
 Spring_spacer::scol_l (int i)
 {
-  return (Score_column*)cols_[i].pcol_l_;
+  return dynamic_cast<Score_column*>(cols_[i].pcol_l_);
 }
 
 const Real COLFUDGE=1e-3;
@@ -66,9 +67,10 @@ Spring_spacer::handle_loose_cols()
 {
   Union_find connected (cols_.size());
   Array<int> fixed;
-  for (PCursor<Idealspacing*> i (ideal_p_list_.top()); i.ok (); i++)
+  
+  for (Cons<Idealspacing> *i  = ideal_p_list_; i; i = i->next_)
     {
-      connected.connect (i->cols_drul_[LEFT],i->cols_drul_[RIGHT]);
+      connected.connect (i->car_->cols_drul_[LEFT],i->car_->cols_drul_[RIGHT]);
     }
   for (int i = 0; i < cols_.size(); i++)
     if (cols_[i].fixed_b())
@@ -176,7 +178,8 @@ Spring_spacer::check_constraints (Vector v) const
   return true;
 }
 
-/** try to generate a solution which obeys the min distances and fixed positions
+/** try to generate a solution which obeys the min
+    distances and fixed positions
  */
 Vector
 Spring_spacer::try_initial_solution() const
@@ -238,8 +241,9 @@ Spring_spacer::make_matrices (Matrix &quad, Vector &lin, Real &c) const
   lin.fill (0);
   c = 0;
 
-  for (PCursor<Idealspacing*> i (ideal_p_list_.top()); i.ok (); i++)
+  for (Cons<Idealspacing> *p =ideal_p_list_; p; p = p->next_)
     {
+      Idealspacing *i = p->car_;
       int l = i->cols_drul_[LEFT];
       int r = i->cols_drul_[RIGHT];
 
@@ -293,8 +297,9 @@ Real
 Spring_spacer::calculate_energy_f (Vector solution) const
 {
   Real e = 0.0;
-  for (PCursor<Idealspacing*> i (ideal_p_list_.top()); i.ok(); i++)
+  for (Cons<Idealspacing>*p =ideal_p_list_; p; p = p->next_)
     {
+      Idealspacing * i = p->car_;
       e += i->energy_f(solution(i->cols_drul_[RIGHT]) - solution(i->cols_drul_[LEFT]));
     }
 
@@ -319,6 +324,7 @@ Spring_spacer::lower_bound_solution (Column_x_positions*positions) const
 
 Spring_spacer::Spring_spacer ()
 {
+  ideal_p_list_ =0;
   energy_normalisation_f_ = 1.0;
 }
 
@@ -387,14 +393,34 @@ Spring_spacer::add_column (Paper_column  *col, bool fixed, Real fixpos)
       r_rod.other_idx_ = this_rank;
       cols_[left_idx].rods_[RIGHT].push (r_rod);
     }
-  
+
+  for (int i=0; i < col->spring_arr_drul_[LEFT].size (); i++)
+    {
+      Column_spring &cr = col->spring_arr_drul_[LEFT][i];
+      int idx = cr.other_l_->rank_i () - cols_[0].pcol_l_->rank_i ();
+      if (idx < 0)
+       continue;
+      
+      if (cols_[idx].pcol_l_ != cr.other_l_)
+           continue;
+      
+      Real d  = cr.distance_f_;
+      if (fabs (d) < EPS)
+       {
+         connect (idx, this_rank, cr.distance_f_, cr.strength_f_); // large strength. 
+       }
+      else
+       connect (idx, this_rank, cr.distance_f_,
+                cr.strength_f_ / cr.distance_f_);
+    }
+      
   cols_.push (c);
 }
 
 Line_of_cols
 Spring_spacer::error_pcol_l_arr() const
 {
-  Array<Paper_column*> retval;
+  Link_array<Paper_column> retval;
   for (int i=0; i< cols_.size(); i++)
     if (cols_[i].ugh_b_)
       retval.push (cols_[i].pcol_l_);
@@ -405,16 +431,27 @@ Spring_spacer::error_pcol_l_arr() const
   return retval;
 }
 
+/*
+  Ugh. Should junk this.
+ */
 void
-Spring_spacer::loosen_column (int i)
+Spring_spacer::loosen_column (int idx)
 {
-  Column_info c=cols_.get (i);
-  for (PCursor<Idealspacing*> j (ideal_p_list_.top()); j.ok (); j++)
+  Column_info c=cols_.get (idx);
+
+  Cons<Idealspacing> **pp = &ideal_p_list_;
+
+  while (*pp)
     {
-      if (j->cols_drul_[LEFT] == i|| j->cols_drul_[RIGHT] == i)
-       j.del();
+      Idealspacing *j = (*pp)->car_;
+      if (j->cols_drul_[LEFT] == idx|| j->cols_drul_[RIGHT] == idx)
+       {
+         delete remove_cons (pp);
+       }
       else
-       j++;
+       {
+         pp = &(*pp)->next_;
+       }
     }
   c.ugh_b_ = true;
 
@@ -437,9 +474,10 @@ Spring_spacer::print() const
       DOUT << "col " << i << " ";
       cols_[i].print();
     }
-  for (PCursor<Idealspacing*> i (ideal_p_list_.top()); i.ok (); i++)
+
+  for (Cons<Idealspacing> *p =ideal_p_list_; p; p = p->next_)
     {
-      i->print();
+      p->car_->print();
     }
 #endif
 }
@@ -458,20 +496,14 @@ Spring_spacer::connect (int i, int j, Real d, Real h)
   s->space_f_ = d;
   s->hooke_f_ = h;
 
-  ideal_p_list_.bottom().add (s);
+  ideal_p_list_ = new Killing_cons<Idealspacing> (s, ideal_p_list_);
 }
 
-
-
-
 void
 Spring_spacer::prepare()
 {
-  DOUT << "Preparing..";
-  calc_idealspacing();
   handle_loose_cols();
   print();
-  DOUT << "finished preparing.\n";
 }
 
 Line_spacer*
@@ -482,228 +514,8 @@ Spring_spacer::constructor()
 
 
 
-/**
-  get the shortest_playing running note at a time. */
-void
-Spring_spacer::get_ruling_durations(Array<Moment> &shortest_playing_arr,
-                                   Array<Moment> &context_shortest_arr)
-{
-  for (int i=0; i < cols_.size(); i++)
-    {
-      scol_l (i)->preprocess();
-      scol_l (i)->print ();
-    }
-  int start_context_i=0;
-  Moment context_shortest;
-  context_shortest.set_infinite (1);
-  context_shortest_arr.set_size(cols_.size());
-
-  for (int i=0; i < cols_.size(); i++)
-    {
-      Moment now = scol_l (i)->when();
-      Moment shortest_playing;
-      shortest_playing.set_infinite (1);
-
-      if (scol_l (i)->breakable_b_)
-       {
-         for (int ji=i; ji >= start_context_i; ji--)
-           context_shortest_arr[ji] = context_shortest;
-         start_context_i = i;
-         context_shortest.set_infinite (1);
-       }
-      if (scol_l (i)->durations.size())
-       {
-         context_shortest = context_shortest <? scol_l(i)->durations[0];
-       }
-      
-      // ji was j, but triggered ICE
-      for (int ji=i+1; ji --;)
-       {
-         if (scol_l(ji)->durations.size() &&
-             now - scol_l(ji)->when() >= shortest_playing)
-           break;
-
-         for (int k =  scol_l (ji)->durations.size();
-              k-- && scol_l(ji)->durations[k] + scol_l(ji)->when() > now;
-              )
-           {
-             shortest_playing = shortest_playing <? scol_l(ji)->durations[k];
-           }
-       }
-      shortest_playing_arr.push(shortest_playing);
-    }
-
-#ifndef NPRINT
-  DOUT << "shortest_playing/:[ ";
-  for (int i=0; i < shortest_playing_arr.size(); i++)
-    {
-      DOUT << shortest_playing_arr[i] << " ";
-      DOUT << context_shortest_arr[i] << ", ";
-    }
-  DOUT << "]\n";
-#endif
-}
-
-/*
-  TODO: take out the refs to width
- */
-/**
-  generate springs between columns.
-
-  TODO: This needs rethinking.
-
-  *  Spacing should take optical
-  effects into account
 
-  *  Should be decentralised
-  
-  The algorithm is taken from :
-
-  John S. Gourlay. ``Spacing a Line of Music,'' Technical Report
-  OSU-CISRC-10/87-TR35, Department of Computer and Information
-  Science, The Ohio State University, 1987.
-
-  */
-void
-Spring_spacer::calc_idealspacing()
+Spring_spacer::~Spring_spacer()
 {
-  Array<Moment> shortest_playing_arr;
-  Array<Moment> context_shortest_arr;
-  get_ruling_durations(shortest_playing_arr, context_shortest_arr);
-
-  Real interline_f = paper_l ()->interline_f ();
-
-
-  Array<Real> ideal_arr_;
-  Array<Real> hooke_arr_;
-  for (int i=0; i < cols_.size() - 1; i++){
-    ideal_arr_.push (-1.0);
-    hooke_arr_.push (1.0);
-  }
-
-  /* 
-     First do all non-musical columns
-  */
-  for (int i=0; i < cols_.size(); i++)
-    {
-      if (!scol_l (i)->musical_b() && i+1 < cols_.size())
-       {
-         Real symbol_distance =cols_[i].width_[RIGHT] + 2 PT;
-         Real durational_distance = 0;
-
-         
-         Moment delta_t =  scol_l (i+1)->when() - scol_l (i)->when () ;
-
-
-         /*
-           ugh should use shortest_playing distance
-         */
-         if (delta_t)
-           {
-             Real k=  paper_l()->arithmetic_constant (context_shortest_arr[i]);
-             durational_distance =  paper_l()->duration_to_dist (delta_t,k);
-           }
-         symbol_distance += -cols_[i+1].width_[LEFT];
-
-         ideal_arr_[i] = symbol_distance >? durational_distance;
-         hooke_arr_[i] = 1; //2.0;
-       }
-    }
-
-  /* 
-     Then musicals
-  */
-  for (int i=0; i < cols_.size(); i++)
-    {
-      if (scol_l (i)->musical_b())
-       {
-         Moment shortest_playing_len = shortest_playing_arr[i];
-         Moment context_shortest = context_shortest_arr[i];
-         if (! shortest_playing_len)
-           {
-             warning (_f ("can't find a ruling note at %s", 
-               scol_l (i)->when().str ()));
-             shortest_playing_len = 1;
-           }
-         if (! context_shortest)
-           {
-             warning (_f ("no minimum in measure at %s", 
-                     scol_l (i)->when().str ()));
-             context_shortest = 1;
-           }
-         Moment delta_t = scol_l (i+1)->when() - scol_l (i)->when ();
-         Real k=  paper_l()->arithmetic_constant(context_shortest);
-         Real dist = paper_l()->duration_to_dist (shortest_playing_len, k);
-         dist *= (double)(delta_t / shortest_playing_len);
-
-         /*
-           According to [Ross] and [Wanske], and from what i've seen:
-            
-           * whitespace at the begin of the bar should be fixed at 
-           (about) one interline.
-           [Ross]:
-           when spacing gets real tight, a smaller fixed value may be 
-           used, so that there are two discrete amounts of whitespace 
-           possible at the begin of a bar; but this is not implemented 
-           right now.
-            
-           * whitespace at the end of the bar is the normal amount of 
-           "hinterfleish" that would have been used, had there been
-           yet another note in the bar.  
-           [Ross]:
-           some editors argue that the bar line should not take any 
-           space, not to hinder the flow of music spaced around a bar 
-           line.  
-           [Ross] and [Wanske] do not suggest this, however.  Further, 
-           it introduces some spacing problems and think that it is ugly 
-           too.
-           [jcn]
-         */
-
-         /* 
-            first musical column of bar
-         */
-         if (i && scol_l (i - 1)->breakable_b_)
-           {
-             // fixed: probably should set minimum (rod/spring)?
-             cols_[i-1].width_[RIGHT] += interline_f;
-             // should adjust dist too?
-             ideal_arr_[i-1] = ideal_arr_[i-1] >? (2 * interline_f);
-           }
-
-         /* 
-            last musical column of bar
-         */
-         if (i + 1 < cols_.size () && scol_l(i+1)->breakable_b_)
-           {
-             // hmm, how bout?
-             dist = dist >? interline_f;
-
-             /*
-               uhuh, this code looks fine, already?
-               someone was junking this last "hinterfleisch" whitespace?!
-
-               but this seems to be fixed now :-)
-             */
-             // set minimum rod 
-             cols_[i].width_[RIGHT] += interline_f;
-           }
-
-         // ugh, do we need this?
-         if (i < cols_.size () - 1 && !scol_l (i + 1)->musical_b ())
-           {
-             Real minimum = -cols_[i + 1].width_[LEFT] + cols_[i].width_[RIGHT]
-               + interline_f / 2;
-             dist = dist >? minimum;
-           }
-         ideal_arr_[i] = dist;
-       }
-    }
-
-  for (int i=0; i < ideal_arr_.size(); i++)
-    {
-      assert (ideal_arr_[i] >=0 && hooke_arr_[i] >=0);
-      connect (i, i+1, ideal_arr_[i], hooke_arr_[i]);
-    }
+  delete ideal_p_list_;
 }