Get regular grid and points of a given stl file

34 views (last 30 days)
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).
  4 Comments
Diego Hens
Diego Hens on 17 Aug 2020
sorry, I answered on the wrong spot. Can you take a look on my other comment?

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 18 Aug 2020
Edited: Bruno Luong on 21 Aug 2020
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 Comments
Bruno Luong
Bruno Luong on 20 Aug 2020
I edit with code to create mesh F and X and N manually
Diego Hens
Diego Hens on 21 Aug 2020
I'm a bit slow, but I think I've got it now (not really the code, but the problem in itself). Your answer was actually what I looked for (the part where you extract the points on the top cap I did not understand, but I could use the rest still). I think I can go on from here.
I have accepted it as an answer for this reason.
I will try to find out why there is noise and if I can avoid it. If not, I will look for other methods.
Thank you again, Bruno :D

Sign in to comment.

More Answers (1)

Diego Hens
Diego Hens on 17 Aug 2020
Ok, here's what I got.
Imagine the triangle on the left side was my object. It has values in X, Y and Z. I want to project the object into the XY plane (as in the right) and represent the Z value with a grayscale value. If I project the object I get the drawing ob the right. As I have only three vertices, I will only get three points with a grayscale value. That means (especially if the object is bigger) that I will have many holes, where no value Z is given, as there is no point.
I would like to be able to interpolate these values in between, so that I have no (or very little) holes.
Is the problem understandable now?
Thanks for the help :D
  7 Comments
Diego Hens
Diego Hens on 18 Aug 2020
I think that's exactly what I want. This is what I get:
F = scatteredInterpolant with properties:
Points: [25634×2 double]
Values: [25634×1 double]
Method: 'linear'
ExtrapolationMethod: 'linear'
How can I plot this now?
Bruno Luong
Bruno Luong on 18 Aug 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!