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>
9 - add support for different stretch/shrink constants?
10 - Use force as a minimizing function, and use it to discourage mixes of
16 #include "simple-spacer.hh"
17 #include "paper-column.hh"
21 #include "column-x-positions.hh"
23 Simple_spacer::Simple_spacer ()
29 Simple_spacer::add_rod (int l, int r, Real dist)
31 Real c = range_stiffness (l,r);
32 Real d = range_ideal_len (l,r);
33 Real block_stretch = dist - d;
35 Real block_force = c * block_stretch;
36 force_f_ = force_f_ >? block_force;
38 for (int i=l; i < r; i++)
39 springs_[i].block_force_f_ = block_force >?
40 springs_[i].block_force_f_ ;
44 Simple_spacer::range_ideal_len (int l, int r) const
47 for (int i=l; i < r; i++)
48 d += springs_[i].ideal_f_;
53 Simple_spacer::range_stiffness (int l, int r) const
56 for (int i=l; i < r; i++)
57 den += 1 / springs_[i].hooke_f_;
63 Simple_spacer::active_blocking_force () const
65 Real bf = - infinity_f;
66 for (int i=0; i < springs_.size (); i++)
67 if (springs_[i].active_b_)
69 bf = bf >? springs_[i].block_force_f_;
75 Simple_spacer::active_springs_stiffness () const
78 for (int i=0; i < springs_.size (); i++)
79 if (springs_[i].active_b_)
81 den += 1 / springs_[i].hooke_f_;
87 Simple_spacer::set_active_states ()
90 // force is only copied.
91 for (int i=0 ; i <springs_.size (); i++)
92 if (springs_[i].block_force_f_ >= force_f_)
93 springs_[i].active_b_ = false;
97 Simple_spacer::configuration_length () const
100 for (int i=0; i < springs_.size (); i++)
101 l += springs_[i].length (force_f_);
107 Spring_description::length (Real f) const
111 return ideal_f_ + f / hooke_f_ ;
115 Simple_spacer::active_b () const
117 for (int i=0; i < springs_.size (); i++)
118 if (springs_[i].active_b_)
124 Simple_spacer::my_solve_linelen ()
128 force_f_ = active_blocking_force ();
129 Real conf = configuration_length ();
131 if (conf < line_len_f_)
133 force_f_ += (line_len_f_ - conf) * active_springs_stiffness ();
137 set_active_states ();
143 Simple_spacer::my_solve_natural_len ()
147 force_f_ = active_blocking_force () >? 0.0;
149 if (force_f_ < 1e-8) // ugh.,
152 set_active_states ();
157 Simple_spacer::add_columns (Link_array<Paper_column> cols)
159 for (int i=0; i < cols.size () - 1; i++)
161 Paper_column * c = cols [i];
162 Column_spring *to_next = 0;
163 for (int j =0; !to_next && j < c->spring_arr_drul_[RIGHT].size( ); j++)
165 Column_spring &sp = c->spring_arr_drul_[RIGHT] [j];
166 if (sp.other_l_ != cols[i+1])
172 Spring_description desc;
175 desc.hooke_f_ = to_next->strength_f_;
176 desc.ideal_f_ = to_next->distance_f_;
181 desc.ideal_f_ = default_space_f_;
183 desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
184 springs_.push (desc);
187 for (int i=0; i < cols.size () - 1; i++)
189 Array<Column_rod> * rods = &cols [i]->minimal_dists_arr_drul_[RIGHT];
190 for (int j =0; j < rods->size( ); j++)
192 int oi = cols.find_i (rods->elem (j).other_l_ );
195 add_rod (i, oi, rods->elem (j).distance_f_);
201 my_solve_natural_len ();
207 Simple_spacer::solve (Column_x_positions *positions) const
209 positions->energy_f_ = energy_f (); // abs (force_f_);
210 positions->config_.push (indent_f_);
211 for (int i=0; i <springs_.size (); i++)
213 positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
216 positions->satisfies_constraints_b_ = active_b ();
220 Simple_spacer::lower_bound_solution (Column_x_positions * posns) const
226 Spring_description::Spring_description( )
231 block_force_f_ = 0.0;
235 Spring_description::energy_f (Real force) const
237 Real stretch = (force >? block_force_f_) / hooke_f_;
238 Real e = 0.5 * stretch * stretch * hooke_f_;
241 be harder to compress.
250 Simple_spacer::energy_f () const
254 for (int i=0; i <springs_.size (); i++)
255 e += springs_[i].energy_f (force_f_);