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