]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/bezier.cc
Release: bump Welcome versions.
[lilypond.git] / lily / bezier.cc
index 5fea33928893670794afa16f7567f89fffaed0f1..eacd4fe6a44e95f224d6a9f4894455198167710a 100644 (file)
 /*
-  bezier.cc -- implement Bezier and Bezier_bow
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
+  Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
 
-  (c) 1998 Jan Nieuwenhuizen <jan@digicash.com>
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <math.h>
 #include "bezier.hh"
+#include "warn.hh"
+#include "libc-extension.hh"
 
-#ifndef STANDALONE
-#include "direction.hh"
-#include "dimen.hh"
-#include "paper-def.hh"
-#include "debug.hh"
-#include "main.hh"
-#define BEZIER_BOW_DOUT if (check_debug && !monitor->silent_b ("Bezier_bow")) cout
-#else
-#define BEZIER_BOW_DOUT cerr
-#endif
-
-void
-Curve::flipy ()
+Real binomial_coefficient_3[]
+=
 {
-  for (int i = 0; i < size (); i++)
-    (*this)[i].mirror (Y_AXIS);
-}
+  1, 3, 3, 1
+};
 
-int
-Curve::largest_disturbing ()
+void
+scale (vector<Offset> *array, Real x, Real y)
 {
-  Real alpha = 0;
-  int j = 0;
-  for (int i = 1; i < size (); i++)
+  for (vsize i = 0; i < array->size (); i++)
     {
-      if ((*this)[i].y () > 0)
-        {
-         Real phi = (*this)[i].y () / (*this)[i].x ();
-         if (phi > alpha)
-           {
-             alpha = phi;
-             j = i;
-           }
-       }
+      (*array)[i][X_AXIS] = x * (*array)[i][X_AXIS];
+      (*array)[i][Y_AXIS] = y * (*array)[i][Y_AXIS];
     }
-  return j;
 }
 
 void
-Curve::rotate (Real phi)
+rotate (vector<Offset> *array, Real deg)
 {
-  Offset rot (complex_exp (Offset (0, phi)));
-  for (int i = 0; i < size (); i++)
-    (*this)[i] = complex_multiply (rot, (*this)[i]);
+  Offset rot (offset_directed (deg));
+  for (vsize i = 0; i < array->size (); i++)
+    (*array)[i] = complex_multiply (rot, (*array)[i]);
 }
 
 void
-Curve::translate (Offset o)
+translate (vector<Offset> *array, Offset o)
 {
-  for (int i = 0; i < size (); i++)
-    (*this)[i] += o;
+  for (vsize i = 0; i < array->size (); i++)
+    (*array)[i] += o;
 }
 
-Bezier::Bezier ()
-{
-  control_.set_size (4);
-}
+/*
+  Formula of the bezier 3-spline
 
-void
-Bezier::calc (int steps)
-{       
-  steps = steps >? 10;
-  curve_.set_size (steps);
-  Real dt = 1.0 / curve_.size ();
-  Offset c = 3.0 * (control_[1] - control_[0]);
-  Offset b = 3.0 * (control_[2] - control_[1]) - c;
-  Offset a = control_[3] - (control_[0] + c + b);
-  Real t = 0.0;
-  for (int i = 0; i < curve_.size (); i++ )
-    {    
-      curve_[i] = ((a * t + b) * t + c) * t + control_[0];
-      t += dt;
-    }
-}
+  sum_{j = 0}^3 (3 over j) z_j (1-t)^ (3-j)  t^j
 
-void
-Bezier::set (Array<Offset> points)
-{       
-  assert (points.size () == 4);
-  control_ = points;
-}
+
+  A is the axis of X coordinate.
+*/
 
 Real
-Bezier::y (Real x)
+Bezier::get_other_coordinate (Axis a, Real x) const
 {
-  for (int i = 1; i < curve_.size (); i++ )
+  Axis other = Axis ((a + 1) % NO_AXES);
+  vector<Real> ts = solve_point (a, x);
+
+  if (ts.size () == 0)
     {
-      if (x < curve_[i].x () || (i == curve_.size () - 1))
-       {
-         Offset z1 = curve_[i-1];
-         Offset z2 = curve_[i];
-         Real multiplier = (x - z2.x ()) / (z1.x () - z2.x ());
-         Real y = z1.y () * multiplier + (1.0 - multiplier) *z2.y();
-
-         return y;
-        }
+      programming_error ("no solution found for Bezier intersection");
+      return 0.0;
     }
-  assert (false);
-  // silly c++
-  return 0;
-}
 
+#ifdef PARANOID
+  Offset c = curve_point (ts[0]);
+  if (fabs (c[a] - x) > 1e-8)
+    programming_error ("bezier intersection not correct?");
+#endif
 
-Bezier_bow::Bezier_bow (Paper_def* paper_l)
-{
-  paper_l_ = paper_l;
-  return_.set_size (4);
+  return curve_coordinate (ts[0], other);
 }
 
-void
-Bezier_bow::blow_fit ()
+vector<Real>
+Bezier::get_other_coordinates (Axis a, Real x) const
 {
-  Real dy1 = check_fit_f ();
-  if (!dy1)
-    return;
-
-  // be careful not to take too big step
-  Real f = 0.3;
-  Real h1 = dy1 * f;
-  control_[1].y () += h1; 
-  control_[2].y () += h1; 
-  return_[1].y () += h1; 
-  return_[2].y () += h1; 
-
-  calc_bezier ();
-  Real dy2 = check_fit_f ();
-  if (!dy2)
-    return;
-
-#ifndef STANDALONE
-  Real epsilon = paper_l_->rule_thickness ();
-#else
-  Real epsilon = 1.5 * 0.4 PT;
-#endif
-  if (abs (dy2 - dy1) < epsilon)
-    return;
-  
-  /*
-    Assume 
-      dy = B (h) 
-    with 
-      B (h) = a * h + b;
-
-    Then we get for h : B (h) = 0
-
-     B(0)  = dy1 = a * 0 + b   =>   b = dy1
-     B(h1) = dy2 = a * h1 + b  =>   a * f * dy1 + b = dy2
-
-        =>
-
-     a * dy1 / 2 + dy1 = dy2  =>  a = (dy2 - dy1) / (f * dy1)
-   */
-
-  Real a = (dy2 - dy1) / (f * dy1);
-  Real b = dy1;
-  Real h = -b / a;
-
-  control_[1].y () += -h1 +h; 
-  control_[2].y () += -h1 +h; 
-  return_[1].y () += -h1 +h;
-  return_[2].y () += -h1 +h; 
+  Axis other = other_axis (a);
+  vector<Real> ts = solve_point (a, x);
+  vector<Real> sols;
+  for (vsize i = 0; i < ts.size (); i++)
+    sols.push_back (curve_coordinate (ts[i], other));
+  return sols;
 }
 
-void
-Bezier_bow::calc_bezier ()
+Real
+Bezier::curve_coordinate (Real t, Axis a) const
 {
-  Real s = sqrt (control_[3].x () * control_[3].x () 
-    + control_[1].y () * control_[2].y ());
-#ifndef STANDALONE
-  Real internote = paper_l_->internote_f ();
-#else
-  Real internote = STAFFHEIGHT / 8;
-#endif
-  int steps = (int)rint (s / internote);
-  Bezier::calc (steps);
+  Real tj = 1;
+  Real one_min_tj[4];
+  one_min_tj[0] = 1;
+  for (int i = 1; i < 4; i++)
+    one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
+
+  Real r = 0.0;
+  for (int j = 0; j < 4; j++)
+    {
+      r += control_[j][a] * binomial_coefficient_3[j]
+           * tj * one_min_tj[3 - j];
+
+      tj *= t;
+    }
+
+  return r;
 }
 
-Real
-Bezier_bow::calc_f (Real height)
+Offset
+Bezier::curve_point (Real t) const
 {
-  transform ();
-  calc_default (height);
+  Real tj = 1;
+  Real one_min_tj[4];
+  one_min_tj[0] = 1;
+  for (int i = 1; i < 4; i++)
+    one_min_tj[i] = one_min_tj[i - 1] * (1 - t);
+
+  Offset o;
+  for (int j = 0; j < 4; j++)
+    {
+      o += control_[j] * binomial_coefficient_3[j]
+           * tj * one_min_tj[3 - j];
 
-  calc_bezier ();
+      tj *= t;
+    }
 
-  Real dy = check_fit_f ();
-  calc_return (0, 0);
+#ifdef PARANOID
+  assert (fabs (o[X_AXIS] - polynomial (X_AXIS).eval (t)) < 1e-8);
+  assert (fabs (o[Y_AXIS] - polynomial (Y_AXIS).eval (t)) < 1e-8);
+#endif
 
-  transform_controls_back ();
-  return dy;
+  return o;
 }
 
-void
-Bezier_bow::calc ()
+// The return value is normalized unless zero or indefinite.
+Offset
+Bezier::dir_at_point (Real t) const
 {
-  transform ();
+  Offset second_order[3];
+  Offset third_order[2];
 
-  calc_controls ();
+  for (vsize i = 0; i < 3; i++)
+    second_order[i] = ((control_[i + 1] - control_[i]) * t) + control_[i];
 
-  transform_controls_back ();
-}
+  for (vsize i = 0; i < 2; i++)
+    third_order[i] = ((second_order[i + 1] - second_order[i]) * t) + second_order[i];
 
-/*
-  [TODO]
-    * see if it works
-    * document in Documentation/fonts.tex
- */
+  return (third_order[1] - third_order[0]).direction ();
+}
 
 /*
-  Clipping
-
-  This function tries to address two issues:
-    * the tangents of the slur should always point inwards 
-      in the actual slur, i.e.  *after rotating back*.
-    * slurs shouldn't be too high ( <= 1.5 staffheight?)
-
-  We could calculate the tangent of the bezier curve from
-  both ends going inward, and clip the slur at the point
-  where the tangent (after rotation) points up (or inward
-  with a certain maximum angle).
-  
-  However, we assume that real clipping is not the best
-  answer.  We expect that moving the outer control point up 
-  if the slur becomes too high will result in a nicer slur 
-  after recalculation.
-
-  Knowing that the tangent is the line through the first
-  two control points, we'll clip (move the outer control
-  point upwards) too if the tangent points outwards.
- */
-
-bool
-Bezier_bow::calc_clipping ()
+  Cache binom (3, j) t^j (1-t)^{3-j}
+*/
+struct Polynomial_cache
 {
-#ifndef STANDALONE
-  Real staffsize_f = paper_l_->get_var ("barsize");
-#else
-  Real staffsize_f = STAFFHEIGHT;
-#endif
-
-  Real clip_h = staffsize_f;
-  Real begin_h = control_[1].y () - control_[0].y ();
-  Real end_h = control_[2].y () - control_[3].y ();
-  Real begin_dy = begin_h - clip_h;
-  Real end_dy = end_h - clip_h;
-  
-  Real pi = M_PI;
-  Real begin_alpha = (control_[1] - control_[0]).arg () + alpha_;
-  Real end_alpha = pi -  (control_[2] - control_[3]).arg () - alpha_;
-
-  Real max_alpha = 1.1 * pi/2;
-  if ((begin_dy < 0) && (end_dy < 0)
-    && (begin_alpha < max_alpha) && (end_alpha < max_alpha))
-    return false;
-
-  encompass_.rotate (alpha_);
-  // ugh
-  origin_.y () *= dir_;
-  encompass_.translate (origin_);
-
-  bool again = true;
-  //ugh
-  if ((begin_dy > 0) || (end_dy > 0))
-    {
-      Real dy = (begin_dy + end_dy) / 4;
-      dy *= cos (alpha_);
-      encompass_[0].y () += dy;
-      encompass_[encompass_.size () - 1].y () += dy;
-    }
-  else
+  Polynomial terms_[4];
+  Polynomial_cache ()
+  {
+    for (int j = 0; j <= 3; j++)
+      terms_[j]
+        = binomial_coefficient_3[j]
+          * Polynomial::power (j, Polynomial (0, 1))
+          * Polynomial::power (3 - j, Polynomial (1, -1));
+  }
+};
+
+static Polynomial_cache poly_cache;
+
+Polynomial
+Bezier::polynomial (Axis a) const
+{
+  Polynomial p (0.0);
+  Polynomial q;
+  for (int j = 0; j <= 3; j++)
     {
-      //ugh
-      Real c = -0.4;
-      if (begin_alpha >= max_alpha)
-       begin_dy = c * begin_alpha / max_alpha * begin_h;
-      if (end_alpha >= max_alpha)
-       end_dy = c * end_alpha / max_alpha * end_h;
-
-      Real dy = end_dy >? begin_dy;
-
-      if (!experimental_features_global_b)
-        {
-         encompass_[0].y () += dy;
-         encompass_[encompass_.size () - 1].y () += dy;
-       }
-      else
-       {
-         encompass_[0].y () += begin_dy;
-         encompass_[encompass_.size () - 1].y () += end_dy;
-
-         Offset delta = encompass_[encompass_.size () - 1] - encompass_[0];
-         alpha_ = delta.arg ();
-         alpha_ *= dir_;
-       }
+      q = poly_cache.terms_[j];
+      q *= control_[j][a];
+      p += q;
     }
 
-  origin_ = encompass_[0];
-  encompass_.translate (-origin_);
-  // ugh
-  origin_.y () *= dir_;
-  encompass_.rotate (-alpha_);
-
-  return again;
+  return p;
 }
 
-void
-Bezier_bow::calc_controls ()
+/**
+   Remove all numbers outside [0, 1] from SOL
+*/
+vector<Real>
+filter_solutions (vector<Real> sol)
 {
-  // try clipping twice
-  for (int i = 0; i < 3; i++)
-    {
-      if (i && !calc_clipping ())
-       return;
-
-      calc_default (0);
-      calc_bezier ();
-      
-      if (check_fit_bo ())
-        {
-         calc_return (0, 0);
-         return;
-       }
-      calc_tangent_controls ();
-
-      blow_fit ();
-      // ugh
-      blow_fit ();
-    }
+  for (vsize i = sol.size (); i--;)
+    if (sol[i] < 0 || sol[i] > 1)
+      sol.erase (sol.begin () + i);
+  return sol;
 }
 
-void
-Bezier_bow::calc_return (Real begin_alpha, Real end_alpha)
+/**
+   find t such that derivative is proportional to DERIV
+*/
+vector<Real>
+Bezier::solve_derivative (Offset deriv) const
 {
-#ifndef STANDALONE
-  Real thick = 1.8 * paper_l_->rule_thickness ();
-#else
-  Real thick = 1.8 * 0.4 PT;
-#endif
+  Polynomial xp = polynomial (X_AXIS);
+  Polynomial yp = polynomial (Y_AXIS);
+  xp.differentiate ();
+  yp.differentiate ();
 
-  return_[0] = control_[3];
-  return_[3] = control_[0];
+  Polynomial combine = xp * deriv[Y_AXIS] - yp * deriv [X_AXIS];
 
-  return_[1] = control_[2] - thick * complex_exp (Offset (0, 90 + end_alpha));
-  return_[2] = control_[1] 
-    - thick * complex_exp (Offset (0, 90 - begin_alpha));  
+  return filter_solutions (combine.solve ());
 }
 
 /*
- See Documentation/fonts.tex
- */
-void
-Bezier_bow::calc_tangent_controls ()
+  Find t such that curve_point (t)[AX] == COORDINATE
+*/
+vector<Real>
+Bezier::solve_point (Axis ax, Real coordinate) const
 {
-  Offset ijk_p (control_[3].x () / 2, control_[1].y ());
-  BEZIER_BOW_DOUT << "ijk: " << ijk_p.x () << ", " << ijk_p.y () << endl;
+  Polynomial p (polynomial (ax));
+  p.coefs_[0] -= coordinate;
 
-  Real default_rc = ijk_p.y () / ijk_p.x ();
+  vector<Real> sol (p.solve ());
+  return filter_solutions (sol);
+}
 
-  int begin_disturb = encompass_.largest_disturbing ();
-  Offset begin_p = begin_disturb ? Offset (encompass_[begin_disturb].x (), 
-    encompass_[begin_disturb].y ()) : ijk_p;
-  Real begin_rc = begin_p.y () / begin_p.x ();
-  if (default_rc > begin_rc)
+/**
+   For the portion of the curve between L and R along axis AX,
+   return the bounding box limit in direction D along the cross axis to AX.
+   If there is no portion between L and R, return 0.0 and report error.
+*/
+Real
+Bezier::minmax (Axis ax, Real l, Real r, Direction d) const
+{
+  Axis bx = other_axis (ax);
+
+  // The curve could hit its bounding box limit along BX at:
+  //  points where the curve is parallel to AX,
+  Offset vec (0.0, 0.0);
+  vec[ax] = 1.0;
+  vector<Real> sols (solve_derivative (vec));
+  //  or endpoints of the curve,
+  sols.push_back (0.999);
+  sols.push_back (0.001);
+  // (using points just inside the ends, so that an endpoint is evaulated
+  //  if it falls within rounding error of L or R and the curve lies inside)
+
+  Interval iv;
+  for (vsize i = sols.size (); i--;)
     {
-      begin_p = ijk_p;
-      begin_rc = default_rc;
+      Offset p (curve_point (sols[i]));
+      if (p[ax] >= l && p[ax] <= r)
+        iv.add_point (p[bx]);
     }
 
-  Curve reversed;
-  reversed.set_size (encompass_.size ());
-  Real b = control_[3].x ();
-  for (int i = 0; i < encompass_.size (); i++ )
+  //  or intersections of the curve with the bounding lines at L and R.
+  Interval lr (l, r);
+  for (LEFT_and_RIGHT (dir))
     {
-      //       b     1  0
-      // r  =     -        *  c 
-      //       0     0 -1   
-      reversed[i].x () = b - encompass_[encompass_.size () - i - 1].x ();
-      reversed[i].y () = encompass_[encompass_.size () - i - 1].y ();
+      vector<Real> v = get_other_coordinates (ax, lr[dir]);
+      for (vsize i = v.size (); i--;)
+        iv.add_point (v[i]);
     }
 
-  int end_disturb = reversed.largest_disturbing ();
-  end_disturb = end_disturb ? encompass_.size () - end_disturb - 1 : 0;
-  Offset end_p = end_disturb ? Offset (encompass_[end_disturb].x (), 
-    encompass_[end_disturb].y ()) : ijk_p;
-  Real end_rc = end_p.y () / (control_[3].x () - end_p.x ());
-  if (default_rc > end_rc)
+  if (iv.is_empty ())
     {
-      end_p = ijk_p;
-      end_rc = default_rc;
+      programming_error ("Bezier curve does not cross region of concern");
+      return 0.0;
     }
-  BEZIER_BOW_DOUT << "begin " << begin_p.x () << ", " << begin_p.y () << endl;
-  BEZIER_BOW_DOUT << "end " << end_p.x () << ", " << end_p.y () << endl;
-
-  Real height =control_[1].y (); 
-  for (int i = 0; i < encompass_.size (); i++ )
-    height = height >? encompass_[i].y ();
-
-  // emperic computer science:
-  //   * tangents somewhat steeper than minimal line
-  Real rc_correct = 2.4;
-
-  begin_rc *= rc_correct;
-  end_rc *= rc_correct;
-  Real rc1 = begin_rc;
-  Real rc2 = -end_rc;
-  
-  Real begin_alpha = atan (begin_rc);
-  Real end_alpha = atan (-end_rc);
-  Real theta = (begin_alpha - end_alpha) / 2;
-
-#ifndef STANDALONE
-  Real internote = paper_l_->internote_f ();
-#else
-  Real internote = STAFFHEIGHT / 8;
-#endif
-  Real epsilon = internote / 5;
-
-  // if we have two disturbing points, have height line through those...
-  if (!((abs (begin_p.x () - end_p.x ()) < epsilon)
-    && (abs (begin_p.y () - end_p.y ()) < epsilon)))
-      theta = atan (end_p.y () - begin_p.y ()) / (end_p.x () - begin_p.x ());
-
-  Real rc3 = tan (theta);
-  // ugh: be less steep
-  rc3 /= 2*rc_correct;
-
-  Real c2 = -rc2 * control_[3].x ();
-  Real c3 = begin_p.y () > end_p.y () ? begin_p.y () 
-    - rc3 * begin_p.x () : end_p.y () - rc3 * end_p.x ();
-
-  BEZIER_BOW_DOUT << "y1 = " << rc1 << " x + 0" << endl;
-  BEZIER_BOW_DOUT << "y2 = " << rc2 << " x + " << c2 << endl;
-  BEZIER_BOW_DOUT << "y3 = " << rc3 << " x + " << c3 << endl;
-  control_[1].x () = c3 / (rc1 - rc3);
-  control_[1].y () = rc1 * control_[1].x ();
-  control_[2].x () = (c3 - c2) / (rc2 - rc3);
-  BEZIER_BOW_DOUT << "c2.x () = " << control_[2].x () << endl;
-  BEZIER_BOW_DOUT << "(c3 - c2) = " << (c3 - c2) << endl;
-  BEZIER_BOW_DOUT << "(rc2 - rc3) = " << (rc2 - rc3) << endl;
-  control_[2].y () = rc2 * control_[2].x () + c2;
-  BEZIER_BOW_DOUT << "c2.y ()" << control_[2].y () << endl;
-
-  calc_return (begin_alpha, end_alpha);
+
+  return iv.at (d);
 }
 
-bool
-Bezier_bow::check_fit_bo ()
+/**
+   Compute the bounding box dimensions in direction of A.
+*/
+Interval
+Bezier::extent (Axis a) const
 {
-  for (int i = 1; i < encompass_.size () - 1; i++)
-    if (encompass_[i].y () > y (encompass_[i].x ()))
-      return false;
-  return true;
+  int o = (a + 1) % NO_AXES;
+  Offset d;
+  d[Axis (o)] = 1.0;
+  Interval iv;
+  vector<Real> sols (solve_derivative (d));
+  sols.push_back (1.0);
+  sols.push_back (0.0);
+  for (vsize i = sols.size (); i--;)
+    {
+      Offset o (curve_point (sols[i]));
+      iv.unite (Interval (o[a], o[a]));
+    }
+  return iv;
 }
 
-Real
-Bezier_bow::check_fit_f ()
+Interval
+Bezier::control_point_extent (Axis a) const
 {
-  Real dy = 0;
-  for (int i = 1; i < encompass_.size () - 1; i++)
-    dy = dy >? (encompass_[i].y () - y (encompass_[i].x ()));
-  return dy;
+  Interval ext;
+  for (int i = CONTROL_COUNT; i--;)
+    ext.add_point (control_[i][a]);
+
+  return ext;
 }
 
+/**
+   Flip around axis A
+*/
 void
-Bezier_bow::set (Array<Offset> points, int dir)
+Bezier::scale (Real x, Real y)
 {
-  dir_ = dir;
-  encompass_ = points;
+  for (int i = CONTROL_COUNT; i--;)
+    {
+      control_[i][X_AXIS] = x * control_[i][X_AXIS];
+      control_[i][Y_AXIS] = y * control_[i][Y_AXIS];
+    }
 }
 
 void
-Bezier_bow::transform ()
+Bezier::rotate (Real deg)
 {
-  origin_ = encompass_[0];
-  encompass_.translate (-origin_);
-
-  Offset delta = encompass_[encompass_.size () - 1] - encompass_[0];
-  alpha_ = delta.arg ();
+  Offset rot (offset_directed (deg));
+  for (int i = 0; i < CONTROL_COUNT; i++)
+    control_[i] = complex_multiply (rot, control_[i]);
+}
 
-  encompass_.rotate (-alpha_);
+void
+Bezier::translate (Offset o)
+{
+  for (int i = 0; i < CONTROL_COUNT; i++)
+    control_[i] += o;
+}
 
-  if (dir_ == DOWN)
-    encompass_.flipy ();
+void
+Bezier::assert_sanity () const
+{
+  for (int i = 0; i < CONTROL_COUNT; i++)
+    assert (!isnan (control_[i].length ())
+            && !isinf (control_[i].length ()));
 }
 
 void
-Bezier_bow::transform_controls_back ()
+Bezier::reverse ()
 {
-  // silly name; let's transform encompass back too
-  // to allow recalculation without re-set()ting encompass array
+  Bezier b2;
+  for (int i = 0; i < CONTROL_COUNT; i++)
+    b2.control_[CONTROL_COUNT - i - 1] = control_[i];
+  *this = b2;
+}
 
-  if (dir_ == DOWN)
+/*
+  Subdivide a bezier at T into LEFT_PART and RIGHT_PART
+  using deCasteljau's algorithm.
+*/
+void
+Bezier::subdivide (Real t, Bezier *left_part, Bezier *right_part) const
+{
+  Offset p[CONTROL_COUNT][CONTROL_COUNT];
+
+  for (int i = 0; i < CONTROL_COUNT; i++)
+    p[i][CONTROL_COUNT - 1 ] = control_[i];
+  for (int j = CONTROL_COUNT - 2; j >= 0; j--)
+    for (int i = 0; i < CONTROL_COUNT - 1; i++)
+      p[i][j] = p[i][j + 1] + t * (p[i + 1][j + 1] - p[i][j + 1]);
+  for (int i = 0; i < CONTROL_COUNT; i++)
     {
-      control_.flipy ();
-      return_.flipy ();
-      encompass_.flipy ();
+      left_part->control_[i] = p[0][CONTROL_COUNT - 1 - i];
+      right_part->control_[i] = p[i][i];
     }
-
-  control_.rotate (alpha_);
-  control_.translate (origin_);
-
-  return_.rotate (alpha_);
-  return_.translate (origin_);
-
-  encompass_.rotate (alpha_);
-  encompass_.translate (origin_);
-
 }
 
 /*
- See Documentation/fonts.tex
- */
-void
-Bezier_bow::calc_default (Real h)
-{
-  Real pi = M_PI;
-#ifndef STANDALONE
-  Real staffsize_f = paper_l_->get_var ("barsize");
-#else
-  Real staffsize_f = STAFFHEIGHT;
-#endif
+  Extract a portion of a bezier from T_MIN to T_MAX
+*/
 
-  Real height_limit = staffsize_f;
-  Real ratio = 1.0/3.0;
-
-  Real alpha = height_limit * 2.0 / pi;
-  Real beta = pi * ratio / (2.0 * height_limit);
-
-  Offset delta (encompass_[encompass_.size () - 1].x () 
-    - encompass_[0].x (), 0);
-  Real b = delta.length ();
-  Real indent = alpha * atan (beta * b);
-  Real height = indent + h;
-#define RESIZE_ICE
-#ifndef RESIZE_ICE
-  Array<Offset> control;
-  control.push (Offset (0, 0));
-  control.push (Offset (indent, height));
-  control.push (Offset (b - indent, height));
-  control.push (Offset (b, 0));
-#else
-  Array<Offset> control (4);
-  control[0] = Offset (0, 0);
-  control[1] = Offset (indent, height);
-  control[2] = Offset (b - indent, height);
-  control[3] = Offset (b, 0);
-#endif
-  Bezier::set (control);
+Bezier
+Bezier::extract (Real t_min, Real t_max) const
+{
+  if ((t_min < 0) || (t_max) > 1)
+    programming_error
+    ("bezier extract arguments outside of limits: curve may have bad shape");
+  if (t_min >= t_max)
+    programming_error
+    ("lower bezier extract value not less than upper value: curve may have bad shape");
+  Bezier bez1, bez2, bez3, bez4;
+  if (t_min == 0.0)
+    bez2 = *this;
+  else
+    subdivide (t_min, &bez1, &bez2);
+  if (t_max == 1.0)
+    return bez2;
+  else
+    {
+      bez2.subdivide ((t_max - t_min) / (1 - t_min), &bez3, &bez4);
+      return bez3;
+    }
 }
-