2 beam-quanting.cc -- implement Beam quanting functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
19 #include "staff-symbol-referencer.hh"
22 #include "output-def.hh"
23 #include "group-interface.hh"
24 #include "align-interface.hh"
26 const int INTER_QUANT_PENALTY = 1000;
27 const Real SECONDARY_BEAM_DEMERIT = 10.0;
28 const int STEM_LENGTH_DEMERIT_FACTOR = 5;
31 threshold to combat rounding errors.
33 const Real BEAM_EPS = 1e-3;
35 // possibly ridiculous, but too short stems just won't do
36 const int STEM_LENGTH_LIMIT_PENALTY = 5000;
37 const int DAMPING_DIRECTION_PENALTY = 800;
38 const int MUSICAL_DIRECTION_FACTOR = 400;
39 const int IDEAL_SLOPE_FACTOR = 10;
40 const Real ROUND_TO_ZERO_SLOPE = 0.02;
41 const int ROUND_TO_ZERO_POINTS = 4;
43 extern bool debug_beam_quanting_flag;
46 shrink_extra_weight (Real x, Real fac)
48 return fabs (x) * ((x < 0) ? fac : 1.0);
67 - Make all demerits customisable
69 - One sensible check per demerit (what's this --hwn)
71 - Add demerits for quants per se, as to forbid a specific quant
76 int best_quant_score_idx (Array<Quant_score> const & qscores)
80 for (int i = qscores.size (); i--;)
82 if (qscores[i].demerits < best)
84 best = qscores [i].demerits ;
92 MAKE_SCHEME_CALLBACK (Beam, quanting, 1);
94 Beam::quanting (SCM smob)
96 Grob *me = unsmob_grob (smob);
98 SCM s = me->get_property ("positions");
99 Real yl = ly_scm2double (ly_car (s));
100 Real yr = ly_scm2double (ly_cdr (s));
104 Calculations are relative to a unit-scaled staff, i.e. the quants are
105 divided by the current staff_space.
108 Real ss = Staff_symbol_referencer::staff_space (me);
109 Real thickness = Beam::get_thickness (me) / ss ;
110 Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
112 SCM sdy = me->get_property ("least-squares-dy");
113 Real dy_mus = ly_c_number_p (sdy) ? ly_scm2double (sdy) : 0.0;
116 Real sit = (thickness - slt) / 2;
118 Real hang = 1.0 - (thickness - slt) / 2;
119 Real quants [] = {straddle, sit, inter, hang };
121 int num_quants = int (sizeof (quants)/sizeof (Real));
126 going to REGION_SIZE == 2, yields another 0.6 second with
130 (result indexes between 70 and 575) ? --hwn.
137 Do stem computations. These depend on YL and YR linearly, so we can
138 precompute for every stem 2 factors.
140 Link_array<Grob> stems=
141 Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems");
142 Array<Stem_info> stem_infos;
143 Array<Real> base_lengths;
144 Array<Real> stem_xposns;
146 Drul_array<bool> dirs_found (0,0);
148 for (int a = 2; a--;)
149 common[a] = common_refpoint_of_array (stems, me, Axis (a));
151 Grob * fvs = first_visible_stem (me);
152 Grob *lvs = last_visible_stem (me);
153 Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
154 Real xr = fvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
157 We store some info to quickly interpolate.
159 Sometimes my head is screwed on backwards. The stemlength are
160 AFFINE linear in YL and YR. If YL == YR == 0, then we might have
161 stem_y != 0.0, when we're cross staff.
164 for (int i= 0; i < stems.size (); i++)
168 Stem_info si (Stem::get_stem_info (s));
170 stem_infos.push (si);
171 dirs_found[stem_infos.top ().dir_] = true;
173 bool f = to_boolean (s->get_property ("french-beaming"))
174 && s != lvs && s != fvs;
176 base_lengths.push (calc_stem_y (me, s, common, xl, xr,
177 Interval (0,0), f) / ss);
178 stem_xposns.push (s->relative_coordinate (common[X_AXIS], X_AXIS));
184 Grob *commony = fvs->common_refpoint (lvs, Y_AXIS);
185 xstaff = Align_interface::has_interface (commony);
188 Direction ldir = Direction (stem_infos[0].dir_);
189 Direction rdir = Direction (stem_infos.top ().dir_);
190 bool is_knee = dirs_found[LEFT] && dirs_found[RIGHT];
193 int region_size = REGION_SIZE;
195 Knees are harder, lets try some more possibilities for knees.
201 Asymetry ? should run to <= region_size ?
203 for (int i = -region_size ; i < region_size; i++)
204 for (int j = 0; j < num_quants; j++)
206 quantsl.push (i + quants[j] + int (yl));
207 quantsr.push (i + quants[j] + int (yr));
210 Array<Quant_score> qscores;
212 for (int l =0; l < quantsl.size (); l++)
213 for (int r =0; r < quantsr.size (); r++)
223 /* This is a longish function, but we don't separate this out into
224 neat modular separate subfunctions, as the subfunctions would be
225 called for many values of YL, YR. By precomputing various
226 parameters outside of the loop, we can save a lot of time. */
227 for (int i = qscores.size (); i--;)
229 Real d = score_slopes_dy (qscores[i].yl, qscores[i].yr,
233 qscores[i].demerits += d;
236 qscores[i].score_card_ += to_string ("S%.2f",d);
240 Real rad = Staff_symbol_referencer::staff_radius (me);
244 Drul_array<int> edge_beam_counts
245 (Stem::beam_multiplicity (stems[0]).length () + 1,
246 Stem::beam_multiplicity (stems.top ()).length () + 1);
248 Real beam_translation = get_beam_translation (me) / ss;
250 Real reasonable_score = (is_knee) ? 200000 : 100;
251 for (int i = qscores.size (); i--;)
252 if (qscores[i].demerits < reasonable_score)
254 Real d = score_forbidden_quants (qscores[i].yl, qscores[i].yr,
255 rad, slt, thickness, beam_translation,
256 edge_beam_counts, ldir, rdir);
257 qscores[i].demerits += d;
260 qscores[i].score_card_ += to_string (" F %.2f", d);
264 for (int i = qscores.size (); i--;)
265 if (qscores[i].demerits < reasonable_score)
267 Real d=score_stem_lengths (stems, stem_infos,
268 base_lengths, stem_xposns,
271 qscores[i].yl, qscores[i].yr);
272 qscores[i].demerits += d;
275 qscores[i].score_card_ += to_string (" L %.2f", d);
279 int best_idx = best_quant_score_idx (qscores);
283 SCM inspect_quants = me->get_property ("inspect-quants");
284 if (debug_beam_quanting_flag
285 && ly_c_pair_p (inspect_quants))
287 Drul_array<Real> ins = ly_scm2interval (inspect_quants);
292 for (; i < qscores.size (); i ++)
294 Real d =fabs (qscores[i].yl- ins[LEFT]) + fabs (qscores[i].yr - ins[RIGHT]);
302 programming_error ("Could not find quant.");
306 me->set_property ("positions",
307 ly_interval2scm (Drul_array<Real> (qscores[best_idx].yl,
308 qscores[best_idx].yr)));
310 if (debug_beam_quanting_flag)
312 qscores[best_idx].score_card_ += to_string ("i%d", best_idx);
315 me->set_property ("quant-score",
316 scm_makfrom0str (qscores[best_idx].score_card_.to_str0 ()));
320 return SCM_UNSPECIFIED;
324 Beam::score_stem_lengths (Link_array<Grob> const &stems,
325 Array<Stem_info> const &stem_infos,
326 Array<Real> const &base_stem_ys,
327 Array<Real> const &stem_xs,
332 Real limit_penalty = STEM_LENGTH_LIMIT_PENALTY;
333 Drul_array<Real> score (0, 0);
334 Drul_array<int> count (0, 0);
336 for (int i=0; i < stems.size (); i++)
339 if (Stem::is_invisible (s))
344 Real beam_y = dx ? yr *(x - xl)/dx + yl * ( xr - x)/dx : (yr + yl)/2;
345 Real current_y = beam_y + base_stem_ys[i];
346 Real length_pen = STEM_LENGTH_DEMERIT_FACTOR;
348 Stem_info info = stem_infos[i];
349 Direction d = info.dir_;
351 score[d] += limit_penalty * (0 >? (d * (info.shortest_y_ - current_y)));
353 Real ideal_diff = d * (current_y - info.ideal_y_);
354 Real ideal_score = shrink_extra_weight (ideal_diff, 1.5);
356 /* We introduce a power, to make the scoring strictly
357 convex. Otherwise a symmetric knee beam (up/down/up/down)
358 does not have an optimum in the middle. */
360 ideal_score = pow (ideal_score, 1.1);
362 score[d] += length_pen * ideal_score;
370 score[d] /= (count[d] >? 1);
372 while (flip (&d) != DOWN);
374 return score[LEFT]+score[RIGHT];
378 Beam::score_slopes_dy (Real yl, Real yr,
379 Real dy_mus, Real dy_damp,
387 DAMPING_DIRECTION_PENALTY is a very harsh measure, while for
388 complex beaming patterns, horizontal is often a good choice.
390 TODO: find a way to incorporate the complexity of the beam in this
393 if (fabs (dy/dx) > ROUND_TO_ZERO_SLOPE
394 && sign (dy_damp) != sign (dy))
396 dem += DAMPING_DIRECTION_PENALTY;
399 dem += MUSICAL_DIRECTION_FACTOR * (0 >? (fabs (dy) - fabs (dy_mus)));
402 Real slope_penalty = IDEAL_SLOPE_FACTOR;
404 /* Xstaff beams tend to use extreme slopes to get short stems. We
405 put in a penalty here. */
409 /* Huh, why would a too steep beam be better than a too flat one ? */
410 dem += shrink_extra_weight (fabs (dy_damp) - fabs (dy), 1.5)
415 almost zero slopes look like errors in horizontal beams.
418 This causes too much problems, because horizontal depends on
419 horizontal spacing details. These errors should be dealt with
420 through concaveness. --hwn.
423 && fabs (dy / dx) < ROUND_TO_ZERO_SLOPE)
424 dem += ROUND_TO_ZERO_POINTS;
433 return x - floor (x);
438 TODO: The fixed value SECONDARY_BEAM_DEMERIT is probably flawed:
439 because for 32nd and 64th beams the forbidden quants are relatively
440 more important than stem lengths.
443 Beam::score_forbidden_quants (Real yl, Real yr,
446 Real thickness, Real beam_translation,
447 Drul_array<int> beam_counts,
448 Direction ldir, Direction rdir)
451 Drul_array<Real> y (yl,yr);
452 Drul_array<Direction> dirs (ldir,rdir);
454 Real extra_demerit = SECONDARY_BEAM_DEMERIT / (beam_counts[LEFT] >? beam_counts[RIGHT]);
457 Inside the staff, inter quants are forbidden.
463 if (fabs (y[d]) <= (radius + 0.5) && fabs (my_modf (y[d]) - 0.5) < BEAM_EPS)
464 dem += INTER_QUANT_PENALTY;
466 while ((flip (&d))!= LEFT);
471 for (int j = 1; j <= beam_counts[d]; j++)
474 see if the outer staffline falls in a beam-gap
476 This test is too weak; we should really check all lines.
478 Direction stem_dir = dirs[d];
481 The 2.2 factor is to provide a little leniency for
482 borderline cases. If we do 2.0, then the upper outer line
483 will be in the gap of the (2,sit) quant, leading to a
486 Real gap1 = y[d] - stem_dir * ((j-1) * beam_translation + thickness / 2 - slt/2.2 );
487 Real gap2 = y[d] - stem_dir * (j * beam_translation - thickness / 2 + slt/2.2);
490 gap.add_point (gap1);
491 gap.add_point (gap2);
493 for (Real k = - radius ;
494 k <= radius + BEAM_EPS; k += 1.0)
495 if (gap.contains (k))
496 dem += extra_demerit;
499 while ((flip (&d))!= LEFT);
502 if ((beam_counts[LEFT] >? beam_counts[RIGHT]) >= 2)
505 Real sit = (thickness - slt) / 2;
507 Real hang = 1.0 - (thickness - slt) / 2;
513 if (beam_counts[d] >= 2
514 && fabs (y[d] - dirs[d] * beam_translation) < radius + inter)
516 if (dirs[d] == UP && dy <= BEAM_EPS
517 && fabs (my_modf (y[d]) - sit) < BEAM_EPS)
518 dem += extra_demerit;
520 if (dirs[d] == DOWN && dy >= BEAM_EPS
521 && fabs (my_modf (y[d]) - hang) < BEAM_EPS)
522 dem += extra_demerit;
525 if (beam_counts[d] >= 3
526 && fabs (y[d] - 2 * dirs[d] * beam_translation) < radius + inter)
528 if (dirs[d] == UP && dy <= BEAM_EPS
529 && fabs (my_modf (y[d]) - straddle) < BEAM_EPS)
530 dem += extra_demerit;
532 if (dirs[d] == DOWN && dy >= BEAM_EPS
533 && fabs (my_modf (y[d]) - straddle) < BEAM_EPS)
534 dem += extra_demerit;
537 while (flip (&d) != LEFT);