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