CGAL 二维点集的凸包提取

一、凸包

  用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点。百度百科——凸包

二、代码实现

#include <iostream>    // io
#include <pcl/io/pcd_io.h>          // PCL读取PCD
#include <pcl/point_types.h>        // PCL点类型
#include <CGAL/Point_set_3/IO/XYZ.h>// CGAL保存点为.xyz
#include <CGAL/convex_hull_2.h>     // 二维凸包
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;

int main()
{
    
    
	//-------------------------加载二维PCD点云数据------------------------------
	pcl::PointCloud<pcl::PointXY>::Ptr cloud(new pcl::PointCloud<pcl::PointXY>);

	if (pcl::io::loadPCDFile<pcl::PointXY>("cgal//cloud.pcd", *cloud) == -1)
	{
    
    
		PCL_ERROR("Could not read file\n");
		return -1;
	}
	// ------------------------转为CGAL支持的格式------------------------------
	std::vector<Kernel::Point_2> points;
	for (size_t i = 0; i < cloud->size(); ++i)
	{
    
    
		double px = cloud->points[i].x;
		double py = cloud->points[i].y;
		points.push_back(Kernel::Point_2(px, py));
	}
	CGAL::IO::write_XYZ("cgal//pro.xyz", points);
	// --------------------------提取二维凸包点--------------------------------
	std::vector<Kernel::Point_2> result;
	CGAL::convex_hull_2(points.begin(), points.end(), std::back_inserter(result));
	std::cout << result.size() << " points on the convex hull" << std::endl;

	CGAL::IO::write_XYZ("cgal//convex_hull_2d.xyz", result);

	return 0;
}


三、结果展示

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36686437/article/details/127144483