2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 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->parent_l (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);
60 bool half_collide = false;
61 bool full_collide = false;
66 filter out the 'o's in this configuration, since they're no part
76 Array<int> ups = Stem::note_head_positions (Note_column::stem_l (cu));
77 Array<int> dps = Stem::note_head_positions (Note_column::stem_l (cd));
80 they're too far apart to collide.
84 if (ups[0] > dps.top () + 1)
87 bool touch = (ups[0] - dps.top () >= 0);
89 bool merge_possible = (ups[0] >= dps[0]) && (ups.top () <= dps.top ());
91 merge_possible = merge_possible &&
92 Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
94 if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
95 merge_possible = merge_possible && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
98 while (i < ups.size () && j < dps.size ())
100 if (abs (ups[i] - dps[j]) == 1)
102 merge_possible = false;
105 else if (ups[i]==dps[j])
107 else if (ups[i] >dps[0] && ups[i] < dps.top ())
108 merge_possible = false;
109 else if (dps[j] >ups[0] && dps[j] < ups.top ())
110 merge_possible = false;
114 else if (ups[i] > dps[j])
123 Drul_array<Real> center_note_shifts;
124 center_note_shifts[LEFT] = 0.0;
125 center_note_shifts[RIGHT] = 0.0;
128 Real shift_amount = 1;
134 for full collisions, the right hand head may obscure dots, so
135 make sure the dotted heads go to the right.
137 if ((Rhythmic_head::dot_count (nu_l) < Rhythmic_head::dot_count (nd_l)
143 else if (half_collide || full_collide)
146 shift_amount *= 0.25;
151 for (int i=0; i < clash_groups[d].size (); i++)
152 (*offsets)[d][i] += d * shift_amount;
154 while ((flip (&d))!= UP);
159 TODO: make callback of this.
163 note-width is hardcoded, making it difficult to handle all note
164 heads sanely. We should really look at the widths of the colliding
165 columns, and have a separate setting for "align stems".
170 Collision::do_shifts (Grob* me)
172 SCM autos (automatic_shift (me));
173 SCM hand (forced_shift (me));
175 Link_array<Grob> done;
179 = gh_scm2double (me->get_grob_property ("note-width"));
181 for (; gh_pair_p (hand); hand =gh_cdr (hand))
183 Grob * s = unsmob_grob (gh_caar (hand));
184 Real amount = gh_scm2double (gh_cdar (hand));
186 s->translate_axis (amount *wid, X_AXIS);
189 for (; gh_pair_p (autos); autos =gh_cdr (autos))
191 Grob * s = unsmob_grob (gh_caar (autos));
192 Real amount = gh_scm2double (gh_cdar (autos));
194 if (!done.find_l (s))
195 s->translate_axis (amount * wid, X_AXIS);
199 /** This complicated routine moves note columns around horizontally to
200 ensure that notes don't clash.
202 This should be put into Scheme.
205 Collision::automatic_shift (Grob *me)
207 Drul_array<Link_array<Grob> > clash_groups;
208 Drul_array<Array<int> > shifts;
211 SCM s = me->get_grob_property ("elements");
212 for (; gh_pair_p (s); s = gh_cdr (s))
214 SCM car = gh_car (s);
216 Grob * se = unsmob_grob (car);
217 if (Note_column::has_interface (se))
218 clash_groups[Note_column::dir (se)].push (se);
225 Array<int> & shift (shifts[d]);
226 Link_array<Grob> & clashes (clash_groups[d]);
228 clashes.sort (Note_column::shift_compare);
230 for (int i=0; i < clashes.size (); i++)
233 = clashes[i]->get_grob_property ("horizontal-shift");
235 if (gh_number_p (sh))
236 shift.push (gh_scm2int (sh));
241 for (int i=1; i < shift.size (); i++)
243 if (shift[i-1] == shift[i])
245 warning (_ ("Too many clashing notecolumns. Ignoring them."));
250 while ((flip (&d))!= UP);
252 Drul_array< Array < Slice > > extents;
253 Drul_array< Array < Real > > offsets;
257 for (int i=0; i < clash_groups[d].size (); i++)
259 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
263 offsets[d].push (d * 0.5 * i);
266 while ((flip (&d))!= UP);
269 do horizontal shifts of each direction
279 for (int i=1; i < clash_groups[d].size (); i++)
281 Slice prev =extents[d][i-1];
282 prev.intersect (extents[d][i]);
283 if (prev.length ()> 0 ||
284 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
285 for (int j = i; j < clash_groups[d].size (); j++)
286 offsets[d][j] += d * 0.5;
289 while ((flip (&d))!= UP);
293 Check if chords are meshing
296 check_meshing_chords (me, &offsets, extents, clash_groups);
300 if the up and down version are close, and can not be merged, move
301 all of them again. */
302 if (extents[UP].size () && extents[DOWN].size ())
304 Grob *cu_l =clash_groups[UP][0];
305 Grob *cd_l =clash_groups[DOWN][0];
311 Grob * nu_l= Note_column::first_head (cu_l);
312 Grob * nd_l = Note_column::first_head (cd_l);
314 int downpos = Note_column::head_positions_interval (cd_l)[BIGGER];
315 int uppos = Note_column::head_positions_interval (cu_l)[SMALLER];
319 && Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
322 if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
323 merge = merge && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
326 notes are close, but can not be merged. Shift
328 if (abs (uppos - downpos) < 2 && !merge)
331 for (int i=0; i < clash_groups[d].size (); i++)
333 if(Rhythmic_head::dot_count (nu_l) > Rhythmic_head::dot_count (nd_l))
334 offsets[d][i] += d * 0.5;
336 offsets[d][i] -= d * 0.5;
339 while ((flip (&d))!= UP);
345 for (int i=0; i < clash_groups[d].size (); i++)
346 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (), gh_double2scm (offsets[d][i])),
349 while (flip (&d) != UP);
355 Collision::forced_shift (Grob *me)
359 SCM s = me->get_grob_property ("elements");
360 for (; gh_pair_p (s); s = gh_cdr (s))
362 Grob * se = unsmob_grob (gh_car (s));
364 SCM force = se->remove_grob_property ("force-hshift");
365 if (gh_number_p (force))
367 tups = gh_cons (gh_cons (se->self_scm (), force),
375 Collision::add_column (Grob*me,Grob* ncol_l)
377 ncol_l->add_offset_callback (Collision::force_shift_callback_proc, X_AXIS);
378 Axis_group_interface::add_element (me, ncol_l);
379 me->add_dependency (ncol_l);