point2D_tool.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // Created by huli on 2020/9/1.
  3. //
  4. #ifndef POINT2D_TOOL_H
  5. #define POINT2D_TOOL_H
  6. //#define OPEN_PCL_FLAG
  7. #ifdef OPEN_PCL_FLAG
  8. #include <pcl/point_types.h>
  9. #include <pcl/PCLPointCloud2.h>
  10. #include <pcl/conversions.h>
  11. #include <pcl/common/common.h>
  12. #endif
  13. #include <stddef.h>
  14. #include <math.h>
  15. class Point2D_tool
  16. {
  17. public:
  18. //极坐标
  19. struct Polar_coordinates
  20. {
  21. float angle=0; //弧度
  22. float distance=0; //距离
  23. };
  24. //极坐标的限定范围
  25. struct Polar_coordinates_box
  26. {
  27. float angle_min=0; //角度最小值
  28. float angle_max=0; //角度最大值
  29. float distance_min=0; //距离最小值
  30. float distance_max=0; //距离最大值
  31. };
  32. //平面坐标
  33. struct Point2D
  34. {
  35. float x=0; //x轴
  36. float y=0; //y轴
  37. };
  38. //平面坐标的限定范围
  39. struct Point2D_box
  40. {
  41. float x_min=0; //x轴最小值
  42. float x_max=0; //x轴最大值
  43. float y_min=0; //y轴最小值
  44. float y_max=0; //y轴最大值
  45. };
  46. //平面坐标的转换矩阵, 可以进行旋转和平移, 不能缩放
  47. struct Point2D_transform
  48. {
  49. float m00=1;
  50. float m01=0;
  51. float m02=0;
  52. float m10=0;
  53. float m11=1;
  54. float m12=0;
  55. };
  56. //极坐标 -> 平面坐标
  57. static bool Polar_coordinates_to_point2D(Point2D_tool::Polar_coordinates* p_polar_coordinates, Point2D_tool::Point2D* p_point2D);
  58. //平面坐标 -> 极坐标
  59. static bool Point2D_to_polar_coordinates(Point2D_tool::Point2D* p_point2D, Point2D_tool::Polar_coordinates* p_polar_coordinates);
  60. //判断极坐标点是否在限制范围
  61. static bool limit_with_polar_coordinates_box(float distance, float angle, Point2D_tool::Polar_coordinates_box* p_polar_coordinates_box);
  62. //判断平面坐标点是否在限制范围
  63. static bool limit_with_point2D_box(float x, float y, Point2D_tool::Point2D_box* p_point2D_box);
  64. #ifdef OPEN_PCL_FLAG
  65. //平面坐标的转换, 可以进行旋转和平移, 不能缩放
  66. static bool transform_with_translation_rotation(float* p_x_in, float* p_y_in, float* p_x_out, float* p_y_out,
  67. Point2D_tool::Point2D_transform* p_point2D_transform);
  68. static bool transform_with_translation_rotation(pcl::PointXYZ* p_point3D_in, pcl::PointXYZ* p_point3D_out,
  69. Point2D_tool::Point2D_transform* p_point2D_transform);
  70. static bool transform_with_translation_rotation(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in,
  71. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_out,
  72. Point2D_tool::Point2D_transform* p_point2D_transform);
  73. #endif
  74. };
  75. //极坐标 -> 平面坐标
  76. bool Point2D_tool::Polar_coordinates_to_point2D(Point2D_tool::Polar_coordinates* p_polar_coordinates, Point2D_tool::Point2D* p_point2D)
  77. {
  78. if ( p_polar_coordinates == NULL || p_point2D == NULL)
  79. {
  80. return false;
  81. }
  82. else
  83. {
  84. p_point2D->x = p_polar_coordinates->distance * cos(p_polar_coordinates->angle);
  85. p_point2D->y = p_polar_coordinates->distance * sin(p_polar_coordinates->angle);
  86. return true;
  87. }
  88. return true;
  89. }
  90. //平面坐标 -> 极坐标
  91. bool Point2D_tool::Point2D_to_polar_coordinates(Point2D_tool::Point2D* p_point2D, Point2D_tool::Polar_coordinates* p_polar_coordinates)
  92. {
  93. if ( p_polar_coordinates == NULL || p_point2D == NULL)
  94. {
  95. return false;
  96. }
  97. else
  98. {
  99. p_polar_coordinates->distance = sqrt(p_point2D->x * p_point2D->x + p_point2D->y * p_point2D->y);
  100. p_polar_coordinates->angle = atan(p_point2D->y / p_point2D->x);
  101. return true;
  102. }
  103. return true;
  104. }
  105. //判断极坐标点是否在限制范围
  106. bool Point2D_tool::limit_with_polar_coordinates_box(float distance, float angle, Point2D_tool::Polar_coordinates_box* p_polar_coordinates_box)
  107. {
  108. if ( p_polar_coordinates_box == NULL )
  109. {
  110. return false;
  111. }
  112. else
  113. {
  114. if ( angle >= p_polar_coordinates_box->angle_min &&
  115. angle <= p_polar_coordinates_box->angle_max &&
  116. distance >= p_polar_coordinates_box->distance_min &&
  117. distance <= p_polar_coordinates_box->distance_max )
  118. {
  119. return true;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. //判断平面坐标点是否在限制范围
  129. bool Point2D_tool::limit_with_point2D_box(float x, float y, Point2D_tool::Point2D_box* p_point2D_box)
  130. {
  131. if ( p_point2D_box == NULL )
  132. {
  133. return false;
  134. }
  135. else
  136. {
  137. if ( x >= p_point2D_box->x_min &&
  138. x <= p_point2D_box->x_max &&
  139. y >= p_point2D_box->y_min &&
  140. y <= p_point2D_box->y_max )
  141. {
  142. return true;
  143. }
  144. else
  145. {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. #ifdef OPEN_PCL_FLAG
  152. //平面坐标的转换, 可以进行旋转和平移, 不能缩放
  153. bool Point2D_tool::transform_with_translation_rotation(float* p_x_in, float* p_y_in, float* p_x_out, float* p_y_out,
  154. Point2D_tool::Point2D_transform* p_point2D_transform)
  155. {
  156. if ( p_x_in == NULL || p_x_in == NULL || p_x_out == NULL || p_y_out == NULL || p_point2D_transform == NULL)
  157. {
  158. return false;
  159. }
  160. else
  161. {
  162. *p_x_out = *p_x_in * p_point2D_transform->m00 + *p_y_in * p_point2D_transform->m01 + p_point2D_transform->m02;
  163. *p_y_out = *p_x_in * p_point2D_transform->m10 + *p_y_in * p_point2D_transform->m11 + p_point2D_transform->m12;
  164. return true;
  165. }
  166. return true;
  167. }
  168. bool Point2D_tool::transform_with_translation_rotation(pcl::PointXYZ* p_point3D_in, pcl::PointXYZ* p_point3D_out,
  169. Point2D_tool::Point2D_transform* p_point2D_transform)
  170. {
  171. if (p_point3D_in == NULL || p_point3D_out == NULL || p_point2D_transform == NULL)
  172. {
  173. return false;
  174. }
  175. else
  176. {
  177. p_point3D_out->x = p_point3D_in->x * p_point2D_transform->m00 + p_point3D_in->y * p_point2D_transform->m01 +
  178. p_point2D_transform->m02;
  179. p_point3D_out->y = p_point3D_in->x * p_point2D_transform->m10 + p_point3D_in->y * p_point2D_transform->m11 +
  180. p_point2D_transform->m12;
  181. return true;
  182. }
  183. return true;
  184. }
  185. bool Point2D_tool::transform_with_translation_rotation(pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in,
  186. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_out,
  187. Point2D_tool::Point2D_transform* p_point2D_transform)
  188. {
  189. if ( p_cloud_in.get() == NULL || p_cloud_out.get() == NULL || p_point2D_transform == NULL)
  190. {
  191. return false;
  192. }
  193. else
  194. {
  195. pcl::PointXYZ point;
  196. for (int i = 0; i < p_cloud_in->size(); ++i)
  197. {
  198. 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;
  199. 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;
  200. p_cloud_out->push_back(point);
  201. }
  202. return true;
  203. }
  204. return true;
  205. }
  206. #endif
  207. #endif //POINT2D_TOOL_H