原理
xx
代码
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/common/common.h>
#include <pcl/visualization/pcl_visualizer.h>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cube(new pcl::PointCloud<pcl::PointXYZ>);
// 读取点云文件
if (pcl::io::loadPCDFile<pcl::PointXYZ>("E:\\File\\gradPaper\\data\\station\\4Clip.pcd", *cloud) == -1)
{
PCL_ERROR("Couldn't read file test_pcd.pcd \n");
return (-1);
}
// 获取最小包围盒
Eigen::Vector4f min_pt, max_pt;
pcl::getMinMax3D(*cloud, min_pt, max_pt);
// 生成均匀分布的点云
float x_range = max_pt[0] - min_pt[0];
float y_range = max_pt[1] - min_pt[1];
float z_range = max_pt[2] - min_pt[2];
for (size_t i = 0; i < 10000; ++i)
{
pcl::PointXYZ point;
int face = rand() % 6;
switch (face)
{
case 0: // x = x_min
point.x = min_pt[0];
point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
break;
case 1: // x = x_max
point.x = max_pt[0];
point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
break;
case 2: // y = y_min
point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
point.y = min_pt[1];
point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
break;
case 3: // y = y_max
point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
point.y = max_pt[1];
point.z = ((float)rand() / (RAND_MAX)) * z_range + min_pt[2];
break;
case 4: // z = z_min
point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
point.z = min_pt[2];
break;
case 5: // z = z_max
point.x = ((float)rand() / (RAND_MAX)) * x_range + min_pt[0];
point.y = ((float)rand() / (RAND_MAX)) * y_range + min_pt[1];
point.z = max_pt[2];
break;
}
cloud_cube->points.push_back(point);
}
// 设置点云的宽度和高度
cloud_cube->width = cloud_cube->points.size();
cloud_cube->height = 1;
// 保存点云
pcl::io::savePCDFileASCII("E:\\File\\gradPaper\\data\\station\\4ClipCube.pcd", *cloud_cube);
// 可视化
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(0, 0, 0);
viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud");
viewer->addCube(min_pt[0], max_pt[0], min_pt[1], max_pt[1], min_pt[2], max_pt[2], 1.0, 1.0, 0.0, "cube");
viewer->spin();
return (0);
}