dijkstraSample.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // Created by zx on 22-11-29.
  3. //
  4. #include "dijkstra/dijkstra.h"
  5. #include <pangolin/var/var.h>
  6. #include <pangolin/var/varextra.h>
  7. #include <pangolin/gl/gl.h>
  8. #include <pangolin/gl/gldraw.h>
  9. #include <pangolin/display/display.h>
  10. #include <pangolin/display/view.h>
  11. #include <pangolin/display/widgets.h>
  12. #include <pangolin/display/default_font.h>
  13. #include <pangolin/handler/handler.h>
  14. int start=1;
  15. int end=5;
  16. void DrawPath(PathMap& map,std::vector<int> path)
  17. {
  18. float z=0;
  19. }
  20. void DrawMap(PathMap& map,std::vector<int> path,pangolin::GlFont* text_font)
  21. {
  22. //绘制顶点
  23. std::map<int,PathMap::MapNode> nodes=map.GetNodes();
  24. std::map<int,PathMap::MapNode>::iterator it=nodes.begin();
  25. float z=0;
  26. glPointSize(50.0);
  27. // 开始画点
  28. glBegin ( GL_POINTS );
  29. glColor3f(0, 1, 0);
  30. for(it;it!=nodes.end();it++)
  31. {
  32. PathMap::MapNode node=it->second;
  33. // 设置点颜色,rgb值,float类型
  34. glVertex3f(node.x, node.y, z);
  35. }
  36. glEnd();
  37. glLineWidth(5);
  38. glBegin(GL_LINES);
  39. //绘制边
  40. glColor3f(1.0, 1.0, 0.0);
  41. std::vector<PathMap::MapEdge> edges=map.GetEdges();
  42. for(int i=0;i<edges.size();++i)
  43. {
  44. PathMap::MapEdge edge=edges[i];
  45. float x1,y1,x2,y2;
  46. map.GetVertexPos(edge.id1,x1,y1);
  47. map.GetVertexPos(edge.id2,x2,y2);
  48. glVertex3d(x1,y1, z);
  49. glVertex3d(x2,y2,z);
  50. }
  51. glEnd();
  52. if(path.size()>0)
  53. {
  54. glLineWidth(10);
  55. glBegin(GL_LINE_STRIP);
  56. glColor3f(0, 0, 1);
  57. for (int i = 0; i < path.size() ; ++i)
  58. {
  59. float x1, y1;
  60. if (map.GetVertexPos(path[i], x1, y1))
  61. {
  62. glVertex3d(x1, y1, z);
  63. }
  64. }
  65. glEnd();
  66. }
  67. glColor3f(1,1,1);
  68. it=nodes.begin();
  69. for(it;it!=nodes.end();it++)
  70. {
  71. PathMap::MapNode node=it->second;
  72. char idx[16]={0};
  73. sprintf(idx,"%d",it->first);
  74. text_font->Text(idx).Draw(node.x,node.y,z);
  75. }
  76. }
  77. int main()
  78. {
  79. pangolin::GlFont *text_font = new pangolin::GlFont("../config/tt0102m_.ttf", 30.0);
  80. pangolin::CreateWindowAndBind("Main", 1280, 960);
  81. // 3D Mouse handler requires depth testing to be enabled
  82. glEnable(GL_DEPTH_TEST);
  83. glEnable(GL_BLEND); // 启用混合
  84. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  85. // Define Camera Render Object (for view / scene browsing)
  86. pangolin::OpenGlRenderState s_cam(
  87. pangolin::ProjectionMatrix(1280, 960, 840, 840, 640, 640, 0.2, 100),
  88. pangolin::ModelViewLookAt(5, 3, 8, 5, 3, 0, pangolin::AxisY)
  89. );
  90. // Choose a sensible left UI Panel width based on the width of 20
  91. // charectors from the default font.
  92. const int UI_WIDTH = 30 * pangolin::default_font().MaxWidth();
  93. // Add named OpenGL viewport to window and provide 3D Handler
  94. pangolin::View &d_cam = pangolin::CreateDisplay()
  95. .SetBounds(0.0, 1.0, pangolin::Attach::Pix(UI_WIDTH), 1.0, 1280.0f / 960.0f)
  96. .SetHandler(new pangolin::Handler3D(s_cam));
  97. // Add named Panel and bind to variables beginning 'ui'
  98. // A Panel is just a View with a default layout and input handling
  99. pangolin::CreatePanel("ui")
  100. .SetBounds(0.0, 1.0, 0.0, pangolin::Attach::Pix(UI_WIDTH));
  101. // Safe and efficient binding of named variables.
  102. // Specialisations mean no conversions take place for exact types
  103. // and conversions between scalar types are cheap.
  104. pangolin::Var<bool> lvx_checkbox("ui.lvx_Checkbox", false, true);
  105. pangolin::Var<std::string> lvx_file("ui.lvx_file_String", "../map/lidarbag/start1.lvx");
  106. pangolin::Var<int> beg_Int("ui.beg", 1, 1, 18);
  107. pangolin::Var<int> end_Int("ui.end", 5, 1, 18);
  108. pangolin::Var<std::string> timequeue_string("ui.tq_String", "time string");
  109. pangolin::Var<std::string> path_string("ui.path", "cost");
  110. pangolin::Var<std::string> cost_string("ui.final_cost", "cost");
  111. // std::function objects can be used for Var's too. These work great with C++11 closures.
  112. pangolin::Var<std::function<void(void)>> save_window("ui.Save_Window", []() {
  113. pangolin::SaveWindowOnRender("window");
  114. });
  115. std::mutex mutex;
  116. std::vector<int> path;
  117. PathMap map;
  118. map.AddVertex(1, 0, 0);
  119. map.AddVertex(2, 3, 0);
  120. map.AddVertex(3, 6, 0);
  121. map.AddVertex(4, 9, 0);
  122. map.AddVertex(5, 9, 2);
  123. map.AddVertex(6, 6, 2);
  124. map.AddVertex(7, 3, 2);
  125. map.AddVertex(8, 0, 2);
  126. map.AddVertex(9, 0, 4);
  127. map.AddVertex(10, 3, 4);
  128. map.AddVertex(11, 6, 4);
  129. map.AddVertex(12, 9, 4);
  130. map.AddVertex(13, 9, 6);
  131. map.AddVertex(14, 6, 6);
  132. map.AddVertex(15, 3, 6);
  133. map.AddVertex(16, 0, 6);
  134. map.AddEdge(1, 2, false);
  135. map.AddEdge(1, 8, false);
  136. map.AddEdge(2, 3, false);
  137. map.AddEdge(2, 7, false);
  138. map.AddEdge(3, 4, false);
  139. map.AddEdge(3, 6, false);
  140. map.AddEdge(4, 5, false);
  141. map.AddEdge(5, 12, false);
  142. map.AddEdge(5, 6, false);
  143. map.AddEdge(6, 11, false);
  144. map.AddEdge(6, 7, false);
  145. map.AddEdge(7, 10, false);
  146. map.AddEdge(7, 8, false);
  147. map.AddEdge(8, 9, false);
  148. map.AddEdge(9, 10, false);
  149. map.AddEdge(9, 16, false);
  150. map.AddEdge(10, 11, false);
  151. map.AddEdge(10, 15, false);
  152. map.AddEdge(11, 12, false);
  153. map.AddEdge(11, 14, false);
  154. map.AddEdge(12, 13, false);
  155. map.AddEdge(13, 14, false);
  156. map.AddEdge(14, 15, false);
  157. map.AddEdge(15, 16, false);
  158. map.AddEdge(15, 17, false);
  159. map.AddEdge(14, 17, false);
  160. map.AddEdge(17, 18, false);
  161. pangolin::Var<std::function<void(void)>> solve_view("ui.Solve", [&]() {
  162. auto start_tm = std::chrono::system_clock::now();
  163. start = beg_Int.Get();
  164. end = end_Int.Get();
  165. float distance = 0;
  166. mutex.lock();
  167. path.clear();
  168. map.GetShortestPath(start, end, path, distance);
  169. mutex.unlock();
  170. char path_str[64] = {0};
  171. sprintf(path_str, "%d-%d:", start, end);
  172. for (int i = path.size() - 1; i >= 0; --i)
  173. {
  174. sprintf(path_str + strlen(path_str), "%d-", path[i]);
  175. }
  176. sprintf(path_str + strlen(path_str), "%d", end);
  177. path_string = path_str;
  178. char cost[62] = {0};
  179. sprintf(cost, "d(%d-%d):%.3f", start, end, distance);
  180. cost_string = cost;
  181. auto end_tm = std::chrono::system_clock::now();
  182. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_tm - start_tm);
  183. double time = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  184. char tm[32] = {0};
  185. sprintf(tm, "time:%.5fs", time);
  186. timequeue_string = tm;
  187. });
  188. // Demonstration of how we can register a keyboard hook to alter a Var
  189. pangolin::RegisterKeyPressCallback(pangolin::PANGO_CTRL + 'b', [&]() {
  190. //a_double = 3.5;
  191. });
  192. // Default hooks for exiting (Esc) and fullscreen (tab).
  193. double zoom = 1.0;
  194. while (!pangolin::ShouldQuit())
  195. {
  196. // Clear entire screen
  197. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  198. if (d_cam.IsShown())
  199. {
  200. //s_cam.SetModelViewMatrix(pangolin::ModelViewLookAt(odom_pt[0],odom_pt[1],60, odom_pt[0],odom_pt[1],0, pangolin::AxisY));
  201. d_cam.Activate(s_cam);
  202. pangolin::glDrawAxis(1);//三维坐标轴,红——x轴,绿——y轴,蓝——z轴
  203. }
  204. mutex.lock();
  205. std::vector<int> solve_path=path;
  206. mutex.unlock();
  207. std::reverse(solve_path.begin(),solve_path.end());
  208. solve_path.push_back(end);
  209. DrawMap(map, solve_path,text_font);
  210. // Swap frames and Process Events
  211. pangolin::FinishFrame();
  212. std::this_thread::sleep_for(std::chrono::microseconds(10));
  213. }
  214. delete text_font;
  215. return 0;
  216. }