Get regular grid and points of a given stl file
70 次查看(过去 30 天)
显示 更早的评论
Hello,
I have have a stl file, which has many points (ca. ten thousand) but they are not uniformly distributed. As you know, a stl object has Faces, Vertices and Normals. The Vertices are the points (they can be repeated a few times, depending on how many triangles you can form with them).
I would like to create points in between the vertices (I don't care if I lose these initial points) so that they are evenly distributed. Imagine I project the object (just the points) in a 2D plane. I would like the points to be regularly distributed (like a grid but without the lines).
采纳的回答
Bruno Luong
2020-8-18
编辑:Bruno Luong
2020-8-21
I load the stl, cleanup the vertices, compute the face normal then I extract the top cap and interpolate on grid.
% Read mesh and compute normal vector
TR = stlread('Zahn_15_Bibliotheksmodell.stl'); % contains "Mesh" structure
[X,~,J] = unique(TR.Points,'rows');
F = J(TR.ConnectivityList).';
X = X.';
XF = reshape(X(:,F),3,3,[]);
N = cross(XF(:,2,:)-XF(:,1,:),XF(:,3,:)-XF(:,2,:),1);
% Extract the points on the top cap
Fup = F(:,N(3,:)>0);
Fup = sort(Fup,1);
n = max(Fup(:));
A = accumarray([Fup([1 2],:), Fup([1 3],:), Fup([2 3],:)]', 1, [n,n], [], 0, true);
G = graph(A+A');
bins = G.conncomp;
n = accumarray(bins(:),1);
[~,icap] = max(n);
icap = find(bins==icap);
Xcap = X(:,icap);
x = Xcap(1,:);
y = Xcap(2,:);
z = Xcap(3,:);
% Interpolation on grid
xmin = min(x); xmax = max(x);
ymin = min(y); ymax = max(y);
nx = 513; ny = 513; % define resolution of grids
xi = linspace(xmin,xmax,nx);
yi = linspace(ymin,ymax,ny);
[XI,YI] = meshgrid(xi,yi);
I = scatteredInterpolant(x(:),y(:),z(:),'linear','none');
ZI = I(XI,YI);
% Plot the result
figure(1);
ax=subplot(1,2,1);
imagesc(ax,xi,yi,ZI);
set(ax,'Ydir','normal','clim',[28 31])
colormap(ax,gray);
axis(ax,'equal');
axis(ax,'tight');
ax=subplot(1,2,2);
surf(ax,xi,yi,ZI,'linestyle','none');
There are some artefact, and did not try to see why, but it gives you an idea.
4 个评论
更多回答(1 个)
Diego Hens
2020-8-17
7 个评论
Bruno Luong
2020-8-18
The difficult part is you need to filter the points and keep only the point on the cap of the tooth. Because - as I said - your data is not of the form z=f(x,y) and there are multiple z for the same (x,y) (close to the boundary if one looks from the above). This will polute the interpolation.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Point Cloud Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!