trans_coor.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <math.h>
  3. #include <pcl/point_cloud.h>
  4. #include <pcl/point_types.h>
  5. //template <typename T>
  6. class CoordinateTransformation {
  7. public:
  8. struct Point2D {
  9. float x;
  10. float y;
  11. };
  12. struct transInfo2D {
  13. Point2D move;
  14. float angle;
  15. };
  16. struct transInfo3D {
  17. pcl::PointXYZ move;
  18. float roll;
  19. float pitch;
  20. float yaw;
  21. transInfo3D() = default;
  22. transInfo3D(pcl::PointXYZ move_, float roll_, float pitch_, float yaw_): move(move_), roll(roll_), pitch(pitch_) {}
  23. };
  24. CoordinateTransformation() = default;
  25. ~CoordinateTransformation() = default;
  26. void setTranInfo(const transInfo2D &trans, bool radian = false) {
  27. }
  28. void setTranInfo(const transInfo3D &trans, bool radian = false) {
  29. trans3D = true;
  30. float cosYaw, sinYaw, cosPitch, sinPitch, cosRoll, sinRoll;
  31. if (!radian) {
  32. cosYaw = cos(trans.yaw * float(M_PI) / 180.0f);
  33. sinYaw = sin(trans.yaw * float(M_PI) / 180.0f);
  34. cosPitch = cos(trans.pitch * float(M_PI) / 180.0f);
  35. sinPitch = sin(trans.pitch * float(M_PI) / 180.0f);
  36. cosRoll = cos(trans.roll * float(M_PI) / 180.0f);
  37. sinRoll = sin(trans.roll * float(M_PI) / 180.0f);
  38. } else {
  39. cosYaw = cos(trans.yaw);
  40. sinYaw = sin(trans.yaw);
  41. cosPitch = cos(trans.pitch);
  42. sinPitch = sin(trans.pitch);
  43. cosRoll = cos(trans.roll);
  44. sinRoll = sin(trans.roll);
  45. }
  46. trans3D_move = trans.move;
  47. rotationMatrix3D[0][0] = cosYaw * cosPitch;
  48. rotationMatrix3D[0][1] = cosYaw * sinPitch * sinRoll - sinYaw * cosRoll;
  49. rotationMatrix3D[0][2] = cosYaw * sinPitch * cosRoll + sinYaw * sinRoll;
  50. rotationMatrix3D[1][0] = sinYaw * cosPitch;
  51. rotationMatrix3D[1][1] = sinYaw * sinPitch * sinRoll + cosYaw * cosRoll;
  52. rotationMatrix3D[1][2] = sinYaw * sinPitch * cosRoll - cosYaw * sinRoll;
  53. rotationMatrix3D[2][0] = -sinPitch;
  54. rotationMatrix3D[2][1] = cosPitch * sinRoll;
  55. rotationMatrix3D[2][2] = cosPitch * cosRoll;
  56. }
  57. void transPoint(const Point2D &in, Point2D &out) {
  58. }
  59. void transPoint(const pcl::PointXYZ &in, pcl::PointXYZ &out) {
  60. trans3DPoint(in, out);
  61. }
  62. private:
  63. bool trans3DPoint(const pcl::PointXYZ &in, pcl::PointXYZ &out) {
  64. if (trans3D) {
  65. out.x = rotationMatrix3D[0][0] * in.x + rotationMatrix3D[0][1] * in.y + rotationMatrix3D[0][2] * in.z + trans3D_move.x;
  66. out.y = rotationMatrix3D[1][0] * in.x + rotationMatrix3D[1][1] * in.y + rotationMatrix3D[1][2] * in.z + trans3D_move.y;
  67. out.z = rotationMatrix3D[2][0] * in.x + rotationMatrix3D[2][1] * in.y + rotationMatrix3D[2][2] * in.z + trans3D_move.z;
  68. }
  69. return trans3D;
  70. }
  71. private:
  72. bool trans2D = false;
  73. bool trans3D = false;
  74. Point2D trans2D_move{};
  75. pcl::PointXYZ trans3D_move{0, 0, 0};
  76. float rotationMatrix2D[1] = {0};
  77. float rotationMatrix3D[3][3] = {0};
  78. };