首页 > 解决方案 > 网格化算法搞砸了 Triangulation_vertex_base_with_id_2

问题描述

我正在尝试应用于Delaunay_mesher_2::refine_mesh一组编号的顶点(即顶点类是Triangulation_vertex_base_with_id_2)。我希望在该过程中创建的任何顶点都应该具有id()==0. 然而事实证明并非如此。相反,ID 具有各种正/负值。

我尝试使用Triangulation_vertex_base_with_info_2,但结果是一样的。

我的代码或多或少是这样工作的:

using K = CGAL::Exact_predicates_inexact_constructions_kernel;
using Vb = CGAL::Triangulation_vertex_base_with_id_2<K>;
using Fb = CGAL::Delaunay_mesh_face_base_2<K>;
using Tds = CGAL::Triangulation_data_structure_2<Vb, Fb>;
using CDT_Tag = CGAL::Exact_intersections_tag;
using CDT = CGAL::Constrained_Delaunay_triangulation_2<K, Tds, CDT_Tag>;
using Criteria = CGAL::Delaunay_mesh_size_criteria_2<CDT>;
CDT cdt;
CGAL::Delaunay_mesher_2<CDT, Criteria> mesher(cdt, Criteria(0.125, 0.5));
... // Add some points and constraints.
static const CDT::Point INFINITE_POINT = { 1e100, 1e100 };
std::vector<CDT::Point> seeds = { INFINITE_POINT };
mesher.set_seeds(seeds.begin(), seeds.end());
mesher.refine_mesh();

for (auto vertices_iter = cdt.finite_vertices_begin();
     vertices_iter != cdt.finite_vertices_end();
     ++vertices_iter) {
  // do something with vertices_iter->id().
}

标签: c++cgal

解决方案


的默认构造函数CGAL::Triangulation_vertex_base_with_id_2<K>不初始化id成员:

template < typename GT,
           typename Vb = Triangulation_vertex_base_2<GT> >
class Triangulation_vertex_base_with_id_2
  : public Vb
{
  int _id;

public:

  // [...]

  Triangulation_vertex_base_with_id_2()
    : Vb() {}

这意味着这_id默认初始化的

解释

默认初始化在三种情况下执行:[...]

  1. 当构造函数初始值设定项列表中未提及基类或非静态数据成员并且调用该构造函数时。

然后阅读下一段:

默认初始化的效果是:

  • 如果 T 是非 POD(C++11 前)类类型,则考虑构造函数并对其进行针对空参数列表的重载决议。调用选择的构造函数(默认构造函数之一)为新对象提供初始值;
  • 如果 T 是数组类型,则数组的每个元素都是默认初始化的;
  • 否则,什么都不做:具有自动存储持续时间的对象(及其子对象)被初始化为不确定的值。

这里的子对象_idif 是 type int,因此它不是类或数组。这就是它没有被初始化的原因。该值将是内存中存在的任何垃圾。


推荐阅读