pose2d.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #include <math.h>
  9. Pose2d::Pose2d()
  10. :m_x(0),m_y(0),m_theta(0)
  11. {
  12. }
  13. Pose2d::Pose2d(float x,float y,float theta)
  14. :m_x(x),m_y(y),m_theta(theta)
  15. {
  16. }
  17. Pose2d::~Pose2d()
  18. {}
  19. std::ostream& operator<<(std::ostream &out,const Pose2d& point)
  20. {
  21. out<<"[x:"<<point.x()<<", y:"<<point.y()<<", theta:"<<point.theta()*180.0/M_PI<<"°]";
  22. return out;
  23. }
  24. const float Pose2d::gridient() const
  25. {
  26. double gradient=tanf(m_theta);
  27. if(fabs(gradient)>200)
  28. return 200.0*(gradient/fabs(gradient));
  29. return gradient;
  30. }
  31. Pose2d Pose2d::PoseByPose(const Pose2d& world_pose,const Pose2d& axis_pose)
  32. {
  33. Pose2d diff=world_pose-axis_pose;
  34. Pose2d nPose=diff.rotate(-axis_pose.theta());
  35. return Pose2d(nPose.x(),nPose.y(),diff.theta());
  36. }
  37. float Pose2d::distance(const Pose2d& pose1,const Pose2d& pose2)
  38. {
  39. Pose2d offset=pose1-pose2;
  40. return sqrt(offset.x()*offset.x()+offset.y()*offset.y());
  41. }
  42. Pose2d Pose2d::rotate(float theta)const
  43. {
  44. double cs=cos(theta);
  45. double sn=sin(theta);
  46. float x=cs*this->x()-sn*this->y();
  47. float y=sn*this->x()+cs*this->y();
  48. float new_theta=this->theta()+theta;
  49. return Pose2d(x,y,new_theta);
  50. }