CGAL连接两个几何体

11

我正在尝试连接不相连的网格部件。从我找到的这个例子 ( blobby_3cc.off ) 开始。

使用 keep_large_connected_componentskeep_largest_connected_components,我删除了所有较小的组件,只保留了下面这3个。

我在文档中找不到将它们连接起来并填充缺失部分的方法。一个解决方案是创建1个三角形,然后填补孔洞(因为那样就是1个对象,有巨大的空隙)。但我找不到将它们连接起来的方法。

有人有解决方案吗?

我正在使用C++的CGAL。

enter image description here

2个回答

3
当我开始使用CGAL时,我几乎立即遇到了这个问题。仔细阅读多边形网格文档后,我找到了解决方案。基本上,通过修改版的Corefinement,您可以平滑地将两个不同的几何体网格拼合在一起,无论它们的多边形数量或形状如何(但是,多边形之间的差异越大,就越不有效)。
您需要做的第一件事是确保几何体不自相交。其次,确保CGAL :: Polygon_mesh_processing :: clip()在这两个几何体上处于活动状态(我建议使用close_volumes = false)。然后,计算这两个新网格的并集:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <fstream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Surface_mesh<K::Point_3>             Mesh;
namespace PMP = CGAL::Polygon_mesh_processing;
int main(int argc, char* argv[])
{
  const char* filename1 = (argc > 1) ? argv[1] : "data/blobby.off";
  const char* filename2 = (argc > 2) ? argv[2] : "data/eight.off";
  std::ifstream input(filename1);
  Mesh mesh1, mesh2;
  if (!input || !(input >> mesh1))
  {
    std::cerr << "First mesh is not a valid off file." << std::endl;
    return 1;
  }
  input.close();
  input.open(filename2);
  if (!input || !(input >> mesh2))
  {
    std::cerr << "Second mesh is not a valid off file." << std::endl;
    return 1;
  }
  Mesh out;
  bool valid_union = PMP::corefine_and_compute_union(mesh1,mesh2, out);
  if (valid_union)
  {
    std::cout << "Union was successfully computed\n";
    std::ofstream output("union.off");
    output << out;
    return 0;
  }
  std::cout << "Union could not be computed\n";
  return 1;
}

与使用来自具有精确构造的内核的点的网格不同,精确点是网格顶点的属性,我们可以在稍后的操作中重用。通过这个属性,我们可以操作具有浮点坐标的点的网格,但又能从精确构造提供的鲁棒性中受益。

#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 <fstream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Exact_predicates_exact_constructions_kernel EK;
typedef CGAL::Surface_mesh<K::Point_3> Mesh;
typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
typedef Mesh::Property_map<vertex_descriptor,EK::Point_3> Exact_point_map;
typedef Mesh::Property_map<vertex_descriptor,bool> Exact_point_computed;
namespace PMP = CGAL::Polygon_mesh_processing;
namespace params = PMP::parameters;
struct Coref_point_map
{
  // typedef for the property map
  typedef boost::property_traits<Exact_point_map>::value_type value_type;
  typedef boost::property_traits<Exact_point_map>::reference reference;
  typedef boost::property_traits<Exact_point_map>::category category;
  typedef boost::property_traits<Exact_point_map>::key_type key_type;
  // exterior references
  Exact_point_computed* exact_point_computed_ptr;
  Exact_point_map* exact_point_ptr;
  Mesh* mesh_ptr;
  Exact_point_computed& exact_point_computed() const
  {
    CGAL_assertion(exact_point_computed_ptr!=NULL);
    return *exact_point_computed_ptr;
  }
  Exact_point_map& exact_point() const
  {
    CGAL_assertion(exact_point_ptr!=NULL);
    return *exact_point_ptr;
  }
  Mesh& mesh() const
  {
    CGAL_assertion(mesh_ptr!=NULL);
    return *mesh_ptr;
  }
  // Converters
  CGAL::Cartesian_converter<K, EK> to_exact;
  CGAL::Cartesian_converter<EK, K> to_input;
  Coref_point_map()
    : exact_point_computed_ptr(NULL)
    , exact_point_ptr(NULL)
    , mesh_ptr(NULL)
  {}
  Coref_point_map(Exact_point_map& ep,
                  Exact_point_computed& epc,
                  Mesh& m)
    : exact_point_computed_ptr(&epc)
    , exact_point_ptr(&ep)
    , mesh_ptr(&m)
  {}
  friend
  reference get(const Coref_point_map& map, key_type k)
  {
    // create exact point if it does not exist
    if (!map.exact_point_computed()[k]){
      map.exact_point()[k]=map.to_exact(map.mesh().point(k));
      map.exact_point_computed()[k]=true;
    }
    return map.exact_point()[k];
  }
  friend
  void put(const Coref_point_map& map, key_type k, const EK::Point_3& p)
  {
    map.exact_point_computed()[k]=true;
    map.exact_point()[k]=p;
    // create the input point from the exact one
    map.mesh().point(k)=map.to_input(p);
  }
};
int main(int argc, char* argv[])
{
  const char* filename1 = (argc > 1) ? argv[1] : "data/blobby.off";
  const char* filename2 = (argc > 2) ? argv[2] : "data/eight.off";
  std::ifstream input(filename1);
  Mesh mesh1, mesh2;
  if (!input || !(input >> mesh1))
  {
    std::cerr << "First mesh is not a valid off file." << std::endl;
    return 1;
  }
  input.close();
  input.open(filename2);
  if (!input || !(input >> mesh2))
  {
    std::cerr << "Second mesh is not a valid off file." << std::endl;
    return 1;
  }
  Exact_point_map mesh1_exact_points =
    mesh1.add_property_map<vertex_descriptor,EK::Point_3>("e:exact_point").first;
  Exact_point_computed mesh1_exact_points_computed =
    mesh1.add_property_map<vertex_descriptor,bool>("e:exact_points_computed").first;
  Exact_point_map mesh2_exact_points =
    mesh2.add_property_map<vertex_descriptor,EK::Point_3>("e:exact_point").first;
  Exact_point_computed mesh2_exact_points_computed =
    mesh2.add_property_map<vertex_descriptor,bool>("e:exact_points_computed").first;
  Coref_point_map mesh1_pm(mesh1_exact_points, mesh1_exact_points_computed, mesh1);
  Coref_point_map mesh2_pm(mesh2_exact_points, mesh2_exact_points_computed, mesh2);
  if ( PMP::corefine_and_compute_intersection(mesh1,
                                              mesh2,
                                              mesh1,
                                              params::vertex_point_map(mesh1_pm),
                                              params::vertex_point_map(mesh2_pm),
                                              params::vertex_point_map(mesh1_pm) ) )
  {
    if ( PMP::corefine_and_compute_union(mesh1,
                                         mesh2,
                                         mesh2,
                                         params::vertex_point_map(mesh1_pm),
                                         params::vertex_point_map(mesh2_pm),
                                         params::vertex_point_map(mesh2_pm) ) )
    {
      std::cout << "Intersection and union were successfully computed\n";
      std::ofstream output("inter_union.off");
      output << mesh2;
      return 0;
    }
    std::cout << "Union could not be computed\n";
    return 1;
  }
  std::cout << "Intersection could not be computed\n";
  return 1;
}

并且为了填补任何空洞,请参见组合修复填充空洞 - Vendetta
谢谢您的回复。我尝试理解您的代码,但是有些函数我似乎不太明白,比如corefine_and_compute_unioncorefine_and_compute_intersection。文档中也没有给出清晰的解释。您能否稍微解释一下? - Niels
本质上,corefine_and_compute_union 计算网格中重叠的部分,并需要将其移除并替换为多边形填充。corefine_and_compute_intersection 与前者类似,但使用现有网格来填充切口,而不是生成平滑的网格填充。第一个函数通常需要精确的输入才能工作,但第二个函数允许将其作为参数传递。 - Vendetta
我确实需要在这个周末检查一下并查看结果,这样我才知道它是如何工作的。在赏金到期之前,我将接受此答案作为正确答案。 - Niels

0
网格最初是什么样子的?合并不同的组件而不是移除最小的部分是否可行?请参见CGAL combinatorical repairing以获取更多信息。
连接不同的组件是一个相当困难的问题。我认为常规的孔填充算法只适用于有界孔,即存在一条开放边缘绕着孔并最终回到起点。
我的建议是分析网格以找到需要连接的开放边缘列表,即红色、绿色、蓝色和紫色线条。找到一种将它们彼此配对的方法,例如reg-green和blue-purple。在这个例子中,只需使用边缘的平均值进行配对就足够了。
然后,您需要一些方法来三角化边缘之间的间隙。正如您提到的那样,创建一个(或两个)三角形来连接部分应该足够了,并使用类似CGAL::Polygon_mesh_processing::triangulate_refine_and_fair_hole的方法来填充其余部分。

为了实现这个目标,您可以尝试找到每个列表中彼此靠近的两个边缘。即点距离之和尽可能小。因此,从一个列表中选择一个边缘,并找到另一个列表中最接近的边缘。当您有两个边缘时,添加一对三角形并使用CGAL填充其余部分。不同的部分应具有相同的表面方向才能正常工作,但这可能是情况。

另一种方法是仅使用顶点从点云创建网格,但不能保证与当前网格匹配。最简单的解决方案可能是尽量避免问题,即确保网格源产生定义良好的连接网格。

example of edges to connect


谢谢您的回复,这确实是我一直在努力的方法,我几乎完成了编程,目前遇到的问题是面朝错误的方向,因此填充孔失败。 - Niels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接