- int count = normal_stem_count (me);
- Interval pos (0,0);
- if (count < 1)
- return ly_interval2scm (no_visible_stem_positions (me, pos));
-
- vector<Real> x_posns;
- extract_grob_set (me, "normal-stems", stems);
- Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
- Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
-
- Real my_y = me->relative_coordinate (commony, Y_AXIS);
-
- Grob *fvs = first_normal_stem (me);
- Grob *lvs = last_normal_stem (me);
-
- Interval ideal (Stem::get_stem_info (fvs).ideal_y_
- + fvs->relative_coordinate (commony, Y_AXIS) - my_y,
- Stem::get_stem_info (lvs).ideal_y_
- + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
-
- Real x0 = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
- for (vsize i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
-
- Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
- x_posns.push_back (x);
- }
- Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
-
- Real y = 0;
- Real slope = 0;
- Real dy = 0;
- Real ldy = 0.0;
- if (!ideal.delta ())
- {
- Interval chord (Stem::chord_start_y (stems[0]),
- Stem::chord_start_y (stems.back ()));
-
- /* Simple beams (2 stems) on middle line should be allowed to be
- slightly sloped.
-
- However, if both stems reach middle line,
- ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
-
- For that case, we apply artificial slope */
- if (!ideal[LEFT] && chord.delta () && count == 2)
- {
- /* FIXME. -> UP */
- Direction d = (Direction) (sign (chord.delta ()) * UP);
- pos[d] = get_beam_thickness (me) / 2;
- pos[-d] = -pos[d];
- }
- else
- pos = ideal;
-
- /*
- For broken beams this doesn't work well. In this case, the
- slope esp. of the first part of a broken beam should predict
- where the second part goes.
- */
- ldy = pos[RIGHT] - pos[LEFT];
- }
- else
- {
- vector<Offset> ideals;
- for (vsize i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
- ideals.push_back (Offset (x_posns[i],
- Stem::get_stem_info (s).ideal_y_
- + s->relative_coordinate (commony, Y_AXIS)
- - my_y));
- }
-
- minimise_least_squares (&slope, &y, ideals);
-
- dy = slope * dx;
-
- set_minimum_dy (me, &dy);
-
- ldy = dy;
- pos = Interval (y, (y + dy));
- }
-
- /*
- "position" is relative to the staff.
- */
- scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
-
- me->set_property ("least-squares-dy", scm_from_double (ldy));
- return ly_interval2scm (pos);
-}
-
-/*
- We can't combine with previous function, since check concave and
- slope damping comes first.
-
- TODO: we should use the concaveness to control the amount of damping
- applied.
-*/
-MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
-SCM
-Beam::shift_region_to_valid (SCM grob, SCM posns)
-{
- Grob *me = unsmob_grob (grob);
- /*
- Code dup.
- */
- vector<Real> x_posns;
- extract_grob_set (me, "stems", stems);
- Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
- Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
-
- Grob *fvs = first_normal_stem (me);
-
- if (!fvs)
- return posns;
-
- Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
- for (vsize i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
-
- Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
- x_posns.push_back (x);
- }
-
- Grob *lvs = last_normal_stem (me);
- if (!lvs)
- return posns;
-
- Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
-
- Drul_array<Real> pos = ly_scm2interval (posns);
-
- scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
-
- Real dy = pos[RIGHT] - pos[LEFT];
- Real y = pos[LEFT];
- Real slope = dx ? (dy / dx) : 0.0;
-
- /*
- Shift the positions so that we have a chance of finding good
- quants (i.e. no short stem failures.)
- */
- Interval feasible_left_point;
- feasible_left_point.set_full ();
- for (vsize i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
- if (Stem::is_invisible (s))
- continue;
-
- Direction d = get_grob_direction (s);
-
- Real left_y
- = Stem::get_stem_info (s).shortest_y_
- - slope * x_posns [i];
-
- /*
- left_y is now relative to the stem S. We want relative to
- ourselves, so translate:
- */
- left_y
- += + s->relative_coordinate (commony, Y_AXIS)
- - me->relative_coordinate (commony, Y_AXIS);
-
- Interval flp;
- flp.set_full ();
- flp[-d] = left_y;
-
- feasible_left_point.intersect (flp);
- }
-
- if (feasible_left_point.is_empty ())
- warning (_ ("no viable initial configuration found: may not find good beam slope"));
- else if (!feasible_left_point.contains (y))
- {
- const int REGION_SIZE = 2; // UGH UGH
- if (isinf (feasible_left_point[DOWN]))
- y = feasible_left_point[UP] - REGION_SIZE;
- else if (isinf (feasible_left_point[UP]))
- y = feasible_left_point[DOWN]+ REGION_SIZE;
- else
- y = feasible_left_point.center ();
- }
-
- pos = Drul_array<Real> (y, (y + dy));
- scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
-
- return ly_interval2scm (pos);
-}
-
-/* This neat trick is by Werner Lemberg,
- damped = tanh (slope)
- corresponds with some tables in [Wanske] CHECKME */
-MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
-SCM
-Beam::slope_damping (SCM smob, SCM posns)
-{
- Grob *me = unsmob_grob (smob);
- Drul_array<Real> pos = ly_scm2interval (posns);
-
- if (normal_stem_count (me) <= 1)
- return posns;
-
- SCM s = me->get_property ("damping");
- Real damping = scm_to_double (s);
- Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
- if (concaveness >= 10000)
- {
- pos[LEFT] = pos[RIGHT];
- me->set_property ("least-squares-dy", scm_from_double (0));
- damping = 0;
- }
-
- if (damping)
- {
- scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
-
- Real dy = pos[RIGHT] - pos[LEFT];
-
- Grob *fvs = first_normal_stem (me);
- Grob *lvs = last_normal_stem (me);
-
- Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
-
- Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS)
- - first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
-
- Real slope = dy && dx ? dy / dx : 0;
-
- slope = 0.6 * tanh (slope) / (damping + concaveness);
-
- Real damped_dy = slope * dx;
-
- set_minimum_dy (me, &damped_dy);
-
- pos[LEFT] += (dy - damped_dy) / 2;
- pos[RIGHT] -= (dy - damped_dy) / 2;
-
- scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
- }
-
- return ly_interval2scm (pos);
-}
-
-
-MAKE_SCHEME_CALLBACK (Beam, quanting, 2);
-SCM
-Beam::quanting (SCM smob, SCM posns)
-{
- Grob *me = unsmob_grob (smob);
- Drul_array<Real> ys(0, 0);
- ys = robust_scm2drul (posns, ys);
- Beam_scoring_problem problem (me, ys);
-