2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
14 * different left/right quanting: slope, multiplicity
20 * Remove #'direction from beam. A beam has no direction per se.
21 It may only set directions for stems.
28 #include <math.h> // tanh.
30 #include "molecule.hh"
31 #include "directional-element-interface.hh"
35 #include "least-squares.hh"
37 #include "paper-def.hh"
39 #include "group-interface.hh"
40 #include "staff-symbol-referencer.hh"
46 Beam::add_stem (Grob *me, Grob *s)
48 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
50 s->add_dependency (me);
52 assert (!Stem::beam_l (s));
53 s->set_grob_property ("beam", me->self_scm ());
55 add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (s));
59 Beam::get_multiplicity (Grob *me)
62 for (SCM s = me->get_grob_property ("stems"); gh_pair_p (s); s = ly_cdr (s))
64 Grob *sc = unsmob_grob (ly_car (s));
66 if (Stem::has_interface (sc))
67 m = m >? Stem::beam_count (sc, LEFT) >? Stem::beam_count (sc, RIGHT);
72 /* After pre-processing all directions should be set.
73 Several post-processing routines (stem, slur, script) need stem/beam
75 Currenly, this means that beam has set all stem's directions.
76 [Alternatively, stems could set its own directions, according to
77 their beam, during 'final-pre-processing'.] */
78 MAKE_SCHEME_CALLBACK (Beam, before_line_breaking, 1);
80 Beam::before_line_breaking (SCM smob)
82 Grob *me = unsmob_grob (smob);
84 /* Beams with less than 2 two stems don't make much sense, but could happen
89 For a beam that only has one stem, we try to do some disappearance magic:
90 we revert the flag, and move on to The Eternal Engraving Fields. */
92 int count = visible_stem_count (me);
95 me->warning (_ ("beam has less than two visible stems"));
97 SCM stems = me->get_grob_property ("stems");
98 if (scm_ilength (stems) == 1)
100 me->warning (_ ("Beam has less than two stems. Removing beam."));
102 unsmob_grob (gh_car (stems))->remove_grob_property ("beam");
105 return SCM_UNSPECIFIED;
107 else if (scm_ilength (stems) == 0)
110 return SCM_UNSPECIFIED;
115 if (!Directional_element_interface::get (me))
116 Directional_element_interface::set (me, get_default_dir (me));
118 consider_auto_knees (me);
119 set_stem_directions (me);
120 set_stem_shorten (me);
126 Beam::get_default_dir (Grob *me)
128 Drul_array<int> total;
129 total[UP] = total[DOWN] = 0;
130 Drul_array<int> count;
131 count[UP] = count[DOWN] = 0;
134 Link_array<Item> stems=
135 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
137 for (int i=0; i <stems.size (); i++)
140 Direction sd = Directional_element_interface::get (s);
141 int current = sd ? (1 + d * sd)/2
142 : Stem::get_center_distance (s, (Direction)-d);
150 } while (flip (&d) != DOWN);
152 SCM func = me->get_grob_property ("dir-function");
153 SCM s = gh_call2 (func,
154 gh_cons (gh_int2scm (count[UP]),
155 gh_int2scm (count[DOWN])),
156 gh_cons (gh_int2scm (total[UP]),
157 gh_int2scm (total[DOWN])));
159 if (gh_number_p (s) && gh_scm2int (s))
162 /* If dir is not determined: get default */
163 return to_dir (me->get_grob_property ("neutral-direction"));
167 /* Set all stems with non-forced direction to beam direction.
168 Urg: non-forced should become `without/with unforced' direction,
169 once stem gets cleaned-up. */
171 Beam::set_stem_directions (Grob *me)
173 Link_array<Item> stems
174 =Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
175 Direction d = Directional_element_interface::get (me);
177 for (int i=0; i <stems.size (); i++)
180 SCM force = s->remove_grob_property ("dir-forced");
181 if (!gh_boolean_p (force) || !gh_scm2bool (force))
182 Directional_element_interface::set (s, d);
186 /* Simplistic auto-knees; only consider vertical gap between two
189 `Forced' stem directions are ignored. If you don't want auto-knees,
190 don't set, or unset auto-knee-gap. */
192 Beam::consider_auto_knees (Grob *me)
194 SCM scm = me->get_grob_property ("auto-knee-gap");
196 if (gh_number_p (scm))
200 Real staff_space = Staff_symbol_referencer::staff_space (me);
201 Real gap = gh_scm2double (scm) / staff_space;
203 Direction d = Directional_element_interface::get (me);
204 Link_array<Item> stems=
205 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
207 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
208 for (int i=1; i < stems.size (); i++)
209 if (!Stem::invisible_b (stems[i]))
210 common = common->common_refpoint (stems[i], Y_AXIS);
213 for (int i=1; i < stems.size (); i++)
215 if (!Stem::invisible_b (stems[i-1]))
217 if (Stem::invisible_b (stems[l]))
219 if (Stem::invisible_b (stems[i]))
222 Real left = Stem::extremal_heads (stems[l])[d]
223 ->relative_coordinate (common, Y_AXIS);
224 Real right = Stem::extremal_heads (stems[i])[-d]
225 ->relative_coordinate (common, Y_AXIS);
227 Real dy = right - left;
231 knee_y = (right + left) / 2;
239 for (int i=0; i < stems.size (); i++)
241 if (Stem::invisible_b (stems[i]))
244 Real y = Stem::extremal_heads (stems[i])[d]
245 ->relative_coordinate (common, Y_AXIS);
247 Directional_element_interface::set (s, y < knee_y ? UP : DOWN);
248 s->set_grob_property ("dir-forced", SCM_BOOL_T);
254 /* Set stem's shorten property if unset.
256 take some y-position (chord/beam/nearest?) into account
257 scmify forced-fraction */
259 Beam::set_stem_shorten (Grob *m)
261 Spanner*me = dynamic_cast<Spanner*> (m);
263 Real forced_fraction = forced_stem_count (me) / visible_stem_count (me);
265 int multiplicity = get_multiplicity (me);
267 SCM shorten = me->get_grob_property ("beamed-stem-shorten");
268 if (shorten == SCM_EOL)
271 int sz = scm_ilength (shorten);
273 Real staff_space = Staff_symbol_referencer::staff_space (me);
274 SCM shorten_elt = scm_list_ref (shorten,
275 gh_int2scm (multiplicity <? (sz - 1)));
276 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
278 /* your similar cute comment here */
279 shorten_f *= forced_fraction;
281 me->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. */
292 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
294 Beam::after_line_breaking (SCM smob)
296 Grob *me = unsmob_grob (smob);
298 /* Copy to mutable list. */
299 SCM s = ly_deep_copy (me->get_grob_property ("positions"));
300 me->set_grob_property ("positions", s);
302 if (ly_car (s) != SCM_BOOL_F)
303 return SCM_UNSPECIFIED;
305 SCM callbacks = me->get_grob_property ("position-callbacks");
306 for (SCM i = callbacks; gh_pair_p (i); i = ly_cdr (i))
307 gh_call1 (ly_car (i), smob);
309 return SCM_UNSPECIFIED;
313 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
315 Beam::least_squares (SCM smob)
317 Grob *me = unsmob_grob (smob);
319 int count = visible_stem_count (me);
324 me->set_grob_property ("positions", ly_interval2scm (pos));
325 return SCM_UNSPECIFIED;
328 Direction dir = Directional_element_interface::get (me);
330 Interval ideal (Stem::calc_stem_info (first_visible_stem (me)).idealy_f_,
331 Stem::calc_stem_info (last_visible_stem (me)).idealy_f_);
334 Interval chord (Stem::chord_start_f (first_visible_stem (me)),
335 Stem::chord_start_f (last_visible_stem (me)));
337 /* Make simple beam on middle line have small tilt */
338 if (!ideal[LEFT] && chord.delta () && count == 2)
340 Direction d = (Direction)(sign (chord.delta ()) * dir);
341 pos[d] = gh_scm2double (me->get_grob_property ("thickness")) / 2
350 Array<Offset> ideals;
352 // ugh -> use commonx
353 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
354 Link_array<Item> stems=
355 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
357 for (int i=0; i < stems.size (); i++)
360 if (Stem::invisible_b (s))
362 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
363 Stem::calc_stem_info (s).idealy_f_));
367 minimise_least_squares (&dydx, &y, ideals);
369 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
371 pos = Interval (y*dir, (y+dy) * dir);
374 me->set_grob_property ("positions", ly_interval2scm (pos));
375 return SCM_UNSPECIFIED;
378 MAKE_SCHEME_CALLBACK (Beam, check_concave, 1);
380 Beam::check_concave (SCM smob)
382 Grob *me = unsmob_grob (smob);
384 Link_array<Item> stems =
385 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
387 for (int i = 0; i < stems.size ();)
389 if (Stem::invisible_b (stems[i]))
395 if (stems.size () < 3)
396 return SCM_UNSPECIFIED;
398 /* Concaveness try #2: Sum distances of inner noteheads that
399 fall outside the interval of the two outer noteheads */
401 Interval iv (Stem::chord_start_f (stems[0]),
402 Stem::chord_start_f (stems.top ()));
404 if (iv[MAX] < iv[MIN])
407 for (int i = 1; i < stems.size () - 1; i++)
410 Real f = Stem::chord_start_f (stems[i]);
411 if ((c = f - iv[MAX]) > 0)
413 else if ((c = f - iv[MIN]) < 0)
416 concave *= Directional_element_interface::get (me);
418 Real concaveness = concave / (stems.size () - 2);
419 /* ugh: this is the a kludge to get input/regression/beam-concave.ly
420 to behave as baerenreiter. */
421 concaveness /= (stems.size () - 2);
423 Real r = gh_scm2double (me->get_grob_property ("concaveness-threshold"));
425 /* TODO: some sort of damping iso -> plain horizontal */
428 Interval pos = ly_scm2interval (me->get_grob_property ("positions"));
429 Real r = pos.linear_combination (0);
430 me->set_grob_property ("positions", ly_interval2scm (Interval (r, r)));
433 return SCM_UNSPECIFIED;
436 /* This neat trick is by Werner Lemberg,
437 damped = tanh (slope)
438 corresponds with some tables in [Wanske] CHECKME */
439 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
441 Beam::slope_damping (SCM smob)
443 Grob *me = unsmob_grob (smob);
445 if (visible_stem_count (me) <= 1)
446 return SCM_UNSPECIFIED;
448 SCM s = me->get_grob_property ("damping");
449 int damping = gh_scm2int (s);
453 Interval pos = ly_scm2interval (me->get_grob_property ("positions"));
454 Real dy = pos.delta ();
456 // ugh -> use commonx
457 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS)
458 - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
459 Real dydx = dy && dx ? dy/dx : 0;
460 dydx = 0.6 * tanh (dydx) / damping;
462 Real damped_dy = dydx * dx;
463 pos[LEFT] += (dy - damped_dy) / 2;
464 pos[RIGHT] -= (dy - damped_dy) / 2;
466 me->set_grob_property ("positions", ly_interval2scm (pos));
468 return SCM_UNSPECIFIED;
472 /* Prevent interference from stafflines. */
474 Beam::quantise_interval (Grob *me, Interval pos, Direction quant_dir)
476 int multiplicity = get_multiplicity (me);
478 Real staff_space = Staff_symbol_referencer::staff_space (me);
479 Real thick = me->paper_l ()->get_var ("stafflinethickness");
483 - left and right should be different, depending on direction and
486 -use different left-position-quant-function,
487 right-position-quant-function for handier slope quanting? */
488 SCM proc = me->get_grob_property ("vertical-position-quant-function");
489 SCM quants = scm_apply (proc,
491 scm_list_n (gh_int2scm (multiplicity),
492 gh_double2scm (1), /* junkme */
493 gh_double2scm (thick / staff_space),
499 for (SCM i = quants; gh_pair_p (i); i = ly_cdr (i))
500 a.push (gh_scm2double (ly_car (i)));
505 Direction dir = Directional_element_interface::get (me);
506 Interval left = quantise_iv (a, pos[LEFT]*dir/staff_space) * staff_space;
507 Interval right = quantise_iv (a, pos[RIGHT]*dir/staff_space) * staff_space;
509 Real dy = pos.delta ();
512 // quant direction hints disabled for now
513 int q = 0;//(int)quant_dir;
515 /* TODO: make smart choice, find best left/right quants pair.
517 Slope should never be steeper than least_squares (before damping)
519 Slope should never be reduced to zero.
521 Interval qpos (0, 20.0 *sign (dy));
522 Direction ldir = LEFT;
525 Direction rdir = LEFT;
528 Interval i (left[ldir]*dir, right[rdir]*dir);
529 if ((abs (abs (i.delta ()) - ady) <= abs (abs (qpos.delta ()) - ady)
530 && sign (i.delta ()) == sign (pos.delta ())
532 || (i[LEFT]*q >= pos[LEFT]*q && i[RIGHT]*q >= pos[RIGHT]*q))))
535 while (flip (&rdir) != LEFT);
537 while (flip (&ldir) != LEFT);
543 /* Quantise vertical position (left and right) of beam.
544 Generalisation of [Ross]. */
545 MAKE_SCHEME_CALLBACK (Beam, quantise_position, 1);
547 Beam::quantise_position (SCM smob)
549 Grob *me = unsmob_grob (smob);
551 Interval pos = ly_scm2interval (me->get_grob_property ("positions"));
552 Real y_shift = check_stem_length_f (me, pos);
554 pos = quantise_interval (me, pos, CENTER);
556 me->set_grob_property ("positions", ly_interval2scm (pos));
557 set_stem_lengths (me);
559 pos = ly_scm2interval (me->get_grob_property ("positions"));
561 y_shift = check_stem_length_f (me, pos);
563 Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
565 if (y_shift > half_space / 4)
569 /* for significantly lengthened or shortened stems,
570 request quanting the other way.
572 if (abs (y_shift) > half_space / 2)
573 quant_dir = sign (y_shift) * Directional_element_interface::get (me);
574 pos = quantise_interval (me, pos, (Direction)quant_dir);
577 me->set_grob_property ("positions", ly_interval2scm (pos));
579 return SCM_UNSPECIFIED;
582 MAKE_SCHEME_CALLBACK (Beam, end_after_line_breaking, 1);
584 Beam::end_after_line_breaking (SCM smob)
586 Grob *me = unsmob_grob (smob);
587 set_stem_lengths (me);
589 return SCM_UNSPECIFIED;
593 Beam::calc_stem_y_f (Grob *me, Item* s, Interval pos)
595 int beam_multiplicity = get_multiplicity (me);
596 int stem_multiplicity = (Stem::flag_i (s) - 2) >? 0;
598 SCM space_proc = me->get_grob_property ("space-function");
599 SCM space = gh_call1 (space_proc, gh_int2scm (beam_multiplicity));
601 Real thick = gh_scm2double (me->get_grob_property ("thickness"));
602 Real interbeam_f = gh_scm2double (space);
604 // ugh -> use commonx
605 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
606 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
607 Real dy = pos.delta ();
608 Real stem_y = (dy && dx
609 ? (s->relative_coordinate (0, X_AXIS) - x0) / dx
614 Direction dir = Directional_element_interface::get (me);
615 Direction sdir = Directional_element_interface::get (s);
620 stem_y -= dir * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
622 // huh, why not for first visible?
623 if (Staff_symbol_referencer::staff_symbol_l (s)
624 != Staff_symbol_referencer::staff_symbol_l (last_visible_stem (me)))
625 stem_y += Directional_element_interface::get (me)
626 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
632 /* Make very sure that we don't have stems that are too short.
633 Try our best not to have stems that are too long (think: knees).
635 Optionally (testing): try to lengthen more, to reach more ideal
638 Beam::check_stem_length_f (Grob *me, Interval pos)
642 Direction dir = Directional_element_interface::get (me);
644 Link_array<Item> stems=
645 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
648 int ideal_lengthen_count = 0;
649 Real ideal_lengthen = 0;
650 int ideal_shorten_count = 0;
651 Real ideal_shorten = 0;
653 for (int i=0; i < stems.size (); i++)
656 if (Stem::invisible_b (s))
659 knee |= dir != Directional_element_interface::get (s);
661 Real stem_y = calc_stem_y_f (me, s, pos);
664 Stem_info info = Stem::calc_stem_info (s);
666 shorten = shorten <? info.maxy_f_ - stem_y;
667 lengthen = lengthen >? info.miny_f_ - stem_y;
669 if (info.idealy_f_ - stem_y > 0)
671 ideal_lengthen += info.idealy_f_ - stem_y;
672 ideal_lengthen_count++;
674 else if (info.idealy_f_ - stem_y < 0)
676 ideal_shorten += info.idealy_f_ - stem_y;
677 ideal_shorten_count++;
681 if (lengthen && shorten)
682 me->warning (_ ("weird beam vertical offset"));
684 if (ideal_lengthen_count)
685 lengthen = (ideal_lengthen / ideal_lengthen_count) >? lengthen;
686 if (knee && ideal_shorten_count)
687 shorten = (ideal_shorten / ideal_shorten_count) <? shorten;
689 if (lengthen && shorten)
690 return dir * (lengthen + shorten);
692 return dir * (shorten ? shorten : lengthen);
696 Hmm. At this time, beam position and slope are determined. Maybe,
697 stem directions and length should set to relative to the chord's
698 position of the beam. */
700 Beam::set_stem_lengths (Grob *me)
702 Link_array<Item> stems=
703 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
705 if (stems.size () <= 1)
708 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
709 for (int i=1; i < stems.size (); i++)
710 if (!Stem::invisible_b (stems[i]))
711 common = common->common_refpoint (stems[i], Y_AXIS);
713 Direction dir = Directional_element_interface::get (me);
714 Interval pos = ly_scm2interval (me->get_grob_property ("positions"));
715 Real staff_space = Staff_symbol_referencer::staff_space (me);
716 Real thick = gh_scm2double (me->get_grob_property ("thickness"));
717 bool ps_testing = to_boolean (ly_symbol2scm ("ps-testing"));
718 for (int i=0; i < stems.size (); i++)
721 if (Stem::invisible_b (s))
724 Real stem_y = calc_stem_y_f (me, s, pos);
726 // doesn't play well with dvips
728 if (Stem::get_direction (s) == dir)
729 stem_y += Stem::get_direction (s) * thick / 2;
731 /* caution: stem measures in staff-positions */
732 Real id = me->relative_coordinate (common, Y_AXIS)
733 - stems[i]->relative_coordinate (common, Y_AXIS);
734 Stem::set_stemend (s, (stem_y + id) / staff_space * 2);
739 Beam::set_beaming (Grob *me, Beaming_info_list *beaming)
741 Link_array<Grob> stems=
742 Pointer_group_interface__extract_grobs (me, (Grob *)0, "stems");
745 for (int i=0; i < stems.size (); i++)
749 /* Don't overwrite user override (?) */
750 if (Stem::beam_count (stems[i], d) == -1
751 /* Don't set beaming for outside of outer stems */
752 && ! (d == LEFT && i == 0)
753 && ! (d == RIGHT && i == stems.size () -1))
755 int b = beaming->infos_.elem (i).beams_i_drul_[d];
756 Stem::set_beaming (stems[i], b, d);
759 while (flip (&d) != LEFT);
766 beams to go with one stem.
771 Beam::stem_beams (Grob *me, Item *here, Item *next, Item *prev, Real dydx)
773 // ugh -> use commonx
775 && !(next->relative_coordinate (0, X_AXIS)
776 > here->relative_coordinate (0, X_AXIS)))
778 && !(prev->relative_coordinate (0, X_AXIS)
779 < here->relative_coordinate (0, X_AXIS))))
780 programming_error ("Beams are not left-to-right");
782 int multiplicity = get_multiplicity (me);
784 SCM space_proc = me->get_grob_property ("space-function");
785 SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
787 Real thick = gh_scm2double (me->get_grob_property ("thickness"));
788 Real interbeam_f = gh_scm2double (space);
790 Real bdy = interbeam_f;
796 if (!Stem::first_head (here))
799 int t = Stem::type_i (here);
801 SCM proc = me->get_grob_property ("flag-width-function");
802 SCM result = gh_call1 (proc, gh_int2scm (t));
803 nw_f = gh_scm2double (result);
807 Direction dir = Directional_element_interface::get (me);
809 /* [Tremolo] beams on whole notes may not have direction set? */
811 dir = Directional_element_interface::get (here);
814 /* half beams extending to the left. */
817 int lhalfs= lhalfs = Stem::beam_count (here, LEFT)
818 - Stem::beam_count (prev, RIGHT);
819 int lwholebeams= Stem::beam_count (here, LEFT)
820 <? Stem::beam_count (prev, RIGHT);
822 /* Half beam should be one note-width,
823 but let's make sure two half-beams never touch */
825 // FIXME: TODO (check) stem width / sloped beams
826 Real w = here->relative_coordinate (0, X_AXIS)
827 - prev->relative_coordinate (0, X_AXIS);
828 Real stem_w = gh_scm2double (prev->get_grob_property ("thickness"))
830 * me->paper_l ()->get_var ("stafflinethickness");
834 if (lhalfs) // generates warnings if not
835 a = Lookup::beam (dydx, w + stem_w, thick);
836 a.translate (Offset (-w, -w * dydx));
837 a.translate_axis (-stem_w/2, X_AXIS);
838 for (int j = 0; j < lhalfs; j++)
841 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
842 leftbeams.add_molecule (b);
848 int rhalfs = Stem::beam_count (here, RIGHT)
849 - Stem::beam_count (next, LEFT);
850 int rwholebeams= Stem::beam_count (here, RIGHT)
851 <? Stem::beam_count (next, LEFT);
853 Real w = next->relative_coordinate (0, X_AXIS)
854 - here->relative_coordinate (0, X_AXIS);
856 Real stem_w = gh_scm2double (next->get_grob_property ("thickness"))
858 * me->paper_l ()->get_var ("stafflinethickness");
860 Molecule a = Lookup::beam (dydx, w + stem_w, thick);
861 a.translate_axis (- stem_w/2, X_AXIS);
865 SCM gap = me->get_grob_property ("gap");
866 if (gh_number_p (gap))
868 int gap_i = gh_scm2int ((gap));
869 int nogap = rwholebeams - gap_i;
871 for (; j < nogap; j++)
874 b.translate_axis (-dir * bdy * j, Y_AXIS);
875 rightbeams.add_molecule (b);
877 if (Stem::invisible_b (here))
882 a = Lookup::beam (dydx, w + stem_w, thick);
885 for (; j < rwholebeams; j++)
889 if (Stem::invisible_b (here))
890 // ugh, see chord-tremolo.ly
891 tx = (-dir + 1) / 2 * nw_f * 1.5 + gap_f/4;
894 b.translate (Offset (tx, -dir * bdy * j));
895 rightbeams.add_molecule (b);
900 a = Lookup::beam (dydx, w, thick);
902 for (; j < rwholebeams + rhalfs; j++)
905 b.translate_axis (- dir * bdy * j, Y_AXIS);
906 rightbeams.add_molecule (b);
910 leftbeams.add_molecule (rightbeams);
912 /* Does beam quanting think of the asymetry of beams?
913 Refpoint is on bottom of symbol. (FIXTHAT) --hwn. */
917 MAKE_SCHEME_CALLBACK (Beam, brew_molecule, 1);
919 Beam::brew_molecule (SCM smob)
921 Grob *me =unsmob_grob (smob);
924 if (!gh_pair_p (me->get_grob_property ("stems")))
927 Link_array<Item>stems =
928 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
929 if (visible_stem_count (me))
931 // ugh -> use commonx
932 x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
933 dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
937 x0 = stems[0]->relative_coordinate (0, X_AXIS);
938 dx = stems.top ()->relative_coordinate (0, X_AXIS) - x0;
941 Interval pos = ly_scm2interval (me->get_grob_property ("positions"));
942 Real dy = pos.delta ();
943 Real dydx = dy && dx ? dy/dx : 0;
945 for (int i=0; i < stems.size (); i++)
947 Item *item = stems[i];
948 Item *prev = (i > 0)? stems[i-1] : 0;
949 Item *next = (i < stems.size ()-1) ? stems[i+1] :0;
951 Molecule sb = stem_beams (me, item, next, prev, dydx);
952 Real x = item->relative_coordinate (0, X_AXIS) - x0;
953 sb.translate (Offset (x, x * dydx + pos[LEFT]));
954 mol.add_molecule (sb);
957 mol.translate_axis (x0
958 - dynamic_cast<Spanner*> (me)
959 ->get_bound (LEFT)->relative_coordinate (0, X_AXIS),
962 return mol.smobbed_copy ();
966 Beam::forced_stem_count (Grob *me)
968 Link_array<Item>stems =
969 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
971 for (int i=0; i < stems.size (); i++)
975 if (Stem::invisible_b (s))
978 if (((int)Stem::chord_start_f (s))
979 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
989 use filter and standard list functions.
992 Beam::visible_stem_count (Grob *me)
994 Link_array<Item>stems =
995 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
997 for (int i = stems.size (); i--;)
999 if (!Stem::invisible_b (stems[i]))
1006 Beam::first_visible_stem (Grob *me)
1008 Link_array<Item>stems =
1009 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1011 for (int i = 0; i < stems.size (); i++)
1013 if (!Stem::invisible_b (stems[i]))
1020 Beam::last_visible_stem (Grob *me)
1022 Link_array<Item>stems =
1023 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1024 for (int i = stems.size (); i--;)
1026 if (!Stem::invisible_b (stems[i]))
1035 handle rest under beam (do_post: beams are calculated now)
1036 what about combination of collisions and rest under beam.
1040 rest -> stem -> beam -> interpolate_y_position ()
1042 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1044 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1046 Grob *rest = unsmob_grob (element_smob);
1047 Axis a = (Axis) gh_scm2int (axis);
1049 assert (a == Y_AXIS);
1051 Grob *st = unsmob_grob (rest->get_grob_property ("stem"));
1054 return gh_double2scm (0.0);
1055 Grob *beam = unsmob_grob (stem->get_grob_property ("beam"));
1057 || !Beam::has_interface (beam)
1058 || !Beam::visible_stem_count (beam))
1059 return gh_double2scm (0.0);
1061 // make callback for rest from this.
1062 // todo: make sure this calced already.
1064 // Interval pos = ly_scm2interval (beam->get_grob_property ("positions"));
1065 Interval pos (0, 0);
1066 SCM s = beam->get_grob_property ("positions");
1067 if (gh_pair_p (s) && gh_number_p (ly_car (s)))
1068 pos = ly_scm2interval (s);
1070 Real dy = pos.delta ();
1071 // ugh -> use commonx
1072 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1073 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1074 Real dydx = dy && dx ? dy/dx : 0;
1076 Direction d = Stem::get_direction (stem);
1077 Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + pos[LEFT];
1079 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1082 Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space; // refp??
1085 = gh_scm2double (rest->get_grob_property ("minimum-beam-collision-distance"));
1087 minimum_dist + -d * (beamy - rest_dim) >? 0;
1089 int stafflines = Staff_symbol_referencer::line_count (rest);
1091 // move discretely by half spaces.
1092 int discrete_dist = int (ceil (dist));
1094 // move by whole spaces inside the staff.
1095 if (discrete_dist < stafflines+1)
1096 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
1098 return gh_double2scm (-d * discrete_dist);
1103 Beam::has_interface (Grob *me)
1105 return me->has_interface (ly_symbol2scm ("beam-interface"));