Skip to main content
GameDev.net gamedev.net
🔒 Locked

Container type for this problem

Started by maya18222 Nov 17, 2008 at 5:52 PM 1 replies 650+ views
Original Post
maya18222
maya18222
I have a mesh file which im extracting data from that contains a list of vertices to be drawn, but this list contains at least 30% duplicates, as the triangles/faces are sharing vertices, so im wondering how i can go through the list, and copy all non duplicates to a second container, such that all the vertices in the second container are different. Now for this i could use an std::set, but the other part of the problem is that i dont want the second list to have any sort of automatic sorting, becuase i'll be taken the position of any any added vertex. Here is the rough code

vector <vertex> data;
vector <vertex> OptimizedData
readFile(data);//fill data from file

//remove duplicates
for (all data)
   index = AddToVectorIfNotDuplicate(data[0], &OptimizedData);

So it goes through the data vector from start to end, and then adds to optimzeddata, only if that value is not already in optimzeddata, either way, it returns the position of the new/existing value. ie //data 1,2,3 3,4,5 6,6,6 6,6,6 1,2,3 //optimized data 1,2,3 3,4,5 6,6,6 //index 0 1 2 2 0
alvaro
alvaro
Here's some code that does what you want using a map. It would be better to use a hash_map, but hash_map is not standard [yet], so I would stick to map if you find it fast enough.
#include <iostream>#include <map>#include <vector>struct Point {  float x,y,z;  Point(float x, float y, float z) : x(x),y(y),z(z) {  }    bool operator<(Point const &p) const {    if(x!=p.x)      return x<p.x;    if(y!=p.y)      return y<p.y;    return z<p.z;  }};// new_vector and indices are supposed to be empty when this function is calledvoid optimize_data(std::vector<Point> const &old_vector, std::vector<Point> &new_vector,                   std::vector<size_t> &indices) {  indices.reserve(old_vector.size());  std::map<Point,size_t> new_index;    for(std::vector<Point>::const_iterator it=old_vector.begin(), end=old_vector.end(); it!=end; ++it) {    std::map<Point,size_t>::const_iterator found=new_index.find(*it);    if(found==new_index.end()) {      indices.push_back(new_vector.size());      new_index[*it]=new_vector.size();      new_vector.push_back(*it);    }    else      indices.push_back(found->second);  }}int main() {  std::vector<Point> data;  data.push_back(Point(1,2,3));  data.push_back(Point(3,4,5));  data.push_back(Point(6,6,6));  data.push_back(Point(6,6,6));  data.push_back(Point(1,2,3));    std::vector<Point> optimized_data;  std::vector<size_t> index;  optimize_data(data, optimized_data, index);    for(std::vector<Point>::const_iterator it=optimized_data.begin(), end=optimized_data.end();      it!=end; ++it)    std::cout << '(' << it->x << ',' << it->y << ',' << it->z << ")\n";  std::cout << '\n';    for(std::vector<size_t>::const_iterator it=index.begin(), end=index.end(); it!=end; ++it)    std::cout << *it << '\n';  std::cout << '\n';}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.