2 spring.cc -- declare Spring
4 source file of the GNU LilyPond music typesetter
6 (c) 2007--2009 Joe Neeman <joeneeman@gmail.com>
15 inverse_stretch_strength_ = 1.0;
16 inverse_compress_strength_ = 1.0;
18 update_blocking_force ();
21 Spring::Spring (Real dist, Real min_dist)
25 inverse_stretch_strength_ = 1.0;
26 inverse_compress_strength_ = 1.0;
29 set_min_distance (min_dist);
30 set_default_strength ();
31 update_blocking_force ();
35 Spring::update_blocking_force ()
37 if (min_distance_ > distance_)
38 blocking_force_ = (min_distance_ - distance_) / inverse_stretch_strength_;
40 blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
42 // If the spring is fixed, it's not clear what the natural value
43 // of blocking_force_ would be. -infinity_f works fine for now.
44 if (isnan (blocking_force_) || blocking_force_ == infinity_f)
45 blocking_force_ = -infinity_f;
47 if (blocking_force_ >= 0)
48 inverse_compress_strength_ = 0;
51 /* scale a spring, but in a way that doesn't violate min_distance */
53 Spring::operator*= (Real r)
55 distance_ = max (min_distance_, distance_ * r);
56 inverse_compress_strength_ = max (0.0, distance_ - min_distance_);
57 inverse_stretch_strength_ *= 0.8;
58 update_blocking_force ();
62 Spring::operator> (Spring const &other) const
64 return blocking_force_ > other.blocking_force_;
67 /* merge springs, basically by averaging them, but leave a little headroom
68 above the largest minimum distance so that things don't get too cramped */
70 merge_springs (vector<Spring> const &springs)
72 Real avg_distance = 0;
73 Real min_distance = 0;
75 Real avg_compress = 0;
77 for (vsize i = 0; i < springs.size (); i++)
79 avg_distance += springs[i].distance ();
80 avg_stretch += springs[i].inverse_stretch_strength ();
81 avg_compress += 1 / springs[i].inverse_compress_strength ();
82 min_distance = max (springs[i].min_distance (), min_distance);
85 avg_stretch /= springs.size ();
86 avg_compress /= springs.size ();
87 avg_distance /= springs.size ();
88 avg_distance = max (min_distance + 0.3, avg_distance);
90 Spring ret = Spring (avg_distance, min_distance);
91 ret.set_inverse_stretch_strength (avg_stretch);
92 ret.set_inverse_compress_strength (1 / avg_compress);
98 Spring::set_distance (Real d)
100 if (d < 0 || isinf (d) || isnan (d))
101 programming_error ("insane spring distance requested, ignoring it");
105 update_blocking_force ();
110 Spring::set_min_distance (Real d)
112 if (d < 0 || isinf (d) || isnan (d))
113 programming_error ("insane spring min_distance requested, ignoring it");
117 update_blocking_force ();
122 Spring::ensure_min_distance (Real d)
124 set_min_distance (max (d, min_distance_));
128 Spring::set_inverse_stretch_strength (Real f)
130 if (isinf (f) || isnan (f) || f < 0)
131 programming_error ("insane spring constant");
133 inverse_stretch_strength_ = f;
135 update_blocking_force ();
139 Spring::set_inverse_compress_strength (Real f)
141 if (isinf (f) || isnan (f) || f < 0)
142 programming_error ("insane spring constant");
144 inverse_compress_strength_ = f;
146 update_blocking_force ();
150 Spring::set_blocking_force (Real f)
152 if (isinf (f) || isnan (f))
154 programming_error ("insane blocking force");
158 blocking_force_ = -infinity_f;
159 min_distance_ = length (f);
160 distance_ = max (distance_, min_distance_);
161 update_blocking_force ();
165 Spring::set_default_strength ()
167 inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
168 inverse_stretch_strength_ = distance_;
169 update_blocking_force ();
173 Spring::length (Real f) const
175 Real force = max (f, blocking_force_);
176 Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
178 if (force == infinity_f)
180 programming_error ("cruelty to springs");
184 // There is a corner case here: if min_distance_ is larger than
185 // distance_ but the spring is fixed, then inv_k will be zero
186 // and we need to make sure that we return min_distance_.
187 return max (min_distance_, distance_ + force * inv_k);