我有以下的代码,我想输入的基础上其3D最快访问可能的unordered_set坐标。最好的办法凑顶点
struct MeshVertex {
float x;
float y;
float z;
std::vector<MeshVertex*> links;
float& operator [](int i) {
switch (i) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw std::out_of_range("");
}
}
MeshVertex& operator=(const STLFileVertex& other)
{
x = other.x;
y = other.y;
z = other.z;
return *this;
}
bool operator<(const MeshVertex& other) const
{
if (x == other.x) {
if (y == other.y)
return z < other.z;
return y < other.y;
}
return x < other.x;
}
bool operator==(const MeshVertex& other) const {
if (x == other.x && y == other.y && z == other.z)
return true;
return false;
}
bool operator!=(const MeshVertex& other) const {
if (x == other.x && y == other.y && z == other.z)
return false;
return true;
}
double distance(const MeshVertex& other) const {
return hypot(hypot(x - other.x, y - other.y), z - other.z);
}
};
正如预期的那样,我得到以下错误:
The C++ Standard doesn't provide a hash for this type.
如何实现以及执行哈希值吗?它仅需要包含成员x,y和z和散列和比较的组合必须是自由的碰撞的100%,这意味着如果新插入一个具有完全相同的值的顶点只能更换。请考虑顶点由float
类型表示。这意味着正常的比较可能会产生误导。
编辑:
我真正做的是读取STL(立体)二进制文件。那些熟悉的STL格式将知道,三角形这样写的文件中:
float normals[3];
float vertex[3][3];
uint16_t attributes;
这意味着在读取文件时,顶点将被复制往往比多。我想标准化顶点(删除重复项)并应用链接重新创建三角形。
我的目标是映射图中的所有顶点,通过广度优先搜索。如前所述,链接在三角形中可用。获得所有链接后,三角形变为一次性的。
如果我没有完美的意思是删除重复的顶点,链接将断开,我的映射将失败。
100%免费的碰撞是没有必要的,在你的情况下,很容易实现一个理智的散列值大小。 – pvg
相关/也许欺骗?:https://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key – NathanOliver
非常相似,但并不完全相同。他们正在讨论一个unordered_map,我正在讨论一个unordered_set。此外,答案并不是非常令人信服,因为我需要的是无碰撞。 (如果无法避免冲突,我期望在回答来解释。 –