point3D_tool.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Created by huli on 2021/3/24.
  3. //
  4. #ifndef NNXX_TESTS_POINT3D_TOOL_H
  5. #define NNXX_TESTS_POINT3D_TOOL_H
  6. #include <pcl/point_cloud.h>
  7. #include <pcl/point_types.h>
  8. #include <vector>
  9. #include <opencv4/opencv2/opencv.hpp>
  10. #include "defines.hpp"
  11. class Point3D_tool
  12. {
  13. public:
  14. //坐标
  15. struct Point3D
  16. {
  17. float x=0; //x轴
  18. float y=0; //y轴
  19. float z=0; //z轴
  20. };
  21. //平面坐标的限定范围
  22. struct Point3D_box
  23. {
  24. float x_min=0; //x轴最小值
  25. float x_max=0; //x轴最大值
  26. float y_min=0; //y轴最小值
  27. float y_max=0; //y轴最大值
  28. float z_min=0; //z轴最小值
  29. float z_max=0; //z轴最大值
  30. };
  31. template <typename PclPoint>
  32. static int getMinRect(cv::RotatedRect &rect, pcl::shared_ptr<pcl::PointCloud<PclPoint>> &clouds) {
  33. if (clouds->points.size() < 4) {
  34. DLOG(ERROR) << "points to little.";
  35. return false;
  36. }
  37. std::vector<cv::Point2f> cv_clouds;
  38. for (auto &point: clouds->points) {
  39. cv_clouds.emplace_back(point.x, point.y);
  40. }
  41. rect = cv::minAreaRect(cv_clouds);
  42. return true;
  43. }
  44. // template <typename PclPoint>
  45. // static int getMinRect(cv::RotatedRect &rect, pcl::shared_ptr<pcl::PointCloud<PclPoint>> &points) {
  46. static int getRectCloud(cv::RotatedRect &rect, pcl::shared_ptr<pcl::PointCloud<pcl::PointXYZ>> &points, float z_value = 0, int numbers = 100) {
  47. cv::Point2f cv_points[4];
  48. rect.points(cv_points);
  49. // PclPoint point;
  50. pcl::PointXYZ point;
  51. // 左侧点
  52. double step_x = (cv_points[1].x - cv_points[0].x) / numbers;
  53. double step_y = (cv_points[1].y - cv_points[0].y) / numbers;
  54. for (int i = 0; i < numbers; i++) {
  55. point.x = cv_points[0].x + i * step_x;
  56. point.y = cv_points[0].y + i * step_y;
  57. point.z = z_value;
  58. points->points.emplace_back(point);
  59. }
  60. // 上侧点
  61. step_x = (cv_points[2].x - cv_points[1].x) / numbers;
  62. step_y = (cv_points[2].y - cv_points[1].y) / numbers;
  63. for (int i = 0; i < numbers; i++) {
  64. point.x = cv_points[1].x + i * step_x;
  65. point.y = cv_points[1].y + i * step_y;
  66. point.z = z_value;
  67. points->points.emplace_back(point);
  68. }
  69. // 右侧点
  70. step_x = (cv_points[3].x - cv_points[2].x) / numbers;
  71. step_y = (cv_points[3].y - cv_points[2].y) / numbers;
  72. for (int i = 0; i < numbers; i++) {
  73. point.x = cv_points[2].x + i * step_x;
  74. point.y = cv_points[2].y + i * step_y;
  75. point.z = z_value;
  76. points->points.emplace_back(point);
  77. }
  78. // 下侧点
  79. step_x = (cv_points[0].x - cv_points[3].x) / numbers;
  80. step_y = (cv_points[0].y - cv_points[3].y) / numbers;
  81. for (int i = 0; i < numbers; i++) {
  82. point.x = cv_points[3].x + i * step_x;
  83. point.y = cv_points[3].y + i * step_y;
  84. point.z = z_value;
  85. points->points.emplace_back(point);
  86. }
  87. return points->size();
  88. }
  89. template <typename PclPoint>
  90. static int getRectCloud(pcl::shared_ptr<pcl::PointCloud<PclPoint>> &clouds, pcl::shared_ptr<pcl::PointCloud<pcl::PointXYZ>> &points, float z_value = 0, int numbers = 100) {
  91. cv::RotatedRect rect = getMinRect(clouds);
  92. getRectCloud(rect, points, z_value, numbers);
  93. return points->size();
  94. }
  95. static void getRectWidthlength(cv::RotatedRect &rect, double &width, double &length) {
  96. cv::Point2f points[4];
  97. rect.points(points);
  98. double a = sqrt((points[1].x - points[0].x) * (points[1].x - points[0].x) + (points[1].y - points[0].y) * (points[1].y - points[0].y));
  99. double b = sqrt((points[2].x - points[1].x) * (points[2].x - points[1].x) + (points[2].y - points[1].y) * (points[2].y - points[1].y));
  100. width = MIN(a, b);
  101. length = MAX(a, b);
  102. }
  103. };
  104. #endif //NNXX_TESTS_POINT3D_TOOL_H