2 tie-formatting-problem.cc -- implement Tie_formatting_problem
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "tie-formatting-problem.hh"
12 #include "paper-column.hh"
14 #include "directional-element-interface.hh"
15 #include "libc-extension.hh"
17 #include "note-head.hh"
18 #include "rhythmic-head.hh"
20 #include "staff-symbol-referencer.hh"
22 #include "tie-configuration.hh"
25 #include "pointer-group-interface.hh"
26 #include "output-def.hh"
29 Tie_formatting_problem::print_ties_configuration (Ties_configuration const *ties)
31 for (vsize i = 0; i < ties->size (); i++)
33 char const *man_pos = (specifications_[i].has_manual_position_) ? "(M)" : "";
34 char const *man_dir = (specifications_[i].has_manual_dir_) ? "(M)" : "";
35 char const *dir = (ties->at (i).dir_ == UP) ? "up" : "dn";
37 printf ("(P%d%s, %s%s) ", ties->at (i).position_, man_pos, dir, man_dir);
43 Tie_formatting_problem::get_attachment (Real y, Drul_array<int> columns) const
45 Interval attachments (0,0);
49 Tuple2<int> key (columns[d], int (d));
50 Chord_outline_map::const_iterator i (chord_outlines_.find (key));
51 if (i == chord_outlines_.end ())
52 programming_error ("Cannot find chord outline");
54 attachments[d] = i->second.height (y);
56 while (flip (&d) != LEFT);
61 Tie_formatting_problem::Tie_formatting_problem ()
66 Tie_formatting_problem::~Tie_formatting_problem ()
68 for (Tie_configuration_map::const_iterator i (possibilities_.begin ());
69 i != possibilities_.end (); i++)
74 Tie_formatting_problem::set_column_chord_outline (vector<Item*> bounds,
78 Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
81 vector<Box> head_boxes;
84 for (vsize i = 0; i < bounds.size (); i++)
86 Grob *head = bounds[i];
87 if (!Note_head::has_interface (head))
91 stem = unsmob_grob (head->get_object ("stem"));
93 Real p = Staff_symbol_referencer::get_position (head);
94 Interval y ((p-1) * 0.5 * staff_space,
95 (p+1) * 0.5 * staff_space);
97 Interval x = head->extent (x_refpoint_, X_AXIS);
98 head_boxes.push_back (Box (x, y));
99 boxes.push_back (Box (x, y));
101 Grob *dots = Rhythmic_head::get_dots (head);
102 if (dir == LEFT && dots)
104 Interval x = dots->extent (x_refpoint_, X_AXIS);
105 int p = int (Staff_symbol_referencer::get_position (dots));
108 TODO: shouldn't this use column-rank dependent key?
110 dot_positions_.insert (p);
113 Interval y (dots->extent (dots, Y_AXIS));
114 y.translate (p * staff_space * 0.5);
116 boxes.push_back (Box (x, y));
120 Tuple2<int> key (column_rank, int (dir));
124 if (Stem::is_normal_stem (stem))
128 x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS));
129 x.widen (staff_space / 20); // ugh.
131 y.add_point (Stem::stem_end_position (stem) * staff_space * .5);
133 Direction stemdir = get_grob_direction (stem);
134 y.add_point (Stem::head_positions (stem)[-stemdir]
140 boxes.push_back (Box (x, y));
142 stem_extents_[key].unite (Box (x, y));
146 Box flag_box = Stem::get_translated_flag (stem).extent_box ();
147 flag_box.translate ( Offset (x[RIGHT], X_AXIS));
148 boxes.push_back (flag_box);
153 Grob *head = Stem::support_head (stem);
156 In case of invisible stem, don't pass x-center of heads.
158 Real x_center = head->extent (x_refpoint_, X_AXIS).center ();
160 x_ext[-dir] = x_center;
162 for (vsize j = 0; j < head_boxes.size (); j++)
163 y_ext.unite (head_boxes[j][Y_AXIS]);
165 boxes.push_back (Box (x_ext, y_ext));
168 extract_grob_set (stem, "note-heads", heads);
169 for (vsize i = 0; i < heads.size (); i ++)
171 if (find (bounds.begin (), bounds.end (), dynamic_cast<Item*> (heads[i])) == bounds.end ())
174 other untied notes in the same chord.
177 Interval y = Staff_symbol_referencer::extent_in_staff (heads[i]);
178 Interval x = heads[i]->extent (x_refpoint_, X_AXIS);
179 boxes.push_back (Box (x, y));
182 Grob *acc = unsmob_grob (heads[i]->get_object ("accidental-grob"));
183 if (acc && acc->is_live () && dir == RIGHT)
185 boxes.push_back (Box (acc->extent (x_refpoint_, X_AXIS),
186 Staff_symbol_referencer::extent_in_staff (acc)));
189 head_positions_[column_rank].add_point (int (Staff_symbol_referencer::get_position (heads[i])));
194 Direction updowndir = DOWN;
199 if (head_boxes.size ())
201 Box b = boundary (head_boxes, updowndir, 0);
203 x[-dir] = b[X_AXIS].linear_combination (-dir / 2);
204 y[-updowndir] = b[Y_AXIS][updowndir];
205 y[updowndir] = updowndir * infinity_f;
209 boxes.push_back (Box (x, y));
211 while (flip (&updowndir) != DOWN);
213 /* todo: the horizon_padding is somewhat arbitrary */
214 chord_outlines_[key] = Skyline (boxes, details_.skyline_padding_, Y_AXIS, -dir);
215 if (bounds[0]->break_status_dir ())
217 Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-dir];
219 chord_outlines_[key].set_minimum_height (x);
224 for (vsize j = 0; j < head_boxes.size (); j++)
226 x.unite (head_boxes[j][X_AXIS]);
229 chord_outlines_[key].set_minimum_height (x[dir]);
232 head_extents_[key].set_empty ();
233 for (vsize i = 0; i < head_boxes.size (); i++)
235 head_extents_[key].unite (head_boxes[i]);
240 Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
245 for (vsize i = 0; i < bounds.size (); i++)
246 ranks.push_back (bounds[i]->get_column ()->get_rank ());
248 vector_sort (ranks, less<int> ());
251 for (vsize i = 0; i < ranks.size (); i++)
253 vector<Item*> col_items;
254 for (vsize j = 0; j < bounds.size (); j ++)
256 if (bounds[j]->get_column ()->get_rank () == ranks[i])
257 col_items.push_back (bounds[j]);
260 set_column_chord_outline (col_items, dir, ranks[i]);
267 Tie_formatting_problem::from_tie (Grob *tie)
270 ties.push_back (tie);
273 details_.from_grob (tie);
277 Tie_formatting_problem::common_x_refpoint () const
283 Tie_formatting_problem::from_ties (vector<Grob*> const &ties)
288 x_refpoint_ = ties[0];
289 for (vsize i = 0; i < ties.size (); i++)
291 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS);
292 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS);
295 details_.from_grob (ties[0]);
300 vector<Item*> bounds;
302 for (vsize i = 0; i < ties.size (); i++)
304 Item *it = dynamic_cast<Spanner*> (ties[i])->get_bound (d);
305 if (it->break_status_dir ())
308 = dynamic_cast<Item*> (unsmob_grob (ties[i]->get_object ("separation-item")));
309 if (sep && sep->get_column () == it->get_column ())
312 bounds.push_back (it);
316 bounds.push_back (it);
320 set_chord_outline (bounds, d);
322 while (flip (&d) != LEFT);
325 for (vsize i = 0; i < ties.size (); i++)
327 Tie_specification spec;
328 spec.from_grob (ties[i]);
332 spec.note_head_drul_[d] = Tie::head (ties[i], d);
333 spec.column_ranks_[d] = Tie::get_column_rank (ties[i], d);
335 while (flip (&d) != LEFT);
336 specifications_.push_back (spec);
341 Tie_formatting_problem::from_semi_ties (vector<Grob*> const &semi_ties, Direction head_dir)
343 if (semi_ties.empty ())
346 details_.from_grob (semi_ties[0]);
349 int column_rank = -1;
350 for (vsize i = 0; i < semi_ties.size (); i++)
352 Tie_specification spec;
353 Item *head = unsmob_item (semi_ties[i]->get_object ("note-head"));
356 programming_error ("LV tie without head?!");
360 spec.position_ = int (Staff_symbol_referencer::get_position (head));
363 spec.from_grob (semi_ties[i]);
365 spec.note_head_drul_[head_dir] = head;
366 column_rank = Tie::get_column_rank (semi_ties[i], head_dir);
367 spec.column_ranks_ = Drul_array<int> (column_rank, column_rank);
368 heads.push_back (head);
369 specifications_.push_back (spec);
372 x_refpoint_ = semi_ties [0];
373 for (vsize i = 0; i < semi_ties.size (); i++)
374 x_refpoint_ = semi_ties[i]->common_refpoint (x_refpoint_, X_AXIS);
375 for (vsize i = 0; i < heads.size (); i++)
376 x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS);
378 set_chord_outline (heads, head_dir);
380 Tuple2<int> head_key (column_rank, head_dir);
381 Tuple2<int> open_key (column_rank, -head_dir);
382 Real extremal = chord_outlines_[head_key].max_height ();
384 chord_outlines_[open_key] = Skyline (head_dir);
385 chord_outlines_[open_key].set_minimum_height (extremal - head_dir * 1.5);
390 Tie_formatting_problem::get_tie_specification (int i) const
392 return specifications_[i];
397 Return configuration, create it if necessary.
400 Tie_formatting_problem::get_configuration (int pos, Direction dir, Drul_array<int> columns,
403 int key_components[] = {
404 pos, dir, columns[LEFT], columns[RIGHT]
406 Tuple<int,4> key (key_components);
408 Tie_configuration_map::const_iterator f = possibilities_.find (key);
409 if (f != possibilities_.end ())
415 Tie_configuration *conf = generate_configuration (pos, dir, columns, tune_dy);
416 ((Tie_formatting_problem*) this)->possibilities_[key] = conf;
421 Tie_formatting_problem::generate_configuration (int pos, Direction dir,
422 Drul_array<int> columns, bool y_tune) const
424 Tie_configuration *conf = new Tie_configuration;
425 conf->position_ = pos;
428 conf->column_ranks_ = columns;
430 Real y = conf->position_ * 0.5 * details_.staff_space_;
432 if (dot_positions_.find (pos) != dot_positions_.end ())
434 conf->delta_y_ += dir * 0.25 * details_.staff_space_;
439 && max (fabs (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y),
440 fabs (get_head_extent (columns[RIGHT], RIGHT, Y_AXIS)[dir] - y)) < 0.25
441 && !Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
444 (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y)
445 + dir * details_.outer_tie_vertical_gap_;
450 conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
451 Real h = conf->height (details_);
456 - should make sliding criterion, should flatten ties if
458 - they're just the wrong (ie. touching line at top & bottom)
462 if (head_positions_slice (columns[LEFT]).contains (pos)
463 || head_positions_slice (columns[RIGHT]).contains (pos)
464 || abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
466 if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
468 if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)
469 && abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
471 conf->center_tie_vertically (details_);
473 else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
475 conf->delta_y_ += dir *
476 details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_;
481 Real top_y = y + conf->delta_y_ + conf->dir_ * h;
482 Real top_pos = top_y / (0.5*details_.staff_space_);
483 int round_pos = int (my_round (top_pos));
485 /* TODO: should use other variable? */
486 Real clearance = details_.center_staff_line_clearance_;
487 if (fabs (top_pos - round_pos) < clearance
488 && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_,
491 Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_;
492 conf->delta_y_ = (new_y - top_y);
497 conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
498 if (conf->height (details_) < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
501 This is less sensible for long ties, since those are more
504 Interval close_by = get_attachment (y
506 + (dir * details_.intra_space_threshold_ * 0.25
507 * details_.staff_space_),
508 conf->column_ranks_);
510 conf->attachment_x_.intersect (close_by);
513 conf->attachment_x_.widen ( - details_.x_gap_);
515 if (conf->column_span_length ())
518 avoid the stems that we attach to as well. We don't do this
519 for semities (span length = 0)
521 It would be better to check D against HEAD-DIRECTION if
527 Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
528 if (get_stem_extent (conf->column_ranks_[d], d, X_AXIS).is_empty ()
529 || !get_stem_extent (conf->column_ranks_[d], d, Y_AXIS).contains (y))
532 conf->attachment_x_[d] =
533 d * min (d * conf->attachment_x_[d],
534 d * (get_stem_extent (conf->column_ranks_[d], d, X_AXIS)[-d] - d * details_.stem_gap_));
536 while (flip (&d) != LEFT);
542 Tie_formatting_problem::get_head_extent (int col, Direction d, Axis a) const
544 Column_extent_map::const_iterator i = head_extents_.find (Tuple2<int> (col, int (d)));
545 if (i != head_extents_.end ())
546 return (*i).second[a];
552 Tie_formatting_problem::get_stem_extent (int col, Direction d, Axis a) const
554 Column_extent_map::const_iterator i = stem_extents_.find (Tuple2<int> (col, int (d)));
555 if (i != stem_extents_.end ())
556 return (*i).second[a];
562 TIE_IDX and TIES_CONF are optional.
565 Tie_formatting_problem::score_aptitude (Tie_configuration *conf,
566 Tie_specification const &spec,
567 Ties_configuration *ties_conf, int tie_idx) const
570 Real curve_y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
571 Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
572 if (sign (curve_y - tie_y) != conf->dir_)
574 Real p = details_.wrong_direction_offset_penalty_;
576 ties_conf->add_tie_score (p, tie_idx, "wrong dir");
582 Real relevant_dist = max (fabs (curve_y - tie_y) - 0.5, 0.0);
583 Real p = details_.vertical_distance_penalty_factor_ * convex_amplifier (1.0, 0.9, relevant_dist);
585 ties_conf->add_tie_score (p, tie_idx, "vdist");
593 if (!spec.note_head_drul_[d])
596 Interval head_x = spec.note_head_drul_[d]->extent (x_refpoint_, X_AXIS);
597 Real dist = head_x.distance (conf->attachment_x_[d]);
601 TODO: flatten with log or sqrt.
603 Real p = details_.horizontal_distance_penalty_factor_
604 * convex_amplifier (1.25, 1.0, dist);
606 ties_conf->add_tie_score (p, tie_idx,
607 (d == LEFT) ? "lhdist" : "rhdist");
612 while (flip (&d) != LEFT);
615 && ties_conf->size () == 1)
618 Drul_array<Grob*> stems (0, 0);
621 if (!spec.note_head_drul_[d])
624 Grob *stem = unsmob_grob (spec.note_head_drul_[d]->get_object ("stem"));
626 && Stem::is_normal_stem (stem))
629 while (flip (&d) != LEFT);
631 bool tie_stem_dir_ok = true;
632 bool tie_position_dir_ok = true;
633 if (stems[LEFT] && !stems[RIGHT])
634 tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[LEFT]);
635 else if (!stems[LEFT] && stems[RIGHT])
636 tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[RIGHT]);
637 else if (stems[LEFT] && stems[RIGHT]
638 && get_grob_direction (stems[LEFT]) == get_grob_direction (stems[RIGHT]))
639 tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[LEFT]);
640 else if (spec.position_)
641 tie_position_dir_ok = conf->dir_ == sign (spec.position_);
643 if (!tie_stem_dir_ok)
644 ties_conf->add_score (details_.same_dir_as_stem_penalty_, "tie/stem dir");
645 if (!tie_position_dir_ok)
646 ties_conf->add_score (details_.same_dir_as_stem_penalty_, "tie/pos dir");
648 while (flip (&d) != LEFT);
656 Tie_formatting_problem::head_positions_slice (int rank) const
658 Position_extent_map::const_iterator i (head_positions_.find (rank));
659 if (i != head_positions_.end ())
668 Score a configuration, ie. how well these ties looks without regard
669 to the note heads that they should connect to.
672 Tie_formatting_problem::score_configuration (Tie_configuration *conf) const
679 Real length = conf->attachment_x_.length ();
681 conf->add_score (details_.min_length_penalty_factor_
682 * peak_around (0.33 * details_.min_length_, details_.min_length_, length),
685 Real tip_pos = conf->position_ + conf->delta_y_ / 0.5 * details_.staff_space_;
686 Real tip_y = tip_pos * details_.staff_space_ * 0.5;
687 Real height = conf->height (details_);
689 Real top_y = tip_y + conf->dir_ * height;
690 Real top_pos = 2 * top_y / details_.staff_space_;
691 Real round_top_pos = rint (top_pos);
692 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
694 && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
697 details_.staff_line_collision_penalty_
698 * peak_around (0.1 * details_.center_staff_line_clearance_,
699 details_.center_staff_line_clearance_,
700 fabs (top_pos - round_top_pos)),
704 int rounded_tip_pos = int (rint (tip_pos));
705 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, rounded_tip_pos)
706 && (head_positions_slice (conf->column_ranks_[LEFT]).contains (rounded_tip_pos)
707 || head_positions_slice (conf->column_ranks_[RIGHT]).contains (rounded_tip_pos)
708 || abs (rounded_tip_pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
711 conf->add_score (details_.staff_line_collision_penalty_
712 * peak_around (0.1 * details_.tip_staff_line_clearance_,
713 details_.tip_staff_line_clearance_,
714 fabs (tip_pos - rint (tip_pos))),
718 if (!dot_x_.is_empty ())
721 Real x = dot_x_.center ();
723 Bezier b = conf->get_transformed_bezier (details_);
724 if (b.control_point_extent (X_AXIS).contains (x))
726 Real y = b.get_other_coordinate (X_AXIS, x);
728 for (set<int>::const_iterator i (dot_positions_.begin ());
729 i != dot_positions_.end (); i ++)
732 conf->add_score (details_.dot_collision_penalty_
733 * peak_around (.1 * details_.dot_collision_clearance_,
734 details_.dot_collision_clearance_,
735 fabs (dot_pos * details_.staff_space_ * 0.5 - y)),
741 conf->scored_ = true;
745 Tie_formatting_problem::score_ties_aptitude (Ties_configuration *ties) const
747 if (ties->size () != specifications_.size ())
749 programming_error ("Huh? Mismatch between sizes.");
753 for (vsize i = 0; i < ties->size (); i++)
754 score_aptitude (&ties->at (i), specifications_[i],
759 Tie_formatting_problem::score_ties (Ties_configuration *ties) const
764 score_ties_configuration (ties);
765 score_ties_aptitude (ties);
766 ties->scored_ = true;
770 Tie_formatting_problem::score_ties_configuration (Ties_configuration *ties) const
772 for (vsize i = 0; i < ties->size (); i++)
774 score_configuration (&ties->at (i));
775 ties->add_tie_score (ties->at (i).score (), i, "conf");
778 Real last_edge = 0.0;
779 Real last_center = 0.0;
780 for (vsize i = 0; i < ties->size (); i++)
782 Bezier b (ties->at (i).get_transformed_bezier (details_));
784 Real center = b.curve_point (0.5)[Y_AXIS];
785 Real edge = b.curve_point (0.0)[Y_AXIS];
789 if (edge <= last_edge)
790 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton edge");
791 if (center <= last_center)
792 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton cent");
794 ties->add_score (details_.tie_tie_collision_penalty_ *
795 peak_around (0.1 * details_.tie_tie_collision_distance_,
796 details_.tie_tie_collision_distance_,
797 fabs (center - last_center)),
799 ties->add_score (details_.tie_tie_collision_penalty_ *
800 peak_around (0.1 * details_.tie_tie_collision_distance_,
801 details_.tie_tie_collision_distance_,
802 fabs (edge - last_edge)), "tietie edge");
806 last_center = center;
809 if (ties->size () > 1)
811 ties->add_score (details_.outer_tie_length_symmetry_penalty_factor_
812 * fabs (ties->at (0).attachment_x_.length () - ties->back ().attachment_x_.length ()),
815 ties->add_score (details_.outer_tie_vertical_distance_symmetry_penalty_factor_
816 * fabs (fabs (specifications_[0].position_ * 0.5 * details_.staff_space_
817 - (ties->at (0).position_ * 0.5 * details_.staff_space_
818 + ties->at (0).delta_y_))
820 fabs (specifications_.back ().position_ * 0.5 * details_.staff_space_
821 - (ties->back ().position_ * 0.5 * details_.staff_space_
822 + ties->back ().delta_y_))),
827 Generate with correct X-attachments and beziers, copying delta_y_
828 from TIES_CONFIG if necessary.
831 Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
833 Ties_configuration copy;
834 for (vsize i = 0; i < ties_config.size (); i++)
836 Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_,
837 ties_config[i].column_ranks_,
838 !specifications_[i].has_manual_delta_y_);
839 if (specifications_[i].has_manual_delta_y_)
842 = (specifications_[i].manual_position_ - ties_config[i].position_)
843 * 0.5 * details_.staff_space_;
845 copy.push_back (*ptr);
852 Tie_formatting_problem::generate_base_chord_configuration ()
854 Ties_configuration ties_config;
855 for (vsize i = 0; i < specifications_.size (); i ++)
857 Tie_configuration conf;
858 if (specifications_[i].has_manual_dir_)
859 conf.dir_ = specifications_[i].manual_dir_;
860 if (specifications_[i].has_manual_position_)
862 conf.position_ = (int) my_round (specifications_[i].manual_position_);
863 if (specifications_[i].has_manual_delta_y_)
864 conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
865 * 0.5 * details_.staff_space_;
869 conf.position_ = specifications_[i].position_;
872 conf.column_ranks_ = specifications_[i].column_ranks_;
873 ties_config.push_back (conf);
876 set_ties_config_standard_directions (&ties_config);
877 for (vsize i = 0; i < ties_config.size (); i++)
878 if (!specifications_[i].manual_position_)
879 ties_config[i].position_ += ties_config[i].dir_;
881 ties_config = generate_ties_configuration (ties_config);
887 Tie_formatting_problem::find_best_variation (Ties_configuration const &base,
888 vector<Tie_configuration_variation> const &vars)
890 Ties_configuration best = base;
893 This simply is 1-opt: we have K substitions, and we try applying
894 exactly every one for each.
896 for (vsize i = 0; i < vars.size (); i++)
898 Ties_configuration variant (base);
899 variant[vars[i].index_] = *vars[i].suggestion_;
901 variant.reset_score ();
902 score_ties (&variant);
904 if (variant.score () < best.score ())
916 Tie_formatting_problem::generate_optimal_configuration ()
918 Ties_configuration base = generate_base_chord_configuration ();
921 vector<Tie_configuration_variation> vars;
922 if (specifications_.size () > 1)
923 vars = generate_collision_variations (base);
925 vars = generate_single_tie_variations (base);
927 Ties_configuration best = find_best_variation (base, vars);
929 if (specifications_.size () > 1)
931 vars = generate_extremal_tie_variations (best);
932 best = find_best_variation (best, vars);
938 Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
940 if (tie_configs->empty ())
943 if (!tie_configs->at (0).dir_)
945 if (tie_configs->size () == 1)
946 tie_configs->at (0).dir_ = Direction (sign (tie_configs->at (0).position_));
948 if (!tie_configs->at (0).dir_)
949 tie_configs->at (0).dir_ = DOWN;
952 if (!tie_configs->back ().dir_)
953 tie_configs->back ().dir_ = UP;
958 for (vsize i = 1; i < tie_configs->size (); i++)
960 Real diff = (tie_configs->at (i).position_
961 -tie_configs->at (i-1).position_);
964 = specifications_[i].column_span () - specifications_[i-1].column_span ();
965 if (span_diff && fabs (diff) <= 2)
968 tie_configs->at (i).dir_ = UP;
969 else if (span_diff < 0)
970 tie_configs->at (i-1).dir_ = DOWN;
972 else if (fabs (diff) <= 1)
974 if (!tie_configs->at (i-1).dir_)
975 tie_configs->at (i-1).dir_ = DOWN;
976 if (!tie_configs->at (i).dir_)
977 tie_configs->at (i).dir_ = UP;
981 for (vsize i = 1; i + 1 < tie_configs->size (); i++)
983 Tie_configuration &conf = tie_configs->at (i);
987 Direction position_dir =
988 Direction (sign (conf.position_));
992 conf.dir_ = position_dir;
996 Tie_configuration_variation::Tie_configuration_variation ()
1002 vector<Tie_configuration_variation>
1003 Tie_formatting_problem::generate_extremal_tie_variations (Ties_configuration const &ties) const
1005 vector<Tie_configuration_variation> vars;
1009 if (boundary (ties, d, 0).dir_ == d
1010 && !boundary (specifications_, d, 0).has_manual_position_)
1011 for (int i = 1; i <= details_.multi_tie_region_size_; i++)
1013 Tie_configuration_variation var;
1014 var.index_ = (d == DOWN) ? 0 : ties.size () - 1;
1015 var.suggestion_ = get_configuration (boundary (ties, d, 0).position_
1017 boundary (ties, d, 0).column_ranks_,
1019 vars.push_back (var);
1022 while (flip (&d) != DOWN);
1027 vector<Tie_configuration_variation>
1028 Tie_formatting_problem::generate_single_tie_variations (Ties_configuration const &ties) const
1030 vector<Tie_configuration_variation> vars;
1032 int sz = details_.single_tie_region_size_;
1033 if (specifications_[0].has_manual_position_)
1035 for (int i = 0; i < sz; i ++)
1041 && ties[0].dir_ == d)
1044 int p = ties[0].position_ + i * d;
1046 if (!specifications_[0].has_manual_dir_
1047 || d == specifications_[0].manual_dir_)
1049 Tie_configuration_variation var;
1051 var.suggestion_ = get_configuration (p,
1052 d, specifications_[0].column_ranks_,
1053 !specifications_[0].has_manual_delta_y_);
1054 vars.push_back (var);
1057 while (flip (&d) != LEFT);
1063 vector<Tie_configuration_variation>
1064 Tie_formatting_problem::generate_collision_variations (Ties_configuration const &ties) const
1066 Real center_distance_tolerance = 0.25;
1068 vector<Tie_configuration_variation> vars;
1069 Real last_center = 0.0;
1070 for (vsize i = 0; i < ties.size (); i++)
1072 Bezier b (ties[i].get_transformed_bezier (details_));
1074 Real center = b.curve_point (0.5)[Y_AXIS];
1078 if (center <= last_center + center_distance_tolerance)
1080 if (!specifications_[i].has_manual_dir_)
1082 Tie_configuration_variation var;
1084 var.suggestion_ = get_configuration (specifications_[i].position_
1088 ties[i].column_ranks_,
1089 !specifications_[i].has_manual_delta_y_
1092 vars.push_back (var);
1095 if (!specifications_[i-1].has_manual_dir_)
1097 Tie_configuration_variation var;
1099 var.suggestion_ = get_configuration (specifications_[i-1].position_
1102 specifications_[i-1].column_ranks_,
1103 !specifications_[i-1].has_manual_delta_y_
1106 vars.push_back (var);
1109 if (i == 1 && !specifications_[i-1].has_manual_position_
1110 && ties[i-1].dir_ == DOWN)
1112 Tie_configuration_variation var;
1114 var.suggestion_ = get_configuration (specifications_[i-1].position_ - 1, DOWN,
1115 specifications_[i-1].column_ranks_,
1116 !specifications_[i-1].has_manual_delta_y_
1119 vars.push_back (var);
1121 if (i == ties.size () && !specifications_[i].has_manual_position_
1122 && ties[i].dir_ == UP)
1124 Tie_configuration_variation var;
1126 var.suggestion_ = get_configuration (specifications_[i].position_
1128 specifications_[i].column_ranks_,
1129 !specifications_[i].has_manual_delta_y_
1131 vars.push_back (var);
1134 else if (dot_positions_.find (ties[i].position_) != dot_positions_.end ()
1135 && !specifications_[i].has_manual_position_)
1137 Tie_configuration_variation var;
1139 var.suggestion_ = get_configuration (ties[i].position_ + ties[i].dir_,
1141 ties[i].column_ranks_,
1142 !specifications_[i].has_manual_delta_y_
1144 vars.push_back (var);
1149 last_center = center;
1157 Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs)
1160 for (SCM s = manual_configs;
1161 scm_is_pair (s) && k < specifications_.size (); s = scm_cdr (s))
1163 SCM entry = scm_car (s);
1164 if (scm_is_pair (entry))
1166 Tie_specification &spec = specifications_[k];
1168 if (scm_is_number (scm_car (entry)))
1170 spec.has_manual_position_ = true;
1171 spec.manual_position_ = scm_to_double (scm_car (entry));
1172 spec.has_manual_delta_y_ = (scm_inexact_p (scm_car (entry)) == SCM_BOOL_T);
1175 if (scm_is_number (scm_cdr (entry)))
1177 spec.has_manual_dir_ = true;
1178 spec.manual_dir_ = Direction (scm_to_int (scm_cdr (entry)));
1187 Tie_formatting_problem::set_debug_scoring (Ties_configuration const &base)
1189 #if DEBUG_TIE_SCORING
1190 if (to_boolean (x_refpoint_->layout ()
1191 ->lookup_variable (ly_symbol2scm ("debug-tie-scoring"))))
1193 for (vsize i = 0; i < base.size (); i++)
1195 string card = base.complete_tie_card (i);
1196 specifications_[i].tie_grob_->set_property ("quant-score",
1197 ly_string2scm (card));