]> git.donarmstrong.com Git - lilypond.git/blob - lily/spring.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / spring.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2007--2011 Joe Neeman <joeneeman@gmail.com>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "spring.hh"
21
22 Spring::Spring ()
23 {
24   distance_ = 1.0;
25   min_distance_ = 1.0;
26   inverse_stretch_strength_ = 1.0;
27   inverse_compress_strength_ = 1.0;
28
29   update_blocking_force ();
30 }
31
32 Spring::Spring (Real dist, Real min_dist)
33 {
34   distance_ = 1.0;
35   min_distance_ = 1.0;
36   inverse_stretch_strength_ = 1.0;
37   inverse_compress_strength_ = 1.0;
38
39   set_distance (dist);
40   set_min_distance (min_dist);
41   set_default_strength ();
42   update_blocking_force ();
43 }
44
45 void
46 Spring::update_blocking_force ()
47 {
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_;
56     else
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_;
62   else
63     blocking_force_ = 0.0;
64 }
65
66 /* scale a spring, but in a way that doesn't violate min_distance */
67 void
68 Spring::operator *= (Real r)
69 {
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 ();
74 }
75
76 bool
77 Spring::operator > (Spring const &other) const
78 {
79   return blocking_force_ > other.blocking_force_;
80 }
81
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 */
84 Spring
85 merge_springs (vector<Spring> const &springs)
86 {
87   Real avg_distance = 0;
88   Real min_distance = 0;
89   Real avg_stretch = 0;
90   Real avg_compress = 0;
91
92   for (vsize i = 0; i < springs.size (); i++)
93     {
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);
98     }
99
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);
104
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);
108
109   return ret;
110 }
111
112 void
113 Spring::set_distance (Real d)
114 {
115   if (d < 0 || isinf (d) || isnan (d))
116     programming_error ("insane spring distance requested, ignoring it");
117   else
118     {
119       distance_ = d;
120       update_blocking_force ();
121     }
122 }
123
124 void
125 Spring::set_min_distance (Real d)
126 {
127   if (d < 0 || isinf (d) || isnan (d))
128     programming_error ("insane spring min_distance requested, ignoring it");
129   else
130     {
131       min_distance_ = d;
132       update_blocking_force ();
133     }
134 }
135
136 void
137 Spring::ensure_min_distance (Real d)
138 {
139   set_min_distance (max (d, min_distance_));
140 }
141
142 void
143 Spring::set_inverse_stretch_strength (Real f)
144 {
145   if (isinf (f) || isnan (f) || f < 0)
146     programming_error ("insane spring constant");
147   else
148     inverse_stretch_strength_ = f;
149
150   update_blocking_force ();
151 }
152
153 void
154 Spring::set_inverse_compress_strength (Real f)
155 {
156   if (isinf (f) || isnan (f) || f < 0)
157     programming_error ("insane spring constant");
158   else
159     inverse_compress_strength_ = f;
160
161   update_blocking_force ();
162 }
163
164 void
165 Spring::set_blocking_force (Real f)
166 {
167   if (isinf (f) || isnan (f))
168     {
169       programming_error ("insane blocking force");
170       return;
171     }
172
173   blocking_force_ = -infinity_f;
174   min_distance_ = length (f);
175   distance_ = max (distance_, min_distance_);
176   update_blocking_force ();
177 }
178
179 void
180 Spring::set_default_strength ()
181 {
182   set_default_stretch_strength ();
183   set_default_compress_strength ();
184 }
185
186 void
187 Spring::set_default_compress_strength ()
188 {
189   inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
190   update_blocking_force ();
191 }
192
193 void
194 Spring::set_default_stretch_strength ()
195 {
196   inverse_stretch_strength_ = distance_;
197 }
198
199 Real
200 Spring::length (Real f) const
201 {
202   Real force = max (f, blocking_force_);
203   Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
204
205   if (isinf (force))
206     {
207       programming_error ("cruelty to springs");
208       force = 0.0;
209     }
210
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);
215 }
216