--- /dev/null
+/*
+ Determine whether a beam is concave.
+ */
+#include <math.h>
+
+
+#include "group-interface.hh"
+#include "array.hh"
+#include "grob.hh"
+#include "stem.hh"
+#include "interval.hh"
+#include "beam.hh"
+#include "staff-symbol-referencer.hh"
+
+bool
+is_concave_single_notes (Array<int> positions, Direction beam_dir)
+{
+ Interval covering;
+ covering.add_point (positions[0]);
+ covering.add_point (positions.top ());
+
+ bool above = false;
+ bool below = false;
+ bool concave = false;
+
+ /*
+ notes above and below the interval covered by 1st and last note.
+ */
+ for (int i = 1; i < positions.size () - 1; i++)
+ {
+ above = above || (positions[i] > covering[UP]);
+ below = below || (positions[i] < covering[DOWN]);
+ }
+
+
+ concave = concave || (above && below);
+ /*
+ A note as close or closer to the beam than begin and end, but the
+ note is reached in the opposite direction as the last-first dy
+ */
+ int dy = positions.top() - positions[0];
+ int closest = (beam_dir * positions.top()) >? (beam_dir *positions[0]);
+ for (int i = 2; !concave && i < positions.size () - 1; i++)
+ {
+ int inner_dy = positions[i] - positions[i-1];
+ if (sign (inner_dy) != sign (dy)
+ && (beam_dir * positions[i] >= closest
+ || beam_dir * positions[i-1] >= closest))
+ concave = true;
+ }
+
+ bool all_closer = true;
+ for (int i = 1; all_closer && i < positions.size ()-1; i++)
+ {
+ all_closer = all_closer &&
+ (beam_dir * positions[i] > closest);
+ }
+
+ concave = concave || all_closer;
+ return concave;
+}
+
+
+MAKE_SCHEME_CALLBACK (Beam, check_concave, 1);
+SCM
+Beam::check_concave (SCM smob)
+{
+ Grob *me = unsmob_grob (smob);
+
+ Link_array<Grob> stems =
+ Pointer_group_interface__extract_grobs (me, (Grob*) 0, "stems");
+
+ if (is_knee (me))
+ return SCM_UNSPECIFIED;
+
+ Direction beam_dir = CENTER;
+ for (int i = stems.size (); i--; )
+ {
+ if (Stem::is_invisible (stems[i]))
+ stems.del (i);
+ else
+ {
+ if (Direction dir = Stem::get_direction (stems[i]))
+ beam_dir = dir;
+ }
+ }
+
+ if (stems.size () <= 2)
+ return SCM_UNSPECIFIED;
+
+
+ Array<int> positions;
+ for (int i= 0; i < stems.size (); i++)
+ {
+ /*
+ For chords, we take the note head that is closest to the beam.
+
+ Hmmm.. wait, for the beams in the last measure of morgenlied,
+ this doesn't look so good. Let's try the heads farthest from
+ the beam.
+
+ */
+ Real pos = Stem::head_positions (stems[i])[-beam_dir];
+
+ positions.push ((int) rint (pos));
+ }
+
+ if (is_concave_single_notes (positions, beam_dir))
+ {
+ Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
+ Real r = linear_combination (pos, 0.0);
+
+ r /= Staff_symbol_referencer::staff_space (me);
+ me->set_property ("positions", ly_interval2scm (Drul_array<Real> (r, r)));
+ me->set_property ("least-squares-dy", scm_make_real (0));
+ }
+ else
+ {
+ Real dy = positions.top () - positions[0];
+ Real slope = dy / Real (positions.size() - 1);
+ Real concaveness = 0.0;
+ for (int i = 1; i < positions.size() - 1; i++)
+ {
+ Real line_y = slope * i + positions[0];
+
+ concaveness += (beam_dir * (positions[i] - line_y)) >? 0.0;
+ }
+
+ concaveness /= positions.size () ;
+
+ /*
+ Normalize. For dy = 0, the slopes ends up as 0 anyway, so
+ the scaling of concaveness doesn't matter much.
+ */
+ if (dy)
+ concaveness /= dy;
+
+ me->set_property ("concaveness", scm_from_double (concaveness));
+ }
+
+ return SCM_UNSPECIFIED;
+}
scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
Real dy = pos[RIGHT] - pos[LEFT];
- Real dydx = (dy && dx) ? dy/dx : 0;
+ Real slope = (dy && dx) ? dy/dx : 0;
Real thick = get_thickness (me);
Real bdy = get_beam_translation (me);
Real blot = me->get_paper ()->get_dimension (ly_symbol2scm ("blotdiameter"));
- Stencil whole = Lookup::beam (dydx, w, thick, blot);
+ Stencil whole = Lookup::beam (slope, w, thick, blot);
Stencil gapped;
int gap_count = 0;
if (scm_is_number (me->get_property ("gap-count")))
{
gap_count = scm_to_int (me->get_property ("gap-count"));
- gapped = Lookup::beam (dydx, w - 2 * gap_length, thick, blot);
+ gapped = Lookup::beam (slope, w - 2 * gap_length, thick, blot);
full_beams.sort (default_compare);
if (stem_dir == UP)
b.translate_axis (gap_length, X_AXIS);
}
b.translate_axis (last_xposn - x0 + stem_offset, X_AXIS);
- b.translate_axis (dydx * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
+ b.translate_axis (slope * (last_xposn - x0) + bdy * full_beams[j], Y_AXIS);
the_beam.add_stencil (b);
}
lw = me->get_bound (RIGHT)->relative_coordinate (xcommon, X_AXIS)
- last_xposn;
- Stencil rhalf = Lookup::beam (dydx, rw, thick, blot);
- Stencil lhalf = Lookup::beam (dydx, lw, thick, blot);
+ Stencil rhalf = Lookup::beam (slope, rw, thick, blot);
+ Stencil lhalf = Lookup::beam (slope, lw, thick, blot);
for (int j = lfliebertjes.size (); j--;)
{
Stencil b (lhalf);
b.translate_axis (last_xposn - x0, X_AXIS);
- b.translate_axis (dydx * (last_xposn-x0) + bdy * lfliebertjes[j], Y_AXIS);
+ b.translate_axis (slope * (last_xposn-x0) + bdy * lfliebertjes[j], Y_AXIS);
the_beam.add_stencil (b);
}
for (int j = rfliebertjes.size (); j--;)
{
Stencil b (rhalf);
b.translate_axis (xposn - x0 - rw , X_AXIS);
- b.translate_axis (dydx * (xposn-x0 -rw) + bdy * rfliebertjes[j], Y_AXIS);
+ b.translate_axis (slope * (xposn-x0 -rw) + bdy * rfliebertjes[j], Y_AXIS);
the_beam.add_stencil (b);
}
}
Real y =0;
- Real dydx = 0;
+ Real slope = 0;
Real dy = 0;
if (!ideal.delta ())
- my_y));
}
- minimise_least_squares (&dydx, &y, ideals);
+ minimise_least_squares (&slope, &y, ideals);
- dy = dydx * dx;
+ dy = slope * dx;
me->set_property ("least-squares-dy", scm_make_real (dy));
pos = Interval (y, (y+dy));
}
Real dy = pos[RIGHT] - pos[LEFT];
Real y = pos[LEFT];
- Real dydx =dy/dx;
+ Real slope =dy/dx;
/*
Real left_y =
Stem::get_stem_info (s).shortest_y_
- - dydx * x_posns [i];
+ - slope * x_posns [i];
/*
left_y is now relative to the stem S. We want relative to
return SCM_UNSPECIFIED;
}
-MAKE_SCHEME_CALLBACK (Beam, check_concave, 1);
-SCM
-Beam::check_concave (SCM smob)
-{
- Grob *me = unsmob_grob (smob);
-
- Link_array<Grob> stems =
- Pointer_group_interface__extract_grobs (me, (Grob*) 0, "stems");
-
- Direction beam_dir = CENTER;
- for (int i = 0; i < stems.size ();)
- {
- if (Stem::is_invisible (stems[i]))
- stems.del (i);
- else
- {
- if (Direction sd = Stem::get_direction (stems[i]))
- {
- /*
- Don't do knee beams.
- */
- if (beam_dir && sd && sd != beam_dir)
- return SCM_UNSPECIFIED;
-
- beam_dir = sd;
- }
- i++;
- }
- }
-
- if (stems.size () < 3)
- return SCM_UNSPECIFIED;
-
-
- /* Concaveness #1: If distance of an inner notehead to line between
- two outer noteheads is bigger than CONCAVENESS-GAP (2.0ss),
- beam is concave (Heinz Stolba).
-
- In the case of knees, the line connecting outer heads is often
- not related to the beam slope (it may even go in the other
- direction). Skip the check when the outer stems point in
- different directions. --hwn
-
- */
- bool is_concave1 = false;
- SCM gap = me->get_property ("concaveness-gap");
- if (scm_is_number (gap))
- {
- Real r1 = scm_to_double (gap);
- Real dy = Stem::chord_start_y (stems.top ())
- - Stem::chord_start_y (stems[0]);
-
-
- Real slope = dy / (stems.size () - 1);
-
- Real y0 = Stem::chord_start_y (stems[0]);
- for (int i = 1; i < stems.size () - 1; i++)
- {
- Real c =
- beam_dir *((Stem::chord_start_y (stems[i]) - y0) - i * slope);
- if (c - r1 > 0)
- {
- is_concave1 = true;
- break;
- }
- }
- }
-
-
- /* Concaveness #2: Sum distances of inner noteheads that fall
- outside the interval of the two outer noteheads.
-
- We only do this for beams where first and last stem have the same
- direction. --hwn.
-
-
- Note that "convex" stems compensate for "concave" stems.
- (is that intentional?) --hwn.
- */
-
- Real concaveness2 = 0;
- SCM thresh = me->get_property ("concaveness-threshold");
- Real r2 = infinity_f;
- if (!is_concave1 && scm_is_number (thresh))
- {
- r2 = scm_to_double (thresh);
-
- Interval iv;
- iv.add_point (Stem::chord_start_y (stems[0]));
- iv.add_point (Stem::chord_start_y (stems.top ()));
-
- for (int i = 1; i < stems.size () - 1; i++)
- {
- Real f = Stem::chord_start_y (stems[i]);
- concaveness2 += ( (f - iv[MAX] ) >? 0) +
- ( (f - iv[MIN] ) <? 0);
- }
-
- concaveness2 *= beam_dir / (stems.size () - 2);
- }
-
- /* TODO: some sort of damping iso -> plain horizontal */
- if (is_concave1 || concaveness2 > r2)
- {
- Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
- Real r = linear_combination (pos, 0.0);
-
- r /= Staff_symbol_referencer::staff_space (me);
- me->set_property ("positions", ly_interval2scm (Drul_array<Real> (r, r)));
- me->set_property ("least-squares-dy", scm_make_real (0));
- }
-
- return SCM_UNSPECIFIED;
-}
-
/* This neat trick is by Werner Lemberg,
damped = tanh (slope)
corresponds with some tables in [Wanske] CHECKME */
return SCM_UNSPECIFIED;
SCM s = me->get_property ("damping");
- int damping = scm_to_int (s);
+ Real damping = scm_to_double (s);
if (damping)
{
- Drul_array<Real> pos = ly_scm2interval (me->get_property ("positions"));
+ 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 dx = last_visible_stem (me)->relative_coordinate (commonx, X_AXIS)
- first_visible_stem (me)->relative_coordinate (commonx, X_AXIS);
- Real dydx = dy && dx ? dy/dx : 0;
- dydx = 0.6 * tanh (dydx) / damping;
- Real damped_dy = dydx * dx;
+ Real slope = dy && dx ? dy/dx : 0;
+
+ Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
+
+ slope = 0.6 * tanh (slope) / (damping + concaveness);
+
+ Real damped_dy = slope * dx;
pos[LEFT] += (dy - damped_dy) / 2;
pos[RIGHT] -= (dy - damped_dy) / 2;
// ugh -> use commonx
Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
- Real dydx = dy && dx ? dy/dx : 0;
+ Real slope = dy && dx ? dy/dx : 0;
Direction d = Stem::get_direction (stem);
- Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * dydx;
+ Real stem_y = pos[LEFT] + (stem->relative_coordinate (0, X_AXIS) - x0) * slope;
Real beam_translation = get_beam_translation (beam);
Real beam_thickness = Beam::get_thickness (beam);
"The @code{thickness} property is the weight of beams, and is measured "
"in staffspace"
,
- "knee positioning-done position-callbacks concaveness-gap "
- "concaveness-threshold dir-function quant-score auto-knee-gap gap "
+ "knee positioning-done position-callbacks "
+ "concaveness dir-function quant-score auto-knee-gap gap "
"gap-count chord-tremolo beamed-stem-shorten shorten least-squares-dy "
"damping inspect-quants flag-width-function neutral-direction positions space-function "
"thickness");