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