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;
143 d = to_dir (stems[0]->get_property ("default-direction"));
150 d = get_default_dir (me);
151 consider_auto_knees (me);
156 set_stem_directions (me, d);
159 return scm_from_int (d);
164 /* We want a maximal number of shared beams, but if there is choice, we
165 * take the one that is closest to the end of the stem. This is for
177 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
181 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
185 for (int i = lslice[-left_dir];
186 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
189 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
191 int k = -right_dir * scm_to_int (scm_car (s)) + i;
192 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
196 if (count >= best_count)
206 MAKE_SCHEME_CALLBACK(Beam, calc_beaming, 1)
208 Beam::calc_beaming (SCM smob)
210 Grob *me = unsmob_grob (smob);
212 extract_grob_set (me, "stems", stems);
215 last_int.set_empty ();
217 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
218 Direction last_dir = CENTER;
219 for (int i = 0; i < stems.size (); i++)
221 Grob *this_stem = stems[i];
222 SCM this_beaming = this_stem->get_property ("beaming");
224 Direction this_dir = get_grob_direction (this_stem);
225 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
227 int start_point = position_with_maximal_common_beams
228 (last_beaming, this_beaming,
229 last_dir ? last_dir : this_dir,
236 new_slice.set_empty ();
237 SCM s = index_get_cell (this_beaming, d);
238 for (; scm_is_pair (s); s = scm_cdr (s))
241 = start_point - this_dir * scm_to_int (scm_car (s));
243 new_slice.add_point (new_beam_pos);
244 scm_set_car_x (s, scm_from_int (new_beam_pos));
247 while (flip (&d) != LEFT);
249 if (!new_slice.is_empty ())
250 last_int = new_slice;
254 SCM s = scm_cdr (this_beaming);
255 for (; scm_is_pair (s); s = scm_cdr (s))
257 int np = -this_dir * scm_to_int (scm_car (s));
258 scm_set_car_x (s, scm_from_int (np));
259 last_int.add_point (np);
263 if (scm_ilength (scm_cdr (this_beaming)) > 0)
265 last_beaming = this_beaming;
274 I really enjoy spaghetti, but spaghetti should be kept on a plate
275 with a little garlic and olive oil. This is too much.
279 MAKE_SCHEME_CALLBACK (Beam, print, 1);
281 Beam::print (SCM grob)
283 Spanner *me = unsmob_spanner (grob);
285 extract_grob_set (me, "stems", stems);
286 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
288 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
289 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
292 if (visible_stem_count (me))
294 // ugh -> use commonx
295 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
296 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
300 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
301 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
304 SCM posns = me->get_property ("quantized-positions");
305 Drul_array<Real> pos;
306 if (!is_number_pair (posns))
308 programming_error ("no beam positions?");
309 pos = Interval (0, 0);
312 pos = ly_scm2realdrul (posns);
314 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
316 Real dy = pos[RIGHT] - pos[LEFT];
317 Real slope = (dy && dx) ? dy / dx : 0;
319 Real thick = get_thickness (me);
320 Real bdy = get_beam_translation (me);
322 SCM last_beaming = SCM_EOL;
323 Real last_xposn = -1;
324 Real last_stem_width = -1;
326 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
329 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
331 for (int i = 0; i <= stems.size (); i++)
333 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
335 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
336 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
337 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
338 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
340 We do the space left of ST, with lfliebertjes pointing to the
341 right from the left stem, and rfliebertjes pointing left from
344 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
345 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
347 Array<int> full_beams;
348 Array<int> lfliebertjes;
349 Array<int> rfliebertjes;
352 scm_is_pair (s); s = scm_cdr (s))
354 int b = scm_to_int (scm_car (s));
355 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
358 lfliebertjes.push (b);
361 scm_is_pair (s); s = scm_cdr (s))
363 int b = scm_to_int (scm_car (s));
364 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
365 rfliebertjes.push (b);
368 Drul_array<Real> break_overshoot
369 = robust_scm2drul (me->get_property ("break-overshoot"),
370 Drul_array<Real> (-0.5, 0.0));
372 Real w = (i > 0 && stem)
373 ? (xposn - last_xposn)
374 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
376 Real stem_offset = 0.0;
379 w += last_stem_width / 2;
380 stem_offset = -last_stem_width / 2;
386 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
387 Stencil whole = Lookup::beam (slope, w, thick, blot);
391 if (scm_is_number (me->get_property ("gap-count")))
393 gap_count = scm_to_int (me->get_property ("gap-count"));
394 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
396 full_beams.sort (default_compare);
398 full_beams.reverse ();
402 for (int j = full_beams.size (); j--;)
409 b.translate_axis (gap_length, X_AXIS);
411 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
412 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
414 the_beam.add_stencil (b);
417 if (lfliebertjes.size () || rfliebertjes.size ())
423 int t = Stem::duration_log (stem);
431 nw_f = break_overshoot[RIGHT] / 2;
433 /* Half beam should be one note-width,
434 but let's make sure two half-beams never touch */
438 rw = min (nw_f, ((xposn - last_xposn) / 2));
441 if (me->get_bound (LEFT)->break_status_dir ())
442 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
443 + break_overshoot[LEFT];
449 lw = min (nw_f, ((xposn - last_xposn) / 2));
452 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
454 + break_overshoot[RIGHT];
456 rw += stem_width / 2;
457 lw += last_stem_width / 2;
459 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
460 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
461 for (int j = lfliebertjes.size (); j--;)
464 b.translate_axis (last_xposn - x0 - last_stem_width /2,
466 b.translate_axis (slope * (last_xposn - x0)
467 + bdy * lfliebertjes[j],
469 the_beam.add_stencil (b);
471 for (int j = rfliebertjes.size (); j--;)
474 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
475 b.translate_axis (slope * (xposn - x0 - rw)
476 + bdy * rfliebertjes[j], Y_AXIS);
477 the_beam.add_stencil (b);
482 last_stem_width = stem_width;
483 last_beaming = this_beaming;
486 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
488 the_beam.translate_axis (pos[LEFT], Y_AXIS);
491 SCM quant_score = me->get_property ("quant-score");
492 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"));
493 if (to_boolean (debug) && scm_is_string (quant_score))
497 This code prints the demerits for each beam. Perhaps this
498 should be switchable for those who want to twiddle with the
502 SCM properties = Font_interface::text_font_alist_chain (me);
504 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
506 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
507 (me->layout ()->self_scm (), properties, quant_score));
509 if (!score.is_empty ())
510 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0, 0);
514 return the_beam.smobbed_copy ();
518 Beam::get_default_dir (Grob *me)
520 Drul_array<int> total;
521 total[UP] = total[DOWN] = 0;
522 Drul_array<int> count;
523 count[UP] = count[DOWN] = 0;
525 extract_grob_set (me, "stems", stems);
527 for (int i = 0; i < stems.size (); i++)
530 Direction stem_dir = CENTER;
531 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
532 if (is_direction (stem_dir_scm))
533 stem_dir = to_dir (stem_dir_scm);
535 stem_dir = to_dir (s->get_property ("default-direction"));
538 stem_dir = to_dir (s->get_property ("neutral-direction"));
543 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
547 Direction dir = CENTER;
549 if (Direction d = (Direction) sign (count[UP] - count[DOWN]))
551 else if (Direction d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN]))
553 else if (Direction d = (Direction) sign (total[UP] - total[DOWN]))
556 dir = to_dir (me->get_property ("neutral-direction"));
561 /* Set all stems with non-forced direction to beam direction.
562 Urg: non-forced should become `without/with unforced' direction,
563 once stem gets cleaned-up. */
565 Beam::set_stem_directions (Grob *me, Direction d)
567 extract_grob_set (me, "stems", stems);
569 for (int i = 0; i < stems.size (); i++)
573 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
574 if (!to_dir (forcedir))
575 set_grob_direction (s, d);
580 Only try horizontal beams for knees. No reliable detection of
581 anything else is possible here, since we don't know funky-beaming
582 settings, or X-distances (slopes!) People that want sloped
583 knee-beams, should set the directions manually.
588 this routine should take into account the stemlength scoring
589 of a possible knee/nonknee beam.
592 Beam::consider_auto_knees (Grob *me)
594 SCM scm = me->get_property ("auto-knee-gap");
595 if (!scm_is_number (scm))
602 extract_grob_set (me, "stems", stems);
604 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
605 Real staff_space = Staff_symbol_referencer::staff_space (me);
607 Array<Interval> head_extents_array;
608 for (int i = 0; i < stems.size (); i++)
610 Grob *stem = stems[i];
611 if (Stem::is_invisible (stem))
614 Interval head_extents = Stem::head_positions (stem);
615 if (!head_extents.is_empty ())
617 head_extents[LEFT] += -1;
618 head_extents[RIGHT] += 1;
619 head_extents *= staff_space * 0.5;
622 We could subtract beam Y position, but this routine only
623 sets stem directions, a constant shift does not have an
626 head_extents += stem->relative_coordinate (common, Y_AXIS);
628 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
630 Direction stemdir = to_dir (stem->get_property ("direction"));
631 head_extents[-stemdir] = -stemdir * infinity_f;
634 head_extents_array.push (head_extents);
636 gaps.remove_interval (head_extents);
640 Real max_gap_len = 0.0;
642 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
644 Interval gap = gaps.allowed_regions_[i];
647 the outer gaps are not knees.
649 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
652 if (gap.length () >= max_gap_len)
654 max_gap_len = gap.length ();
659 Real beam_translation = get_beam_translation (me);
660 Real beam_thickness = Beam::get_thickness (me);
661 int beam_count = Beam::get_beam_count (me);
662 Real height_of_beams = beam_thickness / 2
663 + (beam_count - 1) * beam_translation;
664 Real threshold = scm_to_double (scm) + height_of_beams;
666 if (max_gap_len > threshold)
669 for (int i = 0; i < stems.size (); i++)
671 Grob *stem = stems[i];
672 if (Stem::is_invisible (stem))
675 Interval head_extents = head_extents_array[j++];
677 Direction d = (head_extents.center () < max_gap.center ())
680 stem->set_property ("direction", scm_from_int (d));
682 head_extents.intersect (max_gap);
683 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
688 /* Set stem's shorten property if unset.
691 take some y-position (chord/beam/nearest?) into account
692 scmify forced-fraction
694 This is done in beam because the shorten has to be uniform over the
701 set_minimum_dy (Grob *me, Real *dy)
706 If dy is smaller than the smallest quant, we
707 get absurd direction-sign penalties.
710 Real ss = Staff_symbol_referencer::staff_space (me);
711 Real thickness = Beam::get_thickness (me) / ss;
712 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
713 Real sit = (thickness - slt) / 2;
715 Real hang = 1.0 - (thickness - slt) / 2;
717 *dy = sign (*dy) * max (fabs (*dy),
718 min (min (sit, inter), hang));
724 MAKE_SCHEME_CALLBACK(Beam, calc_stem_shorten, 1)
726 Beam::calc_stem_shorten (SCM smob)
728 Grob *me = unsmob_grob (smob);
731 shortening looks silly for x staff beams
734 return scm_from_int (0);
736 Real forced_fraction = 1.0 * forced_stem_count (me)
737 / visible_stem_count (me);
739 int beam_count = get_beam_count (me);
741 SCM shorten_list = me->get_property ("beamed-stem-shorten");
742 if (shorten_list == SCM_EOL)
743 return scm_from_int (0);
745 Real staff_space = Staff_symbol_referencer::staff_space (me);
748 = robust_list_ref (beam_count -1, shorten_list);
749 Real shorten = scm_to_double (shorten_elt) * staff_space;
751 shorten *= forced_fraction;
755 return scm_from_double (shorten);
757 return scm_from_double (0.0);
763 Compute a first approximation to the beam slope.
765 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
767 Beam::calc_least_squares_positions (SCM smob, SCM posns)
771 Grob *me = unsmob_grob (smob);
773 int count = visible_stem_count (me);
776 return ly_interval2scm (pos);
779 extract_grob_set (me, "stems", stems);
780 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
781 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
783 Real my_y = me->relative_coordinate (commony, Y_AXIS);
785 Grob *fvs = first_visible_stem (me);
786 Grob *lvs = last_visible_stem (me);
788 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
789 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
790 Stem::get_stem_info (lvs).ideal_y_
791 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
793 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
794 for (int i = 0; i < stems.size (); i++)
798 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
801 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
809 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
810 Stem::chord_start_y (last_visible_stem (me)));
812 /* Simple beams (2 stems) on middle line should be allowed to be
815 However, if both stems reach middle line,
816 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
818 For that case, we apply artificial slope */
819 if (!ideal[LEFT] && chord.delta () && count == 2)
822 Direction d = (Direction) (sign (chord.delta ()) * UP);
823 pos[d] = get_thickness (me) / 2;
830 For broken beams this doesn't work well. In this case, the
831 slope esp. of the first part of a broken beam should predict
832 where the second part goes.
834 ldy = pos[RIGHT] - pos[LEFT];
838 Array<Offset> ideals;
839 for (int i = 0; i < stems.size (); i++)
842 if (Stem::is_invisible (s))
844 ideals.push (Offset (x_posns[i],
845 Stem::get_stem_info (s).ideal_y_
846 + s->relative_coordinate (commony, Y_AXIS)
850 minimise_least_squares (&slope, &y, ideals);
854 set_minimum_dy (me, &dy);
857 pos = Interval (y, (y + dy));
861 "position" is relative to the staff.
863 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
865 me->set_property ("least-squares-dy", scm_from_double (ldy));
866 return ly_interval2scm (pos);
870 We can't combine with previous function, since check concave and
871 slope damping comes first.
873 TODO: we should use the concaveness to control the amount of damping
876 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
878 Beam::shift_region_to_valid (SCM grob, SCM posns)
880 Grob *me = unsmob_grob (grob);
885 extract_grob_set (me, "stems", stems);
886 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
887 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
889 Grob *fvs = first_visible_stem (me);
894 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
895 for (int i = 0; i < stems.size (); i++)
899 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
903 Grob *lvs = last_visible_stem (me);
907 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
909 Drul_array<Real> pos = ly_scm2interval (posns);
912 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
914 Real dy = pos[RIGHT] - pos[LEFT];
916 Real slope = dx ? (dy / dx) : 0.0;
919 Shift the positions so that we have a chance of finding good
920 quants (i.e. no short stem failures.)
922 Interval feasible_left_point;
923 feasible_left_point.set_full ();
924 for (int i = 0; i < stems.size (); i++)
927 if (Stem::is_invisible (s))
930 Direction d = get_grob_direction (s);
933 = Stem::get_stem_info (s).shortest_y_
934 - slope * x_posns [i];
937 left_y is now relative to the stem S. We want relative to
938 ourselves, so translate:
941 += + s->relative_coordinate (commony, Y_AXIS)
942 - me->relative_coordinate (commony, Y_AXIS);
948 feasible_left_point.intersect (flp);
951 if (feasible_left_point.is_empty ())
952 warning (_ ("no viable initial configuration found: may not find good beam slope"));
953 else if (!feasible_left_point.contains (y))
955 const int REGION_SIZE = 2; // UGH UGH
956 if (isinf (feasible_left_point[DOWN]))
957 y = feasible_left_point[UP] - REGION_SIZE;
958 else if (isinf (feasible_left_point[UP]))
959 y = feasible_left_point[DOWN]+ REGION_SIZE;
961 y = feasible_left_point.center ();
964 pos = Drul_array<Real> (y, (y + dy));
965 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
967 return ly_interval2scm (pos);
970 /* This neat trick is by Werner Lemberg,
971 damped = tanh (slope)
972 corresponds with some tables in [Wanske] CHECKME */
973 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
975 Beam::slope_damping (SCM smob, SCM posns)
977 Grob *me = unsmob_grob (smob);
978 Drul_array<Real> pos = ly_scm2interval (posns);
980 if (visible_stem_count (me) <= 1)
984 SCM s = me->get_property ("damping");
985 Real damping = scm_to_double (s);
986 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
987 if (concaveness >= 10000)
989 pos[LEFT] = pos[RIGHT];
990 me->set_property ("least-squares-dy", scm_from_double (0));
996 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
998 Real dy = pos[RIGHT] - pos[LEFT];
1000 Grob *fvs = first_visible_stem (me);
1001 Grob *lvs = last_visible_stem (me);
1003 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1005 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1006 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1008 Real slope = dy && dx ? dy / dx : 0;
1010 slope = 0.6 * tanh (slope) / (damping + concaveness);
1012 Real damped_dy = slope * dx;
1014 set_minimum_dy (me, &damped_dy);
1016 pos[LEFT] += (dy - damped_dy) / 2;
1017 pos[RIGHT] -= (dy - damped_dy) / 2;
1019 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1022 return ly_interval2scm (pos);
1026 Report slice containing the numbers that are both in (car BEAMING)
1030 where_are_the_whole_beams (SCM beaming)
1034 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1036 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1038 l.add_point (scm_to_int (scm_car (s)));
1044 /* Return the Y position of the stem-end, given the Y-left, Y-right
1045 in POS for stem S. This Y position is relative to S. */
1047 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1049 Drul_array<Real> pos, bool french)
1051 Real beam_translation = get_beam_translation (me);
1053 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1054 Real dy = pos[RIGHT] - pos[LEFT];
1056 Real stem_y_beam0 = (dy && dx
1061 Direction my_dir = get_grob_direction (s);
1062 SCM beaming = s->get_property ("beaming");
1064 Real stem_y = stem_y_beam0;
1067 Slice bm = where_are_the_whole_beams (beaming);
1068 if (!bm.is_empty ())
1069 stem_y += beam_translation * bm[-my_dir];
1073 Slice bm = Stem::beam_multiplicity (s);
1074 if (!bm.is_empty ())
1075 stem_y += bm[my_dir] * beam_translation;
1078 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1079 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1085 Hmm. At this time, beam position and slope are determined. Maybe,
1086 stem directions and length should set to relative to the chord's
1087 position of the beam. */
1088 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 1);
1090 Beam::set_stem_lengths (SCM smob)
1092 Grob *me = unsmob_grob (smob);
1094 /* trigger callback. */
1095 (void) me->get_property ("direction");
1097 SCM posns = me->get_property ("positions");
1099 extract_grob_set (me, "stems", stems);
1104 for (int a = 2; a--;)
1105 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1107 Drul_array<Real> pos = ly_scm2realdrul (posns);
1108 Real staff_space = Staff_symbol_referencer::staff_space (me);
1109 scale_drul (&pos, staff_space);
1113 if (scm_is_number (me->get_property ("gap-count"))
1114 && scm_to_int (me->get_property ("gap-count")))
1117 thick = get_thickness (me);
1120 Grob *fvs = first_visible_stem (me);
1121 Grob *lvs = last_visible_stem (me);
1123 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1124 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1126 for (int i = 0; i < stems.size (); i++)
1129 if (Stem::is_invisible (s))
1132 bool french = to_boolean (s->get_property ("french-beaming"));
1133 Real stem_y = calc_stem_y (me, s, common,
1135 pos, french && s != lvs && s!= fvs);
1138 Make the stems go up to the end of the beam. This doesn't matter
1139 for normal beams, but for tremolo beams it looks silly otherwise.
1142 stem_y += thick * 0.5 * get_grob_direction (s);
1144 Stem::set_stemend (s, 2 * stem_y / staff_space);
1151 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1153 extract_grob_set (me, "stems", stems);
1156 for (int i = 0; i < stems.size (); i++)
1159 Don't overwrite user settings.
1163 Grob *stem = stems[i];
1164 SCM beaming_prop = stem->get_property ("beaming");
1165 if (beaming_prop == SCM_EOL
1166 || index_get_cell (beaming_prop, d) == SCM_EOL)
1168 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1170 && i < stems.size () -1
1171 && Stem::is_invisible (stem))
1172 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1174 Stem::set_beaming (stem, b, d);
1177 while (flip (&d) != LEFT);
1182 Beam::forced_stem_count (Grob *me)
1184 extract_grob_set (me, "stems", stems);
1187 for (int i = 0; i < stems.size (); i++)
1191 if (Stem::is_invisible (s))
1194 /* I can imagine counting those boundaries as a half forced stem,
1195 but let's count them full for now. */
1196 Direction defdir = to_dir (s->get_property ("default-direction"));
1198 if (abs (Stem::chord_start_y (s)) > 0.1
1200 && get_grob_direction (s) != defdir)
1207 Beam::visible_stem_count (Grob *me)
1209 extract_grob_set (me, "stems", stems);
1211 for (int i = stems.size (); i--;)
1213 if (!Stem::is_invisible (stems[i]))
1220 Beam::first_visible_stem (Grob *me)
1222 extract_grob_set (me, "stems", stems);
1224 for (int i = 0; i < stems.size (); i++)
1226 if (!Stem::is_invisible (stems[i]))
1233 Beam::last_visible_stem (Grob *me)
1235 extract_grob_set (me, "stems", stems);
1237 for (int i = stems.size (); i--;)
1239 if (!Stem::is_invisible (stems[i]))
1248 handle rest under beam (do_post: beams are calculated now)
1249 what about combination of collisions and rest under beam.
1253 rest -> stem -> beam -> interpolate_y_position ()
1255 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1257 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1259 Grob *rest = unsmob_grob (smob);
1260 if (scm_is_number (rest->get_property ("staff-position")))
1261 return scm_from_int (0);
1263 Real offset = robust_scm2double (prev_offset, 0.0);
1265 Grob *st = unsmob_grob (rest->get_object ("stem"));
1268 return scm_from_double (0.0);
1269 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1271 || !Beam::has_interface (beam)
1272 || !Beam::visible_stem_count (beam))
1273 return scm_from_double (0.0);
1275 Drul_array<Real> pos (0, 0);
1276 SCM s = beam->get_property ("positions");
1277 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1278 pos = ly_scm2interval (s);
1280 programming_error ("positions property should always be pair of numbers.");
1282 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1284 scale_drul (&pos, staff_space);
1286 Real dy = pos[RIGHT] - pos[LEFT];
1288 Drul_array<Grob*> visible_stems (first_visible_stem (beam),
1289 last_visible_stem (beam));
1290 extract_grob_set (beam, "stems", stems);
1292 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1294 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1295 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1296 Real slope = dy && dx ? dy / dx : 0;
1298 Direction d = get_grob_direction (stem);
1299 Real stem_y = pos[LEFT]
1300 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1302 Real beam_translation = get_beam_translation (beam);
1303 Real beam_thickness = Beam::get_thickness (beam);
1306 TODO: this is not strictly correct for 16th knee beams.
1309 = Stem::beam_multiplicity (stem).length () + 1;
1311 Real height_of_my_beams = beam_thickness / 2
1312 + (beam_count - 1) * beam_translation;
1313 Real beam_y = stem_y - d * height_of_my_beams;
1315 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1316 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1317 rest_extent.translate (offset);
1319 Real rest_dim = rest_extent[d];
1320 Real minimum_distance
1321 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1322 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1324 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1326 shift /= staff_space;
1327 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1329 /* Always move discretely by half spaces */
1330 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1332 /* Inside staff, move by whole spaces*/
1333 if ((rest_extent[d] + staff_space * shift) * d
1335 || (rest_extent[-d] + staff_space * shift) * -d
1337 shift = ceil (fabs (shift)) * sign (shift);
1339 return scm_from_double (staff_space * shift);
1343 Beam::is_knee (Grob *me)
1345 SCM k = me->get_property ("knee");
1346 if (scm_is_bool (k))
1347 return ly_scm2bool (k);
1351 extract_grob_set (me, "stems", stems);
1352 for (int i = stems.size (); i--;)
1354 Direction dir = get_grob_direction (stems[i]);
1363 me->set_property ("knee", ly_bool2scm (knee));
1369 Beam::get_direction_beam_count (Grob *me, Direction d)
1371 extract_grob_set (me, "stems", stems);
1374 for (int i = stems.size (); i--;)
1377 Should we take invisible stems into account?
1379 if (get_grob_direction (stems[i]) == d)
1380 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1386 ADD_INTERFACE (Beam,
1390 "The @code{thickness} property is the weight of beams, "
1391 "measured in staffspace. The @code{direction} "
1392 "property is not user-serviceable. Use "
1393 "the @code{direction} property of @code{Stem} instead. "
1399 "beamed-stem-shorten "
1413 "neutral-direction "
1416 "quantized-positions "