c++ - CGAL 将非流形 Nef_polyhedron_3 转换为三角形网格
问题描述
在我的应用程序中,我有一个三角形网格数据结构,我想用 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 转换为三角形网格吗?
解决方案
在输出中,您有一个非流形边缘(棱镜的一个边缘与立方体的一个面相切)。所以输出不是有效的表面网格,这就是为什么 PMP 方法不返回输出并且 nef 也不能转换为多边形网格的原因。
我想我可以写一个convert_nef_polyhedron_to_polygon_soup()
给你一组三角形,并且orient_polygon_soup()
能够复制所有非流形边,你会得到一个自相交的三角形网格。
推荐阅读
- python - 使用列表中的值更新字典中的值
- vb.net - 如何查询本地数据表并将信息返回到 VB.net 中的数据表
- java - 如何在java中对多维数组使用foreach或增强的for循环?
- c# - 如何保持 SqlConnection 在 using 块中打开,以便在块内进行异步查询?
- c++ - 尝试为每个字符串初始值设定项输入数据
- react-native - 反应导航问题
- azure - 如何在 Azure 云服务部署运行时更新其 NetworkConfiguration
- vue.js - Vue 路由器 beforeEach
- node.js - Mocha 测试用例在 Promise 获取数据之前执行
- angular - 如何从 s3 存储桶 url Angular 下载文件