pose2d.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * 单独设置属性
  34. */
  35. void set_theta(float theta){ m_theta = theta; }
  36. void set_x(float x){ m_x = x; }
  37. void set_y(float y){ m_y = y; }
  38. /*
  39. * 两位姿相减,角度在-PI ,PI
  40. */
  41. Pose2d operator-(const Pose2d& pose)const
  42. {
  43. float x=m_x-pose.x();
  44. float y=m_y-pose.y();
  45. float theta=m_theta-pose.theta();
  46. if(theta<=-M_PI)
  47. theta+=2*M_PI;
  48. if(theta>M_PI)
  49. theta-=2*M_PI;
  50. return Pose2d(x,y,theta);
  51. }
  52. Pose2d operator+(const Pose2d& pose)const
  53. {
  54. float x=m_x+pose.x();
  55. float y=m_y+pose.y();
  56. float theta=m_theta+pose.theta();
  57. //转换到 (-PI,PI),下
  58. if(theta<=-M_PI)
  59. theta+=2*M_PI;
  60. if(theta>M_PI)
  61. theta-=2*M_PI;
  62. return Pose2d(x,y,theta);
  63. }
  64. static Pose2d abs(const Pose2d& pose)
  65. {
  66. Pose2d ret(fabs(pose.x()),fabs(pose.y()),fabs(pose.theta()));
  67. return ret;
  68. }
  69. bool operator<(const Pose2d& pose)const
  70. {
  71. return x()<pose.x()&&y()<pose.y()&&theta()<pose.theta();
  72. }
  73. Pose2d operator*(const Pose2d& t)const
  74. {
  75. Pose2d rpos=t.rotate(theta());
  76. return Pose2d(x()+rpos.x(),y()+rpos.y(),rpos.theta());
  77. }
  78. bool operator<=(const Pose2d& target)const
  79. {
  80. return x()<=target.x() && y()<=target.y() && theta()<=target.theta();
  81. }
  82. /*
  83. * 将点逆时针旋转theta
  84. */
  85. Pose2d rotate(float theta)const;
  86. //绕xy旋转
  87. Pose2d rotate(float ox,float oy,float theta)const;
  88. friend std::ostream& operator<<(std::ostream &out,const Pose2d& point);
  89. const float x() const {return m_x;};
  90. const float y() const {return m_y;};
  91. const float theta() const {return m_theta;}
  92. float& mutable_theta(){ return m_theta;}
  93. const float gridient()const;
  94. static float distance(const Pose2d& pose1,const Pose2d& pose2);
  95. //前者在后者坐标系下的位姿
  96. static Pose2d relativePose(const Pose2d& world_pose,const Pose2d& axis_pose);
  97. //返回值(-PI,PI]
  98. static float vector2yaw(float x,float y);
  99. static std::vector<Pose2d> generate_rectangle_vertexs(const Pose2d& pose,float w,float h){
  100. std::vector<Pose2d> poses;
  101. //std::cout<<" obs:"<<pose<<", oprator* "<<pose*Pose2d(w/2,h/2,pose.theta())<<",wh:"<<w<<","<<h<<std::endl;
  102. poses.push_back(pose*Pose2d(w/2,h/2,pose.theta()));
  103. poses.push_back(pose*Pose2d(w/2,-h/2,pose.theta()));
  104. poses.push_back(pose*Pose2d(-w/2,h/2,pose.theta()));
  105. poses.push_back(pose*Pose2d(-w/2,-h/2,pose.theta()));
  106. poses.push_back(pose*Pose2d(-w/2,0,pose.theta()));
  107. poses.push_back(pose*Pose2d(w/2,0,pose.theta()));
  108. poses.push_back(pose*Pose2d(0,-h/2.0,pose.theta()));
  109. poses.push_back(pose*Pose2d(0,h/2.0,pose.theta()));
  110. return poses;
  111. }
  112. //public:
  113. // float m_diffYaw1;
  114. // float m_diffYaw2;
  115. protected:
  116. float m_x;
  117. float m_y;
  118. float m_theta; //梯度角,与x轴正方形逆时针为正,单位弧度
  119. };
  120. #endif //LIO_LIVOX_CPP_MPC_POSE2D_H_