2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "collision.hh"
11 #include "note-column.hh"
12 #include "rhythmic-head.hh"
13 #include "paper-def.hh"
14 #include "axis-group-interface.hh"
18 MAKE_SCHEME_CALLBACK (Collision,force_shift_callback,2);
21 Collision::force_shift_callback (SCM element_smob, SCM axis)
23 Grob *me = unsmob_grob (element_smob);
24 Axis a = (Axis) gh_scm2int (axis);
27 me = me->get_parent (a);
29 if (! to_boolean (me->get_grob_property ("collision-done")))
31 me->set_grob_property ("collision-done", SCM_BOOL_T);
35 return gh_double2scm (0.0);
40 check_meshing_chords (Grob*me,
41 Drul_array< Array < Real > > *offsets,
42 Drul_array< Array < Slice > > const &extents,
43 Drul_array<Link_array<Grob> > const &clash_groups)
46 if (!extents[UP].size () || ! extents[DOWN].size ())
50 Grob *cu =clash_groups[UP][0];
51 Grob *cd =clash_groups[DOWN][0];
53 Grob * nu_l= Note_column::first_head (cu);
54 Grob * nd_l = Note_column::first_head (cd);
59 this case (distant half collide),
66 the noteheads may be closer than this case (close half collide)
77 bool close_half_collide = false;
78 bool distant_half_collide = false;
79 bool full_collide = false;
84 filter out the 'o's in this configuration, since they're no part
94 Array<int> ups = Stem::note_head_positions (Note_column::stem_l (cu));
95 Array<int> dps = Stem::note_head_positions (Note_column::stem_l (cd));
98 they're too far apart to collide.
102 if (ups[0] > dps.top () + 1)
105 bool touch = (ups[0] - dps.top () >= 0);
107 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
109 merge_possible = merge_possible &&
110 Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
114 don't merge whole notes (or longer, like breve, longa, maxima)
116 merge_possible = merge_possible && (Rhythmic_head::balltype_i (nu_l) > 0);
118 if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
119 merge_possible = merge_possible && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
122 while (i < ups.size () && j < dps.size ())
124 if (abs (ups[i] - dps[j]) == 1)
126 merge_possible = false;
128 close_half_collide = true;
130 distant_half_collide = true;
132 else if (ups[i]==dps[j])
134 else if (ups[i] >dps[0] && ups[i] < dps.top ())
135 merge_possible = false;
136 else if (dps[j] >ups[0] && dps[j] < ups.top ())
137 merge_possible = false;
141 else if (ups[i] > dps[j])
150 Drul_array<Real> center_note_shifts;
151 center_note_shifts[LEFT] = 0.0;
152 center_note_shifts[RIGHT] = 0.0;
155 Real shift_amount = 1;
161 for full collisions, the right hand head may obscure dots, so
162 make sure the dotted heads go to the right.
164 if ((Rhythmic_head::dot_count (nu_l) > Rhythmic_head::dot_count (nd_l)
169 TODO: these numbers are magic; should devise a set of grob props
170 to tune this behavior. */
174 else if (close_half_collide && !touch)
175 shift_amount *= 0.52;
176 else if (distant_half_collide && !touch)
178 else if (distant_half_collide || close_half_collide || full_collide)
184 else if (Rhythmic_head::dot_count (nu_l) || Rhythmic_head::dot_count (nd_l))
187 shift_amount *= 0.25;
192 for (int i=0; i < clash_groups[d].size (); i++)
193 (*offsets)[d][i] += d * shift_amount;
195 while ((flip (&d))!= UP);
200 TODO: make callback of this.
204 note-width is hardcoded, making it difficult to handle all note
205 heads sanely. We should really look at the widths of the colliding
206 columns, and have a separate setting for "align stems".
211 Collision::do_shifts (Grob* me)
213 SCM autos (automatic_shift (me));
214 SCM hand (forced_shift (me));
216 Link_array<Grob> done;
220 = gh_scm2double (me->get_grob_property ("note-width"));
222 for (; gh_pair_p (hand); hand =ly_cdr (hand))
224 Grob * s = unsmob_grob (ly_caar (hand));
225 Real amount = gh_scm2double (ly_cdar (hand));
227 s->translate_axis (amount *wid, X_AXIS);
230 for (; gh_pair_p (autos); autos =ly_cdr (autos))
232 Grob * s = unsmob_grob (ly_caar (autos));
233 Real amount = gh_scm2double (ly_cdar (autos));
235 if (!done.find_l (s))
236 s->translate_axis (amount * wid, X_AXIS);
240 /** This complicated routine moves note columns around horizontally to
241 ensure that notes don't clash.
243 This should be put into Scheme.
246 Collision::automatic_shift (Grob *me)
248 Drul_array<Link_array<Grob> > clash_groups;
249 Drul_array<Array<int> > shifts;
252 SCM s = me->get_grob_property ("elements");
253 for (; gh_pair_p (s); s = ly_cdr (s))
255 SCM car = ly_car (s);
257 Grob * se = unsmob_grob (car);
258 if (Note_column::has_interface (se))
259 clash_groups[Note_column::dir (se)].push (se);
266 Array<int> & shift (shifts[d]);
267 Link_array<Grob> & clashes (clash_groups[d]);
269 clashes.sort (Note_column::shift_compare);
271 for (int i=0; i < clashes.size (); i++)
274 = clashes[i]->get_grob_property ("horizontal-shift");
276 if (gh_number_p (sh))
277 shift.push (gh_scm2int (sh));
282 for (int i=1; i < shift.size (); i++)
284 if (shift[i-1] == shift[i])
286 me->warning (_ ("Too many clashing notecolumns. Ignoring them."));
291 while ((flip (&d))!= UP);
293 Drul_array< Array < Slice > > extents;
294 Drul_array< Array < Real > > offsets;
298 for (int i=0; i < clash_groups[d].size (); i++)
300 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
304 offsets[d].push (d * 0.5 * i);
307 while ((flip (&d))!= UP);
310 do horizontal shifts of each direction
320 for (int i=1; i < clash_groups[d].size (); i++)
322 Slice prev =extents[d][i-1];
323 prev.intersect (extents[d][i]);
324 if (prev.length ()> 0 ||
325 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
326 for (int j = i; j < clash_groups[d].size (); j++)
327 offsets[d][j] += d * 0.5;
330 while ((flip (&d))!= UP);
334 Check if chords are meshing
337 check_meshing_chords (me, &offsets, extents, clash_groups);
341 for (int i=0; i < clash_groups[d].size (); i++)
342 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (), gh_double2scm (offsets[d][i])),
345 while (flip (&d) != UP);
351 Collision::forced_shift (Grob *me)
355 SCM s = me->get_grob_property ("elements");
356 for (; gh_pair_p (s); s = ly_cdr (s))
358 Grob * se = unsmob_grob (ly_car (s));
360 SCM force = se->remove_grob_property ("force-hshift");
361 if (gh_number_p (force))
363 tups = gh_cons (gh_cons (se->self_scm (), force),
371 Collision::add_column (Grob*me,Grob* ncol_l)
373 ncol_l->add_offset_callback (Collision::force_shift_callback_proc, X_AXIS);
374 Axis_group_interface::add_element (me, ncol_l);
375 me->add_dependency (ncol_l);
379 ADD_INTERFACE (Collision, "note-collision-interface",
380 "An object that handles collisions between notes with different
381 stem directions and horizontal shifts. Most of the interesting
382 properties are to be set in @ref{note-column-interface}",
383 "merge-differently-dotted note-width collision-done");