+ Real a = calc_stem_y (me, s, Interval (0,1), french && i > 0&& (i < stems.size () -1));
+ rbase_lengths.push (a);
+ }
+
+ Direction ldir = Direction (stem_infos[0].dir_);
+ Direction rdir = Direction (stem_infos.top ().dir_);
+ bool knee_b = dirs_found[LEFT] && dirs_found[RIGHT];
+
+
+ int region_size = REGION_SIZE;
+ /*
+ Knees are harder, lets try some more possibilities for knees.
+ */
+ if (knee_b)
+ region_size += 2;
+
+ for (int i = -region_size ; i < region_size; i++)
+ for (int j = 0; j < num_quants; j++)
+ {
+ quantsl.push (i + quants[j] + int (yl));
+ quantsr.push (i + quants[j] + int (yr));
+ }
+
+ Array<Quant_score> qscores;
+
+ for (int l =0; l < quantsl.size (); l++)
+ for (int r =0; r < quantsr.size (); r++)
+ {
+ Quant_score qs;
+ qs.yl = quantsl[l];
+ qs.yr = quantsr[r];
+ qs.demerits = 0.0;
+
+ qscores.push (qs);
+ }
+
+
+ /*
+ This is a longish function, but we don't separate this out into
+ neat modular separate subfunctions, as the subfunctions would be
+ called for many values of YL, YR. By precomputing various
+ parameters outside of the loop, we can save a lot of time.
+
+ */
+ for (int i = qscores.size (); i--;)
+ if (qscores[i].demerits < 100)
+ {
+ qscores[i].demerits
+ += score_slopes_dy (me, qscores[i].yl, qscores[i].yr,
+ dy_mus, yr- yl);
+ }
+
+ Real rad = Staff_symbol_referencer::staff_radius (me);
+ int beam_count = get_beam_count (me);
+ Real beam_space = beam_count < 4
+ ? (2*ss + slt - thickness) / 2.0
+ : (3*ss + slt - thickness) / 3.0;
+
+ for (int i = qscores.size (); i--;)
+ if (qscores[i].demerits < 100)
+ {
+ qscores[i].demerits
+ += score_forbidden_quants (me, qscores[i].yl, qscores[i].yr,
+ rad, slt, thickness, beam_space,
+ beam_count, ldir, rdir);
+ }
+
+
+ for (int i = qscores.size (); i--;)
+ if (qscores[i].demerits < 100)
+ {
+ qscores[i].demerits
+ += score_stem_lengths (stems, stem_infos,
+ lbase_lengths, rbase_lengths,
+ knee_b,
+ me, qscores[i].yl, qscores[i].yr);
+ }
+
+
+ Real best = 1e6;
+ int best_idx = -1;
+ for (int i = qscores.size (); i--;)
+ {
+ if (qscores[i].demerits < best)
+ {
+ best = qscores [i].demerits ;
+ best_idx = i;
+ }
+ }
+
+
+ me->set_grob_property ("positions",
+ gh_cons (gh_double2scm (qscores[best_idx].yl),
+ gh_double2scm (qscores[best_idx].yr))
+ );
+
+#if DEBUG_QUANTING
+
+ // debug quanting
+ me->set_grob_property ("quant-score",
+ gh_double2scm (qscores[best_idx].demerits));
+ me->set_grob_property ("best-idx", gh_int2scm (best_idx));
+#endif
+
+ return SCM_UNSPECIFIED;
+}
+
+Real
+Beam::score_stem_lengths (Link_array<Grob>stems,
+ Array<Stem_info> stem_infos,
+ Array<Real> left_factor,
+ Array<Real> right_factor,
+ bool knee,
+ Grob*me,
+ Real yl, Real yr)
+{
+ Real demerit_score = 0.0 ;
+ Real pen = STEM_LENGTH_LIMIT_PENALTY;
+
+ for (int i=0; i < stems.size (); i++)
+ {
+ Grob* s = stems[i];
+ if (Stem::invisible_b (s))
+ continue;
+
+ Real current_y =
+ yl * left_factor[i] + right_factor[i]* yr;
+
+ Stem_info info = stem_infos[i];
+ Direction d = info.dir_;
+
+ demerit_score += pen
+ * ( 0 >? (info.dir_ * (info.shortest_y_ - current_y)));
+
+ demerit_score += STEM_LENGTH_DEMERIT_FACTOR
+ * shrink_extra_weight (d * current_y - info.dir_ * info.ideal_y_);
+ }
+
+ demerit_score *= 2.0 / stems.size ();
+
+ return demerit_score;
+}
+
+Real
+Beam::score_slopes_dy (Grob *me,
+ Real yl, Real yr,
+ Real dy_mus, Real dy_damp)
+{
+ Real dy = yr - yl;
+
+ Real dem = 0.0;
+ if (sign (dy_damp) != sign (dy))
+ {
+ dem += DAMPING_DIRECTIION_PENALTY;
+ }
+
+ dem += MUSICAL_DIRECTION_FACTOR * (0 >? (fabs (dy) - fabs (dy_mus)));
+ dem += shrink_extra_weight (fabs (dy_damp) - fabs (dy))* IDEAL_SLOPE_FACTOR;
+
+ return dem;
+}
+
+static Real
+my_modf (Real x)
+{
+ return x - floor (x);
+}
+
+Real
+Beam::score_forbidden_quants (Grob*me,
+ Real yl, Real yr,
+ Real rad,
+ Real slt,
+ Real thickness, Real beam_space,
+ int beam_count,
+ Direction ldir, Direction rdir)
+{
+ Real dy = yr - yl;
+
+ Real dem = 0.0;
+ if (fabs (yl) < rad && fabs ( my_modf (yl) - 0.5) < 1e-3)
+ dem += INTER_QUANT_PENALTY;
+ if (fabs (yr) < rad && fabs ( my_modf (yr) - 0.5) < 1e-3)
+ dem += INTER_QUANT_PENALTY;
+
+ // todo: use beam_count of outer stems.
+ if (beam_count >= 2)
+ {
+
+ Real straddle = 0.0;
+ Real sit = (thickness - slt) / 2;
+ Real inter = 0.5;
+ Real hang = 1.0 - (thickness - slt) / 2;
+
+
+ if (fabs (yl - ldir * beam_space) < rad
+ && fabs (my_modf (yl) - inter) < 1e-3)
+ dem += SECONDARY_BEAM_DEMERIT;
+ if (fabs (yr - rdir * beam_space) < rad
+ && fabs (my_modf (yr) - inter) < 1e-3)
+ dem += SECONDARY_BEAM_DEMERIT;
+
+ Real eps = 1e-3;
+
+ /*
+ Can't we simply compute the distance between the nearest
+ staffline and the secondary beam? That would get rid of the
+ silly case analysis here (which is probably not when we have
+ different beam-thicknesses.)
+
+ --hwn
+ */
+
+
+ // hmm, without Interval/Drul_array, you get ~ 4x same code...
+ if (fabs (yl - ldir * beam_space) < rad + inter)
+ {
+ if (ldir == UP && dy <= eps
+ && fabs (my_modf (yl) - sit) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+
+ if (ldir == DOWN && dy >= eps
+ && fabs (my_modf (yl) - hang) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+ }
+
+ if (fabs (yr - rdir * beam_space) < rad + inter)
+ {
+ if (rdir == UP && dy >= eps
+ && fabs (my_modf (yr) - sit) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+
+ if (rdir == DOWN && dy <= eps
+ && fabs (my_modf (yr) - hang) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+ }
+
+ if (beam_count >= 3)
+ {
+ if (fabs (yl - 2 * ldir * beam_space) < rad + inter)
+ {
+ if (ldir == UP && dy <= eps
+ && fabs (my_modf (yl) - straddle) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+
+ if (ldir == DOWN && dy >= eps
+ && fabs (my_modf (yl) - straddle) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+ }
+
+ if (fabs (yr - 2 * rdir * beam_space) < rad + inter)
+ {
+ if (rdir == UP && dy >= eps
+ && fabs (my_modf (yr) - straddle) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+
+ if (rdir == DOWN && dy <= eps
+ && fabs (my_modf (yr) - straddle) < eps)
+ dem += SECONDARY_BEAM_DEMERIT;
+ }
+ }
+ }
+
+ return dem;
+}
+
+
+
+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_grob_property ("positions", ly_interval2scm (pos));
+ return SCM_UNSPECIFIED;
+ }
+
+ Interval ideal (Stem::calc_stem_info (first_visible_stem (me)).ideal_y_,
+ Stem::calc_stem_info (last_visible_stem (me)).ideal_y_);
+
+
+
+ Array<Real> x_posns ;