xodrfunc.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. #include "xodrfunc.h"
  2. #include "limits"
  3. #include <iostream>
  4. #include <Eigen/Dense>
  5. #include <Eigen/Cholesky>
  6. #include <Eigen/LU>
  7. #include <Eigen/QR>
  8. #include <Eigen/SVD>
  9. #include <QDebug>
  10. #include <QPointF>
  11. xodrfunc::xodrfunc()
  12. {
  13. }
  14. inline double xodrfunc::calcpointdis(QPointF p1,QPointF p2)
  15. {
  16. return sqrt(pow(p1.x()-p2.x(),2)+pow(p1.y()-p2.y(),2));
  17. }
  18. bool xodrfunc::pointinarc(GeometryArc * parc,QPointF poingarc,QPointF point1)
  19. {
  20. double hdg = CalcHdg(poingarc,point1);
  21. if(parc->GetCurvature() >0)hdg = hdg + M_PI/2.0;
  22. else hdg = hdg - M_PI/2.0;
  23. if(hdg >= 2.0*M_PI)hdg = hdg - 2.0*M_PI;
  24. if(hdg < 0)hdg = hdg + 2.0*M_PI;
  25. double hdgrange = parc->GetLength()/(1.0/parc->GetCurvature());
  26. double hdgdiff = hdg - parc->GetHdg();
  27. if(hdgrange >= 0 )
  28. {
  29. if(hdgdiff < 0)hdgdiff = hdgdiff + M_PI*2.0;
  30. }
  31. else
  32. {
  33. if(hdgdiff > 0)hdgdiff = hdgdiff - M_PI*2.0;
  34. }
  35. if(fabs(hdgdiff ) < fabs(hdgrange))return true;
  36. return false;
  37. }
  38. /**
  39. * @brief CalcHdg
  40. * 计算点0到点1的航向
  41. * @param p0 Point 0
  42. * @param p1 Point 1
  43. **/
  44. double xodrfunc::CalcHdg(QPointF p0, QPointF p1)
  45. {
  46. double x0,y0,x1,y1;
  47. x0 = p0.x();
  48. y0 = p0.y();
  49. x1 = p1.x();
  50. y1 = p1.y();
  51. if(x0 == x1)
  52. {
  53. if(y0 < y1)
  54. {
  55. return M_PI/2.0;
  56. }
  57. else
  58. return M_PI*3.0/2.0;
  59. }
  60. double ratio = (y1-y0)/(x1-x0);
  61. double hdg = atan(ratio);
  62. if(ratio > 0)
  63. {
  64. if(y1 > y0)
  65. {
  66. }
  67. else
  68. {
  69. hdg = hdg + M_PI;
  70. }
  71. }
  72. else
  73. {
  74. if(y1 > y0)
  75. {
  76. hdg = hdg + M_PI;
  77. }
  78. else
  79. {
  80. hdg = hdg + 2.0*M_PI;
  81. }
  82. }
  83. return hdg;
  84. }
  85. /**
  86. * @brief GetParamPoly3Dis 获得点到贝塞尔曲线的距离。
  87. * @param parc
  88. * @param xnow
  89. * @param ynow
  90. * @param nearx
  91. * @param neary
  92. * @param nearhead
  93. * @return
  94. */
  95. double xodrfunc::GetParamPoly3Dis(GeometryParamPoly3 * parc,double xnow,double ynow,double & nearx,
  96. double & neary,double & nearhead,double & frels)
  97. {
  98. double s = 0.1;
  99. double fdismin = 100000.0;
  100. frels = 0;
  101. double xold,yold;
  102. xold = parc->GetX();
  103. yold = parc->GetY();
  104. double fdis = calcpointdis(QPointF(parc->GetX(),parc->GetY()),QPointF(xnow,ynow));
  105. if(fdis<fdismin)
  106. {
  107. fdismin = fdis;
  108. nearhead = parc->GetHdg();
  109. nearx = parc->GetX();
  110. neary = parc->GetY();
  111. }
  112. while(s < parc->GetLength())
  113. {
  114. double x, y,xtem,ytem;
  115. xtem = parc->GetuA() + parc->GetuB() * s + parc->GetuC() * s*s + parc->GetuD() * s*s*s;
  116. ytem = parc->GetvA() + parc->GetvB() * s + parc->GetvC() * s*s + parc->GetvD() * s*s*s;
  117. x = xtem*cos(parc->GetHdg()) - ytem * sin(parc->GetHdg()) + parc->GetX();
  118. y = xtem*sin(parc->GetHdg()) + ytem * cos(parc->GetHdg()) + parc->GetY();
  119. double hdg = CalcHdg(QPointF(xold,yold),QPointF(x,y));
  120. double fdis = calcpointdis(QPointF(x,y),QPointF(xnow,ynow));
  121. if(fdis<fdismin)
  122. {
  123. fdismin = fdis;
  124. nearhead = hdg;
  125. nearx = x;
  126. neary = y;
  127. frels = s;
  128. }
  129. xold = x;
  130. yold = y;
  131. s = s+ 0.1;
  132. }
  133. return fdismin;
  134. }
  135. /**
  136. * @brief GetArcDis
  137. * 计算点到圆弧的最短距离,首先用点和圆弧中心点的直线与圆的交点,计算点到交点的距离,如果交点在圆弧上,则最短距离是交点之一,否则计算点到圆弧两个端点的距离。
  138. * @param parc pointer to a arc geomery
  139. * @param x current x
  140. * @param y current y
  141. * @param nearx near x
  142. * @param neary near y
  143. * @param nearhead nearhead
  144. **/
  145. double xodrfunc::GetArcDis(GeometryArc * parc,double x,double y,double & nearx,
  146. double & neary,double & nearhead,double & frels)
  147. {
  148. if((parc->GetS()>370)&&(parc->GetS()<370.1))
  149. {
  150. int a = 1;
  151. }
  152. if(parc->GetCurvature() == 0.0)return 1000.0;
  153. double R = fabs(1.0/parc->GetCurvature());
  154. frels = 0.0;
  155. //calculate arc center
  156. double x_center,y_center;
  157. if(parc->GetCurvature() > 0)
  158. {
  159. x_center = parc->GetX() + R * cos(parc->GetHdg() + M_PI/2.0);
  160. y_center = parc->GetY() + R * sin(parc->GetHdg()+ M_PI/2.0);
  161. }
  162. else
  163. {
  164. x_center = parc->GetX() + R * cos(parc->GetHdg() - M_PI/2.0);
  165. y_center = parc->GetY() + R * sin(parc->GetHdg() - M_PI/2.0);
  166. }
  167. double hdgltoa = CalcHdg(QPointF(x,y),QPointF(x_center,y_center));
  168. QPointF arcpoint;
  169. arcpoint.setX(x_center);arcpoint.setY(y_center);
  170. QPointF pointnow;
  171. pointnow.setX(x);pointnow.setY(y);
  172. QPointF point1,point2;
  173. point1.setX(x_center + (R * cos(hdgltoa)));
  174. point1.setY(y_center + (R * sin(hdgltoa)));
  175. point2.setX(x_center + (R * cos(hdgltoa + M_PI)));
  176. point2.setY(y_center + (R * sin(hdgltoa + M_PI)));
  177. //calculat dis
  178. bool bp1inarc,bp2inarc;
  179. bp1inarc =pointinarc(parc,arcpoint,point1);
  180. bp2inarc =pointinarc(parc,arcpoint,point2);
  181. double fdis[4];
  182. fdis[0] = calcpointdis(pointnow,point1);
  183. fdis[1] = calcpointdis(pointnow,point2);
  184. fdis[2] = calcpointdis(pointnow,QPointF(parc->GetX(),parc->GetY()));
  185. QPointF pointend;
  186. double hdgrange = parc->GetLength()*parc->GetCurvature();
  187. double hdgend = parc->GetHdg() + hdgrange;
  188. while(hdgend <0.0)hdgend = hdgend + 2.0 *M_PI;
  189. while(hdgend >= 2.0*M_PI) hdgend = hdgend -2.0*M_PI;
  190. if(parc->GetCurvature() >0)
  191. {
  192. pointend.setX(arcpoint.x() + R*cos(hdgend -M_PI/2.0 ));
  193. pointend.setY(arcpoint.y() + R*sin(hdgend -M_PI/2.0) );
  194. }
  195. else
  196. {
  197. pointend.setX(arcpoint.x() + R*cos(hdgend +M_PI/2.0 ));
  198. pointend.setY(arcpoint.y() + R*sin(hdgend +M_PI/2.0) );
  199. }
  200. fdis[3] = calcpointdis(pointnow,pointend);
  201. int indexmin = -1;
  202. double fdismin = 1000000.0;
  203. if(bp1inarc)
  204. {
  205. indexmin = 0;fdismin = fdis[0];
  206. }
  207. if(bp2inarc)
  208. {
  209. if(indexmin == -1)
  210. {
  211. indexmin = 1;fdismin = fdis[1];
  212. }
  213. else
  214. {
  215. if(fdis[1]<fdismin)
  216. {
  217. indexmin = 1;fdismin = fdis[1];
  218. }
  219. }
  220. }
  221. if(indexmin == -1)
  222. {
  223. indexmin = 2;fdismin = fdis[2];
  224. }
  225. else
  226. {
  227. if(fdis[2]<fdismin)
  228. {
  229. indexmin = 2;fdismin = fdis[2];
  230. }
  231. }
  232. if(fdis[3]<fdismin)
  233. {
  234. indexmin = 3;fdismin = fdis[3];
  235. }
  236. double hdgdiff;
  237. switch (indexmin) {
  238. case 0:
  239. nearx = point1.x();
  240. neary = point1.y();
  241. if(parc->GetCurvature()<0)
  242. {
  243. nearhead = CalcHdg(arcpoint,point1) - M_PI/2.0;
  244. }
  245. else
  246. {
  247. nearhead = CalcHdg(arcpoint,point1) + M_PI/2.0;
  248. }
  249. while(nearhead>2.0*M_PI)nearhead = nearhead -2.0*M_PI;
  250. while(nearhead<0)nearhead = nearhead + 2.0*M_PI;
  251. hdgdiff = (nearhead-parc->GetHdg())*(parc->GetCurvature()/abs(parc->GetCurvature()));
  252. if(hdgdiff>=2.0*M_PI)hdgdiff = hdgdiff - 2.0*M_PI;
  253. if(hdgdiff<0)hdgdiff = hdgdiff + 2.0*M_PI;
  254. frels = hdgdiff * R;
  255. break;
  256. case 1:
  257. nearx = point2.x();
  258. neary = point2.y();
  259. if(parc->GetCurvature()<0)
  260. {
  261. nearhead = CalcHdg(arcpoint,point2) - M_PI/2.0;
  262. }
  263. else
  264. {
  265. nearhead = CalcHdg(arcpoint,point2) + M_PI/2.0;
  266. }
  267. while(nearhead>2.0*M_PI)nearhead = nearhead -2.0*M_PI;
  268. while(nearhead<0)nearhead = nearhead + 2.0*M_PI;
  269. hdgdiff = (nearhead-parc->GetHdg())*(parc->GetCurvature()/abs(parc->GetCurvature()));
  270. if(hdgdiff>=2.0*M_PI)hdgdiff = hdgdiff - 2.0*M_PI;
  271. if(hdgdiff<0)hdgdiff = hdgdiff + 2.0*M_PI;
  272. frels = hdgdiff * R;
  273. break;
  274. case 2:
  275. nearx = parc->GetX();
  276. neary = parc->GetY();
  277. nearhead = parc->GetHdg();
  278. frels = 0;
  279. break;
  280. case 3:
  281. nearx = pointend.x();
  282. neary = pointend.y();
  283. nearhead = hdgend;
  284. frels = parc->GetLength();
  285. break;
  286. default:
  287. std::cout<<"error in arcdis "<<std::endl;
  288. break;
  289. }
  290. while(nearhead>2.0*M_PI)nearhead = nearhead -2.0*M_PI;
  291. while(nearhead<0)nearhead = nearhead + 2.0*M_PI;
  292. return fdismin;
  293. }
  294. double xodrfunc::GetSpiralDis(GeometrySpiral * pspiral,double xnow,double ynow,double & nearx,
  295. double & neary,double & nearhead,double & frels)
  296. {
  297. double x,y,hdg;
  298. double s = 0.0;
  299. double fdismin = 100000.0;
  300. double s0 = pspiral->GetS();
  301. frels = 0;
  302. while(s<pspiral->GetLength())
  303. {
  304. pspiral->GetCoords(s0+s,x,y,hdg);
  305. double fdis = calcpointdis(QPointF(x,y),QPointF(xnow,ynow));
  306. if(fdis<fdismin)
  307. {
  308. fdismin = fdis;
  309. nearhead = hdg;
  310. nearx = x;
  311. neary = y;
  312. frels = s;
  313. }
  314. s = s+0.1;
  315. }
  316. return fdismin;
  317. }
  318. /**
  319. * @brief GetLineDis 获得点到直线Geometry的距离。
  320. * @param pline
  321. * @param x
  322. * @param y
  323. * @param nearx
  324. * @param neary
  325. * @param nearhead
  326. * @return
  327. */
  328. double xodrfunc::GetLineDis(GeometryLine * pline,const double x,const double y,double & nearx,
  329. double & neary,double & nearhead,double & frels)
  330. {
  331. double fRtn = 1000.0;
  332. double a1,a2,a3,a4,b1,b2;
  333. double ratio = pline->GetHdg();
  334. while(ratio >= 2.0* M_PI)ratio = ratio-2.0*M_PI;
  335. while(ratio<0)ratio = ratio+2.0*M_PI;
  336. double dis1,dis2,dis3;
  337. double x1,x2,x3,y1,y2,y3;
  338. x1 = pline->GetX();y1=pline->GetY();
  339. if((ratio == 0)||(ratio == M_PI))
  340. {
  341. a1 = 0;a4=0;
  342. a2 = 1;b1= pline->GetY();
  343. a3 = 1;b2= x;
  344. }
  345. else
  346. {
  347. if((ratio == 0.5*M_PI)||(ratio == 1.5*M_PI))
  348. {
  349. a2=0;a3=0;
  350. a1=1,b1=pline->GetX();
  351. a4 = 1;b2 = y;
  352. }
  353. else
  354. {
  355. a1 = tan(ratio) *(-1.0);
  356. a2 = 1;
  357. a3 = tan(ratio+M_PI/2.0)*(-1.0);
  358. a4 = 1;
  359. b1 = a1*pline->GetX() + a2 * pline->GetY();
  360. b2 = a3*x+a4*y;
  361. }
  362. }
  363. y2 = y1 + pline->GetLength() * sin(ratio);
  364. x2 = x1 + pline->GetLength() * cos(ratio);
  365. Eigen::Matrix2d A;
  366. A<<a1,a2,
  367. a3,a4;
  368. Eigen::Vector2d B(b1,b2);
  369. Eigen::Vector2d opoint = A.lu().solve(B);
  370. x3 = opoint(0);
  371. y3 = opoint(1);
  372. dis1 = sqrt(pow(x1-x,2)+pow(y1-y,2));
  373. dis2 = sqrt(pow(x2-x,2)+pow(y2-y,2));
  374. dis3 = sqrt(pow(x3-x,2)+pow(y3-y,2));
  375. if((dis1>pline->GetLength())||(dis2>pline->GetLength())) //Outoff line
  376. {
  377. // std::cout<<" out line"<<std::endl;
  378. if(dis1<dis2)
  379. {
  380. fRtn = dis1;
  381. nearx = x1;neary=y1;nearhead = pline->GetHdg();
  382. }
  383. else
  384. {
  385. fRtn = dis2;
  386. nearx = x2;neary=y2;nearhead = pline->GetHdg();
  387. }
  388. }
  389. else
  390. {
  391. fRtn = dis3;
  392. nearx = x3;neary=y3;nearhead = pline->GetHdg();
  393. }
  394. frels = sqrt(pow(nearx - pline->GetX(),2)+pow(neary - pline->GetY(),2));
  395. return fRtn;
  396. }
  397. namespace iv {
  398. struct nearoption
  399. {
  400. Road * pRoad;
  401. GeometryBlock * pgeob;
  402. double fdis;
  403. double nearx;
  404. double neary;
  405. double nearhead;
  406. double fs;
  407. int nlane;
  408. double fgeodis;
  409. };
  410. }
  411. int xodrfunc::GetNearPoint(const double x, const double y, OpenDrive *pxodr, Road **pObjRoad, GeometryBlock **pgeo,
  412. double &fdis, double &nearx, double &neary, double &nearhead,
  413. const double nearthresh,double * pfs,int * pnlane,bool bnotuselane)
  414. {
  415. double dismin = std::numeric_limits<double>::infinity();
  416. fdis = dismin;
  417. unsigned int i;
  418. *pObjRoad = 0;
  419. std::vector<iv::nearoption> xvectornearopt;
  420. for(i=0;i<pxodr->GetRoadCount();i++)
  421. {
  422. unsigned int j;
  423. Road * proad = pxodr->GetRoad(i);
  424. double nx,ny,nh,frels;
  425. for(j=0;j<proad->GetGeometryBlockCount();j++)
  426. {
  427. GeometryBlock * pgb = proad->GetGeometryBlock(j);
  428. double dis;
  429. RoadGeometry * pg;
  430. int nlane = 1000;
  431. pg = pgb->GetGeometryAt(0);
  432. switch (pg->GetGeomType()) {
  433. case 0: //line
  434. dis = GetLineDis((GeometryLine *) pg,x,y,nx,ny,nh,frels);
  435. break;
  436. case 1:
  437. dis = GetSpiralDis((GeometrySpiral *)pg,x,y,nx,ny,nh,frels);
  438. break;
  439. case 2: //arc
  440. dis = GetArcDis((GeometryArc *)pg,x,y,nx,ny,nh,frels);
  441. break;
  442. case 3:
  443. dis = 100000.0;
  444. break;
  445. case 4:
  446. dis = GetParamPoly3Dis((GeometryParamPoly3 *)pg,x,y,nx,ny,nh,frels);
  447. break;
  448. default:
  449. dis = 100000.0;
  450. break;
  451. }
  452. double fgeodis;
  453. fgeodis = dis;
  454. if((dis < 100)&&(bnotuselane == false))
  455. {
  456. double faccuratedis;
  457. faccuratedis = GetAcurateDis(x,y,proad,frels+pg->GetS(),nx,ny,nh,&nlane);
  458. if(faccuratedis < dis)dis = faccuratedis;
  459. }
  460. if(dis == 0)
  461. {
  462. iv::nearoption xopt;
  463. xopt.fdis = dis;
  464. xopt.fgeodis = fgeodis;
  465. xopt.fs = frels +pg->GetS();
  466. xopt.nearhead = nh;
  467. xopt.nearx = nx;
  468. xopt.neary = ny;
  469. xopt.nlane = nlane;
  470. xopt.pgeob = pgb;
  471. xopt.pRoad = proad;
  472. xvectornearopt.push_back(xopt);
  473. }
  474. if(dis < dismin)
  475. {
  476. dismin = dis;
  477. nearx = nx;
  478. neary = ny;
  479. nearhead = nh;
  480. fdis = dis;
  481. *pObjRoad = proad;
  482. *pgeo = pgb;
  483. if(pfs != 0)*pfs = frels +pg->GetS();
  484. if(pnlane != 0)*pnlane = nlane;
  485. }
  486. }
  487. }
  488. if(xvectornearopt.size() > 1)
  489. {
  490. double fgeodismin = 1000;
  491. int nindex = 0;
  492. for(i=0;i<xvectornearopt.size();i++)
  493. {
  494. if(xvectornearopt.at(i).fgeodis < fgeodismin)
  495. {
  496. fgeodismin = xvectornearopt.at(i).fgeodis;
  497. nindex = i;
  498. }
  499. }
  500. dismin = xvectornearopt.at(nindex).fdis;
  501. nearx = xvectornearopt.at(nindex).nearx;
  502. neary = xvectornearopt.at(nindex).neary;
  503. nearhead = xvectornearopt.at(nindex).nearhead;
  504. fdis = dismin;
  505. *pObjRoad = xvectornearopt.at(nindex).pRoad;
  506. *pgeo = xvectornearopt.at(nindex).pgeob;
  507. if(pfs != 0)*pfs = xvectornearopt.at(nindex).fs;
  508. if(pnlane != 0)*pnlane = xvectornearopt.at(nindex).nlane;
  509. }
  510. if(fdis > nearthresh)return -1;
  511. return 0;
  512. }
  513. std::vector<iv::LanePoint> xodrfunc::GetAllLanePoint(Road *pRoad, const double s,const double x, const double y,const double fhdg)
  514. {
  515. int i;
  516. int nLSCount = pRoad->GetLaneSectionCount();
  517. double s_section = 0;
  518. std::vector<iv::LanePoint> xvectorlanepoint;
  519. for(i=0;i<nLSCount;i++)
  520. {
  521. // if((pRoad->GetRoadId() == "30012")&&(s>35))
  522. // {
  523. // int a= 1;
  524. // }
  525. LaneSection * pLS = pRoad->GetLaneSection(i);
  526. if(i<(nLSCount -1))
  527. {
  528. if(pRoad->GetLaneSection(i+1)->GetS()<s)
  529. {
  530. continue;
  531. }
  532. }
  533. s_section = pLS->GetS();
  534. int nlanecount = pLS->GetLaneCount();
  535. int j;
  536. for(j=0;j<nlanecount;j++)
  537. {
  538. Lane * pLane = pLS->GetLane(j);
  539. int nlanemarktype = -1; //default no lanetype
  540. int nlanetype = 2; //driving
  541. int nlanecolor = 0;
  542. int k;
  543. double s_lane = 0;
  544. for(k=0;k<pLane->GetLaneRoadMarkCount();k++)
  545. {
  546. LaneRoadMark * plrm = pLane->GetLaneRoadMark(k);
  547. if(k<(pLane->GetLaneRoadMarkCount()-1))
  548. {
  549. if(pLane->GetLaneRoadMark(k+1)->GetS()<s_lane)
  550. {
  551. continue;
  552. }
  553. }
  554. if(plrm->GetType() == "solid")
  555. {
  556. nlanemarktype = 0;
  557. }
  558. if(plrm->GetType() == "broken")
  559. {
  560. nlanemarktype = 1;
  561. }
  562. if(plrm->GetType() == "solid solid")
  563. {
  564. nlanemarktype = 2;
  565. }
  566. if(plrm->GetType() == "solid broken")
  567. {
  568. nlanemarktype = 3;
  569. }
  570. if(plrm->GetType() == "broken solid")
  571. {
  572. nlanemarktype = 4;
  573. }
  574. if(plrm->GetType() == "broken broken")
  575. {
  576. nlanemarktype = 5;
  577. }
  578. if(plrm->GetColor() == "standard")nlanecolor = 0;
  579. if(plrm->GetColor() == "blue")nlanecolor = 1;
  580. if(plrm->GetColor() == "green")nlanecolor = 2;
  581. if(plrm->GetColor() == "red")nlanecolor = 3;
  582. if(plrm->GetColor() == "white")nlanecolor = 4;
  583. if(plrm->GetColor() == "yellow")nlanecolor = 5;
  584. if(plrm->GetColor() == "orange")nlanecolor = 6;
  585. break;
  586. }
  587. if(pLane->GetType() == "shoulder")
  588. {
  589. nlanetype = 0;
  590. }
  591. if(pLane->GetType() == "border")
  592. {
  593. nlanetype = 1;
  594. }
  595. if(pLane->GetType() == "driving")
  596. {
  597. nlanetype = 2;
  598. }
  599. if(pLane->GetType() == "none")
  600. {
  601. nlanetype = 4;
  602. }
  603. if(pLane->GetType() == "biking")
  604. {
  605. nlanetype = 8;
  606. }
  607. if(pLane->GetType() == "sidewalk")
  608. {
  609. nlanetype = 9;
  610. }
  611. if(pLane->GetId() != 0)
  612. {
  613. // if((pRoad->GetRoadId() == "10012")&&(pLane->GetId()==1))
  614. // {
  615. // int a= 1;
  616. // }
  617. // int k;
  618. // double s_lane = 0;
  619. for(k=0;k<pLane->GetLaneWidthCount();k++)
  620. {
  621. if(k<(pLane->GetLaneWidthCount()-1))
  622. {
  623. if((pLane->GetLaneWidth(k+1)->GetS()+s_section)<s)
  624. {
  625. continue;
  626. }
  627. }
  628. s_lane = pLane->GetLaneWidth(k)->GetS();
  629. break;
  630. }
  631. LaneWidth * pLW = pLane->GetLaneWidth(k);
  632. if(pLW == 0)
  633. {
  634. std::cout<<"not find LaneWidth"<<std::endl;
  635. break;
  636. }
  637. iv::LanePoint lp;
  638. lp.mnlanetype = nlanetype;
  639. lp.mnlanemarktype = nlanemarktype;
  640. lp.mnlanecolor = nlanecolor;
  641. lp.mnlane = pLane->GetId();
  642. lp.mnLaneSection = i;
  643. double fds = s - s_lane - s_section;
  644. lp.mflanewidth = pLW->GetA() + pLW->GetB() * fds
  645. +pLW->GetC() * pow(fds,2) + pLW->GetD() * pow(fds,3);
  646. lp.mflanetocenter = 0;
  647. lp.mnlanetype = nlanetype;
  648. lp.mfhdg = fhdg;
  649. lp.mS = s;
  650. xvectorlanepoint.push_back(lp);
  651. }
  652. else
  653. {
  654. iv::LanePoint lp;
  655. lp.mnlanetype = nlanetype;
  656. lp.mnlanemarktype = nlanemarktype;
  657. lp.mnlanecolor = nlanecolor;
  658. lp.mnlane = 0;
  659. lp.mnLaneSection = i;
  660. lp.mflanewidth = 0;
  661. lp.mflanetocenter = 0;
  662. lp.mfhdg = fhdg;
  663. lp.mS = s;
  664. xvectorlanepoint.push_back(lp);
  665. }
  666. }
  667. for(j=0;j<xvectorlanepoint.size();j++)
  668. {
  669. int k;
  670. for(k=0;k<xvectorlanepoint.size();k++)
  671. {
  672. if(abs(xvectorlanepoint[k].mnlane)>abs((xvectorlanepoint[j].mnlane)))
  673. {
  674. continue;
  675. }
  676. if(xvectorlanepoint[k].mnlane * xvectorlanepoint[j].mnlane <= 0)
  677. {
  678. continue;
  679. }
  680. xvectorlanepoint[j].mflanetocenter = xvectorlanepoint[j].mflanetocenter + xvectorlanepoint[k].mflanewidth;
  681. }
  682. }
  683. for(j=0;j<xvectorlanepoint.size();j++)
  684. {
  685. if(xvectorlanepoint[j].mnlane < 0)
  686. {
  687. xvectorlanepoint[j].mfX = x + xvectorlanepoint[j].mflanetocenter * cos(fhdg-M_PI/2.0);
  688. xvectorlanepoint[j].mfY = y + xvectorlanepoint[j].mflanetocenter * sin(fhdg-M_PI/2.0);
  689. }
  690. else
  691. {
  692. xvectorlanepoint[j].mfX = x + xvectorlanepoint[j].mflanetocenter * cos(fhdg+M_PI/2.0);
  693. xvectorlanepoint[j].mfY = y + xvectorlanepoint[j].mflanetocenter * sin(fhdg+M_PI/2.0);
  694. }
  695. }
  696. break;
  697. }
  698. std::vector<iv::LanePoint> xvectorlanepointrtn;
  699. bool bIsSort = true;
  700. for(i=0;i<(xvectorlanepoint.size()-1);i++)
  701. {
  702. if(xvectorlanepoint[i].mnlane < xvectorlanepoint[i+1].mnlane)
  703. {
  704. bIsSort = false;
  705. break;
  706. }
  707. }
  708. if(bIsSort == false)
  709. {
  710. while(xvectorlanepoint.size() > 0)
  711. {
  712. int nlanemin;
  713. nlanemin = 0;
  714. int nlanenum = xvectorlanepoint[0].mnlane;
  715. for(i=1;i<xvectorlanepoint.size();i++)
  716. {
  717. if(xvectorlanepoint[i].mnlane >= nlanenum)
  718. {
  719. nlanenum = xvectorlanepoint[i].mnlane;
  720. nlanemin = i;
  721. }
  722. }
  723. xvectorlanepointrtn.push_back(xvectorlanepoint[nlanemin]);
  724. xvectorlanepoint.erase(xvectorlanepoint.begin() + nlanemin);
  725. }
  726. }
  727. else
  728. {
  729. xvectorlanepointrtn = xvectorlanepoint;
  730. }
  731. return xvectorlanepointrtn;
  732. }
  733. double xodrfunc::GetAcurateDis(const double x, const double y, Road *pRoad, const double s, const double nearx, const double neary, const double nearhead,int * pnlane)
  734. {
  735. double fdismin = 1000;
  736. if(pRoad->GetLaneSectionCount() < 1)return 1000;
  737. std::vector<iv::LanePoint> xvectorlanepoint = GetAllLanePoint(pRoad,s,nearx,neary,nearhead);
  738. double fdistoref = sqrt(pow(x-nearx,2)+pow(y-neary,2));
  739. int i;
  740. std::vector<double> xvectordis;
  741. int nsize = xvectorlanepoint.size();
  742. for(i=0;i<nsize;i++)
  743. {
  744. double fdis = sqrt(pow(x-xvectorlanepoint[i].mfX,2)+pow(y-xvectorlanepoint[i].mfY,2));
  745. xvectordis.push_back(fdis);
  746. if(fdismin>fdis)fdismin = fdis;
  747. }
  748. int nlane = -1000;
  749. for(i=0;i<nsize;i++)
  750. {
  751. if((xvectordis[i]<=xvectorlanepoint[i].mflanewidth)&&(fdistoref <= xvectorlanepoint[i].mflanetocenter))
  752. {
  753. nlane = xvectorlanepoint[i].mnlane;
  754. fdismin = 0; //On Lane, is very near.
  755. break;
  756. }
  757. }
  758. if(pnlane != 0)*pnlane = nlane;
  759. return fdismin;
  760. }
  761. int xodrfunc::GetLineXY(GeometryLine *pline, double soff, double &x, double &y, double &hdg)
  762. {
  763. if(soff<0)return -1;
  764. hdg = pline->GetHdg();
  765. x = pline->GetX() + soff*cos(hdg);
  766. y = pline->GetY() + soff*sin(hdg);
  767. return 0;
  768. }
  769. Road * xodrfunc::GetRoadByID(OpenDrive * pxodr,std::string strroadid)
  770. {
  771. Road * pRoad = 0;
  772. int nroadcount = pxodr->GetRoadCount();
  773. int i;
  774. for(i=0;i<nroadcount;i++)
  775. {
  776. if(pxodr->GetRoad(i)->GetRoadId() == strroadid)
  777. {
  778. pRoad = pxodr->GetRoad(i);
  779. break;
  780. }
  781. }
  782. return pRoad;
  783. }
  784. int xodrfunc::GetSpiralXY(GeometrySpiral *pspira, double soff, double &x, double &y, double &hdg)
  785. {
  786. pspira->GetCoords(pspira->GetS() + soff,x,y,hdg);
  787. return 0;
  788. }
  789. int xodrfunc::GetArcXY(GeometryArc *parc, double soff, double &x, double &y, double &hdg)
  790. {
  791. if(parc->GetCurvature() == 0)return -1;
  792. double R = fabs(1.0/parc->GetCurvature());
  793. //calculate arc center
  794. double x_center = parc->GetX() + (1.0/parc->GetCurvature()) * cos(parc->GetHdg() + M_PI/2.0);
  795. double y_center = parc->GetY() + (1.0/parc->GetCurvature()) * sin(parc->GetHdg()+ M_PI/2.0);
  796. double arcdiff = soff/R;
  797. if(parc->GetCurvature() > 0)
  798. {
  799. x = x_center + R * cos(parc->GetHdg() + arcdiff - M_PI/2.0);
  800. y = y_center + R * sin(parc->GetHdg() + arcdiff - M_PI/2.0);
  801. hdg = parc->GetHdg() + arcdiff;
  802. }
  803. else
  804. {
  805. x = x_center + R * cos(parc->GetHdg() -arcdiff + M_PI/2.0);
  806. y = y_center + R * sin(parc->GetHdg() -arcdiff + M_PI/2.0);
  807. hdg = parc->GetHdg() - arcdiff;
  808. }
  809. return 0;
  810. }
  811. int xodrfunc::GetParamPoly3XY(GeometryParamPoly3 *pparam3d, double soff, double &x, double &y, double &hdg)
  812. {
  813. double xtem,ytem;
  814. double ua,ub,uc,ud,va,vb,vc,vd;
  815. ua = pparam3d->GetuA();ub= pparam3d->GetuB();uc= pparam3d->GetuC();ud = pparam3d->GetuD();
  816. va = pparam3d->GetvA();vb= pparam3d->GetvB();vc= pparam3d->GetvC();vd = pparam3d->GetvD();
  817. // xtem = parc->GetuA() + parc->GetuB() * s * len + parc->GetuC() * s*s *pow(len,2) + parc->GetuD() * s*s*s *pow(len,3);
  818. // ytem = parc->GetvA() + parc->GetvB() * s* len + parc->GetvC() * s*s *pow(len,2) + parc->GetvD() * s*s*s *pow(len,3);
  819. xtem = ua + ub * soff + uc * soff * soff + ud * soff *soff*soff ;
  820. ytem = va + vb * soff + vc * soff*soff + vd * soff*soff*soff ;
  821. x = xtem*cos(pparam3d->GetHdg()) - ytem * sin(pparam3d->GetHdg()) + pparam3d->GetX();
  822. y = xtem*sin(pparam3d->GetHdg()) + ytem * cos(pparam3d->GetHdg()) + pparam3d->GetY();
  823. if(soff<0.3)hdg = pparam3d->GetHdg();
  824. else
  825. {
  826. double soff1 = soff - 0.1;
  827. double x1,y1;
  828. xtem = ua + ub * soff1 + uc * soff1 * soff1 + ud * soff1 *soff1*soff1 ;
  829. ytem = va + vb * soff1 + vc * soff1*soff1 + vd * soff1*soff1*soff1 ;
  830. x1 = xtem*cos(pparam3d->GetHdg()) - ytem * sin(pparam3d->GetHdg()) + pparam3d->GetX();
  831. y1 = xtem*sin(pparam3d->GetHdg()) + ytem * cos(pparam3d->GetHdg()) + pparam3d->GetY();
  832. hdg = CalcHdg(QPointF(x1,y1),QPointF(x,y));
  833. }
  834. return 0;
  835. }
  836. int xodrfunc::GetRoadXYByS(Road *pRoad, const double s, double &x, double &y, double &hdg)
  837. {
  838. if(s<0)return -1;
  839. if(s>(pRoad->GetRoadLength()+0.1))return -2;
  840. if(pRoad == 0)return -3;
  841. if(pRoad->GetGeometryBlockCount()<1)return -4;
  842. int i;
  843. int nroadgeosize = pRoad->GetGeometryBlockCount();
  844. RoadGeometry * pgeosel = pRoad->GetGeometryBlock(nroadgeosize -1)->GetGeometryAt(0);
  845. for(i=0;i<(nroadgeosize-1);i++)
  846. {
  847. if(s<pRoad->GetGeometryBlock(i+1)->GetGeometryAt(0)->GetS())
  848. {
  849. pgeosel = pRoad->GetGeometryBlock(0)->GetGeometryAt(0);
  850. break;
  851. }
  852. }
  853. switch (pgeosel->GetGeomType()) {
  854. case 0:
  855. return GetLineXY((GeometryLine *)pgeosel,(s-pgeosel->GetS()),x,y,hdg);
  856. break;
  857. case 1:
  858. return GetSpiralXY((GeometrySpiral *)pgeosel,(s-pgeosel->GetS()),x,y,hdg);
  859. break;
  860. case 2:
  861. return GetArcXY((GeometryArc *)pgeosel,(s-pgeosel->GetS()),x,y,hdg);
  862. break;
  863. case 3:
  864. break;
  865. case 4:
  866. return GetParamPoly3XY((GeometryParamPoly3 *)pgeosel,(s-pgeosel->GetS()),x,y,hdg);
  867. break;
  868. default:
  869. break;
  870. }
  871. return -5;
  872. }
  873. int xodrfunc::GetRoadIndex(OpenDrive * pxodr, Road *pRoad)
  874. {
  875. int nroadcount = pxodr->GetRoadCount();
  876. int i;
  877. for(i=0;i<nroadcount;i++)
  878. {
  879. if(pxodr->GetRoad(i) == pRoad)
  880. {
  881. return i;
  882. }
  883. }
  884. return -1;
  885. }