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);
148 ugh: stems[0] case happens for chord tremolo.
150 d = to_dir ((stem ? stem : stems[0])->get_property ("default-direction"));
157 d = get_default_dir (me);
159 consider_auto_knees (me);
164 set_stem_directions (me, d);
167 return scm_from_int (d);
172 /* We want a maximal number of shared beams, but if there is choice, we
173 * take the one that is closest to the end of the stem. This is for
185 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
189 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
193 for (int i = lslice[-left_dir];
194 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
197 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
199 int k = -right_dir * scm_to_int (scm_car (s)) + i;
200 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
204 if (count >= best_count)
214 MAKE_SCHEME_CALLBACK(Beam, calc_beaming, 1)
216 Beam::calc_beaming (SCM smob)
218 Grob *me = unsmob_grob (smob);
220 extract_grob_set (me, "stems", stems);
223 last_int.set_empty ();
225 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
226 Direction last_dir = CENTER;
227 for (vsize 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,
237 last_dir ? last_dir : this_dir,
244 new_slice.set_empty ();
245 SCM s = index_get_cell (this_beaming, d);
246 for (; scm_is_pair (s); s = scm_cdr (s))
249 = start_point - this_dir * scm_to_int (scm_car (s));
251 new_slice.add_point (new_beam_pos);
252 scm_set_car_x (s, scm_from_int (new_beam_pos));
255 while (flip (&d) != LEFT);
257 if (!new_slice.is_empty ())
258 last_int = new_slice;
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 (scm_ilength (scm_cdr (this_beaming)) > 0)
273 last_beaming = this_beaming;
282 I really enjoy spaghetti, but spaghetti should be kept on a plate
283 with a little garlic and olive oil. This is too much.
287 MAKE_SCHEME_CALLBACK (Beam, print, 1);
289 Beam::print (SCM grob)
291 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.back ()->relative_coordinate (xcommon, X_AXIS) - x0;
312 SCM posns = me->get_property ("quantized-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->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
339 for (vsize 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 std::vector<int> full_beams;
356 std::vector<int> lfliebertjes;
357 std::vector<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)
364 full_beams.push_back (b);
366 lfliebertjes.push_back (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_back (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->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 vector_sort (full_beams, default_compare);
406 full_beams.reverse ();
410 for (vsize 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);
439 nw_f = break_overshoot[RIGHT] / 2;
441 /* Half beam should be one note-width,
442 but let's make sure two half-beams never touch */
446 rw = min (nw_f, ((xposn - last_xposn) / 2));
449 if (me->get_bound (LEFT)->break_status_dir ())
450 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
451 + break_overshoot[LEFT];
457 lw = min (nw_f, ((xposn - last_xposn) / 2));
460 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
462 + break_overshoot[RIGHT];
464 rw += stem_width / 2;
465 lw += last_stem_width / 2;
467 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
468 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
469 for (vsize j = lfliebertjes.size (); j--;)
472 b.translate_axis (last_xposn - x0 - last_stem_width /2,
474 b.translate_axis (slope * (last_xposn - x0)
475 + bdy * lfliebertjes[j],
477 the_beam.add_stencil (b);
479 for (vsize j = rfliebertjes.size (); j--;)
482 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
483 b.translate_axis (slope * (xposn - x0 - rw)
484 + bdy * rfliebertjes[j], Y_AXIS);
485 the_beam.add_stencil (b);
490 last_stem_width = stem_width;
491 last_beaming = this_beaming;
494 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
496 the_beam.translate_axis (pos[LEFT], Y_AXIS);
499 SCM quant_score = me->get_property ("quant-score");
500 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"));
501 if (to_boolean (debug) && scm_is_string (quant_score))
505 This code prints the demerits for each beam. Perhaps this
506 should be switchable for those who want to twiddle with the
510 SCM properties = Font_interface::text_font_alist_chain (me);
512 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
514 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
515 (me->layout ()->self_scm (), properties, quant_score));
517 if (!score.is_empty ())
518 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0, 0);
522 return the_beam.smobbed_copy ();
526 Beam::get_default_dir (Grob *me)
528 Drul_array<int> total;
529 total[UP] = total[DOWN] = 0;
530 Drul_array<int> count;
531 count[UP] = count[DOWN] = 0;
533 extract_grob_set (me, "stems", stems);
535 for (vsize i = 0; i < stems.size (); i++)
538 Direction stem_dir = CENTER;
539 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
540 if (is_direction (stem_dir_scm))
541 stem_dir = to_dir (stem_dir_scm);
543 stem_dir = to_dir (s->get_property ("default-direction"));
546 stem_dir = to_dir (s->get_property ("neutral-direction"));
551 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
555 Direction dir = CENTER;
556 Direction d = CENTER;
557 if ((d = (Direction) sign (count[UP] - count[DOWN])))
561 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
563 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
566 dir = to_dir (me->get_property ("neutral-direction"));
571 /* Set all stems with non-forced direction to beam direction.
572 Urg: non-forced should become `without/with unforced' direction,
573 once stem gets cleaned-up. */
575 Beam::set_stem_directions (Grob *me, Direction d)
577 extract_grob_set (me, "stems", stems);
579 for (vsize i = 0; i < stems.size (); i++)
583 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
584 if (!to_dir (forcedir))
585 set_grob_direction (s, d);
590 Only try horizontal beams for knees. No reliable detection of
591 anything else is possible here, since we don't know funky-beaming
592 settings, or X-distances (slopes!) People that want sloped
593 knee-beams, should set the directions manually.
598 this routine should take into account the stemlength scoring
599 of a possible knee/nonknee beam.
602 Beam::consider_auto_knees (Grob *me)
604 SCM scm = me->get_property ("auto-knee-gap");
605 if (!scm_is_number (scm))
612 extract_grob_set (me, "stems", stems);
614 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
615 Real staff_space = Staff_symbol_referencer::staff_space (me);
617 std::vector<Interval> head_extents_array;
618 for (vsize i = 0; i < stems.size (); i++)
620 Grob *stem = stems[i];
621 if (Stem::is_invisible (stem))
624 Interval head_extents = Stem::head_positions (stem);
625 if (!head_extents.is_empty ())
627 head_extents[LEFT] += -1;
628 head_extents[RIGHT] += 1;
629 head_extents *= staff_space * 0.5;
632 We could subtract beam Y position, but this routine only
633 sets stem directions, a constant shift does not have an
636 head_extents += stem->relative_coordinate (common, Y_AXIS);
638 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
640 Direction stemdir = to_dir (stem->get_property ("direction"));
641 head_extents[-stemdir] = -stemdir * infinity_f;
644 head_extents_array.push_back (head_extents);
646 gaps.remove_interval (head_extents);
650 Real max_gap_len = 0.0;
652 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
654 Interval gap = gaps.allowed_regions_[i];
657 the outer gaps are not knees.
659 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
662 if (gap.length () >= max_gap_len)
664 max_gap_len = gap.length ();
669 Real beam_translation = get_beam_translation (me);
670 Real beam_thickness = Beam::get_thickness (me);
671 int beam_count = Beam::get_beam_count (me);
672 Real height_of_beams = beam_thickness / 2
673 + (beam_count - 1) * beam_translation;
674 Real threshold = scm_to_double (scm) + height_of_beams;
676 if (max_gap_len > threshold)
679 for (vsize i = 0; i < stems.size (); i++)
681 Grob *stem = stems[i];
682 if (Stem::is_invisible (stem))
685 Interval head_extents = head_extents_array[j++];
687 Direction d = (head_extents.center () < max_gap.center ())
690 stem->set_property ("direction", scm_from_int (d));
692 head_extents.intersect (max_gap);
693 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
698 /* Set stem's shorten property if unset.
701 take some y-position (chord/beam/nearest?) into account
702 scmify forced-fraction
704 This is done in beam because the shorten has to be uniform over the
711 set_minimum_dy (Grob *me, Real *dy)
716 If dy is smaller than the smallest quant, we
717 get absurd direction-sign penalties.
720 Real ss = Staff_symbol_referencer::staff_space (me);
721 Real thickness = Beam::get_thickness (me) / ss;
722 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
723 Real sit = (thickness - slt) / 2;
725 Real hang = 1.0 - (thickness - slt) / 2;
727 *dy = sign (*dy) * max (fabs (*dy),
728 min (min (sit, inter), hang));
734 MAKE_SCHEME_CALLBACK(Beam, calc_stem_shorten, 1)
736 Beam::calc_stem_shorten (SCM smob)
738 Grob *me = unsmob_grob (smob);
741 shortening looks silly for x staff beams
744 return scm_from_int (0);
746 Real forced_fraction = 1.0 * forced_stem_count (me)
747 / visible_stem_count (me);
749 int beam_count = get_beam_count (me);
751 SCM shorten_list = me->get_property ("beamed-stem-shorten");
752 if (shorten_list == SCM_EOL)
753 return scm_from_int (0);
755 Real staff_space = Staff_symbol_referencer::staff_space (me);
758 = robust_list_ref (beam_count -1, shorten_list);
759 Real shorten = scm_to_double (shorten_elt) * staff_space;
761 shorten *= forced_fraction;
765 return scm_from_double (shorten);
767 return scm_from_double (0.0);
773 Compute a first approximation to the beam slope.
775 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
777 Beam::calc_least_squares_positions (SCM smob, SCM posns)
781 Grob *me = unsmob_grob (smob);
783 int count = visible_stem_count (me);
786 return ly_interval2scm (pos);
788 std::vector<Real> x_posns;
789 extract_grob_set (me, "stems", stems);
790 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
791 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
793 Real my_y = me->relative_coordinate (commony, Y_AXIS);
795 Grob *fvs = first_visible_stem (me);
796 Grob *lvs = last_visible_stem (me);
798 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
799 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
800 Stem::get_stem_info (lvs).ideal_y_
801 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
803 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
804 for (vsize i = 0; i < stems.size (); i++)
808 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
809 x_posns.push_back (x);
811 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
819 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
820 Stem::chord_start_y (last_visible_stem (me)));
822 /* Simple beams (2 stems) on middle line should be allowed to be
825 However, if both stems reach middle line,
826 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
828 For that case, we apply artificial slope */
829 if (!ideal[LEFT] && chord.delta () && count == 2)
832 Direction d = (Direction) (sign (chord.delta ()) * UP);
833 pos[d] = get_thickness (me) / 2;
840 For broken beams this doesn't work well. In this case, the
841 slope esp. of the first part of a broken beam should predict
842 where the second part goes.
844 ldy = pos[RIGHT] - pos[LEFT];
848 std::vector<Offset> ideals;
849 for (vsize i = 0; i < stems.size (); i++)
852 if (Stem::is_invisible (s))
854 ideals.push_back (Offset (x_posns[i],
855 Stem::get_stem_info (s).ideal_y_
856 + s->relative_coordinate (commony, Y_AXIS)
860 minimise_least_squares (&slope, &y, ideals);
864 set_minimum_dy (me, &dy);
867 pos = Interval (y, (y + dy));
871 "position" is relative to the staff.
873 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
875 me->set_property ("least-squares-dy", scm_from_double (ldy));
876 return ly_interval2scm (pos);
880 We can't combine with previous function, since check concave and
881 slope damping comes first.
883 TODO: we should use the concaveness to control the amount of damping
886 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
888 Beam::shift_region_to_valid (SCM grob, SCM posns)
890 Grob *me = unsmob_grob (grob);
894 std::vector<Real> x_posns;
895 extract_grob_set (me, "stems", stems);
896 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
897 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
899 Grob *fvs = first_visible_stem (me);
904 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
905 for (vsize i = 0; i < stems.size (); i++)
909 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
910 x_posns.push_back (x);
913 Grob *lvs = last_visible_stem (me);
917 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
919 Drul_array<Real> pos = ly_scm2interval (posns);
922 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
924 Real dy = pos[RIGHT] - pos[LEFT];
926 Real slope = dx ? (dy / dx) : 0.0;
929 Shift the positions so that we have a chance of finding good
930 quants (i.e. no short stem failures.)
932 Interval feasible_left_point;
933 feasible_left_point.set_full ();
934 for (vsize i = 0; i < stems.size (); i++)
937 if (Stem::is_invisible (s))
940 Direction d = get_grob_direction (s);
943 = Stem::get_stem_info (s).shortest_y_
944 - slope * x_posns [i];
947 left_y is now relative to the stem S. We want relative to
948 ourselves, so translate:
951 += + s->relative_coordinate (commony, Y_AXIS)
952 - me->relative_coordinate (commony, Y_AXIS);
958 feasible_left_point.intersect (flp);
961 if (feasible_left_point.is_empty ())
962 warning (_ ("no viable initial configuration found: may not find good beam slope"));
963 else if (!feasible_left_point.contains (y))
965 const int REGION_SIZE = 2; // UGH UGH
966 if (isinf (feasible_left_point[DOWN]))
967 y = feasible_left_point[UP] - REGION_SIZE;
968 else if (isinf (feasible_left_point[UP]))
969 y = feasible_left_point[DOWN]+ REGION_SIZE;
971 y = feasible_left_point.center ();
974 pos = Drul_array<Real> (y, (y + dy));
975 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
977 return ly_interval2scm (pos);
980 /* This neat trick is by Werner Lemberg,
981 damped = tanh (slope)
982 corresponds with some tables in [Wanske] CHECKME */
983 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
985 Beam::slope_damping (SCM smob, SCM posns)
987 Grob *me = unsmob_grob (smob);
988 Drul_array<Real> pos = ly_scm2interval (posns);
990 if (visible_stem_count (me) <= 1)
994 SCM s = me->get_property ("damping");
995 Real damping = scm_to_double (s);
996 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
997 if (concaveness >= 10000)
999 pos[LEFT] = pos[RIGHT];
1000 me->set_property ("least-squares-dy", scm_from_double (0));
1006 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1008 Real dy = pos[RIGHT] - pos[LEFT];
1010 Grob *fvs = first_visible_stem (me);
1011 Grob *lvs = last_visible_stem (me);
1013 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1015 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1016 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1018 Real slope = dy && dx ? dy / dx : 0;
1020 slope = 0.6 * tanh (slope) / (damping + concaveness);
1022 Real damped_dy = slope * dx;
1024 set_minimum_dy (me, &damped_dy);
1026 pos[LEFT] += (dy - damped_dy) / 2;
1027 pos[RIGHT] -= (dy - damped_dy) / 2;
1029 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1032 return ly_interval2scm (pos);
1036 Report slice containing the numbers that are both in (car BEAMING)
1040 where_are_the_whole_beams (SCM beaming)
1044 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1046 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1048 l.add_point (scm_to_int (scm_car (s)));
1054 /* Return the Y position of the stem-end, given the Y-left, Y-right
1055 in POS for stem S. This Y position is relative to S. */
1057 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1059 Drul_array<Real> pos, bool french)
1061 Real beam_translation = get_beam_translation (me);
1063 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1064 Real dy = pos[RIGHT] - pos[LEFT];
1066 Real stem_y_beam0 = (dy && dx
1071 Direction my_dir = get_grob_direction (s);
1072 SCM beaming = s->get_property ("beaming");
1074 Real stem_y = stem_y_beam0;
1077 Slice bm = where_are_the_whole_beams (beaming);
1078 if (!bm.is_empty ())
1079 stem_y += beam_translation * bm[-my_dir];
1083 Slice bm = Stem::beam_multiplicity (s);
1084 if (!bm.is_empty ())
1085 stem_y += bm[my_dir] * beam_translation;
1088 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1089 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1095 Hmm. At this time, beam position and slope are determined. Maybe,
1096 stem directions and length should set to relative to the chord's
1097 position of the beam. */
1098 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 1);
1100 Beam::set_stem_lengths (SCM smob)
1102 Grob *me = unsmob_grob (smob);
1104 /* trigger callback. */
1105 (void) me->get_property ("direction");
1107 SCM posns = me->get_property ("positions");
1109 extract_grob_set (me, "stems", stems);
1114 for (int a = 2; a--;)
1115 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1117 Drul_array<Real> pos = ly_scm2realdrul (posns);
1118 Real staff_space = Staff_symbol_referencer::staff_space (me);
1119 scale_drul (&pos, staff_space);
1123 if (scm_is_number (me->get_property ("gap-count"))
1124 && scm_to_int (me->get_property ("gap-count")))
1127 thick = get_thickness (me);
1130 Grob *fvs = first_visible_stem (me);
1131 Grob *lvs = last_visible_stem (me);
1133 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1134 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1136 for (vsize i = 0; i < stems.size (); i++)
1139 if (Stem::is_invisible (s))
1142 bool french = to_boolean (s->get_property ("french-beaming"));
1143 Real stem_y = calc_stem_y (me, s, common,
1145 pos, french && s != lvs && s!= fvs);
1148 Make the stems go up to the end of the beam. This doesn't matter
1149 for normal beams, but for tremolo beams it looks silly otherwise.
1152 stem_y += thick * 0.5 * get_grob_direction (s);
1154 Stem::set_stemend (s, 2 * stem_y / staff_space);
1161 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1163 extract_grob_set (me, "stems", stems);
1166 for (vsize i = 0; i < stems.size (); i++)
1169 Don't overwrite user settings.
1173 Grob *stem = stems[i];
1174 SCM beaming_prop = stem->get_property ("beaming");
1175 if (beaming_prop == SCM_EOL
1176 || index_get_cell (beaming_prop, d) == SCM_EOL)
1178 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1180 && i < stems.size () -1
1181 && Stem::is_invisible (stem))
1182 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1184 Stem::set_beaming (stem, b, d);
1187 while (flip (&d) != LEFT);
1192 Beam::forced_stem_count (Grob *me)
1194 extract_grob_set (me, "stems", stems);
1197 for (vsize i = 0; i < stems.size (); i++)
1201 if (Stem::is_invisible (s))
1204 /* I can imagine counting those boundaries as a half forced stem,
1205 but let's count them full for now. */
1206 Direction defdir = to_dir (s->get_property ("default-direction"));
1208 if (abs (Stem::chord_start_y (s)) > 0.1
1210 && get_grob_direction (s) != defdir)
1217 Beam::visible_stem_count (Grob *me)
1219 extract_grob_set (me, "stems", stems);
1221 for (vsize i = stems.size (); i--;)
1223 if (!Stem::is_invisible (stems[i]))
1230 Beam::first_visible_stem (Grob *me)
1232 extract_grob_set (me, "stems", stems);
1234 for (vsize i = 0; i < stems.size (); i++)
1236 if (!Stem::is_invisible (stems[i]))
1243 Beam::last_visible_stem (Grob *me)
1245 extract_grob_set (me, "stems", stems);
1247 for (vsize i = stems.size (); i--;)
1249 if (!Stem::is_invisible (stems[i]))
1258 handle rest under beam (do_post: beams are calculated now)
1259 what about combination of collisions and rest under beam.
1263 rest -> stem -> beam -> interpolate_y_position ()
1265 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1267 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1269 Grob *rest = unsmob_grob (smob);
1270 if (scm_is_number (rest->get_property ("staff-position")))
1271 return scm_from_int (0);
1273 Real offset = robust_scm2double (prev_offset, 0.0);
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);
1290 programming_error ("positions property should always be pair of numbers.");
1292 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1294 scale_drul (&pos, staff_space);
1296 Real dy = pos[RIGHT] - pos[LEFT];
1298 Drul_array<Grob*> visible_stems (first_visible_stem (beam),
1299 last_visible_stem (beam));
1300 extract_grob_set (beam, "stems", stems);
1302 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1304 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1305 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1306 Real slope = dy && dx ? dy / dx : 0;
1308 Direction d = get_grob_direction (stem);
1309 Real stem_y = pos[LEFT]
1310 + (stem->relative_coordinate (common, 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);
1326 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1327 rest_extent.translate (offset);
1329 Real rest_dim = rest_extent[d];
1330 Real minimum_distance
1331 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1332 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1334 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1336 shift /= staff_space;
1337 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1339 /* Always move discretely by half spaces */
1340 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1342 /* Inside staff, move by whole spaces*/
1343 if ((rest_extent[d] + staff_space * shift) * d
1345 || (rest_extent[-d] + staff_space * shift) * -d
1347 shift = ceil (fabs (shift)) * sign (shift);
1349 return scm_from_double (staff_space * shift);
1353 Beam::is_knee (Grob *me)
1355 SCM k = me->get_property ("knee");
1356 if (scm_is_bool (k))
1357 return ly_scm2bool (k);
1361 extract_grob_set (me, "stems", stems);
1362 for (vsize i = stems.size (); i--;)
1364 Direction dir = get_grob_direction (stems[i]);
1373 me->set_property ("knee", ly_bool2scm (knee));
1379 Beam::get_direction_beam_count (Grob *me, Direction d)
1381 extract_grob_set (me, "stems", stems);
1384 for (vsize i = stems.size (); i--;)
1387 Should we take invisible stems into account?
1389 if (get_grob_direction (stems[i]) == d)
1390 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1396 ADD_INTERFACE (Beam,
1400 "The @code{thickness} property is the weight of beams, "
1401 "measured in staffspace. The @code{direction} "
1402 "property is not user-serviceable. Use "
1403 "the @code{direction} property of @code{Stem} instead. "
1409 "beamed-stem-shorten "
1423 "neutral-direction "
1426 "quantized-positions "