function [Vol] = calculate_volume_from_surface_mesh(nMat, eMat)
% Attempts to calculate the volume of a surface mesh.
% Useful for estimating the mass of objects enclosed by surface mesh.
%
% :param nMat: [n x 4] Matrix of node indices and coordinates
% :param eMat: [m x 5] Matrix of element indices and connectivity
% :return Vol: double - Volume enclosed by the elements.
%
% NOTE THAT THE CALCULATION IS MISSING A NUMBER OF ERROR CHECKS.
% THIS MEANS IT IS POSSIBLE TO HAVE A NON-CLOSED HULL AND STILL
% GET A RESPONSE.
%
% created by : ABR august@lightness.eu
% date : 2024-04-24

% Compress node numbers
nCompressed = [];
eCompressed = eMat;
for bLoop = 1:size(nMat,1)
  nCompressed(bLoop,:) = [bLoop nMat(bLoop,2:4)];

  selIdx1 = eMat(:,3) == nMat(bLoop,1);
  selIdx2 = eMat(:,4) == nMat(bLoop,1);
  selIdx3 = eMat(:,5) == nMat(bLoop,1);

  eCompressed(selIdx1, 3) = bLoop;
  eCompressed(selIdx2, 4) = bLoop;
  eCompressed(selIdx3, 5) = bLoop;
end

% Calculate Volume.
[Vol, ~, ~, ~, ~] = Polyhedron_VAC(nCompressed(:,2:4),eCompressed(:,3:5));
