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