]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-collision.cc
move ignore-collision to note-column-interface
[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 *cu = clash_groups[UP][0];
37   Grob *cd = clash_groups[DOWN][0];
38
39   /* Every note column should have a stem, but avoid a crash. */
40   if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
41     return;
42
43   Drul_array<Grob*> stems (Note_column::get_stem (cd),
44                            Note_column::get_stem (cu));
45   
46   Grob *nu = Note_column::first_head (cu);
47   Grob *nd = Note_column::first_head (cd);
48
49   vector<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
50   vector<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
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 (nu->get_property ("style"),
62                     nd->get_property ("style")))
63     merge_possible = false;
64
65   int upball_type = Rhythmic_head::duration_log (nu);
66   int dnball_type = Rhythmic_head::duration_log (nd);
67
68   /* Do not merge whole notes (or longer, like breve, longa, maxima).  */
69   if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
70     merge_possible = false;
71
72   if (merge_possible
73       && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
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       && upball_type != dnball_type
81       && !to_boolean (me->get_property ("merge-differently-headed")))
82     merge_possible = false;
83
84   if (merge_possible
85       && nu->get_property ("style") == ly_symbol2scm ("fa")
86       && nd->get_property ("style") == ly_symbol2scm ("fa"))
87     {
88       Interval uphead_size = nu->extent (nu, Y_AXIS);
89       Offset att =  Offset (0.0, -1.0);
90       nu->set_property ("stem-attachment", ly_offset2scm (att));
91       nu->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     if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
182       shift_amount = 1;
183     else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
184       stem_to_stem = true;
185
186   if (merge_possible)
187     {
188       shift_amount = 0;
189
190       /* If possible, don't wipe any heads. Else, wipe shortest head,
191          or head with smallest amount of dots.  Note: when merging
192          different heads, dots on the smaller one disappear. */
193       Grob *wipe_ball = 0;
194       Grob *dot_wipe_head = nu;
195
196       if (upball_type == dnball_type)
197         {
198           if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
199             {
200               wipe_ball = nd;
201               dot_wipe_head = nd;
202             }
203           else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
204             {
205               dot_wipe_head = nu;
206               wipe_ball = nu;
207             }
208           else
209             dot_wipe_head = nu;
210         }
211       else if (dnball_type > upball_type)
212         {
213           wipe_ball = nd;
214           dot_wipe_head = nd;
215         }
216       else if (dnball_type < upball_type)
217         {
218           wipe_ball = nu;
219           dot_wipe_head = nu;
220         }
221
222       if (dot_wipe_head)
223         {
224           if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
225             d->suicide ();
226         }
227
228       if (wipe_ball && wipe_ball->is_live ())
229         {
230           wipe_ball->set_property ("transparent", SCM_BOOL_T);
231         }
232     }
233   /* TODO: these numbers are magic; should devise a set of grob props
234      to tune this behavior.  */
235   else if (stem_to_stem)
236     shift_amount = -abs (shift_amount) * 0.65;
237   else if (close_half_collide && !touch)
238     shift_amount *= 0.52;
239   else if (distant_half_collide && !touch)
240     shift_amount *= 0.4;
241   else if (distant_half_collide || close_half_collide || full_collide)
242     shift_amount *= 0.5;
243
244   /* we're meshing.  */
245   else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
246     shift_amount *= 0.1;
247   else
248     shift_amount *= 0.17;
249
250   /*
251     
252   */
253   if (full_collide
254       && dnball_type * upball_type == 0)
255     {
256       if (upball_type == 0 && dnball_type == 1)
257         shift_amount *= 1.25;
258       else if (upball_type == 0 && dnball_type == 2)
259         shift_amount *= 1.35;
260       else if (dnball_type == 0 && upball_type == 1)
261         shift_amount *= 0.7;
262       else if (dnball_type == 0 && upball_type == 2)
263         shift_amount *= 0.75;
264     }
265   
266   /*
267    * Fix issue #44:
268    *
269    * Dots from left note head collide with right note head. Only occurs
270    * with a close half collide, if the left note head is between
271    * lines and the right note head is on a line, and if right note head
272    * hasn't got any dots.
273    */
274   if (close_half_collide
275       && Rhythmic_head::dot_count (nu)
276       && !Rhythmic_head::dot_count (nd))
277     {
278       Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
279       if (!Staff_symbol_referencer::on_line (staff, ups[0]))
280         {
281           Grob *d = unsmob_grob (nu->get_object ("dot"));
282           Grob *parent = d->get_parent (X_AXIS);
283           if (Dot_column::has_interface (parent))
284             Side_position_interface::add_support (parent, nd);
285         }
286     }
287
288   /* For full or close half collisions, the right hand head may
289      obscure dots.  Move dots to the right.  */
290   if (abs (shift_amount) > 1e-6
291       && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
292       && (full_collide || close_half_collide))
293     {
294       Grob *d = unsmob_grob (nd->get_object ("dot"));
295       Grob *parent = d->get_parent (X_AXIS);
296
297       /*
298         FIXME:
299
300         |
301         x . o
302         |
303
304
305         the . is put right of o which is erroneous o force-shifted
306         far to the right.
307       */
308       if (Dot_column::has_interface (parent))
309         {
310           Grob *stem = unsmob_grob (nu->get_object ("stem"));
311           extract_grob_set (stem, "note-heads", heads);
312           for (vsize i = 0; i < heads.size (); i++)
313             Side_position_interface::add_support (parent, heads[i]);
314         }
315     }
316
317   Direction d = UP;
318   do
319     {
320       for (vsize i = 0; i < clash_groups[d].size (); i++)
321         (*offsets)[d][i] += d * shift_amount;
322     }
323   while ((flip (&d)) != UP);
324 }
325
326
327 MAKE_SCHEME_CALLBACK (Note_collision_interface, calc_positioning_done, 1) 
328 SCM
329 Note_collision_interface::calc_positioning_done (SCM smob)
330 {
331   Grob *me = unsmob_grob (smob);
332   me->set_property ("positioning-done", SCM_BOOL_T);
333   
334   Drul_array<vector<Grob*> > cg = get_clash_groups (me);
335
336   Direction d = UP;
337   do
338     {
339       for (vsize i = cg[d].size (); i--; )
340         {
341           /*
342             Trigger positioning
343            */
344           cg[d][i]->extent (me, X_AXIS);
345         }
346     }
347   while (flip (&d) != UP);
348
349   SCM autos (automatic_shift (me, cg));
350   SCM hand (forced_shift (me));
351
352   Real wid = 0.0;
353   do
354     {
355       if (cg[d].size ())
356         {
357           Grob *h = cg[d][0];
358           Grob *fh = Note_column::first_head (h);
359           if (fh)
360             wid = fh->extent (h, X_AXIS).length ();
361         }
362     }
363   while (flip (&d) != UP);
364
365   vector<Grob*> done;
366   Real left_most = 1e6;
367
368   vector<Real> amounts;
369   for (; scm_is_pair (hand); hand = scm_cdr (hand))
370     {
371       Grob *s = unsmob_grob (scm_caar (hand));
372       Real amount = scm_to_double (scm_cdar (hand)) * wid;
373
374       done.push_back (s);
375       amounts.push_back (amount);
376       if (amount < left_most)
377         left_most = amount;
378     }
379   for (; scm_is_pair (autos); autos = scm_cdr (autos))
380     {
381       Grob *s = unsmob_grob (scm_caar (autos));
382       Real amount = scm_to_double (scm_cdar (autos)) * wid;
383
384       vsize x = find (done, s) - done.begin ();
385       if (x == VPOS || x >= done.size ())
386         {
387           done.push_back (s);
388           amounts.push_back (amount);
389           if (amount < left_most)
390             left_most = amount;
391         }
392     }
393
394   for (vsize i = 0; i < amounts.size (); i++)
395     done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
396
397   return SCM_BOOL_T;
398 }
399
400 Drul_array < vector<Grob*> >
401 Note_collision_interface::get_clash_groups (Grob *me)
402 {
403   Drul_array<vector<Grob*> > clash_groups;
404
405   extract_grob_set (me, "elements", elements);
406   for (vsize i = 0; i < elements.size (); i++)
407     {
408       Grob *se = elements[i];
409       if (Note_column::has_interface (se))
410         {
411           if (!Note_column::dir (se))
412             {
413               se->programming_error ("note-column has no direction");
414             }
415           else
416             clash_groups[Note_column::dir (se)].push_back (se);
417         }
418     }
419
420   Direction d = UP;
421   do
422     {
423       vector<Grob*> &clashes (clash_groups[d]);
424       vector_sort (clashes, Note_column::shift_less);
425     }
426   while ((flip (&d)) != UP);
427
428   return clash_groups;
429 }
430
431 /** This complicated routine moves note columns around horizontally to
432     ensure that notes don't clash.
433
434 */
435 SCM
436 Note_collision_interface::automatic_shift (Grob *me,
437                                            Drul_array<vector<Grob*> > clash_groups)
438 {
439   Drul_array < vector<int> > shifts;
440   SCM tups = SCM_EOL;
441
442   Direction d = UP;
443   do
444     {
445       vector<int> &shift (shifts[d]);
446       vector<Grob*> &clashes (clash_groups[d]);
447
448       for (vsize i = 0; i < clashes.size (); i++)
449         {
450           SCM sh
451             = clashes[i]->get_property ("horizontal-shift");
452
453           if (scm_is_number (sh))
454             shift.push_back (scm_to_int (sh));
455           else
456             shift.push_back (0);
457         }
458
459       for (vsize i = 1; i < shift.size (); i++)
460         {
461           if (shift[i - 1] == shift[i])
462             {
463               clashes[0]->warning (_ ("ignoring too many clashing note columns"));
464               return tups;
465             }
466         }
467     }
468   while ((flip (&d)) != UP);
469
470   Drul_array<vector<Slice> > extents;
471   Drul_array<vector<Real> > offsets;
472   d = UP;
473   do
474     {
475       for (vsize i = 0; i < clash_groups[d].size (); i++)
476         {
477           Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
478           s[LEFT]--;
479           s[RIGHT]++;
480           extents[d].push_back (s);
481           offsets[d].push_back (d * 0.5 * i);
482         }
483     }
484   while ((flip (&d)) != UP);
485
486   /*
487     do horizontal shifts of each direction
488
489     |
490     x||
491     x||
492     x|
493   */
494
495   do
496     {
497       for (vsize i = 1; i < clash_groups[d].size (); i++)
498         {
499           Slice prev = extents[d][i - 1];
500           prev.intersect (extents[d][i]);
501           if (prev.length () > 0
502               || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
503             for (vsize j = i; j < clash_groups[d].size (); j++)
504               offsets[d][j] += d * 0.5;
505         }
506     }
507   while ((flip (&d)) != UP);
508
509
510   /*
511     see input/regression/dot-up-voice-collision.ly
512    */
513   for (vsize i = 0; i < clash_groups[UP].size (); i++)
514     {
515       Grob *g = clash_groups[UP][i];
516       Grob *dc = Note_column::dot_column (g);
517       
518       if (dc)
519         for (vsize j = i + 1;  j < clash_groups[UP].size (); j++)
520           {
521             Grob *stem = Note_column::get_stem (clash_groups[UP][j]);
522             Side_position_interface::add_support (dc, stem);
523           }
524     }
525   
526   /*
527     Check if chords are meshing
528   */
529
530   check_meshing_chords (me, &offsets, extents, clash_groups);
531
532   do
533     {
534       for (vsize i = 0; i < clash_groups[d].size (); i++)
535         tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
536                                    scm_from_double (offsets[d][i])),
537                          tups);
538     }
539   while (flip (&d) != UP);
540
541   return tups;
542 }
543
544 SCM
545 Note_collision_interface::forced_shift (Grob *me)
546 {
547   SCM tups = SCM_EOL;
548
549   extract_grob_set (me, "elements", elements);
550   for (vsize i = 0; i < elements.size (); i++)
551     {
552       Grob *se = elements[i];
553
554       SCM force = se->get_property ("force-hshift");
555       if (scm_is_number (force))
556         {
557           tups = scm_cons (scm_cons (se->self_scm (), force),
558                            tups);
559         }
560     }
561   return tups;
562 }
563
564 void
565 Note_collision_interface::add_column (Grob *me, Grob *ncol)
566 {
567   ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
568   Axis_group_interface::add_element (me, ncol);
569 }
570
571 ADD_INTERFACE (Note_collision_interface,
572                "An object that handles collisions between notes with different stem "
573                "directions and horizontal shifts. Most of the interesting properties "
574                "are to be set in @ref{note-column-interface}: these are "
575                "@code{force-hshift} and @code{horizontal-shift}.",
576
577                /* properties */
578                "merge-differently-dotted "
579                "merge-differently-headed "
580                "positioning-done ");