2 simple-spacer.cc -- implement Simple_spacer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 - add support for different stretch/shrink constants?
14 #include <libc-extension.hh> // isinf
16 #include "simple-spacer.hh"
17 #include "paper-column.hh"
21 #include "column-x-positions.hh"
22 #include "spaceable-grob.hh"
23 #include "dimensions.hh"
25 Simple_spacer::Simple_spacer ()
30 default_space_f_ = 20 PT;
34 Simple_spacer::add_rod (int l, int r, Real dist)
36 if (isinf (dist) || isnan (dist))
38 programming_error ("Weird minimum distance. Ignoring");
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_ ;
56 Simple_spacer::range_ideal_len (int l, int r) const
59 for (int i=l; i < r; i++)
60 d += springs_[i].ideal_f_;
65 Simple_spacer::range_stiffness (int l, int r) const
68 for (int i=l; i < r; i++)
69 den += 1 / springs_[i].hooke_f_;
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_;
87 Simple_spacer::active_springs_stiffness () const
90 for (int i=0; i < springs_.size (); i++)
91 if (springs_[i].active_b_)
93 den += 1 / springs_[i].hooke_f_;
99 Simple_spacer::set_active_states ()
102 // force is only copied.
103 for (int i=0 ; i <springs_.size (); i++)
104 if (springs_[i].active_b_
105 && springs_[i].block_force_f_ >= force_f_)
107 springs_[i].active_b_ = false;
113 Simple_spacer::configuration_length () const
116 for (int i=0; i < springs_.size (); i++)
117 l += springs_[i].length (force_f_);
123 Spring_description::length (Real f) const
127 return ideal_f_ + f / hooke_f_ ;
131 Simple_spacer::active_b () const
133 return active_count_;
137 Simple_spacer::my_solve_linelen ()
141 force_f_ = active_blocking_force ();
142 Real conf = configuration_length ();
144 if (conf < line_len_f_)
146 force_f_ += (line_len_f_ - conf) * active_springs_stiffness ();
150 set_active_states ();
156 Simple_spacer::my_solve_natural_len ()
160 force_f_ = active_blocking_force () >? 0.0;
162 if (force_f_ < 1e-8) // ugh.,
165 set_active_states ();
170 Simple_spacer::add_columns (Link_array<Grob> cols)
172 for (int i = cols.size (); i--;)
173 if (gh_pair_p (cols[i]->get_grob_property ("between-cols")))
175 loose_cols_.push (cols[i]);
180 for (int i=0; i < cols.size () - 1; i++)
182 SCM spring_params = SCM_EOL;
183 for (SCM s = cols[i]->get_grob_property ("ideal-distances");
184 !gh_pair_p (spring_params) && gh_pair_p (s);
187 Grob *other = unsmob_grob (ly_caar (s));
188 if (other != cols[i+1])
191 spring_params = ly_cdar (s);
194 Spring_description desc;
195 if (gh_pair_p (spring_params))
197 desc.ideal_f_ = gh_scm2double (ly_car (spring_params));
198 desc.hooke_f_ = gh_scm2double (ly_cdr (spring_params));
202 programming_error (_f("No spring between column %d and next one",
203 Paper_column::rank_i (cols[i])
206 desc.ideal_f_ = default_space_f_;
211 programming_error ("Insane spring found. Setting to unit spring.");
213 cout << "columns " << Paper_column::rank_i (cols[i])
214 << " " << Paper_column::rank_i (cols[i+1]) << endl;
219 desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
220 springs_.push (desc);
224 for (int i=0; i < cols.size () - 1; i++)
226 for (SCM s = Spaceable_grob::get_minimum_distances (cols[i]);
227 gh_pair_p (s); s = ly_cdr (s))
229 Grob * other = unsmob_grob (ly_caar (s));
230 int oi = cols.find_i (other);
233 add_rod (i, oi, gh_scm2double (ly_cdar (s)));
239 TODO: should support natural length on only the last line.
242 my_solve_natural_len ();
248 Simple_spacer::solve (Column_x_positions *positions) const
250 positions->force_f_ = force_f_;
252 positions->config_.push (indent_f_);
253 for (int i=0; i <springs_.size (); i++)
255 positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
257 positions->cols_ = spaced_cols_;
258 positions->loose_cols_ = loose_cols_;
260 positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
264 Check if breaking constraints are met.
266 bool break_satisfy = true;
267 int sz = positions->cols_.size ();
268 for (int i = sz; i--; )
270 SCM p = positions->cols_[i]->get_grob_property( "penalty");
273 if (gh_scm2double (p) < -9999)
274 break_satisfy = break_satisfy && (i == 0 || i == sz -1);
275 if (gh_scm2double (p) > 9999)
276 break_satisfy = break_satisfy && !(i == 0 || i == sz -1);
281 positions->satisfies_constraints_b_ =
282 positions->satisfies_constraints_b_ && break_satisfy;
290 Spring_description::Spring_description ()
295 block_force_f_ = 0.0;
300 Spring_description::sane_b () const
302 return (hooke_f_ > 0) && ! isinf (ideal_f_) && !isnan (ideal_f_);