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-item.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 me->warning (_ ("beam has less than two visible stems"));
144 SCM stems = me->get_property ("stems");
145 if (scm_ilength (stems) == 1)
147 me->warning (_ ("removing beam with less than two stems"));
149 unsmob_grob (scm_car (stems))->set_property ("beam", SCM_EOL);
152 return SCM_UNSPECIFIED;
154 else if (scm_ilength (stems) == 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_int2num (k), left_beaming) != SCM_BOOL_F)
207 if (count >= best_count)
218 Beam::connect_beams (Grob *me)
220 Link_array<Grob> stems
221 = extract_grob_array (me, ly_symbol2scm ("stems"));
224 last_int.set_empty ();
225 SCM last_beaming = SCM_EOL;
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,
243 if (d == RIGHT && i == stems.size () - 1)
246 new_slice.set_empty ();
247 SCM s = index_get_cell (this_beaming, d);
248 for (; scm_is_pair (s); s = scm_cdr (s))
251 = start_point - this_dir * scm_to_int (scm_car (s));
253 new_slice.add_point (new_beam_pos);
254 scm_set_car_x (s, scm_int2num (new_beam_pos));
257 while (flip (&d) != LEFT);
259 if (!new_slice.is_empty ())
260 last_int = new_slice;
264 scm_set_car_x (this_beaming, SCM_EOL);
265 SCM s = scm_cdr (this_beaming);
266 for (; scm_is_pair (s); s = scm_cdr (s))
268 int np = -this_dir * scm_to_int (scm_car (s));
269 scm_set_car_x (s, scm_int2num (np));
270 last_int.add_point (np);
274 if (i == stems.size () -1)
276 scm_set_cdr_x (this_beaming, SCM_EOL);
279 if (scm_ilength (scm_cdr (this_beaming)) > 0)
281 last_beaming = this_beaming;
288 TODO: should not make beams per stem, but per Y-level.
290 MAKE_SCHEME_CALLBACK (Beam, print, 1);
292 Beam::print (SCM grob)
294 Spanner *me = unsmob_spanner (grob);
297 Link_array<Grob> stems
298 = extract_grob_array (me, ly_symbol2scm ("stems"));
299 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
301 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
302 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
305 if (visible_stem_count (me))
307 // ugh -> use commonx
308 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
309 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
313 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
314 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
317 SCM posns = me->get_property ("positions");
318 Drul_array<Real> pos;
319 if (!is_number_pair (posns))
321 programming_error ("no beam positions?");
322 pos = Interval (0, 0);
325 pos = ly_scm2realdrul (posns);
327 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
329 Real dy = pos[RIGHT] - pos[LEFT];
330 Real slope = (dy && dx) ? dy / dx : 0;
332 Real thick = get_thickness (me);
333 Real bdy = get_beam_translation (me);
335 SCM last_beaming = SCM_EOL;
336 Real last_xposn = -1;
337 Real last_stem_width = -1;
339 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
342 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
344 for (int i = 0; i <= stems.size (); i++)
346 Grob *st = (i < stems.size ()) ? stems[i] : 0;
348 SCM this_beaming = st ? st->get_property ("beaming") : SCM_EOL;
349 Real xposn = st ? st->relative_coordinate (xcommon, X_AXIS) : 0.0;
350 Real stem_width = st ? robust_scm2double (st->get_property ("thickness"), 1.0) * lt : 0;
351 Direction stem_dir = st ? to_dir (st->get_property ("direction")) : CENTER;
353 We do the space left of ST, with lfliebertjes pointing to the
354 right from the left stem, and rfliebertjes pointing left from
357 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
358 SCM right = st ? scm_car (this_beaming) : SCM_EOL;
360 Array<int> full_beams;
361 Array<int> lfliebertjes;
362 Array<int> rfliebertjes;
365 scm_is_pair (s); s = scm_cdr (s))
367 int b = scm_to_int (scm_car (s));
368 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
374 lfliebertjes.push (b);
378 scm_is_pair (s); s = scm_cdr (s))
380 int b = scm_to_int (scm_car (s));
381 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
383 rfliebertjes.push (b);
388 how much to stick out for beams across linebreaks
390 Real break_overshoot = 3.0;
391 Real w = (i > 0 && st) ? (xposn - last_xposn) : break_overshoot;
393 Real stem_offset = 0.0;
396 w += last_stem_width / 2;
397 stem_offset = -last_stem_width / 2;
403 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
404 Stencil whole = Lookup::beam (slope, w, thick, blot);
408 if (scm_is_number (me->get_property ("gap-count")))
410 gap_count = scm_to_int (me->get_property ("gap-count"));
411 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
413 full_beams.sort (default_compare);
415 full_beams.reverse ();
419 for (int j = full_beams.size (); j--;)
426 b.translate_axis (gap_length, X_AXIS);
428 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
429 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
431 the_beam.add_stencil (b);
434 if (lfliebertjes.size () || rfliebertjes.size ())
440 int t = Stem::duration_log (st);
442 SCM proc = me->get_property ("flag-width-function");
443 SCM result = scm_call_1 (proc, scm_int2num (t));
444 nw_f = scm_to_double (result);
447 nw_f = break_overshoot / 2;
449 /* Half beam should be one note-width,
450 but let's make sure two half-beams never touch */
454 rw = min (nw_f, ((xposn - last_xposn) / 2));
457 TODO: 0.5 is a guess.
459 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
463 lw = min (nw_f, ((xposn - last_xposn) / 2));
465 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
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) + 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), X_AXIS);
492 the_beam.translate_axis (pos[LEFT], Y_AXIS);
495 SCM quant_score = me->get_property ("quant-score");
496 if (to_boolean (me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting")))
497 && scm_is_string (quant_score))
501 This code prints the demerits for each beam. Perhaps this
502 should be switchable for those who want to twiddle with the
506 SCM properties = Font_interface::text_font_alist_chain (me);
508 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
510 Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
511 (me->get_layout ()->self_scm (), properties, quant_score));
512 the_beam.add_at_edge (Y_AXIS, stem_dir, tm, 1.0, 0);
516 return the_beam.smobbed_copy ();
520 Beam::get_default_dir (Grob *me)
522 Drul_array<int> total;
523 total[UP] = total[DOWN] = 0;
524 Drul_array<int> count;
525 count[UP] = count[DOWN] = 0;
528 Link_array<Grob> stems
529 = extract_grob_array (me, ly_symbol2scm ("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_int2num (count[UP]),
551 scm_int2num (count[DOWN])),
552 scm_cons (scm_int2num (total[UP]),
553 scm_int2num (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 Link_array<Grob> stems
569 = extract_grob_array (me, ly_symbol2scm ("stems"));
571 for (int i = 0; i < stems.size (); i++)
575 SCM forcedir = s->get_property ("direction");
576 if (!to_dir (forcedir))
577 set_grob_direction (s, d);
582 Only try horizontal beams for knees. No reliable detection of
583 anything else is possible here, since we don't know funky-beaming
584 settings, or X-distances (slopes!) People that want sloped
585 knee-beams, should set the directions manually.
590 this routine should take into account the stemlength scoring
591 of a possible knee/nonknee beam.
594 Beam::consider_auto_knees (Grob *me)
596 SCM scm = me->get_property ("auto-knee-gap");
597 if (!scm_is_number (scm))
604 Link_array<Grob> stems
605 = extract_grob_array (me, ly_symbol2scm ("stems"));
607 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
608 Real staff_space = Staff_symbol_referencer::staff_space (me);
610 Array<Interval> head_extents_array;
611 for (int i = 0; i < stems.size (); i++)
613 Grob *stem = stems[i];
614 if (Stem::is_invisible (stem))
617 Interval head_extents = Stem::head_positions (stem);
618 if (!head_extents.is_empty ())
620 head_extents[LEFT] += -1;
621 head_extents[RIGHT] += 1;
622 head_extents *= staff_space * 0.5;
625 We could subtract beam Y position, but this routine only
626 sets stem directions, a constant shift does not have an
629 head_extents += stem->relative_coordinate (common, Y_AXIS);
631 if (to_dir (stem->get_property ("direction")))
633 Direction stemdir = to_dir (stem->get_property ("direction"));
634 head_extents[-stemdir] = -stemdir * infinity_f;
637 head_extents_array.push (head_extents);
639 gaps.remove_interval (head_extents);
643 Real max_gap_len = 0.0;
645 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
647 Interval gap = gaps.allowed_regions_[i];
650 the outer gaps are not knees.
652 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
655 if (gap.length () >= max_gap_len)
657 max_gap_len = gap.length ();
662 Real beam_translation = get_beam_translation (me);
663 Real beam_thickness = Beam::get_thickness (me);
664 int beam_count = Beam::get_beam_count (me);
665 Real height_of_beams = beam_thickness / 2
666 + (beam_count - 1) * beam_translation;
667 Real threshold = scm_to_double (scm) + height_of_beams;
669 if (max_gap_len > threshold)
672 for (int i = 0; i < stems.size (); i++)
674 Grob *stem = stems[i];
675 if (Stem::is_invisible (stem))
678 Interval head_extents = head_extents_array[j++];
680 Direction d = (head_extents.center () < max_gap.center ())
683 stem->set_property ("direction", scm_int2num (d));
685 head_extents.intersect (max_gap);
686 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
691 /* Set stem's shorten property if unset.
694 take some y-position (chord/beam/nearest?) into account
695 scmify forced-fraction
697 This is done in beam because the shorten has to be uniform over the
701 Beam::set_stem_shorten (Grob *me)
704 shortening looks silly for x staff beams
709 Real forced_fraction = 1.0 * forced_stem_count (me)
710 / visible_stem_count (me);
712 int beam_count = get_beam_count (me);
714 SCM shorten_list = me->get_property ("beamed-stem-shorten");
715 if (shorten_list == SCM_EOL)
718 Real staff_space = Staff_symbol_referencer::staff_space (me);
721 = robust_list_ref (beam_count -1, shorten_list);
722 Real shorten_f = scm_to_double (shorten_elt) * staff_space;
724 /* your similar cute comment here */
725 shorten_f *= forced_fraction;
728 me->set_property ("shorten", scm_make_real (shorten_f));
731 /* Call list of y-dy-callbacks, that handle setting of
734 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
736 Beam::after_line_breaking (SCM smob)
738 Grob *me = unsmob_grob (smob);
741 return SCM_UNSPECIFIED;
745 Beam::position_beam (Grob *me)
749 if (to_boolean (me->get_property ("positioning-done")))
752 me->set_property ("positioning-done", SCM_BOOL_T);
754 /* Copy to mutable list. */
755 SCM s = ly_deep_copy (me->get_property ("positions"));
756 me->set_property ("positions", s);
758 if (scm_car (s) == SCM_BOOL_F)
760 // one wonders if such genericity is necessary --hwn.
761 SCM callbacks = me->get_property ("position-callbacks");
762 for (SCM i = callbacks; scm_is_pair (i); i = scm_cdr (i))
763 scm_call_1 (scm_car (i), me->self_scm ());
766 set_stem_lengths (me);
770 set_minimum_dy (Grob *me, Real *dy)
775 If dy is smaller than the smallest quant, we
776 get absurd direction-sign penalties.
779 Real ss = Staff_symbol_referencer::staff_space (me);
780 Real thickness = Beam::get_thickness (me) / ss;
781 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
782 Real sit = (thickness - slt) / 2;
784 Real hang = 1.0 - (thickness - slt) / 2;
786 *dy = sign (*dy) * max (fabs (*dy),
787 min (min (sit, inter), hang));
792 Compute a first approximation to the beam slope.
794 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
796 Beam::least_squares (SCM smob)
798 Grob *me = unsmob_grob (smob);
800 int count = visible_stem_count (me);
805 me->set_property ("positions", ly_interval2scm (pos));
806 return SCM_UNSPECIFIED;
810 Link_array<Grob> stems
811 = extract_grob_array (me, ly_symbol2scm ("stems"));
812 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
813 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
815 Real my_y = me->relative_coordinate (commony, Y_AXIS);
817 Grob *fvs = first_visible_stem (me);
818 Grob *lvs = last_visible_stem (me);
820 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
821 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
822 Stem::get_stem_info (lvs).ideal_y_
823 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
825 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
826 for (int i = 0; i < stems.size (); i++)
830 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
833 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
841 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
842 Stem::chord_start_y (last_visible_stem (me)));
844 /* Simple beams (2 stems) on middle line should be allowed to be
847 However, if both stems reach middle line,
848 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
850 For that case, we apply artificial slope */
851 if (!ideal[LEFT] && chord.delta () && count == 2)
854 Direction d = (Direction) (sign (chord.delta ()) * UP);
855 pos[d] = get_thickness (me) / 2;
864 For broken beams this doesn't work well. In this case, the
865 slope esp. of the first part of a broken beam should predict
866 where the second part goes.
868 me->set_property ("least-squares-dy",
869 scm_make_real (pos[RIGHT] - pos[LEFT]));
873 Array<Offset> ideals;
874 for (int i = 0; i < stems.size (); i++)
877 if (Stem::is_invisible (s))
879 ideals.push (Offset (x_posns[i],
880 Stem::get_stem_info (s).ideal_y_
881 + s->relative_coordinate (commony, Y_AXIS)
885 minimise_least_squares (&slope, &y, ideals);
889 set_minimum_dy (me, &dy);
890 me->set_property ("least-squares-dy", scm_make_real (dy));
891 pos = Interval (y, (y + dy));
895 "position" is relative to the staff.
897 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
899 me->set_property ("positions", ly_interval2scm (pos));
901 return SCM_UNSPECIFIED;
905 We can't combine with previous function, since check concave and
906 slope damping comes first.
908 TODO: we should use the concaveness to control the amount of damping
911 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
913 Beam::shift_region_to_valid (SCM grob)
915 Grob *me = unsmob_grob (grob);
920 Link_array<Grob> stems
921 = extract_grob_array (me, ly_symbol2scm ("stems"));
922 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
923 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
925 Grob *fvs = first_visible_stem (me);
928 return SCM_UNSPECIFIED;
930 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
931 for (int i = 0; i < stems.size (); i++)
935 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
939 Grob *lvs = last_visible_stem (me);
941 return SCM_UNSPECIFIED;
943 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
945 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
947 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
949 Real dy = pos[RIGHT] - pos[LEFT];
951 Real slope = dy / dx;
954 Shift the positions so that we have a chance of finding good
955 quants (i.e. no short stem failures.)
957 Interval feasible_left_point;
958 feasible_left_point.set_full ();
959 for (int i = 0; i < stems.size (); i++)
962 if (Stem::is_invisible (s))
965 Direction d = Stem::get_direction (s);
968 = Stem::get_stem_info (s).shortest_y_
969 - slope * x_posns [i];
972 left_y is now relative to the stem S. We want relative to
973 ourselves, so translate:
976 += + s->relative_coordinate (commony, Y_AXIS)
977 - me->relative_coordinate (commony, Y_AXIS);
983 feasible_left_point.intersect (flp);
986 if (feasible_left_point.is_empty ())
987 warning (_ ("no viable initial configuration found: may not find good beam slope"));
988 else if (!feasible_left_point.contains (y))
990 if (isinf (feasible_left_point[DOWN]))
991 y = feasible_left_point[UP] - REGION_SIZE;
992 else if (isinf (feasible_left_point[UP]))
993 y = feasible_left_point[DOWN]+ REGION_SIZE;
995 y = feasible_left_point.center ();
998 pos = Drul_array<Real> (y, (y + dy));
999 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1001 me->set_property ("positions", ly_interval2scm (pos));
1002 return SCM_UNSPECIFIED;
1005 /* This neat trick is by Werner Lemberg,
1006 damped = tanh (slope)
1007 corresponds with some tables in [Wanske] CHECKME */
1008 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
1010 Beam::slope_damping (SCM smob)
1012 Grob *me = unsmob_grob (smob);
1014 if (visible_stem_count (me) <= 1)
1015 return SCM_UNSPECIFIED;
1017 SCM s = me->get_property ("damping");
1018 Real damping = scm_to_double (s);
1022 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
1023 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1025 Real dy = pos[RIGHT] - pos[LEFT];
1027 Grob *fvs = first_visible_stem (me);
1028 Grob *lvs = last_visible_stem (me);
1030 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1032 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1033 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1035 Real slope = dy && dx ? dy / dx : 0;
1037 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1039 slope = 0.6 * tanh (slope) / (damping + concaveness);
1041 Real damped_dy = slope * dx;
1043 set_minimum_dy (me, &damped_dy);
1045 pos[LEFT] += (dy - damped_dy) / 2;
1046 pos[RIGHT] -= (dy - damped_dy) / 2;
1048 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1050 me->set_property ("positions", ly_interval2scm (pos));
1052 return SCM_UNSPECIFIED;
1056 Report slice containing the numbers that are both in (car BEAMING)
1060 where_are_the_whole_beams (SCM beaming)
1064 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1066 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1068 l.add_point (scm_to_int (scm_car (s)));
1074 /* Return the Y position of the stem-end, given the Y-left, Y-right
1075 in POS for stem S. This Y position is relative to S. */
1077 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1079 Drul_array<Real> pos, bool french)
1081 Real beam_translation = get_beam_translation (me);
1083 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1084 Real dy = pos[RIGHT] - pos[LEFT];
1086 Real stem_y_beam0 = (dy && dx
1091 Direction my_dir = get_grob_direction (s);
1092 SCM beaming = s->get_property ("beaming");
1094 Real stem_y = stem_y_beam0;
1097 Slice bm = where_are_the_whole_beams (beaming);
1098 if (!bm.is_empty ())
1099 stem_y += beam_translation * bm[-my_dir];
1103 Slice bm = Stem::beam_multiplicity (s);
1104 if (!bm.is_empty ())
1105 stem_y += bm[my_dir] * beam_translation;
1108 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1109 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1115 Hmm. At this time, beam position and slope are determined. Maybe,
1116 stem directions and length should set to relative to the chord's
1117 position of the beam. */
1119 Beam::set_stem_lengths (Grob *me)
1121 Link_array<Grob> stems
1122 = extract_grob_array (me, ly_symbol2scm ("stems"));
1128 for (int a = 2; a--;)
1129 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1131 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1132 Real staff_space = Staff_symbol_referencer::staff_space (me);
1133 scale_drul (&pos, staff_space);
1137 if (scm_is_number (me->get_property ("gap-count"))
1138 &&scm_to_int (me->get_property ("gap-count")))
1141 thick = get_thickness (me);
1144 // ugh -> use commonx
1145 Grob *fvs = first_visible_stem (me);
1146 Grob *lvs = last_visible_stem (me);
1148 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1149 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1151 for (int i = 0; i < stems.size (); i++)
1154 if (Stem::is_invisible (s))
1157 bool french = to_boolean (s->get_property ("french-beaming"));
1158 Real stem_y = calc_stem_y (me, s, common,
1160 pos, french && s != lvs && s!= fvs);
1163 Make the stems go up to the end of the beam. This doesn't matter
1164 for normal beams, but for tremolo beams it looks silly otherwise.
1167 stem_y += thick * 0.5 * get_grob_direction (s);
1169 Stem::set_stemend (s, 2 * stem_y / staff_space);
1174 Beam::set_beaming (Grob *me, Beaming_info_list *beaming)
1176 Link_array<Grob> stems
1177 = extract_grob_array (me, ly_symbol2scm ("stems"));
1180 for (int i = 0; i < stems.size (); i++)
1183 Don't overwrite user settings.
1188 /* Don't set beaming for outside of outer stems */
1189 if ((d == LEFT && i == 0)
1190 || (d == RIGHT && i == stems.size () -1))
1193 Grob *st = stems[i];
1194 SCM beaming_prop = st->get_property ("beaming");
1195 if (beaming_prop == SCM_EOL
1196 || index_get_cell (beaming_prop, d) == SCM_EOL)
1198 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1200 && i < stems.size () -1
1201 && Stem::is_invisible (st))
1202 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1204 Stem::set_beaming (st, b, d);
1207 while (flip (&d) != LEFT);
1212 Beam::forced_stem_count (Grob *me)
1214 Link_array<Grob> stems
1215 = extract_grob_array (me, ly_symbol2scm ("stems"));
1217 for (int i = 0; i < stems.size (); i++)
1221 if (Stem::is_invisible (s))
1224 /* I can imagine counting those boundaries as a half forced stem,
1225 but let's count them full for now. */
1226 if (abs (Stem::chord_start_y (s)) > 0.1
1227 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1234 Beam::visible_stem_count (Grob *me)
1236 Link_array<Grob> stems
1237 = extract_grob_array (me, ly_symbol2scm ("stems"));
1239 for (int i = stems.size (); i--;)
1241 if (!Stem::is_invisible (stems[i]))
1248 Beam::first_visible_stem (Grob *me)
1250 Link_array<Grob> stems
1251 = extract_grob_array (me, ly_symbol2scm ("stems"));
1253 for (int i = 0; i < stems.size (); i++)
1255 if (!Stem::is_invisible (stems[i]))
1262 Beam::last_visible_stem (Grob *me)
1264 Link_array<Grob> stems
1265 = extract_grob_array (me, ly_symbol2scm ("stems"));
1266 for (int i = stems.size (); i--;)
1268 if (!Stem::is_invisible (stems[i]))
1277 handle rest under beam (do_post: beams are calculated now)
1278 what about combination of collisions and rest under beam.
1282 rest -> stem -> beam -> interpolate_y_position ()
1284 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1286 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1288 Grob *rest = unsmob_grob (element_smob);
1291 if (scm_is_number (rest->get_property ("staff-position")))
1292 return scm_int2num (0);
1294 assert (scm_to_int (axis) == Y_AXIS);
1296 Grob *st = unsmob_grob (rest->get_property ("stem"));
1299 return scm_make_real (0.0);
1300 Grob *beam = unsmob_grob (stem->get_property ("beam"));
1302 || !Beam::has_interface (beam)
1303 || !Beam::visible_stem_count (beam))
1304 return scm_make_real (0.0);
1306 Drul_array<Real> pos (0, 0);
1307 SCM s = beam->get_property ("positions");
1308 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1309 pos = ly_scm2interval (s);
1310 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1312 scale_drul (&pos, staff_space);
1314 Real dy = pos[RIGHT] - pos[LEFT];
1316 // ugh -> use commonx
1317 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1318 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1319 Real slope = dy && dx ? dy / dx : 0;
1321 Direction d = Stem::get_direction (stem);
1322 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1324 Real beam_translation = get_beam_translation (beam);
1325 Real beam_thickness = Beam::get_thickness (beam);
1328 TODO: this is not strictly correct for 16th knee beams.
1331 = Stem::beam_multiplicity (stem).length () + 1;
1333 Real height_of_my_beams = beam_thickness / 2
1334 + (beam_count - 1) * beam_translation;
1335 Real beam_y = stem_y - d * height_of_my_beams;
1337 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1339 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1340 Real minimum_distance
1341 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1342 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1344 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1346 shift /= staff_space;
1347 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1349 /* Always move discretely by half spaces */
1350 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1352 /* Inside staff, move by whole spaces*/
1353 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1355 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1357 shift = ceil (fabs (shift)) * sign (shift);
1359 return scm_make_real (staff_space * shift);
1363 Beam::is_knee (Grob *me)
1365 SCM k = me->get_property ("knee");
1366 if (scm_is_bool (k))
1367 return ly_scm2bool (k);
1371 for (SCM s = me->get_property ("stems"); scm_is_pair (s); s = scm_cdr (s))
1373 Direction dir = get_grob_direction (unsmob_grob (scm_car (s)));
1382 me->set_property ("knee", ly_bool2scm (knee));
1388 Beam::get_direction_beam_count (Grob *me, Direction d)
1390 Link_array<Grob> stems
1391 = extract_grob_array (me, ly_symbol2scm ("stems"));
1394 for (int i = stems.size (); i--;)
1397 Should we take invisible stems into account?
1399 if (Stem::get_direction (stems[i]) == d)
1400 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1406 ADD_INTERFACE (Beam, "beam-interface",
1408 "The @code{thickness} property is the weight of beams, and is measured "
1410 "knee positioning-done position-callbacks "
1411 "concaveness dir-function quant-score auto-knee-gap gap "
1412 "gap-count chord-tremolo beamed-stem-shorten shorten least-squares-dy "
1413 "damping inspect-quants flag-width-function neutral-direction positions space-function "