]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/slur-scoring.cc
Use queue for prioritizing slur scores.
[lilypond.git] / lily / slur-scoring.cc
index ba8f5c80ccd52ba9d6c0a1a85a64ef2c4bee3058..551d4b4524e0e1946d11e74fe9aa7f00463b567a 100644 (file)
@@ -1,15 +1,28 @@
 /*
-  slur-scoring.cc -- Score based slur formatting
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
-
-  (c) 1996--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Copyright (C) 1996--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
   Jan Nieuwenhuizen <janneke@gnu.org>
+
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 
 #include "slur-scoring.hh"
 
+#include <queue>
+
 #include "accidental-interface.hh"
 #include "beam.hh"
 #include "directional-element-interface.hh"
@@ -66,51 +79,25 @@ Slur_score_state::~Slur_score_state ()
   junk_pointers (configurations_);
 }
 
-Real
-broken_trend_y (Slur_score_state const &state, Direction hdir)
+/*
+  If a slur is broken across a line break, the direction
+  of the post-break slur must be the same as the pre-break
+  slur.
+*/
+Direction
+Slur_score_state::slur_direction () const
 {
-  /* A broken slur should maintain the same vertical trend
-     the unbroken slur would have had.  */
-  Real by = 0.0;
-  if (Spanner *mother = dynamic_cast<Spanner *> (state.slur_->original ()))
-    {
-      Grob *neighbor = mother->broken_neighbor (hdir);
-      if (!neighbor)
-       return by;
+  Grob *left_neighbor = slur_->broken_neighbor (LEFT);
 
-      
-      Spanner *common_mother
-       = dynamic_cast<Spanner *> (state.common_[Y_AXIS]->original ());
-      int common_k
-       = broken_spanner_index (dynamic_cast<Spanner *> (state.common_[Y_AXIS]));
-      int common_j = common_k + hdir;
+  if (left_neighbor && left_neighbor->is_live ())
+    return get_grob_direction (left_neighbor);
 
-      if (common_j < 0 || vsize (common_j) >= common_mother->broken_intos_.size ())
-       return by;
+  Direction dir = get_grob_direction (slur_);
 
-      Grob *common_next_system = common_mother->broken_intos_[common_j];
+  if (Grob *right_neighbor = slur_->broken_neighbor (RIGHT))
+    set_grob_direction (right_neighbor, dir);
 
-      SCM last_point = scm_car (scm_last_pair (neighbor->get_property ("control-points")));
-
-      return scm_to_double (scm_cdr (last_point))
-       + neighbor->relative_coordinate (common_next_system, Y_AXIS);
-    }
-  return by;
-}
-
-/*
-  copy slur dir forwards across line break.
-*/
-void
-Slur_score_state::set_next_direction ()
-{
-  if (extremes_[RIGHT].note_column_)
-    return;
-
-  if (Grob *neighbor = slur_->broken_neighbor (RIGHT))
-    {
-      set_grob_direction (neighbor, dir_);
-    }
+  return dir;
 }
 
 Encompass_info
@@ -148,7 +135,7 @@ Slur_score_state::get_encompass_info (Grob *col) const
     {
       ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
       if (Grob *b = Stem::get_beam (stem))
-       ei.stem_ += stem_dir * 0.5 * Beam::get_thickness (b);
+       ei.stem_ += stem_dir * 0.5 * Beam::get_beam_thickness (b);
 
       Interval x = stem->extent (common_[X_AXIS], X_AXIS);
       ei.x_ = x.is_empty ()
@@ -224,18 +211,19 @@ Slur_score_state::fill (Grob *me)
   slur_ = dynamic_cast<Spanner *> (me);
   columns_
     = internal_extract_grob_array (me, ly_symbol2scm ("note-columns"));
-
+  
   if (columns_.empty ())
     {
       me->suicide ();
       return;
     }
 
+  Slur::replace_breakable_encompass_objects (me);
   staff_space_ = Staff_symbol_referencer::staff_space (me);
   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
   thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
 
-  dir_ = get_grob_direction (me);
+  dir_ = slur_direction ();
   parameters_.fill (me);
 
   extract_grob_set (me, "note-columns", columns);
@@ -297,14 +285,12 @@ Slur_score_state::fill (Grob *me)
     = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
     || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
 
-  set_next_direction ();
-
   if (is_broken_)
     musical_dy_ = 0.0;
 }
 
 
-MAKE_SCHEME_CALLBACK(Slur, calc_control_points, 1)
+MAKE_SCHEME_CALLBACK (Slur, calc_control_points, 1)
 SCM
 Slur::calc_control_points (SCM smob)
 {
@@ -319,17 +305,31 @@ Slur::calc_control_points (SCM smob)
   state.generate_curves ();
 
   SCM end_ys = me->get_property ("positions");
-  Bezier best;
-
+  SCM inspect_quants = me->get_property ("inspect-quants");
+  if (is_number_pair (inspect_quants))
+    end_ys = inspect_quants;
+  
+  Slur_configuration *best = NULL;
   if (is_number_pair (end_ys))
-    best = state.configurations_[state.get_closest_index (end_ys)]->curve_;
+    best = state.get_forced_configuration (ly_scm2interval(end_ys));
   else
     best = state.get_best_curve ();
 
+#if DEBUG_SLUR_SCORING
+  bool debug_slurs = to_boolean (me->layout ()
+                                ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")));
+  if (debug_slurs)
+    {
+      string total = best->card ();
+      total += to_string (" TOTAL=%.2f idx=%d", best->score (), best->index_); 
+      me->set_property ("quant-score", ly_string2scm (total));
+    }
+#endif
+  
   SCM controls = SCM_EOL;
   for (int i = 4; i--;)
     {
-      Offset o = best.control_[i]
+      Offset o = best->curve_.control_[i]
        - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
                  me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
       controls = scm_cons (ly_offset2scm (o), controls);
@@ -338,72 +338,52 @@ Slur::calc_control_points (SCM smob)
   return controls;
 }
 
-Bezier
-Slur_score_state::get_best_curve ()
+Slur_configuration*
+Slur_score_state::get_forced_configuration (Interval ys) const
 {
-  int opt_idx = -1;
-  Real opt = 1e6;
-
-#if DEBUG_SLUR_SCORING
-  bool debug_slurs = to_boolean (slur_->layout ()
-                                ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")));
-  SCM inspect_quants = slur_->get_property ("inspect-quants");
-  SCM inspect_index = slur_->get_property ("inspect-index");
-  if (debug_slurs
-      && scm_is_integer (inspect_index))
-    {
-      opt_idx = scm_to_int (inspect_index);
-      configurations_[opt_idx]->calculate_score (*this);
-      opt = configurations_[opt_idx]->score ();
-    }
-  else if (debug_slurs
-          && scm_is_pair (inspect_quants))
-    {
-      opt_idx = get_closest_index (inspect_quants);
-      configurations_[opt_idx]->calculate_score (*this);
-      opt = configurations_[opt_idx]->score ();
-    }
-  else
-#endif
+  Slur_configuration *best = NULL;
+  Real mindist = 1e6;
+  for (vsize i = 0; i < configurations_.size (); i++)
     {
-      for (vsize i = 0; i < configurations_.size (); i++)
-       configurations_[i]->calculate_score (*this);
-      for (vsize i = 0; i < configurations_.size (); i++)
+      Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ys[LEFT])
+       + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ys[RIGHT]);
+      if (d < mindist)
        {
-         if (configurations_[i]->score () < opt)
-           {
-             opt = configurations_[i]->score ();
-             opt_idx = i;
-           }
+         best = configurations_[i];
+         mindist = d;
        }
     }
 
-#if DEBUG_SLUR_SCORING
-  if (debug_slurs)
-    {
-      string total;
-      if (opt_idx >= 0)
-       {
-         total = configurations_[opt_idx]->card ();
-         total += to_string (" TOTAL=%.2f idx=%d", configurations_[opt_idx]->score (), opt_idx); 
-       }
-      else
-       {
-         total = "no sol?";
-       }
+  while (!best->done ())
+    best->run_next_scorer (*this);
   
-      slur_->set_property ("quant-score",
-                          scm_makfrom0str (total.c_str ()));
-    }
-#endif
+  if (mindist > 1e5)
+    programming_error ("cannot find quant");
 
-  if (opt_idx < 0)
-    {
-      opt_idx = 0;
-      programming_error ("No optimal slur found. Guessing 0.");
-    }
-  
-  return configurations_[opt_idx]->curve_;
+  return best;
+}
+
+
+Slur_configuration *
+Slur_score_state::get_best_curve () const
+{
+  std::priority_queue<Slur_configuration*, std::vector<Slur_configuration*>,
+                      Slur_configuration_less> queue;
+  for (vsize i = 0; i < configurations_.size (); i++)
+    queue.push (configurations_[i]);
+
+  Slur_configuration *best = NULL;
+  while (true) {
+    best = queue.top ();
+    if (best->done ())
+      break;
+    
+    queue.pop ();
+    best->run_next_scorer (*this);
+    queue.push (best);
+  }
+
+  return best;
 }
 
 Grob *
@@ -423,28 +403,6 @@ Slur_score_state::breakable_bound_item (Direction d) const
   return 0;
 }
 
-int
-Slur_score_state::get_closest_index (SCM inspect_quants) const
-{
-  Drul_array<Real> ins = ly_scm2interval (inspect_quants);
-
-  int opt_idx = -1;
-  Real mindist = 1e6;
-  for (vsize i = 0; i < configurations_.size (); i++)
-    {
-      Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ins[LEFT])
-       + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
-      if (d < mindist)
-       {
-         opt_idx = i;
-         mindist = d;
-       }
-    }
-  if (mindist > 1e5)
-    programming_error ("can't not find quant");
-  return opt_idx;
-}
-
 /*
   TODO: should analyse encompasses to determine sensible region, and
   should limit slopes available.
@@ -652,7 +610,7 @@ Slur_score_state::generate_avoid_offsets () const
 
          if (!xe.is_empty ()
              && !ye.is_empty ())
-           avoid.push_back (Offset (xe.center(), ye[dir_]));
+           avoid.push_back (Offset (xe.center (), ye[dir_]));
        }
     }  
   return avoid;
@@ -684,7 +642,6 @@ Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
       os[RIGHT] = base_attachments_[RIGHT];
       for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
        {
-         Slur_configuration s;
          Direction d = LEFT;
          Drul_array<bool> attach_to_stem (false, false);
          do
@@ -745,10 +702,7 @@ Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
            }
          while (flip (&d) != LEFT);
 
-         s.attachment_ = os;
-         s.index_ = scores.size ();
-
-         scores.push_back (new Slur_configuration (s));
+         scores.push_back (Slur_configuration::new_config (os, scores.size ()));
 
          os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
        }
@@ -814,34 +768,21 @@ Slur_score_state::get_extra_encompass_infos () const
          if (Accidental_interface::has_interface (g))
            {
              penalty = parameters_.accidental_collision_;
-             /* Begin copy accidental.cc */
-             bool parens = false;
-             if (to_boolean (g->get_property ("cautionary")))
-               {
-                 SCM cstyle = g->get_property ("cautionary-style");
-                 parens = ly_is_equal (cstyle, ly_symbol2scm ("parentheses"));
-               }
 
-             SCM accs = g->get_property ("accidentals");
+             Rational alt = ly_scm2rational (g->get_property ("alteration"));
              SCM scm_style = g->get_property ("style");
              if (!scm_is_symbol (scm_style)
-                 && !parens
-                 && scm_ilength (accs) == 1)
+                 && !to_boolean (g->get_property ("parenthesized"))
+                 && !to_boolean (g->get_property ("restore-first")))
                {
                  /* End copy accidental.cc */
-                 switch (scm_to_int (scm_car (accs)))
-                   {
-                   case FLAT:
-                   case DOUBLE_FLAT:
-                     xp = LEFT;
-                     break;
-                   case SHARP:
-                     xp = 0.5 * dir_;
-                     break;
-                   case NATURAL:
-                     xp = -dir_;
-                     break;
-                   }
+                 if (alt == FLAT_ALTERATION
+                     || alt == DOUBLE_FLAT_ALTERATION)
+                   xp = LEFT;
+                 else if (alt == SHARP_ALTERATION)
+                   xp = 0.5 * dir_;
+                 else if (alt == NATURAL_ALTERATION)
+                   xp = -dir_;
                }
            }