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