2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "note-collision.hh"
11 #include "axis-group-interface.hh"
12 #include "dot-column.hh"
13 #include "international.hh"
14 #include "note-column.hh"
15 #include "note-head.hh"
16 #include "output-def.hh"
17 #include "pointer-group-interface.hh"
19 #include "rhythmic-head.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "side-position-interface.hh"
27 check_meshing_chords (Grob *me,
28 Drul_array<vector<Real> > *offsets,
29 Drul_array<vector<Slice> > const &extents,
30 Drul_array<vector<Grob*> > const &clash_groups)
33 if (!extents[UP].size () || ! extents[DOWN].size ())
36 Grob *cu = clash_groups[UP][0];
37 Grob *cd = clash_groups[DOWN][0];
39 /* Every note column should have a stem, but avoid a crash. */
40 if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
43 Drul_array<Grob*> stems (Note_column::get_stem (cd),
44 Note_column::get_stem (cu));
46 Grob *nu = Note_column::first_head (cu);
47 Grob *nd = Note_column::first_head (cd);
49 vector<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
50 vector<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
52 /* Too far apart to collide. */
53 if (ups[0] > dps.back () + 1)
56 /* Merge heads if the notes lie the same line, or if the "stem-up-note" is
57 above the "stem-down-note". */
58 bool merge_possible = (ups[0] >= dps[0]) && (ups.back () >= dps.back ());
60 /* Do not merge notes typeset in different style. */
61 if (!ly_is_equal (nu->get_property ("style"),
62 nd->get_property ("style")))
63 merge_possible = false;
65 int upball_type = Rhythmic_head::duration_log (nu);
66 int dnball_type = Rhythmic_head::duration_log (nd);
68 /* Do not merge whole notes (or longer, like breve, longa, maxima). */
69 if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
70 merge_possible = false;
73 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
74 && !to_boolean (me->get_property ("merge-differently-dotted")))
75 merge_possible = false;
77 /* Can only merge different heads if merge-differently-headed is
80 && upball_type != dnball_type
81 && !to_boolean (me->get_property ("merge-differently-headed")))
82 merge_possible = false;
85 && nu->get_property ("style") == ly_symbol2scm ("fa")
86 && nd->get_property ("style") == ly_symbol2scm ("fa"))
88 Interval uphead_size = nu->extent (nu, Y_AXIS);
89 Offset att = Offset (0.0, -1.0);
90 nu->set_property ("stem-attachment", ly_offset2scm (att));
91 nu->set_property ("transparent", SCM_BOOL_T);
94 /* Should never merge quarter and half notes, as this would make
95 them indistinguishable. */
97 && ((Stem::duration_log (stems[UP]) == 1
98 && Stem::duration_log (stems[DOWN]) == 2)
99 || (Stem::duration_log (stems[UP]) == 2
100 && Stem::duration_log (stems[DOWN]) == 1)))
101 merge_possible = false;
104 this case (distant half collide),
111 the noteheads may be closer than this case (close half collide)
122 /* TODO: filter out the 'o's in this configuration, since they're no
123 part in the collision.
132 bool close_half_collide = false;
133 bool distant_half_collide = false;
134 bool full_collide = false;
136 for (vsize i = 0, j = 0; i < ups.size () && j < dps.size (); )
138 if (abs (ups[i] - dps[j]) == 1)
140 merge_possible = false;
142 close_half_collide = true;
144 distant_half_collide = true;
146 else if (ups[i] == dps[j])
148 else if (ups[i] > dps[0] && ups[i] < dps.back ())
149 merge_possible = false;
150 else if (dps[j] > ups[0] && dps[j] < ups.back ())
151 merge_possible = false;
155 else if (ups[i] > dps[j])
164 full_collide = full_collide || (close_half_collide
165 && distant_half_collide);
167 Drul_array<Real> center_note_shifts;
168 center_note_shifts[LEFT] = 0.0;
169 center_note_shifts[RIGHT] = 0.0;
171 Real shift_amount = 1;
173 bool touch = (ups[0] >= dps.back ());
177 /* For full collisions, the right hand head may obscure dots, so
178 make sure the dotted heads go to the right. */
179 bool stem_to_stem = false;
181 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
183 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
190 /* If possible, don't wipe any heads. Else, wipe shortest head,
191 or head with smallest amount of dots. Note: when merging
192 different heads, dots on the smaller one disappear. */
194 Grob *dot_wipe_head = nu;
196 if (upball_type == dnball_type)
198 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
203 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
211 else if (dnball_type > upball_type)
216 else if (dnball_type < upball_type)
224 if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
228 if (wipe_ball && wipe_ball->is_live ())
230 wipe_ball->set_property ("transparent", SCM_BOOL_T);
233 /* TODO: these numbers are magic; should devise a set of grob props
234 to tune this behavior. */
235 else if (stem_to_stem)
236 shift_amount = -abs (shift_amount) * 0.65;
237 else if (close_half_collide && !touch)
238 shift_amount *= 0.52;
239 else if (distant_half_collide && !touch)
241 else if (distant_half_collide || close_half_collide || full_collide)
245 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
248 shift_amount *= 0.17;
254 && dnball_type * upball_type == 0)
256 if (upball_type == 0 && dnball_type == 1)
257 shift_amount *= 1.25;
258 else if (upball_type == 0 && dnball_type == 2)
259 shift_amount *= 1.35;
260 else if (dnball_type == 0 && upball_type == 1)
262 else if (dnball_type == 0 && upball_type == 2)
263 shift_amount *= 0.75;
269 * Dots from left note head collide with right note head. Only occurs
270 * with a close half collide, if the left note head is between
271 * lines and the right note head is on a line, and if right note head
272 * hasn't got any dots.
274 if (close_half_collide
275 && Rhythmic_head::dot_count (nu)
276 && !Rhythmic_head::dot_count (nd))
278 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
279 if (!Staff_symbol_referencer::on_line (staff, ups[0]))
281 Grob *d = unsmob_grob (nu->get_object ("dot"));
282 Grob *parent = d->get_parent (X_AXIS);
283 if (Dot_column::has_interface (parent))
284 Side_position_interface::add_support (parent, nd);
288 /* For full or close half collisions, the right hand head may
289 obscure dots. Move dots to the right. */
290 if (abs (shift_amount) > 1e-6
291 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
292 && (full_collide || close_half_collide))
294 Grob *d = unsmob_grob (nd->get_object ("dot"));
295 Grob *parent = d->get_parent (X_AXIS);
305 the . is put right of o which is erroneous o force-shifted
308 if (Dot_column::has_interface (parent))
309 Side_position_interface::add_support (parent, nu);
315 for (vsize i = 0; i < clash_groups[d].size (); i++)
316 (*offsets)[d][i] += d * shift_amount;
318 while ((flip (&d)) != UP);
322 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1)
324 Note_collision_interface::calc_positioning_done (SCM smob)
326 Grob *me = unsmob_grob (smob);
327 Drul_array<vector<Grob*> > cg = get_clash_groups (me);
332 for (vsize i = cg[d].size(); i--; )
337 cg[d][i]->extent (me, X_AXIS);
340 while (flip (&d) != UP);
342 SCM autos (automatic_shift (me, cg));
343 SCM hand (forced_shift (me));
351 Grob *fh = Note_column::first_head (h);
353 wid = fh->extent (h, X_AXIS).length ();
356 while (flip (&d) != UP);
359 Real left_most = 1e6;
361 vector<Real> amounts;
362 for (; scm_is_pair (hand); hand = scm_cdr (hand))
364 Grob *s = unsmob_grob (scm_caar (hand));
365 Real amount = scm_to_double (scm_cdar (hand)) * wid;
368 amounts.push_back (amount);
369 if (amount < left_most)
372 for (; scm_is_pair (autos); autos = scm_cdr (autos))
374 Grob *s = unsmob_grob (scm_caar (autos));
375 Real amount = scm_to_double (scm_cdar (autos)) * wid;
377 vsize x = find (done, s) - done.begin ();
378 if (x == VPOS || x >= done.size ())
381 amounts.push_back (amount);
382 if (amount < left_most)
387 for (vsize i = 0; i < amounts.size (); i++)
388 done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
393 Drul_array < vector<Grob*> >
394 Note_collision_interface::get_clash_groups (Grob *me)
396 Drul_array<vector<Grob*> > clash_groups;
398 extract_grob_set (me, "elements", elements);
399 for (vsize i = 0; i < elements.size (); i++)
401 Grob *se = elements[i];
402 if (Note_column::has_interface (se))
404 if (!Note_column::dir (se))
406 se->programming_error ("note-column has no direction");
409 clash_groups[Note_column::dir (se)].push_back (se);
416 vector<Grob*> &clashes (clash_groups[d]);
417 vector_sort (clashes, Note_column::shift_less);
419 while ((flip (&d)) != UP);
424 /** This complicated routine moves note columns around horizontally to
425 ensure that notes don't clash.
429 Note_collision_interface::automatic_shift (Grob *me,
430 Drul_array < vector<Grob*>
433 Drul_array < vector<int> > shifts;
439 vector<int> &shift (shifts[d]);
440 vector<Grob*> &clashes (clash_groups[d]);
442 for (vsize i = 0; i < clashes.size (); i++)
445 = clashes[i]->get_property ("horizontal-shift");
447 if (scm_is_number (sh))
448 shift.push_back (scm_to_int (sh));
453 for (vsize i = 1; i < shift.size (); i++)
455 if (shift[i - 1] == shift[i])
457 clashes[0]->warning (_ ("ignoring too many clashing note columns"));
462 while ((flip (&d)) != UP);
464 Drul_array<vector<Slice> > extents;
465 Drul_array<vector<Real> > offsets;
469 for (vsize i = 0; i < clash_groups[d].size (); i++)
471 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
474 extents[d].push_back (s);
475 offsets[d].push_back (d * 0.5 * i);
478 while ((flip (&d)) != UP);
481 do horizontal shifts of each direction
491 for (vsize i = 1; i < clash_groups[d].size (); i++)
493 Slice prev = extents[d][i - 1];
494 prev.intersect (extents[d][i]);
495 if (prev.length () > 0
496 || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
497 for (vsize j = i; j < clash_groups[d].size (); j++)
498 offsets[d][j] += d * 0.5;
501 while ((flip (&d)) != UP);
504 Check if chords are meshing
507 check_meshing_chords (me, &offsets, extents, clash_groups);
511 for (vsize i = 0; i < clash_groups[d].size (); i++)
512 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
513 scm_from_double (offsets[d][i])),
516 while (flip (&d) != UP);
522 Note_collision_interface::forced_shift (Grob *me)
526 extract_grob_set (me, "elements", elements);
527 for (vsize i = 0; i < elements.size (); i++)
529 Grob *se = elements[i];
531 SCM force = se->get_property ("force-hshift");
532 if (scm_is_number (force))
534 tups = scm_cons (scm_cons (se->self_scm (), force),
542 Note_collision_interface::add_column (Grob *me, Grob *ncol)
544 ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
545 Axis_group_interface::add_element (me, ncol);
548 ADD_INTERFACE (Note_collision_interface,
549 "An object that handles collisions between notes with different stem "
550 "directions and horizontal shifts. Most of the interesting properties "
551 "are to be set in @ref{note-column-interface}: these are "
552 "@code{force-hshift} and @code{horizontal-shift}.",
556 "merge-differently-dotted "
557 "merge-differently-headed "
558 "positioning-done ");