把pcl的点云转成CGAL支持的点云格式,重建后再转回PCL的三角面片格式,info就是pts里面pair的第二个参数,有需要也可以换成其他的,我这里是填入的索引,便于转成PCL支持的格式
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <fstream>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include<pcl/io/ply_io.h>
using K = CGAL::Exact_predicates_inexact_constructions_kernel;
using Vb = CGAL::Triangulation_vertex_base_with_info_2<std::size_t, K>;
using Tds = CGAL::Triangulation_data_structure_2<Vb>;
using Delaunay = CGAL::Delaunay_triangulation_2<K, Tds>;
using cgalPoint = Delaunay::Point;
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> CloudT;
typedef CloudT::Ptr CP;
pcl::PolygonMesh delaunay2D(CP cloud)
{
std::vector< std::pair<cgalPoint, std::size_t > > pts;
pts.reserve(cloud->size());
for (int i = 0; i < cloud->size(); i++)
{
PointT p = cloud->points[i];
pts.emplace_back(cgalPoint(p.x, p.y), i);
}
Delaunay triangulation;
triangulation.insert(pts.begin(), pts.end());
pcl::PolygonMesh triangles;
for (auto f = triangulation.faces_begin(); f != triangulation.faces_end(); ++f) {
pcl::Vertices face_vertices;
face_vertices.vertices.push_back(f->vertex(0)->info());
face_vertices.vertices.push_back(f->vertex(1)->info());
face_vertices.vertices.push_back(f->vertex(2)->info());
triangles.polygons.push_back(face_vertices);
}
pcl::toPCLPointCloud2(*cloud, triangles.cloud);
return triangles;
}