main.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "object.pb.h"
  7. #include "objectarray.pb.h"
  8. #include <thread>
  9. #include <chrono>
  10. #include "modulecomm.h"
  11. #include "cluster.h"
  12. #include <getopt.h>
  13. #include <string>
  14. #include <eigen3/Eigen/Eigen>
  15. using namespace Eigen;
  16. using namespace std;
  17. char gstr_memname[256];
  18. void print_useage()
  19. {
  20. std::cout<<" -m --memname $mappath : share memory name. eq. -m lidar_pc"<<std::endl;
  21. std::cout<<" -h --help print help"<<std::endl;
  22. }
  23. std::vector<Obj> Objresult;
  24. void Listenlidarcnndectect(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  25. {
  26. // std::vector<iv::lidar::object> lidarobjvec;
  27. // strtolidarobj(lidarobjvec,strdata,nSize);
  28. iv::lidar::objectarray lidarobjvec;
  29. std::string in;
  30. in.append(strdata,nSize);
  31. lidarobjvec.ParseFromString(in);
  32. std::cout<<"lidar obj num: "<<lidarobjvec.obj_size()<<std::endl;
  33. Obj result;
  34. Objresult.clear();
  35. for(int i=0; i<lidarobjvec.obj_size();i++){
  36. iv::lidar::lidarobject lidarobj = lidarobjvec.obj(i);
  37. result.center_x = lidarobj.position().x();
  38. result.center_y = lidarobj.position().y();
  39. result.center_z = lidarobj.position().z();
  40. result.l = lidarobj.dimensions().x();
  41. result.w = lidarobj.dimensions().y();
  42. result.h = lidarobj.dimensions().z();
  43. result.yaw = lidarobj.tyaw();
  44. result.type_name = lidarobj.type_name();
  45. Eigen::AngleAxisf rotation_vector(-lidarobj.tyaw(), Eigen::Vector3f::UnitZ());
  46. result.Quaternion = Eigen::Quaternionf(rotation_vector);
  47. Objresult.push_back(result);
  48. }
  49. }
  50. int GetOptLong(int argc, char *argv[]) {
  51. int nRtn = 0;
  52. int opt; // getopt_long() 的返回值
  53. int digit_optind = 0; // 设置短参数类型及是否需要参数
  54. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  55. // 第几个元素的描述,即是long_opts的下标值
  56. int option_index = 0;
  57. // 设置短参数类型及是否需要参数
  58. const char *optstring = "m:h";
  59. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  60. /*
  61. struct option {
  62. const char * name; // 参数的名称
  63. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  64. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  65. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  66. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  67. };
  68. 其中:
  69. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  70. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  71. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  72. */
  73. static struct option long_options[] = {
  74. {"memname", required_argument, NULL, 'm'},
  75. {"help", no_argument, NULL, 'h'},
  76. // {"optarg", optional_argument, NULL, 'o'},
  77. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  78. };
  79. while ( (opt = getopt_long(argc,
  80. argv,
  81. optstring,
  82. long_options,
  83. &option_index)) != -1) {
  84. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  85. // printf("optarg = %s\n", optarg); // 参数内容
  86. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  87. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  88. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  89. // printf("\n");
  90. switch(opt)
  91. {
  92. case 'm':
  93. strncpy(gstr_memname,optarg,255);
  94. break;
  95. case 'h':
  96. print_useage();
  97. nRtn = 1; //because use -h
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. return nRtn;
  104. }
  105. pcl::visualization::CloudViewer viewer("Cloud Viewer");//创建viewer对象
  106. int user_data;
  107. // // read source lidar_pointcloud
  108. //void ListenPointCloud(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  109. //{
  110. // if(nSize <=16)return;
  111. // unsigned int * pHeadSize = (unsigned int *)strdata;
  112. // if(*pHeadSize > nSize)
  113. // {
  114. // std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
  115. // }
  116. // pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  117. // new pcl::PointCloud<pcl::PointXYZI>());
  118. // int nNameSize;
  119. // nNameSize = *pHeadSize - 4-4-8;
  120. // char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
  121. // memcpy(strName,(char *)((char *)strdata +4),nNameSize);
  122. // point_cloud->header.frame_id = strName;
  123. // memcpy(&point_cloud->header.seq,(char *)strdata+4+nNameSize,4);
  124. // memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
  125. // int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
  126. // int i;
  127. // pcl::PointXYZI * p;
  128. // p = (pcl::PointXYZI *)((char *)strdata + *pHeadSize);
  129. // for(i=0;i<nPCount;i++)
  130. // {
  131. // pcl::PointXYZI xp;
  132. // xp.x = p->x;
  133. // xp.y = p->y;
  134. // xp.z = p->z;
  135. // xp.intensity = p->intensity;
  136. // point_cloud->push_back(xp);
  137. // p++;
  138. // }
  139. // user_data++;
  140. // viewer.showCloud(point_cloud);
  141. //}
  142. // read segmentation_cnn output lidar_pointcloud
  143. void ListenPointCloud(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  144. {
  145. iv::lidar::objectarray lidarobjvec;
  146. std::string in;
  147. in.append(strdata,nSize);
  148. lidarobjvec.ParseFromString(in);
  149. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  150. new pcl::PointCloud<pcl::PointXYZI>());
  151. for(int i=0; i<lidarobjvec.obj_size();i++){
  152. iv::lidar::lidarobject lidarobj = lidarobjvec.obj(i);
  153. if(lidarobj.type_name()!="car" && lidarobj.type_name()!="pedestrian")
  154. continue;
  155. for(int j=0;j<lidarobj.cloud_size();j++){
  156. // iv::lidar::PointXYZI Point = lidarobj.cloud(j);
  157. pcl::PointXYZI xp;
  158. xp.x = lidarobj.cloud(j).x();
  159. xp.y = lidarobj.cloud(j).y();
  160. xp.z = lidarobj.cloud(j).z();
  161. xp.intensity = lidarobj.cloud(j).i();
  162. point_cloud->push_back(xp);
  163. }
  164. }
  165. viewer.showCloud(point_cloud);
  166. }
  167. void viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
  168. {
  169. viewer.setBackgroundColor(0.0,0.0,0.0);
  170. }
  171. void viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
  172. {
  173. static unsigned count = 0;
  174. std::stringstream ss;
  175. ss << "Point Cloud Count: " << user_data;
  176. viewer.removeShape ("text", 0);
  177. viewer.addText (ss.str(), 200, 300, "text", 0);
  178. viewer.removeAllShapes();
  179. viewer.addCube(-0.9,0.9,-2.3,2.3,-1.9,-0.4,0.0,0.0,1.0,"car",0);
  180. for(int i=0;i<Objresult.size();i++){
  181. Eigen::Vector3f center(Objresult[i].center_x,Objresult[i].center_y,Objresult[i].center_z);
  182. std::string cube = "box"+std::to_string(i);
  183. viewer.addCube(center, Objresult[i].Quaternion,Objresult[i].w,Objresult[i].h,Objresult[i].l, cube);
  184. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION, pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, cube);
  185. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, cube);
  186. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, 1, cube);
  187. std::string cubeFill = "boxFill"+std::to_string(i);
  188. viewer.addCube(center, Objresult[i].Quaternion,Objresult[i].w,Objresult[i].h,Objresult[i].l, cubeFill);
  189. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION, pcl::visualization::PCL_VISUALIZER_REPRESENTATION_SURFACE, cubeFill);
  190. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, cubeFill);
  191. viewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, 0.3, cubeFill);
  192. }
  193. }
  194. int main(int argc, char *argv[])
  195. {
  196. QCoreApplication a(argc, argv);
  197. snprintf(gstr_memname,255,"lidar_pointpillar");
  198. int nRtn = GetOptLong(argc,argv);
  199. if(nRtn == 1) //show help,so exit.
  200. {
  201. return 0;
  202. }
  203. //This will only get called once
  204. viewer.runOnVisualizationThreadOnce (viewerOneOff);
  205. //This will get called once per visualization iteration
  206. viewer.runOnVisualizationThread (viewerPsycho);
  207. //std::string gstrinput = "lidar_pointpillar";
  208. std::string gstrinput = "lidar_track";
  209. void * pa = iv::modulecomm::RegisterRecv(gstr_memname,ListenPointCloud);
  210. void * gpadetect = iv::modulecomm::RegisterRecv(gstrinput.data(),Listenlidarcnndectect);
  211. while(!viewer.wasStopped())
  212. {
  213. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  214. }
  215. return 0;
  216. return a.exec();
  217. }