]> git.donarmstrong.com Git - lilypond.git/blob - lily/spring.cc
Merge branch 'master' of ssh+git://gpercival@git.sv.gnu.org/srv/git/lilypond
[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 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
26   set_distance (dist);
27   set_min_distance (min_dist);
28   set_default_strength ();
29   update_blocking_force ();
30 }
31
32 void
33 Spring::update_blocking_force ()
34 {
35   if (distance_ == min_distance_)
36     blocking_force_ = 0.0;
37   else
38     blocking_force_ = (min_distance_ - distance_) / inverse_compress_strength_;
39 }
40
41 /* scale a spring, but in a way that doesn't violate min_distance */
42 void
43 Spring::operator*= (Real r)
44 {
45   distance_ = max (min_distance_, distance_ * r);
46   inverse_compress_strength_ = distance_ - min_distance_;
47   inverse_stretch_strength_ *= 0.8;
48 }
49
50 bool
51 Spring::operator> (Spring const &other) const
52 {
53   return blocking_force_ > other.blocking_force_;
54 }
55
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)
60 {
61   Real avg_distance = 0;
62   Real min_distance = 0;
63   Real avg_stretch = 0;
64
65   for (vsize i = 0; i < springs.size (); i++)
66     {
67       avg_distance += springs[i].distance ();
68       avg_stretch += springs[i].inverse_stretch_strength ();
69       min_distance = max (springs[i].min_distance (), min_distance);
70     }
71
72   avg_stretch /= springs.size ();
73   avg_distance /= springs.size ();
74   avg_distance = max (min_distance + 0.3, avg_distance);
75
76   Spring ret = Spring (avg_distance, min_distance);
77   ret.set_inverse_stretch_strength (avg_stretch);
78
79   return ret;
80 }
81
82 void
83 Spring::set_distance (Real d)
84 {
85   if (d < 0 || isinf (d) || isnan (d))
86     programming_error ("insane spring distance requested, ignoring it");
87   else
88     {
89       min_distance_ = min (min_distance_, d);
90       distance_ = d;
91       update_blocking_force ();
92     }
93 }
94
95 void
96 Spring::set_min_distance (Real d)
97 {
98   if (d < 0 || isinf (d) || isnan (d))
99     programming_error ("insane spring min_distance requested, ignoring it");
100   else
101     {
102       min_distance_ = d;
103       distance_ = max (distance_, min_distance_);
104       update_blocking_force ();
105     }
106 }
107
108 void
109 Spring::set_inverse_stretch_strength (Real f)
110 {
111   if (isinf (f) || isnan (f) || f < 0)
112     programming_error ("insane spring constant");
113   else
114     inverse_stretch_strength_ = f;
115 }
116
117 void
118 Spring::set_inverse_compress_strength (Real f)
119 {
120   if (isinf (f) || isnan (f) || f < 0)
121     programming_error ("insane spring constant");
122   else
123     inverse_compress_strength_ = f;
124
125   update_blocking_force ();
126 }
127
128 void
129 Spring::set_blocking_force (Real f)
130 {
131   if (isinf (f) || isnan (f))
132     {
133       programming_error ("insane blocking force");
134       return;
135     }
136
137   blocking_force_ = -infinity_f;
138   min_distance_ = length (f);
139   distance_ = max (distance_, min_distance_);
140   update_blocking_force ();
141 }
142
143 void
144 Spring::set_default_strength ()
145 {
146   inverse_compress_strength_ = distance_ - min_distance_;
147   inverse_stretch_strength_ = distance_ - min_distance_;
148 }
149
150 Real
151 Spring::length (Real f) const
152 {
153   Real force = max (f, blocking_force_);
154   Real inv_k = force < 0.0 ? inverse_compress_strength_ : inverse_stretch_strength_;
155
156   if (isinf (force))
157     {
158       programming_error ("cruelty to springs");
159       force = 0.0;
160     }
161
162   return distance_ + force * inv_k;
163 }