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);
34 Collision::before_line_breaking ()
36 Array<Shift_tup> autos (automatic_shift ());
37 Array<Shift_tup> hand (forced_shift ());
38 Link_array<Score_element> done;
40 Real wid = paper_l ()->get_var ("collision_note_width");
41 for (int i=0; i < hand.size (); i++)
43 hand[i].e1_->translate_axis (hand[i].e2_ *wid, X_AXIS);
44 done.push (hand[i].e1_);
47 for (int i=0; i < autos.size (); i++)
49 if (!done.find_l (autos[i].e1_))
50 autos[i].e1_->translate_axis (autos[i].e2_ * wid, X_AXIS);
54 /** This complicated routine moves note columns around horizontally to
55 ensure that notes don't clash.
57 This should be done better, probably.
61 Collision::automatic_shift ()
63 Drul_array<Link_array<Note_column> > clash_groups;
64 Drul_array<Array<int> > shifts;
65 Array<Shift_tup> tups;
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.push (Shift_tup (clash_groups[d][i], offsets[d][i]));
184 while (flip (&d) != UP);
190 Collision::forced_shift ()
192 Array<Shift_tup> tups;
194 SCM s = get_elt_property ("elements");
195 for (; gh_pair_p (s); s = gh_cdr (s))
197 Score_element * se = unsmob_element ( gh_car (s));
199 SCM force = se->remove_elt_property ("force-hshift");
200 if (gh_number_p (force))
202 tups. push (Shift_tup (se, gh_scm2double (force)));