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