Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / spring.cc
blob264c8abfa159910cc251ef26028571e8f86574c9
1 /*
2 spring.cc -- declare Spring
4 source file of the GNU LilyPond music typesetter
6 (c) 2007--2009 Joe Neeman <joeneeman@gmail.com>
7 */
9 #include "spring.hh"
11 Spring::Spring ()
13 distance_ = 1.0;
14 min_distance_ = 1.0;
15 inverse_stretch_strength_ = 1.0;
16 inverse_compress_strength_ = 1.0;
18 update_blocking_force ();
21 Spring::Spring (Real dist, Real min_dist)
23 distance_ = 1.0;
24 min_distance_ = 1.0;
25 inverse_stretch_strength_ = 1.0;
26 inverse_compress_strength_ = 1.0;
28 set_distance (dist);
29 set_min_distance (min_dist);
30 set_default_strength ();
31 update_blocking_force ();
34 void
35 Spring::update_blocking_force ()
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_;
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;
47 if (blocking_force_ >= 0)
48 inverse_compress_strength_ = 0;
51 /* scale a spring, but in a way that doesn't violate min_distance */
52 void
53 Spring::operator*= (Real r)
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 ();
61 bool
62 Spring::operator> (Spring const &other) const
64 return blocking_force_ > other.blocking_force_;
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)
72 Real avg_distance = 0;
73 Real min_distance = 0;
74 Real avg_stretch = 0;
75 Real avg_compress = 0;
77 for (vsize i = 0; i < springs.size (); i++)
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);
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);
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);
94 return ret;
97 void
98 Spring::set_distance (Real d)
100 if (d < 0 || isinf (d) || isnan (d))
101 programming_error ("insane spring distance requested, ignoring it");
102 else
104 distance_ = d;
105 update_blocking_force ();
109 void
110 Spring::set_min_distance (Real d)
112 if (d < 0 || isinf (d) || isnan (d))
113 programming_error ("insane spring min_distance requested, ignoring it");
114 else
116 min_distance_ = d;
117 update_blocking_force ();
121 void
122 Spring::ensure_min_distance (Real d)
124 set_min_distance (max (d, min_distance_));
127 void
128 Spring::set_inverse_stretch_strength (Real f)
130 if (isinf (f) || isnan (f) || f < 0)
131 programming_error ("insane spring constant");
132 else
133 inverse_stretch_strength_ = f;
135 update_blocking_force ();
138 void
139 Spring::set_inverse_compress_strength (Real f)
141 if (isinf (f) || isnan (f) || f < 0)
142 programming_error ("insane spring constant");
143 else
144 inverse_compress_strength_ = f;
146 update_blocking_force ();
149 void
150 Spring::set_blocking_force (Real f)
152 if (isinf (f) || isnan (f))
154 programming_error ("insane blocking force");
155 return;
158 blocking_force_ = -infinity_f;
159 min_distance_ = length (f);
160 distance_ = max (distance_, min_distance_);
161 update_blocking_force ();
164 void
165 Spring::set_default_strength ()
167 inverse_compress_strength_ = (distance_ >= min_distance_) ? distance_ - min_distance_ : 0;
168 inverse_stretch_strength_ = distance_;
169 update_blocking_force ();
172 Real
173 Spring::length (Real f) const
175 Real force = max (f, blocking_force_);
176 Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
178 if (force == infinity_f)
180 programming_error ("cruelty to springs");
181 force = 0.0;
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);