point2D_tool.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifdef OPEN_PCL_FLAG
  82. //平面坐标的转换, 可以进行旋转和平移, 不能缩放
  83. bool Point2D_tool::transform_with_translation_rotation(float* p_x_in, float* p_y_in, float* p_x_out, float* p_y_out,
  84. Point2D_tool::Point2D_transform* p_point2D_transform)
  85. {
  86. if ( p_x_in == NULL || p_x_in == NULL || p_x_out == NULL || p_y_out == NULL || p_point2D_transform == NULL)
  87. {
  88. return false;
  89. }
  90. else
  91. {
  92. *p_x_out = *p_x_in * p_point2D_transform->m00 + *p_y_in * p_point2D_transform->m01 + p_point2D_transform->m02;
  93. *p_y_out = *p_x_in * p_point2D_transform->m10 + *p_y_in * p_point2D_transform->m11 + p_point2D_transform->m12;
  94. return true;
  95. }
  96. return true;
  97. }
  98. bool Point2D_tool::transform_with_translation_rotation(pcl::PointXYZ* p_point3D_in, pcl::PointXYZ* p_point3D_out,
  99. Point2D_tool::Point2D_transform* p_point2D_transform)
  100. {
  101. if (p_point3D_in == NULL || p_point3D_out == NULL || p_point2D_transform == NULL)
  102. {
  103. return false;
  104. }
  105. else
  106. {
  107. p_point3D_out->x = p_point3D_in->x * p_point2D_transform->m00 + p_point3D_in->y * p_point2D_transform->m01 +
  108. p_point2D_transform->m02;
  109. p_point3D_out->y = p_point3D_in->x * p_point2D_transform->m10 + p_point3D_in->y * p_point2D_transform->m11 +
  110. p_point2D_transform->m12;
  111. return true;
  112. }
  113. return true;
  114. }
  115. bool Point2D_tool::transform_with_translation_rotation(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in,
  116. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_out,
  117. Point2D_tool::Point2D_transform* p_point2D_transform)
  118. {
  119. if ( p_cloud_in.get() == NULL || p_cloud_out.get() == NULL || p_point2D_transform == NULL)
  120. {
  121. return false;
  122. }
  123. else
  124. {
  125. pcl::PointXYZ point;
  126. for (int i = 0; i < p_cloud_in->size(); ++i)
  127. {
  128. 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;
  129. 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;
  130. p_cloud_out->push_back(point);
  131. }
  132. return true;
  133. }
  134. return true;
  135. }
  136. #endif