function vrep = computeSupportSetVrep(vrep, normal, zerotol, reduceDim)
% compute the support set of a V-polytope given a normal vector
%
% Syntax: vrep = computeSupportSetVrep(vrep, normal)
%
% The support set is the intersection of the polytope with a supporting
%   hyperplane, where the supporting hyperplane is a plane that exactly
%   touches the polytope.
%
% computeSupportSetVrep(..., zerotol) allows to set the tolerance for 
%   determining how close a vertex must be to the supporting hyperplane so
%   that it is considered as a vertex of the support set. Default: see 
%   elkZerotol.
%
% computeSupprtSetVrep(.., zerotol, reduceDim) The support set is usually 
%   returned in the appropriately reduced dimension (reduceDim = 1). Facets
%   return 2-dimensional polytopes (2 columns in vrep), edges 
%   1-dimenstional polytopes (1 column in vrep) and vertices return an
%   empty matrix. With reduceDim=0, the corresponding vertices are returned
%   in the original dimension.
%
% See also: projectVrep3d

% 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
if ~exist('zerotol', 'var') || isempty(zerotol)
    zerotol = elkZerotol;
end

if ~exist('reduceDim', 'var')
    reduceDim = 1;
end

if size(normal, 1) ~= size(vrep.V, 2)
    normal = normal';
end

% if reduceDim
%     %% Obtain set with reduction of dimension
%     vrep = rotatePolytope(vrep, 'vector', normal);
%     % so that we can look for the outer-most point in z-direction
%     zMax = max(vrep.V(:,3));
%     % and filter for all points that are in the same level
%     vrep.V = vrep.V(abs(vrep.V(:,3) - zMax) < zerotol, 1:2);
%     % but there might be more
%     vrep = reduceVrepDimension(vrep, zerotol);
%     vrep = reduceVrep(vrep);
% else
    outerDistanceVector = vrep.V * normal;
    maxOuterDistance = max(outerDistanceVector);
    vrep.V = vrep.V(abs(outerDistanceVector - maxOuterDistance) < zerotol, :);
    
    if reduceDim
        vrep = reduceVrepDimension(vrep, zerotol);
    end
% end


end