PCL(point cloud library)系列笔记:http://blog.csdn.net/chentravelling/article/category/2876349
//先上代码:
bool saveThePointCloud()
{
pcl::PointCloud<pcl::PointXYZ> Cloud; //定义点云对象,类型PointXYZ
// 创建点云
Cloud.width=30;
Cloud.height=1;
Cloud.is_dense=false;
Cloud.points.resize(Cloud.width*Cloud.height);
srand(unsigned(int(NULL)));
for(size_t i=0;i<Cloud.points.size();++i)
{//RAND_MAX = 32767
if(i<10)
{
Cloud.points[i].x = 1024*rand()/(RAND_MAX+1.0f);
Cloud.points[i].y = 0.0f;
Cloud.points[i].z = 0.0f;
}
else if(i<20)
{
Cloud.points[i].x = 0.0f;
Cloud.points[i].y = 1024*rand()/(RAND_MAX+1.0f);
Cloud.points[i].z = 0.0f;
}
else
{
Cloud.points[i].x = 0.0f;
Cloud.points[i].y = 0.0f;
Cloud.points[i].z = 1024*rand()/(RAND_MAX+1.0f);
}
}
// pcl::io::savePCDFile("pointCloudValueFile.pcd",Cloud);
pcl::io::savePCDFileASCII("pointCloudFile.pcd",Cloud);//这两种save的结果貌似是一样的。
return true;
}
然后在调用此函数即可。
完整程序:
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
//int user_data;
//Initialize the viewer including the backgroundcolor,coordinate axis,and others.
void viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
{
//set the backgroundcolor:R,G,B
viewer.setBackgroundColor (1.0, 1.0, 1.0);//背景为白色
/* pcl::PointXYZ o;
o.x = 0;
o.y = 0;
o.z = 0;
viewer.addSphere (o, 5, "sphere",0);*/
//viewer.addLine(o,"line",0);
/*std::cout << "i only run once" << std::endl;*/
}
//void viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
//{
// static unsigned count = 0;
// std::stringstream ss;
// ss << "Once per viewer loop: " << count++;
// viewer.removeShape ("text", 0);
// viewer.addText (ss.str(), 200, 300, "text", 0);
//
// //FIXME: possible race condition here:
// user_data++;
//}
//Accomplish loading a PCDFile,creating a viewer,and show the cloud at the viewer.
void showTheCloud()
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
//load
pcl::io::loadPCDFile ("my_point_cloud.pcd", *cloud);
pcl::visualization::CloudViewer viewer("Cloud Viewer");
//blocks until the cloud is actually rendered
viewer.showCloud(cloud);
//use the following functions to get access to the underlying more advanced/powerful
//PCLVisualizer
//This will only get called once
viewer.runOnVisualizationThreadOnce (viewerOneOff);
//This will get called once per visualization iteration
//viewer.runOnVisualizationThread (viewerPsycho);
while (!viewer.wasStopped ())
{
//you can also do cool processing here
//FIXME: Note that this is running in a separate thread from viewerPsycho
//and you should guard against race conditions yourself...
//user_data++;
}
}
//Create a point-cloud object,and generate a PCD File to save the point cloud object.
bool saveThePointCloud()
{
pcl::PointCloud<pcl::PointXYZ> Cloud; //定义点云对象
// 创建点云
Cloud.width=30;
Cloud.height=1;
Cloud.is_dense=false;
Cloud.points.resize(Cloud.width*Cloud.height);
srand(unsigned(int(NULL)));
for(size_t i=0;i<Cloud.points.size();++i)
{//RAND_MAX = 32767
if(i<10)
{
Cloud.points[i].x = 1024*rand()/(RAND_MAX+1.0f);
Cloud.points[i].y = 0.0f;
Cloud.points[i].z = 0.0f;
}
else if(i<20)
{
Cloud.points[i].x = 0.0f;
Cloud.points[i].y = 1024*rand()/(RAND_MAX+1.0f);
Cloud.points[i].z = 0.0f;
}
else
{
Cloud.points[i].x = 0.0f;
Cloud.points[i].y = 0.0f;
Cloud.points[i].z = 1024*rand()/(RAND_MAX+1.0f);
}
}
pcl::io::savePCDFile("pointCloudValueFile.pcd",Cloud);
pcl::io::savePCDFileASCII("pointCloudFile.pcd",Cloud);
return true;
}
int main ()
{
if(saveThePointCloud())
{
showTheCloud();
}
else
{
std::cout<<"Failed"<<endl;
}
return 0;
}
这样,在工程目录下会多一个pointCloudFile.pcd文件。用记事本打开就可以看到数据。【PCD格式说明请阅读本人学习笔记】
至于命令窗口显示的Failed to find match for failed ‘rgba‘,是因为PCD文件一般的格式中是:
FIELDS x y z rgb
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
而我们获得的pcd文件里并没有第四列数据。初步估计是因为生成点云时,我们并没有给每一个point设置rgb参数。