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