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"
13 #include "note-column.hh"
14 #include "note-head.hh"
15 #include "rhythmic-head.hh"
16 #include "output-def.hh"
17 #include "axis-group-interface.hh"
19 #include "side-position-interface.hh"
20 #include "dot-column.hh"
21 #include "pointer-group-interface.hh"
26 check_meshing_chords (Grob *me,
27 Drul_array<Array<Real> > *offsets,
28 Drul_array<Array<Slice> > const &extents,
29 Drul_array<Link_array<Grob> > const &clash_groups)
32 if (!extents[UP].size () || ! extents[DOWN].size ())
35 Grob *cu = clash_groups[UP][0];
36 Grob *cd = clash_groups[DOWN][0];
38 /* Every note column should have a stem, but avoid a crash. */
39 if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
42 Grob *nu = Note_column::first_head (cu);
43 Grob *nd = Note_column::first_head (cd);
45 Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
46 Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
48 /* Too far apart to collide. */
49 if (ups[0] > dps.top () + 1)
52 // FIXME: what's this?
53 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
55 /* Do not merge notes typeset in different style. */
56 if (!ly_is_equal (nu->get_property ("style"),
57 nd->get_property ("style")))
58 merge_possible = false;
60 int upball_type = Note_head::get_balltype (nu);
61 int dnball_type = Note_head::get_balltype (nd);
63 /* Do not merge whole notes (or longer, like breve, longa, maxima). */
64 if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
65 merge_possible = false;
68 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
69 && !to_boolean (me->get_property ("merge-differently-dotted")))
70 merge_possible = false;
72 /* Can only merge different heads if merge-differently-headed is
75 && upball_type != dnball_type
76 && !to_boolean (me->get_property ("merge-differently-headed")))
77 merge_possible = false;
79 merge_possible = merge_possible &&
80 !(nu->get_property ("style") == ly_symbol2scm ("fa")
81 && nd->get_property ("style") == ly_symbol2scm ("fa"));
84 /* Should never merge quarter and half notes, as this would make
85 them indistinguishable. */
87 && ((Rhythmic_head::duration_log (nu) == 1
88 && Rhythmic_head::duration_log (nd) == 2)
89 || (Rhythmic_head::duration_log (nu) == 2
90 && Rhythmic_head::duration_log (nd) == 1)))
91 merge_possible = false;
94 this case (distant half collide),
101 the noteheads may be closer than this case (close half collide)
112 /* TODO: filter out the 'o's in this configuration, since they're no
113 part in the collision.
122 bool close_half_collide = false;
123 bool distant_half_collide = false;
124 bool full_collide = false;
127 while (i < ups.size () && j < dps.size ())
129 if (abs (ups[i] - dps[j]) == 1)
131 merge_possible = false;
133 close_half_collide = true;
135 distant_half_collide = true;
137 else if (ups[i] == dps[j])
139 else if (ups[i] > dps[0] && ups[i] < dps.top ())
140 merge_possible = false;
141 else if (dps[j] > ups[0] && dps[j] < ups.top ())
142 merge_possible = false;
146 else if (ups[i] > dps[j])
155 full_collide = full_collide || (close_half_collide
156 && distant_half_collide);
158 Drul_array<Real> center_note_shifts;
159 center_note_shifts[LEFT] = 0.0;
160 center_note_shifts[RIGHT] = 0.0;
162 Real shift_amount = 1;
164 bool touch = (ups[0] >= dps.top ());
168 /* For full collisions, the right hand head may obscure dots, so
169 make sure the dotted heads go to the right. */
170 bool stem_to_stem = false;
172 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
174 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
181 /* If possible, don't wipe any heads. Else, wipe shortest head,
182 or head with smallest amount of dots. Note: when merging
183 different heads, dots on the smaller one disappear. */
185 Grob *dot_wipe_head = nu;
187 if (upball_type == dnball_type)
189 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
194 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
202 else if (dnball_type > upball_type)
207 else if (dnball_type < upball_type)
215 if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
219 if (wipe_ball && wipe_ball->is_live ())
221 wipe_ball->set_property ("transparent", SCM_BOOL_T);
224 /* TODO: these numbers are magic; should devise a set of grob props
225 to tune this behavior. */
226 else if (stem_to_stem)
227 shift_amount = -abs (shift_amount) * 0.65;
228 else if (close_half_collide && !touch)
229 shift_amount *= 0.52;
230 else if (distant_half_collide && !touch)
232 else if (distant_half_collide || close_half_collide || full_collide)
236 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
239 shift_amount *= 0.17;
241 /* For full or close half collisions, the right hand head may
242 obscure dots. Move dots to the right. */
243 if (abs (shift_amount) > 1e-6
244 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
245 && (full_collide || close_half_collide))
247 Grob *d = unsmob_grob (nd->get_object ("dot"));
248 Grob *parent = d->get_parent (X_AXIS);
258 the . is put right of o which is erroneous o force-shifted
261 if (Dot_column::has_interface (parent))
262 Side_position_interface::add_support (parent, nu);
268 for (int i = 0; i < clash_groups[d].size (); i++)
269 (*offsets)[d][i] += d * shift_amount;
271 while ((flip (&d)) != UP);
275 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1)
277 Note_collision_interface::calc_positioning_done (SCM smob)
279 Grob *me = unsmob_grob (smob);
280 Drul_array<Link_array<Grob> > cg = get_clash_groups (me);
285 for (int i = cg[d].size(); i--; )
290 cg[d][i]->extent (me, X_AXIS);
293 while (flip (&d) != UP);
295 SCM autos (automatic_shift (me, cg));
296 SCM hand (forced_shift (me));
304 wid = Note_column::first_head (h)->extent (h, X_AXIS).length ();
307 while (flip (&d) != UP);
309 Link_array<Grob> done;
310 Real left_most = 1e6;
313 for (; scm_is_pair (hand); hand = scm_cdr (hand))
315 Grob *s = unsmob_grob (scm_caar (hand));
316 Real amount = scm_to_double (scm_cdar (hand)) * wid;
319 amounts.push (amount);
320 if (amount < left_most)
323 for (; scm_is_pair (autos); autos = scm_cdr (autos))
325 Grob *s = unsmob_grob (scm_caar (autos));
326 Real amount = scm_to_double (scm_cdar (autos)) * wid;
331 amounts.push (amount);
332 if (amount < left_most)
337 for (int i = 0; i < amounts.size (); i++)
338 done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
343 Drul_array < Link_array<Grob> >
344 Note_collision_interface::get_clash_groups (Grob *me)
346 Drul_array<Link_array<Grob> > clash_groups;
348 extract_grob_set (me, "elements", elements);
349 for (int i = 0; i < elements.size (); i++)
351 Grob *se = elements[i];
352 if (Note_column::has_interface (se))
353 clash_groups[Note_column::dir (se)].push (se);
359 Link_array<Grob> &clashes (clash_groups[d]);
360 clashes.sort (Note_column::shift_compare);
362 while ((flip (&d)) != UP);
367 /** This complicated routine moves note columns around horizontally to
368 ensure that notes don't clash.
370 This should be put into Scheme.
373 Note_collision_interface::automatic_shift (Grob *me,
374 Drul_array < Link_array<Grob>
377 Drul_array < Array<int> > shifts;
383 Array<int> &shift (shifts[d]);
384 Link_array<Grob> &clashes (clash_groups[d]);
386 for (int i = 0; i < clashes.size (); i++)
389 = clashes[i]->get_property ("horizontal-shift");
391 if (scm_is_number (sh))
392 shift.push (scm_to_int (sh));
397 for (int i = 1; i < shift.size (); i++)
399 if (shift[i - 1] == shift[i])
401 clashes[0]->warning (_ ("ignoring too many clashing note columns"));
406 while ((flip (&d)) != UP);
408 Drul_array<Array<Slice> > extents;
409 Drul_array<Array<Real> > offsets;
413 for (int i = 0; i < clash_groups[d].size (); i++)
415 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
419 offsets[d].push (d * 0.5 * i);
422 while ((flip (&d)) != UP);
425 do horizontal shifts of each direction
435 for (int i = 1; i < clash_groups[d].size (); i++)
437 Slice prev = extents[d][i - 1];
438 prev.intersect (extents[d][i]);
439 if (prev.length () > 0
440 || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
441 for (int j = i; j < clash_groups[d].size (); j++)
442 offsets[d][j] += d * 0.5;
445 while ((flip (&d)) != UP);
448 Check if chords are meshing
451 check_meshing_chords (me, &offsets, extents, clash_groups);
455 for (int i = 0; i < clash_groups[d].size (); i++)
456 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
457 scm_from_double (offsets[d][i])),
460 while (flip (&d) != UP);
466 Note_collision_interface::forced_shift (Grob *me)
470 extract_grob_set (me, "elements", elements);
471 for (int i = 0; i < elements.size (); i++)
473 Grob *se = elements[i];
475 SCM force = se->get_property ("force-hshift");
476 if (scm_is_number (force))
478 tups = scm_cons (scm_cons (se->self_scm (), force),
486 Note_collision_interface::add_column (Grob *me, Grob *ncol)
488 ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
489 Axis_group_interface::add_element (me, ncol);
492 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
493 "An object that handles collisions between notes with different stem "
494 "directions and horizontal shifts. Most of the interesting properties "
495 "are to be set in @ref{note-column-interface}: these are "
496 "@code{force-hshift} and @code{horizontal-shift}.",
499 "merge-differently-dotted "
500 "merge-differently-headed "