【问题标题】:CGAL inheritanceCGAL继承
【发布时间】:2009-05-12 23:57:26
【问题描述】:

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

有什么想法吗?

【问题讨论】:

    标签: c++ cgal


    【解决方案1】:

    我检查了您的程序,您需要重新格式化它,使其适合通用编程模型。让我回忆一下您的代码的作用(github 提供的代码):

    1. 读取命令行
    2. 根据选项,在堆上实例化三角剖分或 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);
    

    【讨论】:

    • 我有一段时间没有连接到 StackOverflow。谢谢,成功了。
    【解决方案2】:

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

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

    【讨论】:

    • 这就是我的意思,Constrained_* 类有一些受保护的虚拟方法(如 virtual_insert 方法),但由于它们受到保护,我不能使用它们。
    【解决方案3】:

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

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

    Triangle *t = new Delaunay;
    

    Triangulation *t = new Delaunay;
    

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2012-10-30
    • 2017-12-27
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多