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 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
105 Beam::calc_direction (SCM smob)
107 Grob *me = unsmob_grob (smob);
109 /* Beams with less than 2 two stems don't make much sense, but could happen
114 For a beam that only has one stem, we try to do some disappearance magic:
115 we revert the flag, and move on to The Eternal Engraving Fields. */
117 Direction d = CENTER;
119 int count = visible_stem_count (me);
122 extract_grob_set (me, "stems", stems);
123 if (stems.size () == 1)
125 me->warning (_ ("removing beam with less than two stems"));
127 stems[0]->set_object ("beam", SCM_EOL);
130 return SCM_UNSPECIFIED;
132 else if (stems.size () == 0)
135 return SCM_UNSPECIFIED;
139 Grob *stem = first_visible_stem (me);
142 ugh: stems[0] case happens for chord tremolo.
144 d = to_dir ((stem ? stem : stems[0])->get_property ("default-direction"));
151 d = get_default_dir (me);
153 consider_auto_knees (me);
158 set_stem_directions (me, d);
161 return scm_from_int (d);
166 /* We want a maximal number of shared beams, but if there is choice, we
167 * take the one that is closest to the end of the stem. This is for
179 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
183 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
187 for (int i = lslice[-left_dir];
188 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
191 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
193 int k = -right_dir * scm_to_int (scm_car (s)) + i;
194 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
198 if (count >= best_count)
208 MAKE_SCHEME_CALLBACK(Beam, calc_beaming, 1)
210 Beam::calc_beaming (SCM smob)
212 Grob *me = unsmob_grob (smob);
214 extract_grob_set (me, "stems", stems);
217 last_int.set_empty ();
219 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
220 Direction last_dir = CENTER;
221 for (vsize i = 0; i < stems.size (); i++)
223 Grob *this_stem = stems[i];
224 SCM this_beaming = this_stem->get_property ("beaming");
226 Direction this_dir = get_grob_direction (this_stem);
227 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
229 int start_point = position_with_maximal_common_beams
230 (last_beaming, this_beaming,
231 last_dir ? last_dir : this_dir,
238 new_slice.set_empty ();
239 SCM s = index_get_cell (this_beaming, d);
240 for (; scm_is_pair (s); s = scm_cdr (s))
243 = start_point - this_dir * scm_to_int (scm_car (s));
245 new_slice.add_point (new_beam_pos);
246 scm_set_car_x (s, scm_from_int (new_beam_pos));
249 while (flip (&d) != LEFT);
251 if (!new_slice.is_empty ())
252 last_int = new_slice;
256 SCM s = scm_cdr (this_beaming);
257 for (; scm_is_pair (s); s = scm_cdr (s))
259 int np = -this_dir * scm_to_int (scm_car (s));
260 scm_set_car_x (s, scm_from_int (np));
261 last_int.add_point (np);
265 if (scm_ilength (scm_cdr (this_beaming)) > 0)
267 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.back ()->relative_coordinate (xcommon, X_AXIS) - x0;
306 SCM posns = me->get_property ("quantized-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 ("line-thickness"));
333 for (vsize 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 vector<int> full_beams;
350 vector<int> lfliebertjes;
351 vector<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)
358 full_beams.push_back (b);
360 lfliebertjes.push_back (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_back (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 ("blot-diameter"));
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 vector_sort (full_beams, default_compare);
400 reverse (full_beams);
404 for (vsize 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 (vsize 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 (vsize 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 ();
519 #define iterof(i,s) typeof((s).begin()) i((s).begin())
522 Beam::get_default_dir (Grob *me)
524 extract_grob_set (me, "stems", stems);
526 Drul_array<Real> extremes (0.0, 0.0);
527 for (iterof (s, stems); s != stems.end (); s++)
529 Interval positions = Stem::head_positions (*s);
533 if (sign (positions[d]) == d)
534 extremes[d] = d * max (d * positions[d], d * extremes[d]);
536 while (flip (&d) != DOWN);
539 Drul_array<int> total (0, 0);
540 Drul_array<int> count (0, 0);
542 bool force_dir = false;
543 for (vsize i = 0; i < stems.size (); i++)
546 Direction stem_dir = CENTER;
547 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
548 if (is_direction (stem_dir_scm))
550 stem_dir = to_dir (stem_dir_scm);
554 stem_dir = to_dir (s->get_property ("default-direction"));
557 stem_dir = to_dir (s->get_property ("neutral-direction"));
562 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
569 if (abs (extremes[UP]) > -extremes[DOWN])
571 else if (extremes[UP] < -extremes[DOWN])
575 Direction dir = CENTER;
576 Direction d = CENTER;
577 if ((d = (Direction) sign (count[UP] - count[DOWN])))
581 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
583 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
586 dir = to_dir (me->get_property ("neutral-direction"));
591 /* Set all stems with non-forced direction to beam direction.
592 Urg: non-forced should become `without/with unforced' direction,
593 once stem gets cleaned-up. */
595 Beam::set_stem_directions (Grob *me, Direction d)
597 extract_grob_set (me, "stems", stems);
599 for (vsize i = 0; i < stems.size (); i++)
603 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
604 if (!to_dir (forcedir))
605 set_grob_direction (s, d);
610 Only try horizontal beams for knees. No reliable detection of
611 anything else is possible here, since we don't know funky-beaming
612 settings, or X-distances (slopes!) People that want sloped
613 knee-beams, should set the directions manually.
618 this routine should take into account the stemlength scoring
619 of a possible knee/nonknee beam.
622 Beam::consider_auto_knees (Grob *me)
624 SCM scm = me->get_property ("auto-knee-gap");
625 if (!scm_is_number (scm))
632 extract_grob_set (me, "stems", stems);
634 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
635 Real staff_space = Staff_symbol_referencer::staff_space (me);
637 vector<Interval> head_extents_array;
638 for (vsize i = 0; i < stems.size (); i++)
640 Grob *stem = stems[i];
641 if (Stem::is_invisible (stem))
644 Interval head_extents = Stem::head_positions (stem);
645 if (!head_extents.is_empty ())
647 head_extents[LEFT] += -1;
648 head_extents[RIGHT] += 1;
649 head_extents *= staff_space * 0.5;
652 We could subtract beam Y position, but this routine only
653 sets stem directions, a constant shift does not have an
656 head_extents += stem->relative_coordinate (common, Y_AXIS);
658 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
660 Direction stemdir = to_dir (stem->get_property ("direction"));
661 head_extents[-stemdir] = -stemdir * infinity_f;
664 head_extents_array.push_back (head_extents);
666 gaps.remove_interval (head_extents);
670 Real max_gap_len = 0.0;
672 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
674 Interval gap = gaps.allowed_regions_[i];
677 the outer gaps are not knees.
679 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
682 if (gap.length () >= max_gap_len)
684 max_gap_len = gap.length ();
689 Real beam_translation = get_beam_translation (me);
690 Real beam_thickness = Beam::get_thickness (me);
691 int beam_count = Beam::get_beam_count (me);
692 Real height_of_beams = beam_thickness / 2
693 + (beam_count - 1) * beam_translation;
694 Real threshold = scm_to_double (scm) + height_of_beams;
696 if (max_gap_len > threshold)
699 for (vsize i = 0; i < stems.size (); i++)
701 Grob *stem = stems[i];
702 if (Stem::is_invisible (stem))
705 Interval head_extents = head_extents_array[j++];
707 Direction d = (head_extents.center () < max_gap.center ())
710 stem->set_property ("direction", scm_from_int (d));
712 head_extents.intersect (max_gap);
713 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
718 /* Set stem's shorten property if unset.
721 take some y-position (chord/beam/nearest?) into account
722 scmify forced-fraction
724 This is done in beam because the shorten has to be uniform over the
731 set_minimum_dy (Grob *me, Real *dy)
736 If dy is smaller than the smallest quant, we
737 get absurd direction-sign penalties.
740 Real ss = Staff_symbol_referencer::staff_space (me);
741 Real thickness = Beam::get_thickness (me) / ss;
742 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
743 Real sit = (thickness - slt) / 2;
745 Real hang = 1.0 - (thickness - slt) / 2;
747 *dy = sign (*dy) * max (fabs (*dy),
748 min (min (sit, inter), hang));
754 MAKE_SCHEME_CALLBACK(Beam, calc_stem_shorten, 1)
756 Beam::calc_stem_shorten (SCM smob)
758 Grob *me = unsmob_grob (smob);
761 shortening looks silly for x staff beams
764 return scm_from_int (0);
766 Real forced_fraction = 1.0 * forced_stem_count (me)
767 / visible_stem_count (me);
769 int beam_count = get_beam_count (me);
771 SCM shorten_list = me->get_property ("beamed-stem-shorten");
772 if (shorten_list == SCM_EOL)
773 return scm_from_int (0);
775 Real staff_space = Staff_symbol_referencer::staff_space (me);
778 = robust_list_ref (beam_count -1, shorten_list);
779 Real shorten = scm_to_double (shorten_elt) * staff_space;
781 shorten *= forced_fraction;
785 return scm_from_double (shorten);
787 return scm_from_double (0.0);
793 Compute a first approximation to the beam slope.
795 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
797 Beam::calc_least_squares_positions (SCM smob, SCM posns)
801 Grob *me = unsmob_grob (smob);
803 int count = visible_stem_count (me);
806 return ly_interval2scm (pos);
808 vector<Real> x_posns;
809 extract_grob_set (me, "stems", stems);
810 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
811 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
813 Real my_y = me->relative_coordinate (commony, Y_AXIS);
815 Grob *fvs = first_visible_stem (me);
816 Grob *lvs = last_visible_stem (me);
818 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
819 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
820 Stem::get_stem_info (lvs).ideal_y_
821 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
823 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
824 for (vsize i = 0; i < stems.size (); i++)
828 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
829 x_posns.push_back (x);
831 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
839 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
840 Stem::chord_start_y (last_visible_stem (me)));
842 /* Simple beams (2 stems) on middle line should be allowed to be
845 However, if both stems reach middle line,
846 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
848 For that case, we apply artificial slope */
849 if (!ideal[LEFT] && chord.delta () && count == 2)
852 Direction d = (Direction) (sign (chord.delta ()) * UP);
853 pos[d] = get_thickness (me) / 2;
860 For broken beams this doesn't work well. In this case, the
861 slope esp. of the first part of a broken beam should predict
862 where the second part goes.
864 ldy = pos[RIGHT] - pos[LEFT];
868 vector<Offset> ideals;
869 for (vsize i = 0; i < stems.size (); i++)
872 if (Stem::is_invisible (s))
874 ideals.push_back (Offset (x_posns[i],
875 Stem::get_stem_info (s).ideal_y_
876 + s->relative_coordinate (commony, Y_AXIS)
880 minimise_least_squares (&slope, &y, ideals);
884 set_minimum_dy (me, &dy);
887 pos = Interval (y, (y + dy));
891 "position" is relative to the staff.
893 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
895 me->set_property ("least-squares-dy", scm_from_double (ldy));
896 return ly_interval2scm (pos);
900 We can't combine with previous function, since check concave and
901 slope damping comes first.
903 TODO: we should use the concaveness to control the amount of damping
906 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
908 Beam::shift_region_to_valid (SCM grob, SCM posns)
910 Grob *me = unsmob_grob (grob);
914 vector<Real> x_posns;
915 extract_grob_set (me, "stems", stems);
916 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
917 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
919 Grob *fvs = first_visible_stem (me);
924 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
925 for (vsize i = 0; i < stems.size (); i++)
929 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
930 x_posns.push_back (x);
933 Grob *lvs = last_visible_stem (me);
937 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
939 Drul_array<Real> pos = ly_scm2interval (posns);
942 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
944 Real dy = pos[RIGHT] - pos[LEFT];
946 Real slope = dx ? (dy / dx) : 0.0;
949 Shift the positions so that we have a chance of finding good
950 quants (i.e. no short stem failures.)
952 Interval feasible_left_point;
953 feasible_left_point.set_full ();
954 for (vsize i = 0; i < stems.size (); i++)
957 if (Stem::is_invisible (s))
960 Direction d = get_grob_direction (s);
963 = Stem::get_stem_info (s).shortest_y_
964 - slope * x_posns [i];
967 left_y is now relative to the stem S. We want relative to
968 ourselves, so translate:
971 += + s->relative_coordinate (commony, Y_AXIS)
972 - me->relative_coordinate (commony, Y_AXIS);
978 feasible_left_point.intersect (flp);
981 if (feasible_left_point.is_empty ())
982 warning (_ ("no viable initial configuration found: may not find good beam slope"));
983 else if (!feasible_left_point.contains (y))
985 const int REGION_SIZE = 2; // UGH UGH
986 if (isinf (feasible_left_point[DOWN]))
987 y = feasible_left_point[UP] - REGION_SIZE;
988 else if (isinf (feasible_left_point[UP]))
989 y = feasible_left_point[DOWN]+ REGION_SIZE;
991 y = feasible_left_point.center ();
994 pos = Drul_array<Real> (y, (y + dy));
995 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
997 return ly_interval2scm (pos);
1000 /* This neat trick is by Werner Lemberg,
1001 damped = tanh (slope)
1002 corresponds with some tables in [Wanske] CHECKME */
1003 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
1005 Beam::slope_damping (SCM smob, SCM posns)
1007 Grob *me = unsmob_grob (smob);
1008 Drul_array<Real> pos = ly_scm2interval (posns);
1010 if (visible_stem_count (me) <= 1)
1014 SCM s = me->get_property ("damping");
1015 Real damping = scm_to_double (s);
1016 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1017 if (concaveness >= 10000)
1019 pos[LEFT] = pos[RIGHT];
1020 me->set_property ("least-squares-dy", scm_from_double (0));
1026 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1028 Real dy = pos[RIGHT] - pos[LEFT];
1030 Grob *fvs = first_visible_stem (me);
1031 Grob *lvs = last_visible_stem (me);
1033 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1035 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1036 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1038 Real slope = dy && dx ? dy / dx : 0;
1040 slope = 0.6 * tanh (slope) / (damping + concaveness);
1042 Real damped_dy = slope * dx;
1044 set_minimum_dy (me, &damped_dy);
1046 pos[LEFT] += (dy - damped_dy) / 2;
1047 pos[RIGHT] -= (dy - damped_dy) / 2;
1049 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1052 return ly_interval2scm (pos);
1056 Report slice containing the numbers that are both in (car BEAMING)
1060 where_are_the_whole_beams (SCM beaming)
1064 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1066 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1068 l.add_point (scm_to_int (scm_car (s)));
1074 /* Return the Y position of the stem-end, given the Y-left, Y-right
1075 in POS for stem S. This Y position is relative to S. */
1077 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1079 Drul_array<Real> pos, bool french)
1081 Real beam_translation = get_beam_translation (me);
1083 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1084 Real dy = pos[RIGHT] - pos[LEFT];
1086 Real stem_y_beam0 = (dy && dx
1091 Direction my_dir = get_grob_direction (s);
1092 SCM beaming = s->get_property ("beaming");
1094 Real stem_y = stem_y_beam0;
1097 Slice bm = where_are_the_whole_beams (beaming);
1098 if (!bm.is_empty ())
1099 stem_y += beam_translation * bm[-my_dir];
1103 Slice bm = Stem::beam_multiplicity (s);
1104 if (!bm.is_empty ())
1105 stem_y += bm[my_dir] * beam_translation;
1108 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1109 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1115 Hmm. At this time, beam position and slope are determined. Maybe,
1116 stem directions and length should set to relative to the chord's
1117 position of the beam. */
1118 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 1);
1120 Beam::set_stem_lengths (SCM smob)
1122 Grob *me = unsmob_grob (smob);
1124 /* trigger callback. */
1125 (void) me->get_property ("direction");
1127 SCM posns = me->get_property ("positions");
1129 extract_grob_set (me, "stems", stems);
1134 for (int a = 2; a--;)
1135 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1137 Drul_array<Real> pos = ly_scm2realdrul (posns);
1138 Real staff_space = Staff_symbol_referencer::staff_space (me);
1139 scale_drul (&pos, staff_space);
1143 if (scm_is_number (me->get_property ("gap-count"))
1144 && scm_to_int (me->get_property ("gap-count")))
1147 thick = get_thickness (me);
1150 Grob *fvs = first_visible_stem (me);
1151 Grob *lvs = last_visible_stem (me);
1153 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1154 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1156 for (vsize i = 0; i < stems.size (); i++)
1159 if (Stem::is_invisible (s))
1162 bool french = to_boolean (s->get_property ("french-beaming"));
1163 Real stem_y = calc_stem_y (me, s, common,
1165 pos, french && s != lvs && s!= fvs);
1168 Make the stems go up to the end of the beam. This doesn't matter
1169 for normal beams, but for tremolo beams it looks silly otherwise.
1172 stem_y += thick * 0.5 * get_grob_direction (s);
1174 Stem::set_stemend (s, 2 * stem_y / staff_space);
1181 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1183 extract_grob_set (me, "stems", stems);
1186 for (vsize i = 0; i < stems.size (); i++)
1189 Don't overwrite user settings.
1193 Grob *stem = stems[i];
1194 SCM beaming_prop = stem->get_property ("beaming");
1195 if (beaming_prop == SCM_EOL
1196 || index_get_cell (beaming_prop, d) == SCM_EOL)
1198 int b = beaming->infos_.at (i).beams_i_drul_[d];
1200 && i < stems.size () -1
1201 && Stem::is_invisible (stem))
1202 b = min (b, beaming->infos_.at (i).beams_i_drul_[-d]);
1204 Stem::set_beaming (stem, b, d);
1207 while (flip (&d) != LEFT);
1212 Beam::forced_stem_count (Grob *me)
1214 extract_grob_set (me, "stems", stems);
1217 for (vsize i = 0; i < stems.size (); i++)
1221 if (Stem::is_invisible (s))
1224 /* I can imagine counting those boundaries as a half forced stem,
1225 but let's count them full for now. */
1226 Direction defdir = to_dir (s->get_property ("default-direction"));
1228 if (abs (Stem::chord_start_y (s)) > 0.1
1230 && get_grob_direction (s) != defdir)
1237 Beam::visible_stem_count (Grob *me)
1239 extract_grob_set (me, "stems", stems);
1241 for (vsize i = stems.size (); i--;)
1243 if (!Stem::is_invisible (stems[i]))
1250 Beam::first_visible_stem (Grob *me)
1252 extract_grob_set (me, "stems", stems);
1254 for (vsize i = 0; i < stems.size (); i++)
1256 if (!Stem::is_invisible (stems[i]))
1263 Beam::last_visible_stem (Grob *me)
1265 extract_grob_set (me, "stems", stems);
1267 for (vsize i = stems.size (); i--;)
1269 if (!Stem::is_invisible (stems[i]))
1278 handle rest under beam (do_post: beams are calculated now)
1279 what about combination of collisions and rest under beam.
1283 rest -> stem -> beam -> interpolate_y_position ()
1285 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1287 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1289 Grob *rest = unsmob_grob (smob);
1290 if (scm_is_number (rest->get_property ("staff-position")))
1291 return scm_from_int (0);
1293 Real offset = robust_scm2double (prev_offset, 0.0);
1295 Grob *st = unsmob_grob (rest->get_object ("stem"));
1298 return scm_from_double (0.0);
1299 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1301 || !Beam::has_interface (beam)
1302 || !Beam::visible_stem_count (beam))
1303 return scm_from_double (0.0);
1305 Drul_array<Real> pos (0, 0);
1306 SCM s = beam->get_property ("positions");
1307 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1308 pos = ly_scm2interval (s);
1310 programming_error ("positions property should always be pair of numbers.");
1312 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1314 scale_drul (&pos, staff_space);
1316 Real dy = pos[RIGHT] - pos[LEFT];
1318 Drul_array<Grob*> visible_stems (first_visible_stem (beam),
1319 last_visible_stem (beam));
1320 extract_grob_set (beam, "stems", stems);
1322 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1324 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1325 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1326 Real slope = dy && dx ? dy / dx : 0;
1328 Direction d = get_grob_direction (stem);
1329 Real stem_y = pos[LEFT]
1330 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1332 Real beam_translation = get_beam_translation (beam);
1333 Real beam_thickness = Beam::get_thickness (beam);
1336 TODO: this is not strictly correct for 16th knee beams.
1339 = Stem::beam_multiplicity (stem).length () + 1;
1341 Real height_of_my_beams = beam_thickness / 2
1342 + (beam_count - 1) * beam_translation;
1343 Real beam_y = stem_y - d * height_of_my_beams;
1345 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1346 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1347 rest_extent.translate (offset);
1349 Real rest_dim = rest_extent[d];
1350 Real minimum_distance
1351 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1352 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1354 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1356 shift /= staff_space;
1357 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1359 /* Always move discretely by half spaces */
1360 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1362 /* Inside staff, move by whole spaces*/
1363 if ((rest_extent[d] + staff_space * shift) * d
1365 || (rest_extent[-d] + staff_space * shift) * -d
1367 shift = ceil (fabs (shift)) * sign (shift);
1369 return scm_from_double (staff_space * shift);
1373 Beam::is_knee (Grob *me)
1375 SCM k = me->get_property ("knee");
1376 if (scm_is_bool (k))
1377 return ly_scm2bool (k);
1381 extract_grob_set (me, "stems", stems);
1382 for (vsize i = stems.size (); i--;)
1384 Direction dir = get_grob_direction (stems[i]);
1393 me->set_property ("knee", ly_bool2scm (knee));
1399 Beam::get_direction_beam_count (Grob *me, Direction d)
1401 extract_grob_set (me, "stems", stems);
1404 for (vsize i = stems.size (); i--;)
1407 Should we take invisible stems into account?
1409 if (get_grob_direction (stems[i]) == d)
1410 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1416 ADD_INTERFACE (Beam,
1420 "The @code{thickness} property is the weight of beams, "
1421 "measured in staffspace. The @code{direction} "
1422 "property is not user-serviceable. Use "
1423 "the @code{direction} property of @code{Stem} instead. "
1429 "beamed-stem-shorten "
1443 "neutral-direction "
1446 "quantized-positions "