lilypond-1.3.28
[lilypond.git] / lily / simple-spacer.cc
blob4910fd27de868d60814da505a9dc28a9c47e1c3a
1 /*
2 simple-spacer.cc -- implement Simple_spacer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 TODO:
9 - add support for different stretch/shrink constants?
10 - Use force as a minimizing function, and use it to discourage mixes of
11 wide and tight lines.
15 #include <math.h>
17 #include "simple-spacer.hh"
18 #include "paper-column.hh"
19 #include "spring.hh"
20 #include "rod.hh"
21 #include "warn.hh"
22 #include "column-x-positions.hh"
23 #include "dimensions.hh"
25 Simple_spacer::Simple_spacer ()
27 force_f_ = 0.;
28 indent_f_ =0.0;
29 default_space_f_ = 20 PT;
30 compression_energy_factor_f_ = 3.0;
33 void
34 Simple_spacer::add_rod (int l, int r, Real dist)
36 if (isinf (dist) || isnan (dist))
38 programming_error ("Weird minimum distance. Ignoring");
39 return;
43 Real c = range_stiffness (l,r);
44 Real d = range_ideal_len (l,r);
45 Real block_stretch = dist - d;
47 Real block_force = c * block_stretch;
48 force_f_ = force_f_ >? block_force;
50 for (int i=l; i < r; i++)
51 springs_[i].block_force_f_ = block_force >?
52 springs_[i].block_force_f_ ;
55 Real
56 Simple_spacer::range_ideal_len (int l, int r) const
58 Real d =0.;
59 for (int i=l; i < r; i++)
60 d += springs_[i].ideal_f_;
61 return d;
64 Real
65 Simple_spacer::range_stiffness (int l, int r) const
67 Real den =0.0;
68 for (int i=l; i < r; i++)
69 den += 1 / springs_[i].hooke_f_;
71 return 1 / den;
74 Real
75 Simple_spacer::active_blocking_force () const
77 Real bf = - infinity_f;
78 for (int i=0; i < springs_.size (); i++)
79 if (springs_[i].active_b_)
81 bf = bf >? springs_[i].block_force_f_;
83 return bf;
86 Real
87 Simple_spacer::active_springs_stiffness () const
89 Real den = 0.0;
90 for (int i=0; i < springs_.size (); i++)
91 if (springs_[i].active_b_)
93 den += 1 / springs_[i].hooke_f_;
95 return 1/den;
98 void
99 Simple_spacer::set_active_states ()
101 // safe, since
102 // force is only copied.
103 for (int i=0 ; i <springs_.size (); i++)
104 if (springs_[i].block_force_f_ >= force_f_)
105 springs_[i].active_b_ = false;
108 Real
109 Simple_spacer::configuration_length () const
111 Real l =0.;
112 for (int i=0; i < springs_.size (); i++)
113 l += springs_[i].length (force_f_);
115 return l;
118 Real
119 Spring_description::length (Real f) const
121 if (!active_b_)
122 f = block_force_f_;
123 return ideal_f_ + f / hooke_f_ ;
126 bool
127 Simple_spacer::active_b () const
129 for (int i=0; i < springs_.size (); i++)
130 if (springs_[i].active_b_)
131 return true;
132 return false;
135 void
136 Simple_spacer::my_solve_linelen ()
138 while (active_b ())
140 force_f_ = active_blocking_force ();
141 Real conf = configuration_length ();
143 if (conf < line_len_f_)
145 force_f_ += (line_len_f_ - conf) * active_springs_stiffness ();
146 break;
148 else
149 set_active_states ();
154 void
155 Simple_spacer::my_solve_natural_len ()
157 while (active_b ())
159 force_f_ = active_blocking_force () >? 0.0;
161 if (force_f_ < 1e-8) // ugh.,
162 break;
164 set_active_states ();
168 void
169 Simple_spacer::add_columns (Link_array<Paper_column> cols)
171 for (int i=0; i < cols.size () - 1; i++)
173 Paper_column * c = cols [i];
174 Column_spring *to_next = 0;
175 for (int j =0; !to_next && j < c->spring_arr_drul_[RIGHT].size( ); j++)
177 Column_spring &sp = c->spring_arr_drul_[RIGHT] [j];
178 if (sp.other_l_ != cols[i+1])
179 continue;
181 to_next = &sp;
184 Spring_description desc;
185 if (to_next)
187 desc.hooke_f_ = to_next->strength_f_;
188 desc.ideal_f_ = to_next->distance_f_;
190 else
192 desc.hooke_f_ = 1.0;
193 desc.ideal_f_ = default_space_f_;
196 if (!desc.sane_b ())
198 programming_error ("Insane spring.");
199 continue;
202 desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
203 springs_.push (desc);
206 for (int i=0; i < cols.size () - 1; i++)
208 Array<Column_rod> * rods = &cols [i]->minimal_dists_arr_drul_[RIGHT];
209 for (int j =0; j < rods->size( ); j++)
211 int oi = cols.find_i (rods->elem (j).other_l_ );
212 if (oi >= 0)
214 add_rod (i, oi, rods->elem (j).distance_f_);
219 if (line_len_f_ < 0)
220 my_solve_natural_len ();
221 else
222 my_solve_linelen ();
225 void
226 Simple_spacer::solve (Column_x_positions *positions) const
228 positions->energy_f_ = energy_f (); // abs (force_f_);
229 positions->force_f_ = force_f_;
231 positions->config_.push (indent_f_);
232 for (int i=0; i <springs_.size (); i++)
234 positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
237 positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
242 Spring_description::Spring_description( )
244 ideal_f_ =0.0;
245 hooke_f_ =0.0;
246 active_b_ = true;
247 block_force_f_ = 0.0;
250 Real
251 Spring_description::energy_f (Real force) const
253 Real stretch = (force >? block_force_f_) / hooke_f_;
254 Real e = 0.5 * stretch * stretch * hooke_f_;
255 return e;
258 bool
259 Spring_description::sane_b () const
261 return (hooke_f_ > 0) && ! isinf (ideal_f_) && !isnan (ideal_f_);
265 Real
266 Simple_spacer::energy_f () const
268 Real e =0.;
270 for (int i=0; i <springs_.size (); i++)
272 e += springs_[i].energy_f (force_f_);
275 if (force_f_ < 0)
276 e *= compression_energy_factor_f_;
278 return e;