function esDec = addUnifyingMappingData(esDec)
% add mapping information
%
% 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/>.

% this will be the fields:
%   .mappingOriginalToNew
%   .mappingNewToOriginal
%
% To map an original hC vector to the extended embedding, we would do the
%   following:
%       1) Compute the support measures for all facets of the extended
%          embedding
%       2) Map to the hE vector via the normal mappingFullToReduced matrix
% Since the computation of the support measures is dependent on the unified
%   partition, the overall mapping (mappingOriginalToNew) will also depend
%   on the unified partition. Additionally, we can construct the matrix
%   that maps from hC to h by the measure computation coefficients.

%% original decomposition with required measures
dec = esDec.originalData;
dec = rmfield(dec, 'measureData');
dec = addMeasureDataDec(dec, 'support', esDec.A);

%% generate mapping matrices
esDec.originalData.mappingOriginalToNewList = cell(1, dec.nPU);
esDec.originalData.mappingNewToOriginalList = cell(1, dec.nPU);

for iPart = 1:dec.nPU
    simplexIndex = esDec.originalData.unifiedToSimplexVector(iPart);
    mappingOriginalToFull = zeros(esDec.nH, dec.nC);
    for iRow = 1:esDec.nH
        mappingOriginalToFull(iRow, :) = ...
            dec.measureData.support.tensorHc{iRow}{simplexIndex};
    end
    
    esDec.originalData.mappingOriginalToNewList{iPart} = ...
        esDec.mappingFullToReduced * mappingOriginalToFull;
    esDec.originalData.mappingNewToOriginalList{iPart} = pinv(...
        esDec.originalData.mappingOriginalToNewList{iPart});
end