function splineMesh = sFit (fi, teta, rho, maxn, epsilon, makeplot)
% sFit - fits points with a spline surfice
%      splineMesh = sFit (fi, teta, rho, maxn, epsilon, makeplot)
%      fi, teta, rho - azimuth, elevation, distance,
%		   coordinates in spherical coordinate system
%      fi is in range (-pi  :pi)
%      teta           (-pi/2:pi/2)
%      rho > 0
%      These values can be obtained by:
%	 [fi, teta, rho]  = cart2sph(x,y,z);
%      maxn - maximal mesh size (see sMBA)
%      epsilon - maximal error  (see sMBA)
%      makeplot - string indicating if surface should be plotted
%	   'p' means plotting, anything else means no plotting
%
%      splineMesha - n*(n+1) matrix with spline coeffitients
%      splineMesh(1,1) corresponds to fi=-pi, teta=-pi/2
%	   fi-space is in 2*pi/n wide segments
%	   teta-space is in pi/n wide segments
%      This matrix can plotted using sPlotSurf
%
%  see also: sPlotSurf, sMBA, sFitCart


%function cart2sph yields azimuth in range (-pi:pi) and elevation
%(-pi/2:pi/2)
% normalizing to (0:1) range
x = (fi+pi)/(2*pi); y = (teta+pi/2)/pi; z = rho;

if nargin == 3,
   splineMesh = sMBA( x,y,z);
elseif nargin == 4,
   splineMesh = sMBA( x,y,z, maxn);
elseif nargin == 5,
   splineMesh = sMBA( x,y,z, maxn, epsilon);
elseif nargin == 6,
   splineMesh = sMBA( x,y,z, maxn, epsilon, makeplot);
else
   disp('sFit:Wrong number/size of parameters');
end;
return
