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);
41 TODO: make callback of this.
44 Collision::do_shifts (Grob* me)
46 SCM autos (automatic_shift (me));
47 SCM hand (forced_shift (me));
49 Link_array<Grob> done;
52 = gh_scm2double (me->get_grob_property ("note-width"));
54 for (; gh_pair_p (hand); hand =gh_cdr (hand))
56 Grob * s = unsmob_grob (gh_caar (hand));
57 Real amount = gh_scm2double (gh_cdar (hand));
59 s->translate_axis (amount *wid, X_AXIS);
62 for (; gh_pair_p (autos); autos =gh_cdr (autos))
64 Grob * s = unsmob_grob (gh_caar (autos));
65 Real amount = gh_scm2double (gh_cdar (autos));
68 s->translate_axis (amount * wid, X_AXIS);
72 /** This complicated routine moves note columns around horizontally to
73 ensure that notes don't clash.
75 This should be put into Scheme.
78 Collision::automatic_shift (Grob *me)
80 Drul_array<Link_array<Grob> > clash_groups;
81 Drul_array<Array<int> > shifts;
84 SCM s = me->get_grob_property ("elements");
85 for (; gh_pair_p (s); s = gh_cdr (s))
89 Grob * se = unsmob_grob (car);
90 if (Note_column::has_interface (se))
91 clash_groups[Note_column::dir (se)].push (se);
98 Array<int> & shift (shifts[d]);
99 Link_array<Grob> & clashes (clash_groups[d]);
101 clashes.sort (Note_column::shift_compare);
103 for (int i=0; i < clashes.size (); i++)
106 = clashes[i]->get_grob_property ("horizontal-shift");
108 if (gh_number_p (sh))
109 shift.push (gh_scm2int (sh));
114 for (int i=1; i < shift.size (); i++)
116 if (shift[i-1] == shift[i])
118 warning (_ ("Too many clashing notecolumns. Ignoring them."));
123 while ((flip (&d))!= UP);
125 Drul_array< Array < Slice > > extents;
126 Drul_array< Array < Real > > offsets;
130 for (int i=0; i < clash_groups[d].size (); i++)
132 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
136 offsets[d].push (d * 0.5 * i);
139 while ((flip (&d))!= UP);
143 for (int i=1; i < clash_groups[d].size (); i++)
145 Slice prev =extents[d][i-1];
146 prev.intersect (extents[d][i]);
147 if (prev.length ()> 0 ||
148 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
149 for (int j = i; j < clash_groups[d].size (); j++)
150 offsets[d][j] += d * 0.5;
153 while ((flip (&d))!= UP);
156 if the up and down version are close, and can not be merged, move
157 all of them again. */
158 if (extents[UP].size () && extents[DOWN].size ())
160 Grob *cu_l =clash_groups[UP][0];
161 Grob *cd_l =clash_groups[DOWN][0];
167 Grob * nu_l= Note_column::first_head (cu_l);
168 Grob * nd_l = Note_column::first_head (cd_l);
170 int downpos = Note_column::head_positions_interval (cd_l)[BIGGER];
171 int uppos = Note_column::head_positions_interval (cu_l)[SMALLER];
175 && Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
178 if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
179 merge = merge && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
182 notes are close, but can not be merged. Shift
184 if (abs (uppos - downpos) < 2 && !merge)
187 for (int i=0; i < clash_groups[d].size (); i++)
189 offsets[d][i] -= d * 0.5;
192 while ((flip (&d))!= UP);
197 for (int i=0; i < clash_groups[d].size (); i++)
198 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (), gh_double2scm (offsets[d][i])),
201 while (flip (&d) != UP);
207 Collision::forced_shift (Grob *me)
211 SCM s = me->get_grob_property ("elements");
212 for (; gh_pair_p (s); s = gh_cdr (s))
214 Grob * se = unsmob_grob (gh_car (s));
216 SCM force = se->remove_grob_property ("force-hshift");
217 if (gh_number_p (force))
219 tups = gh_cons (gh_cons (se->self_scm (), force),
227 Collision::add_column (Grob*me,Grob* ncol_l)
229 ncol_l->add_offset_callback (Collision::force_shift_callback_proc, X_AXIS);
230 Axis_group_interface::add_element (me, ncol_l);
231 me->add_dependency (ncol_l);