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 (Link_array<Item> bounds,
65 Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
70 for (int i = 0; i < bounds.size (); i++)
72 Grob *head = bounds[i];
73 if (!Note_head::has_interface (head))
77 stem = unsmob_grob (head->get_object ("stem"));
79 Real p = Staff_symbol_referencer::get_position (head);
80 Interval y ((p-1) * 0.5 * staff_space,
81 (p+1) * 0.5 * staff_space);
83 Interval x = head->extent (x_refpoint_, X_AXIS);
84 boxes.push (Box (x, y));
86 Grob *dots = Rhythmic_head::get_dots (head);
87 if (d == LEFT && dots)
89 Interval x = dots->extent (x_refpoint_, X_AXIS);
90 Interval y (-0.5, 0.5);
91 int p = int (Staff_symbol_referencer::get_position (dots));
94 dot_positions_.insert (p);
97 y *= staff_space * 0.5;
98 // boxes.push (Box (x, y));
102 chord_outlines_[d] = empty_skyline (-d);
104 if (bounds[0]->break_status_dir ())
106 Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-d];
107 chord_outlines_[d].elem_ref (0).height_ = x;
110 for (int i = 0; i < boxes.size (); i++)
111 insert_extent_into_skyline (&chord_outlines_[d] ,
112 boxes[i], Y_AXIS, -d);
115 && !Stem::is_invisible (stem))
118 x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS));
119 x.widen (staff_space / 20); // ugh.
121 y.add_point (Stem::stem_end_position (stem) * staff_space * .5);
123 Direction stemdir = get_grob_direction (stem);
124 y.add_point (Stem::head_positions (stem)[-stemdir]
127 insert_extent_into_skyline (&chord_outlines_[d], Box (x,y), Y_AXIS, -d);
129 stem_extents_[d].unite (Box (x,y));
133 Box flag_box = Stem::get_translated_flag (stem).extent_box ();
134 flag_box.translate( Offset (x[RIGHT], X_AXIS));
135 insert_extent_into_skyline (&chord_outlines_[d], flag_box,
140 Direction updowndir = DOWN;
147 Box b = boxes.boundary (updowndir, 0);
149 x[-d] = b[X_AXIS].linear_combination (-d / 2);
150 y[-updowndir] = b[Y_AXIS][updowndir];
151 y[updowndir] = updowndir * infinity_f;
155 insert_extent_into_skyline (&chord_outlines_[d],
159 while (flip (&updowndir) != DOWN);
164 Tie_formatting_problem::from_tie (Grob *tie)
166 Link_array<Grob> ties;
170 details_.from_grob (tie);
174 Tie_formatting_problem::common_x_refpoint () const
180 Tie_formatting_problem::from_ties (Link_array<Grob> const &ties)
182 if (ties.is_empty ())
185 x_refpoint_ = ties[0];
186 for (int i = 0; i < ties.size (); i++)
188 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS);
189 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS);
192 details_.from_grob (ties[0]);
197 Link_array<Item> bounds;
199 for (int i = 0; i < ties.size (); i++)
201 Item *it = dynamic_cast<Spanner*> (ties[i])->get_bound (d);
206 set_chord_outline (bounds, d);
208 while (flip (&d) != LEFT);
211 for (int i = 0; i < ties.size (); i++)
213 Tie_specification spec;
215 if (scm_is_number (ties[i]->get_property_data (ly_symbol2scm ("direction"))))
217 spec.manual_dir_ = to_dir (ties[i]->get_property ("direction"));
218 spec.has_manual_dir_ = true;
221 spec.position_ = Tie::get_position (ties[i]);
225 spec.note_head_drul_[d] = Tie::head (ties[i], d);
227 while (flip (&d) != LEFT);
229 specifications_.push (spec);
234 Tie_formatting_problem::from_lv_ties (Link_array<Grob> const &lv_ties)
236 if (lv_ties.is_empty ())
239 details_.from_grob (lv_ties[0]);
240 Link_array<Item> heads;
242 for (int i = 0; i < lv_ties.size (); i++)
244 Tie_specification spec;
245 Item *head = unsmob_item (lv_ties[i]->get_object ("note-head"));
248 programming_error ("LV tie without head?!");
252 spec.position_ = int (Staff_symbol_referencer::get_position (head));
255 spec.note_head_drul_[LEFT] = head;
257 specifications_.push (spec);
260 x_refpoint_ = lv_ties [0];
261 for (int i = 0; i < lv_ties.size (); i++)
263 x_refpoint_ = lv_ties[i]->common_refpoint (x_refpoint_, X_AXIS);
266 set_chord_outline (heads, LEFT);
268 Real right_most = - infinity_f;
270 for (int i = 0; i < chord_outlines_[LEFT].size (); i++)
272 right_most = max (right_most, chord_outlines_[LEFT][i].height_);
275 Skyline_entry right_entry;
276 right_entry.width_.set_full ();
277 right_entry.height_ = right_most + 1.5;
279 chord_outlines_[RIGHT].push (right_entry);
284 Tie_formatting_problem::get_tie_specification (int i) const
286 return specifications_[i];
291 Tie_formatting_problem::get_configuration (int pos, Direction dir)
293 pair<int,int> key (pos, dir);
294 Tie_configuration_map::const_iterator f = possibilities_.find (key);
296 if (f != possibilities_.end ())
302 Tie_configuration *conf = generate_configuration (pos,dir);
303 possibilities_[key] = conf;
308 Tie_formatting_problem::generate_configuration (int pos, Direction dir) const
310 Tie_configuration *conf = new Tie_configuration;
311 conf->position_ = pos;
313 Real y = conf->position_ * 0.5 * details_.staff_space_;
315 if (dot_positions_.find (pos) != dot_positions_.end ())
317 conf->delta_y_ += 0.25 * details_.staff_space_;
320 conf->attachment_x_ = get_attachment (y + conf->delta_y_);
322 Real h = conf->height (details_);
326 TODO: make sliding criterion?
328 if (h < details_.intra_space_threshold_ * 0.5 * details_.staff_space_)
330 if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos)
331 && fabs (pos) < 2 * Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_))
333 conf->center_tie_vertically (details_);
335 else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
337 conf->delta_y_ += dir *
338 details_.tip_staff_line_clearance_ * 0.5 * details_.staff_space_;
343 Real top_y = y + conf->delta_y_ + conf->dir_ * h;
344 Real top_pos = top_y / (0.5*details_.staff_space_);
345 int round_pos = int (my_round (top_pos));
347 /* TODO: should use other variable? */
348 Real clearance = details_.center_staff_line_clearance_;
349 if (fabs (top_pos - round_pos) < clearance
350 && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_,
353 Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_;
354 conf->delta_y_ = (new_y - top_y);
360 we don't recompute attachment_x_ to take changed Y (through
361 delta_Y) into account. Doing would make ties go into small holes between heads, which
362 means we get collisions with neighboring heads.
364 conf->attachment_x_.widen ( - details_.x_gap_);
369 Real y = conf->position_ * details_.staff_space_ * 0.5 + conf->delta_y_;
370 if (stem_extents_[d][X_AXIS].is_empty ()
371 || !stem_extents_[d][Y_AXIS].contains (y))
374 conf->attachment_x_[d] =
375 d* min (d * conf->attachment_x_[d],
376 d * (stem_extents_[d][X_AXIS][-d] - d * details_.stem_gap_));
378 while (flip (&d) != LEFT);
383 Tie_formatting_problem::score_aptitude (Tie_configuration const &conf,
384 Tie_specification const &spec) const
387 Real curve_y = conf.position_ * details_.staff_space_ * 0.5 + conf.delta_y_;
388 Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
389 if (sign (curve_y - tie_y) != conf.dir_)
390 penalty += details_.wrong_direction_offset_penalty_;
392 penalty += details_.vertical_distance_penalty_factor_ * fabs (curve_y - tie_y);
398 if (!spec.note_head_drul_[d])
401 Interval head_x = spec.note_head_drul_[d]->extent (x_refpoint_, X_AXIS);
402 Real dist = head_x.distance (conf.attachment_x_[d]);
403 penalty += details_.horizontal_distance_penalty_factor_ * dist;
405 while (flip (&d) != LEFT);
411 Tie_formatting_problem::score_configuration (Tie_configuration const &conf) const
414 Real length = conf.attachment_x_.length ();
415 if (length < details_.min_length_)
416 penalty += details_.length_penalty_factor_ / max (0.01, length);
418 Real tip_pos = conf.position_ + conf.delta_y_ / 0.5 * details_.staff_space_;
419 Real tip_y = tip_pos * details_.staff_space_ * 0.5;
420 Real height = conf.height (details_);
422 Real top_y = tip_y + conf.dir_ * height;
423 Real top_pos = 2 * top_y / details_.staff_space_;
424 Real round_top_pos = rint (top_pos);
425 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
427 && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
430 details_.staff_line_collision_penalty_
431 * peak_around (0.1 * details_.center_staff_line_clearance_,
432 details_.center_staff_line_clearance_,
433 fabs (top_pos - round_top_pos));
436 if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
437 int (rint (tip_pos))))
439 penalty += details_.staff_line_collision_penalty_
440 * peak_around (0.1 * details_.tip_staff_line_clearance_,
441 details_.tip_staff_line_clearance_,
442 fabs (tip_pos - rint (tip_pos)));
445 if (!dot_x_.is_empty ())
448 Real x = dot_x_.center ();
450 Bezier b = conf.get_transformed_bezier (details_);
451 if (b.control_point_extent (X_AXIS).contains (x))
453 Real y = b.get_other_coordinate (X_AXIS, x);
455 for (set<int>::const_iterator i (dot_positions_.begin ());
456 i != dot_positions_.end (); i ++)
460 details_.dot_collision_penalty_
461 * peak_around (.1 * details_.dot_collision_clearance_,
462 details_.dot_collision_clearance_,
463 fabs (dot_pos * details_.staff_space_ * 0.5 - y));
472 Tie_formatting_problem::find_optimal_tie_configuration (Tie_specification const &spec) const
474 Link_array<Tie_configuration> confs;
476 int pos = spec.position_;
477 Direction dir = spec.manual_dir_;
480 for (int i = 0; i < region_size; i ++)
482 confs.push (generate_configuration (pos + i * dir, dir));
488 Real best_score = 1e6;
489 for (int i = 0; i < confs.size (); i ++)
492 score += score_configuration (*confs[i]);
493 score += score_aptitude (*confs[i], spec);
495 if (score < best_score)
502 Tie_configuration best = *confs[best_idx];
503 for (int i = 0; i < confs.size (); i++)
509 Tie_specification::Tie_specification ()
511 has_manual_position_ = false;
512 has_manual_dir_ = false;
514 manual_position_ = 0;
515 manual_dir_ = CENTER;
516 note_head_drul_[LEFT] =
517 note_head_drul_[RIGHT] = 0;
522 Tie_formatting_problem::score_ties_aptitude (Ties_configuration const &ties) const
525 if (ties.size () != specifications_.size ())
527 programming_error ("Huh? Mismatch between sizes.");
531 for (int i = 0; i < ties.size (); i++)
532 score += score_aptitude (ties[i], specifications_[i]);
538 Tie_formatting_problem::score_ties (Ties_configuration const &ties) const
540 return score_ties_configuration (ties)
541 + score_ties_aptitude (ties);
545 Tie_formatting_problem::score_ties_configuration (Ties_configuration const &ties) const
548 for (int i = 0; i < ties.size (); i++)
550 score += score_configuration (ties[i]);
554 Real last_edge = 0.0;
555 Real last_center = 0.0;
556 for (int i = 0; i < ties.size (); i++)
558 Bezier b (ties[i].get_transformed_bezier (details_));
560 Real center = b.curve_point (0.5)[Y_AXIS];
561 Real edge = b.curve_point (0.0)[Y_AXIS];
565 if (edge <= last_edge)
566 score += details_.tie_column_monotonicity_penalty_;
567 if (center <= last_center)
568 score += details_.tie_column_monotonicity_penalty_;
571 details_.tie_tie_collision_penalty_ *
572 peak_around (0.1 * details_.tie_tie_collision_distance_,
573 details_.tie_tie_collision_distance_,
574 fabs (center - last_center));
576 details_.tie_tie_collision_penalty_ *
577 peak_around (0.1 * details_.tie_tie_collision_distance_,
578 details_.tie_tie_collision_distance_,
579 fabs (edge - last_edge));
583 last_center = center;
590 Generate with correct X-attachments and beziers, copying delta_y_
591 from TIES_CONFIG if necessary.
594 Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
596 Ties_configuration copy;
597 for (int i = 0; i < ties_config.size (); i++)
599 Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_);
600 if (specifications_[i].has_manual_position_)
603 = (specifications_[i].manual_position_ - ties_config[i].position_)
604 * 0.5 * details_.staff_space_;
613 Tie_formatting_problem::generate_base_chord_configuration ()
615 Ties_configuration ties_config;
616 for (int i = 0; i < specifications_.size (); i ++)
618 Tie_configuration conf;
619 if (specifications_[i].has_manual_dir_)
620 conf.dir_ = specifications_[i].manual_dir_;
621 if (specifications_[i].has_manual_position_)
623 conf.position_ = (int) my_round (specifications_[i].manual_position_);
624 conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
625 * 0.5 * details_.staff_space_;
629 conf.position_ = specifications_[i].position_;
631 ties_config.push (conf);
634 set_ties_config_standard_directions (&ties_config);
635 for (int i = 0; i < ties_config.size (); i++)
636 if (!specifications_[i].manual_position_)
637 ties_config[i].position_ += ties_config[i].dir_;
639 ties_config = generate_ties_configuration (ties_config);
645 Tie_formatting_problem::generate_optimal_chord_configuration ()
647 Ties_configuration base = generate_base_chord_configuration ();
648 Array<Tie_configuration_variation> vars = get_variations (base);
650 Ties_configuration best = base;
651 Real best_score = score_ties (best);
654 This simply is 1-opt: we have K substitions, and we try applying
655 exactly every one for each.
657 for (int i = 0; i < vars.size (); i++)
659 Ties_configuration variant = base;
660 variant[vars[i].index_] = *vars[i].suggestion_;
662 Real score = score_ties (variant);
663 if (score < best_score)
674 Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
676 if (tie_configs->is_empty ())
679 if (!tie_configs->elem (0).dir_)
680 tie_configs->elem_ref (0).dir_ = DOWN;
681 if (!tie_configs->top().dir_)
682 tie_configs->top().dir_ = UP;
687 for (int i = 1; i < tie_configs->size (); i++)
689 Real diff = (tie_configs->elem (i-1).position_
690 - tie_configs->elem (i).position_);
692 if (fabs (diff) <= 1)
694 if (!tie_configs->elem (i-1).dir_)
695 tie_configs->elem_ref (i-1).dir_ = DOWN;
696 if (!tie_configs->elem (i).dir_)
697 tie_configs->elem_ref (i).dir_ = UP;
701 for (int i = 1; i < tie_configs->size() - 1; i++)
703 Tie_configuration &conf = tie_configs->elem_ref (i);
707 Direction position_dir =
708 Direction (sign (conf.position_));
712 conf.dir_ = position_dir;
716 Tie_configuration_variation::Tie_configuration_variation ()
722 Array<Tie_configuration_variation>
723 Tie_formatting_problem::get_variations (Ties_configuration const &ties)
725 Real center_distance_tolerance = 0.25;
727 Array<Tie_configuration_variation> vars;
728 Real last_center = 0.0;
729 for (int i = 0; i < ties.size (); i++)
731 Bezier b (ties[i].get_transformed_bezier (details_));
733 Real center = b.curve_point (0.5)[Y_AXIS];
737 if (center <= last_center + center_distance_tolerance)
739 if (!specifications_[i].has_manual_dir_)
741 Tie_configuration_variation var;
743 var.suggestion_ = get_configuration (specifications_[i].position_
750 if (!specifications_[i-1].has_manual_dir_)
752 Tie_configuration_variation var;
754 var.suggestion_ = get_configuration (specifications_[i-1].position_
763 last_center = center;
766 /* TODO: switch off? */
770 if (ties.boundary (d, 0).dir_ == d)
772 Tie_configuration_variation var;
773 var.index_ = (d == DOWN) ? 0 : ties.size () - 1;
774 var.suggestion_ = get_configuration (ties.boundary (d, 0).position_ + d,
779 while (flip (&d) != DOWN);
785 Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs)
788 for (SCM s = manual_configs;
789 scm_is_pair (s) && k < specifications_.size(); s = scm_cdr (s))
791 SCM entry = scm_car (s);
792 if (!scm_is_pair (entry))
795 Tie_specification &spec = specifications_[k];
797 if (scm_is_number (scm_cdr (entry)))
799 spec.has_manual_dir_ = true;
800 spec.manual_dir_ = Direction (scm_to_int (scm_cdr (entry)));
802 if (scm_is_number (scm_car (entry)))
804 spec.has_manual_position_ = true;
805 spec.manual_position_ = scm_to_double (scm_car (entry));