thanks for taking the time to read my question.
I am working on detecting holes in a triangular mesh and fill them with new triangles. I have done some of the parts that are, to get a list of edge vertices, etc. Following are the vertices/edges that make holes, please have a look at the image.
(9, 62) => vertex # 9 and 62 makes an edge (left hole)
(66, 9) => vertex # 66 and 9 makes an edge (left hole)
(70, 66) => vertex # 70 and 66 makes an edge (left hole)
(62, 70) => vertex # 62 and 70 makes an edge (left hole)
(147, 63) => vertex # 147 and 63 makes an edge (right hole)
(55, 148)
(63, 149)
(149, 55)
(148, 147)
The first thing that I need to do is to check which vertices make a cycle (means a hole is detected), and then save in a separate set of cyclic vertices.
The issue is to write such an algorithm that checks whether the given graph(vertices/edges) contains how many cycles? and then save into separate sets.
Please write me some simple and optimized algorithm to solve this problem.
Thank you.
mesh
Let assume your STL mesh got
n
triangles you need to convert it into indexed format. So extract all triangle points and convert to two separate tables. one holding all the points and second holding 3 indexes of points per each triangle. Let assume you gotm
points andn
triangles.You should have the point table (index) sorted and using binary search to speed this up from
O(n.m)
toO(m.log(n))
._edge
structurecreate structure that holds all the edges of your mesh. Something like:
where
p0<p1
.create
_edge edge[]
tableO(n)
It should be a list holding all the edges (
3n
) so loop through all the triangles and add 3 edges per each. The count set tocnt=1
This isO(n)
.Now sort the list by
p0,p1
which isO(n.log(n))
. After that just join all the edges with the samep0,p1
by summing theircnt
and deleting one of them. If coded right then this isO(n)
.detect hole
In regular STL each edge must have
cnt=2
. Ifcnt=1
then triangle is missing and you found your hole. ifcnt>2
you got geometric error in your mesh.So delete all edges with
cnt>=2
from youredge[]
table which isO(n)
.detect loops
let assume we got
k
edges left in ouredge[]
table. Now for each 2 edges that are sharing a point create triangle. Something like:If you use binary search for the inner loop then this will be
O(k.log(k))
. Also you should avoid to add duplicate triangles and correct the winding of them so first add the triangles to separate table (or remember starting index) and then remove duplicates (or you can do it directly inadd_triangle
).Also to handle bigger holes do not forget to add new edges to your
edge[]
table. You can either update the edges after current edges are processed and repeat #4 or incorporate the changes on the run.[Edit1] C++ example
recently I was doing some coding for STL for this QA:
So as I got all the infrastructure already coded I chose to give this a shot and here the result:
The full C++ code and description for the
STL3D
class is in the link above. I used some sphere STL mesh I found in my archive and color the hole patching triangles in green to recognize them. Here the result:The black lines are wireframe and red ones are just debug draw of the
edge[],pnt[]
arrays for debug ...As you can see it works even for holes bigger than just single triangle :) ...