2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 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.
28 #include "interval-set.hh"
29 #include "directional-element-interface.hh"
32 #include "least-squares.hh"
34 #include "output-def.hh"
36 #include "pointer-group-interface.hh"
37 #include "staff-symbol-referencer.hh"
43 #include "text-interface.hh" // debug output.
44 #include "font-interface.hh" // debug output.
48 Beam::add_stem (Grob *me, Grob *s)
50 if (Stem::get_beam (s))
52 programming_error ("Stem already has beam");
56 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
57 s->set_object ("beam", me->self_scm ());
58 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
62 Beam::get_thickness (Grob *me)
64 return robust_scm2double (me->get_property ("thickness"), 0)
65 * Staff_symbol_referencer::staff_space (me);
68 /* Return the translation between 2 adjoining beams. */
70 Beam::get_beam_translation (Grob *me)
72 int beam_count = get_beam_count (me);
73 Real staff_space = Staff_symbol_referencer::staff_space (me);
74 Real line = Staff_symbol_referencer::line_thickness (me);
75 Real thickness = get_thickness (me);
76 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
78 Real beam_translation = beam_count < 4
79 ? (2 * staff_space + line - thickness) / 2.0
80 : (3 * staff_space + line - thickness) / 3.0;
82 return fract * beam_translation;
85 /* Maximum beam_count. */
87 Beam::get_beam_count (Grob *me)
91 extract_grob_set (me, "stems", stems);
92 for (int i = 0; i < stems.size (); i++)
94 Grob *stem = stems[i];
95 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
101 /* After pre-processing all directions should be set.
102 Several post-processing routines (stem, slur, script) need stem/beam
104 Currenly, this means that beam has set all stem's directions.
105 [Alternatively, stems could set its own directions, according to
106 their beam, during 'final-pre-processing'.] */
107 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
109 Beam::calc_direction (SCM smob)
111 Grob *me = unsmob_grob (smob);
113 /* Beams with less than 2 two stems don't make much sense, but could happen
118 For a beam that only has one stem, we try to do some disappearance magic:
119 we revert the flag, and move on to The Eternal Engraving Fields. */
121 Direction d = CENTER;
123 int count = visible_stem_count (me);
126 extract_grob_set (me, "stems", stems);
127 if (stems.size () == 1)
129 me->warning (_ ("removing beam with less than two stems"));
131 stems[0]->set_object ("beam", SCM_EOL);
134 return SCM_UNSPECIFIED;
136 else if (stems.size () == 0)
139 return SCM_UNSPECIFIED;
146 can happen in stem-tremolo case.
149 d = Stem::get_default_dir (stems[0]);
156 d = get_default_dir (me);
157 consider_auto_knees (me);
162 set_stem_directions (me, d);
164 set_stem_shorten (me);
167 return scm_from_int (d);
170 /* We want a maximal number of shared beams, but if there is choice, we
171 * take the one that is closest to the end of the stem. This is for
183 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
187 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
191 for (int i = lslice[-left_dir];
192 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
195 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
197 int k = -right_dir * scm_to_int (scm_car (s)) + i;
198 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
202 if (count >= best_count)
213 Beam::connect_beams (Grob *me)
215 extract_grob_set (me, "stems", stems);
218 last_int.set_empty ();
220 // SCM last_beaming = SCM_EOL;
221 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
222 Direction last_dir = CENTER;
223 for (int 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;
276 I really enjoy spaghetti, but spaghetti should be kept on a plate
277 with a little garlic and olive oil. This is too much.
281 MAKE_SCHEME_CALLBACK (Beam, print, 1);
283 Beam::print (SCM grob)
285 Spanner *me = unsmob_spanner (grob);
287 extract_grob_set (me, "stems", stems);
288 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
290 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
291 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
294 if (visible_stem_count (me))
296 // ugh -> use commonx
297 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
298 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
302 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
303 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
306 SCM posns = me->get_property ("positions");
307 Drul_array<Real> pos;
308 if (!is_number_pair (posns))
310 programming_error ("no beam positions?");
311 pos = Interval (0, 0);
314 pos = ly_scm2realdrul (posns);
316 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
318 Real dy = pos[RIGHT] - pos[LEFT];
319 Real slope = (dy && dx) ? dy / dx : 0;
321 Real thick = get_thickness (me);
322 Real bdy = get_beam_translation (me);
324 SCM last_beaming = SCM_EOL;
325 Real last_xposn = -1;
326 Real last_stem_width = -1;
328 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
331 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
333 for (int i = 0; i <= stems.size (); i++)
335 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
337 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
338 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
339 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
340 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
342 We do the space left of ST, with lfliebertjes pointing to the
343 right from the left stem, and rfliebertjes pointing left from
346 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
347 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
349 Array<int> full_beams;
350 Array<int> lfliebertjes;
351 Array<int> rfliebertjes;
354 scm_is_pair (s); s = scm_cdr (s))
356 int b = scm_to_int (scm_car (s));
357 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
360 lfliebertjes.push (b);
363 scm_is_pair (s); s = scm_cdr (s))
365 int b = scm_to_int (scm_car (s));
366 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
367 rfliebertjes.push (b);
370 Drul_array<Real> break_overshoot
371 = robust_scm2drul (me->get_property ("break-overshoot"),
372 Drul_array<Real> (-0.5, 0.0));
374 Real w = (i > 0 && stem)
375 ? (xposn - last_xposn)
376 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
378 Real stem_offset = 0.0;
381 w += last_stem_width / 2;
382 stem_offset = -last_stem_width / 2;
388 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
389 Stencil whole = Lookup::beam (slope, w, thick, blot);
393 if (scm_is_number (me->get_property ("gap-count")))
395 gap_count = scm_to_int (me->get_property ("gap-count"));
396 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
398 full_beams.sort (default_compare);
400 full_beams.reverse ();
404 for (int j = full_beams.size (); j--;)
411 b.translate_axis (gap_length, X_AXIS);
413 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
414 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
416 the_beam.add_stencil (b);
419 if (lfliebertjes.size () || rfliebertjes.size ())
425 int t = Stem::duration_log (stem);
433 nw_f = break_overshoot[RIGHT] / 2;
435 /* Half beam should be one note-width,
436 but let's make sure two half-beams never touch */
440 rw = min (nw_f, ((xposn - last_xposn) / 2));
443 if (me->get_bound (LEFT)->break_status_dir ())
444 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
445 + break_overshoot[LEFT];
451 lw = min (nw_f, ((xposn - last_xposn) / 2));
454 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
456 + break_overshoot[RIGHT];
458 rw += stem_width / 2;
459 lw += last_stem_width / 2;
461 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
462 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
463 for (int j = lfliebertjes.size (); j--;)
466 b.translate_axis (last_xposn - x0 - last_stem_width /2,
468 b.translate_axis (slope * (last_xposn - x0)
469 + bdy * lfliebertjes[j],
471 the_beam.add_stencil (b);
473 for (int j = rfliebertjes.size (); j--;)
476 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
477 b.translate_axis (slope * (xposn - x0 - rw)
478 + bdy * rfliebertjes[j], Y_AXIS);
479 the_beam.add_stencil (b);
484 last_stem_width = stem_width;
485 last_beaming = this_beaming;
488 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
490 the_beam.translate_axis (pos[LEFT], Y_AXIS);
493 SCM quant_score = me->get_property ("quant-score");
494 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"));
495 if (to_boolean (debug) && scm_is_string (quant_score))
499 This code prints the demerits for each beam. Perhaps this
500 should be switchable for those who want to twiddle with the
504 SCM properties = Font_interface::text_font_alist_chain (me);
506 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
508 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
509 (me->layout ()->self_scm (), properties, quant_score));
511 if (!score.is_empty ())
512 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 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;
527 extract_grob_set (me, "stems", stems);
529 for (int i = 0; i < stems.size (); i++)
532 Direction stem_dir = CENTER;
533 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
534 if (is_direction (stem_dir_scm))
535 stem_dir = to_dir (stem_dir_scm);
537 stem_dir = Stem::get_default_dir (s);
542 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
546 Direction dir = CENTER;
548 if (Direction d = (Direction) sign (count[UP] - count[DOWN]))
550 else if (Direction d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN]))
552 else if (Direction d = (Direction) sign (total[UP] - total[DOWN]))
555 dir = to_dir (me->get_property ("neutral-direction"));
560 /* Set all stems with non-forced direction to beam direction.
561 Urg: non-forced should become `without/with unforced' direction,
562 once stem gets cleaned-up. */
564 Beam::set_stem_directions (Grob *me, Direction d)
566 extract_grob_set (me, "stems", stems);
568 for (int i = 0; i < stems.size (); i++)
572 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
573 if (!to_dir (forcedir))
574 set_grob_direction (s, d);
579 Only try horizontal beams for knees. No reliable detection of
580 anything else is possible here, since we don't know funky-beaming
581 settings, or X-distances (slopes!) People that want sloped
582 knee-beams, should set the directions manually.
587 this routine should take into account the stemlength scoring
588 of a possible knee/nonknee beam.
591 Beam::consider_auto_knees (Grob *me)
593 SCM scm = me->get_property ("auto-knee-gap");
594 if (!scm_is_number (scm))
601 extract_grob_set (me, "stems", stems);
603 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
604 Real staff_space = Staff_symbol_referencer::staff_space (me);
606 Array<Interval> head_extents_array;
607 for (int i = 0; i < stems.size (); i++)
609 Grob *stem = stems[i];
610 if (Stem::is_invisible (stem))
613 Interval head_extents = Stem::head_positions (stem);
614 if (!head_extents.is_empty ())
616 head_extents[LEFT] += -1;
617 head_extents[RIGHT] += 1;
618 head_extents *= staff_space * 0.5;
621 We could subtract beam Y position, but this routine only
622 sets stem directions, a constant shift does not have an
625 head_extents += stem->relative_coordinate (common, Y_AXIS);
627 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
629 Direction stemdir = to_dir (stem->get_property ("direction"));
630 head_extents[-stemdir] = -stemdir * infinity_f;
633 head_extents_array.push (head_extents);
635 gaps.remove_interval (head_extents);
639 Real max_gap_len = 0.0;
641 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
643 Interval gap = gaps.allowed_regions_[i];
646 the outer gaps are not knees.
648 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
651 if (gap.length () >= max_gap_len)
653 max_gap_len = gap.length ();
658 Real beam_translation = get_beam_translation (me);
659 Real beam_thickness = Beam::get_thickness (me);
660 int beam_count = Beam::get_beam_count (me);
661 Real height_of_beams = beam_thickness / 2
662 + (beam_count - 1) * beam_translation;
663 Real threshold = scm_to_double (scm) + height_of_beams;
665 if (max_gap_len > threshold)
668 for (int i = 0; i < stems.size (); i++)
670 Grob *stem = stems[i];
671 if (Stem::is_invisible (stem))
674 Interval head_extents = head_extents_array[j++];
676 Direction d = (head_extents.center () < max_gap.center ())
679 stem->set_property ("direction", scm_from_int (d));
681 head_extents.intersect (max_gap);
682 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
687 /* Set stem's shorten property if unset.
690 take some y-position (chord/beam/nearest?) into account
691 scmify forced-fraction
693 This is done in beam because the shorten has to be uniform over the
697 Beam::set_stem_shorten (Grob *me)
700 shortening looks silly for x staff beams
705 Real forced_fraction = 1.0 * forced_stem_count (me)
706 / visible_stem_count (me);
708 int beam_count = get_beam_count (me);
710 SCM shorten_list = me->get_property ("beamed-stem-shorten");
711 if (shorten_list == SCM_EOL)
714 Real staff_space = Staff_symbol_referencer::staff_space (me);
717 = robust_list_ref (beam_count -1, shorten_list);
718 Real shorten = scm_to_double (shorten_elt) * staff_space;
720 shorten *= forced_fraction;
723 me->set_property ("shorten", scm_from_double (shorten));
729 set_minimum_dy (Grob *me, Real *dy)
734 If dy is smaller than the smallest quant, we
735 get absurd direction-sign penalties.
738 Real ss = Staff_symbol_referencer::staff_space (me);
739 Real thickness = Beam::get_thickness (me) / ss;
740 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
741 Real sit = (thickness - slt) / 2;
743 Real hang = 1.0 - (thickness - slt) / 2;
745 *dy = sign (*dy) * max (fabs (*dy),
746 min (min (sit, inter), hang));
751 Compute a first approximation to the beam slope.
753 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
755 Beam::calc_least_squares_positions (SCM smob, SCM posns)
759 Grob *me = unsmob_grob (smob);
761 int count = visible_stem_count (me);
764 return ly_interval2scm (pos);
767 extract_grob_set (me, "stems", stems);
768 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
769 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
771 Real my_y = me->relative_coordinate (commony, Y_AXIS);
773 Grob *fvs = first_visible_stem (me);
774 Grob *lvs = last_visible_stem (me);
776 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
777 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
778 Stem::get_stem_info (lvs).ideal_y_
779 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
781 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
782 for (int i = 0; i < stems.size (); i++)
786 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
789 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
797 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
798 Stem::chord_start_y (last_visible_stem (me)));
800 /* Simple beams (2 stems) on middle line should be allowed to be
803 However, if both stems reach middle line,
804 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
806 For that case, we apply artificial slope */
807 if (!ideal[LEFT] && chord.delta () && count == 2)
810 Direction d = (Direction) (sign (chord.delta ()) * UP);
811 pos[d] = get_thickness (me) / 2;
818 For broken beams this doesn't work well. In this case, the
819 slope esp. of the first part of a broken beam should predict
820 where the second part goes.
822 ldy = pos[RIGHT] - pos[LEFT];
826 Array<Offset> ideals;
827 for (int i = 0; i < stems.size (); i++)
830 if (Stem::is_invisible (s))
832 ideals.push (Offset (x_posns[i],
833 Stem::get_stem_info (s).ideal_y_
834 + s->relative_coordinate (commony, Y_AXIS)
838 minimise_least_squares (&slope, &y, ideals);
842 set_minimum_dy (me, &dy);
845 pos = Interval (y, (y + dy));
849 "position" is relative to the staff.
851 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
853 me->set_property ("least-squares-dy", scm_from_double (ldy));
854 return ly_interval2scm (pos);
858 We can't combine with previous function, since check concave and
859 slope damping comes first.
861 TODO: we should use the concaveness to control the amount of damping
864 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
866 Beam::shift_region_to_valid (SCM grob, SCM posns)
868 Grob *me = unsmob_grob (grob);
873 extract_grob_set (me, "stems", stems);
874 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
875 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
877 Grob *fvs = first_visible_stem (me);
882 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
883 for (int i = 0; i < stems.size (); i++)
887 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
891 Grob *lvs = last_visible_stem (me);
895 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
897 Drul_array<Real> pos = ly_scm2interval (posns);
900 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
902 Real dy = pos[RIGHT] - pos[LEFT];
904 Real slope = dx ? (dy / dx) : 0.0;
907 Shift the positions so that we have a chance of finding good
908 quants (i.e. no short stem failures.)
910 Interval feasible_left_point;
911 feasible_left_point.set_full ();
912 for (int i = 0; i < stems.size (); i++)
915 if (Stem::is_invisible (s))
918 Direction d = get_grob_direction (s);
921 = Stem::get_stem_info (s).shortest_y_
922 - slope * x_posns [i];
925 left_y is now relative to the stem S. We want relative to
926 ourselves, so translate:
929 += + s->relative_coordinate (commony, Y_AXIS)
930 - me->relative_coordinate (commony, Y_AXIS);
936 feasible_left_point.intersect (flp);
939 if (feasible_left_point.is_empty ())
940 warning (_ ("no viable initial configuration found: may not find good beam slope"));
941 else if (!feasible_left_point.contains (y))
943 const int REGION_SIZE = 2; // UGH UGH
944 if (isinf (feasible_left_point[DOWN]))
945 y = feasible_left_point[UP] - REGION_SIZE;
946 else if (isinf (feasible_left_point[UP]))
947 y = feasible_left_point[DOWN]+ REGION_SIZE;
949 y = feasible_left_point.center ();
952 pos = Drul_array<Real> (y, (y + dy));
953 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
955 return ly_interval2scm (pos);
958 /* This neat trick is by Werner Lemberg,
959 damped = tanh (slope)
960 corresponds with some tables in [Wanske] CHECKME */
961 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
963 Beam::slope_damping (SCM smob, SCM posns)
965 Grob *me = unsmob_grob (smob);
966 Drul_array<Real> pos = ly_scm2interval (posns);
968 if (visible_stem_count (me) <= 1)
972 SCM s = me->get_property ("damping");
973 Real damping = scm_to_double (s);
974 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
975 if (concaveness >= 10000)
977 pos[LEFT] = pos[RIGHT];
978 me->set_property ("least-squares-dy", scm_from_double (0));
984 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
986 Real dy = pos[RIGHT] - pos[LEFT];
988 Grob *fvs = first_visible_stem (me);
989 Grob *lvs = last_visible_stem (me);
991 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
993 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
994 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
996 Real slope = dy && dx ? dy / dx : 0;
998 slope = 0.6 * tanh (slope) / (damping + concaveness);
1000 Real damped_dy = slope * dx;
1002 set_minimum_dy (me, &damped_dy);
1004 pos[LEFT] += (dy - damped_dy) / 2;
1005 pos[RIGHT] -= (dy - damped_dy) / 2;
1007 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1010 return ly_interval2scm (pos);
1014 Report slice containing the numbers that are both in (car BEAMING)
1018 where_are_the_whole_beams (SCM beaming)
1022 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1024 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1026 l.add_point (scm_to_int (scm_car (s)));
1032 /* Return the Y position of the stem-end, given the Y-left, Y-right
1033 in POS for stem S. This Y position is relative to S. */
1035 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1037 Drul_array<Real> pos, bool french)
1039 Real beam_translation = get_beam_translation (me);
1041 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1042 Real dy = pos[RIGHT] - pos[LEFT];
1044 Real stem_y_beam0 = (dy && dx
1049 Direction my_dir = get_grob_direction (s);
1050 SCM beaming = s->get_property ("beaming");
1052 Real stem_y = stem_y_beam0;
1055 Slice bm = where_are_the_whole_beams (beaming);
1056 if (!bm.is_empty ())
1057 stem_y += beam_translation * bm[-my_dir];
1061 Slice bm = Stem::beam_multiplicity (s);
1062 if (!bm.is_empty ())
1063 stem_y += bm[my_dir] * beam_translation;
1066 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1067 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1073 Hmm. At this time, beam position and slope are determined. Maybe,
1074 stem directions and length should set to relative to the chord's
1075 position of the beam. */
1076 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 2);
1078 Beam::set_stem_lengths (SCM smob, SCM posns)
1080 Grob *me = unsmob_grob (smob);
1082 extract_grob_set (me, "stems", stems);
1087 for (int a = 2; a--;)
1088 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1090 Drul_array<Real> pos = ly_scm2realdrul (posns);
1091 Real staff_space = Staff_symbol_referencer::staff_space (me);
1092 scale_drul (&pos, staff_space);
1096 if (scm_is_number (me->get_property ("gap-count"))
1097 &&scm_to_int (me->get_property ("gap-count")))
1100 thick = get_thickness (me);
1103 // ugh -> use commonx
1104 Grob *fvs = first_visible_stem (me);
1105 Grob *lvs = last_visible_stem (me);
1107 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1108 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1110 for (int i = 0; i < stems.size (); i++)
1113 if (Stem::is_invisible (s))
1116 bool french = to_boolean (s->get_property ("french-beaming"));
1117 Real stem_y = calc_stem_y (me, s, common,
1119 pos, french && s != lvs && s!= fvs);
1122 Make the stems go up to the end of the beam. This doesn't matter
1123 for normal beams, but for tremolo beams it looks silly otherwise.
1126 stem_y += thick * 0.5 * get_grob_direction (s);
1128 Stem::set_stemend (s, 2 * stem_y / staff_space);
1135 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1137 extract_grob_set (me, "stems", stems);
1140 for (int i = 0; i < stems.size (); i++)
1143 Don't overwrite user settings.
1148 Grob *stem = stems[i];
1149 SCM beaming_prop = stem->get_property ("beaming");
1150 if (beaming_prop == SCM_EOL
1151 || index_get_cell (beaming_prop, d) == SCM_EOL)
1153 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1155 && i < stems.size () -1
1156 && Stem::is_invisible (stem))
1157 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1159 Stem::set_beaming (stem, b, d);
1162 while (flip (&d) != LEFT);
1167 Beam::forced_stem_count (Grob *me)
1169 extract_grob_set (me, "stems", stems);
1172 for (int i = 0; i < stems.size (); i++)
1176 if (Stem::is_invisible (s))
1179 /* I can imagine counting those boundaries as a half forced stem,
1180 but let's count them full for now. */
1181 if (abs (Stem::chord_start_y (s)) > 0.1
1182 && (get_grob_direction (s) != Stem::get_default_dir (s)))
1189 Beam::visible_stem_count (Grob *me)
1191 extract_grob_set (me, "stems", stems);
1193 for (int i = stems.size (); i--;)
1195 if (!Stem::is_invisible (stems[i]))
1202 Beam::first_visible_stem (Grob *me)
1204 extract_grob_set (me, "stems", stems);
1206 for (int i = 0; i < stems.size (); i++)
1208 if (!Stem::is_invisible (stems[i]))
1215 Beam::last_visible_stem (Grob *me)
1217 extract_grob_set (me, "stems", stems);
1219 for (int i = stems.size (); i--;)
1221 if (!Stem::is_invisible (stems[i]))
1230 handle rest under beam (do_post: beams are calculated now)
1231 what about combination of collisions and rest under beam.
1235 rest -> stem -> beam -> interpolate_y_position ()
1237 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1239 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1241 Grob *rest = unsmob_grob (smob);
1242 if (scm_is_number (rest->get_property ("staff-position")))
1243 return scm_from_int (0);
1245 Real offset = robust_scm2double (prev_offset, 0.0);
1247 Grob *st = unsmob_grob (rest->get_object ("stem"));
1250 return scm_from_double (0.0);
1251 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1253 || !Beam::has_interface (beam)
1254 || !Beam::visible_stem_count (beam))
1255 return scm_from_double (0.0);
1257 Drul_array<Real> pos (0, 0);
1258 SCM s = beam->get_property ("positions");
1259 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1260 pos = ly_scm2interval (s);
1262 programming_error ("positions property should always be pair of numbers.");
1264 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1266 scale_drul (&pos, staff_space);
1268 Real dy = pos[RIGHT] - pos[LEFT];
1270 // ugh -> use commonx
1271 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1272 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1273 Real slope = dy && dx ? dy / dx : 0;
1275 Direction d = get_grob_direction (stem);
1276 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1278 Real beam_translation = get_beam_translation (beam);
1279 Real beam_thickness = Beam::get_thickness (beam);
1282 TODO: this is not strictly correct for 16th knee beams.
1285 = Stem::beam_multiplicity (stem).length () + 1;
1287 Real height_of_my_beams = beam_thickness / 2
1288 + (beam_count - 1) * beam_translation;
1289 Real beam_y = stem_y - d * height_of_my_beams;
1291 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1292 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1293 rest_extent.translate (offset);
1295 Real rest_dim = rest_extent[d];
1296 Real minimum_distance
1297 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1298 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1300 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1302 shift /= staff_space;
1303 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1305 /* Always move discretely by half spaces */
1306 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1308 /* Inside staff, move by whole spaces*/
1309 if ((rest_extent[d] + staff_space * shift) * d
1311 || (rest_extent[-d] + staff_space * shift) * -d
1313 shift = ceil (fabs (shift)) * sign (shift);
1315 return scm_from_double (staff_space * shift);
1319 Beam::is_knee (Grob *me)
1321 SCM k = me->get_property ("knee");
1322 if (scm_is_bool (k))
1323 return ly_scm2bool (k);
1327 extract_grob_set (me, "stems", stems);
1328 for (int i = stems.size (); i--;)
1330 Direction dir = get_grob_direction (stems[i]);
1339 me->set_property ("knee", ly_bool2scm (knee));
1345 Beam::get_direction_beam_count (Grob *me, Direction d)
1347 extract_grob_set (me, "stems", stems);
1350 for (int i = stems.size (); i--;)
1353 Should we take invisible stems into account?
1355 if (get_grob_direction (stems[i]) == d)
1356 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1362 ADD_INTERFACE (Beam,
1366 "The @code{thickness} property is the weight of beams, "
1367 "measured in staffspace. The @code{direction} property is not user-serviceable. Use "
1368 "the @code{direction} property of @code{Stem} instead. "
1374 "beamed-stem-shorten "
1387 "neutral-direction "