module_traffic_light_rule.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef module_traffic_light_rule_h
  2. #define module_traffic_light_rule_h
  3. #include <vector>
  4. #include <list>
  5. #include <map>
  6. #include <unordered_map>
  7. #include <thread>
  8. #include <condition_variable>
  9. #include "module_traffic_light_common.h"
  10. #include "module_traffic_light.h"
  11. using vt_traffic_group = std::vector<traffic_light_group_ptr>;
  12. using mp_traffic_group = std::map<int, vt_traffic_group>;
  13. struct rule{
  14. rule(){}
  15. virtual ~rule(){}
  16. virtual vt_traffic_group handle_rule(pos_data& p) = 0;
  17. virtual void put(traffic_light_group_ptr tlp) = 0;
  18. virtual void put(vt_traffic_group&& vc) = 0;
  19. // 车子按距离红绿灯组坐标点排序
  20. bool sort_vehicle(const uint64_t lhs, const uint64_t rhs, const traffic_light_group_ptr& g);
  21. // 在红绿灯组数组中找离位置点(x,y)最近的灯组信息
  22. traffic_light_ptr find_nearby_light(const point& p, traffic_light_group_ptr g);
  23. };
  24. // 路口规则
  25. struct crossing_rule: rule{
  26. crossing_rule(){}
  27. ~crossing_rule(){}
  28. vt_traffic_group handle_rule(pos_data& p);
  29. // 谁先到达炉口红绿灯指定的距离,变绿灯,其他变红灯
  30. bool get_vehicle_state(const uint64_t& card_id);
  31. // 自动控制
  32. bool handle_manual_ctrl(traffic_light_group_ptr g);
  33. // 查找附近的红绿灯
  34. bool find_light(const uint64_t& cid, traffic_light_group_ptr g, bool a = false);
  35. // 避让处理
  36. bool handle_avoidance(traffic_light_group_ptr g);
  37. // 路口处理
  38. bool handle_crossing(traffic_light_group_ptr g);
  39. // 修改红绿灯组状态
  40. void change_state(const uint64_t cid, traffic_light_group_ptr g);
  41. void put(traffic_light_group_ptr g)
  42. {
  43. m_vt_group.push_back(g);
  44. }
  45. void put(vt_traffic_group&& vc)
  46. {
  47. m_vt_group = vc;
  48. }
  49. int size()
  50. {
  51. return m_vt_group.size();
  52. }
  53. private:
  54. vt_traffic_group m_vt_group;
  55. };
  56. // 避让规则
  57. struct avoidance_rule: rule{
  58. public:
  59. avoidance_rule(){}
  60. ~avoidance_rule(){}
  61. vt_traffic_group handle_rule(pos_data& p);
  62. // 给车卡绑定路口灯组控制,并更改灯组中灯的颜色状态
  63. void insert(traffic_light_group_ptr g, pos_data& p, vt_traffic_group& vg, const int lc, const int lcr);
  64. // 改变点p定位附近灯组的状态,满足条件的灯状态改为lc,否改为lcr
  65. void change_group(const traffic_light_group_ptr& g, const point& p, const int& lc, const int& lcr);
  66. // 把车卡从路口灯组控制中解绑
  67. void erase(bool a, pos_data& p, vt_traffic_group& vg);
  68. // 根据车辆与灯组的位置关系,查找灯组
  69. vt_traffic_group find_group(const pos_data& p);
  70. vt_traffic_group find_group(const point& po, int aid, double v, const pos_data& p);
  71. // 两个红绿灯组中间是否有车
  72. bool get_vehicle(const pos_data& p, vt_traffic_group& vg);
  73. // 求坐标点与红绿灯组的距离
  74. bool is_different(traffic_light_group_ptr g, const point& p, const int& lc, const int& lcr);
  75. // 后续小车避让大车规则,只需要找到前方的两个红绿灯,找到两个对比一下之前的记录,如果灯组改变了,则修改,如果没变,则判断是否有小车,进行改变
  76. // 如果只找到一个红绿灯,查看是否含有记录,如果有,则不做任何处理
  77. bool find_vehicle_in_group(vt_traffic_group& vg, std::vector<pos_data>& vv);
  78. void put(traffic_light_group_ptr ptlg)
  79. {
  80. m_map_area_group[ptlg->m_area_id].push_back(ptlg);
  81. }
  82. void put(vt_traffic_group&& lhs)
  83. {
  84. for(const auto x : lhs){
  85. m_map_area_group[x->m_area_id].push_back(x);
  86. }
  87. }
  88. int size()
  89. {
  90. return m_map_area_group.size();
  91. }
  92. private:
  93. mp_traffic_group m_map_area_group;
  94. };
  95. struct up_down_rule: rule{
  96. };
  97. using hashmap_light = std::unordered_map<int, traffic_light_ptr>;
  98. using hashmap_group = std::unordered_map<int, traffic_light_group_ptr>;
  99. #endif