]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam.cc
patch::: 0.1.37.jcn2: biem 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 "abbreviation-beam.hh"
22 #include "misc.hh"
23 #include "debug.hh"
24 #include "atom.hh"
25 #include "molecule.hh"
26 #include "leastsquares.hh"
27 #include "stem.hh"
28 #include "paper-def.hh"
29 #include "lookup.hh"
30 #include "grouping.hh"
31 #include "stem-info.hh"
32 #include "main.hh"  // experimental features
33
34
35 IMPLEMENT_IS_TYPE_B1 (Beam, Spanner);
36
37 const int MINIMUM_STEMLEN = 5;
38
39 Beam::Beam ()
40 {
41   slope_f = 0;
42   left_y = 0.0;
43 }
44
45 void
46 Beam::add (Stem*s)
47 {
48   stems.push (s);
49   s->add_dependency (this);
50   s->beam_l_ = this;
51
52   if (!spanned_drul_[LEFT])
53     set_bounds (LEFT,s);
54   else
55     set_bounds (RIGHT,s);
56 }
57
58 Molecule*
59 Beam::brew_molecule_p () const
60 {
61   Molecule *mol_p = new Molecule;
62   Real inter_f = paper ()->internote_f ();
63   Real x0 = stems[0]->hpos_f ();
64   for (int j=0; j <stems.size (); j++)
65     {
66       Stem *i = stems[j];
67       Stem * prev = (j > 0)? stems[j-1] : 0;
68       Stem * next = (j < stems.size ()-1) ? stems[j+1] :0;
69
70       Molecule sb = stem_beams (i, next, prev);
71       Real  x = i->hpos_f ()-x0;
72       sb.translate (Offset (x, (x * slope_f  + left_y)* inter_f));
73       mol_p->add (sb);
74     }
75   mol_p->translate_axis (x0 - spanned_drul_[LEFT]->absolute_coordinate (X_AXIS), X_AXIS);
76   return mol_p;
77 }
78
79 Offset
80 Beam::center () const
81 {
82   Real w= (paper ()->note_width () + width ().length ())/2.0;
83   return Offset (w, (left_y + w* slope_f)*paper ()->internote_f ());
84 }
85
86 void
87 Beam::do_pre_processing ()
88 {
89   if (!dir_)
90     set_default_dir ();
91 }
92
93 void
94 Beam::do_print () const
95 {
96 #ifndef NPRINT
97   DOUT << "slope_f " <<slope_f << "left ypos " << left_y;
98   Spanner::do_print ();
99 #endif
100 }
101
102 void
103 Beam::do_post_processing ()
104 {
105   if (stems.size () < 2)
106     {
107       warning (_ ("Beam with less than 2 stems"));
108       transparent_b_ = true;
109       return ;
110     }
111   solve_slope ();
112   set_stemlens ();
113 }
114
115 void
116 Beam::do_substitute_dependent (Score_elem*o,Score_elem*n)
117 {
118   if (o->is_type_b (Stem::static_name ()))
119       stems.substitute ((Stem*)o->item (),  n? (Stem*) n->item ():0);
120 }
121
122 Interval
123 Beam::do_width () const
124 {
125   return Interval (stems[0]->hpos_f (),
126                    stems.top ()->hpos_f ());
127 }
128
129 void
130 Beam::set_default_dir ()
131 {
132   Drul_array<int> total;
133   total[UP]  = total[DOWN] = 0;
134   Drul_array<int> count; 
135   count[UP]  = count[DOWN] = 0;
136   Direction d = DOWN;
137   
138   for (int i=0; i <stems.size (); i++)
139     do {
140       Stem *s = stems[i];
141       int current = s->dir_ 
142         ? (1 + d * s->dir_)/2
143         : s->get_center_distance (Direction (-d));
144
145       if (current)
146         {
147           total[d] += current;
148           count[d] ++;
149         }
150
151     } while ((d *= -1) != DOWN);
152   
153    do {
154     if (!total[d])
155       count[d] = 1;
156   } while ((d *= -1) != DOWN);
157   
158   /* the following relation is equal to
159           up / up_count > down / down_count
160           */
161   dir_ = (total[UP] * count[DOWN] > total[DOWN] * count[UP]) ? UP : DOWN;
162
163   for (int i=0; i <stems.size (); i++)
164     {
165       Stem *sl = stems[i];
166       sl->dir_ = dir_;
167     }
168 }
169
170 /*
171   should use minimum energy formulation (cf linespacing)
172
173 */
174 void
175 Beam::solve_slope ()
176 {
177   Array<Stem_info> sinfo;
178   for (int j=0; j <stems.size (); j++)
179     {
180       Stem *i = stems[j];
181
182       i->set_default_extents ();
183       if (i->invisible_b ())
184         continue;
185
186       Stem_info info (i);
187       sinfo.push (info);
188     }
189   if (! sinfo.size ())
190     slope_f = left_y = 0;
191   else if (sinfo.size () == 1)
192     {
193       slope_f = 0;
194       left_y = sinfo[0].idealy_f_;
195     }
196   else
197     {
198
199       Real leftx = sinfo[0].x;
200       Least_squares l;
201       for (int i=0; i < sinfo.size (); i++)
202         {
203           sinfo[i].x -= leftx;
204           l.input.push (Offset (sinfo[i].x, sinfo[i].idealy_f_));
205         }
206
207       l.minimise (slope_f, left_y);
208     }
209
210   Real dy = 0.0;
211   for (int i=0; i < sinfo.size (); i++)
212     {
213       Real y = sinfo[i].x * slope_f + left_y;
214       Real my = sinfo[i].miny_f_;
215
216       if (my - y > dy)
217         dy = my -y;
218     }
219   left_y += dy;
220   left_y *= dir_;
221
222   slope_f *= dir_;
223
224   /*
225     This neat trick is by Werner Lemberg, damped = tanh (slope_f) corresponds
226     with some tables in [Wanske]
227     */
228   slope_f = 0.6 * tanh (slope_f);
229
230   quantise_yspan ();
231
232   // y-values traditionally use internote dimension: therefore slope = (y/in)/x
233   // but mf and beam-lookup use PT dimension for y (as used for x-values)
234   // ugh --- there goes our simplified but careful quantisation
235   Real sl = slope_f * paper ()->internote_f ();
236   paper ()->lookup_l ()->beam (sl, 20 PT);
237   slope_f = sl / paper ()->internote_f ();
238 }
239
240 void
241 Beam::quantise_yspan ()
242 {
243   /*
244     [Ross] (simplification of)
245     Try to set slope_f complying with y-span of:
246       - zero
247       - beam_thickness / 2 + staffline_thickness / 2
248       - beam_thickness + staffline_thickness
249     + n * interline
250     */
251   Real interline_f = paper ()->interline_f ();
252   Real internote_f = interline_f / 2;
253   Real staffline_thickness = paper ()->rule_thickness ();
254   Real beam_thickness = 0.48 * (interline_f - staffline_thickness);
255
256   const int QUANTS = 3;
257   Real qdy[QUANTS] = {
258     0,
259     beam_thickness / 2 + staffline_thickness / 2,
260     beam_thickness + staffline_thickness
261   };
262
263   Real xspan_f = stems.top ()->hpos_f () - stems[0]->hpos_f ();
264   // y-values traditionally use internote dimension: therefore slope = (y/in)/x
265   Real yspan_f = xspan_f * abs (slope_f * internote_f);
266   int yspan_i = (int)(yspan_f / interline_f);
267   Real q = (yspan_f / interline_f - yspan_i) * interline_f;
268   int i = 0;
269   for (; i < QUANTS - 1; i++)
270     if ((q >= qdy[i]) && (q <= qdy[i + 1]))
271       {
272         if (q - qdy[i] < qdy[i + 1] - q)
273           break;
274         else
275         { 
276           i++;
277           break;
278         }
279       }
280   q = qdy[i];
281
282   yspan_f = (Real)yspan_i * interline_f + q;
283   // y-values traditionally use internote dimension: therefore slope = (y/in)/x
284   slope_f = yspan_f / xspan_f / internote_f * sign (slope_f);
285 }
286
287 void
288 Beam::quantise_left_y (Beam::Pos pos, bool extend_b)
289 {
290   /*
291    quantising left y should suffice, as slope is quantised too
292    if extend then stems must not get shorter
293    */
294
295   Real interline_f = paper ()->interline_f ();
296   Real internote_f = interline_f / 2;
297   Real staffline_thickness = paper ()->rule_thickness ();
298   Real beam_thickness = 0.48 * (interline_f - staffline_thickness);
299
300   const int QUANTS = 6;
301   Real qy[QUANTS] = {
302     -staffline_thickness,
303     beam_thickness / 2,
304     beam_thickness + staffline_thickness / 2,
305     interline_f / 2 + beam_thickness / 2 + staffline_thickness / 2,
306     interline_f - staffline_thickness,
307     interline_f + beam_thickness / 2,
308   };
309   /* 
310    ugh, using i triggers gcc 2.7.2.1 internal compiler error (far down):
311    for (int i = 0; i < QUANTS; i++)
312    */
313   for (int ii = 0; ii < QUANTS; ii++)
314     qy[ii] -= beam_thickness / 2;
315   Pos qpos[QUANTS] = {
316     HANG,
317     STRADDLE,
318     SIT,
319     INTER,
320     HANG,
321     STRADDLE
322   };
323
324   // y-values traditionally use internote dimension
325   Real y = left_y * internote_f;
326   int y_i = (int)floor(y / interline_f);
327   y = (y / interline_f - y_i) * interline_f;
328
329   if (y < 0)
330     for (int ii = 0; ii < QUANTS; ii++)
331       qy[ii] -= interline_f;
332
333   int lower_i = 0;
334   int i = 0;
335   for (; i < QUANTS; i++)
336     {
337       if (qy[i] > y)
338         break;
339       // found if lower_i is allowed, and nearer (from below) y than new pos
340       if ((pos & qpos[lower_i]) && (y - qy[lower_i] < y - qy[i]))
341         break;
342       // if new pos is allowed or old pos isn't: assign new pos
343       if ((pos & qpos[i]) || !(pos & qpos[lower_i]))
344         lower_i = i;
345     }
346
347   int upper_i = QUANTS - 1;
348   for (i = QUANTS - 1; i >= 0; i--)
349     {
350       if (qy[i] < y)
351         break;
352       // found if upper_i is allowed, and nearer (from above) y than new pos
353       if ((pos & qpos[upper_i]) && (qy[upper_i] - y < qy[i] - y))
354         break;
355       // if new pos is allowed or old pos isn't: assign new pos
356       if ((pos & qpos[i]) || !(pos & qpos[upper_i]))
357         upper_i = i;
358     }
359
360   // y-values traditionally use internote dimension
361   Real upper_y = (qy[upper_i] + interline_f * y_i) / internote_f;
362   Real lower_y = (qy[lower_i] + interline_f * y_i) / internote_f;
363
364   if (extend_b)
365     left_y = (dir_ > 0 ? upper_y : lower_y);
366   else
367     left_y = (upper_y - left_y < y - lower_y ? upper_y : lower_y);
368 }
369
370 void
371 Beam::set_stemlens ()
372 {
373   Real x0 = stems[0]->hpos_f ();
374   Real dy = 0;
375
376   Real interline_f = paper ()->interline_f ();
377   Real internote_f = interline_f / 2;
378   Real staffline_thickness = paper ()->rule_thickness ();
379   Real beam_thickness = 0.48 * (interline_f - staffline_thickness);
380   Real xspan_f = stems.top ()->hpos_f () - stems[0]->hpos_f ();
381   /*
382    ugh, y values are in "internote" dimension
383    */
384   Real yspan_f = xspan_f * abs (slope_f * internote_f);
385   int yspan_i = (int)(yspan_f / interline_f);
386
387   Pos left_pos = NONE;
388
389   if (yspan_f < staffline_thickness / 2)
390     left_pos = (Pos)(STRADDLE | SIT | HANG);
391   else
392     left_pos = (Pos) (sign (slope_f) > 0 ? STRADDLE | HANG 
393       : SIT | STRADDLE);
394
395   /* 
396    ugh, slope currently mangled by availability mf chars...
397    be more generous regarding beam position between stafflines
398    */
399   Real q = (yspan_f / interline_f - yspan_i) * interline_f;
400   if (q < interline_f / 3 - beam_thickness / 2)
401     left_pos = (Pos) (left_pos | INTER);
402
403   if (stems[0]->beams_right_i_ > 1)
404     left_pos = (Pos)(left_pos & (STRADDLE | INTER));
405
406   // ugh, rounding problems!
407   const Real EPSILON = interline_f / 10;
408   do
409     { 
410       left_y += dy * dir_;
411       quantise_left_y (left_pos, dy);
412       dy = 0;
413       for (int j=0; j < stems.size (); j++)
414         {
415           Stem *s = stems[j];
416
417           Real x = s->hpos_f () - x0;
418           s->set_stemend (left_y + slope_f * x);
419           Real y = s->stem_length_f ();
420           if (y < MINIMUM_STEMLEN)
421             dy = dy >? (MINIMUM_STEMLEN - y);
422         }
423     } while (abs (dy) > EPSILON)
424 }
425
426 void
427 Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
428 {
429   def.OK ();
430   cur.OK ();
431   assert (cur.children.size () == stems.size ());
432
433   cur.split (def);
434
435   Array<int> b;
436   {
437     Array<int> flags;
438     for (int j=0; j <stems.size (); j++)
439       {
440         Stem *s = stems[j];
441
442         int f = s->flag_i_ - 2;
443         assert (f>0);
444         flags.push (f);
445       }
446     int fi =0;
447     b= cur.generate_beams (flags, fi);
448     b.insert (0,0);
449     b.push (0);
450     assert (stems.size () == b.size ()/2);
451   }
452
453   for (int j=0, i=0; i < b.size () && j <stems.size (); i+= 2, j++)
454     {
455       Stem *s = stems[j];
456       s->beams_left_i_ = b[i];
457       s->beams_right_i_ = b[i+1];
458     }
459 }
460
461 /*
462   beams to go with one stem.
463   */
464 Molecule
465 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
466 {
467   assert (!next || next->hpos_f () > here->hpos_f ());
468   assert (!prev || prev->hpos_f () < here->hpos_f ());
469   //    Real dy=paper ()->internote_f ()*2;
470   Real dy = paper ()->interbeam_f ();
471   Real stemdx = paper ()->rule_thickness ();
472   Real sl = slope_f*paper ()->internote_f ();
473   paper ()->lookup_l ()->beam (sl, 20 PT);
474
475   Molecule leftbeams;
476   Molecule rightbeams;
477
478   /* half beams extending to the left. */
479   if (prev)
480     {
481       int lhalfs= lhalfs = here->beams_left_i_ - prev->beams_right_i_ ;
482       int lwholebeams= here->beams_left_i_ <? prev->beams_right_i_ ;
483       Real w = (here->hpos_f () - prev->hpos_f ())/4;
484       Atom a;
485       if (lhalfs)               // generates warnings if not
486         a =  paper ()->lookup_l ()->beam (sl, w);
487       a.translate (Offset (-w, -w * sl));
488       for (int j = 0; j  < lhalfs; j++)
489         {
490           Atom b (a);
491           b.translate_axis (-dir_ * dy * (lwholebeams+j), Y_AXIS);
492           leftbeams.add (b);
493         }
494     }
495
496   if (next)
497     {
498       int rhalfs = here->beams_right_i_ - next->beams_left_i_;
499       int rwholebeams = here->beams_right_i_ <? next->beams_left_i_;
500
501       Real w = next->hpos_f () - here->hpos_f ();
502       Atom a = paper ()->lookup_l ()->beam (sl, w + stemdx);
503
504       int j = 0;
505       Real gap_f = 0;
506       if (here->beam_gap_i_)
507         {
508           int nogap = rwholebeams - here->beam_gap_i_;
509           for (; j  < nogap; j++)
510             {
511               Atom b (a);
512               b.translate_axis (-dir_ * dy * j, Y_AXIS);
513               rightbeams.add (b);
514             }
515           // TODO: notehead widths differ for different types
516           gap_f = paper ()->note_width () / 2;
517           w -= 2 * gap_f;
518           a = paper ()->lookup_l ()->beam (sl, w + stemdx);
519         }
520
521       for (; j  < rwholebeams; j++)
522         {
523           Atom b (a);
524           b.translate (Offset (gap_f, -dir_ * dy * j));
525           rightbeams.add (b);
526         }
527
528       w /= 4;
529       if (rhalfs)
530         a = paper ()->lookup_l ()->beam (sl, w);
531
532       for (; j  < rwholebeams + rhalfs; j++)
533         {
534           Atom b (a);
535           b.translate_axis (-dir_ * dy * j, Y_AXIS);
536           rightbeams.add (b);
537         }
538
539     }
540   leftbeams.add (rightbeams);
541   return leftbeams;
542 }