function generateSingleProjection(saveFileName, pbeDef, result, ...
    approxOptions, regen, decExt)
% generate projection and save it in local database folder:
%     ./database/<caseName>.matx

if ~exist('regen', 'var') || isempty(regen)
    regen = 0;
end

% get my location
myPath = fileparts(which(mfilename));
% get save file name/location
thisSaveFile = [myPath filesep 'database' filesep saveFileName '.matx'];

if ~exist(thisSaveFile, 'file') || regen
    % data point matrix
    if isstruct(result)
        hcMatrix = [result(:).hcMatrixDynamic];
    else
        hcMatrix = result;
    end
    
    % handle treatment of extended embedding
    if exist('decExt', 'var')
        partitionVector = computePartitionIndexDec(...
            decExt.originalData, hcMatrix, [], 1)
        meanMappingMatrix = zeros(decExt.nC, decExt.originalData.nC);
        nTemp = 0;
        for iPart = 1:decExt.originalData.nPU
            thisFilter = iPart == partitionVector;
            nTemp = nTemp + sum(thisFilter);
            meanMappingMatrix = meanMappingMatrix + ...
                sum(thisFilter)/size(hcMatrix,2) * ...
                decExt.originalData.mappingOriginalToNewList{iPart};
        end
%         meanMappingMatrix = decExt.originalData.mappingOriginalToNewList{1};
        % this tweak is added to avoid numerical problems - the problem
        %   here is that the subspace in the embedding is close to the
        %   boundary of the validity cone and small numerical errors yield
        %   to an empty or dimensionally reduced validity cone for the
        %   projection of the decomposition
        meanMappingMatrix = meanMappingMatrix+2e-7;
        heMatrix = meanMappingMatrix * hcMatrix;
        dec = decExt;
        zerotol = 1e-7;
    else
        dec = pbeDef.dec;
        heMatrix = hcMatrix;
        meanMappingMatrix = [];
        zerotol = 1e-7;
    end
    

    % perform approximation
    dataApprox = approximateDec(heMatrix, dec, approxOptions{:});
    % project decomposition data
    dec = projectDec(dec, dataApprox.mappingReducedToEmbedding, ...
        'zerotol', zerotol);
    
    % save data
    dataApprox.dec = dec;
    dataApprox.cdef = pbeDef.dec.cdef;
    save(thisSaveFile, '-mat', 'dataApprox', 'meanMappingMatrix');
end