2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "collision.hh"
10 #include "note-column.hh"
11 #include "rhythmic-head.hh"
12 #include "paper-def.hh"
13 #include "axis-group-interface.hh"
17 Collision::add_column (Score_element*me,Score_element* ncol_l)
19 ncol_l->add_offset_callback (force_shift_callback, X_AXIS);
20 Axis_group_interface::add_element (me, ncol_l);
21 me->add_dependency (ncol_l);
25 Collision::force_shift_callback (Score_element * c, Axis a)
29 Score_element * me = c->parent_l (a);
31 ugh. the way DONE is done is not clean
33 if (!unsmob_element (me->get_elt_property ("done")))
35 me->set_elt_property ("done", me->self_scm ());
43 TODO: make callback of this.
46 Collision::do_shifts(Score_element* me)
48 SCM autos (automatic_shift (me));
49 SCM hand (forced_shift (me));
51 Link_array<Score_element> done;
54 = gh_scm2double (me->get_elt_property ("note-width"))
55 * me->paper_l ()->get_var ("staffspace");
57 for (; gh_pair_p (hand); hand =gh_cdr (hand))
59 Score_element * s = unsmob_element (gh_caar (hand));
60 Real amount = gh_scm2double (gh_cdar (hand));
62 s->translate_axis (amount *wid, X_AXIS);
65 for (; gh_pair_p (autos); autos =gh_cdr (autos))
67 Score_element * s = unsmob_element (gh_caar (autos));
68 Real amount = gh_scm2double (gh_cdar (autos));
71 s->translate_axis (amount * wid, X_AXIS);
75 /** This complicated routine moves note columns around horizontally to
76 ensure that notes don't clash.
78 This should be put into Scheme.
81 Collision::automatic_shift (Score_element *me)
83 Drul_array<Link_array<Score_element> > clash_groups;
84 Drul_array<Array<int> > shifts;
87 SCM s = me->get_elt_property ("elements");
88 for (; gh_pair_p (s); s = gh_cdr (s))
92 Score_element * se = unsmob_element (car);
93 if (Note_column::has_interface (se))
94 clash_groups[Note_column::dir (se)].push (se);
101 Array<int> & shift (shifts[d]);
102 Link_array<Score_element> & clashes (clash_groups[d]);
104 clashes.sort (Note_column::shift_compare);
106 for (int i=0; i < clashes.size (); i++)
109 = clashes[i]->get_elt_property ("horizontal-shift");
111 if (gh_number_p (sh))
112 shift.push (gh_scm2int (sh));
117 for (int i=1; i < shift.size (); i++)
119 if (shift[i-1] == shift[i])
121 warning (_ ("Too many clashing notecolumns. Ignoring them."));
126 while ((flip (&d))!= UP);
128 Drul_array< Array < Slice > > extents;
129 Drul_array< Array < Real > > offsets;
133 for (int i=0; i < clash_groups[d].size (); i++)
135 Slice s(Note_column::head_positions_interval (clash_groups[d][i]));
139 offsets[d].push (d * 0.5 * i);
142 while ((flip (&d))!= UP);
146 for (int i=1; i < clash_groups[d].size (); i++)
148 Slice prev =extents[d][i-1];
149 prev.intersect (extents[d][i]);
150 if (prev.length ()> 0 ||
151 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
152 for (int j = i; j < clash_groups[d].size (); j++)
153 offsets[d][j] += d * 0.5;
156 while ((flip (&d))!= UP);
159 if the up and down version are close, and can not be merged, move
160 all of them again. */
161 if (extents[UP].size () && extents[DOWN].size ())
163 Score_element *cu_l =clash_groups[UP][0];
164 Score_element *cd_l =clash_groups[DOWN][0];
170 Score_element * nu_l= Note_column::first_head(cu_l);
171 Score_element * nd_l = Note_column::first_head(cd_l);
173 int downpos = Note_column::head_positions_interval (cd_l)[BIGGER];
174 int uppos = Note_column::head_positions_interval (cu_l)[SMALLER];
178 && Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
181 if (!to_boolean (me->get_elt_property ("merge-differently-dotted")))
182 merge = merge && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
185 notes are close, but can not be merged. Shift
187 if (abs(uppos - downpos) < 2 && !merge)
190 for (int i=0; i < clash_groups[d].size (); i++)
192 offsets[d][i] -= d * 0.5;
195 while ((flip (&d))!= UP);
200 for (int i=0; i < clash_groups[d].size (); i++)
201 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm (), gh_double2scm (offsets[d][i])),
204 while (flip (&d) != UP);
210 Collision::forced_shift (Score_element *me)
214 SCM s = me->get_elt_property ("elements");
215 for (; gh_pair_p (s); s = gh_cdr (s))
217 Score_element * se = unsmob_element (gh_car (s));
219 SCM force = se->remove_elt_property ("force-hshift");
220 if (gh_number_p (force))
222 tups = gh_cons (gh_cons (se->self_scm (), force),