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