+ // positions of the endpoints of this beam segment, including any overhangs
+ const Interval x_pos = robust_scm2interval (beams[i]->get_property ("X-positions"),
+ Interval (0.0, 0.0));
+
+ Drul_array<Grob *> edge_stems (Beam::first_normal_stem (beams[i]),
+ Beam::last_normal_stem (beams[i]));
+
+ Drul_array<bool> dirs_found (0, 0);
+
+ Real my_y = beams[i]->relative_coordinate (common[Y_AXIS], Y_AXIS);
+
+ Interval beam_width (-1.0, -1.0);
+ for (vsize j = 0; j < stems.size (); j++)
+ {
+ Grob *s = stems[j];
+ beam_multiplicity_.push_back (Stem::beam_multiplicity (stems[j]));
+ head_positions_.push_back (Stem::head_positions (stems[j]));
+ is_normal_.push_back (Stem::is_normal_stem (stems[j]));
+
+ Stem_info si (Stem::get_stem_info (s));
+ si.scale (1 / staff_space_);
+ stem_infos_.push_back (si);
+ chord_start_y_.push_back (Stem::chord_start_y (s));
+ dirs_found[si.dir_] = true;
+
+ bool f = to_boolean (s->get_property ("french-beaming"))
+ && s != edge_stems[LEFT] && s != edge_stems[RIGHT];
+
+ Real y = Beam::calc_stem_y (beams[i], s, common, x_pos[LEFT], x_pos[RIGHT], CENTER,
+ Interval (0, 0), f);
+ base_lengths_.push_back (y / staff_space_);
+ stem_xpositions_.push_back (s->relative_coordinate (common[X_AXIS], X_AXIS) - x_pos[LEFT] + x_span_);
+ stem_ypositions_.push_back (s->relative_coordinate (common[Y_AXIS], Y_AXIS) - my_y);
+
+ if (is_normal_.back ())
+ {
+ if (beam_width[LEFT] == -1.0)
+ beam_width[LEFT] = stem_xpositions_.back ();
+ beam_width[RIGHT] = stem_xpositions_.back ();
+ }
+ }
+
+ edge_dirs_ = Drul_array<Direction> (CENTER, CENTER);
+ normal_stem_count_ += Beam::normal_stem_count (beams[i]);
+ if (normal_stem_count_)
+ edge_dirs_ = Drul_array<Direction> (stem_infos_[0].dir_,
+ stem_infos_.back ().dir_);
+
+ is_xstaff_ = has_interface<Align_interface> (common[Y_AXIS]);
+ is_knee_ |= dirs_found[DOWN] && dirs_found[UP];
+
+ staff_radius_ = Staff_symbol_referencer::staff_radius (beams[i]);
+ edge_beam_counts_ = Drul_array<int>
+ (Stem::beam_multiplicity (stems[0]).length () + 1,
+ Stem::beam_multiplicity (stems.back ()).length () + 1);
+
+ // TODO - why are we dividing by staff_space_?
+ beam_translation_ = Beam::get_beam_translation (beams[i]) / staff_space_;
+
+ for (LEFT_and_RIGHT (d))
+ {
+ quant_range_[d].set_full ();
+ if (!edge_stems[d])
+ continue;
+
+ Real stem_offset = edge_stems[d]->relative_coordinate (common[Y_AXIS], Y_AXIS)
+ - beams[i]->relative_coordinate (common[Y_AXIS], Y_AXIS);
+ Interval heads = Stem::head_positions (edge_stems[d]) * 0.5 * staff_space_;
+
+ Direction ed = edge_dirs_[d];
+ heads.widen (0.5 * staff_space_
+ + (edge_beam_counts_[d] - 1) * beam_translation_ + beam_thickness_ * .5);
+ quant_range_[d][-ed] = heads[ed] + stem_offset;
+ }
+
+ segments_ = Beam::get_beam_segments (beams[i]);
+ vector_sort (segments_, beam_segment_less);
+ for (vsize j = 0; j < segments_.size (); j++)
+ segments_[j].horizontal_ += (x_span_ - x_pos[LEFT]);
+
+ set<Grob *> colliding_stems;
+ for (vsize j = 0; j < collisions.size (); j++)
+ {
+ if (!collisions[j]->is_live ())
+ continue;
+
+ if (has_interface<Beam> (collisions[j]) && Beam::is_cross_staff (collisions[j]))
+ continue;
+
+ Box b;
+ for (Axis a = X_AXIS; a < NO_AXES; incr (a))
+ b[a] = collisions[j]->extent (common[a], a);
+
+ if (b[X_AXIS][RIGHT] < x_pos[LEFT] || b[X_AXIS][LEFT] > x_pos[RIGHT])
+ continue;
+ if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
+ continue;
+
+ b[X_AXIS] += (x_span_ - x_pos[LEFT]);
+ b[Y_AXIS] -= my_y;
+ Real width = b[X_AXIS].length ();
+ Real width_factor = sqrt (width / staff_space_);
+
+ for (LEFT_and_RIGHT (d))
+ add_collision (b[X_AXIS][d], b[Y_AXIS], width_factor);
+
+ Grob *stem = unsmob<Grob> (collisions[j]->get_object ("stem"));
+ if (has_interface<Stem> (stem) && Stem::is_normal_stem (stem))
+ {
+ colliding_stems.insert (stem);
+ }
+ }
+
+ for (set<Grob *>::const_iterator it (colliding_stems.begin ()); it != colliding_stems.end (); it++)
+ {
+ Grob *s = *it;
+ Real x = (robust_relative_extent (s, common[X_AXIS], X_AXIS) - x_pos[LEFT] + x_span_).center ();
+
+ Direction stem_dir = get_grob_direction (*it);
+ Interval y;
+ y.set_full ();
+ y[-stem_dir] = Stem::chord_start_y (*it) + (*it)->relative_coordinate (common[Y_AXIS], Y_AXIS)
+ - my_y;
+
+ Real factor = parameters_.STEM_COLLISION_FACTOR;
+ if (!unsmob<Grob> (s->get_object ("beam")))
+ factor = 1.0;
+ add_collision (x, y, factor);
+ }
+ x_span_ += beams[i]->spanner_length ();
+ }
+}
+
+Beam_scoring_problem::Beam_scoring_problem (Grob *me, Drul_array<Real> ys, bool align_broken_intos)
+{
+ beam_ = dynamic_cast<Spanner *> (me);
+ unquanted_y_ = ys;
+ align_broken_intos_ = align_broken_intos;
+
+ parameters_.fill (me);
+ init_instance_variables (me, ys, align_broken_intos);
+ if (do_initial_slope_calculations_)
+ {
+ least_squares_positions ();
+ slope_damping ();
+ shift_region_to_valid ();
+ }
+}
+
+// Assuming V is not empty, pick a 'reasonable' point inside V.
+static Real
+point_in_interval (Interval v, Real dist)
+{
+ if (isinf (v[DOWN]))
+ return v[UP] - dist;
+ else if (isinf (v[UP]))
+ return v[DOWN] + dist;
+ else
+ return v.center ();
+}
+
+/* Set stem's shorten property if unset.
+
+TODO:
+take some y-position (chord/beam/nearest?) into account
+scmify forced-fraction
+
+This is done in beam because the shorten has to be uniform over the
+entire beam.
+*/
+
+void
+set_minimum_dy (Grob *me, Real *dy)
+{
+ if (*dy)