]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/simple-spacer.cc
release: 1.3.71
[lilypond.git] / lily / simple-spacer.cc
index 85ec8481234be6b98c6379917507f3df9d6e4721..9e23cb8c52807cf645a187b07204554a50c03ee8 100644 (file)
@@ -7,12 +7,11 @@
 
   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>
 
 #include "simple-spacer.hh"
 #include "paper-column.hh"
@@ -20,6 +19,7 @@
 #include "rod.hh"
 #include "warn.hh"
 #include "column-x-positions.hh"
+#include "spaceable-element.hh"
 #include "dimensions.hh"
 
 Simple_spacer::Simple_spacer ()
@@ -27,7 +27,6 @@ Simple_spacer::Simple_spacer ()
   force_f_ = 0.;
   indent_f_ =0.0;
   default_space_f_ = 20 PT;
-  compression_energy_factor_f_ = 3.0;
 }
 
 void
@@ -166,37 +165,40 @@ Simple_spacer::my_solve_natural_len ()
 }
 
 void
-Simple_spacer::add_columns (Link_array<Paper_column> cols)
+Simple_spacer::add_columns (Link_array<Score_element> 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_UNDEFINED;
+      for (SCM s = Spaceable_element::get_ideal_distances (cols[i]);
+          spring_params == SCM_UNDEFINED && gh_pair_p (s);
+          s = gh_cdr (s))
        {
-         Column_spring &sp = c->spring_arr_drul_[RIGHT] [j];
-         if (sp.other_l_ != cols[i+1])
+         Score_element *other = unsmob_element (gh_caar (s));
+         if (other != cols[i+1])
            continue;
 
-         to_next = &sp;
+         spring_params = gh_cdar (s);
        }
 
       Spring_description desc;
-      if (to_next)
+      if (spring_params != SCM_UNDEFINED)
        {
-         desc.hooke_f_ = to_next->strength_f_;
-         desc.ideal_f_ = to_next->distance_f_;
+         desc.ideal_f_ = gh_scm2double (gh_car (spring_params));
+         desc.hooke_f_ = gh_scm2double (gh_cdr (spring_params));
        }
       else
        {
+         programming_error ("No spring between adjacent columns");
          desc.hooke_f_ = 1.0;
          desc.ideal_f_ = default_space_f_;
        }
 
       if (!desc.sane_b ())
        {
-         programming_error ("Insane spring.");
-         continue;
+         programming_error ("Insane spring found. Setting to unit spring.");
+         desc.hooke_f_ = 1.0;
+         desc.ideal_f_ = 1.0;
        }
       
       desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
@@ -205,17 +207,21 @@ Simple_spacer::add_columns (Link_array<Paper_column> cols)
   
   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_element::get_minimum_distances (cols[i]);
+          gh_pair_p (s); s = gh_cdr (s))
        {
-         int oi = cols.find_i (rods->elem (j).other_l_ );
+         Score_element * other = unsmob_element (gh_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 (gh_cdar (s)));
            }
        }
     }
 
+  /*
+    TODO: should support natural length on only the last line.
+   */
   if (line_len_f_ < 0)
     my_solve_natural_len ();
   else
@@ -225,7 +231,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_);
@@ -247,13 +252,6 @@ 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;
-}
 
 bool
 Spring_description::sane_b () const
@@ -262,18 +260,3 @@ Spring_description::sane_b () const
 }
 
 
-Real
-Simple_spacer::energy_f () const
-{
-  Real e =0.;
-
-  for (int i=0; i <springs_.size (); i++)
-    {
-      e += springs_[i].energy_f (force_f_);
-    }
-
-  if (force_f_ < 0)
-    e *= compression_energy_factor_f_;
-
-  return e;
-}