CGAL将非manifold nef_polyhedron_3转换为三角网格

CGAL convert non-manifold Nef_polyhedron_3 to triangle mesh

本文关键字:转换 三角 网格 polyhedron 将非 manifold nef CGAL      更新时间:2023-10-16

在我的应用程序中,我有一个三角网格数据结构,我想用cgal进行布尔操作,并将结果转换为三角形网格。它可以正常工作,但是当结果是非manifold时,仅与nef_polyhedron_3一起使用。问题是我无法将其转换回三角形网格。

在以下代码中,我有一个立方体和一个棱镜,并且棱镜的边缘之一位于立方体的脸上,因此交叉路口将是非manifold。我可以使用nef_polyhedron_3进行操作,但是在操作后,我无法将其转换回三角形网格。

#include <CGAL/Exact_integer.h>
#include <CGAL/Homogeneous.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/IO/Nef_polyhedron_iostream_3.h>
#include <CGAL/boost/graph/convert_nef_polyhedron_to_polygon_mesh.h>
#include <iostream>
#include <sstream>
#include <fstream>
typedef CGAL::Exact_predicates_exact_constructions_kernel   Kernel;
typedef Kernel::Point_3                                     Point_3;
typedef CGAL::Surface_mesh<Point_3>                         Surface_mesh;
typedef CGAL::Polyhedron_3<Kernel>                          Polyhedron_3;
typedef CGAL::Nef_polyhedron_3<Kernel>                      Nef_polyhedron_3;
typedef Polyhedron_3::HalfedgeDS                            HalfedgeDS;
template <typename T>
void fill_cube (T& result)
{
    std::string input =
"OFFn
8 12 0n
-0.5 -0.5 -0.5n
0.5 -0.5 -0.5n
0.5 -0.5 0.5n
-0.5 -0.5 0.5n
-0.5 0.5 -0.5n
0.5 0.5 -0.5n
0.5 0.5 0.5n
-0.5 0.5 0.5n
3 0 1 2n
3 0 2 3n
3 1 5 6n
3 1 6 2n
3 5 4 7n
3 5 7 6n
3 4 0 3n
3 4 3 7n
3 0 4 5n
3 0 5 1n
3 3 2 6n
3 3 6 7n";
    std::stringstream ss;
    ss << input;
    ss >> result;
}
template <typename T>
void fill_prism (T& result)
{
    std::string input =
"OFFn
6 8 0n
0.5 0 -0.5n
-0.25 0.433013 -0.5n
-0.25 -0.433013 -0.5n
0.5 0 0.5n
-0.25 0.433013 0.5n
-0.25 -0.433013 0.5n
3 2 1 0n
3 3 4 5n
3 0 1 4n
3 0 4 3n
3 1 2 5n
3 1 5 4n
3 2 0 3n
3 2 3 5n";
    std::stringstream ss;
    ss << input;
    ss >> result;
}
int main()
{
    { // try with mesh processing
        Surface_mesh cube;
        Surface_mesh prism;
        fill_cube (cube);
        fill_prism (prism);
        Surface_mesh result;
        // will return false
        bool success = CGAL::Polygon_mesh_processing::corefine_and_compute_difference (cube, prism, result);
    }
    { // try with nef polyhedron
        Polyhedron_3 cube;
        Polyhedron_3 prism;
        fill_cube (cube);
        fill_prism (prism);
        Nef_polyhedron_3 nefCube (cube);
        Nef_polyhedron_3 nefPrism (prism);
        Nef_polyhedron_3 result = nefCube - nefPrism;
        Surface_mesh resultMesh;
        // throws exception because is_polygon_soup_a_polygon_mesh(polygons) is false
        CGAL::convert_nef_polyhedron_to_polygon_mesh (result, resultMesh, true);
    }
    system ("pause");
}

您是否知道如何将nef_polyhedron_3转换为三角网格?

在输出中,您具有非manifold边缘(Prism的一个边缘与立方体的脸相切)。因此,输出不是有效的表面网格,这就是为什么PMP方法未返回输出并且NEF也无法转换为多边形网格的原因。

我想我可以编写一个convert_nef_polyhedron_to_polygon_soup(),它会给您一组三角形,而orient_polygon_soup()可以复制所有非曼尼弗边缘,并且您将获得一个自相隔的三角形网格。