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"
14 #include "note-column.hh"
15 #include "note-head.hh"
16 #include "rhythmic-head.hh"
17 #include "output-def.hh"
18 #include "axis-group-interface.hh"
20 #include "side-position-interface.hh"
21 #include "dot-column.hh"
22 #include "pointer-group-interface.hh"
24 MAKE_SCHEME_CALLBACK (Note_collision_interface, force_shift_callback, 2);
27 Note_collision_interface::force_shift_callback (SCM element_smob, SCM axis)
29 Grob *me = unsmob_grob (element_smob);
30 Axis a = (Axis) scm_to_int (axis);
33 me = me->get_parent (a);
35 if (! to_boolean (me->get_property ("positioning-done")))
37 me->set_property ("positioning-done", SCM_BOOL_T);
41 return scm_from_double (0.0);
45 check_meshing_chords (Grob *me,
46 Drul_array<Array<Real> > *offsets,
47 Drul_array<Array<Slice> > const &extents,
48 Drul_array<Link_array<Grob> > const &clash_groups)
51 if (!extents[UP].size () || ! extents[DOWN].size ())
54 Grob *cu = clash_groups[UP][0];
55 Grob *cd = clash_groups[DOWN][0];
57 /* Every note column should have a stem, but avoid a crash. */
58 if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
61 Grob *nu = Note_column::first_head (cu);
62 Grob *nd = Note_column::first_head (cd);
64 Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
65 Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
67 /* Too far apart to collide. */
68 if (ups[0] > dps.top () + 1)
71 // FIXME: what's this?
72 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
74 /* Do not merge notes typeset in different style. */
75 if (!ly_is_equal (nu->get_property ("style"),
76 nd->get_property ("style")))
77 merge_possible = false;
79 int upball_type = Note_head::get_balltype (nu);
80 int dnball_type = Note_head::get_balltype (nd);
82 /* Do not merge whole notes (or longer, like breve, longa, maxima). */
83 if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
84 merge_possible = false;
87 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
88 && !to_boolean (me->get_property ("merge-differently-dotted")))
89 merge_possible = false;
91 /* Can only merge different heads if merge-differently-headed is
94 && upball_type != dnball_type
95 && !to_boolean (me->get_property ("merge-differently-headed")))
96 merge_possible = false;
98 /* Should never merge quarter and half notes, as this would make
99 them indistinguishable. */
101 && ((Rhythmic_head::duration_log (nu) == 1
102 && Rhythmic_head::duration_log (nd) == 2)
103 || (Rhythmic_head::duration_log (nu) == 2
104 && Rhythmic_head::duration_log (nd) == 1)))
105 merge_possible = false;
108 this case (distant half collide),
115 the noteheads may be closer than this case (close half collide)
126 /* TODO: filter out the 'o's in this configuration, since they're no
127 part in the collision.
136 bool close_half_collide = false;
137 bool distant_half_collide = false;
138 bool full_collide = false;
141 while (i < ups.size () && j < dps.size ())
143 if (abs (ups[i] - dps[j]) == 1)
145 merge_possible = false;
147 close_half_collide = true;
149 distant_half_collide = true;
151 else if (ups[i] == dps[j])
153 else if (ups[i] > dps[0] && ups[i] < dps.top ())
154 merge_possible = false;
155 else if (dps[j] > ups[0] && dps[j] < ups.top ())
156 merge_possible = false;
160 else if (ups[i] > dps[j])
169 full_collide = full_collide || (close_half_collide
170 && distant_half_collide);
172 Drul_array<Real> center_note_shifts;
173 center_note_shifts[LEFT] = 0.0;
174 center_note_shifts[RIGHT] = 0.0;
176 Real shift_amount = 1;
178 bool touch = (ups[0] >= dps.top ());
182 /* For full collisions, the right hand head may obscure dots, so
183 make sure the dotted heads go to the right. */
184 bool stem_to_stem = false;
186 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
188 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
195 /* If possible, don't wipe any heads. Else, wipe shortest head,
196 or head with smallest amount of dots. Note: when merging
197 different heads, dots on the smaller one disappear. */
199 Grob *dot_wipe_head = nu;
201 if (upball_type == dnball_type)
203 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
208 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
216 else if (dnball_type > upball_type)
221 else if (dnball_type < upball_type)
229 if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
233 if (wipe_ball && wipe_ball->is_live ())
235 wipe_ball->set_property ("transparent", SCM_BOOL_T);
236 wipe_ball->set_property ("stencil", SCM_EOL);
239 /* TODO: these numbers are magic; should devise a set of grob props
240 to tune this behavior. */
241 else if (stem_to_stem)
242 shift_amount = -abs (shift_amount) * 0.65;
243 else if (close_half_collide && !touch)
244 shift_amount *= 0.52;
245 else if (distant_half_collide && !touch)
247 else if (distant_half_collide || close_half_collide || full_collide)
251 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
254 shift_amount *= 0.17;
256 /* For full or close half collisions, the right hand head may
257 obscure dots. Move dots to the right. */
258 if (abs (shift_amount) > 1e-6
259 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
260 && (full_collide || close_half_collide))
262 Grob *d = unsmob_grob (nd->get_object ("dot"));
263 Grob *parent = d->get_parent (X_AXIS);
273 the . is put right of o which is erroneous o force-shifted
276 if (Dot_column::has_interface (parent))
277 Side_position_interface::add_support (parent, nu);
283 for (int i = 0; i < clash_groups[d].size (); i++)
284 (*offsets)[d][i] += d * shift_amount;
286 while ((flip (&d)) != UP);
290 Note_collision_interface::do_shifts (Grob *me)
292 Drul_array<Link_array<Grob> > cg = get_clash_groups (me);
294 SCM autos (automatic_shift (me, cg));
295 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);
341 Drul_array < Link_array<Grob>
342 > Note_collision_interface::get_clash_groups (Grob *me)
344 Drul_array<Link_array<Grob> > clash_groups;
346 extract_grob_set (me, "elements", elements);
347 for (int i = 0; i < elements.size (); i++)
349 Grob *se = elements[i];
350 if (Note_column::has_interface (se))
351 clash_groups[Note_column::dir (se)].push (se);
357 Link_array<Grob> &clashes (clash_groups[d]);
358 clashes.sort (Note_column::shift_compare);
360 while ((flip (&d)) != UP);
365 /** This complicated routine moves note columns around horizontally to
366 ensure that notes don't clash.
368 This should be put into Scheme.
371 Note_collision_interface::automatic_shift (Grob *me,
372 Drul_array < Link_array<Grob>
375 Drul_array < Array<int> > shifts;
381 Array<int> &shift (shifts[d]);
382 Link_array<Grob> &clashes (clash_groups[d]);
384 for (int i = 0; i < clashes.size (); i++)
387 = clashes[i]->get_property ("horizontal-shift");
389 if (scm_is_number (sh))
390 shift.push (scm_to_int (sh));
395 for (int i = 1; i < shift.size (); i++)
397 if (shift[i - 1] == shift[i])
399 clashes[0]->warning (_ ("ignoring too many clashing note columns"));
404 while ((flip (&d)) != UP);
406 Drul_array<Array<Slice> > extents;
407 Drul_array<Array<Real> > offsets;
411 for (int i = 0; i < clash_groups[d].size (); i++)
413 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
417 offsets[d].push (d * 0.5 * i);
420 while ((flip (&d)) != UP);
423 do horizontal shifts of each direction
433 for (int i = 1; i < clash_groups[d].size (); i++)
435 Slice prev = extents[d][i - 1];
436 prev.intersect (extents[d][i]);
437 if (prev.length () > 0
438 || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
439 for (int j = i; j < clash_groups[d].size (); j++)
440 offsets[d][j] += d * 0.5;
443 while ((flip (&d)) != UP);
446 Check if chords are meshing
449 check_meshing_chords (me, &offsets, extents, clash_groups);
453 for (int i = 0; i < clash_groups[d].size (); i++)
454 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
455 scm_from_double (offsets[d][i])),
458 while (flip (&d) != UP);
464 Note_collision_interface::forced_shift (Grob *me)
468 extract_grob_set (me, "elements", elements);
469 for (int i = 0; i < elements.size (); i++)
471 Grob *se = elements[i];
473 SCM force = se->get_property ("force-hshift");
474 if (scm_is_number (force))
476 tups = scm_cons (scm_cons (se->self_scm (), force),
484 Note_collision_interface::add_column (Grob *me, Grob *ncol)
486 ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
487 Axis_group_interface::add_element (me, ncol);
488 me->add_dependency (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}.",
497 "merge-differently-dotted merge-differently-headed positioning-done");