CGAL 2D Delaunay三角化remove方法无法删除顶点问题求助
问题
我需要从2D Delaunay三角化中删除部分点,但使用remove方法未能生效,执行remove操作后,顶点数量未发生变化。相关代码如下:
//---TYPEDEFS typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef CGAL::Triangulation_vertex_base_2<K> Vb; typedef CGAL::Constrained_triangulation_face_base_2<K> Fb; typedef CGAL::Triangulation_data_structure_2<Vb,Fb> TDS; typedef CGAL::Exact_predicates_tag Itag; typedef CGAL::Constrained_Delaunay_triangulation_2<K,TDS,Itag> CDT; typedef CDT::Point Point; typedef CDT::Vertex_handle Vertex_handle; typedef CDT::Vertex_circulator Vertex_circulator; typedef CDT::Face_handle Face_handle; ... int RemovePontosDesabilitados(CDT& cdt, tysetPonto3D& SetPonDesabilitados) { for (auto& RPonto : SetPonDesabilitados) { auto Face(cdt.locate(Point(RPonto.x,RPonto.y))); if (Face != NULL) { auto Tam = cdt.number_of_vertices(); auto Pon = cdt.insert(Point(RPonto.x, RPonto.y)); //--- return the existing point cdt.remove(Pon); Tam = cdt.number_of_vertices(); } } return 1; }
请问如何正确从2D Delaunay三角化中删除点?
解决方案
你的代码存在两个核心问题,导致删除操作无效:
错误使用
insert获取顶点句柄
你通过insert来获取已存在点的句柄,但Constrained_Delaunay_triangulation_2的insert方法在插入重复点时,返回的是新创建的临时顶点句柄,而非原三角化中已有的顶点。后续调用remove删除这个临时句柄,自然不会影响原三角化的顶点数量。错误依赖
locate的返回值判断点是否存在locate方法返回的是包含该点的面(或无限面),但即使点不在三角化顶点中,只要点在面的区域内,locate也会返回对应面,无法直接用来判断顶点是否存在。
正确的删除步骤如下:
- 先判断点是否存在于三角化中
使用CDT::find方法替代insert来查找顶点。find会直接返回对应点的Vertex_handle,如果点不存在则返回空句柄。 - 安全删除顶点
只有当find返回有效句柄时,才调用remove方法。
修改后的代码示例:
int RemovePontosDesabilitados(CDT& cdt, tysetPonto3D& SetPonDesabilitados) { int removed_count = 0; for (auto& RPonto : SetPonDesabilitados) { Point target_point(RPonto.x, RPonto.y); Vertex_handle vh = cdt.find(target_point); if (vh != nullptr) { cdt.remove(vh); removed_count++; } } return removed_count; // 返回实际删除的顶点数,方便验证 }
额外注意事项:
- 确保
Point类型的坐标精度匹配,避免因浮点精度问题导致find无法找到目标顶点。 - 如果三角化中存在约束边,删除顶点时需要确保不会破坏约束结构(CGAL会自动处理合法的约束删除,但需保证删除操作符合约束三角化的规则)。
内容的提问来源于stack exchange,提问作者Rabelo
相关产品推荐
相关产品推荐

