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