]> git.donarmstrong.com Git - lilypond.git/blob - lily/spring.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / spring.cc
1 /*
2   spring.cc -- declare Spring
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2007--2009 Joe Neeman <joeneeman@gmail.com>
7 */
8
9 #include "spring.hh"
10
11 Spring::Spring ()
12 {
13   distance_ = 1.0;
14   min_distance_ = 1.0;
15   inverse_stretch_strength_ = 1.0;
16   inverse_compress_strength_ = 1.0;
17
18   update_blocking_force ();
19 }
20
21 Spring::Spring (Real dist, Real min_dist)
22 {
23   distance_ = 1.0;
24   min_distance_ = 1.0;
25
26   set_distance (dist);
27   set_min_distance (min_dist);
28   set_default_strength ();
29   update_blocking_force ();
30 }
31
32 void
33 Spring::update_blocking_force ()
34 {
35   if (min_distance_ > distance_)
36     blocking_force_ = (min_distance_ - distance_) / inverse_stretch_strength_;
37   else
38     blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
39
40   // If the spring is fixed, it's not clear what the natural value
41   // of blocking_force_ would be. -infinity_f works fine for now.
42   if (isnan (blocking_force_) || blocking_force_ == infinity_f)
43     blocking_force_ = -infinity_f;
44
45   if (blocking_force_ >= 0)
46     inverse_compress_strength_ = 0;
47 }
48
49 /* scale a spring, but in a way that doesn't violate min_distance */
50 void
51 Spring::operator*= (Real r)
52 {
53   distance_ = max (min_distance_, distance_ * r);
54   inverse_compress_strength_ = max (0.0, distance_ - min_distance_);
55   inverse_stretch_strength_ *= 0.8;
56   update_blocking_force ();
57 }
58
59 bool
60 Spring::operator> (Spring const &other) const
61 {
62   return blocking_force_ > other.blocking_force_;
63 }
64
65 /* merge springs, basically by averaging them, but leave a little headroom
66    above the largest minimum distance so that things don't get too cramped */
67 Spring
68 merge_springs (vector<Spring> const &springs)
69 {
70   Real avg_distance = 0;
71   Real min_distance = 0;
72   Real avg_stretch = 0;
73   Real avg_compress = 0;
74
75   for (vsize i = 0; i < springs.size (); i++)
76     {
77       avg_distance += springs[i].distance ();
78       avg_stretch += springs[i].inverse_stretch_strength ();
79       avg_compress += 1 / springs[i].inverse_compress_strength ();
80       min_distance = max (springs[i].min_distance (), min_distance);
81     }
82
83   avg_stretch /= springs.size ();
84   avg_compress /= springs.size ();
85   avg_distance /= springs.size ();
86   avg_distance = max (min_distance + 0.3, avg_distance);
87
88   Spring ret = Spring (avg_distance, min_distance);
89   ret.set_inverse_stretch_strength (avg_stretch);
90   ret.set_inverse_compress_strength (1 / avg_compress);
91
92   return ret;
93 }
94
95 void
96 Spring::set_distance (Real d)
97 {
98   if (d < 0 || isinf (d) || isnan (d))
99     programming_error ("insane spring distance requested, ignoring it");
100   else
101     {
102       distance_ = d;
103       update_blocking_force ();
104     }
105 }
106
107 void
108 Spring::set_min_distance (Real d)
109 {
110   if (d < 0 || isinf (d) || isnan (d))
111     programming_error ("insane spring min_distance requested, ignoring it");
112   else
113     {
114       min_distance_ = d;
115       update_blocking_force ();
116     }
117 }
118
119 void
120 Spring::ensure_min_distance (Real d)
121 {
122   set_min_distance (max (d, min_distance_));
123 }
124
125 void
126 Spring::set_inverse_stretch_strength (Real f)
127 {
128   if (isinf (f) || isnan (f) || f < 0)
129     programming_error ("insane spring constant");
130   else
131     inverse_stretch_strength_ = f;
132
133   update_blocking_force ();
134 }
135
136 void
137 Spring::set_inverse_compress_strength (Real f)
138 {
139   if (isinf (f) || isnan (f) || f < 0)
140     programming_error ("insane spring constant");
141   else
142     inverse_compress_strength_ = f;
143
144   update_blocking_force ();
145 }
146
147 void
148 Spring::set_blocking_force (Real f)
149 {
150   if (isinf (f) || isnan (f))
151     {
152       programming_error ("insane blocking force");
153       return;
154     }
155
156   blocking_force_ = -infinity_f;
157   min_distance_ = length (f);
158   distance_ = max (distance_, min_distance_);
159   update_blocking_force ();
160 }
161
162 void
163 Spring::set_default_strength ()
164 {
165   inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
166   inverse_stretch_strength_ = distance_;
167   update_blocking_force ();
168 }
169
170 Real
171 Spring::length (Real f) const
172 {
173   Real force = max (f, blocking_force_);
174   Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
175
176   if (force == infinity_f)
177     {
178       programming_error ("cruelty to springs");
179       force = 0.0;
180     }
181
182   // There is a corner case here: if min_distance_ is larger than
183   // distance_ but the spring is fixed, then inv_k will be zero
184   // and we need to make sure that we return min_distance_.
185   return max (min_distance_, distance_ + force * inv_k);
186 }
187