-void
-set_minimum_dy (Grob *me, Real *dy)
-{
- if (*dy)
- {
- /*
- If dy is smaller than the smallest quant, we
- get absurd direction-sign penalties.
- */
-
- Real ss = Staff_symbol_referencer::staff_space (me);
- Real thickness = Beam::get_thickness (me) / ss;
- Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
- Real sit = (thickness - slt) / 2;
- Real inter = 0.5;
- Real hang = 1.0 - (thickness - slt) / 2;
-
- *dy = sign (*dy) * (fabs (*dy)
- >?
- (sit <? inter <? hang));
- }
-}
-
-/*
- Compute a first approximation to the beam slope.
-*/
-MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
-SCM
-Beam::least_squares (SCM smob)
-{
- Grob *me = unsmob_grob (smob);
-
- int count = visible_stem_count (me);
- Interval pos (0, 0);
-
- if (count < 1)
- {
- me->set_property ("positions", ly_interval2scm (pos));
- return SCM_UNSPECIFIED;
- }
-
- Array<Real> x_posns;
- Link_array<Grob> stems
- = extract_grob_array (me, ly_symbol2scm ("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_visible_stem (me);
- Grob *lvs = last_visible_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_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
- for (int i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
-
- Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
- x_posns.push (x);
- }
- Real dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
-
- Real y = 0;
- Real slope = 0;
- Real dy = 0;
-
- if (!ideal.delta ())
- {
- Interval chord (Stem::chord_start_y (first_visible_stem (me)),
- Stem::chord_start_y (last_visible_stem (me)));
-
- /* 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_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.
- */
- me->set_property ("least-squares-dy",
- scm_make_real (pos[RIGHT] - pos[LEFT]));
- }
- else
- {
- Array<Offset> ideals;
- for (int i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
- if (Stem::is_invisible (s))
- continue;
- ideals.push (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);
- me->set_property ("least-squares-dy", scm_make_real (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 ("positions", ly_interval2scm (pos));
-
- return SCM_UNSPECIFIED;
-}
-
-/*
- 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, 1);
-SCM
-Beam::shift_region_to_valid (SCM grob)
-{
- Grob *me = unsmob_grob (grob);
- /*
- Code dup.
- */
- Array<Real> x_posns;
- Link_array<Grob> stems
- = extract_grob_array (me, ly_symbol2scm ("stems"));
- Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
- Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
-
- Grob *fvs = first_visible_stem (me);
-
- if (!fvs)
- return SCM_UNSPECIFIED;
-
- Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
- for (int i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
-
- Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
- x_posns.push (x);
- }
-
- Grob *lvs = last_visible_stem (me);
- if (!lvs)
- return SCM_UNSPECIFIED;
-
- Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
-
- Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
-
- scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
-
- Real dy = pos[RIGHT] - pos[LEFT];
- Real y = pos[LEFT];
- Real slope = dy / dx;
-
- /*
- 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 (int i = 0; i < stems.size (); i++)
- {
- Grob *s = stems[i];
- if (Stem::is_invisible (s))
- continue;
-
- Direction d = Stem::get_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))
- {
- 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));
-
- me->set_property ("positions", ly_interval2scm (pos));
- return SCM_UNSPECIFIED;
-}
-
-/* This neat trick is by Werner Lemberg,
- damped = tanh (slope)
- corresponds with some tables in [Wanske] CHECKME */
-MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);