main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <pcl/visualization/cloud_viewer.h>
  2. #include <iostream>
  3. #include <pcl/io/io.h>
  4. #include <pcl/io/pcd_io.h>
  5. #include <QCoreApplication>
  6. #include <thread>
  7. #include <chrono>
  8. //#include "modulecomm.h"
  9. #include <getopt.h>
  10. char gstr_mapname[256];
  11. char gstr_xmlpath[256];
  12. char gstr_logpath[256];
  13. void print_useage()
  14. {
  15. std::cout<<" -p --pcd $pcdpath : map path. eq. -p /home/nvidia/map/gpu.pcd"<<std::endl;
  16. std::cout<<" -h --help print help"<<std::endl;
  17. }
  18. int GetOptLong(int argc, char *argv[]) {
  19. int nRtn = 0;
  20. int opt; // getopt_long() 的返回值
  21. int digit_optind = 0; // 设置短参数类型及是否需要参数
  22. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  23. // 第几个元素的描述,即是long_opts的下标值
  24. int option_index = 0;
  25. // 设置短参数类型及是否需要参数
  26. const char *optstring = "p:h";
  27. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  28. /*
  29. struct option {
  30. const char * name; // 参数的名称
  31. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  32. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  33. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  34. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  35. };
  36. 其中:
  37. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  38. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  39. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  40. */
  41. static struct option long_options[] = {
  42. {"pcdpath", required_argument, NULL, 'p'},
  43. {"help", no_argument, NULL, 'h'},
  44. // {"optarg", optional_argument, NULL, 'o'},
  45. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  46. };
  47. while ( (opt = getopt_long(argc,
  48. argv,
  49. optstring,
  50. long_options,
  51. &option_index)) != -1) {
  52. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  53. // printf("optarg = %s\n", optarg); // 参数内容
  54. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  55. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  56. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  57. // printf("\n");
  58. switch(opt)
  59. {
  60. case 'p':
  61. strncpy(gstr_mapname,optarg,255);
  62. break;
  63. case 'h':
  64. print_useage();
  65. nRtn = 1; //because use -h
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. return nRtn;
  72. }
  73. pcl::visualization::CloudViewer viewer1("Cloud Viewer");//创建viewer对象
  74. int user_data;
  75. void viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
  76. {
  77. //设置背景颜色
  78. viewer.setBackgroundColor(0.0,0.0,0.0);
  79. // viewer.setCameraPosition(0,0,1.0,0,0,0);
  80. // viewer.setBackgroundColor (1.0, 0.5, 1.0);
  81. //球体坐标
  82. pcl::PointXYZ o;
  83. o.x = 0;
  84. o.y = 0;
  85. o.z = 0;
  86. //添加球体
  87. // viewer.addSphere (o, 1, "sphere", 0);
  88. // viewer.addCube(-0.9,0.9,9.5,10.5,-1.9,-0.4,0.0,1.0,0.0,"flag",0);
  89. //Draw Car
  90. // viewer.addCube(-0.9,0.9,-2.3,2.3,-1.9,-0.4,0.0,0.0,1.0,"car",0);
  91. std::cout << "i only run once" << std::endl;
  92. }
  93. void viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
  94. {
  95. static unsigned count = 0;
  96. std::stringstream ss;
  97. // ss << "Once per viewer loop: " << count++;
  98. ss << "Point Cloud Count: " << user_data;
  99. viewer.removeShape ("text", 0);
  100. viewer.addText (ss.str(), 200, 300, "text", 0);
  101. //FIXME: possible race condition here:
  102. // user_data++;
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. QCoreApplication a(argc, argv);
  107. snprintf(gstr_mapname,255,"");
  108. int nRtn = GetOptLong(argc,argv);
  109. if(nRtn == 1) //show help,so exit.
  110. {
  111. return 0;
  112. }
  113. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  114. new pcl::PointCloud<pcl::PointXYZI>());
  115. std::string path = gstr_mapname;
  116. if(path.length() < 1)
  117. {
  118. std::cout<<"Please use -p set pcd path."<<std::endl;
  119. return 0;
  120. }
  121. //This will only get called once
  122. viewer1.runOnVisualizationThreadOnce (viewerOneOff);
  123. //This will get called once per visualization iteration
  124. viewer1.runOnVisualizationThread (viewerPsycho);
  125. pcl::io::loadPCDFile<pcl::PointXYZI>(path,*point_cloud);
  126. viewer1.showCloud(point_cloud);
  127. while(!viewer1.wasStopped())
  128. {
  129. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  130. }
  131. return 0;
  132. return a.exec();
  133. }