2 gourlay-breaking.cc -- implement Gourlay_breaking
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "gourlay-breaking.hh"
10 #include "column-x-positions.hh"
11 #include "spring-spacer.hh"
13 #include "paper-column.hh"
14 #include "paper-score.hh"
15 #include "paper-def.hh"
17 #include "killing-cons.tcc"
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_;
47 This algorithms is adapted from the OSU Tech report on breaking lines.
49 Array<Column_x_positions>
50 Gourlay_breaking::do_solve () const
52 Array<Break_node> optimal_paths;
53 Line_of_cols all = pscore_l_->col_l_arr_ ;
54 Array<int> breaks = find_break_indices ();
56 optimal_paths.set_size (breaks.size ());
58 Break_node first_node ;
59 first_node.prev_break_i_ = -1;
60 first_node.line_config_.energy_f_ = 0;
61 first_node.line_i_ = 0;
63 optimal_paths[0] = first_node;
66 for (; break_idx< breaks.size (); break_idx++)
68 Array<int> candidates;
69 Array<Column_x_positions> candidate_lines;
70 Cons_list<Line_spacer> spacer_p_list;
73 start with a short line, add measures. At some point
74 the line becomes infeasible. Then we don't try to add more
76 for (int start_idx = break_idx; start_idx--;)
78 if (break_idx - start_idx > max_measures_i_)
81 if (optimal_paths[start_idx].prev_break_i_ < 0
82 && optimal_paths[start_idx].line_config_.energy_f_)
87 Line_of_cols line = all.slice (breaks[start_idx], breaks[break_idx]+1);
89 line[0] = dynamic_cast<Paper_column*>(line[0]->find_prebroken_piece (RIGHT));
90 line.top () = dynamic_cast<Paper_column*>(line.top ()->find_prebroken_piece (LEFT));
95 Column_x_positions approx;
98 approx.spacer_l_ = generate_spacing_problem (line,
99 pscore_l_->paper_l_->line_dimensions_int (optimal_paths[start_idx].line_i_));
100 spacer_p_list.append (new Killing_cons<Line_spacer> (approx.spacer_l_,0));
102 ( (Break_algorithm*)this)->approx_stats_.add (approx.cols_);
103 approx.approximate_solve_line ();
105 if (approx.energy_f_ > energy_bound_f_)
111 // this is a likely candidate. Store it.
112 candidate_lines.push (approx);
113 candidates.push (start_idx);
118 Real minimal_energy = infinity_f;
119 for (int j=0; j < candidates.size (); j++)
121 int start = candidates[j];
122 if (optimal_paths[start].line_config_.energy_f_
123 + candidate_lines[j].energy_f_ > minimal_energy)
127 if (!candidate_lines[j].satisfies_constraints_b_)
129 candidate_lines[j].solve_line ();
130 ( (Break_algorithm*)this)->exact_stats_.add (candidate_lines[j].cols_);
134 = optimal_paths[start].line_config_.energy_f_
135 + candidate_lines[j].energy_f_ ;
137 if (this_energy < minimal_energy)
140 minimal_energy = this_energy;
146 optimal_paths[break_idx].prev_break_i_ = -1;
147 optimal_paths[break_idx].line_config_.energy_f_ = infinity_f;
151 optimal_paths[break_idx].prev_break_i_ = candidates[minimal_j];
152 optimal_paths[break_idx].line_config_ = candidate_lines[minimal_j];
153 optimal_paths[break_idx].line_i_ =
154 optimal_paths[optimal_paths[break_idx].prev_break_i_].line_i_ + 1;
157 if (! (break_idx % HAPPY_DOTS_I))
158 *mlog << "[" << break_idx << "]" << flush;
160 spacer_p_list.junk ();
163 if (break_idx % HAPPY_DOTS_I)
164 *mlog << "[" << break_idx << "]" << flush;
166 Array<int> final_breaks;
168 Array<Column_x_positions> lines;
170 /* skip 0-th element, since it is a "dummy" elt*/
171 for (int i = optimal_paths.size ()-1; i> 0;)
173 final_breaks.push (i);
174 assert (i > optimal_paths[i].prev_break_i_);
176 // there was no "feasible path"
177 if (!optimal_paths[i].line_config_.config_.size ()) {
178 final_breaks.set_size (0);
181 i = optimal_paths[i].prev_break_i_;
185 for (int i= final_breaks.size (); i--;)
186 lines.push (optimal_paths[final_breaks[i]].line_config_);
192 Gourlay_breaking::Gourlay_breaking ()
194 energy_bound_f_ = infinity_f;
195 max_measures_i_ = INT_MAX;
199 Gourlay_breaking::do_set_pscore ()
201 energy_bound_f_ = pscore_l_->paper_l_->get_var ("gourlay_energybound");
202 max_measures_i_ =int (rint (pscore_l_->paper_l_->get_var ("gourlay_maxmeasures")));