如何在 CGAL 三角剖分的上下文中使用三角剖分的继承类?

基本上我有以下代码:

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;

typedef CGAL::Triangulation_vertex_base_with_info_2<int,K> Vb;
typedef CGAL::Triangulation_face_base_with_info_2<int,K>   Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb>        Tds;
typedef CGAL::Delaunay_triangulation_2<K,Tds>              Delaunay;
typedef CGAL::Triangulation_2<K,Tds>                       Triangulation;

typedef Triangulation::Point Point;

...

Triangulation *t = new Delaunay;

...

// x and y are properly defined and instantiated
t->insert(Point(x,y));

嗯,当然,Delaunay_triangulation_2 继承自 Triangulation_2

因此,当我执行此代码时,链接是针对 Triangulation_2 类完成的,换句话说,它不执行 delaunay 三角剖分,而是执行正常的三角剖分(执行父类方法而不是子方法)。

我认为这是因为 Triangulation_2 的插入方法没有声明为虚拟的,所以重新定义不起作用。

你知道解决这个问题的方法吗?也许使用 Constrained_triangulation_2 和 Constrained_delaunay_triangulation_2 ?(这些类定义了一些虚拟方法,但我已经阅读了源代码,并且我认为在不添加显式约束的情况下不能使用它们)

有任何想法吗?

有帮助吗?

解决方案

我检查了您的程序,您需要稍微重新格式化它,以便它适合通用编程模型。让我回忆一下您的代码的作用(可在 github):

  1. 读取命令行
  2. 根据选项,在堆上实例化 Triangulation 或 Delaunay_triangulation
  3. 使用此对象进行某些处理,假设这些方法是虚拟的(但它们不是)

解决您问题的方法是将步骤 3 放在单独的方法中,并将三角测量类型作为模板参数。像(我使用你的类型和名称):

template < class Triangulation >
void compute_mesh(int n_vertices, int max_x, int max_y)
{
    Triangulation t;
    // DO WHATEVER YOU WANT WITH t
}

然后,在您的 main 函数中,您将通过以下方式触发 Delaunay 或非 Delaunay 三角剖分的使用:

if (triang_type == 'D') 
    compute_mesh<Delaunay>(n_vertices, max_x, max_y);
else 
    compute_mesh<Triangulation>(n_vertices, max_x, max_y);

其他提示

您确定这些功能是虚拟的?没有它们被定义的虚拟编译器将不调用派生类的功能。

这在CGAL头粗略看看它似乎没有这些类在所有具有任何虚函数。

CGAL使用通用编程,而不是虚函数。它类似于STL,只是域是比较困难的一点,你需要更多地依赖于算法不是一个通常与STL一样。

我真的不能告诉,什么是回答您的问题,因为您提供的代码只是一个小片段,但尝试更换

Triangle *t = new Delaunay;

Triangulation *t = new Delaunay;

第一。如果没有帮助,请从你的类型定义添加更多的细节。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top