locate_sample.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // Created by zx on 2019/12/30.
  3. //
  4. /*
  5. * locater 模块测试例子
  6. */
  7. #include <fcntl.h>
  8. #include "../locate/locate_task.h"
  9. #include "../locate/locater.h"
  10. #include <pcl/filters/passthrough.h>
  11. #include <glog/logging.h>
  12. #include <google/protobuf/io/coded_stream.h>
  13. #include <google/protobuf/io/zero_copy_stream_impl.h>
  14. #include <google/protobuf/text_format.h>
  15. using google::protobuf::io::FileInputStream;
  16. using google::protobuf::io::FileOutputStream;
  17. using google::protobuf::io::ZeroCopyInputStream;
  18. using google::protobuf::io::CodedInputStream;
  19. using google::protobuf::io::ZeroCopyOutputStream;
  20. using google::protobuf::io::CodedOutputStream;
  21. using google::protobuf::Message;
  22. #include <sys/stat.h>
  23. #include <dirent.h>
  24. void init_glog()
  25. {
  26. FLAGS_max_log_size = 100;
  27. FLAGS_logbufsecs = 0;
  28. google::InitGoogleLogging("LidarMeasurement_test");
  29. google::SetStderrLogging(google::INFO);
  30. google::InstallFailureSignalHandler();
  31. FLAGS_colorlogtostderr = true; // Set log color
  32. FLAGS_logbufsecs = 0; // Set log output speed(s)
  33. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  34. FLAGS_stop_logging_if_full_disk = true;
  35. }
  36. void list_dir( const char * dir_name,std::vector<std::string>& files)
  37. {
  38. if( 0 == dir_name )
  39. {
  40. std::cout<<" dir_name is null ! "<<std::endl;
  41. return;
  42. }
  43. struct stat s;
  44. lstat( dir_name , &s );
  45. if( ! S_ISDIR( s.st_mode ) )
  46. {
  47. return;
  48. }
  49. struct dirent * filename;
  50. DIR * dir;
  51. dir = opendir( dir_name );
  52. if( NULL == dir )
  53. {
  54. return;
  55. }
  56. int iName=0;
  57. while( ( filename = readdir(dir) ) != NULL )
  58. {
  59. if( strcmp( filename->d_name , "." ) == 0 ||
  60. strcmp( filename->d_name , "..") == 0)
  61. continue;
  62. char wholePath[128] = {0};
  63. sprintf(wholePath, "%s", filename->d_name);
  64. files.push_back(wholePath);
  65. }
  66. }
  67. bool read_proto_param(std::string path, ::google::protobuf::Message& param)
  68. {
  69. int fd = open(path.c_str(), O_RDONLY);
  70. if (fd == -1) return false;
  71. FileInputStream* input = new FileInputStream(fd);
  72. bool success = google::protobuf::TextFormat::Parse(input, &param);
  73. delete input;
  74. close(fd);
  75. return success;
  76. }
  77. bool string2point(std::string str,pcl::PointXYZ& point)
  78. {
  79. std::istringstream iss;
  80. iss.str(str);
  81. float value;
  82. float data[3]={0};
  83. for(int i=0;i<3;++i)
  84. {
  85. if(iss>>value)
  86. {
  87. data[i]=value;
  88. }
  89. else
  90. {
  91. return false;
  92. }
  93. }
  94. point.x=data[0];
  95. point.y=data[1];
  96. point.z=data[2];
  97. return true;
  98. }
  99. void ReadTxtCloud(std::string file, pcl::PointCloud<pcl::PointXYZ> &cloud)
  100. {
  101. cloud.clear();
  102. std::ifstream fin(file.c_str());
  103. const int line_length=64;
  104. char str[line_length]={0};
  105. // pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  106. while(fin.getline(str,line_length))
  107. {
  108. pcl::PointXYZ point;
  109. if(string2point(str,point))
  110. {
  111. cloud.push_back(point);
  112. }
  113. }
  114. // return *cloud;
  115. }
  116. int main(int argc,char* argv[])
  117. {
  118. init_glog();
  119. std::string input_file="cloud";
  120. std::string out_path="./test";
  121. if(argc>=2)
  122. {
  123. input_file=argv[1];
  124. }
  125. if(argc>=3)
  126. {
  127. out_path=argv[2];
  128. }
  129. Error_manager code;
  130. Locater* locater=new Locater();
  131. Measure::LocateParameter parameter;
  132. if(false==read_proto_param("./setting/locate.prototxt",parameter))
  133. {
  134. std::cout<<" read parameter failed..."<<std::endl;
  135. }
  136. code=locater->init(parameter);
  137. if(code!=SUCCESS)
  138. {
  139. LOG(ERROR)<<code.to_string();
  140. }
  141. std::string cloud_path="/home/zx/zzw/code/LidarMeasure/build/";
  142. cloud_path+=input_file;
  143. std::vector<std::string> files;
  144. list_dir(cloud_path.c_str(),files);
  145. int count=files.size();
  146. int correct_size = 0;
  147. while (1)
  148. {
  149. int count = 20;
  150. while(count-->0){
  151. for (int i = 0; i < files.size(); ++i)
  152. {
  153. pcl::PointCloud<pcl::PointXYZ> cloud;
  154. std::string t_file = cloud_path + "/" + files[i];
  155. ReadTxtCloud(t_file, cloud);
  156. std::cout << " input file: " << t_file << std::endl;
  157. //第二步,根据区域筛选点云
  158. pcl::PointCloud<pcl::PointXYZ>::Ptr locate_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  159. pcl::PassThrough<pcl::PointXYZ> passX;
  160. pcl::PassThrough<pcl::PointXYZ> passY;
  161. pcl::PassThrough<pcl::PointXYZ> passZ;
  162. passX.setInputCloud(cloud.makeShared());
  163. passX.setFilterFieldName("x");
  164. passX.setFilterLimits(-100000, 100000);
  165. passX.filter(*locate_cloud);
  166. passY.setInputCloud(locate_cloud);
  167. passY.setFilterFieldName("y");
  168. passY.setFilterLimits(-100000, 100000);
  169. passY.filter(*locate_cloud);
  170. passY.setInputCloud(locate_cloud);
  171. passY.setFilterFieldName("z");
  172. passY.setFilterLimits(-100000, 100000);
  173. passY.filter(*locate_cloud);
  174. LOG(INFO) << "筛选点云点数 : " << locate_cloud->size();
  175. Locate_task* task=new Locate_task();
  176. code = task->set_locate_cloud(locate_cloud);
  177. if (code != SUCCESS)
  178. {
  179. LOG(ERROR) << code.to_string();
  180. return 0;
  181. }
  182. task->set_save_path(out_path);
  183. code = locater->execute_task(task, 5);
  184. Locate_information dj_locate_information = task->get_locate_information();
  185. if (code == SUCCESS)
  186. {
  187. LOG(INFO) << " LOCATE SUCCESS";
  188. correct_size++;
  189. }
  190. else
  191. {
  192. LOG(ERROR) << code.to_string();
  193. //移动文件
  194. usleep(300);
  195. std::string src = "./test/cnn3d.txt";
  196. std::string dst = "./error/";
  197. dst += files[i];
  198. std::string name = "mv " + src + " " + dst;
  199. const char *des_name = name.c_str();
  200. system(des_name); //调用系统命令
  201. usleep(200);
  202. }
  203. delete task;
  204. }
  205. }
  206. getchar();
  207. }
  208. if(count>0)
  209. {
  210. std::cout<<std::endl<<" acc : "<<correct_size<<"/"<<count<<
  211. " percent:"<<float(correct_size)/float(count)<<std::endl;
  212. }
  213. return 0;
  214. }