locate_sample.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <glog/logging.h>
  11. #include <google/protobuf/io/coded_stream.h>
  12. #include <google/protobuf/io/zero_copy_stream_impl.h>
  13. #include <google/protobuf/text_format.h>
  14. using google::protobuf::io::FileInputStream;
  15. using google::protobuf::io::FileOutputStream;
  16. using google::protobuf::io::ZeroCopyInputStream;
  17. using google::protobuf::io::CodedInputStream;
  18. using google::protobuf::io::ZeroCopyOutputStream;
  19. using google::protobuf::io::CodedOutputStream;
  20. using google::protobuf::Message;
  21. bool read_proto_param(std::string path, ::google::protobuf::Message& param)
  22. {
  23. int fd = open(path.c_str(), O_RDONLY);
  24. if (fd == -1) return false;
  25. FileInputStream* input = new FileInputStream(fd);
  26. bool success = google::protobuf::TextFormat::Parse(input, &param);
  27. delete input;
  28. close(fd);
  29. return success;
  30. }
  31. bool string2point(std::string str,pcl::PointXYZ& point)
  32. {
  33. std::istringstream iss;
  34. iss.str(str);
  35. float value;
  36. float data[3]={0};
  37. for(int i=0;i<3;++i)
  38. {
  39. if(iss>>value)
  40. {
  41. data[i]=value;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48. point.x=data[0];
  49. point.y=data[1];
  50. point.z=data[2];
  51. return true;
  52. }
  53. pcl::PointCloud<pcl::PointXYZ>::Ptr ReadTxtCloud(std::string file)
  54. {
  55. std::ifstream fin(file.c_str());
  56. const int line_length=64;
  57. char str[line_length]={0};
  58. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  59. while(fin.getline(str,line_length))
  60. {
  61. pcl::PointXYZ point;
  62. if(string2point(str,point))
  63. {
  64. cloud->push_back(point);
  65. }
  66. }
  67. return cloud;
  68. }
  69. int main(int argc,char* argv[])
  70. {
  71. std::string input_file="20191217-215217.txt";
  72. std::string out_path="./test";
  73. if(argc>=2)
  74. {
  75. input_file=argv[1];
  76. input_file+=".txt";
  77. }
  78. if(argc>=3)
  79. {
  80. out_path=argv[2];
  81. }
  82. Error_manager code;
  83. Locater* locater=new Locater();
  84. Measure::LocateParameter parameter;
  85. if(false==read_proto_param("./setting/locate.prototxt",parameter))
  86. {
  87. std::cout<<" read parameter failed..."<<std::endl;
  88. }
  89. code=locater->init(parameter);
  90. if(code!=SUCCESS)
  91. {
  92. LOG(ERROR)<<code.to_string();
  93. }
  94. Locate_task* task=new Locate_task();
  95. std::string cloud_path="/home/zx/data/samples/src_txt/balck_suv/";
  96. cloud_path+=input_file;
  97. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
  98. cloud=ReadTxtCloud(cloud_path);
  99. std::cout<<" input file: "<<cloud_path<<std::endl;
  100. code=task->set_locate_cloud(cloud);
  101. if(code!=SUCCESS)
  102. {
  103. LOG(ERROR)<<code.to_string();
  104. return 0;
  105. }
  106. task->set_save_path(out_path);
  107. code=locater->execute_task(task,5);
  108. if(code==SUCCESS)
  109. {
  110. LOG(INFO)<<" LOCATE SUCCESS";
  111. }
  112. else
  113. {
  114. LOG(ERROR)<<code.to_string();
  115. }
  116. return 0;
  117. }