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