2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.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 /* Should never merge quarter and half notes, as this would make
80 them indistinguishable. */
82 && ((Rhythmic_head::duration_log (nu) == 1
83 && Rhythmic_head::duration_log (nd) == 2)
84 || (Rhythmic_head::duration_log (nu) == 2
85 && Rhythmic_head::duration_log (nd) == 1)))
86 merge_possible = false;
89 this case (distant half collide),
96 the noteheads may be closer than this case (close half collide)
107 /* TODO: filter out the 'o's in this configuration, since they're no
108 part in the collision.
117 bool close_half_collide = false;
118 bool distant_half_collide = false;
119 bool full_collide = false;
122 while (i < ups.size () && j < dps.size ())
124 if (abs (ups[i] - dps[j]) == 1)
126 merge_possible = false;
128 close_half_collide = true;
130 distant_half_collide = true;
132 else if (ups[i] == dps[j])
134 else if (ups[i] > dps[0] && ups[i] < dps.top ())
135 merge_possible = false;
136 else if (dps[j] > ups[0] && dps[j] < ups.top ())
137 merge_possible = false;
141 else if (ups[i] > dps[j])
150 full_collide = full_collide || (close_half_collide
151 && distant_half_collide);
153 Drul_array<Real> center_note_shifts;
154 center_note_shifts[LEFT] = 0.0;
155 center_note_shifts[RIGHT] = 0.0;
157 Real shift_amount = 1;
159 bool touch = (ups[0] >= dps.top ());
163 /* For full collisions, the right hand head may obscure dots, so
164 make sure the dotted heads go to the right. */
165 bool stem_to_stem = false;
167 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
169 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
176 /* If possible, don't wipe any heads. Else, wipe shortest head,
177 or head with smallest amount of dots. Note: when merging
178 different heads, dots on the smaller one disappear. */
180 Grob *dot_wipe_head = nu;
182 if (upball_type == dnball_type)
184 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
189 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
197 else if (dnball_type > upball_type)
202 else if (dnball_type < upball_type)
210 if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
214 if (wipe_ball && wipe_ball->is_live ())
216 wipe_ball->set_property ("transparent", SCM_BOOL_T);
219 /* TODO: these numbers are magic; should devise a set of grob props
220 to tune this behavior. */
221 else if (stem_to_stem)
222 shift_amount = -abs (shift_amount) * 0.65;
223 else if (close_half_collide && !touch)
224 shift_amount *= 0.52;
225 else if (distant_half_collide && !touch)
227 else if (distant_half_collide || close_half_collide || full_collide)
231 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
234 shift_amount *= 0.17;
236 /* For full or close half collisions, the right hand head may
237 obscure dots. Move dots to the right. */
238 if (abs (shift_amount) > 1e-6
239 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
240 && (full_collide || close_half_collide))
242 Grob *d = unsmob_grob (nd->get_object ("dot"));
243 Grob *parent = d->get_parent (X_AXIS);
253 the . is put right of o which is erroneous o force-shifted
256 if (Dot_column::has_interface (parent))
257 Side_position_interface::add_support (parent, nu);
263 for (int i = 0; i < clash_groups[d].size (); i++)
264 (*offsets)[d][i] += d * shift_amount;
266 while ((flip (&d)) != UP);
270 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1)
272 Note_collision_interface::calc_positioning_done (SCM smob)
274 Grob *me = unsmob_grob (smob);
275 Drul_array<Link_array<Grob> > cg = get_clash_groups (me);
280 for (int i = cg[d].size(); i--; )
285 cg[d][i]->extent (me, X_AXIS);
288 while (flip (&d) != UP);
290 SCM autos (automatic_shift (me, cg));
291 SCM hand (forced_shift (me));
299 wid = Note_column::first_head (h)->extent (h, X_AXIS).length ();
302 while (flip (&d) != UP);
304 Link_array<Grob> done;
305 Real left_most = 1e6;
308 for (; scm_is_pair (hand); hand = scm_cdr (hand))
310 Grob *s = unsmob_grob (scm_caar (hand));
311 Real amount = scm_to_double (scm_cdar (hand)) * wid;
314 amounts.push (amount);
315 if (amount < left_most)
318 for (; scm_is_pair (autos); autos = scm_cdr (autos))
320 Grob *s = unsmob_grob (scm_caar (autos));
321 Real amount = scm_to_double (scm_cdar (autos)) * wid;
326 amounts.push (amount);
327 if (amount < left_most)
332 for (int i = 0; i < amounts.size (); i++)
333 done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
338 Drul_array < Link_array<Grob> >
339 Note_collision_interface::get_clash_groups (Grob *me)
341 Drul_array<Link_array<Grob> > clash_groups;
343 extract_grob_set (me, "elements", elements);
344 for (int i = 0; i < elements.size (); i++)
346 Grob *se = elements[i];
347 if (Note_column::has_interface (se))
348 clash_groups[Note_column::dir (se)].push (se);
354 Link_array<Grob> &clashes (clash_groups[d]);
355 clashes.sort (Note_column::shift_compare);
357 while ((flip (&d)) != UP);
362 /** This complicated routine moves note columns around horizontally to
363 ensure that notes don't clash.
365 This should be put into Scheme.
368 Note_collision_interface::automatic_shift (Grob *me,
369 Drul_array < Link_array<Grob>
372 Drul_array < Array<int> > shifts;
378 Array<int> &shift (shifts[d]);
379 Link_array<Grob> &clashes (clash_groups[d]);
381 for (int i = 0; i < clashes.size (); i++)
384 = clashes[i]->get_property ("horizontal-shift");
386 if (scm_is_number (sh))
387 shift.push (scm_to_int (sh));
392 for (int i = 1; i < shift.size (); i++)
394 if (shift[i - 1] == shift[i])
396 clashes[0]->warning (_ ("ignoring too many clashing note columns"));
401 while ((flip (&d)) != UP);
403 Drul_array<Array<Slice> > extents;
404 Drul_array<Array<Real> > offsets;
408 for (int i = 0; i < clash_groups[d].size (); i++)
410 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
414 offsets[d].push (d * 0.5 * i);
417 while ((flip (&d)) != UP);
420 do horizontal shifts of each direction
430 for (int i = 1; i < clash_groups[d].size (); i++)
432 Slice prev = extents[d][i - 1];
433 prev.intersect (extents[d][i]);
434 if (prev.length () > 0
435 || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
436 for (int j = i; j < clash_groups[d].size (); j++)
437 offsets[d][j] += d * 0.5;
440 while ((flip (&d)) != UP);
443 Check if chords are meshing
446 check_meshing_chords (me, &offsets, extents, clash_groups);
450 for (int i = 0; i < clash_groups[d].size (); i++)
451 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
452 scm_from_double (offsets[d][i])),
455 while (flip (&d) != UP);
461 Note_collision_interface::forced_shift (Grob *me)
465 extract_grob_set (me, "elements", elements);
466 for (int i = 0; i < elements.size (); i++)
468 Grob *se = elements[i];
470 SCM force = se->get_property ("force-hshift");
471 if (scm_is_number (force))
473 tups = scm_cons (scm_cons (se->self_scm (), force),
481 Note_collision_interface::add_column (Grob *me, Grob *ncol)
483 ncol->add_offset_callback (Grob::same_axis_parent_positioning_proc, X_AXIS);
484 Axis_group_interface::add_element (me, ncol);
487 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
488 "An object that handles collisions between notes with different stem "
489 "directions and horizontal shifts. Most of the interesting properties "
490 "are to be set in @ref{note-column-interface}: these are "
491 "@code{force-hshift} and @code{horizontal-shift}.",
494 "merge-differently-dotted "
495 "merge-differently-headed "