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 Tie_formatting_problem::get_attachment (Real y) const
32 attachments[d] = skyline_height (chord_outlines_[d], y, -d);
34 while (flip (&d) != LEFT);
39 Tie_formatting_problem::Tie_formatting_problem()
44 Tie_formatting_problem::~Tie_formatting_problem ()
46 for (Tie_configuration_map::const_iterator i (possibilities_.begin ());
47 i != possibilities_.end (); i++)
52 Tie_formatting_problem::set_chord_outline (Link_array<Item> bounds,
55 Real staff_space = Staff_symbol_referencer::staff_space (bounds[0]);
60 for (int i = 0; i < bounds.size (); i++)
62 Grob *head = bounds[i];
63 if (!Note_head::has_interface (head))
67 stem = unsmob_grob (head->get_object ("stem"));
69 Real p = Staff_symbol_referencer::get_position (head);
70 Interval y ((p-1) * 0.5 * staff_space,
71 (p+1) * 0.5 * staff_space);
73 Interval x = head->extent (x_refpoint_, X_AXIS);
74 boxes.push (Box (x, y));
76 Grob *dots = Rhythmic_head::get_dots (head);
77 if (d == LEFT && dots)
79 Interval x = dots->extent (x_refpoint_, X_AXIS);
80 Interval y (-0.5, 0.5);
81 int p = int (Staff_symbol_referencer::get_position (dots));
84 dot_positions_.insert (p);
87 y *= staff_space * 0.5;
88 // boxes.push (Box (x, y));
92 chord_outlines_[d] = empty_skyline (-d);
94 if (bounds[0]->break_status_dir ())
96 Real x = robust_relative_extent (bounds[0], x_refpoint_, X_AXIS)[-d];
97 chord_outlines_[d].elem_ref (0).height_ = x;
100 for (int i = 0; i < boxes.size (); i++)
101 insert_extent_into_skyline (&chord_outlines_[d] ,
102 boxes[i], Y_AXIS, -d);
105 && !Stem::is_invisible (stem))
108 x.add_point (stem->relative_coordinate (x_refpoint_, X_AXIS));
109 x.widen (staff_space / 20); // ugh.
111 y.add_point (Stem::stem_end_position (stem) * staff_space * .5);
113 Direction stemdir = get_grob_direction (stem);
114 y.add_point (Stem::head_positions (stem)[-stemdir]
117 insert_extent_into_skyline (&chord_outlines_[d], Box (x,y), Y_AXIS, -d);
123 Box flag_box = Stem::get_translated_flag (stem).extent_box ();
124 flag_box.translate( Offset (x[RIGHT], X_AXIS));
125 insert_extent_into_skyline (&chord_outlines_[d], flag_box,
130 Direction updowndir = DOWN;
137 Box b = boxes.boundary (updowndir, 0);
139 x[-d] = b[X_AXIS].linear_combination (-d / 2);
140 y[-updowndir] = b[Y_AXIS][updowndir];
141 y[updowndir] = updowndir * infinity_f;
145 insert_extent_into_skyline (&chord_outlines_[d],
149 while (flip (&updowndir) != DOWN);
154 Tie_formatting_problem::from_tie (Grob *tie)
156 Link_array<Grob> ties;
161 details_.from_grob (tie);
165 Tie_formatting_problem::common_x_refpoint () const
171 Tie_formatting_problem::from_ties (Link_array<Grob> const &ties)
173 if (ties.is_empty ())
176 x_refpoint_ = ties[0];
177 for (int i = 0; i < ties.size (); i++)
179 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (LEFT)->common_refpoint (x_refpoint_, X_AXIS);
180 x_refpoint_ = dynamic_cast<Spanner*> (ties[i])->get_bound (RIGHT)->common_refpoint (x_refpoint_, X_AXIS);
183 details_.from_grob (ties[0]);
188 Link_array<Item> bounds;
190 for (int i = 0; i < ties.size (); i++)
192 Item *it = dynamic_cast<Spanner*> (ties[i])->get_bound (d);
197 set_chord_outline (bounds, d);
199 while (flip (&d) != LEFT);
202 for (int i = 0; i < ties.size (); i++)
204 Tie_specification spec;
206 if (scm_is_number (ties[i]->get_property_data (ly_symbol2scm ("direction"))))
208 spec.manual_dir_ = to_dir (ties[i]->get_property ("direction"));
209 spec.has_manual_dir_ = true;
212 spec.position_ = Tie::get_position (ties[i]);
216 spec.note_head_drul_[d] = Tie::head (ties[i], d);
218 while (flip (&d) != LEFT);
220 specifications_.push (spec);
225 Tie_formatting_problem::from_lv_ties (Link_array<Grob> const &lv_ties)
227 if (lv_ties.is_empty ())
230 details_.from_grob (lv_ties[0]);
231 Link_array<Item> heads;
233 for (int i = 0; i < lv_ties.size (); i++)
235 Tie_specification spec;
236 Item *head = unsmob_item (lv_ties[i]->get_object ("note-head"));
239 programming_error ("LV tie without head?!");
243 spec.position_ = int (Staff_symbol_referencer::get_position (head));
246 spec.note_head_drul_[LEFT] = head;
248 specifications_.push (spec);
251 x_refpoint_ = lv_ties [0];
252 for (int i = 0; i < lv_ties.size (); i++)
254 x_refpoint_ = lv_ties[i]->common_refpoint (x_refpoint_, X_AXIS);
257 set_chord_outline (heads, LEFT);
259 Real right_most = - infinity_f;
261 for (int i = 0; i < chord_outlines_[LEFT].size (); i++)
263 right_most = max (right_most, chord_outlines_[LEFT][i].height_);
266 Skyline_entry right_entry;
267 right_entry.width_.set_full ();
268 right_entry.height_ = right_most + 1.5;
270 chord_outlines_[RIGHT].push (right_entry);
275 Tie_formatting_problem::get_tie_specification (int i) const
277 return specifications_[i];
282 Tie_formatting_problem::get_configuration (int pos, Direction dir)
284 pair<int,int> key (pos, dir);
285 Tie_configuration_map::const_iterator f = possibilities_.find (key);
287 if (f != possibilities_.end ())
293 Tie_configuration *conf = generate_configuration (pos,dir);
294 possibilities_[key] = conf;
299 Tie_formatting_problem::generate_configuration (int pos, Direction dir) const
301 Tie_configuration *conf = new Tie_configuration;
302 conf->position_ = pos;
304 Real y = conf->position_ * 0.5 * details_.staff_space_;
306 if (dot_positions_.find (pos) != dot_positions_.end ())
308 conf->delta_y_ += 0.25 * details_.staff_space_;
311 conf->attachment_x_ = get_attachment (y + conf->delta_y_);
313 Real h = conf->height (details_);
316 if (h < 0.5 * details_.staff_space_)
318 if (!Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
320 conf->center_tie_vertically (details_);
322 else if (Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_, pos))
324 conf->delta_y_ += dir * 0.2 * details_.staff_space_;
329 Real top_y = y + conf->delta_y_ + conf->dir_ * h;
330 Real top_pos = top_y / (0.5*details_.staff_space_);
331 int round_pos = int (my_round (top_pos));
333 /* TODO: should use other variable? */
334 Real clearance = details_.staff_line_clearance_;
335 if (fabs (top_pos - round_pos) < clearance
336 && Staff_symbol_referencer::on_staff_line (details_.staff_symbol_referencer_,
339 Real new_y = (round_pos + clearance * conf->dir_) * 0.5 * details_.staff_space_;
340 conf->delta_y_ = (new_y - top_y);
345 conf->attachment_x_.widen ( - details_.x_gap_);
350 Tie_formatting_problem::score_aptitude (Tie_configuration const &conf,
351 Tie_specification const &spec) const
354 Real curve_y = conf.position_ * details_.staff_space_ * 0.5 + conf.delta_y_;
355 Real tie_y = spec.position_ * details_.staff_space_ * 0.5;
356 if (sign (curve_y - tie_y) != conf.dir_)
357 penalty += details_.wrong_direction_offset_penalty_;
359 penalty += details_.distance_penalty_factor_ * fabs (curve_y - tie_y);
364 Tie_formatting_problem::score_configuration (Tie_configuration const &conf) const
367 Real length = conf.attachment_x_.length ();
368 if (length < details_.min_length_)
369 penalty += details_.length_penalty_factor_ / max (0.01, length);
371 Real tip_pos = conf.position_ + conf.delta_y_ / 0.5 * details_.staff_space_;
372 Real tip_y = tip_pos * details_.staff_space_ * 0.5;
373 Real height = conf.height (details_);
375 Real top_y = tip_y + conf.dir_ * height;
376 Real top_pos = 2 * top_y / details_.staff_space_;
377 Real round_top_pos = rint (top_pos);
378 if (fabs (top_pos - round_top_pos) < details_.staff_line_clearance_
379 && Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
381 && Staff_symbol_referencer::staff_radius (details_.staff_symbol_referencer_) > top_y)
383 penalty += details_.staff_line_collision_penalty_;
386 if (fabs (tip_pos - rint (tip_pos)) < details_.staff_line_clearance_
387 && Staff_symbol_referencer::on_line (details_.staff_symbol_referencer_,
388 int (rint (tip_pos))))
390 penalty += details_.staff_line_collision_penalty_;
393 if (!dot_x_.is_empty ())
396 Real x = dot_x_.center ();
398 Bezier b = conf.get_transformed_bezier (details_);
399 if (b.control_point_extent (X_AXIS).contains (x))
401 Real y = b.get_other_coordinate (X_AXIS, x);
403 for (set<int>::const_iterator i (dot_positions_.begin ());
404 i != dot_positions_.end (); i ++)
407 if (fabs (dot_pos * details_.staff_space_ * 0.5 - y) < details_.dot_collision_clearance_)
409 penalty += details_.dot_collision_penalty_;
419 Tie_formatting_problem::find_optimal_tie_configuration (Tie_specification const &spec) const
421 Link_array<Tie_configuration> confs;
423 int pos = spec.position_;
424 Direction dir = spec.manual_dir_;
427 for (int i = 0; i < region_size; i ++)
429 confs.push (generate_configuration (pos + i * dir, dir));
435 Real best_score = 1e6;
436 for (int i = 0; i < confs.size (); i ++)
439 score += score_configuration (*confs[i]);
440 score += score_aptitude (*confs[i], spec);
442 if (score < best_score)
449 Tie_configuration best = *confs[best_idx];
450 for (int i = 0; i < confs.size (); i++)
456 Tie_specification::Tie_specification ()
458 has_manual_position_ = false;
459 has_manual_dir_ = false;
461 manual_position_ = 0;
462 manual_dir_ = CENTER;
463 note_head_drul_[LEFT] =
464 note_head_drul_[RIGHT] = 0;
469 Tie_formatting_problem::score_ties_aptitude (Ties_configuration const &ties) const
472 if (ties.size () != specifications_.size ())
474 programming_error ("Huh? Mismatch between sizes.");
478 for (int i = 0; i < ties.size (); i++)
479 score += score_aptitude (ties[i], specifications_[i]);
485 Tie_formatting_problem::score_ties (Ties_configuration const &ties) const
487 return score_ties_configuration (ties)
488 + score_ties_aptitude (ties);
492 Tie_formatting_problem::score_ties_configuration (Ties_configuration const &ties) const
495 for (int i = 0; i < ties.size (); i++)
497 score += score_configuration (ties[i]);
501 Real last_edge = 0.0;
502 Real last_center = 0.0;
503 for (int i = 0; i < ties.size (); i++)
505 Bezier b (ties[i].get_transformed_bezier (details_));
507 Real center = b.curve_point (0.5)[Y_AXIS];
508 Real edge = b.curve_point (0.0)[Y_AXIS];
512 if (edge <= last_edge)
513 score += details_.tie_column_monotonicity_penalty_;
514 if (center <= last_center)
515 score +=details_. tie_column_monotonicity_penalty_;
518 details_.tie_tie_collision_penalty_
519 * max (details_.tie_tie_collision_distance_ - fabs (center - last_center), 0.0)
520 / details_.tie_tie_collision_distance_;
522 details_.tie_tie_collision_penalty_
523 * max (details_.tie_tie_collision_distance_ - fabs (edge - last_edge), 0.0)
524 / details_.tie_tie_collision_distance_;
528 last_center = center;
535 Generate with correct X-attachments and beziers, copying delta_y_
536 from TIES_CONFIG if necessary.
539 Tie_formatting_problem::generate_ties_configuration (Ties_configuration const &ties_config)
541 Ties_configuration copy;
542 for (int i = 0; i < ties_config.size (); i++)
544 Tie_configuration * ptr = get_configuration (ties_config[i].position_, ties_config[i].dir_);
545 if (specifications_[i].has_manual_position_)
548 = (specifications_[i].manual_position_ - ties_config[i].position_)
549 * 0.5 * details_.staff_space_;
558 Tie_formatting_problem::generate_base_chord_configuration ()
560 Ties_configuration ties_config;
561 for (int i = 0; i < specifications_.size (); i ++)
563 Tie_configuration conf;
564 if (specifications_[i].has_manual_dir_)
565 conf.dir_ = specifications_[i].manual_dir_;
566 if (specifications_[i].has_manual_position_)
568 conf.position_ = (int) my_round (specifications_[i].manual_position_);
569 conf.delta_y_ = (specifications_[i].manual_position_ - conf.position_)
570 * 0.5 * details_.staff_space_;
573 conf.position_ = specifications_[i].position_;
575 ties_config.push (conf);
578 set_ties_config_standard_directions (&ties_config);
580 ties_config = generate_ties_configuration (ties_config);
586 Tie_formatting_problem::generate_optimal_chord_configuration ()
588 Ties_configuration base = generate_base_chord_configuration ();
589 Array<Tie_configuration_variation> vars = get_variations (base);
591 Ties_configuration best = base;
592 Real best_score = score_ties_configuration (best);
595 This simply is 1-opt: we have K substitions, and we try applying
596 exactly every one for each.
598 for (int i = 0; i < vars.size (); i++)
600 Ties_configuration variant = base;
601 variant[vars[i].index_] = *vars[i].suggestion_;
603 Real score = (score_ties_configuration (variant)
604 + score_ties_aptitude (variant));
605 if (score < best_score)
616 Tie_formatting_problem::set_ties_config_standard_directions (Ties_configuration *tie_configs)
618 if (tie_configs->is_empty ())
621 if (!tie_configs->elem (0).dir_)
622 tie_configs->elem_ref (0).dir_ = DOWN;
623 if (!tie_configs->top().dir_)
624 tie_configs->top().dir_ = UP;
629 for (int i = 1; i < tie_configs->size (); i++)
631 Real diff = (tie_configs->elem (i-1).position_
632 - tie_configs->elem (i).position_);
634 if (fabs (diff) <= 1)
636 if (!tie_configs->elem (i-1).dir_)
637 tie_configs->elem_ref (i-1).dir_ = DOWN;
638 if (!tie_configs->elem (i).dir_)
639 tie_configs->elem_ref (i).dir_ = UP;
643 for (int i = 1; i < tie_configs->size() - 1; i++)
645 Tie_configuration &conf = tie_configs->elem_ref (i);
649 Direction position_dir =
650 Direction (sign (conf.position_));
654 conf.dir_ = position_dir;
658 Tie_configuration_variation::Tie_configuration_variation ()
664 Array<Tie_configuration_variation>
665 Tie_formatting_problem::get_variations (Ties_configuration const &ties)
667 Real center_distance_tolerance = 0.25;
669 Array<Tie_configuration_variation> vars;
670 Real last_center = 0.0;
671 for (int i = 0; i < ties.size (); i++)
673 Bezier b (ties[i].get_transformed_bezier (details_));
675 Real center = b.curve_point (0.5)[Y_AXIS];
679 if (center <= last_center + center_distance_tolerance)
681 if (!specifications_[i].has_manual_dir_)
683 Tie_configuration_variation var;
685 var.suggestion_ = get_configuration (ties[i].position_,
691 if (!specifications_[i-1].has_manual_dir_)
693 Tie_configuration_variation var;
695 var.suggestion_ = get_configuration (ties[i-1].position_,
703 last_center = center;
711 Tie_formatting_problem::set_manual_tie_configuration (SCM manual_configs)
714 for (SCM s = manual_configs;
715 scm_is_pair (s) && k < specifications_.size(); s = scm_cdr (s))
717 SCM entry = scm_car (s);
718 if (!scm_is_pair (entry))
721 Tie_specification &spec = specifications_[k];
723 if (scm_is_number (scm_cdr (entry)))
725 spec.has_manual_dir_ = true;
726 spec.manual_dir_ = Direction (scm_to_int (scm_cdr (entry)));
728 if (scm_is_number (scm_car (entry)))
730 spec.has_manual_position_ = true;
731 spec.manual_position_ = scm_to_double (scm_car (entry));