X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fskyline.cc;h=6e64a2c96b6983ba4fac58ebb05026536c058c1e;hb=5b4b0d6e9a197e8f9eb085b7c2ad78b8be3e5cfc;hp=2175685ee63de4691c84e6140ad47b2934d98f67;hpb=04e10efb0566620b4b7972d6775c8e59f04fa209;p=lilypond.git diff --git a/lily/skyline.cc b/lily/skyline.cc index 2175685ee6..6e64a2c96b 100644 --- a/lily/skyline.cc +++ b/lily/skyline.cc @@ -2,22 +2,23 @@ source file of the GNU LilyPond music typesetter - (c) 2006 Joe Neeman + (c) 2006--2008 Joe Neeman */ #include "skyline.hh" +#include -#include "line-interface.hh" +#include "ly-smobs.icc" /* A skyline is a sequence of non-overlapping buildings: something like this: _______ - / \ ________ - / \ ________/ \ - /\ / \ / \ - / -----/ \ / \ - / \ / \ - / ------------/ ---- + | \ ________ + | \ ________/ \ + /\ | \ / \ + / -------- \ / \ + / \ / \ + / ------------/ ---- -- Each building has a starting position, and ending position, a starting height and an ending height. @@ -41,144 +42,180 @@ but the distance routine does. */ -#define EPS 1e-10 +/* If we start including very thin buildings, numerical accuracy errors can + arise. Therefore, we ignore all buildings that are less than epsilon wide. */ +#define EPS 1e-5 -static inline bool -equal (Real x, Real y) +static void +print_buildings (list const &b) { - return abs (x - y) < EPS || (isinf (x) && isinf (y) && ((x > 0) == (y > 0))); + for (list::const_iterator i = b.begin (); i != b.end (); i++) + i->print (); } -bool -Skyline::is_legal_skyline () const +void +Skyline::print () const { - list::const_iterator i; - Real last_x = -infinity_f; - Real last_h = -infinity_f; - for (i = buildings_.begin (); i != buildings_.end (); i++) - { - if (i->iv_[LEFT] != last_x) - return false; - if (i != buildings_.begin () && !equal (i->start_height_, last_h)) - return false; - last_x = i->iv_[RIGHT]; - last_h = i->end_height_; - } - return last_x == infinity_f; + print_buildings (buildings_); } -Building::Building (Real start, Real start_height, Real end_height, - Real end, Real max_slope) - : iv_ (start, end) +void +Skyline::print_points () const { - start_height_ = start_height; - end_height_ = end_height; + vector ps (to_points (X_AXIS)); - if (isinf (start)) - assert (isinf (start_height) || start_height == end_height); - if (isinf (end)) - assert (isinf (end_height) || start_height == end_height); + for (vsize i = 0; i < ps.size (); i++) + printf ("(%f,%f)%s" , ps[i][X_AXIS], ps[i][Y_AXIS], + (i%2)==1 ? "\n" : " "); +} + +Building::Building (Real start, Real start_height, Real end_height, Real end) +{ + if (isinf (start) || isinf (end)) + assert (start_height == end_height); + + end_ = end; + precompute (start, start_height, end_height, end); +} - precompute (max_slope); +Building::Building (Box const &b, Real horizon_padding, Axis horizon_axis, Direction sky) +{ + Real start = b[horizon_axis][LEFT] - horizon_padding; + Real end = b[horizon_axis][RIGHT] + horizon_padding; + Real height = sky * b[other_axis (horizon_axis)][sky]; + + end_ = end; + precompute (start, height, height, end); } void -Building::precompute (Real max_slope) +Building::precompute (Real start, Real start_height, Real end_height, Real end) { - slope_ = (end_height_ - start_height_) / (iv_.length()); - if (start_height_ == end_height_) + slope_ = (end_height - start_height) / (end - start); + if (start_height == end_height) /* if they were both infinite, we would get nan, not 0, from the prev line */ slope_ = 0; - if (isinf (slope_) || isnan (slope_)) - slope_ = max_slope * (start_height_ < end_height_ ? 1 : -1); - assert (abs (slope_) <= max_slope); - if (isinf (iv_[START])) + assert (!isinf (slope_) && !isnan (slope_)); + + if (isinf (start)) { - if (isinf (iv_[STOP])) - zero_height_ = start_height_; - else - zero_height_ = end_height_ - slope_ * iv_[STOP]; + assert (start_height == end_height); + y_intercept_ = start_height; } else - zero_height_ = start_height_ - slope_ * iv_[START]; + y_intercept_ = start_height - slope_ * start; } Real Building::height (Real x) const { - if (isinf (x)) - return (x > 0) ? end_height_ : start_height_; - return slope_*x + zero_height_; + return isinf (x) ? y_intercept_ : slope_*x + y_intercept_; +} + +void +Building::print () const +{ + printf ("%f x + %f ends at %f\n", slope_, y_intercept_, end_); } Real -Building::intersection (Building const &other) const +Building::intersection_x (Building const &other) const { - return (zero_height_ - other.zero_height_) / (other.slope_ - slope_); + Real ret = (y_intercept_ - other.y_intercept_) / (other.slope_ - slope_); + return isnan (ret) ? -infinity_f : ret; } void -Building::leading_part (Real chop, Real h) +Building::leading_part (Real chop) { - assert (chop > iv_[LEFT] && chop <= iv_[RIGHT] && !equal (chop, iv_[LEFT])); - assert (equal (h, height (chop))); - iv_[RIGHT] = chop; - end_height_ = h; + assert (chop <= end_); + end_ = chop; } -static void -skyline_trailing_part (list *sky, Real x) +Building +Building::sloped_neighbour (Real start, Real horizon_padding, Direction d) const { - if (equal (x, sky->front ().iv_[RIGHT])) - sky->pop_front (); - else - assert (x < sky->front ().iv_[RIGHT]); + Real x = (d == LEFT) ? start : end_; + Real left = x; + Real right = x + d * horizon_padding; + Real left_height = height (x); + Real right_height = left_height - horizon_padding; + if (d == LEFT) + { + swap (left, right); + swap (left_height, right_height); + } + return Building (left, left_height, right_height, right); +} - if (!sky->empty ()) +static Real +first_intersection (Building const &b, list *const s, Real start_x) +{ + while (!s->empty () && start_x < b.end_) { - sky->front ().iv_[LEFT] = x; - sky->front ().start_height_ = sky->front ().height (x); + Building c = s->front (); + if (c.conceals (b, start_x)) + return start_x; + + Real i = b.intersection_x (c); + if (i > start_x && i <= b.end_ && i <= c.end_) + return i; + + start_x = c.end_; + if (b.end_ > c.end_) + s->pop_front (); } + return b.end_; } bool -Building::obstructs (Building const &other) const +Building::conceals (Building const &other, Real x) const { - if (equal (intersection (other), iv_[LEFT]) || equal (start_height_, other.start_height_)) - return slope_ > other.slope_ || (slope_ == other.slope_ && zero_height_ > other.zero_height_); - return start_height_ > other.start_height_; + if (slope_ == other.slope_) + return y_intercept_ > other.y_intercept_; + + /* their slopes were not equal, so there is an intersection point */ + Real i = intersection_x (other); + return (i <= x && slope_ > other.slope_) + || (i > x && slope_ < other.slope_); } void Skyline::internal_merge_skyline (list *s1, list *s2, list *const result) { + if (s1->empty () || s2->empty ()) + { + programming_error ("tried to merge an empty skyline"); + return; + } + + Real x = -infinity_f; while (!s1->empty ()) { - if (s2->front ().obstructs (s1->front ())) + if (s2->front ().conceals (s1->front (), x)) swap (s1, s2); Building b = s1->front (); - while (s2->front ().iv_[RIGHT] < b.iv_[RIGHT] - && s2->front ().end_height_ <= b.height (s2->front ().iv_[RIGHT]) + EPS) - s2->pop_front (); - - /* the front of s2 either intersects with b or it ends after b */ - Real end = infinity_f; - Real s2_end_height = s2->front ().end_height_; - Real s1_end_height = b.height (s2->front ().iv_[RIGHT]); - if (s2_end_height > s1_end_height + EPS) - end = b.intersection (s2->front ()); - end = min (end, b.iv_[RIGHT]); - Real height = b.height (end); - - b.leading_part (end, height); - result->push_front (b); - - skyline_trailing_part (s1, end); - if (!s1->empty ()) - s1->front ().start_height_ = height; - skyline_trailing_part (s2, end); + Real end = first_intersection (b, s2, x); + + if (s2->empty ()) + { + result->push_front (b); + break; + } + + /* only include buildings wider than epsilon */ + if (end > x + EPS) + { + b.leading_part (end); + result->push_front (b); + } + + if (end >= s1->front ().end_) + s1->pop_front (); + + x = end; } result->reverse (); } @@ -186,95 +223,183 @@ Skyline::internal_merge_skyline (list *s1, list *s2, static void empty_skyline (list *const ret) { - ret->push_front (Building (-infinity_f, -infinity_f, -infinity_f, infinity_f, 0)); + ret->push_front (Building (-infinity_f, -infinity_f, -infinity_f, infinity_f)); } static void -single_skyline (Building const &b, list *const ret, Real max_slope) +single_skyline (Building b, Real start, Real horizon_padding, list *const ret) { - if (!isinf (b.iv_[RIGHT])) - ret->push_front (Building (b.iv_[RIGHT], b.end_height_, -infinity_f, infinity_f, max_slope)); - ret->push_front (b); - if (!isinf (b.iv_[LEFT])) - ret->push_front (Building (-infinity_f, -infinity_f, b.start_height_, b.iv_[LEFT], max_slope)); + bool sloped_neighbours = horizon_padding > 0 && !isinf (start) && !isinf (b.end_); + if (!isinf (b.end_)) + ret->push_front (Building (b.end_ + horizon_padding, -infinity_f, + -infinity_f, infinity_f)); + if (sloped_neighbours) + ret->push_front (b.sloped_neighbour (start, horizon_padding, RIGHT)); + + if (b.end_ > start + EPS) + ret->push_front (b); + + if (sloped_neighbours) + ret->push_front (b.sloped_neighbour (start, horizon_padding, LEFT)); + + if (!isinf (start)) + ret->push_front (Building (-infinity_f, -infinity_f, + -infinity_f, start - horizon_padding)); } -void -Skyline::internal_build_skyline (list *buildings, list *const result) +/* remove a non-overlapping set of boxes from BOXES and build a skyline + out of them */ +static list +non_overlapping_skyline (list *const boxes, Real horizon_padding, Axis horizon_axis, Direction sky) +{ + list result; + Real last_end = -infinity_f; + list::iterator i = boxes->begin (); + while (i != boxes->end ()) + { + Interval iv = (*i)[horizon_axis]; + + if (iv[LEFT] - horizon_padding < last_end) + { + i++; + continue; + } + + if (iv[LEFT] - horizon_padding > last_end + EPS) + result.push_front (Building (last_end, -infinity_f, -infinity_f, iv[LEFT] - 2*horizon_padding)); + + Building b (*i, horizon_padding, horizon_axis, sky); + bool sloped_neighbours = horizon_padding > 0 && !isinf (iv.length ()); + if (sloped_neighbours) + result.push_front (b.sloped_neighbour (iv[LEFT] - horizon_padding, horizon_padding, LEFT)); + result.push_front (b); + if (sloped_neighbours) + result.push_front (b.sloped_neighbour (iv[LEFT] - horizon_padding, horizon_padding, RIGHT)); + + list::iterator j = i++; + boxes->erase (j); + last_end = result.front ().end_; + } + if (last_end < infinity_f) + result.push_front (Building (last_end, -infinity_f, -infinity_f, infinity_f)); + result.reverse (); + return result; +} + +class LessThanBox +{ + Axis a_; + +public: + LessThanBox (Axis a) + { + a_ = a; + } + + bool operator() (Box const &b1, Box const &b2) + { + return b1[a_][LEFT] < b2[a_][LEFT]; + } +}; + +list +Skyline::internal_build_skyline (list *boxes, Real horizon_padding, Axis horizon_axis, Direction sky) { - vsize size = buildings->size (); + vsize size = boxes->size (); if (size == 0) { - empty_skyline (result); - return; + list result; + empty_skyline (&result); + return result; } else if (size == 1) { - single_skyline (buildings->front (), result, max_slope_); - return; + list result; + single_skyline (Building (boxes->front (), horizon_padding, horizon_axis, sky), + boxes->front ()[horizon_axis][LEFT], horizon_axis, &result); + return result; } - list right_half; - list::iterator i = buildings->begin (); - - for (vsize s = 0; s < size/2; s++) - i++; - right_half.splice (right_half.end (), *buildings, i, buildings->end ()); + deque > partials; + boxes->sort (LessThanBox (horizon_axis)); + while (!boxes->empty ()) + partials.push_back (non_overlapping_skyline (boxes, horizon_padding, horizon_axis, sky)); - list right; - list 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 merged; + list one = partials.front (); + partials.pop_front (); + if (partials.empty ()) + return one; + + list two = partials.front (); + partials.pop_front (); + internal_merge_skyline (&one, &two, &merged); + partials.push_back (merged); + } + assert (0); + return list (); } Skyline::Skyline () { - max_slope_ = 2; sky_ = UP; empty_skyline (&buildings_); } +Skyline::Skyline (Skyline const &src) +{ + sky_ = src.sky_; + + /* doesn't a list's copy constructor do this? -- jneem */ + for (list::const_iterator i = src.buildings_.begin (); + i != src.buildings_.end (); i++) + { + buildings_.push_back (Building ((*i))); + } +} + Skyline::Skyline (Direction sky) { - max_slope_ = 2; sky_ = sky; empty_skyline (&buildings_); } +/* + build skyline from a set of boxes. If horizon_padding > 0, expand all the boxes + by that amount and add 45-degree sloped boxes to the edges of each box (of + width horizon_padding). That is, the total amount of horizontal expansion is + horizon_padding*4, half of which is sloped and half of which is flat. -Skyline::Skyline (vector const &boxes, Axis a, Direction sky) + Boxes should have fatness in the horizon_axis (after they are expanded by + horizon_padding), otherwise they are ignored. + */ +Skyline::Skyline (vector const &boxes, Real horizon_padding, Axis horizon_axis, Direction sky) { - list bldgs; + list filtered_boxes; sky_ = sky; - max_slope_ = 2; + Axis vert_axis = other_axis (horizon_axis); for (vsize i = 0; i < boxes.size (); i++) { - Interval iv = boxes[i][a]; - Real height = sky * boxes[i][other_axis (a)][sky]; - if (!iv.is_empty () && !isinf (height) && !equal (iv[LEFT], iv[RIGHT])) - bldgs.push_front (Building (iv[LEFT], height, height, iv[RIGHT], max_slope_)); + Interval iv = boxes[i][horizon_axis]; + iv.widen (horizon_padding); + if (iv.length () > EPS && !boxes[i][vert_axis].is_empty ()) + filtered_boxes.push_front (boxes[i]); } - internal_build_skyline (&bldgs, &buildings_); - assert (is_legal_skyline ()); + + buildings_ = internal_build_skyline (&filtered_boxes, horizon_padding, horizon_axis, sky); } -Skyline::Skyline (vector const &points, Real max_slope, Direction sky) +Skyline::Skyline (Box const &b, Real horizon_padding, Axis horizon_axis, Direction sky) { sky_ = sky; - max_slope_ = max_slope; - - for (vsize i = 1; i < points.size (); i++) - { - buildings_.push_back (Building (points[i-1][X_AXIS], sky * points[i-1][Y_AXIS], - points[i][X_AXIS], sky * points[i][Y_AXIS], - max_slope)); - - } - - assert (is_legal_skyline ()); + Building front (b, horizon_padding, horizon_axis, sky); + single_skyline (front, b[horizon_axis][LEFT], horizon_padding, &buildings_); } void @@ -286,34 +411,49 @@ Skyline::merge (Skyline const &other) list my_bld; my_bld.splice (my_bld.begin (), buildings_); internal_merge_skyline (&other_bld, &my_bld, &buildings_); - assert (is_legal_skyline ()); } void -Skyline::insert (Box const &b, Axis a) +Skyline::insert (Box const &b, Real horizon_padding, Axis a) { list other_bld; list my_bld; + + if (isnan (b[other_axis (a)][LEFT]) + || isnan (b[other_axis (a)][RIGHT])) + { + programming_error ("insane box for skyline"); + return; + } + + /* do the same filtering as in Skyline (vector const&, etc.) */ Interval iv = b[a]; - Real height = sky_ * b[other_axis (a)][sky_]; + iv.widen (horizon_padding); + if (iv.length () <= EPS || b[other_axis (a)].is_empty ()) + return; my_bld.splice (my_bld.begin (), buildings_); - single_skyline (Building (iv[LEFT], height, height, iv[RIGHT], max_slope_), &other_bld, max_slope_); + single_skyline (Building (b, horizon_padding, a, sky_), b[a][LEFT], horizon_padding, &other_bld); internal_merge_skyline (&other_bld, &my_bld, &buildings_); - assert (is_legal_skyline ()); } void Skyline::raise (Real r) +{ + list::iterator end = buildings_.end (); + for (list::iterator i = buildings_.begin (); i != end; i++) + i->y_intercept_ += sky_ * r; +} + +void +Skyline::shift (Real s) { list::iterator end = buildings_.end (); for (list::iterator i = buildings_.begin (); i != end; i++) { - i->start_height_ += sky_ * r; - i->end_height_ += sky_ * r; - i->zero_height_ += sky_ * r; + i->end_ += s; + i->y_intercept_ -= s * i->slope_; } - assert (is_legal_skyline ()); } Real @@ -324,15 +464,18 @@ Skyline::distance (Skyline const &other) const list::const_iterator j = other.buildings_.begin (); Real dist = -infinity_f; + Real start = -infinity_f; while (i != buildings_.end () && j != other.buildings_.end ()) { - Interval iv = intersection (i->iv_, j->iv_); - dist = max (dist, max (i->height (iv[LEFT]) + j->height (iv[LEFT]), - i->height (iv[RIGHT]) + j->height (iv[RIGHT]))); - if (i->iv_[RIGHT] <= j->iv_[RIGHT]) + Real end = min (i->end_, j->end_); + Real start_dist = i->height (start) + j->height (start); + Real end_dist = i->height (end) + j->height (end); + dist = max (dist, max (start_dist, end_dist)); + if (i->end_ <= j->end_) i++; else j++; + start = end; } return dist; } @@ -345,7 +488,7 @@ Skyline::height (Real airplane) const list::const_iterator i; for (i = buildings_.begin (); i != buildings_.end (); i++) { - if (i->iv_[RIGHT] >= airplane) + if (i->end_ >= airplane) return sky_ * i->height (airplane); } @@ -365,45 +508,61 @@ void Skyline::set_minimum_height (Real h) { Skyline s (sky_); - s.buildings_.front ().start_height_ = h * sky_; - s.buildings_.front ().end_height_ = h * sky_; - s.buildings_.front ().zero_height_ = h * sky_; + s.buildings_.front ().y_intercept_ = h * sky_; merge (s); } -Stencil -to_stencil (Skyline const &line) -{ - vector points (line.to_points ()); - - Stencil ret; - for (vsize i = 1; i < points.size (); i++) - { - if (points[i-1].is_sane () && points[i].is_sane ()) - { - Stencil line - = Line_interface::make_line (0.1, points[i-1], points[i]); - ret.add_stencil (line); - } - } - return ret; -} vector -Skyline::to_points () const +Skyline::to_points (Axis horizon_axis) const { vector out; - bool first = true; + Real start = -infinity_f; for (list::const_iterator i (buildings_.begin ()); i != buildings_.end (); i++) { - if (first) - out.push_back (Offset ((*i).iv_[LEFT], sky_ * (*i).start_height_)); - first = false; - out.push_back (Offset ((*i).iv_[RIGHT], sky_ * (*i).end_height_)); + out.push_back (Offset (start, sky_ * i->height (start))); + out.push_back (Offset (i->end_, sky_ * i->height (i->end_))); + start = i->end_; } + if (horizon_axis == Y_AXIS) + for (vsize i = 0; i < out.size (); i++) + out[i] = out[i].swapped (); + return out; } +bool +Skyline::is_empty () const +{ + Building b = buildings_.front (); + return b.end_ == infinity_f && b.y_intercept_ == -infinity_f; +} + + +/****************************************************************/ + + +IMPLEMENT_SIMPLE_SMOBS (Skyline); +IMPLEMENT_TYPE_P (Skyline, "ly:skyline?"); +IMPLEMENT_DEFAULT_EQUAL_P (Skyline); + +SCM +Skyline::mark_smob (SCM) +{ + ASSERT_LIVE_IS_ALLOWED (); + return SCM_EOL; +} + +int +Skyline::print_smob (SCM s, SCM port, scm_print_state *) +{ + Skyline *r = (Skyline *) SCM_CELL_WORD_1 (s); + (void) r; + + scm_puts ("#", port); + + return 1; +}