2 slur-scoring.cc -- Score based slur formatting
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
12 #include "libc-extension.hh"
13 #include "slur-configuration.hh"
14 #include "slur-scoring.hh"
16 #include "directional-element-interface.hh"
17 #include "group-interface.hh"
18 #include "lily-guile.hh"
20 #include "note-column.hh"
21 #include "output-def.hh"
25 #include "staff-symbol-referencer.hh"
26 #include "staff-symbol.hh"
29 #include "paper-column.hh"
30 #include "accidental-interface.hh"
35 - curve around flag for y coordinate
37 - this file is a mess, clean it up
39 - short-cut: try a smaller region first.
41 - handle non-visible stems better.
43 - try to prune number of scoring criteria
45 - take encompass-objects more into account when determining
48 - calculate encompass scoring directly after determining slur shape.
53 struct Slur_score_state;
55 Slur_score_state::Slur_score_state()
59 edge_has_beams_ = false;
60 has_same_beam_ = false;
69 Slur_score_state::~Slur_score_state ()
79 get_detail (SCM alist, SCM sym)
81 SCM entry = scm_assq (sym, alist);
82 return robust_scm2double (scm_is_pair (entry)
89 init_score_param (Grob *me,
90 Slur_score_parameters *score_param)
92 SCM details = me->get_property ("slur-details");
94 score_param->region_size_
95 = (int) get_detail (details, ly_symbol2scm ("region-size"));
96 score_param->head_encompass_penalty_
97 = get_detail (details, ly_symbol2scm ("head-encompass-penalty"));
98 score_param->stem_encompass_penalty_
99 = get_detail (details, ly_symbol2scm ("stem-encompass-penalty"));
100 score_param->closeness_factor_
101 = get_detail (details, ly_symbol2scm ("closeness-factor"));
102 score_param->edge_attraction_factor_
103 = get_detail (details, ly_symbol2scm ("edge-attraction-factor"));
104 score_param->same_slope_penalty_
105 = get_detail (details, ly_symbol2scm ("same-slope-penalty"));
106 score_param->steeper_slope_factor_
107 = get_detail (details, ly_symbol2scm ("steeper-slope-factor"));
108 score_param->non_horizontal_penalty_
109 = get_detail (details, ly_symbol2scm ("non-horizontal-penalty"));
110 score_param->max_slope_
111 = get_detail (details, ly_symbol2scm ("max-slope"));
112 score_param->max_slope_factor_
113 = get_detail (details, ly_symbol2scm ("max-slope-factor"));
114 score_param->free_head_distance_
115 = get_detail (details, ly_symbol2scm ("free-head-distance"));
116 score_param->extra_object_collision_
117 = get_detail (details, ly_symbol2scm ("extra-object-collision"));
118 score_param->accidental_collision_
119 = get_detail (details, ly_symbol2scm ("accidental-collision"));
120 score_param->extra_encompass_free_distance_
121 = get_detail (details, ly_symbol2scm ("extra-encompass-free-distance"));
122 score_param->head_slur_distance_factor_
123 = get_detail (details, ly_symbol2scm ("head-slur-distance-factor"));
124 score_param->head_slur_distance_max_ratio_
125 = get_detail (details, ly_symbol2scm ("head-slur-distance-max-ratio"));
126 score_param->free_slur_distance_
127 = get_detail (details, ly_symbol2scm ("free-slur-distance"));
128 score_param->edge_slope_exponent_
129 = get_detail (details, ly_symbol2scm ("edge-slope-exponent"));
133 broken_trend_y (Slur_score_state const &state, Direction hdir)
135 /* A broken slur should maintain the same vertical trend
136 the unbroken slur would have had. */
138 if (Spanner *mother = dynamic_cast<Spanner*> (state.slur_->original_))
140 int k = broken_spanner_index (state.slur_);
142 if (j < 0 || j >= mother->broken_intos_.size ())
145 Grob *neighbor = mother->broken_intos_[j];
146 Spanner *common_mother
147 = dynamic_cast<Spanner*> (state.common_[Y_AXIS]->original_);
149 = broken_spanner_index (dynamic_cast<Spanner*> (state.common_[Y_AXIS]));
150 int common_j = common_k + hdir;
152 if (common_j < 0 || common_j >= common_mother->broken_intos_.size ())
155 Grob *common_next_system = common_mother->broken_intos_[common_j];
157 SCM last_point = scm_car (scm_last_pair (neighbor->get_property ("control-points")));
159 return scm_to_double (scm_cdr (last_point))
160 + neighbor->relative_coordinate (common_next_system, Y_AXIS);
167 copy slur dir forwards across line break.
170 Slur_score_state::set_next_direction ()
172 if (extremes_[RIGHT].note_column_)
175 if (Spanner *mother = dynamic_cast<Spanner*> (slur_->original_))
177 int k = broken_spanner_index (slur_);
179 if (j < 0 || j >= mother->broken_intos_.size ())
182 Grob *neighbor = mother->broken_intos_[j];
183 set_grob_direction (neighbor, dir_);
188 Slur_score_state::get_encompass_info (Grob *col) const
190 Grob *stem = unsmob_grob (col->get_property ("stem"));
195 programming_error ("No stem for note column?");
196 ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
197 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS],
201 Direction stem_dir = get_grob_direction (stem);
203 if (Grob *head = Note_column::first_head (col))
204 ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
206 ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
208 Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
211 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS], Y_AXIS)[dir_];
215 ei.head_ = h->extent (common_[Y_AXIS], Y_AXIS)[dir_];
217 if ((stem_dir == dir_)
218 && !stem->extent (stem, Y_AXIS).is_empty ())
220 ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
221 if (Grob *b = Stem::get_beam (stem))
222 ei.stem_ += stem_dir * 0.5 * Beam::get_thickness (b);
224 Interval x = stem->extent (common_[X_AXIS], X_AXIS);
225 ei.x_ = x.is_empty ()
226 ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
238 Drul_array<Bound_info>
239 Slur_score_state::get_bound_info () const
241 Drul_array<Bound_info> extremes;
244 Direction dir = dir_;
248 extremes[d].bound_ = slur_->get_bound (d);
249 if (Note_column::has_interface (extremes[d].bound_))
251 extremes[d].note_column_ = extremes[d].bound_;
252 extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
253 extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
254 extremes[d].stem_extent_[X_AXIS]
255 = extremes[d].stem_->extent (common_[X_AXIS], X_AXIS);
256 extremes[d].stem_extent_[Y_AXIS]
257 = extremes[d].stem_->extent (common_[Y_AXIS], Y_AXIS);
258 extremes[d].slur_head_
259 = Stem::extremal_heads (extremes[d].stem_)[dir];
260 if (!extremes[d].slur_head_
261 && Note_column::has_rests (extremes[d].bound_))
263 extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
266 if (extremes[d].slur_head_)
267 extremes[d].slur_head_extent_
268 = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
270 extremes[d].staff_ = Staff_symbol_referencer
271 ::get_staff_symbol (extremes[d].stem_);
272 extremes[d].staff_space_ = Staff_symbol_referencer
273 ::staff_space (extremes[d].stem_);
276 while (flip (&d) != LEFT);
281 Slur_score_state::fill (Grob *me)
283 slur_ = dynamic_cast<Spanner*> (me);
285 = Pointer_group_interface__extract_grobs (me, (Grob *) 0, "note-columns");
287 if (columns_.is_empty ())
293 staff_space_ = Staff_symbol_referencer::staff_space (me);
294 Real lt = me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
295 thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
297 dir_ = get_grob_direction (me);
298 init_score_param (me, ¶meters_);
300 SCM eltlist = me->get_property ("note-columns");
301 SCM extra_list = me->get_property ("encompass-objects");
302 Spanner *sp = dynamic_cast<Spanner*> (me);
304 for (int i = X_AXIS; i < NO_AXES; i++)
307 common_[a] = common_refpoint_of_list (eltlist, me, a);
308 common_[a] = common_refpoint_of_list (extra_list, common_[a], a);
312 common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
314 while (flip (&d) != LEFT);
317 extremes_ = get_bound_info ();
318 is_broken_ = (!extremes_[LEFT].note_column_
319 || !extremes_[RIGHT].note_column_);
322 base_attachments_ = get_base_attachments ();
324 Drul_array<Real> end_ys
325 = get_y_attachment_range ();
327 scores_ = enumerate_attachments ( end_ys);
328 for (int i = 0; i < columns_.size (); i++)
329 encompass_infos_.push (get_encompass_info ( columns_[i]));
331 extra_encompass_infos_ = get_extra_encompass_infos ();
341 * extremes_[d].slur_head_->relative_coordinate (common_[Y_AXIS], Y_AXIS);
343 while (flip (&d) != LEFT);
346 = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
347 || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
350 (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
351 && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
354 set_next_direction ();
361 set_slur_control_points (Grob *me)
363 Slur_score_state state;
369 state.generate_curves ();
370 Bezier best (state.get_best_curve());
373 SCM controls = SCM_EOL;
374 for (int i = 4; i--;)
376 Offset o = best.control_[i]
377 - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
378 me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
379 controls = scm_cons (ly_offset2scm (o), controls);
381 me->set_property ("control-points", controls);
386 Slur_score_state::get_best_curve ()
388 for (int i = 0; i < scores_->size (); i++)
390 scores_->elem (i).score (*this);
395 for (int i = 0; i < scores_->size (); i++)
397 if ((*scores_)[i].score_ < opt)
399 opt = (*scores_)[i].score_;
404 #if DEBUG_SLUR_SCORING
405 SCM inspect_quants = slur_->get_property ("inspect-quants");
406 if (to_boolean (slur_->get_paper ()
407 ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
408 && scm_is_pair (inspect_quants))
410 Drul_array<Real> ins = ly_scm2interval (inspect_quants);
412 for (int i = 0; i < scores_->size (); i ++)
414 Real d =fabs ((*scores_)[i].attachment_[LEFT][Y_AXIS] - ins[LEFT])
415 + fabs ((*scores_)[i].attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
423 programming_error ("Could not find quant.");
425 (*scores_)[opt_idx].score_card_ += to_string ("i%d", opt_idx);
428 slur_->set_property ("quant-score",
429 scm_makfrom0str ((*scores_)[opt_idx].score_card_.to_str0 ()));
432 return scores_->elem (opt_idx).curve_;
436 TODO: should analyse encompasses to determine sensible region, and
437 should limit slopes available.
441 Slur_score_state::get_y_attachment_range () const
443 Drul_array<Real> end_ys;
447 if (extremes_[d].note_column_)
450 * ((dir_ * (base_attachments_[d][Y_AXIS] + parameters_.region_size_* dir_))
451 >? (dir_ * (dir_ + extremes_[d].note_column_->extent (common_[Y_AXIS],
453 >? (dir_ * base_attachments_[-d][Y_AXIS]));
456 end_ys[d] = base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_;
458 while (flip (&d) != LEFT);
464 spanner_less (Spanner *s1, Spanner* s2)
470 b1[d] = s1->get_bound (d)->get_column ()->rank_;
471 b2[d] = s2->get_bound (d)->get_column ()->rank_;
472 } while (flip (&d) != LEFT);
474 return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
475 && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
480 Slur_score_state::get_base_attachments () const
482 Drul_array<Offset> base_attachment;
486 Grob *stem = extremes_[d].stem_;
487 Grob *head = extremes_[d].slur_head_;
491 if (extremes_[d].note_column_)
495 fixme: X coord should also be set in this case.
498 && extremes_[d].stem_dir_ == dir_
499 && Stem::get_beaming (stem, -d)
500 && (!spanner_less (slur_, Stem::get_beam (stem))
502 y = extremes_[d].stem_extent_[Y_AXIS][dir_];
504 y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
505 y += dir_ * 0.5 * staff_space_;
508 = (y - extremes_[d].staff_->relative_coordinate (common_[Y_AXIS],
510 * 2.0 / staff_space_;
512 /* start off staffline. */
513 if (fabs (pos - my_round (pos)) < 0.2
514 && Staff_symbol_referencer::on_staffline (head, (int) rint (pos))
515 && Staff_symbol_referencer::line_count (head) - 1 >= rint (pos)
517 // TODO: calc from slur thick & line thick, parameter.
518 y += 1.5 * staff_space_ * dir_ / 10;
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);
528 } while (flip (&d) != LEFT);
532 if (!extremes_[d].note_column_)
537 x = extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS)[d];
541 x = slur_->get_broken_left_end_align ();
543 Grob * col = (d == LEFT) ? columns_[0] : columns_.top();
545 if (extremes_[-d].bound_ != col)
547 y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
548 if (get_grob_direction (col) == dir_)
552 y = base_attachment[-d][Y_AXIS];
554 base_attachment[d] = Offset (x, y);
557 while (flip (&d) != LEFT);
559 return base_attachment;
563 Slur_score_state::generate_curves () const
565 Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
566 Real h_inf = staff_space_ *scm_to_double (slur_->get_property ("height-limit"));
567 for (int i = 0; i < scores_->size (); i++)
568 scores_->elem(i).generate_curve (*this, r_0, h_inf);
574 Array<Slur_configuration> *
575 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
578 Array<Slur_configuration> scores;
581 Drul_array<Offset> os;
582 os[LEFT] = base_attachments_[LEFT];
583 Real minimum_length = staff_space_
584 * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
586 for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
588 os[RIGHT] = base_attachments_[RIGHT];
589 for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
591 Slur_configuration s;
593 Drul_array<bool> attach_to_stem (false, false);
596 os[d][X_AXIS] = base_attachments_[d][X_AXIS];
597 if (extremes_[d].stem_
598 && !Stem::is_invisible (extremes_[d].stem_)
599 && extremes_[d].stem_dir_ == dir_)
601 Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
602 stem_y.widen (0.25 * staff_space_);
604 && stem_y.contains (os[d][Y_AXIS]))
606 os[d][X_AXIS] = extremes_[d].slur_head_extent_[-d]
608 attach_to_stem[d] = true;
610 else if (dir_ *extremes_[d].stem_extent_[Y_AXIS][dir_]
611 < dir_ * os[d][Y_AXIS]
612 && !extremes_[d].stem_extent_[X_AXIS].is_empty ()
615 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
618 while (flip (&d) != LEFT);
621 dz = os[RIGHT] - os[LEFT];
622 if (dz[X_AXIS] < minimum_length
623 || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_
628 if (extremes_[d].slur_head_)
630 os[d][X_AXIS] = extremes_[d].slur_head_extent_.center ();
631 attach_to_stem[d] = false;
634 while (flip (&d) != LEFT);
637 dz = os[RIGHT] - os[LEFT];
640 if (extremes_[d].slur_head_
641 && !attach_to_stem[d])
643 /* Horizontally move tilted slurs a little. Move
644 more for bigger tilts.
648 -= dir_ * extremes_[d].slur_head_extent_.length ()
649 * sin (dz.arg ()) / 3;
652 while (flip (&d) != LEFT);
657 os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
660 os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
663 assert (scores.size () > 0);
664 return new Array<Slur_configuration> (scores);
668 Array<Extra_collision_info>
669 Slur_score_state::get_extra_encompass_infos ( ) const
671 Link_array<Grob> encompasses
672 = Pointer_group_interface__extract_grobs (slur_, (Grob *)0,
673 "encompass-objects");
674 Array<Extra_collision_info> collision_infos;
675 for (int i = encompasses.size (); i--; )
677 if (Slur::has_interface (encompasses[i]))
679 Spanner * small_slur = dynamic_cast<Spanner*> (encompasses[i]);
680 Bezier b = Slur::get_curve (small_slur);
682 Offset relative (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
683 small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
685 for (int k = 0; k < 3; k++)
687 Direction hdir = Direction (k /2 - 1);
690 Only take bound into account if small slur starts
691 together with big slur.
693 if (hdir && small_slur->get_bound (hdir) != slur_->get_bound (hdir))
697 Offset z = b.curve_point ( k / 2.0);
702 yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
704 Interval xext (-1, 1);
705 xext = xext * (thickness_*2) + z[X_AXIS];
706 Extra_collision_info info (small_slur,
710 parameters_.extra_object_collision_);
711 collision_infos.push (info);
716 Grob *g = encompasses [i];
717 Interval xe = g->extent (common_[X_AXIS], X_AXIS);
718 Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
721 Real penalty = parameters_.extra_object_collision_;
722 if (Accidental_interface::has_interface (g))
724 penalty = parameters_.accidental_collision_;
725 /* Begin copy accidental.cc */
727 if (to_boolean (g->get_property ("cautionary")))
729 SCM cstyle = g->get_property ("cautionary-style");
730 parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
733 SCM accs = g->get_property ("accidentals");
734 SCM scm_style = g->get_property ("style");
735 if (!scm_is_symbol (scm_style)
737 && scm_ilength (accs) == 1)
739 /* End copy accidental.cc */
740 switch (scm_to_int (scm_car (accs)))
756 ye.widen (thickness_ * 0.5);
757 xe.widen (thickness_ * 1.0);
758 Extra_collision_info info (g, xp, xe, ye, penalty);
759 collision_infos.push (info);
763 return collision_infos;