| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #include <pcl/visualization/cloud_viewer.h>
- #include <iostream>
- #include <pcl/io/io.h>
- #include <pcl/io/pcd_io.h>
- #include <QCoreApplication>
- #include <thread>
- #include <chrono>
- //#include "modulecomm.h"
- #include <getopt.h>
- char gstr_mapname[256];
- char gstr_xmlpath[256];
- char gstr_logpath[256];
- void print_useage()
- {
- std::cout<<" -p --pcd $pcdpath : map path. eq. -p /home/nvidia/map/gpu.pcd"<<std::endl;
- std::cout<<" -h --help print help"<<std::endl;
- }
- int GetOptLong(int argc, char *argv[]) {
- int nRtn = 0;
- int opt; // getopt_long() 的返回值
- int digit_optind = 0; // 设置短参数类型及是否需要参数
- // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
- // 第几个元素的描述,即是long_opts的下标值
- int option_index = 0;
- // 设置短参数类型及是否需要参数
- const char *optstring = "p:h";
- // 设置长参数类型及其简写,比如 --reqarg <==>-r
- /*
- struct option {
- const char * name; // 参数的名称
- int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
- int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
- // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
- int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
- };
- 其中:
- no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
- required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
- optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
- */
- static struct option long_options[] = {
- {"pcdpath", required_argument, NULL, 'p'},
- {"help", no_argument, NULL, 'h'},
- // {"optarg", optional_argument, NULL, 'o'},
- {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
- };
- while ( (opt = getopt_long(argc,
- argv,
- optstring,
- long_options,
- &option_index)) != -1) {
- // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
- // printf("optarg = %s\n", optarg); // 参数内容
- // printf("optind = %d\n", optind); // 下一个被处理的下标值
- // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
- // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
- // printf("\n");
- switch(opt)
- {
- case 'p':
- strncpy(gstr_mapname,optarg,255);
- break;
- case 'h':
- print_useage();
- nRtn = 1; //because use -h
- break;
- default:
- break;
- }
- }
- return nRtn;
- }
- pcl::visualization::CloudViewer viewer1("Cloud Viewer");//创建viewer对象
- int user_data;
- void viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
- {
- //设置背景颜色
- viewer.setBackgroundColor(0.0,0.0,0.0);
- // viewer.setCameraPosition(0,0,1.0,0,0,0);
- // viewer.setBackgroundColor (1.0, 0.5, 1.0);
- //球体坐标
- pcl::PointXYZ o;
- o.x = 0;
- o.y = 0;
- o.z = 0;
- //添加球体
- // viewer.addSphere (o, 1, "sphere", 0);
- // viewer.addCube(-0.9,0.9,9.5,10.5,-1.9,-0.4,0.0,1.0,0.0,"flag",0);
- //Draw Car
- // viewer.addCube(-0.9,0.9,-2.3,2.3,-1.9,-0.4,0.0,0.0,1.0,"car",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++;
- ss << "Point Cloud Count: " << user_data;
- viewer.removeShape ("text", 0);
- viewer.addText (ss.str(), 200, 300, "text", 0);
- //FIXME: possible race condition here:
- // user_data++;
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- snprintf(gstr_mapname,255,"");
- int nRtn = GetOptLong(argc,argv);
- if(nRtn == 1) //show help,so exit.
- {
- return 0;
- }
- pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
- new pcl::PointCloud<pcl::PointXYZI>());
- std::string path = gstr_mapname;
- if(path.length() < 1)
- {
- std::cout<<"Please use -p set pcd path."<<std::endl;
- return 0;
- }
- //This will only get called once
- viewer1.runOnVisualizationThreadOnce (viewerOneOff);
- //This will get called once per visualization iteration
- viewer1.runOnVisualizationThread (viewerPsycho);
- pcl::io::loadPCDFile<pcl::PointXYZI>(path,*point_cloud);
- viewer1.showCloud(point_cloud);
- while(!viewer1.wasStopped())
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- }
- return 0;
- return a.exec();
- }
|