#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.voidviewerOneOff(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.voidshowTheCloud(){
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);//load
pcl::io::loadPCDFile("Scan00SL100000.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.boolsaveThePointCloud(){
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 = 32767if(i<10){
Cloud.points[i].x =1024*rand()/(RAND_MAX +1.0f);
Cloud.points[i].y =0.0f;
Cloud.points[i].z =0.0f;}elseif(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);returntrue;}intmain(){if(saveThePointCloud()){showTheCloud();}else{
std::cout <<"Failed"<< endl;}return0;}