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