]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/spring.cc
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / spring.cc
index 67d41c9c39640e0add681aa50728ec5e5f240861..fd8e0147cbd5accf74879376ad00f344c46eae5e 100644 (file)
@@ -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;
 }
 
@@ -90,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 ();
     }
@@ -104,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)
 {
@@ -147,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
@@ -163,5 +173,5 @@ Spring::length (Real f) const
       force = 0.0;
     }
 
-  return distance_ + force * inv_k;
+  return max (min_distance_, distance_ + force * inv_k);
 }