]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer.cc
release: 1.5.29
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   TODO:
9   - add support for different stretch/shrink constants?
10   
11 */
12
13 #include <math.h>
14 #include <libc-extension.hh>    // isinf
15
16 #include "simple-spacer.hh"
17 #include "paper-column.hh"
18 #include "spring.hh"
19 #include "rod.hh"
20 #include "warn.hh"
21 #include "column-x-positions.hh"
22 #include "spaceable-grob.hh"
23 #include "dimensions.hh"
24
25 Simple_spacer::Simple_spacer ()
26 {
27   active_count_ = 0;
28   force_f_ = 0.;
29   indent_f_ =0.0;
30   default_space_f_ = 20 PT;
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].active_b_
105         && springs_[i].block_force_f_ >= force_f_)
106       {
107         springs_[i].active_b_ = false;
108         active_count_ --; 
109       }
110 }   
111
112 Real
113 Simple_spacer::configuration_length () const
114 {
115   Real l =0.;
116   for (int i=0; i < springs_.size (); i++)
117     l += springs_[i].length (force_f_);
118
119   return l;
120 }
121
122 Real
123 Spring_description::length (Real f) const
124 {
125   if (!active_b_)
126     f = block_force_f_;
127   return ideal_f_ + f / hooke_f_ ;
128 }
129
130 bool
131 Simple_spacer::active_b () const
132 {
133   return active_count_; 
134 }
135
136 void
137 Simple_spacer::my_solve_linelen ()
138 {
139   while (active_b ())
140     {
141       force_f_ = active_blocking_force ();
142       Real conf = configuration_length ();
143
144       if (conf < line_len_f_)
145         {
146           force_f_ += (line_len_f_  - conf) * active_springs_stiffness ();
147           break;
148         }
149       else
150         set_active_states ();
151     }
152 }
153
154
155 void
156 Simple_spacer::my_solve_natural_len ()
157 {
158   while (active_b ())
159     {
160       force_f_ = active_blocking_force () >? 0.0;
161
162       if (force_f_ < 1e-8) // ugh.,
163         break;
164       
165       set_active_states ();
166     }
167 }
168
169 void
170 Simple_spacer::add_columns (Link_array<Grob> cols)
171 {
172   for (int i =  cols.size (); i--;)
173     if (gh_pair_p (cols[i]->get_grob_property ("between-cols")))
174       {
175         loose_cols_.push (cols[i]);
176         cols.del (i);
177       }
178   
179   spaced_cols_ = cols;
180   for (int i=0; i < cols.size () - 1; i++)
181     {
182       SCM spring_params = SCM_EOL;
183       for (SCM s = cols[i]->get_grob_property ("ideal-distances");
184            !gh_pair_p (spring_params) && gh_pair_p (s);
185            s = ly_cdr (s))
186         {
187           Grob *other = unsmob_grob (ly_caar (s));
188           if (other != cols[i+1])
189             continue;
190
191           spring_params = ly_cdar (s);
192         }
193
194       Spring_description desc;
195       if (gh_pair_p (spring_params))
196         {
197           desc.ideal_f_ = gh_scm2double (ly_car (spring_params));
198           desc.hooke_f_ = gh_scm2double (ly_cdr (spring_params));
199         }
200       else
201         {
202           programming_error (_f("No spring between column %d and next one",
203                                 Paper_column::rank_i (cols[i])
204                                 ));
205           desc.hooke_f_ = 1.0;
206           desc.ideal_f_ = default_space_f_;
207         }
208
209       if (!desc.sane_b ())
210         {
211           programming_error ("Insane spring found. Setting to unit spring.");
212
213           cout << "columns " << Paper_column::rank_i (cols[i])
214                << " " << Paper_column::rank_i (cols[i+1]) << endl;
215           desc.hooke_f_ = 1.0;
216           desc.ideal_f_ = 1.0;
217         }
218       
219       desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
220       springs_.push (desc);
221       active_count_ ++;
222     }
223   
224   for (int i=0; i < cols.size () - 1; i++)
225     {
226       for (SCM s = Spaceable_grob::get_minimum_distances (cols[i]);
227            gh_pair_p (s); s = ly_cdr (s))
228         {
229           Grob * other = unsmob_grob (ly_caar (s));
230           int oi = cols.find_i (other);
231           if (oi >= 0)
232             {
233               add_rod (i, oi, gh_scm2double (ly_cdar (s)));
234             }
235         }
236     }
237
238   /*
239     TODO: should support natural length on only the last line.
240    */
241   if (line_len_f_ < 0)
242     my_solve_natural_len ();
243   else
244     my_solve_linelen ();
245 }
246
247 void
248 Simple_spacer::solve (Column_x_positions *positions) const
249 {
250   positions->force_f_ = force_f_;
251   
252   positions->config_.push (indent_f_);
253   for (int i=0; i <springs_.size (); i++)
254     {
255       positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
256     }
257   positions->cols_ = spaced_cols_;
258   positions->loose_cols_ = loose_cols_;
259   
260   positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
261
262
263   /*
264     Check if breaking constraints are met.
265    */
266   bool break_satisfy = true;
267   int sz =  positions->cols_.size ();
268   for (int i = sz; i--; )
269     {
270       SCM p = positions->cols_[i]->get_grob_property( "penalty");
271       if (gh_number_p (p))
272         {
273           if (gh_scm2double (p) < -9999)
274             break_satisfy = break_satisfy && (i == 0 || i == sz -1);
275           if (gh_scm2double (p) > 9999)
276             break_satisfy = break_satisfy && !(i == 0 || i == sz -1);
277         }
278       
279     }
280
281   positions->satisfies_constraints_b_ =
282     positions->satisfies_constraints_b_ && break_satisfy;
283 }
284
285
286
287
288
289
290 Spring_description::Spring_description ()
291 {
292   ideal_f_ =0.0;
293   hooke_f_ =0.0;
294   active_b_ = true;
295   block_force_f_ = 0.0;
296 }
297
298
299 bool
300 Spring_description::sane_b () const
301 {
302   return (hooke_f_ > 0) &&  ! isinf (ideal_f_) && !isnan (ideal_f_);
303 }
304
305