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