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