2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.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.
30 #include "directional-element-interface.hh"
31 #include "international.hh"
32 #include "interval-set.hh"
34 #include "least-squares.hh"
37 #include "output-def.hh"
38 #include "pointer-group-interface.hh"
40 #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 if (Stem::get_beam (s))
54 programming_error ("Stem already has beam");
58 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
59 s->set_object ("beam", me->self_scm ());
60 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
64 Beam::get_thickness (Grob *me)
66 return robust_scm2double (me->get_property ("thickness"), 0)
67 * Staff_symbol_referencer::staff_space (me);
70 /* Return the translation between 2 adjoining beams. */
72 Beam::get_beam_translation (Grob *me)
74 int beam_count = get_beam_count (me);
75 Real staff_space = Staff_symbol_referencer::staff_space (me);
76 Real line = Staff_symbol_referencer::line_thickness (me);
77 Real thickness = get_thickness (me);
78 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
80 Real beam_translation = beam_count < 4
81 ? (2 * staff_space + line - thickness) / 2.0
82 : (3 * staff_space + line - thickness) / 3.0;
84 return fract * beam_translation;
87 /* Maximum beam_count. */
89 Beam::get_beam_count (Grob *me)
93 extract_grob_set (me, "stems", stems);
94 for (vsize i = 0; i < stems.size (); i++)
96 Grob *stem = stems[i];
97 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
103 /* After pre-processing all directions should be set.
104 Several post-processing routines (stem, slur, script) need stem/beam
106 Currenly, this means that beam has set all stem's directions.
107 [Alternatively, stems could set its own directions, according to
108 their beam, during 'final-pre-processing'.] */
109 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
111 Beam::calc_direction (SCM smob)
113 Grob *me = unsmob_grob (smob);
115 /* Beams with less than 2 two stems don't make much sense, but could happen
120 For a beam that only has one stem, we try to do some disappearance magic:
121 we revert the flag, and move on to The Eternal Engraving Fields. */
123 Direction d = CENTER;
125 int count = visible_stem_count (me);
128 extract_grob_set (me, "stems", stems);
129 if (stems.size () == 1)
131 me->warning (_ ("removing beam with less than two stems"));
133 stems[0]->set_object ("beam", SCM_EOL);
136 return SCM_UNSPECIFIED;
138 else if (stems.size () == 0)
141 return SCM_UNSPECIFIED;
145 Grob *stem = first_visible_stem (me);
146 d = to_dir (stem->get_property ("default-direction"));
153 d = get_default_dir (me);
155 consider_auto_knees (me);
160 set_stem_directions (me, d);
163 return scm_from_int (d);
168 /* We want a maximal number of shared beams, but if there is choice, we
169 * take the one that is closest to the end of the stem. This is for
181 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
185 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
189 for (int i = lslice[-left_dir];
190 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
193 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
195 int k = -right_dir * scm_to_int (scm_car (s)) + i;
196 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
200 if (count >= best_count)
210 MAKE_SCHEME_CALLBACK(Beam, calc_beaming, 1)
212 Beam::calc_beaming (SCM smob)
214 Grob *me = unsmob_grob (smob);
216 extract_grob_set (me, "stems", stems);
219 last_int.set_empty ();
221 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
222 Direction last_dir = CENTER;
223 for (vsize i = 0; i < stems.size (); i++)
225 Grob *this_stem = stems[i];
226 SCM this_beaming = this_stem->get_property ("beaming");
228 Direction this_dir = get_grob_direction (this_stem);
229 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
231 int start_point = position_with_maximal_common_beams
232 (last_beaming, this_beaming,
233 last_dir ? last_dir : this_dir,
240 new_slice.set_empty ();
241 SCM s = index_get_cell (this_beaming, d);
242 for (; scm_is_pair (s); s = scm_cdr (s))
245 = start_point - this_dir * scm_to_int (scm_car (s));
247 new_slice.add_point (new_beam_pos);
248 scm_set_car_x (s, scm_from_int (new_beam_pos));
251 while (flip (&d) != LEFT);
253 if (!new_slice.is_empty ())
254 last_int = new_slice;
258 SCM s = scm_cdr (this_beaming);
259 for (; scm_is_pair (s); s = scm_cdr (s))
261 int np = -this_dir * scm_to_int (scm_car (s));
262 scm_set_car_x (s, scm_from_int (np));
263 last_int.add_point (np);
267 if (scm_ilength (scm_cdr (this_beaming)) > 0)
269 last_beaming = this_beaming;
278 I really enjoy spaghetti, but spaghetti should be kept on a plate
279 with a little garlic and olive oil. This is too much.
283 MAKE_SCHEME_CALLBACK (Beam, print, 1);
285 Beam::print (SCM grob)
287 Spanner *me = unsmob_spanner (grob);
289 extract_grob_set (me, "stems", stems);
290 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
292 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
293 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
296 if (visible_stem_count (me))
298 // ugh -> use commonx
299 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
300 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
304 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
305 dx = stems.back ()->relative_coordinate (xcommon, X_AXIS) - x0;
308 SCM posns = me->get_property ("quantized-positions");
309 Drul_array<Real> pos;
310 if (!is_number_pair (posns))
312 programming_error ("no beam positions?");
313 pos = Interval (0, 0);
316 pos = ly_scm2realdrul (posns);
318 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
320 Real dy = pos[RIGHT] - pos[LEFT];
321 Real slope = (dy && dx) ? dy / dx : 0;
323 Real thick = get_thickness (me);
324 Real bdy = get_beam_translation (me);
326 SCM last_beaming = SCM_EOL;
327 Real last_xposn = -1;
328 Real last_stem_width = -1;
330 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
333 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
335 for (vsize i = 0; i <= stems.size (); i++)
337 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
339 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
340 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
341 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
342 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
344 We do the space left of ST, with lfliebertjes pointing to the
345 right from the left stem, and rfliebertjes pointing left from
348 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
349 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
351 std::vector<int> full_beams;
352 std::vector<int> lfliebertjes;
353 std::vector<int> rfliebertjes;
356 scm_is_pair (s); s = scm_cdr (s))
358 int b = scm_to_int (scm_car (s));
359 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
360 full_beams.push_back (b);
362 lfliebertjes.push_back (b);
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), left) == SCM_BOOL_F)
369 rfliebertjes.push_back (b);
372 Drul_array<Real> break_overshoot
373 = robust_scm2drul (me->get_property ("break-overshoot"),
374 Drul_array<Real> (-0.5, 0.0));
376 Real w = (i > 0 && stem)
377 ? (xposn - last_xposn)
378 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
380 Real stem_offset = 0.0;
383 w += last_stem_width / 2;
384 stem_offset = -last_stem_width / 2;
390 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
391 Stencil whole = Lookup::beam (slope, w, thick, blot);
395 if (scm_is_number (me->get_property ("gap-count")))
397 gap_count = scm_to_int (me->get_property ("gap-count"));
398 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
400 full_beams.sort (default_compare);
402 full_beams.reverse ();
406 for (vsize j = full_beams.size (); j--;)
413 b.translate_axis (gap_length, X_AXIS);
415 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
416 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
418 the_beam.add_stencil (b);
421 if (lfliebertjes.size () || rfliebertjes.size ())
427 int t = Stem::duration_log (stem);
435 nw_f = break_overshoot[RIGHT] / 2;
437 /* Half beam should be one note-width,
438 but let's make sure two half-beams never touch */
442 rw = min (nw_f, ((xposn - last_xposn) / 2));
445 if (me->get_bound (LEFT)->break_status_dir ())
446 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
447 + break_overshoot[LEFT];
453 lw = min (nw_f, ((xposn - last_xposn) / 2));
456 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
458 + break_overshoot[RIGHT];
460 rw += stem_width / 2;
461 lw += last_stem_width / 2;
463 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
464 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
465 for (vsize j = lfliebertjes.size (); j--;)
468 b.translate_axis (last_xposn - x0 - last_stem_width /2,
470 b.translate_axis (slope * (last_xposn - x0)
471 + bdy * lfliebertjes[j],
473 the_beam.add_stencil (b);
475 for (vsize j = rfliebertjes.size (); j--;)
478 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
479 b.translate_axis (slope * (xposn - x0 - rw)
480 + bdy * rfliebertjes[j], Y_AXIS);
481 the_beam.add_stencil (b);
486 last_stem_width = stem_width;
487 last_beaming = this_beaming;
490 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
492 the_beam.translate_axis (pos[LEFT], Y_AXIS);
495 SCM quant_score = me->get_property ("quant-score");
496 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"));
497 if (to_boolean (debug) && 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 score = *unsmob_stencil (Text_interface::interpret_markup
511 (me->layout ()->self_scm (), properties, quant_score));
513 if (!score.is_empty ())
514 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0, 0);
518 return the_beam.smobbed_copy ();
522 Beam::get_default_dir (Grob *me)
524 Drul_array<int> total;
525 total[UP] = total[DOWN] = 0;
526 Drul_array<int> count;
527 count[UP] = count[DOWN] = 0;
529 extract_grob_set (me, "stems", stems);
531 for (vsize i = 0; i < stems.size (); i++)
534 Direction stem_dir = CENTER;
535 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
536 if (is_direction (stem_dir_scm))
537 stem_dir = to_dir (stem_dir_scm);
539 stem_dir = to_dir (s->get_property ("default-direction"));
542 stem_dir = to_dir (s->get_property ("neutral-direction"));
547 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
551 Direction dir = CENTER;
552 Direction d = CENTER;
553 if ((d = (Direction) sign (count[UP] - count[DOWN])))
557 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
559 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
562 dir = to_dir (me->get_property ("neutral-direction"));
567 /* Set all stems with non-forced direction to beam direction.
568 Urg: non-forced should become `without/with unforced' direction,
569 once stem gets cleaned-up. */
571 Beam::set_stem_directions (Grob *me, Direction d)
573 extract_grob_set (me, "stems", stems);
575 for (vsize i = 0; i < stems.size (); i++)
579 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
580 if (!to_dir (forcedir))
581 set_grob_direction (s, d);
586 Only try horizontal beams for knees. No reliable detection of
587 anything else is possible here, since we don't know funky-beaming
588 settings, or X-distances (slopes!) People that want sloped
589 knee-beams, should set the directions manually.
594 this routine should take into account the stemlength scoring
595 of a possible knee/nonknee beam.
598 Beam::consider_auto_knees (Grob *me)
600 SCM scm = me->get_property ("auto-knee-gap");
601 if (!scm_is_number (scm))
608 extract_grob_set (me, "stems", stems);
610 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
611 Real staff_space = Staff_symbol_referencer::staff_space (me);
613 std::vector<Interval> head_extents_array;
614 for (vsize i = 0; i < stems.size (); i++)
616 Grob *stem = stems[i];
617 if (Stem::is_invisible (stem))
620 Interval head_extents = Stem::head_positions (stem);
621 if (!head_extents.is_empty ())
623 head_extents[LEFT] += -1;
624 head_extents[RIGHT] += 1;
625 head_extents *= staff_space * 0.5;
628 We could subtract beam Y position, but this routine only
629 sets stem directions, a constant shift does not have an
632 head_extents += stem->relative_coordinate (common, Y_AXIS);
634 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
636 Direction stemdir = to_dir (stem->get_property ("direction"));
637 head_extents[-stemdir] = -stemdir * infinity_f;
640 head_extents_array.push_back (head_extents);
642 gaps.remove_interval (head_extents);
646 Real max_gap_len = 0.0;
648 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
650 Interval gap = gaps.allowed_regions_[i];
653 the outer gaps are not knees.
655 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
658 if (gap.length () >= max_gap_len)
660 max_gap_len = gap.length ();
665 Real beam_translation = get_beam_translation (me);
666 Real beam_thickness = Beam::get_thickness (me);
667 int beam_count = Beam::get_beam_count (me);
668 Real height_of_beams = beam_thickness / 2
669 + (beam_count - 1) * beam_translation;
670 Real threshold = scm_to_double (scm) + height_of_beams;
672 if (max_gap_len > threshold)
675 for (vsize i = 0; i < stems.size (); i++)
677 Grob *stem = stems[i];
678 if (Stem::is_invisible (stem))
681 Interval head_extents = head_extents_array[j++];
683 Direction d = (head_extents.center () < max_gap.center ())
686 stem->set_property ("direction", scm_from_int (d));
688 head_extents.intersect (max_gap);
689 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
694 /* Set stem's shorten property if unset.
697 take some y-position (chord/beam/nearest?) into account
698 scmify forced-fraction
700 This is done in beam because the shorten has to be uniform over the
707 set_minimum_dy (Grob *me, Real *dy)
712 If dy is smaller than the smallest quant, we
713 get absurd direction-sign penalties.
716 Real ss = Staff_symbol_referencer::staff_space (me);
717 Real thickness = Beam::get_thickness (me) / ss;
718 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
719 Real sit = (thickness - slt) / 2;
721 Real hang = 1.0 - (thickness - slt) / 2;
723 *dy = sign (*dy) * max (fabs (*dy),
724 min (min (sit, inter), hang));
730 MAKE_SCHEME_CALLBACK(Beam, calc_stem_shorten, 1)
732 Beam::calc_stem_shorten (SCM smob)
734 Grob *me = unsmob_grob (smob);
737 shortening looks silly for x staff beams
740 return scm_from_int (0);
742 Real forced_fraction = 1.0 * forced_stem_count (me)
743 / visible_stem_count (me);
745 int beam_count = get_beam_count (me);
747 SCM shorten_list = me->get_property ("beamed-stem-shorten");
748 if (shorten_list == SCM_EOL)
749 return scm_from_int (0);
751 Real staff_space = Staff_symbol_referencer::staff_space (me);
754 = robust_list_ref (beam_count -1, shorten_list);
755 Real shorten = scm_to_double (shorten_elt) * staff_space;
757 shorten *= forced_fraction;
761 return scm_from_double (shorten);
763 return scm_from_double (0.0);
769 Compute a first approximation to the beam slope.
771 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
773 Beam::calc_least_squares_positions (SCM smob, SCM posns)
777 Grob *me = unsmob_grob (smob);
779 int count = visible_stem_count (me);
782 return ly_interval2scm (pos);
784 std::vector<Real> x_posns;
785 extract_grob_set (me, "stems", stems);
786 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
787 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
789 Real my_y = me->relative_coordinate (commony, Y_AXIS);
791 Grob *fvs = first_visible_stem (me);
792 Grob *lvs = last_visible_stem (me);
794 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
795 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
796 Stem::get_stem_info (lvs).ideal_y_
797 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
799 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
800 for (vsize i = 0; i < stems.size (); i++)
804 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
805 x_posns.push_back (x);
807 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
815 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
816 Stem::chord_start_y (last_visible_stem (me)));
818 /* Simple beams (2 stems) on middle line should be allowed to be
821 However, if both stems reach middle line,
822 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
824 For that case, we apply artificial slope */
825 if (!ideal[LEFT] && chord.delta () && count == 2)
828 Direction d = (Direction) (sign (chord.delta ()) * UP);
829 pos[d] = get_thickness (me) / 2;
836 For broken beams this doesn't work well. In this case, the
837 slope esp. of the first part of a broken beam should predict
838 where the second part goes.
840 ldy = pos[RIGHT] - pos[LEFT];
844 std::vector<Offset> ideals;
845 for (vsize i = 0; i < stems.size (); i++)
848 if (Stem::is_invisible (s))
850 ideals.push_back (Offset (x_posns[i],
851 Stem::get_stem_info (s).ideal_y_
852 + s->relative_coordinate (commony, Y_AXIS)
856 minimise_least_squares (&slope, &y, ideals);
860 set_minimum_dy (me, &dy);
863 pos = Interval (y, (y + dy));
867 "position" is relative to the staff.
869 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
871 me->set_property ("least-squares-dy", scm_from_double (ldy));
872 return ly_interval2scm (pos);
876 We can't combine with previous function, since check concave and
877 slope damping comes first.
879 TODO: we should use the concaveness to control the amount of damping
882 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
884 Beam::shift_region_to_valid (SCM grob, SCM posns)
886 Grob *me = unsmob_grob (grob);
890 std::vector<Real> x_posns;
891 extract_grob_set (me, "stems", stems);
892 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
893 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
895 Grob *fvs = first_visible_stem (me);
900 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
901 for (vsize i = 0; i < stems.size (); i++)
905 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
906 x_posns.push_back (x);
909 Grob *lvs = last_visible_stem (me);
913 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
915 Drul_array<Real> pos = ly_scm2interval (posns);
918 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
920 Real dy = pos[RIGHT] - pos[LEFT];
922 Real slope = dx ? (dy / dx) : 0.0;
925 Shift the positions so that we have a chance of finding good
926 quants (i.e. no short stem failures.)
928 Interval feasible_left_point;
929 feasible_left_point.set_full ();
930 for (vsize i = 0; i < stems.size (); i++)
933 if (Stem::is_invisible (s))
936 Direction d = get_grob_direction (s);
939 = Stem::get_stem_info (s).shortest_y_
940 - slope * x_posns [i];
943 left_y is now relative to the stem S. We want relative to
944 ourselves, so translate:
947 += + s->relative_coordinate (commony, Y_AXIS)
948 - me->relative_coordinate (commony, Y_AXIS);
954 feasible_left_point.intersect (flp);
957 if (feasible_left_point.is_empty ())
958 warning (_ ("no viable initial configuration found: may not find good beam slope"));
959 else if (!feasible_left_point.contains (y))
961 const int REGION_SIZE = 2; // UGH UGH
962 if (isinf (feasible_left_point[DOWN]))
963 y = feasible_left_point[UP] - REGION_SIZE;
964 else if (isinf (feasible_left_point[UP]))
965 y = feasible_left_point[DOWN]+ REGION_SIZE;
967 y = feasible_left_point.center ();
970 pos = Drul_array<Real> (y, (y + dy));
971 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
973 return ly_interval2scm (pos);
976 /* This neat trick is by Werner Lemberg,
977 damped = tanh (slope)
978 corresponds with some tables in [Wanske] CHECKME */
979 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
981 Beam::slope_damping (SCM smob, SCM posns)
983 Grob *me = unsmob_grob (smob);
984 Drul_array<Real> pos = ly_scm2interval (posns);
986 if (visible_stem_count (me) <= 1)
990 SCM s = me->get_property ("damping");
991 Real damping = scm_to_double (s);
992 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
993 if (concaveness >= 10000)
995 pos[LEFT] = pos[RIGHT];
996 me->set_property ("least-squares-dy", scm_from_double (0));
1002 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1004 Real dy = pos[RIGHT] - pos[LEFT];
1006 Grob *fvs = first_visible_stem (me);
1007 Grob *lvs = last_visible_stem (me);
1009 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1011 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1012 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1014 Real slope = dy && dx ? dy / dx : 0;
1016 slope = 0.6 * tanh (slope) / (damping + concaveness);
1018 Real damped_dy = slope * dx;
1020 set_minimum_dy (me, &damped_dy);
1022 pos[LEFT] += (dy - damped_dy) / 2;
1023 pos[RIGHT] -= (dy - damped_dy) / 2;
1025 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1028 return ly_interval2scm (pos);
1032 Report slice containing the numbers that are both in (car BEAMING)
1036 where_are_the_whole_beams (SCM beaming)
1040 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1042 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1044 l.add_point (scm_to_int (scm_car (s)));
1050 /* Return the Y position of the stem-end, given the Y-left, Y-right
1051 in POS for stem S. This Y position is relative to S. */
1053 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1055 Drul_array<Real> pos, bool french)
1057 Real beam_translation = get_beam_translation (me);
1059 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1060 Real dy = pos[RIGHT] - pos[LEFT];
1062 Real stem_y_beam0 = (dy && dx
1067 Direction my_dir = get_grob_direction (s);
1068 SCM beaming = s->get_property ("beaming");
1070 Real stem_y = stem_y_beam0;
1073 Slice bm = where_are_the_whole_beams (beaming);
1074 if (!bm.is_empty ())
1075 stem_y += beam_translation * bm[-my_dir];
1079 Slice bm = Stem::beam_multiplicity (s);
1080 if (!bm.is_empty ())
1081 stem_y += bm[my_dir] * beam_translation;
1084 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1085 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1091 Hmm. At this time, beam position and slope are determined. Maybe,
1092 stem directions and length should set to relative to the chord's
1093 position of the beam. */
1094 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 1);
1096 Beam::set_stem_lengths (SCM smob)
1098 Grob *me = unsmob_grob (smob);
1100 /* trigger callback. */
1101 (void) me->get_property ("direction");
1103 SCM posns = me->get_property ("positions");
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 (posns);
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 Grob *fvs = first_visible_stem (me);
1127 Grob *lvs = last_visible_stem (me);
1129 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1130 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1132 for (vsize i = 0; i < stems.size (); i++)
1135 if (Stem::is_invisible (s))
1138 bool french = to_boolean (s->get_property ("french-beaming"));
1139 Real stem_y = calc_stem_y (me, s, common,
1141 pos, french && s != lvs && s!= fvs);
1144 Make the stems go up to the end of the beam. This doesn't matter
1145 for normal beams, but for tremolo beams it looks silly otherwise.
1148 stem_y += thick * 0.5 * get_grob_direction (s);
1150 Stem::set_stemend (s, 2 * stem_y / staff_space);
1157 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1159 extract_grob_set (me, "stems", stems);
1162 for (vsize i = 0; i < stems.size (); i++)
1165 Don't overwrite user settings.
1169 Grob *stem = stems[i];
1170 SCM beaming_prop = stem->get_property ("beaming");
1171 if (beaming_prop == SCM_EOL
1172 || index_get_cell (beaming_prop, d) == SCM_EOL)
1174 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1176 && i < stems.size () -1
1177 && Stem::is_invisible (stem))
1178 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1180 Stem::set_beaming (stem, b, d);
1183 while (flip (&d) != LEFT);
1188 Beam::forced_stem_count (Grob *me)
1190 extract_grob_set (me, "stems", stems);
1193 for (vsize i = 0; i < stems.size (); i++)
1197 if (Stem::is_invisible (s))
1200 /* I can imagine counting those boundaries as a half forced stem,
1201 but let's count them full for now. */
1202 Direction defdir = to_dir (s->get_property ("default-direction"));
1204 if (abs (Stem::chord_start_y (s)) > 0.1
1206 && get_grob_direction (s) != defdir)
1213 Beam::visible_stem_count (Grob *me)
1215 extract_grob_set (me, "stems", stems);
1217 for (vsize i = stems.size (); i--;)
1219 if (!Stem::is_invisible (stems[i]))
1226 Beam::first_visible_stem (Grob *me)
1228 extract_grob_set (me, "stems", stems);
1230 for (vsize i = 0; i < stems.size (); i++)
1232 if (!Stem::is_invisible (stems[i]))
1239 Beam::last_visible_stem (Grob *me)
1241 extract_grob_set (me, "stems", stems);
1243 for (vsize i = stems.size (); i--;)
1245 if (!Stem::is_invisible (stems[i]))
1254 handle rest under beam (do_post: beams are calculated now)
1255 what about combination of collisions and rest under beam.
1259 rest -> stem -> beam -> interpolate_y_position ()
1261 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1263 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1265 Grob *rest = unsmob_grob (smob);
1266 if (scm_is_number (rest->get_property ("staff-position")))
1267 return scm_from_int (0);
1269 Real offset = robust_scm2double (prev_offset, 0.0);
1271 Grob *st = unsmob_grob (rest->get_object ("stem"));
1274 return scm_from_double (0.0);
1275 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1277 || !Beam::has_interface (beam)
1278 || !Beam::visible_stem_count (beam))
1279 return scm_from_double (0.0);
1281 Drul_array<Real> pos (0, 0);
1282 SCM s = beam->get_property ("positions");
1283 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1284 pos = ly_scm2interval (s);
1286 programming_error ("positions property should always be pair of numbers.");
1288 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1290 scale_drul (&pos, staff_space);
1292 Real dy = pos[RIGHT] - pos[LEFT];
1294 Drul_array<Grob*> visible_stems (first_visible_stem (beam),
1295 last_visible_stem (beam));
1296 extract_grob_set (beam, "stems", stems);
1298 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1300 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1301 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1302 Real slope = dy && dx ? dy / dx : 0;
1304 Direction d = get_grob_direction (stem);
1305 Real stem_y = pos[LEFT]
1306 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1308 Real beam_translation = get_beam_translation (beam);
1309 Real beam_thickness = Beam::get_thickness (beam);
1312 TODO: this is not strictly correct for 16th knee beams.
1315 = Stem::beam_multiplicity (stem).length () + 1;
1317 Real height_of_my_beams = beam_thickness / 2
1318 + (beam_count - 1) * beam_translation;
1319 Real beam_y = stem_y - d * height_of_my_beams;
1321 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1322 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1323 rest_extent.translate (offset);
1325 Real rest_dim = rest_extent[d];
1326 Real minimum_distance
1327 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1328 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1330 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1332 shift /= staff_space;
1333 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1335 /* Always move discretely by half spaces */
1336 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1338 /* Inside staff, move by whole spaces*/
1339 if ((rest_extent[d] + staff_space * shift) * d
1341 || (rest_extent[-d] + staff_space * shift) * -d
1343 shift = ceil (fabs (shift)) * sign (shift);
1345 return scm_from_double (staff_space * shift);
1349 Beam::is_knee (Grob *me)
1351 SCM k = me->get_property ("knee");
1352 if (scm_is_bool (k))
1353 return ly_scm2bool (k);
1357 extract_grob_set (me, "stems", stems);
1358 for (vsize i = stems.size (); i--;)
1360 Direction dir = get_grob_direction (stems[i]);
1369 me->set_property ("knee", ly_bool2scm (knee));
1375 Beam::get_direction_beam_count (Grob *me, Direction d)
1377 extract_grob_set (me, "stems", stems);
1380 for (vsize i = stems.size (); i--;)
1383 Should we take invisible stems into account?
1385 if (get_grob_direction (stems[i]) == d)
1386 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1392 ADD_INTERFACE (Beam,
1396 "The @code{thickness} property is the weight of beams, "
1397 "measured in staffspace. The @code{direction} "
1398 "property is not user-serviceable. Use "
1399 "the @code{direction} property of @code{Stem} instead. "
1405 "beamed-stem-shorten "
1419 "neutral-direction "
1422 "quantized-positions "