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 // blocking_force_ is the value of force
49 // below which length(force) is constant, and
50 // above which length(force) varies according to inverse_*_strength.
51 // Simple_spacer::compress_line() depends on the condition above.
52 // We assume inverse_*_strength are non-negative.
53 if (min_distance_ > distance_)
54 if (inverse_stretch_strength_ > 0.0)
55 blocking_force_ = (min_distance_ - distance_) / inverse_stretch_strength_;
57 // Conceptually, this should be +inf, but 0.0 meets the requirements
58 // of Simple_spacer and creates fewer cases of 0.0*inf to handle.
59 blocking_force_ = 0.0;
61 if (inverse_compress_strength_ > 0.0)
62 blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
64 blocking_force_ = 0.0;
67 /* scale a spring, but in a way that doesn't violate min_distance */
69 Spring::operator*= (Real r)
71 distance_ = max (min_distance_, distance_ * r);
72 inverse_compress_strength_ = max (0.0, distance_ - min_distance_);
73 inverse_stretch_strength_ *= 0.8;
74 update_blocking_force ();
78 Spring::operator> (Spring const &other) const
80 return blocking_force_ > other.blocking_force_;
83 /* merge springs, basically by averaging them, but leave a little headroom
84 above the largest minimum distance so that things don't get too cramped */
86 merge_springs (vector<Spring> const &springs)
88 Real avg_distance = 0;
89 Real min_distance = 0;
91 Real avg_compress = 0;
93 for (vsize i = 0; i < springs.size (); i++)
95 avg_distance += springs[i].distance ();
96 avg_stretch += springs[i].inverse_stretch_strength ();
97 avg_compress += 1 / springs[i].inverse_compress_strength ();
98 min_distance = max (springs[i].min_distance (), min_distance);
101 avg_stretch /= springs.size ();
102 avg_compress /= springs.size ();
103 avg_distance /= springs.size ();
104 avg_distance = max (min_distance + 0.3, avg_distance);
106 Spring ret = Spring (avg_distance, min_distance);
107 ret.set_inverse_stretch_strength (avg_stretch);
108 ret.set_inverse_compress_strength (1 / avg_compress);
114 Spring::set_distance (Real d)
116 if (d < 0 || isinf (d) || isnan (d))
117 programming_error ("insane spring distance requested, ignoring it");
121 update_blocking_force ();
126 Spring::set_min_distance (Real d)
128 if (d < 0 || isinf (d) || isnan (d))
129 programming_error ("insane spring min_distance requested, ignoring it");
133 update_blocking_force ();
138 Spring::ensure_min_distance (Real d)
140 set_min_distance (max (d, min_distance_));
144 Spring::set_inverse_stretch_strength (Real f)
146 if (isinf (f) || isnan (f) || f < 0)
147 programming_error ("insane spring constant");
149 inverse_stretch_strength_ = f;
151 update_blocking_force ();
155 Spring::set_inverse_compress_strength (Real f)
157 if (isinf (f) || isnan (f) || f < 0)
158 programming_error ("insane spring constant");
160 inverse_compress_strength_ = f;
162 update_blocking_force ();
166 Spring::set_blocking_force (Real f)
168 if (isinf (f) || isnan (f))
170 programming_error ("insane blocking force");
174 blocking_force_ = -infinity_f;
175 min_distance_ = length (f);
176 distance_ = max (distance_, min_distance_);
177 update_blocking_force ();
181 Spring::set_default_strength ()
183 set_default_stretch_strength ();
184 set_default_compress_strength ();
188 Spring::set_default_compress_strength ()
190 inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
191 update_blocking_force ();
195 Spring::set_default_stretch_strength ()
197 inverse_stretch_strength_ = distance_;
201 Spring::length (Real f) const
203 Real force = max (f, blocking_force_);
204 Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
208 programming_error ("cruelty to springs");
212 // There is a corner case here: if min_distance_ is larger than
213 // distance_ but the spring is fixed, then inv_k will be zero
214 // and we need to make sure that we return min_distance_.
215 return max (min_distance_, distance_ + force * inv_k);