X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Ftie-formatting-problem.cc;h=dbbf1074a159d9044cbe98c54391f103d7f0c8c7;hb=5b4b0d6e9a197e8f9eb085b7c2ad78b8be3e5cfc;hp=0e29d3ca50a1f028c7c1f87270e333bf31538208;hpb=0c289671d105bea58592607cb508aef59aee882b;p=lilypond.git diff --git a/lily/tie-formatting-problem.cc b/lily/tie-formatting-problem.cc index 0e29d3ca50..dbbf1074a1 100644 --- a/lily/tie-formatting-problem.cc +++ b/lily/tie-formatting-problem.cc @@ -3,27 +3,28 @@ source file of the GNU LilyPond music typesetter - (c) 2005--2006 Han-Wen Nienhuys + (c) 2005--2008 Han-Wen Nienhuys */ #include "tie-formatting-problem.hh" +#include "axis-group-interface.hh" #include "paper-column.hh" -#include "bezier.hh" +#include "bezier.hh" #include "directional-element-interface.hh" -#include "item.hh" #include "libc-extension.hh" #include "misc.hh" #include "note-head.hh" #include "rhythmic-head.hh" -#include "spanner.hh" +#include "spanner.hh" #include "staff-symbol-referencer.hh" #include "stem.hh" #include "tie-configuration.hh" #include "tie.hh" #include "warn.hh" - +#include "pointer-group-interface.hh" +#include "output-def.hh" void Tie_formatting_problem::print_ties_configuration (Ties_configuration const *ties) @@ -49,7 +50,7 @@ Tie_formatting_problem::get_attachment (Real y, Drul_array columns) const Tuple2 key (columns[d], int (d)); Chord_outline_map::const_iterator i (chord_outlines_.find (key)); if (i == chord_outlines_.end ()) - programming_error ("Can't find chord outline"); + programming_error ("Cannot find chord outline"); else attachments[d] = i->second.height (y); } @@ -58,9 +59,11 @@ Tie_formatting_problem::get_attachment (Real y, Drul_array columns) const return attachments; } -Tie_formatting_problem::Tie_formatting_problem() +Tie_formatting_problem::Tie_formatting_problem () { x_refpoint_ = 0; + y_refpoint_ = 0; + use_horizontal_spacing_ = true; } Tie_formatting_problem::~Tie_formatting_problem () @@ -104,6 +107,9 @@ Tie_formatting_problem::set_column_chord_outline (vector bounds, Interval x = dots->extent (x_refpoint_, X_AXIS); int p = int (Staff_symbol_referencer::get_position (dots)); + /* + TODO: shouldn't this use column-rank dependent key? + */ dot_positions_.insert (p); dot_x_.unite (x); @@ -116,45 +122,92 @@ Tie_formatting_problem::set_column_chord_outline (vector bounds, Tuple2 key (column_rank, int (dir)); - if (stem - && !Stem::is_invisible (stem)) + if (stem) { - Interval x; - x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS)); - x.widen (staff_space / 20); // ugh. - Interval y; - y.add_point (Stem::stem_end_position (stem) * staff_space * .5); + if (Stem::is_normal_stem (stem)) + { + Interval x; + x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS)); + x.widen (staff_space / 20); // ugh. + Interval y; + + Real stem_end_position = 0.0; + if (Stem::is_cross_staff (stem)) + stem_end_position = get_grob_direction (stem) * infinity_f; + else + { + if (use_horizontal_spacing_ || !Stem::get_beam (stem)) + stem_end_position = Stem::stem_end_position (stem) * staff_space * .5; + else + stem_end_position = Stem::note_head_positions (stem)[get_grob_direction (stem)] + * staff_space * .5; + } + + y.add_point (stem_end_position); - Direction stemdir = get_grob_direction (stem); - y.add_point (Stem::head_positions (stem)[-stemdir] - * staff_space * .5); + Direction stemdir = get_grob_direction (stem); + y.add_point (Stem::head_positions (stem)[-stemdir] + * staff_space * .5); - boxes.push_back (Box (x, y)); + /* + add extents of stem. + */ + boxes.push_back (Box (x, y)); - stem_extents_[key].unite (Box (x,y)); + stem_extents_[key].unite (Box (x, y)); - if (dir == LEFT) + if (dir == LEFT) + { + Box flag_box = Stem::get_translated_flag (stem).extent_box (); + flag_box.translate ( Offset (x[RIGHT], X_AXIS)); + boxes.push_back (flag_box); + } + } + else { - Box flag_box = Stem::get_translated_flag (stem).extent_box (); - flag_box.translate( Offset (x[RIGHT], X_AXIS)); - boxes.push_back (flag_box); + Grob *head = Stem::support_head (stem); + + /* + In case of invisible stem, don't pass x-center of heads. + */ + Real x_center = head->extent (x_refpoint_, X_AXIS).center (); + Interval x_ext; + x_ext[-dir] = x_center; + x_ext[dir] = infinity_f * dir; + Interval y_ext; + for (vsize j = 0; j < head_boxes.size (); j++) + y_ext.unite (head_boxes[j][Y_AXIS]); + + boxes.push_back (Box (x_ext, y_ext)); } - } - else if (stem) - { - Grob *head = Stem::support_head (stem); - /* - In case of invisible stem, don't pass x-center of heads. - */ - Real x_center = head->extent (x_refpoint_, X_AXIS).center (); - Interval x_ext; - x_ext[-dir] = x_center; - Interval y_ext; - for (vsize j = 0; j < head_boxes.size (); j++) - y_ext.unite (head_boxes[j][Y_AXIS]); + extract_grob_set (stem, "note-heads", heads); + for (vsize i = 0; i < heads.size (); i ++) + { + if (find (bounds.begin (), bounds.end (), dynamic_cast (heads[i])) == bounds.end ()) + { + /* + other untied notes in the same chord. + */ + + Interval y = Staff_symbol_referencer::extent_in_staff (heads[i]); + Interval x = heads[i]->extent (x_refpoint_, X_AXIS); + boxes.push_back (Box (x, y)); + } + + Grob *acc = unsmob_grob (heads[i]->get_object ("accidental-grob")); + if (acc) + acc->get_property ("stencil"); /* trigger tie-related suicide */ + + if (acc && acc->is_live () && dir == RIGHT) + { + boxes.push_back (Box (acc->extent (x_refpoint_, X_AXIS), + Staff_symbol_referencer::extent_in_staff (acc))); + } - boxes.push_back (Box (x_ext, y_ext)); + head_positions_[column_rank].add_point (int (Staff_symbol_referencer::get_position (heads[i]))); + } + } Direction updowndir = DOWN; @@ -162,7 +215,7 @@ Tie_formatting_problem::set_column_chord_outline (vector bounds, { Interval x; Interval y; - if (head_boxes.size()) + if (head_boxes.size ()) { Box b = boundary (head_boxes, updowndir, 0); x = b[X_AXIS]; @@ -175,12 +228,16 @@ Tie_formatting_problem::set_column_chord_outline (vector bounds, boxes.push_back (Box (x, y)); } while (flip (&updowndir) != DOWN); - - chord_outlines_[key] = Skyline (boxes, Y_AXIS, -dir); + + /* todo: the horizon_padding is somewhat arbitrary */ + chord_outlines_[key] = Skyline (boxes, details_.skyline_padding_, Y_AXIS, -dir); if (bounds[0]->break_status_dir ()) { - Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-dir]; - chord_outlines_[key].set_minimum_height (x); + Interval iv (Axis_group_interface::staff_extent (bounds[0], x_refpoint_, X_AXIS, y_refpoint_, Y_AXIS)); + if (iv.is_empty ()) + iv.add_point (bounds[0]->relative_coordinate (x_refpoint_, X_AXIS)); + + chord_outlines_[key].set_minimum_height (iv[-dir]); } else { @@ -250,10 +307,20 @@ Tie_formatting_problem::from_ties (vector const &ties) return; x_refpoint_ = ties[0]; + y_refpoint_ = ties[0]; for (vsize i = 0; i < ties.size (); i++) { - x_refpoint_ = dynamic_cast (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS); - x_refpoint_ = dynamic_cast (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS); + Spanner *tie = dynamic_cast (ties[i]); + Item *l = tie->get_bound (LEFT); + Item *r = tie->get_bound (RIGHT); + + x_refpoint_ = l->common_refpoint (x_refpoint_, X_AXIS); + x_refpoint_ = r->common_refpoint (x_refpoint_, X_AXIS); + + if (!l->break_status_dir ()) + y_refpoint_ = l->common_refpoint (y_refpoint_, Y_AXIS); + if (!r->break_status_dir ()) + y_refpoint_ = r->common_refpoint (y_refpoint_, Y_AXIS); } details_.from_grob (ties[0]); @@ -266,7 +333,9 @@ Tie_formatting_problem::from_ties (vector const &ties) for (vsize i = 0; i < ties.size (); i++) { Item *it = dynamic_cast (ties[i])->get_bound (d); - + if (it->break_status_dir ()) + it = it->get_column (); + bounds.push_back (it); } @@ -278,16 +347,12 @@ Tie_formatting_problem::from_ties (vector const &ties) for (vsize i = 0; i < ties.size (); i++) { Tie_specification spec; - - spec.get_tie_manual_settings (ties[i]); - - + spec.from_grob (ties[i]); do { spec.note_head_drul_[d] = Tie::head (ties[i], d); - spec.column_ranks_[d] = - dynamic_cast (ties[i])->get_bound (d)->get_column ()->get_rank (); + spec.column_ranks_[d] = Tie::get_column_rank (ties[i], d); } while (flip (&d) != LEFT); specifications_.push_back (spec); @@ -299,7 +364,8 @@ Tie_formatting_problem::from_semi_ties (vector const &semi_ties, Directio { if (semi_ties.empty ()) return; - + + use_horizontal_spacing_ = false; details_.from_grob (semi_ties[0]); vector heads; @@ -317,20 +383,28 @@ Tie_formatting_problem::from_semi_ties (vector const &semi_ties, Directio spec.position_ = int (Staff_symbol_referencer::get_position (head)); } - spec.get_tie_manual_settings (semi_ties[i]); + spec.from_grob (semi_ties[i]); spec.note_head_drul_[head_dir] = head; - column_rank = dynamic_cast (head)->get_column ()->get_rank (); + column_rank = Tie::get_column_rank (semi_ties[i], head_dir); spec.column_ranks_ = Drul_array (column_rank, column_rank); heads.push_back (head); specifications_.push_back (spec); } - x_refpoint_ = semi_ties [0]; + x_refpoint_ = semi_ties[0]; + y_refpoint_ = semi_ties[0]; + for (vsize i = 0; i < semi_ties.size (); i++) - x_refpoint_ = semi_ties[i]->common_refpoint (x_refpoint_, X_AXIS); + { + x_refpoint_ = semi_ties[i]->common_refpoint (x_refpoint_, X_AXIS); + y_refpoint_ = semi_ties[i]->common_refpoint (y_refpoint_, Y_AXIS); + } for (vsize i = 0; i < heads.size (); i++) - x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS); + { + x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS); + y_refpoint_ = heads[i]->common_refpoint (y_refpoint_, Y_AXIS) ; + } set_chord_outline (heads, head_dir); @@ -354,7 +428,8 @@ Tie_formatting_problem::get_tie_specification (int i) const Return configuration, create it if necessary. */ Tie_configuration* -Tie_formatting_problem::get_configuration (int pos, Direction dir, Drul_array columns) const +Tie_formatting_problem::get_configuration (int pos, Direction dir, Drul_array columns, + bool tune_dy) const { int key_components[] = { pos, dir, columns[LEFT], columns[RIGHT] @@ -368,14 +443,14 @@ Tie_formatting_problem::get_configuration (int pos, Direction dir, Drul_arraypossibilities_[key] = conf; return conf; } Tie_configuration* Tie_formatting_problem::generate_configuration (int pos, Direction dir, - Drul_array columns) const + Drul_array columns, bool y_tune) const { Tie_configuration *conf = new Tie_configuration; conf->position_ = pos; @@ -385,7 +460,6 @@ Tie_formatting_problem::generate_configuration (int pos, Direction dir, Real y = conf->position_ * 0.5 * details_.staff_space_; - bool y_tune = true; if (dot_positions_.find (pos) != dot_positions_.end ()) { conf->delta_y_ += dir * 0.25 * details_.staff_space_; @@ -416,37 +490,41 @@ Tie_formatting_problem::generate_configuration (int pos, Direction dir, size. */ - if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_) + if (head_positions_slice (columns[LEFT]).contains (pos) + || head_positions_slice (columns[RIGHT]).contains (pos) + || abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_)) { - if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos) - && abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_)) - { - conf->center_tie_vertically (details_); - } - else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)) + if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_) { - conf->delta_y_ += dir * - details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_; + if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos) + && abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_)) + { + conf->center_tie_vertically (details_); + } + else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)) + { + conf->delta_y_ += dir * + details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_; + } } - } - else - { - Real top_y = y + conf->delta_y_ + conf->dir_ * h; - Real top_pos = top_y / (0.5*details_.staff_space_); - int round_pos = int (my_round (top_pos)); - - /* TODO: should use other variable? */ - Real clearance = details_.center_staff_line_clearance_; - if (fabs (top_pos - round_pos) < clearance - && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_, - round_pos)) + else { - Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_; - conf->delta_y_ = (new_y - top_y); + Real top_y = y + conf->delta_y_ + conf->dir_ * h; + Real top_pos = top_y / (0.5*details_.staff_space_); + int round_pos = int (my_round (top_pos)); + + /* TODO: should use other variable? */ + Real clearance = details_.center_staff_line_clearance_; + if (fabs (top_pos - round_pos) < clearance + && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_, + round_pos)) + { + Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_; + conf->delta_y_ = (new_y - top_y); + } } } - } - + } conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_); if (conf->height (details_) < details_.intra_space_threshold_ * 0.5 * details_.staff_space_) { @@ -532,7 +610,8 @@ Tie_formatting_problem::score_aptitude (Tie_configuration *conf, } { - Real p = details_.vertical_distance_penalty_factor_ * fabs (curve_y - tie_y); + Real relevant_dist = max (fabs (curve_y - tie_y) - 0.5, 0.0); + Real p = details_.vertical_distance_penalty_factor_ * convex_amplifier (1.0, 0.9, relevant_dist); if (ties_conf) ties_conf->add_tie_score (p, tie_idx, "vdist"); else @@ -547,22 +626,77 @@ Tie_formatting_problem::score_aptitude (Tie_configuration *conf, Interval head_x = spec.note_head_drul_[d]->extent (x_refpoint_, X_AXIS); Real dist = head_x.distance (conf->attachment_x_[d]); + /* TODO: flatten with log or sqrt. */ - Real p = details_.horizontal_distance_penalty_factor_ * dist; + Real p = details_.horizontal_distance_penalty_factor_ + * convex_amplifier (1.25, 1.0, dist); if (ties_conf) ties_conf->add_tie_score (p, tie_idx, (d == LEFT) ? "lhdist" : "rhdist"); else penalty += p; + } while (flip (&d) != LEFT); + if (ties_conf + && ties_conf->size () == 1) + { + Direction d = LEFT; + Drul_array stems (0, 0); + do + { + if (!spec.note_head_drul_[d]) + continue; + + Grob *stem = unsmob_grob (spec.note_head_drul_[d]->get_object ("stem")); + if (stem + && Stem::is_normal_stem (stem)) + stems[d] = stem; + } + while (flip (&d) != LEFT); + + bool tie_stem_dir_ok = true; + bool tie_position_dir_ok = true; + if (stems[LEFT] && !stems[RIGHT]) + tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[LEFT]); + else if (!stems[LEFT] && stems[RIGHT]) + tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[RIGHT]); + else if (stems[LEFT] && stems[RIGHT] + && get_grob_direction (stems[LEFT]) == get_grob_direction (stems[RIGHT])) + tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[LEFT]); + else if (spec.position_) + tie_position_dir_ok = conf->dir_ == sign (spec.position_); + + if (!tie_stem_dir_ok) + ties_conf->add_score (details_.same_dir_as_stem_penalty_, "tie/stem dir"); + if (!tie_position_dir_ok) + ties_conf->add_score (details_.same_dir_as_stem_penalty_, "tie/pos dir"); + } + return penalty; } + +Slice +Tie_formatting_problem::head_positions_slice (int rank) const +{ + Position_extent_map::const_iterator i (head_positions_.find (rank)); + if (i != head_positions_.end ()) + { + return (*i).second; + } + Slice empty; + return empty; +} + +/* + Score a configuration, ie. how well these ties looks without regard + to the note heads that they should connect to. + */ void Tie_formatting_problem::score_configuration (Tie_configuration *conf) const { @@ -573,9 +707,10 @@ Tie_formatting_problem::score_configuration (Tie_configuration *conf) const Real length = conf->attachment_x_.length (); + Real length_penalty + = peak_around (0.33 * details_.min_length_, details_.min_length_, length); conf->add_score (details_.min_length_penalty_factor_ - * peak_around (0.33 * details_.min_length_, details_.min_length_, length), - "minlength"); + * length_penalty, "minlength"); Real tip_pos = conf->position_ + conf->delta_y_ / 0.5 * details_.staff_space_; Real tip_y = tip_pos * details_.staff_space_ * 0.5; @@ -591,13 +726,17 @@ Tie_formatting_problem::score_configuration (Tie_configuration *conf) const conf->add_score ( details_.staff_line_collision_penalty_ * peak_around (0.1 * details_.center_staff_line_clearance_, - details_.center_staff_line_clearance_, + details_.center_staff_line_clearance_, fabs (top_pos - round_top_pos)), "line center"); } - - if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, - int (rint (tip_pos)))) + + int rounded_tip_pos = int (rint (tip_pos)); + if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, rounded_tip_pos) + && (head_positions_slice (conf->column_ranks_[LEFT]).contains (rounded_tip_pos) + || head_positions_slice (conf->column_ranks_[RIGHT]).contains (rounded_tip_pos) + || abs (rounded_tip_pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_)) + ) { conf->add_score (details_.staff_line_collision_penalty_ * peak_around (0.1 * details_.tip_staff_line_clearance_, @@ -632,96 +771,6 @@ Tie_formatting_problem::score_configuration (Tie_configuration *conf) const conf->scored_ = true; } -Tie_configuration -Tie_formatting_problem::find_optimal_tie_configuration (Tie_specification const &spec) const -{ - vector confs; - - int pos = spec.position_; - Direction dir = spec.manual_dir_; - - for (int i = 0; i < details_.single_tie_region_size_; i ++) - { - confs.push_back (generate_configuration (pos + i * dir, dir, - spec.column_ranks_)); - - if (spec.has_manual_position_) - { - confs.back ()->delta_y_ - = (spec.manual_position_ - spec.position_) - * 0.5 * details_.staff_space_; - - break; - } - } - - vector scores; - - int best_idx = -1; - Real best_score = 1e6; - for (vsize i = 0; i < confs.size (); i ++) - { - score_configuration (confs[i]); - Real score = score_aptitude (confs[i], spec, 0, 0) - + confs[i]->score (); - - if (score < best_score) - { - best_score = score; - best_idx = i; - } - } - - if (best_idx < 0) - programming_error ("No best tie configuration found."); - - Tie_configuration best - = (best_idx >= 0) ? *confs[best_idx] : *confs[0]; - - for (vsize i = 0; i < confs.size (); i++) - delete confs[i]; - - return best; -} - -Tie_specification::Tie_specification () -{ - has_manual_position_ = false; - has_manual_dir_ = false; - position_ = 0; - manual_position_ = 0; - manual_dir_ = CENTER; - note_head_drul_[LEFT] = - note_head_drul_[RIGHT] = 0; - column_ranks_[RIGHT] = - column_ranks_[LEFT] = 0; -} - - -void -Tie_specification::get_tie_manual_settings (Grob *tie) -{ - if (scm_is_number (tie->get_property_data ("direction"))) - { - manual_dir_ = to_dir (tie->get_property ("direction")); - has_manual_dir_ = true; - } - - position_ = Tie::get_position (tie); - if (scm_is_number (tie->get_property ("staff-position"))) - { - manual_position_ = scm_to_double (tie->get_property ("staff-position")); - has_manual_position_ = true; - position_ = int (my_round (manual_position_)); - } -} - -int -Tie_specification::column_span () const -{ - return column_ranks_[RIGHT] - column_ranks_[LEFT]; -} - void Tie_formatting_problem::score_ties_aptitude (Ties_configuration *ties) const { @@ -787,19 +836,22 @@ Tie_formatting_problem::score_ties_configuration (Ties_configuration *ties) cons last_center = center; } - ties->add_score (details_.outer_tie_length_symmetry_penalty_factor_ - * fabs (ties->at (0).attachment_x_.length () - ties->back ().attachment_x_.length ()), - "length symm"); + if (ties->size () > 1) + { + ties->add_score (details_.outer_tie_length_symmetry_penalty_factor_ + * fabs (ties->at (0).attachment_x_.length () - ties->back ().attachment_x_.length ()), + "length symm"); - ties->add_score (details_.outer_tie_vertical_distance_symmetry_penalty_factor_ - * fabs (fabs (specifications_[0].position_ * 0.5 * details_.staff_space_ - - (ties->at (0).position_ * 0.5 * details_.staff_space_ - + ties->at (0).delta_y_)) - - - fabs (specifications_.back ().position_ * 0.5 * details_.staff_space_ - - (ties->back ().position_ * 0.5 * details_.staff_space_ - + ties->back ().delta_y_))), - "pos symmetry"); + ties->add_score (details_.outer_tie_vertical_distance_symmetry_penalty_factor_ + * fabs (fabs (specifications_[0].position_ * 0.5 * details_.staff_space_ + - (ties->at (0).position_ * 0.5 * details_.staff_space_ + + ties->at (0).delta_y_)) + - + fabs (specifications_.back ().position_ * 0.5 * details_.staff_space_ + - (ties->back ().position_ * 0.5 * details_.staff_space_ + + ties->back ().delta_y_))), + "pos symmetry"); + } } /* @@ -813,8 +865,9 @@ Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &t for (vsize i = 0; i < ties_config.size (); i++) { Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_, - ties_config[i].column_ranks_); - if (specifications_[i].has_manual_position_) + ties_config[i].column_ranks_, + !specifications_[i].has_manual_delta_y_); + if (specifications_[i].has_manual_delta_y_) { ptr->delta_y_ = (specifications_[i].manual_position_ - ties_config[i].position_) @@ -838,8 +891,9 @@ Tie_formatting_problem::generate_base_chord_configuration () if (specifications_[i].has_manual_position_) { conf.position_ = (int) my_round (specifications_[i].manual_position_); - conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_) - * 0.5 * details_.staff_space_; + if (specifications_[i].has_manual_delta_y_) + conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_) + * 0.5 * details_.staff_space_; } else { @@ -862,7 +916,7 @@ Tie_formatting_problem::generate_base_chord_configuration () Ties_configuration Tie_formatting_problem::find_best_variation (Ties_configuration const &base, - vector vars) + vector const &vars) { Ties_configuration best = base; @@ -873,7 +927,8 @@ Tie_formatting_problem::find_best_variation (Ties_configuration const &base, for (vsize i = 0; i < vars.size (); i++) { Ties_configuration variant (base); - variant[vars[i].index_] = *vars[i].suggestion_; + for (vsize j = 0; j < vars[i].index_suggestion_pairs_.size(); j++) + variant[vars[i].index_suggestion_pairs_[j].first] = *vars[i].index_suggestion_pairs_[j].second; variant.reset_score (); score_ties (&variant); @@ -890,16 +945,24 @@ Tie_formatting_problem::find_best_variation (Ties_configuration const &base, Ties_configuration -Tie_formatting_problem::generate_optimal_chord_configuration () +Tie_formatting_problem::generate_optimal_configuration () { Ties_configuration base = generate_base_chord_configuration (); - vector vars = generate_collision_variations (base); - score_ties (&base); + + vector vars; + if (specifications_.size () > 1) + vars = generate_collision_variations (base); + else + vars = generate_single_tie_variations (base); + Ties_configuration best = find_best_variation (base, vars); - vars = generate_extremal_tie_variations (best); - best = find_best_variation (best, vars); + if (specifications_.size () > 1) + { + vars = generate_extremal_tie_variations (best); + best = find_best_variation (best, vars); + } return best; } @@ -915,7 +978,8 @@ Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration tie_configs->at (0).dir_ = Direction (sign (tie_configs->at (0).position_)); if (!tie_configs->at (0).dir_) - tie_configs->at (0).dir_ = DOWN; + tie_configs->at (0).dir_ + = (tie_configs->size() > 1) ? DOWN : details_.neutral_direction_; } if (!tie_configs->back ().dir_) @@ -947,7 +1011,7 @@ Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration } } - for (vsize i = 1; i < tie_configs->size() - 1; i++) + for (vsize i = 1; i + 1 < tie_configs->size (); i++) { Tie_configuration &conf = tie_configs->at (i); if (conf.dir_) @@ -962,33 +1026,74 @@ Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration } } -Tie_configuration_variation::Tie_configuration_variation () +vector +Tie_formatting_problem::generate_extremal_tie_variations (Ties_configuration const &ties) const { - index_ = 0; - suggestion_ = 0; + vector vars; + Direction d = DOWN; + for (int i = 1; i <= details_.multi_tie_region_size_; i++) + { + Drul_array configs (0, 0); + do + { + const Tie_configuration &config = boundary (ties, d, 0); + if (config.dir_ == d + && !boundary (specifications_, d, 0).has_manual_position_) + { + Tie_configuration_variation var; + configs[d] = get_configuration (config.position_ + d * i, d, + config.column_ranks_, + true); + var.add_suggestion((d == DOWN) ? 0 : ties.size () - 1, + configs[d]); + vars.push_back (var); + } + } + while (flip (&d) != DOWN); + if (configs[LEFT] && configs[RIGHT]) + { + Tie_configuration_variation var; + var.add_suggestion(0, configs[DOWN]); + var.add_suggestion(ties.size() - 1, configs[UP]); + vars.push_back (var); + } + } + + return vars; } vector -Tie_formatting_problem::generate_extremal_tie_variations (Ties_configuration const &ties) const +Tie_formatting_problem::generate_single_tie_variations (Ties_configuration const &ties) const { vector vars; - Direction d = DOWN; - do + + int sz = details_.single_tie_region_size_; + if (specifications_[0].has_manual_position_) + sz = 1; + for (int i = 0; i < sz; i ++) { - if (boundary (ties, d, 0).dir_ == d - && !boundary (specifications_, d, 0).has_manual_position_) - for (int i = 1; i <= details_.multi_tie_region_size_; i++) - { - Tie_configuration_variation var; - var.index_ = (d == DOWN) ? 0 : ties.size () - 1; - var.suggestion_ = get_configuration (boundary (ties, d, 0).position_ - + d * i, d, - boundary (ties, d, 0).column_ranks_); - vars.push_back (var); - } + Direction d = LEFT; + do + { + if (i == 0 + && ties[0].dir_ == d) + continue; + + int p = ties[0].position_ + i * d; + + if (!specifications_[0].has_manual_dir_ + || d == specifications_[0].manual_dir_) + { + Tie_configuration_variation var; + var.add_suggestion(0, + get_configuration (p, + d, specifications_[0].column_ranks_, + !specifications_[0].has_manual_delta_y_)); + vars.push_back (var); + } + } + while (flip (&d) != LEFT); } - while (flip (&d) != DOWN); - return vars; } @@ -1013,13 +1118,14 @@ Tie_formatting_problem::generate_collision_variations (Ties_configuration const if (!specifications_[i].has_manual_dir_) { Tie_configuration_variation var; - var.index_ = i; - var.suggestion_ = get_configuration (specifications_[i].position_ + var.add_suggestion(i, + get_configuration (specifications_[i].position_ - ties[i].dir_, - ties[i].dir_, - ties[i].column_ranks_ - ); + ties[i].column_ranks_, + !specifications_[i].has_manual_delta_y_ + )); vars.push_back (var); } @@ -1027,11 +1133,12 @@ Tie_formatting_problem::generate_collision_variations (Ties_configuration const if (!specifications_[i-1].has_manual_dir_) { Tie_configuration_variation var; - var.index_ = i-1; - var.suggestion_ = get_configuration (specifications_[i-1].position_ - - ties[i-1].dir_, - - ties[i-1].dir_, - specifications_[i-1].column_ranks_); + var.add_suggestion(i-1, + get_configuration (specifications_[i-1].position_ + - ties[i-1].dir_, + - ties[i-1].dir_, + specifications_[i-1].column_ranks_, + !specifications_[i-1].has_manual_delta_y_)); vars.push_back (var); } @@ -1040,19 +1147,23 @@ Tie_formatting_problem::generate_collision_variations (Ties_configuration const && ties[i-1].dir_ == DOWN) { Tie_configuration_variation var; - var.index_ = i-1; - var.suggestion_ = get_configuration (specifications_[i-1].position_ - 1, DOWN, - specifications_[i-1].column_ranks_); + var.add_suggestion(i-1, + get_configuration (specifications_[i-1].position_ - 1, DOWN, + specifications_[i-1].column_ranks_, + !specifications_[i-1].has_manual_delta_y_ + )); vars.push_back (var); } - if (i == ties.size() && !specifications_[i].has_manual_position_ + if (i == ties.size () && !specifications_[i].has_manual_position_ && ties[i].dir_ == UP) { Tie_configuration_variation var; - var.index_ = i; - var.suggestion_ = get_configuration (specifications_[i].position_ - + 1, UP, - specifications_[i].column_ranks_); + var.add_suggestion(i, + get_configuration (specifications_[i].position_ + + 1, UP, + specifications_[i].column_ranks_, + !specifications_[i].has_manual_delta_y_ + )); vars.push_back (var); } } @@ -1060,10 +1171,12 @@ Tie_formatting_problem::generate_collision_variations (Ties_configuration const && !specifications_[i].has_manual_position_) { Tie_configuration_variation var; - var.index_ = i; - var.suggestion_ = get_configuration (ties[i].position_ + ties[i].dir_, - ties[i].dir_, - ties[i].column_ranks_); + var.add_suggestion(i, + get_configuration (ties[i].position_ + ties[i].dir_, + ties[i].dir_, + ties[i].column_ranks_, + !specifications_[i].has_manual_delta_y_ + )); vars.push_back (var); } @@ -1092,7 +1205,9 @@ Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs) { spec.has_manual_position_ = true; spec.manual_position_ = scm_to_double (scm_car (entry)); + spec.has_manual_delta_y_ = (scm_inexact_p (scm_car (entry)) == SCM_BOOL_T); } + if (scm_is_number (scm_cdr (entry))) { spec.has_manual_dir_ = true; @@ -1103,3 +1218,20 @@ Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs) } } + +void +Tie_formatting_problem::set_debug_scoring (Ties_configuration const &base) +{ +#if DEBUG_TIE_SCORING + if (to_boolean (x_refpoint_->layout () + ->lookup_variable (ly_symbol2scm ("debug-tie-scoring")))) + { + for (vsize i = 0; i < base.size (); i++) + { + string card = base.complete_tie_card (i); + specifications_[i].tie_grob_->set_property ("quant-score", + ly_string2scm (card)); + } + } +#endif +}