c++ - what is the best way between : instantiate an object or use pointers -


we want create our own list of triangles list existed of stl mode (it's 3d geometric model, composed of triangle), several triangle can had same point, want use best solution:

s1) goes through list, use coordinates of each triangle (element) of list create triangle object , put in our list (victor). here there points must created multiple times, because have said many triangles can have same point.

s2) there existed list contains points, goes through list of triangle existed , each point of triangle search in list of points (so have use sort , search algorithms) use pointers (point on these points) , create objects contients 3 pointers (*p1, *p2, *p3) , put them in our list.

store points in std::unordered_set store triangles list of structures containing 3 std::unordered_set::const_iterator's.

insertion of points set approximately constant time , insert returns pair contains iterator point can found.

have here more details on how insert works.

here's basic structure of code (untested)

struct point {     float x;     float y;     float z; };  typedef std::unordered_set<point, int, hashfunc, equalsfunc> pset;  // note, see http://stackoverflow.com/questions/16792751/hashmap-for-2d3d-coordinates-i-e-vector-of-doubles more details on how store complex structures in unordered_sets  struct reftriangle {    pset::const_iterator p[3]; };  pset allpoints; std::list<reftriangle> reftriangles  (const triangle& t : trianglelist) {     reftriangle rt;     rt.p[0] = allpoints.insert(t.p1).first;     rt.p[1] = allpoints.insert(t.p2).first;     rt.p[2] = allpoints.insert(t.p3).first;     reftriangles.push_back(rt); } 

in end you'll have set of unique points , list of reference triangle objects have "pointers" points in unique set.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -