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