module_traffic_light_rule.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. #include <cfloat>
  2. #include "module_traffic_light_rule.h"
  3. #include "module_traffic_light_manager.h"
  4. bool rule::sort_vehicle(const uint64_t lhs, const uint64_t rhs, const traffic_light_group_ptr& g)
  5. {
  6. pos_data* v1 = traffic_light_manager::instance()->get_position(lhs);
  7. pos_data* v2 = traffic_light_manager::instance()->get_position(rhs);
  8. double d1 = DBL_MAX, d2 = DBL_MAX;
  9. if(nullptr != v2){
  10. d2 = g->dist(v2->x, v2->y);
  11. }
  12. if(nullptr != v1){
  13. d1 = g->dist(v1->x, v1->y);
  14. }
  15. return d1 < d2;
  16. }
  17. traffic_light_ptr rule::find_nearby_light(const point& p, traffic_light_group_ptr g)
  18. {
  19. traffic_light_ptr pl = nullptr;
  20. double max = DBL_MAX;
  21. //查找最近的红绿灯
  22. for(auto it = g->m_vt_lights.begin(); it != g->m_vt_lights.end(); ++it)
  23. {
  24. traffic_light_ptr tmp = traffic_light_manager::instance()->find_light((*it)->m_light_id);
  25. double dt = tmp->dist(p.x, p.y);
  26. if(dt < max){
  27. max = dt;
  28. pl = tmp;
  29. }
  30. }
  31. return pl;
  32. }
  33. void crossing_rule::change_state(const uint64_t vid, traffic_light_group_ptr g)
  34. {
  35. pos_data* cp = traffic_light_manager::instance()->get_position(vid);
  36. if(nullptr != cp){
  37. // 查找离车辆最近的红绿灯
  38. traffic_light_ptr pl = find_nearby_light(point(cp->x, cp->y), g);
  39. // 设置来车方向为绿灯,其他方向为红灯
  40. if(nullptr != pl){
  41. log_info("[traffic_light] card_id=%lld, light_id=%d", vid, pl->m_light_id);
  42. g->set_light(pl->m_light_id, green, red);
  43. }
  44. }else{
  45. log_warn("[traffic_light] it can't get vehicle position. vid=%lld, lg's id=%d", vid, g->m_group_id);
  46. }
  47. }
  48. // 获得车辆是否大车的标记
  49. bool crossing_rule::get_vehicle_state(const uint64_t& vid)
  50. {
  51. bool s = false;
  52. pos_data* v = traffic_light_manager::instance()->get_position(vid);
  53. if(nullptr != v){
  54. s = v->m_bigger;
  55. }
  56. return s;
  57. }
  58. bool crossing_rule::find_light(const uint64_t& vid, traffic_light_group_ptr g, bool a)
  59. {
  60. pos_data* cp = traffic_light_manager::instance()->get_position(vid);
  61. if(nullptr != cp){
  62. line l;
  63. traffic_light_ptr pl = find_nearby_light(point(cp->x, cp->y), g);
  64. if(nullptr == pl){
  65. return false;
  66. }
  67. if(!a){
  68. if(1 == pl->m_special){
  69. // 判断是否在线上
  70. if(pl->m_line_group.contain(cp->x, cp->y, 0.5)){
  71. // 判断行车方向
  72. double d = cp->m_speed * pl->m_direct_distance;
  73. if(d < 0){
  74. return true;
  75. }
  76. }
  77. }
  78. }else{
  79. // 判断是否在线上
  80. if(pl->m_line_group.contain(cp->x, cp->y, 1.0)){
  81. double d = cp->m_speed * pl->m_direct_distance;
  82. if(d < 0){
  83. return true;
  84. }
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. bool crossing_rule::handle_manual_ctrl(traffic_light_group_ptr g)
  91. {
  92. if(g->is_time_out()){
  93. g->reset();
  94. return true;
  95. }
  96. return false;
  97. }
  98. bool crossing_rule::handle_crossing(traffic_light_group_ptr g)
  99. {
  100. if(g->m_scope <= 0){
  101. return false;
  102. }
  103. // 查找一定范围的车辆
  104. std::vector<uint64_t> vtl = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(g->x), static_cast<int>(g->y)), g->m_scope, 0);
  105. log_info("[traffic_light] handle crossing rule.gid=%d, group_scope=%.2f, vehicle's num=%d", g->m_group_id, g->m_scope, vtl.size());
  106. if(vtl.empty()){
  107. if(g->m_card_id > 0 && g->m_priority == priority_crossing)
  108. {
  109. // 释放红绿灯组
  110. g->reset();
  111. return true;
  112. }
  113. return false;
  114. }
  115. // 是否已经有车记录
  116. if(g->m_card_id > 0)
  117. {
  118. auto it = std::find(vtl.begin(), vtl.end(), g->m_card_id);
  119. if(it != vtl.end()){
  120. log_info("[traffic_light] handle crossing rule, there has vehicle that control the light group, card_id=%d", g->m_card_id);
  121. return false;
  122. }
  123. }
  124. std::sort(vtl.begin(), vtl.end(), [&](const uint64_t& lhs, const uint64_t& rhs){
  125. return sort_vehicle(lhs, rhs, g);
  126. });
  127. // 设置优先级以及卡id
  128. g->set_crossing(vtl[0], priority_crossing);
  129. // 修改红绿灯组状态
  130. change_state(vtl[0], g);
  131. return true;
  132. }
  133. bool crossing_rule::handle_avoidance(traffic_light_group_ptr g)
  134. {
  135. // 查看是否是大车,true表示特殊红绿灯,需要检测大车避让规则
  136. bool flag = false;
  137. do{
  138. auto vt = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(g->x), static_cast<int>(g->y)), g->m_scope, 0);
  139. log_info("[traffic_light] handle avoidance, gid=%d, x=%.2f, y=%.2f, scope=%.2f, vehicle's size=%d", g->m_group_id, g->x, g->y, g->m_scope, vt.size());
  140. // 筛选大车
  141. if(vt.empty()){
  142. flag = true;
  143. break;
  144. }
  145. vt.erase(std::remove_if(vt.begin(), vt.end(), [&](const uint64_t& vid){
  146. return get_vehicle_state(vid);
  147. }),
  148. vt.end());
  149. if(vt.empty()){
  150. flag = true;
  151. break;
  152. }
  153. // 查看离最近的点是否为特殊点,判断位置是否在直线上,判断方向
  154. vt.erase(std::remove_if(vt.begin(), vt.end(), [&](const uint64_t& vid){
  155. return find_light(vid, g);
  156. }),
  157. vt.end());
  158. if(vt.empty()){
  159. flag = true;
  160. break;
  161. }
  162. // 特殊规则保留id且优先级2,避让规则置空id,优先级2
  163. if(priority_avoidance == g->get_priority()){
  164. if(0 == g->m_card_id){
  165. break;
  166. }else{
  167. auto it = std::find(vt.begin(), vt.end(), g->m_card_id);
  168. if(it != vt.end()){
  169. break;
  170. }
  171. }
  172. }
  173. // 排序,找到离之最近的大车
  174. std::sort(vt.begin(), vt.end(), [&](const uint64_t& vid, const uint64_t& cid){
  175. return sort_vehicle(vid, cid, g);
  176. });
  177. change_state(vt[0], g);
  178. g->set_crossing(vt[0], priority_avoidance);
  179. return true;
  180. }while(false);
  181. if(flag){
  182. // 找不到之前的车辆,则初始化
  183. if(priority_avoidance == g->get_priority() && g->m_card_id > 0){
  184. g->reset();
  185. }else{
  186. flag = false;
  187. }
  188. }
  189. return flag;
  190. }
  191. vt_traffic_group crossing_rule::handle_rule(pos_data& p)
  192. {
  193. vt_traffic_group llist;
  194. log_info("[traffic_light] crossing_rule::handle_rule, group's size=%d", m_vt_group.size());
  195. for(auto& it : m_vt_group){
  196. // 获取控制权
  197. it->get_turn();
  198. int priority = it->get_priority();
  199. log_info("[traffic_light] crossing_rule::handle_rule, gid=%d, priority=%d, special=%d", it->m_group_id, priority, it->m_special);
  200. bool flag = false;
  201. while(true){
  202. if(priority_manual_ctrl == priority){
  203. flag = handle_manual_ctrl(it);
  204. log_info("[traffic_light] crossing_rule::handle_rule, after call handle_manual_ctrl, flag=%d", flag);
  205. if(flag){
  206. break;
  207. }
  208. }
  209. if(priority <= priority_avoidance && it->m_special){
  210. flag = handle_avoidance(it);
  211. log_info("[traffic_light] crossing_rule::handle_rule, after call handle_avoidance, flag=%d", flag);
  212. if(flag){
  213. break;
  214. }
  215. }
  216. if(priority <= priority_crossing){
  217. flag = handle_crossing(it);
  218. log_info("[traffic_light] crossing_rule::handle_rule, after call handle_crossing, flag=%d", flag);
  219. if(flag){
  220. break;
  221. }
  222. }
  223. break;
  224. };
  225. it->release_turn();
  226. if(flag){
  227. llist.push_back(it);
  228. }
  229. }
  230. return std::move(llist);
  231. }
  232. vt_traffic_group avoidance_rule::find_group(const pos_data& p)
  233. {
  234. return find_group(point(p.x, p.y), p.m_area_id, p.m_speed, p);
  235. }
  236. vt_traffic_group avoidance_rule::find_group(const point& po , int area_id, double speed, const pos_data& pd)
  237. {
  238. vt_traffic_group llist;
  239. if(0 == pd.m_card_id || 0 == pd.m_type){
  240. std::move(llist);
  241. }
  242. // 获取车辆所在地图区域内的所有红绿灯组
  243. vt_traffic_group vg = m_map_area_group[area_id];
  244. double dist = 0.0;
  245. for(std::size_t i = 0; i < vg.size(); ++i){
  246. traffic_light_group_ptr pg = vg[i];
  247. if(nullptr == pg){
  248. continue;
  249. }
  250. // 如果此灯组被控制了,则不允许使用
  251. if(vg[i]->get_status() && vg[i]->m_card_id != pd.m_card_id){
  252. log_info("[traffic_light] card_id=%lld was control light group, gid=%d", pd.m_card_id, vg[i]->m_group_id);
  253. continue;
  254. }
  255. // 求灯组到车辆的距离(车辆在灯组的左下方,则距离值取反)
  256. dist = vg[i]->dist_direct(po.x, po.y);
  257. // 车辆在灯组左边或下边
  258. log_info("[traffic_light] avoidance_rule::find_group, area_id=%d, area_group=%d, dist=%.2f", area_id, vg.size(), dist);
  259. if(dist <= 0){
  260. // 速度方向与前进方向一致
  261. if(speed > 0){
  262. // 车辆到灯组的距离小于指定距离
  263. if(vg[i]->dist(po.x, po.y) <= g_max_scope){
  264. llist.push_back(vg[i]);
  265. }
  266. // 紧挨着的一个灯组到车距离小于指定阈值
  267. if(i + 1 < vg.size()){
  268. if(vg[i+1]->dist(po.x, po.y) <= g_max_scope){
  269. llist.push_back(vg[i+1]);
  270. }
  271. }
  272. }else if(speed < 0){
  273. // 速度方向与前进方向不一致,判断运动方向的灯组是否在可控范围
  274. if(i >= 1 && vg[i - 1]->dist(po.x, po.y) <= g_max_scope){
  275. llist.push_back(vg[i-1]);
  276. }
  277. if(i >= 2 && vg[i-2]->dist(po.x, po.y) <= g_max_scope){
  278. llist.push_back(vg[i-2]);
  279. }
  280. }
  281. break;
  282. }
  283. log_info("[traffic_light] the vehicle is in light group's left or down side.");
  284. }
  285. // 车辆在灯组右边或上边
  286. if(dist > 0 && speed < 0){
  287. std::size_t i = vg.size();
  288. if(i >= 1 && vg[i-1]->dist(po.x, po.y) <= g_max_scope){
  289. llist.push_back(vg[i-1]);
  290. }
  291. if(i >= 2 && vg[i-2]->dist(po.x, po.y) <= g_max_scope){
  292. llist.push_back(vg[i-2]);
  293. }
  294. log_info("[traffic_light] the vehicle is in light group's right or up side");
  295. }
  296. return std::move(llist);
  297. }
  298. bool avoidance_rule::find_vehicle_in_group(vt_traffic_group& vg, std::vector<pos_data>& vv)
  299. {
  300. if(vv.size() < 2){
  301. log_error("[traffic_light] vv's size less than 2");
  302. return false;
  303. }
  304. bool flag = true;
  305. double d = vg[0]->dist(vv[1].x, vv[1].y);
  306. line tl(*vg[0], *vg[1]);
  307. if(tl.contain(vv[1].x, vv[1].y, 1))
  308. {
  309. if(vv[1].m_speed * vv[0].m_speed < 0 && d > g_mid_vehicle_length_group){
  310. flag = false;
  311. }
  312. }
  313. return flag;
  314. }
  315. bool avoidance_rule::get_vehicle(const pos_data& p, vt_traffic_group& vg)
  316. {
  317. double d = vg[0]->dist(*vg[1]);
  318. std::vector<uint64_t> vt1 = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(vg[0]->x), static_cast<int>(vg[0]->y)), static_cast<int>(d), vg[0]->m_card_id);
  319. std::vector<uint64_t> vt2 = traffic_light_manager::instance()->find_nearby_vehicle(point(static_cast<int>(vg[1]->x), static_cast<int>(vg[1]->y)), static_cast<int>(d), vg[1]->m_card_id);
  320. std::sort(vt1.begin(), vt1.end());
  321. std::sort(vt2.begin(), vt2.end());
  322. std::vector<uint64_t> vti;
  323. std::set_intersection(vt1.begin(), vt1.end(), vt2.begin(), vt2.end(), std::back_inserter(vti));
  324. if(vti.empty()){
  325. return false;
  326. }
  327. // 筛选出符合条件的车辆,离第一个红绿灯距离进行排序
  328. vti.erase(std::remove_if(vti.begin(), vti.end(), [&](const uint64_t& vid) -> bool {
  329. pos_data* tp = traffic_light_manager::instance()->get_position(vid);
  330. if(tp == nullptr){
  331. return true;
  332. }
  333. std::vector<pos_data> vv;
  334. vv.push_back(p);
  335. vv.push_back(*tp);
  336. return find_vehicle_in_group(vg, vv);
  337. }));
  338. return vti.empty()?false:true;
  339. }
  340. void avoidance_rule::insert(traffic_light_group_ptr g, pos_data& p, vt_traffic_group& vg, const int lc, const int lcr)
  341. {
  342. p.m_light_group.push_back(g->m_group_id);
  343. if(g->get_priority() < priority_avoidance){
  344. // 红绿灯组优先级取初始值0或路口控制1
  345. change_group(g, point(p.x, p.y), lc, lcr);
  346. vg.push_back(g);
  347. }
  348. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), g);
  349. g->m_green_light_id = pl->m_light_id;
  350. // 灯组绑定车辆的卡号
  351. g->m_card_id = p.m_card_id;
  352. g->set_status(true);
  353. }
  354. void avoidance_rule::erase(bool a, pos_data& p, vt_traffic_group& vg)
  355. {
  356. if(p.m_light_group.size() <= 0){
  357. return;
  358. }
  359. traffic_light_group_ptr pg = nullptr;
  360. bool flag = false;
  361. if(a){
  362. pg = traffic_light_manager::instance()->find_group(p.m_light_group.front());
  363. p.m_light_group.pop_front();
  364. }else{
  365. pg = traffic_light_manager::instance()->find_group(p.m_light_group.back());
  366. p.m_light_group.pop_back();
  367. }
  368. if(nullptr == pg){
  369. return;
  370. }
  371. pg->get_turn();
  372. if(pg->get_priority() <= priority_avoidance){
  373. // 重置灯组
  374. pg->reset();
  375. flag = true;
  376. }
  377. pg->release_turn();
  378. pg->set_status(false);
  379. if(flag){
  380. vg.push_back(pg);
  381. }
  382. }
  383. void avoidance_rule::change_group(const traffic_light_group_ptr& g, const point& p, const int& lc, const int& lcr)
  384. {
  385. // 1.取离p最近的灯组信息
  386. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), g);
  387. if(nullptr == pl){
  388. return;
  389. }
  390. log_info("[traffic_light] change light's status in %d group", g->m_group_id);
  391. // 2.获取灯组中临近车辆的灯id
  392. g->m_green_light_id = pl->m_light_id;
  393. g->get_turn();
  394. // 3.设置灯组中灯号为ld的灯颜色为lc,其余灯颜色为lcr
  395. g->set_avoidance(pl->m_light_id, lc, lcr);
  396. g->release_turn();
  397. }
  398. bool avoidance_rule::is_different(traffic_light_group_ptr g, const point& p, const int& lc, const int& lcr)
  399. {
  400. bool b = false;
  401. traffic_light_ptr pl = find_nearby_light(p, g);
  402. if(g->is_different(pl->m_light_id, lc, lcr)){
  403. b = true;
  404. }
  405. return b;
  406. }
  407. vt_traffic_group avoidance_rule::handle_rule(pos_data& p)
  408. {
  409. vt_traffic_group llist;
  410. if(p.m_card_id == 0|| p.m_type == 0){
  411. return std::move(llist);
  412. }
  413. log_info("[traffic_light] avoidance_rule::handle_rule, group's size=%d, area_id=%d", m_map_area_group.size(), p.m_area_id);
  414. auto it = m_map_area_group.find(p.m_area_id);
  415. if(it != m_map_area_group.end()){
  416. // 根据车卡的定位坐标以及定位区域信息获取红绿灯组
  417. vt_traffic_group vtg = find_group(point(p.x, p.y), p.m_area_id, p.m_speed, p);
  418. log_info("[traffic_light] handle avoidance rule, area has light, area_id=%d, card_id=%d, light_group's size=%d", p.m_area_id, p.m_card_id, vtg.size());
  419. if(vtg.size() == 0){
  420. // 所在区域没有可以控制的红绿灯组
  421. switch(p.m_light_group.size())
  422. {
  423. case 1:
  424. // 当前车辆控制的路口为1个,释放该炉口控制
  425. erase(true, p, llist);
  426. break;
  427. case 2:
  428. // 当前车辆控制的路口为两个,释放这两个路口的控制
  429. erase(true, p, llist);
  430. erase(false, p, llist);
  431. break;
  432. }
  433. }else if(1 == vtg.size()){
  434. // 所在区域存在1个红绿灯组
  435. switch(p.m_light_group.size())
  436. {
  437. case 0:
  438. // 当前卡没有绑定红绿灯组,绑定此灯组,并设置灯组颜色为绿色
  439. insert(vtg[0], p, llist, green, red);
  440. break;
  441. case 1:
  442. //当前卡绑定了1个红绿灯组
  443. if(p.m_light_group.front() != vtg[0]->m_group_id){
  444. // 检查当前卡绑定的红绿灯组与区域找到的是不是同一个
  445. // 不是,释放之前的绑定,再绑定新灯组
  446. erase(true, p, llist);
  447. insert(vtg[0], p, llist, green, red);
  448. }else{
  449. // 当前卡绑定的红绿灯组与区域找到的是同一个
  450. traffic_light_ptr pl = find_nearby_light(point(p.x, p.y), vtg[0]);
  451. if(nullptr != pl && pl->m_light_id != vtg[0]->m_green_light_id){
  452. erase(true, p, llist);
  453. }
  454. }
  455. break;
  456. }
  457. }else if(2 == p.m_light_group.size()){
  458. // 当前卡绑定了2个红绿灯组
  459. traffic_light_group_ptr g = vtg[0];
  460. if(p.m_light_group.front() == g->m_group_id){
  461. // 第一个绑定的红绿灯组与区域找到的是同一个,删除第二个
  462. erase(false, p, llist);
  463. if(g->get_priority() <= priority_avoidance){
  464. change_group(g, point(p.x, p.y), green, red);
  465. llist.push_back(g);
  466. }
  467. }else if(p.m_light_group.back() == g->m_group_id){
  468. // 第二个绑定的红绿灯组与区域找到的是同一个,删除第一个
  469. erase(true, p, llist);
  470. if(g->get_priority() <= priority_avoidance){
  471. change_group(g, point(p.x, p.y), green, red);
  472. llist.push_back(g);
  473. }
  474. }else{
  475. // 两个都不是,全部删除
  476. erase(true, p, llist);
  477. erase(false, p, llist);
  478. insert(g, p, llist, green, red);
  479. }
  480. }else if(2 == vtg.size()){
  481. // 所在区域存在两个红绿灯组,巷道相遇逻辑
  482. // 判断两个灯组中是否有车
  483. bool b = get_vehicle(p, vtg);
  484. light_color la, lb;
  485. if(b){
  486. la = red;
  487. lb = spark;
  488. }else{
  489. la = green;
  490. lb = red;
  491. }
  492. switch(p.m_light_group.size()){
  493. case 0:
  494. // 当前卡没有绑定红绿灯组,绑定2个灯组
  495. insert(vtg[0], p, llist, la, lb);
  496. insert(vtg[1], p, llist, green, spark);
  497. break;
  498. case 1:
  499. // 当前卡有绑定1个红绿灯组,更新
  500. if(p.m_light_group.front() == vtg[0]->m_group_id){
  501. if(vtg[0]->get_priority() <= priority_avoidance){
  502. change_group(vtg[0], point(p.x, p.y), la, lb);
  503. llist.push_back(vtg[0]);
  504. }
  505. insert(vtg[1], p, llist, green, spark);
  506. }else if(p.m_light_group.front() == vtg[1]->m_group_id){
  507. if(vtg[1]->get_priority() <= priority_avoidance)
  508. {
  509. change_group(vtg[1], point(p.x, p.y), green, spark);
  510. llist.push_back(vtg[1]);
  511. }
  512. p.m_light_group.push_front(vtg[0]->m_group_id);
  513. if(vtg[0]->get_priority() < priority_avoidance)
  514. {
  515. change_group(vtg[0], point(p.x, p.y), la, lb);
  516. llist.push_back(vtg[0]);
  517. }
  518. }else{
  519. // 两个都不相等
  520. erase(false, p, llist);
  521. insert(vtg[0], p, llist, la, lb);
  522. insert(vtg[1], p, llist, green, spark);
  523. }
  524. break;
  525. case 2:
  526. // 当前卡有绑定2个红绿灯组,更新
  527. {
  528. std::list<int>::iterator ito = std::find(p.m_light_group.begin(), p.m_light_group.end(), vtg[0]->m_group_id);
  529. std::list<int>::iterator itt = std::find(p.m_light_group.begin(), p.m_light_group.end(), vtg[1]->m_group_id);
  530. if(ito != p.m_light_group.end())
  531. {
  532. if(itt != p.m_light_group.end())
  533. {
  534. if(p.m_light_group.front() == vtg[0]->m_group_id){
  535. if(is_different(vtg[0], point(p.x, p.y), la, lb)){
  536. llist.push_back(vtg[0]);
  537. }
  538. if(is_different(vtg[1], point(p.x, p.y), green, spark)){
  539. llist.push_back(vtg[1]);
  540. }
  541. }else{
  542. p.m_light_group.clear();
  543. if(vtg[0]->get_priority() <= priority_avoidance){
  544. change_group(vtg[0], point(p.x, p.y), la, lb);
  545. llist.push_back(vtg[0]);
  546. }
  547. if(vtg[1]->get_priority() <= priority_avoidance){
  548. change_group(vtg[1], point(p.x, p.y), green, spark);
  549. llist.push_back(vtg[1]);
  550. }
  551. p.m_light_group.push_back(vtg[0]->m_group_id);
  552. p.m_light_group.push_back(vtg[1]->m_group_id);
  553. }
  554. }
  555. else
  556. {
  557. if(p.m_light_group.front() == vtg[0]->m_group_id){
  558. erase(false, p, llist);
  559. insert(vtg[1], p, llist, green, spark);
  560. }else{
  561. erase(true, p, llist);
  562. if(vtg[0]->get_priority() <= priority_avoidance){
  563. change_group(vtg[0], point(p.x, p.y), la, lb);
  564. llist.push_back(vtg[0]);
  565. }
  566. insert(vtg[1], p, llist, green, spark);
  567. }
  568. }
  569. }
  570. else
  571. {
  572. if(itt != p.m_light_group.end()){
  573. if(p.m_light_group.front() == vtg[1]->m_group_id){
  574. erase(false, p, llist);
  575. }else{
  576. erase(true, p, llist);
  577. }
  578. if(vtg[1]->get_priority() <= priority_avoidance){
  579. change_group(vtg[1], point(p.x, p.y), green, spark);
  580. llist.push_back(vtg[1]);
  581. }
  582. p.m_light_group.push_front(vtg[1]->m_group_id);
  583. if(vtg[0]->get_priority() < priority_avoidance){
  584. change_group(vtg[0], point(p.x, p.y), la, lb);
  585. llist.push_back(vtg[0]);
  586. }
  587. }else{
  588. erase(true, p, llist);
  589. erase(true, p, llist);
  590. insert(vtg[0], p, llist, la, lb);
  591. insert(vtg[1], p, llist, green, spark);
  592. }
  593. }
  594. }
  595. break;
  596. }
  597. }
  598. }else{
  599. log_info("[traffic_light] the %d area has no light group", p.m_area_id);
  600. }
  601. return std::move(llist);
  602. }