2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
13 - Determine auto knees based on positions if it's set by the user.
15 - the code is littered with * and / staff_space calls for
16 #'positions. Consider moving to real-world coordinates?
18 Problematic issue is user tweaks (user tweaks are in staff-coordinates.)
22 - Stems run to the Y-center of the beam.
24 - beam_translation is the offset between Y centers of the beam.
28 #include "interval-set.hh"
29 #include "directional-element-interface.hh"
32 #include "least-squares.hh"
34 #include "output-def.hh"
36 #include "pointer-group-interface.hh"
37 #include "staff-symbol-referencer.hh"
43 #include "text-interface.hh" // debug output.
44 #include "font-interface.hh" // debug output.
48 Beam::add_stem (Grob *me, Grob *s)
50 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
52 s->add_dependency (me);
54 assert (!Stem::get_beam (s));
55 s->set_object ("beam", me->self_scm ());
57 add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
61 Beam::get_thickness (Grob *me)
63 return robust_scm2double (me->get_property ("thickness"), 0)
64 * Staff_symbol_referencer::staff_space (me);
67 /* Return the translation between 2 adjoining beams. */
69 Beam::get_beam_translation (Grob *me)
71 SCM func = me->get_property ("space-function");
73 if (ly_is_procedure (func))
75 SCM s = scm_call_2 (func, me->self_scm (), scm_from_int (get_beam_count (me)));
76 return scm_to_double (s);
82 /* Maximum beam_count. */
84 Beam::get_beam_count (Grob *me)
88 extract_grob_set (me, "stems", stems);
89 for (int i = 0; i < stems.size (); i++)
91 Grob *stem = stems[i];
92 m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
98 Space return space between beams.
100 MAKE_SCHEME_CALLBACK (Beam, space_function, 2);
102 Beam::space_function (SCM smob, SCM beam_count)
104 Grob *me = unsmob_grob (smob);
106 Real staff_space = Staff_symbol_referencer::staff_space (me);
107 Real line = Staff_symbol_referencer::line_thickness (me);
108 Real thickness = get_thickness (me);
110 Real beam_translation = scm_to_int (beam_count) < 4
111 ? (2 * staff_space + line - thickness) / 2.0
112 : (3 * staff_space + line - thickness) / 3.0;
114 return scm_from_double (beam_translation);
117 /* After pre-processing all directions should be set.
118 Several post-processing routines (stem, slur, script) need stem/beam
120 Currenly, this means that beam has set all stem's directions.
121 [Alternatively, stems could set its own directions, according to
122 their beam, during 'final-pre-processing'.] */
123 MAKE_SCHEME_CALLBACK (Beam, before_line_breaking, 1);
125 Beam::before_line_breaking (SCM smob)
127 Grob *me = unsmob_grob (smob);
129 /* Beams with less than 2 two stems don't make much sense, but could happen
134 For a beam that only has one stem, we try to do some disappearance magic:
135 we revert the flag, and move on to The Eternal Engraving Fields. */
137 int count = visible_stem_count (me);
140 extract_grob_set (me, "stems", stems);
141 if (stems.size () == 1)
143 me->warning (_ ("removing beam with less than two stems"));
145 stems[0]->set_object ("beam", SCM_EOL);
148 return SCM_UNSPECIFIED;
150 else if (stems.size () == 0)
153 return SCM_UNSPECIFIED;
158 Direction d = get_default_dir (me);
160 consider_auto_knees (me);
161 set_stem_directions (me, d);
165 set_stem_shorten (me);
171 /* We want a maximal number of shared beams, but if there is choice, we
172 * take the one that is closest to the end of the stem. This is for
184 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
188 Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
192 for (int i = lslice[-left_dir];
193 (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
196 for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
198 int k = -right_dir * scm_to_int (scm_car (s)) + i;
199 if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
203 if (count >= best_count)
214 Beam::connect_beams (Grob *me)
216 extract_grob_set (me, "stems", stems);
219 last_int.set_empty ();
221 // SCM last_beaming = SCM_EOL;
222 SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
223 Direction last_dir = CENTER;
224 for (int i = 0; i < stems.size (); i++)
226 Grob *this_stem = stems[i];
227 SCM this_beaming = this_stem->get_property ("beaming");
229 Direction this_dir = get_grob_direction (this_stem);
230 if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
232 int start_point = position_with_maximal_common_beams
233 (last_beaming, this_beaming,
234 last_dir ? last_dir : this_dir,
241 new_slice.set_empty ();
242 SCM s = index_get_cell (this_beaming, d);
243 for (; scm_is_pair (s); s = scm_cdr (s))
246 = start_point - this_dir * scm_to_int (scm_car (s));
248 new_slice.add_point (new_beam_pos);
249 scm_set_car_x (s, scm_from_int (new_beam_pos));
252 while (flip (&d) != LEFT);
254 if (!new_slice.is_empty ())
255 last_int = new_slice;
259 SCM s = scm_cdr (this_beaming);
260 for (; scm_is_pair (s); s = scm_cdr (s))
262 int np = -this_dir * scm_to_int (scm_car (s));
263 scm_set_car_x (s, scm_from_int (np));
264 last_int.add_point (np);
268 if (scm_ilength (scm_cdr (this_beaming)) > 0)
270 last_beaming = this_beaming;
277 I really enjoy spaghetti, but spaghetti should be kept on a plate
278 with a little garlic and olive oil. This is too much.
282 MAKE_SCHEME_CALLBACK (Beam, print, 1);
284 Beam::print (SCM grob)
286 Spanner *me = unsmob_spanner (grob);
289 extract_grob_set (me, "stems", stems);
290 Grob *xcommon = common_refpoint_of_array (stems, me, X_AXIS);
292 xcommon = me->get_bound (LEFT)->common_refpoint (xcommon, X_AXIS);
293 xcommon = me->get_bound (RIGHT)->common_refpoint (xcommon, X_AXIS);
296 if (visible_stem_count (me))
298 // ugh -> use commonx
299 x0 = first_visible_stem (me)->relative_coordinate (xcommon, X_AXIS);
300 dx = last_visible_stem (me)->relative_coordinate (xcommon, X_AXIS) - x0;
304 x0 = stems[0]->relative_coordinate (xcommon, X_AXIS);
305 dx = stems.top ()->relative_coordinate (xcommon, X_AXIS) - x0;
308 SCM posns = me->get_property ("positions");
309 Drul_array<Real> pos;
310 if (!is_number_pair (posns))
312 programming_error ("no beam positions?");
313 pos = Interval (0, 0);
316 pos = ly_scm2realdrul (posns);
318 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
320 Real dy = pos[RIGHT] - pos[LEFT];
321 Real slope = (dy && dx) ? dy / dx : 0;
323 Real thick = get_thickness (me);
324 Real bdy = get_beam_translation (me);
326 SCM last_beaming = SCM_EOL;
327 Real last_xposn = -1;
328 Real last_stem_width = -1;
330 Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
333 Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
335 for (int i = 0; i <= stems.size (); i++)
337 Grob *stem = (i < stems.size ()) ? stems[i] : 0;
339 SCM this_beaming = stem ? stem->get_property ("beaming") : SCM_EOL;
340 Real xposn = stem ? stem->relative_coordinate (xcommon, X_AXIS) : 0.0;
341 Real stem_width = stem ? robust_scm2double (stem->get_property ("thickness"), 1.0) * lt : 0;
342 Direction stem_dir = stem ? to_dir (stem->get_property ("direction")) : CENTER;
344 We do the space left of ST, with lfliebertjes pointing to the
345 right from the left stem, and rfliebertjes pointing left from
348 SCM left = (i > 0) ? scm_cdr (last_beaming) : SCM_EOL;
349 SCM right = stem ? scm_car (this_beaming) : SCM_EOL;
351 Array<int> full_beams;
352 Array<int> lfliebertjes;
353 Array<int> rfliebertjes;
356 scm_is_pair (s); s = scm_cdr (s))
358 int b = scm_to_int (scm_car (s));
359 if (scm_c_memq (scm_car (s), right) != SCM_BOOL_F)
362 lfliebertjes.push (b);
365 scm_is_pair (s); s = scm_cdr (s))
367 int b = scm_to_int (scm_car (s));
368 if (scm_c_memq (scm_car (s), left) == SCM_BOOL_F)
369 rfliebertjes.push (b);
372 Drul_array<Real> break_overshoot
373 = robust_scm2drul (me->get_property ("break-overshoot"),
374 Drul_array<Real> (-0.5, 0.0));
376 Real w = (i > 0 && stem)
377 ? (xposn - last_xposn)
378 : break_overshoot[ (i == 0) ? LEFT : RIGHT];
380 Real stem_offset = 0.0;
383 w += last_stem_width / 2;
384 stem_offset = -last_stem_width / 2;
390 Real blot = me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"));
391 Stencil whole = Lookup::beam (slope, w, thick, blot);
395 if (scm_is_number (me->get_property ("gap-count")))
397 gap_count = scm_to_int (me->get_property ("gap-count"));
398 gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
400 full_beams.sort (default_compare);
402 full_beams.reverse ();
406 for (int j = full_beams.size (); j--;)
413 b.translate_axis (gap_length, X_AXIS);
415 b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
416 b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
418 the_beam.add_stencil (b);
421 if (lfliebertjes.size () || rfliebertjes.size ())
427 int t = Stem::duration_log (stem);
429 SCM proc = me->get_property ("flag-width-function");
430 SCM result = scm_call_1 (proc, scm_from_int (t));
431 nw_f = scm_to_double (result);
434 nw_f = break_overshoot[RIGHT] / 2;
436 /* Half beam should be one note-width,
437 but let's make sure two half-beams never touch */
441 rw = min (nw_f, ((xposn - last_xposn) / 2));
444 if (me->get_bound (LEFT)->break_status_dir ())
445 rw = xposn - me->get_bound (LEFT)->extent (xcommon, X_AXIS)[RIGHT]
446 + break_overshoot[LEFT];
452 lw = min (nw_f, ((xposn - last_xposn) / 2));
455 lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
457 + break_overshoot[RIGHT];
459 rw += stem_width / 2;
460 lw += last_stem_width / 2;
462 Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
463 Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
464 for (int j = lfliebertjes.size (); j--;)
467 b.translate_axis (last_xposn - x0 - last_stem_width /2,
469 b.translate_axis (slope * (last_xposn - x0)
470 + bdy * lfliebertjes[j],
472 the_beam.add_stencil (b);
474 for (int j = rfliebertjes.size (); j--;)
477 b.translate_axis (xposn - x0 - rw + stem_width / 2, X_AXIS);
478 b.translate_axis (slope * (xposn - x0 - rw)
479 + bdy * rfliebertjes[j], Y_AXIS);
480 the_beam.add_stencil (b);
485 last_stem_width = stem_width;
486 last_beaming = this_beaming;
489 the_beam.translate_axis (x0 - me->relative_coordinate (xcommon, X_AXIS),
491 the_beam.translate_axis (pos[LEFT], Y_AXIS);
494 SCM quant_score = me->get_property ("quant-score");
495 SCM debug = me->get_layout ()->lookup_variable (ly_symbol2scm ("debug-beam-quanting"));
496 if (to_boolean (debug) && scm_is_string (quant_score))
500 This code prints the demerits for each beam. Perhaps this
501 should be switchable for those who want to twiddle with the
505 SCM properties = Font_interface::text_font_alist_chain (me);
507 Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
509 Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
510 (me->get_layout ()->self_scm (), properties, quant_score));
511 the_beam.add_at_edge (Y_AXIS, stem_dir, tm, 1.0, 0);
515 return the_beam.smobbed_copy ();
519 Beam::get_default_dir (Grob *me)
521 Drul_array<int> total;
522 total[UP] = total[DOWN] = 0;
523 Drul_array<int> count;
524 count[UP] = count[DOWN] = 0;
527 extract_grob_set (me, "stems", stems);
529 for (int i = 0; i < stems.size (); i++)
533 Direction sd = get_grob_direction (s);
535 int center_distance = max (int (- d * Stem::head_positions (s) [-d]), 0);
536 int current = sd ? (1 + d * sd) / 2 : center_distance;
544 while (flip (&d) != DOWN);
546 SCM func = me->get_property ("dir-function");
547 SCM s = scm_call_2 (func,
548 scm_cons (scm_from_int (count[UP]),
549 scm_from_int (count[DOWN])),
550 scm_cons (scm_from_int (total[UP]),
551 scm_from_int (total[DOWN])));
553 if (scm_is_number (s) && scm_to_int (s))
556 /* If dir is not determined: get default */
557 return to_dir (me->get_property ("neutral-direction"));
560 /* Set all stems with non-forced direction to beam direction.
561 Urg: non-forced should become `without/with unforced' direction,
562 once stem gets cleaned-up. */
564 Beam::set_stem_directions (Grob *me, Direction d)
566 extract_grob_set (me, "stems", stems);
568 for (int i = 0; i < stems.size (); i++)
572 SCM forcedir = s->get_property ("direction");
573 if (!to_dir (forcedir))
574 set_grob_direction (s, d);
579 Only try horizontal beams for knees. No reliable detection of
580 anything else is possible here, since we don't know funky-beaming
581 settings, or X-distances (slopes!) People that want sloped
582 knee-beams, should set the directions manually.
587 this routine should take into account the stemlength scoring
588 of a possible knee/nonknee beam.
591 Beam::consider_auto_knees (Grob *me)
593 SCM scm = me->get_property ("auto-knee-gap");
594 if (!scm_is_number (scm))
601 extract_grob_set (me, "stems", stems);
603 Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
604 Real staff_space = Staff_symbol_referencer::staff_space (me);
606 Array<Interval> head_extents_array;
607 for (int i = 0; i < stems.size (); i++)
609 Grob *stem = stems[i];
610 if (Stem::is_invisible (stem))
613 Interval head_extents = Stem::head_positions (stem);
614 if (!head_extents.is_empty ())
616 head_extents[LEFT] += -1;
617 head_extents[RIGHT] += 1;
618 head_extents *= staff_space * 0.5;
621 We could subtract beam Y position, but this routine only
622 sets stem directions, a constant shift does not have an
625 head_extents += stem->relative_coordinate (common, Y_AXIS);
627 if (to_dir (stem->get_property ("direction")))
629 Direction stemdir = to_dir (stem->get_property ("direction"));
630 head_extents[-stemdir] = -stemdir * infinity_f;
633 head_extents_array.push (head_extents);
635 gaps.remove_interval (head_extents);
639 Real max_gap_len = 0.0;
641 for (int i = gaps.allowed_regions_.size () -1; i >= 0; i--)
643 Interval gap = gaps.allowed_regions_[i];
646 the outer gaps are not knees.
648 if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
651 if (gap.length () >= max_gap_len)
653 max_gap_len = gap.length ();
658 Real beam_translation = get_beam_translation (me);
659 Real beam_thickness = Beam::get_thickness (me);
660 int beam_count = Beam::get_beam_count (me);
661 Real height_of_beams = beam_thickness / 2
662 + (beam_count - 1) * beam_translation;
663 Real threshold = scm_to_double (scm) + height_of_beams;
665 if (max_gap_len > threshold)
668 for (int i = 0; i < stems.size (); i++)
670 Grob *stem = stems[i];
671 if (Stem::is_invisible (stem))
674 Interval head_extents = head_extents_array[j++];
676 Direction d = (head_extents.center () < max_gap.center ())
679 stem->set_property ("direction", scm_from_int (d));
681 head_extents.intersect (max_gap);
682 assert (head_extents.is_empty () || head_extents.length () < 1e-6);
687 /* Set stem's shorten property if unset.
690 take some y-position (chord/beam/nearest?) into account
691 scmify forced-fraction
693 This is done in beam because the shorten has to be uniform over the
697 Beam::set_stem_shorten (Grob *me)
700 shortening looks silly for x staff beams
705 Real forced_fraction = 1.0 * forced_stem_count (me)
706 / visible_stem_count (me);
708 int beam_count = get_beam_count (me);
710 SCM shorten_list = me->get_property ("beamed-stem-shorten");
711 if (shorten_list == SCM_EOL)
714 Real staff_space = Staff_symbol_referencer::staff_space (me);
717 = robust_list_ref (beam_count -1, shorten_list);
718 Real shorten_f = scm_to_double (shorten_elt) * staff_space;
720 /* your similar cute comment here */
721 shorten_f *= forced_fraction;
724 me->set_property ("shorten", scm_from_double (shorten_f));
727 /* Call list of y-dy-callbacks, that handle setting of
730 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
732 Beam::after_line_breaking (SCM smob)
734 Grob *me = unsmob_grob (smob);
737 return SCM_UNSPECIFIED;
741 Beam::position_beam (Grob *me)
745 if (to_boolean (me->get_property ("positioning-done")))
748 me->set_property ("positioning-done", SCM_BOOL_T);
750 /* Copy to mutable list. */
751 SCM s = ly_deep_copy (me->get_property ("positions"));
752 me->set_property ("positions", s);
754 if (scm_car (s) == SCM_BOOL_F)
756 // one wonders if such genericity is necessary --hwn.
757 SCM callbacks = me->get_property ("position-callbacks");
758 for (SCM i = callbacks; scm_is_pair (i); i = scm_cdr (i))
759 scm_call_1 (scm_car (i), me->self_scm ());
762 set_stem_lengths (me);
766 set_minimum_dy (Grob *me, Real *dy)
771 If dy is smaller than the smallest quant, we
772 get absurd direction-sign penalties.
775 Real ss = Staff_symbol_referencer::staff_space (me);
776 Real thickness = Beam::get_thickness (me) / ss;
777 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
778 Real sit = (thickness - slt) / 2;
780 Real hang = 1.0 - (thickness - slt) / 2;
782 *dy = sign (*dy) * max (fabs (*dy),
783 min (min (sit, inter), hang));
788 Compute a first approximation to the beam slope.
790 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
792 Beam::least_squares (SCM smob)
794 Grob *me = unsmob_grob (smob);
796 int count = visible_stem_count (me);
801 me->set_property ("positions", ly_interval2scm (pos));
802 return SCM_UNSPECIFIED;
806 extract_grob_set (me, "stems", stems);
807 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
808 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
810 Real my_y = me->relative_coordinate (commony, Y_AXIS);
812 Grob *fvs = first_visible_stem (me);
813 Grob *lvs = last_visible_stem (me);
815 Interval ideal (Stem::get_stem_info (fvs).ideal_y_
816 + fvs->relative_coordinate (commony, Y_AXIS) -my_y,
817 Stem::get_stem_info (lvs).ideal_y_
818 + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
820 Real x0 = first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
821 for (int i = 0; i < stems.size (); i++)
825 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
828 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
836 Interval chord (Stem::chord_start_y (first_visible_stem (me)),
837 Stem::chord_start_y (last_visible_stem (me)));
839 /* Simple beams (2 stems) on middle line should be allowed to be
842 However, if both stems reach middle line,
843 ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
845 For that case, we apply artificial slope */
846 if (!ideal[LEFT] && chord.delta () && count == 2)
849 Direction d = (Direction) (sign (chord.delta ()) * UP);
850 pos[d] = get_thickness (me) / 2;
857 For broken beams this doesn't work well. In this case, the
858 slope esp. of the first part of a broken beam should predict
859 where the second part goes.
861 me->set_property ("least-squares-dy",
862 scm_from_double (pos[RIGHT] - pos[LEFT]));
866 Array<Offset> ideals;
867 for (int i = 0; i < stems.size (); i++)
870 if (Stem::is_invisible (s))
872 ideals.push (Offset (x_posns[i],
873 Stem::get_stem_info (s).ideal_y_
874 + s->relative_coordinate (commony, Y_AXIS)
878 minimise_least_squares (&slope, &y, ideals);
882 set_minimum_dy (me, &dy);
883 me->set_property ("least-squares-dy", scm_from_double (dy));
884 pos = Interval (y, (y + dy));
888 "position" is relative to the staff.
890 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
892 me->set_property ("positions", ly_interval2scm (pos));
894 return SCM_UNSPECIFIED;
898 We can't combine with previous function, since check concave and
899 slope damping comes first.
901 TODO: we should use the concaveness to control the amount of damping
904 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
906 Beam::shift_region_to_valid (SCM grob)
908 Grob *me = unsmob_grob (grob);
913 extract_grob_set (me, "stems", stems);
914 Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
915 Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
917 Grob *fvs = first_visible_stem (me);
920 return SCM_UNSPECIFIED;
922 Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
923 for (int i = 0; i < stems.size (); i++)
927 Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
931 Grob *lvs = last_visible_stem (me);
933 return SCM_UNSPECIFIED;
935 Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
937 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
939 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
941 Real dy = pos[RIGHT] - pos[LEFT];
943 Real slope = dx ? (dy / dx) : 0.0;
946 Shift the positions so that we have a chance of finding good
947 quants (i.e. no short stem failures.)
949 Interval feasible_left_point;
950 feasible_left_point.set_full ();
951 for (int i = 0; i < stems.size (); i++)
954 if (Stem::is_invisible (s))
957 Direction d = Stem::get_direction (s);
960 = Stem::get_stem_info (s).shortest_y_
961 - slope * x_posns [i];
964 left_y is now relative to the stem S. We want relative to
965 ourselves, so translate:
968 += + s->relative_coordinate (commony, Y_AXIS)
969 - me->relative_coordinate (commony, Y_AXIS);
975 feasible_left_point.intersect (flp);
978 if (feasible_left_point.is_empty ())
979 warning (_ ("no viable initial configuration found: may not find good beam slope"));
980 else if (!feasible_left_point.contains (y))
982 const int REGION_SIZE = 2; // UGH UGH
983 if (isinf (feasible_left_point[DOWN]))
984 y = feasible_left_point[UP] - REGION_SIZE;
985 else if (isinf (feasible_left_point[UP]))
986 y = feasible_left_point[DOWN]+ REGION_SIZE;
988 y = feasible_left_point.center ();
991 pos = Drul_array<Real> (y, (y + dy));
992 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
994 me->set_property ("positions", ly_interval2scm (pos));
995 return SCM_UNSPECIFIED;
998 /* This neat trick is by Werner Lemberg,
999 damped = tanh (slope)
1000 corresponds with some tables in [Wanske] CHECKME */
1001 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
1003 Beam::slope_damping (SCM smob)
1005 Grob *me = unsmob_grob (smob);
1007 if (visible_stem_count (me) <= 1)
1008 return SCM_UNSPECIFIED;
1010 SCM s = me->get_property ("damping");
1011 Real damping = scm_to_double (s);
1015 Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
1016 scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1018 Real dy = pos[RIGHT] - pos[LEFT];
1020 Grob *fvs = first_visible_stem (me);
1021 Grob *lvs = last_visible_stem (me);
1023 Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1025 Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
1026 - first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
1028 Real slope = dy && dx ? dy / dx : 0;
1030 Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1032 slope = 0.6 * tanh (slope) / (damping + concaveness);
1034 Real damped_dy = slope * dx;
1036 set_minimum_dy (me, &damped_dy);
1038 pos[LEFT] += (dy - damped_dy) / 2;
1039 pos[RIGHT] -= (dy - damped_dy) / 2;
1041 scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1043 me->set_property ("positions", ly_interval2scm (pos));
1045 return SCM_UNSPECIFIED;
1049 Report slice containing the numbers that are both in (car BEAMING)
1053 where_are_the_whole_beams (SCM beaming)
1057 for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1059 if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1061 l.add_point (scm_to_int (scm_car (s)));
1067 /* Return the Y position of the stem-end, given the Y-left, Y-right
1068 in POS for stem S. This Y position is relative to S. */
1070 Beam::calc_stem_y (Grob *me, Grob *s, Grob ** common,
1072 Drul_array<Real> pos, bool french)
1074 Real beam_translation = get_beam_translation (me);
1076 Real r = s->relative_coordinate (common[X_AXIS], X_AXIS) - xl;
1077 Real dy = pos[RIGHT] - pos[LEFT];
1079 Real stem_y_beam0 = (dy && dx
1084 Direction my_dir = get_grob_direction (s);
1085 SCM beaming = s->get_property ("beaming");
1087 Real stem_y = stem_y_beam0;
1090 Slice bm = where_are_the_whole_beams (beaming);
1091 if (!bm.is_empty ())
1092 stem_y += beam_translation * bm[-my_dir];
1096 Slice bm = Stem::beam_multiplicity (s);
1097 if (!bm.is_empty ())
1098 stem_y += bm[my_dir] * beam_translation;
1101 Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1102 - s->relative_coordinate (common[Y_AXIS], Y_AXIS);
1108 Hmm. At this time, beam position and slope are determined. Maybe,
1109 stem directions and length should set to relative to the chord's
1110 position of the beam. */
1112 Beam::set_stem_lengths (Grob *me)
1114 extract_grob_set (me, "stems", stems);
1119 for (int a = 2; a--;)
1120 common[a] = common_refpoint_of_array (stems, me, Axis (a));
1122 Drul_array<Real> pos = ly_scm2realdrul (me->get_property ("positions"));
1123 Real staff_space = Staff_symbol_referencer::staff_space (me);
1124 scale_drul (&pos, staff_space);
1128 if (scm_is_number (me->get_property ("gap-count"))
1129 &&scm_to_int (me->get_property ("gap-count")))
1132 thick = get_thickness (me);
1135 // ugh -> use commonx
1136 Grob *fvs = first_visible_stem (me);
1137 Grob *lvs = last_visible_stem (me);
1139 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1140 Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1142 for (int i = 0; i < stems.size (); i++)
1145 if (Stem::is_invisible (s))
1148 bool french = to_boolean (s->get_property ("french-beaming"));
1149 Real stem_y = calc_stem_y (me, s, common,
1151 pos, french && s != lvs && s!= fvs);
1154 Make the stems go up to the end of the beam. This doesn't matter
1155 for normal beams, but for tremolo beams it looks silly otherwise.
1158 stem_y += thick * 0.5 * get_grob_direction (s);
1160 Stem::set_stemend (s, 2 * stem_y / staff_space);
1165 Beam::set_beaming (Grob *me, Beaming_info_list const *beaming)
1167 extract_grob_set (me, "stems", stems);
1170 for (int i = 0; i < stems.size (); i++)
1173 Don't overwrite user settings.
1178 Grob *stem = stems[i];
1179 SCM beaming_prop = stem->get_property ("beaming");
1180 if (beaming_prop == SCM_EOL
1181 || index_get_cell (beaming_prop, d) == SCM_EOL)
1183 int b = beaming->infos_.elem (i).beams_i_drul_[d];
1185 && i < stems.size () -1
1186 && Stem::is_invisible (stem))
1187 b = min (b, beaming->infos_.elem (i).beams_i_drul_[-d]);
1189 Stem::set_beaming (stem, b, d);
1192 while (flip (&d) != LEFT);
1197 Beam::forced_stem_count (Grob *me)
1199 extract_grob_set (me, "stems", stems);
1202 for (int i = 0; i < stems.size (); i++)
1206 if (Stem::is_invisible (s))
1209 /* I can imagine counting those boundaries as a half forced stem,
1210 but let's count them full for now. */
1211 if (abs (Stem::chord_start_y (s)) > 0.1
1212 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1219 Beam::visible_stem_count (Grob *me)
1221 extract_grob_set (me, "stems", stems);
1223 for (int i = stems.size (); i--;)
1225 if (!Stem::is_invisible (stems[i]))
1232 Beam::first_visible_stem (Grob *me)
1234 extract_grob_set (me, "stems", stems);
1236 for (int i = 0; i < stems.size (); i++)
1238 if (!Stem::is_invisible (stems[i]))
1245 Beam::last_visible_stem (Grob *me)
1247 extract_grob_set (me, "stems", stems);
1249 for (int i = stems.size (); i--;)
1251 if (!Stem::is_invisible (stems[i]))
1260 handle rest under beam (do_post: beams are calculated now)
1261 what about combination of collisions and rest under beam.
1265 rest -> stem -> beam -> interpolate_y_position ()
1267 MAKE_SCHEME_CALLBACK (Beam, rest_collision_callback, 2);
1269 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1271 Grob *rest = unsmob_grob (element_smob);
1274 if (scm_is_number (rest->get_property ("staff-position")))
1275 return scm_from_int (0);
1277 assert (scm_to_int (axis) == Y_AXIS);
1279 Grob *st = unsmob_grob (rest->get_object ("stem"));
1282 return scm_from_double (0.0);
1283 Grob *beam = unsmob_grob (stem->get_object ("beam"));
1285 || !Beam::has_interface (beam)
1286 || !Beam::visible_stem_count (beam))
1287 return scm_from_double (0.0);
1289 Drul_array<Real> pos (0, 0);
1290 SCM s = beam->get_property ("positions");
1291 if (scm_is_pair (s) && scm_is_number (scm_car (s)))
1292 pos = ly_scm2interval (s);
1296 UGH. TODO: fix dependency tracking.
1298 position_beam (beam);
1299 pos = ly_scm2interval (beam->get_property ("positions"));
1302 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1304 scale_drul (&pos, staff_space);
1306 Real dy = pos[RIGHT] - pos[LEFT];
1308 // ugh -> use commonx
1309 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1310 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1311 Real slope = dy && dx ? dy / dx : 0;
1313 Direction d = Stem::get_direction (stem);
1314 Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
1316 Real beam_translation = get_beam_translation (beam);
1317 Real beam_thickness = Beam::get_thickness (beam);
1320 TODO: this is not strictly correct for 16th knee beams.
1323 = Stem::beam_multiplicity (stem).length () + 1;
1325 Real height_of_my_beams = beam_thickness / 2
1326 + (beam_count - 1) * beam_translation;
1327 Real beam_y = stem_y - d * height_of_my_beams;
1329 Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1331 Real rest_dim = rest->extent (common_y, Y_AXIS)[d];
1332 Real minimum_distance
1333 = + staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1334 + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1336 Real shift = d * min (((beam_y - d * minimum_distance) - rest_dim) * d, 0.0);
1338 shift /= staff_space;
1339 Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1341 /* Always move discretely by half spaces */
1342 shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1344 /* Inside staff, move by whole spaces*/
1345 if ((rest->extent (common_y, Y_AXIS)[d] + staff_space * shift) * d
1347 || (rest->extent (common_y, Y_AXIS)[-d] + staff_space * shift) * -d
1349 shift = ceil (fabs (shift)) * sign (shift);
1351 return scm_from_double (staff_space * shift);
1355 Beam::is_knee (Grob *me)
1357 SCM k = me->get_property ("knee");
1358 if (scm_is_bool (k))
1359 return ly_scm2bool (k);
1363 extract_grob_set (me, "stems", stems);
1364 for (int i = stems.size (); i--;)
1366 Direction dir = get_grob_direction (stems[i]);
1375 me->set_property ("knee", ly_bool2scm (knee));
1381 Beam::get_direction_beam_count (Grob *me, Direction d)
1383 extract_grob_set (me, "stems", stems);
1386 for (int i = stems.size (); i--;)
1389 Should we take invisible stems into account?
1391 if (Stem::get_direction (stems[i]) == d)
1392 bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1398 ADD_INTERFACE (Beam, "beam-interface",
1400 "The @code{thickness} property is the weight of beams, "
1401 "measured in staffspace",
1403 "beamed-stem-shorten "
1410 "flag-width-function "
1416 "neutral-direction "
1417 "position-callbacks "