]> git.donarmstrong.com Git - lilypond.git/blob - lily/collision.cc
5c0ae795180a2282a5e0150e3b1567b43854e175
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "debug.hh"
10 #include "collision.hh"
11 #include "note-column.hh"
12 #include "rhythmic-head.hh"
13 #include "paper-def.hh"
14 #include "axis-group-interface.hh"
15 #include "item.hh"
16
17
18 MAKE_SCHEME_CALLBACK (Collision,force_shift_callback,2);
19
20 SCM
21 Collision::force_shift_callback (SCM element_smob, SCM axis)
22 {
23   Grob *me = unsmob_grob (element_smob);
24   Axis a = (Axis) gh_scm2int (axis);
25   assert (a == X_AXIS);
26   
27    me = me->parent_l (a);
28   /*
29     ugh. the way DONE is done is not clean
30    */
31   if (!unsmob_grob (me->get_grob_property ("done")))
32     {
33       me->set_grob_property ("done", me->self_scm ());
34       do_shifts (me);
35     }
36   
37   return gh_double2scm (0.0);
38 }
39
40 /*
41   TODO: make callback of this.
42  */
43 void
44 Collision::do_shifts (Grob* me)
45 {
46   SCM autos (automatic_shift (me));
47   SCM hand (forced_shift (me));
48   
49   Link_array<Grob> done;
50   
51   Real wid
52     = gh_scm2double (me->get_grob_property ("note-width"));
53   
54   for (; gh_pair_p (hand); hand =gh_cdr (hand))
55     {
56       Grob * s = unsmob_grob (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       Grob * s = unsmob_grob (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 (Grob *me)
79 {
80   Drul_array<Link_array<Grob> > clash_groups;
81   Drul_array<Array<int> > shifts;
82   SCM  tups = SCM_EOL;
83
84   SCM s = me->get_grob_property ("elements");
85   for (; gh_pair_p (s); s = gh_cdr (s))
86     {
87       SCM car = gh_car (s);
88
89       Grob * se = unsmob_grob (car);
90       if (Note_column::has_interface (se))
91         clash_groups[Note_column::dir (se)].push (se);
92     }
93
94   
95   Direction d = UP;
96   do
97     {
98       Array<int> & shift (shifts[d]);
99       Link_array<Grob> & 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_grob_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       Grob *cu_l =clash_groups[UP][0];
161       Grob *cd_l =clash_groups[DOWN][0];
162
163
164       /*
165         TODO.
166        */
167       Grob * nu_l= Note_column::first_head (cu_l);
168       Grob * nd_l = Note_column::first_head (cd_l);
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         && Rhythmic_head::balltype_i (nu_l) == Rhythmic_head::balltype_i (nd_l);
176
177
178       if (!to_boolean (me->get_grob_property ("merge-differently-dotted")))
179         merge = merge && Rhythmic_head::dot_count (nu_l) == Rhythmic_head::dot_count (nd_l);
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 (Grob *me)
208 {
209   SCM tups = SCM_EOL;
210   
211   SCM s = me->get_grob_property ("elements");
212   for (; gh_pair_p (s); s = gh_cdr (s))
213     {
214       Grob * se = unsmob_grob (gh_car (s));
215
216       SCM force =  se->remove_grob_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 void
227 Collision::add_column (Grob*me,Grob* ncol_l)
228 {
229   ncol_l->add_offset_callback (Collision::force_shift_callback_proc, X_AXIS);
230   Axis_group_interface::add_element (me, ncol_l);
231   me->add_dependency (ncol_l);
232 }