cut an stl file and regenerate new mesh on the cutted face

6 views (last 30 days)
For my project I'm writing a code so that I can cut stl files in half and determine the volume of the 2 halves to check if the volume is the same and thereby confirm how symetrical the object is.
So far I managed to crop the code but then the object appears with an open side wall as it can be seen in the picture.
Any kind of help would be welcome, thanks in advance.
%% Crop stl file
F1 = cropPatch(ba, [0 3000], [0 3000], [0 3000])
D = patch(F1,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'gouraud', ...
'AmbientStrength', 0.15);
% Add a camera light, and tone down the specular highlighting
camlight('headlight');
material('dull');
% Fix the axes scaling, and set a nice view angle
axis('image');
view([-135 35]);
  7 Comments
Dirk Snoeijen
Dirk Snoeijen on 23 Nov 2023
I've found an other way to do it.
First I'll make a plot with only the mesh with the function below.
%% make a mesh plot
X = ba.Points;
Y = ba.ConnectivityList;
plot_mesh(X,Y) %plot mesh with the "plot_mesh" toolbox
That will make our sphere like in the picture below.
Then I'll us a filter function where all the data points above my given limit will be given the same value as my limit value which can be done using the code below.
% vectorize the matrix into x,y,z axis
Ex = X(:,1); %vector maken van de x as
Ey = X(:,2); %vector maken van de y as
Ez = X(:,3); %vector maken van de z as
first make arrays of the vector
%% filter all points back to the limit value
Ey2 = filtermeetfout_ondergrens(Ey, 10, 0)';
E2 = [Ex, Ey2, Ez];
plot_mesh(E2,Y)
with the function "filtermeetfout_ondergrens" I've filterd all the data points back to y = 10 what results in only a part of the sphere.
DGM
DGM on 2 Apr 2025
Edited: DGM on 2 Apr 2025
This does not work generally. A simple counterexample:
%a file
unzip squarefrust.stl.zip
T = stlread('squarefrust.stl');
F0 = T.ConnectivityList;
V0 = T.Points;
% cut it in "half"
zrange = imrange(V0(:,3)); % the part spans z = [0 1]
cutheight = mean(zrange); % the midpoint
mask = V0(:,3) > cutheight; % make a mask
V1 = V0;
V1(mask,3) = cutheight; % move those points to the cut line
% display it using patch()
patch('faces',F0,'vertices',V0,'facecolor','w', ...
'edgecolor','k','facealpha',0.4); hold on
patch('faces',F0,'vertices',V1, ...
'facecolor','r','edgecolor','k');
view(3); camlight;
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
% measure the volumes
[v0,~] = stlVolume(V0',F0');
[v1,~] = stlVolume(V1',F0');
ratio = v1/v0
ratio = 0.5000
% is the part symmetric?
tol = 0.01;
issymmetric = abs(ratio - 0.5) < tol
issymmetric = logical
1
The part is obviously not symmetric across Z = 0.5, but because of the simplicity of the mesh, our poor attempt at truncation actually becomes a scaling. The naive test gives us the wrong answer
For finer meshes, it may be a fair approximation, but your volume calculation will need to contend with all the degenerate faces you'll likely be creating when you collapse the mesh like that. If I recall, stlVolume() does not return correct results if there are degenerate faces. I'm pretty sure I modified this copy at some point.
In order to actually cut the model, it needs new vertices and faces added. It can be done, but in general it's not something that you'd readily do with base tools.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!