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.
45 note-width is hardcoded, making it difficult to handle all note
46 heads sanely. We should really look at the widths of the colliding
47 columns, and have a separate setting for "align stems".
52 Collision::do_shifts (Grob* me)
54 SCM autos (automatic_shift (me));
55 SCM hand (forced_shift (me));
57 Link_array<Grob> done;
61 = gh_scm2double (me->get_grob_property ("note-width"));
63 for (; gh_pair_p (hand); hand =gh_cdr (hand))
65 Grob * s = unsmob_grob (gh_caar (hand));
66 Real amount = gh_scm2double (gh_cdar (hand));
68 s->translate_axis (amount *wid, X_AXIS);
71 for (; gh_pair_p (autos); autos =gh_cdr (autos))
73 Grob * s = unsmob_grob (gh_caar (autos));
74 Real amount = gh_scm2double (gh_cdar (autos));
77 s->translate_axis (amount * wid, X_AXIS);
81 /** This complicated routine moves note columns around horizontally to
82 ensure that notes don't clash.
84 This should be put into Scheme.
87 Collision::automatic_shift (Grob *me)
89 Drul_array<Link_array<Grob> > clash_groups;
90 Drul_array<Array<int> > shifts;
93 SCM s = me->get_grob_property ("elements");
94 for (; gh_pair_p (s); s = gh_cdr (s))
98 Grob * se = unsmob_grob (car);
99 if (Note_column::has_interface (se))
100 clash_groups[Note_column::dir (se)].push (se);
107 Array<int> & shift (shifts[d]);
108 Link_array<Grob> & clashes (clash_groups[d]);
110 clashes.sort (Note_column::shift_compare);
112 for (int i=0; i < clashes.size (); i++)
115 = clashes[i]->get_grob_property ("horizontal-shift");
117 if (gh_number_p (sh))
118 shift.push (gh_scm2int (sh));
123 for (int i=1; i < shift.size (); i++)
125 if (shift[i-1] == shift[i])
127 warning (_ ("Too many clashing notecolumns. Ignoring them."));
132 while ((flip (&d))!= UP);
134 Drul_array< Array < Slice > > extents;
135 Drul_array< Array < Real > > offsets;
139 for (int i=0; i < clash_groups[d].size (); i++)
141 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
145 offsets[d].push (d * 0.5 * i);
148 while ((flip (&d))!= UP);
152 for (int i=1; i < clash_groups[d].size (); i++)
154 Slice prev =extents[d][i-1];
155 prev.intersect (extents[d][i]);
156 if (prev.length ()> 0 ||
157 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
158 for (int j = i; j < clash_groups[d].size (); j++)
159 offsets[d][j] += d * 0.5;
162 while ((flip (&d))!= UP);
165 if the up and down version are close, and can not be merged, move
166 all of them again. */
167 if (extents[UP].size () && extents[DOWN].size ())
169 Grob *cu_l =clash_groups[UP][0];
170 Grob *cd_l =clash_groups[DOWN][0];
176 Grob * nu_l= Note_column::first_head (cu_l);
177 Grob * nd_l = Note_column::first_head (cd_l);
179 int downpos = Note_column::head_positions_interval (cd_l)[BIGGER];
180 int uppos = Note_column::head_positions_interval (cu_l)[SMALLER];
184 && Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
187 if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
188 merge = merge && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
191 notes are close, but can not be merged. Shift
193 if (abs (uppos - downpos) < 2 && !merge)
196 for (int i=0; i < clash_groups[d].size (); i++)
198 if(Rhythmic_head::dot_count (nu_l) > Rhythmic_head::dot_count (nd_l))
199 offsets[d][i] += d * 0.5;
201 offsets[d][i] -= d * 0.5;
204 while ((flip (&d))!= UP);
209 for (int i=0; i < clash_groups[d].size (); i++)
210 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (), gh_double2scm (offsets[d][i])),
213 while (flip (&d) != UP);
219 Collision::forced_shift (Grob *me)
223 SCM s = me->get_grob_property ("elements");
224 for (; gh_pair_p (s); s = gh_cdr (s))
226 Grob * se = unsmob_grob (gh_car (s));
228 SCM force = se->remove_grob_property ("force-hshift");
229 if (gh_number_p (force))
231 tups = gh_cons (gh_cons (se->self_scm (), force),
239 Collision::add_column (Grob*me,Grob* ncol_l)
241 ncol_l->add_offset_callback (Collision::force_shift_callback_proc, X_AXIS);
242 Axis_group_interface::add_element (me, ncol_l);
243 me->add_dependency (ncol_l);