function esDec = computeUnifyingDec(dec, zerotol)
% create embedding unifying representation from decomposition
%
% Syntax: esDec = computeEmbeddingSimpleDec(dec)
%
% A new matrix of facet normals dec.A is calculated that contains all
%   facets that could be created by adding structuring elements of the
%   input decomposition. A matching group mapping matrix is also generated.
%   The new decomposition would have a unified partitions, where all
%   structuring elements of the input decomposition are extreme rays.
%   Hence, any point represented in the input decomposition will reside at
%   the boundary of the output decomposition (and unified partition)
%
% All data is transformed to the new decomposition. This decomposition is
%   typically not complete (validity cone, unified paritions etc.).
%
% See also: obtainDec, projectDec

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

%! ToDo: this function should be named computeExtendedEmbedding, now. This
%    also changes the name of the private function.

%% input handling
if ~exist('zerotol', 'var') || isempty(zerotol)
    zerotol = elkZerotol;
end
if ~dec.isProper || ~dec.isOrdinary
    error('elk:decomposition:inputError', ['Handling of improper or ' ...
        'singular input data is not supported.'])
end

% copy original data
esDec.originalData = dec;

%% Extended H-representation
% find additional facets from pairwise Minkowski addition of structuring
%   elements. This creates a new embedding H-representation.
esDec.originalData.newFacetNormalMatrix = computeAdditionalFacetMatrix(...
    dec, zerotol);
esDec.A = [dec.A; esDec.originalData.newFacetNormalMatrix];
esDec.nH = size(esDec.A, 1);

%% Extended Hc-representation
% identify which subdomain is spanned by the structuring elements of the
%   original Hc-representation. We obtain a new Hc-representation that
%   might be called extended embedding (since it is not proper but will be
%   used as the embedding representation for an approximation).
esDec = computeExtendedEmbedding(esDec, zerotol);
% add mapping information
esDec = addUnifyingMappingData(esDec);
% assign measure information again (recomputation of al measures)
esDec = recomputeMeasureData(esDec, esDec.originalData);

