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;
64 Real avg_compress = 0;
66 for (vsize i = 0; i < springs.size (); i++)
68 avg_distance += springs[i].distance ();
69 avg_stretch += springs[i].inverse_stretch_strength ();
70 avg_compress += 1 / springs[i].inverse_compress_strength ();
71 min_distance = max (springs[i].min_distance (), min_distance);
74 avg_stretch /= springs.size ();
75 avg_compress /= springs.size ();
76 avg_distance /= springs.size ();
77 avg_distance = max (min_distance + 0.3, avg_distance);
79 Spring ret = Spring (avg_distance, min_distance);
80 ret.set_inverse_stretch_strength (avg_stretch);
81 ret.set_inverse_compress_strength (1 / avg_compress);
87 Spring::set_distance (Real d)
89 if (d < 0 || isinf (d) || isnan (d))
90 programming_error ("insane spring distance requested, ignoring it");
93 min_distance_ = min (min_distance_, d);
95 update_blocking_force ();
100 Spring::set_min_distance (Real d)
102 if (d < 0 || isinf (d) || isnan (d))
103 programming_error ("insane spring min_distance requested, ignoring it");
107 distance_ = max (distance_, min_distance_);
108 update_blocking_force ();
113 Spring::set_inverse_stretch_strength (Real f)
115 if (isinf (f) || isnan (f) || f < 0)
116 programming_error ("insane spring constant");
118 inverse_stretch_strength_ = f;
122 Spring::set_inverse_compress_strength (Real f)
124 if (isinf (f) || isnan (f) || f < 0)
125 programming_error ("insane spring constant");
127 inverse_compress_strength_ = f;
129 update_blocking_force ();
133 Spring::set_blocking_force (Real f)
135 if (isinf (f) || isnan (f))
137 programming_error ("insane blocking force");
141 blocking_force_ = -infinity_f;
142 min_distance_ = length (f);
143 distance_ = max (distance_, min_distance_);
144 update_blocking_force ();
148 Spring::set_default_strength ()
150 inverse_compress_strength_ = distance_ - min_distance_;
151 inverse_stretch_strength_ = distance_ - min_distance_;
155 Spring::length (Real f) const
157 Real force = max (f, blocking_force_);
158 Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
162 programming_error ("cruelty to springs");
166 return distance_ + force * inv_k;