将点存储在std::unordered_set
中,然后将三角形存储为包含3个std::unordered_set::const_iterator
的结构列表。
将点插入到集合中将近似为常量时间,并且插入返回一个包含可找到点的迭代器的对。
查看here了解插入方式的更多详细信息。
下面的代码的基本结构(未经测试)
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 for more details on how to store complex structures in unordered_sets
struct RefTriangle
{
pset::const_iterator p[3];
};
pset allPoints;
std::list<RefTriangle> refTriangles
for (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);
}
最后,你将有一组独特的点和参考三角形对象的列表,有效地在“指针”,以这些点独特的设置。
除非您内存不足(或使用太多),否则存储简单的结构并避免使用指针。另一方面,几何算法可以利用了解属于多个三角形的点。 –
这个问题我不清楚。一个例子会有很大的帮助。 – Nawaz
谢谢先生@DieterLücking – HamidMly