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