engine.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: Sun Yalun
  5. * @Date: 2024-10-17 10:19:38
  6. * @LastEditors: Sun Yalun
  7. * @LastEditTime: 2024-11-08 15:42:41
  8. */
  9. #include <iostream>
  10. #include <sstream>
  11. #include "engine.h"
  12. #include <string>
  13. #include <cmath>
  14. #include <iomanip>
  15. #include <fstream>
  16. #include "utils.h"
  17. #include <map>
  18. #include <algorithm>
  19. #include <vector>
  20. std::vector<InterInfo> getInters(const std::string &intercsvPath, const Point &point, double distance)
  21. {
  22. std::vector<InterInfo> result;
  23. std::vector<InterInfo> intersections = readIntersectionsFromCSV(intercsvPath);
  24. if (intersections.empty())
  25. {
  26. std::cerr << "没有读取到路口信息。" << std::endl;
  27. return result;
  28. }
  29. // 创建一个距离和路口对象的映射
  30. std::map<double, InterInfo> dist_to_inter;
  31. for (const auto &intersection : intersections)
  32. {
  33. Point centeroid(0.0, 0.0, 0.0);
  34. centeroid = calculateCentroid(intersection.inter_coords);
  35. // 计算中心点到查询点的距离
  36. double dist = calculateDistance(point.pos_x, point.pos_y, centeroid.pos_x, centeroid.pos_y);
  37. std::cout << std::fixed << std::setprecision(8);
  38. // std::cout << "dist: " << dist << std::endl;
  39. // 如果满足距离要求,把满足条件的路口加入结果
  40. if (dist <= distance)
  41. {
  42. // result.push_back(intersection);
  43. dist_to_inter[dist] = intersection;
  44. }
  45. }
  46. // 从排序后的列表中提取出路口信息
  47. for (const auto &dist_inter : dist_to_inter)
  48. {
  49. result.push_back(dist_inter.second);
  50. }
  51. if (result.size() == 0)
  52. {
  53. // std::cerr << "没有找到符合条件的路口。" << std::endl;
  54. InterInfo inter;
  55. inter.inter_id = 10000;
  56. inter.inter_ref_link = {10001};
  57. inter.inter_type = 10002;
  58. inter.inter_in_shape = 10003;
  59. inter.inter_coords = {Point(1.0, 1.0, 0.0)};
  60. result.push_back(inter);
  61. }
  62. return result;
  63. }
  64. std::vector<CrosswalkInfo> getCrosswalks(const std::string &crosscsvPath, const Point &point, double distance)
  65. {
  66. std::vector<CrosswalkInfo> result;
  67. std::vector<CrosswalkInfo> crosswalks = readCrosswalksFromCSV(crosscsvPath);
  68. if (crosswalks.empty())
  69. {
  70. std::cerr << "没有读取到人行横道信息。" << std::endl;
  71. return result;
  72. }
  73. std::map<double, CrosswalkInfo> dist_to_cross;
  74. for (const auto &crosswalk : crosswalks)
  75. {
  76. Point centeroid(0.0, 0.0, 0.0);
  77. centeroid = calculateCentroid(crosswalk.cross_coords);
  78. // 计算中心点到查询点的距离
  79. double dist = calculateDistance(point.pos_x, point.pos_y, centeroid.pos_x, centeroid.pos_y);
  80. std::cout << std::fixed << std::setprecision(8);
  81. // std::cout << "dist: " << dist << std::endl;
  82. // 如果满足距离要求,把满足条件的路口加入结果
  83. if (dist <= distance)
  84. {
  85. // result.push_back(crosswalk);
  86. dist_to_cross[dist] = crosswalk;
  87. }
  88. }
  89. for (const auto &dist_cross : dist_to_cross)
  90. {
  91. result.push_back(dist_cross.second);
  92. }
  93. if (result.size() == 0)
  94. {
  95. // std::cerr << "没有找到符合条件的人行横道。" << std::endl;
  96. CrosswalkInfo cross;
  97. cross.cross_id = 20000;
  98. cross.cross_coords = {Point(2.0, 2.0, 0.0)};
  99. result.push_back(cross);
  100. }
  101. return result;
  102. }
  103. std::vector<TrafficlightInfo> getForwardTrafficlight(const std::string &lightcsvPath, const Point &point, double distance)
  104. {
  105. std::vector<TrafficlightInfo> result;
  106. std::vector<TrafficlightInfo> trafficlights = readTrafficlightsFromCSV(lightcsvPath);
  107. std::map<double, TrafficlightInfo> dist_to_light;
  108. for (const auto &light : trafficlights)
  109. {
  110. Point centeroid(0.0, 0.0, 0.0);
  111. centeroid = calculateCentroid(light.trafficlight_coords);
  112. // 计算中心点到查询点的距离
  113. double dist = calculateDistance(point.pos_x, point.pos_y, centeroid.pos_x, centeroid.pos_y);
  114. // 如果距离超过指定范围,跳过
  115. if (dist > distance)
  116. {
  117. continue;
  118. }
  119. // 计算信号灯中心点到信号灯的方向向量
  120. double dx = centeroid.pos_x - point.pos_x;
  121. double dy = centeroid.pos_y - point.pos_y;
  122. double heading_rad = point.heading;
  123. double hx = std::cos(heading_rad);
  124. double hy = std::sin(heading_rad);
  125. double dot_product = dx * hx + dy * hy;
  126. double mag_d = std::sqrt(dx * dx + dy * dy);
  127. double mag_h = std::sqrt(hx * hx + hy * hy);
  128. double cos_theta = dot_product / (mag_d * mag_h);
  129. double theta = std::acos(cos_theta);
  130. double angle_threshold = M_PI / 2;
  131. // 如果夹角小于阈值,认为信号灯在前方
  132. if (theta < angle_threshold)
  133. {
  134. // result.push_back(light);
  135. dist_to_light[dist] = light;
  136. }
  137. }
  138. for (const auto &dist_light : dist_to_light)
  139. {
  140. result.push_back(dist_light.second);
  141. }
  142. if (result.size() == 0)
  143. {
  144. // std::cerr << "没有找到符合条件的交通信号灯。" << std::endl;
  145. TrafficlightInfo tflight;
  146. tflight.trafficlight_id = 30000;
  147. tflight.trafficlight_ref_link = 30001;
  148. tflight.trafficlight_ref_lane = {30002};
  149. tflight.trafficlight_type = 30003;
  150. tflight.trafficlight_head = {30004};
  151. tflight.trafficlight_coords = {Point(3.0, 3.0, 0.0)};
  152. result.push_back(tflight);
  153. }
  154. return result;
  155. }
  156. // 根据位置获取所在道路信息
  157. std::vector<RoadInfo> getLink(const std::string &linkcsvPath, const Point &point)
  158. {
  159. std::vector<RoadInfo> result;
  160. std::vector<RoadInfo> roads = readRoadsFromCSV(linkcsvPath);
  161. if (roads.empty())
  162. {
  163. std::cerr << "没有读取到道路信息。" << std::endl;
  164. return result;
  165. }
  166. for (const auto &road : roads)
  167. {
  168. bool isNear = false;
  169. const auto &coords = road.link_coords;
  170. double min_dist = road.link_lanenum * 3.5 * 0.8; // 根据车道数计算最小距离
  171. double dist1 = computeBazierDistanceToCurve(point, coords);
  172. if (dist1 <= min_dist)
  173. {
  174. result.push_back(road);
  175. isNear = true;
  176. break;
  177. }
  178. if (isNear)
  179. {
  180. continue;
  181. }
  182. }
  183. if (result.size() == 0)
  184. {
  185. // std::cerr << "没有找到符合条件的道路。" << std::endl;
  186. RoadInfo rInfo;
  187. rInfo.link_id = 40000;
  188. rInfo.link_fc = 40001;
  189. rInfo.link_type = 40002;
  190. rInfo.link_speed_max = 180;
  191. rInfo.link_speed_min = 0;
  192. rInfo.link_direction = 40003;
  193. rInfo.link_lanenum = 40004;
  194. rInfo.link_coords = {Point(4.0, 4.0, 0.0)};
  195. result.push_back(rInfo);
  196. }
  197. return result;
  198. }
  199. // 根据所在位置获取所在车道信息
  200. std::vector<LaneInfo> getLane(const std::string &lanecsvPath, const Point &point)
  201. {
  202. // std::cout << point.pos_x << " " << point.pos_y << " " << point.heading << std::endl;
  203. std::vector<LaneInfo> result;
  204. std::vector<LaneInfo> lanes = readLanesFromCSV(lanecsvPath);
  205. if (lanes.empty())
  206. {
  207. std::cerr << "没有读取到车道信息。" << std::endl;
  208. return result;
  209. }
  210. for (const auto &lane : lanes)
  211. {
  212. bool isNear = false;
  213. const auto &coords = lane.lane_coords;
  214. double min_dist = lane.lane_width;
  215. double dist1 = computeMinDistanceToCurve(point, coords, 10);
  216. if (dist1 <= min_dist)
  217. {
  218. result.push_back(lane);
  219. isNear = true;
  220. break;
  221. }
  222. if (isNear)
  223. {
  224. continue;
  225. }
  226. }
  227. if (result.size() == 0)
  228. {
  229. // std::cerr << "没有找到符合条件的车道。" << std::endl;
  230. LaneInfo lInfo;
  231. lInfo.lane_id = 50000;
  232. lInfo.lane_length = 50001.0;
  233. lInfo.lane_type = 50002;
  234. lInfo.lane_width = 50003.0;
  235. lInfo.lane_turn = 50004;
  236. lInfo.lane_ref_link = 50005;
  237. lInfo.lane_speed_max = 180;
  238. lInfo.lane_speed_min = 0;
  239. lInfo.lane_coords = {Point(5.0, 5.0, 0.0)};
  240. result.push_back(lInfo);
  241. }
  242. return result;
  243. }
  244. // 根据位置获取指定范围内交通标识牌信息
  245. std::vector<SignalInfo> getSignals(const std::string &signcsvPath, const Point &point, double distance)
  246. {
  247. std::vector<SignalInfo> result;
  248. std::vector<SignalInfo> signals = readSignalsFromCSV(signcsvPath);
  249. if (signals.empty())
  250. {
  251. std::cerr << "没有读取到交通标牌信息。" << std::endl;
  252. return result;
  253. }
  254. std::map<double, SignalInfo> dist_to_sign;
  255. for (const auto &sign : signals)
  256. {
  257. Point centeroid(0.0, 0.0, 0.0);
  258. centeroid = calculateCentroid(sign.sign_coords);
  259. // 计算中心点到查询点的距离
  260. double dist = calculateDistance(point.pos_x, point.pos_y, centeroid.pos_x, centeroid.pos_y);
  261. std::cout << std::fixed << std::setprecision(8);
  262. // std::cout << "dist: " << dist << std::endl;
  263. // 如果满足距离要求,把满足条件的路口加入结果
  264. if (dist <= distance)
  265. {
  266. // result.push_back(sign);
  267. dist_to_sign[dist] = sign;
  268. }
  269. }
  270. for (const auto &dist_sign : dist_to_sign)
  271. {
  272. result.push_back(dist_sign.second);
  273. }
  274. if (result.size() == 0)
  275. {
  276. // std::cerr << "没有找到符合条件的交通标牌。" << std::endl;
  277. SignalInfo signal;
  278. signal.sign_id = 60000;
  279. signal.sign_type = 60001;
  280. signal.sign_type1 = 60002;
  281. signal.sign_ref_link = 60003;
  282. signal.sign_coords = {Point(6.0, 6.0, 0.0)};
  283. }
  284. return result;
  285. }
  286. // 根据位置获取指定范围内箭头信息
  287. std::vector<ArrowInfo> getArrow(const std::string &arrowcsvPath, const Point &point, double distance)
  288. {
  289. std::vector<ArrowInfo> result;
  290. std::vector<ArrowInfo> arrows = readArrowsFromCSV(arrowcsvPath);
  291. if (arrows.empty())
  292. {
  293. std::cerr << "没有读取到箭头信息。" << std::endl;
  294. return result;
  295. }
  296. std::map<double, ArrowInfo> dist_to_arrow;
  297. for (const auto &arrow : arrows)
  298. {
  299. Point centeroid(0.0, 0.0, 0.0);
  300. centeroid = calculateCentroid(arrow.arrow_coords);
  301. // 计算中心点到查询点的距离
  302. double dist = calculateDistance(point.pos_x, point.pos_y, centeroid.pos_x, centeroid.pos_y);
  303. std::cout << std::fixed << std::setprecision(8);
  304. // std::cout << "dist: " << dist << std::endl;
  305. if (dist > distance)
  306. {
  307. continue;
  308. }
  309. double dx = centeroid.pos_x - point.pos_x;
  310. double dy = centeroid.pos_y - point.pos_y;
  311. double heading_rad = point.heading;
  312. double hx = std::cos(heading_rad);
  313. double hy = std::sin(heading_rad);
  314. double dot_product = dx * hx + dy * hy;
  315. double mag_d = std::sqrt(dx * dx + dy * dy);
  316. double mag_h = std::sqrt(hx * hx + hy * hy);
  317. double cos_theta = dot_product / (mag_d * mag_h);
  318. double theta = std::acos(cos_theta);
  319. double angle_threshold = M_PI / 2;
  320. if (theta < angle_threshold)
  321. {
  322. // result.push_back(arrow);
  323. dist_to_arrow[dist] = arrow;
  324. }
  325. }
  326. for (const auto &dist_arrow : dist_to_arrow)
  327. {
  328. result.push_back(dist_arrow.second);
  329. }
  330. if (result.size() == 0)
  331. {
  332. // std::cerr << "没有找到符合条件的箭头。" << std::endl;
  333. ArrowInfo aInfo;
  334. aInfo.arrow_id = 70000;
  335. aInfo.arrow_ref_lane = 70001;
  336. aInfo.arrow_direction = {70002};
  337. aInfo.arrow_coords = {Point(7.0, 7.0, 0.0)};
  338. result.push_back(aInfo);
  339. }
  340. return result;
  341. }
  342. // 根据位置获取指定范围内车道停止线信息
  343. std::vector<StoplineInfo> getStopline(const std::string &stoplinecsvPath, const Point &point, double distance)
  344. {
  345. std::vector<StoplineInfo> result;
  346. std::vector<StoplineInfo> stoplines = readStopLinesFromCSV(stoplinecsvPath);
  347. if (stoplines.empty())
  348. {
  349. std::cerr << "没有读取到车道停止线信息。" << std::endl;
  350. }
  351. std::map<double, StoplineInfo> dist_to_stopline;
  352. for (const auto &stopline : stoplines)
  353. {
  354. Point centeroid(0.0, 0.0, 0.0);
  355. centeroid = calculateCentroid(stopline.stopline_coords);
  356. // 计算中心点到查询点的距离
  357. double dist = calculateDistance(point.pos_x, point.pos_y, centeroid.pos_x, centeroid.pos_y);
  358. std::cout << std::fixed << std::setprecision(8);
  359. // std::cout << "dist: " << dist << std::endl;
  360. if (dist > distance)
  361. {
  362. continue;
  363. }
  364. double dx = centeroid.pos_x - point.pos_x;
  365. double dy = centeroid.pos_y - point.pos_y;
  366. double heading_rad = point.heading;
  367. double hx = std::cos(heading_rad);
  368. double hy = std::sin(heading_rad);
  369. double dot_product = dx * hx + dy * hy;
  370. double mag_d = std::sqrt(dx * dx + dy * dy);
  371. double mag_h = std::sqrt(hx * hx + hy * hy);
  372. double cos_theta = dot_product / (mag_d * mag_h);
  373. double theta = std::acos(cos_theta);
  374. double angle_threshold = M_PI / 2;
  375. if (theta < angle_threshold)
  376. {
  377. // result.push_back(stopline);
  378. dist_to_stopline[dist] = stopline;
  379. }
  380. }
  381. for (const auto &dist_stopline : dist_to_stopline)
  382. {
  383. result.push_back(dist_stopline.second);
  384. }
  385. if (result.size() == 0)
  386. {
  387. // std::cerr << "没有找到符合条件的车道停止线。" << std::endl;
  388. StoplineInfo sInfo;
  389. sInfo.stopline_id = 80000;
  390. sInfo.stopline_ref_link = 80001;
  391. sInfo.stopline_ref_lane = {80002};
  392. sInfo.stopline_color = 80003;
  393. sInfo.stopline_type = 80004;
  394. sInfo.stopline_coords = {Point(8.0, 8.0, 0.0)};
  395. result.push_back(sInfo);
  396. }
  397. return result;
  398. }