main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <QVector>
  7. #include <thread>
  8. #include <chrono>
  9. #include <qjsondocument.h>
  10. #include <qjsonobject.h>
  11. #include <QJsonArray>
  12. #include <QDebug>
  13. #include <QFile>
  14. //#include "modulecomm.h"
  15. #include "mathlocation.h"
  16. #include <getopt.h>
  17. char gstr_mapname[256];
  18. char gstr_xmlpath[256];
  19. char gstr_logpath[256];
  20. void print_useage()
  21. {
  22. std::cout<<" -p --pcd $pcdpath : map path. eq. -p /home/nvidia/map/gpu.pcd"<<std::endl;
  23. std::cout<<" -h --help print help"<<std::endl;
  24. }
  25. int GetOptLong(int argc, char *argv[]) {
  26. int nRtn = 0;
  27. int opt; // getopt_long() 的返回值
  28. int digit_optind = 0; // 设置短参数类型及是否需要参数
  29. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  30. // 第几个元素的描述,即是long_opts的下标值
  31. int option_index = 0;
  32. // 设置短参数类型及是否需要参数
  33. const char *optstring = "p:h";
  34. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  35. /*
  36. struct option {
  37. const char * name; // 参数的名称
  38. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  39. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  40. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  41. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  42. };
  43. 其中:
  44. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  45. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  46. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  47. */
  48. static struct option long_options[] = {
  49. {"pcdpath", required_argument, NULL, 'p'},
  50. {"help", no_argument, NULL, 'h'},
  51. // {"optarg", optional_argument, NULL, 'o'},
  52. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  53. };
  54. strncpy(gstr_mapname,"/home/nvidia/BorregasAve.pcd",255);
  55. while ( (opt = getopt_long(argc,
  56. argv,
  57. optstring,
  58. long_options,
  59. &option_index)) != -1) {
  60. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  61. // printf("optarg = %s\n", optarg); // 参数内容
  62. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  63. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  64. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  65. // printf("\n");
  66. switch(opt)
  67. {
  68. case 'p':
  69. strncpy(gstr_mapname,optarg,255);
  70. break;
  71. case 'h':
  72. print_useage();
  73. nRtn = 1; //because use -h
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. return nRtn;
  80. }
  81. int writeFile(QJsonObject obj)
  82. {
  83. QByteArray baframe;
  84. QString filename = "/home/nvidia/pointCloud.json";
  85. QFile xFile(filename);
  86. if(!xFile.open(QIODevice::QIODevice::WriteOnly))
  87. {
  88. std::cout<<" File Fail."<<std::endl;
  89. return -1;
  90. }
  91. baframe = QJsonDocument(obj).toJson();
  92. int result = xFile.write(baframe);
  93. xFile.close();
  94. return result;
  95. }
  96. int main(int argc, char *argv[])
  97. {
  98. QCoreApplication a(argc, argv);
  99. snprintf(gstr_mapname,255,"");
  100. int nRtn = GetOptLong(argc,argv);
  101. if(nRtn == 1) //show help,so exit.
  102. {
  103. return 0;
  104. }
  105. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  106. new pcl::PointCloud<pcl::PointXYZI>());
  107. std::string path = gstr_mapname;
  108. if(path.length() < 1)
  109. {
  110. std::cout<<"Please use -p set pcd path."<<std::endl;
  111. return 0;
  112. }
  113. pcl::io::loadPCDFile<pcl::PointXYZI>(path,*point_cloud);
  114. double lat = 39.12072740;
  115. double lon= 117.02800329;
  116. double latx,lonx;
  117. double x,y;
  118. GaussProjCal(lon, lat, &x, &y);
  119. QJsonArray dataArray,featureArry;
  120. QJsonObject dataObj,properObj,geoObj,obj;
  121. dataObj.insert("type",QJsonValue("Feature"));
  122. properObj.insert("color",QJsonValue("#111111"));
  123. dataObj.insert("properties",QJsonValue(properObj));
  124. geoObj.insert("type",QJsonValue("MultiPoint"));
  125. int size = point_cloud->size();
  126. for(int i = 0; i < size; i++)
  127. {
  128. QJsonArray arr;
  129. GaussProjInvCal(point_cloud->points[i].x+x, point_cloud->points[i].y+y, &lon, &lat);
  130. arr.append(QJsonValue(lon));
  131. arr.append(QJsonValue(lat));
  132. arr.append(QJsonValue(point_cloud->points[i].z));
  133. dataArray.append(QJsonValue(arr));
  134. qDebug()<<arr;
  135. }
  136. geoObj.insert("coordinates",QJsonValue(dataArray));
  137. dataObj.insert("geometry",geoObj);
  138. featureArry.append(QJsonValue(dataObj));
  139. obj.insert("type",QJsonValue("FeatureCollection"));
  140. obj.insert("features",QJsonValue(featureArry));
  141. int result = writeFile(obj);
  142. qDebug("Convert compless, %d point has been writed",result);
  143. return a.exec();
  144. }