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 "pointer-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_object ("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_is_procedure (func))
77 SCM s = scm_call_2 (func, me->self_scm (), scm_from_int (get_beam_count (me)));
78 return scm_to_double (s);
86 /* Maximum beam_count. */
88 Beam::get_beam_count (Grob *me)
92 extract_grob_set (me, "stems", stems);
93 for (int i = 0; i < stems.size(); i++)
95 Grob *stem = stems[i];
96 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
102 Space return space between beams.
104 MAKE_SCHEME_CALLBACK (Beam, space_function, 2);
106 Beam::space_function (SCM smob, SCM beam_count)
108 Grob *me = unsmob_grob (smob);
110 Real staff_space = Staff_symbol_referencer::staff_space (me);
111 Real line = Staff_symbol_referencer::line_thickness (me);
112 Real thickness = get_thickness (me);
114 Real beam_translation = scm_to_int (beam_count) < 4
115 ? (2 * staff_space + line - thickness) / 2.0
116 : (3 * staff_space + line - thickness) / 3.0;
118 return scm_from_double (beam_translation);
121 /* After pre-processing all directions should be set.
122 Several post-processing routines (stem, slur, script) need stem/beam
124 Currenly, this means that beam has set all stem's directions.
125 [Alternatively, stems could set its own directions, according to
126 their beam, during 'final-pre-processing'.] */
127 MAKE_SCHEME_CALLBACK (Beam, before_line_breaking, 1);
129 Beam::before_line_breaking (SCM smob)
131 Grob *me = unsmob_grob (smob);
133 /* Beams with less than 2 two stems don't make much sense, but could happen
138 For a beam that only has one stem, we try to do some disappearance magic:
139 we revert the flag, and move on to The Eternal Engraving Fields. */
141 int count = visible_stem_count (me);
144 extract_grob_set (me, "stems", stems);
145 if (stems.size () == 1)
147 me->warning (_ ("removing beam with less than two stems"));
149 stems[0]->set_object ("beam", SCM_EOL);
152 return SCM_UNSPECIFIED;
154 else if (stems.size () == 0)
157 return SCM_UNSPECIFIED;
162 Direction d = get_default_dir (me);
164 consider_auto_knees (me);
165 set_stem_directions (me, d);
169 set_stem_shorten (me);
175 /* We want a maximal number of shared beams, but if there is choice, we
176 * take the one that is closest to the end of the stem. This is for
188 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
192 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
196 for (int i = lslice[-left_dir];
197 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
200 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
202 int k = -right_dir * scm_to_int (scm_car (s)) + i;
203 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
207 if (count >= best_count)
218 Beam::connect_beams (Grob *me)
220 extract_grob_set (me, "stems", stems);
223 last_int.set_empty ();
224 SCM last_beaming = SCM_EOL;
225 Direction last_dir = CENTER;
226 for (int i = 0; i < stems.size (); i++)
228 Grob *this_stem = stems[i];
229 SCM this_beaming = this_stem->get_property ("beaming");
231 Direction this_dir = get_grob_direction (this_stem);
232 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
234 int start_point = position_with_maximal_common_beams
235 (last_beaming, this_beaming,
242 if (d == RIGHT && i == stems.size () - 1)
245 new_slice.set_empty ();
246 SCM s = index_get_cell (this_beaming, d);
247 for (; scm_is_pair (s); s = scm_cdr (s))
250 = start_point - this_dir * scm_to_int (scm_car (s));
252 new_slice.add_point (new_beam_pos);
253 scm_set_car_x (s, scm_from_int (new_beam_pos));
256 while (flip (&d) != LEFT);
258 if (!new_slice.is_empty ())
259 last_int = new_slice;
263 scm_set_car_x (this_beaming, SCM_EOL);
264 SCM s = scm_cdr (this_beaming);
265 for (; scm_is_pair (s); s = scm_cdr (s))
267 int np = -this_dir * scm_to_int (scm_car (s));
268 scm_set_car_x (s, scm_from_int (np));
269 last_int.add_point (np);
273 if (i == stems.size () -1)
275 scm_set_cdr_x (this_beaming, SCM_EOL);
278 if (scm_ilength (scm_cdr (this_beaming)) > 0)
280 last_beaming = this_beaming;
287 TODO: should not make beams per stem, but per Y-level; probably when
288 someone wants to sponsor feathered beaming.
290 MAKE_SCHEME_CALLBACK (Beam, print, 1);
292 Beam::print (SCM grob)
294 Spanner *me = unsmob_spanner (grob);
297 extract_grob_set (me, "stems", stems);
298 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
300 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
301 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
304 if (visible_stem_count (me))
306 // ugh -> use commonx
307 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
308 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
312 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
313 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
316 SCM posns = me->get_property ("positions");
317 Drul_array<Real> pos;
318 if (!is_number_pair (posns))
320 programming_error ("no beam positions?");
321 pos = Interval (0, 0);
324 pos = ly_scm2realdrul (posns);
326 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
328 Real dy = pos[RIGHT] - pos[LEFT];
329 Real slope = (dy && dx) ? dy / dx : 0;
331 Real thick = get_thickness (me);
332 Real bdy = get_beam_translation (me);
334 SCM last_beaming = SCM_EOL;
335 Real last_xposn = -1;
336 Real last_stem_width = -1;
338 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
341 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
343 for (int i = 0; i <= stems.size (); i++)
345 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
347 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
348 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
349 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
350 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
352 We do the space left of ST, with lfliebertjes pointing to the
353 right from the left stem, and rfliebertjes pointing left from
356 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
357 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
359 Array<int> full_beams;
360 Array<int> lfliebertjes;
361 Array<int> rfliebertjes;
364 scm_is_pair (s); s = scm_cdr (s))
366 int b = scm_to_int (scm_car (s));
367 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
373 lfliebertjes.push (b);
377 scm_is_pair (s); s = scm_cdr (s))
379 int b = scm_to_int (scm_car (s));
380 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
382 rfliebertjes.push (b);
386 Drul_array<Real> break_overshoot
387 = robust_scm2drul (me->get_property ("break-overshoot"),
388 Drul_array<Real> (-0.5, 0.0));
391 Real w = (i > 0 && stem)
392 ? (xposn - last_xposn)
393 : break_overshoot[(i==0) ? LEFT: RIGHT];
395 Real stem_offset = 0.0;
398 w += last_stem_width / 2;
399 stem_offset = -last_stem_width / 2;
405 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
406 Stencil whole = Lookup::beam (slope, w, thick, blot);
410 if (scm_is_number (me->get_property ("gap-count")))
412 gap_count = scm_to_int (me->get_property ("gap-count"));
413 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
415 full_beams.sort (default_compare);
417 full_beams.reverse ();
421 for (int j = full_beams.size (); j--;)
428 b.translate_axis (gap_length, X_AXIS);
430 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
431 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
433 the_beam.add_stencil (b);
436 if (lfliebertjes.size () || rfliebertjes.size ())
442 int t = Stem::duration_log (stem);
444 SCM proc = me->get_property ("flag-width-function");
445 SCM result = scm_call_1 (proc, scm_from_int (t));
446 nw_f = scm_to_double (result);
449 nw_f = break_overshoot[RIGHT] / 2;
451 /* Half beam should be one note-width,
452 but let's make sure two half-beams never touch */
456 rw = min (nw_f, ((xposn - last_xposn) / 2));
458 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
459 + break_overshoot[LEFT];
462 lw = min (nw_f, ((xposn - last_xposn) / 2));
464 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
466 + break_overshoot[RIGHT];
468 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
469 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
470 for (int j = lfliebertjes.size (); j--;)
473 b.translate_axis (last_xposn - x0, X_AXIS);
474 b.translate_axis (slope * (last_xposn - x0) + bdy * lfliebertjes[j], Y_AXIS);
475 the_beam.add_stencil (b);
477 for (int j = rfliebertjes.size (); j--;)
480 b.translate_axis (xposn - x0 - rw, X_AXIS);
481 b.translate_axis (slope * (xposn - x0 - rw)
482 + bdy * rfliebertjes[j], Y_AXIS);
483 the_beam.add_stencil (b);
488 last_stem_width = stem_width;
489 last_beaming = this_beaming;
492 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS), X_AXIS);
493 the_beam.translate_axis (pos[LEFT], Y_AXIS);
496 SCM quant_score = me->get_property ("quant-score");
497 if (to_boolean (me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting")))
498 && 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;
529 extract_grob_set (me, "stems", stems);
531 for (int i = 0; i < stems.size (); i++)
535 Direction sd = get_grob_direction (s);
537 int center_distance = max (int (- d * Stem::head_positions (s) [-d]), 0);
538 int current = sd ? (1 + d * sd) / 2 : center_distance;
546 while (flip (&d) != DOWN);
548 SCM func = me->get_property ("dir-function");
549 SCM s = scm_call_2 (func,
550 scm_cons (scm_from_int (count[UP]),
551 scm_from_int (count[DOWN])),
552 scm_cons (scm_from_int (total[UP]),
553 scm_from_int (total[DOWN])));
555 if (scm_is_number (s) && scm_to_int (s))
558 /* If dir is not determined: get default */
559 return to_dir (me->get_property ("neutral-direction"));
562 /* Set all stems with non-forced direction to beam direction.
563 Urg: non-forced should become `without/with unforced' direction,
564 once stem gets cleaned-up. */
566 Beam::set_stem_directions (Grob *me, Direction d)
568 extract_grob_set (me, "stems", stems);
570 for (int i = 0; i < stems.size (); i++)
574 SCM forcedir = s->get_property ("direction");
575 if (!to_dir (forcedir))
576 set_grob_direction (s, d);
581 Only try horizontal beams for knees. No reliable detection of
582 anything else is possible here, since we don't know funky-beaming
583 settings, or X-distances (slopes!) People that want sloped
584 knee-beams, should set the directions manually.
589 this routine should take into account the stemlength scoring
590 of a possible knee/nonknee beam.
593 Beam::consider_auto_knees (Grob *me)
595 SCM scm = me->get_property ("auto-knee-gap");
596 if (!scm_is_number (scm))
603 extract_grob_set (me, "stems", 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_from_int (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_from_double (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 extract_grob_set (me, "stems", stems);
809 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
810 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
812 Real my_y = me->relative_coordinate (commony, Y_AXIS);
814 Grob *fvs = first_visible_stem (me);
815 Grob *lvs = last_visible_stem (me);
817 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
818 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
819 Stem::get_stem_info (lvs).ideal_y_
820 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
822 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
823 for (int i = 0; i < stems.size (); i++)
827 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
830 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
838 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
839 Stem::chord_start_y (last_visible_stem (me)));
841 /* Simple beams (2 stems) on middle line should be allowed to be
844 However, if both stems reach middle line,
845 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
847 For that case, we apply artificial slope */
848 if (!ideal[LEFT] && chord.delta () && count == 2)
851 Direction d = (Direction) (sign (chord.delta ()) * UP);
852 pos[d] = get_thickness (me) / 2;
861 For broken beams this doesn't work well. In this case, the
862 slope esp. of the first part of a broken beam should predict
863 where the second part goes.
865 me->set_property ("least-squares-dy",
866 scm_from_double (pos[RIGHT] - pos[LEFT]));
870 Array<Offset> ideals;
871 for (int i = 0; i < stems.size (); i++)
874 if (Stem::is_invisible (s))
876 ideals.push (Offset (x_posns[i],
877 Stem::get_stem_info (s).ideal_y_
878 + s->relative_coordinate (commony, Y_AXIS)
882 minimise_least_squares (&slope, &y, ideals);
886 set_minimum_dy (me, &dy);
887 me->set_property ("least-squares-dy", scm_from_double (dy));
888 pos = Interval (y, (y + dy));
892 "position" is relative to the staff.
894 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
896 me->set_property ("positions", ly_interval2scm (pos));
898 return SCM_UNSPECIFIED;
902 We can't combine with previous function, since check concave and
903 slope damping comes first.
905 TODO: we should use the concaveness to control the amount of damping
908 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
910 Beam::shift_region_to_valid (SCM grob)
912 Grob *me = unsmob_grob (grob);
917 extract_grob_set (me, "stems", stems);
918 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
919 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
921 Grob *fvs = first_visible_stem (me);
924 return SCM_UNSPECIFIED;
926 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
927 for (int i = 0; i < stems.size (); i++)
931 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
935 Grob *lvs = last_visible_stem (me);
937 return SCM_UNSPECIFIED;
939 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
941 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
943 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
945 Real dy = pos[RIGHT] - pos[LEFT];
947 Real slope = dx ? (dy / dx) : 0.0;
950 Shift the positions so that we have a chance of finding good
951 quants (i.e. no short stem failures.)
953 Interval feasible_left_point;
954 feasible_left_point.set_full ();
955 for (int i = 0; i < stems.size (); i++)
958 if (Stem::is_invisible (s))
961 Direction d = Stem::get_direction (s);
964 = Stem::get_stem_info (s).shortest_y_
965 - slope * x_posns [i];
968 left_y is now relative to the stem S. We want relative to
969 ourselves, so translate:
972 += + s->relative_coordinate (commony, Y_AXIS)
973 - me->relative_coordinate (commony, Y_AXIS);
979 feasible_left_point.intersect (flp);
982 if (feasible_left_point.is_empty ())
983 warning (_ ("no viable initial configuration found: may not find good beam slope"));
984 else if (!feasible_left_point.contains (y))
986 const int REGION_SIZE = 2; // UGH UGH
987 if (isinf (feasible_left_point[DOWN]))
988 y = feasible_left_point[UP] - REGION_SIZE;
989 else if (isinf (feasible_left_point[UP]))
990 y = feasible_left_point[DOWN]+ REGION_SIZE;
992 y = feasible_left_point.center ();
995 pos = Drul_array<Real> (y, (y + dy));
996 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
998 me->set_property ("positions", ly_interval2scm (pos));
999 return SCM_UNSPECIFIED;
1002 /* This neat trick is by Werner Lemberg,
1003 damped = tanh (slope)
1004 corresponds with some tables in [Wanske] CHECKME */
1005 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
1007 Beam::slope_damping (SCM smob)
1009 Grob *me = unsmob_grob (smob);
1011 if (visible_stem_count (me) <= 1)
1012 return SCM_UNSPECIFIED;
1014 SCM s = me->get_property ("damping");
1015 Real damping = scm_to_double (s);
1019 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
1020 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1022 Real dy = pos[RIGHT] - pos[LEFT];
1024 Grob *fvs = first_visible_stem (me);
1025 Grob *lvs = last_visible_stem (me);
1027 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1029 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1030 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1032 Real slope = dy && dx ? dy / dx : 0;
1034 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1036 slope = 0.6 * tanh (slope) / (damping + concaveness);
1038 Real damped_dy = slope * dx;
1040 set_minimum_dy (me, &damped_dy);
1042 pos[LEFT] += (dy - damped_dy) / 2;
1043 pos[RIGHT] -= (dy - damped_dy) / 2;
1045 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1047 me->set_property ("positions", ly_interval2scm (pos));
1049 return SCM_UNSPECIFIED;
1053 Report slice containing the numbers that are both in (car BEAMING)
1057 where_are_the_whole_beams (SCM beaming)
1061 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1063 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1065 l.add_point (scm_to_int (scm_car (s)));
1071 /* Return the Y position of the stem-end, given the Y-left, Y-right
1072 in POS for stem S. This Y position is relative to S. */
1074 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1076 Drul_array<Real> pos, bool french)
1078 Real beam_translation = get_beam_translation (me);
1080 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1081 Real dy = pos[RIGHT] - pos[LEFT];
1083 Real stem_y_beam0 = (dy && dx
1088 Direction my_dir = get_grob_direction (s);
1089 SCM beaming = s->get_property ("beaming");
1091 Real stem_y = stem_y_beam0;
1094 Slice bm = where_are_the_whole_beams (beaming);
1095 if (!bm.is_empty ())
1096 stem_y += beam_translation * bm[-my_dir];
1100 Slice bm = Stem::beam_multiplicity (s);
1101 if (!bm.is_empty ())
1102 stem_y += bm[my_dir] * beam_translation;
1105 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1106 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1112 Hmm. At this time, beam position and slope are determined. Maybe,
1113 stem directions and length should set to relative to the chord's
1114 position of the beam. */
1116 Beam::set_stem_lengths (Grob *me)
1118 extract_grob_set (me, "stems", stems);
1123 for (int a = 2; a--;)
1124 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1126 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1127 Real staff_space = Staff_symbol_referencer::staff_space (me);
1128 scale_drul (&pos, staff_space);
1132 if (scm_is_number (me->get_property ("gap-count"))
1133 &&scm_to_int (me->get_property ("gap-count")))
1136 thick = get_thickness (me);
1139 // ugh -> use commonx
1140 Grob *fvs = first_visible_stem (me);
1141 Grob *lvs = last_visible_stem (me);
1143 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1144 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1146 for (int i = 0; i < stems.size (); i++)
1149 if (Stem::is_invisible (s))
1152 bool french = to_boolean (s->get_property ("french-beaming"));
1153 Real stem_y = calc_stem_y (me, s, common,
1155 pos, french && s != lvs && s!= fvs);
1158 Make the stems go up to the end of the beam. This doesn't matter
1159 for normal beams, but for tremolo beams it looks silly otherwise.
1162 stem_y += thick * 0.5 * get_grob_direction (s);
1164 Stem::set_stemend (s, 2 * stem_y / staff_space);
1169 Beam::set_beaming (Grob *me, Beaming_info_list *beaming)
1171 extract_grob_set (me, "stems", stems);
1174 for (int i = 0; i < stems.size (); i++)
1177 Don't overwrite user settings.
1182 /* Don't set beaming for outside of outer stems */
1183 if ((d == LEFT && i == 0)
1184 || (d == RIGHT && i == stems.size () -1))
1187 Grob *stem = stems[i];
1188 SCM beaming_prop = stem->get_property ("beaming");
1189 if (beaming_prop == SCM_EOL
1190 || index_get_cell (beaming_prop, d) == SCM_EOL)
1192 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1194 && i < stems.size () -1
1195 && Stem::is_invisible (stem))
1196 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1198 Stem::set_beaming (stem, b, d);
1201 while (flip (&d) != LEFT);
1206 Beam::forced_stem_count (Grob *me)
1208 extract_grob_set (me, "stems", stems);
1211 for (int i = 0; i < stems.size (); i++)
1215 if (Stem::is_invisible (s))
1218 /* I can imagine counting those boundaries as a half forced stem,
1219 but let's count them full for now. */
1220 if (abs (Stem::chord_start_y (s)) > 0.1
1221 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1228 Beam::visible_stem_count (Grob *me)
1230 extract_grob_set (me, "stems", stems);
1232 for (int i = stems.size (); i--;)
1234 if (!Stem::is_invisible (stems[i]))
1241 Beam::first_visible_stem (Grob *me)
1243 extract_grob_set (me, "stems", stems);
1245 for (int i = 0; i < stems.size (); i++)
1247 if (!Stem::is_invisible (stems[i]))
1254 Beam::last_visible_stem (Grob *me)
1256 extract_grob_set (me, "stems", stems);
1258 for (int i = stems.size (); i--;)
1260 if (!Stem::is_invisible (stems[i]))
1269 handle rest under beam (do_post: beams are calculated now)
1270 what about combination of collisions and rest under beam.
1274 rest -> stem -> beam -> interpolate_y_position ()
1276 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1278 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1280 Grob *rest = unsmob_grob (element_smob);
1283 if (scm_is_number (rest->get_property ("staff-position")))
1284 return scm_from_int (0);
1286 assert (scm_to_int (axis) == Y_AXIS);
1288 Grob *st = unsmob_grob (rest->get_object ("stem"));
1291 return scm_from_double (0.0);
1292 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1294 || !Beam::has_interface (beam)
1295 || !Beam::visible_stem_count (beam))
1296 return scm_from_double (0.0);
1298 Drul_array<Real> pos (0, 0);
1299 SCM s = beam->get_property ("positions");
1300 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1301 pos = ly_scm2interval (s);
1305 UGH. TODO: fix dependency tracking.
1307 position_beam (beam);
1308 pos = ly_scm2interval (beam->get_property ("positions"));
1311 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1313 scale_drul (&pos, staff_space);
1315 Real dy = pos[RIGHT] - pos[LEFT];
1317 // ugh -> use commonx
1318 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1319 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1320 Real slope = dy && dx ? dy / dx : 0;
1322 Direction d = Stem::get_direction (stem);
1323 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1325 Real beam_translation = get_beam_translation (beam);
1326 Real beam_thickness = Beam::get_thickness (beam);
1329 TODO: this is not strictly correct for 16th knee beams.
1332 = Stem::beam_multiplicity (stem).length () + 1;
1334 Real height_of_my_beams = beam_thickness / 2
1335 + (beam_count - 1) * beam_translation;
1336 Real beam_y = stem_y - d * height_of_my_beams;
1338 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1340 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1341 Real minimum_distance
1342 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1343 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1345 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1347 shift /= staff_space;
1348 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1350 /* Always move discretely by half spaces */
1351 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1353 /* Inside staff, move by whole spaces*/
1354 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1356 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1358 shift = ceil (fabs (shift)) * sign (shift);
1360 return scm_from_double (staff_space * shift);
1364 Beam::is_knee (Grob *me)
1366 SCM k = me->get_property ("knee");
1367 if (scm_is_bool (k))
1368 return ly_scm2bool (k);
1372 extract_grob_set (me, "stems", stems);
1373 for (int i = stems.size (); i--;)
1375 Direction dir = get_grob_direction (stems[i]);
1384 me->set_property ("knee", ly_bool2scm (knee));
1390 Beam::get_direction_beam_count (Grob *me, Direction d)
1392 extract_grob_set (me, "stems", stems);
1395 for (int i = stems.size (); i--;)
1398 Should we take invisible stems into account?
1400 if (Stem::get_direction (stems[i]) == d)
1401 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1407 ADD_INTERFACE (Beam, "beam-interface",
1409 "The @code{thickness} property is the weight of beams, and is measured "
1411 "knee positioning-done position-callbacks "
1412 "concaveness dir-function quant-score auto-knee-gap gap "
1413 "gap-count chord-tremolo beamed-stem-shorten shorten least-squares-dy "
1414 "details damping inspect-quants flag-width-function "
1415 "neutral-direction positions space-function break-overshoot "