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