CGAL将非流形Nef_polyhedron_3转换为三角形网格

问题描述 投票:1回答:1

在我的应用程序中,我有一个三角形网格数据结构,我想用CGAL做布尔操作,并将结果转换回三角形网格。它工作正常,但当结果是非流形时,它只适用于Nef_polyhedron_3。问题是我无法将其转换回三角形网格。

在下面的代码中,我有一个立方体和一个棱镜,其中一个棱镜的边缘位于立方体的面上,因此交叉点将是非流形的。我可以使用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 =
"OFF\n\
8 12 0\n\
-0.5 -0.5 -0.5\n\
0.5 -0.5 -0.5\n\
0.5 -0.5 0.5\n\
-0.5 -0.5 0.5\n\
-0.5 0.5 -0.5\n\
0.5 0.5 -0.5\n\
0.5 0.5 0.5\n\
-0.5 0.5 0.5\n\
3 0 1 2\n\
3 0 2 3\n\
3 1 5 6\n\
3 1 6 2\n\
3 5 4 7\n\
3 5 7 6\n\
3 4 0 3\n\
3 4 3 7\n\
3 0 4 5\n\
3 0 5 1\n\
3 3 2 6\n\
3 3 6 7\n";
    std::stringstream ss;
    ss << input;
    ss >> result;
}

template <typename T>
void fill_prism (T& result)
{
    std::string input =
"OFF\n\
6 8 0\n\
0.5 0 -0.5\n\
-0.25 0.433013 -0.5\n\
-0.25 -0.433013 -0.5\n\
0.5 0 0.5\n\
-0.25 0.433013 0.5\n\
-0.25 -0.433013 0.5\n\
3 2 1 0\n\
3 3 4 5\n\
3 0 1 4\n\
3 0 4 3\n\
3 1 2 5\n\
3 1 5 4\n\
3 2 0 3\n\
3 2 3 5\n";
    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转换为三角形网格吗?

c++ cgal boolean-operations
1个回答
1
投票

在输出中,您有一个非流形边缘(棱镜的一个边缘与立方体的一个面相切)。因此输出不是有效的表面网格,这就是PMP方法不返回输出并且nef也不能转换为多边形网格的原因。

我想我可以写一个convert_nef_polyhedron_to_polygon_soup(),它会给你一组三角形,orient_polygon_soup()能够复制所有非流形边缘,你会得到一个自相交的三角形网格。

© www.soinside.com 2019 - 2024. All rights reserved.