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