Find holes and gaps in .stl files

26 views (last 30 days)
Eduardo
Eduardo on 2 Feb 2012
Commented: mahmoud elmesery on 24 May 2021
Hi,
I need to find gaps and holes in a stl file. I don´t know if there´s a function developed that can help me. I started to work with MATLAB recently and i´m finding some dificulties. I found 2 ways of doing this, each triangle should have 3 adjacent triangles or each edge must have 2 triangles in common. Hope someone could give a hand. Thank´s.
Eduardo G.
  3 Comments
Walter Roberson
Walter Roberson on 9 Dec 2013
Please be more specific about the errors you are observing.
agdfg sdgfdg
agdfg sdgfdg on 10 Dec 2013
[unqEdges, ~, edgeNos] = unique(edges,'rows'); | Error: Expression or statement is incorrect--possibly unbalanced (, {, or [. this the error i observed.
And i request you to suggest some other algorithms or codes for STL File Correction.

Sign in to comment.

Accepted Answer

Sven
Sven on 3 Feb 2012
Hi Eduardo,
Firstly you need to read the .stl file in as faces and vertices. I'll presume you've already got that done (as Anton points out, stlread is available here http://www.mathworks.com/matlabcentral/fileexchange/22409-stl-file-reader).
Next, as you wrote: each edge must have 2 triangles in common... I think this is a good start. Here's some code that will make a test faces/vertices mesh, then determine such a thing:
% MAKE a face/vertices structure
tmpvol = zeros(20,20,20); % Empty voxel volume
tmpvol(8:12,8:12,5:15) = 1; % Turn some voxels on
fv = isosurface(tmpvol, 0.99);
% REMOVE a face
fv.faces(30,:) = []; % Remove a face!
% TEST for gaps or holes
edges = sort(cat(1, fv.faces(:,1:2), fv.faces(:,2:3), fv.faces(:,[3 1])),2);
[unqEdges, ~, edgeNos] = unique(edges,'rows');
if size(edges,1) == size(unqEdges,1)*2
% Every edge is used twice... consistent with a closed manifold mesh
disp('No problem!')
else
badEdgesMask = hist(edgeNos, 1:max(edgeNos))~=2;
badEdgeNos = edgeNos(badEdgesMask);
badNodeNos = edges(badEdgeNos,:);
badFaceNos = find(sum(ismember(fv.faces, badNodeNos),2)>=2);
end
Does this answer the question for you?
  2 Comments
Eduardo
Eduardo on 22 Feb 2012
Thanks. This is exactly what i was looking for. Now i have to fill in the hole if one or more were found... Do you have any idea how to do it?
mahmoud elmesery
mahmoud elmesery on 24 May 2021
Hello, How to substract two stl files in matlab

Sign in to comment.

More Answers (3)

Eduardo
Eduardo on 4 Feb 2012
Hi,
First of all thanks. I´m using this 'stlread':
and then i´m using 'patchslim'. Now i´ll try your code and i´ll give you feedback.

Eduardo
Eduardo on 26 Feb 2012
Hi. I´ve made some tests and discovered that when a hole is found this code is not working correctly. When we axecute "badEdgeNos = edgeNos(badEdgesMask);" this should compare badEdgesMask with the number 1 and not with the first element of edgeNos. Can you understand what i´m trying to explain?
  2 Comments
Sven
Sven on 29 Feb 2012
If you mean that the variable badEdgesMask only contains ones and zeros, then you need to realise that it's using logical indexing... if you're sure there's a problem, make a small example (such as the one I made) and show how it's not working.
Eduardo
Eduardo on 29 Feb 2012
I´ve made a test with a simple cube. I removed 1 face and badEdgesMask is correct. The problem is in "badEdgeNos = edgeNos(badEdgesMask);" because badEdgesMask has 18 elements and edgeNos has 33. The comparison is made 1 by 1 element, so it compares the first logical value with the first element of edgeNos, i think it should be the first logical value with the element 1 of edgeNos (corresponding edge "1 2").

Sign in to comment.


Eduardo
Eduardo on 25 Mar 2012
I made it like this to solve the problem i´ve told you:
edges = sort(cat(1, fv.faces(:,1:2), fv.faces(:,2:3), fv.faces(:,[3 1])),2);
[unqEdges, ~, edgeNos] = unique(edges,'rows');
if size(edges,1) == size(unqEdges,1)*2
% Every edge is used twice... consistent with a closed manifold mesh
disp('No problem!')
else
badEdgesMask = hist(edgeNos, 1:max(edgeNos))~=2;
[~, ~, edgeNos1] = unique(unqEdges,'rows');
badEdgeNos = edgeNos1(badEdgesMask);
badNodeNos = unqEdges(badEdgesMask,:);
badFaceNos = find(sum(ismember(fv.faces, badNodeNos),2)>=2);
end
I don´t know if this the best way to do it, but it works...

Community Treasure Hunt

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

Start Hunting!