+ if (semi_ties.empty ())
+ return;
+
+ use_horizontal_spacing_ = false;
+ details_.from_grob (semi_ties[0]);
+ vector<Item*> heads;
+
+ int column_rank = -1;
+ for (vsize i = 0; i < semi_ties.size (); i++)
+ {
+ Tie_specification spec;
+ Item *head = unsmob_item (semi_ties[i]->get_object ("note-head"));
+
+ if (!head)
+ programming_error ("LV tie without head?!");
+
+ if (head)
+ {
+ spec.position_ = int (Staff_symbol_referencer::get_position (head));
+ }
+
+ spec.from_grob (semi_ties[i]);
+
+ spec.note_head_drul_[head_dir] = head;
+ column_rank = Tie::get_column_rank (semi_ties[i], head_dir);
+ spec.column_ranks_ = Drul_array<int> (column_rank, column_rank);
+ heads.push_back (head);
+ specifications_.push_back (spec);
+ }
+
+ 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);
+ 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);
+ y_refpoint_ = heads[i]->common_refpoint (y_refpoint_, Y_AXIS) ;
+ }
+
+ set_chord_outline (heads, head_dir);
+
+ Tuple2<int> head_key (column_rank, head_dir);
+ Tuple2<int> open_key (column_rank, -head_dir);
+ Real extremal = chord_outlines_[head_key].max_height ();
+
+ chord_outlines_[open_key] = Skyline (head_dir);
+ chord_outlines_[open_key].set_minimum_height (extremal - head_dir * 1.5);
+}
+
+
+Tie_specification
+Tie_formatting_problem::get_tie_specification (int i) const
+{
+ return specifications_[i];
+}
+
+
+/*
+ Return configuration, create it if necessary.
+*/
+Tie_configuration*
+Tie_formatting_problem::get_configuration (int pos, Direction dir, Drul_array<int> columns,
+ bool tune_dy) const
+{
+ int key_components[] = {
+ pos, dir, columns[LEFT], columns[RIGHT]
+ };
+ Tuple<int,4> key (key_components);
+
+ Tie_configuration_map::const_iterator f = possibilities_.find (key);
+ if (f != possibilities_.end ())
+ {
+ return (*f).second;
+ }
+
+
+ Tie_configuration *conf = generate_configuration (pos, dir, columns, tune_dy);
+ ((Tie_formatting_problem*) this)->possibilities_[key] = conf;
+ return conf;
+}
+
+Tie_configuration*
+Tie_formatting_problem::generate_configuration (int pos, Direction dir,
+ Drul_array<int> columns, bool y_tune) const
+{
+ Tie_configuration *conf = new Tie_configuration;
+ conf->position_ = pos;
+ conf->dir_ = dir;
+
+ conf->column_ranks_ = columns;
+
+ Real y = conf->position_ * 0.5 * details_.staff_space_;
+
+ if (dot_positions_.find (pos) != dot_positions_.end ())
+ {
+ conf->delta_y_ += dir * 0.25 * details_.staff_space_;
+ y_tune = false;
+ }
+
+ if (y_tune
+ && max (fabs (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y),
+ fabs (get_head_extent (columns[RIGHT], RIGHT, Y_AXIS)[dir] - y)) < 0.25
+ && !Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
+ {
+ conf->delta_y_ =
+ (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y)
+ + dir * details_.outer_tie_vertical_gap_;
+ }
+
+ if (y_tune)
+ {
+ conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
+ Real h = conf->height (details_);
+
+ /*
+ TODO:
+
+ - should make sliding criterion, should flatten ties if
+
+ - they're just the wrong (ie. touching line at top & bottom)
+ size.
+
+ */
+ 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 (h < details_.intra_space_threshold_ * 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))
+ {
+ 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_)
+ {
+ /*
+ This is less sensible for long ties, since those are more
+ horizontal.
+ */
+ Interval close_by = get_attachment (y
+ + conf->delta_y_
+ + (dir * details_.intra_space_threshold_ * 0.25
+ * details_.staff_space_),
+ conf->column_ranks_);
+
+ conf->attachment_x_.intersect (close_by);
+ }
+
+ conf->attachment_x_.widen ( - details_.x_gap_);
+
+ if (conf->column_span_length ())
+ {
+ /*
+ avoid the stems that we attach to as well. We don't do this
+ for semities (span length = 0)
+
+ It would be better to check D against HEAD-DIRECTION if
+ applicable.
+ */
+ Direction d = LEFT;
+ do
+ {
+ Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
+ if (get_stem_extent (conf->column_ranks_[d], d, X_AXIS).is_empty ()
+ || !get_stem_extent (conf->column_ranks_[d], d, Y_AXIS).contains (y))
+ continue;
+
+ conf->attachment_x_[d] =
+ d * min (d * conf->attachment_x_[d],
+ d * (get_stem_extent (conf->column_ranks_[d], d, X_AXIS)[-d] - d * details_.stem_gap_));
+ }
+ while (flip (&d) != LEFT);
+ }
+ return conf;
+}
+
+Interval
+Tie_formatting_problem::get_head_extent (int col, Direction d, Axis a) const
+{
+ Column_extent_map::const_iterator i = head_extents_.find (Tuple2<int> (col, int (d)));
+ if (i != head_extents_.end ())
+ return (*i).second[a];
+ else
+ return Interval ();
+}
+
+Interval
+Tie_formatting_problem::get_stem_extent (int col, Direction d, Axis a) const
+{
+ Column_extent_map::const_iterator i = stem_extents_.find (Tuple2<int> (col, int (d)));
+ if (i != stem_extents_.end ())
+ return (*i).second[a];
+ else
+ return Interval ();
+}
+
+/**
+ TIE_IDX and TIES_CONF are optional.
+ */
+Real
+Tie_formatting_problem::score_aptitude (Tie_configuration *conf,
+ Tie_specification const &spec,
+ Ties_configuration *ties_conf, int tie_idx) const
+{
+ Real penalty = 0.0;
+ Real curve_y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
+ Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
+ if (sign (curve_y - tie_y) != conf->dir_)
+ {
+ Real p = details_.wrong_direction_offset_penalty_;
+ if (ties_conf)
+ ties_conf->add_tie_score (p, tie_idx, "wrong dir");
+ else
+ penalty += p;
+ }
+
+ {
+ 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
+ penalty += p;
+ }
+
+ Direction d = LEFT;
+ do
+ {
+ if (!spec.note_head_drul_[d])
+ continue;
+
+ 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_
+ * 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<Grob*> 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");
+ }
+ while (flip (&d) != LEFT);
+
+
+ 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
+{
+ if (conf->scored_)
+ {
+ return ;
+ }
+
+ Real length = conf->attachment_x_.length ();
+
+ conf->add_score (details_.min_length_penalty_factor_
+ * peak_around (0.33 * details_.min_length_, details_.min_length_, length),
+ "minlength");
+
+ Real tip_pos = conf->position_ + conf->delta_y_ / 0.5 * details_.staff_space_;
+ Real tip_y = tip_pos * details_.staff_space_ * 0.5;
+ Real height = conf->height (details_);
+
+ Real top_y = tip_y + conf->dir_ * height;
+ Real top_pos = 2 * top_y / details_.staff_space_;
+ Real round_top_pos = rint (top_pos);
+ if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
+ int (round_top_pos))
+ && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
+ {
+ conf->add_score (
+ details_.staff_line_collision_penalty_
+ * peak_around (0.1 * details_.center_staff_line_clearance_,
+ details_.center_staff_line_clearance_,
+ fabs (top_pos - round_top_pos)),
+ "line center");
+ }
+
+ 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_,
+ details_.tip_staff_line_clearance_,
+ fabs (tip_pos - rint (tip_pos))),
+ "tipline");
+ }
+
+ if (!dot_x_.is_empty ())
+ {
+ /* use left edge? */
+ Real x = dot_x_.center ();
+
+ Bezier b = conf->get_transformed_bezier (details_);
+ if (b.control_point_extent (X_AXIS).contains (x))
+ {
+ Real y = b.get_other_coordinate (X_AXIS, x);
+
+ for (set<int>::const_iterator i (dot_positions_.begin ());
+ i != dot_positions_.end (); i ++)
+ {
+ int dot_pos = (*i);
+ conf->add_score (details_.dot_collision_penalty_
+ * peak_around (.1 * details_.dot_collision_clearance_,
+ details_.dot_collision_clearance_,
+ fabs (dot_pos * details_.staff_space_ * 0.5 - y)),
+ "dot collision");
+ }
+ }
+ }
+
+ conf->scored_ = true;
+}
+
+void
+Tie_formatting_problem::score_ties_aptitude (Ties_configuration *ties) const
+{
+ if (ties->size () != specifications_.size ())
+ {
+ programming_error ("Huh? Mismatch between sizes.");
+ return;
+ }
+
+ for (vsize i = 0; i < ties->size (); i++)
+ score_aptitude (&ties->at (i), specifications_[i],
+ ties, i);
+}
+
+void
+Tie_formatting_problem::score_ties (Ties_configuration *ties) const
+{
+ if (ties->scored_)
+ return;
+
+ score_ties_configuration (ties);
+ score_ties_aptitude (ties);
+ ties->scored_ = true;
+}
+
+void
+Tie_formatting_problem::score_ties_configuration (Ties_configuration *ties) const
+{
+ for (vsize i = 0; i < ties->size (); i++)
+ {
+ score_configuration (&ties->at (i));
+ ties->add_tie_score (ties->at (i).score (), i, "conf");
+ }
+
+ Real last_edge = 0.0;
+ Real last_center = 0.0;
+ for (vsize i = 0; i < ties->size (); i++)
+ {
+ Bezier b (ties->at (i).get_transformed_bezier (details_));
+
+ Real center = b.curve_point (0.5)[Y_AXIS];
+ Real edge = b.curve_point (0.0)[Y_AXIS];
+
+ if (i)
+ {
+ if (edge <= last_edge)
+ ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton edge");
+ if (center <= last_center)
+ ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton cent");
+
+ ties->add_score (details_.tie_tie_collision_penalty_ *
+ peak_around (0.1 * details_.tie_tie_collision_distance_,
+ details_.tie_tie_collision_distance_,
+ fabs (center - last_center)),
+ "tietie center");
+ ties->add_score (details_.tie_tie_collision_penalty_ *
+ peak_around (0.1 * details_.tie_tie_collision_distance_,
+ details_.tie_tie_collision_distance_,
+ fabs (edge - last_edge)), "tietie edge");
+ }
+
+ last_edge = edge;
+ last_center = center;
+ }
+
+ 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");
+ }
+}
+/*
+ Generate with correct X-attachments and beziers, copying delta_y_
+ from TIES_CONFIG if necessary.
+*/
+Ties_configuration
+Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
+{
+ Ties_configuration copy;
+ 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_,
+ !specifications_[i].has_manual_delta_y_);
+ if (specifications_[i].has_manual_delta_y_)
+ {
+ ptr->delta_y_
+ = (specifications_[i].manual_position_ - ties_config[i].position_)
+ * 0.5 * details_.staff_space_;
+ }
+ copy.push_back (*ptr);
+ }
+
+ return copy;
+}
+
+Ties_configuration
+Tie_formatting_problem::generate_base_chord_configuration ()
+{
+ Ties_configuration ties_config;
+ for (vsize i = 0; i < specifications_.size (); i ++)
+ {
+ Tie_configuration conf;
+ if (specifications_[i].has_manual_dir_)
+ conf.dir_ = specifications_[i].manual_dir_;
+ if (specifications_[i].has_manual_position_)
+ {
+ conf.position_ = (int) my_round (specifications_[i].manual_position_);
+ if (specifications_[i].has_manual_delta_y_)
+ conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
+ * 0.5 * details_.staff_space_;
+ }
+ else
+ {
+ conf.position_ = specifications_[i].position_;
+ }
+
+ conf.column_ranks_ = specifications_[i].column_ranks_;
+ ties_config.push_back (conf);
+ }
+
+ set_ties_config_standard_directions (&ties_config);
+ for (vsize i = 0; i < ties_config.size (); i++)
+ if (!specifications_[i].manual_position_)
+ ties_config[i].position_ += ties_config[i].dir_;
+
+ ties_config = generate_ties_configuration (ties_config);
+
+ return ties_config;
+}
+
+Ties_configuration
+Tie_formatting_problem::find_best_variation (Ties_configuration const &base,
+ vector<Tie_configuration_variation> const &vars)
+{
+ Ties_configuration best = base;
+
+ /*
+ This simply is 1-opt: we have K substitions, and we try applying
+ exactly every one for each.
+ */
+ for (vsize i = 0; i < vars.size (); i++)
+ {
+ Ties_configuration variant (base);
+ variant[vars[i].index_] = *vars[i].suggestion_;
+
+ variant.reset_score ();
+ score_ties (&variant);
+
+ if (variant.score () < best.score ())
+ {
+ best = variant;
+ }
+ }
+
+ return best;
+}
+
+
+
+Ties_configuration
+Tie_formatting_problem::generate_optimal_configuration ()
+{
+ Ties_configuration base = generate_base_chord_configuration ();
+ score_ties (&base);
+
+ vector<Tie_configuration_variation> 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);
+
+ if (specifications_.size () > 1)
+ {
+ vars = generate_extremal_tie_variations (best);
+ best = find_best_variation (best, vars);
+ }
+ return best;
+}
+
+void
+Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
+{
+ if (tie_configs->empty ())