2 gourlay-breaking.cc -- implement Gourlay_breaking
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 #include <math.h> // rint
10 #include "gourlay-breaking.hh"
11 #include "column-x-positions.hh"
13 #include "paper-column.hh"
14 #include "paper-score.hh"
15 #include "paper-def.hh"
16 #include "simple-spacer.hh"
19 /// How often to print operator pacification marks?
20 const int HAPPY_DOTS_I = 3;
23 Helper to trace back an optimal path
26 /** this was the previous. If negative, this break should not be
27 considered: this path has infinite energy
32 Which system number so far?
37 Column_x_positions line_config_;
48 This algorithms is adapted from the OSU Tech report on breaking lines.
50 this function is longish, but not very complicated.
53 Array<Column_x_positions>
54 Gourlay_breaking::do_solve () const
56 Array<Break_node> optimal_paths;
57 Link_array<Grob> all =
58 pscore_l_->line_l_->column_l_arr ();
60 Array<int> breaks = find_break_indices ();
62 Break_node first_node ;
63 optimal_paths.push (first_node);
65 Real worst_force = 0.0;
67 for (int break_idx=1; break_idx< breaks.size (); break_idx++)
70 start with a short line, add measures. At some point
71 the line becomes infeasible. Then we don't try to add more
73 int minimal_start_idx = -1;
74 Column_x_positions minimal_sol;
75 Column_x_positions backup_sol;
77 Real minimal_demerits = infinity_f;
79 bool ragged = to_boolean (pscore_l_->paper_l_->get_scmvar ("raggedright"));
81 for (int start_idx = break_idx; start_idx--;)
83 Link_array<Grob> line = all.slice (breaks[start_idx], breaks[break_idx]+1);
85 line[0] = dynamic_cast<Item*> (line[0]) ->find_prebroken_piece (RIGHT);
86 line.top () = dynamic_cast<Item*> (line.top ())->find_prebroken_piece (LEFT);
88 Column_x_positions cp;
92 = pscore_l_->paper_l_->line_dimensions_int (optimal_paths[start_idx].line_i_);
93 Simple_spacer * sp = generate_spacing_problem (line, line_dims);
94 sp->solve (&cp, ragged);
97 if (fabs (cp.force_f_) > worst_force)
98 worst_force = fabs (cp.force_f_);
101 We remember this solution as a "should always work
102 solution", in case everything fucks up. */
103 if (start_idx == break_idx - 1)
108 if (optimal_paths[start_idx].demerits_f_ >= infinity_f)
109 this_demerits = infinity_f;
111 this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp)
112 + optimal_paths[start_idx].demerits_f_;
114 if (this_demerits < minimal_demerits)
116 minimal_start_idx = start_idx;
118 minimal_demerits = this_demerits;
122 we couldn't satisfy the constraints, this won't get better
123 if we add more columns, so we get on with the next one
125 if (!cp.satisfies_constraints_b_)
131 if (minimal_start_idx < 0)
133 bnod.demerits_f_ = infinity_f;
134 bnod.line_config_ = backup_sol;
135 bnod.prev_break_i_ = break_idx - 1;
139 bnod.prev_break_i_ = minimal_start_idx;
140 bnod.demerits_f_ = minimal_demerits;
141 bnod.line_config_ = minimal_sol;
143 bnod.line_i_ = optimal_paths[bnod.prev_break_i_].line_i_ + 1;
144 optimal_paths.push (bnod);
146 if (! (break_idx % HAPPY_DOTS_I))
147 progress_indication (String ("[") + to_str (break_idx) + "]");
150 /* do the last one */
151 if (breaks.size () % HAPPY_DOTS_I)
152 progress_indication (String ("[") + to_str (breaks.size()) + "]");
155 progress_indication ("\n");
157 Array<int> final_breaks;
158 Array<Column_x_positions> lines;
160 /* skip 0-th element, since it is a "dummy" elt*/
161 for (int i = optimal_paths.size ()-1; i> 0;)
163 final_breaks.push (i);
164 int prev = optimal_paths[i].prev_break_i_;
169 if (optimal_paths.top ().demerits_f_ >= infinity_f)
170 warning (_ ("No feasible line breaking found"));
172 for (int i= final_breaks.size (); i--;)
174 Column_x_positions cp (optimal_paths[final_breaks[i]].line_config_);
177 if(!cp.satisfies_constraints_b_)
178 warning ("Could not find line breaking that satisfies constraints.");
184 Gourlay_breaking::Gourlay_breaking ()
191 TODO: uniformity parameter to control rel. importance of spacing differences.
195 mixing break penalties and constraint-failing solutions is confusing.
198 Gourlay_breaking::combine_demerits (Column_x_positions const &prev,
199 Column_x_positions const &this_one) const
201 Real break_penalties = 0.0;
202 Grob * pc = this_one.cols_.top ();
205 SCM pen = pc->get_grob_property ("penalty");
206 if (gh_number_p (pen) && fabs (gh_scm2double (pen)) < 10000)
208 break_penalties += gh_scm2double (pen);
214 Q: do want globally non-cramped lines, or locally equally cramped lines.
216 Real demerit = abs (this_one.force_f_) + 0.1 *abs (prev.force_f_ - this_one.force_f_)
219 Real demerit = abs (this_one.force_f_) + break_penalties;
222 if (!this_one.satisfies_constraints_b_)
225 If it doesn't satisfy constraints, we make this one
228 add 20000 to the demerits, so that a break penalty
229 of -10000 won't change the result */
230 demerit = (demerit + 20000) >? 2000;