]> git.donarmstrong.com Git - lilypond.git/commitdiff
optimise skylines.
authorJoe Neeman <joeneeman@gmail.com>
Sun, 7 Jan 2007 17:45:05 +0000 (19:45 +0200)
committerJoe Neeman <joeneeman@gmail.com>
Sun, 7 Jan 2007 17:45:05 +0000 (19:45 +0200)
cleaner solution for accidental::pure-height

buildscripts/build-profile.sh
lily/accidental.cc
lily/grob.cc
lily/include/accidental-interface.hh
lily/include/skyline.hh
lily/skyline.cc
scm/define-grobs.scm

index ee85ec77732004281d35bace7662036639d302c1..4c53c644e49437cbc956bc36c10aa53484c92369 100755 (executable)
@@ -29,12 +29,15 @@ mkdir $resultdir
 cd $resultdir
 
 
-echo 'foo = \new Staff \new Voice \repeat unfold 50 \relative { c4 d8[ d16( e]~ e16[ e e) f] g8  }
+cat > long-score.ly << EOF
+\version "2.10.0"
+foo = \new Staff \new Voice \repeat unfold 50 \relative { c4 d8[ d16( e]~ e16[ e e) f] g8  }
 \new ChoirStaff << 
   \foo \foo \foo \foo 
   \foo \foo \foo \foo 
 
->>' > long-score.ly
+>>
+EOF
 
 rm gmon.sum
 
index ba352e5b84032a315795f68643fcdd3e52d10299..1486c3f2cc113105c3f8c034a795afc2ee915415 100644 (file)
@@ -50,6 +50,15 @@ Accidental_interface::after_line_breaking (SCM smob)
   return SCM_UNSPECIFIED;
 }
 
+/* This callback exists for the sole purpose of allowing us to override
+   its pure equivalent to accidental-interface::pure-height */
+MAKE_SCHEME_CALLBACK (Accidental_interface, height, 1);
+SCM
+Accidental_interface::height (SCM smob)
+{
+  return Grob::stencil_height (smob);
+}
+
 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
 SCM
 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
index 0738e80869fa17a73063d7f8cc033979834f2990..0d6898a1219452517368ecdc94b09fc3e74cff1e 100644 (file)
@@ -430,19 +430,9 @@ Interval
 Grob::pure_height (Grob *refp, int start, int end)
 {
   SCM proc = get_property_data (ly_symbol2scm ("Y-extent"));
-  SCM pure_proc = get_property_data (ly_symbol2scm ("pure-Y-extent"));
-  SCM iv_scm;
-
-  if (ly_is_procedure (pure_proc))
-    iv_scm = scm_apply_3 (pure_proc,
-                         self_scm (),
-                         scm_from_int (start),
-                         scm_from_int (end), SCM_EOL);
-  else
-    iv_scm = call_pure_function (proc,
-                                scm_list_1 (self_scm ()),
-                                start, end);
-  
+  SCM iv_scm = call_pure_function (proc,
+                                  scm_list_1 (self_scm ()),
+                                  start, end);
   Interval iv = robust_scm2interval (iv_scm, Interval (0, 0));
   Real offset = pure_relative_y_coordinate (refp, start, end);
 
@@ -645,7 +635,6 @@ ADD_INTERFACE (Grob,
               "outside-staff-horizontal-padding "
               "outside-staff-padding "
               "outside-staff-priority "
-              "pure-Y-extent "
               "rotation "
               "springs-and-rods "
               "staff-symbol "
index 2a2888c20affe6498d0165b3e30e66e31e11281d..daa4483a200d1a0f6663a61e0e093633a7ba1d8f 100644 (file)
@@ -21,6 +21,7 @@ public:
   DECLARE_SCHEME_CALLBACK (print, (SCM));
   DECLARE_SCHEME_CALLBACK (calc_stencils, (SCM));
   DECLARE_SCHEME_CALLBACK (after_line_breaking, (SCM));
+  DECLARE_SCHEME_CALLBACK (height, (SCM));
   DECLARE_SCHEME_CALLBACK (pure_height, (SCM, SCM, SCM));
   
   DECLARE_GROB_INTERFACE();
index ab8fe30c65dffd579ca0f72599fd4027749549df..c793927c8205ad563b85acd5032e2986414636e4 100644 (file)
@@ -38,6 +38,11 @@ struct Building
   bool conceals (Building const &other) const;
   bool sane () const;
   Building sloped_neighbour (Real horizon_padding, Direction d) const;
+
+  bool operator< (Building const &other)
+  {
+    return iv_[LEFT] < other.iv_[LEFT];
+  }
 };
 
 class Skyline
@@ -48,8 +53,7 @@ private:
   
   void internal_merge_skyline (list<Building>*, list<Building>*,
                               list<Building> *const result);
-  void internal_build_skyline (list<Building>*, list<Building> *const result);
-  bool is_legal_skyline () const;
+  list<Building> internal_build_skyline (list<Building>*);
 
   DECLARE_SIMPLE_SMOBS(Skyline);
 public:
index e61abbaea6bed28b05f7b3a3f69aa532d8952be5..c009608fc96d5b3d12d0c23671c9de112d0f583e 100644 (file)
@@ -6,6 +6,7 @@
 */
 
 #include "skyline.hh"
+#include <deque>
 
 #include "ly-smobs.icc"
 
@@ -108,12 +109,12 @@ Skyline::print () const
     }
 }
 
-bool
-Skyline::is_legal_skyline () const
+static bool
+is_legal_skyline (list<Building> const &buildings)
 {
   list<Building>::const_iterator i;
   Real last_x = -infinity_f;
-  for (i = buildings_.begin (); i != buildings_.end (); i++)
+  for (i = buildings.begin (); i != buildings.end (); i++)
     {
       if (i->iv_[LEFT] != last_x)
        return false;
@@ -325,34 +326,78 @@ single_skyline (Building b, Real horizon_padding, list<Building> *const ret)
                               -infinity_f, b.iv_[LEFT]));
 }
 
-void
-Skyline::internal_build_skyline (list<Building> *buildings, list<Building> *const result)
+/* remove a non-overlapping set of buildings from BUILDINGS and build a skyline
+   out of them */
+static list<Building>
+non_overlapping_skyline (list<Building> *const buildings)
+{
+  list<Building> result;
+  Real last_end = -infinity_f;
+  list<Building>::iterator i = buildings->begin ();
+  while (i != buildings->end ())
+    {
+      if (approx_less_than (i->iv_[LEFT], last_end))
+       {
+         i++;
+         continue;
+       }
+
+      if (approx_greater_than (i->iv_[LEFT], last_end))
+       result.push_back (Building (last_end, -infinity_f, -infinity_f, i->iv_[LEFT]));
+      else
+       i->iv_[LEFT] = last_end;
+
+      last_end = i->iv_[RIGHT];
+      list<Building>::iterator j = i;
+      i++;
+      result.splice (result.end (), *buildings, j);
+    }
+  if (last_end < infinity_f)
+    result.push_back (Building (last_end, -infinity_f, -infinity_f, infinity_f));
+  assert (is_legal_skyline (result));
+  return result;
+}
+
+list<Building>
+Skyline::internal_build_skyline (list<Building> *buildings)
 {
   vsize size = buildings->size ();
 
   if (size == 0)
     {
-      empty_skyline (result);
-      return;
+      list<Building> result;
+      empty_skyline (&result);
+      return result;
     }
   else if (size == 1)
     {
-      single_skyline (buildings->front (), 0, result);
-      return;
+      list<Building> result;
+      single_skyline (buildings->front (), 0, &result);
+      return result;
     }
 
-  list<Building> right_half;
-  list<Building>::iterator i = buildings->begin ();
+  deque<list<Building> > partials;
+  buildings->sort ();
+  while (!buildings->empty ())
+    partials.push_back (non_overlapping_skyline (buildings));
 
-  for (vsize s = 0; s < size/2; s++)
-    i++;
-  right_half.splice (right_half.end (), *buildings, i, buildings->end ());
-
-  list<Building> right;
-  list<Building> left;
-  internal_build_skyline (&right_half, &right);
-  internal_build_skyline (buildings, &left);
-  internal_merge_skyline (&right, &left, result);
+  /* we'd like to say while (partials->size () > 1) but that's O(n).
+     Instead, we exit in the middle of the loop */
+  while (!partials.empty ())
+    {
+      list<Building> merged;
+      list<Building> one = partials.front ();
+      partials.pop_front ();
+      if (partials.empty ())
+       return one;
+
+      list<Building> two = partials.front ();
+      partials.pop_front ();
+      internal_merge_skyline (&one, &two, &merged);
+      partials.push_back (merged);
+    }
+  assert (0);
+  return list<Building> ();
 }
 
 Skyline::Skyline ()
@@ -406,8 +451,8 @@ Skyline::Skyline (vector<Box> const &boxes, Real horizon_padding, Axis horizon_a
        }
     }
   
-  internal_build_skyline (&bldgs, &buildings_);
-  assert (is_legal_skyline ());
+  buildings_ = internal_build_skyline (&bldgs);
+  assert (is_legal_skyline (buildings_));
 }
 
 Skyline::Skyline (Box const &b, Real horizon_padding, Axis horizon_axis, Direction sky)
@@ -426,7 +471,7 @@ Skyline::merge (Skyline const &other)
   list<Building> my_bld;
   my_bld.splice (my_bld.begin (), buildings_);
   internal_merge_skyline (&other_bld, &my_bld, &buildings_);
-  assert (is_legal_skyline ());
+  assert (is_legal_skyline (buildings_));
 }
 
 void
@@ -438,7 +483,7 @@ Skyline::insert (Box const &b, Real horizon_padding, Axis a)
   my_bld.splice (my_bld.begin (), buildings_);
   single_skyline (Building (b, 0, a, sky_), horizon_padding, &other_bld);
   internal_merge_skyline (&other_bld, &my_bld, &buildings_);
-  assert (is_legal_skyline ());
+  assert (is_legal_skyline (buildings_));
 }
 
 void
@@ -451,7 +496,7 @@ Skyline::raise (Real r)
       i->height_[RIGHT] += sky_ * r;
       i->y_intercept_ += sky_ * r;
     }
-  assert (is_legal_skyline ());
+  assert (is_legal_skyline (buildings_));
 }
 
 void
index d3270748bbfa28263ee66322d2ce502ffa7df8aa..64f32c62724513692c05bb0b9fe45af9f919f82f 100644 (file)
@@ -23,7 +23,7 @@
        (glyph-name-alist . ,standard-alteration-glyph-name-alist)
        (alteration . ,accidental-interface::calc-alteration) 
        (stencil . ,ly:accidental-interface::print)
-       (pure-Y-extent . ,ly:accidental-interface::pure-height)
+       (Y-extent . ,ly:accidental-interface::height)
        (after-line-breaking
         . ,ly:accidental-interface::after-line-breaking)
        (meta . ((class . Item)
@@ -36,7 +36,7 @@
        (glyph-name-alist . ,standard-alteration-glyph-name-alist)
        (alteration . ,accidental-interface::calc-alteration) 
        (stencil . ,ly:accidental-interface::print)
-       (pure-Y-extent . ,ly:accidental-interface::pure-height)
+       (Y-extent . ,ly:accidental-interface::height)
        (after-line-breaking
         . ,ly:accidental-interface::after-line-breaking)
        (meta . ((class . Item)
@@ -47,6 +47,7 @@
     (AccidentalSuggestion
      . (
        (stencil . ,ly:accidental-interface::print)
+       (Y-extent . ,ly:accidental-interface::height)
        (X-offset . ,(ly:make-simple-closure
                      `(,+
                        ,(ly:make-simple-closure (list ly:self-alignment-interface::centered-on-x-parent))
        (X-offset . ,ly:side-position-interface::x-aligned-side)
        (direction . ,LEFT)
        (stencil . ,ly:accidental-interface::print)
-       (pure-Y-extent . ,ly:accidental-interface::pure-height)
+       (Y-extent . ,ly:accidental-interface::height)
        (glyph-name-alist . ,standard-alteration-glyph-name-alist)      
        (after-line-breaking . ,ly:accidental-interface::after-line-breaking)
        (side-axis . ,X)
        (font-size . -4)
        (side-axis . ,X)
        (stencil . ,ly:accidental-interface::print)
+       (Y-extent . ,ly:accidental-interface::height)
        (glyph-name-alist . ,standard-alteration-glyph-name-alist)
        (meta . ((class . Item)
                 (interfaces . (trill-pitch-accidental-interface
   (list
    ly:bar-line::print
    ly:note-head::print
-   ly:accidental-interface::print
    ly:dots::print
    ly:clef::print
    ly:text-interface::print
        '(0 . 0))))
 
 (define pure-conversions-alist
-  (list
-   `(,ly:slur::outside-slur-callback . ,ly:slur::pure-outside-slur-callback)
-   `(,ly:stem::height . ,ly:stem::pure-height)
-   `(,ly:rest::height . ,ly:rest::pure-height)
-   `(,ly:grob::stencil-height . ,pure-stencil-height)
-   `(,ly:side-position-interface::y-aligned-side . ,ly:side-position-interface::pure-y-aligned-side)
-   `(,ly:axis-group-interface::height . ,ly:axis-group-interface::pure-height)
-   `(,ly:hara-kiri-group-spanner::y-extent . ,ly:hara-kiri-group-spanner::pure-height)
-   `(,ly:slur::height . ,ly:slur::pure-height)
-   `(,ly:side-position-interface::y-aligned-side . ,ly:side-position-interface::pure-y-aligned-side)))
+  `(
+    (,ly:accidental-interface::height . ,ly:accidental-interface::pure-height)
+    (,ly:slur::outside-slur-callback . ,ly:slur::pure-outside-slur-callback)
+    (,ly:stem::height . ,ly:stem::pure-height)
+    (,ly:rest::height . ,ly:rest::pure-height)
+    (,ly:grob::stencil-height . ,pure-stencil-height)
+    (,ly:side-position-interface::y-aligned-side . ,ly:side-position-interface::pure-y-aligned-side)
+    (,ly:axis-group-interface::height . ,ly:axis-group-interface::pure-height)
+    (,ly:hara-kiri-group-spanner::y-extent . ,ly:hara-kiri-group-spanner::pure-height)
+    (,ly:slur::height . ,ly:slur::pure-height)
+    (,ly:side-position-interface::y-aligned-side . ,ly:side-position-interface::pure-y-aligned-side)))
 
 (define pure-functions
   (list