2

如何在 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?(这些类定义了一些虚拟方法,但我已经阅读了源代码,我认为如果不添加显式约束就不能使用它们)

有任何想法吗?

4

3 回答 3

3

我检查了您的程序,您需要重新格式化它,使其适合通用编程模型。让我回忆一下你的代码做了什么(在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);
于 2009-09-09T07:59:06.677 回答
0

您确定这些功能是虚拟的吗?如果没有将它们定义为虚拟,编译器将不会调用派生类函数。

粗略地看一下 CGAL 标头,这些类似乎根本没有任何虚函数。

于 2009-05-13T07:50:57.210 回答
0

CGAL 使用泛型编程,而不是虚函数。它类似于 STL,只是领域有点困难,而且你需要比通常使用 STL 更多地依赖算法。

我真的不知道,你的问题的答案是什么,因为你只提供了一小段代码,但尝试替换

Triangle *t = new Delaunay;

Triangulation *t = new Delaunay;

第一的。如果它没有帮助,请从您的类型定义中添加更多详细信息。

于 2009-05-13T18:27:37.763 回答