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