]> git.donarmstrong.com Git - lilypond.git/blob - lily/collision.cc
release: 1.3.37
[lilypond.git] / lily / collision.cc
1 /*
2   collision.cc -- implement Collision
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "debug.hh"
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"
14
15
16 Collision::Collision()
17 {
18   Axis_group_interface (this).set_interface ();
19   Axis_group_interface (this).set_axes (X_AXIS, Y_AXIS);
20 }
21
22 void
23 Collision::add_column (Note_column* ncol_l)
24 {
25   Axis_group_interface (this).add_element (ncol_l);
26   add_dependency (ncol_l);
27 }
28
29 /*
30   UGH.  junk Shift_tup .
31  */
32
33 void
34 Collision::before_line_breaking ()
35 {
36   Array<Shift_tup> autos (automatic_shift ());
37   Array<Shift_tup> hand (forced_shift ());
38   Link_array<Score_element> done;
39   
40   Real wid = paper_l ()->get_var ("collision_note_width");
41   for (int i=0; i < hand.size (); i++)
42     {
43       hand[i].e1_->translate_axis (hand[i].e2_ *wid, X_AXIS);
44       done.push (hand[i].e1_);
45     }
46
47   for (int i=0; i < autos.size (); i++)
48     {
49       if (!done.find_l (autos[i].e1_))
50         autos[i].e1_->translate_axis (autos[i].e2_ * wid, X_AXIS);
51     }
52 }
53
54 /** This complicated routine moves note columns around horizontally to
55   ensure that notes don't clash.
56
57   This should be done better, probably.
58
59   */
60 Array< Shift_tup >
61 Collision::automatic_shift ()
62 {
63   Drul_array<Link_array<Note_column> > clash_groups;
64   Drul_array<Array<int> > shifts;
65   Array<Shift_tup>  tups;
66
67
68   SCM s = get_elt_property ("elements");
69   for (; gh_pair_p (s); s = gh_cdr (s))
70     {
71       SCM car = gh_car (s);
72
73       Score_element * se = unsmob_element (car);
74       if (Note_column * col = dynamic_cast<Note_column*> (se))
75         clash_groups[col->dir ()].push (col);
76     }
77
78   
79   Direction d = UP;
80   do
81     {
82       Array<int> & shift (shifts[d]);
83       Link_array<Note_column> & clashes (clash_groups[d]);
84
85       clashes.sort (Note_column::shift_compare);
86
87       for (int i=0; i < clashes.size (); i++)
88         {
89           SCM sh
90             = clashes[i]->remove_elt_property ("horizontal-shift");
91
92           if (gh_number_p (sh))
93             shift.push (gh_scm2int (sh));
94           else
95             shift.push (0);
96         }
97       
98       for (int i=1; i < shift.size (); i++)
99         {
100           if (shift[i-1] == shift[i])
101             {
102               warning (_ ("Too many clashing notecolumns.  Ignoring them."));
103               return tups;
104             }
105         }
106     }
107   while ((flip (&d))!= UP);
108
109   Drul_array< Array < Slice > > extents;
110   Drul_array< Array < Real > > offsets;
111   d = UP;
112   do
113     {
114       for (int i=0; i < clash_groups[d].size (); i++)
115         {
116           Slice s(clash_groups[d][i]->head_positions_interval ());
117           s[LEFT] --;
118           s[RIGHT]++;
119           extents[d].push (s);
120           offsets[d].push (d * 0.5 * i);
121         }
122     }
123   while ((flip (&d))!= UP);
124   
125   do
126     {
127       for (int i=1; i < clash_groups[d].size (); i++)
128         {
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;
135         }
136     }   
137   while ((flip (&d))!= UP);
138
139   /*
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 ())
143     {
144       Note_column *cu_l =clash_groups[UP][0];
145       Note_column *cd_l =clash_groups[DOWN][0];
146
147
148       /*
149         TODO.
150        */
151       Note_head * nu_l= cu_l->first_head();
152       Note_head * nd_l = cd_l->first_head();
153       
154       int downpos =     cd_l->head_positions_interval ()[BIGGER];
155       int uppos =       cu_l->head_positions_interval ()[SMALLER];      
156       
157       bool merge  =
158         downpos == uppos
159         && nu_l->balltype_i () == nd_l->balltype_i ();
160
161
162       if (!to_boolean (get_elt_property ("merge-differently-dotted")))
163         merge = merge && nu_l->dot_count () == nd_l->dot_count ();
164
165       /*
166         notes are close, but can not be merged.  Shift
167        */
168       if (abs(uppos - downpos) < 2 && !merge)
169           do
170           {
171             for (int i=0; i < clash_groups[d].size (); i++)
172               {
173                 offsets[d][i] -= d * 0.5;
174               }
175           }
176           while ((flip (&d))!= UP);
177     }
178
179   do
180     {
181       for (int i=0; i < clash_groups[d].size (); i++)
182         tups.push (Shift_tup (clash_groups[d][i], offsets[d][i]));
183     }
184   while (flip (&d) != UP);
185   return tups;
186 }
187
188
189 Array <Shift_tup>
190 Collision::forced_shift ()
191 {
192   Array<Shift_tup> tups;
193   
194   SCM s = get_elt_property ("elements");
195   for (; gh_pair_p (s); s = gh_cdr (s))
196     {
197       Score_element * se = unsmob_element ( gh_car (s));
198
199       SCM force =  se->remove_elt_property ("force-hshift");
200       if (gh_number_p (force))
201         {
202           tups. push (Shift_tup (se, gh_scm2double (force)));
203         }
204     }
205   return tups;
206 }
207
208
209