2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2007--2009 Joe Neeman <joeneeman@gmail.com>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
26 inverse_stretch_strength_
= 1.0;
27 inverse_compress_strength_
= 1.0;
29 update_blocking_force ();
32 Spring::Spring (Real dist
, Real min_dist
)
36 inverse_stretch_strength_
= 1.0;
37 inverse_compress_strength_
= 1.0;
40 set_min_distance (min_dist
);
41 set_default_strength ();
42 update_blocking_force ();
46 Spring::update_blocking_force ()
48 if (min_distance_
> distance_
)
49 blocking_force_
= (min_distance_
- distance_
) / inverse_stretch_strength_
;
51 blocking_force_
= (min_distance_
- distance_
) / inverse_compress_strength_
;
53 // If the spring is fixed, it's not clear what the natural value
54 // of blocking_force_ would be. -infinity_f works fine for now.
55 if (isnan (blocking_force_
) || blocking_force_
== infinity_f
)
56 blocking_force_
= -infinity_f
;
58 if (blocking_force_
>= 0)
59 inverse_compress_strength_
= 0;
62 /* scale a spring, but in a way that doesn't violate min_distance */
64 Spring::operator*= (Real r
)
66 distance_
= max (min_distance_
, distance_
* r
);
67 inverse_compress_strength_
= max (0.0, distance_
- min_distance_
);
68 inverse_stretch_strength_
*= 0.8;
69 update_blocking_force ();
73 Spring::operator> (Spring
const &other
) const
75 return blocking_force_
> other
.blocking_force_
;
78 /* merge springs, basically by averaging them, but leave a little headroom
79 above the largest minimum distance so that things don't get too cramped */
81 merge_springs (vector
<Spring
> const &springs
)
83 Real avg_distance
= 0;
84 Real min_distance
= 0;
86 Real avg_compress
= 0;
88 for (vsize i
= 0; i
< springs
.size (); i
++)
90 avg_distance
+= springs
[i
].distance ();
91 avg_stretch
+= springs
[i
].inverse_stretch_strength ();
92 avg_compress
+= 1 / springs
[i
].inverse_compress_strength ();
93 min_distance
= max (springs
[i
].min_distance (), min_distance
);
96 avg_stretch
/= springs
.size ();
97 avg_compress
/= springs
.size ();
98 avg_distance
/= springs
.size ();
99 avg_distance
= max (min_distance
+ 0.3, avg_distance
);
101 Spring ret
= Spring (avg_distance
, min_distance
);
102 ret
.set_inverse_stretch_strength (avg_stretch
);
103 ret
.set_inverse_compress_strength (1 / avg_compress
);
109 Spring::set_distance (Real d
)
111 if (d
< 0 || isinf (d
) || isnan (d
))
112 programming_error ("insane spring distance requested, ignoring it");
116 update_blocking_force ();
121 Spring::set_min_distance (Real d
)
123 if (d
< 0 || isinf (d
) || isnan (d
))
124 programming_error ("insane spring min_distance requested, ignoring it");
128 update_blocking_force ();
133 Spring::ensure_min_distance (Real d
)
135 set_min_distance (max (d
, min_distance_
));
139 Spring::set_inverse_stretch_strength (Real f
)
141 if (isinf (f
) || isnan (f
) || f
< 0)
142 programming_error ("insane spring constant");
144 inverse_stretch_strength_
= f
;
146 update_blocking_force ();
150 Spring::set_inverse_compress_strength (Real f
)
152 if (isinf (f
) || isnan (f
) || f
< 0)
153 programming_error ("insane spring constant");
155 inverse_compress_strength_
= f
;
157 update_blocking_force ();
161 Spring::set_blocking_force (Real f
)
163 if (isinf (f
) || isnan (f
))
165 programming_error ("insane blocking force");
169 blocking_force_
= -infinity_f
;
170 min_distance_
= length (f
);
171 distance_
= max (distance_
, min_distance_
);
172 update_blocking_force ();
176 Spring::set_default_strength ()
178 inverse_compress_strength_
= (distance_
>= min_distance_
) ? distance_
- min_distance_
: 0;
179 inverse_stretch_strength_
= distance_
;
180 update_blocking_force ();
184 Spring::length (Real f
) const
186 Real force
= max (f
, blocking_force_
);
187 Real inv_k
= force
< 0.0 ? inverse_compress_strength_
: inverse_stretch_strength_
;
189 if (force
== infinity_f
)
191 programming_error ("cruelty to springs");
195 // There is a corner case here: if min_distance_ is larger than
196 // distance_ but the spring is fixed, then inv_k will be zero
197 // and we need to make sure that we return min_distance_.
198 return max (min_distance_
, distance_
+ force
* inv_k
);