2 slur-scoring.cc -- Score based slur formatting
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
12 #include "slur-scoring.hh"
13 #include "libc-extension.hh"
14 #include "slur-configuration.hh"
16 #include "directional-element-interface.hh"
17 #include "group-interface.hh"
19 #include "note-column.hh"
20 #include "output-def.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "staff-symbol.hh"
27 #include "paper-column.hh"
28 #include "accidental-interface.hh"
33 - curve around flag for y coordinate
35 - this file is a mess, clean it up
37 - short-cut: try a smaller region first.
39 - handle non-visible stems better.
41 - try to prune number of scoring criteria
43 - take encompass-objects more into account when determining
46 - calculate encompass scoring directly after determining slur shape.
50 struct Slur_score_state;
52 Slur_score_state::Slur_score_state ()
56 edge_has_beams_ = false;
57 has_same_beam_ = false;
65 Slur_score_state::~Slur_score_state ()
67 junk_pointers (configurations_);
71 get_detail (SCM alist, SCM sym)
73 SCM entry = scm_assq (sym, alist);
74 return robust_scm2double (scm_is_pair (entry)
81 Slur_score_parameters::fill (Grob *me)
83 SCM details = me->get_property ("slur-details");
86 = (int) get_detail (details, ly_symbol2scm ("region-size"));
87 head_encompass_penalty_
88 = get_detail (details, ly_symbol2scm ("head-encompass-penalty"));
89 stem_encompass_penalty_
90 = get_detail (details, ly_symbol2scm ("stem-encompass-penalty"));
92 = get_detail (details, ly_symbol2scm ("closeness-factor"));
93 edge_attraction_factor_
94 = get_detail (details, ly_symbol2scm ("edge-attraction-factor"));
96 = get_detail (details, ly_symbol2scm ("same-slope-penalty"));
98 = get_detail (details, ly_symbol2scm ("steeper-slope-factor"));
99 non_horizontal_penalty_
100 = get_detail (details, ly_symbol2scm ("non-horizontal-penalty"));
102 = get_detail (details, ly_symbol2scm ("max-slope"));
104 = get_detail (details, ly_symbol2scm ("max-slope-factor"));
106 = get_detail (details, ly_symbol2scm ("free-head-distance"));
107 absolute_closeness_measure_
108 = get_detail (details, ly_symbol2scm ("absolute-closeness-measure"));
109 extra_object_collision_
110 = get_detail (details, ly_symbol2scm ("extra-object-collision"));
111 accidental_collision_
112 = get_detail (details, ly_symbol2scm ("accidental-collision"));
113 extra_encompass_free_distance_
114 = get_detail (details, ly_symbol2scm ("extra-encompass-free-distance"));
115 head_slur_distance_factor_
116 = get_detail (details, ly_symbol2scm ("head-slur-distance-factor"));
117 head_slur_distance_max_ratio_
118 = get_detail (details, ly_symbol2scm ("head-slur-distance-max-ratio"));
120 = get_detail (details, ly_symbol2scm ("free-slur-distance"));
122 = get_detail (details, ly_symbol2scm ("edge-slope-exponent"));
126 broken_trend_y (Slur_score_state const &state, Direction hdir)
128 /* A broken slur should maintain the same vertical trend
129 the unbroken slur would have had. */
131 if (Spanner *mother = dynamic_cast<Spanner *> (state.slur_->original_))
133 int k = broken_spanner_index (state.slur_);
135 if (j < 0 || j >= mother->broken_intos_.size ())
138 Grob *neighbor = mother->broken_intos_[j];
139 Spanner *common_mother
140 = dynamic_cast<Spanner *> (state.common_[Y_AXIS]->original_);
142 = broken_spanner_index (dynamic_cast<Spanner *> (state.common_[Y_AXIS]));
143 int common_j = common_k + hdir;
145 if (common_j < 0 || common_j >= common_mother->broken_intos_.size ())
148 Grob *common_next_system = common_mother->broken_intos_[common_j];
150 SCM last_point = scm_car (scm_last_pair (neighbor->get_property ("control-points")));
152 return scm_to_double (scm_cdr (last_point))
153 + neighbor->relative_coordinate (common_next_system, Y_AXIS);
159 copy slur dir forwards across line break.
162 Slur_score_state::set_next_direction ()
164 if (extremes_[RIGHT].note_column_)
167 if (Spanner *mother = dynamic_cast<Spanner *> (slur_->original_))
169 int k = broken_spanner_index (slur_);
171 if (j < 0 || j >= mother->broken_intos_.size ())
174 Grob *neighbor = mother->broken_intos_[j];
175 set_grob_direction (neighbor, dir_);
180 Slur_score_state::get_encompass_info (Grob *col) const
182 Grob *stem = unsmob_grob (col->get_property ("stem"));
187 programming_error ("No stem for note column?");
188 ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
189 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS],
193 Direction stem_dir = get_grob_direction (stem);
195 if (Grob *head = Note_column::first_head (col))
196 ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
198 ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
200 Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
203 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS], Y_AXIS)[dir_];
207 ei.head_ = h->extent (common_[Y_AXIS], Y_AXIS)[dir_];
209 if ((stem_dir == dir_)
210 && !stem->extent (stem, Y_AXIS).is_empty ())
212 ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
213 if (Grob *b = Stem::get_beam (stem))
214 ei.stem_ += stem_dir * 0.5 * Beam::get_thickness (b);
216 Interval x = stem->extent (common_[X_AXIS], X_AXIS);
217 ei.x_ = x.is_empty ()
218 ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
227 Drul_array<Bound_info>
228 Slur_score_state::get_bound_info () const
230 Drul_array<Bound_info> extremes;
233 Direction dir = dir_;
237 extremes[d].bound_ = slur_->get_bound (d);
238 if (Note_column::has_interface (extremes[d].bound_))
240 extremes[d].note_column_ = extremes[d].bound_;
241 extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
242 extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
243 extremes[d].stem_extent_[X_AXIS]
244 = extremes[d].stem_->extent (common_[X_AXIS], X_AXIS);
245 extremes[d].stem_extent_[Y_AXIS]
246 = extremes[d].stem_->extent (common_[Y_AXIS], Y_AXIS);
247 extremes[d].slur_head_
248 = Stem::extremal_heads (extremes[d].stem_)[dir];
249 if (!extremes[d].slur_head_
250 && Note_column::has_rests (extremes[d].bound_))
252 extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
255 if (extremes[d].slur_head_)
256 extremes[d].slur_head_extent_
257 = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
259 extremes[d].staff_ = Staff_symbol_referencer
260 ::get_staff_symbol (extremes[d].stem_);
261 extremes[d].staff_space_ = Staff_symbol_referencer
262 ::staff_space (extremes[d].stem_);
265 while (flip (&d) != LEFT);
271 Slur_score_state::fill (Grob *me)
273 slur_ = dynamic_cast<Spanner *> (me);
275 = extract_grob_array (me, ly_symbol2scm ("note-columns"));
277 if (columns_.is_empty ())
283 staff_space_ = Staff_symbol_referencer::staff_space (me);
284 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
285 thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
287 dir_ = get_grob_direction (me);
288 parameters_.fill (me);
290 SCM eltlist = me->get_property ("note-columns");
291 SCM extra_list = me->get_property ("encompass-objects");
292 Spanner *sp = dynamic_cast<Spanner *> (me);
294 for (int i = X_AXIS; i < NO_AXES; i++)
297 common_[a] = common_refpoint_of_list (eltlist, me, a);
298 common_[a] = common_refpoint_of_list (extra_list, common_[a], a);
304 If bound is not in note-columns, we don't want to know about
308 common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
310 while (flip (&d) != LEFT);
313 extremes_ = get_bound_info ();
314 is_broken_ = (!extremes_[LEFT].note_column_
315 || !extremes_[RIGHT].note_column_);
318 = (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
319 && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
321 base_attachments_ = get_base_attachments ();
323 Drul_array<Real> end_ys
324 = get_y_attachment_range ();
326 configurations_ = enumerate_attachments (end_ys);
327 for (int i = 0; i < columns_.size (); i++)
328 encompass_infos_.push (get_encompass_info (columns_[i]));
330 extra_encompass_infos_ = get_extra_encompass_infos ();
339 * extremes_[d].slur_head_->relative_coordinate (common_[Y_AXIS], Y_AXIS);
341 while (flip (&d) != LEFT);
344 = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
345 || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
347 set_next_direction ();
354 set_slur_control_points (Grob *me)
356 Slur_score_state state;
362 state.generate_curves ();
364 SCM end_ys = me->get_property ("positions");
367 if (is_number_pair (end_ys))
369 best = state.configurations_[state.get_closest_index (end_ys)]->curve_;
373 best = state.get_best_curve ();
376 SCM controls = SCM_EOL;
377 for (int i = 4; i--;)
379 Offset o = best.control_[i]
380 - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
381 me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
382 controls = scm_cons (ly_offset2scm (o), controls);
384 me->set_property ("control-points", controls);
388 Slur_score_state::get_best_curve ()
390 for (int i = 0; i < configurations_.size (); i++)
392 configurations_[i]->score (*this);
397 for (int i = 0; i < configurations_.size (); i++)
399 if (configurations_[i]->score_ < opt)
401 opt = configurations_[i]->score_;
406 #if DEBUG_SLUR_SCORING
407 SCM inspect_quants = slur_->get_property ("inspect-quants");
408 if (to_boolean (slur_->get_layout ()
409 ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
410 && scm_is_pair (inspect_quants))
411 opt_idx = get_closest_index (inspect_quants);
413 configurations_[opt_idx]->score_card_ += to_string ("=%.2f", opt);
414 configurations_[opt_idx]->score_card_ += to_string ("i%d", opt_idx);
417 slur_->set_property ("quant-score",
418 scm_makfrom0str (configurations_[opt_idx]->score_card_.to_str0 ()));
422 return configurations_[opt_idx]->curve_;
426 Slur_score_state::get_closest_index (SCM inspect_quants) const
428 Drul_array<Real> ins = ly_scm2interval (inspect_quants);
432 for (int i = 0; i < configurations_.size (); i++)
434 Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ins[LEFT])
435 + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
443 programming_error ("Could not find quant.");
448 TODO: should analyse encompasses to determine sensible region, and
449 should limit slopes available.
453 Slur_score_state::get_y_attachment_range () const
455 Drul_array<Real> end_ys;
459 if (extremes_[d].note_column_)
462 * ((dir_ * (base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_))
463 >? (dir_ * (dir_ + extremes_[d].note_column_->extent (common_[Y_AXIS],
465 >? (dir_ * base_attachments_[-d][Y_AXIS]));
468 end_ys[d] = base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_;
470 while (flip (&d) != LEFT);
476 spanner_less (Spanner *s1, Spanner *s2)
482 b1[d] = s1->get_bound (d)->get_column ()->rank_;
483 b2[d] = s2->get_bound (d)->get_column ()->rank_;
485 while (flip (&d) != LEFT);
487 return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
488 && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
492 Slur_score_state::get_base_attachments () const
494 Drul_array<Offset> base_attachment;
498 Grob *stem = extremes_[d].stem_;
499 Grob *head = extremes_[d].slur_head_;
503 if (extremes_[d].note_column_)
507 fixme: X coord should also be set in this case.
510 && extremes_[d].stem_dir_ == dir_
511 && Stem::get_beaming (stem, -d)
512 && (!spanner_less (slur_, Stem::get_beam (stem))
514 y = extremes_[d].stem_extent_[Y_AXIS][dir_];
516 y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
517 y += dir_ * 0.5 * staff_space_;
519 y = move_away_from_staffline (y, head);
521 Grob *fh = Note_column::first_head (extremes_[d].note_column_);
523 = (fh ? fh->extent (common_[X_AXIS], X_AXIS)
524 : extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS))
525 .linear_combination (CENTER);
527 base_attachment[d] = Offset (x, y);
529 while (flip (&d) != LEFT);
533 if (!extremes_[d].note_column_)
538 x = extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS)[d];
542 x = slur_->get_broken_left_end_align ();
544 Grob *col = (d == LEFT) ? columns_[0] : columns_.top ();
546 if (extremes_[-d].bound_ != col)
548 y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
549 y += dir_ * 0.5 * staff_space_;
551 if (get_grob_direction (col) == dir_
552 && Note_column::get_stem (col)
553 && !Stem::is_invisible (Note_column::get_stem (col)))
554 y -= dir_ * 1.5 * staff_space_;
557 y = base_attachment[-d][Y_AXIS];
559 y = move_away_from_staffline (y, col);
561 base_attachment[d] = Offset (x, y);
564 while (flip (&d) != LEFT);
569 for (int a = X_AXIS; a < NO_AXES; a++)
571 Real &b = base_attachment[d][Axis (a)];
573 if (isinf (b) || isnan (b))
576 programming_error ("Slur attachment is inf/nan");
581 while (flip (&d) != LEFT);
584 return base_attachment;
588 Slur_score_state::move_away_from_staffline (Real y,
589 Grob *on_staff) const
592 = (y - Staff_symbol_referencer::get_staff_symbol (on_staff)->relative_coordinate (common_[Y_AXIS],
594 * 2.0 / staff_space_;
596 if (fabs (pos - my_round (pos)) < 0.2
597 && Staff_symbol_referencer::on_staffline (on_staff, (int) rint (pos))
598 && Staff_symbol_referencer::line_count (on_staff) - 1 >= rint (pos))
599 y += 1.5 * staff_space_ * dir_ / 10;
605 Slur_score_state::generate_curves () const
607 Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
608 Real h_inf = staff_space_ * scm_to_double (slur_->get_property ("height-limit"));
609 for (int i = 0; i < configurations_.size (); i++)
610 configurations_[i]->generate_curve (*this, r_0, h_inf);
613 Link_array<Slur_configuration>
614 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
617 Link_array<Slur_configuration> scores;
619 Drul_array<Offset> os;
620 os[LEFT] = base_attachments_[LEFT];
621 Real minimum_length = staff_space_
622 * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
624 for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
626 os[RIGHT] = base_attachments_[RIGHT];
627 for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
629 Slur_configuration s;
631 Drul_array<bool> attach_to_stem (false, false);
634 os[d][X_AXIS] = base_attachments_[d][X_AXIS];
635 if (extremes_[d].stem_
636 && !Stem::is_invisible (extremes_[d].stem_)
637 && extremes_[d].stem_dir_ == dir_)
639 Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
640 stem_y.widen (0.25 * staff_space_);
641 if (stem_y.contains (os[d][Y_AXIS]))
643 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS][-d]
645 attach_to_stem[d] = true;
647 else if (dir_ * extremes_[d].stem_extent_[Y_AXIS][dir_]
648 < dir_ * os[d][Y_AXIS]
649 && !extremes_[d].stem_extent_[X_AXIS].is_empty ())
651 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
654 while (flip (&d) != LEFT);
657 dz = os[RIGHT] - os[LEFT];
658 if (dz[X_AXIS] < minimum_length
659 || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_)
663 if (extremes_[d].slur_head_)
665 os[d][X_AXIS] = extremes_[d].slur_head_extent_.center ();
666 attach_to_stem[d] = false;
669 while (flip (&d) != LEFT);
672 dz = os[RIGHT] - os[LEFT];
675 if (extremes_[d].slur_head_
676 && !attach_to_stem[d])
678 /* Horizontally move tilted slurs a little. Move
679 more for bigger tilts.
683 -= dir_ * extremes_[d].slur_head_extent_.length ()
684 * sin (dz.arg ()) / 3;
687 while (flip (&d) != LEFT);
690 s.index_ = scores.size ();
692 scores.push (new Slur_configuration (s));
694 os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
697 os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
700 assert (scores.size () > 0);
704 Array<Extra_collision_info>
705 Slur_score_state::get_extra_encompass_infos () const
707 Link_array<Grob> encompasses
708 = extract_grob_array (slur_, ly_symbol2scm ("encompass-objects"));
709 Array<Extra_collision_info> collision_infos;
710 for (int i = encompasses.size (); i--;)
712 if (Slur::has_interface (encompasses[i]))
714 Spanner *small_slur = dynamic_cast<Spanner *> (encompasses[i]);
715 Bezier b = Slur::get_curve (small_slur);
717 Offset relative (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
718 small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
720 for (int k = 0; k < 3; k++)
722 Direction hdir = Direction (k / 2 - 1);
725 Only take bound into account if small slur starts
726 together with big slur.
728 if (hdir && small_slur->get_bound (hdir) != slur_->get_bound (hdir))
731 Offset z = b.curve_point (k / 2.0);
736 yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
738 Interval xext (-1, 1);
739 xext = xext * (thickness_ * 2) + z[X_AXIS];
740 Extra_collision_info info (small_slur,
744 parameters_.extra_object_collision_);
745 collision_infos.push (info);
750 Grob *g = encompasses [i];
751 Interval xe = g->extent (common_[X_AXIS], X_AXIS);
752 Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
755 Real penalty = parameters_.extra_object_collision_;
756 if (Accidental_interface::has_interface (g))
758 penalty = parameters_.accidental_collision_;
759 /* Begin copy accidental.cc */
761 if (to_boolean (g->get_property ("cautionary")))
763 SCM cstyle = g->get_property ("cautionary-style");
764 parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
767 SCM accs = g->get_property ("accidentals");
768 SCM scm_style = g->get_property ("style");
769 if (!scm_is_symbol (scm_style)
771 && scm_ilength (accs) == 1)
773 /* End copy accidental.cc */
774 switch (scm_to_int (scm_car (accs)))
790 ye.widen (thickness_ * 0.5);
791 xe.widen (thickness_ * 1.0);
792 Extra_collision_info info (g, xp, xe, ye, penalty);
793 collision_infos.push (info);
797 return collision_infos;