2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2007--2011 Joe Neeman <joeneeman@gmail.com>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
26 inverse_stretch_strength_ = 1.0;
27 inverse_compress_strength_ = 1.0;
29 update_blocking_force ();
32 Spring::Spring (Real dist, Real min_dist)
36 inverse_stretch_strength_ = 1.0;
37 inverse_compress_strength_ = 1.0;
40 set_min_distance (min_dist);
41 set_default_strength ();
42 update_blocking_force ();
46 Spring::update_blocking_force ()
48 if (min_distance_ > distance_)
49 blocking_force_ = (min_distance_ - distance_) / inverse_stretch_strength_;
51 blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
53 // If the spring is fixed, it's not clear what the natural value
54 // of blocking_force_ would be (because it always blocks).
55 // -infinity_f works fine for now.
56 // If inverse_stretch_strength > 0, the spring is not fixed (because it can stretch).
57 if (isnan (blocking_force_) || blocking_force_ == infinity_f)
58 blocking_force_ = (inverse_stretch_strength_ > 0) ? 0.0 : -infinity_f;
60 if (blocking_force_ >= 0)
61 inverse_compress_strength_ = 0;
64 /* scale a spring, but in a way that doesn't violate min_distance */
66 Spring::operator*= (Real r)
68 distance_ = max (min_distance_, distance_ * r);
69 inverse_compress_strength_ = max (0.0, distance_ - min_distance_);
70 inverse_stretch_strength_ *= 0.8;
71 update_blocking_force ();
75 Spring::operator> (Spring const &other) const
77 return blocking_force_ > other.blocking_force_;
80 /* merge springs, basically by averaging them, but leave a little headroom
81 above the largest minimum distance so that things don't get too cramped */
83 merge_springs (vector<Spring> const &springs)
85 Real avg_distance = 0;
86 Real min_distance = 0;
88 Real avg_compress = 0;
90 for (vsize i = 0; i < springs.size (); i++)
92 avg_distance += springs[i].distance ();
93 avg_stretch += springs[i].inverse_stretch_strength ();
94 avg_compress += 1 / springs[i].inverse_compress_strength ();
95 min_distance = max (springs[i].min_distance (), min_distance);
98 avg_stretch /= springs.size ();
99 avg_compress /= springs.size ();
100 avg_distance /= springs.size ();
101 avg_distance = max (min_distance + 0.3, avg_distance);
103 Spring ret = Spring (avg_distance, min_distance);
104 ret.set_inverse_stretch_strength (avg_stretch);
105 ret.set_inverse_compress_strength (1 / avg_compress);
111 Spring::set_distance (Real d)
113 if (d < 0 || isinf (d) || isnan (d))
114 programming_error ("insane spring distance requested, ignoring it");
118 update_blocking_force ();
123 Spring::set_min_distance (Real d)
125 if (d < 0 || isinf (d) || isnan (d))
126 programming_error ("insane spring min_distance requested, ignoring it");
130 update_blocking_force ();
135 Spring::ensure_min_distance (Real d)
137 set_min_distance (max (d, min_distance_));
141 Spring::set_inverse_stretch_strength (Real f)
143 if (isinf (f) || isnan (f) || f < 0)
144 programming_error ("insane spring constant");
146 inverse_stretch_strength_ = f;
148 update_blocking_force ();
152 Spring::set_inverse_compress_strength (Real f)
154 if (isinf (f) || isnan (f) || f < 0)
155 programming_error ("insane spring constant");
157 inverse_compress_strength_ = f;
159 update_blocking_force ();
163 Spring::set_blocking_force (Real f)
165 if (isinf (f) || isnan (f))
167 programming_error ("insane blocking force");
171 blocking_force_ = -infinity_f;
172 min_distance_ = length (f);
173 distance_ = max (distance_, min_distance_);
174 update_blocking_force ();
178 Spring::set_default_strength ()
180 set_default_stretch_strength ();
181 set_default_compress_strength ();
185 Spring::set_default_compress_strength ()
187 inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
188 update_blocking_force ();
192 Spring::set_default_stretch_strength ()
194 inverse_stretch_strength_ = distance_;
198 Spring::length (Real f) const
200 Real force = max (f, blocking_force_);
201 Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
203 if (force == infinity_f)
205 programming_error ("cruelty to springs");
209 // There is a corner case here: if min_distance_ is larger than
210 // distance_ but the spring is fixed, then inv_k will be zero
211 // and we need to make sure that we return min_distance_.
212 return max (min_distance_, distance_ + force * inv_k);