一、概述
CGAL中CGAL::bbox_3
可以用来计算三维点云坐标的最值。
二、代码实现
#include <pcl/io/pcd_io.h> // PCL读取PCD
#include <pcl/point_types.h> // PCL点类型
#include <CGAL/Point_set_3.h>
#include <CGAL/Simple_cartesian.h>
//#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
//using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Kernel = CGAL::Simple_cartesian<double>;
using Point_2 = Kernel::Point_2;
using Point_3 = Kernel::Point_3;
using namespace std;
int main(int argc, char** argv)
{
//-------------------------加载PCD点云数据------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("E://data//bunny.pcd", *cloud) == -1)
{
PCL_ERROR("Could not read file\n");
return -1;
}
// -----------------------转为CGAL支持的格式------------------------------
CGAL::Point_set_3<Point_3> points;
for (size_t i = 0; i < cloud->size(); ++i)
{
double px = cloud->points[i].x;
double py = cloud->points[i].y;
double pz = cloud->points[i].z;
points.insert(Point_3(px, py,pz));
}
// --------------------------计算点云最值--------------------------------
CGAL::Bbox_3 bbox = CGAL::bbox_3(points.points().begin(), points.points().end());
cout << "Min x: " << bbox.xmin() << endl;
cout << "Min y: " << bbox.ymin() << endl;
cout << "Min z: " << bbox.zmin() << endl;
cout << "Max x: " << bbox.xmax() << endl;
cout << "Max y: " << bbox.ymax() << endl;
cout << "Max z: " << bbox.zmax() << endl;
return 0;
}
三、结果展示
Min x: -0.0946899
Min y: 0.0329874
Min z: -0.0618736
Max x: 0.0610091
Max y: 0.187321
Max z: 0.0587997