
Volume defined by vertices to stl file to mesh
5 views (last 30 days)
Show older comments
Hi,
I have the following points defining a hexahedra in space.
x = [0 0 0 0 0.1 0.1 0.1 0.1]';
y = [0 0.1 0 0.1 0 0.1 0 0.1]';
z = [0 0 100e-6 100e-6 0 0 50e-6 50e-6]';
figure
plot3(x,y,z,'.')
DT = delaunayTriangulation(x,y,z)
C = convexHull(DT)
trisurf(C,x,y,z)
stlwrite(TR,'teste.stl') % does not work
How do I create the FACES and VERTICES to use the File Exchange function properly from the points I defined or is there other way to write this stl file?
The FEATool allows us to create and refine the mesh, so I would need only the faces of the hexahedra, not to pass the actual discretized mesh to the toolbox.
Any hints on how to create the FACES and VERTICES from the points defining the hexahedra and how to create the stl file?
0 Comments
Accepted Answer
Precise Simulation
on 8 Mar 2019
As this is a simple case, you can just create a FEATool Multiphysics toolbox compatible STL file manually, for example with the following code
x = [0 0 0 0 0.1 0.1 0.1 0.1]';
y = [0 0.1 0 0.1 0 0.1 0 0.1]';
z = [0 0 100e-6 100e-6 0 0 50e-6 50e-6]';
DT = delaunayTriangulation(x,y,z);
C = convexHull(DT);
fid = fopen( 'featool.stl', 'w' );
s_fmt = [' facet normal %g %g %g\n', ...
' outer loop\n', ...
' vertex %g %g %g\n', ...
' vertex %g %g %g\n', ...
' vertex %g %g %g\n', ...
' endloop\n', ...
' endfacet\n'];
fprintf( fid, 'solid\n' );
for i=1:size(C,1)
p1 = DT.Points(C(i,1),:);
p2 = DT.Points(C(i,2),:);
p3 = DT.Points(C(i,3),:);
n = cross( p1-p2, p1-p3 );
n = n/norm(n);
fprintf( fid, s_fmt, n, p1, p2, p3 );
end
fprintf( fid, 'endsolid' );
fclose( fid );
However, as this is a very simple geometry and one with very large aspect ratio, it is faster and probably also better to just create the mesh manually directly. For example using the built-in blockgrid and gridrefine commands, one gets
grid = blockgrid( 1, 1, 1, [0 0.1;0 0.1;0 100e-6] );
grid.p(3,[6,8]) = 50e-6;
for i=1:3
grid = gridrefine( grid );
end
plotgrid( grid )
axis normal

Please be aware that the aspect ratio of this geometry is 2000 which is very high, to get good results from your simulations you had better scale the equations in the z-direction correspondingly.
0 Comments
More Answers (2)
DGM
on 27 Sep 2025 at 6:05
Edited: DGM
2 minutes ago
You don't need to write an STL encoder from scratch. Just get the faces and vertices and write the file. There are three pitfalls at play here. The first two were more or less figured out, but let's start there.
There are a number of FEX tools that all have the same name, but the are not mutally compatible. You have to pay attention to which tool you're trying to use and use the appropriate syntax.
If you feed a 3D point cloud to delanunayTriangulation(), you get a tetrahedral mesh. None of these tools support tetrahedral meshes. Theoretically STL can represent it, but that's only because the format specification is vague. I know of no encoders or decoders which support anything other than a triangular mesh. It may suffice to just use the convex hull.
% some points in space
x = [0 0 0 0 0.1 0.1 0.1 0.1]';
y = [0 0.1 0 0.1 0 0.1 0 0.1]';
z = [0 0 100e-6 100e-6 0 0 50e-6 50e-6]';
% triangulate the convex hull of the point cloud
DT = delaunayTriangulation(x,y,z);
F = convexHull(DT);
V = DT.Points;
% write the file, assuming we have R2018b+
%T = triangulation(F,V);
%stlwrite(T,'myfile.stl') % built-in
% write the file, assuming we have an older version
stlwrite('myfile.stl',F,V) % FEX #20922
An attentive reader may note that there's an obvious remaining problem, and it's right in front of our face now. While this covers the period-correct case of using the #20922 encoder, it still only writes three faces!
% ... or does it?
actualnumfaces = (dir('myfile.stl').bytes - 84)/50 % assuming the file is a binary STL
Ignore the little message it dumps to console. It's wrong. It's always 3. That's just an old bug in #20922. This bug is fixed in FEX #182013, but that's a bit of an understatement.
0 Comments
See Also
Categories
Find more on Point Cloud Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!