MATLAB: How to convert a triangulation object into volume and create a BITMAP image

bitmapMATLABobjecttriangulationvolume

I have an STL file an save it as an triangulation object. How can I now convert this triangulation object into volume and create a BITMAP image?

Best Answer

Below code is a good example for creating a STL file for 3D geometry, read the STL as traingulation object, save it as bitmap image and calculate the volume:
Script to create a STL file for 3D geometry. eg: cylinder
[X,Y,Z] = cylinder(r,100);
DT = delaunayTriangulation(X(:),Y(:),Z(:));
TR = triangulation(DT.ConnectivityList,DT.Points);
[F,P]=freeBoundary(TR);
TR = triangulation(F,P);
stlwrite(TR,'stl_test000.stl');
Method 1: may not be accurate for complicated shapes
%read the stl as a triangulation
gm = stlread('stl_test000.stl');
% use alphashape to create a bounding volume
shp=alphaShape(gm.Points);
plot(shp)
% If the plot does not look like the shape that you are expecting, change
% the critical radius for the alphashape
shp.Alpha = 5;
plot(shp); % this should plot the cylinder. the plot can be saved as a bitmap
% Get the volume value
shp.volume
Method 2: more accurate. Needs PDE Toolbox
pdem = createpde;
importGeometry(pdem,'stl_test000.stl');
% plot the geometry (3D)
pdegplot(pdem); % the plot can be saved as a bitmap
% mesh the geometry. For more accuracy, adjust the Hmax
msh = generateMesh(pdem,'Hmax',0.1);
% calculate the volume
msh.volume()
Related Question