Format and improve documentation of internal grob properties.
[lilypond.git] / lily / spring.cc
blobc9f3afbe42a3ef4c71041b2cde980245fc283070
1 /*
2 spring.cc -- declare Spring
4 source file of the GNU LilyPond music typesetter
6 (c) 2007 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;
26 set_distance (dist);
27 set_min_distance (min_dist);
28 set_default_strength ();
29 update_blocking_force ();
32 void
33 Spring::update_blocking_force ()
35 if (distance_ == min_distance_)
36 blocking_force_ = 0.0;
37 else
38 blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
41 /* scale a spring, but in a way that doesn't violate min_distance */
42 void
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;
50 bool
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 */
58 Spring
59 merge_springs (vector<Spring> const &springs)
61 Real avg_distance = 0;
62 Real min_distance = 0;
63 Real avg_stretch = 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);
79 return ret;
82 void
83 Spring::set_distance (Real d)
85 if (d < 0 || isinf (d) || isnan (d))
86 programming_error ("insane spring distance requested, ignoring it");
87 else
89 min_distance_ = min (min_distance_, d);
90 distance_ = d;
91 update_blocking_force ();
95 void
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");
100 else
102 min_distance_ = d;
103 distance_ = max (distance_, min_distance_);
104 update_blocking_force ();
108 void
109 Spring::set_inverse_stretch_strength (Real f)
111 if (isinf (f) || isnan (f) || f < 0)
112 programming_error ("insane spring constant");
113 else
114 inverse_stretch_strength_ = f;
117 void
118 Spring::set_inverse_compress_strength (Real f)
120 if (isinf (f) || isnan (f) || f < 0)
121 programming_error ("insane spring constant");
122 else
123 inverse_compress_strength_ = f;
125 update_blocking_force ();
128 void
129 Spring::set_blocking_force (Real f)
131 if (isinf (f) || isnan (f))
133 programming_error ("insane blocking force");
134 return;
137 blocking_force_ = -infinity_f;
138 min_distance_ = length (f);
139 distance_ = max (distance_, min_distance_);
140 update_blocking_force ();
143 void
144 Spring::set_default_strength ()
146 inverse_compress_strength_ = distance_ - min_distance_;
147 inverse_stretch_strength_ = distance_ - min_distance_;
150 Real
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_;
156 if (isinf (force))
158 programming_error ("cruelty to springs");
159 force = 0.0;
162 return distance_ + force * inv_k;