function poly = rotatePolytope(poly, varargin)
% rotate a polytope of H- or V-representation
%
% Syntax: poly = rotatePolytope(poly, ...)
%
% This function is for 2- and 3-dimensional polytopes only.
%
% The arguments are provided in option/value pairs that are:
%   'center' uses the given fixed point for rotation. Default: [] (origin
%              of coordinate system)
%   ''       an empty option name can be used for 2-dimensional rotations
%              and is followed by the angle in radian.
%   'x',     3-dimensional polytopes are rotated around these axes with the
%   'y', 'z'   angle given in radian.
%   'vector' for 3-dimensional polytopes a vector can be provided and the
%              polytope is rotated so that this vector would match the
%              z-axis afterwards.
%
% You can append as many rotation operations, as you like.
%
% Example (3D): rotatePolytope(poly, 'x', pi/4, 'center', [1 0 0], ...
%                 'y', pi/4, 'center', [0 0 1], 'z', pi/4)
%
% Example (2D): rotatePolytope(poly, '', pi/4, 'center', [1 1], '', pi/4)
%
% See also: rotateHrep2d, rotateVrep2d, rotateHrep3d, rotateVrep3d,
%   movePolytope, stretchPolytope

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

infoP = identifyPolytope(poly);

%% evaluate operations subsequently
x0 = [];
for iArg = 1:2:(nargin-1)
    if strcmpi(varargin{iArg}, 'center')
        x0 = varargin{iArg+1};
        continue;
    end
    
    switch infoP.dim
        case 2
            switch infoP.rep
                case 'Vrep'
                    poly = rotateVrep2d(poly, varargin{iArg+1}, x0);
                case 'Hrep'
                    poly = rotateHrep2d(poly, varargin{iArg+1}, x0);
            end
        case 3
            switch infoP.rep
                case 'Vrep'
                    poly = rotateVrep3d(poly, varargin{iArg}, varargin{iArg+1}, x0);
                case 'Hrep'
                    poly = rotateHrep3d(poly, varargin{iArg}, varargin{iArg+1}, x0);
            end
        otherwise
            error('elk:polytope:wrongType', ...
                'This function is only supported for 2- or 3-dimesnional polytopes');
    end
end

end
