2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
22 #include "slur-scoring.hh"
24 #include "accidental-interface.hh"
26 #include "directional-element-interface.hh"
27 #include "libc-extension.hh"
29 #include "note-column.hh"
30 #include "output-def.hh"
31 #include "paper-column.hh"
33 #include "pointer-group-interface.hh"
34 #include "slur-configuration.hh"
37 #include "staff-symbol-referencer.hh"
38 #include "staff-symbol.hh"
45 - curve around flag for y coordinate
47 - short-cut: try a smaller region first.
49 - handle non-visible stems better.
51 - try to prune number of scoring criteria
53 - take encompass-objects more into account when determining
56 - calculate encompass scoring directly after determining slur shape.
60 struct Slur_score_state
;
62 Slur_score_state::Slur_score_state ()
66 edge_has_beams_
= false;
67 has_same_beam_
= false;
75 Slur_score_state::~Slur_score_state ()
77 junk_pointers (configurations_
);
81 copy slur dir forwards across line break.
84 Slur_score_state::set_next_direction ()
86 if (extremes_
[RIGHT
].note_column_
)
89 if (Grob
*neighbor
= slur_
->broken_neighbor (RIGHT
))
91 set_grob_direction (neighbor
, dir_
);
96 Slur_score_state::get_encompass_info (Grob
*col
) const
98 Grob
*stem
= unsmob_grob (col
->get_object ("stem"));
103 programming_error ("no stem for note column");
104 ei
.x_
= col
->relative_coordinate (common_
[X_AXIS
], X_AXIS
);
105 ei
.head_
= ei
.stem_
= col
->extent (common_
[Y_AXIS
],
109 Direction stem_dir
= get_grob_direction (stem
);
111 if (Grob
*head
= Note_column::first_head (col
))
112 ei
.x_
= head
->extent (common_
[X_AXIS
], X_AXIS
).center ();
114 ei
.x_
= col
->extent (common_
[X_AXIS
], X_AXIS
).center ();
116 Grob
*h
= Stem::extremal_heads (stem
)[Direction (dir_
)];
119 ei
.head_
= ei
.stem_
= col
->extent (common_
[Y_AXIS
], Y_AXIS
)[dir_
];
123 ei
.head_
= h
->extent (common_
[Y_AXIS
], Y_AXIS
)[dir_
];
125 if ((stem_dir
== dir_
)
126 && !stem
->extent (stem
, Y_AXIS
).is_empty ())
128 ei
.stem_
= stem
->extent (common_
[Y_AXIS
], Y_AXIS
)[dir_
];
129 if (Grob
*b
= Stem::get_beam (stem
))
130 ei
.stem_
+= stem_dir
* 0.5 * Beam::get_beam_thickness (b
);
132 Interval x
= stem
->extent (common_
[X_AXIS
], X_AXIS
);
133 ei
.x_
= x
.is_empty ()
134 ? stem
->relative_coordinate (common_
[X_AXIS
], X_AXIS
)
143 Drul_array
<Bound_info
>
144 Slur_score_state::get_bound_info () const
146 Drul_array
<Bound_info
> extremes
;
149 Direction dir
= dir_
;
153 extremes
[d
].bound_
= slur_
->get_bound (d
);
154 if (Note_column::has_interface (extremes
[d
].bound_
))
156 extremes
[d
].note_column_
= extremes
[d
].bound_
;
157 extremes
[d
].stem_
= Note_column::get_stem (extremes
[d
].note_column_
);
158 if (extremes
[d
].stem_
)
160 extremes
[d
].stem_dir_
= get_grob_direction (extremes
[d
].stem_
);
162 for (int a
= X_AXIS
; a
< NO_AXES
; a
++)
165 Interval s
= extremes
[d
].stem_
->extent (common_
[ax
], ax
);
169 do not issue warning. This happens for rests and
173 + extremes
[d
].stem_
->relative_coordinate (common_
[ax
], ax
);
175 extremes
[d
].stem_extent_
[ax
] = s
;
178 extremes
[d
].slur_head_
179 = Stem::extremal_heads (extremes
[d
].stem_
)[dir
];
180 if (!extremes
[d
].slur_head_
181 && Note_column::has_rests (extremes
[d
].bound_
))
182 extremes
[d
].slur_head_
= Note_column::get_rest (extremes
[d
].bound_
);
183 extremes
[d
].staff_
= Staff_symbol_referencer
184 ::get_staff_symbol (extremes
[d
].stem_
);
185 extremes
[d
].staff_space_
= Staff_symbol_referencer
186 ::staff_space (extremes
[d
].stem_
);
189 if (extremes
[d
].slur_head_
)
190 extremes
[d
].slur_head_x_extent_
191 = extremes
[d
].slur_head_
->extent (common_
[X_AXIS
], X_AXIS
);
195 while (flip (&d
) != LEFT
);
201 Slur_score_state::fill (Grob
*me
)
203 slur_
= dynamic_cast<Spanner
*> (me
);
205 = internal_extract_grob_array (me
, ly_symbol2scm ("note-columns"));
207 if (columns_
.empty ())
213 Slur::replace_breakable_encompass_objects (me
);
214 staff_space_
= Staff_symbol_referencer::staff_space (me
);
215 Real lt
= me
->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
216 thickness_
= robust_scm2double (me
->get_property ("thickness"), 1.0) * lt
;
218 dir_
= get_grob_direction (me
);
219 parameters_
.fill (me
);
221 extract_grob_set (me
, "note-columns", columns
);
222 extract_grob_set (me
, "encompass-objects", extra_objects
);
224 Spanner
*sp
= dynamic_cast<Spanner
*> (me
);
226 for (int i
= X_AXIS
; i
< NO_AXES
; i
++)
229 common_
[a
] = common_refpoint_of_array (columns
, me
, a
);
230 common_
[a
] = common_refpoint_of_array (extra_objects
, common_
[a
], a
);
236 If bound is not in note-columns, we don't want to know about
240 common_
[a
] = common_
[a
]->common_refpoint (sp
->get_bound (d
), a
);
242 while (flip (&d
) != LEFT
);
245 extremes_
= get_bound_info ();
246 is_broken_
= (!extremes_
[LEFT
].note_column_
247 || !extremes_
[RIGHT
].note_column_
);
250 = (extremes_
[LEFT
].stem_
&& extremes_
[RIGHT
].stem_
251 && Stem::get_beam (extremes_
[LEFT
].stem_
) == Stem::get_beam (extremes_
[RIGHT
].stem_
));
253 base_attachments_
= get_base_attachments ();
255 Drul_array
<Real
> end_ys
256 = get_y_attachment_range ();
258 configurations_
= enumerate_attachments (end_ys
);
259 for (vsize i
= 0; i
< columns_
.size (); i
++)
260 encompass_infos_
.push_back (get_encompass_info (columns_
[i
]));
262 extra_encompass_infos_
= get_extra_encompass_infos ();
270 && extremes_
[d
].slur_head_
)
272 * extremes_
[d
].slur_head_
->relative_coordinate (common_
[Y_AXIS
], Y_AXIS
);
274 while (flip (&d
) != LEFT
);
277 = (extremes_
[LEFT
].stem_
&& Stem::get_beam (extremes_
[LEFT
].stem_
))
278 || (extremes_
[RIGHT
].stem_
&& Stem::get_beam (extremes_
[RIGHT
].stem_
));
280 set_next_direction ();
287 MAKE_SCHEME_CALLBACK (Slur
, calc_control_points
, 1)
289 Slur::calc_control_points (SCM smob
)
291 Spanner
*me
= unsmob_spanner (smob
);
293 Slur_score_state state
;
299 state
.generate_curves ();
301 SCM end_ys
= me
->get_property ("positions");
304 if (is_number_pair (end_ys
))
305 best
= state
.configurations_
[state
.get_closest_index (end_ys
)]->curve_
;
307 best
= state
.get_best_curve ();
309 SCM controls
= SCM_EOL
;
310 for (int i
= 4; i
--;)
312 Offset o
= best
.control_
[i
]
313 - Offset (me
->relative_coordinate (state
.common_
[X_AXIS
], X_AXIS
),
314 me
->relative_coordinate (state
.common_
[Y_AXIS
], Y_AXIS
));
315 controls
= scm_cons (ly_offset2scm (o
), controls
);
322 Slur_score_state::get_best_curve ()
327 #if DEBUG_SLUR_SCORING
328 bool debug_slurs
= to_boolean (slur_
->layout ()
329 ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")));
330 SCM inspect_quants
= slur_
->get_property ("inspect-quants");
331 SCM inspect_index
= slur_
->get_property ("inspect-index");
333 && scm_is_integer (inspect_index
))
335 opt_idx
= scm_to_int (inspect_index
);
336 configurations_
[opt_idx
]->calculate_score (*this);
337 opt
= configurations_
[opt_idx
]->score ();
340 && scm_is_pair (inspect_quants
))
342 opt_idx
= get_closest_index (inspect_quants
);
343 configurations_
[opt_idx
]->calculate_score (*this);
344 opt
= configurations_
[opt_idx
]->score ();
349 for (vsize i
= 0; i
< configurations_
.size (); i
++)
350 configurations_
[i
]->calculate_score (*this);
351 for (vsize i
= 0; i
< configurations_
.size (); i
++)
353 if (configurations_
[i
]->score () < opt
)
355 opt
= configurations_
[i
]->score ();
361 #if DEBUG_SLUR_SCORING
367 total
= configurations_
[opt_idx
]->card ();
368 total
+= to_string (" TOTAL=%.2f idx=%d", configurations_
[opt_idx
]->score (), opt_idx
);
375 slur_
->set_property ("quant-score",
376 ly_string2scm (total
));
383 programming_error ("No optimal slur found. Guessing 0.");
386 return configurations_
[opt_idx
]->curve_
;
390 Slur_score_state::breakable_bound_item (Direction d
) const
392 Grob
*col
= slur_
->get_bound (d
)->get_column ();
394 extract_grob_set (slur_
, "encompass-objects", extra_encompasses
);
396 for (vsize i
= 0; i
< extra_encompasses
.size (); i
++)
398 Item
*item
= dynamic_cast<Item
*> (extra_encompasses
[i
]);
399 if (item
&& col
== item
->get_column ())
407 Slur_score_state::get_closest_index (SCM inspect_quants
) const
409 Drul_array
<Real
> ins
= ly_scm2interval (inspect_quants
);
413 for (vsize i
= 0; i
< configurations_
.size (); i
++)
415 Real d
= fabs (configurations_
[i
]->attachment_
[LEFT
][Y_AXIS
] - ins
[LEFT
])
416 + fabs (configurations_
[i
]->attachment_
[RIGHT
][Y_AXIS
] - ins
[RIGHT
]);
424 programming_error ("cannot find quant");
429 TODO: should analyse encompasses to determine sensible region, and
430 should limit slopes available.
434 Slur_score_state::get_y_attachment_range () const
436 Drul_array
<Real
> end_ys
;
440 if (extremes_
[d
].note_column_
)
443 * max (max (dir_
* (base_attachments_
[d
][Y_AXIS
]
444 + parameters_
.region_size_
* dir_
),
445 dir_
* (dir_
+ extremes_
[d
].note_column_
->extent (common_
[Y_AXIS
], Y_AXIS
)[dir_
])),
446 dir_
* base_attachments_
[-d
][Y_AXIS
]);
449 end_ys
[d
] = base_attachments_
[d
][Y_AXIS
] + parameters_
.region_size_
* dir_
;
451 while (flip (&d
) != LEFT
);
457 spanner_less (Spanner
*s1
, Spanner
*s2
)
463 b1
[d
] = s1
->get_bound (d
)->get_column ()->get_rank ();
464 b2
[d
] = s2
->get_bound (d
)->get_column ()->get_rank ();
466 while (flip (&d
) != LEFT
);
468 return b2
[LEFT
] <= b1
[LEFT
] && b2
[RIGHT
] >= b1
[RIGHT
]
469 && (b2
[LEFT
] != b1
[LEFT
] || b2
[RIGHT
] != b1
[RIGHT
]);
473 Slur_score_state::get_base_attachments () const
475 Drul_array
<Offset
> base_attachment
;
479 Grob
*stem
= extremes_
[d
].stem_
;
480 Grob
*head
= extremes_
[d
].slur_head_
;
484 if (extremes_
[d
].note_column_
)
488 fixme: X coord should also be set in this case.
491 && !Stem::is_invisible (stem
)
492 && extremes_
[d
].stem_dir_
== dir_
493 && Stem::get_beaming (stem
, -d
)
494 && Stem::get_beam (stem
)
495 && (!spanner_less (slur_
, Stem::get_beam (stem
))
497 y
= extremes_
[d
].stem_extent_
[Y_AXIS
][dir_
];
499 y
= head
->extent (common_
[Y_AXIS
], Y_AXIS
)[dir_
];
500 y
+= dir_
* 0.5 * staff_space_
;
502 y
= move_away_from_staffline (y
, head
);
504 Grob
*fh
= Note_column::first_head (extremes_
[d
].note_column_
);
506 = (fh
? fh
->extent (common_
[X_AXIS
], X_AXIS
)
507 : extremes_
[d
].bound_
->extent (common_
[X_AXIS
], X_AXIS
))
508 .linear_combination (CENTER
);
510 base_attachment
[d
] = Offset (x
, y
);
512 while (flip (&d
) != LEFT
);
516 if (!extremes_
[d
].note_column_
)
521 if (Grob
*g
= breakable_bound_item (d
))
523 x
= robust_relative_extent (g
, common_
[X_AXIS
], X_AXIS
)[RIGHT
];
526 x
= robust_relative_extent (extremes_
[d
].bound_
, common_
[X_AXIS
], X_AXIS
)[d
];
528 x
= slur_
->get_broken_left_end_align ();
530 Grob
*col
= (d
== LEFT
) ? columns_
[0] : columns_
.back ();
532 if (extremes_
[-d
].bound_
!= col
)
534 y
= robust_relative_extent (col
, common_
[Y_AXIS
], Y_AXIS
)[dir_
];
535 y
+= dir_
* 0.5 * staff_space_
;
537 if (get_grob_direction (col
) == dir_
538 && Note_column::get_stem (col
)
539 && !Stem::is_invisible (Note_column::get_stem (col
)))
540 y
-= dir_
* 1.5 * staff_space_
;
543 y
= base_attachment
[-d
][Y_AXIS
];
545 y
= move_away_from_staffline (y
, col
);
547 base_attachment
[d
] = Offset (x
, y
);
550 while (flip (&d
) != LEFT
);
554 for (int a
= X_AXIS
; a
< NO_AXES
; a
++)
556 Real
&b
= base_attachment
[d
][Axis (a
)];
558 if (isinf (b
) || isnan (b
))
560 programming_error ("slur attachment is inf/nan");
565 while (flip (&d
) != LEFT
);
567 return base_attachment
;
571 Slur_score_state::move_away_from_staffline (Real y
,
572 Grob
*on_staff
) const
577 Grob
*staff_symbol
= Staff_symbol_referencer::get_staff_symbol (on_staff
);
582 = (y
- staff_symbol
->relative_coordinate (common_
[Y_AXIS
],
584 * 2.0 / staff_space_
;
586 if (fabs (pos
- my_round (pos
)) < 0.2
587 && Staff_symbol_referencer::on_line (on_staff
, (int) rint (pos
))
588 && Staff_symbol_referencer::line_count (on_staff
) - 1 >= rint (pos
))
589 y
+= 1.5 * staff_space_
* dir_
/ 10;
595 Slur_score_state::generate_avoid_offsets () const
597 vector
<Offset
> avoid
;
598 vector
<Grob
*> encompasses
= columns_
;
600 for (vsize i
= 0; i
< encompasses
.size (); i
++)
602 if (extremes_
[LEFT
].note_column_
== encompasses
[i
]
603 || extremes_
[RIGHT
].note_column_
== encompasses
[i
])
606 Encompass_info
inf (get_encompass_info (encompasses
[i
]));
607 Real y
= dir_
* (max (dir_
* inf
.head_
, dir_
* inf
.stem_
));
609 avoid
.push_back (Offset (inf
.x_
, y
+ dir_
* parameters_
.free_head_distance_
));
612 extract_grob_set (slur_
, "encompass-objects", extra_encompasses
);
613 for (vsize i
= 0; i
< extra_encompasses
.size (); i
++)
615 if (Slur::has_interface (extra_encompasses
[i
]))
617 Grob
*small_slur
= extra_encompasses
[i
];
618 Bezier b
= Slur::get_curve (small_slur
);
620 Offset z
= b
.curve_point (0.5);
621 z
+= Offset (small_slur
->relative_coordinate (common_
[X_AXIS
], X_AXIS
),
622 small_slur
->relative_coordinate (common_
[Y_AXIS
], Y_AXIS
));
624 z
[Y_AXIS
] += dir_
* parameters_
.free_slur_distance_
;
627 else if (extra_encompasses
[i
]->get_property ("avoid-slur") == ly_symbol2scm ("inside"))
629 Grob
*g
= extra_encompasses
[i
];
630 Interval xe
= g
->extent (common_
[X_AXIS
], X_AXIS
);
631 Interval ye
= g
->extent (common_
[Y_AXIS
], Y_AXIS
);
635 avoid
.push_back (Offset (xe
.center (), ye
[dir_
]));
642 Slur_score_state::generate_curves () const
644 Real r_0
= robust_scm2double (slur_
->get_property ("ratio"), 0.33);
645 Real h_inf
= staff_space_
* scm_to_double (slur_
->get_property ("height-limit"));
647 vector
<Offset
> avoid
= generate_avoid_offsets ();
648 for (vsize i
= 0; i
< configurations_
.size (); i
++)
649 configurations_
[i
]->generate_curve (*this, r_0
, h_inf
, avoid
);
652 vector
<Slur_configuration
*>
653 Slur_score_state::enumerate_attachments (Drul_array
<Real
> end_ys
) const
655 vector
<Slur_configuration
*> scores
;
657 Drul_array
<Offset
> os
;
658 os
[LEFT
] = base_attachments_
[LEFT
];
659 Real minimum_length
= staff_space_
660 * robust_scm2double (slur_
->get_property ("minimum-length"), 2.0);
662 for (int i
= 0; dir_
* os
[LEFT
][Y_AXIS
] <= dir_
* end_ys
[LEFT
]; i
++)
664 os
[RIGHT
] = base_attachments_
[RIGHT
];
665 for (int j
= 0; dir_
* os
[RIGHT
][Y_AXIS
] <= dir_
* end_ys
[RIGHT
]; j
++)
667 Slur_configuration s
;
669 Drul_array
<bool> attach_to_stem (false, false);
672 os
[d
][X_AXIS
] = base_attachments_
[d
][X_AXIS
];
673 if (extremes_
[d
].stem_
674 && !Stem::is_invisible (extremes_
[d
].stem_
)
675 && extremes_
[d
].stem_dir_
== dir_
)
677 Interval stem_y
= extremes_
[d
].stem_extent_
[Y_AXIS
];
678 stem_y
.widen (0.25 * staff_space_
);
679 if (stem_y
.contains (os
[d
][Y_AXIS
]))
681 os
[d
][X_AXIS
] = extremes_
[d
].stem_extent_
[X_AXIS
][-d
]
683 attach_to_stem
[d
] = true;
685 else if (dir_
* extremes_
[d
].stem_extent_
[Y_AXIS
][dir_
]
686 < dir_
* os
[d
][Y_AXIS
]
687 && !extremes_
[d
].stem_extent_
[X_AXIS
].is_empty ())
689 os
[d
][X_AXIS
] = extremes_
[d
].stem_extent_
[X_AXIS
].center ();
692 while (flip (&d
) != LEFT
);
695 dz
= os
[RIGHT
] - os
[LEFT
];
696 if (dz
[X_AXIS
] < minimum_length
697 || fabs (dz
[Y_AXIS
] / dz
[X_AXIS
]) > parameters_
.max_slope_
)
701 if (extremes_
[d
].slur_head_
702 && !extremes_
[d
].slur_head_x_extent_
.is_empty ())
704 os
[d
][X_AXIS
] = extremes_
[d
].slur_head_x_extent_
.center ();
705 attach_to_stem
[d
] = false;
708 while (flip (&d
) != LEFT
);
711 dz
= os
[RIGHT
] - os
[LEFT
];
714 if (extremes_
[d
].slur_head_
715 && !attach_to_stem
[d
])
717 /* Horizontally move tilted slurs a little. Move
718 more for bigger tilts.
722 -= dir_
* extremes_
[d
].slur_head_x_extent_
.length ()
723 * sin (dz
.arg ()) / 3;
726 while (flip (&d
) != LEFT
);
729 s
.index_
= scores
.size ();
731 scores
.push_back (new Slur_configuration (s
));
733 os
[RIGHT
][Y_AXIS
] += dir_
* staff_space_
/ 2;
736 os
[LEFT
][Y_AXIS
] += dir_
* staff_space_
/ 2;
739 assert (scores
.size () > 0);
743 vector
<Extra_collision_info
>
744 Slur_score_state::get_extra_encompass_infos () const
746 extract_grob_set (slur_
, "encompass-objects", encompasses
);
747 vector
<Extra_collision_info
> collision_infos
;
748 for (vsize i
= encompasses
.size (); i
--;)
750 if (Slur::has_interface (encompasses
[i
]))
752 Spanner
*small_slur
= dynamic_cast<Spanner
*> (encompasses
[i
]);
753 Bezier b
= Slur::get_curve (small_slur
);
755 Offset
relative (small_slur
->relative_coordinate (common_
[X_AXIS
], X_AXIS
),
756 small_slur
->relative_coordinate (common_
[Y_AXIS
], Y_AXIS
));
758 for (int k
= 0; k
< 3; k
++)
760 Direction hdir
= Direction (k
- 1);
763 Only take bound into account if small slur starts
764 together with big slur.
766 if (hdir
&& small_slur
->get_bound (hdir
) != slur_
->get_bound (hdir
))
769 Offset z
= b
.curve_point (k
/ 2.0);
774 yext
[dir_
] = z
[Y_AXIS
] + dir_
* thickness_
* 1.0;
776 Interval
xext (-1, 1);
777 xext
= xext
* (thickness_
* 2) + z
[X_AXIS
];
778 Extra_collision_info
info (small_slur
,
782 parameters_
.extra_object_collision_penalty_
);
783 collision_infos
.push_back (info
);
788 Grob
*g
= encompasses
[i
];
789 Interval xe
= g
->extent (common_
[X_AXIS
], X_AXIS
);
790 Interval ye
= g
->extent (common_
[Y_AXIS
], Y_AXIS
);
793 Real penalty
= parameters_
.extra_object_collision_penalty_
;
794 if (Accidental_interface::has_interface (g
))
796 penalty
= parameters_
.accidental_collision_
;
798 Rational alt
= ly_scm2rational (g
->get_property ("alteration"));
799 SCM scm_style
= g
->get_property ("style");
800 if (!scm_is_symbol (scm_style
)
801 && !to_boolean (g
->get_property ("parenthesized"))
802 && !to_boolean (g
->get_property ("restore-first")))
804 /* End copy accidental.cc */
805 if (alt
== FLAT_ALTERATION
806 || alt
== DOUBLE_FLAT_ALTERATION
)
808 else if (alt
== SHARP_ALTERATION
)
810 else if (alt
== NATURAL_ALTERATION
)
815 ye
.widen (thickness_
* 0.5);
816 xe
.widen (thickness_
* 1.0);
817 Extra_collision_info
info (g
, xp
, xe
, ye
, penalty
);
818 collision_infos
.push_back (info
);
822 return collision_infos
;
825 Extra_collision_info::Extra_collision_info (Grob
*g
, Real idx
, Interval x
, Interval y
, Real p
)
828 extents_
[X_AXIS
] = x
;
829 extents_
[Y_AXIS
] = y
;
832 type_
= g
->get_property ("avoid-slur");
835 Extra_collision_info::Extra_collision_info ()