]> git.donarmstrong.com Git - lilypond.git/blob - lily/collision.cc
release: 1.3.51
[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 void
30 Collision::before_line_breaking ()
31 {
32   do_shifts();
33 }
34
35 /*
36   TODO: make callback of this.
37  */
38 void
39 Collision::do_shifts()
40 {
41   SCM autos (automatic_shift ());
42   SCM hand (forced_shift ());
43   Link_array<Score_element> done;
44   
45   Real wid = paper_l ()->get_var ("collision_note_width");
46   for (; gh_pair_p (hand); hand =gh_cdr (hand))
47     {
48       Score_element * s = unsmob_element (gh_caar (hand));
49       Real amount = gh_scm2double (gh_cdar (hand));
50       
51       s->translate_axis (amount *wid, X_AXIS);
52       done.push (s);
53     }
54   for (; gh_pair_p (autos); autos =gh_cdr (autos))
55     {
56       Score_element * s = unsmob_element (gh_caar (autos));
57       Real amount = gh_scm2double (gh_cdar (autos));
58       
59       if (!done.find_l (s))
60         s->translate_axis (amount * wid, X_AXIS);
61     }
62 }
63
64 /** This complicated routine moves note columns around horizontally to
65   ensure that notes don't clash.
66
67   This should be put into Scheme.  
68   */
69 SCM
70 Collision::automatic_shift ()
71 {
72   Drul_array<Link_array<Note_column> > clash_groups;
73   Drul_array<Array<int> > shifts;
74   SCM  tups = SCM_EOL;
75
76   SCM s = get_elt_property ("elements");
77   for (; gh_pair_p (s); s = gh_cdr (s))
78     {
79       SCM car = gh_car (s);
80
81       Score_element * se = unsmob_element (car);
82       if (Note_column * col = dynamic_cast<Note_column*> (se))
83         clash_groups[col->dir ()].push (col);
84     }
85
86   
87   Direction d = UP;
88   do
89     {
90       Array<int> & shift (shifts[d]);
91       Link_array<Note_column> & clashes (clash_groups[d]);
92
93       clashes.sort (Note_column::shift_compare);
94
95       for (int i=0; i < clashes.size (); i++)
96         {
97           SCM sh
98             = clashes[i]->remove_elt_property ("horizontal-shift");
99
100           if (gh_number_p (sh))
101             shift.push (gh_scm2int (sh));
102           else
103             shift.push (0);
104         }
105       
106       for (int i=1; i < shift.size (); i++)
107         {
108           if (shift[i-1] == shift[i])
109             {
110               warning (_ ("Too many clashing notecolumns.  Ignoring them."));
111               return tups;
112             }
113         }
114     }
115   while ((flip (&d))!= UP);
116
117   Drul_array< Array < Slice > > extents;
118   Drul_array< Array < Real > > offsets;
119   d = UP;
120   do
121     {
122       for (int i=0; i < clash_groups[d].size (); i++)
123         {
124           Slice s(clash_groups[d][i]->head_positions_interval ());
125           s[LEFT] --;
126           s[RIGHT]++;
127           extents[d].push (s);
128           offsets[d].push (d * 0.5 * i);
129         }
130     }
131   while ((flip (&d))!= UP);
132   
133   do
134     {
135       for (int i=1; i < clash_groups[d].size (); i++)
136         {
137           Slice prev =extents[d][i-1];
138           prev.intersect (extents[d][i]);
139           if (prev.length ()> 0 ||
140               (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
141             for (int j = i; j <  clash_groups[d].size (); j++)
142               offsets[d][j] += d * 0.5;
143         }
144     }   
145   while ((flip (&d))!= UP);
146
147   /*
148     if the up and down version are close, and can not be merged, move
149     all of them again. */
150   if (extents[UP].size () && extents[DOWN].size ())
151     {
152       Note_column *cu_l =clash_groups[UP][0];
153       Note_column *cd_l =clash_groups[DOWN][0];
154
155
156       /*
157         TODO.
158        */
159       Note_head * nu_l= cu_l->first_head();
160       Note_head * nd_l = cd_l->first_head();
161       
162       int downpos = cd_l->head_positions_interval ()[BIGGER];
163       int uppos = cu_l->head_positions_interval ()[SMALLER];      
164       
165       bool merge  =
166         downpos == uppos
167         && nu_l->balltype_i () == nd_l->balltype_i ();
168
169
170       if (!to_boolean (get_elt_property ("merge-differently-dotted")))
171         merge = merge && nu_l->dot_count () == nd_l->dot_count ();
172
173       /*
174         notes are close, but can not be merged.  Shift
175        */
176       if (abs(uppos - downpos) < 2 && !merge)
177           do
178           {
179             for (int i=0; i < clash_groups[d].size (); i++)
180               {
181                 offsets[d][i] -= d * 0.5;
182               }
183           }
184           while ((flip (&d))!= UP);
185     }
186
187   do
188     {
189       for (int i=0; i < clash_groups[d].size (); i++)
190         tups = gh_cons (gh_cons (clash_groups[d][i]->self_scm_, gh_double2scm (offsets[d][i])),
191                                  tups);
192     }
193   while (flip (&d) != UP);
194   return tups;
195 }
196
197
198 SCM
199 Collision::forced_shift ()
200 {
201   SCM tups = SCM_EOL;
202   
203   SCM s = get_elt_property ("elements");
204   for (; gh_pair_p (s); s = gh_cdr (s))
205     {
206       Score_element * se = unsmob_element ( gh_car (s));
207
208       SCM force =  se->remove_elt_property ("force-hshift");
209       if (gh_number_p (force))
210         {
211           tups = gh_cons (gh_cons (se->self_scm_, force),
212                           tups);
213         }
214     }
215   return tups;
216 }
217
218
219