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.
27 #include <math.h> // tanh.
30 #include "interval-set.hh"
31 #include "directional-element-interface.hh"
34 #include "least-squares.hh"
36 #include "output-def.hh"
38 #include "group-interface.hh"
39 #include "staff-symbol-referencer.hh"
45 #include "text-interface.hh" // debug output.
46 #include "font-interface.hh" // debug output.
50 Beam::add_stem (Grob *me, Grob *s)
52 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
54 s->add_dependency (me);
56 assert (!Stem::get_beam (s));
57 s->set_property ("beam", me->self_scm ());
59 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
63 Beam::get_thickness (Grob *me)
65 return robust_scm2double (me->get_property ("thickness"), 0)
66 * Staff_symbol_referencer::staff_space (me);
69 /* Return the translation between 2 adjoining beams. */
71 Beam::get_beam_translation (Grob *me)
73 SCM func = me->get_property ("space-function");
75 if (ly_c_procedure_p (func))
77 SCM s = scm_call_2 (func, me->self_scm (), scm_int2num (get_beam_count (me)));
78 return scm_to_double (s);
86 /* Maximum beam_count. */
88 Beam::get_beam_count (Grob *me)
91 for (SCM s = me->get_property ("stems"); scm_is_pair (s); s = scm_cdr (s))
93 Grob *stem = unsmob_grob (scm_car (s));
94 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
100 Space return space between beams.
102 MAKE_SCHEME_CALLBACK (Beam, space_function, 2);
104 Beam::space_function (SCM smob, SCM beam_count)
106 Grob *me = unsmob_grob (smob);
108 Real staff_space = Staff_symbol_referencer::staff_space (me);
109 Real line = Staff_symbol_referencer::line_thickness (me);
110 Real thickness = get_thickness (me);
112 Real beam_translation = scm_to_int (beam_count) < 4
113 ? (2 * staff_space + line - thickness) / 2.0
114 : (3 * staff_space + line - thickness) / 3.0;
116 return scm_make_real (beam_translation);
119 /* After pre-processing all directions should be set.
120 Several post-processing routines (stem, slur, script) need stem/beam
122 Currenly, this means that beam has set all stem's directions.
123 [Alternatively, stems could set its own directions, according to
124 their beam, during 'final-pre-processing'.] */
125 MAKE_SCHEME_CALLBACK (Beam, before_line_breaking, 1);
127 Beam::before_line_breaking (SCM smob)
129 Grob *me = unsmob_grob (smob);
131 /* Beams with less than 2 two stems don't make much sense, but could happen
136 For a beam that only has one stem, we try to do some disappearance magic:
137 we revert the flag, and move on to The Eternal Engraving Fields. */
139 int count = visible_stem_count (me);
142 SCM stems = me->get_property ("stems");
143 if (scm_ilength (stems) == 1)
145 me->warning (_ ("removing beam with less than two stems"));
147 unsmob_grob (scm_car (stems))->set_property ("beam", SCM_EOL);
150 return SCM_UNSPECIFIED;
152 else if (scm_ilength (stems) == 0)
155 return SCM_UNSPECIFIED;
160 Direction d = get_default_dir (me);
162 consider_auto_knees (me);
163 set_stem_directions (me, d);
167 set_stem_shorten (me);
173 /* We want a maximal number of shared beams, but if there is choice, we
174 * take the one that is closest to the end of the stem. This is for
186 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
190 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
194 for (int i = lslice[-left_dir];
195 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
198 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
200 int k = -right_dir * scm_to_int (scm_car (s)) + i;
201 if (scm_c_memq (scm_int2num (k), left_beaming) != SCM_BOOL_F)
205 if (count >= best_count)
216 Beam::connect_beams (Grob *me)
218 Link_array<Grob> stems
219 = extract_grob_array (me, ly_symbol2scm ("stems"));
222 last_int.set_empty ();
223 SCM last_beaming = SCM_EOL;
224 Direction last_dir = CENTER;
225 for (int i = 0; i < stems.size (); i++)
227 Grob *this_stem = stems[i];
228 SCM this_beaming = this_stem->get_property ("beaming");
230 Direction this_dir = get_grob_direction (this_stem);
231 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
233 int start_point = position_with_maximal_common_beams
234 (last_beaming, this_beaming,
241 if (d == RIGHT && i == stems.size () - 1)
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_int2num (new_beam_pos));
255 while (flip (&d) != LEFT);
257 if (!new_slice.is_empty ())
258 last_int = new_slice;
262 scm_set_car_x (this_beaming, SCM_EOL);
263 SCM s = scm_cdr (this_beaming);
264 for (; scm_is_pair (s); s = scm_cdr (s))
266 int np = -this_dir * scm_to_int (scm_car (s));
267 scm_set_car_x (s, scm_int2num (np));
268 last_int.add_point (np);
272 if (i == stems.size () -1)
274 scm_set_cdr_x (this_beaming, SCM_EOL);
277 if (scm_ilength (scm_cdr (this_beaming)) > 0)
279 last_beaming = this_beaming;
286 TODO: should not make beams per stem, but per Y-level.
288 MAKE_SCHEME_CALLBACK (Beam, print, 1);
290 Beam::print (SCM grob)
292 Spanner *me = unsmob_spanner (grob);
295 Link_array<Grob> stems
296 = extract_grob_array (me, ly_symbol2scm ("stems"));
297 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
299 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
300 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
303 if (visible_stem_count (me))
305 // ugh -> use commonx
306 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
307 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
311 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
312 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
315 SCM posns = me->get_property ("positions");
316 Drul_array<Real> pos;
317 if (!is_number_pair (posns))
319 programming_error ("no beam positions?");
320 pos = Interval (0, 0);
323 pos = ly_scm2realdrul (posns);
325 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
327 Real dy = pos[RIGHT] - pos[LEFT];
328 Real slope = (dy && dx) ? dy / dx : 0;
330 Real thick = get_thickness (me);
331 Real bdy = get_beam_translation (me);
333 SCM last_beaming = SCM_EOL;
334 Real last_xposn = -1;
335 Real last_stem_width = -1;
337 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
340 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
342 for (int i = 0; i <= stems.size (); i++)
344 Grob *st = (i < stems.size ()) ? stems[i] : 0;
346 SCM this_beaming = st ? st->get_property ("beaming") : SCM_EOL;
347 Real xposn = st ? st->relative_coordinate (xcommon, X_AXIS) : 0.0;
348 Real stem_width = st ? robust_scm2double (st->get_property ("thickness"), 1.0) * lt : 0;
349 Direction stem_dir = st ? to_dir (st->get_property ("direction")) : CENTER;
351 We do the space left of ST, with lfliebertjes pointing to the
352 right from the left stem, and rfliebertjes pointing left from
355 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
356 SCM right = st ? scm_car (this_beaming) : SCM_EOL;
358 Array<int> full_beams;
359 Array<int> lfliebertjes;
360 Array<int> rfliebertjes;
363 scm_is_pair (s); s = scm_cdr (s))
365 int b = scm_to_int (scm_car (s));
366 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
372 lfliebertjes.push (b);
376 scm_is_pair (s); s = scm_cdr (s))
378 int b = scm_to_int (scm_car (s));
379 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
381 rfliebertjes.push (b);
386 how much to stick out for beams across linebreaks
388 Real break_overshoot = 3.0;
389 Real w = (i > 0 && st) ? (xposn - last_xposn) : break_overshoot;
391 Real stem_offset = 0.0;
394 w += last_stem_width / 2;
395 stem_offset = -last_stem_width / 2;
401 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
402 Stencil whole = Lookup::beam (slope, w, thick, blot);
406 if (scm_is_number (me->get_property ("gap-count")))
408 gap_count = scm_to_int (me->get_property ("gap-count"));
409 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
411 full_beams.sort (default_compare);
413 full_beams.reverse ();
417 for (int j = full_beams.size (); j--;)
424 b.translate_axis (gap_length, X_AXIS);
426 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
427 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
429 the_beam.add_stencil (b);
432 if (lfliebertjes.size () || rfliebertjes.size ())
438 int t = Stem::duration_log (st);
440 SCM proc = me->get_property ("flag-width-function");
441 SCM result = scm_call_1 (proc, scm_int2num (t));
442 nw_f = scm_to_double (result);
445 nw_f = break_overshoot / 2;
447 /* Half beam should be one note-width,
448 but let's make sure two half-beams never touch */
452 rw = min (nw_f, ((xposn - last_xposn) / 2));
455 TODO: 0.5 is a guess.
457 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
461 lw = min (nw_f, ((xposn - last_xposn) / 2));
463 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
466 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
467 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
468 for (int j = lfliebertjes.size (); j--;)
471 b.translate_axis (last_xposn - x0, X_AXIS);
472 b.translate_axis (slope * (last_xposn - x0) + bdy * lfliebertjes[j], Y_AXIS);
473 the_beam.add_stencil (b);
475 for (int j = rfliebertjes.size (); j--;)
478 b.translate_axis (xposn - x0 - rw, X_AXIS);
479 b.translate_axis (slope * (xposn - x0 -rw) + bdy * rfliebertjes[j], Y_AXIS);
480 the_beam.add_stencil (b);
485 last_stem_width = stem_width;
486 last_beaming = this_beaming;
489 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS), X_AXIS);
490 the_beam.translate_axis (pos[LEFT], Y_AXIS);
493 SCM quant_score = me->get_property ("quant-score");
494 if (to_boolean (me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting")))
495 && scm_is_string (quant_score))
499 This code prints the demerits for each beam. Perhaps this
500 should be switchable for those who want to twiddle with the
504 SCM properties = Font_interface::text_font_alist_chain (me);
506 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
508 Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
509 (me->get_layout ()->self_scm (), properties, quant_score));
510 the_beam.add_at_edge (Y_AXIS, stem_dir, tm, 1.0, 0);
514 return the_beam.smobbed_copy ();
518 Beam::get_default_dir (Grob *me)
520 Drul_array<int> total;
521 total[UP] = total[DOWN] = 0;
522 Drul_array<int> count;
523 count[UP] = count[DOWN] = 0;
526 Link_array<Grob> stems
527 = extract_grob_array (me, ly_symbol2scm ("stems"));
529 for (int i = 0; i < stems.size (); i++)
533 Direction sd = get_grob_direction (s);
535 int center_distance = max (int (- d * Stem::head_positions (s) [-d]), 0);
536 int current = sd ? (1 + d * sd) / 2 : center_distance;
544 while (flip (&d) != DOWN);
546 SCM func = me->get_property ("dir-function");
547 SCM s = scm_call_2 (func,
548 scm_cons (scm_int2num (count[UP]),
549 scm_int2num (count[DOWN])),
550 scm_cons (scm_int2num (total[UP]),
551 scm_int2num (total[DOWN])));
553 if (scm_is_number (s) && scm_to_int (s))
556 /* If dir is not determined: get default */
557 return to_dir (me->get_property ("neutral-direction"));
560 /* Set all stems with non-forced direction to beam direction.
561 Urg: non-forced should become `without/with unforced' direction,
562 once stem gets cleaned-up. */
564 Beam::set_stem_directions (Grob *me, Direction d)
566 Link_array<Grob> stems
567 = extract_grob_array (me, ly_symbol2scm ("stems"));
569 for (int i = 0; i < stems.size (); i++)
573 SCM forcedir = s->get_property ("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 Link_array<Grob> stems
603 = extract_grob_array (me, ly_symbol2scm ("stems"));
605 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
606 Real staff_space = Staff_symbol_referencer::staff_space (me);
608 Array<Interval> head_extents_array;
609 for (int i = 0; i < stems.size (); i++)
611 Grob *stem = stems[i];
612 if (Stem::is_invisible (stem))
615 Interval head_extents = Stem::head_positions (stem);
616 if (!head_extents.is_empty ())
618 head_extents[LEFT] += -1;
619 head_extents[RIGHT] += 1;
620 head_extents *= staff_space * 0.5;
623 We could subtract beam Y position, but this routine only
624 sets stem directions, a constant shift does not have an
627 head_extents += stem->relative_coordinate (common, Y_AXIS);
629 if (to_dir (stem->get_property ("direction")))
631 Direction stemdir = to_dir (stem->get_property ("direction"));
632 head_extents[-stemdir] = -stemdir * infinity_f;
635 head_extents_array.push (head_extents);
637 gaps.remove_interval (head_extents);
641 Real max_gap_len = 0.0;
643 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
645 Interval gap = gaps.allowed_regions_[i];
648 the outer gaps are not knees.
650 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
653 if (gap.length () >= max_gap_len)
655 max_gap_len = gap.length ();
660 Real beam_translation = get_beam_translation (me);
661 Real beam_thickness = Beam::get_thickness (me);
662 int beam_count = Beam::get_beam_count (me);
663 Real height_of_beams = beam_thickness / 2
664 + (beam_count - 1) * beam_translation;
665 Real threshold = scm_to_double (scm) + height_of_beams;
667 if (max_gap_len > threshold)
670 for (int i = 0; i < stems.size (); i++)
672 Grob *stem = stems[i];
673 if (Stem::is_invisible (stem))
676 Interval head_extents = head_extents_array[j++];
678 Direction d = (head_extents.center () < max_gap.center ())
681 stem->set_property ("direction", scm_int2num (d));
683 head_extents.intersect (max_gap);
684 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
689 /* Set stem's shorten property if unset.
692 take some y-position (chord/beam/nearest?) into account
693 scmify forced-fraction
695 This is done in beam because the shorten has to be uniform over the
699 Beam::set_stem_shorten (Grob *me)
702 shortening looks silly for x staff beams
707 Real forced_fraction = 1.0 * forced_stem_count (me)
708 / visible_stem_count (me);
710 int beam_count = get_beam_count (me);
712 SCM shorten_list = me->get_property ("beamed-stem-shorten");
713 if (shorten_list == SCM_EOL)
716 Real staff_space = Staff_symbol_referencer::staff_space (me);
719 = robust_list_ref (beam_count -1, shorten_list);
720 Real shorten_f = scm_to_double (shorten_elt) * staff_space;
722 /* your similar cute comment here */
723 shorten_f *= forced_fraction;
726 me->set_property ("shorten", scm_make_real (shorten_f));
729 /* Call list of y-dy-callbacks, that handle setting of
732 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
734 Beam::after_line_breaking (SCM smob)
736 Grob *me = unsmob_grob (smob);
739 return SCM_UNSPECIFIED;
743 Beam::position_beam (Grob *me)
747 if (to_boolean (me->get_property ("positioning-done")))
750 me->set_property ("positioning-done", SCM_BOOL_T);
752 /* Copy to mutable list. */
753 SCM s = ly_deep_copy (me->get_property ("positions"));
754 me->set_property ("positions", s);
756 if (scm_car (s) == SCM_BOOL_F)
758 // one wonders if such genericity is necessary --hwn.
759 SCM callbacks = me->get_property ("position-callbacks");
760 for (SCM i = callbacks; scm_is_pair (i); i = scm_cdr (i))
761 scm_call_1 (scm_car (i), me->self_scm ());
764 set_stem_lengths (me);
768 set_minimum_dy (Grob *me, Real *dy)
773 If dy is smaller than the smallest quant, we
774 get absurd direction-sign penalties.
777 Real ss = Staff_symbol_referencer::staff_space (me);
778 Real thickness = Beam::get_thickness (me) / ss;
779 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
780 Real sit = (thickness - slt) / 2;
782 Real hang = 1.0 - (thickness - slt) / 2;
784 *dy = sign (*dy) * max (fabs (*dy),
785 min (min (sit, inter), hang));
790 Compute a first approximation to the beam slope.
792 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
794 Beam::least_squares (SCM smob)
796 Grob *me = unsmob_grob (smob);
798 int count = visible_stem_count (me);
803 me->set_property ("positions", ly_interval2scm (pos));
804 return SCM_UNSPECIFIED;
808 Link_array<Grob> stems
809 = extract_grob_array (me, ly_symbol2scm ("stems"));
810 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
811 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
813 Real my_y = me->relative_coordinate (commony, Y_AXIS);
815 Grob *fvs = first_visible_stem (me);
816 Grob *lvs = last_visible_stem (me);
818 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
819 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
820 Stem::get_stem_info (lvs).ideal_y_
821 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
823 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
824 for (int i = 0; i < stems.size (); i++)
828 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
831 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
839 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
840 Stem::chord_start_y (last_visible_stem (me)));
842 /* Simple beams (2 stems) on middle line should be allowed to be
845 However, if both stems reach middle line,
846 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
848 For that case, we apply artificial slope */
849 if (!ideal[LEFT] && chord.delta () && count == 2)
852 Direction d = (Direction) (sign (chord.delta ()) * UP);
853 pos[d] = get_thickness (me) / 2;
862 For broken beams this doesn't work well. In this case, the
863 slope esp. of the first part of a broken beam should predict
864 where the second part goes.
866 me->set_property ("least-squares-dy",
867 scm_make_real (pos[RIGHT] - pos[LEFT]));
871 Array<Offset> ideals;
872 for (int i = 0; i < stems.size (); i++)
875 if (Stem::is_invisible (s))
877 ideals.push (Offset (x_posns[i],
878 Stem::get_stem_info (s).ideal_y_
879 + s->relative_coordinate (commony, Y_AXIS)
883 minimise_least_squares (&slope, &y, ideals);
887 set_minimum_dy (me, &dy);
888 me->set_property ("least-squares-dy", scm_make_real (dy));
889 pos = Interval (y, (y + dy));
893 "position" is relative to the staff.
895 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
897 me->set_property ("positions", ly_interval2scm (pos));
899 return SCM_UNSPECIFIED;
903 We can't combine with previous function, since check concave and
904 slope damping comes first.
906 TODO: we should use the concaveness to control the amount of damping
909 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
911 Beam::shift_region_to_valid (SCM grob)
913 Grob *me = unsmob_grob (grob);
918 Link_array<Grob> stems
919 = extract_grob_array (me, ly_symbol2scm ("stems"));
920 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
921 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
923 Grob *fvs = first_visible_stem (me);
926 return SCM_UNSPECIFIED;
928 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
929 for (int i = 0; i < stems.size (); i++)
933 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
937 Grob *lvs = last_visible_stem (me);
939 return SCM_UNSPECIFIED;
941 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
943 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
945 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
947 Real dy = pos[RIGHT] - pos[LEFT];
949 Real slope = dy / dx;
952 Shift the positions so that we have a chance of finding good
953 quants (i.e. no short stem failures.)
955 Interval feasible_left_point;
956 feasible_left_point.set_full ();
957 for (int i = 0; i < stems.size (); i++)
960 if (Stem::is_invisible (s))
963 Direction d = Stem::get_direction (s);
966 = Stem::get_stem_info (s).shortest_y_
967 - slope * x_posns [i];
970 left_y is now relative to the stem S. We want relative to
971 ourselves, so translate:
974 += + s->relative_coordinate (commony, Y_AXIS)
975 - me->relative_coordinate (commony, Y_AXIS);
981 feasible_left_point.intersect (flp);
984 if (feasible_left_point.is_empty ())
985 warning (_ ("no viable initial configuration found: may not find good beam slope"));
986 else if (!feasible_left_point.contains (y))
988 if (isinf (feasible_left_point[DOWN]))
989 y = feasible_left_point[UP] - REGION_SIZE;
990 else if (isinf (feasible_left_point[UP]))
991 y = feasible_left_point[DOWN]+ REGION_SIZE;
993 y = feasible_left_point.center ();
996 pos = Drul_array<Real> (y, (y + dy));
997 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
999 me->set_property ("positions", ly_interval2scm (pos));
1000 return SCM_UNSPECIFIED;
1003 /* This neat trick is by Werner Lemberg,
1004 damped = tanh (slope)
1005 corresponds with some tables in [Wanske] CHECKME */
1006 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
1008 Beam::slope_damping (SCM smob)
1010 Grob *me = unsmob_grob (smob);
1012 if (visible_stem_count (me) <= 1)
1013 return SCM_UNSPECIFIED;
1015 SCM s = me->get_property ("damping");
1016 Real damping = scm_to_double (s);
1020 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
1021 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1023 Real dy = pos[RIGHT] - pos[LEFT];
1025 Grob *fvs = first_visible_stem (me);
1026 Grob *lvs = last_visible_stem (me);
1028 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1030 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1031 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1033 Real slope = dy && dx ? dy / dx : 0;
1035 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1037 slope = 0.6 * tanh (slope) / (damping + concaveness);
1039 Real damped_dy = slope * dx;
1041 set_minimum_dy (me, &damped_dy);
1043 pos[LEFT] += (dy - damped_dy) / 2;
1044 pos[RIGHT] -= (dy - damped_dy) / 2;
1046 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1048 me->set_property ("positions", ly_interval2scm (pos));
1050 return SCM_UNSPECIFIED;
1054 Report slice containing the numbers that are both in (car BEAMING)
1058 where_are_the_whole_beams (SCM beaming)
1062 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1064 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1066 l.add_point (scm_to_int (scm_car (s)));
1072 /* Return the Y position of the stem-end, given the Y-left, Y-right
1073 in POS for stem S. This Y position is relative to S. */
1075 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1077 Drul_array<Real> pos, bool french)
1079 Real beam_translation = get_beam_translation (me);
1081 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1082 Real dy = pos[RIGHT] - pos[LEFT];
1084 Real stem_y_beam0 = (dy && dx
1089 Direction my_dir = get_grob_direction (s);
1090 SCM beaming = s->get_property ("beaming");
1092 Real stem_y = stem_y_beam0;
1095 Slice bm = where_are_the_whole_beams (beaming);
1096 if (!bm.is_empty ())
1097 stem_y += beam_translation * bm[-my_dir];
1101 Slice bm = Stem::beam_multiplicity (s);
1102 if (!bm.is_empty ())
1103 stem_y += bm[my_dir] * beam_translation;
1106 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1107 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1113 Hmm. At this time, beam position and slope are determined. Maybe,
1114 stem directions and length should set to relative to the chord's
1115 position of the beam. */
1117 Beam::set_stem_lengths (Grob *me)
1119 Link_array<Grob> stems
1120 = extract_grob_array (me, ly_symbol2scm ("stems"));
1126 for (int a = 2; a--;)
1127 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1129 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1130 Real staff_space = Staff_symbol_referencer::staff_space (me);
1131 scale_drul (&pos, staff_space);
1135 if (scm_is_number (me->get_property ("gap-count"))
1136 &&scm_to_int (me->get_property ("gap-count")))
1139 thick = get_thickness (me);
1142 // ugh -> use commonx
1143 Grob *fvs = first_visible_stem (me);
1144 Grob *lvs = last_visible_stem (me);
1146 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1147 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1149 for (int i = 0; i < stems.size (); i++)
1152 if (Stem::is_invisible (s))
1155 bool french = to_boolean (s->get_property ("french-beaming"));
1156 Real stem_y = calc_stem_y (me, s, common,
1158 pos, french && s != lvs && s!= fvs);
1161 Make the stems go up to the end of the beam. This doesn't matter
1162 for normal beams, but for tremolo beams it looks silly otherwise.
1165 stem_y += thick * 0.5 * get_grob_direction (s);
1167 Stem::set_stemend (s, 2 * stem_y / staff_space);
1172 Beam::set_beaming (Grob *me, Beaming_info_list *beaming)
1174 Link_array<Grob> stems
1175 = extract_grob_array (me, ly_symbol2scm ("stems"));
1178 for (int i = 0; i < stems.size (); i++)
1181 Don't overwrite user settings.
1186 /* Don't set beaming for outside of outer stems */
1187 if ((d == LEFT && i == 0)
1188 || (d == RIGHT && i == stems.size () -1))
1191 Grob *st = stems[i];
1192 SCM beaming_prop = st->get_property ("beaming");
1193 if (beaming_prop == SCM_EOL
1194 || index_get_cell (beaming_prop, d) == SCM_EOL)
1196 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1198 && i < stems.size () -1
1199 && Stem::is_invisible (st))
1200 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1202 Stem::set_beaming (st, b, d);
1205 while (flip (&d) != LEFT);
1210 Beam::forced_stem_count (Grob *me)
1212 Link_array<Grob> stems
1213 = extract_grob_array (me, ly_symbol2scm ("stems"));
1215 for (int i = 0; i < stems.size (); i++)
1219 if (Stem::is_invisible (s))
1222 /* I can imagine counting those boundaries as a half forced stem,
1223 but let's count them full for now. */
1224 if (abs (Stem::chord_start_y (s)) > 0.1
1225 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1232 Beam::visible_stem_count (Grob *me)
1234 Link_array<Grob> stems
1235 = extract_grob_array (me, ly_symbol2scm ("stems"));
1237 for (int i = stems.size (); i--;)
1239 if (!Stem::is_invisible (stems[i]))
1246 Beam::first_visible_stem (Grob *me)
1248 Link_array<Grob> stems
1249 = extract_grob_array (me, ly_symbol2scm ("stems"));
1251 for (int i = 0; i < stems.size (); i++)
1253 if (!Stem::is_invisible (stems[i]))
1260 Beam::last_visible_stem (Grob *me)
1262 Link_array<Grob> stems
1263 = extract_grob_array (me, ly_symbol2scm ("stems"));
1264 for (int i = stems.size (); i--;)
1266 if (!Stem::is_invisible (stems[i]))
1275 handle rest under beam (do_post: beams are calculated now)
1276 what about combination of collisions and rest under beam.
1280 rest -> stem -> beam -> interpolate_y_position ()
1282 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1284 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1286 Grob *rest = unsmob_grob (element_smob);
1289 if (scm_is_number (rest->get_property ("staff-position")))
1290 return scm_int2num (0);
1292 assert (scm_to_int (axis) == Y_AXIS);
1294 Grob *st = unsmob_grob (rest->get_property ("stem"));
1297 return scm_make_real (0.0);
1298 Grob *beam = unsmob_grob (stem->get_property ("beam"));
1300 || !Beam::has_interface (beam)
1301 || !Beam::visible_stem_count (beam))
1302 return scm_make_real (0.0);
1304 Drul_array<Real> pos (0, 0);
1305 SCM s = beam->get_property ("positions");
1306 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1307 pos = ly_scm2interval (s);
1308 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1310 scale_drul (&pos, staff_space);
1312 Real dy = pos[RIGHT] - pos[LEFT];
1314 // ugh -> use commonx
1315 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1316 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1317 Real slope = dy && dx ? dy / dx : 0;
1319 Direction d = Stem::get_direction (stem);
1320 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1322 Real beam_translation = get_beam_translation (beam);
1323 Real beam_thickness = Beam::get_thickness (beam);
1326 TODO: this is not strictly correct for 16th knee beams.
1329 = Stem::beam_multiplicity (stem).length () + 1;
1331 Real height_of_my_beams = beam_thickness / 2
1332 + (beam_count - 1) * beam_translation;
1333 Real beam_y = stem_y - d * height_of_my_beams;
1335 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1337 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1338 Real minimum_distance
1339 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1340 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1342 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1344 shift /= staff_space;
1345 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1347 /* Always move discretely by half spaces */
1348 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1350 /* Inside staff, move by whole spaces*/
1351 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1353 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1355 shift = ceil (fabs (shift)) * sign (shift);
1357 return scm_make_real (staff_space * shift);
1361 Beam::is_knee (Grob *me)
1363 SCM k = me->get_property ("knee");
1364 if (scm_is_bool (k))
1365 return ly_scm2bool (k);
1369 for (SCM s = me->get_property ("stems"); scm_is_pair (s); s = scm_cdr (s))
1371 Direction dir = get_grob_direction (unsmob_grob (scm_car (s)));
1380 me->set_property ("knee", ly_bool2scm (knee));
1386 Beam::get_direction_beam_count (Grob *me, Direction d)
1388 Link_array<Grob> stems
1389 = extract_grob_array (me, ly_symbol2scm ("stems"));
1392 for (int i = stems.size (); i--;)
1395 Should we take invisible stems into account?
1397 if (Stem::get_direction (stems[i]) == d)
1398 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1404 ADD_INTERFACE (Beam, "beam-interface",
1406 "The @code{thickness} property is the weight of beams, and is measured "
1408 "knee positioning-done position-callbacks "
1409 "concaveness dir-function quant-score auto-knee-gap gap "
1410 "gap-count chord-tremolo beamed-stem-shorten shorten least-squares-dy "
1411 "damping inspect-quants flag-width-function neutral-direction positions space-function "