function dec = cleanupDecStructure(dec)
% ensure proper format fieldnames of the decomposition structure
%
% We ensure that all fields are present and ordered like defined in
%   templateDec. Additionally, we handle the naming of the confinementCone
%   dependent on isComplete and isOrdinary. This shall ensure that
%   confinementCone is used accordingly.
%
% 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/>.

dec = mergeStruct(dec, templateDec);
dec = orderfields(dec, templateDec);

if dec.isOrdinary && dec.isComplete
    dec = rmfield(dec, {'confinementConeHull', 'confinementConePart', ...
        'validityConePart'});
elseif ~dec.isComplete
    dec.confinementConePart = dec.confinementCone;
    dec.validityConePart = dec.validityCone;
    dec = rmfield(dec, {'confinementCone', 'confinementConeHull', ...
        'validityCone', 'confinementMappingData'});
elseif ~dec.isOrdinary && dec.isComplete
    dec.confinementConeHull = dec.confinementCone;
    dec = rmfield(dec, {'confinementCone', 'confinementConePart', ...
        'validityConePart'});
else
    error('elk:decomposition:internalError', 'This case should not exist.');
end

% remove empty fields
removeIndexVector = find(structfun(@isempty, dec));
fieldnameList = fieldnames(dec);
dec = rmfield(dec, fieldnameList(removeIndexVector));