You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在CGAL约束三角剖分面中存储额外信息并通过有限面迭代器访问

在CGAL约束Delaunay三角剖分中存储并访问面的额外信息

核心解决思路

CGAL的Finite_faces_iterator返回的是Face_handle类型,它指向的是你在TDS中指定的自定义面基类实例。由于你已经在Triangulation_data_structure_2中明确指定了自定义面类,直接用静态类型转换就能安全访问额外成员,无需动态转换。

修正后的完整代码示例

#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Constrained_triangulation_face_base_2.h>
#include <CGAL/Triangulation_vertex_base_2.h>
#include <CGAL/Triangulation_data_structure_2.h>
#include <vector>

// 假设EPIC是你使用的内核类型
typedef CGAL::Exact_predicates_inexact_constructions_kernel EPIC;
typedef EPIC::Point_2 Point2d;

class Constrained_triangulation_face_base_with_info_2 
    : public CGAL::Constrained_triangulation_face_base_2<EPIC> {
    
public:
    std::vector<Point2d> containedPoints;

    // 提供CGAL要求的构造函数(可选但推荐,避免潜在问题)
    Constrained_triangulation_face_base_with_info_2() = default;
    
    template <typename TDS2>
    Constrained_triangulation_face_base_with_info_2(
        const typename TDS2::Vertex_handle& v0,
        const typename TDS2::Vertex_handle& v1,
        const typename TDS2::Vertex_handle& v2
    ) : CGAL::Constrained_triangulation_face_base_2<EPIC>(v0, v1, v2) {}

    virtual ~Constrained_triangulation_face_base_with_info_2() = default;
};

// 定义三角剖分类型
typedef CGAL::Triangulation_vertex_base_2<EPIC> VertexBase;
typedef CGAL::Triangulation_data_structure_2<VertexBase, Constrained_triangulation_face_base_with_info_2> TDS;
typedef CGAL::Constrained_Delaunay_triangulation_2<EPIC, TDS> ConstrainedDelaunayTriangulation;
typedef ConstrainedDelaunayTriangulation::Finite_faces_iterator ConstrainedDelaunayFacesIterator;

int main() {
    ConstrainedDelaunayTriangulation constrainedDelaunayTriangulation;

    // 此处添加你的三角剖分构建代码(插入点、约束边等)

    // 遍历有限面并访问额外信息
    for (ConstrainedDelaunayFacesIterator fit = constrainedDelaunayTriangulation.finite_faces_begin();
         fit != constrainedDelaunayTriangulation.finite_faces_end();
         ++fit) {
        
        // 方式1:直接通过迭代器解引用后静态转换
        auto& custom_face = static_cast<Constrained_triangulation_face_base_with_info_2&>(*fit);
        // 访问containedPoints
        custom_face.containedPoints.emplace_back(Point2d(0,0)); // 示例操作

        // 方式2:通过Face_handle访问(等价于方式1)
        Constrained_triangulation_face_base_with_info_2* face_ptr = static_cast<Constrained_triangulation_face_base_with_info_2*>(&*fit);
        face_ptr->containedPoints.clear(); // 示例操作
    }

    return 0;
}

关键说明

  1. 构造函数补充:虽然默认构造函数可能够用,但提供CGAL面基类要求的模板构造函数能避免在复杂场景下(如三角剖分重构)出现未定义行为。
  2. 类型转换的安全性:因为TDS已经明确使用了你的自定义面类,所以Finite_faces_iterator指向的对象必然是Constrained_triangulation_face_base_with_info_2类型,静态转换是完全安全的,比动态转换更高效。
  3. 成员访问方式:可以直接通过引用或指针访问自定义成员,和普通C++类的访问方式一致。

内容的提问来源于stack exchange,提问作者user13583700

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 15:58:20