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