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