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