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