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