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