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"
32 #include "international.hh"
33 #include "interval-set.hh"
35 #include "least-squares.hh"
38 #include "output-def.hh"
39 #include "pointer-group-interface.hh"
41 #include "staff-symbol-referencer.hh"
45 #if DEBUG_BEAM_SCORING
46 #include "text-interface.hh" // debug output.
47 #include "font-interface.hh" // debug output.
51 Beam::add_stem (Grob *me, Grob *s)
53 if (Stem::get_beam (s))
55 programming_error ("Stem already has beam");
59 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
60 s->set_object ("beam", me->self_scm ());
61 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
65 Beam::get_thickness (Grob *me)
67 return robust_scm2double (me->get_property ("thickness"), 0)
68 * Staff_symbol_referencer::staff_space (me);
71 /* Return the translation between 2 adjoining beams. */
73 Beam::get_beam_translation (Grob *me)
75 int beam_count = get_beam_count (me);
76 Real staff_space = Staff_symbol_referencer::staff_space (me);
77 Real line = Staff_symbol_referencer::line_thickness (me);
78 Real thickness = get_thickness (me);
79 Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
81 Real beam_translation = beam_count < 4
82 ? (2 * staff_space + line - thickness) / 2.0
83 : (3 * staff_space + line - thickness) / 3.0;
85 return fract * beam_translation;
88 /* Maximum beam_count. */
90 Beam::get_beam_count (Grob *me)
94 extract_grob_set (me, "stems", stems);
95 for (vsize i = 0; i < stems.size (); i++)
97 Grob *stem = stems[i];
98 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
104 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
106 Beam::calc_direction (SCM smob)
108 Grob *me = unsmob_grob (smob);
110 /* Beams with less than 2 two stems don't make much sense, but could happen
115 For a beam that only has one stem, we try to do some disappearance magic:
116 we revert the flag, and move on to The Eternal Engraving Fields. */
118 Direction d = CENTER;
120 int count = visible_stem_count (me);
123 extract_grob_set (me, "stems", stems);
124 if (stems.size () == 1)
126 me->warning (_ ("removing beam with less than two stems"));
128 stems[0]->set_object ("beam", SCM_EOL);
131 return SCM_UNSPECIFIED;
133 else if (stems.size () == 0)
136 return SCM_UNSPECIFIED;
140 Grob *stem = first_visible_stem (me);
143 ugh: stems[0] case happens for chord tremolo.
145 d = to_dir ((stem ? stem : stems[0])->get_property ("default-direction"));
152 d = get_default_dir (me);
154 consider_auto_knees (me);
159 set_stem_directions (me, d);
162 return scm_from_int (d);
167 /* We want a maximal number of shared beams, but if there is choice, we
168 * take the one that is closest to the end of the stem. This is for
180 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
184 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
188 for (int i = lslice[-left_dir];
189 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
192 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
194 int k = -right_dir * scm_to_int (scm_car (s)) + i;
195 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
199 if (count >= best_count)
209 MAKE_SCHEME_CALLBACK(Beam, calc_beaming, 1)
211 Beam::calc_beaming (SCM smob)
213 Grob *me = unsmob_grob (smob);
215 extract_grob_set (me, "stems", stems);
218 last_int.set_empty ();
220 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
221 Direction last_dir = CENTER;
222 for (vsize i = 0; i < stems.size (); i++)
224 Grob *this_stem = stems[i];
225 SCM this_beaming = this_stem->get_property ("beaming");
227 Direction this_dir = get_grob_direction (this_stem);
228 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
230 int start_point = position_with_maximal_common_beams
231 (last_beaming, this_beaming,
232 last_dir ? last_dir : this_dir,
239 new_slice.set_empty ();
240 SCM s = index_get_cell (this_beaming, d);
241 for (; scm_is_pair (s); s = scm_cdr (s))
244 = start_point - this_dir * scm_to_int (scm_car (s));
246 new_slice.add_point (new_beam_pos);
247 scm_set_car_x (s, scm_from_int (new_beam_pos));
250 while (flip (&d) != LEFT);
252 if (!new_slice.is_empty ())
253 last_int = new_slice;
257 SCM s = scm_cdr (this_beaming);
258 for (; scm_is_pair (s); s = scm_cdr (s))
260 int np = -this_dir * scm_to_int (scm_car (s));
261 scm_set_car_x (s, scm_from_int (np));
262 last_int.add_point (np);
266 if (scm_ilength (scm_cdr (this_beaming)) > 0)
268 last_beaming = this_beaming;
277 I really enjoy spaghetti, but spaghetti should be kept on a plate
278 with a little garlic and olive oil. This is too much.
282 MAKE_SCHEME_CALLBACK (Beam, print, 1);
284 Beam::print (SCM grob)
286 Spanner *me = unsmob_spanner (grob);
288 extract_grob_set (me, "stems", stems);
289 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
291 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
292 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
295 if (visible_stem_count (me))
297 // ugh -> use commonx
298 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
299 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
303 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
304 dx = stems.back ()->relative_coordinate (xcommon, X_AXIS) - x0;
307 SCM posns = me->get_property ("quantized-positions");
308 Drul_array<Real> pos;
309 if (!is_number_pair (posns))
311 programming_error ("no beam positions?");
312 pos = Interval (0, 0);
315 pos = ly_scm2realdrul (posns);
317 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
319 Real dy = pos[RIGHT] - pos[LEFT];
320 Real slope = (dy && dx) ? dy / dx : 0;
322 Real thick = get_thickness (me);
323 Real bdy = get_beam_translation (me);
325 SCM last_beaming = SCM_EOL;
326 Real last_xposn = -1;
327 Real last_stem_width = -1;
329 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
332 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
334 for (vsize i = 0; i <= stems.size (); i++)
336 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
338 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
339 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
340 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
341 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
343 We do the space left of ST, with lfliebertjes pointing to the
344 right from the left stem, and rfliebertjes pointing left from
347 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
348 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
350 vector<int> full_beams;
351 vector<int> lfliebertjes;
352 vector<int> rfliebertjes;
355 scm_is_pair (s); s = scm_cdr (s))
357 int b = scm_to_int (scm_car (s));
358 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
359 full_beams.push_back (b);
361 lfliebertjes.push_back (b);
364 scm_is_pair (s); s = scm_cdr (s))
366 int b = scm_to_int (scm_car (s));
367 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
368 rfliebertjes.push_back (b);
371 Drul_array<Real> break_overshoot
372 = robust_scm2drul (me->get_property ("break-overshoot"),
373 Drul_array<Real> (-0.5, 0.0));
375 Real w = (i > 0 && stem)
376 ? (xposn - last_xposn)
377 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
379 Real stem_offset = 0.0;
382 w += last_stem_width / 2;
383 stem_offset = -last_stem_width / 2;
389 Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
390 Stencil whole = Lookup::beam (slope, w, thick, blot);
394 if (scm_is_number (me->get_property ("gap-count")))
396 gap_count = scm_to_int (me->get_property ("gap-count"));
397 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
399 vector_sort (full_beams, default_compare);
401 reverse (full_beams);
405 for (vsize j = full_beams.size (); j--;)
412 b.translate_axis (gap_length, X_AXIS);
414 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
415 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
417 the_beam.add_stencil (b);
420 if (lfliebertjes.size () || rfliebertjes.size ())
426 int t = Stem::duration_log (stem);
434 nw_f = break_overshoot[RIGHT] / 2;
436 /* Half beam should be one note-width,
437 but let's make sure two half-beams never touch */
441 rw = min (nw_f, ((xposn - last_xposn) / 2));
444 if (me->get_bound (LEFT)->break_status_dir ())
445 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
446 + break_overshoot[LEFT];
452 lw = min (nw_f, ((xposn - last_xposn) / 2));
455 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
457 + break_overshoot[RIGHT];
459 rw += stem_width / 2;
460 lw += last_stem_width / 2;
462 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
463 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
464 for (vsize j = lfliebertjes.size (); j--;)
467 b.translate_axis (last_xposn - x0 - last_stem_width /2,
469 b.translate_axis (slope * (last_xposn - x0)
470 + bdy * lfliebertjes[j],
472 the_beam.add_stencil (b);
474 for (vsize j = rfliebertjes.size (); j--;)
477 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
478 b.translate_axis (slope * (xposn - x0 - rw)
479 + bdy * rfliebertjes[j], Y_AXIS);
480 the_beam.add_stencil (b);
485 last_stem_width = stem_width;
486 last_beaming = this_beaming;
489 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
491 the_beam.translate_axis (pos[LEFT], Y_AXIS);
493 #if (DEBUG_BEAM_SCORING)
494 SCM quant_score = me->get_property ("quant-score");
495 SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-scoring"));
496 if (to_boolean (debug) && scm_is_string (quant_score))
500 This code prints the demerits for each beam. Perhaps this
501 should be switchable for those who want to twiddle with the
505 SCM properties = Font_interface::text_font_alist_chain (me);
507 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
509 Stencil score = *unsmob_stencil (Text_interface::interpret_markup
510 (me->layout ()->self_scm (), properties, quant_score));
512 if (!score.is_empty ())
513 the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0, 0);
517 return the_beam.smobbed_copy ();
521 Beam::get_default_dir (Grob *me)
523 extract_grob_set (me, "stems", stems);
525 Drul_array<Real> extremes (0.0, 0.0);
526 for (iterof (s, stems); s != stems.end (); s++)
528 Interval positions = Stem::head_positions (*s);
532 if (sign (positions[d]) == d)
533 extremes[d] = d * max (d * positions[d], d * extremes[d]);
535 while (flip (&d) != DOWN);
538 Drul_array<int> total (0, 0);
539 Drul_array<int> count (0, 0);
541 bool force_dir = false;
542 for (vsize i = 0; i < stems.size (); i++)
545 Direction stem_dir = CENTER;
546 SCM stem_dir_scm = s->get_property_data (ly_symbol2scm ("direction"));
547 if (is_direction (stem_dir_scm))
549 stem_dir = to_dir (stem_dir_scm);
553 stem_dir = to_dir (s->get_property ("default-direction"));
556 stem_dir = to_dir (s->get_property ("neutral-direction"));
561 total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
568 if (abs (extremes[UP]) > -extremes[DOWN])
570 else if (extremes[UP] < -extremes[DOWN])
574 Direction dir = CENTER;
575 Direction d = CENTER;
576 if ((d = (Direction) sign (count[UP] - count[DOWN])))
580 && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
582 else if ((d = (Direction) sign (total[UP] - total[DOWN])))
585 dir = to_dir (me->get_property ("neutral-direction"));
590 /* Set all stems with non-forced direction to beam direction.
591 Urg: non-forced should become `without/with unforced' direction,
592 once stem gets cleaned-up. */
594 Beam::set_stem_directions (Grob *me, Direction d)
596 extract_grob_set (me, "stems", stems);
598 for (vsize i = 0; i < stems.size (); i++)
602 SCM forcedir = s->get_property_data (ly_symbol2scm ("direction"));
603 if (!to_dir (forcedir))
604 set_grob_direction (s, d);
609 Only try horizontal beams for knees. No reliable detection of
610 anything else is possible here, since we don't know funky-beaming
611 settings, or X-distances (slopes!) People that want sloped
612 knee-beams, should set the directions manually.
617 this routine should take into account the stemlength scoring
618 of a possible knee/nonknee beam.
621 Beam::consider_auto_knees (Grob *me)
623 SCM scm = me->get_property ("auto-knee-gap");
624 if (!scm_is_number (scm))
631 extract_grob_set (me, "stems", stems);
633 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
634 Real staff_space = Staff_symbol_referencer::staff_space (me);
636 vector<Interval> head_extents_array;
637 for (vsize i = 0; i < stems.size (); i++)
639 Grob *stem = stems[i];
640 if (Stem::is_invisible (stem))
643 Interval head_extents = Stem::head_positions (stem);
644 if (!head_extents.is_empty ())
646 head_extents[LEFT] += -1;
647 head_extents[RIGHT] += 1;
648 head_extents *= staff_space * 0.5;
651 We could subtract beam Y position, but this routine only
652 sets stem directions, a constant shift does not have an
655 head_extents += stem->relative_coordinate (common, Y_AXIS);
657 if (to_dir (stem->get_property_data (ly_symbol2scm ("direction"))))
659 Direction stemdir = to_dir (stem->get_property ("direction"));
660 head_extents[-stemdir] = -stemdir * infinity_f;
663 head_extents_array.push_back (head_extents);
665 gaps.remove_interval (head_extents);
669 Real max_gap_len = 0.0;
671 for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
673 Interval gap = gaps.allowed_regions_[i];
676 the outer gaps are not knees.
678 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
681 if (gap.length () >= max_gap_len)
683 max_gap_len = gap.length ();
688 Real beam_translation = get_beam_translation (me);
689 Real beam_thickness = Beam::get_thickness (me);
690 int beam_count = Beam::get_beam_count (me);
691 Real height_of_beams = beam_thickness / 2
692 + (beam_count - 1) * beam_translation;
693 Real threshold = scm_to_double (scm) + height_of_beams;
695 if (max_gap_len > threshold)
698 for (vsize i = 0; i < stems.size (); i++)
700 Grob *stem = stems[i];
701 if (Stem::is_invisible (stem))
704 Interval head_extents = head_extents_array[j++];
706 Direction d = (head_extents.center () < max_gap.center ())
709 stem->set_property ("direction", scm_from_int (d));
711 head_extents.intersect (max_gap);
712 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
717 /* Set stem's shorten property if unset.
720 take some y-position (chord/beam/nearest?) into account
721 scmify forced-fraction
723 This is done in beam because the shorten has to be uniform over the
730 set_minimum_dy (Grob *me, Real *dy)
735 If dy is smaller than the smallest quant, we
736 get absurd direction-sign penalties.
739 Real ss = Staff_symbol_referencer::staff_space (me);
740 Real thickness = Beam::get_thickness (me) / ss;
741 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
742 Real sit = (thickness - slt) / 2;
744 Real hang = 1.0 - (thickness - slt) / 2;
746 *dy = sign (*dy) * max (fabs (*dy),
747 min (min (sit, inter), hang));
753 MAKE_SCHEME_CALLBACK(Beam, calc_stem_shorten, 1)
755 Beam::calc_stem_shorten (SCM smob)
757 Grob *me = unsmob_grob (smob);
760 shortening looks silly for x staff beams
763 return scm_from_int (0);
765 Real forced_fraction = 1.0 * forced_stem_count (me)
766 / visible_stem_count (me);
768 int beam_count = get_beam_count (me);
770 SCM shorten_list = me->get_property ("beamed-stem-shorten");
771 if (shorten_list == SCM_EOL)
772 return scm_from_int (0);
774 Real staff_space = Staff_symbol_referencer::staff_space (me);
777 = robust_list_ref (beam_count -1, shorten_list);
778 Real shorten = scm_to_double (shorten_elt) * staff_space;
780 shorten *= forced_fraction;
784 return scm_from_double (shorten);
786 return scm_from_double (0.0);
792 Compute a first approximation to the beam slope.
794 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
796 Beam::calc_least_squares_positions (SCM smob, SCM posns)
800 Grob *me = unsmob_grob (smob);
802 int count = visible_stem_count (me);
805 return ly_interval2scm (pos);
807 vector<Real> x_posns;
808 extract_grob_set (me, "stems", stems);
809 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
810 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
812 Real my_y = me->relative_coordinate (commony, Y_AXIS);
814 Grob *fvs = first_visible_stem (me);
815 Grob *lvs = last_visible_stem (me);
817 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
818 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
819 Stem::get_stem_info (lvs).ideal_y_
820 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
822 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
823 for (vsize i = 0; i < stems.size (); i++)
827 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
828 x_posns.push_back (x);
830 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
838 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
839 Stem::chord_start_y (last_visible_stem (me)));
841 /* Simple beams (2 stems) on middle line should be allowed to be
844 However, if both stems reach middle line,
845 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
847 For that case, we apply artificial slope */
848 if (!ideal[LEFT] && chord.delta () && count == 2)
851 Direction d = (Direction) (sign (chord.delta ()) * UP);
852 pos[d] = get_thickness (me) / 2;
859 For broken beams this doesn't work well. In this case, the
860 slope esp. of the first part of a broken beam should predict
861 where the second part goes.
863 ldy = pos[RIGHT] - pos[LEFT];
867 vector<Offset> ideals;
868 for (vsize i = 0; i < stems.size (); i++)
871 if (Stem::is_invisible (s))
873 ideals.push_back (Offset (x_posns[i],
874 Stem::get_stem_info (s).ideal_y_
875 + s->relative_coordinate (commony, Y_AXIS)
879 minimise_least_squares (&slope, &y, ideals);
883 set_minimum_dy (me, &dy);
886 pos = Interval (y, (y + dy));
890 "position" is relative to the staff.
892 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
894 me->set_property ("least-squares-dy", scm_from_double (ldy));
895 return ly_interval2scm (pos);
899 We can't combine with previous function, since check concave and
900 slope damping comes first.
902 TODO: we should use the concaveness to control the amount of damping
905 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
907 Beam::shift_region_to_valid (SCM grob, SCM posns)
909 Grob *me = unsmob_grob (grob);
913 vector<Real> x_posns;
914 extract_grob_set (me, "stems", stems);
915 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
916 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
918 Grob *fvs = first_visible_stem (me);
923 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
924 for (vsize i = 0; i < stems.size (); i++)
928 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
929 x_posns.push_back (x);
932 Grob *lvs = last_visible_stem (me);
936 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
938 Drul_array<Real> pos = ly_scm2interval (posns);
941 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
943 Real dy = pos[RIGHT] - pos[LEFT];
945 Real slope = dx ? (dy / dx) : 0.0;
948 Shift the positions so that we have a chance of finding good
949 quants (i.e. no short stem failures.)
951 Interval feasible_left_point;
952 feasible_left_point.set_full ();
953 for (vsize i = 0; i < stems.size (); i++)
956 if (Stem::is_invisible (s))
959 Direction d = get_grob_direction (s);
962 = Stem::get_stem_info (s).shortest_y_
963 - slope * x_posns [i];
966 left_y is now relative to the stem S. We want relative to
967 ourselves, so translate:
970 += + s->relative_coordinate (commony, Y_AXIS)
971 - me->relative_coordinate (commony, Y_AXIS);
977 feasible_left_point.intersect (flp);
980 if (feasible_left_point.is_empty ())
981 warning (_ ("no viable initial configuration found: may not find good beam slope"));
982 else if (!feasible_left_point.contains (y))
984 const int REGION_SIZE = 2; // UGH UGH
985 if (isinf (feasible_left_point[DOWN]))
986 y = feasible_left_point[UP] - REGION_SIZE;
987 else if (isinf (feasible_left_point[UP]))
988 y = feasible_left_point[DOWN]+ REGION_SIZE;
990 y = feasible_left_point.center ();
993 pos = Drul_array<Real> (y, (y + dy));
994 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
996 return ly_interval2scm (pos);
999 /* This neat trick is by Werner Lemberg,
1000 damped = tanh (slope)
1001 corresponds with some tables in [Wanske] CHECKME */
1002 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
1004 Beam::slope_damping (SCM smob, SCM posns)
1006 Grob *me = unsmob_grob (smob);
1007 Drul_array<Real> pos = ly_scm2interval (posns);
1009 if (visible_stem_count (me) <= 1)
1013 SCM s = me->get_property ("damping");
1014 Real damping = scm_to_double (s);
1015 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1016 if (concaveness >= 10000)
1018 pos[LEFT] = pos[RIGHT];
1019 me->set_property ("least-squares-dy", scm_from_double (0));
1025 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1027 Real dy = pos[RIGHT] - pos[LEFT];
1029 Grob *fvs = first_visible_stem (me);
1030 Grob *lvs = last_visible_stem (me);
1032 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1034 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1035 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1037 Real slope = dy && dx ? dy / dx : 0;
1039 slope = 0.6 * tanh (slope) / (damping + concaveness);
1041 Real damped_dy = slope * dx;
1043 set_minimum_dy (me, &damped_dy);
1045 pos[LEFT] += (dy - damped_dy) / 2;
1046 pos[RIGHT] -= (dy - damped_dy) / 2;
1048 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1051 return ly_interval2scm (pos);
1055 Report slice containing the numbers that are both in (car BEAMING)
1059 where_are_the_whole_beams (SCM beaming)
1063 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1065 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1067 l.add_point (scm_to_int (scm_car (s)));
1073 /* Return the Y position of the stem-end, given the Y-left, Y-right
1074 in POS for stem S. This Y position is relative to S. */
1076 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1078 Drul_array<Real> pos, bool french)
1080 Real beam_translation = get_beam_translation (me);
1082 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1083 Real dy = pos[RIGHT] - pos[LEFT];
1085 Real stem_y_beam0 = (dy && dx
1090 Direction my_dir = get_grob_direction (s);
1091 SCM beaming = s->get_property ("beaming");
1093 Real stem_y = stem_y_beam0;
1096 Slice bm = where_are_the_whole_beams (beaming);
1097 if (!bm.is_empty ())
1098 stem_y += beam_translation * bm[-my_dir];
1102 Slice bm = Stem::beam_multiplicity (s);
1103 if (!bm.is_empty ())
1104 stem_y += bm[my_dir] * beam_translation;
1107 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1108 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1114 Hmm. At this time, beam position and slope are determined. Maybe,
1115 stem directions and length should set to relative to the chord's
1116 position of the beam. */
1117 MAKE_SCHEME_CALLBACK(Beam, set_stem_lengths, 1);
1119 Beam::set_stem_lengths (SCM smob)
1121 Grob *me = unsmob_grob (smob);
1123 /* trigger callback. */
1124 (void) me->get_property ("direction");
1126 SCM posns = me->get_property ("positions");
1128 extract_grob_set (me, "stems", stems);
1133 for (int a = 2; a--;)
1134 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1136 Drul_array<Real> pos = ly_scm2realdrul (posns);
1137 Real staff_space = Staff_symbol_referencer::staff_space (me);
1138 scale_drul (&pos, staff_space);
1142 if (scm_is_number (me->get_property ("gap-count"))
1143 && scm_to_int (me->get_property ("gap-count")))
1146 thick = get_thickness (me);
1149 Grob *fvs = first_visible_stem (me);
1150 Grob *lvs = last_visible_stem (me);
1152 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1153 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1155 for (vsize i = 0; i < stems.size (); i++)
1158 if (Stem::is_invisible (s))
1161 bool french = to_boolean (s->get_property ("french-beaming"));
1162 Real stem_y = calc_stem_y (me, s, common,
1164 pos, french && s != lvs && s!= fvs);
1167 Make the stems go up to the end of the beam. This doesn't matter
1168 for normal beams, but for tremolo beams it looks silly otherwise.
1171 stem_y += thick * 0.5 * get_grob_direction (s);
1173 Stem::set_stemend (s, 2 * stem_y / staff_space);
1180 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1182 extract_grob_set (me, "stems", stems);
1185 for (vsize i = 0; i < stems.size (); i++)
1188 Don't overwrite user settings.
1192 Grob *stem = stems[i];
1193 SCM beaming_prop = stem->get_property ("beaming");
1194 if (beaming_prop == SCM_EOL
1195 || index_get_cell (beaming_prop, d) == SCM_EOL)
1197 int b = beaming->infos_.at (i).beams_i_drul_[d];
1199 && i < stems.size () -1
1200 && Stem::is_invisible (stem))
1201 b = min (b, beaming->infos_.at (i).beams_i_drul_[-d]);
1203 Stem::set_beaming (stem, b, d);
1206 while (flip (&d) != LEFT);
1211 Beam::forced_stem_count (Grob *me)
1213 extract_grob_set (me, "stems", stems);
1216 for (vsize i = 0; i < stems.size (); i++)
1220 if (Stem::is_invisible (s))
1223 /* I can imagine counting those boundaries as a half forced stem,
1224 but let's count them full for now. */
1225 Direction defdir = to_dir (s->get_property ("default-direction"));
1227 if (abs (Stem::chord_start_y (s)) > 0.1
1229 && get_grob_direction (s) != defdir)
1236 Beam::visible_stem_count (Grob *me)
1238 extract_grob_set (me, "stems", stems);
1240 for (vsize i = stems.size (); i--;)
1242 if (!Stem::is_invisible (stems[i]))
1249 Beam::first_visible_stem (Grob *me)
1251 extract_grob_set (me, "stems", stems);
1253 for (vsize i = 0; i < stems.size (); i++)
1255 if (!Stem::is_invisible (stems[i]))
1262 Beam::last_visible_stem (Grob *me)
1264 extract_grob_set (me, "stems", stems);
1266 for (vsize i = stems.size (); i--;)
1268 if (!Stem::is_invisible (stems[i]))
1277 handle rest under beam (do_post: beams are calculated now)
1278 what about combination of collisions and rest under beam.
1282 rest -> stem -> beam -> interpolate_y_position ()
1284 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1286 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1288 Grob *rest = unsmob_grob (smob);
1289 if (scm_is_number (rest->get_property ("staff-position")))
1290 return scm_from_int (0);
1292 Real offset = robust_scm2double (prev_offset, 0.0);
1294 Grob *st = unsmob_grob (rest->get_object ("stem"));
1297 return scm_from_double (0.0);
1298 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1300 || !Beam::has_interface (beam)
1301 || !Beam::visible_stem_count (beam))
1302 return scm_from_double (0.0);
1304 Drul_array<Real> pos (0, 0);
1305 SCM s = beam->get_property ("positions");
1306 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1307 pos = ly_scm2interval (s);
1309 programming_error ("positions property should always be pair of numbers.");
1311 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1313 scale_drul (&pos, staff_space);
1315 Real dy = pos[RIGHT] - pos[LEFT];
1317 Drul_array<Grob*> visible_stems (first_visible_stem (beam),
1318 last_visible_stem (beam));
1319 extract_grob_set (beam, "stems", stems);
1321 Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1323 Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1324 Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1325 Real slope = dy && dx ? dy / dx : 0;
1327 Direction d = get_grob_direction (stem);
1328 Real stem_y = pos[LEFT]
1329 + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1331 Real beam_translation = get_beam_translation (beam);
1332 Real beam_thickness = Beam::get_thickness (beam);
1335 TODO: this is not strictly correct for 16th knee beams.
1338 = Stem::beam_multiplicity (stem).length () + 1;
1340 Real height_of_my_beams = beam_thickness / 2
1341 + (beam_count - 1) * beam_translation;
1342 Real beam_y = stem_y - d * height_of_my_beams;
1344 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1345 Interval rest_extent = rest->extent (common_y, Y_AXIS);
1346 rest_extent.translate (offset);
1348 Real rest_dim = rest_extent[d];
1349 Real minimum_distance
1350 = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1351 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1353 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1355 shift /= staff_space;
1356 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1358 /* Always move discretely by half spaces */
1359 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1361 /* Inside staff, move by whole spaces*/
1362 if ((rest_extent[d] + staff_space * shift) * d
1364 || (rest_extent[-d] + staff_space * shift) * -d
1366 shift = ceil (fabs (shift)) * sign (shift);
1368 return scm_from_double (staff_space * shift);
1372 Beam::is_knee (Grob *me)
1374 SCM k = me->get_property ("knee");
1375 if (scm_is_bool (k))
1376 return ly_scm2bool (k);
1380 extract_grob_set (me, "stems", stems);
1381 for (vsize i = stems.size (); i--;)
1383 Direction dir = get_grob_direction (stems[i]);
1392 me->set_property ("knee", ly_bool2scm (knee));
1398 Beam::get_direction_beam_count (Grob *me, Direction d)
1400 extract_grob_set (me, "stems", stems);
1403 for (vsize i = stems.size (); i--;)
1406 Should we take invisible stems into account?
1408 if (get_grob_direction (stems[i]) == d)
1409 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1415 ADD_INTERFACE (Beam,
1419 "The @code{thickness} property is the weight of beams, "
1420 "measured in staffspace. The @code{direction} "
1421 "property is not user-serviceable. Use "
1422 "the @code{direction} property of @code{Stem} instead. "
1428 "beamed-stem-shorten "
1442 "neutral-direction "
1445 "quantized-positions "