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