pangolinViewer.cpp 9.8 KB

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