2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
13 - Determine auto knees based on positions if it's set by the user.
15 - the code is littered with * and / staff_space calls for
16 #'positions. Consider moving to real-world coordinates?
18 Problematic issue is user tweaks (user tweaks are in staff-coordinates.)
22 - Stems run to the Y-center of the beam.
24 - beam_translation is the offset between Y centers of the beam.
28 #include "interval-set.hh"
29 #include "directional-element-interface.hh"
32 #include "least-squares.hh"
34 #include "output-def.hh"
36 #include "pointer-group-interface.hh"
37 #include "staff-symbol-referencer.hh"
43 #include "text-interface.hh" // debug output.
44 #include "font-interface.hh" // debug output.
48 Beam::add_stem (Grob *me, Grob *s)
50 if (Stem::get_beam (s))
52 programming_error ("Stem already has beam");
56 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
57 s->set_object ("beam", me->self_scm ());
58 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
62 Beam::get_thickness (Grob *me)
64 return robust_scm2double (me->get_property ("thickness"), 0)
65 * Staff_symbol_referencer::staff_space (me);
68 /* Return the translation between 2 adjoining beams. */
70 Beam::get_beam_translation (Grob *me)
72 SCM func = me->get_property ("space-function");
74 if (ly_is_procedure (func))
76 SCM s = scm_call_2 (func, me->self_scm (), scm_from_int (get_beam_count (me)));
77 return scm_to_double (s);
83 /* Maximum beam_count. */
85 Beam::get_beam_count (Grob *me)
89 extract_grob_set (me, "stems", stems);
90 for (int i = 0; i < stems.size (); i++)
92 Grob *stem = stems[i];
93 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
99 Space return space between beams.
101 MAKE_SCHEME_CALLBACK (Beam, space_function, 2);
103 Beam::space_function (SCM smob, SCM beam_count)
105 Grob *me = unsmob_grob (smob);
107 Real staff_space = Staff_symbol_referencer::staff_space (me);
108 Real line = Staff_symbol_referencer::line_thickness (me);
109 Real thickness = get_thickness (me);
111 Real beam_translation = scm_to_int (beam_count) < 4
112 ? (2 * staff_space + line - thickness) / 2.0
113 : (3 * staff_space + line - thickness) / 3.0;
115 return scm_from_double (beam_translation);
118 /* After pre-processing all directions should be set.
119 Several post-processing routines (stem, slur, script) need stem/beam
121 Currenly, this means that beam has set all stem's directions.
122 [Alternatively, stems could set its own directions, according to
123 their beam, during 'final-pre-processing'.] */
124 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
126 Beam::calc_direction (SCM smob)
128 Grob *me = unsmob_grob (smob);
130 /* Beams with less than 2 two stems don't make much sense, but could happen
135 For a beam that only has one stem, we try to do some disappearance magic:
136 we revert the flag, and move on to The Eternal Engraving Fields. */
138 int count = visible_stem_count (me);
141 extract_grob_set (me, "stems", stems);
142 if (stems.size () == 1)
144 me->warning (_ ("removing beam with less than two stems"));
146 stems[0]->set_object ("beam", SCM_EOL);
149 return SCM_UNSPECIFIED;
151 else if (stems.size () == 0)
154 return SCM_UNSPECIFIED;
158 Direction d = CENTER;
162 d = get_default_dir (me);
163 consider_auto_knees (me);
164 set_stem_directions (me, d);
168 set_stem_shorten (me);
171 return scm_from_int (d);
174 /* We want a maximal number of shared beams, but if there is choice, we
175 * take the one that is closest to the end of the stem. This is for
187 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
191 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
195 for (int i = lslice[-left_dir];
196 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
199 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
201 int k = -right_dir * scm_to_int (scm_car (s)) + i;
202 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
206 if (count >= best_count)
217 Beam::connect_beams (Grob *me)
219 extract_grob_set (me, "stems", stems);
222 last_int.set_empty ();
224 // SCM last_beaming = SCM_EOL;
225 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
226 Direction last_dir = CENTER;
227 for (int i = 0; i < stems.size (); i++)
229 Grob *this_stem = stems[i];
230 SCM this_beaming = this_stem->get_property ("beaming");
232 Direction this_dir = get_grob_direction (this_stem);
233 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
235 int start_point = position_with_maximal_common_beams
236 (last_beaming, this_beaming,
237 last_dir ? last_dir : this_dir,
244 new_slice.set_empty ();
245 SCM s = index_get_cell (this_beaming, d);
246 for (; scm_is_pair (s); s = scm_cdr (s))
249 = start_point - this_dir * scm_to_int (scm_car (s));
251 new_slice.add_point (new_beam_pos);
252 scm_set_car_x (s, scm_from_int (new_beam_pos));
255 while (flip (&d) != LEFT);
257 if (!new_slice.is_empty ())
258 last_int = new_slice;
262 SCM s = scm_cdr (this_beaming);
263 for (; scm_is_pair (s); s = scm_cdr (s))
265 int np = -this_dir * scm_to_int (scm_car (s));
266 scm_set_car_x (s, scm_from_int (np));
267 last_int.add_point (np);
271 if (scm_ilength (scm_cdr (this_beaming)) > 0)
273 last_beaming = this_beaming;
280 I really enjoy spaghetti, but spaghetti should be kept on a plate
281 with a little garlic and olive oil. This is too much.
285 MAKE_SCHEME_CALLBACK (Beam, print, 1);
287 Beam::print (SCM grob)
289 Spanner *me = unsmob_spanner (grob);
291 extract_grob_set (me, "stems", stems);
292 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
294 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
295 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
298 if (visible_stem_count (me))
300 // ugh -> use commonx
301 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
302 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
306 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
307 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
310 SCM posns = me->get_property ("positions");
311 Drul_array<Real> pos;
312 if (!is_number_pair (posns))
314 programming_error ("no beam positions?");
315 pos = Interval (0, 0);
318 pos = ly_scm2realdrul (posns);
320 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
322 Real dy = pos[RIGHT] - pos[LEFT];
323 Real slope = (dy && dx) ? dy / dx : 0;
325 Real thick = get_thickness (me);
326 Real bdy = get_beam_translation (me);
328 SCM last_beaming = SCM_EOL;
329 Real last_xposn = -1;
330 Real last_stem_width = -1;
332 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
335 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
337 for (int i = 0; i <= stems.size (); i++)
339 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
341 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
342 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
343 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
344 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
346 We do the space left of ST, with lfliebertjes pointing to the
347 right from the left stem, and rfliebertjes pointing left from
350 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
351 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
353 Array<int> full_beams;
354 Array<int> lfliebertjes;
355 Array<int> rfliebertjes;
358 scm_is_pair (s); s = scm_cdr (s))
360 int b = scm_to_int (scm_car (s));
361 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
364 lfliebertjes.push (b);
367 scm_is_pair (s); s = scm_cdr (s))
369 int b = scm_to_int (scm_car (s));
370 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
371 rfliebertjes.push (b);
374 Drul_array<Real> break_overshoot
375 = robust_scm2drul (me->get_property ("break-overshoot"),
376 Drul_array<Real> (-0.5, 0.0));
378 Real w = (i > 0 && stem)
379 ? (xposn - last_xposn)
380 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
382 Real stem_offset = 0.0;
385 w += last_stem_width / 2;
386 stem_offset = -last_stem_width / 2;
392 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
393 Stencil whole = Lookup::beam (slope, w, thick, blot);
397 if (scm_is_number (me->get_property ("gap-count")))
399 gap_count = scm_to_int (me->get_property ("gap-count"));
400 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
402 full_beams.sort (default_compare);
404 full_beams.reverse ();
408 for (int j = full_beams.size (); j--;)
415 b.translate_axis (gap_length, X_AXIS);
417 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
418 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
420 the_beam.add_stencil (b);
423 if (lfliebertjes.size () || rfliebertjes.size ())
429 int t = Stem::duration_log (stem);
431 SCM proc = me->get_property ("flag-width-function");
432 SCM result = scm_call_1 (proc, scm_from_int (t));
433 nw_f = scm_to_double (result);
436 nw_f = break_overshoot[RIGHT] / 2;
438 /* Half beam should be one note-width,
439 but let's make sure two half-beams never touch */
443 rw = min (nw_f, ((xposn - last_xposn) / 2));
446 if (me->get_bound (LEFT)->break_status_dir ())
447 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
448 + break_overshoot[LEFT];
454 lw = min (nw_f, ((xposn - last_xposn) / 2));
457 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
459 + break_overshoot[RIGHT];
461 rw += stem_width / 2;
462 lw += last_stem_width / 2;
464 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
465 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
466 for (int j = lfliebertjes.size (); j--;)
469 b.translate_axis (last_xposn - x0 - last_stem_width /2,
471 b.translate_axis (slope * (last_xposn - x0)
472 + bdy * lfliebertjes[j],
474 the_beam.add_stencil (b);
476 for (int j = rfliebertjes.size (); j--;)
479 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
480 b.translate_axis (slope * (xposn - x0 - rw)
481 + bdy * rfliebertjes[j], Y_AXIS);
482 the_beam.add_stencil (b);
487 last_stem_width = stem_width;
488 last_beaming = this_beaming;
491 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
493 the_beam.translate_axis (pos[LEFT], Y_AXIS);
496 SCM quant_score = me->get_property ("quant-score");
497 SCM debug = me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"));
498 if (to_boolean (debug) && scm_is_string (quant_score))
502 This code prints the demerits for each beam. Perhaps this
503 should be switchable for those who want to twiddle with the
507 SCM properties = Font_interface::text_font_alist_chain (me);
509 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
511 Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
512 (me->get_layout ()->self_scm (), properties, quant_score));
513 the_beam.add_at_edge (Y_AXIS, stem_dir, tm, 1.0, 0);
517 return the_beam.smobbed_copy ();
521 Beam::get_default_dir (Grob *me)
523 Drul_array<int> total;
524 total[UP] = total[DOWN] = 0;
525 Drul_array<int> count;
526 count[UP] = count[DOWN] = 0;
528 extract_grob_set (me, "stems", stems);
530 for (int i = 0; i < stems.size (); i++)
533 Direction stem_dir = CENTER;
534 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
535 if (is_direction (stem_dir_scm))
536 stem_dir = to_dir (stem_dir_scm);
538 stem_dir = Stem::get_default_dir (s);
543 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
547 SCM func = me->get_property ("dir-function");
548 SCM s = scm_call_2 (func,
549 scm_cons (scm_from_int (count[UP]),
550 scm_from_int (count[DOWN])),
551 scm_cons (scm_from_int (total[UP]),
552 scm_from_int (total[DOWN])));
554 if (scm_is_number (s) && scm_to_int (s))
557 /* If dir is not determined: get default */
558 return to_dir (me->get_property ("neutral-direction"));
561 /* Set all stems with non-forced direction to beam direction.
562 Urg: non-forced should become `without/with unforced' direction,
563 once stem gets cleaned-up. */
565 Beam::set_stem_directions (Grob *me, Direction d)
567 extract_grob_set (me, "stems", stems);
569 for (int i = 0; i < stems.size (); i++)
573 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
574 if (!to_dir (forcedir))
575 set_grob_direction (s, d);
580 Only try horizontal beams for knees. No reliable detection of
581 anything else is possible here, since we don't know funky-beaming
582 settings, or X-distances (slopes!) People that want sloped
583 knee-beams, should set the directions manually.
588 this routine should take into account the stemlength scoring
589 of a possible knee/nonknee beam.
592 Beam::consider_auto_knees (Grob *me)
594 SCM scm = me->get_property ("auto-knee-gap");
595 if (!scm_is_number (scm))
602 extract_grob_set (me, "stems", stems);
604 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
605 Real staff_space = Staff_symbol_referencer::staff_space (me);
607 Array<Interval> head_extents_array;
608 for (int i = 0; i < stems.size (); i++)
610 Grob *stem = stems[i];
611 if (Stem::is_invisible (stem))
614 Interval head_extents = Stem::head_positions (stem);
615 if (!head_extents.is_empty ())
617 head_extents[LEFT] += -1;
618 head_extents[RIGHT] += 1;
619 head_extents *= staff_space * 0.5;
622 We could subtract beam Y position, but this routine only
623 sets stem directions, a constant shift does not have an
626 head_extents += stem->relative_coordinate (common, Y_AXIS);
628 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
630 Direction stemdir = to_dir (stem->get_property ("direction"));
631 head_extents[-stemdir] = -stemdir * infinity_f;
634 head_extents_array.push (head_extents);
636 gaps.remove_interval (head_extents);
640 Real max_gap_len = 0.0;
642 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
644 Interval gap = gaps.allowed_regions_[i];
647 the outer gaps are not knees.
649 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
652 if (gap.length () >= max_gap_len)
654 max_gap_len = gap.length ();
659 Real beam_translation = get_beam_translation (me);
660 Real beam_thickness = Beam::get_thickness (me);
661 int beam_count = Beam::get_beam_count (me);
662 Real height_of_beams = beam_thickness / 2
663 + (beam_count - 1) * beam_translation;
664 Real threshold = scm_to_double (scm) + height_of_beams;
666 if (max_gap_len > threshold)
669 for (int i = 0; i < stems.size (); i++)
671 Grob *stem = stems[i];
672 if (Stem::is_invisible (stem))
675 Interval head_extents = head_extents_array[j++];
677 Direction d = (head_extents.center () < max_gap.center ())
680 stem->set_property ("direction", scm_from_int (d));
682 head_extents.intersect (max_gap);
683 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
688 /* Set stem's shorten property if unset.
691 take some y-position (chord/beam/nearest?) into account
692 scmify forced-fraction
694 This is done in beam because the shorten has to be uniform over the
698 Beam::set_stem_shorten (Grob *me)
701 shortening looks silly for x staff beams
706 Real forced_fraction = 1.0 * forced_stem_count (me)
707 / visible_stem_count (me);
709 int beam_count = get_beam_count (me);
711 SCM shorten_list = me->get_property ("beamed-stem-shorten");
712 if (shorten_list == SCM_EOL)
715 Real staff_space = Staff_symbol_referencer::staff_space (me);
718 = robust_list_ref (beam_count -1, shorten_list);
719 Real shorten = scm_to_double (shorten_elt) * staff_space;
721 shorten *= forced_fraction;
724 me->set_property ("shorten", scm_from_double (shorten));
727 MAKE_SCHEME_CALLBACK (Beam, calc_positions, 1);
729 Beam::calc_positions (SCM smob)
731 Grob *me = unsmob_grob (smob);
735 (void) me->get_property ("direction");
737 SCM posns = scm_cons (SCM_BOOL_F, SCM_BOOL_F);
738 me->set_property ("positions", posns);
740 SCM callbacks = me->get_property ("position-callbacks");
741 for (SCM i = callbacks; scm_is_pair (i); i = scm_cdr (i))
742 scm_call_1 (scm_car (i), me->self_scm ());
745 TODO: move this in separate calc function.
747 set_stem_lengths (me);
748 return SCM_UNSPECIFIED;
753 set_minimum_dy (Grob *me, Real *dy)
758 If dy is smaller than the smallest quant, we
759 get absurd direction-sign penalties.
762 Real ss = Staff_symbol_referencer::staff_space (me);
763 Real thickness = Beam::get_thickness (me) / ss;
764 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
765 Real sit = (thickness - slt) / 2;
767 Real hang = 1.0 - (thickness - slt) / 2;
769 *dy = sign (*dy) * max (fabs (*dy),
770 min (min (sit, inter), hang));
775 Compute a first approximation to the beam slope.
777 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
779 Beam::least_squares (SCM smob)
781 Grob *me = unsmob_grob (smob);
783 int count = visible_stem_count (me);
788 me->set_property ("positions", ly_interval2scm (pos));
789 return SCM_UNSPECIFIED;
793 extract_grob_set (me, "stems", stems);
794 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
795 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
797 Real my_y = me->relative_coordinate (commony, Y_AXIS);
799 Grob *fvs = first_visible_stem (me);
800 Grob *lvs = last_visible_stem (me);
802 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
803 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
804 Stem::get_stem_info (lvs).ideal_y_
805 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
807 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
808 for (int i = 0; i < stems.size (); i++)
812 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
815 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
823 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
824 Stem::chord_start_y (last_visible_stem (me)));
826 /* Simple beams (2 stems) on middle line should be allowed to be
829 However, if both stems reach middle line,
830 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
832 For that case, we apply artificial slope */
833 if (!ideal[LEFT] && chord.delta () && count == 2)
836 Direction d = (Direction) (sign (chord.delta ()) * UP);
837 pos[d] = get_thickness (me) / 2;
844 For broken beams this doesn't work well. In this case, the
845 slope esp. of the first part of a broken beam should predict
846 where the second part goes.
848 me->set_property ("least-squares-dy",
849 scm_from_double (pos[RIGHT] - pos[LEFT]));
853 Array<Offset> ideals;
854 for (int i = 0; i < stems.size (); i++)
857 if (Stem::is_invisible (s))
859 ideals.push (Offset (x_posns[i],
860 Stem::get_stem_info (s).ideal_y_
861 + s->relative_coordinate (commony, Y_AXIS)
865 minimise_least_squares (&slope, &y, ideals);
869 set_minimum_dy (me, &dy);
870 me->set_property ("least-squares-dy", scm_from_double (dy));
871 pos = Interval (y, (y + dy));
875 "position" is relative to the staff.
877 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
879 me->set_property ("positions", ly_interval2scm (pos));
881 return SCM_UNSPECIFIED;
885 We can't combine with previous function, since check concave and
886 slope damping comes first.
888 TODO: we should use the concaveness to control the amount of damping
891 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
893 Beam::shift_region_to_valid (SCM grob)
895 Grob *me = unsmob_grob (grob);
900 extract_grob_set (me, "stems", stems);
901 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
902 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
904 Grob *fvs = first_visible_stem (me);
907 return SCM_UNSPECIFIED;
909 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
910 for (int i = 0; i < stems.size (); i++)
914 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
918 Grob *lvs = last_visible_stem (me);
920 return SCM_UNSPECIFIED;
922 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
924 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
926 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
928 Real dy = pos[RIGHT] - pos[LEFT];
930 Real slope = dx ? (dy / dx) : 0.0;
933 Shift the positions so that we have a chance of finding good
934 quants (i.e. no short stem failures.)
936 Interval feasible_left_point;
937 feasible_left_point.set_full ();
938 for (int i = 0; i < stems.size (); i++)
941 if (Stem::is_invisible (s))
944 Direction d = get_grob_direction (s);
947 = Stem::get_stem_info (s).shortest_y_
948 - slope * x_posns [i];
951 left_y is now relative to the stem S. We want relative to
952 ourselves, so translate:
955 += + s->relative_coordinate (commony, Y_AXIS)
956 - me->relative_coordinate (commony, Y_AXIS);
962 feasible_left_point.intersect (flp);
965 if (feasible_left_point.is_empty ())
966 warning (_ ("no viable initial configuration found: may not find good beam slope"));
967 else if (!feasible_left_point.contains (y))
969 const int REGION_SIZE = 2; // UGH UGH
970 if (isinf (feasible_left_point[DOWN]))
971 y = feasible_left_point[UP] - REGION_SIZE;
972 else if (isinf (feasible_left_point[UP]))
973 y = feasible_left_point[DOWN]+ REGION_SIZE;
975 y = feasible_left_point.center ();
978 pos = Drul_array<Real> (y, (y + dy));
979 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
981 me->set_property ("positions", ly_interval2scm (pos));
982 return SCM_UNSPECIFIED;
985 /* This neat trick is by Werner Lemberg,
986 damped = tanh (slope)
987 corresponds with some tables in [Wanske] CHECKME */
988 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
990 Beam::slope_damping (SCM smob)
992 Grob *me = unsmob_grob (smob);
994 if (visible_stem_count (me) <= 1)
995 return SCM_UNSPECIFIED;
997 SCM s = me->get_property ("damping");
998 Real damping = scm_to_double (s);
1002 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
1003 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1005 Real dy = pos[RIGHT] - pos[LEFT];
1007 Grob *fvs = first_visible_stem (me);
1008 Grob *lvs = last_visible_stem (me);
1010 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1012 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1013 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1015 Real slope = dy && dx ? dy / dx : 0;
1017 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1019 slope = 0.6 * tanh (slope) / (damping + concaveness);
1021 Real damped_dy = slope * dx;
1023 set_minimum_dy (me, &damped_dy);
1025 pos[LEFT] += (dy - damped_dy) / 2;
1026 pos[RIGHT] -= (dy - damped_dy) / 2;
1028 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1030 me->set_property ("positions", ly_interval2scm (pos));
1032 return SCM_UNSPECIFIED;
1036 Report slice containing the numbers that are both in (car BEAMING)
1040 where_are_the_whole_beams (SCM beaming)
1044 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1046 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1048 l.add_point (scm_to_int (scm_car (s)));
1054 /* Return the Y position of the stem-end, given the Y-left, Y-right
1055 in POS for stem S. This Y position is relative to S. */
1057 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1059 Drul_array<Real> pos, bool french)
1061 Real beam_translation = get_beam_translation (me);
1063 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1064 Real dy = pos[RIGHT] - pos[LEFT];
1066 Real stem_y_beam0 = (dy && dx
1071 Direction my_dir = get_grob_direction (s);
1072 SCM beaming = s->get_property ("beaming");
1074 Real stem_y = stem_y_beam0;
1077 Slice bm = where_are_the_whole_beams (beaming);
1078 if (!bm.is_empty ())
1079 stem_y += beam_translation * bm[-my_dir];
1083 Slice bm = Stem::beam_multiplicity (s);
1084 if (!bm.is_empty ())
1085 stem_y += bm[my_dir] * beam_translation;
1088 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1089 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1095 Hmm. At this time, beam position and slope are determined. Maybe,
1096 stem directions and length should set to relative to the chord's
1097 position of the beam. */
1099 Beam::set_stem_lengths (Grob *me)
1101 extract_grob_set (me, "stems", stems);
1106 for (int a = 2; a--;)
1107 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1109 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1110 Real staff_space = Staff_symbol_referencer::staff_space (me);
1111 scale_drul (&pos, staff_space);
1115 if (scm_is_number (me->get_property ("gap-count"))
1116 &&scm_to_int (me->get_property ("gap-count")))
1119 thick = get_thickness (me);
1122 // ugh -> use commonx
1123 Grob *fvs = first_visible_stem (me);
1124 Grob *lvs = last_visible_stem (me);
1126 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1127 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1129 for (int i = 0; i < stems.size (); i++)
1132 if (Stem::is_invisible (s))
1135 bool french = to_boolean (s->get_property ("french-beaming"));
1136 Real stem_y = calc_stem_y (me, s, common,
1138 pos, french && s != lvs && s!= fvs);
1141 Make the stems go up to the end of the beam. This doesn't matter
1142 for normal beams, but for tremolo beams it looks silly otherwise.
1145 stem_y += thick * 0.5 * get_grob_direction (s);
1147 Stem::set_stemend (s, 2 * stem_y / staff_space);
1152 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1154 extract_grob_set (me, "stems", stems);
1157 for (int i = 0; i < stems.size (); i++)
1160 Don't overwrite user settings.
1165 Grob *stem = stems[i];
1166 SCM beaming_prop = stem->get_property ("beaming");
1167 if (beaming_prop == SCM_EOL
1168 || index_get_cell (beaming_prop, d) == SCM_EOL)
1170 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1172 && i < stems.size () -1
1173 && Stem::is_invisible (stem))
1174 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1176 Stem::set_beaming (stem, b, d);
1179 while (flip (&d) != LEFT);
1184 Beam::forced_stem_count (Grob *me)
1186 extract_grob_set (me, "stems", stems);
1189 for (int i = 0; i < stems.size (); i++)
1193 if (Stem::is_invisible (s))
1196 /* I can imagine counting those boundaries as a half forced stem,
1197 but let's count them full for now. */
1198 if (abs (Stem::chord_start_y (s)) > 0.1
1199 && (get_grob_direction (s) != Stem::get_default_dir (s)))
1206 Beam::visible_stem_count (Grob *me)
1208 extract_grob_set (me, "stems", stems);
1210 for (int i = stems.size (); i--;)
1212 if (!Stem::is_invisible (stems[i]))
1219 Beam::first_visible_stem (Grob *me)
1221 extract_grob_set (me, "stems", stems);
1223 for (int i = 0; i < stems.size (); i++)
1225 if (!Stem::is_invisible (stems[i]))
1232 Beam::last_visible_stem (Grob *me)
1234 extract_grob_set (me, "stems", stems);
1236 for (int i = stems.size (); i--;)
1238 if (!Stem::is_invisible (stems[i]))
1247 handle rest under beam (do_post: beams are calculated now)
1248 what about combination of collisions and rest under beam.
1252 rest -> stem -> beam -> interpolate_y_position ()
1254 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1256 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1258 Grob *rest = unsmob_grob (element_smob);
1261 if (scm_is_number (rest->get_property ("staff-position")))
1262 return scm_from_int (0);
1264 assert (scm_to_int (axis) == Y_AXIS);
1266 Grob *st = unsmob_grob (rest->get_object ("stem"));
1269 return scm_from_double (0.0);
1270 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1272 || !Beam::has_interface (beam)
1273 || !Beam::visible_stem_count (beam))
1274 return scm_from_double (0.0);
1276 Drul_array<Real> pos (0, 0);
1277 SCM s = beam->get_property ("positions");
1278 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1279 pos = ly_scm2interval (s);
1281 programming_error ("positions property should always be pair of numbers.");
1283 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1285 scale_drul (&pos, staff_space);
1287 Real dy = pos[RIGHT] - pos[LEFT];
1289 // ugh -> use commonx
1290 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1291 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1292 Real slope = dy && dx ? dy / dx : 0;
1294 Direction d = get_grob_direction (stem);
1295 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1297 Real beam_translation = get_beam_translation (beam);
1298 Real beam_thickness = Beam::get_thickness (beam);
1301 TODO: this is not strictly correct for 16th knee beams.
1304 = Stem::beam_multiplicity (stem).length () + 1;
1306 Real height_of_my_beams = beam_thickness / 2
1307 + (beam_count - 1) * beam_translation;
1308 Real beam_y = stem_y - d * height_of_my_beams;
1310 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1312 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1313 Real minimum_distance
1314 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1315 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1317 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1319 shift /= staff_space;
1320 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1322 /* Always move discretely by half spaces */
1323 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1325 /* Inside staff, move by whole spaces*/
1326 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1328 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1330 shift = ceil (fabs (shift)) * sign (shift);
1332 return scm_from_double (staff_space * shift);
1336 Beam::is_knee (Grob *me)
1338 SCM k = me->get_property ("knee");
1339 if (scm_is_bool (k))
1340 return ly_scm2bool (k);
1344 extract_grob_set (me, "stems", stems);
1345 for (int i = stems.size (); i--;)
1347 Direction dir = get_grob_direction (stems[i]);
1356 me->set_property ("knee", ly_bool2scm (knee));
1362 Beam::get_direction_beam_count (Grob *me, Direction d)
1364 extract_grob_set (me, "stems", stems);
1367 for (int i = stems.size (); i--;)
1370 Should we take invisible stems into account?
1372 if (get_grob_direction (stems[i]) == d)
1373 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1379 ADD_INTERFACE (Beam,
1383 "The @code{thickness} property is the weight of beams, "
1384 "measured in staffspace. The @code{direction} property is not user-serviceable. Use "
1385 "the @code{direction} property of @code{Stem} instead. "
1391 "beamed-stem-shorten "
1399 "flag-width-function "
1405 "neutral-direction "
1406 "position-callbacks "