]> 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--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<Array<Real> > *offsets,
27                       Drul_array<Array<Slice> > const &extents,
28                       Drul_array<Link_array<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   Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
45   Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
46
47   /* Too far apart to collide.  */
48   if (ups[0] > dps.top () + 1)
49     return;
50
51   // FIXME: what's this?
52   bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
53
54   /* Do not merge notes typeset in different style. */
55   if (!ly_is_equal (nu->get_property ("style"),
56                     nd->get_property ("style")))
57     merge_possible = false;
58
59   int upball_type = Note_head::get_balltype (nu);
60   int dnball_type = Note_head::get_balltype (nd);
61
62   /* Do not merge whole notes (or longer, like breve, longa, maxima).  */
63   if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
64     merge_possible = false;
65
66   if (merge_possible
67       && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
68       && !to_boolean (me->get_property ("merge-differently-dotted")))
69     merge_possible = false;
70
71   /* Can only merge different heads if merge-differently-headed is
72      set. */
73   if (merge_possible
74       && upball_type != dnball_type
75       && !to_boolean (me->get_property ("merge-differently-headed")))
76     merge_possible = false;
77
78   merge_possible = merge_possible &&
79     !(nu->get_property ("style") == ly_symbol2scm ("fa")
80       && nd->get_property ("style") == ly_symbol2scm ("fa"));
81   
82   
83   /* Should never merge quarter and half notes, as this would make
84      them indistinguishable.  */
85   if (merge_possible
86       && ((Rhythmic_head::duration_log (nu) == 1
87            && Rhythmic_head::duration_log (nd) == 2)
88           || (Rhythmic_head::duration_log (nu) == 2
89               && Rhythmic_head::duration_log (nd) == 1)))
90     merge_possible = false;
91
92   /*
93     this case (distant half collide),
94
95     |
96     x |
97     | x
98     |
99
100     the noteheads may be closer than this case (close half collide)
101
102     |
103     |
104     x
105     x
106     |
107     |
108
109   */
110
111   /* TODO: filter out the 'o's in this configuration, since they're no
112      part in the collision.
113
114      |
115      x|o
116      x|o
117      x
118
119   */
120
121   bool close_half_collide = false;
122   bool distant_half_collide = false;
123   bool full_collide = false;
124
125   int i = 0, j = 0;
126   while (i < ups.size () && j < dps.size ())
127     {
128       if (abs (ups[i] - dps[j]) == 1)
129         {
130           merge_possible = false;
131           if (ups[i] > dps[j])
132             close_half_collide = true;
133           else
134             distant_half_collide = true;
135         }
136       else if (ups[i] == dps[j])
137         full_collide = true;
138       else if (ups[i] > dps[0] && ups[i] < dps.top ())
139         merge_possible = false;
140       else if (dps[j] > ups[0] && dps[j] < ups.top ())
141         merge_possible = false;
142
143       if (ups[i] < dps[j])
144         i++;
145       else if (ups[i] > dps[j])
146         j++;
147       else
148         {
149           i++;
150           j++;
151         }
152     }
153
154   full_collide = full_collide || (close_half_collide
155                                   && distant_half_collide);
156
157   Drul_array<Real> center_note_shifts;
158   center_note_shifts[LEFT] = 0.0;
159   center_note_shifts[RIGHT] = 0.0;
160
161   Real shift_amount = 1;
162
163   bool touch = (ups[0] >= dps.top ());
164   if (touch)
165     shift_amount *= -1;
166
167   /* For full collisions, the right hand head may obscure dots, so
168      make sure the dotted heads go to the right.  */
169   bool stem_to_stem = false;
170   if (full_collide)
171     if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
172       shift_amount = 1;
173     else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
174       stem_to_stem = true;
175
176   if (merge_possible)
177     {
178       shift_amount = 0;
179
180       /* If possible, don't wipe any heads. Else, wipe shortest head,
181          or head with smallest amount of dots.  Note: when merging
182          different heads, dots on the smaller one disappear. */
183       Grob *wipe_ball = 0;
184       Grob *dot_wipe_head = nu;
185
186       if (upball_type == dnball_type)
187         {
188           if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
189             {
190               wipe_ball = nd;
191               dot_wipe_head = nd;
192             }
193           else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
194             {
195               dot_wipe_head = nu;
196               wipe_ball = nu;
197             }
198           else
199             dot_wipe_head = nu;
200         }
201       else if (dnball_type > upball_type)
202         {
203           wipe_ball = nd;
204           dot_wipe_head = nd;
205         }
206       else if (dnball_type < upball_type)
207         {
208           wipe_ball = nu;
209           dot_wipe_head = nu;
210         }
211
212       if (dot_wipe_head)
213         {
214           if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
215             d->suicide ();
216         }
217
218       if (wipe_ball && wipe_ball->is_live ())
219         {
220           wipe_ball->set_property ("transparent", SCM_BOOL_T);
221         }
222     }
223   /* TODO: these numbers are magic; should devise a set of grob props
224      to tune this behavior.  */
225   else if (stem_to_stem)
226     shift_amount = -abs (shift_amount) * 0.65;
227   else if (close_half_collide && !touch)
228     shift_amount *= 0.52;
229   else if (distant_half_collide && !touch)
230     shift_amount *= 0.4;
231   else if (distant_half_collide || close_half_collide || full_collide)
232     shift_amount *= 0.5;
233
234   /* we're meshing.  */
235   else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
236     shift_amount *= 0.1;
237   else
238     shift_amount *= 0.17;
239
240   /* For full or close half collisions, the right hand head may
241      obscure dots.  Move dots to the right.  */
242   if (abs (shift_amount) > 1e-6
243       && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
244       && (full_collide || close_half_collide))
245     {
246       Grob *d = unsmob_grob (nd->get_object ("dot"));
247       Grob *parent = d->get_parent (X_AXIS);
248
249       /*
250         FIXME:
251
252         |
253         x . o
254         |
255
256
257         the . is put right of o which is erroneous o force-shifted
258         far to the right.
259       */
260       if (Dot_column::has_interface (parent))
261         Side_position_interface::add_support (parent, nu);
262     }
263
264   Direction d = UP;
265   do
266     {
267       for (int i = 0; i < clash_groups[d].size (); i++)
268         (*offsets)[d][i] += d * shift_amount;
269     }
270   while ((flip (&d)) != UP);
271 }
272
273
274 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1) 
275 SCM
276 Note_collision_interface::calc_positioning_done (SCM smob)
277 {
278   Grob *me = unsmob_grob (smob);  
279   Drul_array<Link_array<Grob> > cg = get_clash_groups (me);
280
281   Direction d = UP;
282   do
283     {
284       for (int i = cg[d].size(); i--; )
285         {
286           /*
287             Trigger positioning
288            */
289           cg[d][i]->extent (me, X_AXIS);
290         }
291     }
292   while (flip (&d) != UP);
293
294   SCM autos (automatic_shift (me, cg));
295   SCM hand (forced_shift (me));
296
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   return SCM_BOOL_T;
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->set_property ("X-offset", Grob::x_parent_positioning_proc);
488   Axis_group_interface::add_element (me, ncol);
489 }
490
491 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
492                "An object that handles collisions between notes with different stem "
493                "directions and horizontal shifts. Most of the interesting properties "
494                "are to be set in @ref{note-column-interface}: these are "
495                "@code{force-hshift} and @code{horizontal-shift}.",
496
497                /* properties */
498                "merge-differently-dotted "
499                "merge-differently-headed "
500                "positioning-done");