function curve=spcrv(x,k,maxpnt)
% SPCRV	Generate a spline curve.
%
%     [curve=] spcrv(x[,k,maxpnt])
%
%  uses repeated midpoint knot insertion to generate the curve
%         
%     t /-->  sum  B(t-k/2;j,...,j+k)*x(j) ,
%              j          
%
%  from the input (d,n)-array  x  ,  with  d=1 or 2 . 
%  The insertion process stops as soon as there are >= maxpnt knots.

% C. de Boor / latest change: Feb.25, 1989
% Copyright (c) 1990-92 by Carl de Boor and The MathWorks, Inc.


y=x;kntstp=1;
if (nargin<2), k=4;end
[d,n]=size(x);
if (n<k),
   fprintf('The number of points, %.0f, should be at least as\n',n)
   fprintf('big as the given order, k = %.0f.\n',k)
   error('')
elseif (k<2),
   fprintf('The order, k = %.0f, should be at least 2.\n',k)
   error('')
else
   if (k>2);
      if (nargin<3);maxpnt=100;end
      while (n<maxpnt);
         kntstp=2*kntstp;m=2*n;yy(:,2:2:m)=y;yy(:,1:2:m)=y;
         for r=2:k;
            yy(:,2:m)=(yy(:,2:m)+yy(:,1:m-1))*.5;
         end
         y=yy(:,k:m);n=length(y);
      end
   end

   if (nargout==1);curve=y;end

   %  disable the plotting of breakpoints for the time being

%  kl=floor((k-2)*.5);knots=1:kntstp:n;
%  yk=.5*(y(:,knots+kl)+y(:,knots+k-2-kl));
%  if (d==1);
%     plot([1:n],y,symbol,knots,yk,'x');
%  else
%     plot(y(1,:),y(2,:),symbol,yk(1,:),yk(2,:),'x');
%  end

end
