function hrep = reduceHrep(hrep, zerotol)
% reduce a H-representation to non-redundant support hyperplanes
%
% Syntax: hrep = reduceVrep(hrep)
%
% This function wraps a cddlib function that reduces a set of inequalities
%   to a non-redundant set.
%
% Scaling of the input is important, because cddlib applies a fixed zero 
%   tolerance. This tolerance is stored in the function zerotolCddlib 
%   during initialization of this library (buildCddlib).
%
% See also: convertPolytope

% 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 isempty(hrep.A)
    return
end

%! Todo: Zerotol added for empty-polytope handling which must be considered
%    in corresponding calls.
if ~exist('zerotol', 'var') || isempty(zerotol)
    zerotol = elkZerotol;
end

%% handle empty polytopes
% these are unfortunately not handled properly by cddlib
[hrepScaled, scale] = scaleHrep(hrep, zerotol);
if scale == -inf
    hrep = [];
    return;
end

hrep = callCddLib('reduce_h', struct('A', hrep.A, 'B', hrep.h));

hrep = struct('A', hrep.A, 'h', hrep.B);