function dec = projectIncompleteDec(dec, par)
% project a complete proper representation
%
% Initial data is already present (facet normals, mapping, properData).
% 
% THIS IS NO USER FUNCTION

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

%% validity cone + facet validity data
% for an incomplete representation, the validity and confinement cone in
%   its original meaning is unknown. Both are artificially set equal to the
%   only known unified partition.
dec.validityCone.A = dec.properData.unifiedPartition{1}.A * ...
    dec.properData.mappingNewToProper;
% remove zero-rows and trim
dec.validityCone.A = trimConstraintMatrix(dec.validityCone.A, ...
    par.zerotol, 1);
% create unique rows: is done with addValidityCone

% add validity cone information, which adds the fields: validityCone, se
%   seScaledToClose, nS, centerRay. The following fiels are final:
%   validityCone, centerRay - all other fields will be extended
dec = addValidityCone(dec, dec.validityCone.A, par.zerotol);

% confinement cone is equal
dec.confinementCone = dec.validityCone;

%% decomposition
% initialize data
dec = appendUnifiedPartition(dec);
% append the one and only unified parition: it is always valid - if the
%   projection is pointless, this partition is at most empty

%! DEV: this caused trouble and I presume I don't need it when everything
%    happens in the embedding, anyway.
dec = appendUnifiedPartition(dec, 1:dec.nS, par.zerotol, 1, 1);

%% Validity (orthogonal) mapping
dec.validityMappingData = generateOrthogonalProjectionDataForCone(...
    dec.validityCone.A, ...
    dec.seScaledToClose(dec.validityCone.seIndexVector, :), ...
    par.zerotol);
%/

dec.isOrdinary = 0; % we can't know
dec.isProper = 0;
dec.isComplete = 0;

dec.nPS = length(dec.simplexPartition);
dec.nPU = length(dec.unifiedPartition);

%% measure information

% call convert measure tensor that was made for the conversion from the
%   seTensor to the hcTensor:
%     tensorFull - the full "original" tensor
%     indexVector - the entries that are required from this full tensor; in
%       this application, these are ALL entries
%     mapping - the mapping matrix from hC to lam coordinates; here this is
%       the mapping from hA to hE
% Normalization of the tensor is already part of that function
if ~isfield(dec.properData, 'measureData') || isempty(dec.properData.measureData)
    return
end
measureList = fieldnames(dec.properData.measureData);
for iMeasure = 1:length(measureList)
    copyMeasure = dec.properData.measureData.(measureList{iMeasure});
    pasteMeasure.name = copyMeasure.name;
    pasteMeasure.dim = copyMeasure.dim;
    pasteMeasure.isPar = copyMeasure.isPar;
    pasteMeasure.par = copyMeasure.par;
    if ~pasteMeasure.isPar
        pasteMeasure.tensorHc = cell(1, dec.nPS);
        for iPart = 1:dec.nPS
            pasteMeasure.tensorHc{iPart} = convertMeasureTensorSe(...
                copyMeasure.tensorHc{1}, ...
                1:dec.properData.nC, ...
                dec.properData.mappingNewToProper, ...
                pasteMeasure.dim);
        end
    else
        pasteMeasure.tensorHc = cell(1, size(pasteMeasure.par, 1));
        for iPar = 1:size(pasteMeasure.par, 1)
        pasteMeasure.tensorHc{iPar} = cell(1, dec.nPS);
        for iPart = 1:dec.nPS
            pasteMeasure.tensorHc{iPar}{iPart} = convertMeasureTensorSe(...
                copyMeasure.tensorHc{iPar}{1}, ...
                1:dec.properData.nC, ...
                dec.properData.mappingNewToProper, ...
                pasteMeasure.dim);
        end
        end
    end
    dec.measureData.(measureList{iMeasure}) = pasteMeasure;
end
