Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dandelion 1.1.1 几何处理 EdgeRecord 的比较函数可能导致 std::set 行为异常 #33

Open
longskyi opened this issue Nov 15, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@longskyi
Copy link

问题文件路径 src/geometry/meshedit.cpp

问题描述

meshedit.cpp 文件中的 HalfedgeMesh::EdgeRecord 的重载<运算符函数当前实现如下:

bool operator<(const HalfedgeMesh::EdgeRecord& a, const HalfedgeMesh::EdgeRecord& b)
{
    return a.cost < b.cost;
}

该函数会被用于在 std::set<EdgeRecord> edge_queue中对 EdgeRecord 进行排序。然而,当两个 EdgeRecord 对象的 cost 值相等时,由于std::set 要求严格弱序且元素唯一,两个不同edge对应的EdgeRecord对象会被视为相同,可能导致接下来的行为(如对某条edge的EdgeRecord进行erase)不符预期。

问题复现

在简化cow时,会出现10余条边重复

[Halfedge Mesh] [info] original mesh: 2930 vertices, 5856 faces
[Halfedge Mesh] [debug] edges size :8784 edge_queue size :8772

在简化dragon2时,会出现高达20余万条边重复

[Halfedge Mesh] [info] original mesh: 180474 vertices, 360944 faces
[Halfedge Mesh] [debug] edges size :541416 edge_queue size :340212

重复的cost值集中在10e-6量级,并非无意义值。

问题修复

为了解决这个问题,建议修改运算符<重载函数,使其在 cost 相等时比较edge指针:

bool operator<(const HalfedgeMesh::EdgeRecord& a, const HalfedgeMesh::EdgeRecord& b)
{
    if (a.cost != b.cost) {
        return a.cost < b.cost;
    }
    return a.edge < b.edge;
}
@longskyi longskyi added the bug Something isn't working label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant