X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fbeam.cc;h=f2f0c645f35fe12ba30d4d290c0794e105ce2d7a;hb=5bdd64510e31c8c400ea29f6077727d4d6489926;hp=026340b2fe9cafbcedb43c49c7190a2a37622d94;hpb=ef1e1828fcba4536d8c18ffe4c55f1b193a61edf;p=lilypond.git diff --git a/lily/beam.cc b/lily/beam.cc index 026340b2fe..f2f0c645f3 100644 --- a/lily/beam.cc +++ b/lily/beam.cc @@ -1,1505 +1,1511 @@ /* beam.cc -- implement Beam - + source file of the GNU LilyPond music typesetter - - (c) 1997--2002 Han-Wen Nienhuys + + (c) 1997--2007 Han-Wen Nienhuys Jan Nieuwenhuizen - */ /* - [TODO] + TODO: - * Fix TODO - - * Junk stem_info. - - * Remove #'direction from beam. A beam has no direction per se. - It may only set directions for stems. + - Determine auto knees based on positions if it's set by the user. - * Rewrite stem_beams. + - the code is littered with * and / staff_space calls for + #'positions. Consider moving to real-world coordinates? - * Use Number_pair i.s.o Interval to represent (yl, yr). - - */ + Problematic issue is user tweaks (user tweaks are in staff-coordinates.) + Notes: + - Stems run to the Y-center of the beam. + - beam_translation is the offset between Y centers of the beam. +*/ -#include // tanh. +#include "beam.hh" -#include "molecule.hh" +#include "beaming-pattern.hh" #include "directional-element-interface.hh" -#include "beaming.hh" -#include "beam.hh" -#include "misc.hh" +#include "main.hh" +#include "international.hh" +#include "interval-set.hh" +#include "item.hh" #include "least-squares.hh" -#include "stem.hh" -#include "paper-def.hh" #include "lookup.hh" -#include "group-interface.hh" -#include "staff-symbol-referencer.hh" -#include "item.hh" +#include "misc.hh" +#include "output-def.hh" +#include "pointer-group-interface.hh" #include "spanner.hh" +#include "staff-symbol-referencer.hh" +#include "stem.hh" #include "warn.hh" +#include "grob-array.hh" - -#define DEBUG_QUANTING 0 - - -#if DEBUG_QUANTING -#include "text-item.hh" // debug output. -#include "font-interface.hh" // debug output. +#if DEBUG_BEAM_SCORING +#include "text-interface.hh" // debug output. +#include "font-interface.hh" // debug output. #endif +#include -const int INTER_QUANT_PENALTY = 1000; -const int SECONDARY_BEAM_DEMERIT = 15; -const int STEM_LENGTH_DEMERIT_FACTOR = 5; -const int STEM_LENGTH_LIMIT_PENALTY = 500; -const int DAMPING_DIRECTIION_PENALTY = 800; -const int MUSICAL_DIRECTION_FACTOR = 400; -const int IDEAL_SLOPE_FACTOR = 10; +Beam_stem_segment::Beam_stem_segment () +{ + max_connect_ = 1000; // infinity + stem_ = 0; + width_ = 0.0; + stem_x_ = 0.0; + rank_ = 0; + stem_index_ = 0; + dir_ = CENTER; +} -static Real -shrink_extra_weight (Real x) +Beam_segment::Beam_segment () { - return fabs (x) * ((x < 0) ? 1.5 : 1.0); + vertical_count_ = 0; } void Beam::add_stem (Grob *me, Grob *s) { - Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s); - - s->add_dependency (me); + if (Stem::get_beam (s)) + { + programming_error ("Stem already has beam"); + return ; + } - assert (!Stem::beam_l (s)); - s->set_grob_property ("beam", me->self_scm ()); + Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s); + s->set_object ("beam", me->self_scm ()); + add_bound_item (dynamic_cast (me), dynamic_cast (s)); +} - add_bound_item (dynamic_cast (me), dynamic_cast (s)); +Real +Beam::get_thickness (Grob *me) +{ + return robust_scm2double (me->get_property ("thickness"), 0) + * Staff_symbol_referencer::staff_space (me); } +/* Return the translation between 2 adjoining beams. */ Real -Beam::get_interbeam (Grob *me) +Beam::get_beam_translation (Grob *me) { - SCM func = me->get_grob_property ("space-function"); - SCM s = gh_call2 (func, me->self_scm (), gh_int2scm (get_multiplicity (me))); - return gh_scm2double (s); + int beam_count = get_beam_count (me); + Real staff_space = Staff_symbol_referencer::staff_space (me); + Real line = Staff_symbol_referencer::line_thickness (me); + Real thickness = get_thickness (me); + Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0); + + Real beam_translation = beam_count < 4 + ? (2 * staff_space + line - thickness) / 2.0 + : (3 * staff_space + line - thickness) / 3.0; + + return fract * beam_translation; } -/* - Maximum multiplicity. - */ +/* Maximum beam_count. */ int -Beam::get_multiplicity (Grob *me) +Beam::get_beam_count (Grob *me) { int m = 0; - for (SCM s = me->get_grob_property ("stems"); gh_pair_p (s); s = ly_cdr (s)) - { - Grob *sc = unsmob_grob (ly_car (s)); - if (Stem::has_interface (sc)) - m = m >? Stem::beam_count (sc, LEFT) >? Stem::beam_count (sc, RIGHT); + extract_grob_set (me, "stems", stems); + for (vsize i = 0; i < stems.size (); i++) + { + Grob *stem = stems[i]; + m = max (m, (Stem::beam_multiplicity (stem).length () + 1)); } return m; } -MAKE_SCHEME_CALLBACK (Beam, space_function, 2); +MAKE_SCHEME_CALLBACK (Beam, calc_normal_stems, 1); SCM -Beam::space_function (SCM smob, SCM multiplicity) +Beam::calc_normal_stems (SCM smob) { Grob *me = unsmob_grob (smob); - Real staff_space = Staff_symbol_referencer::staff_space (me); - Real line = me->paper_l ()->get_var ("linethickness"); - Real thickness = gh_scm2double (me->get_grob_property ("thickness")) - * staff_space; - - Real interbeam = gh_scm2int (multiplicity) < 4 - ? (2*staff_space + line - thickness) / 2.0 - : (3*staff_space + line - thickness) / 3.0; + extract_grob_set (me, "stems", stems); + SCM val = Grob_array::make_array (); + Grob_array *ga = unsmob_grob_array (val); + for (vsize i = 0; i < stems.size (); i++) + if (Stem::is_normal_stem (stems[i])) + ga->add (stems[i]); - return gh_double2scm (interbeam); + return val; } - -/* After pre-processing all directions should be set. - Several post-processing routines (stem, slur, script) need stem/beam - direction. - Currenly, this means that beam has set all stem's directions. - [Alternatively, stems could set its own directions, according to - their beam, during 'final-pre-processing'.] */ -MAKE_SCHEME_CALLBACK (Beam, before_line_breaking, 1); +MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1); SCM -Beam::before_line_breaking (SCM smob) +Beam::calc_direction (SCM smob) { - Grob *me = unsmob_grob (smob); + Grob *me = unsmob_grob (smob); /* Beams with less than 2 two stems don't make much sense, but could happen when you do - - [r8 c8 r8]. - - For a beam that only has one stem, we try to do some disappearance magic: - we revert the flag, and move on to The Eternal Engraving Fields. */ - int count = visible_stem_count (me); + r8[ c8 r8] + + */ + + Direction dir = CENTER; + + int count = normal_stem_count (me); if (count < 2) { - me->warning (_ ("beam has less than two visible stems")); - - SCM stems = me->get_grob_property ("stems"); - if (scm_ilength (stems) == 1) + extract_grob_set (me, "stems", stems); + if (stems.size () == 0) { - me->warning (_ ("Beam has less than two stems. Removing beam.")); - - unsmob_grob (gh_car (stems))->remove_grob_property ("beam"); + me->warning (_ ("removing beam with no stems")); me->suicide (); return SCM_UNSPECIFIED; } - else if (scm_ilength (stems) == 0) + else { - me->suicide (); - return SCM_UNSPECIFIED; + Grob *stem = first_normal_stem (me); + + /* + ugh: stems[0] case happens for chord tremolo. + */ + dir = to_dir ((stem ? stem : stems[0])->get_property ("default-direction")); } } + if (count >= 1) { - Direction d = get_default_dir (me); - - consider_auto_knees (me, d); - set_stem_directions (me, d); - set_stem_shorten (me); + if (!dir) + dir = get_default_dir (me); + + consider_auto_knees (me); } - return SCM_EOL; + if (dir) + { + set_stem_directions (me, dir); + } + + return scm_from_int (dir); } -Direction -Beam::get_default_dir (Grob *me) -{ - Drul_array total; - total[UP] = total[DOWN] = 0; - Drul_array count; - count[UP] = count[DOWN] = 0; - Direction d = DOWN; - Link_array stems= - Pointer_group_interface__extract_grobs (me, (Item*)0, "stems"); - for (int i=0; i ? 0; - int current = sd ? (1 + d * sd)/2 : center_distance; + int best_count = 0; + int best_start = 0; + for (int i = lslice[-left_dir]; + (i - lslice[left_dir]) * left_dir <= 0; i += left_dir) + { + int count = 0; + for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s)) + { + int k = -right_dir * scm_to_int (scm_car (s)) + i; + if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F) + count++; + } - if (current) + if (count >= best_count) { - total[d] += current; - count[d] ++; + best_count = count; + best_start = i; } - } while (flip (&d) != DOWN); - - SCM func = me->get_grob_property ("dir-function"); - SCM s = gh_call2 (func, - gh_cons (gh_int2scm (count[UP]), - gh_int2scm (count[DOWN])), - gh_cons (gh_int2scm (total[UP]), - gh_int2scm (total[DOWN]))); - - if (gh_number_p (s) && gh_scm2int (s)) - return to_dir (s); - - /* If dir is not determined: get default */ - return to_dir (me->get_grob_property ("neutral-direction")); -} + } + return best_start; +} -/* Set all stems with non-forced direction to beam direction. - Urg: non-forced should become `without/with unforced' direction, - once stem gets cleaned-up. */ -void -Beam::set_stem_directions (Grob *me, Direction d) +MAKE_SCHEME_CALLBACK (Beam, calc_beaming, 1) +SCM +Beam::calc_beaming (SCM smob) { - Link_array stems - =Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems"); + Grob *me = unsmob_grob (smob); - for (int i=0; i remove_grob_property ("dir-forced"); - if (!gh_boolean_p (force) || !gh_scm2bool (force)) - Directional_element_interface::set (s, d); + Grob *this_stem = stems[i]; + SCM this_beaming = this_stem->get_property ("beaming"); + + Direction this_dir = get_grob_direction (this_stem); + if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming)) + { + int start_point = position_with_maximal_common_beams + (last_beaming, this_beaming, + last_dir ? last_dir : this_dir, + this_dir); + + Direction d = LEFT; + Slice new_slice; + do + { + new_slice.set_empty (); + SCM s = index_get_cell (this_beaming, d); + for (; scm_is_pair (s); s = scm_cdr (s)) + { + int new_beam_pos + = start_point - this_dir * scm_to_int (scm_car (s)); + + new_slice.add_point (new_beam_pos); + scm_set_car_x (s, scm_from_int (new_beam_pos)); + } + } + while (flip (&d) != LEFT); + + if (!new_slice.is_empty ()) + last_int = new_slice; + } + else + { + /* + FIXME: what's this for? + */ + SCM s = scm_cdr (this_beaming); + for (; scm_is_pair (s); s = scm_cdr (s)) + { + int np = -this_dir * scm_to_int (scm_car (s)); + scm_set_car_x (s, scm_from_int (np)); + last_int.add_point (np); + } + } + + if (scm_ilength (scm_cdr (this_beaming)) > 0) + { + last_beaming = this_beaming; + last_dir = this_dir; + } } -} -/* Simplistic auto-knees; only consider vertical gap between two - adjacent chords. + return SCM_EOL; +} - `Forced' stem directions are ignored. If you don't want auto-knees, - don't set, or unset auto-knee-gap. */ -void -Beam::consider_auto_knees (Grob *me, Direction d) +bool +operator <(Beam_stem_segment const &a, + Beam_stem_segment const &b) { - SCM scm = me->get_grob_property ("auto-knee-gap"); + return a.rank_ < b.rank_; +} - if (gh_number_p (scm)) - { - bool knee_b = false; - Real knee_y = 0; - Real staff_space = Staff_symbol_referencer::staff_space (me); - Real gap = gh_scm2double (scm) / staff_space; +typedef map > Position_stem_segments_map; +vector +Beam::get_beam_segments (Grob *me_grob, Grob **common) +{ + /* ugh, this has a side-effect that we need to ensure that + Stem #'beaming is correct */ + (void) me_grob->get_property ("beaming"); - Link_array stems= - Pointer_group_interface__extract_grobs (me, (Item*)0, "stems"); - - Grob *common = me->common_refpoint (stems[0], Y_AXIS); - for (int i=1; i < stems.size (); i++) - if (!Stem::invisible_b (stems[i])) - common = common->common_refpoint (stems[i], Y_AXIS); - - int l = 0; - for (int i=1; i < stems.size (); i++) - { - if (!Stem::invisible_b (stems[i-1])) - l = i - 1; - if (Stem::invisible_b (stems[l])) - continue; - if (Stem::invisible_b (stems[i])) - continue; - - Real left = Stem::extremal_heads (stems[l])[d] - ->relative_coordinate (common, Y_AXIS); - Real right = Stem::extremal_heads (stems[i])[-d] - ->relative_coordinate (common, Y_AXIS); + Spanner *me = dynamic_cast (me_grob); + + extract_grob_set (me, "stems", stems); + Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS); + + commonx = me->get_bound (LEFT)->common_refpoint (commonx, X_AXIS); + commonx = me->get_bound (RIGHT)->common_refpoint (commonx, X_AXIS); - Real dy = right - left; + *common = commonx; + + int gap_count = robust_scm2int (me->get_property ("gap-count"), 0); + Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0); + + Position_stem_segments_map stem_segments; + Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness")); - if (abs (dy) >= gap) + Slice ranks; + + for (vsize i = 0; i < stems.size (); i++) + { + Grob *stem = stems[i]; + Real stem_width = robust_scm2double (stem->get_property ("thickness"), 1.0) * lt; + Real stem_x = stem->relative_coordinate (commonx, X_AXIS); + SCM beaming = stem->get_property ("beaming"); + Direction d = LEFT; + do + { + for (SCM s = index_get_cell (beaming, d); + scm_is_pair (s); s = scm_cdr (s)) { - knee_y = (right + left) / 2; - knee_b = true; - break; + if (!scm_is_integer (scm_car (s))) + continue; + + int beam_rank = scm_to_int (scm_car (s)); + ranks.add_point (beam_rank); + } + + for (SCM s = index_get_cell (beaming, d); + scm_is_pair (s); s = scm_cdr (s)) + { + if (!scm_is_integer (scm_car (s))) + continue; + + int beam_rank = scm_to_int (scm_car (s)); + Beam_stem_segment seg; + seg.stem_ = stem; + seg.stem_x_ = stem_x; + seg.rank_ = 2 * i + (d+1)/2; + seg.width_ = stem_width; + seg.stem_index_ = i; + seg.dir_ = d; + seg.max_connect_ = robust_scm2int (stem->get_property ("max-beam-connect"), 1000); + + Direction stem_dir = get_grob_direction (stem); + + seg.gapped_ + = (stem_dir * beam_rank < (stem_dir * ranks[-stem_dir] + gap_count)); + stem_segments[beam_rank].push_back (seg); } } - - if (knee_b) + while (flip (&d) != LEFT); + } + + Drul_array break_overshoot + = robust_scm2drul (me->get_property ("break-overshoot"), + Drul_array (-0.5, 0.0)); + + vector segments; + for (Position_stem_segments_map::const_iterator i (stem_segments.begin ()); + i != stem_segments.end (); i++) + { + vector segs = (*i).second; + vector_sort (segs, less ()); + + Beam_segment current; + + int vertical_count = (*i).first; + for (vsize j = 0; j < segs.size (); j++) { - for (int i=0; i < stems.size (); i++) + /* + event_dir == LEFT: left edge of a beamsegment. + */ + Direction event_dir = LEFT; + do { - if (Stem::invisible_b (stems[i])) + bool on_bound = (event_dir == LEFT) ? j == 0 : + j == segs.size () - 1; + + bool inside_stem = (event_dir == LEFT) + ? segs[j].stem_index_ > 0 + : segs[j].stem_index_ + 1 < stems.size () ; + + bool event = on_bound + || abs (segs[j].rank_ - segs[j+event_dir].rank_) > 1 + || (abs (vertical_count) >= segs[j].max_connect_ + || abs (vertical_count) >= segs[j + event_dir].max_connect_); + + if (!event) continue; - Item *s = stems[i]; - Real y = Stem::extremal_heads (stems[i])[d] - ->relative_coordinate (common, Y_AXIS); - Directional_element_interface::set (s, y < knee_y ? UP : DOWN); - s->set_grob_property ("dir-forced", SCM_BOOL_T); + current.vertical_count_ = vertical_count; + current.horizontal_[event_dir] = segs[j].stem_x_; + if (segs[j].dir_ == event_dir) + { + if (on_bound + && me->get_bound (event_dir)->break_status_dir ()) + { + current.horizontal_[event_dir] + = (robust_relative_extent (me->get_bound (event_dir), commonx, X_AXIS)[RIGHT] + + event_dir * break_overshoot[event_dir]); + } + else + { + Real notehead_width = + Stem::duration_log (segs[j].stem_) == 1 + ? 1.98 + : 1.32; // URG. + + + if (inside_stem) + { + Grob *neighbor_stem = stems[segs[j].stem_index_ + event_dir]; + Real neighbor_stem_x = neighbor_stem->relative_coordinate (commonx, X_AXIS); + + notehead_width = min (notehead_width, + fabs (neighbor_stem_x - segs[j].stem_x_)/2); + } + current.horizontal_[event_dir] += event_dir * notehead_width; + } + } + else + { + current.horizontal_[event_dir] += event_dir * segs[j].width_/2; + if (segs[j].gapped_) + { + current.horizontal_[event_dir] -= event_dir * gap_length; + + if (Stem::is_invisible (segs[j].stem_)) + { + /* + Need to do this in case of whole notes. We don't want the + heads to collide with the beams. + */ + extract_grob_set (segs[j].stem_, "note-heads", heads); + + for (vsize k = 0; k < heads.size (); k ++) + current.horizontal_[event_dir] + = event_dir * min (event_dir * current.horizontal_[event_dir], + - gap_length/2 + + event_dir * heads[k]->extent (commonx, X_AXIS)[-event_dir]); + } + } + } + + if (event_dir == RIGHT) + { + segments.push_back (current); + current = Beam_segment (); + } } + while (flip (&event_dir) != LEFT); } + } + + return segments; } -/* Set stem's shorten property if unset. +MAKE_SCHEME_CALLBACK (Beam, print, 1); +SCM +Beam::print (SCM grob) +{ + Spanner *me = unsmob_spanner (grob); + Grob *commonx = 0; + vector segments = get_beam_segments (me, &commonx); - TODO: - take some y-position (chord/beam/nearest?) into account - scmify forced-fraction + Interval span; + if (normal_stem_count (me)) + { + span[LEFT] = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS); + span[RIGHT] = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS); + } + else + { + extract_grob_set (me, "stems", stems); + span[LEFT] = stems[0]->relative_coordinate (commonx, X_AXIS); + span[RIGHT] = stems.back ()->relative_coordinate (commonx, X_AXIS); + } - TODO: - - why is shorten stored in beam, and not directly in stem? + Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter")); -*/ -void -Beam::set_stem_shorten (Grob *m) -{ - Spanner*me = dynamic_cast (m); + SCM posns = me->get_property ("quantized-positions"); + Interval pos; + if (!is_number_pair (posns)) + { + programming_error ("no beam positions?"); + pos = Interval (0, 0); + } + else + pos = ly_scm2realdrul (posns); - Real forced_fraction = forced_stem_count (me) / visible_stem_count (me); + scale_drul (&pos, Staff_symbol_referencer::staff_space (me)); - int multiplicity = get_multiplicity (me); + Real dy = pos[RIGHT] - pos[LEFT]; + Real slope = (dy && span.length ()) ? dy / span.length () : 0; - SCM shorten = me->get_grob_property ("beamed-stem-shorten"); - if (shorten == SCM_EOL) - return; + Real thick = get_thickness (me); + Real beam_dy = get_beam_translation (me); - int sz = scm_ilength (shorten); + Direction feather_dir = to_dir (me->get_property ("grow-direction")); - Real staff_space = Staff_symbol_referencer::staff_space (me); - SCM shorten_elt = scm_list_ref (shorten, - gh_int2scm (multiplicity get_property ("quant-score"); + SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-scoring")); + if (to_boolean (debug) && scm_is_string (quant_score)) + { + extract_grob_set (me, "stems", stems); - if (shorten_f) - me->set_grob_property ("shorten", gh_double2scm (shorten_f)); -} + /* + This code prints the demerits for each beam. Perhaps this + should be switchable for those who want to twiddle with the + parameters. + */ + string str; + SCM properties = Font_interface::text_font_alist_chain (me); -/* Call list of y-dy-callbacks, that handle setting of - grob-properties y, dy. - - User may set grob-properties: y-position-hs and height-hs - (to be fixed) that override the calculated y and dy. - - Because y and dy cannot be calculated and quanted separately, we - always calculate both, then check for user override. */ -MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1); -SCM -Beam::after_line_breaking (SCM smob) -{ - Grob *me = unsmob_grob (smob); - - /* Copy to mutable list. */ - SCM s = ly_deep_copy (me->get_grob_property ("positions")); - me->set_grob_property ("positions", s); + Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP; - if (ly_car (s) != SCM_BOOL_F) - return SCM_UNSPECIFIED; + Stencil score = *unsmob_stencil (Text_interface::interpret_markup + (me->layout ()->self_scm (), properties, quant_score)); - // one wonders if such genericity is necessary --hwn. - SCM callbacks = me->get_grob_property ("position-callbacks"); - for (SCM i = callbacks; gh_pair_p (i); i = ly_cdr (i)) - gh_call1 (ly_car (i), smob); + if (!score.is_empty ()) + the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0); + } +#endif - set_stem_lengths (me); - return SCM_UNSPECIFIED; + the_beam.translate_axis (-me->relative_coordinate (commonx, X_AXIS), X_AXIS); + return the_beam.smobbed_copy (); } - -struct Quant_score + +Direction +Beam::get_default_dir (Grob *me) { - Real yl; - Real yr; - Real demerits; -}; - - -/* - TODO: - - - Make all demerits customisable + extract_grob_set (me, "stems", stems); - - One sensible check per demerit (what's this --hwn) + Drul_array extremes (0.0, 0.0); + for (iterof (s, stems); s != stems.end (); s++) + { + Interval positions = Stem::head_positions (*s); + Direction d = DOWN; + do + { + if (sign (positions[d]) == d) + extremes[d] = d * max (d * positions[d], d * extremes[d]); + } + while (flip (&d) != DOWN); + } - - Add demerits for quants per se, as to forbid a specific quant - entirely + Drul_array total (0, 0); + Drul_array count (0, 0); -*/ -MAKE_SCHEME_CALLBACK (Beam, quanting, 1); -SCM -Beam::quanting (SCM smob) -{ - Grob *me = unsmob_grob (smob); + bool force_dir = false; + for (vsize i = 0; i < stems.size (); i++) + { + Grob *s = stems[i]; + Direction stem_dir = CENTER; + SCM stem_dir_scm = s->get_property_data ("direction"); + if (is_direction (stem_dir_scm)) + { + stem_dir = to_dir (stem_dir_scm); + force_dir = true; + } + else + stem_dir = to_dir (s->get_property ("default-direction")); - SCM s = me->get_grob_property ("positions"); - Real yl = gh_scm2double (gh_car (s)); - Real yr = gh_scm2double (gh_cdr (s)); + if (!stem_dir) + stem_dir = to_dir (s->get_property ("neutral-direction")); - Real ss = Staff_symbol_referencer::staff_space (me); - Real thickness = gh_scm2double (me->get_grob_property ("thickness")) / ss; - Real slt = me->paper_l ()->get_var ("linethickness") / ss; + if (stem_dir) + { + count[stem_dir] ++; + total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0); + } + } - SCM sdy = me->get_grob_property ("least-squares-dy"); - Real dy_mus = gh_number_p (sdy) ? gh_scm2double (sdy) : 0.0; + if (!force_dir) + { + if (abs (extremes[UP]) > -extremes[DOWN]) + return DOWN; + else if (extremes[UP] < -extremes[DOWN]) + return UP; + } - Real straddle = 0.0; - Real sit = (thickness - slt) / 2; - Real inter = 0.5; - Real hang = 1.0 - (thickness - slt) / 2; - Real quants [] = {straddle, sit, inter, hang }; + Direction dir = CENTER; + Direction d = CENTER; + if ((d = (Direction) sign (count[UP] - count[DOWN]))) + dir = d; + else if (count[UP] + && count[DOWN] + && (d = (Direction) sign (total[UP] / count[UP] - total[DOWN]/count[DOWN]))) + dir = d; + else if ((d = (Direction) sign (total[UP] - total[DOWN]))) + dir = d; + else + dir = to_dir (me->get_property ("neutral-direction")); - int num_quants = int (sizeof (quants)/sizeof (Real)); - Array quantsl; - Array quantsr; + return dir; +} - /* - going to REGION_SIZE == 2, yields another 0.6 second with - wtk1-fugue2. +/* Set all stems with non-forced direction to beam direction. + Urg: non-forced should become `without/with unforced' direction, + once stem gets cleaned-up. */ +void +Beam::set_stem_directions (Grob *me, Direction d) +{ + extract_grob_set (me, "stems", stems); + for (vsize i = 0; i < stems.size (); i++) + { + Grob *s = stems[i]; - (result indexes between 70 and 575) ? --hwn. + SCM forcedir = s->get_property_data ("direction"); + if (!to_dir (forcedir)) + set_grob_direction (s, d); + } +} - */ +/* + Only try horizontal beams for knees. No reliable detection of + anything else is possible here, since we don't know funky-beaming + settings, or X-distances (slopes!) People that want sloped + knee-beams, should set the directions manually. - - /* - Do stem computations. These depend on YL and YR linearly, so we can - precompute for every stem 2 factors. - */ - Link_array stems= - Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems"); - Array stem_infos; - Array lbase_lengths; - Array rbase_lengths; - - Drul_array dirs_found(0,0); - for (int i= 0; i < stems.size(); i++) - { - Grob*s = stems[i]; - stem_infos.push( Stem::calc_stem_info (s)); + TODO: - Real b = calc_stem_y (me, s, Interval (1,0)); - lbase_lengths.push (b); + this routine should take into account the stemlength scoring + of a possible knee/nonknee beam. +*/ +void +Beam::consider_auto_knees (Grob *me) +{ + SCM scm = me->get_property ("auto-knee-gap"); + if (!scm_is_number (scm)) + return; - b = calc_stem_y (me, s, Interval (0,1)); - rbase_lengths.push (b); + Interval_set gaps; - dirs_found [stem_infos.top().dir_] = true; - } + gaps.set_full (); - Direction ldir = Direction (stem_infos[0].dir_); - Direction rdir = Direction (stem_infos.top ().dir_); - bool knee_b = dirs_found[LEFT] && dirs_found[RIGHT]; + extract_grob_set (me, "normal-stems", stems); - - int REGION_SIZE = 2; + Grob *common = common_refpoint_of_array (stems, me, Y_AXIS); + Real staff_space = Staff_symbol_referencer::staff_space (me); - /* - Knees are harder, lets try some more possibilities for knees. - */ - if (knee_b) - REGION_SIZE += 2 ; - - for (int i = -REGION_SIZE ; i < REGION_SIZE; i++) - for (int j = 0; j < num_quants; j++) - { - quantsl.push (i + quants[j] + int (yl)); - quantsr.push (i + quants[j] + int (yr)); - } - - Array qscores; - - for (int l =0; l < quantsl.size (); l++) - for (int r =0; r < quantsr.size (); r++) - { - Quant_score qs; - qs.yl = quantsl[l]; - qs.yr = quantsr[r]; - qs.demerits = 0.0; - - qscores.push (qs); - } + vector head_extents_array; + for (vsize i = 0; i < stems.size (); i++) + { + Grob *stem = stems[i]; + Interval head_extents = Stem::head_positions (stem); + if (!head_extents.is_empty ()) + { + head_extents[LEFT] += -1; + head_extents[RIGHT] += 1; + head_extents *= staff_space * 0.5; - /* - This is a longish function, but we don't separate this out into - neat modular separate subfunctions, as the subfunctions would be - called for many values of YL, YR. By precomputing various - parameters outside of the loop, we can save a lot of time. + /* + We could subtract beam Y position, but this routine only + sets stem directions, a constant shift does not have an + influence. + */ + head_extents += stem->pure_relative_y_coordinate (common, 0, INT_MAX); - */ - for (int i = qscores.size (); i--;) - if (qscores[i].demerits < 100) - { - qscores[i].demerits - += score_slopes_dy (me, qscores[i].yl, qscores[i].yr, - dy_mus, yr- yl); - } - - Real rad = Staff_symbol_referencer::staff_radius (me); - int multiplicity = get_multiplicity (me); - Real interbeam = multiplicity < 4 - ? (2*ss + slt - thickness) / 2.0 - : (3*ss + slt - thickness) / 3.0; - - for (int i = qscores.size (); i--;) - if (qscores[i].demerits < 100) - { - qscores[i].demerits - += score_forbidden_quants (me, qscores[i].yl, qscores[i].yr, - rad, slt, thickness, interbeam, - multiplicity, ldir, rdir); - } - - - for (int i = qscores.size (); i--;) - if (qscores[i].demerits < 100) - { - qscores[i].demerits - += score_stem_lengths (stems, stem_infos, - lbase_lengths, rbase_lengths, - knee_b, - me, qscores[i].yl, qscores[i].yr); - } - - - Real best = 1e6; - int best_idx = -1; - for (int i = qscores.size (); i--;) - { - if (qscores[i].demerits < best) - { - best = qscores [i].demerits ; - best_idx = i; + if (to_dir (stem->get_property_data ("direction"))) + { + Direction stemdir = to_dir (stem->get_property ("direction")); + head_extents[-stemdir] = -stemdir * infinity_f; + } } + head_extents_array.push_back (head_extents); + + gaps.remove_interval (head_extents); } - - me->set_grob_property ("positions", - gh_cons (gh_double2scm (qscores[best_idx].yl), - gh_double2scm (qscores[best_idx].yr)) - ); + Interval max_gap; + Real max_gap_len = 0.0; -#if DEBUG_QUANTING + for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--) + { + Interval gap = gaps.allowed_regions_[i]; - // debug quanting - me->set_grob_property ("quant-score", - gh_double2scm (qscores[best_idx].demerits)); - me->set_grob_property ("best-idx", gh_int2scm (best_idx)); -#endif + /* + the outer gaps are not knees. + */ + if (isinf (gap[LEFT]) || isinf (gap[RIGHT])) + continue; - return SCM_UNSPECIFIED; -} + if (gap.length () >= max_gap_len) + { + max_gap_len = gap.length (); + max_gap = gap; + } + } -Real -Beam::score_stem_lengths (Link_arraystems, - Array stem_infos, - Array left_factor, - Array right_factor, - bool knee, - Grob*me, - Real yl, Real yr) -{ - Real demerit_score = 0.0 ; - Real pen = STEM_LENGTH_LIMIT_PENALTY; - if (knee) - pen = sqrt(pen); + Real beam_translation = get_beam_translation (me); + Real beam_thickness = Beam::get_thickness (me); + int beam_count = Beam::get_beam_count (me); + Real height_of_beams = beam_thickness / 2 + + (beam_count - 1) * beam_translation; + Real threshold = scm_to_double (scm) + height_of_beams; - for (int i=0; i < stems.size (); i++) + if (max_gap_len > threshold) { - Grob* s = stems[i]; - if (Stem::invisible_b (s)) - continue; + int j = 0; + for (vsize i = 0; i < stems.size (); i++) + { + Grob *stem = stems[i]; + Interval head_extents = head_extents_array[j++]; - Real current_y = - yl * left_factor[i] + right_factor[i]* yr; + Direction d = (head_extents.center () < max_gap.center ()) + ? UP : DOWN; - Stem_info info = stem_infos[i]; - Direction d = info.dir_; + stem->set_property ("direction", scm_from_int (d)); - demerit_score += pen - * ( 0 >? (info.dir_ * (info.shortest_y_ - current_y))); - - demerit_score += STEM_LENGTH_DEMERIT_FACTOR - * shrink_extra_weight (d * current_y - info.dir_ * info.ideal_y_); + head_extents.intersect (max_gap); + assert (head_extents.is_empty () || head_extents.length () < 1e-6); + } } - - demerit_score *= 2.0 / stems.size (); - - return demerit_score; } -Real -Beam::score_slopes_dy (Grob *me, - Real yl, Real yr, - Real dy_mus, Real dy_damp) -{ - Real dy = yr - yl; +/* Set stem's shorten property if unset. - Real dem = 0.0; - if (sign (dy_damp) != sign (dy)) - { - dem += DAMPING_DIRECTIION_PENALTY; - } +TODO: +take some y-position (chord/beam/nearest?) into account +scmify forced-fraction - dem += MUSICAL_DIRECTION_FACTOR * (0 >? (fabs (dy) - fabs (dy_mus))); - dem += shrink_extra_weight (fabs (dy_damp) - fabs (dy))* IDEAL_SLOPE_FACTOR; +This is done in beam because the shorten has to be uniform over the +entire beam. +*/ - return dem; -} -static Real -my_modf (Real x) -{ - return x - floor (x); -} -Real -Beam::score_forbidden_quants (Grob*me, - Real yl, Real yr, - Real rad, - Real slt, - Real thickness, Real interbeam, - int multiplicity, - Direction ldir, Direction rdir) +void +set_minimum_dy (Grob *me, Real *dy) { - Real dy = yr - yl; - - Real dem = 0.0; - if (fabs (yl) < rad && fabs ( my_modf (yl) - 0.5) < 1e-3) - dem += INTER_QUANT_PENALTY; - if (fabs (yr) < rad && fabs ( my_modf (yr) - 0.5) < 1e-3) - dem += INTER_QUANT_PENALTY; - - // todo: use multiplicity of outer stems. - if (multiplicity >= 2) + if (*dy) { - - Real straddle = 0.0; + /* + If dy is smaller than the smallest quant, we + get absurd direction-sign penalties. + */ + + Real ss = Staff_symbol_referencer::staff_space (me); + Real thickness = Beam::get_thickness (me) / ss; + Real slt = Staff_symbol_referencer::line_thickness (me) / ss; Real sit = (thickness - slt) / 2; Real inter = 0.5; Real hang = 1.0 - (thickness - slt) / 2; - - if (fabs (yl - ldir * interbeam) < rad - && fabs (my_modf (yl) - inter) < 1e-3) - dem += SECONDARY_BEAM_DEMERIT; - if (fabs (yr - rdir * interbeam) < rad - && fabs (my_modf (yr) - inter) < 1e-3) - dem += SECONDARY_BEAM_DEMERIT; + *dy = sign (*dy) * max (fabs (*dy), + min (min (sit, inter), hang)); + } +} - Real eps = 1e-3; + - /* - Can't we simply compute the distance between the nearest - staffline and the secondary beam? That would get rid of the - silly case analysis here (which is probably not when we have - different beam-thicknesses.) +MAKE_SCHEME_CALLBACK (Beam, calc_stem_shorten, 1) +SCM +Beam::calc_stem_shorten (SCM smob) +{ + Grob *me = unsmob_grob (smob); + + /* + shortening looks silly for x staff beams + */ + if (is_knee (me)) + return scm_from_int (0); - --hwn - */ + Real forced_fraction = 1.0 * forced_stem_count (me) + / normal_stem_count (me); + int beam_count = get_beam_count (me); - // hmm, without Interval/Drul_array, you get ~ 4x same code... - if (fabs (yl - ldir * interbeam) < rad + inter) - { - if (ldir == UP && dy <= eps - && fabs (my_modf (yl) - sit) < eps) - dem += SECONDARY_BEAM_DEMERIT; - - if (ldir == DOWN && dy >= eps - && fabs (my_modf (yl) - hang) < eps) - dem += SECONDARY_BEAM_DEMERIT; - } + SCM shorten_list = me->get_property ("beamed-stem-shorten"); + if (shorten_list == SCM_EOL) + return scm_from_int (0); + + Real staff_space = Staff_symbol_referencer::staff_space (me); + + SCM shorten_elt + = robust_list_ref (beam_count -1, shorten_list); + Real shorten = scm_to_double (shorten_elt) * staff_space; + + shorten *= forced_fraction; - if (fabs (yr - rdir * interbeam) < rad + inter) - { - if (rdir == UP && dy >= eps - && fabs (my_modf (yr) - sit) < eps) - dem += SECONDARY_BEAM_DEMERIT; - - if (rdir == DOWN && dy <= eps - && fabs (my_modf (yr) - hang) < eps) - dem += SECONDARY_BEAM_DEMERIT; - } - - if (multiplicity >= 3) - { - if (fabs (yl - 2 * ldir * interbeam) < rad + inter) - { - if (ldir == UP && dy <= eps - && fabs (my_modf (yl) - straddle) < eps) - dem += SECONDARY_BEAM_DEMERIT; - - if (ldir == DOWN && dy >= eps - && fabs (my_modf (yl) - straddle) < eps) - dem += SECONDARY_BEAM_DEMERIT; - } - - if (fabs (yr - 2 * rdir * interbeam) < rad + inter) - { - if (rdir == UP && dy >= eps - && fabs (my_modf (yr) - straddle) < eps) - dem += SECONDARY_BEAM_DEMERIT; - - if (rdir == DOWN && dy <= eps - && fabs (my_modf (yr) - straddle) < eps) - dem += SECONDARY_BEAM_DEMERIT; - } - } - } - return dem; + if (shorten) + return scm_from_double (shorten); + + return scm_from_double (0.0); } + + +/* + Compute a first approximation to the beam slope. +*/ +MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2); +SCM +Beam::calc_least_squares_positions (SCM smob, SCM posns) +{ + (void) posns; + Grob *me = unsmob_grob (smob); + + int count = normal_stem_count (me); + Interval pos (0,0); + if (count < 1) + return ly_interval2scm (pos); + + vector x_posns; + extract_grob_set (me, "normal-stems", stems); + Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS); + Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS); + + Real my_y = me->relative_coordinate (commony, Y_AXIS); + + Grob *fvs = first_normal_stem (me); + Grob *lvs = last_normal_stem (me); -MAKE_SCHEME_CALLBACK (Beam, least_squares, 1); -SCM -Beam::least_squares (SCM smob) -{ - Grob *me = unsmob_grob (smob); + Interval ideal (Stem::get_stem_info (fvs).ideal_y_ + + fvs->relative_coordinate (commony, Y_AXIS) - my_y, + Stem::get_stem_info (lvs).ideal_y_ + + lvs->relative_coordinate (commony, Y_AXIS) - my_y); - int count = visible_stem_count (me); - Interval pos (0, 0); - - if (count <= 1) + Real x0 = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS); + for (vsize i = 0; i < stems.size (); i++) { - me->set_grob_property ("positions", ly_interval2scm (pos)); - return SCM_UNSPECIFIED; + Grob *s = stems[i]; + + Real x = s->relative_coordinate (commonx, X_AXIS) - x0; + x_posns.push_back (x); } + Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS) - x0; - Interval ideal (Stem::calc_stem_info (first_visible_stem (me)).ideal_y_, - Stem::calc_stem_info (last_visible_stem (me)).ideal_y_); - + Real y = 0; + Real slope = 0; + Real dy = 0; + Real ldy = 0.0; if (!ideal.delta ()) { - Interval chord (Stem::chord_start_y (first_visible_stem (me)), - Stem::chord_start_y (last_visible_stem (me))); + Interval chord (Stem::chord_start_y (stems[0]), + Stem::chord_start_y (stems.back ())); + /* Simple beams (2 stems) on middle line should be allowed to be + slightly sloped. - /* - TODO : use scoring for this. + However, if both stems reach middle line, + ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0. - complicated, because we take stem-info.ideal for determining - beam slopes. - - */ - /* Make simple beam on middle line have small tilt */ + For that case, we apply artificial slope */ if (!ideal[LEFT] && chord.delta () && count == 2) { - - /* - FIXME. -> UP - */ + /* FIXME. -> UP */ Direction d = (Direction) (sign (chord.delta ()) * UP); - pos[d] = gh_scm2double (me->get_grob_property ("thickness")) / 2; - // * dir; - pos[-d] = - pos[d]; + pos[d] = get_thickness (me) / 2; + pos[-d] = -pos[d]; } else - { - pos = ideal; - } + pos = ideal; + + /* + For broken beams this doesn't work well. In this case, the + slope esp. of the first part of a broken beam should predict + where the second part goes. + */ + ldy = pos[RIGHT] - pos[LEFT]; } else { - Array ideals; - - // ugh -> use commonx - Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS); - Link_array stems= - Pointer_group_interface__extract_grobs (me, (Item*)0, "stems"); - - for (int i=0; i < stems.size (); i++) + vector ideals; + for (vsize i = 0; i < stems.size (); i++) { - Item* s = stems[i]; - if (Stem::invisible_b (s)) - continue; - ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0, - Stem::calc_stem_info (s).ideal_y_)); + Grob *s = stems[i]; + ideals.push_back (Offset (x_posns[i], + Stem::get_stem_info (s).ideal_y_ + + s->relative_coordinate (commony, Y_AXIS) + - my_y)); } - Real y; - Real dydx; - minimise_least_squares (&dydx, &y, ideals); - Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0; - Real dy = dydx * dx; - me->set_grob_property ("least-squares-dy", gh_double2scm (dy)); + minimise_least_squares (&slope, &y, ideals); + + dy = slope * dx; - pos = Interval (y, (y+dy)); + set_minimum_dy (me, &dy); + + ldy = dy; + pos = Interval (y, (y + dy)); } - me->set_grob_property ("positions", ly_interval2scm (pos)); - return SCM_UNSPECIFIED; + /* + "position" is relative to the staff. + */ + scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me)); + + me->set_property ("least-squares-dy", scm_from_double (ldy)); + return ly_interval2scm (pos); } -MAKE_SCHEME_CALLBACK (Beam, check_concave, 1); +/* + We can't combine with previous function, since check concave and + slope damping comes first. + + TODO: we should use the concaveness to control the amount of damping + applied. +*/ +MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2); SCM -Beam::check_concave (SCM smob) +Beam::shift_region_to_valid (SCM grob, SCM posns) { - Grob *me = unsmob_grob (smob); - - Link_array stems = - Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems"); - - for (int i = 0; i < stems.size ();) - { - if (Stem::invisible_b (stems[i])) - stems.del (i); - else - i++; - } - - if (stems.size () < 3) - return SCM_UNSPECIFIED; + Grob *me = unsmob_grob (grob); + /* + Code dup. + */ + vector x_posns; + extract_grob_set (me, "stems", stems); + Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS); + Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS); + Grob *fvs = first_normal_stem (me); - /* Concaveness #1: If distance of an inner notehead to line between - two outer noteheads is bigger than CONCAVENESS-GAP (2.0ss), - beam is concave (Heinz Stolba). + if (!fvs) + return posns; - In the case of knees, the line connecting outer heads is often - not related to the beam slope (it may even go in the other - direction). Skip the check when the outer stems point in - different directions. --hwn - - */ - bool concaveness1 = false; - SCM gap = me->get_grob_property ("concaveness-gap"); - if (gh_number_p (gap) - && Stem::get_direction(stems.top ()) - == Stem::get_direction(stems[0])) + Real x0 = fvs->relative_coordinate (commonx, X_AXIS); + for (vsize i = 0; i < stems.size (); i++) { - Real r1 = gh_scm2double (gap); - Real dy = Stem::chord_start_y (stems.top ()) - - Stem::chord_start_y (stems[0]); + Grob *s = stems[i]; - - Real slope = dy / (stems.size () - 1); - - Real y0 = Stem::chord_start_y (stems[0]); - for (int i = 1; i < stems.size () - 1; i++) - { - Real c = (Stem::chord_start_y (stems[i]) - y0) - i * slope; - if (c > r1) - { - concaveness1 = true; - break; - } - } + Real x = s->relative_coordinate (commonx, X_AXIS) - x0; + x_posns.push_back (x); } - - /* Concaveness #2: Sum distances of inner noteheads that fall - outside the interval of the two outer noteheads. + Grob *lvs = last_normal_stem (me); + if (!lvs) + return posns; - We only do this for beams where first and last stem have the same - direction. --hwn. + Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0; + Drul_array pos = ly_scm2interval (posns); - Note that "convex" stems compensate for "concave" stems. - (is that intentional?) --hwn. + scale_drul (&pos, Staff_symbol_referencer::staff_space (me)); + + Real dy = pos[RIGHT] - pos[LEFT]; + Real y = pos[LEFT]; + Real slope = dx ? (dy / dx) : 0.0; + + /* + Shift the positions so that we have a chance of finding good + quants (i.e. no short stem failures.) */ - - Real concaveness2 = 0; - SCM thresh = me->get_grob_property ("concaveness-threshold"); - Real r2 = infinity_f; - if (!concaveness1 && gh_number_p (thresh) - && Stem::get_direction(stems.top ()) - == Stem::get_direction(stems[0])) + Interval feasible_left_point; + feasible_left_point.set_full (); + for (vsize i = 0; i < stems.size (); i++) { - r2 = gh_scm2double (thresh); + Grob *s = stems[i]; + if (Stem::is_invisible (s)) + continue; - Direction dir = Stem::get_direction(stems.top ()); - Real concave = 0; - Interval iv (Stem::chord_start_y (stems[0]), - Stem::chord_start_y (stems.top ())); - - if (iv[MAX] < iv[MIN]) - iv.swap (); - - for (int i = 1; i < stems.size () - 1; i++) - { - Real f = Stem::chord_start_y (stems[i]); - concave += ((f - iv[MAX] ) >? 0) + - ((f - iv[MIN] ) relative_coordinate (commony, Y_AXIS) + - me->relative_coordinate (commony, Y_AXIS); - --hwn. - - */ - concaveness2 /= (stems.size () - 2); + Interval flp; + flp.set_full (); + flp[-d] = left_y; + + feasible_left_point.intersect (flp); } - - /* TODO: some sort of damping iso -> plain horizontal */ - if (concaveness1 || concaveness2 > r2) + + if (feasible_left_point.is_empty ()) + warning (_ ("no viable initial configuration found: may not find good beam slope")); + else if (!feasible_left_point.contains (y)) { - Interval pos = ly_scm2interval (me->get_grob_property ("positions")); - Real r = pos.linear_combination (0); - me->set_grob_property ("positions", ly_interval2scm (Interval (r, r))); - me->set_grob_property ("least-squares-dy", gh_double2scm (0)); + const int REGION_SIZE = 2; // UGH UGH + if (isinf (feasible_left_point[DOWN])) + y = feasible_left_point[UP] - REGION_SIZE; + else if (isinf (feasible_left_point[UP])) + y = feasible_left_point[DOWN]+ REGION_SIZE; + else + y = feasible_left_point.center (); } - return SCM_UNSPECIFIED; + pos = Drul_array (y, (y + dy)); + scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me)); + + return ly_interval2scm (pos); } /* This neat trick is by Werner Lemberg, damped = tanh (slope) corresponds with some tables in [Wanske] CHECKME */ -MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1); +MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2); SCM -Beam::slope_damping (SCM smob) +Beam::slope_damping (SCM smob, SCM posns) { Grob *me = unsmob_grob (smob); + Drul_array pos = ly_scm2interval (posns); - if (visible_stem_count (me) <= 1) - return SCM_UNSPECIFIED; - - SCM s = me->get_grob_property ("damping"); - int damping = gh_scm2int (s); + if (normal_stem_count (me) <= 1) + return posns; - if (damping) + + SCM s = me->get_property ("damping"); + Real damping = scm_to_double (s); + Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0); + if (concaveness >= 10000) { - Interval pos = ly_scm2interval (me->get_grob_property ("positions")); - Real dy = pos.delta (); - - // ugh -> use commonx - Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - - first_visible_stem (me)->relative_coordinate (0, X_AXIS); - Real dydx = dy && dx ? dy/dx : 0; - dydx = 0.6 * tanh (dydx) / damping; - - Real damped_dy = dydx * dx; - pos[LEFT] += (dy - damped_dy) / 2; - pos[RIGHT] -= (dy - damped_dy) / 2; - - me->set_grob_property ("positions", ly_interval2scm (pos)); + pos[LEFT] = pos[RIGHT]; + me->set_property ("least-squares-dy", scm_from_double (0)); + damping = 0; } - return SCM_UNSPECIFIED; -} - -/* - Calculate the Y position of the stem-end, given the Y-left, Y-right - in POS, and for stem S. - */ -Real -Beam::calc_stem_y (Grob *me, Grob* s, Interval pos) -{ - int beam_multiplicity = get_multiplicity (me); - int stem_multiplicity = (Stem::duration_log (s) - 2) >? 0; - - Real thick = gh_scm2double (me->get_grob_property ("thickness")); - Real interbeam = get_interbeam (me); + + if (damping) + { + scale_drul (&pos, Staff_symbol_referencer::staff_space (me)); - // ugh -> use commonx - Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS); - Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0; - Real dy = pos.delta (); - Real stem_y = (dy && dx - ? (s->relative_coordinate (0, X_AXIS) - x0) / dx - * dy - : 0) + pos[LEFT]; + Real dy = pos[RIGHT] - pos[LEFT]; + Grob *fvs = first_normal_stem (me); + Grob *lvs = last_normal_stem (me); - Direction first_dir = Directional_element_interface::get (first_visible_stem (me)); - Direction my_dir = Directional_element_interface::get (s); + Grob *commonx = fvs->common_refpoint (lvs, X_AXIS); - if (my_dir != first_dir) - { - /* - WTF is happening here ? + Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS) + - first_normal_stem (me)->relative_coordinate (commonx, X_AXIS); - It looks as if this is some kind of fixup for multiple kneed - beams to get a piece of stem at the #. - + Real slope = dy && dx ? dy / dx : 0; - x - | - =======| - |======# - | - | - x + slope = 0.6 * tanh (slope) / (damping + concaveness); - Rules for this kind of stuff are hairy. In any event, the - current stem should look at the multiplicity of its - predecessor. + Real damped_dy = slope * dx; - --hwn. - - */ - stem_y += my_dir * (thick / 2 + (beam_multiplicity - 1) * interbeam); + set_minimum_dy (me, &damped_dy); - // huh, why not for first visible? + pos[LEFT] += (dy - damped_dy) / 2; + pos[RIGHT] -= (dy - damped_dy) / 2; - /* - What the heck is happening here?? - */ - Grob *last_visible = last_visible_stem (me); - if (last_visible) - { - if ( Staff_symbol_referencer::staff_symbol_l (s) - != Staff_symbol_referencer::staff_symbol_l (last_visible)) - stem_y += Directional_element_interface::get (me) - * (beam_multiplicity - stem_multiplicity) * interbeam; - } - else - programming_error ("No last visible stem"); + scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me)); } - return stem_y; + return ly_interval2scm (pos); } /* - Hmm. At this time, beam position and slope are determined. Maybe, - stem directions and length should set to relative to the chord's - position of the beam. */ -void -Beam::set_stem_lengths (Grob *me) + Report slice containing the numbers that are both in (car BEAMING) + and (cdr BEAMING) +*/ +Slice +where_are_the_whole_beams (SCM beaming) { - Link_array stems= - Pointer_group_interface__extract_grobs (me, (Item*)0, "stems"); - - if (stems.size () <= 1) - return; - - Grob *common = me->common_refpoint (stems[0], Y_AXIS); - for (int i=1; i < stems.size (); i++) - if (!Stem::invisible_b (stems[i])) - common = common->common_refpoint (stems[i], Y_AXIS); + Slice l; - Interval pos = ly_scm2interval (me->get_grob_property ("positions")); - Real staff_space = Staff_symbol_referencer::staff_space (me); - - /* - DOCUMENT THIS. - */ -#if 0 - Real thick = gh_scm2double (me->get_grob_property ("thickness")); - Direction dir = Directional_element_interface::get (me); - bool ps_testing = to_boolean (ly_symbol2scm ("ps-testing")); -#endif - - for (int i=0; i < stems.size (); i++) + for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s)) { - Item* s = stems[i]; - if (Stem::invisible_b (s)) - continue; - - Real stem_y = calc_stem_y (me, s, pos); + if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F) -#if 0 - // doesn't play well with dvips - if (ps_testing) - if (Stem::get_direction (s) == dir) - stem_y += Stem::get_direction (s) * thick / 2; -#endif - - /* caution: stem measures in staff-positions */ - Real id = me->relative_coordinate (common, Y_AXIS) - - stems[i]->relative_coordinate (common, Y_AXIS); - Stem::set_stemend (s, (stem_y + id) / staff_space * 2); + l.add_point (scm_to_int (scm_car (s))); } -} -void -Beam::set_beaming (Grob *me, Beaming_info_list *beaming) -{ - Link_array stems= - Pointer_group_interface__extract_grobs (me, (Grob *)0, "stems"); - - Direction d = LEFT; - for (int i=0; i < stems.size (); i++) - { - do - { - /* Don't overwrite user override (?) */ - if (Stem::beam_count (stems[i], d) == -1 - /* Don't set beaming for outside of outer stems */ - && ! (d == LEFT && i == 0) - && ! (d == RIGHT && i == stems.size () -1)) - { - int b = beaming->infos_.elem (i).beams_i_drul_[d]; - Stem::set_beaming (stems[i], b, d); - } - } - while (flip (&d) != LEFT); - } + return l; } - - -/* - beams to go with one stem. - - FIXME: clean me up: - - The beam should be constructed by one function that knows where the - X and Y points are, and only inspects the stems to obtain - multiplicity and stem directions. - - */ -Molecule -Beam::stem_beams (Grob *me, Item *here, Item *next, Item *prev, Real dydx) +/* Return the Y position of the stem-end, given the Y-left, Y-right + in POS for stem S. This Y position is relative to S. */ +Real +Beam::calc_stem_y (Grob *me, Grob *stem, Grob **common, + Real xl, Real xr, + Drul_array pos, bool french) { - // ugh -> use commonx - if ((next - && ! (next->relative_coordinate (0, X_AXIS) - > here->relative_coordinate (0, X_AXIS))) - || (prev - && ! (prev->relative_coordinate (0, X_AXIS) - < here->relative_coordinate (0, X_AXIS)))) - programming_error ("Beams are not left-to-right"); - - Real thick = gh_scm2double (me->get_grob_property ("thickness")); - Real bdy = get_interbeam (me); - - Molecule leftbeams; - Molecule rightbeams; - - Real nw_f; - if (!Stem::first_head (here)) - nw_f = 0; - else - { - int t = Stem::duration_log (here); - - SCM proc = me->get_grob_property ("flag-width-function"); - SCM result = gh_call1 (proc, gh_int2scm (t)); - nw_f = gh_scm2double (result); - } + Real beam_translation = get_beam_translation (me); + Real r = stem->relative_coordinate (common[X_AXIS], X_AXIS) - xl; + Real dy = pos[RIGHT] - pos[LEFT]; + Real dx = xr - xl; + Real stem_y_beam0 = (dy && dx + ? r / dx + * dy + : 0) + pos[LEFT]; - /* [Tremolo] beams on whole notes may not have direction set? */ - Direction dir = Directional_element_interface::get (here); + Direction my_dir = get_grob_direction (stem); + SCM beaming = stem->get_property ("beaming"); - /* half beams extending to the left. */ - if (prev) + Real stem_y = stem_y_beam0; + if (french) { - int lhalfs= lhalfs = Stem::beam_count (here, LEFT) - - Stem::beam_count (prev, RIGHT); - int lwholebeams= Stem::beam_count (here, LEFT) - relative_coordinate (0, X_AXIS) - - prev->relative_coordinate (0, X_AXIS); - Real stem_w = gh_scm2double (prev->get_grob_property ("thickness")) - // URG - * me->paper_l ()->get_var ("linethickness"); - - w = w/2 relative_coordinate (0, X_AXIS) - - here->relative_coordinate (0, X_AXIS); - - Real stem_w = gh_scm2double (next->get_grob_property ("thickness")) - // URG - * me->paper_l ()->get_var ("linethickness"); + Slice bm = Stem::beam_multiplicity (stem); + if (!bm.is_empty ()) + stem_y += bm[my_dir] * beam_translation; + } - Molecule a = Lookup::beam (dydx, w + stem_w, thick); - a.translate_axis (- stem_w/2, X_AXIS); - int j = 0; - Real gap_f = 0; - - SCM gap = me->get_grob_property ("gap"); - if (gh_number_p (gap)) - { - int gap_i = gh_scm2int ((gap)); - int nogap = rwholebeams - gap_i; - - for (; j < nogap; j++) - { - Molecule b (a); - b.translate_axis (-dir * bdy * j, Y_AXIS); - rightbeams.add_molecule (b); - } - if (Stem::invisible_b (here)) - gap_f = nw_f; - else - gap_f = nw_f / 2; - w -= 2 * gap_f; - a = Lookup::beam (dydx, w + stem_w, thick); - } + Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS) + - stem->relative_coordinate (common[Y_AXIS], Y_AXIS); - for (; j < rwholebeams; j++) - { - Molecule b (a); - Real tx = 0; - if (Stem::invisible_b (here)) - // ugh, see chord-tremolo.ly - tx = (-dir + 1) / 2 * nw_f * 1.5 + gap_f/4; - else - tx = gap_f; - b.translate (Offset (tx, -dir * bdy * j)); - rightbeams.add_molecule (b); - } + return stem_y + id; +} - w = w/2 get_property ("direction"); + (void) me->get_property ("beaming"); - } - leftbeams.add_molecule (rightbeams); + SCM posns = me->get_property ("positions"); + + extract_grob_set (me, "stems", stems); + if (!stems.size ()) + return posns; - return leftbeams; -} + Grob *common[2]; + for (int a = 2; a--;) + common[a] = common_refpoint_of_array (stems, me, Axis (a)); + Drul_array pos = ly_scm2realdrul (posns); + Real staff_space = Staff_symbol_referencer::staff_space (me); + scale_drul (&pos, staff_space); -MAKE_SCHEME_CALLBACK (Beam, brew_molecule, 1); -SCM -Beam::brew_molecule (SCM smob) -{ - Grob *me =unsmob_grob (smob); - - Molecule mol; - if (!gh_pair_p (me->get_grob_property ("stems"))) - return SCM_EOL; - Real x0, dx; - Link_arraystems = - Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems"); - if (visible_stem_count (me)) - { - // ugh -> use commonx - x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS); - dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0; - } - else + bool gap = false; + Real thick = 0.0; + if (robust_scm2int (me->get_property ("gap-count"), 0)) { - x0 = stems[0]->relative_coordinate (0, X_AXIS); - dx = stems.top ()->relative_coordinate (0, X_AXIS) - x0; + gap = true; + thick = get_thickness (me); } - SCM posns = me->get_grob_property ("positions"); - Interval pos; - if (!ly_number_pair_p (posns)) - { - programming_error ("No beam posns"); - pos = Interval (0,0); - } - else - pos= ly_scm2interval (posns); - Real dy = pos.delta (); - Real dydx = dy && dx ? dy/dx : 0; + Grob *fvs = first_normal_stem (me); + Grob *lvs = last_normal_stem (me); + Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0; + Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0; - Direction firstdir = Directional_element_interface::get ( Beam::first_visible_stem (me) ); - - for (int i=0; i < stems.size (); i++) + for (vsize i = 0; i < stems.size (); i++) { - Item *item = stems[i]; - Item *prev = (i > 0)? stems[i-1] : 0; - Item *next = (i < stems.size ()-1) ? stems[i+1] :0; + Grob *s = stems[i]; + bool french = to_boolean (s->get_property ("french-beaming")); + Real stem_y = calc_stem_y (me, s, common, + xl, xr, + pos, french && s != lvs && s!= fvs); - - Molecule sb = stem_beams (me, item, next, prev, dydx); - Real x = item->relative_coordinate (0, X_AXIS) - x0; - sb.translate (Offset (x, x * dydx + pos[LEFT])); + /* + Make the stems go up to the end of the beam. This doesn't matter + for normal beams, but for tremolo beams it looks silly otherwise. + */ + if (gap + && !Stem::is_invisible (s)) + stem_y += thick * 0.5 * get_grob_direction (s); - Direction sd = Stem::get_direction (item); - mol.add_molecule (sb); + /* + Do set_stemend for invisible stems too, so tuplet brackets + have a reference point for sloping + */ + Stem::set_stemend (s, 2 * stem_y / staff_space); } - - mol.translate_axis (x0 - - dynamic_cast (me) - ->get_bound (LEFT)->relative_coordinate (0, X_AXIS), - X_AXIS); -#if (DEBUG_QUANTING) + return posns; +} + +void +Beam::set_beaming (Grob *me, Beaming_pattern const *beaming) +{ + extract_grob_set (me, "stems", stems); + + Direction d = LEFT; + for (vsize i = 0; i < stems.size (); i++) { /* - This code prints the demerits for each beam. Perhaps this - should be switchable for those who want to twiddle with the - parameters. + Don't overwrite user settings. */ - String str; - if (1) + do { - str += to_str (gh_scm2int (me->get_grob_property ("best-idx"))); - str += ":"; + Grob *stem = stems[i]; + SCM beaming_prop = stem->get_property ("beaming"); + if (beaming_prop == SCM_EOL + || index_get_cell (beaming_prop, d) == SCM_EOL) + { + int count = beaming->beamlet_count (i, d); + if (i > 0 + && i + 1 < stems.size () + && Stem::is_invisible (stem)) + count = min (count, beaming->beamlet_count (i,-d)); + + if ( ((i == 0 && d == LEFT) + || (i == stems.size ()-1 && d == RIGHT)) + && stems.size () > 1 + && to_boolean (me->get_property ("clip-edges"))) + count = 0; + + Stem::set_beaming (stem, count, d); + } } - str += to_str (gh_scm2double (me->get_grob_property ("quant-score")), - "%.2f"); - - SCM properties = Font_interface::font_alist_chain (me); - - - Molecule tm = Text_item::text2molecule (me, ly_str02scm (str.ch_C ()), properties); - mol.add_at_edge (Y_AXIS, UP, tm, 5.0); + while (flip (&d) != LEFT); } -#endif - - return mol.smobbed_copy (); } int -Beam::forced_stem_count (Grob *me) +Beam::forced_stem_count (Grob *me) { - Link_arraystems = - Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems"); + extract_grob_set (me, "normal-stems", stems); + int f = 0; - for (int i=0; i < stems.size (); i++) + for (vsize i = 0; i < stems.size (); i++) { - Item *s = stems[i]; - - if (Stem::invisible_b (s)) - continue; + Grob *s = stems[i]; - if (((int)Stem::chord_start_y (s)) - && (Stem::get_direction (s) != Stem::get_default_dir (s))) - f++; + /* I can imagine counting those boundaries as a half forced stem, + but let's count them full for now. */ + Direction defdir = to_dir (s->get_property ("default-direction")); + + if (abs (Stem::chord_start_y (s)) > 0.1 + && defdir + && get_grob_direction (s) != defdir) + f++; } return f; } - - - int -Beam::visible_stem_count (Grob *me) +Beam::normal_stem_count (Grob *me) { - Link_arraystems = - Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems"); - int c = 0; - for (int i = stems.size (); i--;) - { - if (!Stem::invisible_b (stems[i])) - c++; - } - return c; + extract_grob_set (me, "normal-stems", stems); + return stems.size (); } -Item* -Beam::first_visible_stem (Grob *me) +Grob * +Beam::first_normal_stem (Grob *me) { - Link_arraystems = - Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems"); - - for (int i = 0; i < stems.size (); i++) - { - if (!Stem::invisible_b (stems[i])) - return stems[i]; - } - return 0; + extract_grob_set (me, "normal-stems", stems); + return stems.size () ? stems[0] : 0; } -Item* -Beam::last_visible_stem (Grob *me) +Grob * +Beam::last_normal_stem (Grob *me) { - Link_arraystems = - Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems"); - for (int i = stems.size (); i--;) - { - if (!Stem::invisible_b (stems[i])) - return stems[i]; - } - return 0; + extract_grob_set (me, "normal-stems", stems); + return stems.size () ? stems.back () : 0; } - /* [TODO] - + handle rest under beam (do_post: beams are calculated now) what about combination of collisions and rest under beam. Should lookup - - rest -> stem -> beam -> interpolate_y_position () + + rest -> stem -> beam -> interpolate_y_position () */ -MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2); +MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Beam, rest_collision_callback, 2, 1, ""); SCM -Beam::rest_collision_callback (SCM element_smob, SCM axis) +Beam::rest_collision_callback (SCM smob, SCM prev_offset) { - Grob *rest = unsmob_grob (element_smob); - Axis a = (Axis) gh_scm2int (axis); - - assert (a == Y_AXIS); + Grob *rest = unsmob_grob (smob); + if (scm_is_number (rest->get_property ("staff-position"))) + return scm_from_int (0); - Grob *st = unsmob_grob (rest->get_grob_property ("stem")); + Real offset = robust_scm2double (prev_offset, 0.0); + + Grob *st = unsmob_grob (rest->get_object ("stem")); Grob *stem = st; if (!stem) - return gh_double2scm (0.0); - Grob *beam = unsmob_grob (stem->get_grob_property ("beam")); + return scm_from_double (0.0); + Grob *beam = unsmob_grob (stem->get_object ("beam")); if (!beam || !Beam::has_interface (beam) - || !Beam::visible_stem_count (beam)) - return gh_double2scm (0.0); - - // make callback for rest from this. - // todo: make sure this calced already. - - // Interval pos = ly_scm2interval (beam->get_grob_property ("positions")); - Interval pos (0, 0); - SCM s = beam->get_grob_property ("positions"); - if (gh_pair_p (s) && gh_number_p (ly_car (s))) - pos = ly_scm2interval (s); - - Real dy = pos.delta (); - // ugh -> use commonx - Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS); - Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0; - Real dydx = dy && dx ? dy/dx : 0; - - Direction d = Stem::get_direction (stem); - Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + pos[LEFT]; + || !Beam::normal_stem_count (beam)) + return scm_from_double (0.0); + + Drul_array pos (robust_scm2drul (beam->get_property ("positions"), + Drul_array (0,0))); Real staff_space = Staff_symbol_referencer::staff_space (rest); + scale_drul (&pos, staff_space); + + Real dy = pos[RIGHT] - pos[LEFT]; + + Drul_array visible_stems (first_normal_stem (beam), + last_normal_stem (beam)); + extract_grob_set (beam, "stems", stems); + + Grob *common = common_refpoint_of_array (stems, beam, X_AXIS); - Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space; // refp?? + Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS); + Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0; + Real slope = dy && dx ? dy / dx : 0; - Real minimum_dist - = gh_scm2double (rest->get_grob_property ("minimum-beam-collision-distance")); - Real dist = - minimum_dist + -d * (beamy - rest_dim) >? 0; + Direction d = get_grob_direction (stem); + Real stem_y = pos[LEFT] + + (stem->relative_coordinate (common, X_AXIS) - x0) * slope; - int stafflines = Staff_symbol_referencer::line_count (rest); + Real beam_translation = get_beam_translation (beam); + Real beam_thickness = Beam::get_thickness (beam); - // move discretely by half spaces. - int discrete_dist = int (ceil (dist)); + /* + TODO: this is not strictly correct for 16th knee beams. + */ + int beam_count + = Stem::beam_multiplicity (stem).length () + 1; - // move by whole spaces inside the staff. - if (discrete_dist < stafflines+1) - discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0); + Real height_of_my_beams = beam_thickness / 2 + + (beam_count - 1) * beam_translation; + Real beam_y = stem_y - d * height_of_my_beams; - return gh_double2scm (-d * discrete_dist); -} + Grob *common_y = rest->common_refpoint (beam, Y_AXIS); + + /* + TODO: this is dubious, because this call needs the info we're + computing right now. + */ + Interval rest_extent = rest->extent (common_y, Y_AXIS); + rest_extent.translate (offset); + + Real rest_dim = rest_extent[d]; + Real minimum_distance + = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0) + + robust_scm2double (rest->get_property ("minimum-distance"), 0.0)); + + Real shift = d * min (d * (beam_y - d * minimum_distance - rest_dim), 0.0); + shift /= staff_space; + Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2; + /* Always move discretely by half spaces */ + shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift); + /* Inside staff, move by whole spaces*/ + if ((rest_extent[d] + staff_space * shift) * d + < rad + || (rest_extent[-d] + staff_space * shift) * -d + < rad) + shift = ceil (fabs (shift)) * sign (shift); -ADD_INTERFACE (Beam, "beam-interface", - "A beam. + return scm_from_double (offset + staff_space * shift); +} + +bool +Beam::is_knee (Grob *me) +{ + SCM k = me->get_property ("knee"); + if (scm_is_bool (k)) + return ly_scm2bool (k); + + bool knee = false; + int d = 0; + extract_grob_set (me, "stems", stems); + for (vsize i = stems.size (); i--;) + { + Direction dir = get_grob_direction (stems[i]); + if (d && d != dir) + { + knee = true; + break; + } + d = dir; + } -#'thickness= weight of beams, in staffspace + me->set_property ("knee", ly_bool2scm (knee)); + return knee; +} -We take the least squares line through the ideal-length stems, and -then damp that using +bool +Beam::is_cross_staff (Grob *me) +{ + extract_grob_set (me, "stems", stems); + Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me); + for (vsize i = 0; i < stems.size (); i++) + if (Staff_symbol_referencer::get_staff_symbol (stems[i]) != staff_symbol) + return true; + return false; +} - damped = tanh (slope) +MAKE_SCHEME_CALLBACK (Beam, calc_cross_staff, 1) +SCM +Beam::calc_cross_staff (SCM smob) +{ + return scm_from_bool (is_cross_staff (unsmob_grob (smob))); +} -this gives an unquantized left and right position for the beam end. -Then we take all combinations of quantings near these left and right -positions, and give them a score (according to how close they are to -the ideal slope, how close the result is to the ideal stems, etc.). We -take the best scoring combination. +int +Beam::get_direction_beam_count (Grob *me, Direction d) +{ + extract_grob_set (me, "stems", stems); + int bc = 0; -", - "position-callbacks concaveness-gap concaveness-threshold dir-function quant-score auto-knee-gap gap chord-tremolo beamed-stem-shorten shorten least-squares-dy damping flag-width-function neutral-direction positions space-function thickness"); + for (vsize i = stems.size (); i--;) + { + /* + Should we take invisible stems into account? + */ + if (get_grob_direction (stems[i]) == d) + bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1)); + } + return bc; +} +ADD_INTERFACE (Beam, + + "A beam. \n\n" + "The @code{thickness} property is the weight of beams, " + "measured in staffspace. The @code{direction} " + "property is not user-serviceable. Use " + "the @code{direction} property of @code{Stem} instead. " + + , + + /* properties */ + "auto-knee-gap " + "beamed-stem-shorten " + "beaming " + "break-overshoot " + "clip-edges " + "concaveness " + "damping " + "details " + "direction " + "gap " + "gap-count " + "grow-direction " + "inspect-quants " + "knee " + "length-fraction " + "least-squares-dy " + "neutral-direction " + "normal-stems " + "positions " + "quant-score " + "quantized-positions " + "shorten " + "stems " + "thickness " + );