xodrdijkstra.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. #include "xodrdijkstra.h"
  2. #include <qdebug.h>
  3. #include <iostream>
  4. #include <memory>
  5. extern iv::Ivlog *givlog;
  6. extern iv::Ivfault *gfault;
  7. namespace iv {
  8. struct xodrtreeunit
  9. {
  10. double mlen;
  11. iv::xodrtreeunit * mparent;
  12. int nroad;
  13. iv::xodrtreeunit * mpchild;
  14. iv::xodrtreeunit * mpnextbro;
  15. };
  16. }
  17. namespace iv {
  18. struct vertexsame
  19. {
  20. vertexsame(int a,int b) {ma=a; mb =b;}
  21. int ma;
  22. int mb;
  23. };
  24. }
  25. namespace iv {
  26. struct vertexsamearray
  27. {
  28. std::vector<int> mvertexarray;
  29. vertexsamearray(int a,int b) {mvertexarray.push_back(a);mvertexarray.push_back(b);}
  30. bool isinarray(int x)
  31. {
  32. int i;
  33. for(i=0;i<mvertexarray.size();i++)
  34. {
  35. if(x == mvertexarray[i])return true;
  36. }
  37. return false;
  38. }
  39. void print()
  40. {
  41. char strout[10000];
  42. char strtem[30];
  43. int i;
  44. snprintf(strout,10000,"");
  45. for(i=0;i<mvertexarray.size();i++)
  46. {
  47. snprintf(strtem,30,"%d ",mvertexarray[i]);
  48. strncat(strout,strtem,10000);
  49. }
  50. qDebug(strout);
  51. }
  52. void sort()
  53. {
  54. int i;
  55. for(i=1;i<mvertexarray.size();i++)
  56. {
  57. if(mvertexarray[i]<mvertexarray[0])
  58. {
  59. int a = mvertexarray[0];
  60. mvertexarray[0] = mvertexarray[i];
  61. mvertexarray[i] = a;
  62. }
  63. }
  64. }
  65. };
  66. }
  67. Road * xodrdijkstra::GetRoadByID(int nRoadID)
  68. {
  69. OpenDrive * pxodr = mpxodr;
  70. int i;
  71. int nsize = pxodr->GetRoadCount();
  72. for(i=0;i<nsize;i++)
  73. {
  74. Road * pRoad = pxodr->GetRoad(i);
  75. if(atoi(pRoad->GetRoadId().data()) == nRoadID)
  76. {
  77. return pRoad;
  78. }
  79. }
  80. std::cout<<"xodrdijkstra::GetRoadByID "<<nRoadID<<" Fail."<<std::endl;
  81. return 0;
  82. }
  83. /**
  84. * @brief xodrdijkstra::xodrdijkstra
  85. * opendrive to directed graph ,so can use dijkstra
  86. * 1.将OpenDrive的Road转换为边,双行路转换为两条边,单行路转换为一条边,每条边包含两个顶点。
  87. * 2.寻找相同顶点,首先从Junction里寻找有连接关系的作为相同顶点插入到相同顶点表;然后从Road的Link关系里寻找相同顶点,第一种是Road不是Junction的,如果他的前面道路或后继道路是Road的,作为相同顶点关系插入到相同顶点表,第二种Road属于Junction的,如果他的后继道路是Road的,作为相同顶点插入到相同顶点表。
  88. * 3.根据相同顶点表,将相同顶点合并成相同顶点数组(比如(1,3) (2,3)合并为(1,3,2)),随后进行排序(比如(1,2,3)),然后对每条边的顶点重新设置顶点编号(比如3改为1),形成有向图。
  89. * @param pxodr pointer to a OpenDrive Map
  90. **/
  91. xodrdijkstra::xodrdijkstra(OpenDrive * pxodr)
  92. {
  93. mpxodr = pxodr;
  94. int i;
  95. double nlenth = 0;
  96. int nroadsize = mpxodr->GetRoadCount();
  97. for(i=0;i<nroadsize;i++)
  98. {
  99. Road * px = mpxodr->GetRoad(i);
  100. nlenth = nlenth + px->GetRoadLength();
  101. if(px->GetLaneSectionCount()<1)
  102. {
  103. qDebug("no lanesection");
  104. continue;
  105. }
  106. // qDebug("lane count is %d left %d right %d" ,px->GetLaneSection(0)->GetLaneCount(),px->GetLaneSection(0)->GetLeftLaneCount(),
  107. // px->GetLaneSection(0)->GetRightLaneCount());
  108. int j;
  109. for(j=0;j<px->GetLaneSectionCount();j++)
  110. {
  111. if(px->GetLaneSection(j)->GetLaneCount()<2)
  112. {
  113. givlog->info("no lane");
  114. continue;
  115. }
  116. if(px->GetLaneSection(j)->GetLeftLaneCount()>0)
  117. {
  118. double fseclen = 0;
  119. if(j<(px->GetLaneSectionCount() -1))
  120. {
  121. fseclen = px->GetLaneSection(j+1)->GetS() - px->GetLaneSection(j)->GetS();
  122. }
  123. else
  124. {
  125. fseclen = px->GetRoadLength() - px->GetLaneSection(j)->GetS();
  126. }
  127. roadedge xroad(atoi(px->GetRoadId().data()),atoi(px->GetRoadJunction().data()),fseclen,1,j,px);
  128. // roadedge xroad(atoi(px->GetRoadId().data()),atoi(px->GetRoadJunction().data()),px->GetRoadLength(),1,j,px);
  129. // xroad.mpx = px;
  130. mroadedge.push_back(xroad);
  131. }
  132. if(px->GetLaneSection(j)->GetRightLaneCount()>0)
  133. {
  134. double fseclen = 0;
  135. if(j<(px->GetLaneSectionCount() -1))
  136. {
  137. fseclen = px->GetLaneSection(j+1)->GetS() - px->GetLaneSection(j)->GetS();
  138. }
  139. else
  140. {
  141. fseclen = px->GetRoadLength() - px->GetLaneSection(j)->GetS();
  142. }
  143. // if((px->GetRoadId() == "10019")||(px->GetRoadId() == "10020"))
  144. // {
  145. // int axy = 1;
  146. // }
  147. roadedge xroad(atoi(px->GetRoadId().data()),atoi(px->GetRoadJunction().data()),fseclen,2,j,px);
  148. // roadedge xroad(atoi(px->GetRoadId().data()),atoi(px->GetRoadJunction().data()),px->GetRoadLength(),2,j,px);
  149. // xroad.mpx = px;
  150. mroadedge.push_back(xroad);
  151. }
  152. }
  153. // qDebug("id = %s juncton = %s ",px->GetRoadId().data(),px->GetRoadJunction().data());
  154. }
  155. givlog->debug("road %d ",mroadedge.size());
  156. std::vector<roadedge> * pxxx = &mroadedge;//10021
  157. if(mroadedge.size()<1)return;
  158. int nvertindex = 0;
  159. mroadedge[0].mvertexstart = 0;
  160. mroadedge[1].mvertexend = 1;
  161. //Init Vertex
  162. for(i=0;i<mroadedge.size();i++)
  163. {
  164. mroadedge[i].mbvertex = true;
  165. mroadedge[i].mvertexstart = 2*i;
  166. mroadedge[i].mvertexend = 2*i + 1;
  167. }
  168. //Find Same Vertex
  169. std::vector<iv::vertexsame> xvertexsame;
  170. std::vector<std::vector<int>> xcommonpoint;
  171. Junction * pj;
  172. for(i=0;i<pxodr->GetJunctionCount();i++)
  173. {
  174. pj = pxodr->GetJunction(i);
  175. int j;
  176. // if(atoi(pj->GetId().data()) == 810009)
  177. // {
  178. // int ggg = 11;
  179. // }
  180. for(j=0;j<pj->GetJunctionConnectionCount();j++)
  181. {
  182. JunctionConnection * pjc = pj->GetJunctionConnection(j);
  183. int road_from = atoi(pjc->GetIncomingRoad().data());
  184. int road_to = atoi(pjc->GetConnectingRoad().data());
  185. Road * pRoad_From = GetRoadByID(road_from);
  186. Road * pRoad_To = GetRoadByID(road_to);
  187. int k;
  188. for(k=0;k<pjc->GetJunctionLaneLinkCount();k++)
  189. {
  190. JunctionLaneLink * pjll = pjc->GetJunctionLaneLink(k);
  191. int lane_from = pjll->GetFrom();
  192. int lane_to = pjll->GetTo();
  193. lanenetunit lnu;
  194. int lr_from,lr_to;
  195. if(pjll->GetFrom()>0)lr_from = 1;
  196. else lr_from = 2;
  197. if(pjll->GetTo() > 0)lr_to = 1;
  198. else lr_to = 2;
  199. int fromedge;
  200. int toedge;
  201. fromedge = getroadedge(road_from,lr_from);
  202. toedge = getroadedge(road_to,lr_to);
  203. // qDebug("from %d %d to %d %d fromedge %d toedge %d %s",road_from, pjll->GetFrom(),
  204. // road_to,pjll->GetTo(),fromedge,toedge,pjc->GetContactPoint().data());
  205. if((fromedge == -1)||(toedge == -1))
  206. {
  207. givlog->debug("from %d %d to %d %d fromedge %d toedge %d %s",road_from, pjll->GetFrom(),
  208. road_to,pjll->GetTo(),fromedge,toedge,pjc->GetContactPoint().data());
  209. continue;
  210. }
  211. int a,b;
  212. if(strncmp(pjc->GetContactPoint().data(),"end",255) == 0)
  213. {
  214. // if(road_from == 30011)
  215. // {
  216. // int xx = 1;
  217. // }
  218. // if(lr_from == 2)
  219. if(lr_to == 1)
  220. {
  221. fromedge = getroadedge(road_from,lr_from,pRoad_From->GetLaneSectionCount() -1);
  222. toedge = getroadedge(road_to,lr_to);
  223. a = mroadedge[fromedge].mvertexend;
  224. b = mroadedge[toedge].mvertexstart;
  225. lnu.mpFromRoad = GetRoadByID(road_from);
  226. lnu.mpToRoad = GetRoadByID(road_to);
  227. lnu.mnFromSection = lnu.mpFromRoad->GetLaneSectionCount() - 1;
  228. lnu.mnToSection = 0;
  229. lnu.mnFromLane = lane_from;
  230. lnu.mnToLane = lane_to;
  231. }
  232. else
  233. {
  234. fromedge = getroadedge(road_from,lr_from);
  235. toedge = getroadedge(road_to,lr_to,pRoad_To->GetLaneSectionCount() -1);
  236. a = mroadedge[fromedge].mvertexstart;
  237. b = mroadedge[toedge].mvertexend;
  238. lnu.mpFromRoad = GetRoadByID(road_to);
  239. lnu.mpToRoad = GetRoadByID(road_from);
  240. lnu.mnFromSection = 0;
  241. lnu.mnToSection = lnu.mpToRoad->GetLaneSectionCount()-1;
  242. lnu.mnFromLane = lane_to;
  243. lnu.mnToLane = lane_from;
  244. }
  245. }
  246. if(strncmp(pjc->GetContactPoint().data(),"start",255) == 0)
  247. {
  248. // if(lr_from == 1)
  249. if(lr_to == 2)
  250. {
  251. fromedge = getroadedge(road_from,lr_from,pRoad_From->GetLaneSectionCount() -1);
  252. toedge = getroadedge(road_to,lr_to);
  253. a = mroadedge[fromedge].mvertexend;
  254. b = mroadedge[toedge].mvertexstart;
  255. lnu.mpFromRoad = GetRoadByID(road_from);
  256. lnu.mpToRoad = GetRoadByID(road_to);
  257. lnu.mnFromSection = lnu.mpFromRoad->GetLaneSectionCount() - 1;
  258. lnu.mnToSection = 0;
  259. lnu.mnFromLane = lane_from;
  260. lnu.mnToLane = lane_to;
  261. }
  262. else
  263. {
  264. fromedge = getroadedge(road_from,lr_from);
  265. toedge = getroadedge(road_to,lr_to,pRoad_To->GetLaneSectionCount() -1);
  266. a = mroadedge[fromedge].mvertexstart;
  267. b = mroadedge[toedge].mvertexend;
  268. lnu.mpFromRoad = GetRoadByID(road_to);
  269. lnu.mpToRoad = GetRoadByID(road_from);
  270. lnu.mnFromSection = 0;
  271. lnu.mnToSection = lnu.mpToRoad->GetLaneSectionCount()-1;
  272. lnu.mnFromLane = lane_to;
  273. lnu.mnToLane = lane_from;
  274. }
  275. mlanenet.push_back(lnu);
  276. iv::vertexsame vx(a,b);
  277. xvertexsame.push_back(vx);
  278. }
  279. }
  280. }
  281. }
  282. givlog->debug("After Junction vertex same is %d ",xvertexsame.size());
  283. for(i=0;i<nroadsize;i++)
  284. {
  285. Road * px = mpxodr->GetRoad(i);
  286. if(px->GetLaneSectionCount() == 1)
  287. {
  288. if(px->GetPredecessor() != 0)
  289. {
  290. if(strncmp(px->GetPredecessor()->GetElementType().data(),"road",255) == 0)
  291. {
  292. int k;
  293. int nroadpre = atoi(px->GetPredecessor()->GetElementId().data());
  294. int nroadcur = atoi(px->GetRoadId().data());
  295. for(k=0;k<px->GetLaneSection(0)->GetLaneCount();k++)
  296. {
  297. Lane * plane = px->GetLaneSection(0)->GetLane(k);
  298. if(plane->GetId() == 0)continue;
  299. if(plane->IsPredecessorSet())
  300. {
  301. int a,b;
  302. int npre = plane->GetPredecessor();
  303. int nid = plane->GetId();
  304. lanenetunit lnu;
  305. lnu.mnFromSection = 0;
  306. lnu.mnToSection = 0;
  307. if(strncmp(px->GetPredecessor()->GetContactPoint().data(),"end",255) == 0)
  308. {
  309. if(npre < 0)
  310. {
  311. int nedgepre = getroadedge(nroadpre,2,GetRoadByID(nroadpre)->GetLaneSectionCount()-1);
  312. lnu.mnFromSection = GetRoadByID(nroadpre)->GetLaneSectionCount()-1;
  313. a = mroadedge[nedgepre].mvertexend;
  314. lnu.mpFromRoad = GetRoadByID(nroadpre);
  315. lnu.mpToRoad = GetRoadByID(nroadcur);
  316. lnu.mnFromLane = npre;
  317. lnu.mnToLane = nid;
  318. }
  319. else
  320. {
  321. int nedgepre = getroadedge(nroadpre,1,GetRoadByID(nroadpre)->GetLaneSectionCount()-1);
  322. lnu.mnToSection = GetRoadByID(nroadpre)->GetLaneSectionCount()-1;
  323. a = mroadedge[nedgepre].mvertexstart;
  324. lnu.mpFromRoad = GetRoadByID(nroadcur);
  325. lnu.mpToRoad = GetRoadByID(nroadpre);
  326. lnu.mnFromLane = nid;
  327. lnu.mnToLane = npre;
  328. }
  329. }
  330. if(strncmp(px->GetPredecessor()->GetContactPoint().data(),"start",255) == 0)
  331. {
  332. if(npre < 0)
  333. {
  334. int nedgepre = getroadedge(nroadpre,2);
  335. a = mroadedge[nedgepre].mvertexstart;
  336. lnu.mpFromRoad = GetRoadByID(nroadcur);
  337. lnu.mpToRoad = GetRoadByID(nroadpre);
  338. lnu.mnFromLane = nid;
  339. lnu.mnToLane = npre;
  340. }
  341. else
  342. {
  343. int nedgepre = getroadedge(nroadpre,1);
  344. a = mroadedge[nedgepre].mvertexend;
  345. lnu.mpFromRoad = GetRoadByID(nroadpre);
  346. lnu.mpToRoad = GetRoadByID(nroadcur);
  347. lnu.mnFromLane = npre;
  348. lnu.mnToLane = nid;
  349. }
  350. }
  351. if(nid < 0)
  352. {
  353. int nedgecur = getroadedge(nroadcur,2);
  354. b = mroadedge[nedgecur].mvertexstart;
  355. }
  356. else
  357. {
  358. int nedgecur = getroadedge(nroadcur,1);
  359. b = mroadedge[nedgecur].mvertexend;
  360. }
  361. mlanenet.push_back(lnu);
  362. iv::vertexsame vx(a,b);
  363. xvertexsame.push_back(vx);
  364. }
  365. if(plane->IsSuccessorSet())
  366. {
  367. }
  368. }
  369. }
  370. }
  371. if(px->GetSuccessor() != 0)
  372. {
  373. if(strncmp(px->GetSuccessor()->GetElementType().data(),"road",255) == 0)
  374. {
  375. int k;
  376. int nroadnext = atoi(px->GetSuccessor()->GetElementId().data());
  377. int nroadcur = atoi(px->GetRoadId().data());
  378. for(k=0;k<px->GetLaneSection(px->GetLaneSectionCount()-1)->GetLaneCount();k++)
  379. {
  380. // if(nroadcur == 30001)
  381. // {
  382. // int xg = 0;
  383. // xg++;
  384. // }
  385. Lane * plane = px->GetLaneSection(px->GetLaneSectionCount()-1)->GetLane(k);
  386. if(plane->GetId() == 0)continue;
  387. if(plane->IsSuccessorSet())
  388. {
  389. int a,b;
  390. int nnext = plane->GetSuccessor();
  391. int nid = plane->GetId();
  392. lanenetunit lnu;
  393. lnu.mnFromSection = 0;
  394. lnu.mnToSection = 0;
  395. if(strncmp(px->GetSuccessor()->GetContactPoint().data(),"end",255) == 0)
  396. {
  397. if(nnext < 0)
  398. {
  399. int nedgenext = getroadedge(nroadnext,2,GetRoadByID(nroadnext)->GetLaneSectionCount()-1);
  400. b = mroadedge[nedgenext].mvertexend;
  401. lnu.mnFromSection = GetRoadByID(nroadnext)->GetLaneSectionCount()-1;
  402. lnu.mpFromRoad = GetRoadByID(nroadnext);
  403. lnu.mpToRoad = GetRoadByID(nroadcur);
  404. lnu.mnFromLane = nnext;
  405. lnu.mnToLane = nid;
  406. }
  407. else
  408. {
  409. int nedgenext = getroadedge(nroadnext,1,GetRoadByID(nroadnext)->GetLaneSectionCount()-1);
  410. b = mroadedge[nedgenext].mvertexstart;
  411. lnu.mpFromRoad = GetRoadByID(nroadcur);
  412. lnu.mnToSection = GetRoadByID(nroadnext)->GetLaneSectionCount()-1;
  413. lnu.mpToRoad = GetRoadByID(nroadnext);
  414. lnu.mnFromLane = nid;
  415. lnu.mnToLane = nnext;
  416. }
  417. }
  418. if(strncmp(px->GetSuccessor()->GetContactPoint().data(),"start",255) == 0)
  419. {
  420. if(nnext < 0)
  421. {
  422. int nedgenext = getroadedge(nroadnext,2);
  423. b = mroadedge[nedgenext].mvertexstart;
  424. lnu.mpFromRoad = GetRoadByID(nroadcur);
  425. lnu.mpToRoad = GetRoadByID(nroadnext);
  426. lnu.mnFromLane = nid;
  427. lnu.mnToLane = nnext;
  428. }
  429. else
  430. {
  431. int nedgenext = getroadedge(nroadnext,1);
  432. b = mroadedge[nedgenext].mvertexend;
  433. lnu.mpFromRoad = GetRoadByID(nroadnext);
  434. lnu.mpToRoad = GetRoadByID(nroadcur);
  435. lnu.mnFromLane = nnext;
  436. lnu.mnToLane = nid;
  437. }
  438. }
  439. if(nid < 0)
  440. {
  441. int nedgecur = getroadedge(nroadcur,2);
  442. a = mroadedge[nedgecur].mvertexend;
  443. }
  444. else
  445. {
  446. int nedgecur = getroadedge(nroadcur,1);
  447. a = mroadedge[nedgecur].mvertexstart;
  448. }
  449. iv::vertexsame vx(a,b);
  450. xvertexsame.push_back(vx);
  451. mlanenet.push_back(lnu);
  452. }
  453. }
  454. }
  455. }
  456. }
  457. else
  458. {
  459. if(px->GetPredecessor() != 0)
  460. {
  461. if(strncmp(px->GetPredecessor()->GetElementType().data(),"road",255) == 0)
  462. {
  463. int k;
  464. int nroadpre = atoi(px->GetPredecessor()->GetElementId().data());
  465. int nroadcur = atoi(px->GetRoadId().data());
  466. Road * pRoad_Pre = GetRoadByID(nroadpre);
  467. if(pRoad_Pre == 0)
  468. {
  469. qDebug("Pre Road Missing.");
  470. continue;
  471. }
  472. // if(nroadcur == 30012)
  473. // {
  474. // int ag;
  475. // ag= 1;
  476. // }
  477. for(k=0;k<px->GetLaneSection(0)->GetLaneCount();k++)
  478. {
  479. Lane * plane = px->GetLaneSection(0)->GetLane(k);
  480. if(plane->GetId() == 0)continue;
  481. if(plane->IsPredecessorSet())
  482. {
  483. int a,b;
  484. int npre = plane->GetPredecessor();
  485. int nid = plane->GetId();
  486. lanenetunit lnu;
  487. if(strncmp(px->GetPredecessor()->GetContactPoint().data(),"end",255) == 0)
  488. {
  489. if(npre < 0)
  490. {
  491. int nedgepre = getroadedge(nroadpre,2,pRoad_Pre->GetLaneSectionCount()-1);
  492. a = mroadedge[nedgepre].mvertexend;
  493. lnu.mpFromRoad = GetRoadByID(nroadpre);
  494. lnu.mpToRoad = GetRoadByID(nroadcur);
  495. lnu.mnFromSection = lnu.mpFromRoad->GetLaneSectionCount() - 1;
  496. lnu.mnToSection = 0;
  497. lnu.mnFromLane = npre;
  498. lnu.mnToLane = nid;
  499. }
  500. else
  501. {
  502. int nedgepre = getroadedge(nroadpre,1,pRoad_Pre->GetLaneSectionCount()-1);
  503. a = mroadedge[nedgepre].mvertexstart;
  504. lnu.mpFromRoad = GetRoadByID(nroadcur);
  505. lnu.mpToRoad = GetRoadByID(nroadpre);
  506. lnu.mnFromSection = 0;
  507. lnu.mnToSection = lnu.mpToRoad->GetLaneSectionCount() -1;
  508. lnu.mnFromLane = nid;
  509. lnu.mnToLane = npre;
  510. }
  511. }
  512. if(strncmp(px->GetPredecessor()->GetContactPoint().data(),"start",255) == 0)
  513. {
  514. if(npre < 0)
  515. {
  516. int nedgepre = getroadedge(nroadpre,2);
  517. a = mroadedge[nedgepre].mvertexstart;
  518. lnu.mpFromRoad = GetRoadByID(nroadcur);
  519. lnu.mpToRoad = GetRoadByID(nroadpre);
  520. lnu.mnFromSection = 0;
  521. lnu.mnToSection = lnu.mpToRoad->GetLaneSectionCount() -1;
  522. lnu.mnFromLane = nid;
  523. lnu.mnToLane = npre;
  524. }
  525. else
  526. {
  527. int nedgepre = getroadedge(nroadpre,1);
  528. a = mroadedge[nedgepre].mvertexend;
  529. lnu.mpFromRoad = GetRoadByID(nroadpre);
  530. lnu.mpToRoad = GetRoadByID(nroadcur);
  531. lnu.mnFromSection = lnu.mpFromRoad->GetLaneSectionCount() - 1;
  532. lnu.mnToSection = 0;
  533. lnu.mnFromLane = npre;
  534. lnu.mnToLane = nid;
  535. }
  536. }
  537. if(nid < 0)
  538. {
  539. int nedgecur = getroadedge(nroadcur,2);
  540. b = mroadedge[nedgecur].mvertexstart;
  541. }
  542. else
  543. {
  544. int nedgecur = getroadedge(nroadcur,1);
  545. b = mroadedge[nedgecur].mvertexend;
  546. }
  547. iv::vertexsame vx(a,b);
  548. xvertexsame.push_back(vx);
  549. mlanenet.push_back(lnu);
  550. }
  551. if(plane->IsSuccessorSet())
  552. {
  553. }
  554. }
  555. }
  556. }
  557. int j;
  558. for(j=1;j<px->GetLaneSectionCount();j++)
  559. {
  560. int k;
  561. int nroadpre = atoi(px->GetRoadId().data());
  562. int nroadcur = atoi(px->GetRoadId().data());
  563. for(k=0;k<px->GetLaneSection(j)->GetLaneCount();k++)
  564. {
  565. Lane * plane = px->GetLaneSection(j)->GetLane(k);
  566. if(plane->GetId() == 0)continue;
  567. if(plane->IsPredecessorSet())
  568. {
  569. int a,b;
  570. int npre = plane->GetPredecessor();
  571. int nid = plane->GetId();
  572. lanenetunit lnu;
  573. lnu.mpFromRoad = GetRoadByID(nroadcur);
  574. lnu.mpToRoad = lnu.mpFromRoad;
  575. if(npre < 0)
  576. {
  577. int nedgepre = getroadedge(nroadpre,2,j-1);
  578. a = mroadedge[nedgepre].mvertexend;
  579. lnu.mnFromSection = j-1;
  580. lnu.mnToSection = j;
  581. lnu.mnFromLane = npre;
  582. lnu.mnToLane = nid;
  583. }
  584. else
  585. {
  586. int nedgepre = getroadedge(nroadpre,1,j-1);
  587. a = mroadedge[nedgepre].mvertexstart;
  588. lnu.mnFromSection = j;
  589. lnu.mnToSection = j-1;
  590. lnu.mnFromLane = nid;
  591. lnu.mnToLane = npre;
  592. }
  593. if(nid < 0)
  594. {
  595. int nedgecur = getroadedge(nroadcur,2,j);
  596. b = mroadedge[nedgecur].mvertexstart;
  597. }
  598. else
  599. {
  600. int nedgecur = getroadedge(nroadcur,1,j);
  601. b = mroadedge[nedgecur].mvertexend;
  602. }
  603. mlanenet.push_back(lnu);
  604. iv::vertexsame vx(a,b);
  605. xvertexsame.push_back(vx);
  606. }
  607. if(plane->IsSuccessorSet())
  608. {
  609. }
  610. }
  611. }
  612. if(px->GetSuccessor() != 0)
  613. {
  614. if(strncmp(px->GetSuccessor()->GetElementType().data(),"road",255) == 0)
  615. {
  616. int k;
  617. int nroadnext = atoi(px->GetSuccessor()->GetElementId().data());
  618. int nroadcur = atoi(px->GetRoadId().data());
  619. if(nroadcur == 116)
  620. {
  621. int xxxxx = 1;
  622. }
  623. for(k=0;k<px->GetLaneSection(px->GetLaneSectionCount()-1)->GetLaneCount();k++)
  624. {
  625. Lane * plane = px->GetLaneSection(px->GetLaneSectionCount()-1)->GetLane(k);
  626. if(plane->GetId() == 0)continue;
  627. if(plane->IsSuccessorSet())
  628. {
  629. int a,b;
  630. int nnext = plane->GetSuccessor();
  631. int nid = plane->GetId();
  632. lanenetunit lnu;
  633. if(strncmp(px->GetSuccessor()->GetContactPoint().data(),"end",255) == 0)
  634. {
  635. if(nnext < 0)
  636. {
  637. int nedgenext = getroadedge(nroadnext,2,GetRoadByID(nroadnext)->GetLaneSectionCount()-1);
  638. b = mroadedge[nedgenext].mvertexend;
  639. lnu.mpFromRoad = GetRoadByID(nroadnext);
  640. lnu.mpToRoad = GetRoadByID(nroadcur);
  641. lnu.mnFromSection = GetRoadByID(nroadnext)->GetLaneSectionCount()-1;
  642. lnu.mnToSection = lnu.mpToRoad->GetLaneSectionCount() - 1;
  643. lnu.mnFromLane = nnext;
  644. lnu.mnToLane = nid;
  645. }
  646. else
  647. {
  648. int nedgenext = getroadedge(nroadnext,1,GetRoadByID(nroadnext)->GetLaneSectionCount()-1);
  649. b = mroadedge[nedgenext].mvertexstart;
  650. lnu.mpFromRoad = GetRoadByID(nroadcur);
  651. lnu.mpToRoad = GetRoadByID(nroadnext);
  652. lnu.mnFromSection = lnu.mpFromRoad->GetLaneSectionCount() -1;
  653. lnu.mnToSection = GetRoadByID(nroadnext)->GetLaneSectionCount()-1;
  654. lnu.mnFromLane = nid;
  655. lnu.mnToLane = nnext;
  656. }
  657. }
  658. if(strncmp(px->GetSuccessor()->GetContactPoint().data(),"start",255) == 0)
  659. {
  660. if(nnext < 0)
  661. {
  662. int nedgenext = getroadedge(nroadnext,2);
  663. b = mroadedge[nedgenext].mvertexstart;
  664. lnu.mpFromRoad = GetRoadByID(nroadcur);
  665. lnu.mpToRoad = GetRoadByID(nroadnext);
  666. lnu.mnFromSection = lnu.mpFromRoad->GetLaneSectionCount() -1;
  667. lnu.mnToSection = 0;
  668. lnu.mnFromLane = nid;
  669. lnu.mnToLane = nnext;
  670. }
  671. else
  672. {
  673. int nedgenext = getroadedge(nroadnext,1);
  674. b = mroadedge[nedgenext].mvertexend;
  675. lnu.mpFromRoad = GetRoadByID(nroadnext);
  676. lnu.mpToRoad = GetRoadByID(nroadcur);
  677. lnu.mnFromSection = 0;
  678. lnu.mnToSection = lnu.mpToRoad->GetLaneSectionCount() - 1;
  679. lnu.mnFromLane = nnext;
  680. lnu.mnToLane = nid;
  681. }
  682. }
  683. if(nid < 0)
  684. {
  685. int nedgecur = getroadedge(nroadcur,2,px->GetLaneSectionCount()-1);
  686. a = mroadedge[nedgecur].mvertexend;
  687. }
  688. else
  689. {
  690. int nedgecur = getroadedge(nroadcur,1,px->GetLaneSectionCount()-1);
  691. a = mroadedge[nedgecur].mvertexstart;
  692. }
  693. iv::vertexsame vx(a,b);
  694. xvertexsame.push_back(vx);
  695. mlanenet.push_back(lnu);
  696. }
  697. }
  698. }
  699. }
  700. }
  701. }
  702. givlog->debug("vertex same is %d ",xvertexsame.size());
  703. for(i=0;i<xvertexsame.size();i++)
  704. {
  705. givlog->debug("%d: %d %d",i,xvertexsame[i].ma,xvertexsame[i].mb);
  706. }
  707. //Merge Same Vertex
  708. std::vector<iv::vertexsamearray> xvertexsamearray;
  709. for(i=0;i<xvertexsame.size();i++)
  710. {
  711. int a,b;
  712. a = xvertexsame[i].ma;
  713. b = xvertexsame[i].mb;
  714. int j;
  715. int napos = -1;
  716. for(j=0;j<xvertexsamearray.size();j++)
  717. {
  718. if(xvertexsamearray[j].isinarray(a))
  719. {
  720. napos = j;
  721. break;
  722. }
  723. }
  724. if(napos >= 0)
  725. {
  726. if(!xvertexsamearray[napos].isinarray(b))
  727. {
  728. xvertexsamearray[napos].mvertexarray.push_back(b);
  729. }
  730. }
  731. else
  732. {
  733. int nbpos = -1;
  734. for(j=0;j<xvertexsamearray.size();j++)
  735. {
  736. if(xvertexsamearray[j].isinarray(b))
  737. {
  738. nbpos = j;
  739. break;
  740. }
  741. }
  742. if(nbpos >= 0)
  743. {
  744. xvertexsamearray[nbpos].mvertexarray.push_back(a);
  745. }
  746. else
  747. {
  748. iv::vertexsamearray va(a,b);
  749. xvertexsamearray.push_back(va);
  750. }
  751. }
  752. }
  753. for(i=0;i<xvertexsamearray.size();i++)
  754. {
  755. xvertexsamearray[i].sort();
  756. // xvertexsamearray[i].print();
  757. }
  758. for(i=0;i<mroadedge.size();i++)
  759. {
  760. int j;
  761. for(j=0;j<xvertexsamearray.size();j++)
  762. {
  763. if(xvertexsamearray[j].isinarray(mroadedge[i].mvertexstart))
  764. {
  765. mroadedge[i].mvertexstart = xvertexsamearray[j].mvertexarray[0];
  766. }
  767. if(xvertexsamearray[j].isinarray(mroadedge[i].mvertexend))
  768. {
  769. mroadedge[i].mvertexend = xvertexsamearray[j].mvertexarray[0];
  770. }
  771. }
  772. }
  773. for(i=0;i<mroadedge.size();i++)
  774. {
  775. givlog->debug("%d %d %d: %d %d",mroadedge[i].mroadid,mroadedge[i].mnleftright,mroadedge[i].mnsectionid, mroadedge[i].mvertexstart,mroadedge[i].mvertexend);
  776. }
  777. int * xset = new int[mroadedge.size()*2];
  778. std::shared_ptr<int> pxset;pxset.reset(xset);
  779. for(i=0;i<(mroadedge.size()*2);i++)
  780. {
  781. xset[i] = -1;
  782. }
  783. int nnow = -1;
  784. std::vector<int > xvalidvertex;
  785. for(i=0;i<(mroadedge.size()*2);i++)
  786. {
  787. bool bfind = false;
  788. int j;
  789. int ntempnow = 0;
  790. for(j=0;j<mroadedge.size();j++)
  791. {
  792. if((mroadedge[j].mvertexstart - nnow)>0)
  793. {
  794. if(bfind == false)
  795. {
  796. ntempnow = mroadedge[j].mvertexstart;
  797. bfind = true;
  798. }
  799. else
  800. {
  801. if(mroadedge[j].mvertexstart<ntempnow)ntempnow = mroadedge[j].mvertexstart;
  802. }
  803. }
  804. if((mroadedge[j].mvertexend - nnow)>0)
  805. {
  806. if(bfind == false)
  807. {
  808. ntempnow = mroadedge[j].mvertexend;
  809. bfind = true;
  810. }
  811. else
  812. {
  813. if(mroadedge[j].mvertexend<ntempnow)ntempnow = mroadedge[j].mvertexend;
  814. }
  815. }
  816. }
  817. if(bfind == false)break;
  818. else
  819. {
  820. nnow = ntempnow;
  821. xvalidvertex.push_back(nnow);
  822. }
  823. }
  824. for(i=0;i<mroadedge.size();i++)
  825. {
  826. int j;
  827. for(j=0;j<xvalidvertex.size();j++)
  828. {
  829. if(mroadedge[i].mvertexstart == xvalidvertex[j])
  830. {
  831. mroadedge[i].mvertexstart = j;
  832. break;
  833. }
  834. }
  835. if(j == xvalidvertex.size())qDebug("not found vertex");
  836. for(j=0;j<xvalidvertex.size();j++)
  837. {
  838. if(mroadedge[i].mvertexend == xvalidvertex[j])
  839. {
  840. mroadedge[i].mvertexend = j;
  841. break;
  842. }
  843. }
  844. if(j == xvalidvertex.size())qDebug("not found vertex");
  845. }
  846. for(i=0;i<mroadedge.size();i++)
  847. {
  848. givlog->debug("%d %d %d: %d %d",mroadedge[i].mroadid,mroadedge[i].mnleftright,mroadedge[i].mnsectionid, mroadedge[i].mvertexstart,mroadedge[i].mvertexend);
  849. }
  850. int nvertexnum = xvalidvertex.size(); //vertex num
  851. mvertexnum = nvertexnum;
  852. mvectorvertexedge.clear();
  853. int nmaxvertex = 0;
  854. for(i=0;i<nvertexnum;i++)
  855. {
  856. if(xvalidvertex[i]>nmaxvertex)nmaxvertex = xvalidvertex[i];
  857. }
  858. // for(i=0;i<mroadedge.size();i++)
  859. // {
  860. // if(mroadedge[i].mvertexstart>nmaxvertex)nmaxvertex = mroadedge[i].mvertexstart;
  861. // }
  862. for(i=0;i<=nmaxvertex;i++)
  863. {
  864. vertexedge x;
  865. mvectorvertexedge.push_back(x);
  866. }
  867. for(i=0;i<mroadedge.size();i++)
  868. {
  869. mvectorvertexedge[mroadedge[i].mvertexstart].mvectorroadedge.push_back(&mroadedge[i]);
  870. }
  871. std::cout<<" max is "<<nmaxvertex<<" num is "<<nvertexnum<<std::endl;
  872. nmaxvertex = nmaxvertex + 0;
  873. }
  874. int xodrdijkstra::getroadedge(int nroad,int leftright,int nsection )
  875. {
  876. int nrtn = -1;
  877. int i;
  878. // for(i=0;i<mroadedge.size();i++)
  879. // {
  880. // if((mroadedge[i].mroadid == nroad) &&(mroadedge[i].mnleftright == leftright) )
  881. // {
  882. // qDebug("equal, road %d edge %d ",nroad,i);
  883. // }
  884. // }
  885. for(i=0;i<mroadedge.size();i++)
  886. {
  887. if((mroadedge[i].mroadid == nroad) &&(mroadedge[i].mnleftright == leftright) &&(nsection == mroadedge[i].mnsectionid))
  888. {
  889. nrtn = i;
  890. return nrtn;
  891. }
  892. }
  893. return nrtn;
  894. }
  895. inline double xodrdijkstra::getedgedis(int vs, int vd)
  896. {
  897. int i;
  898. std::vector<roadedge > * proadedge = &mroadedge;
  899. double dis = std::numeric_limits<double>::max();
  900. for(i=0;i<mvectorvertexedge[vs].mvectorroadedge.size();i++)
  901. {
  902. if((vs == mvectorvertexedge[vs].mvectorroadedge[i]->mvertexstart)&&(vd == mvectorvertexedge[vs].mvectorroadedge[i]->mvertexend))
  903. {
  904. dis = mroadedge[i].mlen;
  905. return dis;
  906. }
  907. }
  908. return dis;
  909. for(i=0;i<mroadedge.size();i++)
  910. {
  911. if((vs == mroadedge[i].mvertexstart)&&(vd == mroadedge[i].mvertexend))
  912. {
  913. dis = mroadedge[i].mlen;
  914. return dis;
  915. }
  916. }
  917. return dis;
  918. }
  919. int xodrdijkstra::GetRoadSectionIndexByS(Road *pRoad, const double s)
  920. {
  921. int nrtn = -1;
  922. if(s<0)return nrtn;
  923. if(s>pRoad->GetRoadLength())return nrtn;
  924. int i;
  925. int nseccount = pRoad->GetLaneSectionCount();
  926. if(nseccount== 1)return 0;
  927. for(i=0;i<(nseccount-1);i++)
  928. {
  929. if(s<(pRoad->GetLaneSection(i+1)->GetS()))
  930. {
  931. break;
  932. }
  933. }
  934. return i;
  935. }
  936. double xodrdijkstra::getpathlength(std::vector<int> xvectorpath)
  937. {
  938. int i;
  939. int nsize = xvectorpath.size();
  940. double flen = 0;
  941. for(i=0;i<nsize;i++)
  942. {
  943. flen = flen + mroadedge[xvectorpath[i]].mlen;
  944. }
  945. return flen;
  946. }
  947. std::vector<int> xodrdijkstra::getpath(int srcroadid, int nsrclr, int dstroadid, int ndstlr,const double s_src ,const double s_obj )
  948. {
  949. std::vector<int> rtnpath;
  950. int nvertexnum = mvertexnum;
  951. int i;
  952. int nsecsrc =0;
  953. int nsecdst =0;
  954. nsecsrc = GetRoadSectionIndexByS(GetRoadByID(srcroadid),s_src);
  955. nsecdst = GetRoadSectionIndexByS(GetRoadByID(dstroadid),s_obj);
  956. if((nsecsrc<0)||(nsecdst<0))
  957. {
  958. qDebug("getpath section error.");
  959. return rtnpath;
  960. }
  961. int srcedge = getroadedge(srcroadid,nsrclr,nsecsrc);
  962. int dstedge = getroadedge(dstroadid,ndstlr,nsecdst);
  963. // roadedge * px1 = &mroadedge[srcedge];
  964. // roadedge * px2 = &mroadedge[dstedge];
  965. if((srcedge == -1)||(dstedge == -1))
  966. {
  967. qDebug("srcedge = %d dstedge = %d ",srcedge,dstedge);
  968. return rtnpath;
  969. }
  970. int * flag = new int[nvertexnum];
  971. int * prev = new int[nvertexnum];
  972. double * dist = new double[nvertexnum];
  973. std::shared_ptr<int> pflag; pflag.reset(flag);
  974. std::shared_ptr<int> pprev; pprev.reset(prev);
  975. std::shared_ptr<double> pdist; pdist.reset(dist);
  976. double min;
  977. int vs = mroadedge[srcedge].mvertexend;
  978. // int dstvertex = mroadedge[dstedge].mvertexend;
  979. int dstvertex = mroadedge[dstedge].mvertexstart;
  980. if(srcedge == dstedge)dstvertex = mroadedge[dstedge].mvertexstart;
  981. givlog->debug("src edge is %d vertex is %d",srcedge,vs);
  982. givlog->debug("dst vertex is %d ",dstvertex);
  983. // 初始化
  984. for (i = 0; i < nvertexnum; i++)
  985. {
  986. flag[i] = 0; // 顶点i的最短路径还没获取到。
  987. prev[i] = vs; // 顶点i的前驱顶点为0。
  988. dist[i] = getedgedis(vs,i);// 顶点i的最短路径为"顶点vs"到"顶点i"的权。
  989. }
  990. // 对"顶点vs"自身进行初始化
  991. flag[vs] = 1;
  992. dist[vs] = 0;
  993. // 遍历G.vexnum-1次;每次找出一个顶点的最短路径。
  994. for (i = 1; i < nvertexnum; i++)
  995. {
  996. // qDebug("i = %d",i);
  997. int k,j;
  998. // 寻找当前最小的路径;
  999. // 即,在未获取最短路径的顶点中,找到离vs最近的顶点(k)。
  1000. k = -1;
  1001. min = std::numeric_limits<double>::max();;
  1002. for (j = 0; j < nvertexnum; j++)
  1003. {
  1004. if (flag[j]==0 && dist[j]<min)
  1005. {
  1006. min = dist[j];
  1007. k = j;
  1008. }
  1009. }
  1010. if(min < 0)
  1011. {
  1012. qDebug("hi");
  1013. }
  1014. if(k == -1)
  1015. {
  1016. givlog->debug("i = %d not found k",i);
  1017. break;
  1018. }
  1019. // 标记"顶点k"为已经获取到最短路径
  1020. flag[k] = 1;
  1021. // 修正当前最短路径和前驱顶点
  1022. // 即,当已经"顶点k的最短路径"之后,更新"未获取最短路径的顶点的最短路径和前驱顶点"。
  1023. for (j = 0; j < nvertexnum; j++)
  1024. {
  1025. double tmp;
  1026. tmp = getedgedis(k,j);
  1027. if(tmp == std::numeric_limits<double>::max())
  1028. {
  1029. }
  1030. else
  1031. {
  1032. tmp = min + tmp;
  1033. }
  1034. // tmp = (G.matrix[k][j]==INF ? INF : (min + G.matrix[k][j])); // 防止溢出
  1035. if (flag[j] == 0 && (tmp < dist[j]) )
  1036. {
  1037. dist[j] = tmp;
  1038. prev[j] = k;
  1039. }
  1040. }
  1041. }
  1042. // for (i = 0; i < nvertexnum; i++)
  1043. // qDebug(" shortest(%d, %d)=%f\n", vs, i, dist[i]);
  1044. for (i = 0; i < nvertexnum; i++)
  1045. givlog->debug(" %d =%d\n", i, prev[i]);
  1046. if(flag[dstvertex] == 1)
  1047. {
  1048. int nstart;
  1049. int nend;
  1050. nend = dstvertex;
  1051. nstart = prev[nend];
  1052. while(nend != vs)
  1053. {
  1054. nstart = prev[nend];
  1055. int nedge = getroadedgefromvertex(nstart,nend);
  1056. if(nedge<0)
  1057. {
  1058. givlog->error("prev path error.");
  1059. break;
  1060. }
  1061. rtnpath.push_back(nedge);
  1062. nend = nstart;
  1063. }
  1064. }
  1065. std::vector<int> rtnpath2;
  1066. rtnpath2.push_back(srcedge);
  1067. for(i=0;i<rtnpath.size();i++)
  1068. {
  1069. givlog->debug("%d %d %d %d",i,mroadedge[rtnpath[rtnpath.size()-1-i]].mroadid,mroadedge[rtnpath[rtnpath.size()-1-i]].mnleftright,
  1070. mroadedge[rtnpath[rtnpath.size()-1-i]].mnsectionid);
  1071. // qDebug("%d %d %d %d",i,mroadedge[rtnpath[rtnpath.size()-1-i]].mroadid,mroadedge[rtnpath[rtnpath.size()-1-i]].mnleftright,
  1072. // mroadedge[rtnpath[rtnpath.size()-1-i]].mnsectionid);
  1073. rtnpath2.push_back(rtnpath[rtnpath.size()-1-i]);
  1074. }
  1075. /* if(srcedge == dstedge)*/rtnpath2.push_back(dstedge);
  1076. givlog->debug("rt = %d ",rtnpath.size());
  1077. //gfault->SetFaultState(0,0,"ok");
  1078. return rtnpath2;
  1079. }
  1080. int xodrdijkstra::getroadedgefromvertex(int nstart, int nend)
  1081. {
  1082. int i;
  1083. int nrtn = -1;
  1084. double flen = -1;
  1085. // int nlast = -1;
  1086. for(i=0;i<mroadedge.size();i++)
  1087. {
  1088. if((nstart == mroadedge[i].mvertexstart)&&(nend == mroadedge[i].mvertexend))
  1089. {
  1090. if(flen >0)
  1091. {
  1092. if(mroadedge[i].mlen < flen)
  1093. {
  1094. flen = mroadedge[i].mlen;
  1095. nrtn = i;
  1096. }
  1097. }
  1098. else
  1099. {
  1100. flen = mroadedge[i].mlen;
  1101. nrtn = i;
  1102. }
  1103. // break;
  1104. }
  1105. }
  1106. return nrtn;
  1107. }
  1108. Junction * xodrdijkstra::GetJunctionByID(string junctionname)
  1109. {
  1110. int i;
  1111. for(i=0;i<mpxodr->GetJunctionCount();i++)
  1112. {
  1113. if(strncmp(mpxodr->GetJunction(i)->GetId().data(),junctionname.data(),255) == 0)
  1114. {
  1115. return mpxodr->GetJunction(i);
  1116. }
  1117. }
  1118. std::cout<<"xodrdijkstra::GetJunctionByID can't find"<<std::endl;
  1119. return 0;
  1120. }
  1121. /**
  1122. * @brief xodrdijkstra::CalcLaneChange
  1123. * Decide Lane Chage,if need.
  1124. * @param p1 pre path
  1125. * @param p2 next path
  1126. **/
  1127. void xodrdijkstra::CalcLaneChange(pathsection *p1, pathsection *p2)
  1128. {
  1129. // int njunctionpos = 1; //if 1 p1 is junction if 2 p2 is junction, in junction from it's start have several road.
  1130. int nlr1,nlr2;
  1131. nlr1 = mroadedge[p1->mnroadedgeid].mnleftright;
  1132. nlr2 = mroadedge[p2->mnroadedgeid].mnleftright;
  1133. std::vector<lanenetunit> xln = GetLaneNet(p1->mpRoad,p1->msectionid,p2->mpRoad,p2->msectionid);
  1134. int i;
  1135. int nsel1 = p1->mainsel;
  1136. int nsel2 = p2->mainsel;
  1137. if(IsInLaneNet(xln,p1->mpRoad,p1->msectionid,p1->mainsel,p2->mpRoad,p2->msectionid,p2->mainsel))
  1138. {
  1139. }
  1140. else
  1141. {
  1142. bool bhave = false;
  1143. for(i=0;i<xln.size();i++)
  1144. {
  1145. if(xln.at(i).mnToLane == nsel2) //Have To Next Lane
  1146. {
  1147. nsel1 = xln.at(i).mnFromLane;
  1148. bhave = true;
  1149. break;
  1150. }
  1151. }
  1152. if(bhave == false)
  1153. {
  1154. bool bhave2 = false;
  1155. for(i=0;i<xln.size();i++)
  1156. {
  1157. if(xln.at(i).mnFromLane == nsel1) //Have from Lane
  1158. {
  1159. nsel2 = xln.at(i).mnToLane;
  1160. bhave2= true;
  1161. break;
  1162. }
  1163. }
  1164. if(bhave2 == false)
  1165. {
  1166. int nseldiff = 100;
  1167. for(i=0;i<xln.size();i++)
  1168. {
  1169. if(abs(xln.at(i).mnFromLane - p1->mainsel)<nseldiff) //Find Change Lane Less Lane
  1170. {
  1171. nsel1 = xln.at(i).mnFromLane;
  1172. nsel2 = xln.at(i).mnToLane;
  1173. nseldiff = abs(xln.at(i).mnFromLane - p1->mainsel);
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. p1->mnEndLaneSel = nsel1;
  1180. p2->mnStartLaneSel = nsel2;
  1181. }
  1182. /**
  1183. * @brief getgpspoint
  1184. * get gps trace
  1185. * @param srcroadidr source road id.
  1186. * @param nsrclr source road left or right
  1187. * @param dstroadid destination road id
  1188. * @param ndstlr destination road left or right
  1189. * @param xvectorpath grapth path vector,this path is calculated by getpath
  1190. **/
  1191. std::vector<pathsection> xodrdijkstra::getgpspoint(int srcroadid, int nsrclr, int dstroadid, int ndstlr, std::vector<int> xvectorpath,int nSel)
  1192. {
  1193. // int nSel = 1;
  1194. std::vector<int> nvectorlane1; //Road Start Select Lane
  1195. std::vector<int> nvectorlane2; //Road Center Select Lane;
  1196. std::vector<int> nvectorlane3; //Road End Select Lane
  1197. std::vector<pathsection> xpathsection;
  1198. int nlanecount = mroadedge[getroadedge(srcroadid,nsrclr)].mpx->GetLaneSectionCount();
  1199. std::cout<<" section count is "<<nlanecount<<std::endl;;
  1200. //First Get Every Path's lane section for calculate point.
  1201. int i;
  1202. int nsize = xvectorpath.size();
  1203. for(i=0;i<nsize;i++)
  1204. {
  1205. Road * pRoad = mroadedge[xvectorpath[i]].mpx;
  1206. qDebug("road %d is %s",i,pRoad->GetRoadId().data());
  1207. }
  1208. Road * pLastRoad = mroadedge[xvectorpath[nsize-1]].mpx;
  1209. for(i=0;i<nsize;i++)
  1210. {
  1211. pathsection xps;
  1212. Road * pRoad = mroadedge[xvectorpath[i]].mpx;
  1213. int nsuggest = nSel;
  1214. if(mroadedge[xvectorpath[i]].mnleftright == 2)nsuggest = nsuggest *(-1);
  1215. int nlane = xodrfunc::GetDrivingLane(pRoad,mroadedge[xvectorpath[i]].mnsectionid,nsuggest);
  1216. xps.mainsel = nlane;
  1217. xps.mnStartLaneSel = nlane;
  1218. xps.mnEndLaneSel = nlane;
  1219. xps.mainselindex = 0;//mainselindex;
  1220. xps.mroadid = mroadedge[xvectorpath[i]].mroadid;
  1221. xps.msectionid = mroadedge[xvectorpath[i]].mnsectionid;
  1222. xps.mnroadedgeid = xvectorpath[i];
  1223. xps.mpRoad = pRoad;
  1224. xpathsection.push_back(xps);
  1225. }
  1226. for(i=(nsize-1);i>0;i--)
  1227. {
  1228. Road * pRoad = mroadedge[xvectorpath[i]].mpx;
  1229. LaneSection * pLS = pRoad->GetLaneSection(mroadedge[xvectorpath[i]].mnsectionid);
  1230. Lane * pLane = xodrfunc::GetLaneByID(pLS,xpathsection[i].mainsel);
  1231. Road * pRoad2 = mroadedge[xvectorpath[i-1]].mpx;
  1232. LaneSection * pLS2 = pRoad2->GetLaneSection(mroadedge[xvectorpath[i-1]].mnsectionid);
  1233. Lane * pLane2 = xodrfunc::GetLaneByID(pLS2,xpathsection[i-1].mainsel);
  1234. int nlr = mroadedge[xvectorpath[i]].mnleftright;
  1235. bool bNeedChangeLane = true;
  1236. if(nlr == 2)
  1237. {
  1238. if(pLane2->IsSuccessorSet())
  1239. {
  1240. if(pLane2->GetSuccessor() != xpathsection[i].mainsel)
  1241. {
  1242. bool bfindgood = false;
  1243. int ngoodlane = 0;
  1244. int j;
  1245. for(j=0;j<pLS2->GetLaneCount();j++)
  1246. {
  1247. if(pLS2->GetLane(j)->IsSuccessorSet())
  1248. {
  1249. if(pLS2->GetLane(j)->GetSuccessor() == xpathsection[i].mainsel)
  1250. {
  1251. ngoodlane = pLS2->GetLane(j)->GetId();
  1252. bfindgood = true;
  1253. break;
  1254. }
  1255. }
  1256. }
  1257. if(bfindgood)
  1258. {
  1259. xpathsection[i-1].mainsel = ngoodlane;
  1260. xpathsection[i-1].mnStartLaneSel = ngoodlane;
  1261. xpathsection[i-1].mnEndLaneSel = ngoodlane;
  1262. bNeedChangeLane = false;
  1263. }
  1264. }
  1265. else
  1266. {
  1267. bNeedChangeLane = false;
  1268. }
  1269. }
  1270. else
  1271. {
  1272. if(pLane->IsPredecessorSet())
  1273. {
  1274. if(pLane->GetPredecessor() != xpathsection[i-1].mainsel)
  1275. {
  1276. xpathsection[i-1].mainsel = pLane->GetPredecessor();
  1277. xpathsection[i-1].mnStartLaneSel = pLane->GetPredecessor();
  1278. xpathsection[i-1].mnEndLaneSel = pLane->GetPredecessor();
  1279. bNeedChangeLane = false;
  1280. }
  1281. }
  1282. }
  1283. }
  1284. else
  1285. {
  1286. if(pLane2->IsPredecessorSet())
  1287. {
  1288. if(pLane2->GetPredecessor() != xpathsection[i].mainsel)
  1289. {
  1290. bool bfindgood = false;
  1291. int ngoodlane = 0;
  1292. int j;
  1293. for(j=0;j<pLS2->GetLaneCount();j++)
  1294. {
  1295. if(pLS2->GetLane(j)->IsPredecessorSet())
  1296. {
  1297. if(pLS2->GetLane(j)->GetPredecessor() == xpathsection[i].mainsel)
  1298. {
  1299. ngoodlane = pLS2->GetLane(j)->GetId();
  1300. bfindgood = true;
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. if(bfindgood)
  1306. {
  1307. xpathsection[i-1].mainsel = ngoodlane;
  1308. xpathsection[i-1].mnStartLaneSel = ngoodlane;
  1309. xpathsection[i-1].mnEndLaneSel = ngoodlane;
  1310. bNeedChangeLane = false;
  1311. }
  1312. }
  1313. else
  1314. {
  1315. bNeedChangeLane = false;
  1316. }
  1317. }
  1318. else
  1319. {
  1320. if(pLane->IsSuccessorSet())
  1321. {
  1322. if(pLane->GetSuccessor() != xpathsection[i-1].mainsel)
  1323. {
  1324. xpathsection[i-1].mainsel = pLane->GetSuccessor();
  1325. xpathsection[i-1].mnStartLaneSel = pLane->GetSuccessor();
  1326. xpathsection[i-1].mnEndLaneSel = pLane->GetSuccessor();
  1327. bNeedChangeLane = false;
  1328. }
  1329. }
  1330. }
  1331. }
  1332. if(bNeedChangeLane == true)
  1333. {
  1334. std::cout<<" Road "<<pRoad->GetRoadId()<<" need change lane "<<std::endl;
  1335. if(pRoad == pRoad2)
  1336. {
  1337. std::cout<<" Lane Change in road inter. "<<std::endl;
  1338. }
  1339. else
  1340. {
  1341. if(nlr == 2)
  1342. {
  1343. if(pLane2->IsSuccessorSet())
  1344. {
  1345. xpathsection[i].secondsel = pLane2->GetSuccessor();
  1346. }
  1347. else
  1348. {
  1349. int k;
  1350. int xlanecount = pLS->GetLaneCount();
  1351. for(k=0;k<xlanecount;k++)
  1352. {
  1353. if(pLS->GetLane(k)->IsPredecessorSet())
  1354. {
  1355. if(pLS->GetLane(k)->GetPredecessor() == pLane2->GetSuccessor())
  1356. {
  1357. xpathsection[i].secondsel = k;
  1358. break;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. }
  1364. else
  1365. {
  1366. if(pLane2->IsPredecessorSet())
  1367. {
  1368. xpathsection[i].secondsel = pLane2->GetPredecessor();
  1369. }
  1370. else
  1371. {
  1372. int k;
  1373. int xlanecount = pLS->GetLaneCount();
  1374. for(k=0;k<xlanecount;k++)
  1375. {
  1376. if(pLS->GetLane(k)->IsSuccessorSet())
  1377. {
  1378. if(pLS->GetLane(k)->GetSuccessor() == pLane2->GetPredecessor())
  1379. {
  1380. xpathsection[i].secondsel = k;
  1381. break;
  1382. }
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. int k;
  1389. for(k=(nsize-1);k>i;k--)
  1390. {
  1391. if(xpathsection[k].mpRoad == xpathsection[i].mpRoad)
  1392. {
  1393. xpathsection[k].secondsel = xpathsection[i].secondsel;
  1394. }
  1395. }
  1396. }
  1397. else
  1398. {
  1399. xpathsection[i].secondsel = xpathsection[i].mainsel;
  1400. xpathsection[i-1].secondsel = xpathsection[i-1].mainsel;
  1401. }
  1402. }
  1403. for(i=0;i<xpathsection.size();i++)
  1404. {
  1405. std::cout<<"path "<<i<<" road:"<<xpathsection[i].mroadid<<" section "<<xpathsection[i].msectionid
  1406. <<" lane "<<xpathsection[i].mainsel<<" sec lane is "<<xpathsection[i].secondsel<<std::endl;
  1407. }
  1408. return xpathsection;
  1409. // for(i=(nsize-1);i>=0;i--)
  1410. // {
  1411. // Road * pRoad = mroadedge[xvectorpathi]
  1412. // }
  1413. for(i=0;i<nsize;i++)
  1414. {
  1415. Road * pRoad = mroadedge[xvectorpath[i]].mpx;
  1416. int nlr = mroadedge[xvectorpath[i]].mnleftright;
  1417. // int nsectioncount = pRoad->GetLaneSectionCount();
  1418. int j;
  1419. j = mroadedge[xvectorpath[i]].mnsectionid;
  1420. LaneSection * pLaneSection = pRoad->GetLaneSection(j);
  1421. int nlane = nSel;
  1422. if(nlane <= 0)nlane = 10;
  1423. int mainselindex = 0;
  1424. if(nlr == 2)nlane = nlane*(-1);
  1425. int nlanecount = pLaneSection->GetLaneCount();
  1426. int k;
  1427. bool bFind = false;
  1428. while(bFind == false)
  1429. {
  1430. for(k=0;k<nlanecount;k++)
  1431. {
  1432. Lane * pLane = pLaneSection->GetLane(k);
  1433. if((nlane == pLane->GetId())&&(strncmp(pLane->GetType().data(),"driving",255) == 0))
  1434. {
  1435. bFind = true;
  1436. mainselindex = k;
  1437. break;
  1438. }
  1439. }
  1440. if(bFind)continue;
  1441. if(nlr == 1)nlane--;
  1442. else nlane++;
  1443. if(nlane == 0) //Find A can use lane;
  1444. {
  1445. // std::cout<<" Fail. can't find lane"<<std::endl;
  1446. for(k=0;k<nlanecount;k++)
  1447. {
  1448. Lane * pLane = pLaneSection->GetLane(k);
  1449. if(strncmp(pLane->GetType().data(),"driving",255) == 0)
  1450. {
  1451. if(nlr == 1)
  1452. {
  1453. if(pLane->GetId()> 0)
  1454. {
  1455. nlane = pLane->GetId();
  1456. mainselindex = k;
  1457. bFind = true;
  1458. break;
  1459. }
  1460. }
  1461. else
  1462. {
  1463. if(pLane->GetId()<0)
  1464. {
  1465. nlane = pLane->GetId();
  1466. mainselindex = k;
  1467. bFind = true;
  1468. break;
  1469. }
  1470. }
  1471. }
  1472. }
  1473. if(nlane == 0)
  1474. {
  1475. std::cout<<" Fail. can't find lane"<<std::endl;
  1476. }
  1477. break;
  1478. }
  1479. }
  1480. // std::cout<<" select lane is "<<nlane<<std::endl;
  1481. pathsection xps;
  1482. xps.mainsel = nlane;
  1483. xps.mnStartLaneSel = nlane;
  1484. xps.mnEndLaneSel = nlane;
  1485. xps.mainselindex = mainselindex;
  1486. // if((i>0)&&(pRoad->GetRoadLength()<300)) //Use Connection,not use default.
  1487. // {
  1488. // int nlast = xpathsection[i-1].mainselindex;
  1489. // for(k=0;k<nlanecount;k++)
  1490. // {
  1491. // Lane * pLane = pLaneSection->GetLane(k);
  1492. // if(pLane->IsPredecessorSet())
  1493. // {
  1494. // if(nlast == pLane->GetPredecessor())
  1495. // {
  1496. // xps.mainsel = pLane->GetId();
  1497. // break;
  1498. // }
  1499. // }
  1500. // }
  1501. // }
  1502. xps.mroadid = mroadedge[xvectorpath[i]].mroadid;
  1503. xps.msectionid = j;
  1504. xps.mnroadedgeid = xvectorpath[i];
  1505. xps.mpRoad = pRoad;
  1506. xpathsection.push_back(xps);
  1507. }
  1508. int nps = xpathsection.size();
  1509. for(i=0;i<nps;i++)
  1510. {
  1511. std::cout<<xpathsection[i].mroadid<<" "<<xpathsection[i].msectionid<<" "<<xpathsection[i].mainsel<<" "
  1512. <<xpathsection[i].mainselindex<<std::endl;
  1513. }
  1514. for(i=0;i<(nps-1);i++)
  1515. {
  1516. CalcLaneChange(&xpathsection[i],&xpathsection[i+1]);
  1517. }
  1518. for(i=0;i<nps;i++)
  1519. {
  1520. if((xpathsection[i].mnStartLaneSel == xpathsection[i].mnEndLaneSel) &&(xpathsection[i].mainsel != xpathsection[i].mnStartLaneSel))
  1521. {
  1522. xpathsection[i].mainsel = xpathsection[i].mnStartLaneSel;
  1523. }
  1524. if(xpathsection[i].mnStartLaneSel == xpathsection[i].mainsel)
  1525. {
  1526. xpathsection[i].mbStartToMainChange = false;
  1527. }
  1528. else
  1529. {
  1530. xpathsection[i].mbStartToMainChange = true;
  1531. }
  1532. if(xpathsection[i].mnEndLaneSel == xpathsection[i].mainsel)
  1533. {
  1534. xpathsection[i].mbMainToEndChange = false;
  1535. }
  1536. else
  1537. {
  1538. xpathsection[i].mbMainToEndChange = true;
  1539. }
  1540. }
  1541. // CalcLaneChange(&xpathsection[10], &xpathsection[11]);
  1542. // for(i=1;i<nsize;i++)
  1543. // {
  1544. // pathsection * p1 = &xpathsection[i-1];
  1545. // pathsection * p2 = &xpathsection[i];
  1546. // if(xpathsection[i-1].mpRoad != xpathsection[i].mpRoad)
  1547. // {
  1548. // CalcLaneChange(&xpathsection[i-1], &xpathsection[i]);
  1549. // }
  1550. // else
  1551. // {
  1552. // if(p1->mainsel != p2->mainsel)
  1553. // {
  1554. // p1->mbMainToEndChange = true;
  1555. // p1->mnEndLaneSel = p2->mainsel;
  1556. // if(abs(p1->mainsel) > abs(p2->mainsel))
  1557. // {
  1558. // p1->mnMainToEndLeftRight = 1;
  1559. // }
  1560. // else
  1561. // {
  1562. // p1->mnMainToEndLeftRight = 2;
  1563. // }
  1564. // }
  1565. // }
  1566. // }
  1567. for(i=0;i<nsize;i++)
  1568. {
  1569. std::cout<<xpathsection[i].mroadid<<" "<<xpathsection[i].mbStartToMainChange<<" "
  1570. <<xpathsection[i].mbMainToEndChange<<std::endl;
  1571. if(xpathsection[i].mbStartToMainChange == false)
  1572. {
  1573. xpathsection[i].mnStartLaneSel = xpathsection[i].mainsel;
  1574. }
  1575. if(xpathsection[i].mbMainToEndChange == false)
  1576. {
  1577. xpathsection[i].mnEndLaneSel = xpathsection[i].mainsel;
  1578. }
  1579. }
  1580. return xpathsection;
  1581. }
  1582. std::vector<lanenetunit> xodrdijkstra::GetLaneNet(Road *p1, int nsec1, Road *p2, int nsec2)
  1583. {
  1584. int i;
  1585. std::vector<lanenetunit> xln;
  1586. int nsize = mlanenet.size();
  1587. for(i=0;i<nsize;i++)
  1588. {
  1589. if((p1== mlanenet.at(i).mpFromRoad) &&(p2 == mlanenet.at(i).mpToRoad))
  1590. {
  1591. if((nsec1 == mlanenet.at(i).mnFromSection)&&(nsec2 == mlanenet.at(i).mnToSection))
  1592. {
  1593. xln.push_back(mlanenet.at(i));
  1594. }
  1595. }
  1596. }
  1597. return xln;
  1598. }
  1599. bool xodrdijkstra::IsInLaneNet(std::vector<lanenetunit> xln, Road *p1, int nsec1, int nlane1, Road *p2, int nsec2, int nlane2)
  1600. {
  1601. int i;
  1602. int nsize = xln.size();
  1603. for(i=0;i<nsize;i++)
  1604. {
  1605. if((p1== xln.at(i).mpFromRoad) &&(p2 == xln.at(i).mpToRoad))
  1606. {
  1607. if((nsec1 == xln.at(i).mnFromSection)&&(nsec2 == xln.at(i).mnToSection))
  1608. {
  1609. if((nlane1 == xln.at(i).mnFromLane)&&(nlane2 == xln.at(i).mnToLane))
  1610. {
  1611. return true;
  1612. }
  1613. }
  1614. }
  1615. }
  1616. return false;
  1617. }