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