]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/gourlay-breaking.cc
release: 1.5.4
[lilypond.git] / lily / gourlay-breaking.cc
index 533092aeeee15c6ce705d8e1cf96224b8c771d53..04112971d318513083b415e3f3373c949da1f243 100644 (file)
@@ -3,8 +3,9 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
+#include <math.h>              // rint
 
 #include "gourlay-breaking.hh"
 #include "column-x-positions.hh"
@@ -13,8 +14,7 @@
 #include "paper-score.hh"
 #include "paper-def.hh"
 #include "simple-spacer.hh"
-
-#include "killing-cons.tcc"
+#include "line-of-score.hh"
 
 /// How often to print operator pacification marks?
 const int HAPPY_DOTS_I = 3;
@@ -40,26 +40,28 @@ struct Break_node {
   {
     prev_break_i_ = -1;
     line_i_ = 0;
+    demerits_f_ = 0;
   }
 };
 
 /**
   This algorithms is adapted from the OSU Tech report on breaking lines.
+
+  this function is longish, but not very complicated.
+  
  */
 Array<Column_x_positions>
 Gourlay_breaking::do_solve () const
 {
   Array<Break_node> optimal_paths;
-  Line_of_cols all = pscore_l_->col_l_arr_ ;
+  Link_array<Grob> all =
+    pscore_l_->line_l_->column_l_arr ();
+  
   Array<int> breaks = find_break_indices ();
   
   optimal_paths.set_size (breaks.size ());
 
   Break_node first_node ;
-  first_node.prev_break_i_ = -1;
-  first_node.line_config_.force_f_ = 0;
-  first_node.line_config_.energy_f_ = 0;  
-  first_node.line_i_ = 0;
   
   optimal_paths[0] = first_node; 
   int break_idx=1;
@@ -72,28 +74,16 @@ Gourlay_breaking::do_solve () const
        */
       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--;)
        {
-#if 0
-         if  (break_idx - start_idx > max_measures_i_) 
-           break;
-#endif
-         
-         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);
+         Link_array<Grob> line = all.slice (breaks[start_idx], breaks[break_idx]+1);
   
-         line[0] = dynamic_cast<Paper_column*>(line[0]->find_broken_piece (RIGHT));
-         line.top () =  dynamic_cast<Paper_column*>(line.top ()->find_broken_piece (LEFT));
-           
-         if (!feasible (line))
-           break;
+         line[0]     = dynamic_cast<Item*> (line[0])    ->find_prebroken_piece (RIGHT);
+         line.top () = dynamic_cast<Item*> (line.top ())->find_prebroken_piece (LEFT);
            
          Column_x_positions cp;
          cp.cols_ = line;
@@ -103,13 +93,19 @@ Gourlay_breaking::do_solve () const
          Simple_spacer * sp = generate_spacing_problem (line, line_dims);
          sp->solve (&cp);
          delete sp;
-         
+
+         if (start_idx == break_idx - 1)
+           backup_sol = cp;    // in case everything fucks up
          if (!cp.satisfies_constraints_b_)
            break;
 
-         Real this_demerits 
-           = combine_demerits (optimal_paths[start_idx].line_config_, cp)
-           + optimal_paths[start_idx].demerits_f_;
+         
+         Real this_demerits;
+         if (optimal_paths[start_idx].demerits_f_ >= infinity_f)
+           this_demerits = infinity_f;
+         else
+           this_demerits = combine_demerits (optimal_paths[start_idx].line_config_, cp)
+             + optimal_paths[start_idx].demerits_f_;
 
          if (this_demerits < minimal_demerits) 
            {
@@ -119,29 +115,31 @@ Gourlay_breaking::do_solve () const
            }
        }
 
+      int prev =break_idx - 1;
       if (minimal_start_idx < 0) 
        {
-         optimal_paths[break_idx].prev_break_i_ = -1;
-         optimal_paths[break_idx].line_config_.energy_f_ = infinity_f;
+         optimal_paths[break_idx].demerits_f_ = infinity_f;
+         optimal_paths[break_idx].line_config_ = backup_sol;     
        }
       else 
        {
-         optimal_paths[break_idx].prev_break_i_ = minimal_start_idx;
+         prev = minimal_start_idx;
          optimal_paths[break_idx].line_config_ = minimal_sol;
          optimal_paths[break_idx].demerits_f_ = minimal_demerits;
-         optimal_paths[break_idx].line_i_ =
-           optimal_paths[minimal_start_idx].line_i_ + 1;
        }
+      optimal_paths[break_idx].prev_break_i_ = prev;
+      optimal_paths[break_idx].line_i_ = optimal_paths[prev].line_i_ + 1;
 
       if (! (break_idx % HAPPY_DOTS_I))
-       *mlog << "[" << break_idx << "]" << flush;
+       progress_indication (String ("[") + to_str (break_idx) + "]");
     }
 
   /* do the last one */
-  if  (break_idx % HAPPY_DOTS_I) 
-    *mlog << "[" << break_idx << "]";
+  if (break_idx % HAPPY_DOTS_I)
+    progress_indication (String ("[") + to_str (break_idx) + "]");    
+
 
-  *mlog << endl;
+  progress_indication ("\n");
 
   Array<int> final_breaks;
   Array<Column_x_positions> lines;
@@ -150,17 +148,14 @@ Gourlay_breaking::do_solve () const
   for (int i = optimal_paths.size ()-1; i> 0;) 
     {
       final_breaks.push (i);
-      assert (i > optimal_paths[i].prev_break_i_);
-
-      // there was no "feasible path"
-      if (!optimal_paths[i].line_config_.config_.size ()) {
-       final_breaks.set_size (0);
-       break;
-      }
-      i = optimal_paths[i].prev_break_i_;
+      int prev = optimal_paths[i].prev_break_i_;
+      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_);
   
@@ -170,15 +165,8 @@ Gourlay_breaking::do_solve () const
 
 Gourlay_breaking::Gourlay_breaking ()
 {
-  energy_bound_f_ = infinity_f;
-  max_measures_i_ = INT_MAX;
 }
 
-void
-Gourlay_breaking::do_set_pscore ()
-{
-  max_measures_i_ =int (rint (pscore_l_->paper_l_->get_var ("gourlay_maxmeasures")));
-}
 
 
 /*
@@ -188,5 +176,18 @@ Real
 Gourlay_breaking::combine_demerits (Column_x_positions const &prev,
                                    Column_x_positions const &this_one) const
 {
-  return abs (this_one.force_f_) + abs (prev.force_f_ - this_one.force_f_);
+  Real break_penalties = 0.0;
+  Grob * pc = this_one.cols_.top ();
+  if (pc->original_l_)
+    {
+      SCM pen = pc->get_grob_property ("penalty");
+      if (gh_number_p (pen))
+       {
+         break_penalties += gh_scm2double (pen);
+       }
+    }
+
+  return abs (this_one.force_f_) + abs (prev.force_f_ - this_one.force_f_)
+    + break_penalties;
 }
+