]> git.donarmstrong.com Git - lilypond.git/blob - lily/spring.cc
Fix 899.
[lilypond.git] / lily / spring.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2007--2010 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. -infinity_f works fine for now.
55   if (isnan (blocking_force_) || blocking_force_ == infinity_f)
56     blocking_force_ = -infinity_f;
57
58   if (blocking_force_ >= 0)
59     inverse_compress_strength_ = 0;
60 }
61
62 /* scale a spring, but in a way that doesn't violate min_distance */
63 void
64 Spring::operator*= (Real r)
65 {
66   distance_ = max (min_distance_, distance_ * r);
67   inverse_compress_strength_ = max (0.0, distance_ - min_distance_);
68   inverse_stretch_strength_ *= 0.8;
69   update_blocking_force ();
70 }
71
72 bool
73 Spring::operator> (Spring const &other) const
74 {
75   return blocking_force_ > other.blocking_force_;
76 }
77
78 /* merge springs, basically by averaging them, but leave a little headroom
79    above the largest minimum distance so that things don't get too cramped */
80 Spring
81 merge_springs (vector<Spring> const &springs)
82 {
83   Real avg_distance = 0;
84   Real min_distance = 0;
85   Real avg_stretch = 0;
86   Real avg_compress = 0;
87
88   for (vsize i = 0; i < springs.size (); i++)
89     {
90       avg_distance += springs[i].distance ();
91       avg_stretch += springs[i].inverse_stretch_strength ();
92       avg_compress += 1 / springs[i].inverse_compress_strength ();
93       min_distance = max (springs[i].min_distance (), min_distance);
94     }
95
96   avg_stretch /= springs.size ();
97   avg_compress /= springs.size ();
98   avg_distance /= springs.size ();
99   avg_distance = max (min_distance + 0.3, avg_distance);
100
101   Spring ret = Spring (avg_distance, min_distance);
102   ret.set_inverse_stretch_strength (avg_stretch);
103   ret.set_inverse_compress_strength (1 / avg_compress);
104
105   return ret;
106 }
107
108 void
109 Spring::set_distance (Real d)
110 {
111   if (d < 0 || isinf (d) || isnan (d))
112     programming_error ("insane spring distance requested, ignoring it");
113   else
114     {
115       distance_ = d;
116       update_blocking_force ();
117     }
118 }
119
120 void
121 Spring::set_min_distance (Real d)
122 {
123   if (d < 0 || isinf (d) || isnan (d))
124     programming_error ("insane spring min_distance requested, ignoring it");
125   else
126     {
127       min_distance_ = d;
128       update_blocking_force ();
129     }
130 }
131
132 void
133 Spring::ensure_min_distance (Real d)
134 {
135   set_min_distance (max (d, min_distance_));
136 }
137
138 void
139 Spring::set_inverse_stretch_strength (Real f)
140 {
141   if (isinf (f) || isnan (f) || f < 0)
142     programming_error ("insane spring constant");
143   else
144     inverse_stretch_strength_ = f;
145
146   update_blocking_force ();
147 }
148
149 void
150 Spring::set_inverse_compress_strength (Real f)
151 {
152   if (isinf (f) || isnan (f) || f < 0)
153     programming_error ("insane spring constant");
154   else
155     inverse_compress_strength_ = f;
156
157   update_blocking_force ();
158 }
159
160 void
161 Spring::set_blocking_force (Real f)
162 {
163   if (isinf (f) || isnan (f))
164     {
165       programming_error ("insane blocking force");
166       return;
167     }
168
169   blocking_force_ = -infinity_f;
170   min_distance_ = length (f);
171   distance_ = max (distance_, min_distance_);
172   update_blocking_force ();
173 }
174
175 void
176 Spring::set_default_strength ()
177 {
178   set_default_stretch_strength ();
179   set_default_compress_strength ();
180 }
181
182 void
183 Spring::set_default_compress_strength ()
184 {
185   inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
186   update_blocking_force ();
187 }
188
189 void
190 Spring::set_default_stretch_strength ()
191 {
192   inverse_stretch_strength_ = distance_;
193 }
194
195 Real
196 Spring::length (Real f) const
197 {
198   Real force = max (f, blocking_force_);
199   Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
200
201   if (force == infinity_f)
202     {
203       programming_error ("cruelty to springs");
204       force = 0.0;
205     }
206
207   // There is a corner case here: if min_distance_ is larger than
208   // distance_ but the spring is fixed, then inv_k will be zero
209   // and we need to make sure that we return min_distance_.
210   return max (min_distance_, distance_ + force * inv_k);
211 }
212