pangolinViewer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Created by zx on 22-12-2.
  3. //
  4. #include "pangolinViewer.h"
  5. PangolinViewer* PangolinViewer::viewer_= nullptr;
  6. PangolinViewer* PangolinViewer::CreateViewer()
  7. {
  8. if (viewer_== nullptr)
  9. {
  10. viewer_=new PangolinViewer();
  11. }
  12. return viewer_;
  13. }
  14. PangolinViewer::~PangolinViewer()
  15. {
  16. if(thread_!= nullptr)
  17. {
  18. if (thread_->joinable())
  19. thread_->join();
  20. delete thread_;
  21. thread_= nullptr;
  22. }
  23. }
  24. PangolinViewer::PangolinViewer()
  25. {
  26. thread_=new std::thread(&PangolinViewer::loop,this);
  27. }
  28. void PangolinViewer::Join()
  29. {
  30. if(thread_!= nullptr)
  31. {
  32. if (thread_->joinable())
  33. thread_->join();
  34. }
  35. }
  36. void PangolinViewer::SetCallbacks(ViewerSpinOnceCallback spinOnce,StartLocateBtnCallback startBtn,
  37. StopBtnCallback stopBtn,FreqChangedCallback freqChanged)
  38. {
  39. spinOnce_callback_=spinOnce;
  40. startBtn_callback_=startBtn;
  41. stopBtn_callback_=stopBtn;
  42. freqChanged_callback_=freqChanged;
  43. }
  44. void PangolinViewer::Init()
  45. {
  46. pangolin::CreateWindowAndBind("Main", 1280, 760);
  47. // 3D Mouse handler requires depth testing to be enabled
  48. glEnable(GL_DEPTH_TEST);
  49. glEnable(GL_BLEND); // 启用混合
  50. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  51. text_font_ = new pangolin::GlFont("../config/tt0102m_.ttf", 30.0);
  52. s_cam_=new pangolin::OpenGlRenderState(
  53. pangolin::ProjectionMatrix(1480, 760, 840, 840, 480, 380, 0.1, 1000),
  54. pangolin::ModelViewLookAt(25, 0, 90, 25, 0, 20, pangolin::AxisY)
  55. );
  56. const int UI_WIDTH = 30 * pangolin::default_font().MaxWidth();
  57. d_cam_ = &pangolin::CreateDisplay()
  58. .SetBounds(0.0, 1.0, pangolin::Attach::Pix(UI_WIDTH), 1.0, 1280.0f / 760.0f)
  59. .SetHandler(new pangolin::Handler3D(*s_cam_));
  60. pangolin::CreatePanel("ui")
  61. .SetBounds(0.0, 1.0, 0.0, pangolin::Attach::Pix(UI_WIDTH));
  62. lvx_checkbox_=new pangolin::Var<bool>("ui.lvx_Checkbox", true, true);
  63. lvx_file_=new pangolin::Var<std::string> ("ui.lvx_file_String", "../map/bbt/start1.lvx");
  64. start_button_=new pangolin::Var<bool>("ui.start_Button", false, false);
  65. stop_button_ =new pangolin::Var<bool>("ui.stop_Button", false, false);
  66. freq_double_=new pangolin::Var<double>("ui.Freq_Double", 10, 1, 15);
  67. timequeue_string_=new pangolin::Var<std::string>("ui.tq_String", "timequeue_string");
  68. xyz_string_=new pangolin::Var<std::string>("ui.xyz_String", "xyz");
  69. rpy_string_=new pangolin::Var<std::string>("ui.rpy_String", "rpy");
  70. cost_string_=new pangolin::Var<std::string>("ui.final_cost", "cost");
  71. }
  72. void PangolinViewer::DrawCloud(PointCloud::Ptr cloud,double r,double g,double b,
  73. double alpha,double psize)
  74. {
  75. glPointSize(psize);
  76. glBegin(GL_POINTS);
  77. glColor4f(r, g, b, alpha);
  78. for (int i = 0; i < cloud->size(); ++i)
  79. {
  80. PointType pt = cloud->points[i];
  81. glVertex3d(pt.x , pt.y , pt.z );
  82. }
  83. glEnd();
  84. }
  85. void PangolinViewer::ShowRealtimePose(Eigen::Matrix4d pose)
  86. {
  87. double l=2;
  88. Eigen::Vector3d Ow=pose.topRightCorner(3, 1);
  89. Eigen::Vector3d Xw=pose.topLeftCorner(3,3)*(l*Eigen::Vector3d(1,0,0))+Ow;
  90. Eigen::Vector3d Yw=pose.topLeftCorner(3,3)*(l*Eigen::Vector3d(0,1,0))+Ow;
  91. Eigen::Vector3d Zw=pose.topLeftCorner(3,3)*(l*Eigen::Vector3d(0,0,1))+Ow;
  92. glLineWidth(5);
  93. glBegin(GL_LINES);
  94. glColor3f(1.0, 0.0, 0.0);
  95. glVertex3d(Ow[0], Ow[1], Ow[2]);
  96. glVertex3d(Xw[0], Xw[1], Xw[2]);
  97. glColor3f(0.0, 1.0, 0.0);
  98. glVertex3d(Ow[0], Ow[1], Ow[2]);
  99. glVertex3d(Yw[0], Yw[1], Yw[2]);
  100. glColor3f(0.0, 0.0, 1.0);
  101. glVertex3d(Ow[0], Ow[1], Ow[2]);
  102. glVertex3d(Zw[0], Zw[1], Zw[2]);
  103. glEnd();
  104. Eigen::Matrix3d rotation = pose.topLeftCorner(3, 3);
  105. Eigen::Vector3d eulerAngle = rotation.eulerAngles(0, 1, 2);
  106. Eigen::Vector3d t = pose.topRightCorner(3, 1);
  107. char rpy[64] = {0};
  108. sprintf(rpy,
  109. "%.3f,%.3f,%.3f",
  110. eulerAngle[0] * 180.0 / M_PI,
  111. eulerAngle[1] * 180.0 / M_PI,
  112. eulerAngle[2] * 180.0 / M_PI);
  113. char translation[64] = {0};
  114. sprintf(translation, "%.3f,%.3f,%.3f", t[0], t[1], t[2]);
  115. *xyz_string_ = translation;
  116. *rpy_string_ = rpy;
  117. }
  118. void PangolinViewer::ShowSlamCost(double cost)
  119. {
  120. char cost_str[64] = {0};
  121. sprintf(cost_str, "cost:%f", cost);
  122. *cost_string_ = cost_str;
  123. }
  124. void PangolinViewer::ShowSlamQueueTime(int queue,double tm)
  125. {
  126. char info_str[64]={0};
  127. sprintf(info_str,"time:%.5fs queue:%d",tm,queue);
  128. *timequeue_string_=info_str;
  129. }
  130. void PangolinViewer::loop(PangolinViewer* p)
  131. {
  132. if(p== nullptr)
  133. return ;
  134. p->Init();
  135. while (!pangolin::ShouldQuit())
  136. {
  137. // Clear entire screen
  138. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  139. if (pangolin::Pushed(*p->start_button_))
  140. {
  141. if (*p->lvx_checkbox_)
  142. {
  143. std::string file = p->lvx_file_->Get();
  144. if(p->startBtn_callback_!= nullptr)
  145. p->startBtn_callback_(file);
  146. }
  147. else
  148. {
  149. if(p->startBtn_callback_!= nullptr)
  150. p->startBtn_callback_("");
  151. }
  152. }
  153. if (pangolin::Pushed(*p->stop_button_))
  154. {
  155. if(p->stopBtn_callback_!= nullptr)
  156. p->stopBtn_callback_();
  157. }
  158. if (p->freq_double_->GuiChanged())
  159. {
  160. double publish_freq=p->freq_double_->Get();
  161. if(p->freqChanged_callback_!= nullptr)
  162. p->freqChanged_callback_(publish_freq);
  163. }
  164. if (p->d_cam_->IsShown())
  165. {
  166. p->d_cam_->Activate(*p->s_cam_);
  167. pangolin::glDrawAxis(3);//三维坐标轴,红——x轴,绿——y轴,蓝——z轴
  168. if(p->spinOnce_callback_!= nullptr)
  169. p->spinOnce_callback_(p);
  170. }
  171. pangolin::FinishFrame();
  172. std::this_thread::sleep_for(std::chrono::microseconds(10));
  173. }
  174. }