X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fbeam-quanting.cc;h=f3505772f1a70940c4deb9014d400b9158e485a2;hb=90e4d7057f3857da049dfda3d130017d4719bd6b;hp=774cf1b02b89e15c35bd9d8a1eb5ca3e3fa43c73;hpb=3cfcd0df1125efe0eddc1f361f9bf24243ed12b7;p=lilypond.git diff --git a/lily/beam-quanting.cc b/lily/beam-quanting.cc index 774cf1b02b..f3505772f1 100644 --- a/lily/beam-quanting.cc +++ b/lily/beam-quanting.cc @@ -1,7 +1,7 @@ /* This file is part of LilyPond, the GNU music typesetter. - Copyright (C) 1997--2011 Han-Wen Nienhuys + Copyright (C) 1997--2015 Han-Wen Nienhuys Jan Nieuwenhuizen LilyPond is free software: you can redistribute it and/or modify @@ -33,6 +33,7 @@ using namespace std; #include "grob-array.hh" #include "item.hh" #include "international.hh" +#include "interval-minefield.hh" #include "least-squares.hh" #include "libc-extension.hh" #include "main.hh" @@ -44,6 +45,7 @@ using namespace std; #include "stencil.hh" #include "stem.hh" #include "warn.hh" +#include "string-convert.hh" Real get_detail (SCM alist, SCM sym, Real def) @@ -65,7 +67,10 @@ Beam_quant_parameters::fill (Grob *him) REGION_SIZE = get_detail (details, ly_symbol2scm ("region-size"), 2); // forbidden quants - SECONDARY_BEAM_DEMERIT = get_detail (details, ly_symbol2scm ("secondary-beam-demerit"), 10.0); + SECONDARY_BEAM_DEMERIT = get_detail (details, ly_symbol2scm ("secondary-beam-demerit"), 10.0) + // For stems that are non-standard, the forbidden beam quanting + // doesn't really work, so decrease their importance. + * exp(- 8*fabs (1.0 - robust_scm2double(him->get_property ("length-fraction"), 1.0))); STEM_LENGTH_DEMERIT_FACTOR = get_detail (details, ly_symbol2scm ("stem-length-demerit-factor"), 5); HORIZONTAL_INTER_QUANT_PENALTY = get_detail (details, ly_symbol2scm ("horizontal-inter-quant"), 500); @@ -78,7 +83,14 @@ Beam_quant_parameters::fill (Grob *him) // Collisions COLLISION_PENALTY = get_detail (details, ly_symbol2scm ("collision-penalty"), 500); - COLLISION_PADDING = get_detail (details, ly_symbol2scm ("collision-padding"), 0.5); + + /* For grace notes, beams get scaled down to 80%, but glyphs go down + to 63% (magstep -4 for accidentals). To make the padding + commensurate with glyph size for grace notes, we take the square + of the length fraction, yielding a 64% decrease. + */ + COLLISION_PADDING = get_detail (details, ly_symbol2scm ("collision-padding"), 0.5) + * sqr (robust_scm2double(him->get_property ("length-fraction"), 1.0)); STEM_COLLISION_FACTOR = get_detail (details, ly_symbol2scm ("stem-collision-factor"), 0.1); } @@ -192,10 +204,8 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y the stems. Otherwise, we want to do initial slope calculations. */ do_initial_slope_calculations_ = false; - Direction d = LEFT; - do + for (LEFT_and_RIGHT (d)) do_initial_slope_calculations_ |= isinf (unquanted_y_[d]) || isnan (unquanted_y_[d]); - while (flip (&d) != LEFT); /* Calculations are relative to a unit-scaled staff, i.e. the quants are @@ -244,10 +254,8 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y for (int a = 2; a--;) common[a] = common_refpoint_of_array (stems, beams[i], Axis (a)); - Direction d = LEFT; - do + for (LEFT_and_RIGHT (d)) common[X_AXIS] = beams[i]->get_bound (d)->common_refpoint (common[X_AXIS], X_AXIS); - while (flip (&d) != LEFT); // positions of the endpoints of this beam segment, including any overhangs const Interval x_pos = robust_scm2interval (beams[i]->get_property ("X-positions"), @@ -260,7 +268,7 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y Real my_y = beams[i]->relative_coordinate (common[Y_AXIS], Y_AXIS); - Interval beam_width (-1.0,-1.0); + Interval beam_width (-1.0, -1.0); for (vsize j = 0; j < stems.size (); j++) { Grob *s = stems[j]; @@ -282,6 +290,7 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y base_lengths_.push_back (y / staff_space_); stem_xpositions_.push_back (s->relative_coordinate (common[X_AXIS], X_AXIS) - x_pos[LEFT] + x_span_); stem_ypositions_.push_back (s->relative_coordinate (common[Y_AXIS], Y_AXIS) - my_y); + if (is_normal_.back ()) { if (beam_width[LEFT] == -1.0) @@ -296,19 +305,18 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y edge_dirs_ = Drul_array (stem_infos_[0].dir_, stem_infos_.back ().dir_); - is_xstaff_ = Align_interface::has_interface (common[Y_AXIS]); + is_xstaff_ = has_interface (common[Y_AXIS]); is_knee_ |= dirs_found[DOWN] && dirs_found[UP]; staff_radius_ = Staff_symbol_referencer::staff_radius (beams[i]); edge_beam_counts_ = Drul_array - (Stem::beam_multiplicity (stems[0]).length () + 1, - Stem::beam_multiplicity (stems.back ()).length () + 1); + (Stem::beam_multiplicity (stems[0]).length () + 1, + Stem::beam_multiplicity (stems.back ()).length () + 1); // TODO - why are we dividing by staff_space_? beam_translation_ = Beam::get_beam_translation (beams[i]) / staff_space_; - d = LEFT; - do + for (LEFT_and_RIGHT (d)) { quant_range_[d].set_full (); if (!edge_stems[d]) @@ -323,7 +331,6 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y + (edge_beam_counts_[d] - 1) * beam_translation_ + beam_thickness_ * .5); quant_range_[d][-ed] = heads[ed] + stem_offset; } - while (flip (&d) != LEFT); segments_ = Beam::get_beam_segments (beams[i]); vector_sort (segments_, beam_segment_less); @@ -336,7 +343,7 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y if (!collisions[j]->is_live ()) continue; - if (Beam::has_interface (collisions[j]) && Beam::is_cross_staff (collisions[j])) + if (has_interface (collisions[j]) && Beam::is_cross_staff (collisions[j])) continue; Box b; @@ -349,16 +356,15 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y continue; b[X_AXIS] += (x_span_ - x_pos[LEFT]); + b[Y_AXIS] -= my_y; Real width = b[X_AXIS].length (); Real width_factor = sqrt (width / staff_space_); - Direction d = LEFT; - do + for (LEFT_and_RIGHT (d)) add_collision (b[X_AXIS][d], b[Y_AXIS], width_factor); - while (flip (&d) != LEFT); - Grob *stem = unsmob_grob (collisions[j]->get_object ("stem")); - if (stem && Stem::has_interface (stem) && Stem::is_normal_stem (stem)) + Grob *stem = unsmob (collisions[j]->get_object ("stem")); + if (has_interface (stem) && Stem::is_normal_stem (stem)) { colliding_stems.insert (stem); } @@ -367,16 +373,16 @@ void Beam_scoring_problem::init_instance_variables (Grob *me, Drul_array y for (set::const_iterator it (colliding_stems.begin ()); it != colliding_stems.end (); it++) { Grob *s = *it; - Real x = (s->extent (common[X_AXIS], X_AXIS) - x_pos[LEFT] + x_span_).center (); + Real x = (robust_relative_extent (s, common[X_AXIS], X_AXIS) - x_pos[LEFT] + x_span_).center (); Direction stem_dir = get_grob_direction (*it); Interval y; y.set_full (); y[-stem_dir] = Stem::chord_start_y (*it) + (*it)->relative_coordinate (common[Y_AXIS], Y_AXIS) - - beams[i]->relative_coordinate (common[Y_AXIS], Y_AXIS); + - my_y; Real factor = parameters_.STEM_COLLISION_FACTOR; - if (!unsmob_grob (s->get_object ("beam"))) + if (!unsmob (s->get_object ("beam"))) factor = 1.0; add_collision (x, y, factor); } @@ -656,7 +662,7 @@ Beam_scoring_problem::calc_concaveness () if (scm_is_number (conc)) return scm_to_double (conc); - if (is_knee_) + if (is_knee_ || is_xstaff_) return 0.0; Direction beam_dir = CENTER; @@ -682,7 +688,7 @@ Beam_scoring_problem::calc_concaveness () close_positions.push_back ((int) rint (head_positions_[i][beam_dir])); far_positions.push_back ((int) rint (head_positions_[i][-beam_dir])); - } + } Real concaveness = 0.0; @@ -791,72 +797,38 @@ Beam_scoring_problem::shift_region_to_valid () if (collisions_[i].y_.length () < min_y_size) continue; - Direction d = LEFT; - do + for (LEFT_and_RIGHT (d)) { Real dy = slope * collisions_[i].x_; - Direction yd = DOWN; Interval disallowed; - do + for (DOWN_and_UP (yd)) { Real left_y = collisions_[i].y_[yd] - dy; disallowed[yd] = left_y; } - while (flip (&yd) != DOWN); forbidden_intervals.push_back (disallowed); } - while (flip (&d) != LEFT); } vector_sort (forbidden_intervals, Interval::left_less); - Real epsilon = 1.0e-10; Real beam_left_y = unquanted_y_[LEFT]; Interval feasible_beam_placements (beam_left_y, beam_left_y); - /* - forbidden_intervals contains a vector of intervals in which - the beam cannot start. it iterates through these intervals, - pushing feasible_beam_placements epsilon over or epsilon under a - collision. when this type of change happens, the loop is marked - as "dirty" and re-iterated. - - TODO: figure out a faster ways that this loop can happen via - a better search algorithm and/or OOP. - */ - - bool dirty = false; - do - { - dirty = false; - for (vsize i = 0; i < forbidden_intervals.size (); i++) - { - Direction d = DOWN; - do - { - if (forbidden_intervals[i][d] == d * infinity_f) - feasible_beam_placements[d] = d * infinity_f; - else if (forbidden_intervals[i].contains (feasible_beam_placements[d])) - { - feasible_beam_placements[d] = d * epsilon + forbidden_intervals[i][d]; - dirty = true; - } - } - while (flip (&d) != DOWN); - } - } - while (dirty); + Interval_minefield minefield (feasible_beam_placements, 0.0); + for (vsize i = 0; i < forbidden_intervals.size (); i++) + minefield.add_forbidden_interval (forbidden_intervals[i]); + minefield.solve (); + feasible_beam_placements = minefield.feasible_placements (); // if the beam placement falls out of the feasible region, we push it // to infinity so that it can never be a feasible candidate below - Direction d = DOWN; - do + for (DOWN_and_UP (d)) { if (!feasible_left_point.contains (feasible_beam_placements[d])) feasible_beam_placements[d] = d * infinity_f; } - while (flip (&d) != DOWN); if ((feasible_beam_placements[UP] == infinity_f && feasible_beam_placements[DOWN] == -infinity_f) && !feasible_left_point.is_empty ()) { @@ -917,8 +889,7 @@ Beam_scoring_problem::generate_quants (vector *scores) con Interval (unshifted_quants[i], unshifted_quants[j])); - Direction d = LEFT; - do + for (LEFT_and_RIGHT (d)) { if (!quant_range_[d].contains (c->y[d])) { @@ -927,7 +898,6 @@ Beam_scoring_problem::generate_quants (vector *scores) con break; } } - while (flip (&d) != LEFT); if (c) scores->push_back (c); } @@ -1123,10 +1093,8 @@ Beam_scoring_problem::score_stem_lengths (Beam_configuration *config) const } /* Divide by number of stems, to make the measure scale-free. */ - Direction d = DOWN; - do + for (DOWN_and_UP (d)) score[d] /= max (count[d], 1); - while (flip (&d) != DOWN); /* sometimes, two perfectly symmetric kneed beams will have the same score @@ -1233,27 +1201,29 @@ Beam_scoring_problem::score_forbidden_quants (Beam_configuration *config) const { Real dy = config->y.delta (); - Real extra_demerit = parameters_.SECONDARY_BEAM_DEMERIT - / max (edge_beam_counts_[LEFT], edge_beam_counts_[RIGHT]); - - Direction d = LEFT; + Real extra_demerit = + parameters_.SECONDARY_BEAM_DEMERIT + / max (edge_beam_counts_[LEFT], edge_beam_counts_[RIGHT]); + Real dem = 0.0; Real eps = parameters_.BEAM_EPS; - do + for (LEFT_and_RIGHT (d)) { for (int j = 1; j <= edge_beam_counts_[d]; j++) { Direction stem_dir = edge_dirs_[d]; /* - The 2.2 factor is to provide a little leniency for + The fudge_factor is to provide a little leniency for borderline cases. If we do 2.0, then the upper outer line will be in the gap of the (2, sit) quant, leading to a - false demerit. + false demerit. By increasing the fudge factor to 2.2, we + fix this case. */ - Real gap1 = config->y[d] - stem_dir * ((j - 1) * beam_translation_ + beam_thickness_ / 2 - line_thickness_ / 2.2); - Real gap2 = config->y[d] - stem_dir * (j * beam_translation_ - beam_thickness_ / 2 + line_thickness_ / 2.2); + Real fudge_factor = 2.2; + Real gap1 = config->y[d] - stem_dir * ((j - 1) * beam_translation_ + beam_thickness_ / 2 - line_thickness_ / fudge_factor); + Real gap2 = config->y[d] - stem_dir * (j * beam_translation_ - beam_thickness_ / 2 + line_thickness_ / fudge_factor); Interval gap; gap.add_point (gap1); @@ -1283,8 +1253,9 @@ Beam_scoring_problem::score_forbidden_quants (Beam_configuration *config) const } } } - while ((flip (&d)) != LEFT); + config->add (dem, "Fl"); + dem = 0.0; if (max (edge_beam_counts_[LEFT], edge_beam_counts_[RIGHT]) >= 2) { Real straddle = 0.0; @@ -1292,8 +1263,7 @@ Beam_scoring_problem::score_forbidden_quants (Beam_configuration *config) const Real inter = 0.5; Real hang = 1.0 - (beam_thickness_ - line_thickness_) / 2; - Direction d = LEFT; - do + for (LEFT_and_RIGHT (d)) { if (edge_beam_counts_[d] >= 2 && fabs (config->y[d] - edge_dirs_[d] * beam_translation_) < staff_radius_ + inter) @@ -1321,10 +1291,9 @@ Beam_scoring_problem::score_forbidden_quants (Beam_configuration *config) const dem += extra_demerit; } } - while (flip (&d) != LEFT); } - config->add (dem, "F"); + config->add (dem, "Fs"); } void @@ -1342,16 +1311,20 @@ Beam_scoring_problem::score_collisions (Beam_configuration *config) const Real dist = infinity_f; if (!intersection (beam_y, collision_y).is_empty ()) dist = 0.0; - else + else dist = min (beam_y.distance (collision_y[DOWN]), beam_y.distance (collision_y[UP])); + Real scale_free - = max (parameters_.COLLISION_PADDING - dist, 0.0) / - parameters_.COLLISION_PADDING; - demerits - += collisions_[i].base_penalty_ * + = max (parameters_.COLLISION_PADDING - dist, 0.0) + / parameters_.COLLISION_PADDING; + Real collision_demerit = collisions_[i].base_penalty_ * pow (scale_free, 3) * parameters_.COLLISION_PENALTY; + + if (collision_demerit > 0) { + demerits += collision_demerit; + } } config->add (demerits, "C");