Easy3D开发——点云孔洞填充

Easy3D开发——点云孔洞填充

  • 一、效果展示
  • 二、代码
    • 2.1参考链接:
    • 2.2完整代码
  • 三、其他效果
    • 多个孔洞点云填充效果对比
  • 四、后续


一、效果展示


二、代码

2.1参考链接:

参考论文链接:Filling Holes in Meshes
Easy3D开发——点云孔洞填充_第1张图片

2.2完整代码

#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace easy3d;

SurfaceMesh* hole_filliing_apply(SurfaceMesh* mesh,int size) {

    if (!mesh)
        return mesh;

    const int allowed_boundary_size = size;
    std::vector< std::pair<SurfaceMesh::Halfedge, int> > holes;

    auto visited = mesh->add_halfedge_property<bool>("DialogSurfaceMeshHoleFilling::h::visited", false);

    for (auto h : mesh->halfedges()) {
        if (!visited[h] && mesh->is_border(h)) {
            int size = 0;
            SurfaceMesh::Halfedge hh = h;
            do {
                visited[hh] = true;
                ++size;
                if (!mesh->is_manifold(mesh->target(hh))) {
                    size += 123456;
                    break;
                }
                hh = mesh->next(hh);
            } while (hh != h);

            if (size < allowed_boundary_size) {
                holes.push_back({ h, size });
            }
        }
    }
    mesh->remove_halfedge_property(visited);


    int num_closed = 0;

    ProgressLogger progress(holes.size(), true, false);
    for (const auto& hole : holes) {
        if (progress.is_canceled()) {
            LOG(WARNING) << "hole filling cancelled";
            return mesh;
        }
        SurfaceMeshHoleFilling hf(mesh);
        hf.fill_hole(hole.first);

        ++num_closed;
        progress.next();
    }
    if (holes.empty()) {
        LOG(WARNING) << "no holes found in the model";
    }
    else {
        LOG(INFO) << num_closed << " (out of " << holes.size() << ") holes filled";
    }
   
    return mesh;
}



int main(int argc, char** argv)
{
    //加载点云路径
    const std::string file_in_name = "bunnyhole1.obj";
    std::cout << "filename: " << file_in_name << std::endl;
    const std::string file_out_name ="bunny_fix.obj";
    SurfaceMesh* mesh = SurfaceMeshIO::load(file_in_name);

    //---孔洞填充--
    auto mesh_out=hole_filliing_apply(mesh,100);
    SurfaceMeshIO::save(file_out_name, mesh_out);

    //----可视化----
    Viewer viewer("viewer");
    viewer.add_model(mesh_out);
    viewer.run();

    return 0;
}

三、其他效果

多个孔洞点云填充效果对比

Easy3D开发——点云孔洞填充_第2张图片

Easy3D开发——点云孔洞填充_第3张图片

Easy3D开发——点云孔洞填充_第4张图片

Easy3D开发——点云孔洞填充_第5张图片


四、后续

后续会将easy3d中相关算法集成到《Easy3d+QT》专栏中。

你可能感兴趣的:(Easy3D开发,3d,c++,算法,孔洞修复,点云)