如何在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的insert方法没有声明为虚拟的,所以重定义将不起作用。
你知道解决这个问题的方法吗?也许使用Constrained_triangulation_2和Constrained_delaunay_triangulation_2?(这些类定义了一些虚方法,但我读过源代码,我认为如果不添加显式约束,就不能使用它们)
有什么想法吗?
发布于 2009-09-09 07:59:07
我检查了你的程序,你需要重新格式化它,以便它适合泛型编程模型。让我回顾一下您的代码做了什么(在github上提供的代码):
您的问题的一个解决方案是将步骤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-05-13 07:50:57
您确定这些函数是虚拟的吗?如果没有将它们定义为虚的,编译器将不会调用派生类函数。
粗略地看一下CGAL头文件,这些类似乎根本没有任何虚函数。
发布于 2009-05-13 18:27:38
CGAL使用泛型编程,而不是虚函数。它类似于STL,只是领域有点困难,而且你需要比通常使用STL更多地依赖算法。
我真的不能说,您的问题的答案是什么,因为您只提供了一小段代码,但请尝试替换
Triangle *t = new Delaunay;使用
Triangulation *t = new Delaunay;第一。如果没有帮助,请从您的类型定义添加更多详细信息。
https://stackoverflow.com/questions/855470
复制相似问题