Create triangular mesh
Show older comments
Hello, I have a matrice with some edges that i need to conect. This results from a .stl file that has a hole and i need to fix...
badEdges =
1 2
1 4
2 4
What i need to do is to look for comum vertices in the rows and if there are 2, create a new edge conecting them. For example as the first and the second rows have the vertice 1 in comum, we should creat a new edge [2 4]. The same happens with the second and the third vertices with the vertice 4. Can someone help me with this? Thank´s.
Answers (2)
Eduardo
on 2 Apr 2012
0 votes
In order to fill the hole, you need three vertices and you need them in the correct direction. Your edge segments aren't in a consistent direction, so it's a 50/50 guess what the correct direction is. It's either [1 2 4] or [1 4 2]. Get it backwards, and the face will be inside-out. I suppose we could test the normal vector for intersection with the rest of the mesh, but it seems like an avoidable problem.
I think it would have been better to get the open edge segments as they are ordered in their parent triangles. The easy way would be to use triangulation() -- or since we're in 2012, TriRep(). Likewise, we're assuming that stlread() and stlwrite() don't exist yet.
unzip sphere_20.stl.zip
% a model with some isolated missing triangles
[V F] = stlRead('sphere_20.stl'); % FEX #51200
F([110 120],:) = []; % remove some faces
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','k');
view(3); camlight; view(10,33)
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
% get the open edges of the surface
% between R2009a - R2012b, TriRep() can be used;
% R2013a+, triangulation() can be used.
%T = triangulation(F,V);
T = TriRep(F,V);
BV = freeBoundary(T);
% prior to R2018b, segments of BV will not be sequentially ordered,
% but they will still have consistent winding direction.
% reorder the list if necessary; if there are multiple holes, split them
boundaries = fixboundaries(BV);
% each 3-segment boundary is easy to fill with a single triangle.
% filling larger holes is a further complication.
% neighboring triangles traverse their shared edge in
% opposite direction, so flip the boundary to get the triangle.
for k = 1:numel(boundaries)
thisb = boundaries{k}(:,1);
if numel(thisb) == 3
F = [F; fliplr(thisb.')]; %#ok<AGROW>
end
end
% write the new STL
stlWrite('test.stl',F,V) % FEX #20922, #51200
% show the repaired model
figure
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','k');
view(3); camlight; view(10,33)
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
See also:
Categories
Find more on STL (STereoLithography) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
