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;
68 Slur_score_state::~Slur_score_state ()
70 junk_pointers (configurations_);
74 get_detail (SCM alist, SCM sym)
76 SCM entry = scm_assq (sym, alist);
77 return robust_scm2double (scm_is_pair (entry)
84 Slur_score_parameters::fill (Grob *me)
86 SCM details = me->get_property ("slur-details");
89 = (int) get_detail (details, ly_symbol2scm ("region-size"));
90 head_encompass_penalty_
91 = get_detail (details, ly_symbol2scm ("head-encompass-penalty"));
92 stem_encompass_penalty_
93 = get_detail (details, ly_symbol2scm ("stem-encompass-penalty"));
95 = get_detail (details, ly_symbol2scm ("closeness-factor"));
96 edge_attraction_factor_
97 = get_detail (details, ly_symbol2scm ("edge-attraction-factor"));
99 = get_detail (details, ly_symbol2scm ("same-slope-penalty"));
100 steeper_slope_factor_
101 = get_detail (details, ly_symbol2scm ("steeper-slope-factor"));
102 non_horizontal_penalty_
103 = get_detail (details, ly_symbol2scm ("non-horizontal-penalty"));
105 = get_detail (details, ly_symbol2scm ("max-slope"));
107 = get_detail (details, ly_symbol2scm ("max-slope-factor"));
109 = get_detail (details, ly_symbol2scm ("free-head-distance"));
110 absolute_closeness_measure_
111 = get_detail (details, ly_symbol2scm ("absolute-closeness-measure"));
112 extra_object_collision_
113 = get_detail (details, ly_symbol2scm ("extra-object-collision"));
114 accidental_collision_
115 = get_detail (details, ly_symbol2scm ("accidental-collision"));
116 extra_encompass_free_distance_
117 = get_detail (details, ly_symbol2scm ("extra-encompass-free-distance"));
118 head_slur_distance_factor_
119 = get_detail (details, ly_symbol2scm ("head-slur-distance-factor"));
120 head_slur_distance_max_ratio_
121 = get_detail (details, ly_symbol2scm ("head-slur-distance-max-ratio"));
123 = get_detail (details, ly_symbol2scm ("free-slur-distance"));
125 = get_detail (details, ly_symbol2scm ("edge-slope-exponent"));
128 broken_trend_y (Slur_score_state const &state, Direction hdir)
130 /* A broken slur should maintain the same vertical trend
131 the unbroken slur would have had. */
133 if (Spanner *mother = dynamic_cast<Spanner*> (state.slur_->original_))
135 int k = broken_spanner_index (state.slur_);
137 if (j < 0 || j >= mother->broken_intos_.size ())
140 Grob *neighbor = mother->broken_intos_[j];
141 Spanner *common_mother
142 = dynamic_cast<Spanner*> (state.common_[Y_AXIS]->original_);
144 = broken_spanner_index (dynamic_cast<Spanner*> (state.common_[Y_AXIS]));
145 int common_j = common_k + hdir;
147 if (common_j < 0 || common_j >= common_mother->broken_intos_.size ())
150 Grob *common_next_system = common_mother->broken_intos_[common_j];
152 SCM last_point = scm_car (scm_last_pair (neighbor->get_property ("control-points")));
154 return scm_to_double (scm_cdr (last_point))
155 + neighbor->relative_coordinate (common_next_system, Y_AXIS);
162 copy slur dir forwards across line break.
165 Slur_score_state::set_next_direction ()
167 if (extremes_[RIGHT].note_column_)
170 if (Spanner *mother = dynamic_cast<Spanner*> (slur_->original_))
172 int k = broken_spanner_index (slur_);
174 if (j < 0 || j >= mother->broken_intos_.size ())
177 Grob *neighbor = mother->broken_intos_[j];
178 set_grob_direction (neighbor, dir_);
183 Slur_score_state::get_encompass_info (Grob *col) const
185 Grob *stem = unsmob_grob (col->get_property ("stem"));
190 programming_error ("No stem for note column?");
191 ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
192 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS],
196 Direction stem_dir = get_grob_direction (stem);
198 if (Grob *head = Note_column::first_head (col))
199 ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
201 ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
203 Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
206 ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS], Y_AXIS)[dir_];
210 ei.head_ = h->extent (common_[Y_AXIS], Y_AXIS)[dir_];
212 if ((stem_dir == dir_)
213 && !stem->extent (stem, Y_AXIS).is_empty ())
215 ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
216 if (Grob *b = Stem::get_beam (stem))
217 ei.stem_ += stem_dir * 0.5 * Beam::get_thickness (b);
219 Interval x = stem->extent (common_[X_AXIS], X_AXIS);
220 ei.x_ = x.is_empty ()
221 ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
233 Drul_array<Bound_info>
234 Slur_score_state::get_bound_info () const
236 Drul_array<Bound_info> extremes;
239 Direction dir = dir_;
243 extremes[d].bound_ = slur_->get_bound (d);
244 if (Note_column::has_interface (extremes[d].bound_))
246 extremes[d].note_column_ = extremes[d].bound_;
247 extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
248 extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
249 extremes[d].stem_extent_[X_AXIS]
250 = extremes[d].stem_->extent (common_[X_AXIS], X_AXIS);
251 extremes[d].stem_extent_[Y_AXIS]
252 = extremes[d].stem_->extent (common_[Y_AXIS], Y_AXIS);
253 extremes[d].slur_head_
254 = Stem::extremal_heads (extremes[d].stem_)[dir];
255 if (!extremes[d].slur_head_
256 && Note_column::has_rests (extremes[d].bound_))
258 extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
261 if (extremes[d].slur_head_)
262 extremes[d].slur_head_extent_
263 = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
265 extremes[d].staff_ = Staff_symbol_referencer
266 ::get_staff_symbol (extremes[d].stem_);
267 extremes[d].staff_space_ = Staff_symbol_referencer
268 ::staff_space (extremes[d].stem_);
271 while (flip (&d) != LEFT);
276 Slur_score_state::fill (Grob *me)
278 slur_ = dynamic_cast<Spanner*> (me);
280 = Pointer_group_interface__extract_grobs (me, (Grob *) 0, "note-columns");
282 if (columns_.is_empty ())
288 staff_space_ = Staff_symbol_referencer::staff_space (me);
289 Real lt = me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
290 thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
292 dir_ = get_grob_direction (me);
293 parameters_.fill (me);
295 SCM eltlist = me->get_property ("note-columns");
296 SCM extra_list = me->get_property ("encompass-objects");
297 Spanner *sp = dynamic_cast<Spanner*> (me);
299 for (int i = X_AXIS; i < NO_AXES; i++)
302 common_[a] = common_refpoint_of_list (eltlist, me, a);
303 common_[a] = common_refpoint_of_list (extra_list, common_[a], a);
307 common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
309 while (flip (&d) != LEFT);
312 extremes_ = get_bound_info ();
313 is_broken_ = (!extremes_[LEFT].note_column_
314 || !extremes_[RIGHT].note_column_);
317 base_attachments_ = get_base_attachments ();
319 Drul_array<Real> end_ys
320 = get_y_attachment_range ();
322 configurations_ = enumerate_attachments ( end_ys);
323 for (int i = 0; i < columns_.size (); i++)
324 encompass_infos_.push (get_encompass_info ( columns_[i]));
326 extra_encompass_infos_ = get_extra_encompass_infos ();
336 * extremes_[d].slur_head_->relative_coordinate (common_[Y_AXIS], Y_AXIS);
338 while (flip (&d) != LEFT);
341 = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
342 || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
345 (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
346 && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
349 set_next_direction ();
356 set_slur_control_points (Grob *me)
358 Slur_score_state state;
364 state.generate_curves ();
365 Bezier best (state.get_best_curve());
368 SCM controls = SCM_EOL;
369 for (int i = 4; i--;)
371 Offset o = best.control_[i]
372 - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
373 me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
374 controls = scm_cons (ly_offset2scm (o), controls);
376 me->set_property ("control-points", controls);
381 Slur_score_state::get_best_curve ()
383 for (int i = 0; i < configurations_.size (); i++)
385 configurations_[i]->score (*this);
390 for (int i = 0; i < configurations_.size (); i++)
392 if (configurations_[i]->score_ < opt)
394 opt = configurations_[i]->score_;
399 #if DEBUG_SLUR_SCORING
400 SCM inspect_quants = slur_->get_property ("inspect-quants");
401 if (to_boolean (slur_->get_paper ()
402 ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
403 && scm_is_pair (inspect_quants))
405 Drul_array<Real> ins = ly_scm2interval (inspect_quants);
407 for (int i = 0; i < configurations_.size (); i ++)
409 Real d =fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ins[LEFT])
410 + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
418 programming_error ("Could not find quant.");
421 configurations_[opt_idx]->score_card_ += to_string ("=%.2f", opt);
422 configurations_[opt_idx]->score_card_ += to_string ("i%d", opt_idx);
425 slur_->set_property ("quant-score",
426 scm_makfrom0str (configurations_[opt_idx]->score_card_.to_str0 ()));
430 return configurations_[opt_idx]->curve_;
434 TODO: should analyse encompasses to determine sensible region, and
435 should limit slopes available.
439 Slur_score_state::get_y_attachment_range () const
441 Drul_array<Real> end_ys;
445 if (extremes_[d].note_column_)
448 * ((dir_ * (base_attachments_[d][Y_AXIS] + parameters_.region_size_* dir_))
449 >? (dir_ * (dir_ + extremes_[d].note_column_->extent (common_[Y_AXIS],
451 >? (dir_ * base_attachments_[-d][Y_AXIS]));
454 end_ys[d] = base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_;
456 while (flip (&d) != LEFT);
462 spanner_less (Spanner *s1, Spanner* s2)
468 b1[d] = s1->get_bound (d)->get_column ()->rank_;
469 b2[d] = s2->get_bound (d)->get_column ()->rank_;
470 } while (flip (&d) != LEFT);
472 return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
473 && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
478 Slur_score_state::get_base_attachments () const
480 Drul_array<Offset> base_attachment;
484 Grob *stem = extremes_[d].stem_;
485 Grob *head = extremes_[d].slur_head_;
489 if (extremes_[d].note_column_)
493 fixme: X coord should also be set in this case.
496 && extremes_[d].stem_dir_ == dir_
497 && Stem::get_beaming (stem, -d)
498 && (!spanner_less (slur_, Stem::get_beam (stem))
500 y = extremes_[d].stem_extent_[Y_AXIS][dir_];
502 y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
503 y += dir_ * 0.5 * staff_space_;
506 = (y - extremes_[d].staff_->relative_coordinate (common_[Y_AXIS],
508 * 2.0 / staff_space_;
510 /* start off staffline. */
511 if (fabs (pos - my_round (pos)) < 0.2
512 && Staff_symbol_referencer::on_staffline (head, (int) rint (pos))
513 && Staff_symbol_referencer::line_count (head) - 1 >= rint (pos)
515 // TODO: calc from slur thick & line thick, parameter.
516 y += 1.5 * staff_space_ * dir_ / 10;
518 Grob * fh = Note_column::first_head (extremes_[d].note_column_);
520 (fh ? fh->extent (common_[X_AXIS], X_AXIS)
521 : extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS))
522 .linear_combination (CENTER);
524 base_attachment[d] = Offset (x, y);
526 } while (flip (&d) != LEFT);
530 if (!extremes_[d].note_column_)
535 x = extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS)[d];
539 x = slur_->get_broken_left_end_align ();
541 Grob * col = (d == LEFT) ? columns_[0] : columns_.top();
543 if (extremes_[-d].bound_ != col)
545 y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
546 if (get_grob_direction (col) == dir_)
550 y = base_attachment[-d][Y_AXIS];
552 base_attachment[d] = Offset (x, y);
555 while (flip (&d) != LEFT);
557 return base_attachment;
561 Slur_score_state::generate_curves () const
563 Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
564 Real h_inf = staff_space_ *scm_to_double (slur_->get_property ("height-limit"));
565 for (int i = 0; i < configurations_.size (); i++)
566 configurations_[i]->generate_curve (*this, r_0, h_inf);
572 Link_array<Slur_configuration>
573 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
576 Link_array<Slur_configuration> scores;
579 Drul_array<Offset> os;
580 os[LEFT] = base_attachments_[LEFT];
581 Real minimum_length = staff_space_
582 * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
584 for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
586 os[RIGHT] = base_attachments_[RIGHT];
587 for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
589 Slur_configuration s;
591 Drul_array<bool> attach_to_stem (false, false);
594 os[d][X_AXIS] = base_attachments_[d][X_AXIS];
595 if (extremes_[d].stem_
596 && !Stem::is_invisible (extremes_[d].stem_)
597 && extremes_[d].stem_dir_ == dir_)
599 Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
600 stem_y.widen (0.25 * staff_space_);
602 && stem_y.contains (os[d][Y_AXIS]))
604 os[d][X_AXIS] = extremes_[d].slur_head_extent_[-d]
606 attach_to_stem[d] = true;
608 else if (dir_ *extremes_[d].stem_extent_[Y_AXIS][dir_]
609 < dir_ * os[d][Y_AXIS]
610 && !extremes_[d].stem_extent_[X_AXIS].is_empty ()
613 os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
616 while (flip (&d) != LEFT);
619 dz = os[RIGHT] - os[LEFT];
620 if (dz[X_AXIS] < minimum_length
621 || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_
626 if (extremes_[d].slur_head_)
628 os[d][X_AXIS] = extremes_[d].slur_head_extent_.center ();
629 attach_to_stem[d] = false;
632 while (flip (&d) != LEFT);
635 dz = os[RIGHT] - os[LEFT];
638 if (extremes_[d].slur_head_
639 && !attach_to_stem[d])
641 /* Horizontally move tilted slurs a little. Move
642 more for bigger tilts.
646 -= dir_ * extremes_[d].slur_head_extent_.length ()
647 * sin (dz.arg ()) / 3;
650 while (flip (&d) != LEFT);
653 s.index_ = scores.size ();
655 scores.push (new Slur_configuration (s));
657 os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
660 os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
663 assert (scores.size () > 0);
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))
696 Offset z = b.curve_point (k / 2.0);
701 yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
703 Interval xext (-1, 1);
704 xext = xext * (thickness_*2) + z[X_AXIS];
705 Extra_collision_info info (small_slur,
709 parameters_.extra_object_collision_);
710 collision_infos.push (info);
715 Grob *g = encompasses [i];
716 Interval xe = g->extent (common_[X_AXIS], X_AXIS);
717 Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
720 Real penalty = parameters_.extra_object_collision_;
721 if (Accidental_interface::has_interface (g))
723 penalty = parameters_.accidental_collision_;
724 /* Begin copy accidental.cc */
726 if (to_boolean (g->get_property ("cautionary")))
728 SCM cstyle = g->get_property ("cautionary-style");
729 parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
732 SCM accs = g->get_property ("accidentals");
733 SCM scm_style = g->get_property ("style");
734 if (!scm_is_symbol (scm_style)
736 && scm_ilength (accs) == 1)
738 /* End copy accidental.cc */
739 switch (scm_to_int (scm_car (accs)))
755 ye.widen (thickness_ * 0.5);
756 xe.widen (thickness_ * 1.0);
757 Extra_collision_info info (g, xp, xe, ye, penalty);
758 collision_infos.push (info);
762 return collision_infos;