X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fgourlay-breaking.cc;h=8599a6a57c58665047b5a533cd4510d5d25f11fd;hb=e10092cbaddbbe8bf2a3668eb70ee4dafb54af8a;hp=3f9662a99a95b07f3600de8c3ca6b327ed74839c;hpb=d7416352b825c3e9b4858e1ddcf14f0c00e26d89;p=lilypond.git diff --git a/lily/gourlay-breaking.cc b/lily/gourlay-breaking.cc index 3f9662a99a..8599a6a57c 100644 --- a/lily/gourlay-breaking.cc +++ b/lily/gourlay-breaking.cc @@ -3,191 +3,262 @@ source file of the GNU LilyPond music typesetter - (c) 1997--2000 Han-Wen Nienhuys + (c) 1997--2006 Han-Wen Nienhuys */ -#include // rint #include "gourlay-breaking.hh" -#include "column-x-positions.hh" -#include "debug.hh" + +#include // rint +#include +using namespace std; + +#include "international.hh" +#include "main.hh" +#include "output-def.hh" #include "paper-column.hh" #include "paper-score.hh" -#include "paper-def.hh" #include "simple-spacer.hh" -#include "line-of-score.hh" +#include "system.hh" +#include "warn.hh" /// How often to print operator pacification marks? -const int HAPPY_DOTS_I = 3; +const int HAPPY_DOTS = 3; /** - Helper to trace back an optimal path - */ -struct Break_node { + Helper to trace back an optimal path +*/ +struct Break_node +{ /** this was the previous. If negative, this break should not be - considered: this path has infinite energy - - */ - int prev_break_i_; + considered: this path has infinite energy + + */ + int prev_break_; /** Which system number so far? - */ - int line_i_; + */ + int line_; - Real demerits_f_; + Real demerits_; Column_x_positions line_config_; - - Break_node () + + Break_node () + { + prev_break_ = -1; + line_ = 0; + demerits_ = 0; + } + + void print () const { - prev_break_i_ = -1; - line_i_ = 0; - demerits_f_ = 0; + printf ("prev break %d, line %d, demerits %f\n", + prev_break_, line_, demerits_); } }; +void +print_break_nodes (vector const &arr) +{ + for (vsize i = 0; i < arr.size (); i++) + { + printf ("node %d: ", i); + arr[i].print (); + } +} + /** - This algorithms is adapted from the OSU Tech report on breaking lines. + This algorithms is adapted from the OSU Tech report on breaking lines. - this function is longish, but not very complicated. - - */ -Array -Gourlay_breaking::do_solve () const + this function is longish, but not very complicated. + + TODO: should rewrite. See the function in scm/page-layout.scm for + inspiration. +*/ +vector +Gourlay_breaking::solve () { - Array optimal_paths; - Link_array all = - pscore_l_->line_l_->column_l_arr (); - - Array breaks = find_break_indices (); - - optimal_paths.set_size (breaks.size ()); - - Break_node first_node ; - - optimal_paths[0] = first_node; - int break_idx=1; - - for (; break_idx< breaks.size (); break_idx++) + vector optimal_paths; + vector all + = pscore_->root_system ()->columns (); + + vector breaks = pscore_->find_break_indices (); + + Break_node first_node; + optimal_paths.push_back (first_node); + + bool ragged_right = to_boolean (pscore_->layout ()->c_variable ("ragged-right")); + bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("ragged-last")); + + Real worst_force = 0.0; + for (vsize break_idx = 1; break_idx < breaks.size (); break_idx++) { /* - start with a short line, add measures. At some point - the line becomes infeasible. Then we don't try to add more - */ + start with a short line, add measures. At some point + the line becomes infeasible. Then we don't try to add more + */ int minimal_start_idx = -1; Column_x_positions minimal_sol; Column_x_positions backup_sol; - + Real minimal_demerits = infinity_f; - for (int start_idx = break_idx; start_idx--;) + for (vsize start_idx = break_idx; start_idx--;) { - Link_array line = all.slice (breaks[start_idx], breaks[break_idx]+1); - - line[0] = dynamic_cast (line[0] ->find_prebroken_piece (RIGHT)); - line.top () = dynamic_cast (line.top ()->find_prebroken_piece (LEFT)); - - Column_x_positions cp; - cp.cols_ = line; + vector line (all.begin () + breaks[start_idx], + all.begin () + breaks[break_idx] + 1); Interval line_dims - = pscore_l_->paper_l_->line_dimensions_int (optimal_paths[start_idx].line_i_); - Simple_spacer * sp = generate_spacing_problem (line, line_dims); - sp->solve (&cp); - delete sp; + = line_dimensions_int (pscore_->layout (), optimal_paths[start_idx].line_); + bool last_line = break_idx == breaks.size () - 1; + bool ragged = ragged_right || (last_line && ragged_last); + Column_x_positions cp = get_line_configuration (line, line_dims[RIGHT] - line_dims[LEFT], + line_dims[LEFT], ragged); + if (ragged && last_line) + cp.force_ = 0.0; + + if (fabs (cp.force_) > worst_force) + worst_force = fabs (cp.force_); + + /* + We remember this solution as a "should always work + solution", in case everything fucks up. */ if (start_idx == break_idx - 1) - backup_sol = cp; // in case everything fucks up - if (!cp.satisfies_constraints_b_) - break; + backup_sol = cp; - Real this_demerits; - if (optimal_paths[start_idx].demerits_f_ >= infinity_f) + + if (optimal_paths[start_idx].demerits_ >= infinity_f) this_demerits = infinity_f; else this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp) - + optimal_paths[start_idx].demerits_f_; + + optimal_paths[start_idx].demerits_; - if (this_demerits < minimal_demerits) + if (this_demerits < minimal_demerits) { minimal_start_idx = start_idx; minimal_sol = cp; minimal_demerits = this_demerits; } + + /* + we couldn't satisfy the constraints, this won't get better + if we add more columns, so we get on with the next one + */ + if (!cp.satisfies_constraints_) + break; } - int prev =break_idx - 1; - if (minimal_start_idx < 0) + Break_node bnod; + if (minimal_start_idx < 0) { - optimal_paths[break_idx].demerits_f_ = infinity_f; - optimal_paths[break_idx].line_config_ = backup_sol; + bnod.demerits_ = infinity_f; + bnod.line_config_ = backup_sol; + bnod.prev_break_ = break_idx - 1; } - else + else { - prev = minimal_start_idx; - optimal_paths[break_idx].line_config_ = minimal_sol; - optimal_paths[break_idx].demerits_f_ = minimal_demerits; + bnod.prev_break_ = minimal_start_idx; + bnod.demerits_ = minimal_demerits; + bnod.line_config_ = minimal_sol; } - optimal_paths[break_idx].prev_break_i_ = prev; - optimal_paths[break_idx].line_i_ = optimal_paths[prev].line_i_ + 1; + bnod.line_ = optimal_paths[bnod.prev_break_].line_ + 1; + optimal_paths.push_back (bnod); - if (! (break_idx % HAPPY_DOTS_I)) - progress_indication (String ("[") + to_str (break_idx) + "]"); + if (! (break_idx % HAPPY_DOTS)) + progress_indication (string ("[") + to_string (break_idx) + "]"); } /* do the last one */ - if (break_idx % HAPPY_DOTS_I) - progress_indication (String ("[") + to_str (break_idx) + "]"); - + if (breaks.size () % HAPPY_DOTS) + progress_indication (string ("[") + to_string (breaks.size ()) + "]"); progress_indication ("\n"); - Array final_breaks; - Array lines; + vector final_breaks; + vector lines; /* skip 0-th element, since it is a "dummy" elt*/ - for (int i = optimal_paths.size ()-1; i> 0;) + for (vsize i = optimal_paths.size () - 1; i > 0;) { - final_breaks.push (i); - int prev = optimal_paths[i].prev_break_i_; + final_breaks.push_back (i); + vsize prev = optimal_paths[i].prev_break_; assert (i > prev); i = prev; } - if (optimal_paths.top ().demerits_f_ >= infinity_f) - warning (_ ("No feasible line breaking found")); - - for (int i= final_breaks.size (); i--;) - lines.push (optimal_paths[final_breaks[i]].line_config_); - + if (be_verbose_global) + { + message (_f ("Optimal demerits: %f", + optimal_paths.back ().demerits_) + "\n"); + } + + if (optimal_paths.back ().demerits_ >= infinity_f) + warning (_ ("no feasible line breaking found")); + + for (vsize i = final_breaks.size (); i--;) + { + Column_x_positions cp (optimal_paths[final_breaks[i]].line_config_); + + lines.push_back (cp); + if (!cp.satisfies_constraints_) + warning (_ ("can't find line breaking that satisfies constraints")); + } return lines; } - Gourlay_breaking::Gourlay_breaking () { } - - /* TODO: uniformity parameter to control rel. importance of spacing differences. - */ + + TODO: + + mixing break penalties and constraint-failing solutions is confusing. +*/ Real Gourlay_breaking::combine_demerits (Column_x_positions const &prev, Column_x_positions const &this_one) const { Real break_penalties = 0.0; - Paper_column * pc = this_one.cols_.top (); - if (pc->original_l_) + Grob *pc = this_one.cols_.back (); + if (pc->original ()) { - SCM pen = pc->get_elt_property ("penalty"); - if (gh_number_p (pen)) - { - break_penalties += gh_scm2double (pen); - } + SCM pen = pc->get_property ("line-break-penalty"); + if (scm_is_number (pen) && fabs (scm_to_double (pen)) < 10000) + break_penalties += scm_to_double (pen); } - return abs (this_one.force_f_) + abs (prev.force_f_ - this_one.force_f_) + /* + Q: do we want globally non-cramped lines, or locally equally + cramped lines? + + There used to be an example file input/test/uniform-breaking to + demonstrate problems with this approach. When music is gradually + becoming denser, the uniformity requirement makes lines go from + cramped to even more cramped (because going from cramped + 3meas/line to relatively loose 2meas/line is such a big step. + + */ + + Real demerit = abs (this_one.force_) + abs (prev.force_ - this_one.force_) + break_penalties; + + if (!this_one.satisfies_constraints_) + { + /* + If it doesn't satisfy constraints, we make this one + really unattractive. + + add 20000 to the demerits, so that a break penalty + of -10000 won't change the result */ + demerit = max ((demerit + 20000), 2000.0); + + demerit *= 10; + } + + return demerit; }