【问题标题】:corefine_and_compute_difference CGAL error: precondition violationcorefine_and_compute_difference CGAL 错误:违反先决条件
【发布时间】:2021-11-01 02:10:30
【问题描述】:

问题描述

我从文件“blank.off”中读取网格并将其加载到surface_mesh 变量blank。一个名为“hepoints49.txt”的文件存储点云。我使用函数CGAL::advancing_front_surface_reconstruction()将此点云转换为surface_mesh sv,然后使用函数corefine_and_compute_difference(blank,sv,res)blanksv之间进行布尔减法strong>。但是程序抛出异常并终止。终端显示如下:

Using context  4 . 3 GL
load sv...
Using context  4 . 3 GL
start difference...
CGAL error: precondition violation!
Expression : CGAL::is_valid_polygon_mesh(tm)
File       : D:\dev\vcpkg\installed\x64-windows\include\CGAL/Polygon_mesh_processing/orientation.h
Line       : 190

你能帮我解决这个问题吗?

代码

#include<iostream>
#include<io.h>
#include<fstream>
#include<algorithm>
#include<array>
#include<CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include<CGAL/Advancing_front_surface_reconstruction.h>
#include<CGAL/Surface_mesh.h>
#include<CGAL/disable_warnings.h>
#include<CGAL/draw_surface_mesh.h>
#include<ctime>
#include<string>
#include<CGAL/polygon_mesh_processing/corefinement.h>
#include<CGAL/polygon_mesh_processing/remesh.h>
#include<CGAL/boost/graph/selection.h>
#include<CGAL/polygon_mesh_processing/repair_self_intersections.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;

namespace PMP = CGAL::Polygon_mesh_processing;

typedef std::array<std::size_t, 3> Facet;
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point_3;
typedef CGAL::Surface_mesh<Point_3> Mesh;

struct Construct {
    Mesh& mesh;

    template <typename PointIterator>
    Construct(Mesh& mesh, PointIterator b, PointIterator e):mesh(mesh) {
        for (; b != e; ++b) {
            boost::graph_traits<Mesh>::vertex_descriptor v;
            v = add_vertex(mesh);
            mesh.point(v) = *b;
        }
    }

    Construct& operator=(const Facet f) {
        typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
        typedef boost::graph_traits<Mesh>::vertices_size_type size_type;
        mesh.add_face(vertex_descriptor(static_cast<size_type>(f[0])),
            vertex_descriptor(static_cast<size_type>(f[1])),
            vertex_descriptor(static_cast<size_type>(f[2])));
        return *this;
    }
    
    Construct& operator*() { return *this; }
    Construct& operator++() { return *this; }
    Construct& operator++(int) { return *this; }
};

int main() {
    
    //load blank
    Mesh blank, sv,res;
    std::ifstream fin("blank.off");
    fin>>blank;
    fin.close();

    CGAL::draw(blank);

    //load sv
    string filename = "hepoints49.txt" ;
    std::cout << "load sv..."<< std::endl;
    fin.open(filename);
    std::vector<Point_3> points;
    std::vector<Facet> facets;
    std::copy(std::istream_iterator<Point_3>(fin),
        std::istream_iterator<Point_3>(),
        std::back_inserter(points));//load points
    fin.close();
    Construct construct(sv, points.begin(), points.end());
    CGAL::advancing_front_surface_reconstruction(points.begin(), points.end(), construct);//convert sv to surface_mesh
    CGAL::draw(sv);

    std::cout << "start difference..." << std::endl;
    bool valid_difference = PMP::corefine_and_compute_difference(blank,sv,res);

    if (valid_difference) {
        std::cout << "difference was successfully computed. " << std::endl;
        CGAL::draw(res);
    }
    else {
        std::cout << "difference could not be completed. Skip. " << endl << endl;
    }
    
    //CGAL::draw(res);
    return 0;
}

运行时环境

CGAL 版本:5.3

IDE:VS2017

解决方案配置:调试 x64

我尝试在Release模式下运行这个程序,当然没有抛出异常。但我得到的结果却与我想要的相反。

文件

出现在代码中的文件如下:

https://github.com/wenzaifou/for-stack-overflow-question3.git

由于文件比较大,所以提供了Github链接。

【问题讨论】:

    标签: cgal


    【解决方案1】:

    从前进的前端输出构造网格的方式不会过滤掉孤立的顶点,这会导致引发异常。添加对CGAL::Polygon_mesh_processing::remove_isolated_vertices(sv)的调用将解决问题。

    然后您可能会遇到网格不向外的问题(这意味着然后代表空间的无限部分)。添加以下调用将解决问题:

    if (!CGAL::Polygon_mesh_processing::is_outward_oriented(blank))
      CGAL::Polygon_mesh_processing::reverse_face_orientations(blank);
    if (!CGAL::Polygon_mesh_processing::is_outward_oriented(sv))
      CGAL::Polygon_mesh_processing::reverse_face_orientations(sv);
    

    文档参考 herethere

    【讨论】:

    • 感谢您的回复。使用您的建议,大多数情况都可以正确计算,但仍有一些情况会出错。例如,如果我使用 blank2.off 而不是 blank.offhepoints82。 txt 代替 hepoints49.txt,在终端显示以下内容:CGAL error: assertion violation! Expression : res!=boost::none File : D:\dev\vcpkg\installed\x64-windows\include\CGAL/Kernel/function_objects.h Line : 1719 New files here
    • blank2.off 有自相交。您必须在进行布尔运算之前对其进行修复。你可以试试CGAL::Polygon_processing_processing::experimental::remove_degenerate_faces(),然后是CGAL::Polygon_mesh_processing::experimentatl::remove_self_intersections()。请注意,基于 corefinement 的函数有一个命名参数,以便在检测到某些可能对操作有问题的自相交时引发异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多