function  Zi = cart2pol2(X,Y,Z,THi,Ri,method)

%CART2POL2 -- Transform an image from Cartesian to polar coordinates.
%
%  Zi = cart2pol2(X,Y,Z,THi,Ri)
%  Zi = cart2pol2(X,Y,Z,THi,Ri,method)
%
%  Z specifies image intensities as a function of X and Y.  All three are
%  matrices of the same size.  X and Y must be monotonic and plaid (as if
%  created by MESHGRID) but need not be uniformly spaced.
%
%  THi and Ri define a region in polar coordinates. THi can be a row
%  vector, in which case it specifies a matrix with constant columns.
%  Similarly, Ri can be a column vector ==> matrix with constant rows.
%
%  Algorithm: The polar coordinates THi and Ri are transformed back to
%  Cartesian Xi and Yi using POL2CART. Then INTERP2 is called to find
%  Zi, the values of the underlying 2-D function Z at the points Xi,Yi.
%  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 CART2POL2 is '*linear'. (Note that this is
%  different from the default for INTERP2.)
%
%  Example: Power spectrum of a Gabor patch
%    [X,Y] = meshgrid([-31:32],[-31:32]); params = gabor_params ;
%    params.orient = pi/6 ; params.freq=0.2 ; params.sigma = 10 ;
%    G = gabor(X,Y,params) ; imagesc1(X(1,:),Y(:,1),G) ;
%    [Fx,Fy] = fftmesh(64) ; Fx=fftshift(Fx) ; Fy=fftshift(Fy) ;
%    P = abs(fftshift(fft2(G))) ; figure;contourf(Fx,Fy,P);colorbar ;
%    orient = [-pi/2:0.05:+pi/2] ; sp_freq = [0:0.1:0.3]' ;
%    Pi = cart2pol2(Fx,Fy,P,orient,sp_freq) ; contourf(orient,sp_freq,Pi) ;
%      
%  See also POL2CART2, CART2POL, INTERP2.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2002/03/12 14:44 $
%
% 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(Ri,1) ;
N_cols = size(THi,2) ;
if (size(THi,1)==1)   % row vector?
   THi = repmat(THi,N_rows,1) ;
end
if (size(Ri,2)==1)   % column vector?
   Ri = repmat(Ri,1,N_cols) ;
end

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

%-- Interpolate
Zi = interp2(X,Y,Z,Xi,Yi,method) ;

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