I am working with a serialization pipeline. I am taking a model and exporting it. I do not wish to export any model with a hole in a face. How would I detect a hole and report a error?
I have access to all vertexes, edges, faces etc.
Here is a picture of what I mean.
As you can see there is a hole in the face. I am fairly new to geometry so please try to explain in layman terms.
If a 3D object is "simple", meaning that it doesn't have holes, it satisfies Euler's Formula for Polyhedra,
V - E + F = 2
whereV
is the number of vertices in the figure,E
is the number of edges, andF
is the number of faces. If you can easily obtain those three numbers, you can calculate the formulaV - E + F
. If the result is not 2, the object has a hole (or some other pathology like a pinch). In fact, you can tell how many holes the object has byV - E + F
: if the number is 0, it has one hole; if the number is -2, it has two holes; etc.Calculating
V
,E
, andF
can be a little tricky because vertices are generally shared by two or more edges, and edges are generally shared by two faces. You don't want to overcount; if three edges meet in a single vertex, you only want to count the vertex once, not three times.Not only that, but it's easy to make a mistake counting when the shapes have holes (which is exactly the case you're interested in). The easiest way to avoid making a mistake is to break the figure up into convex parts with e.g., triangulation.
The formula doesn't tell you which face has the hole, but if you know that the figure has a hole you can apply Euler's formula to each face individually, again with triangulation. In that case, faces without holes will have
V - E + F = 1
whereV,E,F
are now restricted to the face in question. (the discrepancy with the previous formula is resolved if you count the region outside the face as another (infinite) face). Faces with holes will haveV - E + F < 1
.For example, a triangle on the plane has
V=3
,E=3
, andF=1
(the "face" of the triangle represented by its interior) givingV-E+F=1
. On the other hand, a triangle with a similarly shaped triangular hole inside, in which corresponding vertices of inner and outer triangles are connected, will haveV=6
,E=9
, andF=3
forV-E+F=0
. I have broken the figure into three convex quadrilaterals in this case.Most books on computer graphics have a discussion of this topic.