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