point2D_tool.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Created by huli on 2020/9/1.
  3. //
  4. #include "point2D_tool.h"
  5. //极坐标 -> 平面坐标
  6. bool Point2D_tool::Polar_coordinates_to_point2D(Point2D_tool::Polar_coordinates* p_polar_coordinates, Point2D_tool::Point2D* p_point2D)
  7. {
  8. if ( p_polar_coordinates == NULL || p_point2D == NULL)
  9. {
  10. return false;
  11. }
  12. else
  13. {
  14. p_point2D->x = p_polar_coordinates->distance * cos(p_polar_coordinates->angle);
  15. p_point2D->y = p_polar_coordinates->distance * sin(p_polar_coordinates->angle);
  16. return true;
  17. }
  18. return true;
  19. }
  20. //平面坐标 -> 极坐标
  21. bool Point2D_tool::Point2D_to_polar_coordinates(Point2D_tool::Point2D* p_point2D, Point2D_tool::Polar_coordinates* p_polar_coordinates)
  22. {
  23. if ( p_polar_coordinates == NULL || p_point2D == NULL)
  24. {
  25. return false;
  26. }
  27. else
  28. {
  29. p_polar_coordinates->distance = sqrt(p_point2D->x * p_point2D->x + p_point2D->y * p_point2D->y);
  30. p_polar_coordinates->angle = atan(p_point2D->y / p_point2D->x);
  31. return true;
  32. }
  33. return true;
  34. }
  35. //判断极坐标点是否在限制范围
  36. bool Point2D_tool::limit_with_polar_coordinates_box(float distance, float angle, Point2D_tool::Polar_coordinates_box* p_polar_coordinates_box)
  37. {
  38. if ( p_polar_coordinates_box == NULL )
  39. {
  40. return false;
  41. }
  42. else
  43. {
  44. if ( angle >= p_polar_coordinates_box->angle_min &&
  45. angle <= p_polar_coordinates_box->angle_max &&
  46. distance >= p_polar_coordinates_box->distance_min &&
  47. distance <= p_polar_coordinates_box->distance_max )
  48. {
  49. return true;
  50. }
  51. else
  52. {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. //判断平面坐标点是否在限制范围
  59. bool Point2D_tool::limit_with_point2D_box(float x, float y, Point2D_tool::Point2D_box* p_point2D_box)
  60. {
  61. if ( p_point2D_box == NULL )
  62. {
  63. return false;
  64. }
  65. else
  66. {
  67. if ( x >= p_point2D_box->x_min &&
  68. x <= p_point2D_box->x_max &&
  69. y >= p_point2D_box->y_min &&
  70. y <= p_point2D_box->y_max )
  71. {
  72. return true;
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. //平面坐标的转换, 可以进行旋转和平移, 不能缩放
  82. bool Point2D_tool::transform_with_translation_rotation(float* p_x_in, float* p_y_in, float* p_x_out, float* p_y_out,
  83. Point2D_tool::Point2D_transform* p_point2D_transform)
  84. {
  85. if ( p_x_in == NULL || p_x_in == NULL || p_x_out == NULL || p_y_out == NULL || p_point2D_transform == NULL)
  86. {
  87. return false;
  88. }
  89. else
  90. {
  91. *p_x_out = *p_x_in * p_point2D_transform->m00 + *p_y_in * p_point2D_transform->m01 + p_point2D_transform->m02;
  92. *p_y_out = *p_x_in * p_point2D_transform->m10 + *p_y_in * p_point2D_transform->m11 + p_point2D_transform->m12;
  93. return true;
  94. }
  95. return true;
  96. }
  97. bool Point2D_tool::transform_with_translation_rotation(pcl::PointXYZ* p_point3D_in, pcl::PointXYZ* p_point3D_out,
  98. Point2D_tool::Point2D_transform* p_point2D_transform)
  99. {
  100. if (p_point3D_in == NULL || p_point3D_out == NULL || p_point2D_transform == NULL)
  101. {
  102. return false;
  103. }
  104. else
  105. {
  106. p_point3D_out->x = p_point3D_in->x * p_point2D_transform->m00 + p_point3D_in->y * p_point2D_transform->m01 +
  107. p_point2D_transform->m02;
  108. p_point3D_out->y = p_point3D_in->x * p_point2D_transform->m10 + p_point3D_in->y * p_point2D_transform->m11 +
  109. p_point2D_transform->m12;
  110. return true;
  111. }
  112. return true;
  113. }
  114. bool Point2D_tool::transform_with_translation_rotation(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in,
  115. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_out,
  116. Point2D_tool::Point2D_transform* p_point2D_transform)
  117. {
  118. if ( p_cloud_in.get() == NULL || p_cloud_out.get() == NULL || p_point2D_transform == NULL)
  119. {
  120. return false;
  121. }
  122. else
  123. {
  124. pcl::PointXYZ point;
  125. for (int i = 0; i < p_cloud_in->size(); ++i)
  126. {
  127. point.x = p_cloud_in->points[i].x * p_point2D_transform->m00 + p_cloud_in->points[i].y * p_point2D_transform->m01 + p_point2D_transform->m02;
  128. point.y = p_cloud_in->points[i].x * p_point2D_transform->m10 + p_cloud_in->points[i].y * p_point2D_transform->m11 + p_point2D_transform->m12;
  129. p_cloud_out->push_back(point);
  130. }
  131. return true;
  132. }
  133. return true;
  134. }