+
+/*
+ We can't combine with previous function, since check concave and
+ slope damping comes first.
+ */
+MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 1);
+SCM
+Beam::shift_region_to_valid (SCM grob)
+{
+ Grob *me = unsmob_grob (grob);
+ /*
+ Code dup.
+ */
+ Array<Real> x_posns ;
+ Link_array<Item> stems=
+ Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
+ Grob *common = stems[0];
+ for (int i=1; i < stems.size (); i++)
+ common = stems[i]->common_refpoint (common, X_AXIS);
+
+ Real x0 = first_visible_stem (me)->relative_coordinate (common, X_AXIS);
+ for (int i=0; i < stems.size (); i++)
+ {
+ Item* s = stems[i];
+
+ Real x = s->relative_coordinate (common, X_AXIS) - x0;
+ x_posns.push (x);
+ }
+ Real dx = last_visible_stem (me)->relative_coordinate (common, X_AXIS) - x0;
+
+ Interval pos = ly_scm2interval ( me->get_grob_property ("positions"));
+ Real dy = pos.delta();
+ Real y = pos[LEFT];
+ Real dydx =dy/dx;
+
+
+ /*
+ Shift the positions so that we have a chance of finding good
+ quants (i.e. no short stem failures.)
+ */
+ Interval feasible_left_point;
+ feasible_left_point.set_full ();
+ for (int i=0; i < stems.size (); i++)
+ {
+ Item* s = stems[i];
+ if (Stem::invisible_b (s))
+ continue;
+
+
+ Direction d = Stem::get_direction (s);
+
+
+ /*
+ TODO: use real beam space function
+ */
+ Real left_y = Stem::calc_stem_info (s).shortest_y_
+ - dydx * (x_posns [i] - x0);
+
+ Interval flp ;
+ flp.set_full ();
+ flp[-d] = left_y;
+
+ feasible_left_point.intersect (flp);
+ }
+
+ if (feasible_left_point.empty_b())
+ {
+ warning (_("Not sure that we can find a nice beam slope (no viable initial configuration found)."));
+ }
+ else if (!feasible_left_point.elem_b(y))
+ {
+ if (isinf (feasible_left_point[DOWN]))
+ y = feasible_left_point[UP] - REGION_SIZE;
+ else if (isinf (feasible_left_point[UP]))
+ y = feasible_left_point[DOWN]+ REGION_SIZE;
+ else
+ y = feasible_left_point.center ();
+ }
+ pos = Interval (y, (y+dy));
+ me->set_grob_property ("positions", ly_interval2scm (pos));
+ return SCM_UNSPECIFIED;
+}
+
+