CGAL - 在 Delaunay 三角剖分后检索顶点索引

8
我正在计算数千个点的2D Delaunay三角剖分。每个点除了x和y坐标外,还有更多的数据与之关联。因此,我想知道是否可以检索每个点的索引,以便我可以访问另一个向量中的我的点结构体。
目前,当我从Face_handle访问顶点时,它返回一个点(即x、y坐标)。如何通过ID(索引)而不是其x、y坐标来返回每个顶点?谢谢。
#include <vector>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Delaunay_triangulation_2<Kernel> Delaunay;
typedef Kernel::Point_2 Point;

void example() {

  std::vector<Point> points;
  points.push_back(Point(1,1)); //index 0
  points.push_back(Point(1,2)); //index 1
  points.push_back(Point(1,3)); //index 2
  points.push_back(Point(2,1)); //index 3
  points.push_back(Point(2,2)); //index 4
  points.push_back(Point(2,3)); //index 5

  Delaunay triangulation;
  triangulation.insert(points.begin(),points.end());

  for(Delaunay::Finite_faces_iterator fit = triangulation.finite_faces_begin();
      fit != triangulation.finite_faces_end(); ++fit) {

    Delaunay::Face_handle face = fit;
    std::cout << "Triangle:\t" << triangulation.triangle(face) << std::endl;
    std::cout << "Vertex 0:\t" << triangulation.triangle(face)[0] << std::endl;
  }
}

输出(x,y坐标):

Triangle:   1 3 1 2 2 2
Vertex 0:   1 3
Triangle:   1 2 1 1 2 1
Vertex 0:   1 2
Triangle:   1 3 2 2 2 3
Vertex 0:   1 3
Triangle:   1 2 2 1 2 2
Vertex 0:   1 2

期望的输出(索引):
Triangle:   2   1   4
Vertex 0:   2
Triangle:   1   0   3
Vertex 0:   1 
Triangle:   2   4   5
Vertex 0:   2
Triangle:   1   3   4
Vertex 0:   1
1个回答

15

您可以将任何信息附加到三角剖分的顶点上。例如,要添加索引(unsigned int),可以执行以下操作:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include <vector>

typedef CGAL::Exact_predicates_inexact_constructions_kernel            Kernel;
typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned int, Kernel> Vb;
typedef CGAL::Triangulation_data_structure_2<Vb>                       Tds;
typedef CGAL::Delaunay_triangulation_2<Kernel, Tds>                    Delaunay;
typedef Kernel::Point_2                                                Point;

int main() {

  std::vector< std::pair<Point,unsigned> > points;
  points.push_back( std::make_pair( Point(1,1), 0 ) );
  points.push_back( std::make_pair( Point(1,2), 1 ) );
  points.push_back( std::make_pair( Point(1,3), 2 ) );
  points.push_back( std::make_pair( Point(2,1), 3 ) );
  points.push_back( std::make_pair( Point(2,2), 4 ) );
  points.push_back( std::make_pair( Point(2,3), 5 ) );

  Delaunay triangulation;
  triangulation.insert(points.begin(),points.end());

  for(Delaunay::Finite_faces_iterator fit = triangulation.finite_faces_begin();
      fit != triangulation.finite_faces_end(); ++fit) {

    Delaunay::Face_handle face = fit;
    std::cout << "Triangle:\t" << triangulation.triangle(face) << std::endl;
    std::cout << "Vertex 0:\t" << triangulation.triangle(face)[0] << std::endl;
    std::cout << "Vertex 0:\t" << face->vertex(0)->info() << std::endl;
  }
}

谢谢,这将解决我的问题。我不确定是否可能为每个顶点分配更多的信息?例如使用std::tuple? - socratic
1
我从未尝试过,但我几乎确定您可以分配任何类型(std::tuple 或您自己的类等)。只需将其用作 Vb 的模板参数,并根据需要调整其余代码即可。 - Sigroad
4
让我确认一下,您可以向顶点添加任何内容。我想指出的是,这些都在这里有记录:http://doc.cgal.org/latest/Triangulation_2/index.html#title40 - Andreas Fabri
非常感谢,先生们。 - socratic
我可以用相同的方式嵌入Surface_mesh的索引吗? - user3667089

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接