2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "note-collision.hh"
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"
25 check_meshing_chords (Grob *me,
26 Drul_array<vector<Real> > *offsets,
27 Drul_array<vector<Slice> > const &extents,
28 Drul_array<vector<Grob*> > const &clash_groups)
31 if (!extents[UP].size () || ! extents[DOWN].size ())
34 Grob *cu = clash_groups[UP][0];
35 Grob *cd = clash_groups[DOWN][0];
37 /* Every note column should have a stem, but avoid a crash. */
38 if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
41 Grob *nu = Note_column::first_head (cu);
42 Grob *nd = Note_column::first_head (cd);
44 vector<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
45 vector<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
47 /* Too far apart to collide. */
48 if (ups[0] > dps.back () + 1)
51 // FIXME: what's this?
52 bool merge_possible = (ups[0] >= dps[0]) && (ups.back () >= dps.back ());
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;
59 int upball_type = Note_head::get_balltype (nu);
60 int dnball_type = Note_head::get_balltype (nd);
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;
67 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
68 && !to_boolean (me->get_property ("merge-differently-dotted")))
69 merge_possible = false;
71 /* Can only merge different heads if merge-differently-headed is
74 && upball_type != dnball_type
75 && !to_boolean (me->get_property ("merge-differently-headed")))
76 merge_possible = false;
79 && nu->get_property ("style") == ly_symbol2scm ("fa")
80 && nd->get_property ("style") == ly_symbol2scm ("fa"))
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);
88 /* Should never merge quarter and half notes, as this would make
89 them indistinguishable. */
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;
98 this case (distant half collide),
105 the noteheads may be closer than this case (close half collide)
116 /* TODO: filter out the 'o's in this configuration, since they're no
117 part in the collision.
126 bool close_half_collide = false;
127 bool distant_half_collide = false;
128 bool full_collide = false;
130 for (vsize i = 0, j = 0; i < ups.size () && j < dps.size (); )
132 if (abs (ups[i] - dps[j]) == 1)
134 merge_possible = false;
136 close_half_collide = true;
138 distant_half_collide = true;
140 else if (ups[i] == dps[j])
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;
149 else if (ups[i] > dps[j])
158 full_collide = full_collide || (close_half_collide
159 && distant_half_collide);
161 Drul_array<Real> center_note_shifts;
162 center_note_shifts[LEFT] = 0.0;
163 center_note_shifts[RIGHT] = 0.0;
165 Real shift_amount = 1;
167 bool touch = (ups[0] >= dps.back ());
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;
175 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
177 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
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. */
188 Grob *dot_wipe_head = nu;
190 if (upball_type == dnball_type)
192 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
197 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
205 else if (dnball_type > upball_type)
210 else if (dnball_type < upball_type)
218 if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
222 if (wipe_ball && wipe_ball->is_live ())
224 wipe_ball->set_property ("transparent", SCM_BOOL_T);
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)
235 else if (distant_half_collide || close_half_collide || full_collide)
239 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
242 shift_amount *= 0.17;
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))
250 Grob *d = unsmob_grob (nd->get_object ("dot"));
251 Grob *parent = d->get_parent (X_AXIS);
261 the . is put right of o which is erroneous o force-shifted
264 if (Dot_column::has_interface (parent))
265 Side_position_interface::add_support (parent, nu);
271 for (vsize i = 0; i < clash_groups[d].size (); i++)
272 (*offsets)[d][i] += d * shift_amount;
274 while ((flip (&d)) != UP);
278 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1)
280 Note_collision_interface::calc_positioning_done (SCM smob)
282 Grob *me = unsmob_grob (smob);
283 Drul_array<vector<Grob*> > cg = get_clash_groups (me);
288 for (vsize i = cg[d].size(); i--; )
293 cg[d][i]->extent (me, X_AXIS);
296 while (flip (&d) != UP);
298 SCM autos (automatic_shift (me, cg));
299 SCM hand (forced_shift (me));
307 wid = Note_column::first_head (h)->extent (h, X_AXIS).length ();
310 while (flip (&d) != UP);
313 Real left_most = 1e6;
315 vector<Real> amounts;
316 for (; scm_is_pair (hand); hand = scm_cdr (hand))
318 Grob *s = unsmob_grob (scm_caar (hand));
319 Real amount = scm_to_double (scm_cdar (hand)) * wid;
322 amounts.push_back (amount);
323 if (amount < left_most)
326 for (; scm_is_pair (autos); autos = scm_cdr (autos))
328 Grob *s = unsmob_grob (scm_caar (autos));
329 Real amount = scm_to_double (scm_cdar (autos)) * wid;
331 vsize x = find (done, s) - done.begin ();
332 if (x == VPOS || x >= done.size ())
335 amounts.push_back (amount);
336 if (amount < left_most)
341 for (vsize i = 0; i < amounts.size (); i++)
342 done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
347 Drul_array < vector<Grob*> >
348 Note_collision_interface::get_clash_groups (Grob *me)
350 Drul_array<vector<Grob*> > clash_groups;
352 extract_grob_set (me, "elements", elements);
353 for (vsize i = 0; i < elements.size (); i++)
355 Grob *se = elements[i];
356 if (Note_column::has_interface (se))
357 clash_groups[Note_column::dir (se)].push_back (se);
363 vector<Grob*> &clashes (clash_groups[d]);
364 vector_sort (clashes, Note_column::shift_compare);
366 while ((flip (&d)) != UP);
371 /** This complicated routine moves note columns around horizontally to
372 ensure that notes don't clash.
374 This should be put into Scheme.
377 Note_collision_interface::automatic_shift (Grob *me,
378 Drul_array < vector<Grob*>
381 Drul_array < vector<int> > shifts;
387 vector<int> &shift (shifts[d]);
388 vector<Grob*> &clashes (clash_groups[d]);
390 for (vsize i = 0; i < clashes.size (); i++)
393 = clashes[i]->get_property ("horizontal-shift");
395 if (scm_is_number (sh))
396 shift.push_back (scm_to_int (sh));
401 for (vsize i = 1; i < shift.size (); i++)
403 if (shift[i - 1] == shift[i])
405 clashes[0]->warning (_ ("ignoring too many clashing note columns"));
410 while ((flip (&d)) != UP);
412 Drul_array<vector<Slice> > extents;
413 Drul_array<vector<Real> > offsets;
417 for (vsize i = 0; i < clash_groups[d].size (); i++)
419 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
422 extents[d].push_back (s);
423 offsets[d].push_back (d * 0.5 * i);
426 while ((flip (&d)) != UP);
429 do horizontal shifts of each direction
439 for (vsize i = 1; i < clash_groups[d].size (); i++)
441 Slice prev = extents[d][i - 1];
442 prev.intersect (extents[d][i]);
443 if (prev.length () > 0
444 || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
445 for (vsize j = i; j < clash_groups[d].size (); j++)
446 offsets[d][j] += d * 0.5;
449 while ((flip (&d)) != UP);
452 Check if chords are meshing
455 check_meshing_chords (me, &offsets, extents, clash_groups);
459 for (vsize i = 0; i < clash_groups[d].size (); i++)
460 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
461 scm_from_double (offsets[d][i])),
464 while (flip (&d) != UP);
470 Note_collision_interface::forced_shift (Grob *me)
474 extract_grob_set (me, "elements", elements);
475 for (vsize i = 0; i < elements.size (); i++)
477 Grob *se = elements[i];
479 SCM force = se->get_property ("force-hshift");
480 if (scm_is_number (force))
482 tups = scm_cons (scm_cons (se->self_scm (), force),
490 Note_collision_interface::add_column (Grob *me, Grob *ncol)
492 ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
493 Axis_group_interface::add_element (me, ncol);
496 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
497 "An object that handles collisions between notes with different stem "
498 "directions and horizontal shifts. Most of the interesting properties "
499 "are to be set in @ref{note-column-interface}: these are "
500 "@code{force-hshift} and @code{horizontal-shift}.",
503 "merge-differently-dotted "
504 "merge-differently-headed "