]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam.cc
release: 1.3.21
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8
9 */
10
11 /*
12   [TODO]
13     * less hairy code
14     * move paper vars to scm
15
16 */
17
18
19 #include <math.h>               // tanh.
20
21 #include "directional-element-interface.hh"
22 #include "beaming.hh"
23 #include "dimensions.hh"
24 #include "beam.hh"
25 #include "misc.hh"
26 #include "debug.hh"
27 #include "least-squares.hh"
28 #include "stem.hh"
29 #include "paper-def.hh"
30 #include "lookup.hh"
31 #include "group-interface.hh"
32 #include "staff-symbol-referencer.hh"
33 #include "cross-staff.hh"
34 #include "lily-guile.icc"
35
36 Beam::Beam ()
37 {
38   Group_interface g (this, "stems");
39   g.set_interface ();
40 }
41
42 void
43 Beam::add_stem (Stem*s)
44 {
45   Group_interface gi (this, "stems");
46   gi.add_element (s);
47   
48   s->add_dependency (this);
49
50   assert (!s->beam_l ());
51   s->set_elt_property ("beam", self_scm_);
52
53   if (!spanned_drul_[LEFT])
54     set_bounds (LEFT,s);
55   else
56     set_bounds (RIGHT,s);
57 }
58
59 int
60 Beam::get_multiplicity () const
61 {
62   int m = 0;
63   for (SCM s = get_elt_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
64     {
65       Score_element * sc = unsmob_element (gh_car (s));
66
67       if (Stem * st = dynamic_cast<Stem*> (sc))
68         m = m >? st->beam_count (LEFT) >? st->beam_count (RIGHT);
69     }
70   return m;
71 }
72
73 /*
74   After pre-processing all directions should be set.
75   Several post-processing routines (stem, slur, script) need stem/beam
76   direction.
77   Currenly, this means that beam has set all stem's directions.
78   [Alternatively, stems could set its own directions, according to
79    their beam, during 'final-pre-processing'.]
80  */
81 void
82 Beam::do_pre_processing ()
83 {
84   // Why?
85   if (visible_stem_count () < 2)
86     {
87       warning (_ ("beam has less than two stems"));
88       set_elt_property ("transparent", SCM_BOOL_T);
89     }
90
91   if (!directional_element (this).get ())
92     directional_element (this).set (get_default_dir ());
93
94   auto_knees ();
95   set_stem_directions ();
96
97   set_stem_shorten (); 
98 }
99
100 /*
101  FIXME
102  */
103 Direction
104 Beam::get_default_dir () const
105 {
106   Drul_array<int> total;
107   total[UP]  = total[DOWN] = 0;
108   Drul_array<int> count; 
109   count[UP]  = count[DOWN] = 0;
110   Direction d = DOWN;
111
112   for (int i=0; i <stem_count (); i++)
113     do { // HUH -- waar slaat dit op?
114       Stem *s = stem (i);
115       Direction sd = directional_element (s).get ();
116       int current = sd  ? (1 + d * sd)/2
117         : s->get_center_distance ((Direction)-d);
118
119       if (current)
120         {
121           total[d] += current;
122           count[d] ++;
123         }
124
125     } while (flip(&d) != DOWN);
126   
127
128   SCM s = scm_eval (gh_list (ly_symbol2scm ("beam-dir-algorithm"),
129                              ly_quote_scm (gh_cons (gh_int2scm (count[UP]),
130                                                     gh_int2scm (count[DOWN]))),
131                              ly_quote_scm (gh_cons (gh_int2scm (total[UP]),
132                                                     gh_int2scm (total[DOWN]))),
133                              SCM_UNDEFINED));
134   if (gh_number_p (s) && gh_scm2int (s))
135     return to_dir (s);
136   
137   /*
138     If dir is not determined: get from paper
139   */
140   return (Direction)(int)
141     paper_l ()->get_var ("stem_default_neutral_direction");
142 }
143
144
145 /*
146   Set all stems with non-forced direction to beam direction.
147   Urg: non-forced should become `without/with unforced' direction,
148        once stem gets cleaned-up.
149  */
150 void
151 Beam::set_stem_directions ()
152 {
153   Direction d = directional_element (this).get ();
154   for (int i=0; i <stem_count (); i++)
155     {
156       Stem *s = stem (i);
157       SCM force = s->remove_elt_property ("dir-forced");
158       if (!gh_boolean_p (force) || !gh_scm2bool (force))
159         directional_element (s).set (d);
160     }
161
162
163 void
164 Beam::auto_knees ()
165 {
166   if (!auto_knee ("auto-interstaff-knee-gap", true))
167     auto_knee ("auto-knee-gap", false);
168 }
169
170 /*
171   Simplistic auto-knees; only consider vertical gap between two
172   adjacent chords.
173
174   `Forced' stem directions are ignored.  If you don't want auto-knees,
175   don't set, or unset autoKneeGap/autoInterstaffKneeGap.
176  */
177 bool
178 Beam::auto_knee (String gap_str, bool interstaff_b)
179 {
180   bool knee_b = false;
181   int knee_y = 0;
182   SCM gap = get_elt_property (gap_str);
183   Direction d = directional_element (this).get ();
184   
185   if (gh_number_p (gap))
186     {
187       int auto_gap_i = gh_scm2int (gap);
188       for (int i=1; i < stem_count (); i++)
189         {
190           bool is_b = (bool)(calc_interstaff_dist (stem (i), this) 
191             - calc_interstaff_dist (stem (i-1), this));
192           int l_y = (int)(stem (i-1)->head_positions()[d])
193             + (int)calc_interstaff_dist (stem (i-1), this);
194           int r_y = (int)(stem (i)->head_positions()[d])
195             + (int)calc_interstaff_dist (stem (i), this);
196           int gap_i = r_y - l_y;
197
198           if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
199             {
200               knee_y = (r_y + l_y) / 2;
201               knee_b = true;
202               break;
203             }
204         }
205     }
206   if (knee_b)
207     {
208       for (int i=0; i < stem_count (); i++)
209         {
210           int y = (int)(stem (i)->head_positions()[d])
211             + (int)calc_interstaff_dist (stem (i), this);
212           directional_element (stem (i)).set (y < knee_y ? UP : DOWN);
213           stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
214         }
215     }
216   return knee_b;
217 }
218
219 /*
220  Set stem's shorten property if unset.
221  TODO:
222     take some y-position (chord/beam/nearest?) into account
223     scmify forced-fraction
224  */
225 void
226 Beam::set_stem_shorten ()
227 {
228   if (!visible_stem_count ())
229     return;
230
231   Real forced_fraction = forced_stem_count () / visible_stem_count ();
232   if (forced_fraction < 0.5)
233     return;
234
235   int multiplicity = get_multiplicity ();
236   // grace stems?
237   SCM shorten = ly_eval_str ("beamed-stem-shorten");
238
239   Array<Real> a;
240   scm_to_array (shorten, &a);
241   if (!a.size ())
242     return;
243
244   Staff_symbol_referencer_interface st (this);
245   Real staff_space = st.staff_space ();
246   Real shorten_f = a[multiplicity <? (a.size () - 1)] * staff_space;
247
248   /* cute, but who invented this -- how to customise ? */
249   if (forced_fraction < 1)
250     shorten_f /= 2;
251
252   for (int i=0; i < stem_count (); i++)
253     {
254       Stem* s = stem (i);
255       if (s->invisible_b ())
256         continue;
257       if (gh_number_p (s->get_elt_property ("shorten")))
258         s->set_elt_property ("shorten", gh_double2scm (shorten_f));
259     }
260 }
261
262 /*
263   Set elt properties height and y-position if not set.
264   Adjust stem lengths to reach beam.
265  */
266 void
267 Beam::do_post_processing ()
268 {
269   /* first, calculate y, dy */
270   Real y, dy;
271   calc_position_and_height (&y, &dy);
272   if (suspect_slope_b (y, dy))
273     dy = 0;
274
275   Real damped_dy = calc_slope_damping_f (dy);
276   Real quantised_dy = quantise_dy_f (damped_dy);
277
278   y += (dy - quantised_dy) / 2;
279   dy = quantised_dy;
280   
281   /*
282     until here, we used only stem_info, which acts as if dir=up
283    */
284   y *= directional_element (this).get ();
285   dy *= directional_element (this).get ();
286
287   Staff_symbol_referencer_interface st (this);
288   Real half_space = st.staff_space () / 2;
289
290   /* check for user-override of dy */
291   SCM s = remove_elt_property ("height-hs");
292   if (gh_number_p (s))
293     {
294       dy = gh_scm2double (s) * half_space;
295     }
296   set_elt_property ("height", gh_double2scm (dy));
297
298   /* check for user-override of y */
299   s = remove_elt_property ("y-position-hs");
300   if (gh_number_p (s))
301     {
302       y = gh_scm2double (s) * half_space;
303     }
304   else
305     { 
306       /* we can modify y, so we should quantise y */
307       Real y_shift = check_stem_length_f (y, dy);
308       y += y_shift;
309       y = quantise_y_f (y, dy, 0);
310       set_stem_length (y, dy);
311       y_shift = check_stem_length_f (y, dy);
312
313       if (y_shift > half_space / 4)
314         {
315           y += y_shift;
316
317           /*
318             for significantly lengthened or shortened stems,
319             request quanting the other way.
320           */
321           int quant_dir = 0;
322           if (abs (y_shift) > half_space / 2)
323             quant_dir = sign (y_shift) * directional_element (this).get ();
324           y = quantise_y_f (y, dy, quant_dir);
325         }
326     }
327   // UGH. Y is not in staff position unit?
328   // Ik dacht datwe daar juist van weg wilden?
329   set_stem_length (y, dy);
330   set_elt_property ("y-position", gh_double2scm (y)); 
331 }
332
333 /*
334   See Documentation/tex/fonts.doc
335  */
336 void
337 Beam::calc_position_and_height (Real* y, Real* dy) const
338 {
339   *y = *dy = 0;
340   if (visible_stem_count () <= 1)
341     return;
342
343   Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
344   if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
345     {
346       *dy = 0;
347       *y = first_ideal;
348       return;
349     }
350
351   Array<Offset> ideals;
352   Real x0 = first_visible_stem ()->hpos_f ();
353   for (int i=0; i < stem_count (); i++)
354     {
355       Stem* s = stem (i);
356       if (s->invisible_b ())
357         continue;
358       ideals.push (Offset (s->hpos_f () - x0, 
359                            s->calc_stem_info ().idealy_f_));
360     }
361   Real dydx;
362   minimise_least_squares (&dydx, y, ideals); // duh, takes references
363
364   Real dx = last_visible_stem ()->hpos_f () - x0;
365   *dy = dydx * dx;
366 }
367
368 bool
369 Beam::suspect_slope_b (Real y, Real dy) const
370 {
371   /*
372     steep slope running against lengthened stem is suspect
373   */
374   Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
375   Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
376   Real lengthened = paper_l ()->get_var ("beam_lengthened");
377   Real steep = paper_l ()->get_var ("beam_steep_slope");
378
379   Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
380   Real dydx = dy && dx ? dy/dx : 0;
381
382   if (((y - first_ideal > lengthened) && (dydx > steep))
383       || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
384     {
385       return true;
386     }
387   return false;
388 }
389
390 /*
391   This neat trick is by Werner Lemberg,
392   damped = tanh (slope)
393   corresponds with some tables in [Wanske]
394 */
395 Real
396 Beam::calc_slope_damping_f (Real dy) const
397 {
398   SCM damp = get_elt_property ("damping"); // remove?
399   int damping = 1;              // ugh.
400   if (gh_number_p (damp))
401     damping = gh_scm2int (damp);
402
403   if (damping)
404     {
405       Real dx = last_visible_stem ()->hpos_f ()
406         - first_visible_stem ()->hpos_f ();
407       Real dydx = dy && dx ? dy/dx : 0;
408       dydx = 0.6 * tanh (dydx) / damping;
409       return dydx * dx;
410     }
411   return dy;
412 }
413
414 Real
415 Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
416 {
417   Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
418   int beam_multiplicity = get_multiplicity ();
419   int stem_multiplicity = (s->flag_i () - 2) >? 0;
420
421   Real interbeam_f = paper_l ()->interbeam_f (beam_multiplicity);
422   Real x0 = first_visible_stem ()->hpos_f ();
423   Real dx = last_visible_stem ()->hpos_f () - x0;
424   Real stem_y = (dy && dx ? (s->hpos_f () - x0) / dx * dy : 0) + y;
425
426   /* knee */
427    Direction dir  = directional_element(this).get ();
428    Direction sdir = directional_element (s).get ();
429    
430     /* knee */
431    if (dir!= sdir)
432       {
433        stem_y -= dir 
434         * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
435
436       Staff_symbol_referencer_interface me (s);
437       Staff_symbol_referencer_interface last (last_visible_stem ());
438       
439       // huh, why not for first visible?
440       if (//(s != first_visible_stem ()) &&
441           me.staff_symbol_l () != last.staff_symbol_l ())
442         stem_y += directional_element (this).get ()
443           * (beam_multiplicity - stem_multiplicity) * interbeam_f;
444     }
445   return stem_y;
446 }
447
448 Real
449 Beam::check_stem_length_f (Real y, Real dy) const
450 {
451   Real shorten = 0;
452   Real lengthen = 0;
453   Direction dir = directional_element (this).get ();
454   
455   for (int i=0; i < stem_count (); i++)
456     {
457       Stem* s = stem (i);
458       if (s->invisible_b ())
459         continue;
460
461       Real stem_y = calc_stem_y_f (s, y, dy);
462         
463       stem_y *= dir;
464       Stem_info info = s->calc_stem_info ();
465
466       // if (0 > info.maxy_f_ - stem_y)
467       shorten = shorten <? info.maxy_f_ - stem_y;
468       // if (0 < info.miny_f_ - stem_y)
469       lengthen = lengthen >? info.miny_f_ - stem_y; 
470     }
471
472   if (lengthen && shorten)
473     warning (_ ("weird beam vertical offset"));
474
475   /* when all stems are too short, normal stems win */
476   return dir * ((shorten) ?  shorten : lengthen);
477 }
478
479 /*
480   Hmm.  At this time, beam position and slope are determined.  Maybe,
481   stem directions and length should set to relative to the chord's
482   position of the beam.  */
483 void
484 Beam::set_stem_length (Real y, Real dy)
485 {
486   Staff_symbol_referencer_interface st (this);
487   Real half_space = st.staff_space ()/2;
488   for (int i=0; i < stem_count (); i++)
489     {
490       Stem* s = stem (i);
491       if (s->invisible_b ())
492         continue;
493
494       Real stem_y = calc_stem_y_f (s, y, dy);
495
496       /* caution: stem measures in staff-positions */
497       s->set_stemend ((stem_y + calc_interstaff_dist (s, this)) / half_space);
498     }
499 }
500
501 /*
502   [Ross] (simplification of)
503   Set dy complying with:
504     - zero
505     - thick / 2 + staffline_f / 2
506     - thick + staffline_f
507   + n * staff_space
508 */
509 Real
510 Beam::quantise_dy_f (Real dy) const
511 {
512   SCM quants = ly_eval_str ("beam-height-quants");
513
514   Array<Real> a;
515   scm_to_array (quants, &a);
516   if (a.size () <= 1)
517     return dy;
518
519   Staff_symbol_referencer_interface st (this);
520   Real staff_space = st.staff_space ();
521   
522   Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
523   Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
524     ? iv[SMALLER]
525     : iv[BIGGER];
526   
527   return q * sign (dy);
528 }
529
530 /*
531   Prevent interference from stafflines and beams.
532   See Documentation/tex/fonts.doc
533
534   We only need to quantise the (left) y-position of the beam,
535   since dy is quantised too.
536   if extend_b then stems must *not* get shorter
537  */
538 Real
539 Beam::quantise_y_f (Real y, Real dy, int quant_dir)
540 {
541   int multiplicity = get_multiplicity ();
542   Staff_symbol_referencer_interface st (this);
543   Real staff_space = st.staff_space ();
544   SCM quants = scm_eval (gh_list (
545                                   ly_symbol2scm ("beam-vertical-position-quants"),
546                                   gh_int2scm (multiplicity),
547                                   gh_double2scm (dy/staff_space),
548                                   SCM_UNDEFINED));
549   Array<Real> a;
550   scm_to_array (quants, &a);
551   if (a.size () <= 1)
552     return y;
553
554   Real up_y = directional_element (this).get () * y;
555   Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
556
557   Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y 
558     ? iv[SMALLER] : iv[BIGGER];
559   if (quant_dir)
560     q = iv[(Direction)quant_dir];
561
562   return q * directional_element (this).get ();
563 }
564
565 void
566 Beam::set_beaming (Beaming_info_list *beaming)
567 {
568   Direction d = LEFT;
569   for (int i=0; i  < stem_count (); i++)
570     {
571       do
572         {
573           if (stem (i)->beam_count (d) == 0)
574             stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
575         }
576       while (flip (&d) != LEFT);
577     }
578 }
579
580
581
582 /*
583   beams to go with one stem.
584
585   BURP
586   clean  me up.
587   */
588 Molecule
589 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
590 {
591   if ((next && !(next->hpos_f () > here->hpos_f ())) ||
592       (prev && !(prev->hpos_f () < here->hpos_f ())))
593       programming_error ("Beams are not left-to-right");
594
595   Real staffline_f = paper_l ()->get_var ("stafflinethickness");
596   int   multiplicity = get_multiplicity ();
597
598
599   Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
600   Real thick = gh_scm2double (get_elt_property ("beam-thickness"));;
601
602   Real bdy = interbeam_f;
603   Real stemdx = staffline_f;
604
605   Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
606   Real dy = get_real ("height");
607   Real dydx = dy && dx ? dy/dx : 0;
608
609   Molecule leftbeams;
610   Molecule rightbeams;
611
612   // UGH
613   Real nw_f;
614   if (!here->first_head ())
615     nw_f = 0;
616   else if (here->type_i ()== 1)
617     nw_f = paper_l ()->get_var ("wholewidth");
618   else if (here->type_i () == 2)
619     nw_f = paper_l ()->get_var ("notewidth") * 0.8;
620   else
621     nw_f = paper_l ()->get_var ("quartwidth");
622
623
624   Direction dir = directional_element (this).get ();
625   
626   /* half beams extending to the left. */
627   if (prev)
628     {
629       int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
630       int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
631       /*
632        Half beam should be one note-width, 
633        but let's make sure two half-beams never touch
634        */
635       Real w = here->hpos_f () - prev->hpos_f ();
636       w = w/2 <? nw_f;
637       Molecule a;
638       if (lhalfs)               // generates warnings if not
639         a =  lookup_l ()->beam (dydx, w, thick);
640       a.translate (Offset (-w, -w * dydx));
641       for (int j = 0; j  < lhalfs; j++)
642         {
643           Molecule b (a);
644           b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
645           leftbeams.add_molecule (b);
646         }
647     }
648
649   if (next)
650     {
651       int rhalfs  = here->beam_count (RIGHT) - next->beam_count (LEFT);
652       int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
653
654       Real w = next->hpos_f () - here->hpos_f ();
655       Molecule a = lookup_l ()->beam (dydx, w + stemdx, thick);
656       a.translate_axis( - stemdx/2, X_AXIS);
657       int j = 0;
658       Real gap_f = 0;
659
660       SCM gap = get_elt_property ("beam-gap");
661       if (gh_number_p (gap))
662         {
663           int gap_i = gh_scm2int ( (gap));
664           int nogap = rwholebeams - gap_i;
665           
666           for (; j  < nogap; j++)
667             {
668               Molecule b (a);
669               b.translate_axis (-dir  * bdy * j, Y_AXIS);
670               rightbeams.add_molecule (b);
671             }
672           // TODO: notehead widths differ for different types
673           gap_f = nw_f / 2;
674           w -= 2 * gap_f;
675           a = lookup_l ()->beam (dydx, w + stemdx, thick);
676         }
677
678       for (; j  < rwholebeams; j++)
679         {
680           Molecule b (a);
681           b.translate (Offset (here->invisible_b () ? 0 : gap_f, -dir * bdy * j));
682           rightbeams.add_molecule (b);
683         }
684
685       w = w/2 <? nw_f;
686       if (rhalfs)
687         a = lookup_l ()->beam (dydx, w, thick);
688
689       for (; j  < rwholebeams + rhalfs; j++)
690         {
691           Molecule b (a);
692           b.translate_axis (- dir * bdy * j, Y_AXIS);
693           rightbeams.add_molecule (b);
694         }
695
696     }
697   leftbeams.add_molecule (rightbeams);
698
699   /*
700     Does beam quanting think  of the asymetry of beams? 
701     Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
702    */
703   return leftbeams;
704 }
705
706
707 Molecule*
708 Beam::do_brew_molecule_p () const
709 {
710   Molecule *mol_p = new Molecule;
711   if (!stem_count ())
712     return mol_p;
713   
714   Real x0 = first_visible_stem ()->hpos_f ();
715   Real dx = last_visible_stem ()->hpos_f () - x0;
716   Real dy = get_real ("height");
717   Real dydx = dy && dx ? dy/dx : 0;
718   Real y = get_real ("y-position");
719   for (int j=0; j <stem_count (); j++)
720     {
721       Stem *i = stem (j);
722       Stem * prev = (j > 0)? stem (j-1) : 0;
723       Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
724
725       Molecule sb = stem_beams (i, next, prev);
726       Real x = i->hpos_f ()-x0;
727       sb.translate (Offset (x, x * dydx + y));
728       mol_p->add_molecule (sb);
729     }
730   mol_p->translate_axis (x0 
731     - spanned_drul_[LEFT]->relative_coordinate (0, X_AXIS), X_AXIS);
732
733   return mol_p;
734 }
735
736 int
737 Beam::forced_stem_count () const
738 {
739   int f = 0;
740   for (int i=0; i < stem_count (); i++)
741     {
742       Stem *s = stem (i);
743
744       if (s->invisible_b ())
745         continue;
746
747       if (((int)s->chord_start_f ()) 
748         && (s->get_direction () != s->get_default_dir ()))
749         f++;
750     }
751   return f;
752 }
753
754
755
756 /*
757   TODO: Fix this class. This is wildly inefficient.
758   And it sux.  Yet another array/list 'interface'.
759  */
760 Stem *
761 Beam::stem (int i) const
762 {
763   return Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
764 }
765
766 int
767 Beam::stem_count () const
768 {
769   Group_interface gi (this, "stems");
770   return gi.count ();
771 }
772
773 Stem*
774 Beam::stem_top () const
775 {
776   SCM s = get_elt_property ("stems");
777   
778   return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
779     
780   //Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[stem_count () - 1];
781 }
782
783 /* burp */
784 int
785 Beam::visible_stem_count () const
786 {
787   int c = 0;
788   for (int i = 0; i < stem_count (); i++)
789     {
790       if (!stem (i)->invisible_b ())
791         c++;
792     }
793   return c;
794 }
795
796 Stem*
797 Beam::first_visible_stem () const
798 {
799   for (int i = 0; i < stem_count (); i++)
800     {
801       Stem* s = stem (i);
802       if (!s->invisible_b ())
803         return s;
804     }
805
806   assert (0);
807
808   return 0;
809 }
810
811 Stem*
812 Beam::last_visible_stem () const
813 {
814   for (int i = stem_count (); i > 0; i--)
815     {
816       Stem* s = stem (i - 1);
817       if (!s->invisible_b ())
818         return s;
819     }
820
821   assert (0);
822   // sigh
823   return 0;
824 }