將點存儲在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