2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "note-collision.hh"
13 #include "note-column.hh"
14 #include "note-head.hh"
15 #include "rhythmic-head.hh"
16 #include "paper-def.hh"
17 #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) ly_scm2int (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->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.25;
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);
271 if (Dot_column::has_interface (parent))
272 Side_position_interface::add_support (parent, nu);
278 for (int i=0; i < clash_groups[d].size (); i++)
279 (*offsets)[d][i] += d * shift_amount;
281 while ((flip (&d))!= UP);
285 Note_collision_interface::do_shifts (Grob* me)
287 Drul_array< Link_array <Grob> > cg = get_clash_groups (me);
289 SCM autos (automatic_shift (me, cg));
290 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 for (; ly_c_pair_p (hand); hand =ly_cdr (hand))
307 Grob * s = unsmob_grob (ly_caar (hand));
308 Real amount = ly_scm2double (ly_cdar (hand));
310 s->translate_axis (amount *wid, X_AXIS);
313 for (; ly_c_pair_p (autos); autos =ly_cdr (autos))
315 Grob * s = unsmob_grob (ly_caar (autos));
316 Real amount = ly_scm2double (ly_cdar (autos));
319 s->translate_axis (amount * wid, X_AXIS);
323 Drul_array< Link_array <Grob> >
324 Note_collision_interface::get_clash_groups (Grob *me)
326 Drul_array<Link_array<Grob> > clash_groups;
328 SCM s = me->get_property ("elements");
329 for (; ly_c_pair_p (s); s = ly_cdr (s))
331 SCM car = ly_car (s);
333 Grob * se = unsmob_grob (car);
334 if (Note_column::has_interface (se))
335 clash_groups[Note_column::dir (se)].push (se);
341 Link_array<Grob> & clashes (clash_groups[d]);
342 clashes.sort (Note_column::shift_compare);
344 while ((flip (&d))!= UP);
349 /** This complicated routine moves note columns around horizontally to
350 ensure that notes don't clash.
352 This should be put into Scheme.
355 Note_collision_interface::automatic_shift (Grob *me,
356 Drul_array< Link_array <Grob> >
359 Drul_array<Array<int> > shifts;
366 Array<int> & shift (shifts[d]);
367 Link_array<Grob> & clashes (clash_groups[d]);
369 for (int i=0; i < clashes.size (); i++)
372 = clashes[i]->get_property ("horizontal-shift");
374 if (ly_c_number_p (sh))
375 shift.push (ly_scm2int (sh));
380 for (int i=1; i < shift.size (); i++)
382 if (shift[i-1] == shift[i])
384 clashes[0]->warning (_ ("Too many clashing notecolumns. Ignoring them."));
389 while ((flip (&d))!= UP);
391 Drul_array< Array < Slice > > extents;
392 Drul_array< Array < Real > > offsets;
396 for (int i=0; i < clash_groups[d].size (); i++)
398 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
402 offsets[d].push (d * 0.5 * i);
405 while ((flip (&d))!= UP);
408 do horizontal shifts of each direction
418 for (int i=1; i < clash_groups[d].size (); i++)
420 Slice prev =extents[d][i-1];
421 prev.intersect (extents[d][i]);
422 if (prev.length ()> 0 ||
423 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
424 for (int j = i; j < clash_groups[d].size (); j++)
425 offsets[d][j] += d * 0.5;
428 while ((flip (&d))!= UP);
432 Check if chords are meshing
435 check_meshing_chords (me, &offsets, extents, clash_groups);
439 for (int i=0; i < clash_groups[d].size (); i++)
440 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
441 scm_make_real (offsets[d][i])),
444 while (flip (&d) != UP);
450 Note_collision_interface::forced_shift (Grob *me)
454 SCM s = me->get_property ("elements");
455 for (; ly_c_pair_p (s); s = ly_cdr (s))
457 Grob * se = unsmob_grob (ly_car (s));
459 SCM force = se->get_property ("force-hshift");
460 if (ly_c_number_p (force))
462 tups = scm_cons (scm_cons (se->self_scm (), force),
470 Note_collision_interface::add_column (Grob*me,Grob* ncol)
472 ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
473 Axis_group_interface::add_element (me, ncol);
474 me->add_dependency (ncol);
478 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
479 "An object that handles collisions between notes with different stem "
480 "directions and horizontal shifts. Most of the interesting properties "
481 "are to be set in @ref{note-column-interface}: these are "
482 "@code{force-hshift} and @code{horizontal-shift}."
486 "merge-differently-dotted merge-differently-headed positioning-done");