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 ugh. the way DONE is done is not clean
31 if (!unsmob_grob (me->get_grob_property ("done")))
33 me->set_grob_property ("done", me->self_scm ());
37 return gh_double2scm (0.0);
42 check_meshing_chords (Grob*me,
43 Drul_array< Array < Real > > *offsets,
44 Drul_array< Array < Slice > > const &extents,
45 Drul_array<Link_array<Grob> > const &clash_groups)
48 if (!extents[UP].size () || ! extents[DOWN].size ())
52 Grob *cu =clash_groups[UP][0];
53 Grob *cd =clash_groups[DOWN][0];
55 Grob * nu_l= Note_column::first_head (cu);
56 Grob * nd_l = Note_column::first_head (cd);
61 this case (distant half collide),
68 the noteheads may be closer than this case (close half collide)
79 bool close_half_collide = false;
80 bool distant_half_collide = false;
81 bool full_collide = false;
86 filter out the 'o's in this configuration, since they're no part
96 Array<int> ups = Stem::note_head_positions (Note_column::stem_l (cu));
97 Array<int> dps = Stem::note_head_positions (Note_column::stem_l (cd));
100 they're too far apart to collide.
104 if (ups[0] > dps.top () + 1)
107 bool touch = (ups[0] - dps.top () >= 0);
109 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
111 merge_possible = merge_possible &&
112 Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
114 if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
115 merge_possible = merge_possible && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
118 while (i < ups.size () && j < dps.size ())
120 if (abs (ups[i] - dps[j]) == 1)
122 merge_possible = false;
124 close_half_collide = true;
126 distant_half_collide = true;
128 else if (ups[i]==dps[j])
130 else if (ups[i] >dps[0] && ups[i] < dps.top ())
131 merge_possible = false;
132 else if (dps[j] >ups[0] && dps[j] < ups.top ())
133 merge_possible = false;
137 else if (ups[i] > dps[j])
146 Drul_array<Real> center_note_shifts;
147 center_note_shifts[LEFT] = 0.0;
148 center_note_shifts[RIGHT] = 0.0;
151 Real shift_amount = 1;
157 for full collisions, the right hand head may obscure dots, so
158 make sure the dotted heads go to the right.
160 if ((Rhythmic_head::dot_count (nu_l) > Rhythmic_head::dot_count (nd_l)
165 TODO: these numbers are magic; should devise a set of grob props
166 to tune this behavior. */
170 else if (close_half_collide && !touch)
171 shift_amount *= 0.52;
172 else if (distant_half_collide && !touch)
174 else if (distant_half_collide || close_half_collide || full_collide)
179 else if (Rhythmic_head::dot_count (nu_l) || Rhythmic_head::dot_count (nd_l))
182 shift_amount *= 0.25;
187 for (int i=0; i < clash_groups[d].size (); i++)
188 (*offsets)[d][i] += d * shift_amount;
190 while ((flip (&d))!= UP);
195 TODO: make callback of this.
199 note-width is hardcoded, making it difficult to handle all note
200 heads sanely. We should really look at the widths of the colliding
201 columns, and have a separate setting for "align stems".
206 Collision::do_shifts (Grob* me)
208 SCM autos (automatic_shift (me));
209 SCM hand (forced_shift (me));
211 Link_array<Grob> done;
215 = gh_scm2double (me->get_grob_property ("note-width"));
217 for (; gh_pair_p (hand); hand =ly_cdr (hand))
219 Grob * s = unsmob_grob (ly_caar (hand));
220 Real amount = gh_scm2double (ly_cdar (hand));
222 s->translate_axis (amount *wid, X_AXIS);
225 for (; gh_pair_p (autos); autos =ly_cdr (autos))
227 Grob * s = unsmob_grob (ly_caar (autos));
228 Real amount = gh_scm2double (ly_cdar (autos));
230 if (!done.find_l (s))
231 s->translate_axis (amount * wid, X_AXIS);
235 /** This complicated routine moves note columns around horizontally to
236 ensure that notes don't clash.
238 This should be put into Scheme.
241 Collision::automatic_shift (Grob *me)
243 Drul_array<Link_array<Grob> > clash_groups;
244 Drul_array<Array<int> > shifts;
247 SCM s = me->get_grob_property ("elements");
248 for (; gh_pair_p (s); s = ly_cdr (s))
250 SCM car = ly_car (s);
252 Grob * se = unsmob_grob (car);
253 if (Note_column::has_interface (se))
254 clash_groups[Note_column::dir (se)].push (se);
261 Array<int> & shift (shifts[d]);
262 Link_array<Grob> & clashes (clash_groups[d]);
264 clashes.sort (Note_column::shift_compare);
266 for (int i=0; i < clashes.size (); i++)
269 = clashes[i]->get_grob_property ("horizontal-shift");
271 if (gh_number_p (sh))
272 shift.push (gh_scm2int (sh));
277 for (int i=1; i < shift.size (); i++)
279 if (shift[i-1] == shift[i])
281 warning (_ ("Too many clashing notecolumns. Ignoring them."));
286 while ((flip (&d))!= UP);
288 Drul_array< Array < Slice > > extents;
289 Drul_array< Array < Real > > offsets;
293 for (int i=0; i < clash_groups[d].size (); i++)
295 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
299 offsets[d].push (d * 0.5 * i);
302 while ((flip (&d))!= UP);
305 do horizontal shifts of each direction
315 for (int i=1; i < clash_groups[d].size (); i++)
317 Slice prev =extents[d][i-1];
318 prev.intersect (extents[d][i]);
319 if (prev.length ()> 0 ||
320 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
321 for (int j = i; j < clash_groups[d].size (); j++)
322 offsets[d][j] += d * 0.5;
325 while ((flip (&d))!= UP);
329 Check if chords are meshing
332 check_meshing_chords (me, &offsets, extents, clash_groups);
336 for (int i=0; i < clash_groups[d].size (); i++)
337 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (), gh_double2scm (offsets[d][i])),
340 while (flip (&d) != UP);
346 Collision::forced_shift (Grob *me)
350 SCM s = me->get_grob_property ("elements");
351 for (; gh_pair_p (s); s = ly_cdr (s))
353 Grob * se = unsmob_grob (ly_car (s));
355 SCM force = se->remove_grob_property ("force-hshift");
356 if (gh_number_p (force))
358 tups = gh_cons (gh_cons (se->self_scm (), force),
366 Collision::add_column (Grob*me,Grob* ncol_l)
368 ncol_l->add_offset_callback (Collision::force_shift_callback_proc, X_AXIS);
369 Axis_group_interface::add_element (me, ncol_l);
370 me->add_dependency (ncol_l);