]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-collision.cc
* input/regression/collision-dots-move.ly: New test.
[lilypond.git] / lily / note-collision.cc
1 /*
2   collision.cc -- implement Collision
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "warn.hh"
10 #include "note-collision.hh"
11 #include "note-column.hh"
12 #include "note-head.hh"
13 #include "rhythmic-head.hh"
14 #include "paper-def.hh"
15 #include "axis-group-interface.hh"
16 #include "item.hh"
17 #include "stem.hh"
18 #include "side-position-interface.hh"
19 #include "dot-column.hh"
20
21 MAKE_SCHEME_CALLBACK (Note_collision_interface,force_shift_callback,2);
22
23 SCM
24 Note_collision_interface::force_shift_callback (SCM element_smob, SCM axis)
25 {
26   Grob *me = unsmob_grob (element_smob);
27   Axis a = (Axis) gh_scm2int (axis);
28   assert (a == X_AXIS);
29   
30    me = me->get_parent (a);
31
32    if (! to_boolean (me->get_grob_property ("positioning-done")))
33     {
34       me->set_grob_property ("positioning-done", SCM_BOOL_T);
35       do_shifts (me);
36     }
37   
38   return gh_double2scm (0.0);
39 }
40
41
42 void
43 check_meshing_chords (Grob *me,
44                       Drul_array< Array < Real > > *offsets,
45                       Drul_array< Array < Slice > > const &extents,
46                       Drul_array<Link_array<Grob> > const &clash_groups)
47         
48 {
49   if (!extents[UP].size () || ! extents[DOWN].size ())
50     return;
51   
52   Grob *cu = clash_groups[UP][0];
53   Grob *cd = clash_groups[DOWN][0];
54
55   /* Every note column should have a stem, but avoid a crash. */
56   if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
57     return;
58
59   Grob *nu = Note_column::first_head (cu);
60   Grob *nd = Note_column::first_head (cd);
61
62   Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
63   Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
64
65   /* Too far apart to collide.  */
66   if (ups[0] > dps.top () + 1)
67     return; 
68
69   // FIXME: what's this?
70   bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
71
72   int upball_type = Note_head::get_balltype (nu);
73   int dnball_type = Note_head::get_balltype (nd);
74   
75   /* Do not merge whole notes (or longer, like breve, longa, maxima).  */
76   if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
77     merge_possible = false;
78
79   if (merge_possible
80       && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
81       && !to_boolean (me->get_grob_property ("merge-differently-dotted")))
82     merge_possible = false;
83
84   /* Can only merge different heads if merge-differently-headed is
85      set. */
86   if (merge_possible
87       && upball_type != dnball_type
88       && !to_boolean (me->get_grob_property ("merge-differently-headed")))
89     merge_possible = false;
90
91   /* Can never merge quarter and half notes. */
92   if (merge_possible
93       && ((Rhythmic_head::duration_log (nu) == 1
94            && Rhythmic_head::duration_log (nd) == 2)
95           || (Rhythmic_head::duration_log (nu) == 2
96              && Rhythmic_head::duration_log (nd) == 1)))
97     merge_possible = false;
98
99
100   /*
101     this case (distant half collide), 
102     
103         |
104       x |
105      | x
106      |
107
108    the noteheads may be closer than this case (close half collide)
109
110        |
111        |
112       x 
113      x
114     |
115     |
116     
117    */
118   
119   /* TODO: filter out the 'o's in this configuration, since they're no
120   part in the collision.
121
122      |
123     x|o
124     x|o
125     x
126     
127    */
128   
129   bool close_half_collide = false;
130   bool distant_half_collide = false;  
131   bool full_collide = false;  
132
133   int i = 0, j=0;
134   while (i < ups.size () && j < dps.size ())
135   {
136     if (abs (ups[i] - dps[j]) == 1)
137       {
138         merge_possible = false;
139         if (ups[i] > dps[j])
140           close_half_collide = true;
141         else
142           distant_half_collide = true;
143       }
144     else if (ups[i]==dps[j])
145       full_collide = true;
146     else if (ups[i] >dps[0] && ups[i] < dps.top ())
147       merge_possible = false;
148     else if (dps[j] >ups[0] && dps[j] < ups.top ())
149       merge_possible = false;
150     
151     if (ups[i] < dps[j])
152       i++;
153     else if (ups[i] > dps[j])
154       j++;
155     else
156       {
157         i++;
158         j++;
159       }
160   }
161
162   Drul_array<Real> center_note_shifts;
163   center_note_shifts[LEFT] = 0.0;
164   center_note_shifts[RIGHT] = 0.0;
165
166   
167   Real shift_amount = 1;
168
169   bool touch = (ups[0] - dps.top () >= 0);
170   if (touch)
171     shift_amount *= -1;
172
173   /* For full collisions, the right hand head may obscure dots, so
174      make sure the dotted heads go to the right.  */
175   if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd)
176       && full_collide)
177     shift_amount = 1;
178
179   if (merge_possible)
180     {
181       shift_amount = 0;
182
183       /* Wipe shortest head, or head with smallest amount of dots.
184          Note: when merging different heads, dots on shortest
185          disappear. */
186       
187       Grob *wipe_ball = nu;
188       
189       if (upball_type == dnball_type)
190         {
191           if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
192             wipe_ball = nd;
193         }
194       else if (dnball_type > upball_type)
195         wipe_ball = nd;
196
197       if (wipe_ball->live ())
198         {
199           wipe_ball->set_grob_property ("transparent", SCM_BOOL_T);
200           wipe_ball->set_grob_property ("molecule", SCM_EOL);
201
202           if (Grob *d = unsmob_grob (wipe_ball->get_grob_property ("dot")))
203             d->suicide ();
204         }
205     }
206   /* TODO: these numbers are magic; should devise a set of grob props
207      to tune this behavior.  */
208   else if (close_half_collide && !touch)
209     shift_amount *= 0.52;
210   else if (distant_half_collide && !touch)
211     shift_amount *= 0.4;
212   else if (distant_half_collide || close_half_collide || full_collide)
213     shift_amount *= 0.5;
214   
215   /* we're meshing.  */
216   else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
217     shift_amount *= 0.1;
218   else
219     shift_amount *= 0.25;
220
221   /* For full or close half collisions, the right hand head may
222      obscure dots.  Move dots to the right.  */
223   if (abs (shift_amount) > 1e-6
224       && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
225       && (full_collide || close_half_collide))
226     {
227       Grob *d = unsmob_grob (nd->get_grob_property ("dot"));
228       Grob *parent = d->get_parent (X_AXIS);
229       if (Dot_column::has_interface (parent))
230         Side_position_interface::add_support (parent, nu);
231     }
232
233   Direction d = UP;
234   do
235     {
236       for (int i=0; i < clash_groups[d].size (); i++)
237         (*offsets)[d][i] += d * shift_amount;
238     }
239   while ((flip (&d))!= UP);
240 }
241
242 void
243 Note_collision_interface::do_shifts (Grob* me)
244 {
245   Drul_array< Link_array <Grob>  > cg = get_clash_groups (me);
246
247   SCM autos (automatic_shift (me, cg));
248   SCM hand (forced_shift (me));
249
250   
251   
252   Direction d = UP;
253   Real wid = 0.0;
254   do
255     {
256       if(cg[d].size())
257         {
258           Grob  *h = cg[d][0];
259           wid = Note_column::first_head(h)->extent(h,X_AXIS).length() ;
260         }
261     }
262   
263   while (flip (&d) != UP);
264
265   
266   Link_array<Grob> done;
267   for (; gh_pair_p (hand); hand =ly_cdr (hand))
268     {
269       Grob * s = unsmob_grob (ly_caar (hand));
270       Real amount = gh_scm2double (ly_cdar (hand));
271       
272       s->translate_axis (amount *wid, X_AXIS);
273       done.push (s);
274     }
275   for (; gh_pair_p (autos); autos =ly_cdr (autos))
276     {
277       Grob * s = unsmob_grob (ly_caar (autos));
278       Real amount = gh_scm2double (ly_cdar (autos));
279       
280       if (!done.find (s))
281         s->translate_axis (amount * wid, X_AXIS);
282     }
283 }
284
285 Drul_array< Link_array <Grob>  >
286 Note_collision_interface::get_clash_groups (Grob *me)
287 {
288   Drul_array<Link_array<Grob> > clash_groups;
289  
290   SCM s = me->get_grob_property ("elements");
291   for (; gh_pair_p (s); s = ly_cdr (s))
292     {
293       SCM car = ly_car (s);
294
295       Grob * se = unsmob_grob (car);
296       if (Note_column::has_interface (se))
297         clash_groups[Note_column::dir (se)].push (se);
298     }
299   
300   Direction d = UP;
301   do
302     {
303       Link_array<Grob> & clashes (clash_groups[d]);
304       clashes.sort (Note_column::shift_compare);
305     }
306   while ((flip (&d))!= UP);
307
308   return clash_groups;
309 }
310
311 /** This complicated routine moves note columns around horizontally to
312   ensure that notes don't clash.
313
314   This should be put into Scheme.  
315   */
316 SCM
317 Note_collision_interface::automatic_shift (Grob *me,
318                             Drul_array< Link_array <Grob> > 
319                             clash_groups)
320 {
321   Drul_array<Array<int> > shifts;
322   SCM  tups = SCM_EOL;
323
324   
325   Direction d = UP;
326   do
327     {
328       Array<int> & shift (shifts[d]);
329       Link_array<Grob> & clashes (clash_groups[d]);
330
331       for (int i=0; i < clashes.size (); i++)
332         {
333           SCM sh
334             = clashes[i]->get_grob_property ("horizontal-shift");
335
336           if (gh_number_p (sh))
337             shift.push (gh_scm2int (sh));
338           else
339             shift.push (0);
340         }
341       
342       for (int i=1; i < shift.size (); i++)
343         {
344           if (shift[i-1] == shift[i])
345             {
346               clashes[0]->warning (_ ("Too many clashing notecolumns.  Ignoring them."));
347               return tups;
348             }
349         }
350     }
351   while ((flip (&d))!= UP);
352
353   Drul_array< Array < Slice > > extents;
354   Drul_array< Array < Real > > offsets;
355   d = UP;
356   do
357     {
358       for (int i=0; i < clash_groups[d].size (); i++)
359         {
360           Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
361           s[LEFT] --;
362           s[RIGHT]++;
363           extents[d].push (s);
364           offsets[d].push (d * 0.5 * i);
365         }
366     }
367   while ((flip (&d))!= UP);
368
369   /*
370     do horizontal shifts of each direction 
371
372        | 
373       x||
374        x||
375         x|
376    */
377   
378   do
379     {
380       for (int i=1; i < clash_groups[d].size (); i++)
381         {
382           Slice prev =extents[d][i-1];
383           prev.intersect (extents[d][i]);
384           if (prev.length ()> 0 ||
385  (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
386             for (int j = i; j <  clash_groups[d].size (); j++)
387               offsets[d][j] += d * 0.5;
388         }
389     }   
390   while ((flip (&d))!= UP);
391
392
393   /*
394     Check if chords are meshing
395    */
396
397   check_meshing_chords (me, &offsets, extents, clash_groups);
398   
399   do
400     {
401       for (int i=0; i < clash_groups[d].size (); i++)
402         tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (),
403                                  gh_double2scm (offsets[d][i])),
404                         tups);
405     }
406   while (flip (&d) != UP);
407   return tups;
408 }
409
410
411 SCM
412 Note_collision_interface::forced_shift (Grob *me)
413 {
414   SCM tups = SCM_EOL;
415   
416   SCM s = me->get_grob_property ("elements");
417   for (; gh_pair_p (s); s = ly_cdr (s))
418     {
419       Grob * se = unsmob_grob (ly_car (s));
420
421       SCM force =  se->get_grob_property ("force-hshift");
422       if (gh_number_p (force))
423         {
424           tups = gh_cons (gh_cons (se->self_scm (), force),
425                           tups);
426         }
427     }
428   return tups;
429 }
430
431 void
432 Note_collision_interface::add_column (Grob*me,Grob* ncol)
433 {
434   ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
435   Axis_group_interface::add_element (me, ncol);
436   me->add_dependency (ncol);
437 }
438
439
440 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
441   "An object that handles collisions between notes with different stem " 
442 "directions and horizontal shifts. Most of the interesting properties "
443 "are to be set in @ref{note-column-interface}: these are "
444 "@code{force-hshift} and @code{horizontal-shift}. ",
445   "merge-differently-dotted merge-differently-headed positioning-done");