2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "slur-scoring.hh"
25 #include "axis-group-interface.hh"
26 #include "accidental-interface.hh"
29 #include "directional-element-interface.hh"
31 #include "libc-extension.hh"
34 #include "note-column.hh"
35 #include "output-def.hh"
36 #include "paper-column.hh"
38 #include "pointer-group-interface.hh"
39 #include "slur-configuration.hh"
42 #include "staff-symbol-referencer.hh"
43 #include "staff-symbol.hh"
45 #include "time-signature.hh"
51 - curve around flag for y coordinate
53 - short-cut: try a smaller region first.
55 - handle non-visible stems better.
57 - try to prune number of scoring criteria
59 - take encompass-objects more into account when determining
62 - calculate encompass scoring directly after determining slur shape.
66 struct Slur_score_state;
68 Slur_score_state::Slur_score_state ()
72 edge_has_beams_ = false;
73 has_same_beam_ = false;
81 Slur_score_state::~Slur_score_state ()
83 junk_pointers (configurations_);
87 If a slur is broken across a line break, the direction
88 of the post-break slur must be the same as the pre-break
92 Slur_score_state::slur_direction () const
94 Grob *left_neighbor = slur_->broken_neighbor (LEFT);
96 if (left_neighbor && left_neighbor->is_live ())
97 return get_grob_direction (left_neighbor);
99 Direction dir = get_grob_direction (slur_);
101 if (Grob *right_neighbor = slur_->broken_neighbor (RIGHT))
102 set_grob_direction (right_neighbor, dir);
108 Slur_score_state::get_encompass_info (Grob *col) const
110 Grob *stem = unsmob_grob (col->get_object ("stem"));
115 programming_error ("no stem for note column");
116 ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
117 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS],
121 Direction stem_dir = get_grob_direction (stem);
123 if (Grob *head = Note_column::first_head (col))
124 ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
126 ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
128 Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
131 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS], Y_AXIS)[dir_];
135 ei.head_ = h->extent (common_[Y_AXIS], Y_AXIS)[dir_];
137 if ((stem_dir == dir_)
138 && !stem->extent (stem, Y_AXIS).is_empty ())
140 ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
141 if (Grob *b = Stem::get_beam (stem))
142 ei.stem_ += stem_dir * 0.5 * Beam::get_beam_thickness (b);
144 Interval x = stem->extent (common_[X_AXIS], X_AXIS);
145 ei.x_ = x.is_empty ()
146 ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
155 Drul_array<Bound_info>
156 Slur_score_state::get_bound_info () const
158 Drul_array<Bound_info> extremes;
160 Direction dir = dir_;
162 for (LEFT_and_RIGHT (d))
164 extremes[d].bound_ = slur_->get_bound (d);
165 if (Note_column::has_interface (extremes[d].bound_))
167 extremes[d].note_column_ = extremes[d].bound_;
168 extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
169 extremes[d].flag_ = Note_column::get_flag (extremes[d].note_column_);
171 if (extremes[d].stem_)
173 extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
175 for (int a = X_AXIS; a < NO_AXES; a++)
178 Interval s = extremes[d].stem_->extent (common_[ax], ax);
179 if (extremes[d].flag_)
180 s.unite (extremes[d].flag_->extent (common_[ax], ax));
184 do not issue warning. This happens for rests and
188 + extremes[d].stem_->relative_coordinate (common_[ax], ax);
190 extremes[d].stem_extent_[ax] = s;
193 extremes[d].slur_head_
194 = Stem::extremal_heads (extremes[d].stem_)[dir];
195 if (!extremes[d].slur_head_
196 && Note_column::has_rests (extremes[d].bound_))
197 extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
198 extremes[d].staff_ = Staff_symbol_referencer
199 ::get_staff_symbol (extremes[d].stem_);
200 extremes[d].staff_space_ = Staff_symbol_referencer
201 ::staff_space (extremes[d].stem_);
204 if (extremes[d].slur_head_)
205 extremes[d].slur_head_x_extent_
206 = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
215 Slur_score_state::fill (Grob *me)
217 slur_ = dynamic_cast<Spanner *> (me);
219 = internal_extract_grob_array (me, ly_symbol2scm ("note-columns"));
221 if (columns_.empty ())
227 Slur::replace_breakable_encompass_objects (me);
228 staff_space_ = Staff_symbol_referencer::staff_space (me);
229 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
230 thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
232 dir_ = slur_direction ();
233 parameters_.fill (me);
235 extract_grob_set (me, "note-columns", columns);
236 extract_grob_set (me, "encompass-objects", extra_objects);
238 Spanner *sp = dynamic_cast<Spanner *> (me);
240 for (int i = X_AXIS; i < NO_AXES; i++)
243 common_[a] = common_refpoint_of_array (columns, me, a);
244 common_[a] = common_refpoint_of_array (extra_objects, common_[a], a);
246 for (LEFT_and_RIGHT (d))
249 If bound is not in note-columns, we don't want to know about
253 common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
257 extremes_ = get_bound_info ();
258 is_broken_ = (!extremes_[LEFT].note_column_
259 || !extremes_[RIGHT].note_column_);
262 = (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
263 && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
265 base_attachments_ = get_base_attachments ();
267 Drul_array<Real> end_ys
268 = get_y_attachment_range ();
270 extra_encompass_infos_ = get_extra_encompass_infos ();
272 Interval additional_ys (0.0, 0.0);
274 for (vsize i = 0; i < extra_encompass_infos_.size (); i++)
276 if (extra_encompass_infos_[i].extents_[X_AXIS].is_empty ())
279 Real y_place = linear_interpolate (extra_encompass_infos_[i].extents_[X_AXIS].center (),
280 base_attachments_[RIGHT][X_AXIS],
281 base_attachments_[LEFT][X_AXIS],
284 Real encompass_place = extra_encompass_infos_[i].extents_[Y_AXIS][dir_];
285 if (extra_encompass_infos_[i].type_ == ly_symbol2scm ("inside")
286 && minmax (dir_, encompass_place, y_place) == encompass_place
287 && (!extra_encompass_infos_[i].grob_->internal_has_interface (ly_symbol2scm ("key-signature-interface"))
288 && !Clef::has_interface (extra_encompass_infos_[i].grob_)
289 && !Time_signature::has_interface (extra_encompass_infos_[i].grob_)))
291 for (LEFT_and_RIGHT (d))
292 additional_ys[d] = minmax (dir_,
295 * (parameters_.encompass_object_range_overshoot_
296 + (y_place - encompass_place)
297 * (normalize (extra_encompass_infos_[i].extents_[X_AXIS].center (),
298 base_attachments_[RIGHT][X_AXIS],
299 base_attachments_[LEFT][X_AXIS])
300 + (dir_ == LEFT ? 0 : -1)))));
304 for (LEFT_and_RIGHT (d))
305 end_ys[d] += additional_ys[d];
307 configurations_ = enumerate_attachments (end_ys);
308 for (vsize i = 0; i < columns_.size (); i++)
309 encompass_infos_.push_back (get_encompass_info (columns_[i]));
314 for (LEFT_and_RIGHT (d))
317 && extremes_[d].slur_head_)
319 * extremes_[d].slur_head_->relative_coordinate (common_[Y_AXIS], Y_AXIS);
323 = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
324 || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
330 MAKE_SCHEME_CALLBACK (Slur, calc_control_points, 1)
332 Slur::calc_control_points (SCM smob)
334 Spanner *me = unsmob_spanner (smob);
336 Slur_score_state state;
342 state.generate_curves ();
344 SCM end_ys = me->get_property ("positions");
345 SCM inspect_quants = me->get_property ("inspect-quants");
346 bool debug_slurs = to_boolean (me->layout ()
347 ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")));
349 if (is_number_pair (inspect_quants))
352 end_ys = inspect_quants;
355 Slur_configuration *best = NULL;
356 if (is_number_pair (end_ys))
357 best = state.get_forced_configuration (ly_scm2interval (end_ys));
359 best = state.get_best_curve ();
361 #if DEBUG_SLUR_SCORING
364 string total = best->card ();
365 total += to_string (" TOTAL=%.2f idx=%d", best->score (), best->index_);
366 me->set_property ("annotation", ly_string2scm (total));
370 SCM controls = SCM_EOL;
371 for (int i = 4; i--;)
373 Offset o = best->curve_.control_[i]
374 - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
375 me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
376 controls = scm_cons (ly_offset2scm (o), controls);
383 Slur_score_state::get_forced_configuration (Interval ys) const
385 Slur_configuration *best = NULL;
387 for (vsize i = 0; i < configurations_.size (); i++)
389 Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ys[LEFT])
390 + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ys[RIGHT]);
393 best = configurations_[i];
398 while (!best->done ())
399 best->run_next_scorer (*this);
402 programming_error ("cannot find quant");
408 Slur_score_state::get_best_curve () const
410 std::priority_queue < Slur_configuration *, std::vector<Slur_configuration *>,
411 Slur_configuration_less > queue;
412 for (vsize i = 0; i < configurations_.size (); i++)
413 queue.push (configurations_[i]);
415 Slur_configuration *best = NULL;
423 best->run_next_scorer (*this);
431 Slur_score_state::breakable_bound_extent (Direction d) const
433 Grob *col = slur_->get_bound (d)->get_column ();
437 extract_grob_set (slur_, "encompass-objects", extra_encompasses);
439 for (vsize i = 0; i < extra_encompasses.size (); i++)
441 Item *item = dynamic_cast<Item *> (extra_encompasses[i]);
442 if (item && col == item->get_column ())
443 ret.unite (robust_relative_extent (item, common_[X_AXIS], X_AXIS));
450 TODO: should analyse encompasses to determine sensible region, and
451 should limit slopes available.
455 Slur_score_state::get_y_attachment_range () const
457 Drul_array<Real> end_ys;
458 for (LEFT_and_RIGHT (d))
460 if (extremes_[d].note_column_)
463 * max (max (dir_ * (base_attachments_[d][Y_AXIS]
464 + parameters_.region_size_ * dir_),
465 dir_ * (dir_ + extremes_[d].note_column_->extent (common_[Y_AXIS], Y_AXIS)[dir_])),
466 dir_ * base_attachments_[-d][Y_AXIS]);
469 end_ys[d] = base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_;
476 spanner_less (Spanner *s1, Spanner *s2)
479 for (LEFT_and_RIGHT (d))
481 b1[d] = s1->get_bound (d)->get_column ()->get_rank ();
482 b2[d] = s2->get_bound (d)->get_column ()->get_rank ();
485 return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
486 && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
490 Slur_score_state::get_base_attachments () const
492 Drul_array<Offset> base_attachment;
493 for (LEFT_and_RIGHT (d))
495 Grob *stem = extremes_[d].stem_;
496 Grob *head = extremes_[d].slur_head_;
500 if (extremes_[d].note_column_)
504 fixme: X coord should also be set in this case.
507 && !Stem::is_invisible (stem)
508 && extremes_[d].stem_dir_ == dir_
509 && Stem::get_beaming (stem, -d)
510 && Stem::get_beam (stem)
511 && (!spanner_less (slur_, Stem::get_beam (stem))
513 y = extremes_[d].stem_extent_[Y_AXIS][dir_];
515 y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
516 y += dir_ * 0.5 * staff_space_;
518 y = move_away_from_staffline (y, head);
520 Grob *fh = Note_column::first_head (extremes_[d].note_column_);
522 = (fh ? fh->extent (common_[X_AXIS], X_AXIS)
523 : extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS))
524 .linear_combination (CENTER);
526 base_attachment[d] = Offset (x, y);
529 for (LEFT_and_RIGHT (d))
531 if (!extremes_[d].note_column_)
536 Interval ext = breakable_bound_extent (d);
538 ext = Axis_group_interface::
539 generic_bound_extent (extremes_[d].bound_,
540 common_[X_AXIS], X_AXIS);
543 Grob *col = (d == LEFT) ? columns_[0] : columns_.back ();
545 if (extremes_[-d].bound_ != col)
547 y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
548 y += dir_ * 0.5 * staff_space_;
550 if (get_grob_direction (col) == dir_
551 && Note_column::get_stem (col)
552 && !Stem::is_invisible (Note_column::get_stem (col)))
553 y -= dir_ * 1.5 * staff_space_;
556 y = base_attachment[-d][Y_AXIS];
558 y = move_away_from_staffline (y, col);
560 base_attachment[d] = Offset (x, y);
564 for (LEFT_and_RIGHT (d))
566 for (int a = X_AXIS; a < NO_AXES; a++)
568 Real &b = base_attachment[d][Axis (a)];
570 if (isinf (b) || isnan (b))
572 programming_error ("slur attachment is inf/nan");
578 return base_attachment;
582 Slur_score_state::move_away_from_staffline (Real y,
583 Grob *on_staff) const
588 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (on_staff);
593 = (y - staff_symbol->relative_coordinate (common_[Y_AXIS],
595 * 2.0 / staff_space_;
597 if (fabs (pos - my_round (pos)) < 0.2
598 && Staff_symbol_referencer::on_line (on_staff, (int) rint (pos))
599 && Staff_symbol_referencer::line_count (on_staff) - 1 >= rint (pos))
600 y += 1.5 * staff_space_ * dir_ / 10;
606 Slur_score_state::generate_avoid_offsets () const
608 vector<Offset> avoid;
609 vector<Grob *> encompasses = columns_;
611 for (vsize i = 0; i < encompasses.size (); i++)
613 if (extremes_[LEFT].note_column_ == encompasses[i]
614 || extremes_[RIGHT].note_column_ == encompasses[i])
617 Encompass_info inf (get_encompass_info (encompasses[i]));
618 Real y = dir_ * (max (dir_ * inf.head_, dir_ * inf.stem_));
620 avoid.push_back (Offset (inf.x_, y + dir_ * parameters_.free_head_distance_));
623 extract_grob_set (slur_, "encompass-objects", extra_encompasses);
624 for (vsize i = 0; i < extra_encompasses.size (); i++)
626 if (Slur::has_interface (extra_encompasses[i]))
628 Grob *small_slur = extra_encompasses[i];
629 Bezier b = Slur::get_curve (small_slur);
631 Offset z = b.curve_point (0.5);
632 z += Offset (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
633 small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
635 z[Y_AXIS] += dir_ * parameters_.free_slur_distance_;
638 else if (extra_encompasses[i]->get_property ("avoid-slur") == ly_symbol2scm ("inside"))
640 Grob *g = extra_encompasses [i];
641 Interval xe = g->extent (common_[X_AXIS], X_AXIS);
642 Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
646 avoid.push_back (Offset (xe.center (), ye[dir_]));
653 Slur_score_state::generate_curves () const
655 Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
656 Real h_inf = staff_space_ * scm_to_double (slur_->get_property ("height-limit"));
658 vector<Offset> avoid = generate_avoid_offsets ();
659 for (vsize i = 0; i < configurations_.size (); i++)
660 configurations_[i]->generate_curve (*this, r_0, h_inf, avoid);
663 vector<Slur_configuration *>
664 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
666 vector<Slur_configuration *> scores;
668 Drul_array<Offset> os;
669 os[LEFT] = base_attachments_[LEFT];
670 Real minimum_length = staff_space_
671 * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
673 for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
675 os[RIGHT] = base_attachments_[RIGHT];
676 for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
679 Drul_array<bool> attach_to_stem (false, false);
680 for (LEFT_and_RIGHT (d))
682 os[d][X_AXIS] = base_attachments_[d][X_AXIS];
683 if (extremes_[d].stem_
684 && !Stem::is_invisible (extremes_[d].stem_)
685 && extremes_[d].stem_dir_ == dir_)
687 Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
688 stem_y.widen (0.25 * staff_space_);
689 if (stem_y.contains (os[d][Y_AXIS]))
691 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS][-d]
693 attach_to_stem[d] = true;
695 else if (dir_ * extremes_[d].stem_extent_[Y_AXIS][dir_]
696 < dir_ * os[d][Y_AXIS]
697 && !extremes_[d].stem_extent_[X_AXIS].is_empty ())
699 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
704 dz = os[RIGHT] - os[LEFT];
705 if (dz[X_AXIS] < minimum_length
706 || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_)
708 for (LEFT_and_RIGHT (d))
710 if (extremes_[d].slur_head_
711 && !extremes_[d].slur_head_x_extent_.is_empty ())
713 os[d][X_AXIS] = extremes_[d].slur_head_x_extent_.center ();
714 attach_to_stem[d] = false;
719 dz = os[RIGHT] - os[LEFT];
720 for (LEFT_and_RIGHT (d))
722 if (extremes_[d].slur_head_
723 && !attach_to_stem[d])
725 /* Horizontally move tilted slurs a little. Move
726 more for bigger tilts.
730 -= dir_ * extremes_[d].slur_head_x_extent_.length ()
731 * sin (dz.arg ()) / 3;
735 scores.push_back (Slur_configuration::new_config (os, scores.size ()));
737 os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
740 os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
743 assert (scores.size () > 0);
747 vector<Extra_collision_info>
748 Slur_score_state::get_extra_encompass_infos () const
750 extract_grob_set (slur_, "encompass-objects", encompasses);
751 vector<Extra_collision_info> collision_infos;
752 for (vsize i = encompasses.size (); i--;)
754 if (Slur::has_interface (encompasses[i]))
756 Spanner *small_slur = dynamic_cast<Spanner *> (encompasses[i]);
757 Bezier b = Slur::get_curve (small_slur);
759 Offset relative (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
760 small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
762 for (int k = 0; k < 3; k++)
764 Direction hdir = Direction (k - 1);
767 Only take bound into account if small slur starts
768 together with big slur.
770 if (hdir && small_slur->get_bound (hdir) != slur_->get_bound (hdir))
773 Offset z = b.curve_point (k / 2.0);
778 yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
780 Interval xext (-1, 1);
781 xext = xext * (thickness_ * 2) + z[X_AXIS];
782 Extra_collision_info info (small_slur,
786 parameters_.extra_object_collision_penalty_);
787 collision_infos.push_back (info);
792 Grob *g = encompasses [i];
793 Interval xe = g->extent (common_[X_AXIS], X_AXIS);
794 Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
795 if (Dots::has_interface (g))
799 Real penalty = parameters_.extra_object_collision_penalty_;
800 if (Accidental_interface::has_interface (g))
802 penalty = parameters_.accidental_collision_;
804 Rational alt = ly_scm2rational (g->get_property ("alteration"));
805 SCM scm_style = g->get_property ("style");
806 if (!scm_is_symbol (scm_style)
807 && !to_boolean (g->get_property ("parenthesized"))
808 && !to_boolean (g->get_property ("restore-first")))
810 /* End copy accidental.cc */
811 if (alt == FLAT_ALTERATION
812 || alt == DOUBLE_FLAT_ALTERATION)
814 else if (alt == SHARP_ALTERATION)
816 else if (alt == NATURAL_ALTERATION)
821 ye.widen (thickness_ * 0.5);
822 xe.widen (thickness_ * 1.0);
823 Extra_collision_info info (g, xp, xe, ye, penalty);
824 collision_infos.push_back (info);
828 return collision_infos;
831 Extra_collision_info::Extra_collision_info (Grob *g, Real idx, Interval x, Interval y, Real p)
834 extents_[X_AXIS] = x;
835 extents_[Y_AXIS] = y;
838 type_ = g->get_property ("avoid-slur");
841 Extra_collision_info::Extra_collision_info ()