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 ()
64 use_horizontal_spacing_ = true;
67 Tie_formatting_problem::~Tie_formatting_problem ()
69 for (Tie_configuration_map::const_iterator i (possibilities_.begin ());
70 i != possibilities_.end (); i++)
75 Tie_formatting_problem::set_column_chord_outline (vector<Item*> bounds,
79 Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
82 vector<Box> head_boxes;
85 for (vsize i = 0; i < bounds.size (); i++)
87 Grob *head = bounds[i];
88 if (!Note_head::has_interface (head))
92 stem = unsmob_grob (head->get_object ("stem"));
94 Real p = Staff_symbol_referencer::get_position (head);
95 Interval y ((p-1) * 0.5 * staff_space,
96 (p+1) * 0.5 * staff_space);
98 Interval x = head->extent (x_refpoint_, X_AXIS);
99 head_boxes.push_back (Box (x, y));
100 boxes.push_back (Box (x, y));
102 Grob *dots = Rhythmic_head::get_dots (head);
103 if (dir == LEFT && dots)
105 Interval x = dots->extent (x_refpoint_, X_AXIS);
106 int p = int (Staff_symbol_referencer::get_position (dots));
109 TODO: shouldn't this use column-rank dependent key?
111 dot_positions_.insert (p);
114 Interval y (dots->extent (dots, Y_AXIS));
115 y.translate (p * staff_space * 0.5);
117 boxes.push_back (Box (x, y));
121 Tuple2<int> key (column_rank, int (dir));
125 if (Stem::is_normal_stem (stem))
128 x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS));
129 x.widen (staff_space / 20); // ugh.
132 Real stem_end_position = 0.0;
133 if (Stem::is_cross_staff (stem))
134 stem_end_position = get_grob_direction (stem) * infinity_f;
137 if (use_horizontal_spacing_ || !Stem::get_beam (stem))
138 stem_end_position = Stem::stem_end_position (stem) * staff_space * .5;
140 stem_end_position = Stem::note_head_positions (stem)[get_grob_direction (stem)]
144 y.add_point (stem_end_position);
146 Direction stemdir = get_grob_direction (stem);
147 y.add_point (Stem::head_positions (stem)[-stemdir]
153 boxes.push_back (Box (x, y));
155 stem_extents_[key].unite (Box (x, y));
159 Box flag_box = Stem::get_translated_flag (stem).extent_box ();
160 flag_box.translate ( Offset (x[RIGHT], X_AXIS));
161 boxes.push_back (flag_box);
166 Grob *head = Stem::support_head (stem);
169 In case of invisible stem, don't pass x-center of heads.
171 Real x_center = head->extent (x_refpoint_, X_AXIS).center ();
173 x_ext[-dir] = x_center;
175 for (vsize j = 0; j < head_boxes.size (); j++)
176 y_ext.unite (head_boxes[j][Y_AXIS]);
178 boxes.push_back (Box (x_ext, y_ext));
181 extract_grob_set (stem, "note-heads", heads);
182 for (vsize i = 0; i < heads.size (); i ++)
184 if (find (bounds.begin (), bounds.end (), dynamic_cast<Item*> (heads[i])) == bounds.end ())
187 other untied notes in the same chord.
190 Interval y = Staff_symbol_referencer::extent_in_staff (heads[i]);
191 Interval x = heads[i]->extent (x_refpoint_, X_AXIS);
192 boxes.push_back (Box (x, y));
195 Grob *acc = unsmob_grob (heads[i]->get_object ("accidental-grob"));
196 if (acc && acc->is_live () && dir == RIGHT)
198 boxes.push_back (Box (acc->extent (x_refpoint_, X_AXIS),
199 Staff_symbol_referencer::extent_in_staff (acc)));
202 head_positions_[column_rank].add_point (int (Staff_symbol_referencer::get_position (heads[i])));
207 Direction updowndir = DOWN;
212 if (head_boxes.size ())
214 Box b = boundary (head_boxes, updowndir, 0);
216 x[-dir] = b[X_AXIS].linear_combination (-dir / 2);
217 y[-updowndir] = b[Y_AXIS][updowndir];
218 y[updowndir] = updowndir * infinity_f;
222 boxes.push_back (Box (x, y));
224 while (flip (&updowndir) != DOWN);
226 /* todo: the horizon_padding is somewhat arbitrary */
227 chord_outlines_[key] = Skyline (boxes, details_.skyline_padding_, Y_AXIS, -dir);
228 if (bounds[0]->break_status_dir ())
230 Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-dir];
232 chord_outlines_[key].set_minimum_height (x);
237 for (vsize j = 0; j < head_boxes.size (); j++)
239 x.unite (head_boxes[j][X_AXIS]);
242 chord_outlines_[key].set_minimum_height (x[dir]);
245 head_extents_[key].set_empty ();
246 for (vsize i = 0; i < head_boxes.size (); i++)
248 head_extents_[key].unite (head_boxes[i]);
253 Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
258 for (vsize i = 0; i < bounds.size (); i++)
259 ranks.push_back (bounds[i]->get_column ()->get_rank ());
261 vector_sort (ranks, less<int> ());
264 for (vsize i = 0; i < ranks.size (); i++)
266 vector<Item*> col_items;
267 for (vsize j = 0; j < bounds.size (); j ++)
269 if (bounds[j]->get_column ()->get_rank () == ranks[i])
270 col_items.push_back (bounds[j]);
273 set_column_chord_outline (col_items, dir, ranks[i]);
280 Tie_formatting_problem::from_tie (Grob *tie)
283 ties.push_back (tie);
286 details_.from_grob (tie);
290 Tie_formatting_problem::common_x_refpoint () const
296 Tie_formatting_problem::from_ties (vector<Grob*> const &ties)
301 x_refpoint_ = ties[0];
302 for (vsize i = 0; i < ties.size (); i++)
304 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS);
305 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS);
308 details_.from_grob (ties[0]);
313 vector<Item*> bounds;
315 for (vsize i = 0; i < ties.size (); i++)
317 Item *it = dynamic_cast<Spanner*> (ties[i])->get_bound (d);
318 if (it->break_status_dir ())
321 = dynamic_cast<Item*> (unsmob_grob (ties[i]->get_object ("separation-item")));
322 if (sep && sep->get_column () == it->get_column ())
325 bounds.push_back (it);
329 bounds.push_back (it);
333 set_chord_outline (bounds, d);
335 while (flip (&d) != LEFT);
338 for (vsize i = 0; i < ties.size (); i++)
340 Tie_specification spec;
341 spec.from_grob (ties[i]);
345 spec.note_head_drul_[d] = Tie::head (ties[i], d);
346 spec.column_ranks_[d] = Tie::get_column_rank (ties[i], d);
348 while (flip (&d) != LEFT);
349 specifications_.push_back (spec);
354 Tie_formatting_problem::from_semi_ties (vector<Grob*> const &semi_ties, Direction head_dir)
356 if (semi_ties.empty ())
359 use_horizontal_spacing_ = false;
360 details_.from_grob (semi_ties[0]);
363 int column_rank = -1;
364 for (vsize i = 0; i < semi_ties.size (); i++)
366 Tie_specification spec;
367 Item *head = unsmob_item (semi_ties[i]->get_object ("note-head"));
370 programming_error ("LV tie without head?!");
374 spec.position_ = int (Staff_symbol_referencer::get_position (head));
377 spec.from_grob (semi_ties[i]);
379 spec.note_head_drul_[head_dir] = head;
380 column_rank = Tie::get_column_rank (semi_ties[i], head_dir);
381 spec.column_ranks_ = Drul_array<int> (column_rank, column_rank);
382 heads.push_back (head);
383 specifications_.push_back (spec);
386 x_refpoint_ = semi_ties [0];
387 for (vsize i = 0; i < semi_ties.size (); i++)
388 x_refpoint_ = semi_ties[i]->common_refpoint (x_refpoint_, X_AXIS);
389 for (vsize i = 0; i < heads.size (); i++)
390 x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS);
392 set_chord_outline (heads, head_dir);
394 Tuple2<int> head_key (column_rank, head_dir);
395 Tuple2<int> open_key (column_rank, -head_dir);
396 Real extremal = chord_outlines_[head_key].max_height ();
398 chord_outlines_[open_key] = Skyline (head_dir);
399 chord_outlines_[open_key].set_minimum_height (extremal - head_dir * 1.5);
404 Tie_formatting_problem::get_tie_specification (int i) const
406 return specifications_[i];
411 Return configuration, create it if necessary.
414 Tie_formatting_problem::get_configuration (int pos, Direction dir, Drul_array<int> columns,
417 int key_components[] = {
418 pos, dir, columns[LEFT], columns[RIGHT]
420 Tuple<int,4> key (key_components);
422 Tie_configuration_map::const_iterator f = possibilities_.find (key);
423 if (f != possibilities_.end ())
429 Tie_configuration *conf = generate_configuration (pos, dir, columns, tune_dy);
430 ((Tie_formatting_problem*) this)->possibilities_[key] = conf;
435 Tie_formatting_problem::generate_configuration (int pos, Direction dir,
436 Drul_array<int> columns, bool y_tune) const
438 Tie_configuration *conf = new Tie_configuration;
439 conf->position_ = pos;
442 conf->column_ranks_ = columns;
444 Real y = conf->position_ * 0.5 * details_.staff_space_;
446 if (dot_positions_.find (pos) != dot_positions_.end ())
448 conf->delta_y_ += dir * 0.25 * details_.staff_space_;
453 && max (fabs (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y),
454 fabs (get_head_extent (columns[RIGHT], RIGHT, Y_AXIS)[dir] - y)) < 0.25
455 && !Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
458 (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y)
459 + dir * details_.outer_tie_vertical_gap_;
464 conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
465 Real h = conf->height (details_);
470 - should make sliding criterion, should flatten ties if
472 - they're just the wrong (ie. touching line at top & bottom)
476 if (head_positions_slice (columns[LEFT]).contains (pos)
477 || head_positions_slice (columns[RIGHT]).contains (pos)
478 || abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
480 if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
482 if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)
483 && abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
485 conf->center_tie_vertically (details_);
487 else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
489 conf->delta_y_ += dir *
490 details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_;
495 Real top_y = y + conf->delta_y_ + conf->dir_ * h;
496 Real top_pos = top_y / (0.5*details_.staff_space_);
497 int round_pos = int (my_round (top_pos));
499 /* TODO: should use other variable? */
500 Real clearance = details_.center_staff_line_clearance_;
501 if (fabs (top_pos - round_pos) < clearance
502 && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_,
505 Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_;
506 conf->delta_y_ = (new_y - top_y);
511 conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
512 if (conf->height (details_) < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
515 This is less sensible for long ties, since those are more
518 Interval close_by = get_attachment (y
520 + (dir * details_.intra_space_threshold_ * 0.25
521 * details_.staff_space_),
522 conf->column_ranks_);
524 conf->attachment_x_.intersect (close_by);
527 conf->attachment_x_.widen ( - details_.x_gap_);
529 if (conf->column_span_length ())
532 avoid the stems that we attach to as well. We don't do this
533 for semities (span length = 0)
535 It would be better to check D against HEAD-DIRECTION if
541 Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
542 if (get_stem_extent (conf->column_ranks_[d], d, X_AXIS).is_empty ()
543 || !get_stem_extent (conf->column_ranks_[d], d, Y_AXIS).contains (y))
546 conf->attachment_x_[d] =
547 d * min (d * conf->attachment_x_[d],
548 d * (get_stem_extent (conf->column_ranks_[d], d, X_AXIS)[-d] - d * details_.stem_gap_));
550 while (flip (&d) != LEFT);
556 Tie_formatting_problem::get_head_extent (int col, Direction d, Axis a) const
558 Column_extent_map::const_iterator i = head_extents_.find (Tuple2<int> (col, int (d)));
559 if (i != head_extents_.end ())
560 return (*i).second[a];
566 Tie_formatting_problem::get_stem_extent (int col, Direction d, Axis a) const
568 Column_extent_map::const_iterator i = stem_extents_.find (Tuple2<int> (col, int (d)));
569 if (i != stem_extents_.end ())
570 return (*i).second[a];
576 TIE_IDX and TIES_CONF are optional.
579 Tie_formatting_problem::score_aptitude (Tie_configuration *conf,
580 Tie_specification const &spec,
581 Ties_configuration *ties_conf, int tie_idx) const
584 Real curve_y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
585 Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
586 if (sign (curve_y - tie_y) != conf->dir_)
588 Real p = details_.wrong_direction_offset_penalty_;
590 ties_conf->add_tie_score (p, tie_idx, "wrong dir");
596 Real relevant_dist = max (fabs (curve_y - tie_y) - 0.5, 0.0);
597 Real p = details_.vertical_distance_penalty_factor_ * convex_amplifier (1.0, 0.9, relevant_dist);
599 ties_conf->add_tie_score (p, tie_idx, "vdist");
607 if (!spec.note_head_drul_[d])
610 Interval head_x = spec.note_head_drul_[d]->extent (x_refpoint_, X_AXIS);
611 Real dist = head_x.distance (conf->attachment_x_[d]);
615 TODO: flatten with log or sqrt.
617 Real p = details_.horizontal_distance_penalty_factor_
618 * convex_amplifier (1.25, 1.0, dist);
620 ties_conf->add_tie_score (p, tie_idx,
621 (d == LEFT) ? "lhdist" : "rhdist");
626 while (flip (&d) != LEFT);
629 && ties_conf->size () == 1)
632 Drul_array<Grob*> stems (0, 0);
635 if (!spec.note_head_drul_[d])
638 Grob *stem = unsmob_grob (spec.note_head_drul_[d]->get_object ("stem"));
640 && Stem::is_normal_stem (stem))
643 while (flip (&d) != LEFT);
645 bool tie_stem_dir_ok = true;
646 bool tie_position_dir_ok = true;
647 if (stems[LEFT] && !stems[RIGHT])
648 tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[LEFT]);
649 else if (!stems[LEFT] && stems[RIGHT])
650 tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[RIGHT]);
651 else if (stems[LEFT] && stems[RIGHT]
652 && get_grob_direction (stems[LEFT]) == get_grob_direction (stems[RIGHT]))
653 tie_stem_dir_ok = conf->dir_ != get_grob_direction (stems[LEFT]);
654 else if (spec.position_)
655 tie_position_dir_ok = conf->dir_ == sign (spec.position_);
657 if (!tie_stem_dir_ok)
658 ties_conf->add_score (details_.same_dir_as_stem_penalty_, "tie/stem dir");
659 if (!tie_position_dir_ok)
660 ties_conf->add_score (details_.same_dir_as_stem_penalty_, "tie/pos dir");
662 while (flip (&d) != LEFT);
670 Tie_formatting_problem::head_positions_slice (int rank) const
672 Position_extent_map::const_iterator i (head_positions_.find (rank));
673 if (i != head_positions_.end ())
682 Score a configuration, ie. how well these ties looks without regard
683 to the note heads that they should connect to.
686 Tie_formatting_problem::score_configuration (Tie_configuration *conf) const
693 Real length = conf->attachment_x_.length ();
695 conf->add_score (details_.min_length_penalty_factor_
696 * peak_around (0.33 * details_.min_length_, details_.min_length_, length),
699 Real tip_pos = conf->position_ + conf->delta_y_ / 0.5 * details_.staff_space_;
700 Real tip_y = tip_pos * details_.staff_space_ * 0.5;
701 Real height = conf->height (details_);
703 Real top_y = tip_y + conf->dir_ * height;
704 Real top_pos = 2 * top_y / details_.staff_space_;
705 Real round_top_pos = rint (top_pos);
706 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
708 && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
711 details_.staff_line_collision_penalty_
712 * peak_around (0.1 * details_.center_staff_line_clearance_,
713 details_.center_staff_line_clearance_,
714 fabs (top_pos - round_top_pos)),
718 int rounded_tip_pos = int (rint (tip_pos));
719 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, rounded_tip_pos)
720 && (head_positions_slice (conf->column_ranks_[LEFT]).contains (rounded_tip_pos)
721 || head_positions_slice (conf->column_ranks_[RIGHT]).contains (rounded_tip_pos)
722 || abs (rounded_tip_pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
725 conf->add_score (details_.staff_line_collision_penalty_
726 * peak_around (0.1 * details_.tip_staff_line_clearance_,
727 details_.tip_staff_line_clearance_,
728 fabs (tip_pos - rint (tip_pos))),
732 if (!dot_x_.is_empty ())
735 Real x = dot_x_.center ();
737 Bezier b = conf->get_transformed_bezier (details_);
738 if (b.control_point_extent (X_AXIS).contains (x))
740 Real y = b.get_other_coordinate (X_AXIS, x);
742 for (set<int>::const_iterator i (dot_positions_.begin ());
743 i != dot_positions_.end (); i ++)
746 conf->add_score (details_.dot_collision_penalty_
747 * peak_around (.1 * details_.dot_collision_clearance_,
748 details_.dot_collision_clearance_,
749 fabs (dot_pos * details_.staff_space_ * 0.5 - y)),
755 conf->scored_ = true;
759 Tie_formatting_problem::score_ties_aptitude (Ties_configuration *ties) const
761 if (ties->size () != specifications_.size ())
763 programming_error ("Huh? Mismatch between sizes.");
767 for (vsize i = 0; i < ties->size (); i++)
768 score_aptitude (&ties->at (i), specifications_[i],
773 Tie_formatting_problem::score_ties (Ties_configuration *ties) const
778 score_ties_configuration (ties);
779 score_ties_aptitude (ties);
780 ties->scored_ = true;
784 Tie_formatting_problem::score_ties_configuration (Ties_configuration *ties) const
786 for (vsize i = 0; i < ties->size (); i++)
788 score_configuration (&ties->at (i));
789 ties->add_tie_score (ties->at (i).score (), i, "conf");
792 Real last_edge = 0.0;
793 Real last_center = 0.0;
794 for (vsize i = 0; i < ties->size (); i++)
796 Bezier b (ties->at (i).get_transformed_bezier (details_));
798 Real center = b.curve_point (0.5)[Y_AXIS];
799 Real edge = b.curve_point (0.0)[Y_AXIS];
803 if (edge <= last_edge)
804 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton edge");
805 if (center <= last_center)
806 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton cent");
808 ties->add_score (details_.tie_tie_collision_penalty_ *
809 peak_around (0.1 * details_.tie_tie_collision_distance_,
810 details_.tie_tie_collision_distance_,
811 fabs (center - last_center)),
813 ties->add_score (details_.tie_tie_collision_penalty_ *
814 peak_around (0.1 * details_.tie_tie_collision_distance_,
815 details_.tie_tie_collision_distance_,
816 fabs (edge - last_edge)), "tietie edge");
820 last_center = center;
823 if (ties->size () > 1)
825 ties->add_score (details_.outer_tie_length_symmetry_penalty_factor_
826 * fabs (ties->at (0).attachment_x_.length () - ties->back ().attachment_x_.length ()),
829 ties->add_score (details_.outer_tie_vertical_distance_symmetry_penalty_factor_
830 * fabs (fabs (specifications_[0].position_ * 0.5 * details_.staff_space_
831 - (ties->at (0).position_ * 0.5 * details_.staff_space_
832 + ties->at (0).delta_y_))
834 fabs (specifications_.back ().position_ * 0.5 * details_.staff_space_
835 - (ties->back ().position_ * 0.5 * details_.staff_space_
836 + ties->back ().delta_y_))),
841 Generate with correct X-attachments and beziers, copying delta_y_
842 from TIES_CONFIG if necessary.
845 Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
847 Ties_configuration copy;
848 for (vsize i = 0; i < ties_config.size (); i++)
850 Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_,
851 ties_config[i].column_ranks_,
852 !specifications_[i].has_manual_delta_y_);
853 if (specifications_[i].has_manual_delta_y_)
856 = (specifications_[i].manual_position_ - ties_config[i].position_)
857 * 0.5 * details_.staff_space_;
859 copy.push_back (*ptr);
866 Tie_formatting_problem::generate_base_chord_configuration ()
868 Ties_configuration ties_config;
869 for (vsize i = 0; i < specifications_.size (); i ++)
871 Tie_configuration conf;
872 if (specifications_[i].has_manual_dir_)
873 conf.dir_ = specifications_[i].manual_dir_;
874 if (specifications_[i].has_manual_position_)
876 conf.position_ = (int) my_round (specifications_[i].manual_position_);
877 if (specifications_[i].has_manual_delta_y_)
878 conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
879 * 0.5 * details_.staff_space_;
883 conf.position_ = specifications_[i].position_;
886 conf.column_ranks_ = specifications_[i].column_ranks_;
887 ties_config.push_back (conf);
890 set_ties_config_standard_directions (&ties_config);
891 for (vsize i = 0; i < ties_config.size (); i++)
892 if (!specifications_[i].manual_position_)
893 ties_config[i].position_ += ties_config[i].dir_;
895 ties_config = generate_ties_configuration (ties_config);
901 Tie_formatting_problem::find_best_variation (Ties_configuration const &base,
902 vector<Tie_configuration_variation> const &vars)
904 Ties_configuration best = base;
907 This simply is 1-opt: we have K substitions, and we try applying
908 exactly every one for each.
910 for (vsize i = 0; i < vars.size (); i++)
912 Ties_configuration variant (base);
913 variant[vars[i].index_] = *vars[i].suggestion_;
915 variant.reset_score ();
916 score_ties (&variant);
918 if (variant.score () < best.score ())
930 Tie_formatting_problem::generate_optimal_configuration ()
932 Ties_configuration base = generate_base_chord_configuration ();
935 vector<Tie_configuration_variation> vars;
936 if (specifications_.size () > 1)
937 vars = generate_collision_variations (base);
939 vars = generate_single_tie_variations (base);
941 Ties_configuration best = find_best_variation (base, vars);
943 if (specifications_.size () > 1)
945 vars = generate_extremal_tie_variations (best);
946 best = find_best_variation (best, vars);
952 Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
954 if (tie_configs->empty ())
957 if (!tie_configs->at (0).dir_)
959 if (tie_configs->size () == 1)
960 tie_configs->at (0).dir_ = Direction (sign (tie_configs->at (0).position_));
962 if (!tie_configs->at (0).dir_)
963 tie_configs->at (0).dir_ = DOWN;
966 if (!tie_configs->back ().dir_)
967 tie_configs->back ().dir_ = UP;
972 for (vsize i = 1; i < tie_configs->size (); i++)
974 Real diff = (tie_configs->at (i).position_
975 -tie_configs->at (i-1).position_);
978 = specifications_[i].column_span () - specifications_[i-1].column_span ();
979 if (span_diff && fabs (diff) <= 2)
982 tie_configs->at (i).dir_ = UP;
983 else if (span_diff < 0)
984 tie_configs->at (i-1).dir_ = DOWN;
986 else if (fabs (diff) <= 1)
988 if (!tie_configs->at (i-1).dir_)
989 tie_configs->at (i-1).dir_ = DOWN;
990 if (!tie_configs->at (i).dir_)
991 tie_configs->at (i).dir_ = UP;
995 for (vsize i = 1; i + 1 < tie_configs->size (); i++)
997 Tie_configuration &conf = tie_configs->at (i);
1001 Direction position_dir =
1002 Direction (sign (conf.position_));
1004 position_dir = DOWN;
1006 conf.dir_ = position_dir;
1010 Tie_configuration_variation::Tie_configuration_variation ()
1016 vector<Tie_configuration_variation>
1017 Tie_formatting_problem::generate_extremal_tie_variations (Ties_configuration const &ties) const
1019 vector<Tie_configuration_variation> vars;
1023 if (boundary (ties, d, 0).dir_ == d
1024 && !boundary (specifications_, d, 0).has_manual_position_)
1025 for (int i = 1; i <= details_.multi_tie_region_size_; i++)
1027 Tie_configuration_variation var;
1028 var.index_ = (d == DOWN) ? 0 : ties.size () - 1;
1029 var.suggestion_ = get_configuration (boundary (ties, d, 0).position_
1031 boundary (ties, d, 0).column_ranks_,
1033 vars.push_back (var);
1036 while (flip (&d) != DOWN);
1041 vector<Tie_configuration_variation>
1042 Tie_formatting_problem::generate_single_tie_variations (Ties_configuration const &ties) const
1044 vector<Tie_configuration_variation> vars;
1046 int sz = details_.single_tie_region_size_;
1047 if (specifications_[0].has_manual_position_)
1049 for (int i = 0; i < sz; i ++)
1055 && ties[0].dir_ == d)
1058 int p = ties[0].position_ + i * d;
1060 if (!specifications_[0].has_manual_dir_
1061 || d == specifications_[0].manual_dir_)
1063 Tie_configuration_variation var;
1065 var.suggestion_ = get_configuration (p,
1066 d, specifications_[0].column_ranks_,
1067 !specifications_[0].has_manual_delta_y_);
1068 vars.push_back (var);
1071 while (flip (&d) != LEFT);
1077 vector<Tie_configuration_variation>
1078 Tie_formatting_problem::generate_collision_variations (Ties_configuration const &ties) const
1080 Real center_distance_tolerance = 0.25;
1082 vector<Tie_configuration_variation> vars;
1083 Real last_center = 0.0;
1084 for (vsize i = 0; i < ties.size (); i++)
1086 Bezier b (ties[i].get_transformed_bezier (details_));
1088 Real center = b.curve_point (0.5)[Y_AXIS];
1092 if (center <= last_center + center_distance_tolerance)
1094 if (!specifications_[i].has_manual_dir_)
1096 Tie_configuration_variation var;
1098 var.suggestion_ = get_configuration (specifications_[i].position_
1102 ties[i].column_ranks_,
1103 !specifications_[i].has_manual_delta_y_
1106 vars.push_back (var);
1109 if (!specifications_[i-1].has_manual_dir_)
1111 Tie_configuration_variation var;
1113 var.suggestion_ = get_configuration (specifications_[i-1].position_
1116 specifications_[i-1].column_ranks_,
1117 !specifications_[i-1].has_manual_delta_y_
1120 vars.push_back (var);
1123 if (i == 1 && !specifications_[i-1].has_manual_position_
1124 && ties[i-1].dir_ == DOWN)
1126 Tie_configuration_variation var;
1128 var.suggestion_ = get_configuration (specifications_[i-1].position_ - 1, DOWN,
1129 specifications_[i-1].column_ranks_,
1130 !specifications_[i-1].has_manual_delta_y_
1133 vars.push_back (var);
1135 if (i == ties.size () && !specifications_[i].has_manual_position_
1136 && ties[i].dir_ == UP)
1138 Tie_configuration_variation var;
1140 var.suggestion_ = get_configuration (specifications_[i].position_
1142 specifications_[i].column_ranks_,
1143 !specifications_[i].has_manual_delta_y_
1145 vars.push_back (var);
1148 else if (dot_positions_.find (ties[i].position_) != dot_positions_.end ()
1149 && !specifications_[i].has_manual_position_)
1151 Tie_configuration_variation var;
1153 var.suggestion_ = get_configuration (ties[i].position_ + ties[i].dir_,
1155 ties[i].column_ranks_,
1156 !specifications_[i].has_manual_delta_y_
1158 vars.push_back (var);
1163 last_center = center;
1171 Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs)
1174 for (SCM s = manual_configs;
1175 scm_is_pair (s) && k < specifications_.size (); s = scm_cdr (s))
1177 SCM entry = scm_car (s);
1178 if (scm_is_pair (entry))
1180 Tie_specification &spec = specifications_[k];
1182 if (scm_is_number (scm_car (entry)))
1184 spec.has_manual_position_ = true;
1185 spec.manual_position_ = scm_to_double (scm_car (entry));
1186 spec.has_manual_delta_y_ = (scm_inexact_p (scm_car (entry)) == SCM_BOOL_T);
1189 if (scm_is_number (scm_cdr (entry)))
1191 spec.has_manual_dir_ = true;
1192 spec.manual_dir_ = Direction (scm_to_int (scm_cdr (entry)));
1201 Tie_formatting_problem::set_debug_scoring (Ties_configuration const &base)
1203 #if DEBUG_TIE_SCORING
1204 if (to_boolean (x_refpoint_->layout ()
1205 ->lookup_variable (ly_symbol2scm ("debug-tie-scoring"))))
1207 for (vsize i = 0; i < base.size (); i++)
1209 string card = base.complete_tie_card (i);
1210 specifications_[i].tie_grob_->set_property ("quant-score",
1211 ly_string2scm (card));