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 "note-head.hh"
12 #include "paper-def.hh"
13 #include "axis-group-interface.hh"
16 Collision::Collision()
18 Axis_group_interface (this).set_interface ();
19 Axis_group_interface (this).set_axes (X_AXIS, Y_AXIS);
23 Collision::add_column (Note_column* ncol_l)
25 Axis_group_interface (this).add_element (ncol_l);
26 add_dependency (ncol_l);
30 Collision::before_line_breaking ()
32 SCM autos (automatic_shift ());
33 SCM hand (forced_shift ());
34 Link_array<Score_element> done;
36 Real wid = paper_l ()->get_var ("collision_note_width");
37 for (; gh_pair_p (hand); hand =gh_cdr (hand))
39 Score_element * s = unsmob_element (gh_caar (hand));
40 Real amount = gh_scm2double (gh_cdar (hand));
42 s->translate_axis (amount *wid, X_AXIS);
45 for (; gh_pair_p (autos); autos =gh_cdr (autos))
47 Score_element * s = unsmob_element (gh_caar (autos));
48 Real amount = gh_scm2double (gh_cdar (autos));
51 s->translate_axis (amount * wid, X_AXIS);
55 /** This complicated routine moves note columns around horizontally to
56 ensure that notes don't clash.
58 This should be done better, probably.
62 Collision::automatic_shift ()
64 Drul_array<Link_array<Note_column> > clash_groups;
65 Drul_array<Array<int> > shifts;
68 SCM s = get_elt_property ("elements");
69 for (; gh_pair_p (s); s = gh_cdr (s))
73 Score_element * se = unsmob_element (car);
74 if (Note_column * col = dynamic_cast<Note_column*> (se))
75 clash_groups[col->dir ()].push (col);
82 Array<int> & shift (shifts[d]);
83 Link_array<Note_column> & clashes (clash_groups[d]);
85 clashes.sort (Note_column::shift_compare);
87 for (int i=0; i < clashes.size (); i++)
90 = clashes[i]->remove_elt_property ("horizontal-shift");
93 shift.push (gh_scm2int (sh));
98 for (int i=1; i < shift.size (); i++)
100 if (shift[i-1] == shift[i])
102 warning (_ ("Too many clashing notecolumns. Ignoring them."));
107 while ((flip (&d))!= UP);
109 Drul_array< Array < Slice > > extents;
110 Drul_array< Array < Real > > offsets;
114 for (int i=0; i < clash_groups[d].size (); i++)
116 Slice s(clash_groups[d][i]->head_positions_interval ());
120 offsets[d].push (d * 0.5 * i);
123 while ((flip (&d))!= UP);
127 for (int i=1; i < clash_groups[d].size (); i++)
129 Slice prev =extents[d][i-1];
130 prev.intersect (extents[d][i]);
131 if (prev.length ()> 0 ||
132 (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
133 for (int j = i; j < clash_groups[d].size (); j++)
134 offsets[d][j] += d * 0.5;
137 while ((flip (&d))!= UP);
140 if the up and down version are close, and can not be merged, move
141 all of them again. */
142 if (extents[UP].size () && extents[DOWN].size ())
144 Note_column *cu_l =clash_groups[UP][0];
145 Note_column *cd_l =clash_groups[DOWN][0];
151 Note_head * nu_l= cu_l->first_head();
152 Note_head * nd_l = cd_l->first_head();
154 int downpos = cd_l->head_positions_interval ()[BIGGER];
155 int uppos = cu_l->head_positions_interval ()[SMALLER];
159 && nu_l->balltype_i () == nd_l->balltype_i ();
162 if (!to_boolean (get_elt_property ("merge-differently-dotted")))
163 merge = merge && nu_l->dot_count () == nd_l->dot_count ();
166 notes are close, but can not be merged. Shift
168 if (abs(uppos - downpos) < 2 && !merge)
171 for (int i=0; i < clash_groups[d].size (); i++)
173 offsets[d][i] -= d * 0.5;
176 while ((flip (&d))!= UP);
181 for (int i=0; i < clash_groups[d].size (); i++)
182 tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm_, gh_double2scm (offsets[d][i])),
185 while (flip (&d) != UP);
191 Collision::forced_shift ()
195 SCM s = get_elt_property ("elements");
196 for (; gh_pair_p (s); s = gh_cdr (s))
198 Score_element * se = unsmob_element ( gh_car (s));
200 SCM force = se->remove_elt_property ("force-hshift");
201 if (gh_number_p (force))
203 tups = gh_cons (gh_cons (se->self_scm_, force),