]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
Issue 4550 (2/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / rest-collision.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "rest-collision.hh"
21
22 #include <cmath>                // ceil.
23
24 #include "directional-element-interface.hh"
25 #include "duration.hh"
26 #include "international.hh"
27 #include "item.hh"
28 #include "note-column.hh"
29 #include "output-def.hh"
30 #include "pointer-group-interface.hh"
31 #include "rest.hh"
32 #include "rhythmic-head.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "stem.hh"
35 #include "grob.hh"
36 #include "unpure-pure-container.hh"
37 #include "warn.hh"
38 #include "lily-imports.hh"
39
40 using std::vector;
41
42 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Rest_collision, force_shift_callback_rest, 2, 1, "");
43 SCM
44 Rest_collision::force_shift_callback_rest (SCM rest, SCM offset)
45 {
46   Grob *rest_grob = unsmob<Grob> (rest);
47   Grob *parent = rest_grob->get_parent (X_AXIS);
48
49   /*
50     translate REST; we need the result of this translation later on,
51     while the offset probably still is 0/calculation-in-progress.
52    */
53   if (scm_is_number (offset))
54     rest_grob->translate_axis (scm_to_double (offset), Y_AXIS);
55
56   if (has_interface<Note_column> (parent) && Note_column::has_rests (parent))
57     {
58       Grob *collision = unsmob<Grob> (parent->get_object ("rest-collision"));
59
60       if (collision)
61         (void) collision->get_property ("positioning-done");
62     }
63
64   return scm_from_double (0.0);
65 }
66
67 void
68 Rest_collision::add_column (Grob *me, Grob *p)
69 {
70   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
71
72   p->set_object ("rest-collision", me->self_scm ());
73
74   Grob *rest = unsmob<Grob> (p->get_object ("rest"));
75   if (rest)
76     {
77       chain_offset_callback (rest,
78                              Unpure_pure_container::make_smob
79                              (Rest_collision::force_shift_callback_rest_proc,
80                               Lily::pure_chain_offset_callback),
81                              Y_AXIS);
82     }
83 }
84
85 static bool
86 rest_shift_less (Grob *const &r1, Grob *const &r2)
87 {
88   Grob *col1 = r1->get_parent (X_AXIS);
89   Grob *col2 = r2->get_parent (X_AXIS);
90   return Note_column::shift_less (col1, col2);
91 }
92
93 /*
94   TODO: look at horizontal-shift to determine ordering between rests
95   for more than two voices.
96 */
97 MAKE_SCHEME_CALLBACK (Rest_collision, calc_positioning_done, 1);
98 SCM
99 Rest_collision::calc_positioning_done (SCM smob)
100 {
101   Grob *me = unsmob<Grob> (smob);
102
103   me->set_property ("positioning-done", SCM_BOOL_T);
104
105   extract_grob_set (me, "elements", elts);
106
107   vector<Grob *> rests;
108   vector<Grob *> notes;
109
110   for (vsize i = 0; i < elts.size (); i++)
111     {
112       Grob *e = elts[i];
113       if (has_interface<Note_column> (e))
114         {
115           if (unsmob<Grob> (e->get_object ("rest")))
116             rests.push_back (e);
117           else
118             notes.push_back (e);
119         }
120     }
121
122   /*
123     handle rest-rest and rest-note collisions
124
125     [todo]
126     * decide not to print rest if too crowded?
127     */
128
129   /*
130     no partners to collide with
131   */
132   if (rests.size () + notes.size () < 2)
133     return SCM_BOOL_T;
134
135   Real staff_space = Staff_symbol_referencer::staff_space (me);
136   /*
137     only rests
138   */
139   if (!notes.size ())
140     {
141
142       /*
143         This is incomplete: in case of an uneven number of rests, the
144         center one should be centered on the staff.
145       */
146       Drul_array<vector<Grob *> > ordered_rests;
147       for (vsize i = 0; i < rests.size (); i++)
148         {
149           Grob *r = Note_column::get_rest (rests[i]);
150
151           Direction d = get_grob_direction (r);
152           if (d)
153             ordered_rests[d].push_back (r);
154           else
155             rests[d]->warning (_ ("cannot resolve rest collision: rest direction not set"));
156         }
157
158       for (LEFT_and_RIGHT (d))
159         vector_sort (ordered_rests[d], rest_shift_less);
160
161       for (LEFT_and_RIGHT (d))
162         {
163           if (ordered_rests[d].size () < 1)
164             {
165               if (ordered_rests[-d].size () > 1)
166                 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
167
168               return SCM_BOOL_T;
169             }
170         }
171
172       Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
173       common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
174
175       Real diff
176         = (ordered_rests[DOWN].back ()->extent (common, Y_AXIS)[UP]
177            - ordered_rests[UP].back ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
178
179       if (diff > 0)
180         {
181           int amount_down = (int) ceil (diff / 2);
182           diff -= amount_down;
183           Rest::translate (ordered_rests[DOWN].back (),
184                            -2 * amount_down);
185           if (diff > 0)
186             Rest::translate (ordered_rests[UP].back (),
187                              2 * int (ceil (diff)));
188         }
189
190       for (LEFT_and_RIGHT (d))
191         {
192           for (vsize i = ordered_rests[d].size () - 1; i-- > 0;)
193             {
194               Real last_y = ordered_rests[d][i + 1]->extent (common, Y_AXIS)[d];
195               Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
196
197               Real diff = d * ((last_y - y) / staff_space);
198               if (diff > 0)
199                 Rest::translate (ordered_rests[d][i], d * (int) ceil (diff) * 2);
200             }
201         }
202     }
203   else
204     {
205       /*
206         Rests and notes.
207       */
208       // Count how many rests we move
209       Drul_array<int> rcount (0, 0);
210
211       for (vsize i = 0; i < rests.size (); i++)
212         {
213           Grob *rcol = rests[i];
214           Grob *rest = Note_column::get_rest (rcol);
215
216           Direction dir = get_grob_direction (rest);
217           if (!dir)
218             dir = Note_column::dir (rcol);
219
220           // Do not compute a translation for pre-positioned rests,
221           //  nor count them for the "too many colliding rests" warning
222           if (scm_is_number (rest->get_property ("staff-position")))
223             continue;
224
225           Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
226           Interval restdim = rest->extent (common, Y_AXIS);
227           if (restdim.is_empty ())
228             continue;
229
230           Real staff_space = Staff_symbol_referencer::staff_space (rcol);
231           Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
232
233           Interval notedim;
234           for (vsize i = 0; i < notes.size (); i++)
235             {
236               if (Note_column::dir (notes[i]) == -dir
237                   // If the note has already happened (but it has a long
238                   // duration, so there is a collision), don't look at the stem.
239                   // If we do, the rest gets shifted down a lot and it looks bad.
240                   || dynamic_cast<Item *> (notes[i])->get_column () != dynamic_cast<Item *> (rest)->get_column ())
241                 {
242                   /* try not to look at the stem, as looking at a beamed
243                      note may trigger beam positioning prematurely.
244
245                      This happens with dotted rests, which need Y
246                      positioning to compute X-positioning.
247                   */
248                   Grob *head = Note_column::first_head (notes[i]);
249                   if (head)
250                     notedim.unite (head->extent (common, Y_AXIS));
251                   else
252                     programming_error ("Note_column without first_head()");
253                 }
254               else
255                 notedim.unite (notes[i]->extent (common, Y_AXIS));
256             }
257
258           Real y = dir * std::max (0.0,
259                               -dir * restdim[-dir] + dir * notedim[dir] + minimum_dist);
260
261           // move discretely by half spaces.
262           int discrete_y = dir * int (ceil (y / (0.5 * dir * staff_space)));
263
264           Interval staff_span = Staff_symbol_referencer::staff_span (rest);
265           staff_span.widen (1);
266           // move by whole spaces inside the staff.
267           if (staff_span.contains
268               (Staff_symbol_referencer::get_position (rest) + discrete_y))
269             {
270               discrete_y = dir * int (ceil (dir * discrete_y / 2.0) * 2.0);
271             }
272
273           Rest::translate (rest, discrete_y);
274           if (rcount[dir]++)
275             rest->warning (_ ("too many colliding rests"));
276         }
277     }
278   return SCM_BOOL_T;
279 }
280
281 ADD_INTERFACE (Rest_collision,
282                "Move ordinary rests (not multi-measure nor pitched rests)"
283                " to avoid conflicts.",
284
285                /* properties */
286                "minimum-distance "
287                "positioning-done "
288                "elements "
289               );
290