I'm in the process of trying to write a mesh preprocessor which (should) be identifying all edges in my mesh. It isn't - it tends to identify roughly half of them, and I'm trying to figure out why.
(All of this code is from the Content Processor, and is part of the GeometryContent processing, btw)
The code I'm using for finding the edges (distilled down to the
essential method - I know there would be issues trying to run this as
written) is:
for (int i = 0; i + 2 < myGeometry.Indices.Count; i += 3)
{
myEdge1 = new myEdge(myGeometry.Indices
, myGeometry.Indices[i+1]);
myEdge2 = new myEdge(myGeometry.Indices[i+1], myGeometry.Indices[i+2]);
myEdge3 = new myEdge(myGeometry.Indices[i+2], myGeometry.Indices
);
myEdgeList.Add(myEdge1);
myEdgeList.Add(myEdge2);
myEdgeList.Add(myEdge3);
}
On the theory that every triangle we render for the mesh is going to have three edges - and that every edge should therefore be referenced by two triangles. (in the real code, I check to see if I've already found the edge before adding it - but it's easier to see the basic approach this way)
And I added an extra function (shown here) in order to do some validation:
public int EdgeFound(int i1, int i2, IndexCollection Icollection)
{
int count = 0;
for (int i = 0; i + 2 < Icollection.Count; i += 3)
{
int vFound = 0;
if ((Icollection
== i1) || (Icollection[i+1] == i1) || (Icollection[i+2] == i1))
vFound++;
if ((Icollection
== i2) || (Icollection[i + 1] == i2) || (Icollection[i + 2] == i2))
vFound++;
if (vFound == 2) count++;
}
return count;
}
Now, in theory, in my mesh any pair of vertices should occur in either 0 or 2 triangles. At least, I designed the edge processor (and basically everything else) to operate under that assumption. When I run the validator on the edges in a sample GeometryContent though, I get very weird results:
Example (This is the tank.fbx mesh from the CustomModel sample, for reference)
First GeometryContent:
# of Edges which only occur once: 3814
# of Edges which occur twice: 8900 (or 4450)
Second GeometryContent:
# of Edges which only occur once: 1868
# of Edges which occur twice: 3688(or 1844)
Third GeometryContent:
# of Edges which only occur once: 910
# of Edges which occur twice: 1526(or 763)
Fourth GeometryContent:
# of Edges which only occur once: 1580
# of Edges which occur twice: 3604(or 1802)
... etc
Now, some of these I can explain due to boundaries between model parts (ie, the triangle on the other half of the edge was in a different GeometryContent, assuming that there's no requirement for an individual GeometryContent to be a closed mesh) but I don't understand how 50% of the edges I'm finding (in what looks like a closed mesh to me) are being flagged as only occuring once. Add this to the fact that when I render the edge points I'm finding, half of them are obviously not being detected, it's clear that something's going wrong somewhere in here.
I'm just not sure if it's the mesh, the code, or something else entirely. I half suspect that I'm missing something blindingly obvious - or am making a stupid API mistake.
Anyone have any ideas?
(Edit: Note, these are the results from the tank.fbx model, but I'm getting similar behavior from everything I try, so it's probable that I'm doing something stupid here...)