]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/spring.cc
Merge branch 'master' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / spring.cc
index 679ccfa29db3f00d2f792785a950e40be85f0b6a..fd8e0147cbd5accf74879376ad00f344c46eae5e 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2007 Joe Neeman <joeneeman@gmail.com>
+  (c) 2007--2009 Joe Neeman <joeneeman@gmail.com>
 */
 
 #include "spring.hh"
@@ -32,10 +32,16 @@ Spring::Spring (Real dist, Real min_dist)
 void
 Spring::update_blocking_force ()
 {
-  if (distance_ == min_distance_)
-    blocking_force_ = 0.0;
+  if (min_distance_ > distance_)
+    blocking_force_ = (min_distance_ - distance_) / inverse_stretch_strength_;
   else
     blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
+
+  if (isnan (blocking_force_) || blocking_force_ == infinity_f)
+    blocking_force_ = 0;
+
+  if (blocking_force_ >= 0)
+    inverse_compress_strength_ = 0;
 }
 
 /* scale a spring, but in a way that doesn't violate min_distance */
@@ -43,7 +49,7 @@ void
 Spring::operator*= (Real r)
 {
   distance_ = max (min_distance_, distance_ * r);
-  inverse_compress_strength_ = distance_ - min_distance_;
+  inverse_compress_strength_ = max (0.0, distance_ - min_distance_);
   inverse_stretch_strength_ *= 0.8;
 }
 
@@ -61,20 +67,24 @@ merge_springs (vector<Spring> const &springs)
   Real avg_distance = 0;
   Real min_distance = 0;
   Real avg_stretch = 0;
+  Real avg_compress = 0;
 
   for (vsize i = 0; i < springs.size (); i++)
     {
       avg_distance += springs[i].distance ();
       avg_stretch += springs[i].inverse_stretch_strength ();
+      avg_compress += 1 / springs[i].inverse_compress_strength ();
       min_distance = max (springs[i].min_distance (), min_distance);
     }
 
   avg_stretch /= springs.size ();
+  avg_compress /= springs.size ();
   avg_distance /= springs.size ();
   avg_distance = max (min_distance + 0.3, avg_distance);
 
   Spring ret = Spring (avg_distance, min_distance);
   ret.set_inverse_stretch_strength (avg_stretch);
+  ret.set_inverse_compress_strength (1 / avg_compress);
 
   return ret;
 }
@@ -86,7 +96,6 @@ Spring::set_distance (Real d)
     programming_error ("insane spring distance requested, ignoring it");
   else
     {
-      min_distance_ = min (min_distance_, d);
       distance_ = d;
       update_blocking_force ();
     }
@@ -100,11 +109,16 @@ Spring::set_min_distance (Real d)
   else
     {
       min_distance_ = d;
-      distance_ = max (distance_, min_distance_);
       update_blocking_force ();
     }
 }
 
+void
+Spring::ensure_min_distance (Real d)
+{
+  set_min_distance (max (d, min_distance_));
+}
+
 void
 Spring::set_inverse_stretch_strength (Real f)
 {
@@ -143,8 +157,8 @@ Spring::set_blocking_force (Real f)
 void
 Spring::set_default_strength ()
 {
-  inverse_compress_strength_ = distance_ - min_distance_;
-  inverse_stretch_strength_ = distance_ - min_distance_;
+  inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
+  inverse_stretch_strength_ = distance_;
 }
 
 Real
@@ -152,5 +166,12 @@ Spring::length (Real f) const
 {
   Real force = max (f, blocking_force_);
   Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
-  return distance_ + force * inv_k;
+
+  if (isinf (force))
+    {
+      programming_error ("cruelty to springs");
+      force = 0.0;
+    }
+
+  return max (min_distance_, distance_ + force * inv_k);
 }