pose2d.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // Created by zx on 22-12-1.
  3. //
  4. //
  5. // Created by zx on 2020/9/9.
  6. //
  7. #include "pose2d.h"
  8. Pose2d::Pose2d()
  9. :m_x(0),m_y(0),m_theta(0)
  10. {
  11. }
  12. Pose2d::Pose2d(float x,float y,float theta)
  13. :m_x(x),m_y(y),m_theta(theta)
  14. {
  15. }
  16. Pose2d::~Pose2d()
  17. {}
  18. std::ostream& operator<<(std::ostream &out,const Pose2d& point)
  19. {
  20. out<<"[x:"<<point.x()<<", y:"<<point.y()<<", theta:"<<point.theta()*180.0/M_PI<<"°]";
  21. return out;
  22. }
  23. const float Pose2d::gridient() const
  24. {
  25. double gradient=tanf(m_theta);
  26. if(fabs(gradient)>200)
  27. return 200.0*(gradient/fabs(gradient));
  28. return gradient;
  29. }
  30. Pose2d Pose2d::PoseByPose(const Pose2d& world_pose,const Pose2d& axis_pose)
  31. {
  32. Pose2d diff=world_pose-axis_pose;
  33. Pose2d nPose=diff.rotate(-axis_pose.theta());
  34. return Pose2d(nPose.x(),nPose.y(),diff.theta());
  35. }
  36. float Pose2d::distance(const Pose2d& pose1,const Pose2d& pose2)
  37. {
  38. Pose2d offset=pose1-pose2;
  39. return sqrt(offset.x()*offset.x()+offset.y()*offset.y());
  40. }
  41. Pose2d Pose2d::rotate(float theta)const
  42. {
  43. double cs=cos(theta);
  44. double sn=sin(theta);
  45. float x=cs*this->x()-sn*this->y();
  46. float y=sn*this->x()+cs*this->y();
  47. float new_theta=this->theta()+theta;
  48. //转换到 [-pi/2, pi/2]下
  49. int n=int(new_theta/(M_PI));
  50. if(new_theta<-M_PI/2)
  51. {
  52. new_theta += M_PI*(n+1);
  53. }
  54. if(new_theta>M_PI/2)
  55. {
  56. new_theta -= M_PI*(n+1);
  57. }
  58. return Pose2d(x,y,new_theta);
  59. }