xodrdijkstra.cpp 62 KB

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