]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam.cc
patch::: 0.1.12.jcn2: kliener pats
[lilypond.git] / lily / beam.cc
1 /*
2   beam.cc -- implement Beam
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7
8   TODO
9
10   Less hairy code.  knee: ([\stem 1; c8 \stem -1; c8]
11   
12 */
13
14 #include <math.h>
15
16 #include "p-col.hh"
17 #include "varray.hh"
18 #include "proto.hh"
19 #include "dimen.hh"
20 #include "beam.hh"
21 #include "misc.hh"
22 #include "debug.hh"
23 #include "symbol.hh"
24 #include "molecule.hh"
25 #include "leastsquares.hh"
26 #include "stem.hh"
27 #include "paper-def.hh"
28 #include "lookup.hh"
29 #include "grouping.hh"
30 #include "stem-info.hh"
31
32
33 IMPLEMENT_IS_TYPE_B1(Beam, Spanner);
34
35 Beam::Beam()
36 {
37   slope = 0;
38   left_pos = 0.0;
39 }
40
41 void
42 Beam::add (Stem*s)
43 {
44   stems.push (s);
45   s->add_dependency (this);
46   s->beam_l_ = this;
47
48   if (!spanned_drul_[LEFT])
49     set_bounds(LEFT,s);
50   set_bounds(RIGHT,s);
51 }
52
53 Molecule*
54 Beam::brew_molecule_p() const 
55 {
56   Molecule *mol_p = new Molecule;
57   // huh? inter-what
58   //    Real inter_f = paper()->interbeam_f ();
59   Real inter_f = paper()->internote_f ();
60   Real x0 = stems[0]->hpos_f();
61   for (int j=0; j <stems.size(); j++) 
62     {
63       Stem *i = stems[j];
64       Stem * prev = (j > 0)? stems[j-1] : 0;
65       Stem * next = (j < stems.size()-1) ? stems[j+1] :0;
66
67       Molecule sb = stem_beams (i, next, prev);
68       Real  x = i->hpos_f()-x0;
69       sb.translate (Offset (x, (x * slope  + left_pos)* inter_f));
70       mol_p->add (sb);
71     }
72   mol_p->translate (x0 - spanned_drul_[LEFT]->absolute_coordinate(X_AXIS), X_AXIS);
73   return mol_p;
74 }
75
76 Offset
77 Beam::center() const
78 {
79   Real w=(paper()->note_width () + width ().length ())/2.0;
80   return Offset (w, (left_pos + w* slope)*paper()->internote_f ());
81 }
82
83 void
84 Beam::do_pre_processing()
85 {
86   if (!dir_)
87     set_default_dir();
88 }
89
90 void
91 Beam::do_print() const
92 {
93 #ifndef NPRINT
94   DOUT << "slope " <<slope << "left ypos " << left_pos;
95   Spanner::do_print();
96 #endif
97 }
98
99 void
100 Beam::do_post_processing()
101 {
102   if (stems.size() < 2) 
103     {
104       warning ("Beam with less than 2 stems");
105       transparent_b_ = true;
106       return ;
107     }
108   solve_slope();    
109   set_stemlens();
110 }
111
112 void
113 Beam::do_substitute_dependent (Score_elem*o,Score_elem*n)
114 {
115   if (o->is_type_b (Stem::static_name())) 
116       stems.substitute ((Stem*)o->item(),  n?(Stem*) n->item ():0);
117 }
118
119 Interval
120 Beam::do_width() const
121 {
122   return Interval (stems[0]->hpos_f(),
123                    stems.top()->hpos_f ());
124 }
125
126 void
127 Beam::set_default_dir()
128 {
129   int up = 0, down = 0;
130   int up_count = 0, down_count = 0;
131
132   for (int i=0; i <stems.size(); i++) 
133     {
134       Stem *sl = stems[i];
135       int cur_down = sl->get_center_distance_from_top();
136       int cur_up = sl->get_center_distance_from_bottom();
137       if (cur_down) 
138         {
139           down += cur_down;
140           down_count++;
141         }
142       if (cur_up) 
143         {
144           up += cur_up;
145           up_count++;
146         }
147     }
148   if (!down)
149     down_count = 1;
150   if (!up)
151     up_count = 1;
152
153   // the following relation is equal to
154   //        up / up_count > down / down_count
155   dir_ = (up * down_count > down * up_count) ? UP : DOWN;
156
157   for (int i=0; i <stems.size(); i++) 
158     {
159       Stem *sl = stems[i];
160       sl->dir_ = dir_;
161     }
162 }
163
164 /*
165   should use minimum energy formulation (cf linespacing)
166
167   [todo]
168   the y of the (start) of the beam should be quantisized,
169   so that no stafflines appear just in between two beam-flags
170
171 */
172 void
173 Beam::solve_slope()
174 {
175   Array<Stem_info> sinfo;
176   for (int j=0; j <stems.size(); j++) 
177     {
178       Stem *i = stems[j];
179
180       i->set_default_extents();
181       if (i->invisible_b())
182         continue;
183         
184       Stem_info info (i);
185       sinfo.push (info);
186     }
187   if (! sinfo.size())
188     slope = left_pos = 0;
189   else if (sinfo.size() == 1) 
190     {
191       slope = 0;
192       left_pos = sinfo[0].idealy_f_;
193     }
194   else 
195     {
196         
197       Real leftx = sinfo[0].x;
198       Least_squares l;
199       for (int i=0; i < sinfo.size(); i++) 
200         {
201           sinfo[i].x -= leftx;
202           l.input.push (Offset (sinfo[i].x, sinfo[i].idealy_f_));
203         }
204
205       l.minimise (slope, left_pos);
206     }
207   
208   Real dy = 0.0;
209   for (int i=0; i < sinfo.size(); i++) 
210     {
211       Real y = sinfo[i].x * slope + left_pos;
212       Real my = sinfo[i].miny_f_;
213
214       if (my - y > dy)
215         dy = my -y;     
216     }
217   left_pos += dy;
218   left_pos *= dir_;    
219
220   slope *= dir_;
221
222   /*
223     This neat trick is by Werner Lemberg, damped = tanh (slope) corresponds
224     with some tables in [Wanske]
225     */
226   slope = 0.6 * tanh (slope);  
227
228   // ugh
229   Real sl = slope*paper()->internote_f ();
230   paper()->lookup_l ()->beam (sl, 20 PT);
231   slope = sl /paper()->internote_f ();
232 }
233
234 void
235 Beam::set_stemlens()
236 {
237   Real x0 = stems[0]->hpos_f();    
238   for (int j=0; j <stems.size(); j++) 
239     {
240       Stem *s = stems[j];
241
242       Real x =  s->hpos_f()-x0;
243       s->set_stemend (left_pos + slope * x);    
244     }
245 }
246
247 void
248 Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
249 {
250   def.OK();
251   cur.OK();
252   assert (cur.children.size() == stems.size ());
253   
254   cur.split (def);
255
256   Array<int> b;
257   {
258     Array<int> flags;
259     for (int j=0; j <stems.size(); j++) 
260       {
261         Stem *s = stems[j];
262
263         int f = intlog2(abs (s->flag_i_))-2;
264         assert (f>0);
265         flags.push (f);
266       }
267     int fi =0;
268     b= cur.generate_beams (flags, fi);
269     b.insert (0,0);
270     b.push (0);
271     assert (stems.size() == b.size ()/2);
272   }
273
274   for (int j=0, i=0; i < b.size() && j <stems.size (); i+= 2, j++) 
275     {
276       Stem *s = stems[j];
277       s->beams_left_i_ = b[i];
278       s->beams_right_i_ = b[i+1];
279     }
280 }
281
282 /*
283   beams to go with one stem.
284   */
285 Molecule
286 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
287 {
288   assert (!next || next->hpos_f() > here->hpos_f ());
289   assert (!prev || prev->hpos_f() < here->hpos_f ());
290   //    Real dy=paper()->internote_f ()*2;
291   Real dy = paper()->interbeam_f ();
292   Real stemdx = paper()->rule_thickness ();
293   Real sl = slope*paper()->internote_f ();
294   paper()->lookup_l ()->beam (sl, 20 PT);
295
296   Molecule leftbeams;
297   Molecule rightbeams;
298
299   /* half beams extending to the left. */
300   if (prev) 
301     {
302       int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
303       int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
304       Real w = (here->hpos_f() - prev->hpos_f ())/4;
305       Symbol dummy;
306       Atom a (dummy);
307       if (lhalfs)               // generates warnings if not
308         a =  paper()->lookup_l ()->beam (sl, w);
309       a.translate (Offset (-w, -w * sl));
310       for (int j = 0; j  < lhalfs; j++) 
311         {
312           Atom b (a);
313           b.translate (-dir_ * dy * (lwholebeams+j), Y_AXIS);
314           leftbeams.add (b);
315         }
316     }
317         
318   if (next)
319     {
320       int rhalfs = here->beams_right_i_ - next->beams_left_i_;
321       int rwholebeams = here->beams_right_i_ <? next->beams_left_i_; 
322
323       Real w = next->hpos_f() - here->hpos_f ();
324       Atom a = paper()->lookup_l ()->beam (sl, w + stemdx);
325         
326       int j = 0;
327       for (; j  < rwholebeams; j++) 
328         {
329           Atom b (a);
330           b.translate (-dir_ * dy * j, Y_AXIS);
331           rightbeams.add (b); 
332         }
333
334       w /= 4;
335       if (rhalfs)
336         a = paper()->lookup_l ()->beam (sl, w);
337         
338       for (; j  < rwholebeams + rhalfs; j++) 
339         {
340           Atom b (a);
341           b.translate (-dir_ * dy * j, Y_AXIS);
342           rightbeams.add (b); 
343         }
344         
345     }
346   leftbeams.add (rightbeams);
347   return leftbeams;
348 }