Is there a reason why my stl file isn't being read correctly?

12 views (last 30 days)
I have a code that reads stl files created in Inventor and plots them. Although it works well for some parts, there are some with which stlread has trouble. Instead of plotting the correct surface, it deforms it. My code is:
% Reads the file
data=stlread('Countercurves.stl');
x = data.Points(:,1);
y = data.Points(:,2);
z = data.Points(:,3);
%Meshing of the file
DT = delaunayTriangulation(x,y,z);
[Tfb,Xfb] = freeBoundary(DT);
TR = triangulation(Tfb,Xfb);
V = vertexNormal(TR);
%Plots the surface with the surface normals
figure
trisurf(TR,'FaceColor','cyan','FaceAlpha', 0.8);
axis equal
hold on
quiver3(Xfb(:,1),Xfb(:,2),Xfb(:,3), ...
V(:,1),V(:,2),V(:,3),0.5,'Color','r');
xlabel('X direction');
ylabel('Y direction');
zlabel('Z direction');
In the good cases, I get something like the following hemiellipsoid, whereas in the bad ones, I get the figure below:
The latter should be a sinusoidal surface. However, for some reason the spaces are filled in, and the code plots the surface normals of the edges. Any help?
  6 Comments
Cris LaPierre
Cris LaPierre on 2 Apr 2021
Edited: Cris LaPierre on 2 Apr 2021
Yes, now I do. stlread is loading the stl files just fine. It is your code to extract the surface normals that seems to be the issue.
unzip('stls.zip');
data=stlread('Countercurves.stl');
trisurf(data)
figure
data2=stlread('SingleCurvature_r100.stl');
trisurf(data2)
It looks like you are duplicating the information already contained in an stl file. Try simplifying your code to the following (after loading the file).
%Meshing of the file
V = vertexNormal(data);
%Plots the surface with the surface normals
figure
trisurf(data,'FaceColor','cyan','FaceAlpha', 0.8);
axis equal
hold on
quiver3(data.Points(:,1),data.Points(:,2),data.Points(:,3), ...
V(:,1),V(:,2),V(:,3),0.5,'Color','r');
hold off
xlabel('X direction');
ylabel('Y direction');
zlabel('Z direction');
I've gone this far, so we might as well see what it looks like for the other stl file you shared.
%Meshing of the file
V = vertexNormal(data2);
%Plots the surface with the surface normals
figure
trisurf(data2,'FaceColor','cyan','FaceAlpha', 0.8);
axis equal
hold on
quiver3(data2.Points(:,1),data2.Points(:,2),data2.Points(:,3), ...
V(:,1),V(:,2),V(:,3),0.5,'Color','r');
hold off
xlabel('X direction');
ylabel('Y direction');
zlabel('Z direction');
Jaime Castiblanques
Jaime Castiblanques on 3 Apr 2021
Hello Cris,
This has solved my problem. I had not realized I was duplicating the data, so thank you for pointing that out.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 1 Apr 2021
Edited: Bruno Luong on 1 Apr 2021
You should not use delaunayTriangulation to make connectiviy of the points. It creates a convex hull of all the points.
Usualy the connectivity is from structure read from the file.
  2 Comments
Bruno Luong
Bruno Luong on 2 Apr 2021
Edited: Bruno Luong on 2 Apr 2021
See Cris answer, he plots TRISURF directly from the data structure read from file.
The STL data has a list of point AND the connectivity.
Please inspect the structure returns by stlread and read documentation of TRISURF.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!