2 slur-scoring.cc -- Score based slur formatting
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
11 #include "slur-scoring.hh"
12 #include "libc-extension.hh"
13 #include "slur-configuration.hh"
15 #include "directional-element-interface.hh"
16 #include "pointer-group-interface.hh"
18 #include "note-column.hh"
19 #include "output-def.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "staff-symbol.hh"
26 #include "paper-column.hh"
27 #include "accidental-interface.hh"
32 - curve around flag for y coordinate
34 - short-cut: try a smaller region first.
36 - handle non-visible stems better.
38 - try to prune number of scoring criteria
40 - take encompass-objects more into account when determining
43 - calculate encompass scoring directly after determining slur shape.
47 struct Slur_score_state;
49 Slur_score_state::Slur_score_state ()
53 edge_has_beams_ = false;
54 has_same_beam_ = false;
62 Slur_score_state::~Slur_score_state ()
64 junk_pointers (configurations_);
68 get_detail (SCM alist, SCM sym)
70 SCM entry = scm_assq (sym, alist);
71 return robust_scm2double (scm_is_pair (entry)
78 Slur_score_parameters::fill (Grob *me)
80 SCM details = me->get_property ("details");
83 = (int) get_detail (details, ly_symbol2scm ("region-size"));
84 head_encompass_penalty_
85 = get_detail (details, ly_symbol2scm ("head-encompass-penalty"));
86 stem_encompass_penalty_
87 = get_detail (details, ly_symbol2scm ("stem-encompass-penalty"));
89 = get_detail (details, ly_symbol2scm ("closeness-factor"));
90 edge_attraction_factor_
91 = get_detail (details, ly_symbol2scm ("edge-attraction-factor"));
93 = get_detail (details, ly_symbol2scm ("same-slope-penalty"));
95 = get_detail (details, ly_symbol2scm ("steeper-slope-factor"));
96 non_horizontal_penalty_
97 = get_detail (details, ly_symbol2scm ("non-horizontal-penalty"));
99 = get_detail (details, ly_symbol2scm ("max-slope"));
101 = get_detail (details, ly_symbol2scm ("max-slope-factor"));
103 = get_detail (details, ly_symbol2scm ("free-head-distance"));
104 absolute_closeness_measure_
105 = get_detail (details, ly_symbol2scm ("absolute-closeness-measure"));
106 extra_object_collision_penalty_
107 = get_detail (details, ly_symbol2scm ("extra-object-collision-penalty"));
108 accidental_collision_
109 = get_detail (details, ly_symbol2scm ("accidental-collision"));
110 extra_encompass_free_distance_
111 = get_detail (details, ly_symbol2scm ("extra-encompass-free-distance"));
112 head_slur_distance_factor_
113 = get_detail (details, ly_symbol2scm ("head-slur-distance-factor"));
114 head_slur_distance_max_ratio_
115 = get_detail (details, ly_symbol2scm ("head-slur-distance-max-ratio"));
117 = get_detail (details, ly_symbol2scm ("free-slur-distance"));
119 = get_detail (details, ly_symbol2scm ("edge-slope-exponent"));
123 broken_trend_y (Slur_score_state const &state, Direction hdir)
125 /* A broken slur should maintain the same vertical trend
126 the unbroken slur would have had. */
128 if (Spanner *mother = dynamic_cast<Spanner *> (state.slur_->original ()))
130 int k = broken_spanner_index (state.slur_);
132 if (j < 0 || j >= mother->broken_intos_.size ())
135 Grob *neighbor = mother->broken_intos_[j];
136 Spanner *common_mother
137 = dynamic_cast<Spanner *> (state.common_[Y_AXIS]->original ());
139 = broken_spanner_index (dynamic_cast<Spanner *> (state.common_[Y_AXIS]));
140 int common_j = common_k + hdir;
142 if (common_j < 0 || common_j >= common_mother->broken_intos_.size ())
145 Grob *common_next_system = common_mother->broken_intos_[common_j];
147 SCM last_point = scm_car (scm_last_pair (neighbor->get_property ("control-points")));
149 return scm_to_double (scm_cdr (last_point))
150 + neighbor->relative_coordinate (common_next_system, Y_AXIS);
156 copy slur dir forwards across line break.
159 Slur_score_state::set_next_direction ()
161 if (extremes_[RIGHT].note_column_)
164 if (Spanner *mother = dynamic_cast<Spanner *> (slur_->original ()))
166 int k = broken_spanner_index (slur_);
168 if (j < 0 || j >= mother->broken_intos_.size ())
171 Grob *neighbor = mother->broken_intos_[j];
172 set_grob_direction (neighbor, dir_);
177 Slur_score_state::get_encompass_info (Grob *col) const
179 Grob *stem = unsmob_grob (col->get_object ("stem"));
184 programming_error ("no stem for note column");
185 ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
186 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS],
190 Direction stem_dir = get_grob_direction (stem);
192 if (Grob *head = Note_column::first_head (col))
193 ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
195 ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
197 Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
200 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS], Y_AXIS)[dir_];
204 ei.head_ = h->extent (common_[Y_AXIS], Y_AXIS)[dir_];
206 if ((stem_dir == dir_)
207 && !stem->extent (stem, Y_AXIS).is_empty ())
209 ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
210 if (Grob *b = Stem::get_beam (stem))
211 ei.stem_ += stem_dir * 0.5 * Beam::get_thickness (b);
213 Interval x = stem->extent (common_[X_AXIS], X_AXIS);
214 ei.x_ = x.is_empty ()
215 ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
224 Drul_array<Bound_info>
225 Slur_score_state::get_bound_info () const
227 Drul_array<Bound_info> extremes;
230 Direction dir = dir_;
234 extremes[d].bound_ = slur_->get_bound (d);
235 if (Note_column::has_interface (extremes[d].bound_))
237 extremes[d].note_column_ = extremes[d].bound_;
238 extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
239 if (extremes[d].stem_)
241 extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
243 for (int a = X_AXIS; a < NO_AXES; a++)
246 Interval s = extremes[d].stem_->extent (common_[ax], ax);
250 do not issue warning. This happens for rests and
254 + extremes[d].stem_->relative_coordinate (common_[ax], ax);
256 extremes[d].stem_extent_[ax] = s;
259 extremes[d].slur_head_
260 = Stem::extremal_heads (extremes[d].stem_)[dir];
261 if (!extremes[d].slur_head_
262 && Note_column::has_rests (extremes[d].bound_))
263 extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
264 extremes[d].staff_ = Staff_symbol_referencer
265 ::get_staff_symbol (extremes[d].stem_);
266 extremes[d].staff_space_ = Staff_symbol_referencer
267 ::staff_space (extremes[d].stem_);
270 if (extremes[d].slur_head_)
271 extremes[d].slur_head_x_extent_
272 = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
276 while (flip (&d) != LEFT);
282 Slur_score_state::fill (Grob *me)
284 slur_ = dynamic_cast<Spanner *> (me);
286 = internal_extract_grob_array (me, ly_symbol2scm ("note-columns"));
288 if (columns_.is_empty ())
294 staff_space_ = Staff_symbol_referencer::staff_space (me);
295 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
296 thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
298 dir_ = get_grob_direction (me);
299 parameters_.fill (me);
301 extract_grob_set (me, "note-columns", columns);
302 extract_grob_set (me, "encompass-objects", extra_objects);
304 Spanner *sp = dynamic_cast<Spanner *> (me);
306 for (int i = X_AXIS; i < NO_AXES; i++)
309 common_[a] = common_refpoint_of_array (columns, me, a);
310 common_[a] = common_refpoint_of_array (extra_objects, common_[a], a);
316 If bound is not in note-columns, we don't want to know about
320 common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
322 while (flip (&d) != LEFT);
325 extremes_ = get_bound_info ();
326 is_broken_ = (!extremes_[LEFT].note_column_
327 || !extremes_[RIGHT].note_column_);
330 = (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
331 && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
333 base_attachments_ = get_base_attachments ();
335 Drul_array<Real> end_ys
336 = get_y_attachment_range ();
338 configurations_ = enumerate_attachments (end_ys);
339 for (int i = 0; i < columns_.size (); i++)
340 encompass_infos_.push (get_encompass_info (columns_[i]));
342 extra_encompass_infos_ = get_extra_encompass_infos ();
350 && extremes_[d].slur_head_)
352 * extremes_[d].slur_head_->relative_coordinate (common_[Y_AXIS], Y_AXIS);
354 while (flip (&d) != LEFT);
357 = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
358 || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
360 set_next_direction ();
367 MAKE_SCHEME_CALLBACK(Slur, calc_control_points, 1)
369 Slur::calc_control_points (SCM smob)
371 Spanner *me = unsmob_spanner (smob);
373 Slur_score_state state;
379 state.generate_curves ();
381 SCM end_ys = me->get_property ("positions");
384 if (is_number_pair (end_ys))
385 best = state.configurations_[state.get_closest_index (end_ys)]->curve_;
387 best = state.get_best_curve ();
389 SCM controls = SCM_EOL;
390 for (int i = 4; i--;)
392 Offset o = best.control_[i]
393 - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
394 me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
395 controls = scm_cons (ly_offset2scm (o), controls);
402 Slur_score_state::get_best_curve ()
407 #if DEBUG_SLUR_SCORING
408 SCM inspect_quants = slur_->get_property ("inspect-quants");
409 if (to_boolean (slur_->layout ()
410 ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
411 && scm_is_pair (inspect_quants))
413 opt_idx = get_closest_index (inspect_quants);
414 configurations_[opt_idx]->score (*this);
415 opt = configurations_[opt_idx]->score_;
420 for (int i = 0; i < configurations_.size (); i++)
421 configurations_[i]->score (*this);
422 for (int i = 0; i < configurations_.size (); i++)
424 if (configurations_[i]->score_ < opt)
426 opt = configurations_[i]->score_;
432 #if DEBUG_SLUR_SCORING
435 configurations_[opt_idx]->score_card_ += to_string ("=%.2f", opt);
436 configurations_[opt_idx]->score_card_ += to_string ("i%d", opt_idx);
440 programming_error ("No optimal slur found. Guessing 0.");
445 slur_->set_property ("quant-score",
446 scm_makfrom0str (configurations_[opt_idx]->score_card_.c_str ()));
450 return configurations_[opt_idx]->curve_;
454 Slur_score_state::get_closest_index (SCM inspect_quants) const
456 Drul_array<Real> ins = ly_scm2interval (inspect_quants);
460 for (int i = 0; i < configurations_.size (); i++)
462 Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ins[LEFT])
463 + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
471 programming_error ("can't not find quant");
476 TODO: should analyse encompasses to determine sensible region, and
477 should limit slopes available.
481 Slur_score_state::get_y_attachment_range () const
483 Drul_array<Real> end_ys;
487 if (extremes_[d].note_column_)
490 * max (max (dir_ * (base_attachments_[d][Y_AXIS]
491 + parameters_.region_size_ * dir_),
492 dir_ * (dir_ + extremes_[d].note_column_->extent (common_[Y_AXIS], Y_AXIS)[dir_])),
493 dir_ * base_attachments_[-d][Y_AXIS]);
496 end_ys[d] = base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_;
498 while (flip (&d) != LEFT);
504 spanner_less (Spanner *s1, Spanner *s2)
510 b1[d] = s1->get_bound (d)->get_column ()->get_rank ();
511 b2[d] = s2->get_bound (d)->get_column ()->get_rank ();
513 while (flip (&d) != LEFT);
515 return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
516 && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
520 Slur_score_state::get_base_attachments () const
522 Drul_array<Offset> base_attachment;
526 Grob *stem = extremes_[d].stem_;
527 Grob *head = extremes_[d].slur_head_;
531 if (extremes_[d].note_column_)
535 fixme: X coord should also be set in this case.
538 && !Stem::is_invisible (stem)
539 && extremes_[d].stem_dir_ == dir_
540 && Stem::get_beaming (stem, -d)
541 && Stem::get_beam (stem)
542 && (!spanner_less (slur_, Stem::get_beam (stem))
544 y = extremes_[d].stem_extent_[Y_AXIS][dir_];
546 y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
547 y += dir_ * 0.5 * staff_space_;
549 y = move_away_from_staffline (y, head);
551 Grob *fh = Note_column::first_head (extremes_[d].note_column_);
553 = (fh ? fh->extent (common_[X_AXIS], X_AXIS)
554 : extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS))
555 .linear_combination (CENTER);
557 base_attachment[d] = Offset (x, y);
559 while (flip (&d) != LEFT);
563 if (!extremes_[d].note_column_)
567 x = extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS)[d];
569 x = slur_->get_broken_left_end_align ();
570 Grob *col = (d == LEFT) ? columns_[0] : columns_.top ();
572 if (extremes_[-d].bound_ != col)
574 y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
575 y += dir_ * 0.5 * staff_space_;
577 if (get_grob_direction (col) == dir_
578 && Note_column::get_stem (col)
579 && !Stem::is_invisible (Note_column::get_stem (col)))
580 y -= dir_ * 1.5 * staff_space_;
583 y = base_attachment[-d][Y_AXIS];
585 y = move_away_from_staffline (y, col);
587 base_attachment[d] = Offset (x, y);
590 while (flip (&d) != LEFT);
594 for (int a = X_AXIS; a < NO_AXES; a++)
596 Real &b = base_attachment[d][Axis (a)];
598 if (isinf (b) || isnan (b))
600 programming_error ("slur attachment is inf/nan");
605 while (flip (&d) != LEFT);
607 return base_attachment;
611 Slur_score_state::move_away_from_staffline (Real y,
612 Grob *on_staff) const
617 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (on_staff);
622 = (y - staff_symbol->relative_coordinate (common_[Y_AXIS],
624 * 2.0 / staff_space_;
626 if (fabs (pos - my_round (pos)) < 0.2
627 && Staff_symbol_referencer::on_line (on_staff, (int) rint (pos))
628 && Staff_symbol_referencer::line_count (on_staff) - 1 >= rint (pos))
629 y += 1.5 * staff_space_ * dir_ / 10;
635 Slur_score_state::generate_avoid_offsets () const
638 Link_array<Grob> encompasses = columns_;
640 for (int i = 0; i < encompasses.size (); i++)
642 if (extremes_[LEFT].note_column_ == encompasses[i]
643 || extremes_[RIGHT].note_column_ == encompasses[i])
646 Encompass_info inf (get_encompass_info (encompasses[i]));
647 Real y = dir_ * (max (dir_ * inf.head_, dir_ * inf.stem_));
649 avoid.push (Offset (inf.x_, y + dir_ * parameters_.free_head_distance_));
652 extract_grob_set (slur_, "encompass-objects", extra_encompasses);
653 for (int i = 0; i < extra_encompasses.size (); i++)
655 if (Slur::has_interface (extra_encompasses[i]))
657 Grob *small_slur = extra_encompasses[i];
658 Bezier b = Slur::get_curve (small_slur);
660 Offset z = b.curve_point (0.5);
661 z += Offset (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
662 small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
664 z[Y_AXIS] += dir_ * parameters_.free_slur_distance_;
667 else if (extra_encompasses[i]->get_property ("avoid-slur") == ly_symbol2scm ("inside"))
669 Grob *g = extra_encompasses [i];
670 Interval xe = g->extent (common_[X_AXIS], X_AXIS);
671 Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
675 avoid.push (Offset (xe.center(), ye[dir_]));
682 Slur_score_state::generate_curves () const
684 Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
685 Real h_inf = staff_space_ * scm_to_double (slur_->get_property ("height-limit"));
687 Array<Offset> avoid = generate_avoid_offsets ();
688 for (int i = 0; i < configurations_.size (); i++)
689 configurations_[i]->generate_curve (*this, r_0, h_inf, avoid);
692 Link_array<Slur_configuration>
693 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
695 Link_array<Slur_configuration> scores;
697 Drul_array<Offset> os;
698 os[LEFT] = base_attachments_[LEFT];
699 Real minimum_length = staff_space_
700 * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
702 for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
704 os[RIGHT] = base_attachments_[RIGHT];
705 for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
707 Slur_configuration s;
709 Drul_array<bool> attach_to_stem (false, false);
712 os[d][X_AXIS] = base_attachments_[d][X_AXIS];
713 if (extremes_[d].stem_
714 && !Stem::is_invisible (extremes_[d].stem_)
715 && extremes_[d].stem_dir_ == dir_)
717 Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
718 stem_y.widen (0.25 * staff_space_);
719 if (stem_y.contains (os[d][Y_AXIS]))
721 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS][-d]
723 attach_to_stem[d] = true;
725 else if (dir_ * extremes_[d].stem_extent_[Y_AXIS][dir_]
726 < dir_ * os[d][Y_AXIS]
727 && !extremes_[d].stem_extent_[X_AXIS].is_empty ())
729 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
732 while (flip (&d) != LEFT);
735 dz = os[RIGHT] - os[LEFT];
736 if (dz[X_AXIS] < minimum_length
737 || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_)
741 if (extremes_[d].slur_head_
742 && !extremes_[d].slur_head_x_extent_.is_empty ())
744 os[d][X_AXIS] = extremes_[d].slur_head_x_extent_.center ();
745 attach_to_stem[d] = false;
748 while (flip (&d) != LEFT);
751 dz = os[RIGHT] - os[LEFT];
754 if (extremes_[d].slur_head_
755 && !attach_to_stem[d])
757 /* Horizontally move tilted slurs a little. Move
758 more for bigger tilts.
762 -= dir_ * extremes_[d].slur_head_x_extent_.length ()
763 * sin (dz.arg ()) / 3;
766 while (flip (&d) != LEFT);
769 s.index_ = scores.size ();
771 scores.push (new Slur_configuration (s));
773 os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
776 os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
779 assert (scores.size () > 0);
783 Array<Extra_collision_info>
784 Slur_score_state::get_extra_encompass_infos () const
786 extract_grob_set (slur_, "encompass-objects", encompasses);
787 Array<Extra_collision_info> collision_infos;
788 for (int i = encompasses.size (); i--;)
790 if (Slur::has_interface (encompasses[i]))
792 Spanner *small_slur = dynamic_cast<Spanner *> (encompasses[i]);
793 Bezier b = Slur::get_curve (small_slur);
795 Offset relative (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
796 small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
798 for (int k = 0; k < 3; k++)
800 Direction hdir = Direction (k / 2 - 1);
803 Only take bound into account if small slur starts
804 together with big slur.
806 if (hdir && small_slur->get_bound (hdir) != slur_->get_bound (hdir))
809 Offset z = b.curve_point (k / 2.0);
814 yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
816 Interval xext (-1, 1);
817 xext = xext * (thickness_ * 2) + z[X_AXIS];
818 Extra_collision_info info (small_slur,
822 parameters_.extra_object_collision_penalty_);
823 collision_infos.push (info);
828 Grob *g = encompasses [i];
829 Interval xe = g->extent (common_[X_AXIS], X_AXIS);
830 Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
833 Real penalty = parameters_.extra_object_collision_penalty_;
834 if (Accidental_interface::has_interface (g))
836 penalty = parameters_.accidental_collision_;
837 /* Begin copy accidental.cc */
839 if (to_boolean (g->get_property ("cautionary")))
841 SCM cstyle = g->get_property ("cautionary-style");
842 parens = ly_is_equal (cstyle, ly_symbol2scm ("parentheses"));
845 SCM accs = g->get_property ("accidentals");
846 SCM scm_style = g->get_property ("style");
847 if (!scm_is_symbol (scm_style)
849 && scm_ilength (accs) == 1)
851 /* End copy accidental.cc */
852 switch (scm_to_int (scm_car (accs)))
868 ye.widen (thickness_ * 0.5);
869 xe.widen (thickness_ * 1.0);
870 Extra_collision_info info (g, xp, xe, ye, penalty);
871 collision_infos.push (info);
875 return collision_infos;
878 Extra_collision_info::Extra_collision_info (Grob *g, Real idx, Interval x, Interval y, Real p)
881 extents_[X_AXIS] = x;
882 extents_[Y_AXIS] = y;
885 type_ = g->get_property ("avoid-slur");
888 Extra_collision_info::Extra_collision_info ()