function partitionData = generateMinkowskiPartition(dec, hC, zerotol)
% generate a unified partition from Minkowski decomposition
%
% THIS IS NO USER FUNCTION

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2013 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/>.

%% initialize acuumulating array structure
partitionData.flagResult = [];
partitionData.hrep = [];
partitionData.seMatrix = [];
    
% quit if no input is given; decomposeCone uses this call to initialize
%   the accumulating array
if ~exist('dec', 'var')
    partitionData(:) = [];
    return
end

%% perform Minkowski decomposition
seMatrix = decomposeMinkowskiHrep(struct(...
    'A', dec.A, 'h', dec.mappingReducedToFull * hC), ...
    'constraint', dec.constraintMatrix, ...
    'zerotol', zerotol, ...
    'result', 'indecomposable', ...
    'matrix', 1);

%% project to reduced space
seMatrix = (dec.mappingFullToReduced * seMatrix')';

%% valid or not
% if full-dimensional, that is a partition! otherwise, we generate a
% new hC
if rank(seMatrix) == dec.nC
    % accept partition
    partitionData.flagResult = 1;
    partitionData.seMatrix = scaleToClose(seMatrix, dec);
    partitionData.hrep = openCone(convertVrepToHrep(closeCone(struct('V', ...
        partitionData.seMatrix ))));
      
else
    % strange, retry with new hC
    partitionData.flagResult = 0;
    
end

end