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"
13 #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) const
48 attachments[d] = skyline_height (chord_outlines_[d], y, -d);
50 while (flip (&d) != LEFT);
55 Tie_formatting_problem::Tie_formatting_problem()
60 Tie_formatting_problem::~Tie_formatting_problem ()
62 for (Tie_configuration_map::const_iterator i (possibilities_.begin ());
63 i != possibilities_.end (); i++)
68 Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
71 Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
74 vector<Box> head_boxes;
77 for (vsize i = 0; i < bounds.size (); i++)
79 Grob *head = bounds[i];
80 if (!Note_head::has_interface (head))
84 stem = unsmob_grob (head->get_object ("stem"));
86 Real p = Staff_symbol_referencer::get_position (head);
87 Interval y ((p-1) * 0.5 * staff_space,
88 (p+1) * 0.5 * staff_space);
90 Interval x = head->extent (x_refpoint_, X_AXIS);
91 head_boxes.push_back (Box (x, y));
92 boxes.push_back (Box (x, y));
94 Grob *dots = Rhythmic_head::get_dots (head);
95 if (dir == LEFT && dots)
97 Interval x = dots->extent (x_refpoint_, X_AXIS);
98 int p = int (Staff_symbol_referencer::get_position (dots));
100 dot_positions_.insert (p);
103 Interval y (dots->extent (dots, Y_AXIS));
104 y.translate (p * staff_space * 0.5);
106 boxes.push_back (Box (x, y));
110 chord_outlines_[dir] = empty_skyline (-dir);
111 if (bounds[0]->break_status_dir ())
113 Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-dir];
114 chord_outlines_[dir].at (0).height_ = x;
119 for (vsize i = 0; i < head_boxes.size (); i++)
121 x.unite (head_boxes[i][X_AXIS]);
124 chord_outlines_[dir].at (0).height_ = x[dir];
127 for (vsize i = 0; i < boxes.size (); i++)
128 insert_extent_into_skyline (&chord_outlines_[dir] ,
129 boxes[i], Y_AXIS, -dir);
132 && !Stem::is_invisible (stem))
135 x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS));
136 x.widen (staff_space / 20); // ugh.
138 y.add_point (Stem::stem_end_position (stem) * staff_space * .5);
140 Direction stemdir = get_grob_direction (stem);
141 y.add_point (Stem::head_positions (stem)[-stemdir]
144 insert_extent_into_skyline (&chord_outlines_[dir], Box (x,y), Y_AXIS, -dir);
146 stem_extents_[dir].unite (Box (x,y));
150 Box flag_box = Stem::get_translated_flag (stem).extent_box ();
151 flag_box.translate( Offset (x[RIGHT], X_AXIS));
152 insert_extent_into_skyline (&chord_outlines_[dir], flag_box,
157 Direction updowndir = DOWN;
162 if (head_boxes.size())
164 Box b = boundary (head_boxes, updowndir, 0);
166 x[-dir] = b[X_AXIS].linear_combination (-dir / 2);
167 y[-updowndir] = b[Y_AXIS][updowndir];
168 y[updowndir] = updowndir * infinity_f;
172 insert_extent_into_skyline (&chord_outlines_[dir],
176 while (flip (&updowndir) != DOWN);
178 head_extents_[dir].set_empty ();
179 for (vsize i = 0; i < head_boxes.size (); i++)
181 head_extents_[dir].unite (head_boxes[i]);
187 Tie_formatting_problem::from_tie (Grob *tie)
190 ties.push_back (tie);
193 details_.from_grob (tie);
197 Tie_formatting_problem::common_x_refpoint () const
203 Tie_formatting_problem::from_ties (vector<Grob*> const &ties)
208 x_refpoint_ = ties[0];
209 for (vsize i = 0; i < ties.size (); i++)
211 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS);
212 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS);
215 details_.from_grob (ties[0]);
220 vector<Item*> bounds;
222 for (vsize i = 0; i < ties.size (); i++)
224 Item *it = dynamic_cast<Spanner*> (ties[i])->get_bound (d);
226 bounds.push_back (it);
229 set_chord_outline (bounds, d);
231 while (flip (&d) != LEFT);
234 for (vsize i = 0; i < ties.size (); i++)
236 Tie_specification spec;
238 if (scm_is_number (ties[i]->get_property_data (ly_symbol2scm ("direction"))))
240 spec.manual_dir_ = to_dir (ties[i]->get_property ("direction"));
241 spec.has_manual_dir_ = true;
244 spec.position_ = Tie::get_position (ties[i]);
248 spec.note_head_drul_[d] = Tie::head (ties[i], d);
250 while (flip (&d) != LEFT);
252 specifications_.push_back (spec);
257 Tie_formatting_problem::from_semi_ties (vector<Grob*> const &lv_ties, Direction head_dir)
259 if (lv_ties.empty ())
262 details_.from_grob (lv_ties[0]);
265 for (vsize i = 0; i < lv_ties.size (); i++)
267 Tie_specification spec;
268 Item *head = unsmob_item (lv_ties[i]->get_object ("note-head"));
271 programming_error ("LV tie without head?!");
275 spec.position_ = int (Staff_symbol_referencer::get_position (head));
278 spec.note_head_drul_[head_dir] = head;
279 heads.push_back (head);
280 specifications_.push_back (spec);
283 x_refpoint_ = lv_ties [0];
284 for (vsize i = 0; i < lv_ties.size (); i++)
285 x_refpoint_ = lv_ties[i]->common_refpoint (x_refpoint_, X_AXIS);
286 for (vsize i = 0; i < heads.size (); i++)
287 x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS);
289 set_chord_outline (heads, head_dir);
291 Real extremal = head_dir * infinity_f;
293 for (vsize i = 0; i < chord_outlines_[head_dir].size (); i++)
295 extremal = head_dir * min (head_dir * extremal,
296 head_dir * chord_outlines_[head_dir][i].height_);
299 Skyline_entry right_entry;
300 right_entry.width_.set_full ();
301 right_entry.height_ = extremal - head_dir * 1.5;
303 chord_outlines_[-head_dir].push_back (right_entry);
308 Tie_formatting_problem::get_tie_specification (int i) const
310 return specifications_[i];
315 Tie_formatting_problem::get_configuration (int pos, Direction dir) const
317 pair<int,int> key (pos, dir);
318 Tie_configuration_map::const_iterator f = possibilities_.find (key);
320 if (f != possibilities_.end ())
326 Tie_configuration *conf = generate_configuration (pos, dir);
327 ((Tie_formatting_problem*) this)->possibilities_[key] = conf;
332 Tie_formatting_problem::generate_configuration (int pos, Direction dir) const
334 Tie_configuration *conf = new Tie_configuration;
335 conf->position_ = pos;
337 Real y = conf->position_ * 0.5 * details_.staff_space_;
341 if (dot_positions_.find (pos) != dot_positions_.end ())
343 conf->delta_y_ += dir * 0.25 * details_.staff_space_;
350 && max (fabs (head_extents_[LEFT][Y_AXIS][dir] - y),
351 fabs (head_extents_[RIGHT][Y_AXIS][dir] - y)) < 0.25
352 && !Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
355 (head_extents_[LEFT][Y_AXIS][dir] - y)
356 + dir * details_.outer_tie_vertical_gap_;
361 conf->attachment_x_ = get_attachment (y + conf->delta_y_);
362 Real h = conf->height (details_);
367 - should make sliding criterion, should flatten ties if
369 - they're just the wrong (ie. touching line at top & bottom)
373 if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
375 if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)
376 && abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
378 conf->center_tie_vertically (details_);
380 else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
382 conf->delta_y_ += dir *
383 details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_;
388 Real top_y = y + conf->delta_y_ + conf->dir_ * h;
389 Real top_pos = top_y / (0.5*details_.staff_space_);
390 int round_pos = int (my_round (top_pos));
392 /* TODO: should use other variable? */
393 Real clearance = details_.center_staff_line_clearance_;
394 if (fabs (top_pos - round_pos) < clearance
395 && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_,
398 Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_;
399 conf->delta_y_ = (new_y - top_y);
404 conf->attachment_x_ = get_attachment (y + conf->delta_y_);
405 if (conf->height (details_) < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
408 This is less sensible for long ties, since those are more
411 Interval close_by = get_attachment (y
413 + (dir * details_.intra_space_threshold_ * 0.25
414 * details_.staff_space_));
416 conf->attachment_x_.intersect (close_by);
419 conf->attachment_x_.widen ( - details_.x_gap_);
424 Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
425 if (stem_extents_[d][X_AXIS].is_empty ()
426 || !stem_extents_[d][Y_AXIS].contains (y))
429 conf->attachment_x_[d] =
430 d* min (d * conf->attachment_x_[d],
431 d * (stem_extents_[d][X_AXIS][-d] - d * details_.stem_gap_));
433 while (flip (&d) != LEFT);
438 TIE_IDX and TIES_CONF are optional.
441 Tie_formatting_problem::score_aptitude (Tie_configuration *conf,
442 Tie_specification const &spec,
443 Ties_configuration *ties_conf, int tie_idx) const
446 Real curve_y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
447 Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
448 if (sign (curve_y - tie_y) != conf->dir_)
450 Real p = details_.wrong_direction_offset_penalty_;
452 ties_conf->add_tie_score (p, tie_idx, "wrong dir");
458 Real p = details_.vertical_distance_penalty_factor_ * fabs (curve_y - tie_y);
460 ties_conf->add_tie_score (p, tie_idx, "vdist");
468 if (!spec.note_head_drul_[d])
471 Interval head_x = spec.note_head_drul_[d]->extent (x_refpoint_, X_AXIS);
472 Real dist = head_x.distance (conf->attachment_x_[d]);
475 TODO: flatten with log or sqrt.
477 Real p = details_.horizontal_distance_penalty_factor_ * dist;
479 ties_conf->add_tie_score (p, tie_idx,
480 (d == LEFT) ? "lhdist" : "rhdist");
484 while (flip (&d) != LEFT);
490 Tie_formatting_problem::score_configuration (Tie_configuration *conf) const
497 Real length = conf->attachment_x_.length ();
499 conf->add_score (details_.min_length_penalty_factor_
500 * peak_around (0.33 * details_.min_length_, details_.min_length_, length),
503 Real tip_pos = conf->position_ + conf->delta_y_ / 0.5 * details_.staff_space_;
504 Real tip_y = tip_pos * details_.staff_space_ * 0.5;
505 Real height = conf->height (details_);
507 Real top_y = tip_y + conf->dir_ * height;
508 Real top_pos = 2 * top_y / details_.staff_space_;
509 Real round_top_pos = rint (top_pos);
510 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
512 && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
515 details_.staff_line_collision_penalty_
516 * peak_around (0.1 * details_.center_staff_line_clearance_,
517 details_.center_staff_line_clearance_,
518 fabs (top_pos - round_top_pos)),
522 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
523 int (rint (tip_pos))))
525 conf->add_score (details_.staff_line_collision_penalty_
526 * peak_around (0.1 * details_.tip_staff_line_clearance_,
527 details_.tip_staff_line_clearance_,
528 fabs (tip_pos - rint (tip_pos))),
532 if (!dot_x_.is_empty ())
535 Real x = dot_x_.center ();
537 Bezier b = conf->get_transformed_bezier (details_);
538 if (b.control_point_extent (X_AXIS).contains (x))
540 Real y = b.get_other_coordinate (X_AXIS, x);
542 for (set<int>::const_iterator i (dot_positions_.begin ());
543 i != dot_positions_.end (); i ++)
546 conf->add_score (details_.dot_collision_penalty_
547 * peak_around (.1 * details_.dot_collision_clearance_,
548 details_.dot_collision_clearance_,
549 fabs (dot_pos * details_.staff_space_ * 0.5 - y)),
555 conf->scored_ = true;
559 Tie_formatting_problem::find_optimal_tie_configuration (Tie_specification const &spec) const
561 vector<Tie_configuration*> confs;
563 int pos = spec.position_;
564 Direction dir = spec.manual_dir_;
566 for (int i = 0; i < details_.single_tie_region_size_; i ++)
568 confs.push_back (generate_configuration (pos + i * dir, dir));
574 Real best_score = 1e6;
575 for (vsize i = 0; i < confs.size (); i ++)
577 score_configuration (confs[i]);
578 Real score = score_aptitude (confs[i], spec, 0, 0)
579 + confs[i]->score ();
581 if (score < best_score)
588 Tie_configuration best = *confs[best_idx];
589 for (vsize i = 0; i < confs.size (); i++)
595 Tie_specification::Tie_specification ()
597 has_manual_position_ = false;
598 has_manual_dir_ = false;
600 manual_position_ = 0;
601 manual_dir_ = CENTER;
602 note_head_drul_[LEFT] =
603 note_head_drul_[RIGHT] = 0;
608 Tie_formatting_problem::score_ties_aptitude (Ties_configuration *ties) const
610 if (ties->size () != specifications_.size ())
612 programming_error ("Huh? Mismatch between sizes.");
616 for (vsize i = 0; i < ties->size (); i++)
617 score_aptitude (&ties->at (i), specifications_[i],
622 Tie_formatting_problem::score_ties (Ties_configuration *ties) const
627 score_ties_configuration (ties);
628 score_ties_aptitude (ties);
629 ties->scored_ = true;
633 Tie_formatting_problem::score_ties_configuration (Ties_configuration *ties) const
635 for (vsize i = 0; i < ties->size (); i++)
637 score_configuration (&ties->at (i));
638 ties->add_tie_score (ties->at (i).score (), i, "conf");
641 Real last_edge = 0.0;
642 Real last_center = 0.0;
643 for (vsize i = 0; i < ties->size (); i++)
645 Bezier b (ties->at (i).get_transformed_bezier (details_));
647 Real center = b.curve_point (0.5)[Y_AXIS];
648 Real edge = b.curve_point (0.0)[Y_AXIS];
652 if (edge <= last_edge)
653 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton edge");
654 if (center <= last_center)
655 ties->add_score (details_.tie_column_monotonicity_penalty_, "monoton cent");
657 ties->add_score (details_.tie_tie_collision_penalty_ *
658 peak_around (0.1 * details_.tie_tie_collision_distance_,
659 details_.tie_tie_collision_distance_,
660 fabs (center - last_center)),
662 ties->add_score (details_.tie_tie_collision_penalty_ *
663 peak_around (0.1 * details_.tie_tie_collision_distance_,
664 details_.tie_tie_collision_distance_,
665 fabs (edge - last_edge)), "tietie edge");
669 last_center = center;
672 ties->add_score (details_.outer_tie_length_symmetry_penalty_factor_
673 * fabs (ties->at (0).attachment_x_.length () - ties->back ().attachment_x_.length ()),
676 ties->add_score (details_.outer_tie_vertical_distance_symmetry_penalty_factor_
677 * fabs (fabs (specifications_[0].position_ * 0.5 * details_.staff_space_
678 - (ties->at (0).position_ * 0.5 * details_.staff_space_
679 + ties->at (0).delta_y_))
681 fabs (specifications_.back ().position_ * 0.5 * details_.staff_space_
682 - (ties->back ().position_ * 0.5 * details_.staff_space_
683 + ties->back ().delta_y_))),
688 Generate with correct X-attachments and beziers, copying delta_y_
689 from TIES_CONFIG if necessary.
692 Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
694 Ties_configuration copy;
695 for (vsize i = 0; i < ties_config.size (); i++)
697 Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_);
698 if (specifications_[i].has_manual_position_)
701 = (specifications_[i].manual_position_ - ties_config[i].position_)
702 * 0.5 * details_.staff_space_;
704 copy.push_back (*ptr);
711 Tie_formatting_problem::generate_base_chord_configuration ()
713 Ties_configuration ties_config;
714 for (vsize i = 0; i < specifications_.size (); i ++)
716 Tie_configuration conf;
717 if (specifications_[i].has_manual_dir_)
718 conf.dir_ = specifications_[i].manual_dir_;
719 if (specifications_[i].has_manual_position_)
721 conf.position_ = (int) my_round (specifications_[i].manual_position_);
722 conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
723 * 0.5 * details_.staff_space_;
727 conf.position_ = specifications_[i].position_;
729 ties_config.push_back (conf);
732 set_ties_config_standard_directions (&ties_config);
733 for (vsize i = 0; i < ties_config.size (); i++)
734 if (!specifications_[i].manual_position_)
735 ties_config[i].position_ += ties_config[i].dir_;
737 ties_config = generate_ties_configuration (ties_config);
743 Tie_formatting_problem::find_best_variation (Ties_configuration const &base,
744 vector<Tie_configuration_variation> vars)
746 Ties_configuration best = base;
749 This simply is 1-opt: we have K substitions, and we try applying
750 exactly every one for each.
752 for (vsize i = 0; i < vars.size (); i++)
754 Ties_configuration variant (base);
755 variant[vars[i].index_] = *vars[i].suggestion_;
757 variant.reset_score ();
758 score_ties (&variant);
760 if (variant.score () < best.score ())
772 Tie_formatting_problem::generate_optimal_chord_configuration ()
775 Ties_configuration base = generate_base_chord_configuration ();
776 vector<Tie_configuration_variation> vars = generate_collision_variations (base);
779 Ties_configuration best = find_best_variation (base, vars);
780 vars = generate_extremal_tie_variations (best);
781 best = find_best_variation (best, vars);
787 Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
789 if (tie_configs->empty ())
792 if (!tie_configs->at (0).dir_)
793 tie_configs->at (0).dir_ = DOWN;
794 if (!tie_configs->back ().dir_)
795 tie_configs->back ().dir_ = UP;
800 for (vsize i = 1; i < tie_configs->size (); i++)
802 Real diff = (tie_configs->at (i-1).position_
803 - tie_configs->at (i).position_);
805 if (fabs (diff) <= 1)
807 if (!tie_configs->at (i-1).dir_)
808 tie_configs->at (i-1).dir_ = DOWN;
809 if (!tie_configs->at (i).dir_)
810 tie_configs->at (i).dir_ = UP;
814 for (vsize i = 1; i < tie_configs->size() - 1; i++)
816 Tie_configuration &conf = tie_configs->at (i);
820 Direction position_dir =
821 Direction (sign (conf.position_));
825 conf.dir_ = position_dir;
829 Tie_configuration_variation::Tie_configuration_variation ()
835 vector<Tie_configuration_variation>
836 Tie_formatting_problem::generate_extremal_tie_variations (Ties_configuration const &ties) const
838 vector<Tie_configuration_variation> vars;
842 if (boundary (ties, d, 0).dir_ == d
843 && !boundary (specifications_, d, 0).has_manual_position_)
844 for (int i = 1; i <= details_.multi_tie_region_size_; i++)
846 Tie_configuration_variation var;
847 var.index_ = (d == DOWN) ? 0 : ties.size () - 1;
848 var.suggestion_ = get_configuration (boundary (ties, d, 0).position_
850 vars.push_back (var);
853 while (flip (&d) != DOWN);
859 vector<Tie_configuration_variation>
860 Tie_formatting_problem::generate_collision_variations (Ties_configuration const &ties) const
862 Real center_distance_tolerance = 0.25;
864 vector<Tie_configuration_variation> vars;
865 Real last_center = 0.0;
866 for (vsize i = 0; i < ties.size (); i++)
868 Bezier b (ties[i].get_transformed_bezier (details_));
870 Real center = b.curve_point (0.5)[Y_AXIS];
874 if (center <= last_center + center_distance_tolerance)
876 if (!specifications_[i].has_manual_dir_)
878 Tie_configuration_variation var;
880 var.suggestion_ = get_configuration (specifications_[i].position_
884 vars.push_back (var);
887 if (!specifications_[i-1].has_manual_dir_)
889 Tie_configuration_variation var;
891 var.suggestion_ = get_configuration (specifications_[i-1].position_
895 vars.push_back (var);
898 if (i == 1 && !specifications_[i-1].has_manual_position_
899 && ties[i-1].dir_ == DOWN)
901 Tie_configuration_variation var;
903 var.suggestion_ = get_configuration (specifications_[i-1].position_
905 vars.push_back (var);
907 if (i == ties.size() && !specifications_[i].has_manual_position_
908 && ties[i].dir_ == UP)
910 Tie_configuration_variation var;
912 var.suggestion_ = get_configuration (specifications_[i].position_
914 vars.push_back (var);
917 else if (dot_positions_.find (ties[i].position_) != dot_positions_.end ()
918 && !specifications_[i].has_manual_position_)
920 Tie_configuration_variation var;
922 var.suggestion_ = get_configuration (ties[i].position_ + ties[i].dir_,
924 vars.push_back (var);
929 last_center = center;
937 Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs)
940 for (SCM s = manual_configs;
941 scm_is_pair (s) && k < specifications_.size (); s = scm_cdr (s))
943 SCM entry = scm_car (s);
944 if (scm_is_pair (entry))
946 Tie_specification &spec = specifications_[k];
948 if (scm_is_number (scm_car (entry)))
950 spec.has_manual_position_ = true;
951 spec.manual_position_ = scm_to_double (scm_car (entry));
953 if (scm_is_number (scm_cdr (entry)))
955 spec.has_manual_dir_ = true;
956 spec.manual_dir_ = Direction (scm_to_int (scm_cdr (entry)));