RoadNetwork.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /// \file RoadNetwork.h
  2. /// \brief Definition of OpenPlanner's data types
  3. /// \author Hatem Darweesh
  4. /// \date May 19, 2016
  5. #ifndef ROADNETWORK_H_
  6. #define ROADNETWORK_H_
  7. #include <string>
  8. #include <vector>
  9. #include <sstream>
  10. #include "op_utility/UtilityH.h"
  11. #define OPENPLANNER_ENABLE_LOGS
  12. namespace PlannerHNS
  13. {
  14. enum DIRECTION_TYPE { FORWARD_DIR, FORWARD_LEFT_DIR, FORWARD_RIGHT_DIR,
  15. BACKWARD_DIR, BACKWARD_LEFT_DIR, BACKWARD_RIGHT_DIR, STANDSTILL_DIR};
  16. enum OBSTACLE_TYPE {SIDEWALK, TREE, CAR, TRUCK, HOUSE, PEDESTRIAN, CYCLIST, GENERAL_OBSTACLE};
  17. enum DRIVABLE_TYPE {DIRT, TARMAC, PARKINGAREA, INDOOR, GENERAL_AREA};
  18. enum GLOBAL_STATE_TYPE {G_WAITING_STATE, G_PLANING_STATE, G_FORWARD_STATE, G_BRANCHING_STATE, G_FINISH_STATE};
  19. enum STATE_TYPE {INITIAL_STATE, WAITING_STATE, FORWARD_STATE, STOPPING_STATE, EMERGENCY_STATE,
  20. TRAFFIC_LIGHT_STOP_STATE,TRAFFIC_LIGHT_WAIT_STATE, STOP_SIGN_STOP_STATE, STOP_SIGN_WAIT_STATE, FOLLOW_STATE, LANE_CHANGE_STATE, OBSTACLE_AVOIDANCE_STATE, GOAL_STATE, FINISH_STATE, YIELDING_STATE, BRANCH_LEFT_STATE, BRANCH_RIGHT_STATE};
  21. enum LIGHT_INDICATOR {INDICATOR_LEFT, INDICATOR_RIGHT, INDICATOR_BOTH , INDICATOR_NONE};
  22. enum SHIFT_POS {SHIFT_POS_PP = 0x60, SHIFT_POS_RR = 0x40, SHIFT_POS_NN = 0x20,
  23. SHIFT_POS_DD = 0x10, SHIFT_POS_BB = 0xA0, SHIFT_POS_SS = 0x0f, SHIFT_POS_UU = 0xff };
  24. enum ACTION_TYPE {FORWARD_ACTION, BACKWARD_ACTION, STOP_ACTION, LEFT_TURN_ACTION,
  25. RIGHT_TURN_ACTION, U_TURN_ACTION, SWERVE_ACTION, OVERTACK_ACTION, START_ACTION, SLOWDOWN_ACTION, CHANGE_DESTINATION, WAITING_ACTION, DESTINATION_REACHED, UNKOWN_ACTION};
  26. enum BEH_STATE_TYPE {BEH_FORWARD_STATE=0,BEH_STOPPING_STATE=1, BEH_BRANCH_LEFT_STATE=2, BEH_BRANCH_RIGHT_STATE=3, BEH_YIELDING_STATE=4, BEH_ACCELERATING_STATE=5, BEH_SLOWDOWN_STATE=6};
  27. enum SEGMENT_TYPE {NORMAL_ROAD_SEG, INTERSECTION_ROAD_SEG, UTURN_ROAD_SEG, EXIT_ROAD_SEG, MERGE_ROAD_SEG, HIGHWAY_ROAD_SEG};
  28. enum RoadSegmentType {NORMAL_ROAD, INTERSECTION_ROAD, UTURN_ROAD, EXIT_ROAD, MERGE_ROAD, HIGHWAY_ROAD};
  29. enum MARKING_TYPE {UNKNOWN_MARK, TEXT_MARK, AF_MARK, AL_MARK, AR_MARK, AFL_MARK, AFR_MARK, ALR_MARK, UTURN_MARK, NOUTURN_MARK};
  30. enum TrafficSignTypes {UNKNOWN_SIGN, STOP_SIGN, MAX_SPEED_SIGN, MIN_SPEED_SIGN};
  31. class Lane;
  32. class TrafficLight;
  33. class RoadSegment;
  34. class ObjTimeStamp
  35. {
  36. public:
  37. timespec tStamp;
  38. ObjTimeStamp()
  39. {
  40. tStamp.tv_nsec = 0;
  41. tStamp.tv_sec = 0;
  42. }
  43. };
  44. //class POINT2D
  45. //{
  46. //public:
  47. // double x;
  48. // double y;
  49. // double z;
  50. // POINT2D()
  51. // {
  52. // x=0;y=0;z=0;
  53. // }
  54. // POINT2D(double px, double py, double pz = 0)
  55. // {
  56. // x = px;
  57. // y = py;
  58. // z = pz;
  59. // }
  60. //};
  61. class GPSPoint
  62. {
  63. public:
  64. double lat, x;
  65. double lon, y;
  66. double alt, z;
  67. double dir, a;
  68. GPSPoint()
  69. {
  70. lat = x = 0;
  71. lon = y = 0;
  72. alt = z = 0;
  73. dir = a = 0;
  74. }
  75. GPSPoint(const double& x, const double& y, const double& z, const double& a)
  76. {
  77. this->x = x;
  78. this->y = y;
  79. this->z = z;
  80. this->a = a;
  81. lat = 0;
  82. lon = 0;
  83. alt = 0;
  84. dir = 0;
  85. }
  86. std::string ToString()
  87. {
  88. std::stringstream str;
  89. str.precision(12);
  90. str << "X:" << x << ", Y:" << y << ", Z:" << z << ", A:" << a << std::endl;
  91. str << "Lon:" << lon << ", Lat:" << lat << ", Alt:" << alt << ", Dir:" << dir << std::endl;
  92. return str.str();
  93. }
  94. };
  95. class RECTANGLE
  96. {
  97. public:
  98. GPSPoint bottom_left;
  99. GPSPoint top_right;
  100. double width;
  101. double length;
  102. bool bObstacle;
  103. inline bool PointInRect(GPSPoint p)
  104. {
  105. return p.x >= bottom_left.x && p.x <= top_right.x && p.y >= bottom_left.y && p.y <= top_right.y;
  106. }
  107. inline bool PointInsideRect(GPSPoint p)
  108. {
  109. return p.x > bottom_left.x && p.x < top_right.x && p.y > bottom_left.y && p.y < top_right.y;
  110. }
  111. inline bool HitTest(GPSPoint p)
  112. {
  113. return PointInRect(p) && bObstacle;
  114. }
  115. RECTANGLE()
  116. {
  117. width=0;
  118. length = 0;
  119. bObstacle = true;
  120. }
  121. virtual ~RECTANGLE(){}
  122. };
  123. class PolygonShape
  124. {
  125. public:
  126. std::vector<GPSPoint> points;
  127. inline int PointInsidePolygon(const PolygonShape& polygon,const GPSPoint& p)
  128. {
  129. int counter = 0;
  130. int i;
  131. double xinters;
  132. GPSPoint p1,p2;
  133. int N = polygon.points.size();
  134. if(N <=0 ) return -1;
  135. p1 = polygon.points.at(0);
  136. for (i=1;i<=N;i++)
  137. {
  138. p2 = polygon.points.at(i % N);
  139. if (p.y > MIN(p1.y,p2.y))
  140. {
  141. if (p.y <= MAX(p1.y,p2.y))
  142. {
  143. if (p.x <= MAX(p1.x,p2.x))
  144. {
  145. if (p1.y != p2.y)
  146. {
  147. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  148. if (p1.x == p2.x || p.x <= xinters)
  149. counter++;
  150. }
  151. }
  152. }
  153. }
  154. p1 = p2;
  155. }
  156. if (counter % 2 == 0)
  157. return 0;
  158. else
  159. return 1;
  160. }
  161. };
  162. class MapItem
  163. {
  164. public:
  165. int id;
  166. GPSPoint sp; //start point
  167. GPSPoint ep; // end point
  168. GPSPoint center;
  169. double c; //curvature
  170. double w; //width
  171. double l; //length
  172. std::string fileName; //
  173. std::vector<GPSPoint> polygon;
  174. MapItem(int ID, GPSPoint start, GPSPoint end, double curvature, double width, double length, std::string objName)
  175. {
  176. id = ID;
  177. sp = start;
  178. ep = end;
  179. c = curvature;
  180. w = width;
  181. l = length;
  182. fileName = objName;
  183. }
  184. MapItem()
  185. {
  186. id = 0; c = 0; w = 0; l = 0;
  187. }
  188. virtual ~MapItem(){}
  189. MapItem(const MapItem & cmi)
  190. {
  191. id = cmi.id;
  192. sp = cmi.sp;
  193. ep = cmi.ep;
  194. c = cmi.c;
  195. w = cmi.w;
  196. l = cmi.l;
  197. fileName = cmi.fileName;
  198. }
  199. MapItem &operator=(const MapItem &cmi)
  200. {
  201. this->id = cmi.id;
  202. this->sp = cmi.sp;
  203. this->ep = cmi.ep;
  204. this->c = cmi.c;
  205. this->w = cmi.w;
  206. this->l = cmi.l;
  207. this->fileName = cmi.fileName;
  208. return *this;
  209. }
  210. virtual int operator==(const MapItem &mi) const
  211. {
  212. return this->id == mi.id;
  213. }
  214. };
  215. class Obstacle : public MapItem
  216. {
  217. public:
  218. OBSTACLE_TYPE t;
  219. Obstacle(int ID, GPSPoint start, GPSPoint end, double curvature, double width, double length,OBSTACLE_TYPE type, std::string fileName ) : MapItem(ID, start, end, curvature, width, length, fileName)
  220. {
  221. t = type;
  222. }
  223. virtual ~Obstacle()
  224. {
  225. }
  226. Obstacle() : MapItem()
  227. {
  228. t = SIDEWALK;
  229. }
  230. Obstacle(const Obstacle& ob) : MapItem(ob)
  231. {
  232. t = ob.t;
  233. }
  234. Obstacle& operator=(const Obstacle& ob)
  235. {
  236. this->id = ob.id;
  237. this->sp = ob.sp;
  238. this->ep = ob.ep;
  239. this->c = ob.c;
  240. this->w = ob.w;
  241. this->l = ob.l;
  242. this->t = ob.t;
  243. this->fileName = ob.fileName;
  244. return *this;
  245. }
  246. virtual int operator==(const Obstacle &ob) const
  247. {
  248. return this->id == ob.id && this->t == ob.t;
  249. }
  250. };
  251. class DrivableArea : public MapItem
  252. {
  253. public:
  254. DRIVABLE_TYPE t; // drivable area type
  255. DrivableArea(int ID, GPSPoint start, GPSPoint end, double curvature, double width, double length,DRIVABLE_TYPE type, std::string fileName ) : MapItem( ID, start, end, curvature, width, length, fileName)
  256. {
  257. t = type;
  258. }
  259. virtual ~DrivableArea()
  260. {
  261. }
  262. DrivableArea() : MapItem()
  263. {
  264. t = PARKINGAREA;
  265. }
  266. DrivableArea(const DrivableArea& da) : MapItem(da)
  267. {
  268. t = da.t;
  269. }
  270. DrivableArea& operator=(const DrivableArea& da)
  271. {
  272. this->id = da.id;
  273. this->sp = da.sp;
  274. this->ep = da.ep;
  275. this->c = da.c;
  276. this->w = da.w;
  277. this->l = da.l;
  278. this->t = da.t;
  279. this->fileName = da.fileName;
  280. return *this;
  281. }
  282. virtual int operator==(const DrivableArea &da) const
  283. {
  284. return this->id == da.id && this->t == da.t;
  285. }
  286. };
  287. class Rotation
  288. {
  289. public:
  290. double x;
  291. double y;
  292. double z;
  293. double w;
  294. Rotation()
  295. {
  296. x = 0;
  297. y = 0;
  298. z = 0;
  299. w = 0;
  300. }
  301. };
  302. class WayPoint
  303. {
  304. public:
  305. GPSPoint pos;
  306. Rotation rot;
  307. double v;
  308. double cost;
  309. double timeCost;
  310. double totalReward;
  311. double collisionCost;
  312. double laneChangeCost;
  313. int laneId;
  314. int id;
  315. int LeftPointId;
  316. int RightPointId;
  317. int LeftLnId;
  318. int RightLnId;
  319. int stopLineID;
  320. DIRECTION_TYPE bDir;
  321. STATE_TYPE state;
  322. BEH_STATE_TYPE beh_state;
  323. int iOriginalIndex;
  324. Lane* pLane;
  325. WayPoint* pLeft;
  326. WayPoint* pRight;
  327. std::vector<int> toIds;
  328. std::vector<int> fromIds;
  329. std::vector<WayPoint*> pFronts;
  330. std::vector<WayPoint*> pBacks;
  331. std::vector<std::pair<ACTION_TYPE, double> > actionCost;
  332. int originalMapID;
  333. int gid;
  334. WayPoint()
  335. {
  336. id = 0;
  337. v = 0;
  338. cost = 0;
  339. laneId = -1;
  340. pLane = 0;
  341. pLeft = 0;
  342. pRight = 0;
  343. bDir = FORWARD_DIR;
  344. LeftPointId = 0;
  345. RightPointId = 0;
  346. LeftLnId = 0;
  347. RightLnId = 0;
  348. timeCost = 0;
  349. totalReward = 0;
  350. collisionCost = 0;
  351. laneChangeCost = 0;
  352. stopLineID = -1;
  353. state = INITIAL_STATE;
  354. beh_state = BEH_STOPPING_STATE;
  355. iOriginalIndex = 0;
  356. gid = 0;
  357. originalMapID = -1;
  358. }
  359. WayPoint(const double& x, const double& y, const double& z, const double& a)
  360. {
  361. pos.x = x;
  362. pos.y = y;
  363. pos.z = z;
  364. pos.a = a;
  365. id = 0;
  366. v = 0;
  367. cost = 0;
  368. laneId = -1;
  369. pLane = 0;
  370. pLeft = 0;
  371. pRight = 0;
  372. bDir = FORWARD_DIR;
  373. LeftPointId = 0;
  374. RightPointId = 0;
  375. LeftLnId = 0;
  376. RightLnId = 0;
  377. timeCost = 0;
  378. totalReward = 0;
  379. collisionCost = 0;
  380. laneChangeCost = 0;
  381. stopLineID = -1;
  382. iOriginalIndex = 0;
  383. state = INITIAL_STATE;
  384. beh_state = BEH_STOPPING_STATE;
  385. gid = 0;
  386. originalMapID = -1;
  387. }
  388. };
  389. class RelativeInfo
  390. {
  391. public:
  392. double perp_distance;
  393. double to_front_distance; //negative
  394. double from_back_distance;
  395. int iFront;
  396. int iBack;
  397. int iGlobalPath;
  398. WayPoint perp_point;
  399. double angle_diff; // degrees
  400. bool bBefore;
  401. bool bAfter;
  402. double after_angle;
  403. RelativeInfo()
  404. {
  405. after_angle = 0;
  406. bBefore = false;
  407. bAfter = false;
  408. perp_distance = 0;
  409. to_front_distance = 0;
  410. from_back_distance = 0;
  411. iFront = 0;
  412. iBack = 0;
  413. iGlobalPath = 0;
  414. angle_diff = 0;
  415. }
  416. };
  417. class Boundary //represent wayarea in vector map
  418. {
  419. public:
  420. int id;
  421. int roadId;
  422. std::vector<GPSPoint> points;
  423. RoadSegment* pSegment;
  424. Boundary()
  425. {
  426. id = 0;
  427. roadId =0;
  428. pSegment = nullptr;
  429. }
  430. };
  431. class Curb
  432. {
  433. public:
  434. int id;
  435. int laneId;
  436. int roadId;
  437. std::vector<GPSPoint> points;
  438. Lane* pLane;
  439. Curb()
  440. {
  441. id = 0;
  442. laneId =0;
  443. roadId =0;
  444. pLane = 0;
  445. }
  446. };
  447. class Crossing
  448. {
  449. public:
  450. int id;
  451. int roadId;
  452. std::vector<GPSPoint> points;
  453. RoadSegment* pSegment;
  454. Crossing()
  455. {
  456. id = 0;
  457. roadId =0;
  458. pSegment = nullptr;
  459. }
  460. };
  461. class StopLine
  462. {
  463. public:
  464. int id;
  465. int laneId;
  466. int roadId;
  467. int trafficLightID;
  468. int stopSignID;
  469. std::vector<GPSPoint> points;
  470. Lane* pLane;
  471. int linkID;
  472. StopLine()
  473. {
  474. id = 0;
  475. laneId =0;
  476. roadId =0;
  477. pLane = 0;
  478. trafficLightID = -1;
  479. stopSignID = -1;
  480. linkID = 0;
  481. }
  482. };
  483. class WaitingLine
  484. {
  485. public:
  486. int id;
  487. int laneId;
  488. int roadId;
  489. std::vector<GPSPoint> points;
  490. Lane* pLane;
  491. WaitingLine()
  492. {
  493. id = 0;
  494. laneId =0;
  495. roadId =0;
  496. pLane = 0;
  497. }
  498. };
  499. class TrafficSign
  500. {
  501. public:
  502. int id;
  503. int laneId;
  504. int roadId;
  505. GPSPoint pos;
  506. TrafficSignTypes signType;
  507. double value;
  508. double fromValue;
  509. double toValue;
  510. std::string strValue;
  511. timespec timeValue;
  512. timespec fromTimeValue;
  513. timespec toTimeValue;
  514. Lane* pLane;
  515. TrafficSign()
  516. {
  517. id = 0;
  518. laneId = 0;
  519. roadId = 0;
  520. signType = UNKNOWN_SIGN;
  521. value = 0;
  522. fromValue = 0;
  523. toValue = 0;
  524. // timeValue = 0;
  525. // fromTimeValue = 0;
  526. // toTimeValue = 0;
  527. pLane = 0;
  528. }
  529. };
  530. enum TrafficLightState {UNKNOWN_LIGHT, RED_LIGHT, GREEN_LIGHT, YELLOW_LIGHT, LEFT_GREEN, FORWARD_GREEN, RIGHT_GREEN, FLASH_YELLOW, FLASH_RED};
  531. class TrafficLight
  532. {
  533. public:
  534. int id;
  535. GPSPoint pos;
  536. TrafficLightState lightState;
  537. double stoppingDistance;
  538. std::vector<int> laneIds;
  539. std::vector<Lane*> pLanes;
  540. int linkID;
  541. TrafficLight()
  542. {
  543. stoppingDistance = 2;
  544. id = 0;
  545. lightState = GREEN_LIGHT;
  546. linkID = 0;
  547. }
  548. bool CheckLane(const int& laneId)
  549. {
  550. for(unsigned int i=0; i < laneIds.size(); i++)
  551. {
  552. if(laneId == laneIds.at(i))
  553. return true;
  554. }
  555. return false;
  556. }
  557. };
  558. class Marking
  559. {
  560. public:
  561. int id;
  562. int laneId;
  563. int roadId;
  564. MARKING_TYPE mark_type;
  565. GPSPoint center;
  566. std::vector<GPSPoint> points;
  567. Lane* pLane;
  568. Marking()
  569. {
  570. id = 0;
  571. laneId = 0;
  572. roadId = 0;
  573. mark_type = UNKNOWN_MARK;
  574. pLane = nullptr;
  575. }
  576. };
  577. class RoadSegment
  578. {
  579. public:
  580. int id;
  581. SEGMENT_TYPE roadType;
  582. Boundary boundary;
  583. Crossing start_crossing;
  584. Crossing finish_crossing;
  585. double avgWidth;
  586. std::vector<int> fromIds;
  587. std::vector<int> toIds;
  588. std::vector<Lane> Lanes;
  589. std::vector<RoadSegment*> fromLanes;
  590. std::vector<RoadSegment*> toLanes;
  591. RoadSegment()
  592. {
  593. id = 0;
  594. avgWidth = 0;
  595. roadType = NORMAL_ROAD_SEG;
  596. }
  597. };
  598. enum LaneType{NORMAL_LANE, MERGE_LANE, EXIT_LANE, BUS_LANE, BUS_STOP_LANE, EMERGENCY_LANE};
  599. class Lane
  600. {
  601. public:
  602. int id;
  603. int roadId;
  604. int areaId;
  605. int fromAreaId;
  606. int toAreaId;
  607. std::vector<int> fromIds;
  608. std::vector<int> toIds;
  609. int num; //lane number in the road segment from left to right
  610. double speed;
  611. double length;
  612. double dir;
  613. LaneType type;
  614. double width;
  615. std::vector<WayPoint> points;
  616. std::vector<TrafficLight> trafficlights;
  617. std::vector<StopLine> stopLines;
  618. WaitingLine waitingLine;
  619. std::vector<Lane*> fromLanes;
  620. std::vector<Lane*> toLanes;
  621. Lane* pLeftLane;
  622. Lane* pRightLane;
  623. RoadSegment * pRoad;
  624. Lane()
  625. {
  626. id = 0;
  627. num = 0;
  628. speed = 0;
  629. length = 0;
  630. dir = 0;
  631. type = NORMAL_LANE;
  632. width = 0;
  633. pLeftLane = 0;
  634. pRightLane = 0;
  635. pRoad = 0;
  636. roadId = 0;
  637. areaId = 0;
  638. fromAreaId = 0;
  639. toAreaId = 0;
  640. }
  641. };
  642. class RoadNetwork
  643. {
  644. public:
  645. std::vector<RoadSegment> roadSegments;
  646. std::vector<TrafficLight> trafficLights;
  647. std::vector<StopLine> stopLines;
  648. std::vector<Curb> curbs;
  649. std::vector<Boundary> boundaries;
  650. std::vector<Crossing> crossings;
  651. std::vector<Marking> markings;
  652. std::vector<TrafficSign> signs;
  653. };
  654. class VehicleState : public ObjTimeStamp
  655. {
  656. public:
  657. double speed;
  658. double steer;
  659. SHIFT_POS shift;
  660. VehicleState()
  661. {
  662. speed = 0;
  663. steer = 0;
  664. shift = SHIFT_POS_NN;
  665. }
  666. };
  667. class BehaviorState
  668. {
  669. public:
  670. STATE_TYPE state;
  671. double maxVelocity;
  672. double minVelocity;
  673. double stopDistance;
  674. double followVelocity;
  675. double followDistance;
  676. LIGHT_INDICATOR indicator;
  677. bool bNewPlan;
  678. int iTrajectory;
  679. BehaviorState()
  680. {
  681. state = INITIAL_STATE;
  682. maxVelocity = 0;
  683. minVelocity = 0;
  684. stopDistance = 0;
  685. followVelocity = 0;
  686. followDistance = 0;
  687. indicator = INDICATOR_NONE;
  688. bNewPlan = false;
  689. iTrajectory = -1;
  690. }
  691. };
  692. class DetectedObject
  693. {
  694. public:
  695. int id;
  696. std::string label;
  697. OBSTACLE_TYPE t;
  698. WayPoint center;
  699. WayPoint predicted_center;
  700. WayPoint noisy_center;
  701. STATE_TYPE predicted_behavior;
  702. std::vector<WayPoint> centers_list;
  703. std::vector<GPSPoint> contour;
  704. std::vector<std::vector<WayPoint> > predTrajectories;
  705. std::vector<WayPoint*> pClosestWaypoints;
  706. double w;
  707. double l;
  708. double h;
  709. double distance_to_center;
  710. double actual_speed;
  711. double actual_yaw;
  712. bool bDirection;
  713. bool bVelocity;
  714. int acceleration;
  715. int acceleration_desc;
  716. double acceleration_raw;
  717. LIGHT_INDICATOR indicator_state;
  718. int originalID;
  719. BEH_STATE_TYPE behavior_state;
  720. DetectedObject()
  721. {
  722. bDirection = false;
  723. bVelocity = false;
  724. acceleration = 0;
  725. acceleration_desc = 0;
  726. acceleration_raw = 0.0;
  727. id = 0;
  728. w = 0;
  729. l = 0;
  730. h = 0;
  731. t = GENERAL_OBSTACLE;
  732. distance_to_center = 0;
  733. predicted_behavior = INITIAL_STATE;
  734. actual_speed = 0;
  735. actual_yaw = 0;
  736. acceleration_desc = 0;
  737. acceleration_raw = 0.0;
  738. indicator_state = INDICATOR_NONE;
  739. originalID = -1;
  740. behavior_state = BEH_STOPPING_STATE;
  741. }
  742. };
  743. class PlanningParams
  744. {
  745. public:
  746. double maxSpeed;
  747. double minSpeed;
  748. double planningDistance;
  749. double microPlanDistance;
  750. double carTipMargin;
  751. double rollInMargin;
  752. double rollInSpeedFactor;
  753. double pathDensity;
  754. double rollOutDensity;
  755. int rollOutNumber;
  756. double horizonDistance;
  757. double minFollowingDistance; //should be bigger than Distance to follow
  758. double minDistanceToAvoid; // should be smaller than minFollowingDistance and larger than maxDistanceToAvoid
  759. double maxDistanceToAvoid; // should be smaller than minDistanceToAvoid
  760. double speedProfileFactor;
  761. double smoothingDataWeight;
  762. double smoothingSmoothWeight;
  763. double smoothingToleranceError;
  764. double stopSignStopTime;
  765. double additionalBrakingDistance;
  766. double verticalSafetyDistance;
  767. double horizontalSafetyDistancel;
  768. double giveUpDistance;
  769. int nReliableCount;
  770. bool enableLaneChange;
  771. bool enableSwerving;
  772. bool enableFollowing;
  773. bool enableHeadingSmoothing;
  774. bool enableTrafficLightBehavior;
  775. bool enableStopSignBehavior;
  776. bool enabTrajectoryVelocities;
  777. double minIndicationDistance;
  778. PlanningParams()
  779. {
  780. maxSpeed = 3;
  781. minSpeed = 0;
  782. planningDistance = 10000;
  783. microPlanDistance = 30;
  784. carTipMargin = 4.0;
  785. rollInMargin = 12.0;
  786. rollInSpeedFactor = 0.25;
  787. pathDensity = 0.25;
  788. rollOutDensity = 0.5;
  789. rollOutNumber = 4;
  790. horizonDistance = 120;
  791. minFollowingDistance = 35;
  792. minDistanceToAvoid = 15;
  793. maxDistanceToAvoid = 5;
  794. speedProfileFactor = 1.0;
  795. smoothingDataWeight = 0.47;
  796. smoothingSmoothWeight = 0.2;
  797. smoothingToleranceError = 0.05;
  798. stopSignStopTime = 2.0;
  799. additionalBrakingDistance = 10.0;
  800. verticalSafetyDistance = 0.0;
  801. horizontalSafetyDistancel = 0.0;
  802. giveUpDistance = -4;
  803. nReliableCount = 2;
  804. enableHeadingSmoothing = false;
  805. enableSwerving = false;
  806. enableFollowing = false;
  807. enableTrafficLightBehavior = false;
  808. enableLaneChange = false;
  809. enableStopSignBehavior = false;
  810. enabTrajectoryVelocities = false;
  811. minIndicationDistance = 15;
  812. }
  813. };
  814. class HMIPreCalculatedConditions
  815. {
  816. public:
  817. HMIPreCalculatedConditions()
  818. {
  819. }
  820. };
  821. class PreCalculatedConditions
  822. {
  823. public:
  824. //-------------------------------------------//
  825. //Global Goals
  826. int currentGoalID;
  827. int prevGoalID;
  828. //-------------------------------------------//
  829. //Following
  830. double distanceToNext;
  831. double velocityOfNext;
  832. //-------------------------------------------//
  833. //For Lane Change
  834. int iPrevSafeLane;
  835. int iCurrSafeLane;
  836. double distanceToGoBack;
  837. double timeToGoBack;
  838. double distanceToChangeLane;
  839. double timeToChangeLane;
  840. int currentLaneID;
  841. int originalLaneID;
  842. int targetLaneID;
  843. bool bUpcomingLeft;
  844. bool bUpcomingRight;
  845. bool bCanChangeLane;
  846. bool bTargetLaneSafe;
  847. //-------------------------------------------//
  848. //Traffic Lights & Stop Sign
  849. int currentStopSignID;
  850. int prevStopSignID;
  851. int currentTrafficLightID;
  852. int prevTrafficLightID;
  853. bool bTrafficIsRed; //On , off status
  854. //-------------------------------------------//
  855. //Swerving
  856. int iPrevSafeTrajectory;
  857. int iCurrSafeTrajectory;
  858. int iCentralTrajectory;
  859. bool bFullyBlock;
  860. LIGHT_INDICATOR indicator;
  861. //-------------------------------------------//
  862. //General
  863. bool bNewGlobalPath;
  864. bool bRePlan;
  865. double currentVelocity;
  866. double minStoppingDistance; //comfortably
  867. int bOutsideControl; // 0 waiting, 1 start, 2 Green Traffic Light, 3 Red Traffic Light, 5 Emergency Stop
  868. bool bGreenOutsideControl;
  869. std::vector<double> stoppingDistances;
  870. double distanceToStop()
  871. {
  872. if(stoppingDistances.size()==0) return 0;
  873. double minS = stoppingDistances.at(0);
  874. for(unsigned int i=0; i< stoppingDistances.size(); i++)
  875. {
  876. if(stoppingDistances.at(i) < minS)
  877. minS = stoppingDistances.at(i);
  878. }
  879. return minS;
  880. }
  881. PreCalculatedConditions()
  882. {
  883. currentGoalID = 0;
  884. prevGoalID = -1;
  885. currentVelocity = 0;
  886. minStoppingDistance = 1;
  887. bOutsideControl = 0;
  888. bGreenOutsideControl = false;
  889. //distance to stop
  890. distanceToNext = -1;
  891. velocityOfNext = 0;
  892. currentStopSignID = -1;
  893. prevStopSignID = -1;
  894. currentTrafficLightID = -1;
  895. prevTrafficLightID = -1;
  896. bTrafficIsRed = false;
  897. iCurrSafeTrajectory = -1;
  898. bFullyBlock = false;
  899. iPrevSafeTrajectory = -1;
  900. iCentralTrajectory = -1;
  901. bRePlan = false;
  902. bNewGlobalPath = false;
  903. bCanChangeLane = false;
  904. distanceToGoBack = 0;
  905. timeToGoBack = 0;
  906. distanceToChangeLane = 0;
  907. timeToChangeLane = 0;
  908. bTargetLaneSafe = true;
  909. bUpcomingLeft = false;
  910. bUpcomingRight = false;
  911. targetLaneID = -1;
  912. currentLaneID = -1;
  913. originalLaneID = -1;
  914. iCurrSafeLane = -1;
  915. iPrevSafeLane = -1;
  916. indicator = INDICATOR_NONE;
  917. }
  918. virtual ~PreCalculatedConditions(){}
  919. std::string ToStringHeader()
  920. {
  921. return "Time:General>>:currentVelocity:distanceToStop:minStoppingDistance:bStartBehaviorGenerator:bGoalReached:"
  922. "Following>>:velocityOfNext:distanceToNext:"
  923. "TrafficLight>>:currentTrafficLightID:bTrafficIsRed:"
  924. "Swerving>>:iSafeTrajectory:bFullyBlock:";
  925. }
  926. std::string ToString(STATE_TYPE beh)
  927. {
  928. std::string str = "Unknown";
  929. switch(beh)
  930. {
  931. case PlannerHNS::INITIAL_STATE:
  932. str = "Init";
  933. break;
  934. case PlannerHNS::WAITING_STATE:
  935. str = "Waiting";
  936. break;
  937. case PlannerHNS::FORWARD_STATE:
  938. str = "Forward";
  939. break;
  940. case PlannerHNS::STOPPING_STATE:
  941. str = "Stop";
  942. break;
  943. case PlannerHNS::FINISH_STATE:
  944. str = "End";
  945. break;
  946. case PlannerHNS::FOLLOW_STATE:
  947. str = "Follow";
  948. break;
  949. case PlannerHNS::OBSTACLE_AVOIDANCE_STATE:
  950. str = "Swerving";
  951. break;
  952. case PlannerHNS::TRAFFIC_LIGHT_STOP_STATE:
  953. str = "Light Stop";
  954. break;
  955. case PlannerHNS::TRAFFIC_LIGHT_WAIT_STATE:
  956. str = "Light Wait";
  957. break;
  958. case PlannerHNS::STOP_SIGN_STOP_STATE:
  959. str = "Sign Stop";
  960. break;
  961. case PlannerHNS::STOP_SIGN_WAIT_STATE:
  962. str = "Sign Wait";
  963. break;
  964. default:
  965. str = "Unknown";
  966. break;
  967. }
  968. return str;
  969. }
  970. };
  971. class TrajectoryCost
  972. {
  973. public:
  974. int index;
  975. int relative_index;
  976. double closest_obj_velocity;
  977. double distance_from_center;
  978. double priority_cost; //0 to 1
  979. double transition_cost; // 0 to 1
  980. double closest_obj_cost; // 0 to 1
  981. double cost;
  982. double closest_obj_distance;
  983. int lane_index;
  984. double lane_change_cost;
  985. double lateral_cost;
  986. double longitudinal_cost;
  987. bool bBlocked;
  988. std::vector<std::pair<int, double> > lateral_costs;
  989. TrajectoryCost()
  990. {
  991. lane_index = -1;
  992. index = -1;
  993. relative_index = -100;
  994. closest_obj_velocity = 0;
  995. priority_cost = 0;
  996. transition_cost = 0;
  997. closest_obj_cost = 0;
  998. distance_from_center = 0;
  999. cost = 0;
  1000. closest_obj_distance = -1;
  1001. lane_change_cost = 0;
  1002. lateral_cost = 0;
  1003. longitudinal_cost = 0;
  1004. bBlocked = false;
  1005. }
  1006. std::string ToString()
  1007. {
  1008. std::ostringstream str;
  1009. str.precision(4);
  1010. str << "LI : " << lane_index;
  1011. str << ", In : " << relative_index;
  1012. str << ", Co : " << cost;
  1013. str << ", Pr : " << priority_cost;
  1014. str << ", Tr : " << transition_cost;
  1015. str << ", La : " << lateral_cost;
  1016. str << ", Lo : " << longitudinal_cost;
  1017. str << ", Ln : " << lane_change_cost;
  1018. str << ", Bl : " << bBlocked;
  1019. str << "\n";
  1020. for (unsigned int i=0; i<lateral_costs.size(); i++ )
  1021. {
  1022. str << " - (" << lateral_costs.at(i).first << ", " << lateral_costs.at(i).second << ")";
  1023. }
  1024. return str.str();
  1025. }
  1026. };
  1027. class OccupancyToGridMap
  1028. {
  1029. public:
  1030. int width;
  1031. int length;
  1032. double res;
  1033. WayPoint center;
  1034. OccupancyToGridMap(const int& _width, const int& _length, const double& _res, const WayPoint& _center)
  1035. {
  1036. width = _width;
  1037. length = _length;
  1038. res = _res;
  1039. center = _center;
  1040. }
  1041. OccupancyToGridMap()
  1042. {
  1043. width = 0;
  1044. length = 0;
  1045. res = 0.0;
  1046. }
  1047. bool GetCellIndexFromPoint(const GPSPoint& p, const std::vector<int>& data, int& _cell)
  1048. {
  1049. int col = floor(p.x / res);
  1050. int row = floor(p.y / res);
  1051. int index = -1;
  1052. if(row >= 0 && row < length && col >=0 && col < width)
  1053. {
  1054. index = get2dIndex(row,col);
  1055. if(index >= 0 && index < (int)data.size())
  1056. {
  1057. _cell = data.at((unsigned int)index);
  1058. //printf("Cell Info: P(%f,%f) , D(%f,%f), G(%d,%d), index = %d \n", p.x, p.y, p.x-center.pos.x, p.y-center.pos.y, col, row , index);
  1059. return true;
  1060. }
  1061. }
  1062. if(row+1 >= 0 && row+1 < length && col >=0 && col < width)
  1063. {
  1064. index = get2dIndex(row+1,col);
  1065. if(index >= 0 && index < (int)data.size())
  1066. {
  1067. _cell = data.at((unsigned int)index);
  1068. return true;
  1069. }
  1070. }
  1071. if(row >= 0 && row < length && col+1 >=0 && col+1 < width)
  1072. {
  1073. index = get2dIndex(row,col+1);
  1074. if(index >= 0 && index < (int)data.size())
  1075. {
  1076. _cell = data.at((unsigned int)index);
  1077. return true;
  1078. }
  1079. }
  1080. if(row-1 >= 0 && row-1 < length && col >=0 && col < width)
  1081. {
  1082. index = get2dIndex(row-1,col);
  1083. if(index >= 0 && index < (int)data.size())
  1084. {
  1085. _cell = data.at((unsigned int)index);
  1086. return true;
  1087. }
  1088. }
  1089. if(row >= 0 && row < length && col-1 >=0 && col-1 < width)
  1090. {
  1091. index = get2dIndex(row,col-1);
  1092. if(index >= 0 && index < (int)data.size())
  1093. {
  1094. _cell = data.at((unsigned int)index);
  1095. return true;
  1096. }
  1097. }
  1098. if(row+1 >= 0 && row+1 < length && col+1 >=0 && col+1 < width)
  1099. {
  1100. index = get2dIndex(row+1,col+1);
  1101. if(index >= 0 && index < (int)data.size())
  1102. {
  1103. _cell = data.at((unsigned int)index);
  1104. return true;
  1105. }
  1106. }
  1107. if(row-1 >= 0 && row-1 < length && col-1 >=0 && col-1 < width)
  1108. {
  1109. index = get2dIndex(row-1,col-1);
  1110. if(index >= 0 && index < (int)data.size())
  1111. {
  1112. _cell = data.at((unsigned int)index);
  1113. return true;
  1114. }
  1115. }
  1116. if(row-1 >= 0 && row-1 < length && col+1 >=0 && col+1 < width)
  1117. {
  1118. index = get2dIndex(row-1,col+1);
  1119. if(index >= 0 && index < (int)data.size())
  1120. {
  1121. _cell = data.at((unsigned int)index);
  1122. return true;
  1123. }
  1124. }
  1125. if(row+1 >= 0 && row+1 < length && col-1 >=0 && col-1 < width)
  1126. {
  1127. index = get2dIndex(row+1,col-1);
  1128. if(index >= 0 && index < (int)data.size())
  1129. {
  1130. _cell = data.at((unsigned int)index);
  1131. return true;
  1132. }
  1133. }
  1134. //printf("Error Getting Cell with Info: P(%f,%f) , C(%d,%d), index = %d \n", p.x, p.y, row, col, index);
  1135. return false;
  1136. }
  1137. private:
  1138. int get2dIndex(const int& r,const int& c)
  1139. {
  1140. return ((r*width) + c);
  1141. }
  1142. };
  1143. class ParticleInfo
  1144. {
  1145. public:
  1146. double vel;
  1147. int acl; //slow down -1 braking , 0 cruising , 1 accelerating
  1148. PlannerHNS::LIGHT_INDICATOR indicator;
  1149. PlannerHNS::STATE_TYPE state;
  1150. ParticleInfo()
  1151. {
  1152. vel = 0;
  1153. acl = 0;
  1154. indicator = PlannerHNS::INDICATOR_NONE;
  1155. state = PlannerHNS::FORWARD_STATE;
  1156. }
  1157. };
  1158. }
  1159. #endif /* ROADNETWORK_H_ */