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