Vertices2Volume

This function computes the volume of a 3D objects given its vertices X, Y, Z.
14.0 次下载
更新时间 2023/11/9

查看许可证

% 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;

引用格式

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

MATLAB 版本兼容性
创建方式 R2023b
兼容任何版本
平台兼容性
Windows macOS Linux
标签 添加标签

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0