]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer.cc
release: 1.3.22
[lilypond.git] / lily / simple-spacer.cc
1 /*   
2   simple-spacer.cc -- implement Simple_spacer
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   TODO:
9   - add support for different stretch/shrink constants?
10   - Use force as a minimizing function, and use it to discourage mixes of
11   wide and tight lines.
12   
13 */
14
15 #include <math.h>
16
17 #include "simple-spacer.hh"
18 #include "paper-column.hh"
19 #include "spring.hh"
20 #include "rod.hh"
21 #include "warn.hh"
22 #include "column-x-positions.hh"
23 #include "dimensions.hh"
24
25 Simple_spacer::Simple_spacer ()
26 {
27   force_f_ = 0.;
28   indent_f_ =0.0;
29   default_space_f_ = 20 PT;
30   compression_energy_factor_f_ = 3.0;
31 }
32
33 void
34 Simple_spacer::add_rod (int l, int r, Real dist)
35 {
36   if (isinf (dist) || isnan (dist))
37     {
38       programming_error ("Weird minimum distance. Ignoring");
39       return;
40     }
41   
42   
43   Real c = range_stiffness (l,r);
44   Real d = range_ideal_len (l,r);
45   Real block_stretch = dist - d;
46   
47   Real block_force = c * block_stretch;
48   force_f_ = force_f_ >? block_force;
49
50   for (int i=l; i < r; i++)
51     springs_[i].block_force_f_ = block_force >?
52       springs_[i].block_force_f_ ;
53 }
54
55 Real
56 Simple_spacer::range_ideal_len (int l, int r)   const
57 {
58   Real d =0.;
59   for (int i=l; i < r; i++)
60     d += springs_[i].ideal_f_;
61   return d;
62 }
63
64 Real
65 Simple_spacer::range_stiffness (int l, int r) const
66 {
67   Real den =0.0;
68   for (int i=l; i < r; i++)
69     den += 1 / springs_[i].hooke_f_;
70
71   return 1 / den;
72 }
73
74 Real
75 Simple_spacer::active_blocking_force () const
76 {
77   Real bf = - infinity_f; 
78   for (int i=0; i < springs_.size (); i++)
79     if (springs_[i].active_b_)
80       {
81         bf = bf >? springs_[i].block_force_f_;
82       }
83   return bf;
84 }
85
86 Real
87 Simple_spacer::active_springs_stiffness () const
88 {
89   Real den = 0.0;
90   for (int i=0; i < springs_.size (); i++)
91     if (springs_[i].active_b_)
92       {
93         den += 1 / springs_[i].hooke_f_;
94       }
95   return 1/den;
96 }
97
98 void
99 Simple_spacer::set_active_states ()
100 {
101   // safe, since
102   // force is only copied.
103   for (int i=0 ; i <springs_.size (); i++)
104     if (springs_[i].block_force_f_ >= force_f_) 
105       springs_[i].active_b_ = false;
106 }   
107
108 Real
109 Simple_spacer::configuration_length () const
110 {
111   Real l =0.;
112   for (int i=0; i < springs_.size (); i++)
113     l += springs_[i].length (force_f_);
114
115   return l;
116 }
117
118 Real
119 Spring_description::length (Real f) const
120 {
121   if (!active_b_)
122     f = block_force_f_;
123   return ideal_f_ + f / hooke_f_ ;
124 }
125
126 bool
127 Simple_spacer::active_b () const
128 {
129    for (int i=0; i < springs_.size (); i++)
130     if (springs_[i].active_b_)
131       return true;
132    return false;
133 }
134
135 void
136 Simple_spacer::my_solve_linelen ()
137 {
138   while (active_b ())
139     {
140       force_f_ = active_blocking_force ();
141       Real conf = configuration_length ();
142
143       if (conf < line_len_f_)
144         {
145           force_f_ +=  (line_len_f_  - conf) * active_springs_stiffness ();
146           break;
147         }
148       else
149         set_active_states ();
150     }
151 }
152
153
154 void
155 Simple_spacer::my_solve_natural_len ()
156 {
157   while (active_b ())
158     {
159       force_f_ = active_blocking_force () >? 0.0;
160
161       if (force_f_ < 1e-8) // ugh.,
162         break;
163       
164       set_active_states ();
165     }
166 }
167
168 void
169 Simple_spacer::add_columns (Link_array<Paper_column> cols)
170 {
171   for (int i=0; i < cols.size () - 1; i++)
172     {
173       Paper_column * c = cols [i];
174       Column_spring *to_next = 0;
175       for (int j =0; !to_next && j < c->spring_arr_drul_[RIGHT].size( ); j++)
176         {
177           Column_spring &sp = c->spring_arr_drul_[RIGHT] [j];
178           if (sp.other_l_ != cols[i+1])
179             continue;
180
181           to_next = &sp;
182         }
183
184       Spring_description desc;
185       if (to_next)
186         {
187           desc.hooke_f_ = to_next->strength_f_;
188           desc.ideal_f_ = to_next->distance_f_;
189         }
190       else
191         {
192           desc.hooke_f_ = 1.0;
193           desc.ideal_f_ = default_space_f_;
194         }
195
196       if (!desc.sane_b ())
197         {
198           programming_error ("Insane spring.");
199           continue;
200         }
201       
202       desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
203       springs_.push  (desc);
204     }
205   
206   for (int i=0; i < cols.size () - 1; i++)
207     {
208       Array<Column_rod> * rods = &cols [i]->minimal_dists_arr_drul_[RIGHT];
209       for (int j =0; j < rods->size( ); j++)
210         {
211           int oi = cols.find_i (rods->elem (j).other_l_ );
212           if (oi >= 0)
213             {
214               add_rod (i, oi, rods->elem (j).distance_f_);
215             }
216         }
217     }
218
219   if (line_len_f_ < 0)
220     my_solve_natural_len ();
221   else
222     my_solve_linelen ();
223 }
224
225 void
226 Simple_spacer::solve (Column_x_positions *positions) const
227 {
228   positions->energy_f_  = energy_f ();  // abs (force_f_);
229   positions->force_f_ = force_f_;
230   
231   positions->config_.push (indent_f_);
232   for (int i=0; i <springs_.size (); i++)
233     {
234       positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
235     }
236
237   positions->satisfies_constraints_b_ =  (line_len_f_ < 0) || active_b ();
238 }
239
240
241
242 Spring_description::Spring_description( )
243 {
244   ideal_f_ =0.0;
245   hooke_f_ =0.0;
246   active_b_ = true;
247   block_force_f_ = 0.0;
248 }
249
250 Real
251 Spring_description::energy_f (Real force) const
252 {
253   Real stretch = (force >? block_force_f_) / hooke_f_;
254   Real e = 0.5 * stretch * stretch * hooke_f_;
255   return e;
256 }
257
258 bool
259 Spring_description::sane_b () const
260 {
261   return (hooke_f_ > 0) &&  ! isinf (ideal_f_) && !isnan (ideal_f_);
262 }
263
264
265 Real
266 Simple_spacer::energy_f () const
267 {
268   Real e =0.;
269
270   for (int i=0; i <springs_.size (); i++)
271     {
272       e += springs_[i].energy_f (force_f_);
273     }
274
275   if (force_f_ < 0)
276     e *= compression_energy_factor_f_;
277
278   return e;
279 }