% Two and threedimensional (centered) Fourier transformation
% where the input can be subject to rigid body motion:
%    matrix vector multiplication
%
% (c) by Alexander Loktyushin, MPI for Biological Cybernetics, 2011 February 19

function y = mvm(A,x,ctransp)

  t = A.t(:);                                             % translation
  if strcmp(A.dkind,'none')
      x = matFFTN(A.imsz)*x(:);
      
      y = zeros(A.imsz);
      
      for i = 1:size(A.rot,2)
          temp = reshape(matRot2(A.imsz,A.rot(i),[],'shear')*x,A.imsz);
          y(:,i) = temp(:,i);
      end;
      
      y = y(:);
      
      y = t.*y; % Fourier transform followed by rotation & translation
  elseif strcmp(A.dkind,'all')
      
      x = matFFTN(A.imsz)*x(:);
      
      y = zeros([A.imsz 4]);
      
      for i = 1:size(A.rot,2)
          temp = reshape(matRot2(A.imsz,A.rot(i),[],'shear')*x,A.imsz);
          y(:,i,1) = temp(:,i);
          
          temp = reshape(d(matRot2(A.imsz,A.rot(i),[],'shear'),1)*x,A.imsz);
          y(:,i,2) = temp(:,i);
      end;

      y = reshape(y,[prod(A.imsz) 4]);
      
      y(:,3:4) = A.dt.*repmat(y(:,1),1,size(A.dt,2));       % Translation

      y(:,1) = t.*y(:,1);                                            % No derivative
      y(:,2) = t.*y(:,2);                                            % Rotation
      
      % NO TRANSPOSE
      
        %{
        y = zeros(prod(A.imsz),size(A.dk,4));
        dy = zeros(prod(A.imsz),A.ndims);
      
        if strcmp(A.mode,'mex')
          [y(:,1) dy(:,1) dy(:,2)] = resample_mex(A.k(:),A.F*(A.f*x),2*A.nuimsz,ctransp,[0 1 2],2);
        else
          [y(:,1) dy(:,1) dy(:,2)] = resample_gpu(A.k(:),A.F*(A.f*x),2*A.nuimsz,ctransp,[0 1 2],2);
        end;        
      
        for i=1:A.ndims
          dy(:,i) = 2*A.nuimsz(i)*dy(:,i);
        end  
          
        for dd=1:size(A.dk,3)
          y(:,dd+1) = t.*reshape(sum(dy.'.*A.dk(:,:,dd),1),[],1);        % Rotation
        end
   
        y(:,3:4) = A.dt.*repmat(A.tc(:).*y(:,1),1,size(A.dt,2));       % Translation
        %}
  end