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"
23 MAKE_SCHEME_CALLBACK (Note_collision_interface, force_shift_callback, 2);
26 Note_collision_interface::force_shift_callback (SCM element_smob, SCM axis)
28 Grob *me = unsmob_grob (element_smob);
29 Axis a = (Axis) scm_to_int (axis);
32 me = me->get_parent (a);
34 if (! to_boolean (me->get_property ("positioning-done")))
36 me->set_property ("positioning-done", SCM_BOOL_T);
40 return scm_make_real (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 ());
76 /* Do not merge notes typeset in different style. */
77 if ( !ly_c_equal_p (nu->get_property ("style"),
78 nd->get_property ("style") ) )
79 merge_possible = false;
81 int upball_type = Note_head::get_balltype (nu);
82 int dnball_type = Note_head::get_balltype (nd);
84 /* Do not merge whole notes (or longer, like breve, longa, maxima). */
85 if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
86 merge_possible = false;
89 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
90 && !to_boolean (me->get_property ("merge-differently-dotted")))
91 merge_possible = false;
93 /* Can only merge different heads if merge-differently-headed is
96 && upball_type != dnball_type
97 && !to_boolean (me->get_property ("merge-differently-headed")))
98 merge_possible = false;
100 /* Should never merge quarter and half notes, as this would make
101 them indistinguishable. */
103 && ((Rhythmic_head::duration_log (nu) == 1
104 && Rhythmic_head::duration_log (nd) == 2)
105 || (Rhythmic_head::duration_log (nu) == 2
106 && Rhythmic_head::duration_log (nd) == 1)))
107 merge_possible = false;
111 this case (distant half collide),
118 the noteheads may be closer than this case (close half collide)
129 /* TODO: filter out the 'o's in this configuration, since they're no
130 part in the collision.
139 bool close_half_collide = false;
140 bool distant_half_collide = false;
141 bool full_collide = false;
144 while (i < ups.size () && j < dps.size ())
146 if (abs (ups[i] - dps[j]) == 1)
148 merge_possible = false;
150 close_half_collide = true;
152 distant_half_collide = true;
154 else if (ups[i]== dps[j])
156 else if (ups[i] >dps[0] && ups[i] < dps.top ())
157 merge_possible = false;
158 else if (dps[j] >ups[0] && dps[j] < ups.top ())
159 merge_possible = false;
163 else if (ups[i] > dps[j])
172 full_collide = full_collide || (close_half_collide
173 && distant_half_collide);
175 Drul_array<Real> center_note_shifts;
176 center_note_shifts[LEFT] = 0.0;
177 center_note_shifts[RIGHT] = 0.0;
180 Real shift_amount = 1;
182 bool touch = (ups[0] >= dps.top ());
186 /* For full collisions, the right hand head may obscure dots, so
187 make sure the dotted heads go to the right. */
188 bool stem_to_stem = false;
190 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
192 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
200 /* If possible, don't wipe any heads. Else, wipe shortest head,
201 or head with smallest amount of dots. Note: when merging
202 different heads, dots on the smaller one disappear. */
204 Grob *dot_wipe_head = nu;
206 if (upball_type == dnball_type)
208 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
213 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
223 else if (dnball_type > upball_type)
228 else if (dnball_type < upball_type)
236 if (Grob *d = unsmob_grob (dot_wipe_head->get_property ("dot")))
240 if (wipe_ball && wipe_ball->is_live ())
242 wipe_ball->set_property ("transparent", SCM_BOOL_T);
243 wipe_ball->set_property ("stencil", SCM_EOL);
246 /* TODO: these numbers are magic; should devise a set of grob props
247 to tune this behavior. */
248 else if (stem_to_stem)
249 shift_amount = -abs (shift_amount) * 0.65;
250 else if (close_half_collide && !touch)
251 shift_amount *= 0.52;
252 else if (distant_half_collide && !touch)
254 else if (distant_half_collide || close_half_collide || full_collide)
258 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
261 shift_amount *= 0.17;
263 /* For full or close half collisions, the right hand head may
264 obscure dots. Move dots to the right. */
265 if (abs (shift_amount) > 1e-6
266 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
267 && (full_collide || close_half_collide))
269 Grob *d = unsmob_grob (nd->get_property ("dot"));
270 Grob *parent = d->get_parent (X_AXIS);
281 the . is put right of o which is erroneous o force-shifted
284 if (Dot_column::has_interface (parent))
285 Side_position_interface::add_support (parent, nu);
291 for (int i = 0; i < clash_groups[d].size (); i++)
292 (*offsets)[d][i] += d * shift_amount;
294 while ((flip (&d))!= UP);
298 Note_collision_interface::do_shifts (Grob* me)
300 Drul_array< Link_array <Grob> > cg = get_clash_groups (me);
302 SCM autos (automatic_shift (me, cg));
303 SCM hand (forced_shift (me));
312 wid = Note_column::first_head (h)->extent (h, X_AXIS).length () ;
315 while (flip (&d) != UP);
317 Link_array<Grob> done;
318 Real left_most = 1e6;
321 for (; scm_is_pair (hand); hand = scm_cdr (hand))
323 Grob * s = unsmob_grob (scm_caar (hand));
324 Real amount = scm_to_double (scm_cdar (hand)) * wid;
327 amounts.push (amount);
328 if (amount < left_most)
332 for (; scm_is_pair (autos); autos = scm_cdr (autos))
334 Grob * s = unsmob_grob (scm_caar (autos));
335 Real amount = scm_to_double (scm_cdar (autos)) *wid;
340 amounts.push (amount);
341 if (amount < left_most)
346 for (int i = 0; i < amounts.size(); i++)
348 done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
352 Drul_array< Link_array <Grob> >
353 Note_collision_interface::get_clash_groups (Grob *me)
355 Drul_array<Link_array<Grob> > clash_groups;
357 SCM s = me->get_property ("elements");
358 for (; scm_is_pair (s); s = scm_cdr (s))
360 SCM car = scm_car (s);
362 Grob * se = unsmob_grob (car);
363 if (Note_column::has_interface (se))
364 clash_groups[Note_column::dir (se)].push (se);
370 Link_array<Grob> & clashes (clash_groups[d]);
371 clashes.sort (Note_column::shift_compare);
373 while ((flip (&d))!= UP);
378 /** This complicated routine moves note columns around horizontally to
379 ensure that notes don't clash.
381 This should be put into Scheme.
384 Note_collision_interface::automatic_shift (Grob *me,
385 Drul_array< Link_array <Grob> >
388 Drul_array<Array<int> > shifts;
395 Array<int> & shift (shifts[d]);
396 Link_array<Grob> & clashes (clash_groups[d]);
398 for (int i = 0; i < clashes.size (); i++)
401 = clashes[i]->get_property ("horizontal-shift");
403 if (scm_is_number (sh))
404 shift.push (scm_to_int (sh));
409 for (int i =1; i < shift.size (); i++)
411 if (shift[i-1] == shift[i])
413 clashes[0]->warning (_ ("Too many clashing notecolumns. Ignoring them."));
418 while ((flip (&d))!= UP);
420 Drul_array< Array < Slice > > extents;
421 Drul_array< Array < Real > > offsets;
425 for (int i = 0; i < clash_groups[d].size (); i++)
427 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
431 offsets[d].push (d * 0.5 * i);
434 while ((flip (&d))!= UP);
437 do horizontal shifts of each direction
447 for (int i =1; i < clash_groups[d].size (); i++)
449 Slice prev = extents[d][i-1];
450 prev.intersect (extents[d][i]);
451 if (prev.length ()> 0 ||
452 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
453 for (int j = i; j < clash_groups[d].size (); j++)
454 offsets[d][j] += d * 0.5;
457 while ((flip (&d))!= UP);
461 Check if chords are meshing
464 check_meshing_chords (me, &offsets, extents, clash_groups);
468 for (int i = 0; i < clash_groups[d].size (); i++)
469 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
470 scm_make_real (offsets[d][i])),
473 while (flip (&d) != UP);
479 Note_collision_interface::forced_shift (Grob *me)
483 SCM s = me->get_property ("elements");
484 for (; scm_is_pair (s); s = scm_cdr (s))
486 Grob * se = unsmob_grob (scm_car (s));
488 SCM force = se->get_property ("force-hshift");
489 if (scm_is_number (force))
491 tups = scm_cons (scm_cons (se->self_scm (), force),
499 Note_collision_interface::add_column (Grob*me, Grob* ncol)
501 ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
502 Axis_group_interface::add_element (me, ncol);
503 me->add_dependency (ncol);
507 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
508 "An object that handles collisions between notes with different stem "
509 "directions and horizontal shifts. Most of the interesting properties "
510 "are to be set in @ref{note-column-interface}: these are "
511 "@code{force-hshift} and @code{horizontal-shift}."
515 "merge-differently-dotted merge-differently-headed positioning-done");