pangolinViewer.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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::SetNavigationCallbacks(StartBtnCallback startBtn,
  37. PauseBtnCallback pauseBtn,
  38. StopBtnCallback stopBtn)
  39. {
  40. naviagtion_startBtn_callback_=startBtn;
  41. naviagtion_pauseBtn_callback_=pauseBtn;
  42. navigation_stopBtn_callback_=stopBtn;
  43. }
  44. void PangolinViewer::SetCallbacks(ViewerSpinOnceCallback spinOnce,DijkstraBtnCallback dijkstra)
  45. {
  46. spinOnce_callback_=spinOnce;
  47. dijkstraBtn_callback_=dijkstra;
  48. }
  49. void PangolinViewer::Init()
  50. {
  51. pangolin::CreateWindowAndBind("Main", 1280, 760);
  52. // 3D Mouse handler requires depth testing to be enabled
  53. glEnable(GL_DEPTH_TEST);
  54. glEnable(GL_BLEND); // 启用混合
  55. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  56. text_font_ = new pangolin::GlFont("../config/tt0102m_.ttf", 30.0);
  57. s_cam_=new pangolin::OpenGlRenderState(
  58. pangolin::ProjectionMatrix(1480, 760, 840, 840, 480, 380, 0.1, 1000),
  59. pangolin::ModelViewLookAt(25, 0, 90, 25, 0, 20, pangolin::AxisY)
  60. );
  61. const int UI_WIDTH = 30 * pangolin::default_font().MaxWidth();
  62. d_cam_ = &pangolin::CreateDisplay()
  63. .SetBounds(0.0, 1.0, pangolin::Attach::Pix(UI_WIDTH), 1.0, 1280.0f / 760.0f)
  64. .SetHandler(new pangolin::Handler3D(*s_cam_));
  65. pangolin::CreatePanel("ui")
  66. .SetBounds(0.0, 1.0, 0.0, pangolin::Attach::Pix(UI_WIDTH));
  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. velocity_string_=new pangolin::Var<std::string>("ui.velocity", "");
  71. beg_Int_=new pangolin::Var<int>("ui.beg", 1, 1, 7);
  72. end_Int_=new pangolin::Var<int>("ui.end", 5, 1, 7);
  73. v_=new pangolin::Var<double>("ui.v", 1.0, -2.0,2.0 );
  74. a_=new pangolin::Var<double>("ui.a", 1.0, -10.0, 10.0);
  75. path_string_=new pangolin::Var<std::string>("ui.path", "cost");
  76. distance_string_=new pangolin::Var<std::string>("ui.distance", "distance");
  77. pangolin::Var<std::function<void(void)>> solve_view("ui.Solve", [&]()
  78. {
  79. if(dijkstraBtn_callback_!= nullptr)
  80. {
  81. auto start_tm = std::chrono::system_clock::now();
  82. int node_beg = beg_Int_->Get();
  83. int node_end = end_Int_->Get();
  84. float distance=std::numeric_limits<float>().max();
  85. std::vector<int> shortestPath;
  86. if(dijkstraBtn_callback_(node_beg,node_end,distance,shortestPath))
  87. {
  88. shortest_path_=shortestPath;
  89. char path_str[64] = {0};
  90. sprintf(path_str, "%d-%d:", node_beg, node_end);
  91. for (int i = shortestPath.size() - 1; i >= 0; --i)
  92. {
  93. sprintf(path_str + strlen(path_str), "%d-", shortestPath[i]);
  94. }
  95. sprintf(path_str + strlen(path_str), "%d", node_end);
  96. *path_string_=path_str;
  97. char cost[62] = {0};
  98. sprintf(cost, "d(%d-%d):%f", node_beg, node_end, distance);
  99. *distance_string_ = cost;
  100. auto end_tm = std::chrono::system_clock::now();
  101. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_tm - start_tm);
  102. double time = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  103. char tm[32] = {0};
  104. sprintf(tm, "time:%.5fs", time);
  105. *timequeue_string_ = tm;
  106. }
  107. }
  108. });
  109. pangolin::Var<std::function<void(void)>> navigation_beg("ui.nav_Beg", [&]()
  110. {
  111. if(naviagtion_startBtn_callback_!= nullptr)
  112. naviagtion_startBtn_callback_();
  113. });
  114. pangolin::Var<std::function<void(void)>> navigation_pause("ui.nav_PAUSE", [&]()
  115. {
  116. if(naviagtion_pauseBtn_callback_!= nullptr)
  117. naviagtion_pauseBtn_callback_();
  118. });
  119. pangolin::Var<std::function<void(void)>> navigation_end("ui.nav_End", [&]()
  120. {
  121. if(navigation_stopBtn_callback_!= nullptr)
  122. navigation_stopBtn_callback_();
  123. });
  124. }
  125. void PangolinViewer::DrawCloud(PointCloud::Ptr cloud,double r,double g,double b,
  126. double alpha,double psize)
  127. {
  128. glPointSize(psize);
  129. glBegin(GL_POINTS);
  130. glColor4f(r, g, b, alpha);
  131. for (int i = 0; i < cloud->size(); ++i)
  132. {
  133. PointType pt = cloud->points[i];
  134. glVertex3d(pt.x , pt.y , pt.z );
  135. }
  136. glEnd();
  137. }
  138. void PangolinViewer::DrawTrajectory(const Trajectory& traj,
  139. double r,double g,double b,double ptsize)
  140. {
  141. glPointSize(ptsize);
  142. glBegin(GL_POINTS);
  143. glColor4f(r, g, b, 1);
  144. for (int i = 0; i < traj.size(); ++i)
  145. {
  146. Pose2d pt =traj[i];
  147. glVertex3d(pt.x() , pt.y() , 0);
  148. }
  149. glEnd();
  150. }
  151. void PangolinViewer::DrawDijkastraMap(PathMap& map,std::vector<int> path)
  152. {
  153. //绘制顶点
  154. std::map<int,PathMap::MapNode> nodes=map.GetNodes();
  155. std::map<int,PathMap::MapNode>::iterator it=nodes.begin();
  156. float z=0;
  157. glPointSize(10.0);
  158. // 开始画点
  159. glBegin ( GL_POINTS );
  160. glColor3f(1, 0, 1);
  161. for(it;it!=nodes.end();it++)
  162. {
  163. PathMap::MapNode node=it->second;
  164. // 设置点颜色,rgb值,float类型
  165. glVertex3f(node.x, node.y, z);
  166. }
  167. glEnd();
  168. glLineWidth(5);
  169. glBegin(GL_LINES);
  170. //绘制边
  171. glColor3f(1.0, 0.9, 0.0);
  172. std::vector<PathMap::MapEdge> edges=map.GetEdges();
  173. for(int i=0;i<edges.size();++i)
  174. {
  175. PathMap::MapEdge edge=edges[i];
  176. float x1,y1,x2,y2;
  177. map.GetVertexPos(edge.id1,x1,y1);
  178. map.GetVertexPos(edge.id2,x2,y2);
  179. glVertex3d(x1,y1, z);
  180. glVertex3d(x2,y2,z);
  181. }
  182. glEnd();
  183. if(path.size()>0)
  184. {
  185. glLineWidth(10);
  186. glBegin(GL_LINE_STRIP);
  187. glColor3f(0, 0, 1);
  188. for (int i = 0; i < path.size() ; ++i)
  189. {
  190. float x1, y1;
  191. if (map.GetVertexPos(path[i], x1, y1))
  192. {
  193. glVertex3d(x1, y1, z);
  194. }
  195. }
  196. glEnd();
  197. }
  198. glColor3f(1,1,1);
  199. it=nodes.begin();
  200. for(it;it!=nodes.end();it++)
  201. {
  202. PathMap::MapNode node=it->second;
  203. char idx[16]={0};
  204. sprintf(idx,"%d",it->first);
  205. text_font_->Text(idx).Draw(node.x,node.y,z);
  206. }
  207. }
  208. void PangolinViewer::ShowRealtimeStatu(Eigen::Matrix4d pose,double velocity,double angular)
  209. {
  210. double l=2;
  211. Eigen::Vector3d Ow=pose.topRightCorner(3, 1);
  212. Eigen::Vector3d Xw=pose.topLeftCorner(3,3)*(l*Eigen::Vector3d(1,0,0))+Ow;
  213. Eigen::Vector3d Yw=pose.topLeftCorner(3,3)*(l*Eigen::Vector3d(0,1,0))+Ow;
  214. Eigen::Vector3d Zw=pose.topLeftCorner(3,3)*(l*Eigen::Vector3d(0,0,1))+Ow;
  215. glLineWidth(5);
  216. glBegin(GL_LINES);
  217. glColor3f(1.0, 0.0, 0.0);
  218. glVertex3d(Ow[0], Ow[1], Ow[2]);
  219. glVertex3d(Xw[0], Xw[1], Xw[2]);
  220. glColor3f(0.0, 1.0, 0.0);
  221. glVertex3d(Ow[0], Ow[1], Ow[2]);
  222. glVertex3d(Yw[0], Yw[1], Yw[2]);
  223. glColor3f(0.0, 0.0, 1.0);
  224. glVertex3d(Ow[0], Ow[1], Ow[2]);
  225. glVertex3d(Zw[0], Zw[1], Zw[2]);
  226. glEnd();
  227. Eigen::Matrix3d rotation = pose.topLeftCorner(3, 3);
  228. Eigen::Vector3d eulerAngle = rotation.eulerAngles(0, 1, 2);
  229. Eigen::Vector3d t = pose.topRightCorner(3, 1);
  230. char rpy[64] = {0};
  231. sprintf(rpy,
  232. "%.3f,%.3f,%.3f",
  233. eulerAngle[0] * 180.0 / M_PI,
  234. eulerAngle[1] * 180.0 / M_PI,
  235. eulerAngle[2] * 180.0 / M_PI);
  236. char translation[64] = {0};
  237. sprintf(translation, "%.3f,%.3f,%.3f", t[0], t[1], t[2]);
  238. *xyz_string_ = translation;
  239. *rpy_string_ = rpy;
  240. char twist[64]={0};
  241. sprintf(twist,"vel:%.5f, \tagl:%.5f\n",velocity,angular);
  242. *velocity_string_=twist;
  243. }
  244. void PangolinViewer::loop(PangolinViewer* p)
  245. {
  246. if(p== nullptr)
  247. return ;
  248. p->Init();
  249. while (!pangolin::ShouldQuit())
  250. {
  251. // Clear entire screen
  252. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  253. /*if (pangolin::Pushed(*p->start_button_))
  254. {
  255. if (*p->lvx_checkbox_)
  256. {
  257. std::string file = p->lvx_file_->Get();
  258. }
  259. else
  260. {
  261. }
  262. }
  263. if (pangolin::Pushed(*p->stop_button_))
  264. {
  265. }
  266. if (p->freq_double_->GuiChanged())
  267. {
  268. double publish_freq=p->freq_double_->Get();
  269. }*/
  270. if (p->d_cam_->IsShown())
  271. {
  272. p->d_cam_->Activate(*p->s_cam_);
  273. pangolin::glDrawAxis(3);//三维坐标轴,红——x轴,绿——y轴,蓝——z轴
  274. if(p->spinOnce_callback_!= nullptr)
  275. p->spinOnce_callback_(p);
  276. }
  277. pangolin::FinishFrame();
  278. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  279. }
  280. }