]> git.donarmstrong.com Git - lilypond.git/blob - lily/collision.cc
ef3b75af4cedb76eb9d3c763b34a4c4420834203
[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--1999 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 "ly-symbols.hh"
14 #include "tuple.hh"
15
16 Collision::Collision()
17 {
18   set_axes (X_AXIS, Y_AXIS);
19 }
20
21 void
22 Collision::add_column (Note_column* ncol_l)
23 {
24   clash_l_arr_.push (ncol_l);
25   add_element (ncol_l);
26   add_dependency (ncol_l);
27 }
28
29 void
30 Collision::do_pre_processing()
31 {
32   Array<Shift_tup> autos (automatic_shift ());
33   Array<Shift_tup> hand (forced_shift ());
34   Link_array<Note_column> done;
35   
36   Real wid = paper_l ()->note_width ();
37   for (int i=0; i < hand.size (); i++)
38     {
39       hand[i].e1_->translate_axis (hand[i].e2_ *wid, X_AXIS);
40       done.push (hand[i].e1_);
41     }
42
43   for (int i=0; i < autos.size (); i++)
44     {
45       if (!done.find_l (autos[i].e1_))
46         autos[i].e1_->translate_axis (autos[i].e2_ * wid, X_AXIS);
47     }
48 }
49
50 /** This complicated routine moves note columns around horizontally to
51   ensure that notes don't clash.
52
53   This should be done better, probably.
54
55   TODO: forced hshift
56   
57   */
58 Array< Shift_tup >
59 Collision::automatic_shift ()
60 {
61   Drul_array<Link_array<Note_column> > clash_groups;
62   Drul_array<Array<int> > shifts;
63   Array<Shift_tup>  tups;
64
65   
66   for (int i=0; i < clash_l_arr_.size(); i++)
67     {
68       clash_groups[clash_l_arr_[i]->dir ()].push (clash_l_arr_[i]);
69     }
70
71   
72   Direction d = UP;
73   do
74     {
75       Array<int> & shift (shifts[d]);
76       Link_array<Note_column> & clashes (clash_groups[d]);
77
78       clashes.sort (Note_column::shift_compare);
79
80       for (int i=0; i < clashes.size (); i++)
81         {
82           SCM sh
83             = clashes[i]->remove_elt_property (horizontal_shift_scm_sym);
84
85           if (sh == SCM_BOOL_F)
86             shift.push (0);
87           else
88             shift.push (gh_scm2int (SCM_CDR (sh)));
89         }
90       
91       for (int i=1; i < shift.size (); i++)
92         {
93           if (shift[i-1] == shift[i])
94             {
95               warning (_ ("Too many clashing notecolumns. Ignoring them."));
96               return tups;
97             }
98         }
99     }
100   while ((flip (&d))!= UP);
101
102   Drul_array< Array < Slice > > extents;
103   Drul_array< Array < Real > > offsets;
104   d = UP;
105   do
106     {
107       for (int i=0; i < clash_groups[d].size (); i++)
108         {
109           Slice s(clash_groups[d][i]->head_positions_interval ());
110           s[LEFT] --;
111           s[RIGHT]++;
112           extents[d].push (s);
113           offsets[d].push (d * 0.5 * i);
114         }
115     }
116   while ((flip (&d))!= UP);
117   
118   do
119     {
120       for (int i=1; i < clash_groups[d].size (); i++)
121         {
122           Slice prev =extents[d][i-1];
123           prev.intersect (extents[d][i]);
124           if (prev.length ()> 0 ||
125               (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
126             for (int j = i; j <  clash_groups[d].size (); j++)
127               offsets[d][j] += d * 0.5;
128         }
129     }   
130   while ((flip (&d))!= UP);
131
132   /*
133     if the up and down version are close, and can not be merged, move
134     all of them again. */
135   if (extents[UP].size () && extents[DOWN].size ())
136     {
137       Note_column *cu_l =clash_groups[UP][0];
138       Note_column *cd_l =clash_groups[DOWN][0];
139       Note_head * nu_l= cu_l->head_l_arr_[0];
140       Note_head * nd_l = cd_l->head_l_arr_.top();
141       int downpos =     cd_l->head_positions_interval ()[BIGGER];
142       int uppos =       cu_l->head_positions_interval ()[SMALLER];      
143       
144       bool merge  =
145         downpos == uppos
146         && nu_l->balltype_i_ == nd_l->balltype_i_
147         && nu_l->dots_i_ == nd_l->dots_i_;
148
149       /*
150         notes are close, but can not be merged.  Shift
151        */
152       if (abs(uppos - downpos) < 2 && !merge)
153           do
154           {
155             for (int i=0; i < clash_groups[d].size (); i++)
156               {
157                 offsets[d][i] -= d * 0.5;
158               }
159           }
160           while ((flip (&d))!= UP);
161     }
162
163
164   do
165     {
166       for (int i=0; i < clash_groups[d].size (); i++)
167         tups.push (Shift_tup (clash_groups[d][i], offsets[d][i]));
168     }
169   while (flip (&d) != UP);
170   return tups;
171 }
172
173
174 Array <Shift_tup>
175 Collision::forced_shift ()
176 {
177   Array<Shift_tup> tups;
178   
179   for (int i=0; i < clash_l_arr_.size (); i++)
180     {
181       SCM force =  clash_l_arr_[i]->remove_elt_property (force_hshift_scm_sym);
182       if (force != SCM_BOOL_F)
183         {
184           force = SCM_CDR (force);
185           tups. push (Shift_tup (clash_l_arr_[i],
186                                                  gh_scm2double (force)));
187         }
188     }
189   return tups;
190 }
191
192
193 void
194 Collision::do_substitute_element_pointer (Score_element*o_l,Score_element*n_l)
195 {
196   if (o_l)
197     {
198       clash_l_arr_.substitute (dynamic_cast<Note_column *> (o_l),
199                                dynamic_cast <Note_column *> (n_l));
200
201     }
202 }