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"
16 #include "note-head.hh"
17 #include "rhythmic-head.hh"
19 #include "staff-symbol-referencer.hh"
21 #include "tie-configuration.hh"
26 0 at threshold, 1 at 0, with 1/x falloff.
28 Real peak_around (Real epsilon, Real threshold, Real x)
32 return max (- epsilon * (x - threshold) / ((x + epsilon) * threshold), 0.0);
36 Tie_formatting_problem::get_attachment (Real y) const
42 attachments[d] = skyline_height (chord_outlines_[d], y, -d);
44 while (flip (&d) != LEFT);
49 Tie_formatting_problem::Tie_formatting_problem()
54 Tie_formatting_problem::~Tie_formatting_problem ()
56 for (Tie_configuration_map::const_iterator i (possibilities_.begin ());
57 i != possibilities_.end (); i++)
62 Tie_formatting_problem::set_chord_outline (vector<Item*> bounds,
65 Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
68 vector<Box> head_boxes;
71 for (vsize i = 0; i < bounds.size (); i++)
73 Grob *head = bounds[i];
74 if (!Note_head::has_interface (head))
78 stem = unsmob_grob (head->get_object ("stem"));
80 Real p = Staff_symbol_referencer::get_position (head);
81 Interval y ((p-1) * 0.5 * staff_space,
82 (p+1) * 0.5 * staff_space);
84 Interval x = head->extent (x_refpoint_, X_AXIS);
85 head_boxes.push_back (Box (x, y));
86 boxes.push_back (Box (x, y));
88 Grob *dots = Rhythmic_head::get_dots (head);
89 if (d == LEFT && dots)
91 Interval x = dots->extent (x_refpoint_, X_AXIS);
92 int p = int (Staff_symbol_referencer::get_position (dots));
94 dot_positions_.insert (p);
97 Interval y (dots->extent (dots, Y_AXIS));
98 y.translate (p * staff_space * 0.5);
100 boxes.push_back (Box (x, y));
104 chord_outlines_[d] = empty_skyline (-d);
105 if (bounds[0]->break_status_dir ())
107 Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-d];
108 chord_outlines_[d].at (0).height_ = x;
111 for (vsize i = 0; i < boxes.size (); i++)
112 insert_extent_into_skyline (&chord_outlines_[d] ,
113 boxes[i], Y_AXIS, -d);
116 && !Stem::is_invisible (stem))
119 x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS));
120 x.widen (staff_space / 20); // ugh.
122 y.add_point (Stem::stem_end_position (stem) * staff_space * .5);
124 Direction stemdir = get_grob_direction (stem);
125 y.add_point (Stem::head_positions (stem)[-stemdir]
128 insert_extent_into_skyline (&chord_outlines_[d], Box (x,y), Y_AXIS, -d);
130 stem_extents_[d].unite (Box (x,y));
134 Box flag_box = Stem::get_translated_flag (stem).extent_box ();
135 flag_box.translate( Offset (x[RIGHT], X_AXIS));
136 insert_extent_into_skyline (&chord_outlines_[d], flag_box,
141 Direction updowndir = DOWN;
146 if (head_boxes.size())
148 Box b = boundary (head_boxes, updowndir, 0);
150 x[-d] = b[X_AXIS].linear_combination (-d / 2);
151 y[-updowndir] = b[Y_AXIS][updowndir];
152 y[updowndir] = updowndir * infinity_f;
156 insert_extent_into_skyline (&chord_outlines_[d],
160 while (flip (&updowndir) != DOWN);
165 Tie_formatting_problem::from_tie (Grob *tie)
168 ties.push_back (tie);
171 details_.from_grob (tie);
175 Tie_formatting_problem::common_x_refpoint () const
181 Tie_formatting_problem::from_ties (vector<Grob*> const &ties)
186 x_refpoint_ = ties[0];
187 for (vsize i = 0; i < ties.size (); i++)
189 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS);
190 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS);
193 details_.from_grob (ties[0]);
198 vector<Item*> bounds;
200 for (vsize i = 0; i < ties.size (); i++)
202 Item *it = dynamic_cast<Spanner*> (ties[i])->get_bound (d);
204 bounds.push_back (it);
207 set_chord_outline (bounds, d);
209 while (flip (&d) != LEFT);
212 for (vsize i = 0; i < ties.size (); i++)
214 Tie_specification spec;
216 if (scm_is_number (ties[i]->get_property_data (ly_symbol2scm ("direction"))))
218 spec.manual_dir_ = to_dir (ties[i]->get_property ("direction"));
219 spec.has_manual_dir_ = true;
222 spec.position_ = Tie::get_position (ties[i]);
226 spec.note_head_drul_[d] = Tie::head (ties[i], d);
228 while (flip (&d) != LEFT);
230 specifications_.push_back (spec);
235 Tie_formatting_problem::from_lv_ties (vector<Grob*> const &lv_ties)
237 if (lv_ties.empty ())
240 details_.from_grob (lv_ties[0]);
243 for (vsize i = 0; i < lv_ties.size (); i++)
245 Tie_specification spec;
246 Item *head = unsmob_item (lv_ties[i]->get_object ("note-head"));
249 programming_error ("LV tie without head?!");
253 spec.position_ = int (Staff_symbol_referencer::get_position (head));
256 spec.note_head_drul_[LEFT] = head;
257 heads.push_back (head);
258 specifications_.push_back (spec);
261 x_refpoint_ = lv_ties [0];
262 for (vsize i = 0; i < lv_ties.size (); i++)
263 x_refpoint_ = lv_ties[i]->common_refpoint (x_refpoint_, X_AXIS);
264 for (vsize i = 0; i < heads.size (); i++)
265 x_refpoint_ = heads[i]->common_refpoint (x_refpoint_, X_AXIS);
267 set_chord_outline (heads, LEFT);
269 Real right_most = - infinity_f;
271 for (vsize i = 0; i < chord_outlines_[LEFT].size (); i++)
273 right_most = max (right_most, chord_outlines_[LEFT][i].height_);
276 Skyline_entry right_entry;
277 right_entry.width_.set_full ();
278 right_entry.height_ = right_most + 1.5;
280 chord_outlines_[RIGHT].push_back (right_entry);
285 Tie_formatting_problem::get_tie_specification (int i) const
287 return specifications_[i];
292 Tie_formatting_problem::get_configuration (int pos, Direction dir)
294 pair<int,int> key (pos, dir);
295 Tie_configuration_map::const_iterator f = possibilities_.find (key);
297 if (f != possibilities_.end ())
303 Tie_configuration *conf = generate_configuration (pos, dir);
304 possibilities_[key] = conf;
309 Tie_formatting_problem::generate_configuration (int pos, Direction dir) const
311 Tie_configuration *conf = new Tie_configuration;
312 conf->position_ = pos;
314 Real y = conf->position_ * 0.5 * details_.staff_space_;
316 if (dot_positions_.find (pos) != dot_positions_.end ())
318 conf->delta_y_ += 0.25 * details_.staff_space_;
321 conf->attachment_x_ = get_attachment (y + conf->delta_y_);
323 Real h = conf->height (details_);
325 if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
328 This is less sensible for long ties, since those are more
331 Interval close_by = get_attachment (y
333 + (dir * details_.intra_space_threshold_ * 0.25
334 * details_.staff_space_));
336 conf->attachment_x_.intersect (close_by);
344 - should make sliding criterion, should flatten ties if
346 - they're just the wrong (ie. touching line at top & bottom)
350 if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
352 if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)
353 && abs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
355 conf->center_tie_vertically (details_);
357 else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
359 conf->delta_y_ += dir *
360 details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_;
365 Real top_y = y + conf->delta_y_ + conf->dir_ * h;
366 Real top_pos = top_y / (0.5*details_.staff_space_);
367 int round_pos = int (my_round (top_pos));
369 /* TODO: should use other variable? */
370 Real clearance = details_.center_staff_line_clearance_;
371 if (fabs (top_pos - round_pos) < clearance
372 && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_,
375 Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_;
376 conf->delta_y_ = (new_y - top_y);
382 we don't recompute attachment_x_ to take changed Y (through
383 delta_Y) into account. Doing would make ties go into small holes between heads, which
384 means we get collisions with neighboring heads.
386 conf->attachment_x_.widen ( - details_.x_gap_);
391 Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
392 if (stem_extents_[d][X_AXIS].is_empty ()
393 || !stem_extents_[d][Y_AXIS].contains (y))
396 conf->attachment_x_[d] =
397 d* min (d * conf->attachment_x_[d],
398 d * (stem_extents_[d][X_AXIS][-d] - d * details_.stem_gap_));
400 while (flip (&d) != LEFT);
405 Tie_formatting_problem::score_aptitude (Tie_configuration const &conf,
406 Tie_specification const &spec) const
409 Real curve_y = conf.position_ * details_.staff_space_ * 0.5 + conf.delta_y_;
410 Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
411 if (sign (curve_y - tie_y) != conf.dir_)
412 penalty += details_.wrong_direction_offset_penalty_;
414 penalty += details_.vertical_distance_penalty_factor_ * fabs (curve_y - tie_y);
420 if (!spec.note_head_drul_[d])
423 Interval head_x = spec.note_head_drul_[d]->extent (x_refpoint_, X_AXIS);
424 Real dist = head_x.distance (conf.attachment_x_[d]);
425 penalty += details_.horizontal_distance_penalty_factor_ * dist;
427 while (flip (&d) != LEFT);
433 Tie_formatting_problem::score_configuration (Tie_configuration const &conf) const
436 Real length = conf.attachment_x_.length ();
437 if (length < details_.min_length_)
438 penalty += details_.length_penalty_factor_ / max (0.01, length);
440 Real tip_pos = conf.position_ + conf.delta_y_ / 0.5 * details_.staff_space_;
441 Real tip_y = tip_pos * details_.staff_space_ * 0.5;
442 Real height = conf.height (details_);
444 Real top_y = tip_y + conf.dir_ * height;
445 Real top_pos = 2 * top_y / details_.staff_space_;
446 Real round_top_pos = rint (top_pos);
447 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
449 && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
452 details_.staff_line_collision_penalty_
453 * peak_around (0.1 * details_.center_staff_line_clearance_,
454 details_.center_staff_line_clearance_,
455 fabs (top_pos - round_top_pos));
458 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
459 int (rint (tip_pos))))
461 penalty += details_.staff_line_collision_penalty_
462 * peak_around (0.1 * details_.tip_staff_line_clearance_,
463 details_.tip_staff_line_clearance_,
464 fabs (tip_pos - rint (tip_pos)));
467 if (!dot_x_.is_empty ())
470 Real x = dot_x_.center ();
472 Bezier b = conf.get_transformed_bezier (details_);
473 if (b.control_point_extent (X_AXIS).contains (x))
475 Real y = b.get_other_coordinate (X_AXIS, x);
477 for (set<int>::const_iterator i (dot_positions_.begin ());
478 i != dot_positions_.end (); i ++)
482 details_.dot_collision_penalty_
483 * peak_around (.1 * details_.dot_collision_clearance_,
484 details_.dot_collision_clearance_,
485 fabs (dot_pos * details_.staff_space_ * 0.5 - y));
494 Tie_formatting_problem::find_optimal_tie_configuration (Tie_specification const &spec) const
496 vector<Tie_configuration*> confs;
498 int pos = spec.position_;
499 Direction dir = spec.manual_dir_;
502 for (int i = 0; i < region_size; i ++)
504 confs.push_back (generate_configuration (pos + i * dir, dir));
510 Real best_score = 1e6;
511 for (vsize i = 0; i < confs.size (); i ++)
514 score += score_configuration (*confs[i]);
515 score += score_aptitude (*confs[i], spec);
517 if (score < best_score)
524 Tie_configuration best = *confs[best_idx];
525 for (vsize i = 0; i < confs.size (); i++)
531 Tie_specification::Tie_specification ()
533 has_manual_position_ = false;
534 has_manual_dir_ = false;
536 manual_position_ = 0;
537 manual_dir_ = CENTER;
538 note_head_drul_[LEFT] =
539 note_head_drul_[RIGHT] = 0;
544 Tie_formatting_problem::score_ties_aptitude (Ties_configuration const &ties) const
547 if (ties.size () != specifications_.size ())
549 programming_error ("Huh? Mismatch between sizes.");
553 for (vsize i = 0; i < ties.size (); i++)
554 score += score_aptitude (ties[i], specifications_[i]);
560 Tie_formatting_problem::score_ties (Ties_configuration const &ties) const
562 return score_ties_configuration (ties)
563 + score_ties_aptitude (ties);
567 Tie_formatting_problem::score_ties_configuration (Ties_configuration const &ties) const
570 for (vsize i = 0; i < ties.size (); i++)
572 score += score_configuration (ties[i]);
575 Real last_edge = 0.0;
576 Real last_center = 0.0;
577 for (vsize i = 0; i < ties.size (); i++)
579 Bezier b (ties[i].get_transformed_bezier (details_));
581 Real center = b.curve_point (0.5)[Y_AXIS];
582 Real edge = b.curve_point (0.0)[Y_AXIS];
586 if (edge <= last_edge)
587 score += details_.tie_column_monotonicity_penalty_;
588 if (center <= last_center)
589 score += details_.tie_column_monotonicity_penalty_;
592 details_.tie_tie_collision_penalty_ *
593 peak_around (0.1 * details_.tie_tie_collision_distance_,
594 details_.tie_tie_collision_distance_,
595 fabs (center - last_center));
597 details_.tie_tie_collision_penalty_ *
598 peak_around (0.1 * details_.tie_tie_collision_distance_,
599 details_.tie_tie_collision_distance_,
600 fabs (edge - last_edge));
604 last_center = center;
609 details_.outer_tie_length_symmetry_penalty_factor_
610 * fabs (ties[0].attachment_x_.length () - ties.back ().attachment_x_.length ());
613 details_.outer_tie_vertical_distance_symmetry_penalty_factor_
614 * (fabs (specifications_[0].position_
615 - (ties[0].position_ * 0.5 * details_.staff_space_ + ties[0].delta_y_))
617 fabs (specifications_.back ().position_
618 - (ties.back ().position_ * 0.5 * details_.staff_space_ + ties.back ().delta_y_)));
624 Generate with correct X-attachments and beziers, copying delta_y_
625 from TIES_CONFIG if necessary.
628 Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
630 Ties_configuration copy;
631 for (vsize i = 0; i < ties_config.size (); i++)
633 Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_);
634 if (specifications_[i].has_manual_position_)
637 = (specifications_[i].manual_position_ - ties_config[i].position_)
638 * 0.5 * details_.staff_space_;
640 copy.push_back (*ptr);
647 Tie_formatting_problem::generate_base_chord_configuration ()
649 Ties_configuration ties_config;
650 for (vsize i = 0; i < specifications_.size (); i ++)
652 Tie_configuration conf;
653 if (specifications_[i].has_manual_dir_)
654 conf.dir_ = specifications_[i].manual_dir_;
655 if (specifications_[i].has_manual_position_)
657 conf.position_ = (int) my_round (specifications_[i].manual_position_);
658 conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
659 * 0.5 * details_.staff_space_;
663 conf.position_ = specifications_[i].position_;
665 ties_config.push_back (conf);
668 set_ties_config_standard_directions (&ties_config);
669 for (vsize i = 0; i < ties_config.size (); i++)
670 if (!specifications_[i].manual_position_)
671 ties_config[i].position_ += ties_config[i].dir_;
673 ties_config = generate_ties_configuration (ties_config);
679 Tie_formatting_problem::generate_optimal_chord_configuration ()
681 Ties_configuration base = generate_base_chord_configuration ();
682 vector<Tie_configuration_variation> vars = get_variations (base);
684 Ties_configuration best = base;
685 Real best_score = score_ties (best);
688 This simply is 1-opt: we have K substitions, and we try applying
689 exactly every one for each.
691 for (vsize i = 0; i < vars.size (); i++)
693 Ties_configuration variant = base;
694 variant[vars[i].index_] = *vars[i].suggestion_;
696 Real score = score_ties (variant);
697 if (score < best_score)
708 Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
710 if (tie_configs->empty ())
713 if (!tie_configs->at (0).dir_)
714 tie_configs->at (0).dir_ = DOWN;
715 if (!tie_configs->back ().dir_)
716 tie_configs->back ().dir_ = UP;
721 for (vsize i = 1; i < tie_configs->size (); i++)
723 Real diff = (tie_configs->at (i-1).position_
724 - tie_configs->at (i).position_);
726 if (fabs (diff) <= 1)
728 if (!tie_configs->at (i-1).dir_)
729 tie_configs->at (i-1).dir_ = DOWN;
730 if (!tie_configs->at (i).dir_)
731 tie_configs->at (i).dir_ = UP;
735 for (vsize i = 1; i < tie_configs->size() - 1; i++)
737 Tie_configuration &conf = tie_configs->at (i);
741 Direction position_dir =
742 Direction (sign (conf.position_));
746 conf.dir_ = position_dir;
750 Tie_configuration_variation::Tie_configuration_variation ()
756 vector<Tie_configuration_variation>
757 Tie_formatting_problem::get_variations (Ties_configuration const &ties)
759 Real center_distance_tolerance = 0.25;
761 vector<Tie_configuration_variation> vars;
762 Real last_center = 0.0;
763 for (vsize i = 0; i < ties.size (); i++)
765 Bezier b (ties[i].get_transformed_bezier (details_));
767 Real center = b.curve_point (0.5)[Y_AXIS];
771 if (center <= last_center + center_distance_tolerance)
773 if (!specifications_[i].has_manual_dir_)
775 Tie_configuration_variation var;
777 var.suggestion_ = get_configuration (specifications_[i].position_
781 vars.push_back (var);
784 if (!specifications_[i-1].has_manual_dir_)
786 Tie_configuration_variation var;
788 var.suggestion_ = get_configuration (specifications_[i-1].position_
792 vars.push_back (var);
795 else if (dot_positions_.find (ties[i].position_) != dot_positions_.end ()
796 && !specifications_[i].has_manual_position_)
798 Tie_configuration_variation var;
800 var.suggestion_ = get_configuration (ties[i].position_ + ties[i].dir_,
802 vars.push_back (var);
807 last_center = center;
810 /* TODO: switch off? */
814 if (boundary (ties, d, 0).dir_ == d)
816 Tie_configuration_variation var;
817 var.index_ = (d == DOWN) ? 0 : ties.size () - 1;
818 var.suggestion_ = get_configuration (boundary (ties, d, 0).position_
820 vars.push_back (var);
823 while (flip (&d) != DOWN);
829 Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs)
832 for (SCM s = manual_configs;
833 scm_is_pair (s) && k < specifications_.size (); s = scm_cdr (s))
835 SCM entry = scm_car (s);
836 if (scm_is_pair (entry))
838 Tie_specification &spec = specifications_[k];
840 if (scm_is_number (scm_car (entry)))
842 spec.has_manual_position_ = true;
843 spec.manual_position_ = scm_to_double (scm_car (entry));
845 if (scm_is_number (scm_cdr (entry)))
847 spec.has_manual_dir_ = true;
848 spec.manual_dir_ = Direction (scm_to_int (scm_cdr (entry)));