function XTable = untransformPoints(this, X, HonorInts)
% Convert a matrix of transformed points to a table in native
% space. HonorInts=true means that the transformation will
% round reals to ints (and then to cats if necessary). false
% means the points are left as reals after transforming to
% native space. 'false' is only used for plotting meshes.
%

%   Copyright 2016-2022 The MathWorks, Inc.

if isempty(X)
    XTable = table;
else
    VarSpec = this.PrivOptions.VarSpec;
    Vars = struct;
    for v = 1:this.NumVars
        Name = VarSpec.Names{v};
        switch VarSpec.Types{v}
            case 'real'
                if VarSpec.isLog(v)
                    Vars.(Name) = exp(X(:,v));
                else
                    Vars.(Name) = X(:,v);
                end
            case 'integer'
                if VarSpec.isLog(v)
                    if VarSpec.LBs(v)==0
                        Vars.(Name) = expm1(X(:,v));            % Undo log1p
                    else
                        Vars.(Name) = exp(X(:,v));
                    end
                else
                    Vars.(Name) = X(:,v);
                end
                if HonorInts
                    % Convert real to int
                    Vars.(Name) = round(Vars.(Name));
                end
            case 'categorical'
                Vars.(Name) = X(:,v);
                if HonorInts
                    % Convert real to int to cat
                    idx              = round(Vars.(Name));
                    nanIdx           = isnan(idx);
                    idx(nanIdx)      = 1;                       % Brief dummy value
                    catNames         = VarSpec.Categories{v}(idx);
                    catNames(nanIdx) = '<undefined>';
                    Vars.(Name)      = catNames;
                end
        end
    end
    XTable = struct2table(Vars);
end
end
