+ Direction d = LEFT;
+ do
+ common[X_AXIS] = beams[i]->get_bound (d)->common_refpoint (common[X_AXIS], X_AXIS);
+ while (flip (&d) != LEFT);
+
+ // 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_ = Align_interface::has_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_;
+
+ d = LEFT;
+ do
+ {
+ 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;
+ }
+ while (flip (&d) != LEFT);
+
+ 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 (Beam::has_interface (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_);
+
+ Direction d = LEFT;
+ do
+ add_collision (b[X_AXIS][d], b[Y_AXIS], width_factor);
+ while (flip (&d) != LEFT);
+
+ Grob *stem = unsmob_grob (collisions[j]->get_object ("stem"));
+ if (stem && Stem::has_interface (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 = (s->extent (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)
+ {
+ /*
+ If dy is smaller than the smallest quant, we
+ get absurd direction-sign penalties.
+ */
+
+ Real ss = Staff_symbol_referencer::staff_space (me);
+ Real beam_thickness = Beam::get_beam_thickness (me) / ss;
+ Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
+ Real sit = (beam_thickness - slt) / 2;
+ Real inter = 0.5;
+ Real hang = 1.0 - (beam_thickness - slt) / 2;
+
+ *dy = sign (*dy) * max (fabs (*dy),
+ min (min (sit, inter), hang));
+ }
+}
+
+void
+Beam_scoring_problem::no_visible_stem_positions ()
+{
+ if (!head_positions_.size ())
+ {
+ unquanted_y_ = Interval (0, 0);
+ return;
+ }
+
+ Interval head_positions;
+ Slice multiplicity;
+ for (vsize i = 0; i < head_positions_.size (); i++)
+ {
+ head_positions.unite (head_positions_[i]);
+ multiplicity.unite (beam_multiplicity_[i]);
+ }
+
+ Direction dir = get_grob_direction (beam_);
+
+ if (!dir)
+ programming_error ("The beam should have a direction by now.");
+
+ Real y = head_positions.linear_combination (dir)
+ * 0.5 * staff_space_
+ + dir * beam_translation_ * (multiplicity.length () + 1);
+
+ unquanted_y_ = Interval (y, y);
+}
+
+vsize
+Beam_scoring_problem::first_normal_index ()
+{
+ for (vsize i = 0; i < is_normal_.size (); i++)
+ if (is_normal_[i])
+ return i;
+
+ beam_->programming_error ("No normal stems, but asking for first normal stem index.");
+ return 0;
+}
+
+vsize
+Beam_scoring_problem::last_normal_index ()
+{
+ for (vsize i = is_normal_.size (); i--;)
+ if (is_normal_[i])
+ return i;
+
+ beam_->programming_error ("No normal stems, but asking for first normal stem index.");
+ return 0;
+}
+
+void
+Beam_scoring_problem::least_squares_positions ()
+{
+ if (!normal_stem_count_)
+ {
+ no_visible_stem_positions ();
+ return;
+ }
+
+ if (stem_infos_.size () < 1)
+ return;
+
+ vsize fnx = first_normal_index ();
+ vsize lnx = last_normal_index ();
+
+ Interval ideal (stem_infos_[fnx].ideal_y_ + stem_ypositions_[fnx],
+ stem_infos_[lnx].ideal_y_ + stem_ypositions_[lnx]);
+
+ Real y = 0;
+ Real slope = 0;
+ Real dy = 0;
+ Real ldy = 0.0;
+ if (!ideal.delta ())
+ {
+ Interval chord (chord_start_y_[0],
+ chord_start_y_.back ());
+
+ /* 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 () && stem_infos_.size () == 2)
+ {
+ /* FIXME. -> UP */
+ Direction d = (Direction) (sign (chord.delta ()) * UP);
+ unquanted_y_[d] = Beam::get_beam_thickness (beam_) / 2;
+ unquanted_y_[-d] = -unquanted_y_[d];
+ }
+ else
+ unquanted_y_ = ideal;
+
+ ldy = unquanted_y_[RIGHT] - unquanted_y_[LEFT];
+ }
+ else
+ {
+ vector<Offset> ideals;
+ for (vsize i = 0; i < stem_infos_.size (); i++)
+ if (is_normal_[i])
+ ideals.push_back (Offset (stem_xpositions_[i],
+ stem_infos_[i].ideal_y_
+ + stem_ypositions_[i]));
+
+ minimise_least_squares (&slope, &y, ideals);
+
+ dy = slope * x_span_;
+
+ set_minimum_dy (beam_, &dy);
+
+ ldy = dy;
+ unquanted_y_ = Interval (y, (y + dy));
+ }
+
+ musical_dy_ = ldy;
+ beam_->set_property ("least-squares-dy", scm_from_double (musical_dy_));
+}
+
+/*
+ Determine whether a beam is concave.
+
+ A beam is concave when the middle notes get closer to the
+ beam than the left and right edge notes.
+
+ This is determined in two ways: by looking at the positions of the
+ middle notes, or by looking at the deviation of the inside notes
+ compared to the line connecting first and last.
+
+ The tricky thing is what to do with beams with chords. There are no
+ real guidelines in this case.
+*/
+
+bool
+is_concave_single_notes (vector<int> const &positions, Direction beam_dir)
+{
+ Interval covering;
+ covering.add_point (positions[0]);
+ covering.add_point (positions.back ());
+
+ bool above = false;
+ bool below = false;
+ bool concave = false;