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