2 tie-formatting-problem.cc -- implement Tie_formatting_problem
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 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"
28 Tie_formatting_problem::print_ties_configuration (Ties_configuration const *ties)
30 for (vsize i = 0; i < ties->size (); i++)
32 char const *man_pos = (specifications_[i].has_manual_position_) ? "(M)" : "";
33 char const *man_dir = (specifications_[i].has_manual_dir_) ? "(M)" : "";
34 char const *dir = (ties->at (i).dir_ == UP) ? "up" : "dn";
36 printf ("(P%d%s, %s%s) ", ties->at (i).position_, man_pos, dir, man_dir);
42 Tie_formatting_problem::get_attachment (Real y, Drul_array<int> columns) const
44 Interval attachments (0,0);
48 Tuple2<int> key (columns[d], int (d));
49 Chord_outline_map::const_iterator i (chord_outlines_.find (key));
50 if (i == chord_outlines_.end ())
51 programming_error ("Cannot find chord outline");
53 attachments[d] = i->second.height (y);
55 while (flip (&d) != LEFT);
60 Tie_formatting_problem::Tie_formatting_problem()
65 Tie_formatting_problem::~Tie_formatting_problem ()
67 for (Tie_configuration_map::const_iterator i (possibilities_.begin ());
68 i != possibilities_.end (); i++)
73 Tie_formatting_problem::set_column_chord_outline (vector<Item*> bounds,
77 Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
80 vector<Box> head_boxes;
83 for (vsize i = 0; i < bounds.size (); i++)
85 Grob *head = bounds[i];
86 if (!Note_head::has_interface (head))
90 stem = unsmob_grob (head->get_object ("stem"));
92 Real p = Staff_symbol_referencer::get_position (head);
93 Interval y ((p-1) * 0.5 * staff_space,
94 (p+1) * 0.5 * staff_space);
96 Interval x = head->extent (x_refpoint_, X_AXIS);
97 head_boxes.push_back (Box (x, y));
98 boxes.push_back (Box (x, y));
100 Grob *dots = Rhythmic_head::get_dots (head);
101 if (dir == LEFT && dots)
103 Interval x = dots->extent (x_refpoint_, X_AXIS);
104 int p = int (Staff_symbol_referencer::get_position (dots));
106 dot_positions_.insert (p);
109 Interval y (dots->extent (dots, Y_AXIS));
110 y.translate (p * staff_space * 0.5);
112 boxes.push_back (Box (x, y));
116 Tuple2<int> key (column_rank, int (dir));
119 && !Stem::is_invisible (stem))
122 x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS));
123 x.widen (staff_space / 20); // ugh.
125 y.add_point (Stem::stem_end_position (stem) * staff_space * .5);
127 Direction stemdir = get_grob_direction (stem);
128 y.add_point (Stem::head_positions (stem)[-stemdir]
131 boxes.push_back (Box (x, y));
133 stem_extents_[key].unite (Box (x,y));
137 Box flag_box = Stem::get_translated_flag (stem).extent_box ();
138 flag_box.translate( Offset (x[RIGHT], X_AXIS));
139 boxes.push_back (flag_box);
144 Grob *head = Stem::support_head (stem);
147 In case of invisible stem, don't pass x-center of heads.
149 Real x_center = head->extent (x_refpoint_, X_AXIS).center ();
151 x_ext[-dir] = x_center;
153 for (vsize j = 0; j < head_boxes.size (); j++)
154 y_ext.unite (head_boxes[j][Y_AXIS]);
156 boxes.push_back (Box (x_ext, y_ext));
159 Direction updowndir = DOWN;
164 if (head_boxes.size())
166 Box b = boundary (head_boxes, updowndir, 0);
168 x[-dir] = b[X_AXIS].linear_combination (-dir / 2);
169 y[-updowndir] = b[Y_AXIS][updowndir];
170 y[updowndir] = updowndir * infinity_f;
174 boxes.push_back (Box (x, y));
176 while (flip (&updowndir) != DOWN);
178 /* todo: the horizon_padding is somewhat arbitrary */
179 chord_outlines_[key] = Skyline (boxes, 0.1, Y_AXIS, -dir);
180 if (bounds[0]->break_status_dir ())
182 Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-dir];
184 chord_outlines_[key].set_minimum_height (x);
189 for (vsize j = 0; j < head_boxes.size (); j++)
191 x.unite (head_boxes[j][X_AXIS]);
194 chord_outlines_[key].set_minimum_height (x[dir]);
197 head_extents_[key].set_empty ();
198 for (vsize i = 0; i < head_boxes.size (); i++)
200 head_extents_[key].unite (head_boxes[i]);
205 Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
210 for (vsize i = 0; i < bounds.size (); i++)
211 ranks.push_back (bounds[i]->get_column ()->get_rank ());
213 vector_sort (ranks, less<int> ());
216 for (vsize i = 0; i < ranks.size (); i++)
218 vector<Item*> col_items;
219 for (vsize j = 0; j < bounds.size (); j ++)
221 if (bounds[j]->get_column ()->get_rank () == ranks[i])
222 col_items.push_back (bounds[j]);
225 set_column_chord_outline (col_items, dir, ranks[i]);
232 Tie_formatting_problem::from_tie (Grob *tie)
235 ties.push_back (tie);
238 details_.from_grob (tie);
242 Tie_formatting_problem::common_x_refpoint () const
248 Tie_formatting_problem::from_ties (vector<Grob*> const &ties)
253 x_refpoint_ = ties[0];
254 for (vsize i = 0; i < ties.size (); i++)
256 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS);
257 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS);
260 details_.from_grob (ties[0]);
265 vector<Item*> bounds;
267 for (vsize i = 0; i < ties.size (); i++)
269 Item *it = dynamic_cast<Spanner*> (ties[i])->get_bound (d);
270 if (it->break_status_dir ())
273 = dynamic_cast<Item*> (unsmob_grob (ties[i]->get_object ("separation-item")));
274 if (sep && sep->get_column () == it->get_column ())
278 bounds.push_back (it);
281 set_chord_outline (bounds, d);
283 while (flip (&d) != LEFT);
286 for (vsize i = 0; i < ties.size (); i++)
288 Tie_specification spec;
290 spec.get_tie_manual_settings (ties[i]);
296 spec.note_head_drul_[d] = Tie::head (ties[i], d);
297 spec.column_ranks_[d] = Tie::get_column_rank (ties[i], d);
299 while (flip (&d) != LEFT);
300 specifications_.push_back (spec);
305 Tie_formatting_problem::from_semi_ties (vector<Grob*> const &semi_ties, Direction head_dir)
307 if (semi_ties.empty ())
310 details_.from_grob (semi_ties[0]);
313 int column_rank = -1;
314 for (vsize i = 0; i < semi_ties.size (); i++)
316 Tie_specification spec;
317 Item *head = unsmob_item (semi_ties[i]->get_object ("note-head"));
320 programming_error ("LV tie without head?!");
324 spec.position_ = int (Staff_symbol_referencer::get_position (head));
327 spec.get_tie_manual_settings (semi_ties[i]);
329 spec.note_head_drul_[head_dir] = head;
330 column_rank = Tie::get_column_rank (semi_ties[i], head_dir);
331 spec.column_ranks_ = Drul_array<int> (column_rank, column_rank);
332 heads.push_back (head);
333 specifications_.push_back (spec);
336 x_refpoint_ = semi_ties [0];
337 for (vsize i = 0; i < semi_ties.size (); i++)
338 x_refpoint_ = semi_ties[i]->common_refpoint (x_refpoint_, X_AXIS);
339 for (vsize i = 0; i < heads.size (); i++)
340 x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS);
342 set_chord_outline (heads, head_dir);
344 Tuple2<int> head_key (column_rank, head_dir);
345 Tuple2<int> open_key (column_rank, -head_dir);
346 Real extremal = chord_outlines_[head_key].max_height ();
348 chord_outlines_[open_key] = Skyline (head_dir);
349 chord_outlines_[open_key].set_minimum_height (extremal - head_dir * 1.5);
354 Tie_formatting_problem::get_tie_specification (int i) const
356 return specifications_[i];
361 Return configuration, create it if necessary.
364 Tie_formatting_problem::get_configuration (int pos, Direction dir, Drul_array<int> columns) const
366 int key_components[] = {
367 pos, dir, columns[LEFT], columns[RIGHT]
369 Tuple<int,4> key (key_components);
371 Tie_configuration_map::const_iterator f = possibilities_.find (key);
372 if (f != possibilities_.end ())
378 Tie_configuration *conf = generate_configuration (pos, dir, columns);
379 ((Tie_formatting_problem*) this)->possibilities_[key] = conf;
384 Tie_formatting_problem::generate_configuration (int pos, Direction dir,
385 Drul_array<int> columns) const
387 Tie_configuration *conf = new Tie_configuration;
388 conf->position_ = pos;
391 conf->column_ranks_ = columns;
393 Real y = conf->position_ * 0.5 * details_.staff_space_;
396 if (dot_positions_.find (pos) != dot_positions_.end ())
398 conf->delta_y_ += dir * 0.25 * details_.staff_space_;
403 && max (fabs (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y),
404 fabs (get_head_extent (columns[RIGHT], RIGHT, Y_AXIS)[dir] - y)) < 0.25
405 && !Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
408 (get_head_extent (columns[LEFT], LEFT, Y_AXIS)[dir] - y)
409 + dir * details_.outer_tie_vertical_gap_;
414 conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
415 Real h = conf->height (details_);
420 - should make sliding criterion, should flatten ties if
422 - they're just the wrong (ie. touching line at top & bottom)
426 if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
428 if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)
429 && abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
431 conf->center_tie_vertically (details_);
433 else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
435 conf->delta_y_ += dir *
436 details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_;
441 Real top_y = y + conf->delta_y_ + conf->dir_ * h;
442 Real top_pos = top_y / (0.5*details_.staff_space_);
443 int round_pos = int (my_round (top_pos));
445 /* TODO: should use other variable? */
446 Real clearance = details_.center_staff_line_clearance_;
447 if (fabs (top_pos - round_pos) < clearance
448 && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_,
451 Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_;
452 conf->delta_y_ = (new_y - top_y);
457 conf->attachment_x_ = get_attachment (y + conf->delta_y_, conf->column_ranks_);
458 if (conf->height (details_) < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
461 This is less sensible for long ties, since those are more
464 Interval close_by = get_attachment (y
466 + (dir * details_.intra_space_threshold_ * 0.25
467 * details_.staff_space_),
468 conf->column_ranks_);
470 conf->attachment_x_.intersect (close_by);
473 conf->attachment_x_.widen ( - details_.x_gap_);
475 if (conf->column_span_length ())
478 avoid the stems that we attach to as well. We don't do this
479 for semities (span length = 0)
481 It would be better to check D against HEAD-DIRECTION if
487 Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
488 if (get_stem_extent (conf->column_ranks_[d], d, X_AXIS).is_empty ()
489 || !get_stem_extent (conf->column_ranks_[d], d, Y_AXIS).contains (y))
492 conf->attachment_x_[d] =
493 d * min (d * conf->attachment_x_[d],
494 d * (get_stem_extent (conf->column_ranks_[d], d, X_AXIS)[-d] - d * details_.stem_gap_));
496 while (flip (&d) != LEFT);
502 Tie_formatting_problem::get_head_extent (int col, Direction d, Axis a) const
504 Column_extent_map::const_iterator i = head_extents_.find (Tuple2<int> (col, int (d)));
505 if (i != head_extents_.end ())
506 return (*i).second[a];
512 Tie_formatting_problem::get_stem_extent (int col, Direction d, Axis a) const
514 Column_extent_map::const_iterator i = stem_extents_.find (Tuple2<int> (col, int (d)));
515 if (i != stem_extents_.end ())
516 return (*i).second[a];
522 TIE_IDX and TIES_CONF are optional.
525 Tie_formatting_problem::score_aptitude (Tie_configuration *conf,
526 Tie_specification const &spec,
527 Ties_configuration *ties_conf, int tie_idx) const
530 Real curve_y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
531 Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
532 if (sign (curve_y - tie_y) != conf->dir_)
534 Real p = details_.wrong_direction_offset_penalty_;
536 ties_conf->add_tie_score (p, tie_idx, "wrong dir");
542 Real p = details_.vertical_distance_penalty_factor_ * fabs (curve_y - tie_y);
544 ties_conf->add_tie_score (p, tie_idx, "vdist");
552 if (!spec.note_head_drul_[d])
555 Interval head_x = spec.note_head_drul_[d]->extent (x_refpoint_, X_AXIS);
556 Real dist = head_x.distance (conf->attachment_x_[d]);
559 TODO: flatten with log or sqrt.
561 Real p = details_.horizontal_distance_penalty_factor_ * dist;
563 ties_conf->add_tie_score (p, tie_idx,
564 (d == LEFT) ? "lhdist" : "rhdist");
568 while (flip (&d) != LEFT);
574 Tie_formatting_problem::score_configuration (Tie_configuration *conf) const
581 Real length = conf->attachment_x_.length ();
583 conf->add_score (details_.min_length_penalty_factor_
584 * peak_around (0.33 * details_.min_length_, details_.min_length_, length),
587 Real tip_pos = conf->position_ + conf->delta_y_ / 0.5 * details_.staff_space_;
588 Real tip_y = tip_pos * details_.staff_space_ * 0.5;
589 Real height = conf->height (details_);
591 Real top_y = tip_y + conf->dir_ * height;
592 Real top_pos = 2 * top_y / details_.staff_space_;
593 Real round_top_pos = rint (top_pos);
594 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
596 && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
599 details_.staff_line_collision_penalty_
600 * peak_around (0.1 * details_.center_staff_line_clearance_,
601 details_.center_staff_line_clearance_,
602 fabs (top_pos - round_top_pos)),
606 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
607 int (rint (tip_pos))))
609 conf->add_score (details_.staff_line_collision_penalty_
610 * peak_around (0.1 * details_.tip_staff_line_clearance_,
611 details_.tip_staff_line_clearance_,
612 fabs (tip_pos - rint (tip_pos))),
616 if (!dot_x_.is_empty ())
619 Real x = dot_x_.center ();
621 Bezier b = conf->get_transformed_bezier (details_);
622 if (b.control_point_extent (X_AXIS).contains (x))
624 Real y = b.get_other_coordinate (X_AXIS, x);
626 for (set<int>::const_iterator i (dot_positions_.begin ());
627 i != dot_positions_.end (); i ++)
630 conf->add_score (details_.dot_collision_penalty_
631 * peak_around (.1 * details_.dot_collision_clearance_,
632 details_.dot_collision_clearance_,
633 fabs (dot_pos * details_.staff_space_ * 0.5 - y)),
639 conf->scored_ = true;
643 Tie_formatting_problem::find_optimal_tie_configuration (Tie_specification const &spec) const
645 vector<Tie_configuration*> confs;
647 int pos = spec.position_;
648 Direction dir = spec.manual_dir_;
650 for (int i = 0; i < details_.single_tie_region_size_; i ++)
652 confs.push_back (generate_configuration (pos + i * dir, dir,
653 spec.column_ranks_));
655 if (spec.has_manual_position_)
657 confs.back ()->delta_y_
658 = (spec.manual_position_ - spec.position_)
659 * 0.5 * details_.staff_space_;
668 Real best_score = 1e6;
669 for (vsize i = 0; i < confs.size (); i ++)
671 score_configuration (confs[i]);
672 Real score = score_aptitude (confs[i], spec, 0, 0)
673 + confs[i]->score ();
675 if (score < best_score)
683 programming_error ("No best tie configuration found.");
685 Tie_configuration best
686 = (best_idx >= 0) ? *confs[best_idx] : *confs[0];
688 for (vsize i = 0; i < confs.size (); i++)
694 Tie_specification::Tie_specification ()
696 has_manual_position_ = false;
697 has_manual_dir_ = false;
699 manual_position_ = 0;
700 manual_dir_ = CENTER;
701 note_head_drul_[LEFT] =
702 note_head_drul_[RIGHT] = 0;
703 column_ranks_[RIGHT] =
704 column_ranks_[LEFT] = 0;
709 Tie_specification::get_tie_manual_settings (Grob *tie)
711 if (scm_is_number (tie->get_property_data ("direction")))
713 manual_dir_ = to_dir (tie->get_property ("direction"));
714 has_manual_dir_ = true;
717 position_ = Tie::get_position (tie);
718 if (scm_is_number (tie->get_property ("staff-position")))
720 manual_position_ = scm_to_double (tie->get_property ("staff-position"));
721 has_manual_position_ = true;
722 position_ = int (my_round (manual_position_));
727 Tie_specification::column_span () const
729 return column_ranks_[RIGHT] - column_ranks_[LEFT];
733 Tie_formatting_problem::score_ties_aptitude (Ties_configuration *ties) const
735 if (ties->size () != specifications_.size ())
737 programming_error ("Huh? Mismatch between sizes.");
741 for (vsize i = 0; i < ties->size (); i++)
742 score_aptitude (&ties->at (i), specifications_[i],
747 Tie_formatting_problem::score_ties (Ties_configuration *ties) const
752 score_ties_configuration (ties);
753 score_ties_aptitude (ties);
754 ties->scored_ = true;
758 Tie_formatting_problem::score_ties_configuration (Ties_configuration *ties) const
760 for (vsize i = 0; i < ties->size (); i++)
762 score_configuration (&ties->at (i));
763 ties->add_tie_score (ties->at (i).score (), i, "conf");
766 Real last_edge = 0.0;
767 Real last_center = 0.0;
768 for (vsize i = 0; i < ties->size (); i++)
770 Bezier b (ties->at (i).get_transformed_bezier (details_));
772 Real center = b.curve_point (0.5)[Y_AXIS];
773 Real edge = b.curve_point (0.0)[Y_AXIS];
777 if (edge <= last_edge)
778 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton edge");
779 if (center <= last_center)
780 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton cent");
782 ties->add_score (details_.tie_tie_collision_penalty_ *
783 peak_around (0.1 * details_.tie_tie_collision_distance_,
784 details_.tie_tie_collision_distance_,
785 fabs (center - last_center)),
787 ties->add_score (details_.tie_tie_collision_penalty_ *
788 peak_around (0.1 * details_.tie_tie_collision_distance_,
789 details_.tie_tie_collision_distance_,
790 fabs (edge - last_edge)), "tietie edge");
794 last_center = center;
797 ties->add_score (details_.outer_tie_length_symmetry_penalty_factor_
798 * fabs (ties->at (0).attachment_x_.length () - ties->back ().attachment_x_.length ()),
801 ties->add_score (details_.outer_tie_vertical_distance_symmetry_penalty_factor_
802 * fabs (fabs (specifications_[0].position_ * 0.5 * details_.staff_space_
803 - (ties->at (0).position_ * 0.5 * details_.staff_space_
804 + ties->at (0).delta_y_))
806 fabs (specifications_.back ().position_ * 0.5 * details_.staff_space_
807 - (ties->back ().position_ * 0.5 * details_.staff_space_
808 + ties->back ().delta_y_))),
813 Generate with correct X-attachments and beziers, copying delta_y_
814 from TIES_CONFIG if necessary.
817 Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
819 Ties_configuration copy;
820 for (vsize i = 0; i < ties_config.size (); i++)
822 Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_,
823 ties_config[i].column_ranks_);
824 if (specifications_[i].has_manual_position_)
827 = (specifications_[i].manual_position_ - ties_config[i].position_)
828 * 0.5 * details_.staff_space_;
830 copy.push_back (*ptr);
837 Tie_formatting_problem::generate_base_chord_configuration ()
839 Ties_configuration ties_config;
840 for (vsize i = 0; i < specifications_.size (); i ++)
842 Tie_configuration conf;
843 if (specifications_[i].has_manual_dir_)
844 conf.dir_ = specifications_[i].manual_dir_;
845 if (specifications_[i].has_manual_position_)
847 conf.position_ = (int) my_round (specifications_[i].manual_position_);
848 conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
849 * 0.5 * details_.staff_space_;
853 conf.position_ = specifications_[i].position_;
856 conf.column_ranks_ = specifications_[i].column_ranks_;
857 ties_config.push_back (conf);
860 set_ties_config_standard_directions (&ties_config);
861 for (vsize i = 0; i < ties_config.size (); i++)
862 if (!specifications_[i].manual_position_)
863 ties_config[i].position_ += ties_config[i].dir_;
865 ties_config = generate_ties_configuration (ties_config);
871 Tie_formatting_problem::find_best_variation (Ties_configuration const &base,
872 vector<Tie_configuration_variation> vars)
874 Ties_configuration best = base;
877 This simply is 1-opt: we have K substitions, and we try applying
878 exactly every one for each.
880 for (vsize i = 0; i < vars.size (); i++)
882 Ties_configuration variant (base);
883 variant[vars[i].index_] = *vars[i].suggestion_;
885 variant.reset_score ();
886 score_ties (&variant);
888 if (variant.score () < best.score ())
900 Tie_formatting_problem::generate_optimal_chord_configuration ()
902 Ties_configuration base = generate_base_chord_configuration ();
903 vector<Tie_configuration_variation> vars = generate_collision_variations (base);
906 Ties_configuration best = find_best_variation (base, vars);
907 vars = generate_extremal_tie_variations (best);
908 best = find_best_variation (best, vars);
914 Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
916 if (tie_configs->empty ())
919 if (!tie_configs->at (0).dir_)
921 if (tie_configs->size () == 1)
922 tie_configs->at (0).dir_ = Direction (sign (tie_configs->at (0).position_));
924 if (!tie_configs->at (0).dir_)
925 tie_configs->at (0).dir_ = DOWN;
928 if (!tie_configs->back ().dir_)
929 tie_configs->back ().dir_ = UP;
934 for (vsize i = 1; i < tie_configs->size (); i++)
936 Real diff = (tie_configs->at (i).position_
937 -tie_configs->at (i-1).position_);
940 = specifications_[i].column_span () - specifications_[i-1].column_span ();
941 if (span_diff && fabs (diff) <= 2)
944 tie_configs->at (i).dir_ = UP;
945 else if (span_diff < 0)
946 tie_configs->at (i-1).dir_ = DOWN;
948 else if (fabs (diff) <= 1)
950 if (!tie_configs->at (i-1).dir_)
951 tie_configs->at (i-1).dir_ = DOWN;
952 if (!tie_configs->at (i).dir_)
953 tie_configs->at (i).dir_ = UP;
957 for (vsize i = 1; i < tie_configs->size() - 1; i++)
959 Tie_configuration &conf = tie_configs->at (i);
963 Direction position_dir =
964 Direction (sign (conf.position_));
968 conf.dir_ = position_dir;
972 Tie_configuration_variation::Tie_configuration_variation ()
978 vector<Tie_configuration_variation>
979 Tie_formatting_problem::generate_extremal_tie_variations (Ties_configuration const &ties) const
981 vector<Tie_configuration_variation> vars;
985 if (boundary (ties, d, 0).dir_ == d
986 && !boundary (specifications_, d, 0).has_manual_position_)
987 for (int i = 1; i <= details_.multi_tie_region_size_; i++)
989 Tie_configuration_variation var;
990 var.index_ = (d == DOWN) ? 0 : ties.size () - 1;
991 var.suggestion_ = get_configuration (boundary (ties, d, 0).position_
993 boundary (ties, d, 0).column_ranks_);
994 vars.push_back (var);
997 while (flip (&d) != DOWN);
1003 vector<Tie_configuration_variation>
1004 Tie_formatting_problem::generate_collision_variations (Ties_configuration const &ties) const
1006 Real center_distance_tolerance = 0.25;
1008 vector<Tie_configuration_variation> vars;
1009 Real last_center = 0.0;
1010 for (vsize i = 0; i < ties.size (); i++)
1012 Bezier b (ties[i].get_transformed_bezier (details_));
1014 Real center = b.curve_point (0.5)[Y_AXIS];
1018 if (center <= last_center + center_distance_tolerance)
1020 if (!specifications_[i].has_manual_dir_)
1022 Tie_configuration_variation var;
1024 var.suggestion_ = get_configuration (specifications_[i].position_
1028 ties[i].column_ranks_
1031 vars.push_back (var);
1034 if (!specifications_[i-1].has_manual_dir_)
1036 Tie_configuration_variation var;
1038 var.suggestion_ = get_configuration (specifications_[i-1].position_
1041 specifications_[i-1].column_ranks_);
1043 vars.push_back (var);
1046 if (i == 1 && !specifications_[i-1].has_manual_position_
1047 && ties[i-1].dir_ == DOWN)
1049 Tie_configuration_variation var;
1051 var.suggestion_ = get_configuration (specifications_[i-1].position_ - 1, DOWN,
1052 specifications_[i-1].column_ranks_);
1053 vars.push_back (var);
1055 if (i == ties.size() && !specifications_[i].has_manual_position_
1056 && ties[i].dir_ == UP)
1058 Tie_configuration_variation var;
1060 var.suggestion_ = get_configuration (specifications_[i].position_
1062 specifications_[i].column_ranks_);
1063 vars.push_back (var);
1066 else if (dot_positions_.find (ties[i].position_) != dot_positions_.end ()
1067 && !specifications_[i].has_manual_position_)
1069 Tie_configuration_variation var;
1071 var.suggestion_ = get_configuration (ties[i].position_ + ties[i].dir_,
1073 ties[i].column_ranks_);
1074 vars.push_back (var);
1079 last_center = center;
1087 Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs)
1090 for (SCM s = manual_configs;
1091 scm_is_pair (s) && k < specifications_.size (); s = scm_cdr (s))
1093 SCM entry = scm_car (s);
1094 if (scm_is_pair (entry))
1096 Tie_specification &spec = specifications_[k];
1098 if (scm_is_number (scm_car (entry)))
1100 spec.has_manual_position_ = true;
1101 spec.manual_position_ = scm_to_double (scm_car (entry));
1103 if (scm_is_number (scm_cdr (entry)))
1105 spec.has_manual_dir_ = true;
1106 spec.manual_dir_ = Direction (scm_to_int (scm_cdr (entry)));