How can I cut(crop) patch(3d,stl file) object?
Show older comments
Hi I want to know how can I crop 3D patch in matlab
I have a Data 'skull' it is from .STL file ( It is from stl file and I read stl file from my matlab)
skull = stlread('Skull.stl')
patch(skull,'FaceColor', [0.8 0.8 1], 'EdgeColor','none', 'FaceLighting', 'gouraud', 'AmbientStrength', 0.15);
camlight('headlight');
material('dull');
(( skull data is struct like this))

And then I make a patch about skull (like this)

But that skull data quite large So I want to crop some region which is I interested (Like this)

((this picture just specify the plot range. like below ))
axis ([0,100,0,100,0,100])
But I want to save plot(patch) which is cropped as a Data like 'skull'
So I try it like below (I interest in range that is bigger than(0,0,0))
x = 0;
y = 0;
z = 0;
[r] = find((skull.vertices(:,1)>=x) & (skull.vertices(:,2)>=y) & (skull.vertices(:,3)>=z));
for i = 1:size(r,1)
interest.vertices(i,:) = skull.vertices(r(i,1),:);
% interest.faces(i,:) = skull.faces(r(i,1),:);
end
for i = 1:size(r,1)/3
interest.faces(i,3) = 3*i;
interest.faces(i,2) = 3*i-1;
interest.faces(i,1) =3*i-2;
end
And then I patched Data interest
patch(interest,'FaceColor', [1 0 0], 'EdgeColor','none', 'FaceLighting', 'gouraud', 'AmbientStrength', 0.15);
camlight('headlight');
material('dull');
So I got a patch(plot) like this


The overall shape exists but there is a lot of hole in the middle. How can I solve this problem? and What did I do wrong?
4 Comments
tejas alva
on 29 Nov 2018
Edited: tejas alva
on 29 Nov 2018
Hey! Did you figure out how to do this?
Tom Cristiani
on 14 Nov 2020
Edited: Tom Cristiani
on 14 Nov 2020
I was trying to do the same thing and came up with this function. I hope it helps!
The edges wind up a bit frayed. I might clean that up in a revision.
function [fv] = cropPatch(fvIn,xRange,yRange,zRange)
% fvIn is a typical patch structure containing "vertices" and "faces" fields
% xRange, yRange, and zRange are two element vectors in the form [xMin xMax], [yMin yMax], [zMin zMax]
% fv is the new cropped patch structure
vertexIndex = (fvIn.vertices(:,1) > xRange(1)) & (fvIn.vertices(:,1) < xRange(2)) & ...
(fvIn.vertices(:,2) > yRange(1)) & (fvIn.vertices(:,2) < yRange(2)) & ...
(fvIn.vertices(:,3) > zRange(1)) & (fvIn.vertices(:,3) < zRange(2));
vertexNums = find(vertexIndex);
fv = struct('vertices',[],'faces',[]);
faceIndex = ismember(fvIn.faces(:,1),vertexNums) & ...
ismember(fvIn.faces(:,2),vertexNums) & ...
ismember(fvIn.faces(:,3),vertexNums);
fv.vertices = fvIn.vertices(vertexIndex,:);
fv.faces = fvIn.faces(faceIndex,:);
[~,replaceIndex] = ismember(fv.faces,vertexNums);
fv.faces = replaceIndex;
end
Rawan
on 7 Dec 2022
Where is the model please?
naomy_yf
on 19 Dec 2022
i'm using the face and ver files to reindex the face and ver I need then creat the patch again.
Answers (1)
Mitchell Carlson
on 6 Mar 2020
0 votes
I had a similar problem and found that using the xlim, ylim, and zlim functions worked. In this case you could do something like this:
xlim = ([0 100]) % This only displays values with x between 0 and 100
ylim = ([0 100]) % Same thing for y
zlim = ([0 100])
Categories
Find more on STL (STereoLithography) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!