MappingHelpers.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445
  1. /// \file MappingHelpers.cpp
  2. /// \brief Helper functions for mapping operation such as (load and initialize vector maps , convert map from one format to another, .. )
  3. /// \author Hatem Darweesh
  4. /// \date Jul 2, 2016
  5. #include "op_planner/MappingHelpers.h"
  6. #include "op_planner/MatrixOperations.h"
  7. #include "op_planner/PlanningHelpers.h"
  8. #include <float.h>
  9. #include "math.h"
  10. #include <fstream>
  11. using namespace UtilityHNS;
  12. using namespace std;
  13. #define RIGHT_INITIAL_TURNS_COST 0
  14. #define LEFT_INITIAL_TURNS_COST 0
  15. #define DEBUG_MAP_PARSING 0
  16. #define DEFAULT_REF_VELOCITY 60 //km/h
  17. namespace PlannerHNS
  18. {
  19. int MappingHelpers::g_max_point_id = 0;
  20. int MappingHelpers::g_max_lane_id = 0;
  21. int MappingHelpers::g_max_stop_line_id = 0;
  22. int MappingHelpers::g_max_traffic_light_id = 0;
  23. double MappingHelpers::m_USING_VER_ZERO = 0;
  24. MappingHelpers::MappingHelpers() {
  25. }
  26. MappingHelpers::~MappingHelpers() {
  27. }
  28. GPSPoint MappingHelpers::GetTransformationOrigin(const int& bToyotaCityMap)
  29. {
  30. // if(bToyotaCityMap == 1)
  31. // return GPSPoint(-3700, 99427, -88,0); //toyota city
  32. // else if(bToyotaCityMap == 2)
  33. // return GPSPoint(14805.945, 84680.211, -39.59, 0); // for moriyama map
  34. // else
  35. return GPSPoint();
  36. //return GPSPoint(18221.1, 93546.1, -36.19, 0);
  37. }
  38. Lane* MappingHelpers::GetLaneById(const int& id,RoadNetwork& map)
  39. {
  40. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  41. {
  42. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  43. {
  44. if(map.roadSegments.at(rs).Lanes.at(i).id == id)
  45. return &map.roadSegments.at(rs).Lanes.at(i);
  46. }
  47. }
  48. return nullptr;
  49. }
  50. int MappingHelpers::GetLaneIdByWaypointId(const int& id,std::vector<Lane>& lanes)
  51. {
  52. for(unsigned int in_l= 0; in_l < lanes.size(); in_l++)
  53. {
  54. for(unsigned int in_p = 0; in_p<lanes.at(in_l).points.size(); in_p++)
  55. {
  56. if(id == lanes.at(in_l).points.at(in_p).id)
  57. {
  58. return lanes.at(in_l).points.at(in_p).laneId;
  59. }
  60. }
  61. }
  62. return 0;
  63. }
  64. int MappingHelpers::ReplaceMyID(int& id,const std::vector<std::pair<int,int> >& rep_list)
  65. {
  66. for(unsigned int i=0; i < rep_list.size(); i++)
  67. {
  68. if(rep_list.at(i).first == id)
  69. {
  70. id = rep_list.at(i).second;
  71. return id;
  72. }
  73. }
  74. return -1;
  75. }
  76. void MappingHelpers::ConstructRoadNetworkFromROSMessage(const std::vector<UtilityHNS::AisanLanesFileReader::AisanLane>& lanes_data,
  77. const std::vector<UtilityHNS::AisanPointsFileReader::AisanPoints>& points_data,
  78. const std::vector<UtilityHNS::AisanCenterLinesFileReader::AisanCenterLine>& dt_data,
  79. const std::vector<UtilityHNS::AisanIntersectionFileReader::AisanIntersection>& intersection_data,
  80. const std::vector<UtilityHNS::AisanAreasFileReader::AisanArea>& area_data,
  81. const std::vector<UtilityHNS::AisanLinesFileReader::AisanLine>& line_data,
  82. const std::vector<UtilityHNS::AisanStopLineFileReader::AisanStopLine>& stop_line_data,
  83. const std::vector<UtilityHNS::AisanSignalFileReader::AisanSignal>& signal_data,
  84. const std::vector<UtilityHNS::AisanVectorFileReader::AisanVector>& vector_data,
  85. const std::vector<UtilityHNS::AisanCurbFileReader::AisanCurb>& curb_data,
  86. const std::vector<UtilityHNS::AisanRoadEdgeFileReader::AisanRoadEdge>& roadedge_data,
  87. const std::vector<UtilityHNS::AisanWayareaFileReader::AisanWayarea>& wayarea_data,
  88. const std::vector<UtilityHNS::AisanCrossWalkFileReader::AisanCrossWalk>& crosswalk_data,
  89. const std::vector<UtilityHNS::AisanNodesFileReader::AisanNode>& nodes_data,
  90. const std::vector<UtilityHNS::AisanDataConnFileReader::DataConn>& conn_data,
  91. const GPSPoint& origin, RoadNetwork& map, const bool& bSpecialFlag,
  92. const bool& bFindLaneChangeLanes, const bool& bFindCurbsAndWayArea)
  93. {
  94. vector<Lane> roadLanes;
  95. Lane lane_obj;
  96. int laneIDSeq = 0;
  97. WayPoint prevWayPoint;
  98. UtilityHNS::AisanLanesFileReader::AisanLane prev_lane_point;
  99. UtilityHNS::AisanLanesFileReader::AisanLane curr_lane_point;
  100. UtilityHNS::AisanLanesFileReader::AisanLane next_lane_point;
  101. vector<pair<int,int> > id_replace_list;
  102. for(unsigned int l= 0; l < lanes_data.size(); l++)
  103. {
  104. curr_lane_point = lanes_data.at(l);
  105. curr_lane_point.originalMapID = -1;
  106. if(l+1 < lanes_data.size())
  107. {
  108. next_lane_point = lanes_data.at(l+1);
  109. if(curr_lane_point.FLID == next_lane_point.LnID && curr_lane_point.DID == next_lane_point.DID)
  110. {
  111. next_lane_point.BLID = curr_lane_point.BLID;
  112. if(next_lane_point.LaneDir == 'F')
  113. next_lane_point.LaneDir = curr_lane_point.LaneDir;
  114. if(curr_lane_point.BLID2 != 0)
  115. {
  116. if(next_lane_point.BLID2 == 0) next_lane_point.BLID2 = curr_lane_point.BLID2;
  117. else if(next_lane_point.BLID3 == 0) next_lane_point.BLID3 = curr_lane_point.BLID2;
  118. else if(next_lane_point.BLID4 == 0) next_lane_point.BLID4 = curr_lane_point.BLID2;
  119. }
  120. if(curr_lane_point.BLID3 != 0)
  121. {
  122. if(next_lane_point.BLID2 == 0) next_lane_point.BLID2 = curr_lane_point.BLID3;
  123. else if(next_lane_point.BLID3 == 0) next_lane_point.BLID3 = curr_lane_point.BLID3;
  124. else if(next_lane_point.BLID4 == 0) next_lane_point.BLID4 = curr_lane_point.BLID3;
  125. }
  126. if(curr_lane_point.BLID3 != 0)
  127. {
  128. if(next_lane_point.BLID2 == 0) next_lane_point.BLID2 = curr_lane_point.BLID4;
  129. else if(next_lane_point.BLID3 == 0) next_lane_point.BLID3 = curr_lane_point.BLID4;
  130. else if(next_lane_point.BLID4 == 0) next_lane_point.BLID4 = curr_lane_point.BLID4;
  131. }
  132. if(curr_lane_point.FLID2 != 0)
  133. {
  134. if(next_lane_point.FLID2 == 0) next_lane_point.FLID2 = curr_lane_point.FLID2;
  135. else if(next_lane_point.FLID3 == 0) next_lane_point.FLID3 = curr_lane_point.FLID2;
  136. else if(next_lane_point.FLID4 == 0) next_lane_point.FLID4 = curr_lane_point.FLID2;
  137. }
  138. if(curr_lane_point.FLID3 != 0)
  139. {
  140. if(next_lane_point.FLID2 == 0) next_lane_point.FLID2 = curr_lane_point.FLID3;
  141. else if(next_lane_point.FLID3 == 0) next_lane_point.FLID3 = curr_lane_point.FLID3;
  142. else if(next_lane_point.FLID4 == 0) next_lane_point.FLID4 = curr_lane_point.FLID3;
  143. }
  144. if(curr_lane_point.FLID3 != 0)
  145. {
  146. if(next_lane_point.FLID2 == 0) next_lane_point.FLID2 = curr_lane_point.FLID4;
  147. else if(next_lane_point.FLID3 == 0) next_lane_point.FLID3 = curr_lane_point.FLID4;
  148. else if(next_lane_point.FLID4 == 0) next_lane_point.FLID4 = curr_lane_point.FLID4;
  149. }
  150. if(prev_lane_point.FLID == curr_lane_point.LnID)
  151. prev_lane_point.FLID = next_lane_point.LnID;
  152. id_replace_list.push_back(make_pair(curr_lane_point.LnID, next_lane_point.LnID));
  153. int originalMapID = curr_lane_point.LnID;
  154. curr_lane_point = next_lane_point;
  155. curr_lane_point.originalMapID = originalMapID;
  156. l++;
  157. }
  158. }
  159. if(curr_lane_point.LnID != prev_lane_point.FLID)
  160. {
  161. if(laneIDSeq != 0) //first lane
  162. {
  163. lane_obj.toIds.push_back(prev_lane_point.FLID);
  164. roadLanes.push_back(lane_obj);
  165. // if(lane_obj.points.size() <= 1)
  166. // prev_FLID = 0;
  167. }
  168. laneIDSeq++;
  169. lane_obj = Lane();
  170. lane_obj.speed = curr_lane_point.LimitVel;
  171. lane_obj.id = curr_lane_point.LnID;
  172. lane_obj.fromIds.push_back(curr_lane_point.BLID);
  173. lane_obj.roadId = laneIDSeq;
  174. }
  175. WayPoint wp;
  176. bool bFound = GetWayPoint(curr_lane_point.LnID, lane_obj.id, curr_lane_point.RefVel,curr_lane_point.DID,
  177. dt_data, points_data,origin, wp);
  178. wp.originalMapID = curr_lane_point.originalMapID;
  179. if(curr_lane_point.LaneDir == 'L')
  180. {
  181. wp.actionCost.push_back(make_pair(LEFT_TURN_ACTION, LEFT_INITIAL_TURNS_COST));
  182. //std::cout << " Left Lane : " << curr_lane_point.LnID << std::endl ;
  183. }
  184. else if(curr_lane_point.LaneDir == 'R')
  185. {
  186. wp.actionCost.push_back(make_pair(RIGHT_TURN_ACTION, RIGHT_INITIAL_TURNS_COST));
  187. //std::cout << " Right Lane : " << curr_lane_point.LnID << std::endl ;
  188. }
  189. else
  190. {
  191. wp.actionCost.push_back(make_pair(FORWARD_ACTION, 0));
  192. }
  193. wp.fromIds.push_back(curr_lane_point.BLID);
  194. wp.toIds.push_back(curr_lane_point.FLID);
  195. //if(curr_lane_point.JCT > 0)
  196. if(curr_lane_point.FLID2 > 0)
  197. {
  198. lane_obj.toIds.push_back(curr_lane_point.FLID2);
  199. wp.toIds.push_back(curr_lane_point.FLID2);
  200. }
  201. if(curr_lane_point.FLID3 > 0)
  202. {
  203. lane_obj.toIds.push_back(curr_lane_point.FLID3);
  204. wp.toIds.push_back(curr_lane_point.FLID3);
  205. }
  206. if(curr_lane_point.FLID4 > 0)
  207. {
  208. lane_obj.toIds.push_back(curr_lane_point.FLID4);
  209. wp.toIds.push_back(curr_lane_point.FLID4);
  210. }
  211. if(curr_lane_point.BLID2 > 0)
  212. {
  213. lane_obj.fromIds.push_back(curr_lane_point.BLID2);
  214. wp.fromIds.push_back(curr_lane_point.BLID2);
  215. }
  216. if(curr_lane_point.BLID3 > 0)
  217. {
  218. lane_obj.fromIds.push_back(curr_lane_point.BLID3);
  219. wp.fromIds.push_back(curr_lane_point.BLID3);
  220. }
  221. if(curr_lane_point.BLID4 > 0)
  222. {
  223. lane_obj.fromIds.push_back(curr_lane_point.BLID4);
  224. wp.fromIds.push_back(curr_lane_point.BLID4);
  225. }
  226. //if(prev_lane_point.DID == curr_lane_point.DID && curr_lane_point.LnID == prev_lane_point.FLID)
  227. // if(prevWayPoint.pos.x == wp.pos.x && prevWayPoint.pos.y == wp.pos.y)
  228. // {
  229. // //if((prev_lane_point.FLID2 != 0 && curr_lane_point.FLID2 != 0) || (prev_lane_point.FLID3 != 0 && curr_lane_point.FLID3 != 0) || (prev_lane_point.FLID4 != 0 && curr_lane_point.FLID4 != 0))
  230. // {
  231. // cout << "Prev WP, LnID: " << prev_lane_point.LnID << ",BLID: " << prev_lane_point.BLID << ",FLID: " << prev_lane_point.FLID << ",DID: " << prev_lane_point.DID
  232. // << ", Begin: " << prev_lane_point.BLID2 << "," << prev_lane_point.BLID3 << "," << prev_lane_point.BLID4
  233. // << ", End: " << prev_lane_point.FLID2 << "," << prev_lane_point.FLID3 << "," << prev_lane_point.FLID4 << ": " << prev_lane_point.LaneDir << endl;
  234. // cout << "Curr WP, LnID: " << curr_lane_point.LnID << ",BLID: " << curr_lane_point.BLID << ",FLID: " << curr_lane_point.FLID << ",DID: " << curr_lane_point.DID
  235. // << ", Begin: " << curr_lane_point.BLID2 << "," << curr_lane_point.BLID3 << "," << curr_lane_point.BLID4
  236. // << ", End: " << curr_lane_point.FLID2 << "," <<curr_lane_point.FLID3 << "," << curr_lane_point.FLID4 << ": " << curr_lane_point.LaneDir << endl << endl;
  237. // }
  238. // }
  239. if(bFound)
  240. {
  241. lane_obj.points.push_back(wp);
  242. prevWayPoint = wp;
  243. }
  244. else
  245. cout << " Strange ! point is not in the map !! " << endl;
  246. prev_lane_point = curr_lane_point;
  247. }
  248. //delete first two lanes !!!!! Don't know why , you don't know why ! , these two line cost you a lot .. ! why why , works for toyota map , but not with moriyama
  249. if(bSpecialFlag)
  250. {
  251. if(roadLanes.size() > 0)
  252. roadLanes.erase(roadLanes.begin()+0);
  253. if(roadLanes.size() > 0)
  254. roadLanes.erase(roadLanes.begin()+0);
  255. }
  256. roadLanes.push_back(lane_obj);
  257. for(unsigned int i =0; i < roadLanes.size(); i++)
  258. {
  259. Lane* pL = &roadLanes.at(i);
  260. ReplaceMyID(pL->id, id_replace_list);
  261. for(unsigned int j = 0 ; j < pL->fromIds.size(); j++)
  262. {
  263. int id = ReplaceMyID(pL->fromIds.at(j), id_replace_list);
  264. if(id != -1)
  265. pL->fromIds.at(j) = id;
  266. }
  267. for(unsigned int j = 0 ; j < pL->toIds.size(); j++)
  268. {
  269. int id = ReplaceMyID(pL->toIds.at(j), id_replace_list);
  270. if(id != -1)
  271. pL->toIds.at(j) = id;
  272. }
  273. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  274. {
  275. ReplaceMyID(pL->points.at(j).id, id_replace_list);
  276. ReplaceMyID(pL->points.at(j).laneId, id_replace_list);
  277. for(unsigned int jj = 0 ; jj < pL->points.at(j).fromIds.size(); jj++)
  278. {
  279. int id = ReplaceMyID(pL->points.at(j).fromIds.at(jj), id_replace_list);
  280. if(id != -1)
  281. pL->points.at(j).fromIds.at(jj) = id;
  282. }
  283. for(unsigned int jj = 0 ; jj < pL->points.at(j).toIds.size(); jj++)
  284. {
  285. int id = ReplaceMyID(pL->points.at(j).toIds.at(jj), id_replace_list);
  286. if(id != -1)
  287. pL->points.at(j).toIds.at(jj) = id;
  288. }
  289. }
  290. }
  291. //Link Lanes and lane's waypoints by pointers
  292. //For each lane, the previous code set the fromId as the id of the last waypoint of the previos lane.
  293. //here we fix that by finding from each fromID the corresponding point and replace the fromId by the LaneID associated with that point.
  294. for(unsigned int l= 0; l < roadLanes.size(); l++)
  295. {
  296. for(unsigned int fp = 0; fp< roadLanes.at(l).fromIds.size(); fp++)
  297. {
  298. roadLanes.at(l).fromIds.at(fp) = GetLaneIdByWaypointId(roadLanes.at(l).fromIds.at(fp), roadLanes);
  299. }
  300. for(unsigned int tp = 0; tp< roadLanes.at(l).toIds.size(); tp++)
  301. {
  302. roadLanes.at(l).toIds.at(tp) = GetLaneIdByWaypointId(roadLanes.at(l).toIds.at(tp), roadLanes);
  303. }
  304. double sum_a = 0;
  305. for(unsigned int j = 0 ; j < roadLanes.at(l).points.size(); j++)
  306. {
  307. sum_a += roadLanes.at(l).points.at(j).pos.a;
  308. }
  309. roadLanes.at(l).dir = sum_a/(double)roadLanes.at(l).points.size();
  310. }
  311. //map has one road segment
  312. RoadSegment roadSegment1;
  313. roadSegment1.id = 1;
  314. roadSegment1.Lanes = roadLanes;
  315. map.roadSegments.push_back(roadSegment1);
  316. //Link Lanes and lane's waypoints by pointers
  317. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  318. {
  319. //Link Lanes
  320. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  321. {
  322. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  323. for(unsigned int j = 0 ; j < pL->fromIds.size(); j++)
  324. {
  325. for(unsigned int l= 0; l < map.roadSegments.at(rs).Lanes.size(); l++)
  326. {
  327. if(map.roadSegments.at(rs).Lanes.at(l).id == pL->fromIds.at(j))
  328. {
  329. pL->fromLanes.push_back(&map.roadSegments.at(rs).Lanes.at(l));
  330. }
  331. }
  332. }
  333. for(unsigned int j = 0 ; j < pL->toIds.size(); j++)
  334. {
  335. for(unsigned int l= 0; l < map.roadSegments.at(rs).Lanes.size(); l++)
  336. {
  337. if(map.roadSegments.at(rs).Lanes.at(l).id == pL->toIds.at(j))
  338. {
  339. pL->toLanes.push_back(&map.roadSegments.at(rs).Lanes.at(l));
  340. }
  341. }
  342. }
  343. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  344. {
  345. pL->points.at(j).pLane = pL;
  346. }
  347. }
  348. }
  349. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  350. {
  351. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  352. {
  353. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  354. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  355. {
  356. if(pL->points.at(j).actionCost.size() > 0)
  357. {
  358. if(pL->points.at(j).actionCost.at(0).first == LEFT_TURN_ACTION)
  359. {
  360. AssignActionCostToLane(pL, LEFT_TURN_ACTION, LEFT_INITIAL_TURNS_COST);
  361. break;
  362. }
  363. else if(pL->points.at(j).actionCost.at(0).first == RIGHT_TURN_ACTION)
  364. {
  365. AssignActionCostToLane(pL, RIGHT_TURN_ACTION, RIGHT_INITIAL_TURNS_COST);
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. if(bFindLaneChangeLanes)
  373. {
  374. cout << " >> Extract Lane Change Information... " << endl;
  375. FindAdjacentLanes(map);
  376. }
  377. //Extract Signals and StopLines
  378. // Signals
  379. ExtractSignalData(signal_data, vector_data, points_data, origin, map);
  380. //Stop Lines
  381. ExtractStopLinesData(stop_line_data, line_data, points_data, origin, map);
  382. //Link waypoints
  383. LinkMissingBranchingWayPoints(map);
  384. //Link StopLines and Traffic Lights
  385. LinkTrafficLightsAndStopLines(map);
  386. //LinkTrafficLightsAndStopLinesConData(conn_data, id_replace_list, map);
  387. if(bFindCurbsAndWayArea)
  388. {
  389. //Curbs
  390. ExtractCurbData(curb_data, line_data, points_data, origin, map);
  391. //Wayarea
  392. ExtractWayArea(area_data, wayarea_data, line_data, points_data, origin, map);
  393. }
  394. //Fix angle for lanes
  395. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  396. {
  397. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  398. {
  399. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  400. PlannerHNS::PlanningHelpers::FixAngleOnly(pL->points);
  401. }
  402. }
  403. cout << "Map loaded from data with " << roadLanes.size() << " lanes" << endl;
  404. }
  405. void MappingHelpers::AssignActionCostToLane(Lane* pL, ACTION_TYPE action, double cost)
  406. {
  407. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  408. {
  409. pL->points.at(j).actionCost.clear();
  410. pL->points.at(j).actionCost.push_back(make_pair(action, cost));
  411. }
  412. }
  413. WayPoint* MappingHelpers::FindWaypoint(const int& id, RoadNetwork& map)
  414. {
  415. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  416. {
  417. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  418. {
  419. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  420. {
  421. if(map.roadSegments.at(rs).Lanes.at(i).points.at(p).id == id)
  422. return &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  423. }
  424. }
  425. }
  426. return nullptr;
  427. }
  428. WayPoint* MappingHelpers::FindWaypointV2(const int& id, const int& l_id, RoadNetwork& map)
  429. {
  430. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  431. {
  432. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  433. {
  434. Lane* pLane = &map.roadSegments.at(rs).Lanes.at(i);
  435. if(pLane ->id != l_id)
  436. {
  437. for(unsigned int p= 0; p < pLane->points.size(); p++)
  438. {
  439. if(pLane->points.at(p).id == id)
  440. return &pLane->points.at(p);
  441. }
  442. }
  443. }
  444. }
  445. return nullptr;
  446. }
  447. void MappingHelpers::ConstructRoadNetworkFromDataFiles(const std::string vectoMapPath, RoadNetwork& map, const bool& bZeroOrigin)
  448. {
  449. /**
  450. * Exporting the center lines
  451. */
  452. string laneLinesDetails = vectoMapPath + "point.csv";
  453. string center_lines_info = vectoMapPath + "dtlane.csv";
  454. string lane_info = vectoMapPath + "lane.csv";
  455. string node_info = vectoMapPath + "node.csv";
  456. string area_info = vectoMapPath + "area.csv";
  457. string line_info = vectoMapPath + "line.csv";
  458. string signal_info = vectoMapPath + "signaldata.csv";
  459. string stop_line_info = vectoMapPath + "stopline.csv";
  460. string vector_info = vectoMapPath + "vector.csv";
  461. string curb_info = vectoMapPath + "curb.csv";
  462. string roadedge_info = vectoMapPath + "roadedge.csv";
  463. string wayarea_info = vectoMapPath + "wayarea.csv";
  464. string crosswalk_info = vectoMapPath + "crosswalk.csv";
  465. string conn_info = vectoMapPath + "dataconnection.csv";
  466. string intersection_info = vectoMapPath + "intersection.csv";
  467. cout << " >> Loading vector map data files ... " << endl;
  468. AisanCenterLinesFileReader center_lanes(center_lines_info);
  469. AisanLanesFileReader lanes(lane_info);
  470. AisanPointsFileReader points(laneLinesDetails);
  471. AisanNodesFileReader nodes(node_info);
  472. AisanLinesFileReader lines(line_info);
  473. AisanStopLineFileReader stop_line(stop_line_info);
  474. AisanSignalFileReader signal(signal_info);
  475. AisanVectorFileReader vec(vector_info);
  476. AisanCurbFileReader curb(curb_info);
  477. AisanRoadEdgeFileReader roadedge(roadedge_info);
  478. AisanDataConnFileReader conn(conn_info);
  479. AisanAreasFileReader areas(area_info);
  480. AisanWayareaFileReader way_area(wayarea_info);
  481. AisanCrossWalkFileReader cross_walk(crosswalk_info);
  482. vector<AisanIntersectionFileReader::AisanIntersection> intersection_data;
  483. vector<AisanNodesFileReader::AisanNode> nodes_data;
  484. vector<AisanLanesFileReader::AisanLane> lanes_data;
  485. vector<AisanPointsFileReader::AisanPoints> points_data;
  486. vector<AisanCenterLinesFileReader::AisanCenterLine> dt_data;
  487. vector<AisanLinesFileReader::AisanLine> line_data;
  488. vector<AisanStopLineFileReader::AisanStopLine> stop_line_data;
  489. vector<AisanSignalFileReader::AisanSignal> signal_data;
  490. vector<AisanVectorFileReader::AisanVector> vector_data;
  491. vector<AisanCurbFileReader::AisanCurb> curb_data;
  492. vector<AisanRoadEdgeFileReader::AisanRoadEdge> roadedge_data;
  493. vector<AisanAreasFileReader::AisanArea> area_data;
  494. vector<AisanWayareaFileReader::AisanWayarea> way_area_data;
  495. vector<AisanCrossWalkFileReader::AisanCrossWalk> crosswalk_data;
  496. vector<AisanDataConnFileReader::DataConn> conn_data;
  497. nodes.ReadAllData(nodes_data);
  498. lanes.ReadAllData(lanes_data);
  499. points.ReadAllData(points_data);
  500. center_lanes.ReadAllData(dt_data);
  501. lines.ReadAllData(line_data);
  502. stop_line.ReadAllData(stop_line_data);
  503. signal.ReadAllData(signal_data);
  504. vec.ReadAllData(vector_data);
  505. curb.ReadAllData(curb_data);
  506. roadedge.ReadAllData(roadedge_data);
  507. areas.ReadAllData(area_data);
  508. way_area.ReadAllData(way_area_data);
  509. cross_walk.ReadAllData(crosswalk_data);
  510. conn.ReadAllData(conn_data);
  511. if(points_data.size() == 0)
  512. {
  513. std::cout << std::endl << "## Alert Can't Read Points Data from vector map files in path: " << vectoMapPath << std::endl;
  514. return;
  515. }
  516. //Special Condtion to be able to pars old data structures
  517. int bSpecialMap = 0;
  518. // use this to transform data to origin (0,0,0)
  519. if(nodes_data.size() > 0 && bSpecialMap == 0)
  520. {
  521. ConstructRoadNetworkFromROSMessageV2(lanes_data, points_data, dt_data, intersection_data, area_data,
  522. line_data, stop_line_data, signal_data, vector_data, curb_data, roadedge_data,
  523. way_area_data, crosswalk_data, nodes_data, conn_data, &lanes, &points, &nodes, &lines,
  524. GetTransformationOrigin(0), map, false);
  525. }
  526. else
  527. {
  528. ConstructRoadNetworkFromROSMessage(lanes_data, points_data, dt_data, intersection_data, area_data,
  529. line_data, stop_line_data, signal_data, vector_data, curb_data, roadedge_data,
  530. way_area_data, crosswalk_data, nodes_data, conn_data,
  531. GetTransformationOrigin(0), map, bSpecialMap == 1);
  532. }
  533. WayPoint origin = GetFirstWaypoint(map);
  534. cout << origin.pos.ToString() ;
  535. }
  536. bool MappingHelpers::GetWayPoint(const int& id, const int& laneID,const double& refVel, const int& did,
  537. const std::vector<UtilityHNS::AisanCenterLinesFileReader::AisanCenterLine>& dtpoints,
  538. const std::vector<UtilityHNS::AisanPointsFileReader::AisanPoints>& points,
  539. const GPSPoint& origin, WayPoint& way_point)
  540. {
  541. for(unsigned int dtp =0; dtp < dtpoints.size(); dtp++)
  542. {
  543. if(dtpoints.at(dtp).DID == did)
  544. {
  545. for(unsigned int p =0; p < points.size(); p++)
  546. {
  547. if(dtpoints.at(dtp).PID == points.at(p).PID)
  548. {
  549. WayPoint wp;
  550. wp.id = id;
  551. wp.laneId = laneID;
  552. wp.v = refVel;
  553. // double integ_part = points.at(p).L;
  554. // double deg = trunc(points.at(p).L);
  555. // double min = trunc((points.at(p).L - deg) * 100.0) / 60.0;
  556. // double sec = modf((points.at(p).L - deg) * 100.0, &integ_part)/36.0;
  557. // double L = deg + min + sec;
  558. //
  559. // deg = trunc(points.at(p).B);
  560. // min = trunc((points.at(p).B - deg) * 100.0) / 60.0;
  561. // sec = modf((points.at(p).B - deg) * 100.0, &integ_part)/36.0;
  562. // double B = deg + min + sec;
  563. wp.pos = GPSPoint(points.at(p).Ly + origin.x, points.at(p).Bx + origin.y, points.at(p).H + origin.z, dtpoints.at(dtp).Dir);
  564. wp.pos.lat = points.at(p).L;
  565. wp.pos.lon = points.at(p).B;
  566. wp.pos.alt = points.at(p).H;
  567. wp.pos.dir = dtpoints.at(dtp).Dir;
  568. wp.iOriginalIndex = p;
  569. way_point = wp;
  570. return 1;
  571. }
  572. }
  573. }
  574. }
  575. return false;
  576. }
  577. void MappingHelpers::LoadKML(const std::string& kmlFile, RoadNetwork& map)
  578. {
  579. //First, Get the main element
  580. TiXmlElement* pHeadElem = 0;
  581. TiXmlElement* pElem = 0;
  582. ifstream f(kmlFile.c_str());
  583. if(!f.good())
  584. {
  585. cout << "Can't Open KML Map File: (" << kmlFile << ")" << endl;
  586. return;
  587. }
  588. std::cout << " >> Loading KML Map file ... " << std::endl;
  589. TiXmlDocument doc(kmlFile);
  590. try
  591. {
  592. doc.LoadFile();
  593. }
  594. catch(exception& e)
  595. {
  596. cout << "KML Custom Reader Error, Can't Load .kml File, path is: "<< kmlFile << endl;
  597. cout << e.what() << endl;
  598. return;
  599. }
  600. std::cout << " >> Reading Data from KML map file ... " << std::endl;
  601. pElem = doc.FirstChildElement();
  602. pHeadElem = GetHeadElement(pElem);
  603. std::cout << " >> Load Lanes from KML file .. " << std::endl;
  604. vector<Lane> laneLinksList = GetLanesList(pHeadElem);
  605. map.roadSegments.clear();
  606. map.roadSegments = GetRoadSegmentsList(pHeadElem);
  607. std::cout << " >> Load Traffic lights from KML file .. " << std::endl;
  608. vector<TrafficLight> trafficLights = GetTrafficLightsList(pHeadElem);
  609. std::cout << " >> Load Stop lines from KML file .. " << std::endl;
  610. vector<StopLine> stopLines = GetStopLinesList(pHeadElem);
  611. std::cout << " >> Load Signes from KML file .. " << std::endl;
  612. vector<TrafficSign> signs = GetTrafficSignsList(pHeadElem);
  613. std::cout << " >> Load Crossings from KML file .. " << std::endl;
  614. vector<Crossing> crossings = GetCrossingsList(pHeadElem);
  615. std::cout << " >> Load Markings from KML file .. " << std::endl;
  616. vector<Marking> markings = GetMarkingsList(pHeadElem);
  617. std::cout << " >> Load Road boundaries from KML file .. " << std::endl;
  618. vector<Boundary> boundaries = GetBoundariesList(pHeadElem);
  619. std::cout << " >> Load Curbs from KML file .. " << std::endl;
  620. vector<Curb> curbs = GetCurbsList(pHeadElem);
  621. map.signs.clear();
  622. map.signs = signs;
  623. map.crossings.clear();
  624. map.crossings = crossings;
  625. map.markings.clear();
  626. map.markings = markings;
  627. map.boundaries.clear();
  628. map.boundaries = boundaries;
  629. map.curbs.clear();
  630. map.curbs = curbs;
  631. //Fill the relations
  632. for(unsigned int i= 0; i<map.roadSegments.size(); i++ )
  633. {
  634. for(unsigned int j=0; j < laneLinksList.size(); j++)
  635. {
  636. PlanningHelpers::CalcAngleAndCost(laneLinksList.at(j).points);
  637. map.roadSegments.at(i).Lanes.push_back(laneLinksList.at(j));
  638. }
  639. }
  640. cout << " >> Link lanes and waypoints with pointers ... " << endl;
  641. //Link Lanes and lane's waypoints by pointers
  642. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  643. {
  644. //Link Lanes
  645. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  646. {
  647. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  648. for(unsigned int j = 0 ; j < pL->fromIds.size(); j++)
  649. {
  650. for(unsigned int l= 0; l < map.roadSegments.at(rs).Lanes.size(); l++)
  651. {
  652. if(map.roadSegments.at(rs).Lanes.at(l).id == pL->fromIds.at(j))
  653. {
  654. pL->fromLanes.push_back(&map.roadSegments.at(rs).Lanes.at(l));
  655. }
  656. }
  657. }
  658. for(unsigned int j = 0 ; j < pL->toIds.size(); j++)
  659. {
  660. for(unsigned int l= 0; l < map.roadSegments.at(rs).Lanes.size(); l++)
  661. {
  662. if(map.roadSegments.at(rs).Lanes.at(l).id == pL->toIds.at(j))
  663. {
  664. pL->toLanes.push_back(&map.roadSegments.at(rs).Lanes.at(l));
  665. }
  666. }
  667. }
  668. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  669. {
  670. pL->points.at(j).pLane = pL;
  671. }
  672. }
  673. }
  674. //Link waypoints
  675. cout << " >> Link missing branches and waypoints... " << endl;
  676. LinkMissingBranchingWayPointsV2(map);
  677. cout << " >> Link Lane change semantics ... " << endl;
  678. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  679. {
  680. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  681. {
  682. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  683. {
  684. WayPoint* pWP = &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  685. if(pWP->pLeft == 0 && pWP->LeftPointId > 0)
  686. {
  687. pWP->pLeft = FindWaypointV2(pWP->LeftPointId, pWP->laneId, map);
  688. if(pWP->pLeft != nullptr)
  689. {
  690. pWP->LeftLnId = pWP->pLeft->laneId;
  691. pWP->pLane->pLeftLane = pWP->pLeft->pLane;
  692. if(pWP->pLeft->RightPointId == pWP->id)
  693. {
  694. pWP->pLeft->pRight = pWP;
  695. pWP->pLeft->RightLnId = pWP->laneId;
  696. pWP->pLeft->pLane->pRightLane = pWP->pLane;
  697. }
  698. }
  699. }
  700. if(pWP->pRight == 0 && pWP->RightPointId > 0)
  701. {
  702. pWP->pRight = FindWaypointV2(pWP->RightPointId, pWP->laneId, map);
  703. if(pWP->pRight != nullptr)
  704. {
  705. pWP->RightLnId = pWP->pRight->laneId;
  706. pWP->pLane->pRightLane = pWP->pRight->pLane;
  707. if(pWP->pRight->LeftPointId == pWP->id)
  708. {
  709. pWP->pRight->pLeft = pWP;
  710. pWP->pRight->LeftLnId = pWP->laneId;
  711. pWP->pRight->pLane->pLeftLane = pWP->pLane;
  712. }
  713. }
  714. }
  715. }
  716. }
  717. }
  718. map.stopLines = stopLines;
  719. map.trafficLights = trafficLights;
  720. //Link waypoints && StopLines
  721. cout << " >> Link Stop lines and Traffic lights ... " << endl;
  722. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  723. {
  724. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  725. {
  726. for(unsigned int itl = 0; itl < map.trafficLights.size(); itl++)
  727. {
  728. if(map.trafficLights.at(itl).CheckLane(map.roadSegments.at(rs).Lanes.at(i).id))
  729. {
  730. map.trafficLights.at(itl).pLanes.push_back(&map.roadSegments.at(rs).Lanes.at(i));
  731. map.roadSegments.at(rs).Lanes.at(i).trafficlights.push_back(map.trafficLights.at(itl));
  732. }
  733. }
  734. for(unsigned int isl = 0; isl < map.stopLines.size(); isl++)
  735. {
  736. if(map.stopLines.at(isl).laneId == map.roadSegments.at(rs).Lanes.at(i).id)
  737. {
  738. map.stopLines.at(isl).pLane = &map.roadSegments.at(rs).Lanes.at(i);
  739. map.roadSegments.at(rs).Lanes.at(i).stopLines.push_back(map.stopLines.at(isl));
  740. WayPoint wp((map.stopLines.at(isl).points.at(0).x+map.stopLines.at(isl).points.at(1).x)/2.0, (map.stopLines.at(isl).points.at(0).y+map.stopLines.at(isl).points.at(1).y)/2.0, (map.stopLines.at(isl).points.at(0).z+map.stopLines.at(isl).points.at(1).z)/2.0, (map.stopLines.at(isl).points.at(0).a+map.stopLines.at(isl).points.at(1).a)/2.0);
  741. map.roadSegments.at(rs).Lanes.at(i).points.at(PlanningHelpers::GetClosestNextPointIndexFast(map.roadSegments.at(rs).Lanes.at(i).points, wp)).stopLineID = map.stopLines.at(isl).id;
  742. }
  743. }
  744. }
  745. }
  746. cout << " >> Find Max IDs ... " << endl;
  747. GetMapMaxIds(map);
  748. cout << "Map loaded from kml file with (" << laneLinksList.size() << ") lanes, First Point ( " << GetFirstWaypoint(map).pos.ToString() << ")"<< endl;
  749. }
  750. TiXmlElement* MappingHelpers::GetHeadElement(TiXmlElement* pMainElem)
  751. {
  752. TiXmlElement* pElem = pMainElem;
  753. if(pElem)
  754. pElem = pElem->FirstChildElement("Folder");
  755. if(pElem && pElem->FirstChildElement("Folder"))
  756. pElem = pElem->FirstChildElement("Folder");
  757. if(pElem && pElem->FirstChildElement("Document"))
  758. pElem = pElem->FirstChildElement("Document");
  759. if(!pElem)
  760. {
  761. return nullptr;
  762. }
  763. return pElem;
  764. }
  765. TiXmlElement* MappingHelpers::GetDataFolder(const string& folderName, TiXmlElement* pMainElem)
  766. {
  767. if(!pMainElem) return nullptr;
  768. TiXmlElement* pElem = pMainElem->FirstChildElement("Folder");
  769. string folderID="";
  770. for(; pElem; pElem=pElem->NextSiblingElement())
  771. {
  772. folderID="";
  773. if(pElem->FirstChildElement("name")->GetText()) //Map Name
  774. folderID = pElem->FirstChildElement("name")->GetText();
  775. if(folderID.compare(folderName)==0)
  776. return pElem;
  777. }
  778. return nullptr;
  779. }
  780. WayPoint* MappingHelpers::GetClosestWaypointFromMap(const WayPoint& pos, RoadNetwork& map, const bool bDirectionBased)
  781. {
  782. double distance_to_nearest_lane = 1;
  783. Lane* pLane = 0;
  784. while(distance_to_nearest_lane < 100 && pLane == 0)
  785. {
  786. pLane = GetClosestLaneFromMap(pos, map, distance_to_nearest_lane, bDirectionBased);
  787. distance_to_nearest_lane += 1;
  788. }
  789. if(!pLane) return nullptr;
  790. int closest_index = PlanningHelpers::GetClosestNextPointIndexFast(pLane->points, pos);
  791. return &pLane->points.at(closest_index);
  792. }
  793. vector<WayPoint*> MappingHelpers::GetClosestWaypointsListFromMap(const WayPoint& pos, RoadNetwork& map, const double& distance, const bool bDirectionBased)
  794. {
  795. vector<Lane*> pLanes = GetClosestLanesListFromMap(pos, map, distance, bDirectionBased);
  796. vector<WayPoint*> waypoints_list;
  797. if(pLanes.size() == 0) return waypoints_list;
  798. for(unsigned int i = 0; i < pLanes.size(); i++)
  799. {
  800. if(pLanes.at(i))
  801. {
  802. int closest_index = PlanningHelpers::GetClosestNextPointIndexFast(pLanes.at(i)->points, pos);
  803. waypoints_list.push_back(&pLanes.at(i)->points.at(closest_index));
  804. }
  805. }
  806. return waypoints_list;
  807. }
  808. WayPoint* MappingHelpers::GetClosestBackWaypointFromMap(const WayPoint& pos, RoadNetwork& map)
  809. {
  810. double distance_to_nearest_lane = 1;
  811. Lane* pLane = 0;
  812. while(distance_to_nearest_lane < 100 && pLane == 0)
  813. {
  814. pLane = GetClosestLaneFromMap(pos, map, distance_to_nearest_lane);
  815. distance_to_nearest_lane += 1;
  816. }
  817. if(!pLane) return nullptr;
  818. int closest_index = PlanningHelpers::GetClosestNextPointIndexFast(pLane->points, pos);
  819. if(closest_index>2)
  820. return &pLane->points.at(closest_index-3);
  821. else if(closest_index>1)
  822. return &pLane->points.at(closest_index-2);
  823. else if(closest_index>0)
  824. return &pLane->points.at(closest_index-1);
  825. else
  826. return &pLane->points.at(closest_index);
  827. }
  828. std::vector<Lane*> MappingHelpers::GetClosestLanesFast(const WayPoint& center, RoadNetwork& map, const double& distance)
  829. {
  830. vector<Lane*> lanesList;
  831. for(unsigned int j=0; j< map.roadSegments.size(); j ++)
  832. {
  833. for(unsigned int k=0; k< map.roadSegments.at(j).Lanes.size(); k ++)
  834. {
  835. Lane* pL = &map.roadSegments.at(j).Lanes.at(k);
  836. int index = PlanningHelpers::GetClosestNextPointIndexFast(pL->points, center);
  837. if(index < 0 || index >= pL->points.size()) continue;
  838. double d = hypot(pL->points.at(index).pos.y - center.pos.y, pL->points.at(index).pos.x - center.pos.x);
  839. if(d <= distance)
  840. lanesList.push_back(pL);
  841. }
  842. }
  843. return lanesList;
  844. }
  845. Lane* MappingHelpers::GetClosestLaneFromMap(const WayPoint& pos, RoadNetwork& map, const double& distance, const bool bDirectionBased)
  846. {
  847. vector<pair<double, Lane*> > laneLinksList;
  848. double d = 0;
  849. double min_d = DBL_MAX;
  850. //std::cout<<"road segmentt size: "<<map.roadSegments.size()<<std::endl;
  851. for(unsigned int j=0; j< map.roadSegments.size(); j ++)
  852. {
  853. for(unsigned int k=0; k< map.roadSegments.at(j).Lanes.size(); k ++)
  854. {
  855. //Lane* pLane = &pEdge->lanes.at(k);
  856. d = 0;
  857. min_d = DBL_MAX;
  858. for(unsigned int pindex=0; pindex< map.roadSegments.at(j).Lanes.at(k).points.size(); pindex ++)
  859. {
  860. d = distance2points(map.roadSegments.at(j).Lanes.at(k).points.at(pindex).pos, pos.pos);
  861. if(d < min_d)
  862. min_d = d;
  863. }
  864. if(min_d < distance)
  865. laneLinksList.push_back(make_pair(min_d, &map.roadSegments.at(j).Lanes.at(k)));
  866. }
  867. }
  868. if(laneLinksList.size() == 0) return nullptr;
  869. min_d = DBL_MAX;
  870. Lane* closest_lane = 0;
  871. for(unsigned int i = 0; i < laneLinksList.size(); i++)
  872. {
  873. RelativeInfo info;
  874. PlanningHelpers::GetRelativeInfo(laneLinksList.at(i).second->points, pos, info);
  875. if(info.perp_distance == 0 && laneLinksList.at(i).first != 0)
  876. continue;
  877. if(bDirectionBased && fabs(info.perp_distance) < min_d && fabs(info.angle_diff) < 45)
  878. {
  879. min_d = fabs(info.perp_distance);
  880. closest_lane = laneLinksList.at(i).second;
  881. }
  882. else if(!bDirectionBased && fabs(info.perp_distance) < min_d)
  883. {
  884. min_d = fabs(info.perp_distance);
  885. closest_lane = laneLinksList.at(i).second;
  886. }
  887. }
  888. //std::cout<<" min d : "<<min_d<<std::endl;;
  889. return closest_lane;
  890. }
  891. vector<Lane*> MappingHelpers::GetClosestLanesListFromMap(const WayPoint& pos, RoadNetwork& map, const double& distance, const bool bDirectionBased)
  892. {
  893. vector<pair<double, Lane*> > laneLinksList;
  894. double d = 0;
  895. double min_d = DBL_MAX;
  896. for(unsigned int j=0; j< map.roadSegments.size(); j ++)
  897. {
  898. for(unsigned int k=0; k< map.roadSegments.at(j).Lanes.size(); k ++)
  899. {
  900. //Lane* pLane = &pEdge->lanes.at(k);
  901. d = 0;
  902. min_d = DBL_MAX;
  903. for(unsigned int pindex=0; pindex< map.roadSegments.at(j).Lanes.at(k).points.size(); pindex ++)
  904. {
  905. d = distance2points(map.roadSegments.at(j).Lanes.at(k).points.at(pindex).pos, pos.pos);
  906. if(d < min_d)
  907. min_d = d;
  908. }
  909. if(min_d < distance)
  910. laneLinksList.push_back(make_pair(min_d, &map.roadSegments.at(j).Lanes.at(k)));
  911. }
  912. }
  913. vector<Lane*> closest_lanes;
  914. if(laneLinksList.size() == 0) return closest_lanes;
  915. for(unsigned int i = 0; i < laneLinksList.size(); i++)
  916. {
  917. RelativeInfo info;
  918. PlanningHelpers::GetRelativeInfo(laneLinksList.at(i).second->points, pos, info);
  919. if(info.perp_distance == 0 && laneLinksList.at(i).first != 0)
  920. continue;
  921. if(bDirectionBased && fabs(info.perp_distance) < distance && fabs(info.angle_diff) < 30)
  922. {
  923. closest_lanes.push_back(laneLinksList.at(i).second);
  924. }
  925. else if(!bDirectionBased && fabs(info.perp_distance) < distance)
  926. {
  927. closest_lanes.push_back(laneLinksList.at(i).second);
  928. }
  929. }
  930. return closest_lanes;
  931. }
  932. Lane* MappingHelpers::GetClosestLaneFromMapDirectionBased(const WayPoint& pos, RoadNetwork& map, const double& distance)
  933. {
  934. vector<pair<double, WayPoint*> > laneLinksList;
  935. double d = 0;
  936. double min_d = DBL_MAX;
  937. int min_i = 0;
  938. for(unsigned int j=0; j< map.roadSegments.size(); j ++)
  939. {
  940. for(unsigned int k=0; k< map.roadSegments.at(j).Lanes.size(); k ++)
  941. {
  942. //Lane* pLane = &pEdge->lanes.at(k);
  943. d = 0;
  944. min_d = DBL_MAX;
  945. for(unsigned int pindex=0; pindex< map.roadSegments.at(j).Lanes.at(k).points.size(); pindex ++)
  946. {
  947. d = distance2points(map.roadSegments.at(j).Lanes.at(k).points.at(pindex).pos, pos.pos);
  948. if(d < min_d)
  949. {
  950. min_d = d;
  951. min_i = pindex;
  952. }
  953. }
  954. if(min_d < distance)
  955. laneLinksList.push_back(make_pair(min_d, &map.roadSegments.at(j).Lanes.at(k).points.at(min_i)));
  956. }
  957. }
  958. if(laneLinksList.size() == 0) return nullptr;
  959. min_d = DBL_MAX;
  960. Lane* closest_lane = 0;
  961. double a_diff = 0;
  962. for(unsigned int i = 0; i < laneLinksList.size(); i++)
  963. {
  964. RelativeInfo info;
  965. PlanningHelpers::GetRelativeInfo(laneLinksList.at(i).second->pLane->points, pos, info);
  966. if(info.perp_distance == 0 && laneLinksList.at(i).first != 0)
  967. continue;
  968. a_diff = UtilityH::AngleBetweenTwoAnglesPositive(laneLinksList.at(i).second->pos.a, pos.pos.a);
  969. if(fabs(info.perp_distance)<min_d && a_diff <= M_PI_4)
  970. {
  971. min_d = fabs(info.perp_distance);
  972. closest_lane = laneLinksList.at(i).second->pLane;
  973. }
  974. }
  975. return closest_lane;
  976. }
  977. std::vector<Lane*> MappingHelpers::GetClosestMultipleLanesFromMap(const WayPoint& pos, RoadNetwork& map, const double& distance)
  978. {
  979. vector<Lane*> lanesList;
  980. double d = 0;
  981. double a_diff = 0;
  982. for(unsigned int j=0; j< map.roadSegments.size(); j ++)
  983. {
  984. for(unsigned int k=0; k< map.roadSegments.at(j).Lanes.size(); k ++)
  985. {
  986. for(unsigned int pindex=0; pindex< map.roadSegments.at(j).Lanes.at(k).points.size(); pindex ++)
  987. {
  988. d = distance2points(map.roadSegments.at(j).Lanes.at(k).points.at(pindex).pos, pos.pos);
  989. a_diff = UtilityH::AngleBetweenTwoAnglesPositive(map.roadSegments.at(j).Lanes.at(k).points.at(pindex).pos.a, pos.pos.a);
  990. if(d <= distance && a_diff <= M_PI_4)
  991. {
  992. bool bLaneExist = false;
  993. for(unsigned int il = 0; il < lanesList.size(); il++)
  994. {
  995. if(lanesList.at(il)->id == map.roadSegments.at(j).Lanes.at(k).id)
  996. {
  997. bLaneExist = true;
  998. break;
  999. }
  1000. }
  1001. if(!bLaneExist)
  1002. lanesList.push_back(&map.roadSegments.at(j).Lanes.at(k));
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. }
  1008. return lanesList;
  1009. }
  1010. WayPoint MappingHelpers::GetFirstWaypoint(RoadNetwork& map)
  1011. {
  1012. for(unsigned int j=0; j< map.roadSegments.size(); j ++)
  1013. {
  1014. for(unsigned int k=0; k< map.roadSegments.at(j).Lanes.size(); k ++)
  1015. {
  1016. for(unsigned int pindex=0; pindex< map.roadSegments.at(j).Lanes.at(k).points.size(); pindex ++)
  1017. {
  1018. WayPoint fp = map.roadSegments.at(j).Lanes.at(k).points.at(pindex);
  1019. return fp;
  1020. }
  1021. }
  1022. }
  1023. return WayPoint();
  1024. }
  1025. WayPoint* MappingHelpers::GetLastWaypoint(RoadNetwork& map)
  1026. {
  1027. if(map.roadSegments.size() > 0 && map.roadSegments.at(map.roadSegments.size()-1).Lanes.size() > 0)
  1028. {
  1029. std::vector<Lane>* lanes = &map.roadSegments.at(map.roadSegments.size()-1).Lanes;
  1030. if(lanes->at(lanes->size()-1).points.size() > 0)
  1031. return &lanes->at(lanes->size()-1).points.at(lanes->at(lanes->size()-1).points.size()-1);
  1032. }
  1033. return nullptr;
  1034. }
  1035. void MappingHelpers::GetUniqueNextLanes(const Lane* l, const vector<Lane*>& traversed_lanes, vector<Lane*>& lanes_list)
  1036. {
  1037. if(!l) return;
  1038. for(unsigned int i=0; i< l->toLanes.size(); i++)
  1039. {
  1040. bool bFound = false;
  1041. for(unsigned int j = 0; j < traversed_lanes.size(); j++)
  1042. if(l->toLanes.at(i)->id == traversed_lanes.at(j)->id)
  1043. {
  1044. bFound = true;
  1045. break;
  1046. }
  1047. if(!bFound)
  1048. lanes_list.push_back(l->toLanes.at(i));
  1049. }
  1050. }
  1051. Lane* MappingHelpers::GetLaneFromPath(const WayPoint& currPos, const std::vector<WayPoint>& currPath)
  1052. {
  1053. if(currPath.size() < 1) return nullptr;
  1054. int closest_index = PlanningHelpers::GetClosestNextPointIndexFast(currPath, currPos);
  1055. return currPath.at(closest_index).pLane;
  1056. }
  1057. std::vector<Curb> MappingHelpers::GetCurbsList(TiXmlElement* pElem)
  1058. {
  1059. vector<Curb> cList;
  1060. TiXmlElement* pCurbs = GetDataFolder("CurbsLines", pElem);
  1061. if(!pCurbs)
  1062. return cList;
  1063. TiXmlElement* pE = pCurbs->FirstChildElement("Placemark");
  1064. for( ; pE; pE=pE->NextSiblingElement())
  1065. {
  1066. string tfID;
  1067. TiXmlElement* pNameXml = pE->FirstChildElement("name");
  1068. if(pNameXml)
  1069. {
  1070. tfID = pNameXml->GetText();
  1071. Curb c;
  1072. c.id = GetIDsFromPrefix(tfID, "BID", "LnID").at(0);
  1073. c.laneId = GetIDsFromPrefix(tfID, "LnID", "RdID").at(0);
  1074. c.roadId = GetIDsFromPrefix(tfID, "RdID", "").at(0);
  1075. TiXmlElement* pPoints = pE->FirstChildElement("LineString")->FirstChildElement("coordinates");
  1076. c.points = GetPointsData(pPoints);
  1077. cList.push_back(c);
  1078. }
  1079. }
  1080. return cList;
  1081. }
  1082. std::vector<Boundary> MappingHelpers::GetBoundariesList(TiXmlElement* pElem)
  1083. {
  1084. vector<Boundary> bList;
  1085. TiXmlElement* pBoundaries = GetDataFolder("Boundaries", pElem);
  1086. if(!pBoundaries)
  1087. return bList;
  1088. TiXmlElement* pE = pBoundaries->FirstChildElement("Placemark");
  1089. for( ; pE; pE=pE->NextSiblingElement())
  1090. {
  1091. string tfID;
  1092. TiXmlElement* pNameXml = pE->FirstChildElement("name");
  1093. if(pNameXml)
  1094. {
  1095. tfID = pNameXml->GetText();
  1096. Boundary b;
  1097. b.id = GetIDsFromPrefix(tfID, "BID", "RdID").at(0);
  1098. b.roadId = GetIDsFromPrefix(tfID, "RdID", "").at(0);
  1099. TiXmlElement* pPoints = pE->FirstChildElement("LineString")->FirstChildElement("coordinates");
  1100. b.points = GetPointsData(pPoints);
  1101. bList.push_back(b);
  1102. }
  1103. }
  1104. return bList;
  1105. }
  1106. std::vector<Marking> MappingHelpers::GetMarkingsList(TiXmlElement* pElem)
  1107. {
  1108. vector<Marking> mList;
  1109. TiXmlElement* pMarkings= GetDataFolder("Markings", pElem);
  1110. if(!pMarkings)
  1111. return mList;
  1112. TiXmlElement* pE = pMarkings->FirstChildElement("Placemark");
  1113. for( ; pE; pE=pE->NextSiblingElement())
  1114. {
  1115. string tfID;
  1116. TiXmlElement* pNameXml =pE->FirstChildElement("name");
  1117. if(pNameXml)
  1118. {
  1119. tfID = pNameXml->GetText();
  1120. Marking m;
  1121. m.id = GetIDsFromPrefix(tfID, "MID", "LnID").at(0);
  1122. m.laneId = GetIDsFromPrefix(tfID, "LnID", "RdID").at(0);
  1123. m.roadId = GetIDsFromPrefix(tfID, "RdID", "").at(0);
  1124. TiXmlElement* pPoints = pE->FirstChildElement("LineString")->FirstChildElement("coordinates");
  1125. m.points = GetPointsData(pPoints);
  1126. if(m.points.size() > 0)
  1127. {
  1128. //first item is the center of the marking
  1129. m.center = m.points.at(0);
  1130. m.points.erase(m.points.begin()+0);
  1131. }
  1132. mList.push_back(m);
  1133. }
  1134. }
  1135. return mList;
  1136. }
  1137. std::vector<Crossing> MappingHelpers::GetCrossingsList(TiXmlElement* pElem)
  1138. {
  1139. vector<Crossing> crossList;
  1140. TiXmlElement* pCrossings= GetDataFolder("Crossings", pElem);
  1141. if(!pCrossings)
  1142. return crossList;
  1143. TiXmlElement* pE = pCrossings->FirstChildElement("Placemark");
  1144. for( ; pE; pE=pE->NextSiblingElement())
  1145. {
  1146. string tfID;
  1147. TiXmlElement* pNameXml =pE->FirstChildElement("name");
  1148. if(pNameXml)
  1149. {
  1150. tfID = pNameXml->GetText();
  1151. Crossing cross;
  1152. cross.id = GetIDsFromPrefix(tfID, "CRID", "RdID").at(0);
  1153. cross.roadId = GetIDsFromPrefix(tfID, "RdID", "").at(0);
  1154. TiXmlElement* pPoints = pE->FirstChildElement("LineString")->FirstChildElement("coordinates");
  1155. cross.points = GetPointsData(pPoints);
  1156. crossList.push_back(cross);
  1157. }
  1158. }
  1159. return crossList;
  1160. }
  1161. std::vector<TrafficSign> MappingHelpers::GetTrafficSignsList(TiXmlElement* pElem)
  1162. {
  1163. vector<TrafficSign> tsList;
  1164. TiXmlElement* pSigns = GetDataFolder("TrafficSigns", pElem);
  1165. if(!pSigns)
  1166. return tsList;
  1167. TiXmlElement* pE = pSigns->FirstChildElement("Placemark");
  1168. for( ; pE; pE=pE->NextSiblingElement())
  1169. {
  1170. string tfID;
  1171. TiXmlElement* pNameXml =pE->FirstChildElement("name");
  1172. if(pNameXml)
  1173. {
  1174. tfID = pNameXml->GetText();
  1175. TrafficSign ts;
  1176. ts.id = GetIDsFromPrefix(tfID, "TSID", "LnID").at(0);
  1177. ts.laneId = GetIDsFromPrefix(tfID, "LnID", "RdID").at(0);
  1178. ts.roadId = GetIDsFromPrefix(tfID, "RdID", "Type").at(0);
  1179. int iType = GetIDsFromPrefix(tfID, "Type", "").at(0);
  1180. switch(iType)
  1181. {
  1182. case 0:
  1183. ts.signType = UNKNOWN_SIGN;
  1184. break;
  1185. case 1:
  1186. ts.signType = STOP_SIGN;
  1187. break;
  1188. case 2:
  1189. ts.signType = MAX_SPEED_SIGN;
  1190. break;
  1191. case 3:
  1192. ts.signType = MIN_SPEED_SIGN;
  1193. break;
  1194. default:
  1195. ts.signType = STOP_SIGN;
  1196. break;
  1197. }
  1198. TiXmlElement* pPoints = pE->FirstChildElement("LineString")->FirstChildElement("coordinates");
  1199. ts.pos = GetPointsData(pPoints).at(0);
  1200. tsList.push_back(ts);
  1201. }
  1202. }
  1203. return tsList;
  1204. }
  1205. std::vector<StopLine> MappingHelpers::GetStopLinesList(TiXmlElement* pElem)
  1206. {
  1207. vector<StopLine> slList;
  1208. TiXmlElement* pStopLines = GetDataFolder("StopLines", pElem);
  1209. if(!pStopLines)
  1210. return slList;
  1211. TiXmlElement* pE = pStopLines->FirstChildElement("Placemark");
  1212. for( ; pE; pE=pE->NextSiblingElement())
  1213. {
  1214. string tfID;
  1215. TiXmlElement* pNameXml =pE->FirstChildElement("name");
  1216. if(pNameXml)
  1217. {
  1218. tfID = pNameXml->GetText();
  1219. StopLine sl;
  1220. sl.id = GetIDsFromPrefix(tfID, "SLID", "LnID").at(0);
  1221. sl.laneId = GetIDsFromPrefix(tfID, "LnID", "TSID").at(0);
  1222. sl.stopSignID = GetIDsFromPrefix(tfID, "TSID", "TLID").at(0);
  1223. sl.trafficLightID = GetIDsFromPrefix(tfID, "TLID", "").at(0);
  1224. TiXmlElement* pPoints = pE->FirstChildElement("LineString")->FirstChildElement("coordinates");
  1225. sl.points = GetPointsData(pPoints);
  1226. slList.push_back(sl);
  1227. }
  1228. }
  1229. return slList;
  1230. }
  1231. std::vector<TrafficLight> MappingHelpers::GetTrafficLightsList(TiXmlElement* pElem)
  1232. {
  1233. vector<TrafficLight> tlList;
  1234. TiXmlElement* pLightsLines = GetDataFolder("TrafficLights", pElem);
  1235. if(!pLightsLines)
  1236. return tlList;
  1237. TiXmlElement* pE = pLightsLines->FirstChildElement("Placemark");
  1238. for( ; pE; pE=pE->NextSiblingElement())
  1239. {
  1240. string tfID;
  1241. TiXmlElement* pNameXml =pE->FirstChildElement("name");
  1242. if(pNameXml)
  1243. {
  1244. tfID = pNameXml->GetText();
  1245. TrafficLight tl;
  1246. tl.id = GetIDsFromPrefix(tfID, "TLID", "LnID").at(0);
  1247. tl.laneIds = GetIDsFromPrefix(tfID, "LnID", "");
  1248. TiXmlElement* pPoints = pE->FirstChildElement("Point")->FirstChildElement("coordinates");
  1249. tl.pos = GetPointsData(pPoints).at(0);
  1250. tlList.push_back(tl);
  1251. }
  1252. }
  1253. return tlList;
  1254. }
  1255. vector<Lane> MappingHelpers::GetLanesList(TiXmlElement* pElem)
  1256. {
  1257. vector<Lane> llList;
  1258. TiXmlElement* pLaneLinks = GetDataFolder("Lanes", pElem);
  1259. if(!pLaneLinks)
  1260. return llList;
  1261. TiXmlElement* pE = pLaneLinks->FirstChildElement("Folder");
  1262. for( ; pE; pE=pE->NextSiblingElement())
  1263. {
  1264. string tfID;
  1265. TiXmlElement* pNameXml =pE->FirstChildElement("description");
  1266. if(pNameXml)
  1267. {
  1268. tfID = pNameXml->GetText();
  1269. Lane ll;
  1270. ll.id = GetIDsFromPrefix(tfID, "LID", "RSID").at(0);
  1271. ll.roadId = GetIDsFromPrefix(tfID, "RSID", "NUM").at(0);
  1272. ll.num = GetIDsFromPrefix(tfID, "NUM", "From").at(0);
  1273. ll.fromIds = GetIDsFromPrefix(tfID, "From", "To");
  1274. ll.toIds = GetIDsFromPrefix(tfID, "To", "Vel");
  1275. ll.speed = GetIDsFromPrefix(tfID, "Vel", "").at(0);
  1276. if(m_USING_VER_ZERO == 1)
  1277. ll.points = GetCenterLaneDataVer0(pE, ll.id);
  1278. else
  1279. ll.points = GetCenterLaneData(pE, ll.id);
  1280. llList.push_back(ll);
  1281. }
  1282. }
  1283. return llList;
  1284. }
  1285. vector<RoadSegment> MappingHelpers::GetRoadSegmentsList(TiXmlElement* pElem)
  1286. {
  1287. vector<RoadSegment> rlList;
  1288. TiXmlElement* pRoadLinks = GetDataFolder("RoadSegments", pElem);
  1289. if(!pRoadLinks)
  1290. return rlList;
  1291. TiXmlElement* pE = pRoadLinks->FirstChildElement("Placemark");
  1292. for( ; pE; pE=pE->NextSiblingElement())
  1293. {
  1294. string tfID;
  1295. TiXmlElement* pNameXml =pE->FirstChildElement("description");
  1296. if(pNameXml)
  1297. tfID = pNameXml->GetText();
  1298. RoadSegment rl;
  1299. rl.id = GetIDsFromPrefix(tfID, "RSID", "").at(0);
  1300. rlList.push_back(rl);
  1301. }
  1302. return rlList;
  1303. }
  1304. std::vector<GPSPoint> MappingHelpers::GetPointsData(TiXmlElement* pElem)
  1305. {
  1306. std::vector<GPSPoint> points;
  1307. if(pElem)
  1308. {
  1309. string coordinate_list;
  1310. if(!pElem->NoChildren())
  1311. coordinate_list = pElem->GetText();
  1312. istringstream str_stream(coordinate_list);
  1313. string token, temp;
  1314. while(getline(str_stream, token, ' '))
  1315. {
  1316. string lat, lon, alt;
  1317. double numLat=0, numLon=0, numAlt=0;
  1318. istringstream ss(token);
  1319. getline(ss, lat, ',');
  1320. getline(ss, lon, ',');
  1321. getline(ss, alt, ',');
  1322. numLat = atof(lat.c_str());
  1323. numLon = atof(lon.c_str());
  1324. numAlt = atof(alt.c_str());
  1325. GPSPoint p;
  1326. p.x = p.lat = numLat;
  1327. p.y = p.lon = numLon;
  1328. p.z = p.alt = numAlt;
  1329. points.push_back(p);
  1330. }
  1331. }
  1332. return points;
  1333. }
  1334. vector<WayPoint> MappingHelpers::GetCenterLaneData(TiXmlElement* pElem, const int& currLaneID)
  1335. {
  1336. vector<WayPoint> gps_points;
  1337. TiXmlElement* pV = pElem->FirstChildElement("Placemark");
  1338. if(pV)
  1339. pV = pV->FirstChildElement("LineString");
  1340. if(pV)
  1341. pV = pV->FirstChildElement("coordinates");
  1342. if(pV)
  1343. {
  1344. string coordinate_list;
  1345. if(!pV->NoChildren())
  1346. coordinate_list = pV->GetText();
  1347. istringstream str_stream(coordinate_list);
  1348. string token, temp;
  1349. while(getline(str_stream, token, ' '))
  1350. {
  1351. string lat, lon, alt;
  1352. double numLat=0, numLon=0, numAlt=0;
  1353. istringstream ss(token);
  1354. getline(ss, lat, ',');
  1355. getline(ss, lon, ',');
  1356. getline(ss, alt, ',');
  1357. numLat = atof(lat.c_str());
  1358. numLon = atof(lon.c_str());
  1359. numAlt = atof(alt.c_str());
  1360. WayPoint wp;
  1361. wp.pos.x = wp.pos.lat = numLat;
  1362. wp.pos.y = wp.pos.lon = numLon;
  1363. wp.pos.z = wp.pos.alt = numAlt;
  1364. wp.laneId = currLaneID;
  1365. gps_points.push_back(wp);
  1366. }
  1367. TiXmlElement* pInfoEl =pElem->FirstChildElement("Folder")->FirstChildElement("description");
  1368. string additional_info;
  1369. if(pInfoEl)
  1370. additional_info = pInfoEl->GetText();
  1371. additional_info.insert(additional_info.begin(), ',');
  1372. additional_info.erase(additional_info.end()-1);
  1373. vector<string> add_info_list = SplitString(additional_info, ",");
  1374. if(gps_points.size() == add_info_list.size())
  1375. {
  1376. for(unsigned int i=0; i< gps_points.size(); i++)
  1377. {
  1378. gps_points.at(i).id = GetIDsFromPrefix(add_info_list.at(i), "WPID", "AC").at(0);
  1379. gps_points.at(i).actionCost.push_back(GetActionPairFromPrefix(add_info_list.at(i), "AC", "From"));
  1380. gps_points.at(i).fromIds = GetIDsFromPrefix(add_info_list.at(i), "From", "To");
  1381. gps_points.at(i).toIds = GetIDsFromPrefix(add_info_list.at(i), "To", "Lid");
  1382. vector<int> ids = GetIDsFromPrefix(add_info_list.at(i), "Lid", "Rid");
  1383. if(ids.size() > 0)
  1384. gps_points.at(i).LeftPointId = ids.at(0);
  1385. ids = GetIDsFromPrefix(add_info_list.at(i), "Rid", "Vel");
  1386. if(ids.size() > 0)
  1387. gps_points.at(i).RightPointId = ids.at(0);
  1388. vector<double> dnums = GetDoubleFromPrefix(add_info_list.at(i), "Vel", "Dir");
  1389. if(dnums.size() > 0)
  1390. gps_points.at(i).v = dnums.at(0);
  1391. dnums = GetDoubleFromPrefix(add_info_list.at(i), "Dir", "");
  1392. if(dnums.size() > 0)
  1393. gps_points.at(i).pos.a = gps_points.at(i).pos.dir = dnums.at(0);
  1394. }
  1395. }
  1396. }
  1397. return gps_points;
  1398. }
  1399. vector<WayPoint> MappingHelpers::GetCenterLaneDataVer0(TiXmlElement* pElem, const int& currLaneID)
  1400. {
  1401. vector<WayPoint> gps_points;
  1402. TiXmlElement* pV = pElem->FirstChildElement("Placemark");
  1403. if(pV)
  1404. pV = pV->FirstChildElement("LineString");
  1405. if(pV)
  1406. pV = pV->FirstChildElement("coordinates");
  1407. if(pV)
  1408. {
  1409. string coordinate_list;
  1410. if(!pV->NoChildren())
  1411. coordinate_list = pV->GetText();
  1412. istringstream str_stream(coordinate_list);
  1413. string token, temp;
  1414. while(getline(str_stream, token, ' '))
  1415. {
  1416. string lat, lon, alt;
  1417. double numLat=0, numLon=0, numAlt=0;
  1418. istringstream ss(token);
  1419. getline(ss, lat, ',');
  1420. getline(ss, lon, ',');
  1421. getline(ss, alt, ',');
  1422. numLat = atof(lat.c_str());
  1423. numLon = atof(lon.c_str());
  1424. numAlt = atof(alt.c_str());
  1425. WayPoint wp;
  1426. wp.pos.x = wp.pos.lat = numLat;
  1427. wp.pos.y = wp.pos.lon = numLon;
  1428. wp.pos.z = wp.pos.alt = numAlt;
  1429. wp.laneId = currLaneID;
  1430. gps_points.push_back(wp);
  1431. }
  1432. TiXmlElement* pInfoEl =pElem->FirstChildElement("Folder")->FirstChildElement("description");
  1433. string additional_info;
  1434. if(pInfoEl)
  1435. additional_info = pInfoEl->GetText();
  1436. additional_info.insert(additional_info.begin(), ',');
  1437. additional_info.erase(additional_info.end()-1);
  1438. vector<string> add_info_list = SplitString(additional_info, ",");
  1439. if(gps_points.size() == add_info_list.size())
  1440. {
  1441. for(unsigned int i=0; i< gps_points.size(); i++)
  1442. {
  1443. gps_points.at(i).id = GetIDsFromPrefix(add_info_list.at(i), "WPID", "C").at(0);
  1444. gps_points.at(i).fromIds = GetIDsFromPrefix(add_info_list.at(i), "From", "To");
  1445. gps_points.at(i).toIds = GetIDsFromPrefix(add_info_list.at(i), "To", "Vel");
  1446. gps_points.at(i).v = GetDoubleFromPrefix(add_info_list.at(i), "Vel", "Dir").at(0);
  1447. gps_points.at(i).pos.a = gps_points.at(i).pos.dir = GetDoubleFromPrefix(add_info_list.at(i), "Dir", "").at(0);
  1448. // if(currLaneID == 11115)
  1449. // {
  1450. //
  1451. // pair<ACTION_TYPE, double> act_cost;
  1452. // act_cost.first = FORWARD_ACTION;
  1453. // act_cost.second = 100;
  1454. // gps_points.at(i).actionCost.push_back(act_cost);
  1455. // }
  1456. }
  1457. }
  1458. }
  1459. return gps_points;
  1460. }
  1461. vector<int> MappingHelpers::GetIDsFromPrefix(const string& str, const string& prefix, const string& postfix)
  1462. {
  1463. int index1 = str.find(prefix)+prefix.size();
  1464. int index2 = str.find(postfix, index1);
  1465. if(index2 < 0 || postfix.size() ==0)
  1466. index2 = str.size();
  1467. string str_ids = str.substr(index1, index2-index1);
  1468. vector<int> ids;
  1469. vector<string> idstr = SplitString(str_ids, "_");
  1470. for(unsigned int i=0; i< idstr.size(); i++ )
  1471. {
  1472. if(idstr.at(i).size()>0)
  1473. {
  1474. int num = atoi(idstr.at(i).c_str());
  1475. //if(num>-1)
  1476. ids.push_back(num);
  1477. }
  1478. }
  1479. return ids;
  1480. }
  1481. vector<double> MappingHelpers::GetDoubleFromPrefix(const string& str, const string& prefix, const string& postfix)
  1482. {
  1483. int index1 = str.find(prefix)+prefix.size();
  1484. int index2 = str.find(postfix, index1);
  1485. if(index2 < 0 || postfix.size() ==0)
  1486. index2 = str.size();
  1487. string str_ids = str.substr(index1, index2-index1);
  1488. vector<double> ids;
  1489. vector<string> idstr = SplitString(str_ids, "_");
  1490. for(unsigned int i=0; i< idstr.size(); i++ )
  1491. {
  1492. if(idstr.at(i).size()>0)
  1493. {
  1494. double num = atof(idstr.at(i).c_str());
  1495. //if(num>-1)
  1496. ids.push_back(num);
  1497. }
  1498. }
  1499. return ids;
  1500. }
  1501. pair<ACTION_TYPE, double> MappingHelpers::GetActionPairFromPrefix(const string& str, const string& prefix, const string& postfix)
  1502. {
  1503. int index1 = str.find(prefix)+prefix.size();
  1504. int index2 = str.find(postfix, index1);
  1505. if(index2<0 || postfix.size() ==0)
  1506. index2 = str.size();
  1507. string str_ids = str.substr(index1, index2-index1);
  1508. pair<ACTION_TYPE, double> act_cost;
  1509. act_cost.first = FORWARD_ACTION;
  1510. act_cost.second = 0;
  1511. vector<string> idstr = SplitString(str_ids, "_");
  1512. if(idstr.size() >= 2)
  1513. {
  1514. if(idstr.at(0).size() > 0 && idstr.at(0).at(0) == 'L')
  1515. act_cost.first = LEFT_TURN_ACTION;
  1516. else if(idstr.at(0).size() > 0 && idstr.at(0).at(0) == 'R')
  1517. act_cost.first = RIGHT_TURN_ACTION;
  1518. if(idstr.at(1).size() > 0)
  1519. act_cost.second = atof(idstr.at(1).c_str());
  1520. }
  1521. return act_cost;
  1522. }
  1523. vector<string> MappingHelpers::SplitString(const string& str, const string& token)
  1524. {
  1525. vector<string> str_parts;
  1526. int iFirstPart = str.find(token);
  1527. while(iFirstPart >= 0)
  1528. {
  1529. iFirstPart++;
  1530. int iSecondPart = str.find(token, iFirstPart);
  1531. if(iSecondPart>0)
  1532. {
  1533. str_parts.push_back(str.substr(iFirstPart,iSecondPart - iFirstPart));
  1534. }
  1535. else
  1536. {
  1537. str_parts.push_back(str.substr(iFirstPart,str.size() - iFirstPart));
  1538. }
  1539. iFirstPart = iSecondPart;
  1540. }
  1541. return str_parts;
  1542. }
  1543. void MappingHelpers::FindAdjacentLanes(RoadNetwork& map)
  1544. {
  1545. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1546. {
  1547. //Link Lanes
  1548. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1549. {
  1550. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  1551. //Link left and right lanes
  1552. for(unsigned int rs_2 = 0; rs_2 < map.roadSegments.size(); rs_2++)
  1553. {
  1554. for(unsigned int i2 =0; i2 < map.roadSegments.at(rs_2).Lanes.size(); i2++)
  1555. {
  1556. int iCenter1 = pL->points.size()/2;
  1557. WayPoint wp_1 = pL->points.at(iCenter1);
  1558. int iCenter2 = PlanningHelpers::GetClosestNextPointIndexFast(map.roadSegments.at(rs_2).Lanes.at(i2).points, wp_1 );
  1559. WayPoint closest_p = map.roadSegments.at(rs_2).Lanes.at(i2).points.at(iCenter2);
  1560. double mid_a1 = wp_1.pos.a;
  1561. double mid_a2 = closest_p.pos.a;
  1562. double angle_diff = UtilityH::AngleBetweenTwoAnglesPositive(mid_a1, mid_a2);
  1563. double distance = distance2points(wp_1.pos, closest_p.pos);
  1564. if(pL->id != map.roadSegments.at(rs_2).Lanes.at(i2).id && angle_diff < 0.05 && distance < 3.5 && distance > 2.5)
  1565. {
  1566. double perp_distance = DBL_MAX;
  1567. if(pL->points.size() > 2 && map.roadSegments.at(rs_2).Lanes.at(i2).points.size()>2)
  1568. {
  1569. RelativeInfo info;
  1570. PlanningHelpers::GetRelativeInfo(pL->points, closest_p, info);
  1571. perp_distance = info.perp_distance;
  1572. //perp_distance = PlanningHelpers::GetPerpDistanceToVectorSimple(pL->points.at(iCenter1-1), pL->points.at(iCenter1+1), closest_p);
  1573. }
  1574. if(perp_distance > 1.0 && perp_distance < 10.0)
  1575. {
  1576. pL->pRightLane = &map.roadSegments.at(rs_2).Lanes.at(i2);
  1577. for(unsigned int i_internal = 0; i_internal< pL->points.size(); i_internal++)
  1578. {
  1579. if(i_internal<map.roadSegments.at(rs_2).Lanes.at(i2).points.size())
  1580. {
  1581. pL->points.at(i_internal).RightPointId = map.roadSegments.at(rs_2).Lanes.at(i2).id;
  1582. pL->points.at(i_internal).pRight = &map.roadSegments.at(rs_2).Lanes.at(i2).points.at(i_internal);
  1583. // map.roadSegments.at(rs_2).Lanes.at(i2).points.at(i_internal).pLeft = &pL->points.at(i_internal);
  1584. }
  1585. }
  1586. }
  1587. else if(perp_distance < -1.0 && perp_distance > -10.0)
  1588. {
  1589. pL->pLeftLane = &map.roadSegments.at(rs_2).Lanes.at(i2);
  1590. for(unsigned int i_internal = 0; i_internal< pL->points.size(); i_internal++)
  1591. {
  1592. if(i_internal<map.roadSegments.at(rs_2).Lanes.at(i2).points.size())
  1593. {
  1594. pL->points.at(i_internal).LeftPointId = map.roadSegments.at(rs_2).Lanes.at(i2).id;
  1595. pL->points.at(i_internal).pLeft = &map.roadSegments.at(rs_2).Lanes.at(i2).points.at(i_internal);
  1596. // map.roadSegments.at(rs_2).Lanes.at(i2).points.at(i_internal).pRight = &pL->points.at(i_internal);
  1597. }
  1598. }
  1599. }
  1600. }
  1601. }
  1602. }
  1603. }
  1604. }
  1605. }
  1606. void MappingHelpers::ExtractSignalData(const std::vector<UtilityHNS::AisanSignalFileReader::AisanSignal>& signal_data,
  1607. const std::vector<UtilityHNS::AisanVectorFileReader::AisanVector>& vector_data,
  1608. const std::vector<UtilityHNS::AisanPointsFileReader::AisanPoints>& points_data,
  1609. const GPSPoint& origin, RoadNetwork& map)
  1610. {
  1611. for(unsigned int is=0; is< signal_data.size(); is++)
  1612. {
  1613. if(signal_data.at(is).Type == 2)
  1614. {
  1615. TrafficLight tl;
  1616. tl.id = signal_data.at(is).ID;
  1617. tl.linkID = signal_data.at(is).LinkID;
  1618. tl.stoppingDistance = 0;
  1619. for(unsigned int iv = 0; iv < vector_data.size(); iv++)
  1620. {
  1621. if(signal_data.at(is).VID == vector_data.at(iv).VID)
  1622. {
  1623. for(unsigned int ip = 0; ip < points_data.size(); ip++)
  1624. {
  1625. if(vector_data.at(iv).PID == points_data.at(ip).PID)
  1626. {
  1627. tl.pos = GPSPoint(points_data.at(ip).Ly + origin.x, points_data.at(ip).Bx + origin.y, points_data.at(ip).H + origin.z, vector_data.at(iv).Hang*DEG2RAD);
  1628. break;
  1629. }
  1630. }
  1631. }
  1632. }
  1633. map.trafficLights.push_back(tl);
  1634. if(tl.id > g_max_traffic_light_id)
  1635. g_max_traffic_light_id = tl.id;
  1636. }
  1637. }
  1638. }
  1639. void MappingHelpers::ExtractStopLinesData(const std::vector<UtilityHNS::AisanStopLineFileReader::AisanStopLine>& stop_line_data,
  1640. const std::vector<UtilityHNS::AisanLinesFileReader::AisanLine>& line_data,
  1641. const std::vector<UtilityHNS::AisanPointsFileReader::AisanPoints>& points_data,
  1642. const GPSPoint& origin, RoadNetwork& map)
  1643. {
  1644. for(unsigned int ist=0; ist < stop_line_data.size(); ist++)
  1645. {
  1646. StopLine sl;
  1647. sl.linkID = stop_line_data.at(ist).LinkID;
  1648. sl.id = stop_line_data.at(ist).ID;
  1649. if(stop_line_data.at(ist).TLID>0)
  1650. sl.trafficLightID = stop_line_data.at(ist).TLID;
  1651. else
  1652. sl.stopSignID = 100+ist;
  1653. for(unsigned int il=0; il < line_data.size(); il++)
  1654. {
  1655. if(stop_line_data.at(ist).LID == line_data.at(il).LID)
  1656. {
  1657. int s_id = line_data.at(il).BPID;
  1658. int e_id = line_data.at(il).FPID;
  1659. for(unsigned int ip = 0; ip < points_data.size(); ip++)
  1660. {
  1661. if(points_data.at(ip).PID == s_id || points_data.at(ip).PID == e_id)
  1662. {
  1663. sl.points.push_back(GPSPoint(points_data.at(ip).Ly + origin.x, points_data.at(ip).Bx + origin.y, points_data.at(ip).H + origin.z, 0));
  1664. }
  1665. }
  1666. }
  1667. }
  1668. map.stopLines.push_back(sl);
  1669. if(sl.id > g_max_stop_line_id)
  1670. g_max_stop_line_id = sl.id;
  1671. }
  1672. }
  1673. void MappingHelpers::ExtractCurbData(const std::vector<UtilityHNS::AisanCurbFileReader::AisanCurb>& curb_data,
  1674. const std::vector<UtilityHNS::AisanLinesFileReader::AisanLine>& line_data,
  1675. const std::vector<UtilityHNS::AisanPointsFileReader::AisanPoints>& points_data,
  1676. const GPSPoint& origin, RoadNetwork& map)
  1677. {
  1678. for(unsigned int ic=0; ic < curb_data.size(); ic++)
  1679. {
  1680. Curb c;
  1681. c.id = curb_data.at(ic).ID;
  1682. for(unsigned int il=0; il < line_data.size(); il++)
  1683. {
  1684. if(curb_data.at(ic).LID == line_data.at(il).LID)
  1685. {
  1686. int s_id = line_data.at(il).BPID;
  1687. //int e_id = line_data.at(il).FPID;
  1688. for(unsigned int ip = 0; ip < points_data.size(); ip++)
  1689. {
  1690. if(points_data.at(ip).PID == s_id)
  1691. {
  1692. c.points.push_back(GPSPoint(points_data.at(ip).Ly + origin.x, points_data.at(ip).Bx + origin.y, points_data.at(ip).H + origin.z, 0));
  1693. WayPoint wp;
  1694. wp.pos = c.points.at(0);
  1695. Lane* pLane = GetClosestLaneFromMap(wp, map, 5);
  1696. if(pLane)
  1697. {
  1698. c.laneId = pLane->id;
  1699. c.pLane = pLane;
  1700. }
  1701. }
  1702. }
  1703. }
  1704. }
  1705. map.curbs.push_back(c);
  1706. }
  1707. }
  1708. void MappingHelpers::ExtractWayArea(const std::vector<UtilityHNS::AisanAreasFileReader::AisanArea>& area_data,
  1709. const std::vector<UtilityHNS::AisanWayareaFileReader::AisanWayarea>& wayarea_data,
  1710. const std::vector<UtilityHNS::AisanLinesFileReader::AisanLine>& line_data,
  1711. const std::vector<UtilityHNS::AisanPointsFileReader::AisanPoints>& points_data,
  1712. const GPSPoint& origin, RoadNetwork& map)
  1713. {
  1714. for(unsigned int iw=0; iw < wayarea_data.size(); iw ++)
  1715. {
  1716. Boundary bound;
  1717. bound.id = wayarea_data.at(iw).ID;
  1718. for(unsigned int ia=0; ia < area_data.size(); ia ++)
  1719. {
  1720. if(wayarea_data.at(iw).AID == area_data.at(ia).AID)
  1721. {
  1722. int s_id = area_data.at(ia).SLID;
  1723. int e_id = area_data.at(ia).ELID;
  1724. for(unsigned int il=0; il< line_data.size(); il++)
  1725. {
  1726. if(line_data.at(il).LID >= s_id && line_data.at(il).LID <= e_id)
  1727. {
  1728. for(unsigned int ip=0; ip < points_data.size(); ip++)
  1729. {
  1730. if(points_data.at(ip).PID == line_data.at(il).BPID)
  1731. {
  1732. GPSPoint p(points_data.at(ip).Ly + origin.x, points_data.at(ip).Bx + origin.y, points_data.at(ip).H + origin.z, 0);
  1733. bound.points.push_back(p);
  1734. }
  1735. }
  1736. }
  1737. }
  1738. }
  1739. }
  1740. map.boundaries.push_back(bound);
  1741. }
  1742. }
  1743. void MappingHelpers::LinkMissingBranchingWayPoints(RoadNetwork& map)
  1744. {
  1745. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1746. {
  1747. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1748. {
  1749. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  1750. {
  1751. WayPoint* pWP = &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  1752. for(unsigned int j = 0 ; j < pWP->toIds.size(); j++)
  1753. {
  1754. pWP->pFronts.push_back(FindWaypoint(pWP->toIds.at(j), map));
  1755. }
  1756. }
  1757. }
  1758. }
  1759. }
  1760. void MappingHelpers::LinkMissingBranchingWayPointsV2(RoadNetwork& map)
  1761. {
  1762. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1763. {
  1764. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1765. {
  1766. Lane* pLane = &map.roadSegments.at(rs).Lanes.at(i);
  1767. for(unsigned int p= 0; p < pLane->points.size(); p++)
  1768. {
  1769. WayPoint* pWP = &pLane->points.at(p);
  1770. if(p+1 == pLane->points.size()) // Last Point in Lane
  1771. {
  1772. for(unsigned int j = 0 ; j < pLane->toLanes.size(); j++)
  1773. {
  1774. //cout << "Link, Next Lane: " << pWP->laneId << ", WP: " << pWP->id << " To WP: " << pWP->toIds.at(j) << endl;
  1775. pWP->pFronts.push_back(&pLane->toLanes.at(j)->points.at(0));
  1776. }
  1777. }
  1778. else
  1779. {
  1780. if(pWP->toIds.size() > 1)
  1781. {
  1782. cout << "Error Error Erro ! Lane: " << pWP->laneId << ", Point: " << pWP->originalMapID << endl;
  1783. }
  1784. else
  1785. {
  1786. // cout << "Link, Same Lane: " << pWP->laneId << ", WP: " << pWP->id << " To WP: " << map.roadSegments.at(rs).Lanes.at(i).points.at(p+1).id << endl;
  1787. pWP->pFronts.push_back(&pLane->points.at(p+1));
  1788. }
  1789. }
  1790. }
  1791. }
  1792. }
  1793. }
  1794. void MappingHelpers::LinkTrafficLightsAndStopLines(RoadNetwork& map)
  1795. {
  1796. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1797. {
  1798. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1799. {
  1800. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  1801. {
  1802. for(unsigned int isl = 0; isl < map.stopLines.size(); isl++)
  1803. {
  1804. WayPoint* pWP = &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  1805. if(map.stopLines.at(isl).linkID == pWP->id)
  1806. {
  1807. map.stopLines.at(isl).laneId = pWP->laneId;
  1808. map.stopLines.at(isl).pLane = pWP->pLane;
  1809. map.roadSegments.at(rs).Lanes.at(i).stopLines.push_back(map.stopLines.at(isl));
  1810. pWP->stopLineID = map.stopLines.at(isl).id;
  1811. for(unsigned int itl = 0; itl < map.trafficLights.size(); itl++)
  1812. {
  1813. if(map.trafficLights.at(itl).id == map.stopLines.at(isl).trafficLightID)
  1814. {
  1815. map.trafficLights.at(itl).laneIds.push_back(pWP->laneId);
  1816. map.trafficLights.at(itl).pLanes.push_back(pWP->pLane);
  1817. }
  1818. }
  1819. break;
  1820. }
  1821. }
  1822. }
  1823. }
  1824. }
  1825. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1826. {
  1827. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1828. {
  1829. for(unsigned int itl = 0; itl < map.trafficLights.size(); itl++)
  1830. {
  1831. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  1832. {
  1833. WayPoint* pWP = &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  1834. if(map.trafficLights.at(itl).linkID == pWP->id)
  1835. {
  1836. map.roadSegments.at(rs).Lanes.at(i).trafficlights.push_back(map.trafficLights.at(itl));
  1837. break;
  1838. }
  1839. }
  1840. }
  1841. }
  1842. }
  1843. }
  1844. void MappingHelpers::LinkTrafficLightsAndStopLinesConData(const std::vector<UtilityHNS::AisanDataConnFileReader::DataConn>& conn_data,
  1845. const std::vector<std::pair<int,int> >& id_replace_list, RoadNetwork& map)
  1846. {
  1847. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1848. {
  1849. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1850. {
  1851. for(unsigned int ic = 0; ic < conn_data.size(); ic++)
  1852. {
  1853. UtilityHNS::AisanDataConnFileReader::DataConn data_conn = conn_data.at(ic);
  1854. ReplaceMyID(data_conn.LID , id_replace_list);
  1855. if(map.roadSegments.at(rs).Lanes.at(i).id == data_conn.LID)
  1856. {
  1857. for(unsigned int itl = 0; itl < map.trafficLights.size(); itl++)
  1858. {
  1859. if(map.trafficLights.at(itl).id == data_conn.SID)
  1860. {
  1861. map.trafficLights.at(itl).laneIds.push_back(map.roadSegments.at(rs).Lanes.at(i).id);
  1862. map.trafficLights.at(itl).pLanes.push_back(&map.roadSegments.at(rs).Lanes.at(i));
  1863. map.roadSegments.at(rs).Lanes.at(i).trafficlights.push_back(map.trafficLights.at(itl));
  1864. }
  1865. }
  1866. for(unsigned int isl = 0; isl < map.stopLines.size(); isl++)
  1867. {
  1868. if(map.stopLines.at(isl).id == data_conn.SLID)
  1869. {
  1870. map.stopLines.at(isl).laneId = map.roadSegments.at(rs).Lanes.at(i).id;
  1871. map.stopLines.at(isl).pLane = &map.roadSegments.at(rs).Lanes.at(i);
  1872. map.stopLines.at(isl).trafficLightID = data_conn.SID;
  1873. map.stopLines.at(isl).stopSignID = data_conn.SSID;
  1874. map.roadSegments.at(rs).Lanes.at(i).stopLines.push_back(map.stopLines.at(isl));
  1875. WayPoint wp((map.stopLines.at(isl).points.at(0).x+map.stopLines.at(isl).points.at(1).x)/2.0, (map.stopLines.at(isl).points.at(0).y+map.stopLines.at(isl).points.at(1).y)/2.0, (map.stopLines.at(isl).points.at(0).z+map.stopLines.at(isl).points.at(1).z)/2.0, (map.stopLines.at(isl).points.at(0).a+map.stopLines.at(isl).points.at(1).a)/2.0);
  1876. map.roadSegments.at(rs).Lanes.at(i).points.at(PlanningHelpers::GetClosestNextPointIndexFast(map.roadSegments.at(rs).Lanes.at(i).points, wp)).stopLineID = map.stopLines.at(isl).id;
  1877. }
  1878. }
  1879. }
  1880. }
  1881. }
  1882. }
  1883. }
  1884. void MappingHelpers::UpdateMapWithOccupancyGrid(OccupancyToGridMap& map_info, const std::vector<int>& data, RoadNetwork& map, std::vector<WayPoint*>& updated_list)
  1885. {
  1886. PlannerHNS::Mat3 rotationMat(- map_info.center.pos.a);
  1887. PlannerHNS::Mat3 translationMat(-map_info.center.pos.x, -map_info.center.pos.y);
  1888. updated_list.clear();
  1889. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1890. {
  1891. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1892. {
  1893. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  1894. {
  1895. WayPoint* pWP = &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  1896. GPSPoint relative_point = pWP->pos;
  1897. relative_point = translationMat * relative_point;
  1898. relative_point = rotationMat *relative_point;
  1899. int cell_value = 0;
  1900. if(map_info.GetCellIndexFromPoint(relative_point, data, cell_value) == true)
  1901. {
  1902. if(cell_value == 0)
  1903. {
  1904. bool bFound = false;
  1905. for(unsigned int i_action=0; i_action < pWP->actionCost.size(); i_action++)
  1906. {
  1907. if(pWP->actionCost.at(i_action).first == FORWARD_ACTION)
  1908. {
  1909. pWP->actionCost.at(i_action).second = 100;
  1910. bFound = true;
  1911. }
  1912. }
  1913. if(!bFound)
  1914. pWP->actionCost.push_back(make_pair(FORWARD_ACTION, 100));
  1915. updated_list.push_back(pWP);
  1916. }
  1917. }
  1918. }
  1919. }
  1920. }
  1921. }
  1922. void MappingHelpers::ConstructRoadNetworkFromROSMessageV2(const std::vector<UtilityHNS::AisanLanesFileReader::AisanLane>& lanes_data,
  1923. const std::vector<UtilityHNS::AisanPointsFileReader::AisanPoints>& points_data,
  1924. const std::vector<UtilityHNS::AisanCenterLinesFileReader::AisanCenterLine>& dt_data,
  1925. const std::vector<UtilityHNS::AisanIntersectionFileReader::AisanIntersection>& intersection_data,
  1926. const std::vector<UtilityHNS::AisanAreasFileReader::AisanArea>& area_data,
  1927. const std::vector<UtilityHNS::AisanLinesFileReader::AisanLine>& line_data,
  1928. const std::vector<UtilityHNS::AisanStopLineFileReader::AisanStopLine>& stop_line_data,
  1929. const std::vector<UtilityHNS::AisanSignalFileReader::AisanSignal>& signal_data,
  1930. const std::vector<UtilityHNS::AisanVectorFileReader::AisanVector>& vector_data,
  1931. const std::vector<UtilityHNS::AisanCurbFileReader::AisanCurb>& curb_data,
  1932. const std::vector<UtilityHNS::AisanRoadEdgeFileReader::AisanRoadEdge>& roadedge_data,
  1933. const std::vector<UtilityHNS::AisanWayareaFileReader::AisanWayarea>& wayarea_data,
  1934. const std::vector<UtilityHNS::AisanCrossWalkFileReader::AisanCrossWalk>& crosswalk_data,
  1935. const std::vector<UtilityHNS::AisanNodesFileReader::AisanNode>& nodes_data,
  1936. const std::vector<UtilityHNS::AisanDataConnFileReader::DataConn>& conn_data,
  1937. UtilityHNS::AisanLanesFileReader* pLaneData,
  1938. UtilityHNS::AisanPointsFileReader* pPointsData,
  1939. UtilityHNS::AisanNodesFileReader* pNodesData,
  1940. UtilityHNS::AisanLinesFileReader* pLinedata,
  1941. const GPSPoint& origin, RoadNetwork& map, const bool& bSpecialFlag,
  1942. const bool& bFindLaneChangeLanes, const bool& bFindCurbsAndWayArea)
  1943. {
  1944. vector<Lane> roadLanes;
  1945. for(unsigned int i=0; i< pLaneData->m_data_list.size(); i++)
  1946. {
  1947. if(pLaneData->m_data_list.at(i).LnID > g_max_lane_id)
  1948. g_max_lane_id = pLaneData->m_data_list.at(i).LnID;
  1949. }
  1950. cout << " >> Extracting Lanes ... " << endl;
  1951. CreateLanes(pLaneData, pPointsData, pNodesData, roadLanes);
  1952. cout << " >> Fix Waypoints errors ... " << endl;
  1953. FixTwoPointsLanes(roadLanes);
  1954. FixRedundantPointsLanes(roadLanes);
  1955. ConnectLanes(pLaneData, roadLanes);
  1956. cout << " >> Create Missing lane connections ... " << endl;
  1957. FixUnconnectedLanes(roadLanes);
  1958. ////FixTwoPointsLanes(roadLanes);
  1959. //map has one road segment
  1960. RoadSegment roadSegment1;
  1961. roadSegment1.id = 1;
  1962. roadSegment1.Lanes = roadLanes;
  1963. map.roadSegments.push_back(roadSegment1);
  1964. //Fix angle for lanes
  1965. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1966. {
  1967. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1968. {
  1969. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  1970. PlannerHNS::PlanningHelpers::FixAngleOnly(pL->points);
  1971. }
  1972. }
  1973. //Link Lanes and lane's waypoints by pointers
  1974. cout << " >> Link lanes and waypoints with pointers ... " << endl;
  1975. LinkLanesPointers(map);
  1976. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  1977. {
  1978. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  1979. {
  1980. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  1981. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  1982. {
  1983. if(pL->points.at(j).actionCost.size() > 0)
  1984. {
  1985. if(pL->points.at(j).actionCost.at(0).first == LEFT_TURN_ACTION)
  1986. {
  1987. AssignActionCostToLane(pL, LEFT_TURN_ACTION, LEFT_INITIAL_TURNS_COST);
  1988. break;
  1989. }
  1990. else if(pL->points.at(j).actionCost.at(0).first == RIGHT_TURN_ACTION)
  1991. {
  1992. AssignActionCostToLane(pL, RIGHT_TURN_ACTION, RIGHT_INITIAL_TURNS_COST);
  1993. break;
  1994. }
  1995. }
  1996. }
  1997. }
  1998. }
  1999. if(bFindLaneChangeLanes)
  2000. {
  2001. cout << " >> Extract Lane Change Information... " << endl;
  2002. FindAdjacentLanesV2(map);
  2003. }
  2004. //Extract Signals and StopLines
  2005. cout << " >> Extract Signal data ... " << endl;
  2006. ExtractSignalDataV2(signal_data, vector_data, pPointsData, origin, map);
  2007. //Stop Lines
  2008. cout << " >> Extract Stop lines data ... " << endl;
  2009. ExtractStopLinesDataV2(stop_line_data, pLinedata, pPointsData, origin, map);
  2010. if(bFindCurbsAndWayArea)
  2011. {
  2012. //Curbs
  2013. cout << " >> Extract curbs data ... " << endl;
  2014. ExtractCurbDataV2(curb_data, pLinedata, pPointsData, origin, map);
  2015. //Wayarea
  2016. cout << " >> Extract wayarea data ... " << endl;
  2017. ExtractWayArea(area_data, wayarea_data, line_data, points_data, origin, map);
  2018. }
  2019. //Link waypoints
  2020. cout << " >> Link missing branches and waypoints... " << endl;
  2021. LinkMissingBranchingWayPointsV2(map);
  2022. //Link StopLines and Traffic Lights
  2023. cout << " >> Link StopLines and Traffic Lights ... " << endl;
  2024. LinkTrafficLightsAndStopLinesV2(map);
  2025. // //LinkTrafficLightsAndStopLinesConData(conn_data, id_replace_list, map);
  2026. cout << " >> Map loaded from data with " << roadLanes.size() << " lanes" << endl;
  2027. }
  2028. bool MappingHelpers::GetPointFromDataList(UtilityHNS::AisanPointsFileReader* pPointsData,const int& pid, WayPoint& out_wp)
  2029. {
  2030. if(pPointsData == nullptr) return false;
  2031. AisanPointsFileReader::AisanPoints* pP = pPointsData->GetDataRowById(pid);
  2032. if(pP!=nullptr)
  2033. {
  2034. out_wp.id = pP->PID;
  2035. out_wp.pos.x = pP->Ly;
  2036. out_wp.pos.y = pP->Bx;
  2037. out_wp.pos.z = pP->H;
  2038. return true;
  2039. }
  2040. return false;
  2041. }
  2042. int MappingHelpers::GetBeginPointIdFromLaneNo(UtilityHNS::AisanLanesFileReader* pLaneData,
  2043. UtilityHNS::AisanPointsFileReader* pPointsData,
  2044. UtilityHNS::AisanNodesFileReader* pNodesData,const int& LnID)
  2045. {
  2046. if(pLaneData == nullptr) return false;
  2047. UtilityHNS::AisanLanesFileReader::AisanLane* pL = nullptr;
  2048. UtilityHNS::AisanPointsFileReader::AisanPoints* pP = nullptr;
  2049. UtilityHNS::AisanNodesFileReader::AisanNode* pN = nullptr;
  2050. pL = pLaneData->GetDataRowById(LnID);
  2051. if(pL!=nullptr)
  2052. {
  2053. return pL->FNID;
  2054. }
  2055. return 0;
  2056. }
  2057. int MappingHelpers::GetEndPointIdFromLaneNo(UtilityHNS::AisanLanesFileReader* pLaneData,
  2058. UtilityHNS::AisanPointsFileReader* pPointsData,
  2059. UtilityHNS::AisanNodesFileReader* pNodesData,const int& LnID)
  2060. {
  2061. UtilityHNS::AisanLanesFileReader::AisanLane* pL = nullptr;
  2062. UtilityHNS::AisanPointsFileReader::AisanPoints* pP = nullptr;
  2063. UtilityHNS::AisanNodesFileReader::AisanNode* pN = nullptr;
  2064. pL = pLaneData->GetDataRowById(LnID);
  2065. if(pL!=nullptr)
  2066. {
  2067. return pL->BNID;
  2068. }
  2069. return 0;
  2070. }
  2071. bool MappingHelpers::IsStartLanePoint(UtilityHNS::AisanLanesFileReader* pLaneData, UtilityHNS::AisanLanesFileReader::AisanLane* pL)
  2072. {
  2073. if(pL->JCT > 0 || pL->BLID == 0)
  2074. return true;
  2075. if(pL->BLID2 != 0 || pL->BLID3 != 0 || pL->BLID4 != 0)
  2076. return true;
  2077. UtilityHNS::AisanLanesFileReader::AisanLane* pPrevL = pLaneData->GetDataRowById(pL->BLID);
  2078. if(pPrevL == nullptr || pPrevL->FLID2 > 0 || pPrevL->FLID3 > 0 || pPrevL->FLID4 > 0 || pPrevL->JCT > 0)
  2079. return true;
  2080. return false;
  2081. }
  2082. bool MappingHelpers::IsEndLanePoint(UtilityHNS::AisanLanesFileReader* pLaneData, UtilityHNS::AisanLanesFileReader::AisanLane* pL)
  2083. {
  2084. if(pL->FLID2 > 0 || pL->FLID3 > 0 || pL->FLID4 > 0)
  2085. return true;
  2086. UtilityHNS::AisanLanesFileReader::AisanLane* pNextL = pLaneData->GetDataRowById(pL->FLID);
  2087. return IsStartLanePoint(pLaneData, pNextL);
  2088. }
  2089. void MappingHelpers::GetLanesStartPoints(UtilityHNS::AisanLanesFileReader* pLaneData,
  2090. std::vector<int>& m_LanesStartIds)
  2091. {
  2092. m_LanesStartIds.clear();
  2093. UtilityHNS::AisanLanesFileReader::AisanLane* pL = nullptr;
  2094. UtilityHNS::AisanLanesFileReader::AisanLane* pPrevL = nullptr;
  2095. for(unsigned int il=0; il < pLaneData->m_data_list.size(); il++)
  2096. {
  2097. pL = &pLaneData->m_data_list.at(il);
  2098. if(IsStartLanePoint(pLaneData, pL) == true)
  2099. {
  2100. m_LanesStartIds.push_back(pL->LnID);
  2101. }
  2102. if(DEBUG_MAP_PARSING)
  2103. {
  2104. if(IsStartLanePoint(pLaneData, pL) && IsEndLanePoint(pLaneData, pL))
  2105. cout << " :( :( :( Start And End in the same time !! " << pL->LnID << endl;
  2106. }
  2107. }
  2108. }
  2109. void MappingHelpers::ConnectLanes(UtilityHNS::AisanLanesFileReader* pLaneData, std::vector<PlannerHNS::Lane>& lanes)
  2110. {
  2111. for(unsigned int il = 0; il < lanes.size(); il++)
  2112. {
  2113. WayPoint fp = lanes.at(il).points.at(0);
  2114. UtilityHNS::AisanLanesFileReader::AisanLane* pFirstL = pLaneData->GetDataRowById(fp.originalMapID);
  2115. if(pFirstL!=nullptr)
  2116. {
  2117. if(pFirstL->BLID > 0)
  2118. lanes.at(il).fromIds.push_back(pFirstL->BLID);
  2119. if(pFirstL->BLID2 > 0)
  2120. lanes.at(il).fromIds.push_back(pFirstL->BLID2);
  2121. if(pFirstL->BLID3 > 0)
  2122. lanes.at(il).fromIds.push_back(pFirstL->BLID3);
  2123. if(pFirstL->BLID4 > 0)
  2124. lanes.at(il).fromIds.push_back(pFirstL->BLID4);
  2125. }
  2126. WayPoint ep = lanes.at(il).points.at(lanes.at(il).points.size()-1);
  2127. UtilityHNS::AisanLanesFileReader::AisanLane* pEndL = pLaneData->GetDataRowById(ep.originalMapID);
  2128. if(pEndL!=nullptr)
  2129. {
  2130. if(pEndL->FLID > 0)
  2131. lanes.at(il).toIds.push_back(pEndL->FLID);
  2132. if(pEndL->FLID2 > 0)
  2133. lanes.at(il).toIds.push_back(pEndL->FLID2);
  2134. if(pEndL->FLID3 > 0)
  2135. lanes.at(il).toIds.push_back(pEndL->FLID3);
  2136. if(pEndL->FLID4 > 0)
  2137. lanes.at(il).toIds.push_back(pEndL->FLID4);
  2138. }
  2139. }
  2140. }
  2141. void MappingHelpers::CreateLanes(UtilityHNS::AisanLanesFileReader* pLaneData,
  2142. UtilityHNS::AisanPointsFileReader* pPointsData,
  2143. UtilityHNS::AisanNodesFileReader* pNodesData,
  2144. std::vector<PlannerHNS::Lane>& out_lanes)
  2145. {
  2146. out_lanes.clear();
  2147. std::vector<int> start_lines;
  2148. GetLanesStartPoints(pLaneData, start_lines);
  2149. for(unsigned int l =0; l < start_lines.size(); l++)
  2150. {
  2151. Lane _lane;
  2152. GetLanePoints(pLaneData, pPointsData, pNodesData, start_lines.at(l), _lane);
  2153. out_lanes.push_back(_lane);
  2154. }
  2155. }
  2156. void MappingHelpers::GetLanePoints(UtilityHNS::AisanLanesFileReader* pLaneData,
  2157. UtilityHNS::AisanPointsFileReader* pPointsData,
  2158. UtilityHNS::AisanNodesFileReader* pNodesData, int lnID,
  2159. PlannerHNS::Lane& out_lane)
  2160. {
  2161. int next_lnid = lnID;
  2162. bool bStart = false;
  2163. bool bLast = false;
  2164. int _rID = 0;
  2165. out_lane.points.clear();
  2166. UtilityHNS::AisanLanesFileReader::AisanLane* pL = nullptr;
  2167. out_lane.id = lnID;
  2168. out_lane.speed = 0;
  2169. while(!bStart)
  2170. {
  2171. pL = pLaneData->GetDataRowById(next_lnid);
  2172. if(pL == nullptr)
  2173. {
  2174. cout << "## Error, Can't find lane from ID: " << next_lnid <<endl;
  2175. break;
  2176. }
  2177. next_lnid = pL->FLID;
  2178. if(next_lnid == 0)
  2179. bStart = true;
  2180. else
  2181. bStart = IsStartLanePoint(pLaneData, pLaneData->GetDataRowById(next_lnid));
  2182. // if(_lnid == 1267 ) //|| _lnid == 1268 || _lnid == 1269 || _lnid == 958)
  2183. // out_lane.id = lnID;
  2184. if(out_lane.points.size() == 0)
  2185. {
  2186. WayPoint wp1, wp2;
  2187. GetPointFromDataList(pPointsData, pNodesData->GetDataRowById(pL->BNID)->PID, wp1);
  2188. wp1.v = pL->RefVel == 0 ? DEFAULT_REF_VELOCITY : pL->RefVel;
  2189. wp1.id = pL->BNID;
  2190. if(pL->BLID != 0)
  2191. {
  2192. _rID = GetBeginPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->BLID);
  2193. if(_rID > 0)
  2194. wp1.fromIds.push_back(_rID);
  2195. }
  2196. if(pL->BLID2 != 0)
  2197. {
  2198. _rID = GetBeginPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->BLID2);
  2199. if(_rID > 0)
  2200. wp1.fromIds.push_back(_rID);
  2201. }
  2202. if(pL->BLID3 != 0)
  2203. {
  2204. _rID = GetBeginPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->BLID3);
  2205. if(_rID > 0)
  2206. wp1.fromIds.push_back(_rID);
  2207. }
  2208. if(pL->BLID4 != 0)
  2209. {
  2210. _rID = GetBeginPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->BLID4);
  2211. if(_rID > 0)
  2212. wp1.fromIds.push_back(_rID);
  2213. }
  2214. GetPointFromDataList(pPointsData, pNodesData->GetDataRowById(pL->FNID)->PID, wp2);
  2215. wp2.v = pL->RefVel == 0 ? DEFAULT_REF_VELOCITY : pL->RefVel;
  2216. wp2.id = pL->FNID;
  2217. if(bStart && pL->FLID != 0)
  2218. {
  2219. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID);
  2220. if(_rID > 0)
  2221. wp2.toIds.push_back(_rID);
  2222. }
  2223. if(pL->FLID2 != 0)
  2224. {
  2225. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID2);
  2226. if(_rID > 0)
  2227. wp2.toIds.push_back(_rID);
  2228. }
  2229. if(pL->FLID3 != 0)
  2230. {
  2231. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID3);
  2232. if(_rID > 0)
  2233. wp2.toIds.push_back(_rID);
  2234. }
  2235. if(pL->FLID4 != 0)
  2236. {
  2237. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID4);
  2238. if(_rID > 0)
  2239. wp2.toIds.push_back(_rID);
  2240. }
  2241. if(pL->LaneDir == 'L')
  2242. {
  2243. wp1.actionCost.push_back(make_pair(LEFT_TURN_ACTION, LEFT_INITIAL_TURNS_COST));
  2244. wp2.actionCost.push_back(make_pair(LEFT_TURN_ACTION, LEFT_INITIAL_TURNS_COST));
  2245. }
  2246. else if(pL->LaneDir == 'R')
  2247. {
  2248. wp1.actionCost.push_back(make_pair(RIGHT_TURN_ACTION, RIGHT_INITIAL_TURNS_COST));
  2249. wp2.actionCost.push_back(make_pair(RIGHT_TURN_ACTION, RIGHT_INITIAL_TURNS_COST));
  2250. }
  2251. else
  2252. {
  2253. wp1.actionCost.push_back(make_pair(FORWARD_ACTION, 0));
  2254. wp2.actionCost.push_back(make_pair(FORWARD_ACTION, 0));
  2255. }
  2256. wp1.originalMapID = pL->LnID;
  2257. wp2.originalMapID = pL->LnID;
  2258. wp1.laneId = lnID;
  2259. wp2.laneId = lnID;
  2260. wp1.toIds.push_back(wp2.id);
  2261. wp2.fromIds.push_back(wp1.id);
  2262. out_lane.points.push_back(wp1);
  2263. out_lane.points.push_back(wp2);
  2264. out_lane.speed = pL->RefVel == 0 ? DEFAULT_REF_VELOCITY : pL->RefVel;
  2265. }
  2266. else
  2267. {
  2268. WayPoint wp;
  2269. GetPointFromDataList(pPointsData, pNodesData->GetDataRowById(pL->FNID)->PID, wp);
  2270. wp.v = pL->RefVel == 0 ? DEFAULT_REF_VELOCITY : pL->RefVel;
  2271. wp.id = pL->FNID;
  2272. out_lane.points.at(out_lane.points.size()-1).toIds.push_back(wp.id);
  2273. wp.fromIds.push_back(out_lane.points.at(out_lane.points.size()-1).id);
  2274. if(bStart && pL->FLID != 0)
  2275. {
  2276. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID);
  2277. if(_rID > 0)
  2278. wp.toIds.push_back(_rID);
  2279. }
  2280. if(pL->FLID2 != 0)
  2281. {
  2282. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID2);
  2283. if(_rID > 0)
  2284. wp.toIds.push_back(_rID);
  2285. }
  2286. if(pL->FLID3 != 0)
  2287. {
  2288. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID3);
  2289. if(_rID > 0)
  2290. wp.toIds.push_back(_rID);
  2291. }
  2292. if(pL->FLID4 != 0)
  2293. {
  2294. _rID = GetEndPointIdFromLaneNo(pLaneData, pPointsData, pNodesData, pL->FLID4);
  2295. if(_rID > 0)
  2296. wp.toIds.push_back(_rID);
  2297. }
  2298. if(pL->LaneDir == 'L')
  2299. {
  2300. wp.actionCost.push_back(make_pair(LEFT_TURN_ACTION, LEFT_INITIAL_TURNS_COST));
  2301. }
  2302. else if(pL->LaneDir == 'R')
  2303. {
  2304. wp.actionCost.push_back(make_pair(RIGHT_TURN_ACTION, RIGHT_INITIAL_TURNS_COST));
  2305. }
  2306. else
  2307. {
  2308. wp.actionCost.push_back(make_pair(FORWARD_ACTION, 0));
  2309. }
  2310. wp.originalMapID = pL->LnID;
  2311. wp.laneId = lnID;
  2312. out_lane.points.push_back(wp);
  2313. }
  2314. // if(next_lnid == 0)
  2315. // break;
  2316. }
  2317. }
  2318. void MappingHelpers::FixRedundantPointsLanes(std::vector<Lane>& lanes)
  2319. {
  2320. //Fix Redundant point for two points in a row
  2321. for(unsigned int il=0; il < lanes.size(); il ++)
  2322. {
  2323. for(int ip = 1; ip < lanes.at(il).points.size(); ip++)
  2324. {
  2325. WayPoint* p1 = &lanes.at(il).points.at(ip-1);
  2326. WayPoint* p2 = &lanes.at(il).points.at(ip);
  2327. WayPoint* p3 = nullptr;
  2328. if(ip+1 < lanes.at(il).points.size())
  2329. p3 = &lanes.at(il).points.at(ip+1);
  2330. double d = hypot(p2->pos.y-p1->pos.y, p2->pos.x-p1->pos.x);
  2331. if(d == 0)
  2332. {
  2333. p1->toIds = p2->toIds;
  2334. p1->originalMapID = p2->originalMapID;
  2335. if(p3 != nullptr)
  2336. p3->fromIds = p2->fromIds;
  2337. lanes.at(il).points.erase(lanes.at(il).points.begin()+ip);
  2338. ip--;
  2339. if(DEBUG_MAP_PARSING)
  2340. cout << "Fixed Redundant Points for Lane:" << lanes.at(il).id << ", Current: " << ip << ", Size: " << lanes.at(il).points.size() << endl;
  2341. }
  2342. }
  2343. }
  2344. }
  2345. void MappingHelpers::FixTwoPointsLane(Lane& l)
  2346. {
  2347. if(l.points.size() == 2)
  2348. {
  2349. g_max_point_id++;
  2350. WayPoint wp = l.points.at(0);
  2351. wp.id = g_max_point_id;
  2352. wp.fromIds.clear();
  2353. wp.fromIds.push_back(l.points.at(0).id);
  2354. wp.toIds.clear();
  2355. wp.toIds.push_back(l.points.at(1).id);
  2356. l.points.at(0).toIds.clear();
  2357. l.points.at(0).toIds.push_back(wp.id);
  2358. l.points.at(1).fromIds.clear();
  2359. l.points.at(1).fromIds.push_back(wp.id);
  2360. wp.pos.x = (l.points.at(0).pos.x + l.points.at(1).pos.x)/2.0;
  2361. wp.pos.y = (l.points.at(0).pos.y + l.points.at(1).pos.y)/2.0;
  2362. wp.pos.z = (l.points.at(0).pos.z + l.points.at(1).pos.z)/2.0;
  2363. l.points.insert(l.points.begin()+1, wp);
  2364. }
  2365. else if(l.points.size() < 2)
  2366. {
  2367. cout << "## WOW Lane " << l.id << " With Size (" << l.points.size() << ") " << endl;
  2368. }
  2369. }
  2370. void MappingHelpers::FixTwoPointsLanes(std::vector<Lane>& lanes)
  2371. {
  2372. for(unsigned int il=0; il < lanes.size(); il ++)
  2373. {
  2374. for(unsigned int ip = 0; ip < lanes.at(il).points.size(); ip++)
  2375. {
  2376. if(lanes.at(il).points.at(ip).id > g_max_point_id)
  2377. {
  2378. g_max_point_id = lanes.at(il).points.at(ip).id;
  2379. }
  2380. }
  2381. }
  2382. for(unsigned int il=0; il < lanes.size(); il ++)
  2383. {
  2384. FixTwoPointsLane(lanes.at(il));
  2385. PlannerHNS::PlanningHelpers::CalcAngleAndCost(lanes.at(il).points);
  2386. }
  2387. }
  2388. void MappingHelpers::InsertWayPointToBackOfLane(const WayPoint& wp, Lane& lane, int& global_id)
  2389. {
  2390. WayPoint* pFirst = &lane.points.at(0);
  2391. pFirst->pos = wp.pos;
  2392. // WayPoint f_wp = *pFirst;
  2393. // f_wp.pos = wp.pos;
  2394. //
  2395. // //Give old first new ID
  2396. // global_id++;
  2397. // pFirst->id = global_id;
  2398. //
  2399. // //link ids
  2400. // f_wp.toIds.clear(); //only see front
  2401. // f_wp.toIds.push_back(pFirst->id);
  2402. //
  2403. // pFirst->fromIds.clear();
  2404. // pFirst->fromIds.push_back(f_wp.id);
  2405. //
  2406. // if(lane.points.size() > 1)
  2407. // {
  2408. // lane.points.at(1).fromIds.clear();
  2409. // lane.points.at(1).fromIds.push_back(pFirst->id);
  2410. // }
  2411. //
  2412. // lane.points.insert(lane.points.begin(), f_wp);
  2413. }
  2414. void MappingHelpers::InsertWayPointToFrontOfLane(const WayPoint& wp, Lane& lane, int& global_id)
  2415. {
  2416. WayPoint* pLast = &lane.points.at(lane.points.size()-1);
  2417. pLast->pos = wp.pos;
  2418. // WayPoint l_wp = *pLast;
  2419. // l_wp.pos = wp.pos;
  2420. //
  2421. // //Give old first new ID
  2422. // global_id++;
  2423. // pLast->id = global_id;
  2424. //
  2425. // //link ids
  2426. // l_wp.fromIds.clear(); //only see front
  2427. // l_wp.fromIds.push_back(pLast->id);
  2428. //
  2429. // pLast->toIds.clear();
  2430. // pLast->toIds.push_back(l_wp.id);
  2431. //
  2432. // if(lane.points.size() > 1)
  2433. // {
  2434. // lane.points.at(lane.points.size()-2).toIds.clear();
  2435. // lane.points.at(lane.points.size()-2).toIds.push_back(pLast->id);
  2436. // }
  2437. //
  2438. // lane.points.push_back(l_wp);
  2439. }
  2440. void MappingHelpers::FixUnconnectedLanes(std::vector<Lane>& lanes)
  2441. {
  2442. std::vector<Lane> sp_lanes = lanes;
  2443. bool bAtleastOneChange = false;
  2444. //Find before lanes
  2445. for(unsigned int il=0; il < lanes.size(); il ++)
  2446. {
  2447. if(lanes.at(il).fromIds.size() == 0)
  2448. {
  2449. double closest_d = DBL_MAX;
  2450. Lane* pL = nullptr;
  2451. Lane* pFL = nullptr;
  2452. for(int l=0; l < sp_lanes.size(); l ++)
  2453. {
  2454. if(lanes.at(il).id == sp_lanes.at(l).id)
  2455. {
  2456. pFL = &sp_lanes.at(l);
  2457. break;
  2458. }
  2459. }
  2460. PlannerHNS::RelativeInfo closest_info;
  2461. int closest_index = -1;
  2462. for(int l=0; l < sp_lanes.size(); l ++)
  2463. {
  2464. if(pFL->id != sp_lanes.at(l).id )
  2465. {
  2466. PlannerHNS::RelativeInfo info;
  2467. WayPoint lastofother = sp_lanes.at(l).points.at(sp_lanes.at(l).points.size()-1);
  2468. PlanningHelpers::GetRelativeInfoLimited(sp_lanes.at(l).points, pFL->points.at(0), info, 0);
  2469. double back_distance = hypot(lastofother.pos.y - pFL->points.at(0).pos.y, lastofother.pos.x - pFL->points.at(0).pos.x);
  2470. bool bCloseFromBack = false;
  2471. if((info.bAfter == true && back_distance < 15.0) || info.bAfter == false)
  2472. bCloseFromBack = true;
  2473. if(fabs(info.perp_distance) < 2 && fabs(info.perp_distance) < closest_d && info.bBefore == false && bCloseFromBack)
  2474. {
  2475. closest_d = fabs(info.perp_distance);
  2476. pL = &sp_lanes.at(l);
  2477. closest_info = info;
  2478. closest_info.angle_diff = back_distance;
  2479. closest_index = l;
  2480. }
  2481. }
  2482. }
  2483. if(pL != nullptr && pFL != nullptr)
  2484. {
  2485. if(closest_info.iFront == pL->points.size()-1)
  2486. {
  2487. pL->toIds.push_back(pFL->id);
  2488. pL->points.at(closest_info.iFront).toIds.push_back(pFL->points.at(0).id);
  2489. pFL->points.at(0).fromIds.push_back(pL->points.at(closest_info.iFront).id);
  2490. pFL->fromIds.push_back(pL->id);
  2491. bAtleastOneChange = true;
  2492. if(DEBUG_MAP_PARSING)
  2493. {
  2494. cout << "Closest Next Lane For: " << pFL->id << " , Is:" << pL->id << ", Distance=" << fabs(closest_info.perp_distance) << ", Size: " << pL->points.size() << ", back_index: " << closest_info.iBack <<", front_index: " << closest_info.iFront << ", Direct: " << closest_info.angle_diff << endl;
  2495. cout << "Don't Split , Perfect !" << endl;
  2496. }
  2497. }
  2498. else
  2499. {
  2500. // split from previous point
  2501. if(DEBUG_MAP_PARSING)
  2502. cout << "Closest Next Lane For: " << pFL->id << " , Is:" << pL->id << ", Distance=" << fabs(closest_info.perp_distance) << ", Size: " << pL->points.size() << ", back_index: " << closest_info.iBack <<", front_index: " << closest_info.iFront << ", Direct: " << closest_info.angle_diff << endl;
  2503. Lane front_half, back_half;
  2504. front_half.points.insert(front_half.points.begin(), pL->points.begin()+closest_info.iFront, pL->points.end());
  2505. front_half.toIds = pL->toIds;
  2506. front_half.fromIds.push_back(pL->id);
  2507. front_half.id = front_half.points.at(0).originalMapID;
  2508. front_half.areaId = pL->areaId;
  2509. front_half.dir = pL->dir;
  2510. front_half.num = pL->num;
  2511. front_half.roadId = pL->roadId;
  2512. front_half.speed = pL->speed;
  2513. front_half.type = pL->type;
  2514. front_half.width = pL->width;
  2515. back_half.points.insert(back_half.points.begin(), pL->points.begin(), pL->points.begin()+closest_info.iFront);
  2516. back_half.toIds.clear();
  2517. back_half.toIds.push_back(front_half.id);
  2518. back_half.toIds.push_back(pFL->id);
  2519. back_half.fromIds = pL->fromIds;
  2520. back_half.id = pL->id;
  2521. back_half.areaId = pL->areaId;
  2522. back_half.dir = pL->dir;
  2523. back_half.num = pL->num;
  2524. back_half.roadId = pL->roadId;
  2525. back_half.speed = pL->speed;
  2526. back_half.type = pL->type;
  2527. back_half.width = pL->width;
  2528. WayPoint* last_from_back = &back_half.points.at(back_half.points.size()-1);
  2529. WayPoint* first_from_front = &pFL->points.at(0);
  2530. last_from_back->toIds.push_back(first_from_front->id);
  2531. first_from_front->fromIds.push_back(last_from_back->id);
  2532. if(front_half.points.size() > 1 && back_half.points.size() > 1)
  2533. {
  2534. if(DEBUG_MAP_PARSING)
  2535. cout << "Split this one Nicely! first_half_size: " << front_half.points.size() << ", second_hald_size: " << back_half.points.size() << endl;
  2536. pFL->fromIds.push_back(back_half.id);
  2537. if(closest_index >= 0)
  2538. sp_lanes.erase(sp_lanes.begin()+closest_index);
  2539. else
  2540. cout << "## Alert Alert Alert !!!! " << endl;
  2541. // add perp point to lane points
  2542. InsertWayPointToBackOfLane(closest_info.perp_point, front_half, g_max_point_id);
  2543. InsertWayPointToFrontOfLane(closest_info.perp_point, back_half, g_max_point_id);
  2544. sp_lanes.push_back(front_half);
  2545. sp_lanes.push_back(back_half);
  2546. bAtleastOneChange = true;
  2547. }
  2548. else
  2549. {
  2550. if(DEBUG_MAP_PARSING)
  2551. cout << "=> Can't Split this one :( !" << endl;
  2552. }
  2553. }
  2554. }
  2555. else
  2556. {
  2557. if(DEBUG_MAP_PARSING)
  2558. cout << "=> Can't find Before Lanes For: " << lanes.at(il).id << endl;
  2559. }
  2560. }
  2561. }
  2562. lanes = sp_lanes;
  2563. //Find to lanes
  2564. for(unsigned int il=0; il < lanes.size(); il ++)
  2565. {
  2566. if(lanes.at(il).toIds.size() == 0)
  2567. {
  2568. double closest_d = DBL_MAX;
  2569. Lane* pL = nullptr;
  2570. Lane* pBL = nullptr;
  2571. for(int l=0; l < sp_lanes.size(); l ++)
  2572. {
  2573. if(lanes.at(il).id == sp_lanes.at(l).id)
  2574. {
  2575. pBL = &sp_lanes.at(l);
  2576. break;
  2577. }
  2578. }
  2579. PlannerHNS::RelativeInfo closest_info;
  2580. int closest_index = -1;
  2581. for(int l=0; l < sp_lanes.size(); l ++)
  2582. {
  2583. if(pBL->id != sp_lanes.at(l).id )
  2584. {
  2585. PlannerHNS::RelativeInfo info;
  2586. WayPoint firstofother = sp_lanes.at(l).points.at(0);
  2587. WayPoint last_point = pBL->points.at(pBL->points.size()-1);
  2588. PlanningHelpers::GetRelativeInfoLimited(sp_lanes.at(l).points, last_point, info, 0);
  2589. double front_distance = hypot(firstofother.pos.y - last_point.pos.y, firstofother.pos.x - last_point.pos.x);
  2590. bool bCloseFromFront = false;
  2591. if((info.bBefore == true && front_distance < 15.0) || info.bBefore == false)
  2592. bCloseFromFront = true;
  2593. if(fabs(info.perp_distance) < 2 && fabs(info.perp_distance) < closest_d && info.bAfter == false && bCloseFromFront)
  2594. {
  2595. closest_d = fabs(info.perp_distance);
  2596. pL = &sp_lanes.at(l);
  2597. closest_info = info;
  2598. closest_info.angle_diff = front_distance;
  2599. closest_index = l;
  2600. }
  2601. }
  2602. }
  2603. if(pL != nullptr && pBL != nullptr)
  2604. {
  2605. if(closest_info.iFront == 0)
  2606. {
  2607. pL->fromIds.push_back(pBL->id);
  2608. pL->points.at(closest_info.iFront).fromIds.push_back(pBL->points.at(pBL->points.size()-1).id);
  2609. pBL->points.at(pBL->points.size()-1).toIds.push_back(pL->points.at(closest_info.iFront).id);
  2610. pBL->toIds.push_back(pL->id);
  2611. bAtleastOneChange = true;
  2612. if(DEBUG_MAP_PARSING)
  2613. {
  2614. cout << "Closest Back Lane For: " << pBL->id << " , Is:" << pL->id << ", Distance=" << fabs(closest_info.perp_distance) << ", Size: " << pL->points.size() << ", back_index: " << closest_info.iBack <<", front_index: " << closest_info.iFront << ", Direct: " << closest_info.angle_diff << endl;
  2615. cout << "Don't Split , Perfect !" << endl;
  2616. }
  2617. }
  2618. else
  2619. {
  2620. // split from previous point
  2621. if(DEBUG_MAP_PARSING)
  2622. cout << "Closest Back Lane For: " << pBL->id << " , Is:" << pL->id << ", Distance=" << fabs(closest_info.perp_distance) << ", Size: " << pL->points.size() << ", back_index: " << closest_info.iBack <<", front_index: " << closest_info.iFront << ", Direct: " << closest_info.angle_diff << endl;
  2623. Lane front_half, back_half;
  2624. front_half.points.insert(front_half.points.begin(), pL->points.begin()+closest_info.iFront, pL->points.end());
  2625. front_half.toIds = pL->toIds;
  2626. front_half.fromIds.push_back(pL->id);
  2627. front_half.fromIds.push_back(pBL->id);
  2628. front_half.id = front_half.points.at(0).originalMapID;
  2629. front_half.areaId = pL->areaId;
  2630. front_half.dir = pL->dir;
  2631. front_half.num = pL->num;
  2632. front_half.roadId = pL->roadId;
  2633. front_half.speed = pL->speed;
  2634. front_half.type = pL->type;
  2635. front_half.width = pL->width;
  2636. back_half.points.insert(back_half.points.begin(), pL->points.begin(), pL->points.begin()+closest_info.iFront);
  2637. back_half.toIds.push_back(front_half.id);
  2638. back_half.fromIds = pL->fromIds;
  2639. back_half.id = pL->id;
  2640. back_half.areaId = pL->areaId;
  2641. back_half.dir = pL->dir;
  2642. back_half.num = pL->num;
  2643. back_half.roadId = pL->roadId;
  2644. back_half.speed = pL->speed;
  2645. back_half.type = pL->type;
  2646. back_half.width = pL->width;
  2647. WayPoint* first_from_next = &front_half.points.at(0);
  2648. WayPoint* last_from_front = &pBL->points.at(pBL->points.size()-1);
  2649. first_from_next->fromIds.push_back(last_from_front->id);
  2650. last_from_front->toIds.push_back(first_from_next->id);
  2651. if(front_half.points.size() > 1 && back_half.points.size() > 1)
  2652. {
  2653. if(DEBUG_MAP_PARSING)
  2654. cout << "Split this one Nicely! first_half_size: " << front_half.points.size() << ", second_hald_size: " << back_half.points.size() << endl;
  2655. pBL->toIds.push_back(front_half.id);
  2656. if(closest_index >= 0)
  2657. sp_lanes.erase(sp_lanes.begin()+closest_index);
  2658. else
  2659. cout << "## Alert Alert Alert !!!! " << endl;
  2660. // add perp point to lane points
  2661. InsertWayPointToBackOfLane(closest_info.perp_point, front_half, g_max_point_id);
  2662. InsertWayPointToFrontOfLane(closest_info.perp_point, back_half, g_max_point_id);
  2663. sp_lanes.push_back(front_half);
  2664. sp_lanes.push_back(back_half);
  2665. bAtleastOneChange = true;
  2666. }
  2667. else
  2668. {
  2669. if(DEBUG_MAP_PARSING)
  2670. cout << "=> Can't Split this one :( !" << endl;
  2671. }
  2672. }
  2673. }
  2674. else
  2675. {
  2676. if(DEBUG_MAP_PARSING)
  2677. cout << "=> Can't find After Lanes For: " << lanes.at(il).id << endl;
  2678. }
  2679. }
  2680. }
  2681. lanes = sp_lanes;
  2682. }
  2683. void MappingHelpers::LinkLanesPointers(PlannerHNS::RoadNetwork& map)
  2684. {
  2685. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  2686. {
  2687. //Link Lanes
  2688. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  2689. {
  2690. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  2691. for(unsigned int j = 0 ; j < pL->fromIds.size(); j++)
  2692. {
  2693. for(unsigned int l= 0; l < map.roadSegments.at(rs).Lanes.size(); l++)
  2694. {
  2695. if(map.roadSegments.at(rs).Lanes.at(l).id == pL->fromIds.at(j))
  2696. {
  2697. pL->fromLanes.push_back(&map.roadSegments.at(rs).Lanes.at(l));
  2698. }
  2699. }
  2700. }
  2701. for(unsigned int j = 0 ; j < pL->toIds.size(); j++)
  2702. {
  2703. for(unsigned int l= 0; l < map.roadSegments.at(rs).Lanes.size(); l++)
  2704. {
  2705. if(map.roadSegments.at(rs).Lanes.at(l).id == pL->toIds.at(j))
  2706. {
  2707. pL->toLanes.push_back(&map.roadSegments.at(rs).Lanes.at(l));
  2708. }
  2709. }
  2710. }
  2711. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  2712. {
  2713. pL->points.at(j).pLane = pL;
  2714. }
  2715. }
  2716. }
  2717. }
  2718. void MappingHelpers::ExtractCurbDataV2(const std::vector<UtilityHNS::AisanCurbFileReader::AisanCurb>& curb_data,
  2719. UtilityHNS::AisanLinesFileReader* pLinedata,
  2720. UtilityHNS::AisanPointsFileReader* pPointsData,
  2721. const GPSPoint& origin, RoadNetwork& map)
  2722. {
  2723. for(unsigned int ic=0; ic < curb_data.size(); ic++)
  2724. {
  2725. Curb c;
  2726. c.id = curb_data.at(ic).ID;
  2727. for(unsigned int il=0; il < pLinedata->m_data_list.size() ; il++)
  2728. {
  2729. if(curb_data.at(ic).LID == pLinedata->m_data_list.at(il).LID)
  2730. {
  2731. int s_id = pLinedata->m_data_list.at(il).BPID;
  2732. if(s_id == 0)
  2733. s_id = pLinedata->m_data_list.at(il).FPID;
  2734. AisanPointsFileReader::AisanPoints* pP = pPointsData->GetDataRowById(s_id);
  2735. if(pP != nullptr)
  2736. {
  2737. c.points.push_back(GPSPoint(pP->Ly + origin.x, pP->Bx + origin.y, pP->H + origin.z, 0));
  2738. WayPoint wp;
  2739. wp.pos = c.points.at(0);
  2740. Lane* pLane = GetClosestLaneFromMap(wp, map, 5);
  2741. if(pLane != nullptr)
  2742. {
  2743. c.laneId = pLane->id;
  2744. c.pLane = pLane;
  2745. }
  2746. }
  2747. }
  2748. }
  2749. map.curbs.push_back(c);
  2750. }
  2751. }
  2752. void MappingHelpers::ExtractSignalDataV2(const std::vector<UtilityHNS::AisanSignalFileReader::AisanSignal>& signal_data,
  2753. const std::vector<UtilityHNS::AisanVectorFileReader::AisanVector>& vector_data,
  2754. UtilityHNS::AisanPointsFileReader* pPointData,
  2755. const GPSPoint& origin, RoadNetwork& map)
  2756. {
  2757. for(unsigned int is=0; is< signal_data.size(); is++)
  2758. {
  2759. //if(signal_data.at(is).Type == 2)
  2760. {
  2761. TrafficLight tl;
  2762. tl.id = signal_data.at(is).ID;
  2763. tl.linkID = signal_data.at(is).LinkID;
  2764. tl.stoppingDistance = 0;
  2765. for(unsigned int iv = 0; iv < vector_data.size(); iv++)
  2766. {
  2767. if(signal_data.at(is).VID == vector_data.at(iv).VID)
  2768. {
  2769. AisanPointsFileReader::AisanPoints* pP = pPointData->GetDataRowById(vector_data.at(iv).PID);
  2770. if(pP != nullptr)
  2771. {
  2772. tl.pos = GPSPoint(pP->Ly + origin.x, pP->Bx + origin.y, pP->H + origin.z, vector_data.at(iv).Hang*DEG2RAD);
  2773. }
  2774. }
  2775. }
  2776. map.trafficLights.push_back(tl);
  2777. if(tl.id > g_max_traffic_light_id)
  2778. g_max_traffic_light_id = tl.id;
  2779. }
  2780. }
  2781. }
  2782. void MappingHelpers::ExtractStopLinesDataV2(const std::vector<UtilityHNS::AisanStopLineFileReader::AisanStopLine>& stop_line_data,
  2783. UtilityHNS::AisanLinesFileReader* pLineData,
  2784. UtilityHNS::AisanPointsFileReader* pPointData,
  2785. const GPSPoint& origin, RoadNetwork& map)
  2786. {
  2787. for(unsigned int ist=0; ist < stop_line_data.size(); ist++)
  2788. {
  2789. StopLine sl;
  2790. sl.linkID = stop_line_data.at(ist).LinkID;
  2791. sl.id = stop_line_data.at(ist).ID;
  2792. if(stop_line_data.at(ist).TLID>0)
  2793. sl.trafficLightID = stop_line_data.at(ist).TLID;
  2794. else
  2795. sl.stopSignID = 100+ist;
  2796. AisanLinesFileReader::AisanLine* pLine = pLineData->GetDataRowById(stop_line_data.at(ist).LID);
  2797. if(pLine != nullptr)
  2798. {
  2799. UtilityHNS::AisanPointsFileReader::AisanPoints* pStart = pPointData->GetDataRowById(pLine->BPID);
  2800. UtilityHNS::AisanPointsFileReader::AisanPoints* pEnd = pPointData->GetDataRowById(pLine->FPID);
  2801. if(pStart != nullptr)
  2802. sl.points.push_back(GPSPoint(pStart->Ly + origin.x, pStart->Bx + origin.y, pStart->H + origin.z, 0));
  2803. if(pEnd != nullptr)
  2804. sl.points.push_back(GPSPoint(pEnd->Ly + origin.x, pEnd->Bx + origin.y, pEnd->H + origin.z, 0));
  2805. }
  2806. map.stopLines.push_back(sl);
  2807. if(sl.id > g_max_stop_line_id)
  2808. g_max_stop_line_id = sl.id;
  2809. }
  2810. }
  2811. void MappingHelpers::LinkTrafficLightsAndStopLinesV2(RoadNetwork& map)
  2812. {
  2813. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  2814. {
  2815. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  2816. {
  2817. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  2818. {
  2819. WayPoint* pWP = &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  2820. for(unsigned int isl = 0; isl < map.stopLines.size(); isl++)
  2821. {
  2822. if(map.stopLines.at(isl).linkID == pWP->originalMapID)
  2823. {
  2824. map.stopLines.at(isl).laneId = pWP->laneId;
  2825. map.stopLines.at(isl).pLane = pWP->pLane;
  2826. map.roadSegments.at(rs).Lanes.at(i).stopLines.push_back(map.stopLines.at(isl));
  2827. pWP->stopLineID = map.stopLines.at(isl).id;
  2828. for(unsigned int itl = 0; itl < map.trafficLights.size(); itl++)
  2829. {
  2830. if(map.trafficLights.at(itl).id == map.stopLines.at(isl).trafficLightID)
  2831. {
  2832. map.trafficLights.at(itl).laneIds.push_back(pWP->laneId);
  2833. map.trafficLights.at(itl).pLanes.push_back(pWP->pLane);
  2834. }
  2835. }
  2836. break;
  2837. }
  2838. }
  2839. }
  2840. }
  2841. }
  2842. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  2843. {
  2844. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  2845. {
  2846. for(unsigned int itl = 0; itl < map.trafficLights.size(); itl++)
  2847. {
  2848. for(unsigned int p= 0; p < map.roadSegments.at(rs).Lanes.at(i).points.size(); p++)
  2849. {
  2850. WayPoint* pWP = &map.roadSegments.at(rs).Lanes.at(i).points.at(p);
  2851. if(map.trafficLights.at(itl).linkID == pWP->originalMapID)
  2852. {
  2853. map.roadSegments.at(rs).Lanes.at(i).trafficlights.push_back(map.trafficLights.at(itl));
  2854. break;
  2855. }
  2856. }
  2857. }
  2858. }
  2859. }
  2860. }
  2861. void MappingHelpers::FindAdjacentLanesV2(RoadNetwork& map)
  2862. {
  2863. bool bTestDebug = false;
  2864. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  2865. {
  2866. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  2867. {
  2868. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  2869. for(unsigned int i2 =0; i2 < map.roadSegments.at(rs).Lanes.size(); i2++)
  2870. {
  2871. Lane* pL2 = &map.roadSegments.at(rs).Lanes.at(i2);
  2872. if(pL->id == pL2->id) continue;
  2873. if(pL->id == 1683)
  2874. bTestDebug = true;
  2875. for(unsigned int p=0; p < pL->points.size(); p++)
  2876. {
  2877. WayPoint* pWP = &pL->points.at(p);
  2878. RelativeInfo info;
  2879. PlanningHelpers::GetRelativeInfoLimited(pL2->points, *pWP, info);
  2880. if(!info.bAfter && !info.bBefore && fabs(info.perp_distance) > 1.2 && fabs(info.perp_distance) < 3.5 && UtilityH::AngleBetweenTwoAnglesPositive(info.perp_point.pos.a, pWP->pos.a) < 0.06)
  2881. {
  2882. WayPoint* pWP2 = &pL2->points.at(info.iFront);
  2883. if(info.perp_distance < 0)
  2884. {
  2885. if(pWP->pRight == 0)
  2886. {
  2887. pWP->pRight = pWP2;
  2888. pWP->RightPointId = pWP2->id;
  2889. pWP->RightLnId = pL2->id;
  2890. pL->pRightLane = pL2;
  2891. }
  2892. if(pWP2->pLeft == 0)
  2893. {
  2894. pWP2->pLeft = pWP;
  2895. pWP2->LeftPointId = pWP->id;
  2896. pWP2->LeftLnId = pL->id;
  2897. pL2->pLeftLane = pL;
  2898. }
  2899. }
  2900. else
  2901. {
  2902. if(pWP->pLeft == 0)
  2903. {
  2904. pWP->pLeft = pWP2;
  2905. pWP->LeftPointId = pWP2->id;
  2906. pWP->LeftLnId = pL2->id;
  2907. pL->pLeftLane = pL2;
  2908. }
  2909. if(pWP2->pRight == 0)
  2910. {
  2911. pWP2->pRight = pWP->pLeft;
  2912. pWP2->RightPointId = pWP->id;
  2913. pWP2->RightLnId = pL->id;
  2914. pL2->pRightLane = pL;
  2915. }
  2916. }
  2917. }
  2918. }
  2919. }
  2920. }
  2921. }
  2922. }
  2923. void MappingHelpers::GetMapMaxIds(PlannerHNS::RoadNetwork& map)
  2924. {
  2925. for(unsigned int rs = 0; rs < map.roadSegments.size(); rs++)
  2926. {
  2927. for(unsigned int i =0; i < map.roadSegments.at(rs).Lanes.size(); i++)
  2928. {
  2929. Lane* pL = &map.roadSegments.at(rs).Lanes.at(i);
  2930. if(pL->id > g_max_lane_id)
  2931. g_max_lane_id = pL->id;
  2932. for(unsigned int j = 0 ; j < pL->points.size(); j++)
  2933. {
  2934. if(pL->points.at(j).id > g_max_point_id)
  2935. {
  2936. g_max_point_id = pL->points.at(j).id;
  2937. }
  2938. }
  2939. }
  2940. }
  2941. for(unsigned int i=0; i < map.stopLines.size(); i++)
  2942. {
  2943. if(map.stopLines.at(i).id > g_max_stop_line_id)
  2944. g_max_stop_line_id = map.stopLines.at(i).id;
  2945. }
  2946. for(unsigned int i=0; i < map.trafficLights.size(); i++)
  2947. {
  2948. if(map.trafficLights.at(i).id > g_max_traffic_light_id)
  2949. g_max_traffic_light_id = map.trafficLights.at(i).id;
  2950. }
  2951. }
  2952. } /* namespace PlannerHNS */