Vertices2Volume

This function computes the volume of a 3D objects given its vertices X, Y, Z.
11 Downloads
Updated 9 Nov 2023

View License

% Volume of a cylinder of radius 2, and height 3
r = 2; h = 3;
[X, Y, Z] = cylinder([0,r,r,0], 32);
Z([1,2],:) = 0; Z([3,4],:) = h;
figure(color = 'w')
% using matlab's convex hull function
[tri, vol1] = convhull(X, Y, Z);
subplot(1,2,1)
trisurf(tri, X, Y, Z, FaceColor='cyan')
title('Convex Hull')
axis equal
% new Vertices to volume function
vol2 = Vertices2Volume(X, Y, Z);
subplot(1,2,2)
surf(X, Y, Z, FaceColor='cyan');
title('Actual object')
axis equal;
% analytical solution
vol3 = pi*r^2*h;
error1 = 100*abs(vol1-vol3)/vol3;
error2 = 100*abs(vol2-vol3)/vol3;
% Volume of a sphere of radius 2,
r = 2;
[X, Y, Z] = sphere(32);
X = r*X; Y = r*Y; Z = r*Z;
figure(color = 'w')
% using matlab's convex hull function
[tri, vol1] = convhull(X, Y, Z);
subplot(1,2,1)
trisurf(tri, X, Y, Z, FaceColor='cyan')
title('Convex Hull')
axis equal
% new Vertices to volume function
vol2 = Vertices2Volume(X, Y, Z);
subplot(1,2,2)
surf(X, Y, Z, FaceColor='cyan');
title('Actual object')
axis equal;
% analytical solution
vol3 = 4*pi*r^3/3;
error1 = 100*abs(vol1-vol3)/vol3;
error2 = 100*abs(vol2-vol3)/vol3;
% Volume of a Torus with minor radius 1 and major radius 5
r = 1.5; R = 4; u = linspace(0, 2*pi, 33);
z = r*sin(u); x = R + r*cos(u);
v = linspace(0, 2*pi, 65)';
X = cos(v)*x; Y = sin(v)*x;
Z = ones(size(v))*z;
figure(color = 'w')
% using matlab's convex hull function
[tri, vol1] = convhull(X, Y, Z);
subplot(1,2,1)
trisurf(tri, X, Y, Z, FaceColor='cyan')
title('Convex Hull')
axis equal
% new Vertices to volume function
vol2 = Vertices2Volume(X, Y, Z);
subplot(1,2,2)
surf(X, Y, Z, FaceColor='cyan');
title('Actual object')
axis equal;
% analytical solution
vol3 = 2*pi^2*r^2*R;
error1 = 100*abs(vol1-vol3)/vol3;
error2 = 100*abs(vol2-vol3)/vol3;

Cite As

Lateef Adewale Kareem (2024). Vertices2Volume (https://www.mathworks.com/matlabcentral/fileexchange/154825-vertices2volume), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0