2 simple-spacer.cc -- implement Simple_spacer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 - add support for different stretch/shrink constants?
14 #include <libc-extension.hh>
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;
32 Simple_spacer::add_rod (int l, int r, Real dist)
34 if (isinf (dist) || isnan (dist))
36 programming_error ("Weird minimum distance. Ignoring");
41 Real c = range_stiffness (l,r);
42 Real d = range_ideal_len (l,r);
43 Real block_stretch = dist - d;
45 Real block_force = c * block_stretch;
46 force_f_ = force_f_ >? block_force;
48 for (int i=l; i < r; i++)
49 springs_[i].block_force_f_ = block_force >?
50 springs_[i].block_force_f_ ;
54 Simple_spacer::range_ideal_len (int l, int r) const
57 for (int i=l; i < r; i++)
58 d += springs_[i].ideal_f_;
63 Simple_spacer::range_stiffness (int l, int r) const
66 for (int i=l; i < r; i++)
67 den += 1 / springs_[i].hooke_f_;
73 Simple_spacer::active_blocking_force () const
75 Real bf = - infinity_f;
76 for (int i=0; i < springs_.size (); i++)
77 if (springs_[i].active_b_)
79 bf = bf >? springs_[i].block_force_f_;
85 Simple_spacer::active_springs_stiffness () const
88 for (int i=0; i < springs_.size (); i++)
89 if (springs_[i].active_b_)
91 den += 1 / springs_[i].hooke_f_;
97 Simple_spacer::set_active_states ()
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;
107 Simple_spacer::configuration_length () const
110 for (int i=0; i < springs_.size (); i++)
111 l += springs_[i].length (force_f_);
117 Spring_description::length (Real f) const
121 return ideal_f_ + f / hooke_f_ ;
125 Simple_spacer::active_b () const
127 for (int i=0; i < springs_.size (); i++)
128 if (springs_[i].active_b_)
134 Simple_spacer::my_solve_linelen ()
138 force_f_ = active_blocking_force ();
139 Real conf = configuration_length ();
141 if (conf < line_len_f_)
143 force_f_ += (line_len_f_ - conf) * active_springs_stiffness ();
147 set_active_states ();
153 Simple_spacer::my_solve_natural_len ()
157 force_f_ = active_blocking_force () >? 0.0;
159 if (force_f_ < 1e-8) // ugh.,
162 set_active_states ();
167 Simple_spacer::add_columns (Link_array<Paper_column> cols)
169 for (int i=0; i < cols.size () - 1; i++)
171 Paper_column * c = cols [i];
172 Column_spring *to_next = 0;
173 for (int j =0; !to_next && j < c->springs_.size( ); j++)
175 Column_spring &sp = c->springs_ [j];
176 if (sp.other_l_ != cols[i+1])
182 Spring_description desc;
185 desc.hooke_f_ = to_next->strength_f_;
186 desc.ideal_f_ = to_next->distance_f_;
191 desc.ideal_f_ = default_space_f_;
196 programming_error ("Insane spring found. Setting to unit spring.");
201 desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
202 springs_.push (desc);
205 for (int i=0; i < cols.size () - 1; i++)
207 Array<Column_rod> * rods = &cols [i]->minimal_dists_;
208 for (int j =0; j < rods->size( ); j++)
210 int oi = cols.find_i (rods->elem (j).other_l_ );
213 add_rod (i, oi, rods->elem (j).distance_f_);
219 TODO: should support natural length on only the last line.
222 my_solve_natural_len ();
228 Simple_spacer::solve (Column_x_positions *positions) const
230 positions->force_f_ = force_f_;
232 positions->config_.push (indent_f_);
233 for (int i=0; i <springs_.size (); i++)
235 positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
238 positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
243 Spring_description::Spring_description( )
248 block_force_f_ = 0.0;
253 Spring_description::sane_b () const
255 return (hooke_f_ > 0) && ! isinf (ideal_f_) && !isnan (ideal_f_);