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