]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-collision.cc
run cvs up before patching.
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "note-collision.hh"
10
11 #include "axis-group-interface.hh"
12 #include "dot-column.hh"
13 #include "international.hh"
14 #include "note-column.hh"
15 #include "note-head.hh"
16 #include "output-def.hh"
17 #include "pointer-group-interface.hh"
18 #include "rhythmic-head.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "side-position-interface.hh"
21 #include "stem.hh"
22 #include "warn.hh"
23
24
25 void
26 check_meshing_chords (Grob *me,
27                       Drul_array<vector<Real> > *offsets,
28                       Drul_array<vector<Slice> > const &extents,
29                       Drul_array<vector<Grob*> > const &clash_groups)
30
31 {
32   if (!extents[UP].size () || ! extents[DOWN].size ())
33     return;
34
35   Grob *cu = clash_groups[UP][0];
36   Grob *cd = clash_groups[DOWN][0];
37
38   /* Every note column should have a stem, but avoid a crash. */
39   if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
40     return;
41
42   Drul_array<Grob*> stems (Note_column::get_stem (cd),
43                            Note_column::get_stem (cu));
44   
45   Grob *nu = Note_column::first_head (cu);
46   Grob *nd = Note_column::first_head (cd);
47
48   vector<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
49   vector<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
50
51   /* Too far apart to collide.  */
52   if (ups[0] > dps.back () + 1)
53     return;
54
55   /* Merge heads if the notes lie the same line, or if the "stem-up-note" is
56      above the "stem-down-note". */
57   bool merge_possible = (ups[0] >= dps[0]) && (ups.back () >= dps.back ());
58
59   /* Do not merge notes typeset in different style. */
60   if (!ly_is_equal (nu->get_property ("style"),
61                     nd->get_property ("style")))
62     merge_possible = false;
63
64   int upball_type = Rhythmic_head::duration_log (nu);
65   int dnball_type = Rhythmic_head::duration_log (nd);
66
67   /* Do not merge whole notes (or longer, like breve, longa, maxima).  */
68   if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
69     merge_possible = false;
70
71   if (merge_possible
72       && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
73       && !to_boolean (me->get_property ("merge-differently-dotted")))
74     merge_possible = false;
75
76   /* Can only merge different heads if merge-differently-headed is
77      set. */
78   if (merge_possible
79       && upball_type != dnball_type
80       && !to_boolean (me->get_property ("merge-differently-headed")))
81     merge_possible = false;
82
83   if (merge_possible
84       && nu->get_property ("style") == ly_symbol2scm ("fa")
85       && nd->get_property ("style") == ly_symbol2scm ("fa"))
86     {
87       Interval uphead_size = nu->extent (nu, Y_AXIS);
88       Offset att =  Offset (0.0, -1.0);
89       nu->set_property ("stem-attachment", ly_offset2scm (att));
90       nu->set_property ("transparent", SCM_BOOL_T); 
91     }
92   
93   /* Should never merge quarter and half notes, as this would make
94      them indistinguishable.  */
95   if (merge_possible
96       && ((Stem::duration_log (stems[UP]) == 1
97            && Stem::duration_log (stems[DOWN]) == 2)
98           || (Stem::duration_log (stems[UP]) == 2
99               && Stem::duration_log (stems[DOWN]) == 1)))
100     merge_possible = false;
101
102   /*
103     this case (distant half collide),
104
105     |
106     x |
107     | x
108     |
109
110     the noteheads may be closer than this case (close half collide)
111
112     |
113     |
114     x
115     x
116     |
117     |
118
119   */
120
121   /* TODO: filter out the 'o's in this configuration, since they're no
122      part in the collision.
123
124      |
125      x|o
126      x|o
127      x
128
129   */
130
131   bool close_half_collide = false;
132   bool distant_half_collide = false;
133   bool full_collide = false;
134
135   for (vsize i = 0, j = 0; 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.back ())
148         merge_possible = false;
149       else if (dps[j] > ups[0] && dps[j] < ups.back ())
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   Real shift_amount = 1;
171
172   bool touch = (ups[0] >= dps.back ());
173   if (touch)
174     shift_amount *= -1;
175
176   /* For full collisions, the right hand head may obscure dots, so
177      make sure the dotted heads go to the right.  */
178   bool stem_to_stem = false;
179   if (full_collide)
180     if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
181       shift_amount = 1;
182     else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
183       stem_to_stem = true;
184
185   if (merge_possible)
186     {
187       shift_amount = 0;
188
189       /* If possible, don't wipe any heads. Else, wipe shortest head,
190          or head with smallest amount of dots.  Note: when merging
191          different heads, dots on the smaller one disappear. */
192       Grob *wipe_ball = 0;
193       Grob *dot_wipe_head = nu;
194
195       if (upball_type == dnball_type)
196         {
197           if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
198             {
199               wipe_ball = nd;
200               dot_wipe_head = nd;
201             }
202           else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
203             {
204               dot_wipe_head = nu;
205               wipe_ball = nu;
206             }
207           else
208             dot_wipe_head = nu;
209         }
210       else if (dnball_type > upball_type)
211         {
212           wipe_ball = nd;
213           dot_wipe_head = nd;
214         }
215       else if (dnball_type < upball_type)
216         {
217           wipe_ball = nu;
218           dot_wipe_head = nu;
219         }
220
221       if (dot_wipe_head)
222         {
223           if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
224             d->suicide ();
225         }
226
227       if (wipe_ball && wipe_ball->is_live ())
228         {
229           wipe_ball->set_property ("transparent", SCM_BOOL_T);
230         }
231     }
232   /* TODO: these numbers are magic; should devise a set of grob props
233      to tune this behavior.  */
234   else if (stem_to_stem)
235     shift_amount = -abs (shift_amount) * 0.65;
236   else if (close_half_collide && !touch)
237     shift_amount *= 0.52;
238   else if (distant_half_collide && !touch)
239     shift_amount *= 0.4;
240   else if (distant_half_collide || close_half_collide || full_collide)
241     shift_amount *= 0.5;
242
243   /* we're meshing.  */
244   else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
245     shift_amount *= 0.1;
246   else
247     shift_amount *= 0.17;
248
249   /*
250    * Fix issue #44:
251    *
252    * Dots from left note head collide with right note head. Only occurs
253    * with a close half collide, if the left note head is between
254    * lines and the right note head is on a line, and if right note head
255    * hasn't got any dots.
256    */
257   if (close_half_collide
258       && Rhythmic_head::dot_count (nu)
259       && !Rhythmic_head::dot_count (nd))
260     {
261       Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
262       if (!Staff_symbol_referencer::on_line (staff, ups[0]))
263         {
264           Grob *d = unsmob_grob (nu->get_object ("dot"));
265           Grob *parent = d->get_parent (X_AXIS);
266           if (Dot_column::has_interface (parent))
267             Side_position_interface::add_support (parent, nd);
268         }
269     }
270
271   /* For full or close half collisions, the right hand head may
272      obscure dots.  Move dots to the right.  */
273   if (abs (shift_amount) > 1e-6
274       && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
275       && (full_collide || close_half_collide))
276     {
277       Grob *d = unsmob_grob (nd->get_object ("dot"));
278       Grob *parent = d->get_parent (X_AXIS);
279
280       /*
281         FIXME:
282
283         |
284         x . o
285         |
286
287
288         the . is put right of o which is erroneous o force-shifted
289         far to the right.
290       */
291       if (Dot_column::has_interface (parent))
292         Side_position_interface::add_support (parent, nu);
293     }
294
295   Direction d = UP;
296   do
297     {
298       for (vsize i = 0; i < clash_groups[d].size (); i++)
299         (*offsets)[d][i] += d * shift_amount;
300     }
301   while ((flip (&d)) != UP);
302 }
303
304
305 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1) 
306 SCM
307 Note_collision_interface::calc_positioning_done (SCM smob)
308 {
309   Grob *me = unsmob_grob (smob);  
310   Drul_array<vector<Grob*> > cg = get_clash_groups (me);
311
312   Direction d = UP;
313   do
314     {
315       for (vsize i = cg[d].size(); i--; )
316         {
317           /*
318             Trigger positioning
319            */
320           cg[d][i]->extent (me, X_AXIS);
321         }
322     }
323   while (flip (&d) != UP);
324
325   SCM autos (automatic_shift (me, cg));
326   SCM hand (forced_shift (me));
327
328   Real wid = 0.0;
329   do
330     {
331       if (cg[d].size ())
332         {
333           Grob *h = cg[d][0];
334           Grob *fh = Note_column::first_head (h);
335           if (fh)
336             wid = fh->extent (h, X_AXIS).length ();
337         }
338     }
339   while (flip (&d) != UP);
340
341   vector<Grob*> done;
342   Real left_most = 1e6;
343
344   vector<Real> amounts;
345   for (; scm_is_pair (hand); hand = scm_cdr (hand))
346     {
347       Grob *s = unsmob_grob (scm_caar (hand));
348       Real amount = scm_to_double (scm_cdar (hand)) * wid;
349
350       done.push_back (s);
351       amounts.push_back (amount);
352       if (amount < left_most)
353         left_most = amount;
354     }
355   for (; scm_is_pair (autos); autos = scm_cdr (autos))
356     {
357       Grob *s = unsmob_grob (scm_caar (autos));
358       Real amount = scm_to_double (scm_cdar (autos)) * wid;
359
360       vsize x = find (done, s) - done.begin ();
361       if (x == VPOS || x >= done.size ())
362         {
363           done.push_back (s);
364           amounts.push_back (amount);
365           if (amount < left_most)
366             left_most = amount;
367         }
368     }
369
370   for (vsize i = 0; i < amounts.size (); i++)
371     done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
372
373   return SCM_BOOL_T;
374 }
375
376 Drul_array < vector<Grob*> >
377 Note_collision_interface::get_clash_groups (Grob *me)
378 {
379   Drul_array<vector<Grob*> > clash_groups;
380
381   extract_grob_set (me, "elements", elements);
382   for (vsize i = 0; i < elements.size (); i++)
383     {
384       Grob *se = elements[i];
385       if (Note_column::has_interface (se))
386         {
387           if (!Note_column::dir (se))
388             {
389               se->programming_error ("note-column has no direction");
390             }
391           else
392             clash_groups[Note_column::dir (se)].push_back (se);
393         }
394     }
395
396   Direction d = UP;
397   do
398     {
399       vector<Grob*> &clashes (clash_groups[d]);
400       vector_sort (clashes, Note_column::shift_less);
401     }
402   while ((flip (&d)) != UP);
403
404   return clash_groups;
405 }
406
407 /** This complicated routine moves note columns around horizontally to
408     ensure that notes don't clash.
409
410     This should be put into Scheme.
411 */
412 SCM
413 Note_collision_interface::automatic_shift (Grob *me,
414                                            Drul_array < vector<Grob*>
415                                            > clash_groups)
416 {
417   Drul_array < vector<int> > shifts;
418   SCM tups = SCM_EOL;
419
420   Direction d = UP;
421   do
422     {
423       vector<int> &shift (shifts[d]);
424       vector<Grob*> &clashes (clash_groups[d]);
425
426       for (vsize i = 0; i < clashes.size (); i++)
427         {
428           SCM sh
429             = clashes[i]->get_property ("horizontal-shift");
430
431           if (scm_is_number (sh))
432             shift.push_back (scm_to_int (sh));
433           else
434             shift.push_back (0);
435         }
436
437       for (vsize i = 1; i < shift.size (); i++)
438         {
439           if (shift[i - 1] == shift[i])
440             {
441               clashes[0]->warning (_ ("ignoring too many clashing note columns"));
442               return tups;
443             }
444         }
445     }
446   while ((flip (&d)) != UP);
447
448   Drul_array<vector<Slice> > extents;
449   Drul_array<vector<Real> > offsets;
450   d = UP;
451   do
452     {
453       for (vsize i = 0; i < clash_groups[d].size (); i++)
454         {
455           Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
456           s[LEFT]--;
457           s[RIGHT]++;
458           extents[d].push_back (s);
459           offsets[d].push_back (d * 0.5 * i);
460         }
461     }
462   while ((flip (&d)) != UP);
463
464   /*
465     do horizontal shifts of each direction
466
467     |
468     x||
469     x||
470     x|
471   */
472
473   do
474     {
475       for (vsize i = 1; i < clash_groups[d].size (); i++)
476         {
477           Slice prev = extents[d][i - 1];
478           prev.intersect (extents[d][i]);
479           if (prev.length () > 0
480               || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
481             for (vsize j = i; j < clash_groups[d].size (); j++)
482               offsets[d][j] += d * 0.5;
483         }
484     }
485   while ((flip (&d)) != UP);
486
487   /*
488     Check if chords are meshing
489   */
490
491   check_meshing_chords (me, &offsets, extents, clash_groups);
492
493   do
494     {
495       for (vsize i = 0; i < clash_groups[d].size (); i++)
496         tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
497                                    scm_from_double (offsets[d][i])),
498                          tups);
499     }
500   while (flip (&d) != UP);
501
502   return tups;
503 }
504
505 SCM
506 Note_collision_interface::forced_shift (Grob *me)
507 {
508   SCM tups = SCM_EOL;
509
510   extract_grob_set (me, "elements", elements);
511   for (vsize i = 0; i < elements.size (); i++)
512     {
513       Grob *se = elements[i];
514
515       SCM force = se->get_property ("force-hshift");
516       if (scm_is_number (force))
517         {
518           tups = scm_cons (scm_cons (se->self_scm (), force),
519                            tups);
520         }
521     }
522   return tups;
523 }
524
525 void
526 Note_collision_interface::add_column (Grob *me, Grob *ncol)
527 {
528   ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
529   Axis_group_interface::add_element (me, ncol);
530 }
531
532 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
533                "An object that handles collisions between notes with different stem "
534                "directions and horizontal shifts. Most of the interesting properties "
535                "are to be set in @ref{note-column-interface}: these are "
536                "@code{force-hshift} and @code{horizontal-shift}.",
537
538                /* properties */
539                "merge-differently-dotted "
540                "merge-differently-headed "
541                "positioning-done ");