目录
一个代码完成功能,不需要下载其它的点云文件数据,
代码功能
1.生成一个在正方体内的随机点云;
2.将生成的点云保存为PCD文件;
3.读取PCD文件得到点云;
4.可视化点云;
代码
/*
by : 手口一斤
date : 2020-12-16
*/
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
int
genwrite()
{
pcl::PointCloud<pcl::PointXYZ> cloud;
// 创建点云
cloud.width = 10000;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize(cloud.width * cloud.height);
for (size_t i = 0; i < cloud.points.size(); ++i)
{
// 坐标范围在[0,1024),正方体内随机取点
cloud.points[i].x = 1024 * (rand() / (RAND_MAX + 1.0f));
cloud.points[i].y = 1024 * (rand() / (RAND_MAX + 1.0f));
cloud.points[i].z = 1024 * (rand() / (RAND_MAX + 1.0f));
}
pcl::io::savePCDFileASCII("selfgen.pcd", cloud);
return(0);
}
int
main(int argc, char** argv)
{
genwrite();
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("selfgen.pcd", *cloud) == -1)//*打开点云文件
{
PCL_ERROR("Couldn't read file test_pcd.pcd\n");
return(-1);
}
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");
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return(0);
}
运行结果
由于点云是在一个正方体内取得点,动这个视角看,近似一个正方形,用鼠标拖动,可看到一个正方体;
鼠标拖动后: