(process_acknowledged_grobs):
[lilypond.git] / lily / skyline.cc
blob65f267aa176b9e9529e1c49d1549c6dddfa959b9
1 /*
2 skyline.cc -- implement Skyline_entry and funcs.
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "skyline.hh"
13 A skyline is a shape of the form:
16 ----
17 | |
18 ---------| |
19 | |
20 | |
21 | |______
22 --------| |___
26 This file deals with building such skyline structure, and computing
27 the minimum distance between two opposing skylines.
30 Invariants for a skyline:
32 skyline[...].width_ forms a partition of the real interval, where
33 the segments are adjacent, and ascending. Hence we have
35 skyline.top().width_[RIGHT] = inf
36 skyline[0].width_[LEFT] = -inf
41 const Real EPS = 1e-12;
44 TODO: avoid unnecessary fragmentation.
46 This is O(n^2), searching and insertion. Could be O(n log n) with
47 binsearch.
49 void
50 insert_extent_into_skyline (Array<Skyline_entry> *line, Box b, Axis line_axis,
51 Direction d)
53 Interval extent = b[line_axis];
54 if (extent.empty_b())
55 return;
57 Real stick_out = b[other_axis (line_axis)][d];
60 Intersect each segment of LINE with EXTENT, and if non-empty, insert relevant segments.
62 for (int i = line->size(); i--;)
64 Interval w = line->elem(i).width_;
65 w.intersect (extent);
67 if (extent[LEFT] >= w[RIGHT])
68 break;
70 Real my_height = line->elem(i).height_;
72 if (!w.empty_b () &&
73 w.length() > EPS
74 && d* (my_height - stick_out) < 0)
76 Interval e1 (line->elem(i).width_[LEFT], extent[LEFT]);
77 Interval e3 (extent[RIGHT], line->elem(i).width_[RIGHT]);
79 if (!e3.empty_b () && e3.length() > EPS)
80 line->insert (Skyline_entry (e3, my_height), i+1);
82 line->elem_ref(i).height_ = stick_out;
83 line->elem_ref(i).width_ = w;
84 if (!e1.empty_b () && e1.length() > EPS)
85 line->insert (Skyline_entry (e1, my_height), i );
93 Array<Skyline_entry>
94 empty_skyline (Direction d)
96 Array<Skyline_entry> skyline;
98 Interval i;
99 i.set_empty();
100 i.swap();
101 Skyline_entry e;
102 e.width_ = i;
103 e.height_ = -d * infinity_f;
104 skyline.push (e);
105 return skyline;
108 Array<Skyline_entry>
109 extents_to_skyline (Array<Box> const &extents, Axis a, Direction d)
112 Array<Skyline_entry> skyline = empty_skyline(d);
115 This makes a cubic algorithm (array insertion is O(n),
116 searching the array dumbly is O(n), and for n items, we get O(n^3).)
118 We could do a lot better (n log (n), using a balanced tree) but
119 that seems overkill for now.
121 for (int j = extents.size(); j--; )
122 insert_extent_into_skyline (&skyline, extents[j], a, d);
124 return skyline;
129 minimum distance that can be achieved between baselines. "Clouds" is
130 a skyline pointing down.
132 This is an O(n) algorithm.
134 Real
135 skyline_meshing_distance (Array<Skyline_entry> const &buildings,
136 Array<Skyline_entry> const &clouds)
138 int i = buildings.size () -1;
139 int j = clouds.size() -1;
141 Real distance = - infinity_f;
143 while (i > 0 || j > 0)
145 Interval w = buildings[i].width_;
146 w.intersect(clouds[j].width_);
148 if (!w.empty_b())
149 distance = distance >? (buildings[i].height_ - clouds[j].height_);
151 if (i>0 && buildings[i].width_[LEFT] >= clouds[j].width_[LEFT])
153 i--;
155 else if (j > 0 && buildings[i].width_[LEFT] <= clouds[j].width_[LEFT])
157 j--;
161 return distance;
164 Skyline_entry::Skyline_entry()
166 height_ = 0.0;
169 Skyline_entry::Skyline_entry (Interval i, Real r)
171 width_ = i;
172 height_ = r;