Is there a reason why my stl file isn't being read correctly?
조회 수: 22 (최근 30일)
이전 댓글 표시
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/553997/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/554002/image.png)
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
Cris LaPierre
2021년 4월 2일
편집: Cris LaPierre
2021년 4월 2일
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');
채택된 답변
Bruno Luong
2021년 4월 1일
편집: Bruno Luong
2021년 4월 1일
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
Bruno Luong
2021년 4월 2일
편집: Bruno Luong
2021년 4월 2일
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.
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!