]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-collision.cc
Merge branch 'master' of git://git.sv.gnu.org/lilypond.git into td-lily
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "note-collision.hh"
10
11 #include "axis-group-interface.hh"
12 #include "dot-column.hh"
13 #include "international.hh"
14 #include "note-column.hh"
15 #include "note-head.hh"
16 #include "output-def.hh"
17 #include "pointer-group-interface.hh"
18 #include "item.hh"
19 #include "rhythmic-head.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "side-position-interface.hh"
22 #include "stem.hh"
23 #include "warn.hh"
24
25
26 void
27 check_meshing_chords (Grob *me,
28                       Drul_array<vector<Real> > *offsets,
29                       Drul_array<vector<Slice> > const &extents,
30                       Drul_array<vector<Grob*> > const &clash_groups)
31
32 {
33   if (!extents[UP].size () || ! extents[DOWN].size ())
34     return;
35
36   Grob *clash_up = clash_groups[UP][0];
37   Grob *clash_down = clash_groups[DOWN][0];
38
39   /* Every note column should have a stem, but avoid a crash. */
40   if (!Note_column::get_stem (clash_up) || !Note_column::get_stem (clash_down))
41     return;
42
43   Drul_array<Grob*> stems (Note_column::get_stem (clash_down),
44                            Note_column::get_stem (clash_up));
45   
46   Grob *head_up = Note_column::first_head (clash_up);
47   Grob *head_down = Note_column::first_head (clash_down);
48
49   vector<int> ups = Stem::note_head_positions (Note_column::get_stem (clash_up));
50   vector<int> dps = Stem::note_head_positions (Note_column::get_stem (clash_down));
51
52   /* Too far apart to collide.  */
53   if (ups[0] > dps.back () + 1)
54     return;
55
56   /* Merge heads if the notes lie the same line, or if the "stem-up-note" is
57      above the "stem-down-note". */
58   bool merge_possible = (ups[0] >= dps[0]) && (ups.back () >= dps.back ());
59
60   /* Do not merge notes typeset in different style. */
61   if (!ly_is_equal (head_up->get_property ("style"),
62                     head_down->get_property ("style")))
63     merge_possible = false;
64
65   int up_ball_type = Rhythmic_head::duration_log (head_up);
66   int down_ball_type = Rhythmic_head::duration_log (head_down);
67
68   /* Do not merge whole notes (or longer, like breve, longa, maxima).  */
69   if (merge_possible && (up_ball_type <= 0 || down_ball_type <= 0))
70     merge_possible = false;
71
72   if (merge_possible
73       && Rhythmic_head::dot_count (head_up) != Rhythmic_head::dot_count (head_down)
74       && !to_boolean (me->get_property ("merge-differently-dotted")))
75     merge_possible = false;
76
77   /* Can only merge different heads if merge-differently-headed is
78      set. */
79   if (merge_possible
80       && up_ball_type != down_ball_type
81       && !to_boolean (me->get_property ("merge-differently-headed")))
82     merge_possible = false;
83
84   /* what's this for? In particular, why do we check merge_possible when we
85      haven't finished determining it yet? --jneem */
86   if (merge_possible
87       && head_up->get_property ("style") == ly_symbol2scm ("fa")
88       && head_down->get_property ("style") == ly_symbol2scm ("fa"))
89     {
90       Interval uphead_size = head_up->extent (head_up, Y_AXIS);
91       Offset att =  Offset (0.0, -1.0);
92       head_up->set_property ("stem-attachment", ly_offset2scm (att));
93       head_up->set_property ("transparent", SCM_BOOL_T); 
94     }
95   
96   /* Should never merge quarter and half notes, as this would make
97      them indistinguishable.  */
98   if (merge_possible
99       && ((Stem::duration_log (stems[UP]) == 1
100            && Stem::duration_log (stems[DOWN]) == 2)
101           || (Stem::duration_log (stems[UP]) == 2
102               && Stem::duration_log (stems[DOWN]) == 1)))
103     merge_possible = false;
104
105   /*
106     this case (distant half collide),
107
108     |
109     x |
110     | x
111     |
112
113     the noteheads may be closer than this case (close half collide)
114
115     |
116     |
117     x
118     x
119     |
120     |
121
122   */
123
124   /* TODO: filter out the 'o's in this configuration, since they're no
125      part in the collision.
126
127      |
128      x|o
129      x|o
130      x
131
132   */
133
134   bool close_half_collide = false;
135   bool distant_half_collide = false;
136   bool full_collide = false;
137
138   for (vsize i = 0, j = 0; i < ups.size () && j < dps.size (); )
139     {
140       if (abs (ups[i] - dps[j]) == 1)
141         {
142           merge_possible = false;
143           if (ups[i] > dps[j])
144             close_half_collide = true;
145           else
146             distant_half_collide = true;
147         }
148       else if (ups[i] == dps[j])
149         full_collide = true;
150       else if (ups[i] > dps[0] && ups[i] < dps.back ())
151         merge_possible = false;
152       else if (dps[j] > ups[0] && dps[j] < ups.back ())
153         merge_possible = false;
154
155       if (ups[i] < dps[j])
156         i++;
157       else if (ups[i] > dps[j])
158         j++;
159       else
160         {
161           i++;
162           j++;
163         }
164     }
165
166   full_collide = full_collide || (close_half_collide
167                                   && distant_half_collide);
168
169   Drul_array<Real> center_note_shifts;
170   center_note_shifts[LEFT] = 0.0;
171   center_note_shifts[RIGHT] = 0.0;
172
173   Real shift_amount = 1;
174
175   bool touch = (ups[0] >= dps.back ());
176   if (touch)
177     shift_amount *= -1;
178
179   /* For full collisions, the right hand head may obscure dots, so
180      make sure the dotted heads go to the right.  */
181   bool stem_to_stem = false;
182   if (full_collide)
183     {
184       if (Rhythmic_head::dot_count (head_up) > Rhythmic_head::dot_count (head_down))
185         shift_amount = 1;
186       else if (Rhythmic_head::dot_count (head_up) < Rhythmic_head::dot_count (head_down))
187         stem_to_stem = true;
188     }
189   
190   if (merge_possible)
191     {
192       shift_amount = 0;
193
194       /* If possible, don't wipe any heads. Else, wipe shortest head,
195          or head with smallest amount of dots.  Note: when merging
196          different heads, dots on the smaller one disappear. */
197       Grob *wipe_ball = 0;
198       Grob *dot_wipe_head = head_up;
199
200       if (up_ball_type == down_ball_type)
201         {
202           if (Rhythmic_head::dot_count (head_down) < Rhythmic_head::dot_count (head_up))
203             {
204               wipe_ball = head_down;
205               dot_wipe_head = head_down;
206             }
207           else if (Rhythmic_head::dot_count (head_down) > Rhythmic_head::dot_count (head_up))
208             {
209               dot_wipe_head = head_up;
210               wipe_ball = head_up;
211             }
212           else
213             dot_wipe_head = head_up;
214         }
215       else if (down_ball_type > up_ball_type)
216         {
217           wipe_ball = head_down;
218           dot_wipe_head = head_down;
219         }
220       else if (down_ball_type < up_ball_type)
221         {
222           wipe_ball = head_up;
223           dot_wipe_head = head_up;
224         }
225
226       if (dot_wipe_head)
227         {
228           if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
229             d->suicide ();
230         }
231
232       if (wipe_ball && wipe_ball->is_live ())
233         {
234           wipe_ball->set_property ("transparent", SCM_BOOL_T);
235         }
236     }
237   /* TODO: these numbers are magic; should devise a set of grob props
238      to tune this behavior.  */
239   else if (stem_to_stem)
240     shift_amount = -abs (shift_amount) * 0.65;
241   else if (close_half_collide && !touch)
242     shift_amount *= 0.52;
243   else if (distant_half_collide && !touch)
244     shift_amount *= 0.4;
245   else if (distant_half_collide || close_half_collide || full_collide)
246     shift_amount *= 0.5;
247
248   /* we're meshing.  */
249   else if (Rhythmic_head::dot_count (head_up) || Rhythmic_head::dot_count (head_down))
250     shift_amount *= 0.1;
251   else
252     shift_amount *= 0.17;
253
254   /*
255     
256   */
257   if (full_collide
258       && down_ball_type * up_ball_type == 0)
259     {
260       if (up_ball_type == 0 && down_ball_type == 1)
261         shift_amount *= 1.25;
262       else if (up_ball_type == 0 && down_ball_type == 2)
263         shift_amount *= 1.35;
264       else if (down_ball_type == 0 && up_ball_type == 1)
265         shift_amount *= 0.7;
266       else if (down_ball_type == 0 && up_ball_type == 2)
267         shift_amount *= 0.75;
268     }
269   
270   /*
271    * Fix issue #44:
272    *
273    * Dots from left note head collide with right note head. Only occurs
274    * with a close half collide, if the left note head is between
275    * lines and the right note head is on a line, and if right note head
276    * hasn't got any dots.
277    */
278   if (close_half_collide
279       && Rhythmic_head::dot_count (head_up)
280       && !Rhythmic_head::dot_count (head_down))
281     {
282       Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
283       if (!Staff_symbol_referencer::on_line (staff, ups[0]))
284         /*
285           TODO: consider junking the else body.
286         */
287         if (to_boolean (me->get_property ("prefer-dotted-right")))
288           {
289             shift_amount = 0.5;
290           }
291         else
292           {
293             Grob *d = unsmob_grob (head_up->get_object ("dot"));
294             Grob *parent = d->get_parent (X_AXIS);
295             if (Dot_column::has_interface (parent))
296               Side_position_interface::add_support (parent, head_down);
297           }
298     }
299
300   /* For full or close half collisions, the right hand head may
301      obscure dots.  Move dots to the right.  */
302   if (abs (shift_amount) > 1e-6
303       && Rhythmic_head::dot_count (head_down) > Rhythmic_head::dot_count (head_up)
304       && (full_collide || close_half_collide))
305     {
306       Grob *d = unsmob_grob (head_down->get_object ("dot"));
307       Grob *parent = d->get_parent (X_AXIS);
308
309       /*
310         FIXME:
311
312         |
313         x . o
314         |
315
316
317         the . is put right of o which is erroneous o force-shifted
318         far to the right.
319       */
320       if (Dot_column::has_interface (parent))
321         {
322           Grob *stem = unsmob_grob (head_up->get_object ("stem"));
323           extract_grob_set (stem, "note-heads", heads);
324           for (vsize i = 0; i < heads.size (); i++)
325             Side_position_interface::add_support (parent, heads[i]);
326         }
327     }
328
329   Direction d = UP;
330   do
331     {
332       for (vsize i = 0; i < clash_groups[d].size (); i++)
333         (*offsets)[d][i] += d * shift_amount;
334     }
335   while ((flip (&d)) != UP);
336 }
337
338
339 MAKE_SCHEME_CALLBACK (Note_collision_interface, calc_positioning_done, 1) 
340 SCM
341 Note_collision_interface::calc_positioning_done (SCM smob)
342 {
343   Grob *me = unsmob_grob (smob);
344   me->set_property ("positioning-done", SCM_BOOL_T);
345   
346   Drul_array<vector<Grob*> > clash_groups = get_clash_groups (me);
347
348   Direction d = UP;
349   do
350     {
351       for (vsize i = clash_groups[d].size (); i--; )
352         {
353           /*
354             Trigger positioning
355            */
356           clash_groups[d][i]->extent (me, X_AXIS);
357         }
358     }
359   while (flip (&d) != UP);
360
361   SCM autos (automatic_shift (me, clash_groups));
362   SCM hand (forced_shift (me));
363
364   Real wid = 0.0;
365   do
366     {
367       if (clash_groups[d].size ())
368         {
369           Grob *h = clash_groups[d][0];
370           Grob *fh = Note_column::first_head (h);
371           if (fh)
372             wid = fh->extent (h, X_AXIS).length ();
373         }
374     }
375   while (flip (&d) != UP);
376
377   vector<Grob*> done;
378   Real left_most = 1e6;
379
380   vector<Real> amounts;
381   for (; scm_is_pair (hand); hand = scm_cdr (hand))
382     {
383       Grob *s = unsmob_grob (scm_caar (hand));
384       Real amount = scm_to_double (scm_cdar (hand)) * wid;
385
386       done.push_back (s);
387       amounts.push_back (amount);
388       if (amount < left_most)
389         left_most = amount;
390     }
391   for (; scm_is_pair (autos); autos = scm_cdr (autos))
392     {
393       Grob *s = unsmob_grob (scm_caar (autos));
394       Real amount = scm_to_double (scm_cdar (autos)) * wid;
395
396       vsize x = find (done, s) - done.begin ();
397       if (x == VPOS || x >= done.size ())
398         {
399           done.push_back (s);
400           amounts.push_back (amount);
401           if (amount < left_most)
402             left_most = amount;
403         }
404     }
405
406   for (vsize i = 0; i < amounts.size (); i++)
407     done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
408
409   return SCM_BOOL_T;
410 }
411
412 Drul_array < vector<Grob*> >
413 Note_collision_interface::get_clash_groups (Grob *me)
414 {
415   Drul_array<vector<Grob*> > clash_groups;
416
417   extract_grob_set (me, "elements", elements);
418   for (vsize i = 0; i < elements.size (); i++)
419     {
420       Grob *se = elements[i];
421       if (Note_column::has_interface (se))
422         {
423           if (!Note_column::dir (se))
424             {
425               se->programming_error ("note-column has no direction");
426             }
427           else
428             clash_groups[Note_column::dir (se)].push_back (se);
429         }
430     }
431
432   Direction d = UP;
433   do
434     {
435       vector<Grob*> &clashes (clash_groups[d]);
436       vector_sort (clashes, Note_column::shift_less);
437     }
438   while ((flip (&d)) != UP);
439
440   return clash_groups;
441 }
442
443 /** This complicated routine moves note columns around horizontally to
444     ensure that notes don't clash.
445
446 */
447 SCM
448 Note_collision_interface::automatic_shift (Grob *me,
449                                            Drul_array<vector<Grob*> > clash_groups)
450 {
451   Drul_array < vector<int> > shifts;
452   SCM tups = SCM_EOL;
453
454   Direction d = UP;
455   do
456     {
457       vector<int> &shift (shifts[d]);
458       vector<Grob*> &clashes (clash_groups[d]);
459
460       for (vsize i = 0; i < clashes.size (); i++)
461         {
462           SCM sh
463             = clashes[i]->get_property ("horizontal-shift");
464
465           if (scm_is_number (sh))
466             shift.push_back (scm_to_int (sh));
467           else
468             shift.push_back (0);
469         }
470
471       for (vsize i = 1; i < shift.size (); i++)
472         {
473           if (shift[i - 1] == shift[i])
474             {
475               clashes[0]->warning (_ ("ignoring too many clashing note columns"));
476               return tups;
477             }
478         }
479     }
480   while ((flip (&d)) != UP);
481
482   Drul_array<vector<Slice> > extents;
483   Drul_array<vector<Real> > offsets;
484   d = UP;
485   do
486     {
487       for (vsize i = 0; i < clash_groups[d].size (); i++)
488         {
489           Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
490           s[LEFT]--;
491           s[RIGHT]++;
492           extents[d].push_back (s);
493           offsets[d].push_back (d * 0.5 * i);
494         }
495     }
496   while ((flip (&d)) != UP);
497
498   /*
499     do horizontal shifts of each direction
500
501     |
502     x||
503     x||
504     x|
505   */
506
507   do
508     {
509       for (vsize i = 1; i < clash_groups[d].size (); i++)
510         {
511           Slice prev = extents[d][i - 1];
512           prev.intersect (extents[d][i]);
513           if (prev.length () > 0
514               || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
515             for (vsize j = i; j < clash_groups[d].size (); j++)
516               offsets[d][j] += d * 0.5;
517         }
518     }
519   while ((flip (&d)) != UP);
520
521
522   /*
523     see input/regression/dot-up-voice-collision.ly
524    */
525   for (vsize i = 0; i < clash_groups[UP].size (); i++)
526     {
527       Grob *g = clash_groups[UP][i];
528       Grob *dc = Note_column::dot_column (g);
529       
530       if (dc)
531         for (vsize j = i + 1;  j < clash_groups[UP].size (); j++)
532           {
533             Grob *stem = Note_column::get_stem (clash_groups[UP][j]);
534             Side_position_interface::add_support (dc, stem);
535           }
536     }
537   
538   /*
539     Check if chords are meshing
540   */
541
542   check_meshing_chords (me, &offsets, extents, clash_groups);
543
544   do
545     {
546       for (vsize i = 0; i < clash_groups[d].size (); i++)
547         tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
548                                    scm_from_double (offsets[d][i])),
549                          tups);
550     }
551   while (flip (&d) != UP);
552
553   return tups;
554 }
555
556 SCM
557 Note_collision_interface::forced_shift (Grob *me)
558 {
559   SCM tups = SCM_EOL;
560
561   extract_grob_set (me, "elements", elements);
562   for (vsize i = 0; i < elements.size (); i++)
563     {
564       Grob *se = elements[i];
565
566       SCM force = se->get_property ("force-hshift");
567       if (scm_is_number (force))
568         {
569           tups = scm_cons (scm_cons (se->self_scm (), force),
570                            tups);
571         }
572     }
573   return tups;
574 }
575
576 void
577 Note_collision_interface::add_column (Grob *me, Grob *ncol)
578 {
579   ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
580   Axis_group_interface::add_element (me, ncol);
581 }
582
583 ADD_INTERFACE (Note_collision_interface,
584                "An object that handles collisions between notes with"
585                " different stem directions and horizontal shifts.  Most of"
586                " the interesting properties are to be set in"
587                " @ref{note-column-interface}: these are @code{force-hshift}"
588                " and @code{horizontal-shift}.",
589
590                /* properties */
591                "merge-differently-dotted "
592                "merge-differently-headed "
593                "positioning-done "
594                "prefer-dotted-right "
595                );