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