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