1
0

tunnel.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * 文件 tunnel.cpp
  3. * 简述 巷道类的实现
  4. * 详细信息
  5. * 作者 曾敏果; 邮箱:57862207@qq.com
  6. * 日期 2022-7-14
  7. */
  8. #include "tunnel.h"
  9. tunnel_cell::tunnel_cell(double x, double y, double z)
  10. {
  11. this->x = x;
  12. this->y = y;
  13. this->z = z;
  14. }
  15. tunnel_cell::~tunnel_cell()
  16. {
  17. }
  18. tunnel::tunnel()
  19. {
  20. }
  21. //初始化格子列表,由一系列线段按顺序构成巷道的所有路径。
  22. //自第一条线段的顶点开始,每隔一米为一个格子。
  23. bool tunnel::init_cells()
  24. {
  25. bool bRet = true;
  26. read_lines_from_db();
  27. transform_lines_to_cells();
  28. return bRet;
  29. }
  30. //初始化巷道
  31. bool tunnel::init_tunnel()
  32. {
  33. init_cells();
  34. }
  35. //通过格子索引号获取格子的具体坐标
  36. bool tunnel::get_position_by_cell_index(int index, point& pos)
  37. {
  38. bool bRet = true;
  39. if (index < 0 || index >= m_vec_cells.size())
  40. {
  41. bRet = false;
  42. return bRet;
  43. }
  44. pos = point(m_vec_cells[index].x, m_vec_cells[index].y, m_vec_cells[index].z);
  45. return bRet;
  46. }
  47. int tunnel::get_cells_count()
  48. {
  49. return m_vec_cells.size();
  50. }
  51. //用于将基站坐标映射到对应的格子索引,这样就能通过基站索引加距离来确定当前车辆所在的格子
  52. int tunnel::get_cell_index_by_position(const point& pos)
  53. {
  54. //取巷道格子中离基站最近的点作为基站的格子号
  55. int index = 0;
  56. double dist_min = 1000;//
  57. for (int i = 0; i < m_vec_cells.size(); i++)
  58. {
  59. double dist = pos.dist(point(m_vec_cells[i].x, m_vec_cells[i].y, m_vec_cells[i].z));
  60. if (dist < dist_min)
  61. {
  62. dist_min = dist;
  63. index = i;
  64. }
  65. }
  66. return index;
  67. }
  68. tunnel::~tunnel()
  69. {
  70. }
  71. //从数据库中读取构成整个巷道的所有线段
  72. bool tunnel::read_lines_from_db()
  73. {
  74. bool ret = true;
  75. m_queue_lines.clear();
  76. std::string sql = "SELECT id, b_x, b_y, b_z, e_x, e_y, e_z FROM dat_tunnel_path_v ORDER BY id ASC;";
  77. std::string Error;
  78. YADB::CDBResultSet DBRes;
  79. sDBConnPool.Query(sql.c_str(), DBRes, Error);
  80. int nCount = DBRes.GetRecordCount(Error);
  81. if (nCount < 1)
  82. {
  83. log_error("初始化巷道格子集失败");
  84. ret = false;
  85. return ret;
  86. }
  87. log_info("init_cells. The record count=%ld\n", nCount);
  88. while (DBRes.GetNextRecod(Error))
  89. {
  90. double b_x = 0.0;
  91. double b_y = 0.0;
  92. double b_z = 0.0;
  93. double e_x = 0.0;
  94. double e_y = 0.0;
  95. double e_z = 0.0;
  96. DBRes.GetField("b_x", b_x, Error);
  97. DBRes.GetField("b_y", b_y, Error);
  98. DBRes.GetField("b_z", b_z, Error);
  99. DBRes.GetField("e_x", e_x, Error);
  100. DBRes.GetField("e_y", e_y, Error);
  101. DBRes.GetField("e_z", e_z, Error);
  102. line_v tunnel_line(point(b_x, b_y, b_z), point(e_x, e_y, e_z));
  103. m_queue_lines.push_back(tunnel_line);
  104. }
  105. return ret;
  106. }
  107. //将线段队列转换成巷道格子队列
  108. bool tunnel::transform_lines_to_cells()
  109. {
  110. bool bRet = true;
  111. for (auto iter_line = m_queue_lines.begin(); iter_line != m_queue_lines.end(); iter_line++)
  112. {
  113. std::deque<point> deque_points;
  114. iter_line->calc_point_list(deque_points);
  115. for (auto iter_point = deque_points.begin(); iter_point != deque_points.end(); iter_point++)
  116. {
  117. m_vec_cells.push_back(tunnel_cell(iter_point->x, iter_point->y, iter_point->z));
  118. }
  119. }
  120. return bRet;
  121. }