function Hrep = rotateHrep3d(Hrep, axis, angle, x0)
% rotates a polytope in H-representation by given axis and angle
%
% Syntax: Hrep = rotateHrep(Hrep, axis, radian)
%   or    Hrep = rotateHrep(Hrep, 'vector', v)
%
% This function is for 3d polytopes only. The polytope is rotated around 
%   the axis 'x', 'y' or 'z' with the angle in radian (3D).  With the case 
%   'vector', the rotation matrix is calculated so that the normalized 
%   vector v matches the unit vector of the z-axis after rotation. This 
%   operation is not uniquely defined, because any subsequent rotation
%   around the z-axis would still fulfill the requirement.
%
% rotateHrep3d(.., x0) specifies the fixed point x0 for the rotation.
%
% See also: rotatePolytope, getRotationMatrix3d, rotateHrep2d, rotateVrep3d

% 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/>.

if nargin < 4
    x0 = [];
end

if isempty(x0)
    Hrep.A = (getRotationMatrix3d(axis, angle) * Hrep.A')';
else
    Hrep = moveHrep(Hrep, -x0);
    Hrep.A = (getRotationMatrix3d(axis, angle) * Hrep.A')';
    Hrep = moveHrep(Hrep, x0);
end

end