]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur-scoring.cc
Better approximations for cross-staff slurs
[lilypond.git] / lily / slur-scoring.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
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.
11
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.
16
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/>.
19 */
20
21 #include "slur-scoring.hh"
22
23 #include <queue>
24
25 #include "axis-group-interface.hh"
26 #include "accidental-interface.hh"
27 #include "beam.hh"
28 #include "clef.hh"
29 #include "directional-element-interface.hh"
30 #include "dots.hh"
31 #include "libc-extension.hh"
32 #include "main.hh"
33 #include "misc.hh"
34 #include "note-column.hh"
35 #include "output-def.hh"
36 #include "paper-column.hh"
37 #include "pitch.hh"
38 #include "pointer-group-interface.hh"
39 #include "slur-configuration.hh"
40 #include "slur.hh"
41 #include "spanner.hh"
42 #include "staff-symbol-referencer.hh"
43 #include "staff-symbol.hh"
44 #include "stem.hh"
45 #include "time-signature.hh"
46 #include "warn.hh"
47
48 /*
49   TODO:
50
51   - curve around flag for y coordinate
52
53   - short-cut: try a smaller region first.
54
55   - handle non-visible stems better.
56
57   - try to prune number of scoring criteria
58
59   - take encompass-objects more into account when determining
60   slur shape
61
62   - calculate encompass scoring directly after determining slur shape.
63
64   - optimize.
65 */
66 struct Slur_score_state;
67
68 Slur_score_state::Slur_score_state ()
69 {
70   musical_dy_ = 0.0;
71   valid_ = false;
72   stub_ = false;
73   edge_has_beams_ = false;
74   has_same_beam_ = false;
75   is_broken_ = false;
76   dir_ = CENTER;
77   slur_ = 0;
78   common_[X_AXIS] = 0;
79   common_[Y_AXIS] = 0;
80 }
81
82 Slur_score_state::~Slur_score_state ()
83 {
84   junk_pointers (configurations_);
85 }
86
87 /*
88   If a slur is broken across a line break, the direction
89   of the post-break slur must be the same as the pre-break
90   slur.
91 */
92 Direction
93 Slur_score_state::slur_direction () const
94 {
95   Grob *left_neighbor = slur_->broken_neighbor (LEFT);
96
97   if (left_neighbor && left_neighbor->is_live ())
98     return get_grob_direction (left_neighbor);
99
100   Direction dir = get_grob_direction (slur_);
101
102   if (Grob *right_neighbor = slur_->broken_neighbor (RIGHT))
103     set_grob_direction (right_neighbor, dir);
104
105   return dir;
106 }
107
108 Encompass_info
109 Slur_score_state::get_encompass_info (Grob *col) const
110 {
111   Grob *stem = unsmob_grob (col->get_object ("stem"));
112   Encompass_info ei;
113
114   if (!stem)
115     {
116       programming_error ("no stem for note column");
117       ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
118       ei.head_ = ei.stem_ = col->maybe_pure_extent (common_[Y_AXIS],
119                                          Y_AXIS, stub_, 0, INT_MAX)[dir_];
120       return ei;
121     }
122   Direction stem_dir = get_grob_direction (stem);
123
124   if (Grob *head = Note_column::first_head (col))
125     ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
126   else
127     ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
128
129   Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
130   if (!h)
131     {
132       ei.head_ = ei.stem_ = col->maybe_pure_extent (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX)[dir_];
133       return ei;
134     }
135
136   ei.head_ = h->maybe_pure_extent (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX)[dir_];
137
138   if ((stem_dir == dir_)
139       && !stem->maybe_pure_extent (stem, Y_AXIS, stub_, 0, INT_MAX).is_empty ())
140     {
141       ei.stem_ = stem->maybe_pure_extent (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX)[dir_];
142       if (Grob *b = Stem::get_beam (stem))
143         ei.stem_ += stem_dir * 0.5 * Beam::get_beam_thickness (b);
144
145       Interval x = stem->extent (common_[X_AXIS], X_AXIS);
146       ei.x_ = x.is_empty ()
147               ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
148               : x.center ();
149     }
150   else
151     ei.stem_ = ei.head_;
152
153   return ei;
154 }
155
156 Drul_array<Bound_info>
157 Slur_score_state::get_bound_info () const
158 {
159   Drul_array<Bound_info> extremes;
160
161   Direction dir = dir_;
162
163   for (LEFT_and_RIGHT (d))
164     {
165       extremes[d].bound_ = slur_->get_bound (d);
166       if (Note_column::has_interface (extremes[d].bound_))
167         {
168           extremes[d].note_column_ = extremes[d].bound_;
169           extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
170           extremes[d].flag_ = Note_column::get_flag (extremes[d].note_column_);
171
172           if (extremes[d].stem_)
173             {
174               extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
175
176               for (int a = X_AXIS; a < NO_AXES; a++)
177                 {
178                   Axis ax = Axis (a);
179                   Interval s = ax == Y_AXIS
180                                ? extremes[d].stem_->maybe_pure_extent (common_[ax], ax, stub_, 0, INT_MAX)
181                                : extremes[d].stem_->extent (common_[ax], ax);
182                   if (extremes[d].flag_)
183                     s.unite (ax == Y_AXIS
184                              ? extremes[d].flag_->maybe_pure_extent (common_[ax], ax, stub_, 0, INT_MAX)
185                              : extremes[d].flag_->extent (common_[ax], ax));
186                   if (s.is_empty ())
187                     {
188                       /*
189                         do not issue warning. This happens for rests and
190                         whole notes.
191                       */
192                       s = Interval (0, 0)
193                           + (ax == Y_AXIS
194                              ? extremes[d].stem_->maybe_pure_coordinate (common_[ax], ax, stub_, 0, INT_MAX)
195                              : extremes[d].stem_->relative_coordinate (common_[ax], ax));
196                     }
197                   extremes[d].stem_extent_[ax] = s;
198                 }
199
200               extremes[d].slur_head_
201                 = Stem::extremal_heads (extremes[d].stem_)[dir];
202               if (!extremes[d].slur_head_
203                   && Note_column::has_rests (extremes[d].bound_))
204                 extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
205               extremes[d].staff_ = Staff_symbol_referencer
206                                    ::get_staff_symbol (extremes[d].stem_);
207               extremes[d].staff_space_ = Staff_symbol_referencer
208                                          ::staff_space (extremes[d].stem_);
209             }
210
211           if (extremes[d].slur_head_)
212             extremes[d].slur_head_x_extent_
213               = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
214
215         }
216     }
217
218   return extremes;
219 }
220
221 void
222 Slur_score_state::fill (Grob *me)
223 {
224   slur_ = dynamic_cast<Spanner *> (me);
225   stub_ = slur_->internal_has_interface (ly_symbol2scm ("cross-staff-stub-interface"));
226
227   columns_
228     = internal_extract_grob_array (me, ly_symbol2scm ("note-columns"));
229
230   if (columns_.empty ())
231     {
232       me->suicide ();
233       return;
234     }
235
236   Slur::replace_breakable_encompass_objects (me);
237   staff_space_ = Staff_symbol_referencer::staff_space (me);
238   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
239   thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
240
241   dir_ = slur_direction ();
242   parameters_.fill (me);
243
244   extract_grob_set (me, "note-columns", columns);
245   extract_grob_set (me, "encompass-objects", extra_objects);
246
247   Spanner *sp = dynamic_cast<Spanner *> (me);
248
249   for (int i = X_AXIS; i < NO_AXES; i++)
250     {
251       Axis a = (Axis)i;
252       common_[a] = common_refpoint_of_array (columns, me, a);
253       common_[a] = common_refpoint_of_array (extra_objects, common_[a], a);
254
255       for (LEFT_and_RIGHT (d))
256         {
257           /*
258             If bound is not in note-columns, we don't want to know about
259             its Y-position
260           */
261           if (a != Y_AXIS)
262             common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
263         }
264     }
265
266   extremes_ = get_bound_info ();
267   is_broken_ = (!extremes_[LEFT].note_column_
268                 || !extremes_[RIGHT].note_column_);
269
270   has_same_beam_
271     = (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
272        && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
273
274   base_attachments_ = get_base_attachments ();
275
276   Drul_array<Real> end_ys
277     = get_y_attachment_range ();
278
279   extra_encompass_infos_ = get_extra_encompass_infos ();
280
281   Interval additional_ys (0.0, 0.0);
282
283   for (vsize i = 0; i < extra_encompass_infos_.size (); i++)
284     {
285       if (extra_encompass_infos_[i].extents_[X_AXIS].is_empty ())
286         continue;
287
288       Real y_place = linear_interpolate (extra_encompass_infos_[i].extents_[X_AXIS].center (),
289                                          base_attachments_[RIGHT][X_AXIS],
290                                          base_attachments_[LEFT][X_AXIS],
291                                          end_ys[RIGHT],
292                                          end_ys[LEFT]);
293       Real encompass_place = extra_encompass_infos_[i].extents_[Y_AXIS][dir_];
294       if (extra_encompass_infos_[i].type_ == ly_symbol2scm ("inside")
295           && minmax (dir_, encompass_place, y_place) == encompass_place
296           && (!extra_encompass_infos_[i].grob_->internal_has_interface (ly_symbol2scm ("key-signature-interface"))
297               && !Clef::has_interface (extra_encompass_infos_[i].grob_)
298               && !Time_signature::has_interface (extra_encompass_infos_[i].grob_)))
299         {
300           for (LEFT_and_RIGHT (d))
301             additional_ys[d] = minmax (dir_,
302                                        additional_ys[d],
303                                        (dir_
304                                         * (parameters_.encompass_object_range_overshoot_
305                                            + (y_place - encompass_place)
306                                            * (normalize (extra_encompass_infos_[i].extents_[X_AXIS].center (),
307                                                          base_attachments_[RIGHT][X_AXIS],
308                                                          base_attachments_[LEFT][X_AXIS])
309                                               + (dir_ == LEFT ? 0 : -1)))));
310         }
311     }
312
313   for (LEFT_and_RIGHT (d))
314     end_ys[d] += additional_ys[d];
315
316   configurations_ = enumerate_attachments (end_ys);
317   for (vsize i = 0; i < columns_.size (); i++)
318     encompass_infos_.push_back (get_encompass_info (columns_[i]));
319
320   valid_ = true;
321
322   musical_dy_ = 0.0;
323   for (LEFT_and_RIGHT (d))
324     {
325       if (!is_broken_
326           && extremes_[d].slur_head_)
327         musical_dy_ += d
328                        * extremes_[d].slur_head_->maybe_pure_coordinate (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX);
329     }
330
331   edge_has_beams_
332     = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
333       || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
334
335   if (is_broken_)
336     musical_dy_ = 0.0;
337 }
338
339 MAKE_SCHEME_CALLBACK (Slur, calc_control_points, 1)
340 SCM
341 Slur::calc_control_points (SCM smob)
342 {
343   Spanner *me = unsmob_spanner (smob);
344
345   if (!to_boolean (me->get_property ("cross-staff"))
346       && me->internal_has_interface (ly_symbol2scm ("cross-staff-stub-interface")))
347     {
348       me->suicide ();
349       return SCM_EOL;
350     }
351
352   Slur_score_state state;
353   state.fill (me);
354
355   if (!state.valid_)
356     return SCM_EOL;
357
358   state.generate_curves ();
359
360   SCM end_ys = me->get_property ("positions");
361   SCM inspect_quants = me->get_property ("inspect-quants");
362   bool debug_slurs = to_boolean (me->layout ()
363                                  ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")));
364
365   if (is_number_pair (inspect_quants))
366     {
367       debug_slurs = true;
368       end_ys = inspect_quants;
369     }
370
371   Slur_configuration *best = NULL;
372   if (is_number_pair (end_ys))
373     best = state.get_forced_configuration (ly_scm2interval (end_ys));
374   else
375     best = state.get_best_curve ();
376
377 #if DEBUG_SLUR_SCORING
378   if (debug_slurs)
379     {
380       string total = best->card ();
381       total += to_string (" TOTAL=%.2f idx=%d", best->score (), best->index_);
382       me->set_property ("annotation", ly_string2scm (total));
383     }
384 #endif
385
386   SCM controls = SCM_EOL;
387   for (int i = 4; i--;)
388     {
389       Offset o = best->curve_.control_[i]
390                  - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
391                            me->maybe_pure_coordinate (state.common_[Y_AXIS], Y_AXIS,
392                                state.stub_, 0, INT_MAX));
393
394       controls = scm_cons (ly_offset2scm (o), controls);
395     }
396
397   return controls;
398 }
399
400 Slur_configuration *
401 Slur_score_state::get_forced_configuration (Interval ys) const
402 {
403   Slur_configuration *best = NULL;
404   Real mindist = 1e6;
405   for (vsize i = 0; i < configurations_.size (); i++)
406     {
407       Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ys[LEFT])
408                + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ys[RIGHT]);
409       if (d < mindist)
410         {
411           best = configurations_[i];
412           mindist = d;
413         }
414     }
415
416   while (!best->done ())
417     best->run_next_scorer (*this);
418
419   if (mindist > 1e5)
420     programming_error ("cannot find quant");
421
422   return best;
423 }
424
425 Slur_configuration *
426 Slur_score_state::get_best_curve () const
427 {
428   std::priority_queue < Slur_configuration *, std::vector<Slur_configuration *>,
429       Slur_configuration_less > queue;
430   for (vsize i = 0; i < configurations_.size (); i++)
431     queue.push (configurations_[i]);
432
433   Slur_configuration *best = NULL;
434   while (true)
435     {
436       best = queue.top ();
437       if (best->done ())
438         break;
439
440       queue.pop ();
441       best->run_next_scorer (*this);
442       queue.push (best);
443     }
444
445   return best;
446 }
447
448 Interval
449 Slur_score_state::breakable_bound_extent (Direction d) const
450 {
451   Grob *col = slur_->get_bound (d)->get_column ();
452   Interval ret;
453   ret.set_empty ();
454
455   extract_grob_set (slur_, "encompass-objects", extra_encompasses);
456
457   for (vsize i = 0; i < extra_encompasses.size (); i++)
458     {
459       Item *item = dynamic_cast<Item *> (extra_encompasses[i]);
460       if (item && col == item->get_column ())
461         ret.unite (robust_relative_extent (item, common_[X_AXIS], X_AXIS));
462     }
463
464   return ret;
465 }
466
467 /*
468   TODO: should analyse encompasses to determine sensible region, and
469   should limit slopes available.
470 */
471
472 Drul_array<Real>
473 Slur_score_state::get_y_attachment_range () const
474 {
475   Drul_array<Real> end_ys;
476   for (LEFT_and_RIGHT (d))
477     {
478       if (extremes_[d].note_column_)
479         {
480           end_ys[d] = dir_
481                       * max (max (dir_ * (base_attachments_[d][Y_AXIS]
482                                           + parameters_.region_size_ * dir_),
483                                   dir_ * (dir_ + extremes_[d].note_column_->maybe_pure_extent
484                                                    (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX)[dir_])),
485                              dir_ * base_attachments_[-d][Y_AXIS]);
486         }
487       else
488         end_ys[d] = base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_;
489     }
490
491   return end_ys;
492 }
493
494 bool
495 spanner_less (Spanner *s1, Spanner *s2)
496 {
497   Slice b1, b2;
498   for (LEFT_and_RIGHT (d))
499     {
500       b1[d] = s1->get_bound (d)->get_column ()->get_rank ();
501       b2[d] = s2->get_bound (d)->get_column ()->get_rank ();
502     }
503
504   return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
505          && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
506 }
507
508 Drul_array<Offset>
509 Slur_score_state::get_base_attachments () const
510 {
511   Drul_array<Offset> base_attachment;
512   for (LEFT_and_RIGHT (d))
513     {
514       Grob *stem = extremes_[d].stem_;
515       Grob *head = extremes_[d].slur_head_;
516
517       Real x = 0.0;
518       Real y = 0.0;
519       if (extremes_[d].note_column_)
520         {
521
522           /*
523             fixme: X coord should also be set in this case.
524           */
525           if (stem
526               && !Stem::is_invisible (stem)
527               && extremes_[d].stem_dir_ == dir_
528               && Stem::get_beaming (stem, -d)
529               && Stem::get_beam (stem)
530               && (!spanner_less (slur_, Stem::get_beam (stem))
531                   || has_same_beam_))
532             y = extremes_[d].stem_extent_[Y_AXIS][dir_];
533           else if (head)
534             y = head->maybe_pure_extent (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX)[dir_];
535           y += dir_ * 0.5 * staff_space_;
536
537           y = move_away_from_staffline (y, head);
538
539           Grob *fh = Note_column::first_head (extremes_[d].note_column_);
540           x
541             = (fh ? fh->extent (common_[X_AXIS], X_AXIS)
542                : extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS))
543               .linear_combination (CENTER);
544         }
545       base_attachment[d] = Offset (x, y);
546     }
547
548   for (LEFT_and_RIGHT (d))
549     {
550       if (!extremes_[d].note_column_)
551         {
552           Real x = 0;
553           Real y = 0;
554
555           Interval ext = breakable_bound_extent (d);
556           if (ext.is_empty ())
557             ext = Axis_group_interface::
558                   generic_bound_extent (extremes_[d].bound_,
559                                         common_[X_AXIS], X_AXIS);
560           x = ext[-d];
561
562           Grob *col = (d == LEFT) ? columns_[0] : columns_.back ();
563
564           if (extremes_[-d].bound_ != col)
565             {
566               y = maybe_pure_robust_relative_extent (col, common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX)[dir_];
567               y += dir_ * 0.5 * staff_space_;
568
569               if (get_grob_direction (col) == dir_
570                   && Note_column::get_stem (col)
571                   && !Stem::is_invisible (Note_column::get_stem (col)))
572                 y -= dir_ * 1.5 * staff_space_;
573             }
574           else
575             y = base_attachment[-d][Y_AXIS];
576
577           y = move_away_from_staffline (y, col);
578
579           base_attachment[d] = Offset (x, y);
580         }
581     }
582
583   for (LEFT_and_RIGHT (d))
584     {
585       for (int a = X_AXIS; a < NO_AXES; a++)
586         {
587           Real &b = base_attachment[d][Axis (a)];
588
589           if (isinf (b) || isnan (b))
590             {
591               programming_error ("slur attachment is inf/nan");
592               b = 0.0;
593             }
594         }
595     }
596
597   return base_attachment;
598 }
599
600 Real
601 Slur_score_state::move_away_from_staffline (Real y,
602                                             Grob *on_staff) const
603 {
604   if (!on_staff)
605     return y;
606
607   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (on_staff);
608   if (!staff_symbol)
609     return y;
610
611   Real pos
612     = (y - staff_symbol->maybe_pure_coordinate (common_[Y_AXIS],
613                                               Y_AXIS, stub_, 0, INT_MAX))
614       * 2.0 / staff_space_;
615
616   if (fabs (pos - my_round (pos)) < 0.2
617       && Staff_symbol_referencer::on_staff_line (on_staff, (int) rint (pos)))
618     y += 1.5 * staff_space_ * dir_ / 10;
619
620   return y;
621 }
622
623 vector<Offset>
624 Slur_score_state::generate_avoid_offsets () const
625 {
626   vector<Offset> avoid;
627   vector<Grob *> encompasses = columns_;
628
629   for (vsize i = 0; i < encompasses.size (); i++)
630     {
631       if (extremes_[LEFT].note_column_ == encompasses[i]
632           || extremes_[RIGHT].note_column_ == encompasses[i])
633         continue;
634
635       Encompass_info inf (get_encompass_info (encompasses[i]));
636       Real y = dir_ * (max (dir_ * inf.head_, dir_ * inf.stem_));
637
638       avoid.push_back (Offset (inf.x_, y + dir_ * parameters_.free_head_distance_));
639     }
640
641   extract_grob_set (slur_, "encompass-objects", extra_encompasses);
642   for (vsize i = 0; i < extra_encompasses.size (); i++)
643     {
644       if (Slur::has_interface (extra_encompasses[i]))
645         {
646           Grob *small_slur = extra_encompasses[i];
647           Bezier b = Slur::get_curve (small_slur);
648
649           Offset z = b.curve_point (0.5);
650           z += Offset (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
651                        small_slur->maybe_pure_coordinate (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX));
652
653           z[Y_AXIS] += dir_ * parameters_.free_slur_distance_;
654           avoid.push_back (z);
655         }
656       else if (extra_encompasses[i]->get_property ("avoid-slur") == ly_symbol2scm ("inside"))
657         {
658           Grob *g = extra_encompasses [i];
659           Interval xe = g->extent (common_[X_AXIS], X_AXIS);
660           Interval ye = g->maybe_pure_extent (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX);
661
662           if (!xe.is_empty ()
663               && !ye.is_empty ())
664             avoid.push_back (Offset (xe.center (), ye[dir_]));
665         }
666     }
667   return avoid;
668 }
669
670 void
671 Slur_score_state::generate_curves () const
672 {
673   Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
674   Real h_inf = staff_space_ * scm_to_double (slur_->get_property ("height-limit"));
675
676   vector<Offset> avoid = generate_avoid_offsets ();
677   for (vsize i = 0; i < configurations_.size (); i++)
678     configurations_[i]->generate_curve (*this, r_0, h_inf, avoid);
679 }
680
681 vector<Slur_configuration *>
682 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
683 {
684   vector<Slur_configuration *> scores;
685
686   Drul_array<Offset> os;
687   os[LEFT] = base_attachments_[LEFT];
688   Real minimum_length = staff_space_
689                         * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
690
691   for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
692     {
693       os[RIGHT] = base_attachments_[RIGHT];
694       for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
695         {
696
697           Drul_array<bool> attach_to_stem (false, false);
698           for (LEFT_and_RIGHT (d))
699             {
700               os[d][X_AXIS] = base_attachments_[d][X_AXIS];
701               if (extremes_[d].stem_
702                   && !Stem::is_invisible (extremes_[d].stem_)
703                   && extremes_[d].stem_dir_ == dir_)
704                 {
705                   Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
706                   stem_y.widen (0.25 * staff_space_);
707                   if (stem_y.contains (os[d][Y_AXIS]))
708                     {
709                       os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS][-d]
710                                       - d * 0.3;
711                       attach_to_stem[d] = true;
712                     }
713                   else if (dir_ * extremes_[d].stem_extent_[Y_AXIS][dir_]
714                            < dir_ * os[d][Y_AXIS]
715                            && !extremes_[d].stem_extent_[X_AXIS].is_empty ())
716
717                     os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
718                 }
719             }
720
721           Offset dz;
722           dz = os[RIGHT] - os[LEFT];
723           if (dz[X_AXIS] < minimum_length
724               || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_)
725             {
726               for (LEFT_and_RIGHT (d))
727                 {
728                   if (extremes_[d].slur_head_
729                       && !extremes_[d].slur_head_x_extent_.is_empty ())
730                     {
731                       os[d][X_AXIS] = extremes_[d].slur_head_x_extent_.center ();
732                       attach_to_stem[d] = false;
733                     }
734                 }
735             }
736
737           dz = os[RIGHT] - os[LEFT];
738           for (LEFT_and_RIGHT (d))
739             {
740               if (extremes_[d].slur_head_
741                   && !attach_to_stem[d])
742                 {
743                   /* Horizontally move tilted slurs a little.  Move
744                      more for bigger tilts.
745
746                      TODO: parameter */
747                   os[d][X_AXIS]
748                   -= dir_ * extremes_[d].slur_head_x_extent_.length ()
749                      * sin (dz.arg ()) / 3;
750                 }
751             }
752
753           scores.push_back (Slur_configuration::new_config (os, scores.size ()));
754
755           os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
756         }
757
758       os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
759     }
760
761   assert (scores.size () > 0);
762   return scores;
763 }
764
765 vector<Extra_collision_info>
766 Slur_score_state::get_extra_encompass_infos () const
767 {
768   extract_grob_set (slur_, "encompass-objects", encompasses);
769   vector<Extra_collision_info> collision_infos;
770   for (vsize i = encompasses.size (); i--;)
771     {
772       if (Slur::has_interface (encompasses[i]))
773         {
774           Spanner *small_slur = dynamic_cast<Spanner *> (encompasses[i]);
775           Bezier b = Slur::get_curve (small_slur);
776
777           Offset relative (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
778                            small_slur->maybe_pure_coordinate (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX));
779
780           for (int k = 0; k < 3; k++)
781             {
782               Direction hdir = Direction (k - 1);
783
784               /*
785                 Only take bound into account if small slur starts
786                 together with big slur.
787               */
788               if (hdir && small_slur->get_bound (hdir) != slur_->get_bound (hdir))
789                 continue;
790
791               Offset z = b.curve_point (k / 2.0);
792               z += relative;
793
794               Interval yext;
795               yext.set_full ();
796               yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
797
798               Interval xext (-1, 1);
799               xext = xext * (thickness_ * 2) + z[X_AXIS];
800               Extra_collision_info info (small_slur,
801                                          hdir,
802                                          xext,
803                                          yext,
804                                          parameters_.extra_object_collision_penalty_);
805               collision_infos.push_back (info);
806             }
807         }
808       else
809         {
810           Grob *g = encompasses [i];
811           Interval xe = g->extent (common_[X_AXIS], X_AXIS);
812           Interval ye = g->maybe_pure_extent (common_[Y_AXIS], Y_AXIS, stub_, 0, INT_MAX);
813           if (Dots::has_interface (g))
814             ye.widen (0.2);
815
816           Real xp = 0.0;
817           Real penalty = parameters_.extra_object_collision_penalty_;
818           if (Accidental_interface::has_interface (g))
819             {
820               penalty = parameters_.accidental_collision_;
821
822               Rational alt = ly_scm2rational (g->get_property ("alteration"));
823               SCM scm_style = g->get_property ("style");
824               if (!scm_is_symbol (scm_style)
825                   && !to_boolean (g->get_property ("parenthesized"))
826                   && !to_boolean (g->get_property ("restore-first")))
827                 {
828                   /* End copy accidental.cc */
829                   if (alt == FLAT_ALTERATION
830                       || alt == DOUBLE_FLAT_ALTERATION)
831                     xp = LEFT;
832                   else if (alt == SHARP_ALTERATION)
833                     xp = 0.5 * dir_;
834                   else if (alt == NATURAL_ALTERATION)
835                     xp = -dir_;
836                 }
837             }
838
839           ye.widen (thickness_ * 0.5);
840           xe.widen (thickness_ * 1.0);
841           Extra_collision_info info (g, xp, xe, ye, penalty);
842           collision_infos.push_back (info);
843         }
844     }
845
846   return collision_infos;
847 }
848
849 Extra_collision_info::Extra_collision_info (Grob *g, Real idx, Interval x, Interval y, Real p)
850 {
851   idx_ = idx;
852   extents_[X_AXIS] = x;
853   extents_[Y_AXIS] = y;
854   penalty_ = p;
855   grob_ = g;
856   type_ = g->get_property ("avoid-slur");
857 }
858
859 Extra_collision_info::Extra_collision_info ()
860 {
861   idx_ = 0.0;
862   penalty_ = 0.;
863   grob_ = 0;
864   type_ = SCM_EOL;
865 }