You can use a map with the vertex as the key and the index as value
Starting from int counter = 0, traverse all vertices, if the map already contain this vertex then
index to this vertex = counter then set map[vertex] = counter++
Otherwise the index = map[vertex]
Of course you will to overload < operator for any type you use for vertex as the map expects its key to be comparable
Here is a sample code of map usage in unifying vertices
#include <iostream>
#include <map>
using namespace std;
struct Point
{
float x;
float y;
float z;
Point()
{
}
Point(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
bool operator<( const Point& p ) const
{
if(x < p.x)
return true;
if(y < p.y)
return true;
if(z < p.z)
return true;
return false;
}
};
void main()
{
Point p[4];
p[0] = Point(0,0,0);
p[1] = Point(1,1,1);
p[2] = Point(0,0,0);
p[3] = Point(1,1,1);
std::map<Point, int> indicesMap;
int counter = 0;
for(int i=0;i<4;i++)
{
if(indicesMap.find(p[i]) == indicesMap.cend()) // new vertex
{
indicesMap[p[i]] = counter++;
}
}
for(int i=0;i<4;i++)
{
std::cout << indicesMap[p[i]] << std::endl;
}
}
The output will be
0
1
0
1
as p[2] is p[0] and p[3] is p[1]