]> 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   if (min_distance_ > distance_)
49     blocking_force_ = (min_distance_ - distance_) / inverse_stretch_strength_;
50   else
51     blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
52
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;
59
60   if (blocking_force_ >= 0)
61     inverse_compress_strength_ = 0;
62 }
63
64 /* scale a spring, but in a way that doesn't violate min_distance */
65 void
66 Spring::operator*= (Real r)
67 {
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 ();
72 }
73
74 bool
75 Spring::operator> (Spring const &other) const
76 {
77   return blocking_force_ > other.blocking_force_;
78 }
79
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 */
82 Spring
83 merge_springs (vector<Spring> const &springs)
84 {
85   Real avg_distance = 0;
86   Real min_distance = 0;
87   Real avg_stretch = 0;
88   Real avg_compress = 0;
89
90   for (vsize i = 0; i < springs.size (); i++)
91     {
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);
96     }
97
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);
102
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);
106
107   return ret;
108 }
109
110 void
111 Spring::set_distance (Real d)
112 {
113   if (d < 0 || isinf (d) || isnan (d))
114     programming_error ("insane spring distance requested, ignoring it");
115   else
116     {
117       distance_ = d;
118       update_blocking_force ();
119     }
120 }
121
122 void
123 Spring::set_min_distance (Real d)
124 {
125   if (d < 0 || isinf (d) || isnan (d))
126     programming_error ("insane spring min_distance requested, ignoring it");
127   else
128     {
129       min_distance_ = d;
130       update_blocking_force ();
131     }
132 }
133
134 void
135 Spring::ensure_min_distance (Real d)
136 {
137   set_min_distance (max (d, min_distance_));
138 }
139
140 void
141 Spring::set_inverse_stretch_strength (Real f)
142 {
143   if (isinf (f) || isnan (f) || f < 0)
144     programming_error ("insane spring constant");
145   else
146     inverse_stretch_strength_ = f;
147
148   update_blocking_force ();
149 }
150
151 void
152 Spring::set_inverse_compress_strength (Real f)
153 {
154   if (isinf (f) || isnan (f) || f < 0)
155     programming_error ("insane spring constant");
156   else
157     inverse_compress_strength_ = f;
158
159   update_blocking_force ();
160 }
161
162 void
163 Spring::set_blocking_force (Real f)
164 {
165   if (isinf (f) || isnan (f))
166     {
167       programming_error ("insane blocking force");
168       return;
169     }
170
171   blocking_force_ = -infinity_f;
172   min_distance_ = length (f);
173   distance_ = max (distance_, min_distance_);
174   update_blocking_force ();
175 }
176
177 void
178 Spring::set_default_strength ()
179 {
180   set_default_stretch_strength ();
181   set_default_compress_strength ();
182 }
183
184 void
185 Spring::set_default_compress_strength ()
186 {
187   inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
188   update_blocking_force ();
189 }
190
191 void
192 Spring::set_default_stretch_strength ()
193 {
194   inverse_stretch_strength_ = distance_;
195 }
196
197 Real
198 Spring::length (Real f) const
199 {
200   Real force = max (f, blocking_force_);
201   Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
202
203   if (force == infinity_f)
204     {
205       programming_error ("cruelty to springs");
206       force = 0.0;
207     }
208
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);
213 }
214