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;
60 else if (inverse_compress_strength_ > 0.0)
61 blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
63 blocking_force_ = 0.0;
66 /* scale a spring, but in a way that doesn't violate min_distance */
68 Spring::operator *= (Real r)
70 distance_ = max (min_distance_, distance_ * r);
71 inverse_compress_strength_ = max (0.0, distance_ - min_distance_);
72 inverse_stretch_strength_ *= 0.8;
73 update_blocking_force ();
77 Spring::operator > (Spring const &other) const
79 return blocking_force_ > other.blocking_force_;
82 /* merge springs, basically by averaging them, but leave a little headroom
83 above the largest minimum distance so that things don't get too cramped */
85 merge_springs (vector<Spring> const &springs)
87 Real avg_distance = 0;
88 Real min_distance = 0;
90 Real avg_compress = 0;
92 for (vsize i = 0; i < springs.size (); i++)
94 avg_distance += springs[i].distance ();
95 avg_stretch += springs[i].inverse_stretch_strength ();
96 avg_compress += 1 / springs[i].inverse_compress_strength ();
97 min_distance = max (springs[i].min_distance (), min_distance);
100 avg_stretch /= Real (springs.size ());
101 avg_compress /= Real (springs.size ());
102 avg_distance /= Real (springs.size ());
103 avg_distance = max (min_distance + 0.3, avg_distance);
105 Spring ret = Spring (avg_distance, min_distance);
106 ret.set_inverse_stretch_strength (avg_stretch);
107 ret.set_inverse_compress_strength (1 / avg_compress);
113 Spring::set_distance (Real d)
115 if (d < 0 || isinf (d) || isnan (d))
116 programming_error ("insane spring distance requested, ignoring it");
120 update_blocking_force ();
125 Spring::set_min_distance (Real d)
127 if (d < 0 || isinf (d) || isnan (d))
128 programming_error ("insane spring min_distance requested, ignoring it");
132 update_blocking_force ();
137 Spring::ensure_min_distance (Real d)
139 set_min_distance (max (d, min_distance_));
143 Spring::set_inverse_stretch_strength (Real f)
145 if (isinf (f) || isnan (f) || f < 0)
146 programming_error ("insane spring constant");
148 inverse_stretch_strength_ = f;
150 update_blocking_force ();
154 Spring::set_inverse_compress_strength (Real f)
156 if (isinf (f) || isnan (f) || f < 0)
157 programming_error ("insane spring constant");
159 inverse_compress_strength_ = f;
161 update_blocking_force ();
165 Spring::set_blocking_force (Real f)
167 if (isinf (f) || isnan (f))
169 programming_error ("insane blocking force");
173 blocking_force_ = -infinity_f;
174 min_distance_ = length (f);
175 distance_ = max (distance_, min_distance_);
176 update_blocking_force ();
180 Spring::set_default_strength ()
182 set_default_stretch_strength ();
183 set_default_compress_strength ();
187 Spring::set_default_compress_strength ()
189 inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
190 update_blocking_force ();
194 Spring::set_default_stretch_strength ()
196 inverse_stretch_strength_ = distance_;
200 Spring::length (Real f) const
202 Real force = max (f, blocking_force_);
203 Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
207 programming_error ("cruelty to springs");
211 // There is a corner case here: if min_distance_ is larger than
212 // distance_ but the spring is fixed, then inv_k will be zero
213 // and we need to make sure that we return min_distance_.
214 return max (min_distance_, distance_ + force * inv_k);