Indexing Vertexs Algorithm

Hi everyone I am having issues with my algorithm for indexing my vertexs.

Here is my code.

 public void IndexAllVertexs()
        {
            if (HaveIndexsVertexs)
                return;

            List<VertexPositionColourNormal> buffer = new List<VertexPositionColourNormal>(); //a buffer to store all unique vertes positions
            List<int> theindexs = new List<int>();
            for (int i =0; i < _originVerts.Length; i++)
            {
                if (!buffer.Contains(_originVerts[i])) //if its not in the buffer
                {
                    buffer.Add(_originVerts[i]); //add to the buffer
                    theindexs.Add(buffer.Count - 1); //add to the index
                }
                else
                {
                    //now if its already in the buffer find the location
                    theindexs.Add(buffer.IndexOf(_originVerts[i]));
                }
            }

            IndexVertexs = theindexs.ToArray();

            _originVerts = buffer.ToArray();
            HaveIndexsVertexs = true;
        }

Now even though some of the vertexs are at the same positions my code is not removing the duplicates and then indexing the vertexs.
I wondered if it is something to do with the rounding errors on floats and therefore that the vector3 are not comparing?

Did you try with Equals instead of your “contains()” part ?

https://msdn.microsoft.com/en-us/library/bb196782.aspx

Is the vertex type you use a class or a struct? If it’s a class and you didn’t overwrite the Equals method it wil check if the references are the same, rather than the values for the vertices. So if there are multiple instances of ‘the same’ vertex this won’t work because the references are different.

Being a vertex type, it should be a struct. For this, it will be doing a value equality comparison rather than a reference equality comparison. So in this case it just requires one of the numbers to be fractionally different for the equality test to fail and it to be considered a unique vertex. If the struct is defined in your code, you could implement an override of the Equals method and provide your own equality tests with some tolerance so fractional differences don’t matter so much.

It turned out to be the rounding errors on floats. Managed to manualy round the floats to get it to work.