]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/simple-spacer.cc
release: 1.5.29
[lilypond.git] / lily / simple-spacer.cc
index a053d9f6f873b47d0fb3495e8c0957efed11b9c3..f6052dd08c78cff71dd7b4908a84cd5a595387a3 100644 (file)
@@ -3,15 +3,15 @@
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1999--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 
   TODO:
   - add support for different stretch/shrink constants?
-  - Use force as a minimizing function, and use it to discourage mixes of
-  wide and tight lines.
   
 */
 
+#include <math.h>
+#include <libc-extension.hh>   // isinf
 
 #include "simple-spacer.hh"
 #include "paper-column.hh"
 #include "rod.hh"
 #include "warn.hh"
 #include "column-x-positions.hh"
+#include "spaceable-grob.hh"
 #include "dimensions.hh"
 
 Simple_spacer::Simple_spacer ()
 {
+  active_count_ = 0;
   force_f_ = 0.;
   indent_f_ =0.0;
   default_space_f_ = 20 PT;
-  compression_energy_factor_f_ = 3.0;
 }
 
 void
 Simple_spacer::add_rod (int l, int r, Real dist)
 {
+  if (isinf (dist) || isnan (dist))
+    {
+      programming_error ("Weird minimum distance. Ignoring");
+      return;
+    }
+  
+  
   Real c = range_stiffness (l,r);
   Real d = range_ideal_len (l,r);
   Real block_stretch = dist - d;
   
   Real block_force = c * block_stretch;
   force_f_ = force_f_ >? block_force;
-  
+
   for (int i=l; i < r; i++)
     springs_[i].block_force_f_ = block_force >?
       springs_[i].block_force_f_ ;
@@ -93,8 +101,12 @@ Simple_spacer::set_active_states ()
   // safe, since
   // force is only copied.
   for (int i=0 ; i <springs_.size (); i++)
-    if (springs_[i].block_force_f_ >= force_f_) 
-      springs_[i].active_b_ = false;
+    if (springs_[i].active_b_
+       && springs_[i].block_force_f_ >= force_f_)
+      {
+       springs_[i].active_b_ = false;
+       active_count_ --; 
+      }
 }   
 
 Real
@@ -118,10 +130,7 @@ Spring_description::length (Real f) const
 bool
 Simple_spacer::active_b () const
 {
-   for (int i=0; i < springs_.size (); i++)
-    if (springs_[i].active_b_)
-      return true;
-   return false;
+  return active_count_; 
 }
 
 void
@@ -134,7 +143,7 @@ Simple_spacer::my_solve_linelen ()
 
       if (conf < line_len_f_)
        {
-         force_f_ +=  (line_len_f_  - conf) * active_springs_stiffness ();
+         force_f_ += (line_len_f_  - conf) * active_springs_stiffness ();
          break;
        }
       else
@@ -158,49 +167,77 @@ Simple_spacer::my_solve_natural_len ()
 }
 
 void
-Simple_spacer::add_columns (Link_array<Paper_column> cols)
+Simple_spacer::add_columns (Link_array<Grob> cols)
 {
+  for (int i =  cols.size (); i--;)
+    if (gh_pair_p (cols[i]->get_grob_property ("between-cols")))
+      {
+       loose_cols_.push (cols[i]);
+       cols.del (i);
+      }
+  
+  spaced_cols_ = cols;
   for (int i=0; i < cols.size () - 1; i++)
     {
-      Paper_column * c = cols [i];
-      Column_spring *to_next = 0;
-      for (int j =0; !to_next && j < c->spring_arr_drul_[RIGHT].size( ); j++)
+      SCM spring_params = SCM_EOL;
+      for (SCM s = cols[i]->get_grob_property ("ideal-distances");
+          !gh_pair_p (spring_params) && gh_pair_p (s);
+          s = ly_cdr (s))
        {
-         Column_spring &sp = c->spring_arr_drul_[RIGHT] [j];
-         if (sp.other_l_ != cols[i+1])
+         Grob *other = unsmob_grob (ly_caar (s));
+         if (other != cols[i+1])
            continue;
 
-         to_next = &sp;
+         spring_params = ly_cdar (s);
        }
 
       Spring_description desc;
-      if (to_next)
+      if (gh_pair_p (spring_params))
        {
-         desc.hooke_f_ = to_next->strength_f_;
-         desc.ideal_f_ = to_next->distance_f_;
+         desc.ideal_f_ = gh_scm2double (ly_car (spring_params));
+         desc.hooke_f_ = gh_scm2double (ly_cdr (spring_params));
        }
       else
        {
+         programming_error (_f("No spring between column %d and next one",
+                               Paper_column::rank_i (cols[i])
+                               ));
          desc.hooke_f_ = 1.0;
          desc.ideal_f_ = default_space_f_;
        }
+
+      if (!desc.sane_b ())
+       {
+         programming_error ("Insane spring found. Setting to unit spring.");
+
+         cout << "columns " << Paper_column::rank_i (cols[i])
+              << " " << Paper_column::rank_i (cols[i+1]) << endl;
+         desc.hooke_f_ = 1.0;
+         desc.ideal_f_ = 1.0;
+       }
+      
       desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
-      springs_.push  (desc);
+      springs_.push (desc);
+      active_count_ ++;
     }
   
   for (int i=0; i < cols.size () - 1; i++)
     {
-      Array<Column_rod> * rods = &cols [i]->minimal_dists_arr_drul_[RIGHT];
-      for (int j =0; j < rods->size( ); j++)
+      for (SCM s = Spaceable_grob::get_minimum_distances (cols[i]);
+          gh_pair_p (s); s = ly_cdr (s))
        {
-         int oi = cols.find_i (rods->elem (j).other_l_ );
+         Grob * other = unsmob_grob (ly_caar (s));
+         int oi = cols.find_i (other);
          if (oi >= 0)
            {
-             add_rod (i, oi, rods->elem (j).distance_f_);
+             add_rod (i, oi, gh_scm2double (ly_cdar (s)));
            }
        }
     }
 
+  /*
+    TODO: should support natural length on only the last line.
+   */
   if (line_len_f_ < 0)
     my_solve_natural_len ();
   else
@@ -210,7 +247,6 @@ Simple_spacer::add_columns (Link_array<Paper_column> cols)
 void
 Simple_spacer::solve (Column_x_positions *positions) const
 {
-  positions->energy_f_  = energy_f ();  // abs (force_f_);
   positions->force_f_ = force_f_;
   
   positions->config_.push (indent_f_);
@@ -218,13 +254,40 @@ Simple_spacer::solve (Column_x_positions *positions) const
     {
       positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
     }
+  positions->cols_ = spaced_cols_;
+  positions->loose_cols_ = loose_cols_;
+  
+  positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
+
+
+  /*
+    Check if breaking constraints are met.
+   */
+  bool break_satisfy = true;
+  int sz =  positions->cols_.size ();
+  for (int i = sz; i--; )
+    {
+      SCM p = positions->cols_[i]->get_grob_property( "penalty");
+      if (gh_number_p (p))
+       {
+         if (gh_scm2double (p) < -9999)
+           break_satisfy = break_satisfy && (i == 0 || i == sz -1);
+         if (gh_scm2double (p) > 9999)
+           break_satisfy = break_satisfy && !(i == 0 || i == sz -1);
+       }
+      
+    }
 
-  positions->satisfies_constraints_b_ =  (line_len_f_ < 0) || active_b ();
+  positions->satisfies_constraints_b_ =
+    positions->satisfies_constraints_b_ && break_satisfy;
 }
 
 
 
-Spring_description::Spring_description( )
+
+
+
+Spring_description::Spring_description ()
 {
   ideal_f_ =0.0;
   hooke_f_ =0.0;
@@ -232,26 +295,11 @@ Spring_description::Spring_description( )
   block_force_f_ = 0.0;
 }
 
-Real
-Spring_description::energy_f (Real force) const
-{
-  Real stretch = (force >? block_force_f_) / hooke_f_;
-  Real e = 0.5 * stretch * stretch * hooke_f_;
-  return e;
-}
 
-Real
-Simple_spacer::energy_f () const
+bool
+Spring_description::sane_b () const
 {
-  Real e =0.;
-
-  for (int i=0; i <springs_.size (); i++)
-    {
-      e += springs_[i].energy_f (force_f_);
-    }
+  return (hooke_f_ > 0) &&  ! isinf (ideal_f_) && !isnan (ideal_f_);
+}
 
-  if (force_f_ < 0)
-    e *= compression_energy_factor_f_;
 
-  return e;
-}