mainwindow.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <pcl/io/ply_io.h>
  4. #include <pcl/io/obj_io.h>
  5. static pose gCurPose;
  6. #include <cmath>
  7. struct Quaternion {
  8. double w, x, y, z;
  9. };
  10. struct EulerAngles {
  11. double roll, pitch, yaw;
  12. };
  13. EulerAngles ToEulerAngles(Quaternion q) {
  14. EulerAngles angles;
  15. // roll (x-axis rotation)
  16. double sinr_cosp = 2 * (q.w * q.x + q.y * q.z);
  17. double cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
  18. angles.roll = std::atan2(sinr_cosp, cosr_cosp);
  19. // pitch (y-axis rotation)
  20. double sinp = 2 * (q.w * q.y - q.z * q.x);
  21. if (std::abs(sinp) >= 1)
  22. angles.pitch = std::copysign(M_PI / 2, sinp); // use 90 degrees if out of range
  23. else
  24. angles.pitch = std::asin(sinp);
  25. // yaw (z-axis rotation)
  26. double siny_cosp = 2 * (q.w * q.z + q.x * q.y);
  27. double cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z);
  28. angles.yaw = std::atan2(siny_cosp, cosy_cosp);
  29. return angles;
  30. }
  31. Quaternion ToQuaternion(double yaw, double pitch, double roll) // yaw (Z), pitch (Y), roll (X)
  32. {
  33. // Abbreviations for the various angular functions
  34. double cy = cos(yaw * 0.5);
  35. double sy = sin(yaw * 0.5);
  36. double cp = cos(pitch * 0.5);
  37. double sp = sin(pitch * 0.5);
  38. double cr = cos(roll * 0.5);
  39. double sr = sin(roll * 0.5);
  40. Quaternion q;
  41. q.w = cy * cp * cr + sy * sp * sr;
  42. q.x = cy * cp * sr - sy * sp * cr;
  43. q.y = sy * cp * sr + cy * sp * cr;
  44. q.z = sy * cp * cr - cy * sp * sr;
  45. return q;
  46. }
  47. double mpos_x = 0;
  48. void viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
  49. {
  50. //设置背景颜色
  51. viewer.setBackgroundColor(0.0,0.0,0.0);
  52. viewer.resetCamera();
  53. viewer.addCube(-0.9,0.9,-2.3,2.3,-1.9,-0.4,0.0,0.0,1.0,"car",0);
  54. }
  55. void viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
  56. {
  57. std::cout<<std::chrono::system_clock::now().time_since_epoch().count()/1000000<<" psycho."<<std::endl;
  58. viewer.removeShape("car");
  59. Eigen::Vector3f trans;
  60. Quaternion q = ToQuaternion(gCurPose.yaw,gCurPose.pitch,gCurPose.roll);
  61. Eigen::Quaternionf quat(q.w,q.x,q.y,q.z);
  62. trans(0) = gCurPose.x;trans(1) = gCurPose.y;trans(2) = gCurPose.z;
  63. // viewer.addCube(mpos_x+20 -0.9,mpos_x + 20+0.9,-2.3,2.3,-1.9,-0.4,0.0,0.0,1.0,"car",0);
  64. viewer.addCube(trans,quat,6.0,3.0,1.0,"car");
  65. double yaw_calc = M_PI/2.0 - gCurPose.yaw;
  66. viewer.setCameraPosition(gCurPose.x,gCurPose.y,100,gCurPose.x,gCurPose.y,gCurPose.z,sin(yaw_calc),cos(yaw_calc),0);
  67. std::stringstream ss;
  68. // viewer.setCameraPosition(mpos_x,0,30,mpos_x + 20,0,0,0,0,0);
  69. // ss << "Once per viewer loop: " << count++;
  70. }
  71. MainWindow::MainWindow(QWidget *parent) :
  72. QMainWindow(parent),
  73. ui(new Ui::MainWindow)
  74. {
  75. ui->setupUi(this);
  76. mCurPose.x = 0; mCurPose.y = 0; mCurPose.z = 0;
  77. mCurPose.yaw = 0; mCurPose.pitch = 0; mCurPose.roll = 0;
  78. gCurPose = mCurPose;
  79. \
  80. mpthreadpcd = new std::thread(&MainWindow::threadpcdview,this);
  81. }
  82. MainWindow::~MainWindow()
  83. {
  84. mbRun = false;
  85. mpthreadpcd->join();
  86. delete ui;
  87. }
  88. void MainWindow::threadpcdview()
  89. {
  90. mpviewer = new pcl::visualization::CloudViewer("Cloud View");
  91. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  92. new pcl::PointCloud<pcl::PointXYZI>());
  93. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud2(
  94. new pcl::PointCloud<pcl::PointXYZI>());
  95. pcl::io::loadPCDFile<pcl::PointXYZI>("/home/yuchuli/map/map.pcd",*point_cloud);
  96. // pcl::io::loadOBJFile("/home/yuchuli/car.obj",*point_cloud);
  97. // pcl::io::loadPLYFile("/home/yuchuli/car.ply",*point_cloud);
  98. mpviewer->showCloud(point_cloud);
  99. //This will only get called once
  100. mpviewer->runOnVisualizationThreadOnce (viewerOneOff);
  101. mpviewer->runOnVisualizationThread (viewerPsycho);
  102. while((!mpviewer->wasStopped()) && mbRun)
  103. {
  104. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  105. }
  106. delete mpviewer;
  107. }
  108. void MainWindow::on_pushButton_Test_clicked()
  109. {
  110. double x = ui->lineEdit_x->text().toDouble();
  111. double y = ui->lineEdit_y->text().toDouble();
  112. double z = ui->lineEdit_z->text().toDouble();
  113. double yaw = ui->lineEdit_yaw->text().toDouble();
  114. double pitch = ui->lineEdit_pitch->text().toDouble();
  115. double roll = ui->lineEdit_roll->text().toDouble();
  116. mCurPose.x = x;
  117. mCurPose.y = y;
  118. mCurPose.z = z;
  119. mCurPose.yaw = yaw;
  120. mCurPose.pitch = pitch;
  121. mCurPose.roll = roll;
  122. gCurPose = mCurPose;
  123. }