wheel_model_3d.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Created by zx on 2023/7/20.
  3. //
  4. #include "wheel_model_3d.h"
  5. Error_manager WheelModel3D::init() {
  6. return (loadLeftModel() == Error_code::SUCCESS && loadRightModel() == Error_code::SUCCESS) ? Error_code::SUCCESS : Error_code::FAILED;
  7. }
  8. Error_manager WheelModel3D::loadLeftModel() {
  9. pcl::PointCloud<pcl::PointXYZ>::Ptr left_clouds(new pcl::PointCloud<pcl::PointXYZ>);
  10. if (ReadTxtCloud(left_file, left_clouds)) {
  11. mp_left_grid = updateModel(left_clouds);
  12. if (mp_left_grid == nullptr) {
  13. LOG(INFO) << "------------>WO KAO, FAILED TO LOAD LEFT MODEL";
  14. exit(0);
  15. }
  16. return Error_code::SUCCESS;
  17. }
  18. return Error_code::FAILED;
  19. }
  20. Error_manager WheelModel3D::loadRightModel() {
  21. pcl::PointCloud<pcl::PointXYZ>::Ptr right_clouds(new pcl::PointCloud<pcl::PointXYZ>);
  22. if (ReadTxtCloud(right_file, right_clouds)) {
  23. mp_right_grid = updateModel(right_clouds);
  24. if (mp_right_grid == nullptr) {
  25. LOG(INFO) << "------------>WO KAO, FAILED TO LOAD RIGHT MODEL";
  26. exit(0);
  27. }
  28. return Error_code::SUCCESS;
  29. }
  30. return Error_code::FAILED;
  31. }
  32. HybridGrid* WheelModel3D::updateModel(pcl::PointCloud<pcl::PointXYZ>::Ptr &clouds, float ratio)
  33. {
  34. float del_x=0.05;
  35. float del_y=0.03;
  36. float del_z=0.05;
  37. Eigen::Matrix3f delta=Eigen::MatrixXf::Identity(3,3);
  38. delta(0,0)=del_x*del_x;
  39. delta(1,1)=del_y*del_y;
  40. delta(2,2)=del_z*del_z;
  41. Eigen::Matrix3f delta_inv=delta.inverse();
  42. Eigen::Vector3f d(0,0,0.02);
  43. Eigen::MatrixXf ce=-0.5*((d).transpose()*delta_inv*(d));
  44. float cut_p=exp(ce(0,0));
  45. float resolution=0.01;
  46. HybridGrid *grid = new HybridGrid(resolution);
  47. for (auto &point: clouds->points) {
  48. point.x *= ratio;
  49. point.y *= ratio;
  50. point.z *= ratio;
  51. }
  52. for(int i = 0; i<clouds->size(); ++i)
  53. {
  54. pcl::PointXYZ pt = clouds->points[i];
  55. Eigen::Vector3f u(pt.x,pt.y,pt.z);
  56. for(float x=pt.x-0.1;x<pt.x+0.1;x+=grid->resolution())
  57. {
  58. for(float y=pt.y-0.1;y<pt.y+0.1;y+=grid->resolution())
  59. {
  60. for(float z=pt.z-0.1;z<pt.z+0.1;z+=grid->resolution())
  61. {
  62. Eigen::Vector3f p(x,y,z);
  63. Eigen::MatrixXf B=-0.5*((p-u).transpose()*delta_inv*(p-u));
  64. float prob=exp(B(0,0));
  65. if(prob>cut_p)
  66. prob=cut_p;
  67. Eigen::Array3i index=grid->GetCellIndex(p);
  68. if(grid->GetProbability(index)<prob){
  69. grid->SetProbability(index,prob);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return grid;
  76. }