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 ()
29 default_space_f_ = 20 PT;
33 Simple_spacer::add_rod (int l, int r, Real dist)
35 if (isinf (dist) || isnan (dist))
37 programming_error ("Weird minimum distance. Ignoring");
42 Real c = range_stiffness (l,r);
43 Real d = range_ideal_len (l,r);
44 Real block_stretch = dist - d;
46 Real block_force = c * block_stretch;
47 force_f_ = force_f_ >? block_force;
49 for (int i=l; i < r; i++)
50 springs_[i].block_force_f_ = block_force >?
51 springs_[i].block_force_f_ ;
55 Simple_spacer::range_ideal_len (int l, int r) const
58 for (int i=l; i < r; i++)
59 d += springs_[i].ideal_f_;
64 Simple_spacer::range_stiffness (int l, int r) const
67 for (int i=l; i < r; i++)
68 den += 1 / springs_[i].hooke_f_;
74 Simple_spacer::active_blocking_force () const
76 Real bf = - infinity_f;
77 for (int i=0; i < springs_.size (); i++)
78 if (springs_[i].active_b_)
80 bf = bf >? springs_[i].block_force_f_;
86 Simple_spacer::active_springs_stiffness () const
89 for (int i=0; i < springs_.size (); i++)
90 if (springs_[i].active_b_)
92 den += 1 / springs_[i].hooke_f_;
98 Simple_spacer::set_active_states ()
101 // force is only copied.
102 for (int i=0 ; i <springs_.size (); i++)
103 if (springs_[i].block_force_f_ >= force_f_)
104 springs_[i].active_b_ = false;
108 Simple_spacer::configuration_length () const
111 for (int i=0; i < springs_.size (); i++)
112 l += springs_[i].length (force_f_);
118 Spring_description::length (Real f) const
122 return ideal_f_ + f / hooke_f_ ;
126 Simple_spacer::active_b () const
128 for (int i=0; i < springs_.size (); i++)
129 if (springs_[i].active_b_)
135 Simple_spacer::my_solve_linelen ()
139 force_f_ = active_blocking_force ();
140 Real conf = configuration_length ();
142 if (conf < line_len_f_)
144 force_f_ += (line_len_f_ - conf) * active_springs_stiffness ();
148 set_active_states ();
154 Simple_spacer::my_solve_natural_len ()
158 force_f_ = active_blocking_force () >? 0.0;
160 if (force_f_ < 1e-8) // ugh.,
163 set_active_states ();
168 Simple_spacer::add_columns (Link_array<Grob> cols)
170 for (int i = cols.size (); i--;)
171 if (gh_pair_p (cols[i]->get_grob_property ("between-cols")))
173 loose_cols_.push (cols[i]);
178 for (int i=0; i < cols.size () - 1; i++)
180 SCM spring_params = SCM_EOL;
181 for (SCM s = cols[i]->get_grob_property ("ideal-distances");
182 !gh_pair_p (spring_params) && gh_pair_p (s);
185 Grob *other = unsmob_grob (gh_caar (s));
186 if (other != cols[i+1])
189 spring_params = gh_cdar (s);
192 Spring_description desc;
193 if (gh_pair_p (spring_params))
195 desc.ideal_f_ = gh_scm2double (ly_car (spring_params));
196 desc.hooke_f_ = gh_scm2double (ly_cdr (spring_params));
200 programming_error (_f("No spring between column %d and next one",
201 Paper_column::rank_i (cols[i])
204 desc.ideal_f_ = default_space_f_;
209 programming_error ("Insane spring found. Setting to unit spring.");
211 cout << "columns " << Paper_column::rank_i (cols[i])
212 << " " << Paper_column::rank_i (cols[i+1]) << endl;
217 desc.block_force_f_ = - desc.hooke_f_ * desc.ideal_f_; // block at distance 0
218 springs_.push (desc);
221 for (int i=0; i < cols.size () - 1; i++)
223 for (SCM s = Spaceable_grob::get_minimum_distances (cols[i]);
224 gh_pair_p (s); s = ly_cdr (s))
226 Grob * other = unsmob_grob (gh_caar (s));
227 int oi = cols.find_i (other);
230 add_rod (i, oi, gh_scm2double (gh_cdar (s)));
236 TODO: should support natural length on only the last line.
239 my_solve_natural_len ();
245 Simple_spacer::solve (Column_x_positions *positions) const
247 positions->force_f_ = force_f_;
249 positions->config_.push (indent_f_);
250 for (int i=0; i <springs_.size (); i++)
252 positions->config_.push (positions->config_.top () + springs_[i].length (force_f_));
254 positions->cols_ = spaced_cols_;
255 positions->loose_cols_ = loose_cols_;
257 positions->satisfies_constraints_b_ = (line_len_f_ < 0) || active_b ();
262 Spring_description::Spring_description ()
267 block_force_f_ = 0.0;
272 Spring_description::sane_b () const
274 return (hooke_f_ > 0) && ! isinf (ideal_f_) && !isnan (ideal_f_);