如何使用 CSGTree 进行多个布尔运算

How to use CSGTree for multiple boolean operations

我正在尝试利用 libigl 的 Csg Tree 一次执行多个布尔运算。在链接教程中,只有这一行:

// Compute result of (A ∩ B) \ ((C ∪ D) ∪ E)
igl::copyleft::cgal::CSGTree<MatrixXi> CSGTree =
  {{{VA,FA},{VB,FB},"i"},{{{VC,FC},{VD,FD},"u"},{VE,FE},"u"},"m"};

我不知道如何使用 CSGTree class API。任何人都可以通过样板示例提供理想的帮助吗?

根据 libigl 教程:

Libigl uses exact arithmetic internally to construct the intermediary boolean results robustly. “Rounding” this result to floating point (even double precision) would cause problems if re-injected into a further boolean operation. To facilitate CSG tree operations and encourage callers not to call igl::copyleft::cgal::mesh_boolean multiple times explicitly, libigl implements a class igl::copyleft::cgal::CSGTree.

简而言之 - 您可以为所有带坐标的中间计算构造 CSG meshes using the mesh_boolean function, but in this case you have to explicitly take care of robustness of intermediate calculations. The CSGTree class does that for you automatically because it uses the CGAL 精确算法。 CSGTree class 的另一个好处 - 只需一行代码即可构建多级 CSG 树。下面的示例显示了如何从两个网格构建最简单的 CSG 树并将其可视化:

#include <Eigen/Dense>

#include <igl/copyleft/cgal/CSGTree.h>
#include <igl/opengl/glfw/Viewer.h>
#include <igl/read_triangle_mesh.h>

int main()
{
  // ------ load mesh #1
  Eigen::MatrixXd V1;
  Eigen::MatrixXi F1;
  igl::read_triangle_mesh("data/sphere.obj", V1, F1);
  // ------ load mesh #2
  Eigen::MatrixXd V2;
  Eigen::MatrixXi F2;
  igl::read_triangle_mesh("data/xcylinder.obj", V2, F2);
  // ------ combine meshes #1 and #2
  const igl::copyleft::cgal::CSGTree t{{V1, F1}, {V2, F2}, "union"};
  // ------ plot the combined mesh
  const auto V = t.cast_V<Eigen::MatrixXd>();
  const auto F = t.F();
  igl::opengl::glfw::Viewer viewer;
  viewer.data().set_mesh(V, F);
  viewer.launch();
}

这样的单行构造是可能的,因为 CSGTree 具有以下构造函数(以及其他构造函数):

CSGTree(const CSGTree & A, const CSGTree & B, const MeshBooleanType & type)

正如您在上面看到的那样 - 如果需要,cast_V 模板函数可以将生成的坐标数组转换为双精度数组 - 例如,用于可视化。此可视化结果如下: