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>
10 #include "note-collision.hh"
11 #include "note-column.hh"
12 #include "note-head.hh"
13 #include "rhythmic-head.hh"
14 #include "paper-def.hh"
15 #include "axis-group-interface.hh"
18 #include "side-position-interface.hh"
19 #include "dot-column.hh"
21 MAKE_SCHEME_CALLBACK (Note_collision_interface,force_shift_callback,2);
24 Note_collision_interface::force_shift_callback (SCM element_smob, SCM axis)
26 Grob *me = unsmob_grob (element_smob);
27 Axis a = (Axis) gh_scm2int (axis);
30 me = me->get_parent (a);
32 if (! to_boolean (me->get_property ("positioning-done")))
34 me->set_property ("positioning-done", SCM_BOOL_T);
38 return gh_double2scm (0.0);
43 check_meshing_chords (Grob *me,
44 Drul_array< Array < Real > > *offsets,
45 Drul_array< Array < Slice > > const &extents,
46 Drul_array<Link_array<Grob> > const &clash_groups)
49 if (!extents[UP].size () || ! extents[DOWN].size ())
52 Grob *cu = clash_groups[UP][0];
53 Grob *cd = clash_groups[DOWN][0];
55 /* Every note column should have a stem, but avoid a crash. */
56 if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
59 Grob *nu = Note_column::first_head (cu);
60 Grob *nd = Note_column::first_head (cd);
62 Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
63 Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
65 /* Too far apart to collide. */
66 if (ups[0] > dps.top () + 1)
69 // FIXME: what's this?
70 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
72 int upball_type = Note_head::get_balltype (nu);
73 int dnball_type = Note_head::get_balltype (nd);
75 /* Do not merge whole notes (or longer, like breve, longa, maxima). */
76 if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
77 merge_possible = false;
80 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
81 && !to_boolean (me->get_property ("merge-differently-dotted")))
82 merge_possible = false;
84 /* Can only merge different heads if merge-differently-headed is
87 && upball_type != dnball_type
88 && !to_boolean (me->get_property ("merge-differently-headed")))
89 merge_possible = false;
91 /* Should never merge quarter and half notes, as this would make
92 them indistinguishable. */
94 && ((Rhythmic_head::duration_log (nu) == 1
95 && Rhythmic_head::duration_log (nd) == 2)
96 || (Rhythmic_head::duration_log (nu) == 2
97 && Rhythmic_head::duration_log (nd) == 1)))
98 merge_possible = false;
102 this case (distant half collide),
109 the noteheads may be closer than this case (close half collide)
120 /* TODO: filter out the 'o's in this configuration, since they're no
121 part in the collision.
130 bool close_half_collide = false;
131 bool distant_half_collide = false;
132 bool full_collide = false;
135 while (i < ups.size () && j < dps.size ())
137 if (abs (ups[i] - dps[j]) == 1)
139 merge_possible = false;
141 close_half_collide = true;
143 distant_half_collide = true;
145 else if (ups[i]==dps[j])
147 else if (ups[i] >dps[0] && ups[i] < dps.top ())
148 merge_possible = false;
149 else if (dps[j] >ups[0] && dps[j] < ups.top ())
150 merge_possible = false;
154 else if (ups[i] > dps[j])
163 full_collide = full_collide || (close_half_collide
164 && distant_half_collide);
166 Drul_array<Real> center_note_shifts;
167 center_note_shifts[LEFT] = 0.0;
168 center_note_shifts[RIGHT] = 0.0;
171 Real shift_amount = 1;
173 bool touch = (ups[0] >= dps.top ());
177 /* For full collisions, the right hand head may obscure dots, so
178 make sure the dotted heads go to the right. */
179 bool stem_to_stem = false;
181 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
183 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
191 /* If possible, don't wipe any heads. Else, wipe shortest head,
192 or head with smallest amount of dots. Note: when merging
193 different heads, dots on the smaller one disappear. */
195 Grob *dot_wipe_head = nu;
197 if (upball_type == dnball_type)
199 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
204 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
214 else if (dnball_type > upball_type)
219 else if (dnball_type < upball_type)
227 if (Grob *d = unsmob_grob (dot_wipe_head->get_property ("dot")))
231 if (wipe_ball && wipe_ball->live ())
233 wipe_ball->set_property ("transparent", SCM_BOOL_T);
234 wipe_ball->set_property ("stencil", SCM_EOL);
237 /* TODO: these numbers are magic; should devise a set of grob props
238 to tune this behavior. */
239 else if (stem_to_stem)
240 shift_amount *= -0.65;
241 else if (close_half_collide && !touch)
242 shift_amount *= 0.52;
243 else if (distant_half_collide && !touch)
245 else if (distant_half_collide || close_half_collide || full_collide)
249 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
252 shift_amount *= 0.25;
254 /* For full or close half collisions, the right hand head may
255 obscure dots. Move dots to the right. */
256 if (abs (shift_amount) > 1e-6
257 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
258 && (full_collide || close_half_collide))
260 Grob *d = unsmob_grob (nd->get_property ("dot"));
261 Grob *parent = d->get_parent (X_AXIS);
262 if (Dot_column::has_interface (parent))
263 Side_position_interface::add_support (parent, nu);
269 for (int i=0; i < clash_groups[d].size (); i++)
270 (*offsets)[d][i] += d * shift_amount;
272 while ((flip (&d))!= UP);
276 Note_collision_interface::do_shifts (Grob* me)
278 Drul_array< Link_array <Grob> > cg = get_clash_groups (me);
280 SCM autos (automatic_shift (me, cg));
281 SCM hand (forced_shift (me));
290 wid = Note_column::first_head (h)->extent (h,X_AXIS).length() ;
293 while (flip (&d) != UP);
295 Link_array<Grob> done;
296 for (; gh_pair_p (hand); hand =ly_cdr (hand))
298 Grob * s = unsmob_grob (ly_caar (hand));
299 Real amount = gh_scm2double (ly_cdar (hand));
301 s->translate_axis (amount *wid, X_AXIS);
304 for (; gh_pair_p (autos); autos =ly_cdr (autos))
306 Grob * s = unsmob_grob (ly_caar (autos));
307 Real amount = gh_scm2double (ly_cdar (autos));
310 s->translate_axis (amount * wid, X_AXIS);
314 Drul_array< Link_array <Grob> >
315 Note_collision_interface::get_clash_groups (Grob *me)
317 Drul_array<Link_array<Grob> > clash_groups;
319 SCM s = me->get_property ("elements");
320 for (; gh_pair_p (s); s = ly_cdr (s))
322 SCM car = ly_car (s);
324 Grob * se = unsmob_grob (car);
325 if (Note_column::has_interface (se))
326 clash_groups[Note_column::dir (se)].push (se);
332 Link_array<Grob> & clashes (clash_groups[d]);
333 clashes.sort (Note_column::shift_compare);
335 while ((flip (&d))!= UP);
340 /** This complicated routine moves note columns around horizontally to
341 ensure that notes don't clash.
343 This should be put into Scheme.
346 Note_collision_interface::automatic_shift (Grob *me,
347 Drul_array< Link_array <Grob> >
350 Drul_array<Array<int> > shifts;
357 Array<int> & shift (shifts[d]);
358 Link_array<Grob> & clashes (clash_groups[d]);
360 for (int i=0; i < clashes.size (); i++)
363 = clashes[i]->get_property ("horizontal-shift");
365 if (gh_number_p (sh))
366 shift.push (gh_scm2int (sh));
371 for (int i=1; i < shift.size (); i++)
373 if (shift[i-1] == shift[i])
375 clashes[0]->warning (_ ("Too many clashing notecolumns. Ignoring them."));
380 while ((flip (&d))!= UP);
382 Drul_array< Array < Slice > > extents;
383 Drul_array< Array < Real > > offsets;
387 for (int i=0; i < clash_groups[d].size (); i++)
389 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
393 offsets[d].push (d * 0.5 * i);
396 while ((flip (&d))!= UP);
399 do horizontal shifts of each direction
409 for (int i=1; i < clash_groups[d].size (); i++)
411 Slice prev =extents[d][i-1];
412 prev.intersect (extents[d][i]);
413 if (prev.length ()> 0 ||
414 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
415 for (int j = i; j < clash_groups[d].size (); j++)
416 offsets[d][j] += d * 0.5;
419 while ((flip (&d))!= UP);
423 Check if chords are meshing
426 check_meshing_chords (me, &offsets, extents, clash_groups);
430 for (int i=0; i < clash_groups[d].size (); i++)
431 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (),
432 gh_double2scm (offsets[d][i])),
435 while (flip (&d) != UP);
441 Note_collision_interface::forced_shift (Grob *me)
445 SCM s = me->get_property ("elements");
446 for (; gh_pair_p (s); s = ly_cdr (s))
448 Grob * se = unsmob_grob (ly_car (s));
450 SCM force = se->get_property ("force-hshift");
451 if (gh_number_p (force))
453 tups = gh_cons (gh_cons (se->self_scm (), force),
461 Note_collision_interface::add_column (Grob*me,Grob* ncol)
463 ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
464 Axis_group_interface::add_element (me, ncol);
465 me->add_dependency (ncol);
469 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
470 "An object that handles collisions between notes with different stem "
471 "directions and horizontal shifts. Most of the interesting properties "
472 "are to be set in @ref{note-column-interface}: these are "
473 "@code{force-hshift} and @code{horizontal-shift}."
477 "merge-differently-dotted merge-differently-headed positioning-done");