簡體   English   中英

從CGAL的Delaunay約束三角剖分中檢索頂點

[英]Retrieve vertices from CGAL's Delaunay Constrained Triangulation

我有一個2D點列表,它們描述了一個簡單的多邊形。 由於CGAL,我計算了Delaunay約束。

    Polygon_2 polygon;
    //Create a polygon from a vector
    for(int j=0; j < (*itVectorPoint2D).size(); j++) {
        polygon.push_back(Point((*itVectorPoint2D)[j].x,(*itVectorPoint2D)[j].y));
    }

    //Insert the polygon into a constrained triangulation
    CDT cdt;
    insert_polygon(cdt,polygon);
    //Mark facets that are inside the domain bounded by the polygon
    mark_domains(cdt);

    for (CDT::Finite_faces_iterator fit=cdt.finite_faces_begin(); fit!=cdt.finite_faces_end();++fit)
    {
        if (fit->info().in_domain()) {
            std::cout << (*(fit->vertex(0))) << ";" << (*(fit->vertex(1))) << ";" << (*(fit->vertex(2))) << std::endl;
        }

    }

我的問題是我想從初始向量中檢索具有索引三元組的每個三角形,例如,對於一個由0 :(0,0) 1 :(1,0) 2 :(1,1)定義的正方形3:(0,1),我想要檢索(0,1,2)(0,2,3)。 我可以檢索當前頂點的坐標並將它們與向量中的每個點進行比較,但是我認為這很丑陋 (而且效率低下,可能是問題的根源?)。

有辦法嗎?

以下在CGAL 4.2上正常運行。 請注意,在CGAL 4.3中,將有一個專用功能以更集成的方式完成工作,就像在此處Delaunay_triangulation_2類所做的那樣。

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include <CGAL/Spatial_sort_traits_adapter_2.h>
#include <vector>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;

typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned, 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 CGAL::Spatial_sort_traits_adapter_2<K,Point*> Search_traits;

template <class InputIterator>
void insert_with_info(CDT& cdt, InputIterator first,InputIterator last)
{
  std::vector<std::ptrdiff_t> indices;
  std::vector<Point> points;
  std::ptrdiff_t index=0;

  for (InputIterator it=first;it!=last;++it){
    points.push_back( *it);
    indices.push_back(index++);
  }

  CGAL::spatial_sort(indices.begin(),indices.end(),Search_traits(&(points[0]),cdt.geom_traits()));

  CDT::Vertex_handle v_hint;
  CDT::Face_handle hint;
  for (typename std::vector<std::ptrdiff_t>::const_iterator
    it = indices.begin(), end = indices.end();
    it != end; ++it){
    v_hint = cdt.insert(points[*it], hint);
    if (v_hint!=CDT::Vertex_handle()){
      v_hint->info()=*it;
      hint=v_hint->face();
    }
  }
}

int main()
{
  std::vector< Point > points;
  points.push_back( Point(0,0) );
  points.push_back( Point(1,0) );
  points.push_back( Point(0,1) );
  points.push_back( Point(14,4) );
  points.push_back( Point(2,2) );
  points.push_back( Point(-4,0) );


  CDT cdt;
  insert_with_info(cdt, points.begin(), points.end());

  CGAL_assertion( cdt.number_of_vertices() == 6 );

  // check that the info was correctly set.
  CDT::Finite_vertices_iterator vit;
  for (vit = cdt.finite_vertices_begin(); vit != cdt.finite_vertices_end(); ++vit)
    if( points[ vit->info() ] != vit->point() ){
      std::cerr << "Error different info" << std::endl;
      exit(EXIT_FAILURE);
    }
  std::cout << "OK" << std::endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM