pose2d.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Created by zx on 22-12-1.
  3. //
  4. #ifndef LIO_LIVOX_CPP_MPC_POSE2D_H_
  5. #define LIO_LIVOX_CPP_MPC_POSE2D_H_
  6. #include <iostream>
  7. #include <atomic>
  8. #include <vector>
  9. #include <math.h>
  10. /*
  11. * 带direction的二维点,与x轴正方向逆时针为正方向
  12. */
  13. class Pose2d
  14. {
  15. public:
  16. Pose2d();
  17. Pose2d(float x,float y,float theta);
  18. Pose2d(const Pose2d& point)
  19. {
  20. m_x=point.x();
  21. m_y=point.y();
  22. m_theta=point.theta();
  23. }
  24. ~Pose2d();
  25. Pose2d& operator=(const Pose2d& point)
  26. {
  27. m_x=point.x();
  28. m_y=point.y();
  29. m_theta=point.theta();
  30. return *this;
  31. }
  32. /*
  33. * 两位姿相减,角度在-PI ,PI
  34. */
  35. Pose2d operator-(const Pose2d& pose)const
  36. {
  37. float x=m_x-pose.x();
  38. float y=m_y-pose.y();
  39. float theta=m_theta-pose.theta();
  40. if(theta<=-M_PI)
  41. theta+=2*M_PI;
  42. if(theta>M_PI)
  43. theta-=2*M_PI;
  44. return Pose2d(x,y,theta);
  45. }
  46. Pose2d operator+(const Pose2d& pose)const
  47. {
  48. float x=m_x+pose.x();
  49. float y=m_y+pose.y();
  50. float theta=m_theta+pose.theta();
  51. //转换到 (-PI,PI),下
  52. if(theta<=-M_PI)
  53. theta+=2*M_PI;
  54. if(theta>M_PI)
  55. theta-=2*M_PI;
  56. return Pose2d(x,y,theta);
  57. }
  58. static Pose2d abs(const Pose2d& pose)
  59. {
  60. Pose2d ret(fabs(pose.x()),fabs(pose.y()),fabs(pose.theta()));
  61. return ret;
  62. }
  63. bool operator<(const Pose2d& pose)const
  64. {
  65. return x()<pose.x()&&y()<pose.y()&&theta()<pose.theta();
  66. }
  67. Pose2d operator*(const Pose2d& t)const
  68. {
  69. Pose2d rpos=t.rotate(theta());
  70. return Pose2d(x()+rpos.x(),y()+rpos.y(),rpos.theta());
  71. }
  72. bool operator<=(const Pose2d& target)const
  73. {
  74. return x()<=target.x() && y()<=target.y() && theta()<=target.theta();
  75. }
  76. /*
  77. * 将点顺时针旋转theta
  78. */
  79. Pose2d rotate(float theta)const;
  80. //绕xy旋转
  81. Pose2d rotate(float ox,float oy,float theta)const;
  82. friend std::ostream& operator<<(std::ostream &out,const Pose2d& point);
  83. const float x() const {return m_x;};
  84. const float y() const {return m_y;};
  85. const float theta() const {return m_theta;}
  86. float& mutable_theta(){ return m_theta;}
  87. const float gridient()const;
  88. static float distance(const Pose2d& pose1,const Pose2d& pose2);
  89. static Pose2d relativePose(const Pose2d& world_pose,const Pose2d& axis_pose);
  90. //返回值(-PI,PI]
  91. static float vector2yaw(float x,float y);
  92. static std::vector<Pose2d> generate_rectangle_vertexs(const Pose2d& pose,float w,float h){
  93. std::vector<Pose2d> poses;
  94. //std::cout<<" obs:"<<pose<<", oprator* "<<pose*Pose2d(w/2,h/2,pose.theta())<<",wh:"<<w<<","<<h<<std::endl;
  95. poses.push_back(pose*Pose2d(w/2,h/2,pose.theta()));
  96. poses.push_back(pose*Pose2d(w/2,-h/2,pose.theta()));
  97. poses.push_back(pose*Pose2d(-w/2,h/2,pose.theta()));
  98. poses.push_back(pose*Pose2d(-w/2,-h/2,pose.theta()));
  99. poses.push_back(pose*Pose2d(-w/2,0,pose.theta()));
  100. poses.push_back(pose*Pose2d(w/2,0,pose.theta()));
  101. poses.push_back(pose*Pose2d(0,-h/2.0,pose.theta()));
  102. poses.push_back(pose*Pose2d(0,h/2.0,pose.theta()));
  103. return poses;
  104. }
  105. protected:
  106. float m_x;
  107. float m_y;
  108. float m_theta; //梯度角,与x轴正方形逆时针为正,单位弧度
  109. };
  110. #endif //LIO_LIVOX_CPP_MPC_POSE2D_H_