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"
22 #include "dimensions.hh"
24 Simple_spacer::Simple_spacer ()
28 default_space_f_ = 20 PT;
29 compression_energy_factor_f_ = 3.0;
33 Simple_spacer::add_rod (int l, int r, Real dist)
35 Real c = range_stiffness (l,r);
36 Real d = range_ideal_len (l,r);
37 Real block_stretch = dist - d;
39 Real block_force = c * block_stretch;
40 force_f_ = force_f_ >? block_force;
42 for (int i=l; i < r; i++)
43 springs_[i].block_force_f_ = block_force >?
44 springs_[i].block_force_f_ ;
48 Simple_spacer::range_ideal_len (int l, int r) const
51 for (int i=l; i < r; i++)
52 d += springs_[i].ideal_f_;
57 Simple_spacer::range_stiffness (int l, int r) const
60 for (int i=l; i < r; i++)
61 den += 1 / springs_[i].hooke_f_;
67 Simple_spacer::active_blocking_force () const
69 Real bf = - infinity_f;
70 for (int i=0; i < springs_.size (); i++)
71 if (springs_[i].active_b_)
73 bf = bf >? springs_[i].block_force_f_;
79 Simple_spacer::active_springs_stiffness () const
82 for (int i=0; i < springs_.size (); i++)
83 if (springs_[i].active_b_)
85 den += 1 / springs_[i].hooke_f_;
91 Simple_spacer::set_active_states ()
94 // force is only copied.
95 for (int i=0 ; i <springs_.size (); i++)
96 if (springs_[i].block_force_f_ >= force_f_)
97 springs_[i].active_b_ = false;
101 Simple_spacer::configuration_length () const
104 for (int i=0; i < springs_.size (); i++)
105 l += springs_[i].length (force_f_);
111 Spring_description::length (Real f) const
115 return ideal_f_ + f / hooke_f_ ;
119 Simple_spacer::active_b () const
121 for (int i=0; i < springs_.size (); i++)
122 if (springs_[i].active_b_)
128 Simple_spacer::my_solve_linelen ()
132 force_f_ = active_blocking_force ();
133 Real conf = configuration_length ();
135 if (conf < line_len_f_)
137 force_f_ += (line_len_f_ - conf) * active_springs_stiffness ();
141 set_active_states ();
147 Simple_spacer::my_solve_natural_len ()
151 force_f_ = active_blocking_force () >? 0.0;
153 if (force_f_ < 1e-8) // ugh.,
156 set_active_states ();
161 Simple_spacer::add_columns (Link_array<Paper_column> cols)
163 for (int i=0; i < cols.size () - 1; i++)
165 Paper_column * c = cols [i];
166 Column_spring *to_next = 0;
167 for (int j =0; !to_next && j < c->spring_arr_drul_[RIGHT].size( ); j++)
169 Column_spring &sp = c->spring_arr_drul_[RIGHT] [j];
170 if (sp.other_l_ != cols[i+1])
176 Spring_description desc;
179 desc.hooke_f_ = to_next->strength_f_;
180 desc.ideal_f_ = to_next->distance_f_;
185 desc.ideal_f_ = default_space_f_;
187 desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
188 springs_.push (desc);
191 for (int i=0; i < cols.size () - 1; i++)
193 Array<Column_rod> * rods = &cols [i]->minimal_dists_arr_drul_[RIGHT];
194 for (int j =0; j < rods->size( ); j++)
196 int oi = cols.find_i (rods->elem (j).other_l_ );
199 add_rod (i, oi, rods->elem (j).distance_f_);
205 my_solve_natural_len ();
211 Simple_spacer::solve (Column_x_positions *positions) const
213 positions->energy_f_ = energy_f (); // abs (force_f_);
214 positions->force_f_ = force_f_;
216 positions->config_.push (indent_f_);
217 for (int i=0; i <springs_.size (); i++)
219 positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
222 positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
227 Spring_description::Spring_description( )
232 block_force_f_ = 0.0;
236 Spring_description::energy_f (Real force) const
238 Real stretch = (force >? block_force_f_) / hooke_f_;
239 Real e = 0.5 * stretch * stretch * hooke_f_;
244 Simple_spacer::energy_f () const
248 for (int i=0; i <springs_.size (); i++)
250 e += springs_[i].energy_f (force_f_);
254 e *= compression_energy_factor_f_;