2 collision.cc -- implement Collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 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"
18 #include "rhythmic-head.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "side-position-interface.hh"
26 check_meshing_chords (Grob *me,
27 Drul_array<vector<Real> > *offsets,
28 Drul_array<vector<Slice> > const &extents,
29 Drul_array<vector<Grob*> > const &clash_groups)
32 if (!extents[UP].size () || ! extents[DOWN].size ())
35 Grob *cu = clash_groups[UP][0];
36 Grob *cd = clash_groups[DOWN][0];
38 /* Every note column should have a stem, but avoid a crash. */
39 if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
42 Grob *nu = Note_column::first_head (cu);
43 Grob *nd = Note_column::first_head (cd);
45 vector<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
46 vector<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
48 /* Too far apart to collide. */
49 if (ups[0] > dps.back () + 1)
52 /* Merge heads if the notes lie the same line, or if the "stem-up-note" is
53 above the "stem-down-note". */
54 bool merge_possible = (ups[0] >= dps[0]) && (ups.back () >= dps.back ());
56 /* Do not merge notes typeset in different style. */
57 if (!ly_is_equal (nu->get_property ("style"),
58 nd->get_property ("style")))
59 merge_possible = false;
61 int upball_type = Note_head::get_balltype (nu);
62 int dnball_type = Note_head::get_balltype (nd);
64 /* Do not merge whole notes (or longer, like breve, longa, maxima). */
65 if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
66 merge_possible = false;
69 && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
70 && !to_boolean (me->get_property ("merge-differently-dotted")))
71 merge_possible = false;
73 /* Can only merge different heads if merge-differently-headed is
76 && upball_type != dnball_type
77 && !to_boolean (me->get_property ("merge-differently-headed")))
78 merge_possible = false;
81 && nu->get_property ("style") == ly_symbol2scm ("fa")
82 && nd->get_property ("style") == ly_symbol2scm ("fa"))
84 Interval uphead_size = nu->extent (nu, Y_AXIS);
85 Offset att = Offset (0.0, -1.0);
86 nu->set_property ("stem-attachment", ly_offset2scm (att));
87 nu->set_property ("transparent", SCM_BOOL_T);
90 /* Should never merge quarter and half notes, as this would make
91 them indistinguishable. */
93 && ((Rhythmic_head::duration_log (nu) == 1
94 && Rhythmic_head::duration_log (nd) == 2)
95 || (Rhythmic_head::duration_log (nu) == 2
96 && Rhythmic_head::duration_log (nd) == 1)))
97 merge_possible = false;
100 this case (distant half collide),
107 the noteheads may be closer than this case (close half collide)
118 /* TODO: filter out the 'o's in this configuration, since they're no
119 part in the collision.
128 bool close_half_collide = false;
129 bool distant_half_collide = false;
130 bool full_collide = false;
132 for (vsize i = 0, j = 0; i < ups.size () && j < dps.size (); )
134 if (abs (ups[i] - dps[j]) == 1)
136 merge_possible = false;
138 close_half_collide = true;
140 distant_half_collide = true;
142 else if (ups[i] == dps[j])
144 else if (ups[i] > dps[0] && ups[i] < dps.back ())
145 merge_possible = false;
146 else if (dps[j] > ups[0] && dps[j] < ups.back ())
147 merge_possible = false;
151 else if (ups[i] > dps[j])
160 full_collide = full_collide || (close_half_collide
161 && distant_half_collide);
163 Drul_array<Real> center_note_shifts;
164 center_note_shifts[LEFT] = 0.0;
165 center_note_shifts[RIGHT] = 0.0;
167 Real shift_amount = 1;
169 bool touch = (ups[0] >= dps.back ());
173 /* For full collisions, the right hand head may obscure dots, so
174 make sure the dotted heads go to the right. */
175 bool stem_to_stem = false;
177 if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
179 else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
186 /* If possible, don't wipe any heads. Else, wipe shortest head,
187 or head with smallest amount of dots. Note: when merging
188 different heads, dots on the smaller one disappear. */
190 Grob *dot_wipe_head = nu;
192 if (upball_type == dnball_type)
194 if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
199 else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
207 else if (dnball_type > upball_type)
212 else if (dnball_type < upball_type)
220 if (Grob *d = unsmob_grob (dot_wipe_head->get_object ("dot")))
224 if (wipe_ball && wipe_ball->is_live ())
226 wipe_ball->set_property ("transparent", SCM_BOOL_T);
229 /* TODO: these numbers are magic; should devise a set of grob props
230 to tune this behavior. */
231 else if (stem_to_stem)
232 shift_amount = -abs (shift_amount) * 0.65;
233 else if (close_half_collide && !touch)
234 shift_amount *= 0.52;
235 else if (distant_half_collide && !touch)
237 else if (distant_half_collide || close_half_collide || full_collide)
241 else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
244 shift_amount *= 0.17;
249 * Dots from left note head collide with right note head. Only occurs
250 * with a close half collide, if the left note head is between
251 * lines and the right note head is on a line, and if right note head
252 * hasn't got any dots.
254 if (close_half_collide
255 && Rhythmic_head::dot_count (nu)
256 && !Rhythmic_head::dot_count (nd))
258 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
259 if (!Staff_symbol_referencer::on_line (staff, ups[0]))
261 Grob *d = unsmob_grob (nu->get_object ("dot"));
262 Grob *parent = d->get_parent (X_AXIS);
263 if (Dot_column::has_interface (parent))
264 Side_position_interface::add_support (parent, nd);
268 /* For full or close half collisions, the right hand head may
269 obscure dots. Move dots to the right. */
270 if (abs (shift_amount) > 1e-6
271 && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
272 && (full_collide || close_half_collide))
274 Grob *d = unsmob_grob (nd->get_object ("dot"));
275 Grob *parent = d->get_parent (X_AXIS);
285 the . is put right of o which is erroneous o force-shifted
288 if (Dot_column::has_interface (parent))
289 Side_position_interface::add_support (parent, nu);
295 for (vsize i = 0; i < clash_groups[d].size (); i++)
296 (*offsets)[d][i] += d * shift_amount;
298 while ((flip (&d)) != UP);
302 MAKE_SCHEME_CALLBACK(Note_collision_interface, calc_positioning_done, 1)
304 Note_collision_interface::calc_positioning_done (SCM smob)
306 Grob *me = unsmob_grob (smob);
307 Drul_array<vector<Grob*> > cg = get_clash_groups (me);
312 for (vsize i = cg[d].size(); i--; )
317 cg[d][i]->extent (me, X_AXIS);
320 while (flip (&d) != UP);
322 SCM autos (automatic_shift (me, cg));
323 SCM hand (forced_shift (me));
331 Grob *fh = Note_column::first_head (h);
333 wid = fh->extent (h, X_AXIS).length ();
336 while (flip (&d) != UP);
339 Real left_most = 1e6;
341 vector<Real> amounts;
342 for (; scm_is_pair (hand); hand = scm_cdr (hand))
344 Grob *s = unsmob_grob (scm_caar (hand));
345 Real amount = scm_to_double (scm_cdar (hand)) * wid;
348 amounts.push_back (amount);
349 if (amount < left_most)
352 for (; scm_is_pair (autos); autos = scm_cdr (autos))
354 Grob *s = unsmob_grob (scm_caar (autos));
355 Real amount = scm_to_double (scm_cdar (autos)) * wid;
357 vsize x = find (done, s) - done.begin ();
358 if (x == VPOS || x >= done.size ())
361 amounts.push_back (amount);
362 if (amount < left_most)
367 for (vsize i = 0; i < amounts.size (); i++)
368 done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
373 Drul_array < vector<Grob*> >
374 Note_collision_interface::get_clash_groups (Grob *me)
376 Drul_array<vector<Grob*> > clash_groups;
378 extract_grob_set (me, "elements", elements);
379 for (vsize i = 0; i < elements.size (); i++)
381 Grob *se = elements[i];
382 if (Note_column::has_interface (se))
384 if (!Note_column::dir (se))
386 se->programming_error ("note-column has no direction");
389 clash_groups[Note_column::dir (se)].push_back (se);
396 vector<Grob*> &clashes (clash_groups[d]);
397 vector_sort (clashes, Note_column::shift_less);
399 while ((flip (&d)) != UP);
404 /** This complicated routine moves note columns around horizontally to
405 ensure that notes don't clash.
407 This should be put into Scheme.
410 Note_collision_interface::automatic_shift (Grob *me,
411 Drul_array < vector<Grob*>
414 Drul_array < vector<int> > shifts;
420 vector<int> &shift (shifts[d]);
421 vector<Grob*> &clashes (clash_groups[d]);
423 for (vsize i = 0; i < clashes.size (); i++)
426 = clashes[i]->get_property ("horizontal-shift");
428 if (scm_is_number (sh))
429 shift.push_back (scm_to_int (sh));
434 for (vsize i = 1; i < shift.size (); i++)
436 if (shift[i - 1] == shift[i])
438 clashes[0]->warning (_ ("ignoring too many clashing note columns"));
443 while ((flip (&d)) != UP);
445 Drul_array<vector<Slice> > extents;
446 Drul_array<vector<Real> > offsets;
450 for (vsize i = 0; i < clash_groups[d].size (); i++)
452 Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
455 extents[d].push_back (s);
456 offsets[d].push_back (d * 0.5 * i);
459 while ((flip (&d)) != UP);
462 do horizontal shifts of each direction
472 for (vsize i = 1; i < clash_groups[d].size (); i++)
474 Slice prev = extents[d][i - 1];
475 prev.intersect (extents[d][i]);
476 if (prev.length () > 0
477 || (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
478 for (vsize j = i; j < clash_groups[d].size (); j++)
479 offsets[d][j] += d * 0.5;
482 while ((flip (&d)) != UP);
485 Check if chords are meshing
488 check_meshing_chords (me, &offsets, extents, clash_groups);
492 for (vsize i = 0; i < clash_groups[d].size (); i++)
493 tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
494 scm_from_double (offsets[d][i])),
497 while (flip (&d) != UP);
503 Note_collision_interface::forced_shift (Grob *me)
507 extract_grob_set (me, "elements", elements);
508 for (vsize i = 0; i < elements.size (); i++)
510 Grob *se = elements[i];
512 SCM force = se->get_property ("force-hshift");
513 if (scm_is_number (force))
515 tups = scm_cons (scm_cons (se->self_scm (), force),
523 Note_collision_interface::add_column (Grob *me, Grob *ncol)
525 ncol->set_property ("X-offset", Grob::x_parent_positioning_proc);
526 Axis_group_interface::add_element (me, ncol);
529 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
530 "An object that handles collisions between notes with different stem "
531 "directions and horizontal shifts. Most of the interesting properties "
532 "are to be set in @ref{note-column-interface}: these are "
533 "@code{force-hshift} and @code{horizontal-shift}.",
536 "merge-differently-dotted "
537 "merge-differently-headed "