2 spring.cc -- declare Spring
4 source file of the GNU LilyPond music typesetter
6 (c) 2007 Joe Neeman <joeneeman@gmail.com>
15 inverse_stretch_strength_ = 1.0;
16 inverse_compress_strength_ = 1.0;
18 update_blocking_force ();
21 Spring::Spring (Real dist, Real min_dist)
27 set_min_distance (min_dist);
28 set_default_strength ();
29 update_blocking_force ();
33 Spring::update_blocking_force ()
35 if (distance_ == min_distance_)
36 blocking_force_ = 0.0;
38 blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
41 /* scale a spring, but in a way that doesn't violate min_distance */
43 Spring::operator*= (Real r)
45 distance_ = max (min_distance_, distance_ * r);
46 inverse_compress_strength_ = distance_ - min_distance_;
47 inverse_stretch_strength_ *= 0.8;
51 Spring::operator> (Spring const &other) const
53 return blocking_force_ > other.blocking_force_;
56 /* merge springs, basically by averaging them, but leave a little headroom
57 above the largest minimum distance so that things don't get too cramped */
59 merge_springs (vector<Spring> const &springs)
61 Real avg_distance = 0;
62 Real min_distance = 0;
65 for (vsize i = 0; i < springs.size (); i++)
67 avg_distance += springs[i].distance ();
68 avg_stretch += springs[i].inverse_stretch_strength ();
69 min_distance = max (springs[i].min_distance (), min_distance);
72 avg_stretch /= springs.size ();
73 avg_distance /= springs.size ();
74 avg_distance = max (min_distance + 0.3, avg_distance);
76 Spring ret = Spring (avg_distance, min_distance);
77 ret.set_inverse_stretch_strength (avg_stretch);
83 Spring::set_distance (Real d)
85 if (d < 0 || isinf (d) || isnan (d))
86 programming_error ("insane spring distance requested, ignoring it");
89 min_distance_ = min (min_distance_, d);
91 update_blocking_force ();
96 Spring::set_min_distance (Real d)
98 if (d < 0 || isinf (d) || isnan (d))
99 programming_error ("insane spring min_distance requested, ignoring it");
103 distance_ = max (distance_, min_distance_);
104 update_blocking_force ();
109 Spring::set_inverse_stretch_strength (Real f)
111 if (isinf (f) || isnan (f) || f < 0)
112 programming_error ("insane spring constant");
114 inverse_stretch_strength_ = f;
118 Spring::set_inverse_compress_strength (Real f)
120 if (isinf (f) || isnan (f) || f < 0)
121 programming_error ("insane spring constant");
123 inverse_compress_strength_ = f;
125 update_blocking_force ();
129 Spring::set_blocking_force (Real f)
131 if (isinf (f) || isnan (f))
133 programming_error ("insane blocking force");
137 blocking_force_ = -infinity_f;
138 min_distance_ = length (f);
139 distance_ = max (distance_, min_distance_);
140 update_blocking_force ();
144 Spring::set_default_strength ()
146 inverse_compress_strength_ = distance_ - min_distance_;
147 inverse_stretch_strength_ = distance_ - min_distance_;
151 Spring::length (Real f) const
153 Real force = max (f, blocking_force_);
154 Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
158 programming_error ("cruelty to springs");
162 return distance_ + force * inv_k;