function X=ProjectOnSn(X)

%*****************************************************************************80
%
%% project_on_sn() projects a set of points onto Sn.
%
%  Modified:
%
%    11 December 2021
%
%  Author:
%
%    Anton Semechko (a.semechko@gmail.com)
%
%  Input:
%
%   - X : N-by-d array of point coordinates, where N is the number of
%         points.
%   
%  Output:
%
%   - X : N-by-d array of point coordinates such that norm(X(i,:))=1
%


if ismatrix(X) && isnumeric(X)
    X=bsxfun(@rdivide,X,sqrt(sum(X.^2,2)));
else
    try
        [ Tri, X, fmt ] = get_mesh_data ( X );
    catch
        error('Invalid entry for 1st input argument (X)')
    end
    
    X=bsxfun(@rdivide,X,sqrt(sum(X.^2,2)));
    
    if fmt==1
        X=triangulation(Tri,X);
    elseif fmt==2
        X=TriRep(Tri,X); %#ok<*DTRIREP>
    elseif fmt==3
        X={Tri X};
    else
        X=struct('faces',Tri,'vertices',X);
    end
end

