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<Array<Real> > *offsets,
27 Drul_array<Array<Slice> > const &extents,
28 Drul_array<Link_array<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 Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
45 Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
47 /* Too far apart to collide. */
48 if (ups[0] > dps.top () + 1)
51 // FIXME: what's this?
52 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
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;
78 merge_possible = merge_possible &&
79 !(nu->get_property ("style") == ly_symbol2scm ("fa")
80 && nd->get_property ("style") == ly_symbol2scm ("fa"));
83 /* Should never merge quarter and half notes, as this would make
84 them indistinguishable. */
86 && ((Rhythmic_head::duration_log (nu) == 1
87 && Rhythmic_head::duration_log (nd) == 2)
88 || (Rhythmic_head::duration_log (nu) == 2
89 && Rhythmic_head::duration_log (nd) == 1)))
90 merge_possible = false;
93 this case (distant half collide),
100 the noteheads may be closer than this case (close half collide)
111 /* TODO: filter out the 'o's in this configuration, since they're no
112 part in the collision.
121 bool close_half_collide = false;
122 bool distant_half_collide = false;
123 bool full_collide = false;
126 while (i < ups.size () && j < dps.size ())
128 if (abs (ups[i] - dps[j]) == 1)
130 merge_possible = false;
132 close_half_collide = true;
134 distant_half_collide = true;
136 else if (ups[i] == dps[j])
138 else if (ups[i] > dps[0] && ups[i] < dps.top ())
139 merge_possible = false;
140 else if (dps[j] > ups[0] && dps[j] < ups.top ())
141 merge_possible = false;
145 else if (ups[i] > dps[j])
154 full_collide = full_collide || (close_half_collide
155 && distant_half_collide);
157 Drul_array<Real> center_note_shifts;
158 center_note_shifts[LEFT] = 0.0;
159 center_note_shifts[RIGHT] = 0.0;
161 Real shift_amount = 1;
163 bool touch = (ups[0] >= dps.top ());
167 /* For full collisions, the right hand head may obscure dots, so
168 make sure the dotted heads go to the right. */
169 bool stem_to_stem = false;
171 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
173 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
180 /* If possible, don't wipe any heads. Else, wipe shortest head,
181 or head with smallest amount of dots. Note: when merging
182 different heads, dots on the smaller one disappear. */
184 Grob *dot_wipe_head = nu;
186 if (upball_type == dnball_type)
188 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
193 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
201 else if (dnball_type > upball_type)
206 else if (dnball_type < upball_type)
214 if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
218 if (wipe_ball && wipe_ball->is_live ())
220 wipe_ball->set_property ("transparent", SCM_BOOL_T);
223 /* TODO: these numbers are magic; should devise a set of grob props
224 to tune this behavior. */
225 else if (stem_to_stem)
226 shift_amount = -abs (shift_amount) * 0.65;
227 else if (close_half_collide && !touch)
228 shift_amount *= 0.52;
229 else if (distant_half_collide && !touch)
231 else if (distant_half_collide || close_half_collide || full_collide)
235 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
238 shift_amount *= 0.17;
240 /* For full or close half collisions, the right hand head may
241 obscure dots. Move dots to the right. */
242 if (abs (shift_amount) > 1e-6
243 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
244 && (full_collide || close_half_collide))
246 Grob *d = unsmob_grob (nd->get_object ("dot"));
247 Grob *parent = d->get_parent (X_AXIS);
257 the . is put right of o which is erroneous o force-shifted
260 if (Dot_column::has_interface (parent))
261 Side_position_interface::add_support (parent, nu);
267 for (int i = 0; i < clash_groups[d].size (); i++)
268 (*offsets)[d][i] += d * shift_amount;
270 while ((flip (&d)) != UP);
274 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1)
276 Note_collision_interface::calc_positioning_done (SCM smob)
278 Grob *me = unsmob_grob (smob);
279 Drul_array<Link_array<Grob> > cg = get_clash_groups (me);
284 for (int i = cg[d].size(); i--; )
289 cg[d][i]->extent (me, X_AXIS);
292 while (flip (&d) != UP);
294 SCM autos (automatic_shift (me, cg));
295 SCM hand (forced_shift (me));
303 wid = Note_column::first_head (h)->extent (h, X_AXIS).length ();
306 while (flip (&d) != UP);
308 Link_array<Grob> done;
309 Real left_most = 1e6;
312 for (; scm_is_pair (hand); hand = scm_cdr (hand))
314 Grob *s = unsmob_grob (scm_caar (hand));
315 Real amount = scm_to_double (scm_cdar (hand)) * wid;
318 amounts.push (amount);
319 if (amount < left_most)
322 for (; scm_is_pair (autos); autos = scm_cdr (autos))
324 Grob *s = unsmob_grob (scm_caar (autos));
325 Real amount = scm_to_double (scm_cdar (autos)) * wid;
330 amounts.push (amount);
331 if (amount < left_most)
336 for (int i = 0; i < amounts.size (); i++)
337 done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
342 Drul_array < Link_array<Grob> >
343 Note_collision_interface::get_clash_groups (Grob *me)
345 Drul_array<Link_array<Grob> > clash_groups;
347 extract_grob_set (me, "elements", elements);
348 for (int i = 0; i < elements.size (); i++)
350 Grob *se = elements[i];
351 if (Note_column::has_interface (se))
352 clash_groups[Note_column::dir (se)].push (se);
358 Link_array<Grob> &clashes (clash_groups[d]);
359 clashes.sort (Note_column::shift_compare);
361 while ((flip (&d)) != UP);
366 /** This complicated routine moves note columns around horizontally to
367 ensure that notes don't clash.
369 This should be put into Scheme.
372 Note_collision_interface::automatic_shift (Grob *me,
373 Drul_array < Link_array<Grob>
376 Drul_array < Array<int> > shifts;
382 Array<int> &shift (shifts[d]);
383 Link_array<Grob> &clashes (clash_groups[d]);
385 for (int i = 0; i < clashes.size (); i++)
388 = clashes[i]->get_property ("horizontal-shift");
390 if (scm_is_number (sh))
391 shift.push (scm_to_int (sh));
396 for (int i = 1; i < shift.size (); i++)
398 if (shift[i - 1] == shift[i])
400 clashes[0]->warning (_ ("ignoring too many clashing note columns"));
405 while ((flip (&d)) != UP);
407 Drul_array<Array<Slice> > extents;
408 Drul_array<Array<Real> > offsets;
412 for (int i = 0; i < clash_groups[d].size (); i++)
414 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
418 offsets[d].push (d * 0.5 * i);
421 while ((flip (&d)) != UP);
424 do horizontal shifts of each direction
434 for (int i = 1; i < clash_groups[d].size (); i++)
436 Slice prev = extents[d][i - 1];
437 prev.intersect (extents[d][i]);
438 if (prev.length () > 0
439 || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
440 for (int j = i; j < clash_groups[d].size (); j++)
441 offsets[d][j] += d * 0.5;
444 while ((flip (&d)) != UP);
447 Check if chords are meshing
450 check_meshing_chords (me, &offsets, extents, clash_groups);
454 for (int i = 0; i < clash_groups[d].size (); i++)
455 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
456 scm_from_double (offsets[d][i])),
459 while (flip (&d) != UP);
465 Note_collision_interface::forced_shift (Grob *me)
469 extract_grob_set (me, "elements", elements);
470 for (int i = 0; i < elements.size (); i++)
472 Grob *se = elements[i];
474 SCM force = se->get_property ("force-hshift");
475 if (scm_is_number (force))
477 tups = scm_cons (scm_cons (se->self_scm (), force),
485 Note_collision_interface::add_column (Grob *me, Grob *ncol)
487 ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
488 Axis_group_interface::add_element (me, ncol);
491 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
492 "An object that handles collisions between notes with different stem "
493 "directions and horizontal shifts. Most of the interesting properties "
494 "are to be set in @ref{note-column-interface}: these are "
495 "@code{force-hshift} and @code{horizontal-shift}.",
498 "merge-differently-dotted "
499 "merge-differently-headed "