]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer.cc
patch::: 1.5.11.jcn2
[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--2001 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   force_f_ = 0.;
28   indent_f_ =0.0;
29   default_space_f_ = 20 PT;
30 }
31
32 void
33 Simple_spacer::add_rod (int l, int r, Real dist)
34 {
35   if (isinf (dist) || isnan (dist))
36     {
37       programming_error ("Weird minimum distance. Ignoring");
38       return;
39     }
40   
41   
42   Real c = range_stiffness (l,r);
43   Real d = range_ideal_len (l,r);
44   Real block_stretch = dist - d;
45   
46   Real block_force = c * block_stretch;
47   force_f_ = force_f_ >? block_force;
48
49   for (int i=l; i < r; i++)
50     springs_[i].block_force_f_ = block_force >?
51       springs_[i].block_force_f_ ;
52 }
53
54 Real
55 Simple_spacer::range_ideal_len (int l, int r)   const
56 {
57   Real d =0.;
58   for (int i=l; i < r; i++)
59     d += springs_[i].ideal_f_;
60   return d;
61 }
62
63 Real
64 Simple_spacer::range_stiffness (int l, int r) const
65 {
66   Real den =0.0;
67   for (int i=l; i < r; i++)
68     den += 1 / springs_[i].hooke_f_;
69
70   return 1 / den;
71 }
72
73 Real
74 Simple_spacer::active_blocking_force () const
75 {
76   Real bf = - infinity_f; 
77   for (int i=0; i < springs_.size (); i++)
78     if (springs_[i].active_b_)
79       {
80         bf = bf >? springs_[i].block_force_f_;
81       }
82   return bf;
83 }
84
85 Real
86 Simple_spacer::active_springs_stiffness () const
87 {
88   Real den = 0.0;
89   for (int i=0; i < springs_.size (); i++)
90     if (springs_[i].active_b_)
91       {
92         den += 1 / springs_[i].hooke_f_;
93       }
94   return 1/den;
95 }
96
97 void
98 Simple_spacer::set_active_states ()
99 {
100   // safe, since
101   // force is only copied.
102   for (int i=0 ; i <springs_.size (); i++)
103     if (springs_[i].block_force_f_ >= force_f_) 
104       springs_[i].active_b_ = false;
105 }   
106
107 Real
108 Simple_spacer::configuration_length () const
109 {
110   Real l =0.;
111   for (int i=0; i < springs_.size (); i++)
112     l += springs_[i].length (force_f_);
113
114   return l;
115 }
116
117 Real
118 Spring_description::length (Real f) const
119 {
120   if (!active_b_)
121     f = block_force_f_;
122   return ideal_f_ + f / hooke_f_ ;
123 }
124
125 bool
126 Simple_spacer::active_b () const
127 {
128    for (int i=0; i < springs_.size (); i++)
129     if (springs_[i].active_b_)
130       return true;
131    return false;
132 }
133
134 void
135 Simple_spacer::my_solve_linelen ()
136 {
137   while (active_b ())
138     {
139       force_f_ = active_blocking_force ();
140       Real conf = configuration_length ();
141
142       if (conf < line_len_f_)
143         {
144           force_f_ += (line_len_f_  - conf) * active_springs_stiffness ();
145           break;
146         }
147       else
148         set_active_states ();
149     }
150 }
151
152
153 void
154 Simple_spacer::my_solve_natural_len ()
155 {
156   while (active_b ())
157     {
158       force_f_ = active_blocking_force () >? 0.0;
159
160       if (force_f_ < 1e-8) // ugh.,
161         break;
162       
163       set_active_states ();
164     }
165 }
166
167 void
168 Simple_spacer::add_columns (Link_array<Grob> cols)
169 {
170   for (int i =  cols.size (); i--;)
171     if (gh_pair_p (cols[i]->get_grob_property ("between-cols")))
172       {
173         loose_cols_.push (cols[i]);
174         cols.del (i);
175       }
176   
177   spaced_cols_ = cols;
178   for (int i=0; i < cols.size () - 1; i++)
179     {
180       SCM spring_params = SCM_EOL;
181       for (SCM s = cols[i]->get_grob_property ("ideal-distances");
182            !gh_pair_p (spring_params) && gh_pair_p (s);
183            s = ly_cdr (s))
184         {
185           Grob *other = unsmob_grob (gh_caar (s));
186           if (other != cols[i+1])
187             continue;
188
189           spring_params = gh_cdar (s);
190         }
191
192       Spring_description desc;
193       if (gh_pair_p (spring_params))
194         {
195           desc.ideal_f_ = gh_scm2double (ly_car (spring_params));
196           desc.hooke_f_ = gh_scm2double (ly_cdr (spring_params));
197         }
198       else
199         {
200           programming_error (_f("No spring between column %d and next one",
201                                 Paper_column::rank_i (cols[i])
202                                 ));
203           desc.hooke_f_ = 1.0;
204           desc.ideal_f_ = default_space_f_;
205         }
206
207       if (!desc.sane_b ())
208         {
209           programming_error ("Insane spring found. Setting to unit spring.");
210
211           cout << "columns " << Paper_column::rank_i (cols[i])
212                << " " << Paper_column::rank_i (cols[i+1]) << endl;
213           desc.hooke_f_ = 1.0;
214           desc.ideal_f_ = 1.0;
215         }
216       
217       desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
218       springs_.push (desc);
219     }
220   
221   for (int i=0; i < cols.size () - 1; i++)
222     {
223       for (SCM s = Spaceable_grob::get_minimum_distances (cols[i]);
224            gh_pair_p (s); s = ly_cdr (s))
225         {
226           Grob * other = unsmob_grob (gh_caar (s));
227           int oi = cols.find_i (other);
228           if (oi >= 0)
229             {
230               add_rod (i, oi, gh_scm2double (gh_cdar (s)));
231             }
232         }
233     }
234
235   /*
236     TODO: should support natural length on only the last line.
237    */
238   if (line_len_f_ < 0)
239     my_solve_natural_len ();
240   else
241     my_solve_linelen ();
242 }
243
244 void
245 Simple_spacer::solve (Column_x_positions *positions) const
246 {
247   positions->force_f_ = force_f_;
248   
249   positions->config_.push (indent_f_);
250   for (int i=0; i <springs_.size (); i++)
251     {
252       positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
253     }
254   positions->cols_ = spaced_cols_;
255   positions->loose_cols_ = loose_cols_;
256   
257   positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
258 }
259
260
261
262 Spring_description::Spring_description ()
263 {
264   ideal_f_ =0.0;
265   hooke_f_ =0.0;
266   active_b_ = true;
267   block_force_f_ = 0.0;
268 }
269
270
271 bool
272 Spring_description::sane_b () const
273 {
274   return (hooke_f_ > 0) &&  ! isinf (ideal_f_) && !isnan (ideal_f_);
275 }
276
277