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