%ROTATION   Rotate a 2D/3D image around an axis
%
% The rotation center is in the center of the image.
% Image rotation by means of shears
%
% SYNOPSIS:
%   image_out = rotation(image_in, angle, axis, method, bgval)
%
%   axis
%      2D: meaningless
%      3D: 1 - x-axis, 2 - y-axis, 3 - z-axis
%   method
%      Interpolation method. String containing one of the following values:
%      'default', 'bspline', '4-cubic', '3-cubic', 'linear', 'zoh'.
%   bgval
%      Value used to fill up the background. String containing one of the
%      following values:
%      'zero', 'min', 'max'.
%
% DEFAULTS:
%   axis   = '3' z-axis
%   method = 'bspline'
%   bgval  = 'zero'
%
% NOTES:
%   Sign of the 2D rotation: implementation in the mathmetical sense, but
%   note the y-axis is positive downwards! Thus: left turning has negative
%   sign, and right positive.
%
%   For exact rotations of multiples of 90 degrees, use the rot90 method
%   instead. Note that rot90 reverses the sign of the angle as compared to
%   this function.
%
% BUGS:
%   A white border around the rotated image can occur if image_in
%   is of type integer, convert to a floating point data type using
%      dip_image(image_in,'float')
%
%   Some bgval and method combinations produce wrong output.
%
% SEE ALSO: dip_image/rot90, rotation3d, dip_image/rotate

% (C) Copyright 1999-2001               Pattern Recognition Group
%     All rights reserved               Faculty of Applied Physics
%                                       Delft University of Technology
%                                       Lorentzweg 1
%                                       2628 CJ Delft
%                                       The Netherlands
%
% Bernd Rieger, Feb 2001

function out = rotation(varargin)

d = struct('menu','Manipulation',...
           'display','Rotation around an axis',...
           'inparams',struct('name',{'in',   'angle', 'axis',  'method',     'bgval'},...
                  'description',{'Input image','Angle (rad)', 'Axis',     'Interpolation Method',     'Background Value'},...
                  'type',       {'image',      'array',     'option',     'option',            'option'},...
                  'dim_check',  {0,            0,           0,             0,             0},...
                  'range_check',{[],           [], {1,2,3}, {'bspline', '4-cubic', '3-cubic', 'linear', 'zoh'},{'zero', 'min', 'max'}},...
                  'required',   {1,            1,           0,             0,                  0},...
                  'default',    {'a',          pi/4,        3,           'bspline',       'zero'}...
                   ),...
           'outparams',struct('name',{'out'},...
                   'description',{'Output image'},...
                   'type',{'image'}...
                   )...
           );
if nargin == 1
   s = varargin{1};
   if ischar(s) & strcmp(s,'DIP_GetParamList')
      out = d;
      return
   end
end
if nargin >= 4 & isequal(varargin{4},'bilinear')
   varargin{4} = 'linear';
end
try
   [in,angle, axis,method,bgval] = getparams(d,varargin{:});
catch
   if ~isempty(paramerror)
      error(paramerror)
   else
      error(firsterr)
   end
end
d = ndims(in{1});
R = [cos(angle) sin(angle); -sin(angle) cos(angle)];
ps_in = reshape(in.pixelsize,1,length(in.pixelsize));
ps_out = ps_in;
% unclear if this is the way to go? this preserves the norm but is 
% strange for pi/4 rotation
if d == 2
   out =  iterate('dip_rotation',in,angle,method,bgval);
   ps_out = abs(R * ps_in');
elseif d == 3
   out = dip_rotation3d_axis(in,angle,axis-1,method,bgval);
   switch axis
      case 1
         ps_out(2:3) = abs(R*[ps_in(2:3)]');
      case 2
         ps_out([1,3]) = abs(R*[ps_in([1,3])]');
      case 3
         ps_out(1:2) = abs(R*[ps_in(1:2)]');
   end
else
   error('Rotation only supported for 2D/3D images.');
end
out.pixelsize = ps_out;
