]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer.cc
release: 1.3.71
[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 "spaceable-element.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<Score_element> cols)
169 {
170   for (int i=0; i < cols.size () - 1; i++)
171     {
172       SCM spring_params = SCM_UNDEFINED;
173       for (SCM s = Spaceable_element::get_ideal_distances (cols[i]);
174            spring_params == SCM_UNDEFINED && gh_pair_p (s);
175            s = gh_cdr (s))
176         {
177           Score_element *other = unsmob_element (gh_caar (s));
178           if (other != cols[i+1])
179             continue;
180
181           spring_params = gh_cdar (s);
182         }
183
184       Spring_description desc;
185       if (spring_params != SCM_UNDEFINED)
186         {
187           desc.ideal_f_ = gh_scm2double (gh_car (spring_params));
188           desc.hooke_f_ = gh_scm2double (gh_cdr (spring_params));
189         }
190       else
191         {
192           programming_error ("No spring between adjacent columns");
193           desc.hooke_f_ = 1.0;
194           desc.ideal_f_ = default_space_f_;
195         }
196
197       if (!desc.sane_b ())
198         {
199           programming_error ("Insane spring found. Setting to unit spring.");
200           desc.hooke_f_ = 1.0;
201           desc.ideal_f_ = 1.0;
202         }
203       
204       desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
205       springs_.push  (desc);
206     }
207   
208   for (int i=0; i < cols.size () - 1; i++)
209     {
210       for (SCM s = Spaceable_element::get_minimum_distances (cols[i]);
211            gh_pair_p (s); s = gh_cdr (s))
212         {
213           Score_element * other = unsmob_element (gh_caar (s));
214           int oi = cols.find_i (other);
215           if (oi >= 0)
216             {
217               add_rod (i, oi, gh_scm2double (gh_cdar (s)));
218             }
219         }
220     }
221
222   /*
223     TODO: should support natural length on only the last line.
224    */
225   if (line_len_f_ < 0)
226     my_solve_natural_len ();
227   else
228     my_solve_linelen ();
229 }
230
231 void
232 Simple_spacer::solve (Column_x_positions *positions) const
233 {
234   positions->force_f_ = force_f_;
235   
236   positions->config_.push (indent_f_);
237   for (int i=0; i <springs_.size (); i++)
238     {
239       positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
240     }
241
242   positions->satisfies_constraints_b_ =  (line_len_f_ < 0) || active_b ();
243 }
244
245
246
247 Spring_description::Spring_description( )
248 {
249   ideal_f_ =0.0;
250   hooke_f_ =0.0;
251   active_b_ = true;
252   block_force_f_ = 0.0;
253 }
254
255
256 bool
257 Spring_description::sane_b () const
258 {
259   return (hooke_f_ > 0) &&  ! isinf (ideal_f_) && !isnan (ideal_f_);
260 }
261
262