]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer.cc
release: 1.3.53
[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--2000 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
15 #include "simple-spacer.hh"
16 #include "paper-column.hh"
17 #include "spring.hh"
18 #include "rod.hh"
19 #include "warn.hh"
20 #include "column-x-positions.hh"
21 #include "dimensions.hh"
22
23 Simple_spacer::Simple_spacer ()
24 {
25   force_f_ = 0.;
26   indent_f_ =0.0;
27   default_space_f_ = 20 PT;
28 }
29
30 void
31 Simple_spacer::add_rod (int l, int r, Real dist)
32 {
33   if (isinf (dist) || isnan (dist))
34     {
35       programming_error ("Weird minimum distance. Ignoring");
36       return;
37     }
38   
39   
40   Real c = range_stiffness (l,r);
41   Real d = range_ideal_len (l,r);
42   Real block_stretch = dist - d;
43   
44   Real block_force = c * block_stretch;
45   force_f_ = force_f_ >? block_force;
46
47   for (int i=l; i < r; i++)
48     springs_[i].block_force_f_ = block_force >?
49       springs_[i].block_force_f_ ;
50 }
51
52 Real
53 Simple_spacer::range_ideal_len (int l, int r)   const
54 {
55   Real d =0.;
56   for (int i=l; i < r; i++)
57     d += springs_[i].ideal_f_;
58   return d;
59 }
60
61 Real
62 Simple_spacer::range_stiffness (int l, int r) const
63 {
64   Real den =0.0;
65   for (int i=l; i < r; i++)
66     den += 1 / springs_[i].hooke_f_;
67
68   return 1 / den;
69 }
70
71 Real
72 Simple_spacer::active_blocking_force () const
73 {
74   Real bf = - infinity_f; 
75   for (int i=0; i < springs_.size (); i++)
76     if (springs_[i].active_b_)
77       {
78         bf = bf >? springs_[i].block_force_f_;
79       }
80   return bf;
81 }
82
83 Real
84 Simple_spacer::active_springs_stiffness () const
85 {
86   Real den = 0.0;
87   for (int i=0; i < springs_.size (); i++)
88     if (springs_[i].active_b_)
89       {
90         den += 1 / springs_[i].hooke_f_;
91       }
92   return 1/den;
93 }
94
95 void
96 Simple_spacer::set_active_states ()
97 {
98   // safe, since
99   // force is only copied.
100   for (int i=0 ; i <springs_.size (); i++)
101     if (springs_[i].block_force_f_ >= force_f_) 
102       springs_[i].active_b_ = false;
103 }   
104
105 Real
106 Simple_spacer::configuration_length () const
107 {
108   Real l =0.;
109   for (int i=0; i < springs_.size (); i++)
110     l += springs_[i].length (force_f_);
111
112   return l;
113 }
114
115 Real
116 Spring_description::length (Real f) const
117 {
118   if (!active_b_)
119     f = block_force_f_;
120   return ideal_f_ + f / hooke_f_ ;
121 }
122
123 bool
124 Simple_spacer::active_b () const
125 {
126    for (int i=0; i < springs_.size (); i++)
127     if (springs_[i].active_b_)
128       return true;
129    return false;
130 }
131
132 void
133 Simple_spacer::my_solve_linelen ()
134 {
135   while (active_b ())
136     {
137       force_f_ = active_blocking_force ();
138       Real conf = configuration_length ();
139
140       if (conf < line_len_f_)
141         {
142           force_f_ +=  (line_len_f_  - conf) * active_springs_stiffness ();
143           break;
144         }
145       else
146         set_active_states ();
147     }
148 }
149
150
151 void
152 Simple_spacer::my_solve_natural_len ()
153 {
154   while (active_b ())
155     {
156       force_f_ = active_blocking_force () >? 0.0;
157
158       if (force_f_ < 1e-8) // ugh.,
159         break;
160       
161       set_active_states ();
162     }
163 }
164
165 void
166 Simple_spacer::add_columns (Link_array<Paper_column> cols)
167 {
168   for (int i=0; i < cols.size () - 1; i++)
169     {
170       Paper_column * c = cols [i];
171       Column_spring *to_next = 0;
172       for (int j =0; !to_next && j < c->springs_.size( ); j++)
173         {
174           Column_spring &sp = c->springs_ [j];
175           if (sp.other_l_ != cols[i+1])
176             continue;
177
178           to_next = &sp;
179         }
180
181       Spring_description desc;
182       if (to_next)
183         {
184           desc.hooke_f_ = to_next->strength_f_;
185           desc.ideal_f_ = to_next->distance_f_;
186         }
187       else
188         {
189           desc.hooke_f_ = 1.0;
190           desc.ideal_f_ = default_space_f_;
191         }
192
193       if (!desc.sane_b ())
194         {
195           programming_error ("Insane spring found. Setting to unit spring.");
196           desc.hooke_f_ = 1.0;
197           desc.ideal_f_ = 1.0;
198         }
199       
200       desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
201       springs_.push  (desc);
202     }
203   
204   for (int i=0; i < cols.size () - 1; i++)
205     {
206       Array<Column_rod> * rods = &cols [i]->minimal_dists_;
207       for (int j =0; j < rods->size( ); j++)
208         {
209           int oi = cols.find_i (rods->elem (j).other_l_ );
210           if (oi >= 0)
211             {
212               add_rod (i, oi, rods->elem (j).distance_f_);
213             }
214         }
215     }
216
217   /*
218     TODO: should support natural length on only the last line.
219    */
220   if (line_len_f_ < 0)
221     my_solve_natural_len ();
222   else
223     my_solve_linelen ();
224 }
225
226 void
227 Simple_spacer::solve (Column_x_positions *positions) const
228 {
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
251 bool
252 Spring_description::sane_b () const
253 {
254   return (hooke_f_ > 0) &&  ! isinf (ideal_f_) && !isnan (ideal_f_);
255 }
256
257