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?
15 #include "simple-spacer.hh"
16 #include "paper-column.hh"
20 #include "column-x-positions.hh"
21 #include "dimensions.hh"
23 Simple_spacer::Simple_spacer ()
27 default_space_f_ = 20 PT;
31 Simple_spacer::add_rod (int l, int r, Real dist)
33 if (isinf (dist) || isnan (dist))
35 programming_error ("Weird minimum distance. Ignoring");
40 Real c = range_stiffness (l,r);
41 Real d = range_ideal_len (l,r);
42 Real block_stretch = dist - d;
44 Real block_force = c * block_stretch;
45 force_f_ = force_f_ >? block_force;
47 for (int i=l; i < r; i++)
48 springs_[i].block_force_f_ = block_force >?
49 springs_[i].block_force_f_ ;
53 Simple_spacer::range_ideal_len (int l, int r) const
56 for (int i=l; i < r; i++)
57 d += springs_[i].ideal_f_;
62 Simple_spacer::range_stiffness (int l, int r) const
65 for (int i=l; i < r; i++)
66 den += 1 / springs_[i].hooke_f_;
72 Simple_spacer::active_blocking_force () const
74 Real bf = - infinity_f;
75 for (int i=0; i < springs_.size (); i++)
76 if (springs_[i].active_b_)
78 bf = bf >? springs_[i].block_force_f_;
84 Simple_spacer::active_springs_stiffness () const
87 for (int i=0; i < springs_.size (); i++)
88 if (springs_[i].active_b_)
90 den += 1 / springs_[i].hooke_f_;
96 Simple_spacer::set_active_states ()
99 // force is only copied.
100 for (int i=0 ; i <springs_.size (); i++)
101 if (springs_[i].block_force_f_ >= force_f_)
102 springs_[i].active_b_ = false;
106 Simple_spacer::configuration_length () const
109 for (int i=0; i < springs_.size (); i++)
110 l += springs_[i].length (force_f_);
116 Spring_description::length (Real f) const
120 return ideal_f_ + f / hooke_f_ ;
124 Simple_spacer::active_b () const
126 for (int i=0; i < springs_.size (); i++)
127 if (springs_[i].active_b_)
133 Simple_spacer::my_solve_linelen ()
137 force_f_ = active_blocking_force ();
138 Real conf = configuration_length ();
140 if (conf < line_len_f_)
142 force_f_ += (line_len_f_ - conf) * active_springs_stiffness ();
146 set_active_states ();
152 Simple_spacer::my_solve_natural_len ()
156 force_f_ = active_blocking_force () >? 0.0;
158 if (force_f_ < 1e-8) // ugh.,
161 set_active_states ();
166 Simple_spacer::add_columns (Link_array<Paper_column> cols)
168 for (int i=0; i < cols.size () - 1; i++)
170 Paper_column * c = cols [i];
171 Column_spring *to_next = 0;
172 for (int j =0; !to_next && j < c->springs_.size( ); j++)
174 Column_spring &sp = c->springs_ [j];
175 if (sp.other_l_ != cols[i+1])
181 Spring_description desc;
184 desc.hooke_f_ = to_next->strength_f_;
185 desc.ideal_f_ = to_next->distance_f_;
190 desc.ideal_f_ = default_space_f_;
195 programming_error ("Insane spring found. Setting to unit spring.");
200 desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
201 springs_.push (desc);
204 for (int i=0; i < cols.size () - 1; i++)
206 Array<Column_rod> * rods = &cols [i]->minimal_dists_;
207 for (int j =0; j < rods->size( ); j++)
209 int oi = cols.find_i (rods->elem (j).other_l_ );
212 add_rod (i, oi, rods->elem (j).distance_f_);
218 TODO: should support natural length on only the last line.
221 my_solve_natural_len ();
227 Simple_spacer::solve (Column_x_positions *positions) const
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( )
247 block_force_f_ = 0.0;
252 Spring_description::sane_b () const
254 return (hooke_f_ > 0) && ! isinf (ideal_f_) && !isnan (ideal_f_);