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);
84 /* Maximum beam_count. */
86 Beam::get_beam_count (Grob *me)
90 extract_grob_set (me, "stems", stems);
91 for (int i = 0; i < stems.size (); i++)
93 Grob *stem = stems[i];
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_from_double (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 extract_grob_set (me, "stems", stems);
143 if (stems.size () == 1)
145 me->warning (_ ("removing beam with less than two stems"));
147 stems[0]->set_object ("beam", SCM_EOL);
150 return SCM_UNSPECIFIED;
152 else if (stems.size () == 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_from_int (k), left_beaming) != SCM_BOOL_F)
205 if (count >= best_count)
216 Beam::connect_beams (Grob *me)
218 extract_grob_set (me, "stems", stems);
221 last_int.set_empty ();
222 SCM last_beaming = SCM_EOL;
223 Direction last_dir = CENTER;
224 for (int i = 0; i < stems.size (); i++)
226 Grob *this_stem = stems[i];
227 SCM this_beaming = this_stem->get_property ("beaming");
229 Direction this_dir = get_grob_direction (this_stem);
230 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
232 int start_point = position_with_maximal_common_beams
233 (last_beaming, this_beaming,
240 if (d == RIGHT && i == stems.size () - 1)
243 new_slice.set_empty ();
244 SCM s = index_get_cell (this_beaming, d);
245 for (; scm_is_pair (s); s = scm_cdr (s))
248 = start_point - this_dir * scm_to_int (scm_car (s));
250 new_slice.add_point (new_beam_pos);
251 scm_set_car_x (s, scm_from_int (new_beam_pos));
254 while (flip (&d) != LEFT);
256 if (!new_slice.is_empty ())
257 last_int = new_slice;
261 scm_set_car_x (this_beaming, SCM_EOL);
262 SCM s = scm_cdr (this_beaming);
263 for (; scm_is_pair (s); s = scm_cdr (s))
265 int np = -this_dir * scm_to_int (scm_car (s));
266 scm_set_car_x (s, scm_from_int (np));
267 last_int.add_point (np);
271 if (i == stems.size () -1)
272 scm_set_cdr_x (this_beaming, SCM_EOL);
274 if (scm_ilength (scm_cdr (this_beaming)) > 0)
276 last_beaming = this_beaming;
283 TODO: should not make beams per stem, but per Y-level; probably when
284 someone wants to sponsor feathered beaming.
286 MAKE_SCHEME_CALLBACK (Beam, print, 1);
288 Beam::print (SCM grob)
290 Spanner *me = unsmob_spanner (grob);
293 extract_grob_set (me, "stems", stems);
294 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
296 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
297 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
300 if (visible_stem_count (me))
302 // ugh -> use commonx
303 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
304 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
308 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
309 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
312 SCM posns = me->get_property ("positions");
313 Drul_array<Real> pos;
314 if (!is_number_pair (posns))
316 programming_error ("no beam positions?");
317 pos = Interval (0, 0);
320 pos = ly_scm2realdrul (posns);
322 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
324 Real dy = pos[RIGHT] - pos[LEFT];
325 Real slope = (dy && dx) ? dy / dx : 0;
327 Real thick = get_thickness (me);
328 Real bdy = get_beam_translation (me);
330 SCM last_beaming = SCM_EOL;
331 Real last_xposn = -1;
332 Real last_stem_width = -1;
334 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
337 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
339 for (int i = 0; i <= stems.size (); i++)
341 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
343 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
344 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
345 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
346 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
348 We do the space left of ST, with lfliebertjes pointing to the
349 right from the left stem, and rfliebertjes pointing left from
352 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
353 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
355 Array<int> full_beams;
356 Array<int> lfliebertjes;
357 Array<int> rfliebertjes;
360 scm_is_pair (s); s = scm_cdr (s))
362 int b = scm_to_int (scm_car (s));
363 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
366 lfliebertjes.push (b);
369 scm_is_pair (s); s = scm_cdr (s))
371 int b = scm_to_int (scm_car (s));
372 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
373 rfliebertjes.push (b);
376 Drul_array<Real> break_overshoot
377 = robust_scm2drul (me->get_property ("break-overshoot"),
378 Drul_array<Real> (-0.5, 0.0));
380 Real w = (i > 0 && stem)
381 ? (xposn - last_xposn)
382 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
384 Real stem_offset = 0.0;
387 w += last_stem_width / 2;
388 stem_offset = -last_stem_width / 2;
394 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
395 Stencil whole = Lookup::beam (slope, w, thick, blot);
399 if (scm_is_number (me->get_property ("gap-count")))
401 gap_count = scm_to_int (me->get_property ("gap-count"));
402 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
404 full_beams.sort (default_compare);
406 full_beams.reverse ();
410 for (int j = full_beams.size (); j--;)
417 b.translate_axis (gap_length, X_AXIS);
419 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
420 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
422 the_beam.add_stencil (b);
425 if (lfliebertjes.size () || rfliebertjes.size ())
431 int t = Stem::duration_log (stem);
433 SCM proc = me->get_property ("flag-width-function");
434 SCM result = scm_call_1 (proc, scm_from_int (t));
435 nw_f = scm_to_double (result);
438 nw_f = break_overshoot[RIGHT] / 2;
440 /* Half beam should be one note-width,
441 but let's make sure two half-beams never touch */
445 rw = min (nw_f, ((xposn - last_xposn) / 2));
447 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
448 + break_overshoot[LEFT];
451 lw = min (nw_f, ((xposn - last_xposn) / 2));
453 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
455 + break_overshoot[RIGHT];
457 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
458 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
459 for (int j = lfliebertjes.size (); j--;)
462 b.translate_axis (last_xposn - x0, X_AXIS);
463 b.translate_axis (slope * (last_xposn - x0) + bdy * lfliebertjes[j], Y_AXIS);
464 the_beam.add_stencil (b);
466 for (int j = rfliebertjes.size (); j--;)
469 b.translate_axis (xposn - x0 - rw, X_AXIS);
470 b.translate_axis (slope * (xposn - x0 - rw)
471 + bdy * rfliebertjes[j], Y_AXIS);
472 the_beam.add_stencil (b);
477 last_stem_width = stem_width;
478 last_beaming = this_beaming;
481 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS), X_AXIS);
482 the_beam.translate_axis (pos[LEFT], Y_AXIS);
485 SCM quant_score = me->get_property ("quant-score");
486 if (to_boolean (me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting")))
487 && scm_is_string (quant_score))
491 This code prints the demerits for each beam. Perhaps this
492 should be switchable for those who want to twiddle with the
496 SCM properties = Font_interface::text_font_alist_chain (me);
498 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
500 Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
501 (me->get_layout ()->self_scm (), properties, quant_score));
502 the_beam.add_at_edge (Y_AXIS, stem_dir, tm, 1.0, 0);
506 return the_beam.smobbed_copy ();
510 Beam::get_default_dir (Grob *me)
512 Drul_array<int> total;
513 total[UP] = total[DOWN] = 0;
514 Drul_array<int> count;
515 count[UP] = count[DOWN] = 0;
518 extract_grob_set (me, "stems", stems);
520 for (int i = 0; i < stems.size (); i++)
524 Direction sd = get_grob_direction (s);
526 int center_distance = max (int (- d * Stem::head_positions (s) [-d]), 0);
527 int current = sd ? (1 + d * sd) / 2 : center_distance;
535 while (flip (&d) != DOWN);
537 SCM func = me->get_property ("dir-function");
538 SCM s = scm_call_2 (func,
539 scm_cons (scm_from_int (count[UP]),
540 scm_from_int (count[DOWN])),
541 scm_cons (scm_from_int (total[UP]),
542 scm_from_int (total[DOWN])));
544 if (scm_is_number (s) && scm_to_int (s))
547 /* If dir is not determined: get default */
548 return to_dir (me->get_property ("neutral-direction"));
551 /* Set all stems with non-forced direction to beam direction.
552 Urg: non-forced should become `without/with unforced' direction,
553 once stem gets cleaned-up. */
555 Beam::set_stem_directions (Grob *me, Direction d)
557 extract_grob_set (me, "stems", stems);
559 for (int i = 0; i < stems.size (); i++)
563 SCM forcedir = s->get_property ("direction");
564 if (!to_dir (forcedir))
565 set_grob_direction (s, d);
570 Only try horizontal beams for knees. No reliable detection of
571 anything else is possible here, since we don't know funky-beaming
572 settings, or X-distances (slopes!) People that want sloped
573 knee-beams, should set the directions manually.
578 this routine should take into account the stemlength scoring
579 of a possible knee/nonknee beam.
582 Beam::consider_auto_knees (Grob *me)
584 SCM scm = me->get_property ("auto-knee-gap");
585 if (!scm_is_number (scm))
592 extract_grob_set (me, "stems", stems);
594 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
595 Real staff_space = Staff_symbol_referencer::staff_space (me);
597 Array<Interval> head_extents_array;
598 for (int i = 0; i < stems.size (); i++)
600 Grob *stem = stems[i];
601 if (Stem::is_invisible (stem))
604 Interval head_extents = Stem::head_positions (stem);
605 if (!head_extents.is_empty ())
607 head_extents[LEFT] += -1;
608 head_extents[RIGHT] += 1;
609 head_extents *= staff_space * 0.5;
612 We could subtract beam Y position, but this routine only
613 sets stem directions, a constant shift does not have an
616 head_extents += stem->relative_coordinate (common, Y_AXIS);
618 if (to_dir (stem->get_property ("direction")))
620 Direction stemdir = to_dir (stem->get_property ("direction"));
621 head_extents[-stemdir] = -stemdir * infinity_f;
624 head_extents_array.push (head_extents);
626 gaps.remove_interval (head_extents);
630 Real max_gap_len = 0.0;
632 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
634 Interval gap = gaps.allowed_regions_[i];
637 the outer gaps are not knees.
639 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
642 if (gap.length () >= max_gap_len)
644 max_gap_len = gap.length ();
649 Real beam_translation = get_beam_translation (me);
650 Real beam_thickness = Beam::get_thickness (me);
651 int beam_count = Beam::get_beam_count (me);
652 Real height_of_beams = beam_thickness / 2
653 + (beam_count - 1) * beam_translation;
654 Real threshold = scm_to_double (scm) + height_of_beams;
656 if (max_gap_len > threshold)
659 for (int i = 0; i < stems.size (); i++)
661 Grob *stem = stems[i];
662 if (Stem::is_invisible (stem))
665 Interval head_extents = head_extents_array[j++];
667 Direction d = (head_extents.center () < max_gap.center ())
670 stem->set_property ("direction", scm_from_int (d));
672 head_extents.intersect (max_gap);
673 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
678 /* Set stem's shorten property if unset.
681 take some y-position (chord/beam/nearest?) into account
682 scmify forced-fraction
684 This is done in beam because the shorten has to be uniform over the
688 Beam::set_stem_shorten (Grob *me)
691 shortening looks silly for x staff beams
696 Real forced_fraction = 1.0 * forced_stem_count (me)
697 / visible_stem_count (me);
699 int beam_count = get_beam_count (me);
701 SCM shorten_list = me->get_property ("beamed-stem-shorten");
702 if (shorten_list == SCM_EOL)
705 Real staff_space = Staff_symbol_referencer::staff_space (me);
708 = robust_list_ref (beam_count -1, shorten_list);
709 Real shorten_f = scm_to_double (shorten_elt) * staff_space;
711 /* your similar cute comment here */
712 shorten_f *= forced_fraction;
715 me->set_property ("shorten", scm_from_double (shorten_f));
718 /* Call list of y-dy-callbacks, that handle setting of
721 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
723 Beam::after_line_breaking (SCM smob)
725 Grob *me = unsmob_grob (smob);
728 return SCM_UNSPECIFIED;
732 Beam::position_beam (Grob *me)
736 if (to_boolean (me->get_property ("positioning-done")))
739 me->set_property ("positioning-done", SCM_BOOL_T);
741 /* Copy to mutable list. */
742 SCM s = ly_deep_copy (me->get_property ("positions"));
743 me->set_property ("positions", s);
745 if (scm_car (s) == SCM_BOOL_F)
747 // one wonders if such genericity is necessary --hwn.
748 SCM callbacks = me->get_property ("position-callbacks");
749 for (SCM i = callbacks; scm_is_pair (i); i = scm_cdr (i))
750 scm_call_1 (scm_car (i), me->self_scm ());
753 set_stem_lengths (me);
757 set_minimum_dy (Grob *me, Real *dy)
762 If dy is smaller than the smallest quant, we
763 get absurd direction-sign penalties.
766 Real ss = Staff_symbol_referencer::staff_space (me);
767 Real thickness = Beam::get_thickness (me) / ss;
768 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
769 Real sit = (thickness - slt) / 2;
771 Real hang = 1.0 - (thickness - slt) / 2;
773 *dy = sign (*dy) * max (fabs (*dy),
774 min (min (sit, inter), hang));
779 Compute a first approximation to the beam slope.
781 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
783 Beam::least_squares (SCM smob)
785 Grob *me = unsmob_grob (smob);
787 int count = visible_stem_count (me);
792 me->set_property ("positions", ly_interval2scm (pos));
793 return SCM_UNSPECIFIED;
797 extract_grob_set (me, "stems", stems);
798 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
799 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
801 Real my_y = me->relative_coordinate (commony, Y_AXIS);
803 Grob *fvs = first_visible_stem (me);
804 Grob *lvs = last_visible_stem (me);
806 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
807 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
808 Stem::get_stem_info (lvs).ideal_y_
809 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
811 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
812 for (int i = 0; i < stems.size (); i++)
816 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
819 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
827 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
828 Stem::chord_start_y (last_visible_stem (me)));
830 /* Simple beams (2 stems) on middle line should be allowed to be
833 However, if both stems reach middle line,
834 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
836 For that case, we apply artificial slope */
837 if (!ideal[LEFT] && chord.delta () && count == 2)
840 Direction d = (Direction) (sign (chord.delta ()) * UP);
841 pos[d] = get_thickness (me) / 2;
848 For broken beams this doesn't work well. In this case, the
849 slope esp. of the first part of a broken beam should predict
850 where the second part goes.
852 me->set_property ("least-squares-dy",
853 scm_from_double (pos[RIGHT] - pos[LEFT]));
857 Array<Offset> ideals;
858 for (int i = 0; i < stems.size (); i++)
861 if (Stem::is_invisible (s))
863 ideals.push (Offset (x_posns[i],
864 Stem::get_stem_info (s).ideal_y_
865 + s->relative_coordinate (commony, Y_AXIS)
869 minimise_least_squares (&slope, &y, ideals);
873 set_minimum_dy (me, &dy);
874 me->set_property ("least-squares-dy", scm_from_double (dy));
875 pos = Interval (y, (y + dy));
879 "position" is relative to the staff.
881 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
883 me->set_property ("positions", ly_interval2scm (pos));
885 return SCM_UNSPECIFIED;
889 We can't combine with previous function, since check concave and
890 slope damping comes first.
892 TODO: we should use the concaveness to control the amount of damping
895 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
897 Beam::shift_region_to_valid (SCM grob)
899 Grob *me = unsmob_grob (grob);
904 extract_grob_set (me, "stems", stems);
905 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
906 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
908 Grob *fvs = first_visible_stem (me);
911 return SCM_UNSPECIFIED;
913 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
914 for (int i = 0; i < stems.size (); i++)
918 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
922 Grob *lvs = last_visible_stem (me);
924 return SCM_UNSPECIFIED;
926 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
928 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
930 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
932 Real dy = pos[RIGHT] - pos[LEFT];
934 Real slope = dx ? (dy / dx) : 0.0;
937 Shift the positions so that we have a chance of finding good
938 quants (i.e. no short stem failures.)
940 Interval feasible_left_point;
941 feasible_left_point.set_full ();
942 for (int i = 0; i < stems.size (); i++)
945 if (Stem::is_invisible (s))
948 Direction d = Stem::get_direction (s);
951 = Stem::get_stem_info (s).shortest_y_
952 - slope * x_posns [i];
955 left_y is now relative to the stem S. We want relative to
956 ourselves, so translate:
959 += + s->relative_coordinate (commony, Y_AXIS)
960 - me->relative_coordinate (commony, Y_AXIS);
966 feasible_left_point.intersect (flp);
969 if (feasible_left_point.is_empty ())
970 warning (_ ("no viable initial configuration found: may not find good beam slope"));
971 else if (!feasible_left_point.contains (y))
973 const int REGION_SIZE = 2; // UGH UGH
974 if (isinf (feasible_left_point[DOWN]))
975 y = feasible_left_point[UP] - REGION_SIZE;
976 else if (isinf (feasible_left_point[UP]))
977 y = feasible_left_point[DOWN]+ REGION_SIZE;
979 y = feasible_left_point.center ();
982 pos = Drul_array<Real> (y, (y + dy));
983 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
985 me->set_property ("positions", ly_interval2scm (pos));
986 return SCM_UNSPECIFIED;
989 /* This neat trick is by Werner Lemberg,
990 damped = tanh (slope)
991 corresponds with some tables in [Wanske] CHECKME */
992 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
994 Beam::slope_damping (SCM smob)
996 Grob *me = unsmob_grob (smob);
998 if (visible_stem_count (me) <= 1)
999 return SCM_UNSPECIFIED;
1001 SCM s = me->get_property ("damping");
1002 Real damping = scm_to_double (s);
1006 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
1007 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1009 Real dy = pos[RIGHT] - pos[LEFT];
1011 Grob *fvs = first_visible_stem (me);
1012 Grob *lvs = last_visible_stem (me);
1014 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1016 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1017 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1019 Real slope = dy && dx ? dy / dx : 0;
1021 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1023 slope = 0.6 * tanh (slope) / (damping + concaveness);
1025 Real damped_dy = slope * dx;
1027 set_minimum_dy (me, &damped_dy);
1029 pos[LEFT] += (dy - damped_dy) / 2;
1030 pos[RIGHT] -= (dy - damped_dy) / 2;
1032 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1034 me->set_property ("positions", ly_interval2scm (pos));
1036 return SCM_UNSPECIFIED;
1040 Report slice containing the numbers that are both in (car BEAMING)
1044 where_are_the_whole_beams (SCM beaming)
1048 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1050 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1052 l.add_point (scm_to_int (scm_car (s)));
1058 /* Return the Y position of the stem-end, given the Y-left, Y-right
1059 in POS for stem S. This Y position is relative to S. */
1061 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1063 Drul_array<Real> pos, bool french)
1065 Real beam_translation = get_beam_translation (me);
1067 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1068 Real dy = pos[RIGHT] - pos[LEFT];
1070 Real stem_y_beam0 = (dy && dx
1075 Direction my_dir = get_grob_direction (s);
1076 SCM beaming = s->get_property ("beaming");
1078 Real stem_y = stem_y_beam0;
1081 Slice bm = where_are_the_whole_beams (beaming);
1082 if (!bm.is_empty ())
1083 stem_y += beam_translation * bm[-my_dir];
1087 Slice bm = Stem::beam_multiplicity (s);
1088 if (!bm.is_empty ())
1089 stem_y += bm[my_dir] * beam_translation;
1092 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1093 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1099 Hmm. At this time, beam position and slope are determined. Maybe,
1100 stem directions and length should set to relative to the chord's
1101 position of the beam. */
1103 Beam::set_stem_lengths (Grob *me)
1105 extract_grob_set (me, "stems", stems);
1110 for (int a = 2; a--;)
1111 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1113 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1114 Real staff_space = Staff_symbol_referencer::staff_space (me);
1115 scale_drul (&pos, staff_space);
1119 if (scm_is_number (me->get_property ("gap-count"))
1120 &&scm_to_int (me->get_property ("gap-count")))
1123 thick = get_thickness (me);
1126 // ugh -> use commonx
1127 Grob *fvs = first_visible_stem (me);
1128 Grob *lvs = last_visible_stem (me);
1130 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1131 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1133 for (int i = 0; i < stems.size (); i++)
1136 if (Stem::is_invisible (s))
1139 bool french = to_boolean (s->get_property ("french-beaming"));
1140 Real stem_y = calc_stem_y (me, s, common,
1142 pos, french && s != lvs && s!= fvs);
1145 Make the stems go up to the end of the beam. This doesn't matter
1146 for normal beams, but for tremolo beams it looks silly otherwise.
1149 stem_y += thick * 0.5 * get_grob_direction (s);
1151 Stem::set_stemend (s, 2 * stem_y / staff_space);
1156 Beam::set_beaming (Grob *me, Beaming_info_list *beaming)
1158 extract_grob_set (me, "stems", stems);
1161 for (int i = 0; i < stems.size (); i++)
1164 Don't overwrite user settings.
1169 /* Don't set beaming for outside of outer stems */
1170 if ((d == LEFT && i == 0)
1171 || (d == RIGHT && i == stems.size () -1))
1174 Grob *stem = stems[i];
1175 SCM beaming_prop = stem->get_property ("beaming");
1176 if (beaming_prop == SCM_EOL
1177 || index_get_cell (beaming_prop, d) == SCM_EOL)
1179 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1181 && i < stems.size () -1
1182 && Stem::is_invisible (stem))
1183 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1185 Stem::set_beaming (stem, b, d);
1188 while (flip (&d) != LEFT);
1193 Beam::forced_stem_count (Grob *me)
1195 extract_grob_set (me, "stems", stems);
1198 for (int i = 0; i < stems.size (); i++)
1202 if (Stem::is_invisible (s))
1205 /* I can imagine counting those boundaries as a half forced stem,
1206 but let's count them full for now. */
1207 if (abs (Stem::chord_start_y (s)) > 0.1
1208 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1215 Beam::visible_stem_count (Grob *me)
1217 extract_grob_set (me, "stems", stems);
1219 for (int i = stems.size (); i--;)
1221 if (!Stem::is_invisible (stems[i]))
1228 Beam::first_visible_stem (Grob *me)
1230 extract_grob_set (me, "stems", stems);
1232 for (int i = 0; i < stems.size (); i++)
1234 if (!Stem::is_invisible (stems[i]))
1241 Beam::last_visible_stem (Grob *me)
1243 extract_grob_set (me, "stems", stems);
1245 for (int i = stems.size (); i--;)
1247 if (!Stem::is_invisible (stems[i]))
1256 handle rest under beam (do_post: beams are calculated now)
1257 what about combination of collisions and rest under beam.
1261 rest -> stem -> beam -> interpolate_y_position ()
1263 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1265 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1267 Grob *rest = unsmob_grob (element_smob);
1270 if (scm_is_number (rest->get_property ("staff-position")))
1271 return scm_from_int (0);
1273 assert (scm_to_int (axis) == Y_AXIS);
1275 Grob *st = unsmob_grob (rest->get_object ("stem"));
1278 return scm_from_double (0.0);
1279 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1281 || !Beam::has_interface (beam)
1282 || !Beam::visible_stem_count (beam))
1283 return scm_from_double (0.0);
1285 Drul_array<Real> pos (0, 0);
1286 SCM s = beam->get_property ("positions");
1287 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1288 pos = ly_scm2interval (s);
1292 UGH. TODO: fix dependency tracking.
1294 position_beam (beam);
1295 pos = ly_scm2interval (beam->get_property ("positions"));
1298 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1300 scale_drul (&pos, staff_space);
1302 Real dy = pos[RIGHT] - pos[LEFT];
1304 // ugh -> use commonx
1305 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1306 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1307 Real slope = dy && dx ? dy / dx : 0;
1309 Direction d = Stem::get_direction (stem);
1310 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1312 Real beam_translation = get_beam_translation (beam);
1313 Real beam_thickness = Beam::get_thickness (beam);
1316 TODO: this is not strictly correct for 16th knee beams.
1319 = Stem::beam_multiplicity (stem).length () + 1;
1321 Real height_of_my_beams = beam_thickness / 2
1322 + (beam_count - 1) * beam_translation;
1323 Real beam_y = stem_y - d * height_of_my_beams;
1325 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1327 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1328 Real minimum_distance
1329 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1330 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1332 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1334 shift /= staff_space;
1335 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1337 /* Always move discretely by half spaces */
1338 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1340 /* Inside staff, move by whole spaces*/
1341 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1343 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1345 shift = ceil (fabs (shift)) * sign (shift);
1347 return scm_from_double (staff_space * shift);
1351 Beam::is_knee (Grob *me)
1353 SCM k = me->get_property ("knee");
1354 if (scm_is_bool (k))
1355 return ly_scm2bool (k);
1359 extract_grob_set (me, "stems", stems);
1360 for (int i = stems.size (); i--;)
1362 Direction dir = get_grob_direction (stems[i]);
1371 me->set_property ("knee", ly_bool2scm (knee));
1377 Beam::get_direction_beam_count (Grob *me, Direction d)
1379 extract_grob_set (me, "stems", stems);
1382 for (int i = stems.size (); i--;)
1385 Should we take invisible stems into account?
1387 if (Stem::get_direction (stems[i]) == d)
1388 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1394 ADD_INTERFACE (Beam, "beam-interface",
1396 "The @code{thickness} property is the weight of beams, and is measured "
1398 "knee positioning-done position-callbacks "
1399 "concaveness dir-function quant-score auto-knee-gap gap "
1400 "gap-count chord-tremolo beamed-stem-shorten shorten least-squares-dy "
1401 "details damping inspect-quants flag-width-function "
1402 "neutral-direction positions space-function break-overshoot "