2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 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"
19 MAKE_SCHEME_CALLBACK (Note_collision_interface,force_shift_callback,2);
22 Note_collision_interface::force_shift_callback (SCM element_smob, SCM axis)
24 Grob *me = unsmob_grob (element_smob);
25 Axis a = (Axis) gh_scm2int (axis);
28 me = me->get_parent (a);
30 if (! to_boolean (me->get_grob_property ("positioning-done")))
32 me->set_grob_property ("positioning-done", SCM_BOOL_T);
36 return gh_double2scm (0.0);
41 check_meshing_chords (Grob *me,
42 Drul_array< Array < Real > > *offsets,
43 Drul_array< Array < Slice > > const &extents,
44 Drul_array<Link_array<Grob> > const &clash_groups)
47 if (!extents[UP].size () || ! extents[DOWN].size ())
50 Grob *cu = clash_groups[UP][0];
51 Grob *cd = clash_groups[DOWN][0];
53 /* Every note column should have a stem, but avoid a crash. */
54 if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
57 Grob *nu = Note_column::first_head (cu);
58 Grob *nd = Note_column::first_head (cd);
60 Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
61 Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
63 /* Too far apart to collide. */
64 if (ups[0] > dps.top () + 1)
67 // FIXME: what's this?
68 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
70 int upball_type = Note_head::get_balltype (nu);
71 int dnball_type = Note_head::get_balltype (nd);
73 /* Do not merge whole notes (or longer, like breve, longa, maxima). */
74 if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
75 merge_possible = false;
78 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
79 && !to_boolean (me->get_grob_property ("merge-differently-dotted")))
80 merge_possible = false;
82 /* Can only merge different heads if merge-differently-headed is
85 && upball_type != dnball_type
86 && !to_boolean (me->get_grob_property ("merge-differently-headed")))
87 merge_possible = false;
89 /* Can never merge quarter and half notes. */
91 && ((Rhythmic_head::duration_log (nu) == 1
92 && Rhythmic_head::duration_log (nd) == 2)
93 || (Rhythmic_head::duration_log (nu) == 2
94 && Rhythmic_head::duration_log (nd) == 1)))
95 merge_possible = false;
99 this case (distant half collide),
106 the noteheads may be closer than this case (close half collide)
117 /* TODO: filter out the 'o's in this configuration, since they're no
118 part in the collision.
127 bool close_half_collide = false;
128 bool distant_half_collide = false;
129 bool full_collide = false;
132 while (i < ups.size () && j < dps.size ())
134 if (abs (ups[i] - dps[j]) == 1)
136 merge_possible = false;
138 close_half_collide = true;
140 distant_half_collide = true;
142 else if (ups[i]==dps[j])
144 else if (ups[i] >dps[0] && ups[i] < dps.top ())
145 merge_possible = false;
146 else if (dps[j] >ups[0] && dps[j] < ups.top ())
147 merge_possible = false;
151 else if (ups[i] > dps[j])
160 Drul_array<Real> center_note_shifts;
161 center_note_shifts[LEFT] = 0.0;
162 center_note_shifts[RIGHT] = 0.0;
165 Real shift_amount = 1;
167 bool touch = (ups[0] - dps.top () >= 0);
172 for full collisions, the right hand head may obscure dots, so
173 make sure the dotted heads go to the right.
175 if ((Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd)
183 /* Wipe shortest head, or head with smallest amount of dots.
184 Note: when merging different heads, dots on shortest
187 Grob *wipe_ball = nu;
189 if (upball_type == dnball_type)
191 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
194 else if (dnball_type > upball_type)
197 if (wipe_ball->live ())
199 wipe_ball->set_grob_property ("transparent", SCM_BOOL_T);
200 wipe_ball->set_grob_property ("molecule", SCM_EOL);
202 if (Grob *d = unsmob_grob (wipe_ball->get_grob_property ("dot")))
206 /* TODO: these numbers are magic; should devise a set of grob props
207 to tune this behavior. */
208 else if (close_half_collide && !touch)
209 shift_amount *= 0.52;
210 else if (distant_half_collide && !touch)
212 else if (distant_half_collide || close_half_collide || full_collide)
216 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
219 shift_amount *= 0.25;
224 for (int i=0; i < clash_groups[d].size (); i++)
225 (*offsets)[d][i] += d * shift_amount;
227 while ((flip (&d))!= UP);
231 Note_collision_interface::do_shifts (Grob* me)
233 Drul_array< Link_array <Grob> > cg = get_clash_groups (me);
235 SCM autos (automatic_shift (me, cg));
236 SCM hand (forced_shift (me));
247 wid = Note_column::first_head(h)->extent(h,X_AXIS).length() ;
251 while (flip (&d) != UP);
254 Link_array<Grob> done;
255 for (; gh_pair_p (hand); hand =ly_cdr (hand))
257 Grob * s = unsmob_grob (ly_caar (hand));
258 Real amount = gh_scm2double (ly_cdar (hand));
260 s->translate_axis (amount *wid, X_AXIS);
263 for (; gh_pair_p (autos); autos =ly_cdr (autos))
265 Grob * s = unsmob_grob (ly_caar (autos));
266 Real amount = gh_scm2double (ly_cdar (autos));
269 s->translate_axis (amount * wid, X_AXIS);
273 Drul_array< Link_array <Grob> >
274 Note_collision_interface::get_clash_groups (Grob *me)
276 Drul_array<Link_array<Grob> > clash_groups;
278 SCM s = me->get_grob_property ("elements");
279 for (; gh_pair_p (s); s = ly_cdr (s))
281 SCM car = ly_car (s);
283 Grob * se = unsmob_grob (car);
284 if (Note_column::has_interface (se))
285 clash_groups[Note_column::dir (se)].push (se);
291 Link_array<Grob> & clashes (clash_groups[d]);
292 clashes.sort (Note_column::shift_compare);
294 while ((flip (&d))!= UP);
299 /** This complicated routine moves note columns around horizontally to
300 ensure that notes don't clash.
302 This should be put into Scheme.
305 Note_collision_interface::automatic_shift (Grob *me,
306 Drul_array< Link_array <Grob> >
309 Drul_array<Array<int> > shifts;
316 Array<int> & shift (shifts[d]);
317 Link_array<Grob> & clashes (clash_groups[d]);
319 for (int i=0; i < clashes.size (); i++)
322 = clashes[i]->get_grob_property ("horizontal-shift");
324 if (gh_number_p (sh))
325 shift.push (gh_scm2int (sh));
330 for (int i=1; i < shift.size (); i++)
332 if (shift[i-1] == shift[i])
334 clashes[0]->warning (_ ("Too many clashing notecolumns. Ignoring them."));
339 while ((flip (&d))!= UP);
341 Drul_array< Array < Slice > > extents;
342 Drul_array< Array < Real > > offsets;
346 for (int i=0; i < clash_groups[d].size (); i++)
348 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
352 offsets[d].push (d * 0.5 * i);
355 while ((flip (&d))!= UP);
358 do horizontal shifts of each direction
368 for (int i=1; i < clash_groups[d].size (); i++)
370 Slice prev =extents[d][i-1];
371 prev.intersect (extents[d][i]);
372 if (prev.length ()> 0 ||
373 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
374 for (int j = i; j < clash_groups[d].size (); j++)
375 offsets[d][j] += d * 0.5;
378 while ((flip (&d))!= UP);
382 Check if chords are meshing
385 check_meshing_chords (me, &offsets, extents, clash_groups);
389 for (int i=0; i < clash_groups[d].size (); i++)
390 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (), gh_double2scm (offsets[d][i])),
393 while (flip (&d) != UP);
399 Note_collision_interface::forced_shift (Grob *me)
403 SCM s = me->get_grob_property ("elements");
404 for (; gh_pair_p (s); s = ly_cdr (s))
406 Grob * se = unsmob_grob (ly_car (s));
408 SCM force = se->get_grob_property ("force-hshift");
409 if (gh_number_p (force))
411 tups = gh_cons (gh_cons (se->self_scm (), force),
419 Note_collision_interface::add_column (Grob*me,Grob* ncol)
421 ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
422 Axis_group_interface::add_element (me, ncol);
423 me->add_dependency (ncol);
427 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
428 "An object that handles collisions between notes with different stem "
429 "directions and horizontal shifts. Most of the interesting properties "
430 "are to be set in @ref{note-column-interface}: these are "
431 "@code{force-hshift} and @code{horizontal-shift}. ",
432 "merge-differently-dotted merge-differently-headed positioning-done");