Meshing a geometry and find mesh face normal vectors
Mostrar comentarios más antiguos
Hi
I want to know how to import a 3D model (.stl file),
mesh it using MATLAB,
identify only the mesh formed on the surface of the geometry and
find the normal vectors of each of the mesh element and its surface area?
Respuestas (2)
KSSV
el 9 de Sept. de 2020
0 votos
Emily Rolley-Parnell
el 24 de Oct. de 2022
Editada: DGM
el 1 de Abr. de 2025
Here's a snippet of code that should do the job
%Import mesh from file as a "discreteGeometry object" requires the Partial
%Differential Equation Toolbox
meshObj = importGeometry("sample_shape.stl");
x = meshObj.Vertices(:,1);
y = meshObj.Vertices(:,2);
z = meshObj.Vertices(:,3);
DT = delaunayTriangulation(x,y,z); %Note: Shape will always be a convex triangulation
[T,Xb] = freeBoundary(DT);
freeBoundaryT = triangulation(T,Xb);
%Plotting STL and surface normals
%incenter and faceNormal both require *triangulations*
P = incenter(freeBoundaryT);
F = faceNormal(freeBoundaryT);
trisurf(T,Xb(:,1),Xb(:,2),Xb(:,3), ...
'FaceColor','cyan','FaceAlpha',0.8);
axis equal
hold on
quiver3(P(:,1),P(:,2),P(:,3), ...
F(:,1),F(:,2),F(:,3),0.5,'color','r');
%% To find surface area
%Find the number of faces that make up the outer surface
nFaces = size(T,1);
areaArray = nan([nFaces,1]);
for n = 1:nFaces
P = Xb(T(n,1),:);
Q = Xb(T(n,2),:);
R = Xb(T(n,3),:);
PQ = Q - P;
PR = R - P;
areaArray(n) = 0.5 * vecnorm(cross(PQ, PR),2,2);
end
totalArea = sum(areaArray);
1 comentario
I don't see why it's necessary or desirable to use importGeometry() for this. The convexity restriction of this approach is severely limiting, and the file can be read directly without any need to retriangulate it.
unzip stepholecube.stl.zip
% read the file and convert it into discrete geometry
% and then try to convert it back into a triangulated surface
fname = 'stepholecube.stl';
meshObj = importGeometry(fname);
x = meshObj.Vertices(:,1);
y = meshObj.Vertices(:,2);
z = meshObj.Vertices(:,3);
DT = delaunayTriangulation(x,y,z); %Note: Shape will always be a convex triangulation
[T,Xb] = freeBoundary(DT);
T = triangulation(T,Xb);
% display it using patch()
patch('faces',T.ConnectivityList,'vertices',T.Points, ...
'facecolor','w','edgecolor','k');
view(3); camlight; view(10,33)
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
% instead, just read it using stlread()
% the result is a triangulated surface
T = stlread(fname);
% display it using patch()
patch('faces',T.ConnectivityList,'vertices',T.Points, ...
'facecolor','w','edgecolor','k');
view(3); camlight; view(10,33)
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
Perhaps the convex hull is your interpretation of "the mesh on the surface of the geometry". I admit, it seems like a vague enough of a question, so maybe that's fair. I'm inclined to suspect that it might also be referring to interior faces or other defects in a model such as a 3D scan, or something similar.
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

