2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
14 * remove *-hs variables, and do all y-position stuff in staff-space.
15 This is not trivial, as Stem, and Stem_info both use point dimensions
16 (indicated by _f suffix) in several places too.
18 * shorter! (now +- 1000 lines)
20 * move paper vars to scm
25 #include <math.h> // tanh.
27 #include "molecule.hh"
28 #include "directional-element-interface.hh"
32 #include "least-squares.hh"
34 #include "paper-def.hh"
36 #include "group-interface.hh"
37 #include "staff-symbol-referencer.hh"
43 Beam::add_stem (Grob*me, Grob*s)
45 Pointer_group_interface:: add_element(me, "stems", s);
47 s->add_dependency (me);
49 assert (!Stem::beam_l (s));
50 s->set_grob_property ("beam", me->self_scm ());
52 add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (s));
56 Beam::get_multiplicity (Grob*me)
59 for (SCM s = me->get_grob_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
61 Grob * sc = unsmob_grob (gh_car (s));
63 if (Stem::has_interface (sc))
64 m = m >? Stem::beam_count (sc,LEFT) >? Stem::beam_count (sc,RIGHT);
70 After pre-processing all directions should be set.
71 Several post-processing routines (stem, slur, script) need stem/beam
73 Currenly, this means that beam has set all stem's directions.
74 [Alternatively, stems could set its own directions, according to
75 their beam, during 'final-pre-processing'.]
77 MAKE_SCHEME_CALLBACK(Beam,before_line_breaking,1);
79 Beam::before_line_breaking (SCM smob)
81 Grob * me = unsmob_grob (smob);
85 Why what? Why the warning (beams with less than 2 stems are
86 degenerate beams, should never happen), or why would this ever
87 happen (don't know). */
88 if (visible_stem_count (me) < 2)
90 warning (_ ("beam has less than two stems"));
92 if (visible_stem_count (me) >= 1)
94 if (!Directional_element_interface::get (me))
95 Directional_element_interface::set (me, get_default_dir (me));
97 consider_auto_knees (me);
98 set_stem_directions (me);
99 set_stem_shorten (me);
105 Beam::get_default_dir (Grob*me)
107 Drul_array<int> total;
108 total[UP] = total[DOWN] = 0;
109 Drul_array<int> count;
110 count[UP] = count[DOWN] = 0;
113 Link_array<Item> stems=
114 Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
116 for (int i=0; i <stems.size (); i++)
119 Direction sd = Directional_element_interface::get (s);
120 int current = sd ? (1 + d * sd)/2
121 : Stem::get_center_distance (s, (Direction)-d);
129 } while (flip(&d) != DOWN);
131 SCM func = me->get_grob_property ("dir-function");
132 SCM s = gh_call2 (func,
133 gh_cons (gh_int2scm (count[UP]),
134 gh_int2scm (count[DOWN])),
135 gh_cons (gh_int2scm (total[UP]),
136 gh_int2scm (total[DOWN])));
138 if (gh_number_p (s) && gh_scm2int (s))
142 If dir is not determined: get default
144 return to_dir (me->get_grob_property ("default-neutral-direction"));
149 Set all stems with non-forced direction to beam direction.
150 Urg: non-forced should become `without/with unforced' direction,
151 once stem gets cleaned-up.
154 Beam::set_stem_directions (Grob*me)
156 Link_array<Item> stems
157 =Pointer_group_interface__extract_elements (me, (Item*) 0, "stems");
158 Direction d = Directional_element_interface::get (me);
160 for (int i=0; i <stems.size (); i++)
163 SCM force = s->remove_grob_property ("dir-forced");
164 if (!gh_boolean_p (force) || !gh_scm2bool (force))
165 Directional_element_interface ::set (s,d);
170 Simplistic auto-knees; only consider vertical gap between two
173 `Forced' stem directions are ignored. If you don't want auto-knees,
174 don't set, or unset auto-knee-gap.
177 Beam::consider_auto_knees (Grob *me)
179 SCM scm = me->get_grob_property ("auto-knee-gap");
181 if (gh_number_p (scm))
185 Real staff_space = Staff_symbol_referencer::staff_space (me);
186 Real gap = gh_scm2double (scm) / staff_space;
188 Direction d = Directional_element_interface::get (me);
189 Link_array<Item> stems=
190 Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
192 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
193 for (int i=1; i < stems.size (); i++)
194 if (!Stem::invisible_b (stems[i]))
195 common = common->common_refpoint (stems[i], Y_AXIS);
198 for (int i=1; i < stems.size (); i++)
200 if (!Stem::invisible_b (stems[i-1]))
202 if (Stem::invisible_b (stems[l]))
204 if (Stem::invisible_b (stems[i]))
207 Real left = Stem::extremal_heads (stems[l])[d]
208 ->relative_coordinate (common, Y_AXIS);
209 Real right = Stem::extremal_heads (stems[i])[-d]
210 ->relative_coordinate (common, Y_AXIS);
212 Real dy = right - left;
216 knee_y = (right + left) / 2;
224 for (int i=0; i < stems.size (); i++)
226 if (Stem::invisible_b (stems[i]))
229 Real y = Stem::extremal_heads (stems[i])[d]
230 ->relative_coordinate (common, Y_AXIS);
232 Directional_element_interface::set (s, y < knee_y ? UP : DOWN);
233 s->set_grob_property ("dir-forced", SCM_BOOL_T);
240 Set stem's shorten property if unset.
242 take some y-position (chord/beam/nearest?) into account
243 scmify forced-fraction
246 Beam::set_stem_shorten (Grob*m)
248 Spanner*me = dynamic_cast<Spanner*> (m);
250 Real forced_fraction = forced_stem_count (me) / visible_stem_count (me);
251 if (forced_fraction < 0.5)
254 int multiplicity = get_multiplicity (me);
256 SCM shorten = me->get_grob_property ("beamed-stem-shorten");
257 if (shorten == SCM_EOL)
260 int sz = scm_ilength (shorten);
262 Real staff_space = Staff_symbol_referencer::staff_space (me);
263 SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
264 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
266 /* cute, but who invented me -- how to customise ? */
267 if (forced_fraction < 1)
270 Link_array<Item> stems=
271 Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
273 for (int i=0; i < stems.size (); i++)
276 if (Stem::invisible_b (s))
278 if (gh_number_p (s->get_grob_property ("shorten")))
279 s->set_grob_property ("shorten", gh_double2scm (shorten_f));
284 Call list of y-dy-callbacks, that handle setting of
285 grob-properties y, dy.
287 User may set grob-properties: y-position-hs and height-hs
288 (to be fixed) that override the calculated y and dy.
290 Because y and dy cannot be calculated and quanted separately, we
291 always calculate both, then check for user override.
293 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
295 Beam::after_line_breaking (SCM smob)
297 Grob * me = unsmob_grob (smob);
299 me->set_grob_property ("y", gh_double2scm (0));
300 me->set_grob_property ("dy", gh_double2scm (0));
302 /* Hmm, callbacks should be called by, a eh, callback mechanism
303 somewhere(?), I guess, not by looping here. */
305 SCM list = me->get_grob_property ("y-dy-callbacks");
306 for (SCM i = list; gh_pair_p (i); i = gh_cdr (i))
307 gh_call1 (gh_car (i), smob);
309 // UGH. Y is not in staff position unit?
310 // Ik dacht datwe daar juist van weg wilden?
312 // Hmm, nu hebben we 3 dimensies, want inmiddels zijn we daar
313 // weer terug, maar dan / 2
314 // (staff-space iso staff-position)
316 set_stem_lengths (me);
318 return SCM_UNSPECIFIED;
322 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
324 Beam::least_squares (SCM smob)
326 Grob *me = unsmob_grob (smob);
328 if (visible_stem_count (me) <= 1)
329 return SCM_UNSPECIFIED;
334 Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
335 if (first_ideal == Stem::calc_stem_info (last_visible_stem (me)).idealy_f_)
342 Array<Offset> ideals;
344 // ugh -> use commonx
345 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
346 Link_array<Item> stems=
347 Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
349 for (int i=0; i < stems.size (); i++)
352 if (Stem::invisible_b (s))
354 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
355 Stem::calc_stem_info (s).idealy_f_));
358 minimise_least_squares (&dydx, &y, ideals);
360 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
364 me->set_grob_property ("y", gh_double2scm (y));
365 me->set_grob_property ("dy", gh_double2scm (dy));
366 return SCM_UNSPECIFIED;
369 MAKE_SCHEME_CALLBACK (Beam, cancel_suspect_slope, 1);
371 Beam::cancel_suspect_slope (SCM smob)
373 Grob *me = unsmob_grob (smob);
375 if (visible_stem_count (me) <= 1)
376 return SCM_UNSPECIFIED;
378 Real y = gh_scm2double (me->get_grob_property ("y"));
379 Real dy = gh_scm2double (me->get_grob_property ("dy"));
381 /* steep slope running against lengthened stem is suspect */
382 Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
383 Real last_ideal = Stem::calc_stem_info (last_visible_stem (me)).idealy_f_;
384 Real lengthened = gh_scm2double (me->get_grob_property ("outer-stem-length-limit"));
385 Real steep = gh_scm2double (me->get_grob_property ("slope-limit"));
387 // ugh -> use commonx
388 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
389 Real dydx = dy && dx ? dy/dx : 0;
391 if (((y - first_ideal > lengthened) && (dydx > steep))
392 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
394 Real adjusted_y = y + dy / 2;
395 me->set_grob_property ("y", gh_double2scm (adjusted_y));
396 me->set_grob_property ("dy", gh_double2scm (0));
398 return SCM_UNSPECIFIED;
402 This neat trick is by Werner Lemberg,
403 damped = tanh (slope)
404 corresponds with some tables in [Wanske]
406 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
408 Beam::slope_damping (SCM smob)
410 Grob *me = unsmob_grob (smob);
412 if (visible_stem_count (me) <= 1)
413 return SCM_UNSPECIFIED;
415 SCM s = me->get_grob_property ("damping");
416 int damping = gh_scm2int (s);
420 Real y = gh_scm2double (me->get_grob_property ("y"));
421 Real dy = gh_scm2double (me->get_grob_property ("dy"));
423 // ugh -> use commonx
424 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS)
425 - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
426 Real dydx = dy && dx ? dy/dx : 0;
427 dydx = 0.6 * tanh (dydx) / damping;
429 Real damped_dy = dydx * dx;
430 Real adjusted_y = y + (dy - damped_dy) / 2;
431 me->set_grob_property ("y", gh_double2scm (adjusted_y));
432 me->set_grob_property ("dy", gh_double2scm (damped_dy));
434 return SCM_UNSPECIFIED;
438 Quantise dy (height) of beam.
439 Generalisation of [Ross].
441 MAKE_SCHEME_CALLBACK (Beam, quantise_dy, 1);
443 Beam::quantise_dy (SCM smob)
445 Grob *me = unsmob_grob (smob);
447 if (visible_stem_count (me) <= 1)
448 return SCM_UNSPECIFIED;
451 SCM proc = me->get_grob_property ("height-quants");
452 SCM quants = gh_call2 (proc, me->self_scm (),
453 gh_double2scm (me->paper_l ()->get_var ("stafflinethickness")
456 for (SCM s = quants; gh_pair_p (s); s = gh_cdr (s))
457 a.push (gh_scm2double (gh_car (s)));
461 Real y = gh_scm2double (me->get_grob_property ("y"));
462 Real dy = gh_scm2double (me->get_grob_property ("dy"));
464 Real staff_space = Staff_symbol_referencer::staff_space (me);
466 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
467 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
471 Real quantised_dy = q * sign (dy);
472 Real adjusted_y = y + (dy - quantised_dy) / 2;
473 me->set_grob_property ("y", gh_double2scm (adjusted_y));
474 me->set_grob_property ("dy", gh_double2scm (quantised_dy));
476 return SCM_UNSPECIFIED;
481 What to do? Why do we have two dimensions (staff-position and
482 staff-space)? Do other grobs export staff-position to the user,
485 height-hs -> staff-position-height
486 y-position-hs -> staff-position
490 height-hs -> height / 2
491 y-postion-hs -> y-position / 2
494 UGHUGH. IF this callback is omitted, we hang.
495 FIXME: until here, we used only stem_info, which acts as if dir=up.
497 MAKE_SCHEME_CALLBACK (Beam, user_override, 1);
499 Beam::user_override (SCM smob)
501 Grob *me = unsmob_grob (smob);
502 Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
504 Real y = gh_scm2double (me->get_grob_property ("y"));
505 Real dy = gh_scm2double (me->get_grob_property ("dy"));
508 SCM s = me->get_grob_property ("y-position-hs");
510 y = gh_scm2double (s) * half_space;
513 y *= Directional_element_interface::get (me);
515 s = me->get_grob_property ("height-hs");
517 dy = gh_scm2double (s) * half_space;
520 dy *= Directional_element_interface::get (me);
523 me->set_grob_property ("y", gh_double2scm (y));
524 me->set_grob_property ("dy", gh_double2scm (dy));
526 return SCM_UNSPECIFIED;
530 Ugh, this must be last, after user_override
531 Assumes directionised y/dy.
533 MAKE_SCHEME_CALLBACK (Beam, do_quantise_y, 1);
535 Beam::do_quantise_y (SCM smob)
537 Grob *me = unsmob_grob (smob);
540 If the user set y-position, we shouldn't do quanting.
542 if (gh_number_p (me->get_grob_property ("y-position-hs")))
543 return SCM_UNSPECIFIED;
545 Real y = gh_scm2double (me->get_grob_property ("y"));
546 Real dy = gh_scm2double (me->get_grob_property ("dy"));
548 /* we can modify y, so we should quantise y */
549 Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
550 Real y_shift = check_stem_length_f (me, y, dy);
552 y = quantise_y_f (me, y, dy, 0);
555 Hmm, this is a bit keyhole operation: we're passing `this' as a
556 parameter, and member vars as SCM properties. We should decide on
557 SCM/C/C++ boundary */
558 me->set_grob_property ("y", gh_double2scm (y));
559 set_stem_lengths (me);
560 y = gh_scm2double (me->get_grob_property ("y"));
562 y_shift = check_stem_length_f (me, y, dy);
564 if (y_shift > half_space / 4)
569 for significantly lengthened or shortened stems,
570 request quanting the other way.
573 if (abs (y_shift) > half_space / 2)
574 quant_dir = sign (y_shift) * Directional_element_interface::get (me);
575 y = quantise_y_f (me, y, dy, quant_dir);
578 me->set_grob_property ("y", gh_double2scm (y));
579 // me->set_grob_property ("dy", gh_double2scm (dy));
580 return SCM_UNSPECIFIED;
585 Beam::calc_stem_y_f (Grob*me,Item* s, Real y, Real dy)
587 int beam_multiplicity = get_multiplicity (me);
588 int stem_multiplicity = (Stem::flag_i (s) - 2) >? 0;
590 SCM space_proc = me->get_grob_property ("space-function");
591 SCM space = gh_call1 (space_proc, gh_int2scm (beam_multiplicity));
593 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
594 Real interbeam_f = gh_scm2double (space) ;
596 // ugh -> use commonx
597 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
598 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
599 Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
602 Direction dir = Directional_element_interface::get (me);
603 Direction sdir = Directional_element_interface::get (s);
609 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
613 // huh, why not for first visible?
614 if (Staff_symbol_referencer::staff_symbol_l (s)
615 != Staff_symbol_referencer::staff_symbol_l (last_visible_stem (me)))
616 stem_y += Directional_element_interface::get (me)
617 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
624 Beam::check_stem_length_f (Grob*me,Real y, Real dy)
628 Direction dir = Directional_element_interface::get (me);
630 Link_array<Item> stems=
631 Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
633 for (int i=0; i < stems.size(); i++)
636 if (Stem::invisible_b (s))
639 Real stem_y = calc_stem_y_f (me, s, y, dy);
642 Stem_info info = Stem::calc_stem_info (s);
644 // if (0 > info.maxy_f_ - stem_y)
645 shorten = shorten <? info.maxy_f_ - stem_y;
646 // if (0 < info.miny_f_ - stem_y)
647 lengthen = lengthen >? info.miny_f_ - stem_y;
650 if (lengthen && shorten)
651 warning (_ ("weird beam vertical offset"));
653 /* when all stems are too short, normal stems win */
654 return dir * ((shorten) ? shorten : lengthen);
658 Hmm. At this time, beam position and slope are determined. Maybe,
659 stem directions and length should set to relative to the chord's
660 position of the beam. */
662 Beam::set_stem_lengths (Grob *me)
664 if (visible_stem_count (me) <= 1)
667 Real y = gh_scm2double (me->get_grob_property ("y"));
668 Real dy = gh_scm2double (me->get_grob_property ("dy"));
670 Real half_space = Staff_symbol_referencer::staff_space (me)/2;
671 Link_array<Item> stems=
672 Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
674 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
675 for (int i=1; i < stems.size (); i++)
676 if (!Stem::invisible_b (stems[i]))
677 common = common->common_refpoint (stems[i], Y_AXIS);
679 for (int i=0; i < stems.size (); i++)
682 if (Stem::invisible_b (s))
685 Real stem_y = calc_stem_y_f (me, s, y, dy);
687 /* caution: stem measures in staff-positions */
688 Real id = me->relative_coordinate (common, Y_AXIS)
689 - stems[i]->relative_coordinate (common, Y_AXIS);
690 Stem::set_stemend (s, (stem_y + id) / half_space);
695 Prevent interference from stafflines and beams.
697 We only need to quantise the (left) y of the beam,
698 since dy is quantised too.
699 if extend_b then stems must *not* get shorter
702 Beam::quantise_y_f (Grob*me,Real y, Real dy, int quant_dir)
704 int multiplicity = get_multiplicity (me);
706 Real staff_space = Staff_symbol_referencer::staff_space (me);
707 Real thick = me->paper_l ()->get_var ("stafflinethickness");
710 SCM proc = me->get_grob_property ("vertical-position-quant-function");
711 SCM quants = scm_apply (proc,
713 gh_list (gh_int2scm (multiplicity),
714 gh_double2scm (dy/staff_space),
715 gh_double2scm (thick/staff_space),
716 SCM_EOL, SCM_UNDEFINED));
720 for (; gh_pair_p (quants); quants = gh_cdr (quants))
721 a.push (gh_scm2double (gh_car (quants)));
726 Real up_y = Directional_element_interface::get (me) * y;
727 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
729 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
730 ? iv[SMALLER] : iv[BIGGER];
732 q = iv[(Direction)quant_dir];
734 return q * Directional_element_interface::get (me);
738 Beam::set_beaming (Grob*me,Beaming_info_list *beaming)
740 Link_array<Grob> stems=
741 Pointer_group_interface__extract_elements (me, (Grob*)0, "stems");
744 for (int i=0; i < stems.size(); i++)
748 /* Don't overwrite user override (?) */
749 if (Stem::beam_count (stems[i], d) == 0
750 /* Don't set beaming for outside of outer stems */
751 && ! (d == LEFT && i == 0)
752 && ! (d == RIGHT && i == stems.size () -1))
754 int b = beaming->infos_.elem (i).beams_i_drul_[d];
755 Stem::set_beaming (stems[i], b, d);
758 while (flip (&d) != LEFT);
765 beams to go with one stem.
770 Beam::stem_beams (Grob*me,Item *here, Item *next, Item *prev)
772 // ugh -> use commonx
773 if ((next && !(next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
774 (prev && !(prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
775 programming_error ("Beams are not left-to-right");
777 Real staffline_f = me->paper_l ()->get_var ("stafflinethickness");
778 int multiplicity = get_multiplicity (me);
780 SCM space_proc = me->get_grob_property ("space-function");
781 SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
783 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
784 Real interbeam_f = gh_scm2double (space) ;
786 Real bdy = interbeam_f;
787 Real stemdx = staffline_f;
789 // ugh -> use commonx
790 Real dx = visible_stem_count (me) ?
791 last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS)
793 Real dy = gh_scm2double (me->get_grob_property ("dy"));
794 Real dydx = dy && dx ? dy/dx : 0;
800 if (!Stem::first_head (here))
803 int t = Stem::type_i (here);
805 SCM proc = me->get_grob_property ("flag-width-function");
806 SCM result = gh_call1 (proc, gh_int2scm (t));
807 nw_f = gh_scm2double (result);
811 Direction dir = Directional_element_interface::get (me);
813 /* half beams extending to the left. */
816 int lhalfs= lhalfs = Stem::beam_count (here,LEFT) - Stem::beam_count (prev,RIGHT);
817 int lwholebeams= Stem::beam_count (here,LEFT) <? Stem::beam_count (prev,RIGHT) ;
819 Half beam should be one note-width,
820 but let's make sure two half-beams never touch
822 Real w = here->relative_coordinate (0, X_AXIS) - prev->relative_coordinate (0, X_AXIS);
825 if (lhalfs) // generates warnings if not
826 a = Lookup::beam (dydx, w, thick);
827 a.translate (Offset (-w, -w * dydx));
828 for (int j = 0; j < lhalfs; j++)
831 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
832 leftbeams.add_molecule (b);
838 int rhalfs = Stem::beam_count (here,RIGHT) - Stem::beam_count (next,LEFT);
839 int rwholebeams= Stem::beam_count (here,RIGHT) <? Stem::beam_count (next,LEFT) ;
841 Real w = next->relative_coordinate (0, X_AXIS) - here->relative_coordinate (0, X_AXIS);
842 Molecule a = Lookup::beam (dydx, w + stemdx, thick);
843 a.translate_axis( - stemdx/2, X_AXIS);
847 SCM gap = me->get_grob_property ("gap");
848 if (gh_number_p (gap))
850 int gap_i = gh_scm2int ( (gap));
851 int nogap = rwholebeams - gap_i;
853 for (; j < nogap; j++)
856 b.translate_axis (-dir * bdy * j, Y_AXIS);
857 rightbeams.add_molecule (b);
859 // TODO: notehead widths differ for different types
862 a = Lookup::beam (dydx, w + stemdx, thick);
865 for (; j < rwholebeams; j++)
868 b.translate (Offset (Stem::invisible_b (here) ? 0 : gap_f, -dir * bdy * j));
869 rightbeams.add_molecule (b);
874 a = Lookup::beam (dydx, w, thick);
876 for (; j < rwholebeams + rhalfs; j++)
879 b.translate_axis (- dir * bdy * j, Y_AXIS);
880 rightbeams.add_molecule (b);
884 leftbeams.add_molecule (rightbeams);
887 Does beam quanting think of the asymetry of beams?
888 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
893 MAKE_SCHEME_CALLBACK(Beam,brew_molecule,1);
895 Beam::brew_molecule (SCM smob)
897 Grob * me =unsmob_grob (smob);
900 if (!gh_pair_p (me->get_grob_property ("stems")))
903 Link_array<Item>stems =
904 Pointer_group_interface__extract_elements (me, (Item*) 0, "stems");
905 if (visible_stem_count (me))
907 // ugh -> use commonx
908 x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
909 dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
913 x0 = stems[0]->relative_coordinate (0, X_AXIS);
914 dx = stems.top()->relative_coordinate (0, X_AXIS) - x0;
918 Real dy = gh_scm2double (me->get_grob_property ("dy"));
919 Real dydx = dy && dx ? dy/dx : 0;
920 Real y = gh_scm2double (me->get_grob_property ("y"));
923 for (int j=0; j <stems.size (); j++)
926 Item * prev = (j > 0)? stems[j-1] : 0;
927 Item * next = (j < stems.size()-1) ? stems[j+1] :0;
929 Molecule sb = stem_beams (me, i, next, prev);
930 Real x = i->relative_coordinate (0, X_AXIS)-x0;
931 sb.translate (Offset (x, x * dydx + y));
932 mol.add_molecule (sb);
934 mol.translate_axis (x0
935 - dynamic_cast<Spanner*> (me)->get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
937 return mol.smobbed_copy ();
941 Beam::forced_stem_count (Grob*me)
943 Link_array<Item>stems =
944 Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
946 for (int i=0; i < stems.size (); i++)
950 if (Stem::invisible_b (s))
953 if (((int)Stem::chord_start_f (s))
954 && (Stem::get_direction (s ) != Stem::get_default_dir (s )))
964 use filter and standard list functions.
967 Beam::visible_stem_count (Grob*me)
969 Link_array<Item>stems =
970 Pointer_group_interface__extract_elements (me, (Item*) 0, "stems");
972 for (int i = stems.size (); i--;)
974 if (!Stem::invisible_b (stems[i]))
981 Beam::first_visible_stem(Grob*me)
983 Link_array<Item>stems =
984 Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
986 for (int i = 0; i < stems.size (); i++)
988 if (!Stem::invisible_b (stems[i]))
995 Beam::last_visible_stem(Grob*me)
997 Link_array<Item>stems =
998 Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
999 for (int i = stems.size (); i--;)
1001 if (!Stem::invisible_b (stems[i]))
1010 handle rest under beam (do_post: beams are calculated now)
1011 what about combination of collisions and rest under beam.
1015 rest -> stem -> beam -> interpolate_y_position ()
1017 MAKE_SCHEME_CALLBACK(Beam,rest_collision_callback,2);
1019 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1021 Grob *rest = unsmob_grob (element_smob);
1022 Axis a = (Axis) gh_scm2int (axis);
1024 assert (a == Y_AXIS);
1026 Grob * st = unsmob_grob (rest->get_grob_property ("stem"));
1029 return gh_double2scm (0.0);
1030 Grob * beam = unsmob_grob (stem->get_grob_property ("beam"));
1031 if (!beam || !Beam::has_interface (beam) || !Beam::visible_stem_count (beam))
1032 return gh_double2scm (0.0);
1034 // make callback for rest from this.
1039 // todo: make sure this calced already.
1040 SCM s = beam->get_grob_property ("dy");
1041 if (gh_number_p (s))
1042 beam_dy = gh_scm2double (s);
1044 s = beam->get_grob_property ("y");
1045 if (gh_number_p (s))
1046 beam_y = gh_scm2double (s);
1048 // ugh -> use commonx
1049 Real x0 = first_visible_stem(beam)->relative_coordinate (0, X_AXIS);
1050 Real dx = last_visible_stem(beam)->relative_coordinate (0, X_AXIS) - x0;
1051 Real dydx = beam_dy && dx ? beam_dy/dx : 0;
1053 Direction d = Stem::get_direction (stem);
1054 Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + beam_y;
1056 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1059 Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space ; // refp??
1062 = gh_scm2double (rest->get_grob_property ("minimum-beam-collision-distance"));
1064 minimum_dist + -d * (beamy - rest_dim) >? 0;
1066 int stafflines = Staff_symbol_referencer::line_count (rest);
1068 // move discretely by half spaces.
1069 int discrete_dist = int (ceil (dist));
1071 // move by whole spaces inside the staff.
1072 if (discrete_dist < stafflines+1)
1073 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
1075 return gh_double2scm (-d * discrete_dist);
1080 Beam::has_interface (Grob*me)
1082 return me->has_interface (ly_symbol2scm ("beam-interface"));
1086 Beam::set_interface (Grob*me)
1090 why the init? No way to tell difference between default and user
1092 me->set_grob_property ("y" ,gh_double2scm (0));
1093 me->set_grob_property ("dy", gh_double2scm (0));
1094 me->set_interface (ly_symbol2scm("beam-interface"));