]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-collision.cc
* lily/note-collision.cc (do_shifts): align colliding notes to
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10
11 #include "warn.hh"
12 #include "note-collision.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 "item.hh"
19 #include "stem.hh"
20 #include "side-position-interface.hh"
21 #include "dot-column.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) ly_scm2int (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_make_real (0.0);
41 }
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
75
76   /* Do not merge notes typeset in different style. */
77   if ( !ly_c_equal_p (nu->get_property ("style"),
78                      nd->get_property ("style") ) )
79     merge_possible = false;
80   
81   int upball_type = Note_head::get_balltype (nu);
82   int dnball_type = Note_head::get_balltype (nd);
83   
84   /* Do not merge whole notes (or longer, like breve, longa, maxima).  */
85   if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
86     merge_possible = false;
87
88   if (merge_possible
89       && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
90       && !to_boolean (me->get_property ("merge-differently-dotted")))
91     merge_possible = false;
92
93   /* Can only merge different heads if merge-differently-headed is
94      set. */
95   if (merge_possible
96       && upball_type != dnball_type
97       && !to_boolean (me->get_property ("merge-differently-headed")))
98     merge_possible = false;
99
100   /* Should never merge quarter and half notes, as this would make
101      them indistinguishable.  */
102   if (merge_possible
103       && ((Rhythmic_head::duration_log (nu) == 1
104            && Rhythmic_head::duration_log (nd) == 2)
105           || (Rhythmic_head::duration_log (nu) == 2
106              && Rhythmic_head::duration_log (nd) == 1)))
107     merge_possible = false;
108
109
110   /*
111     this case (distant half collide), 
112     
113         |
114       x |
115      | x
116      |
117
118    the noteheads may be closer than this case (close half collide)
119
120        |
121        |
122       x 
123      x
124     |
125     |
126     
127    */
128   
129   /* TODO: filter out the 'o's in this configuration, since they're no
130   part in the collision.
131
132      |
133     x|o
134     x|o
135     x
136     
137    */
138   
139   bool close_half_collide = false;
140   bool distant_half_collide = false;  
141   bool full_collide = false;  
142
143   int i = 0, j=0;
144   while (i < ups.size () && j < dps.size ())
145   {
146     if (abs (ups[i] - dps[j]) == 1)
147       {
148         merge_possible = false;
149         if (ups[i] > dps[j])
150           close_half_collide = true;
151         else
152           distant_half_collide = true;
153       }
154     else if (ups[i]==dps[j])
155       full_collide = true;
156     else if (ups[i] >dps[0] && ups[i] < dps.top ())
157       merge_possible = false;
158     else if (dps[j] >ups[0] && dps[j] < ups.top ())
159       merge_possible = false;
160     
161     if (ups[i] < dps[j])
162       i++;
163     else if (ups[i] > dps[j])
164       j++;
165     else
166       {
167         i++;
168         j++;
169       }
170   }
171
172   full_collide = full_collide || (close_half_collide
173                                   && distant_half_collide);
174   
175   Drul_array<Real> center_note_shifts;
176   center_note_shifts[LEFT] = 0.0;
177   center_note_shifts[RIGHT] = 0.0;
178
179   
180   Real shift_amount = 1;
181
182   bool touch = (ups[0] >= dps.top ());
183   if (touch)
184     shift_amount *= -1;
185
186   /* For full collisions, the right hand head may obscure dots, so
187      make sure the dotted heads go to the right.  */
188   bool stem_to_stem = false;
189   if (full_collide)
190     if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
191       shift_amount = 1;
192     else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
193       stem_to_stem = true;
194   
195   if (merge_possible)
196     {
197       shift_amount = 0;
198
199
200       /* If possible, don't wipe any heads. Else, wipe shortest head,
201          or head with smallest amount of dots.  Note: when merging
202          different heads, dots on the smaller one disappear. */
203       Grob *wipe_ball = 0;
204       Grob *dot_wipe_head = nu;
205       
206       if (upball_type == dnball_type)
207         {
208           if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
209             {
210               wipe_ball = nd;
211               dot_wipe_head = nd;
212             }
213           else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
214             {
215               dot_wipe_head = nu;
216               wipe_ball = nu;
217             }
218           else
219             {
220               dot_wipe_head = nu;
221             }
222         }
223       else if (dnball_type > upball_type)
224         {
225           wipe_ball = nd;
226           dot_wipe_head = nd;
227         }
228       else if (dnball_type < upball_type)
229         {
230           wipe_ball = nu;
231           dot_wipe_head = nu;
232         }
233
234       if (dot_wipe_head)
235         {
236           if (Grob *d = unsmob_grob (dot_wipe_head->get_property ("dot")))
237             d->suicide ();
238         }
239       
240       if (wipe_ball && wipe_ball->is_live ())
241         {
242           wipe_ball->set_property ("transparent", SCM_BOOL_T);
243           wipe_ball->set_property ("stencil", SCM_EOL);
244         }
245     }
246   /* TODO: these numbers are magic; should devise a set of grob props
247      to tune this behavior.  */
248   else if (stem_to_stem)
249     shift_amount = -abs (shift_amount) * 0.65; 
250   else if (close_half_collide && !touch)
251     shift_amount *= 0.52;
252   else if (distant_half_collide && !touch)
253     shift_amount *= 0.4;
254   else if (distant_half_collide || close_half_collide || full_collide)
255     shift_amount *= 0.5;
256   
257   /* we're meshing.  */
258   else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
259     shift_amount *= 0.1;
260   else
261     shift_amount *= 0.25;
262
263   /* For full or close half collisions, the right hand head may
264      obscure dots.  Move dots to the right.  */
265   if (abs (shift_amount) > 1e-6
266       && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
267       && (full_collide || close_half_collide))
268     {
269       Grob *d = unsmob_grob (nd->get_property ("dot"));
270       Grob *parent = d->get_parent (X_AXIS);
271       if (Dot_column::has_interface (parent))
272         Side_position_interface::add_support (parent, nu);
273     }
274
275   Direction d = UP;
276   do
277     {
278       for (int i=0; i < clash_groups[d].size (); i++)
279         (*offsets)[d][i] += d * shift_amount;
280     }
281   while ((flip (&d))!= UP);
282 }
283
284 void
285 Note_collision_interface::do_shifts (Grob* me)
286 {
287   Drul_array< Link_array <Grob>  > cg = get_clash_groups (me);
288
289   SCM autos (automatic_shift (me, cg));
290   SCM hand (forced_shift (me));
291   
292   Direction d = UP;
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 (; ly_c_pair_p (hand); hand =ly_cdr (hand))
309     {
310       Grob * s = unsmob_grob (ly_caar (hand));
311       Real amount = ly_scm2double (ly_cdar (hand)) * wid;
312
313       done.push (s);
314       amounts.push (amount);
315       if (amount < left_most)
316         left_most = amount;
317       
318     }
319   for (; ly_c_pair_p (autos); autos =ly_cdr (autos))
320     {
321       Grob * s = unsmob_grob (ly_caar (autos));
322       Real amount = ly_scm2double (ly_cdar (autos)) *wid;
323       
324       if (!done.find (s))
325         {
326           done.push (s);
327           amounts.push (amount);
328           if (amount < left_most)
329             left_most = amount;
330         }
331     }
332
333   for (int i = 0; i < amounts.size(); i++)
334     {
335       done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
336     }
337 }
338
339 Drul_array< Link_array <Grob>  >
340 Note_collision_interface::get_clash_groups (Grob *me)
341 {
342   Drul_array<Link_array<Grob> > clash_groups;
343  
344   SCM s = me->get_property ("elements");
345   for (; ly_c_pair_p (s); s = ly_cdr (s))
346     {
347       SCM car = ly_car (s);
348
349       Grob * se = unsmob_grob (car);
350       if (Note_column::has_interface (se))
351         clash_groups[Note_column::dir (se)].push (se);
352     }
353   
354   Direction d = UP;
355   do
356     {
357       Link_array<Grob> & clashes (clash_groups[d]);
358       clashes.sort (Note_column::shift_compare);
359     }
360   while ((flip (&d))!= UP);
361
362   return clash_groups;
363 }
364
365 /** This complicated routine moves note columns around horizontally to
366   ensure that notes don't clash.
367
368   This should be put into Scheme.  
369   */
370 SCM
371 Note_collision_interface::automatic_shift (Grob *me,
372                             Drul_array< Link_array <Grob> > 
373                             clash_groups)
374 {
375   Drul_array<Array<int> > shifts;
376   SCM  tups = SCM_EOL;
377
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 (ly_c_number_p (sh))
391             shift.push (ly_scm2int (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 (_ ("Too many clashing notecolumns.  Ignoring them."));
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   /*
448     Check if chords are meshing
449    */
450
451   check_meshing_chords (me, &offsets, extents, clash_groups);
452   
453   do
454     {
455       for (int i=0; i < clash_groups[d].size (); i++)
456         tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
457                                  scm_make_real (offsets[d][i])),
458                         tups);
459     }
460   while (flip (&d) != UP);
461   return tups;
462 }
463
464
465 SCM
466 Note_collision_interface::forced_shift (Grob *me)
467 {
468   SCM tups = SCM_EOL;
469   
470   SCM s = me->get_property ("elements");
471   for (; ly_c_pair_p (s); s = ly_cdr (s))
472     {
473       Grob * se = unsmob_grob (ly_car (s));
474
475       SCM force =  se->get_property ("force-hshift");
476       if (ly_c_number_p (force))
477         {
478           tups = scm_cons (scm_cons (se->self_scm (), force),
479                           tups);
480         }
481     }
482   return tups;
483 }
484
485 void
486 Note_collision_interface::add_column (Grob*me,Grob* ncol)
487 {
488   ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
489   Axis_group_interface::add_element (me, ncol);
490   me->add_dependency (ncol);
491 }
492
493
494 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
495                "An object that handles collisions between notes with different stem " 
496                "directions and horizontal shifts. Most of the interesting properties "
497                "are to be set in @ref{note-column-interface}: these are "
498                "@code{force-hshift} and @code{horizontal-shift}."
499
500                ,
501                
502                "merge-differently-dotted merge-differently-headed positioning-done");