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