function [y,xg] = bssgrideval(xgrid,bss,evalfun)
% BSSGRIDEVAL - Generate the grid points and evaluate for the desired
% evaluation function and B-spline series.
%
% INPUTS:
%
%    xgrid:
%       A cell array containing grid vectors defining a compact grid used
%       for a gridded interpolation.
%
%    bss:
%       The B-Spline series data structure containing the series to be used
%       for evaluation.
%
% OUTPUTS:
%
%    y:
%       The values for the specified evaluation function at the grid points
%       defined by XGRID.
%
%       BSSGRIDEVAL generates and evaluates the points in the same order as
%       NDGRID except that it generates the gridded data points in subsets,
%       iterating over the dimensions to conserve memory, rather than
%       producing the full grid as separate variables and evaluating them.
%

%
% Copyright 2023 Biometrics Northwest LLC
%

%
% Created: 2023-02-18
% Author : Kevin R. Gehringer
%

if ( length(xgrid) ~= bss.ndim )
    error('Dimension mismatch: Dimensions of the compact grid and B-spline series did not agree.')
end

ndims = length(xgrid);
if ( ndims > 1 )
    xg = bssgridpoints(xgrid);
    switch( ndims )
        case 2
            y = bsseval2d(xg,bss,evalfun);
        case 3
            y = bsseval3d(xg,bss,evalfun);
        otherwise
            y = bssevalnd(xg,bss,evalfun);
    end
else
    if (iscolumn(xgrid{1}) )
        xg = xgrid{1};
    else
        xg = xgrid{1}';
    end
    y = bsseval1d(xg,bss,evalfun);
end

end
