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