sample_center_calib.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Created by zx on 2021/8/27.
  3. //
  4. #include "rotate_center_caliber.h"
  5. #include "point_tool.h"
  6. #include <pcl/visualization/pcl_visualizer.h>
  7. #include <pcl/common/transforms.h>
  8. #include "tool/pathcreator.h"
  9. #include "proto_tool.h"
  10. #include <iostream>
  11. #include <glog/logging.h>
  12. //using google::protobuf::io::FileInputStream;
  13. //using google::protobuf::io::FileOutputStream;
  14. using google::protobuf::io::ZeroCopyInputStream;
  15. using google::protobuf::io::CodedInputStream;
  16. using google::protobuf::io::ZeroCopyOutputStream;
  17. using google::protobuf::io::CodedOutputStream;
  18. using google::protobuf::Message;
  19. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  20. {
  21. time_t tt;
  22. time( &tt );
  23. tt = tt + 8*3600; // transform the time zone
  24. tm* t= gmtime( &tt );
  25. char buf[255]={0};
  26. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  27. t->tm_year + 1900,
  28. t->tm_mon + 1,
  29. t->tm_mday,
  30. t->tm_hour,
  31. t->tm_min,
  32. t->tm_sec);
  33. FILE* tp_file=fopen(buf,"w");
  34. fprintf(tp_file,data,strlen(data));
  35. fclose(tp_file);
  36. }
  37. void init_glog()
  38. {
  39. time_t tt = time(0);//时间cuo
  40. struct tm* t = localtime(&tt);
  41. char strYear[255]={0};
  42. char strMonth[255]={0};
  43. char strDay[255]={0};
  44. sprintf(strYear,"%04d", t->tm_year+1900);
  45. sprintf(strMonth,"%02d", t->tm_mon+1);
  46. sprintf(strDay,"%02d", t->tm_mday);
  47. char buf[255]={0};
  48. getcwd(buf,255);
  49. char strdir[255]={0};
  50. sprintf(strdir,"%s/caliber_log/%s/%s/%s", buf,strYear,strMonth,strDay);
  51. PathCreator creator;
  52. creator.Mkdir(strdir);
  53. char logPath[255] = { 0 };
  54. sprintf(logPath, "%s/", strdir);
  55. FLAGS_max_log_size = 100;
  56. FLAGS_logbufsecs = 0;
  57. google::InitGoogleLogging("shutter_verify");
  58. google::SetStderrLogging(google::INFO);
  59. google::SetLogDestination(0, logPath);
  60. google::SetLogFilenameExtension("zxlog");
  61. google::InstallFailureSignalHandler();
  62. google::InstallFailureWriter(&shut_down_logging);
  63. FLAGS_colorlogtostderr = true; // Set log color
  64. FLAGS_logbufsecs = 0; // Set log output speed(s)
  65. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  66. FLAGS_stop_logging_if_full_disk = true;
  67. }
  68. pcl::visualization::PCLVisualizer viewer;
  69. pcl::PointCloud<pcl::PointXYZ>::Ptr rotation_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,
  70. float theta,float ox,float oy)
  71. {
  72. //先平移点云,后旋转,再平移
  73. Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();
  74. pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud(new pcl::PointCloud<pcl::PointXYZ>());
  75. transform_2.translation() << -ox, -oy, 0;
  76. pcl::transformPointCloud(*cloud, *transformed_cloud, transform_2);
  77. Eigen::Affine3f transform_r = Eigen::Affine3f::Identity();
  78. pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud1(new pcl::PointCloud<pcl::PointXYZ>());
  79. transform_r.rotate(Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ()));
  80. pcl::transformPointCloud(*transformed_cloud, *transformed_cloud1, transform_r);
  81. pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud2(new pcl::PointCloud<pcl::PointXYZ>());
  82. Eigen::Affine3f transform_t = Eigen::Affine3f::Identity();
  83. transform_t.translation() << ox, oy, 0;
  84. pcl::transformPointCloud(*transformed_cloud1, *transformed_cloud2, transform_t);
  85. return transformed_cloud2;
  86. }
  87. #include "tick.hpp"
  88. #include <thread>
  89. #include <mutex>
  90. std::mutex mut;
  91. void run()
  92. {
  93. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud=ReadTxtCloud("./center_caliber_cloud_sample3.txt");
  94. LOG(INFO)<<" cloud size:"<<cloud->size();
  95. mut.lock();
  96. viewer.addPointCloud(cloud,"cloud_src");
  97. mut.unlock();
  98. rotate_center_caliber caliber;
  99. caliber.set_first_frame(cloud);
  100. float rotation_x=6.8;
  101. float rotation_y=-3.5;
  102. float theta=0;
  103. for(theta=-7./180.*M_PI;theta<8./180.*M_PI;theta+=1./180.*M_PI)
  104. {
  105. pcl::PointCloud<pcl::PointXYZ>::Ptr transform_cloud=rotation_cloud(cloud,theta,rotation_x,rotation_y);
  106. Tick tick;
  107. caliber.push_cloud(transform_cloud);
  108. std::cout<<"time : "<<tick.tic_relatively()<<std::endl;
  109. usleep(1000*500);
  110. }
  111. }
  112. int main()
  113. {
  114. init_glog();
  115. viewer.addCoordinateSystem(1.0,0,0,0,"car_center");
  116. std::thread* thread=new std::thread(run);
  117. while(1)
  118. {
  119. mut.lock();
  120. viewer.spinOnce();
  121. mut.unlock();
  122. usleep(50*1000);
  123. }
  124. return 0;
  125. }