function polyConverted = convertPolytope(polyInput, targetRep, varargin)
% converting a polytope between V-representation and H-representation
%
% Syntax: polyConverted = convertPolytope(polyInput, targetRep)
%
% This function converts an arbitrary polytope representation (PtInput)
%   into the representation given by the string targetRep. Possible 
%   strings for representations are:
%      Vrep - V-representation (convex hull or extreme points)
%      Hrep - H-representation (halfspaces)
%
% convertPolytope(.., 'zerotol', zerotol) lets you specify the zerotol that
%   is used to identify values that are close to zero by zero (default is 
%   elkZerotol).
%
% convertPolytope(.., 'flatness', flatness) the parameter flatness is only 
%   applied if the dimension of the object is smaller than the dimension it
%   is embedded in (e.g. a 2D object in 3D space). The flatness is applied 
%   by adding [flatness/2] to all facet distances for which the 
%   corresponding hyperplane fully contains the polytope. Default: 0.
%
% convertPolytope(.., 'infinityBoxSize', size) is used only when the given
%   H-representation is unbounded. In that case the V-representation of the
%   intersection of the original object with an infinity box is calculated. 
%   The infinity box is a hypercube, centered at the origin and extending
%   to [-1, 1] for each dimension.
%
% convertPolytope(.., 'useCddlib', useCddlib) is a switch that enforces or
%   prevents from using the double-description method (cddlib) as a basis
%   for converions. The alternative is always tried, anyway.
% 
% See also: identifyPolytope, obtainPolytope, convertVrepToHrep,
%   convertHrepToVrep

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

%% Input handling
infoInput = identifyPolytope(polyInput);

[zerotol, flatness, infinityBoxSize, useCddlib] = mapOption(varargin, ...
    'zerotol', elkZerotol, ...
    'flatness', 0, ...
    'infinityBoxSize', inf, ...
    'useCddlib', 1);


%% conversion
if strcmpi(infoInput.rep, targetRep)
    polyConverted = polyInput;
    
elseif strcmpi(infoInput.rep, 'Vrep') && strcmpi(targetRep, 'Hrep')
    polyConverted = convertVrepToHrep(polyInput, zerotol, flatness, useCddlib);

elseif strcmpi(infoInput.rep, 'Hrep') && strcmpi(targetRep, 'Vrep')
    polyConverted = convertHrepToVrep(polyInput, zerotol, infinityBoxSize, useCddlib);
    
else
    error('elk:polytope:wrongType', ...
        ['Check representation of input polytope; ' ...
         'Check name of target representation; ' ...
         'Check if conversion is supported']);
end
