pose2d.h 2.7 KB

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