function  Zi = pol2cart2(TH,R,Z,Xi,Yi,method)

%POL2CART2 -- Transform an image from polar to Cartesian coordinates.
%
%  Zi = pol2cart2(TH,R,Z,Xi,Yi)
%  Zi = pol2cart2(TH,R,Z,Xi,Yi,method)
%
%  Z specifies image intensities as a function of angle TH and radius R.
%  All three are matrices of the same size. TH and R must be monotonic 
%  and plaid (as if created by MESHGRID) but need not be uniformly spaced.
%
%  Xi and Yi define a region in Cartesian coordinates. Xi can be a row
%  vector, in which case it specifies a matrix with constant columns.
%  Similarly, Yi can be a column vector ==> matrix with constant rows.
%
%  Algorithm: The Cartesian coordinates Xi and Yi are transformed back
%  to polar THi and Ri using CART2POL. Then INTERP2 is called to find
%  Zi, the values of the underlying 2-D function Z at the points THi,Ri.
%  Out of range values are returned as NaN.
%
%  METHOD is an optional string passed as a last argument to INTERP2.
%  Available methods include 'nearest', 'linear', 'cubic', and 'spline'.
%  For faster interpolation when X and Y are equally spaced, use the
%  methods '*linear', '*cubic', or '*nearest' (see INTERP2).
%  The default method for POL2CART2 is '*linear'. (Note that this is
%  different from the default for INTERP2.)
%
%  See also CART2POL2, POL2CART, INTERP2.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2002/03/12 16:15 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

%-- Handle input arguments
if (nargin == 5) 
   method = '*linear' ;    % default
elseif (nargin ~= 6)
   error('I expect either 5 or 6 input arguments.') ;
end

N_rows = size(Yi,1) ;
N_cols = size(Xi,2) ;
if (size(Xi,1)==1)   % row vector?
   Xi = repmat(Xi,N_rows,1) ;
end
if (size(Yi,2)==1)   % column vector?
   Yi = repmat(Yi,1,N_cols) ;
end

%-- Find the pre-images of the polar coordinates
[THi,Ri] = cart2pol(Xi,Yi) ;

%-- Interpolate
Zi = interp2(TH,R,Z,THi,Ri,method) ;

%--- Return Zi
%%%%% End of file POL2CART2.M
