Sign in to follow this  

Container type for this problem

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] = 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

Share this post


Link to post
Share on other sites
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 called
void 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';
}



Share this post


Link to post
Share on other sites

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this