]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/gourlay-breaking.cc
2003 -> 2004
[lilypond.git] / lily / gourlay-breaking.cc
index 85533c517b02ad5b79d122bfa492898fc8d900d5..8f7b0ec9f896776a7474519568ffa2e04b6c2353 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
+#include <math.h>              // rint
+#include <stdio.h>
 
 #include "gourlay-breaking.hh"
-#include "colhpos.hh"
-#include "spring-spacer.hh"
-#include "debug.hh"
-#include "p-col.hh"
-#include "p-score.hh"
+#include "column-x-positions.hh"
+#include "warn.hh"
+#include "main.hh"
+#include "paper-column.hh"
+#include "paper-score.hh"
 #include "paper-def.hh"
+#include "simple-spacer.hh"
+#include "system.hh"
 
-
-const HAPPY_DOTS_I = 3;
+/// How often to print operator pacification marks?
+const int HAPPY_DOTS_I = 3;
 
 /**
   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_;
-    Real energy_f_;
-    Col_hpositions line_config_;
-    Break_node() {
-       prev_break_i_ = -1;
-    }
+  /** this was the previous. If negative, this break should not be
+    considered: this path has infinite energy
+    
+    */
+  int prev_break_;
+  /**
+     Which system number so far?
+   */
+  int line_;
+
+  Real demerits_;
+  Column_x_positions line_config_;
+  
+  Break_node () 
+  {
+    prev_break_ = -1;
+    line_ = 0;
+    demerits_ = 0;
+  }
+
+  void print () const
+  {
+    printf ("prev break %d, line %d, demerits %f\n",
+           prev_break_, line_, demerits_);
+  }
 };
 
+void
+print_break_nodes (Array<Break_node> const & arr)
+{
+  for (int i =0; i < arr.size (); i++)
+    {
+      printf ( "node %d: ", i); 
+      arr[i].print ();
+    }      
+}
+
 /**
-  This algorithms is adapted from 
- */
+  This algorithms is adapted from the OSU Tech report on breaking lines.
 
-Array<Col_hpositions>
-Gourlay_breaking::do_solve()const
+  this function is longish, but not very complicated.
+  
+ */
+Array<Column_x_positions>
+Gourlay_breaking::do_solve () const
 {
+  Array<Break_node> optimal_paths;
+  Link_array<Grob> all =
+    pscore_->system_->columns ();
+  
+  Array<int> breaks = find_break_indices ();
+  
+  Break_node first_node ;
+  optimal_paths.push (first_node);
 
-    Array<Break_node> optimal_paths;
-    Line_of_cols all = all_cols();
-    Array<int> breaks = find_break_indices();
-    
-    optimal_paths.set_size (breaks.size());
+  Real worst_force = 0.0;
+  
+  for (int 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 
+       */
+      int minimal_start_idx = -1;
+      Column_x_positions minimal_sol;
+      Column_x_positions backup_sol;
+      
+      Real minimal_demerits = infinity_f;
 
-    Break_node first_node ;
-    first_node.prev_break_i_ = -1;
-    first_node.line_config_.energy_f_ = 0;
-    
-    optimal_paths[0] = first_node; 
-    int break_idx=1;
+      bool ragged = to_boolean (pscore_->paper_->get_scmvar ("raggedright"));
 
+      for (int start_idx = break_idx; start_idx--;)
+       {
+         Link_array<Grob> line = all.slice (breaks[start_idx], breaks[break_idx]+1);
   
-    for (; break_idx< breaks.size(); break_idx++) {
-       Array<int> candidates;
-       Array<Col_hpositions> candidate_lines;
-       Pointer_list<Line_spacer*> spacer_p_list;
-       
-       /*
-         start with a short line, add measures. At some point 
-         the line becomes infeasible. Then we don't try to add more 
-        */
-       for (int start_idx = break_idx; start_idx--;){
-           if  (break_idx - start_idx > max_measures_i_) 
-               break;
-
-           if (optimal_paths[start_idx].prev_break_i_ < 0
-               && optimal_paths[start_idx].line_config_.energy_f_)
-               
-               continue;
-           
-           Line_of_cols line = all.slice (breaks[start_idx], breaks[break_idx]+1);
-           
-           line[0] = line[0]->postbreak_p_;
-           line.top() = line.top ()->prebreak_p_;
-           
-           if (!feasible (line))
-               break;
-           
-           Col_hpositions approx;
-           approx.cols = line;
+         line[0]     = dynamic_cast<Item*> (line[0])    ->find_prebroken_piece (RIGHT);
+         line.top () = dynamic_cast<Item*> (line.top ())->find_prebroken_piece (LEFT);
            
-           approx.spacer_l_ = generate_spacing_problem (line);
-           spacer_p_list.bottom().add (approx.spacer_l_);
+         Column_x_positions cp;
+         cp.cols_ = line;
 
-           ((Break_algorithm*)this)->approx_stats_.add (approx.cols);
-           approx.approximate_solve_line();
-           
-           if  (approx.energy_f_  > energy_bound_f_){ 
-               continue;
-           }
+         Interval line_dims
+           = pscore_->paper_->line_dimensions_int (optimal_paths[start_idx].line_);
+         Simple_spacer * sp = generate_spacing_problem (line, line_dims);
+         sp->solve (&cp, ragged);
+         delete sp;
 
-           
-           // this is a likely candidate. Store it.
-           candidate_lines.push (approx);
-           candidates.push (start_idx);
-       }
+         if (fabs (cp.force_) > worst_force)
+           worst_force = fabs (cp.force_);
 
-           
-       int minimal_j = -1;
-       Real minimal_energy = infinity_f;
-       for (int j=0; j < candidates.size(); j++) {
-           int start = candidates[j];
-           if ( optimal_paths[start].line_config_.energy_f_
-                + candidate_lines[j].energy_f_  > minimal_energy)
-               
-               continue;
-
-           if ( !candidate_lines[j].satisfies_constraints_b_) {
-                candidate_lines[j].solve_line();
-                ((Break_algorithm*)this)->exact_stats_.add ( candidate_lines[j].cols);
-           }
-           
-           Real this_energy 
-               = optimal_paths[start].line_config_.energy_f_ 
-               + candidate_lines[j].energy_f_ ;
-           
-           if ( this_energy < minimal_energy) {
-               minimal_j = j;
-               minimal_energy = this_energy;
+         /*
+           We remember this solution as a "should always work
+           solution", in case everything fucks up.  */
+         if (start_idx == break_idx - 1)
+           backup_sol = cp;
+                 
+         Real this_demerits;
+
+         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_;
+
+         if (this_demerits < minimal_demerits) 
+           {
+             minimal_start_idx = start_idx;
+             minimal_sol = cp;
+             minimal_demerits = this_demerits;
            }
-       }
 
-       if (minimal_j < 0) {
-           optimal_paths[break_idx].prev_break_i_ = -1;
-           optimal_paths[break_idx].line_config_.energy_f_ = infinity_f;
-       } else {
-           optimal_paths[break_idx].prev_break_i_ = candidates[minimal_j];
-           optimal_paths[break_idx].line_config_ = candidate_lines[minimal_j];
+         /*
+           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_b_)
+           break ; 
        }
 
-       if ( !(break_idx % HAPPY_DOTS_I))
-           *mlog << "[" << break_idx << "]"<<flush;
-    }
 
-    if  ( break_idx % HAPPY_DOTS_I) 
-       *mlog << "[" << break_idx << "]"<<flush;
-    print_stats();
+      Break_node bnod;
+      if (minimal_start_idx < 0) 
+       {
+         bnod.demerits_ = infinity_f;
+         bnod.line_config_ = backup_sol;
+         bnod.prev_break_ = break_idx - 1;       
+       }
+      else 
+       {
+         bnod.prev_break_ = minimal_start_idx;
+         bnod.demerits_ = minimal_demerits;
+         bnod.line_config_ = minimal_sol;
+       }
+      bnod.line_ = optimal_paths[bnod.prev_break_].line_ + 1;
+      optimal_paths.push (bnod);
+      
+      if (! (break_idx % HAPPY_DOTS_I))
+       progress_indication (String ("[") + to_string (break_idx) + "]");
+    }
 
-    
+  /* do the last one */
+  if (breaks.size () % HAPPY_DOTS_I)
+    progress_indication (String ("[") + to_string (breaks.size()) + "]");    
 
-    Array<int> final_breaks;
+  progress_indication ("\n");
 
-    Array<Col_hpositions> lines;
-    /* skip 0-th element, since it is a "dummy" elt*/
-    for (int i = optimal_paths.size()-1; i> 0;) {
-       final_breaks.push ( i);
-       assert ( i > optimal_paths[i].prev_break_i_);
+  Array<int> final_breaks;
+  Array<Column_x_positions> lines;
 
-       // there was no "feasible path"
-       if (!optimal_paths[i].line_config_.config.size())
-          return lines; 
-       i = optimal_paths[i].prev_break_i_;
+  /* skip 0-th element, since it is a "dummy" elt*/
+  for (int i = optimal_paths.size ()-1; i> 0;) 
+    {
+      final_breaks.push (i);
+      int prev = optimal_paths[i].prev_break_;
+      assert (i > prev);
+      i = prev;
     }
 
-    for (int i= final_breaks.size(); i--;) 
-       lines.push ( optimal_paths[final_breaks[i]].line_config_);
-
-    
-    return lines;
+  if (verbose_global_b)
+    {
+      progress_indication (_f ("Optimal demerits: %f",
+                              optimal_paths.top ().demerits_) + "\n");
+    }
+  
+  if (optimal_paths.top ().demerits_ >= infinity_f)
+    warning (_ ("No feasible line breaking found"));
+  
+  for (int i= final_breaks.size (); i--;)
+    {
+      Column_x_positions cp (optimal_paths[final_breaks[i]].line_config_);
+      
+      lines.push (cp);
+      if(!cp.satisfies_constraints_b_)
+       warning ("Could not find line breaking that satisfies constraints.");
+    }
+  return lines;
 }
 
 
-Gourlay_breaking::Gourlay_breaking()
+Gourlay_breaking::Gourlay_breaking ()
 {
-    get_line_spacer = Spring_spacer::constructor;
-    energy_bound_f_ = infinity_f;
-    max_measures_i_ = INT_MAX;
 }
 
-void
-Gourlay_breaking::do_set_pscore()
+
+
+/*
+  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
 {
-    energy_bound_f_ = pscore_l_->paper_l_->get_var ("gourlay_energybound");
-    max_measures_i_ =int (rint (pscore_l_->paper_l_->get_var ("gourlay_maxmeasures")));
+  Real break_penalties = 0.0;
+  Grob * pc = this_one.cols_.top ();
+  if (pc->original_)
+    {
+      SCM pen = pc->get_grob_property ("penalty");
+      if (gh_number_p (pen) && fabs (gh_scm2double (pen)) < 10000)
+       {
+         break_penalties += gh_scm2double (pen);
+       }
+    }
+
+  /*
+    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_b_)
+     {
+       /*
+        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 = (demerit + 20000) >? 2000;
+       
+       demerit *= 10;
+     }
+
+   return demerit;
 }