globalplan.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066
  1. #include "globalplan.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. using namespace Eigen;
  12. extern "C"
  13. {
  14. #include "dubins.h"
  15. }
  16. static void AddSignalMark(Road * pRoad, int nlane,std::vector<PlanPoint> & xvectorPP);
  17. /**
  18. * @brief GetLineDis 获得点到直线Geometry的距离。
  19. * @param pline
  20. * @param x
  21. * @param y
  22. * @param nearx
  23. * @param neary
  24. * @param nearhead
  25. * @return
  26. */
  27. static double GetLineDis(GeometryLine * pline,const double x,const double y,double & nearx,
  28. double & neary,double & nearhead)
  29. {
  30. double fRtn = 1000.0;
  31. double a1,a2,a3,a4,b1,b2;
  32. double ratio = pline->GetHdg();
  33. while(ratio >= 2.0* M_PI)ratio = ratio-2.0*M_PI;
  34. while(ratio<0)ratio = ratio+2.0*M_PI;
  35. double dis1,dis2,dis3;
  36. double x1,x2,x3,y1,y2,y3;
  37. x1 = pline->GetX();y1=pline->GetY();
  38. if((ratio == 0)||(ratio == M_PI))
  39. {
  40. a1 = 0;a4=0;
  41. a2 = 1;b1= pline->GetY();
  42. a3 = 1;b2= x;
  43. }
  44. else
  45. {
  46. if((ratio == 0.5*M_PI)||(ratio == 1.5*M_PI))
  47. {
  48. a2=0;a3=0;
  49. a1=1,b1=pline->GetX();
  50. a4 = 1;b2 = y;
  51. }
  52. else
  53. {
  54. a1 = tan(ratio) *(-1.0);
  55. a2 = 1;
  56. a3 = tan(ratio+M_PI/2.0)*(-1.0);
  57. a4 = 1;
  58. b1 = a1*pline->GetX() + a2 * pline->GetY();
  59. b2 = a3*x+a4*y;
  60. }
  61. }
  62. y2 = y1 + pline->GetLength() * sin(ratio);
  63. x2 = x1 + pline->GetLength() * cos(ratio);
  64. Eigen::Matrix2d A;
  65. A<<a1,a2,
  66. a3,a4;
  67. Eigen::Vector2d B(b1,b2);
  68. Eigen::Vector2d opoint = A.lu().solve(B);
  69. // std::cout<<opoint<<std::endl;
  70. x3 = opoint(0);
  71. y3 = opoint(1);
  72. dis1 = sqrt(pow(x1-x,2)+pow(y1-y,2));
  73. dis2 = sqrt(pow(x2-x,2)+pow(y2-y,2));
  74. dis3 = sqrt(pow(x3-x,2)+pow(y3-y,2));
  75. // std::cout<<" dis 1 is "<<dis1<<std::endl;
  76. // std::cout<<" dis 2 is "<<dis2<<std::endl;
  77. // std::cout<<" dis 3 is "<<dis3<<std::endl;
  78. if((dis1>pline->GetLength())||(dis2>pline->GetLength())) //Outoff line
  79. {
  80. // std::cout<<" out line"<<std::endl;
  81. if(dis1<dis2)
  82. {
  83. fRtn = dis1;
  84. nearx = x1;neary=y1;nearhead = pline->GetHdg();
  85. }
  86. else
  87. {
  88. fRtn = dis2;
  89. nearx = x2;neary=y2;nearhead = pline->GetHdg();
  90. }
  91. }
  92. else
  93. {
  94. fRtn = dis3;
  95. nearx = x3;neary=y3;nearhead = pline->GetHdg();
  96. }
  97. // std::cout<<"dis is "<<fRtn<<" x is "<<nearx<<" y is "<<neary<<" head is "<<nearhead<<std::endl;
  98. return fRtn;
  99. }
  100. static int getcirclecrosspoint(QPointF startPos, QPointF endPos, QPointF agvPos, double R,QPointF & point1,QPointF & point2 )
  101. {
  102. double m=0;
  103. double k;
  104. double b;
  105. //计算分子
  106. m=startPos.x()-endPos.x();
  107. //求直线的方程
  108. if(0==m)
  109. {
  110. k=100000;
  111. b=startPos.y()-k*startPos.x();
  112. }
  113. else
  114. {
  115. k=(endPos.y()-startPos.y())/(endPos.x()-startPos.x());
  116. b=startPos.y()-k*startPos.x();
  117. }
  118. // qDebug()<<k<<b;
  119. double temp = fabs(k*agvPos.x()-agvPos.y()+b)/sqrt(k*k+b*b);
  120. //求直线与圆的交点 前提是圆需要与直线有交点
  121. if(fabs(k*agvPos.x()-agvPos.y()+b)/sqrt(k*k+b*b)<R)
  122. {
  123. point1.setX((2*agvPos.x()-2*k*(b-agvPos.y())+sqrt(pow((2*k*(b-agvPos.y())-2*agvPos.x()),2)-4*(k*k+1)*((b-agvPos.y())*(b-agvPos.y())+agvPos.x()*agvPos.x()-R*R)))/(2*k*k+2));
  124. point2.setX((2*agvPos.x()-2*k*(b-agvPos.y())-sqrt(pow((2*k*(b-agvPos.y())-2*agvPos.x()),2)-4*(k*k+1)*((b-agvPos.y())*(b-agvPos.y())+agvPos.x()*agvPos.x()-R*R)))/(2*k*k+2));
  125. point1.setY(k*point1.x()+b);
  126. point2.setY(k*point2.x()+b);
  127. return 0;
  128. }
  129. return -1;
  130. }
  131. /**
  132. * @brief CalcHdg
  133. * 计算点0到点1的航向
  134. * @param p0 Point 0
  135. * @param p1 Point 1
  136. **/
  137. static double CalcHdg(QPointF p0, QPointF p1)
  138. {
  139. double x0,y0,x1,y1;
  140. x0 = p0.x();
  141. y0 = p0.y();
  142. x1 = p1.x();
  143. y1 = p1.y();
  144. if(x0 == x1)
  145. {
  146. if(y0 < y1)
  147. {
  148. return M_PI/2.0;
  149. }
  150. else
  151. return M_PI*3.0/2.0;
  152. }
  153. double ratio = (y1-y0)/(x1-x0);
  154. double hdg = atan(ratio);
  155. if(ratio > 0)
  156. {
  157. if(y1 > y0)
  158. {
  159. }
  160. else
  161. {
  162. hdg = hdg + M_PI;
  163. }
  164. }
  165. else
  166. {
  167. if(y1 > y0)
  168. {
  169. hdg = hdg + M_PI;
  170. }
  171. else
  172. {
  173. hdg = hdg + 2.0*M_PI;
  174. }
  175. }
  176. return hdg;
  177. }
  178. static bool pointinarc(GeometryArc * parc,QPointF poingarc,QPointF point1)
  179. {
  180. double hdg = CalcHdg(poingarc,point1);
  181. if(parc->GetCurvature() >0)hdg = hdg + M_PI/2.0;
  182. else hdg = hdg - M_PI/2.0;
  183. if(hdg >= 2.0*M_PI)hdg = hdg - 2.0*M_PI;
  184. if(hdg < 0)hdg = hdg + 2.0*M_PI;
  185. double hdgrange = parc->GetLength()/(1.0/parc->GetCurvature());
  186. double hdgdiff = hdg - parc->GetHdg();
  187. if(hdgrange >= 0 )
  188. {
  189. if(hdgdiff < 0)hdgdiff = hdgdiff + M_PI*2.0;
  190. }
  191. else
  192. {
  193. if(hdgdiff > 0)hdgdiff = hdgdiff - M_PI*2.0;
  194. }
  195. if(fabs(hdgdiff ) < fabs(hdgrange))return true;
  196. return false;
  197. }
  198. static inline double calcpointdis(QPointF p1,QPointF p2)
  199. {
  200. return sqrt(pow(p1.x()-p2.x(),2)+pow(p1.y()-p2.y(),2));
  201. }
  202. /**
  203. * @brief GetArcDis
  204. * 计算点到圆弧的最短距离,首先用点和圆弧中心点的直线与圆的交点,计算点到交点的距离,如果交点在圆弧上,则最短距离是交点之一,否则计算点到圆弧两个端点的距离。
  205. * @param parc pointer to a arc geomery
  206. * @param x current x
  207. * @param y current y
  208. * @param nearx near x
  209. * @param neary near y
  210. * @param nearhead nearhead
  211. **/
  212. static double GetArcDis(GeometryArc * parc,double x,double y,double & nearx,
  213. double & neary,double & nearhead)
  214. {
  215. // double fRtn = 1000.0;
  216. if(parc->GetCurvature() == 0.0)return 1000.0;
  217. double R = fabs(1.0/parc->GetCurvature());
  218. // if(parc->GetX() == 4.8213842690322309e+01)
  219. // {
  220. // int a = 1;
  221. // a = 3;
  222. // }
  223. // if(parc->GetCurvature() == -0.0000110203)
  224. // {
  225. // int a = 1;
  226. // a = 3;
  227. // }
  228. //calculate arc center
  229. double x_center,y_center;
  230. if(parc->GetCurvature() > 0)
  231. {
  232. x_center = parc->GetX() + R * cos(parc->GetHdg() + M_PI/2.0);
  233. y_center = parc->GetY() + R * sin(parc->GetHdg()+ M_PI/2.0);
  234. }
  235. else
  236. {
  237. x_center = parc->GetX() + R * cos(parc->GetHdg() - M_PI/2.0);
  238. y_center = parc->GetY() + R * sin(parc->GetHdg() - M_PI/2.0);
  239. }
  240. double hdgltoa = CalcHdg(QPointF(x,y),QPointF(x_center,y_center));
  241. QPointF arcpoint;
  242. arcpoint.setX(x_center);arcpoint.setY(y_center);
  243. QPointF pointnow;
  244. pointnow.setX(x);pointnow.setY(y);
  245. QPointF point1,point2;
  246. point1.setX(x_center + (R * cos(hdgltoa)));
  247. point1.setY(y_center + (R * sin(hdgltoa)));
  248. point2.setX(x_center + (R * cos(hdgltoa + M_PI)));
  249. point2.setY(y_center + (R * sin(hdgltoa + M_PI)));
  250. //calculat dis
  251. bool bp1inarc,bp2inarc;
  252. bp1inarc =pointinarc(parc,arcpoint,point1);
  253. bp2inarc =pointinarc(parc,arcpoint,point2);
  254. double fdis[4];
  255. fdis[0] = calcpointdis(pointnow,point1);
  256. fdis[1] = calcpointdis(pointnow,point2);
  257. fdis[2] = calcpointdis(pointnow,QPointF(parc->GetX(),parc->GetY()));
  258. QPointF pointend;
  259. double hdgrange = parc->GetLength()*parc->GetCurvature();
  260. double hdgend = parc->GetHdg() + hdgrange;
  261. while(hdgend <0.0)hdgend = hdgend + 2.0 *M_PI;
  262. while(hdgend >= 2.0*M_PI) hdgend = hdgend -2.0*M_PI;
  263. if(parc->GetCurvature() >0)
  264. {
  265. pointend.setX(arcpoint.x() + R*cos(hdgend -M_PI/2.0 ));
  266. pointend.setY(arcpoint.y() + R*sin(hdgend -M_PI/2.0) );
  267. }
  268. else
  269. {
  270. pointend.setX(arcpoint.x() + R*cos(hdgend +M_PI/2.0 ));
  271. pointend.setY(arcpoint.y() + R*sin(hdgend +M_PI/2.0) );
  272. }
  273. fdis[3] = calcpointdis(pointnow,pointend);
  274. int indexmin = -1;
  275. double fdismin = 1000000.0;
  276. if(bp1inarc)
  277. {
  278. indexmin = 0;fdismin = fdis[0];
  279. }
  280. if(bp2inarc)
  281. {
  282. if(indexmin == -1)
  283. {
  284. indexmin = 1;fdismin = fdis[1];
  285. }
  286. else
  287. {
  288. if(fdis[1]<fdismin)
  289. {
  290. indexmin = 1;fdismin = fdis[1];
  291. }
  292. }
  293. }
  294. if(indexmin == -1)
  295. {
  296. indexmin = 2;fdismin = fdis[2];
  297. }
  298. else
  299. {
  300. if(fdis[2]<fdismin)
  301. {
  302. indexmin = 2;fdismin = fdis[2];
  303. }
  304. }
  305. if(fdis[3]<fdismin)
  306. {
  307. indexmin = 3;fdismin = fdis[3];
  308. }
  309. switch (indexmin) {
  310. case 0:
  311. nearx = point1.x();
  312. neary = point1.y();
  313. if(parc->GetCurvature()<0)
  314. {
  315. nearhead = CalcHdg(arcpoint,point1) - M_PI/2.0;
  316. }
  317. else
  318. {
  319. nearhead = CalcHdg(arcpoint,point1) + M_PI/2.0;
  320. }
  321. break;
  322. case 1:
  323. nearx = point2.x();
  324. neary = point2.y();
  325. if(parc->GetCurvature()<0)
  326. {
  327. nearhead = CalcHdg(arcpoint,point2) - M_PI/2.0;
  328. }
  329. else
  330. {
  331. nearhead = CalcHdg(arcpoint,point2) + M_PI/2.0;
  332. }
  333. break;
  334. case 2:
  335. nearx = parc->GetX();
  336. neary = parc->GetY();
  337. nearhead = parc->GetHdg();
  338. break;
  339. case 3:
  340. nearx = pointend.x();
  341. neary = pointend.y();
  342. nearhead = hdgend;
  343. break;
  344. default:
  345. std::cout<<"error in arcdis "<<std::endl;
  346. break;
  347. }
  348. while(nearhead>2.0*M_PI)nearhead = nearhead -2.0*M_PI;
  349. while(nearhead<0)nearhead = nearhead + 2.0*M_PI;
  350. return fdismin;
  351. }
  352. static double GetSpiralDis(GeometrySpiral * pspiral,double xnow,double ynow,double & nearx,
  353. double & neary,double & nearhead)
  354. {
  355. double x,y,hdg;
  356. double s = 0.0;
  357. double fdismin = 100000.0;
  358. double s0 = pspiral->GetS();
  359. while(s<pspiral->GetLength())
  360. {
  361. pspiral->GetCoords(s0+s,x,y,hdg);
  362. s = s+0.1;
  363. double fdis = calcpointdis(QPointF(x,y),QPointF(xnow,ynow));
  364. if(fdis<fdismin)
  365. {
  366. fdismin = fdis;
  367. nearhead = hdg;
  368. nearx = x;
  369. neary = y;
  370. }
  371. }
  372. return fdismin;
  373. }
  374. /**
  375. * @brief GetParamPoly3Dis 获得点到贝塞尔曲线的距离。
  376. * @param parc
  377. * @param xnow
  378. * @param ynow
  379. * @param nearx
  380. * @param neary
  381. * @param nearhead
  382. * @return
  383. */
  384. static double GetParamPoly3Dis(GeometryParamPoly3 * parc,double xnow,double ynow,double & nearx,
  385. double & neary,double & nearhead)
  386. {
  387. double s = 0.1;
  388. double fdismin = 100000.0;
  389. double xold,yold;
  390. xold = parc->GetX();
  391. yold = parc->GetY();
  392. double fdis = calcpointdis(QPointF(parc->GetX(),parc->GetY()),QPointF(xnow,ynow));
  393. if(fdis<fdismin)
  394. {
  395. fdismin = fdis;
  396. nearhead = parc->GetHdg();
  397. nearx = parc->GetX();
  398. neary = parc->GetY();
  399. }
  400. double s_start = parc->GetS();
  401. while(s < parc->GetLength())
  402. {
  403. double x, y,hdg;//xtem,ytem;
  404. parc->GetCoords(s_start+s,x,y,hdg);
  405. if(fdis<fdismin)
  406. {
  407. fdismin = fdis;
  408. nearhead = hdg;
  409. nearx = x;
  410. neary = y;
  411. }
  412. // xold = x;
  413. // yold = y;
  414. s = s+ 0.1;
  415. }
  416. return fdismin;
  417. }
  418. static double GetPoly3Dis(GeometryPoly3 * ppoly,double xnow,double ynow,double & nearx,
  419. double & neary,double & nearhead)
  420. {
  421. double x,y,hdg;
  422. // double s = 0.0;
  423. double fdismin = 100000.0;
  424. // double s0 = ppoly->GetS();
  425. x = ppoly->GetX();
  426. y = ppoly->GetY();
  427. double A,B,C,D;
  428. A = ppoly->GetA();
  429. B = ppoly->GetB();
  430. C = ppoly->GetC();
  431. D = ppoly->GetD();
  432. const double steplim = 0.3;
  433. double du = steplim;
  434. double u = 0;
  435. double v = 0;
  436. double oldx,oldy;
  437. oldx = x;
  438. oldy = y;
  439. double xstart,ystart;
  440. xstart = x;
  441. ystart = y;
  442. double hdgstart = ppoly->GetHdg();
  443. double flen = 0;
  444. u = u+du;
  445. while(flen < ppoly->GetLength())
  446. {
  447. double fdis = 0;
  448. v = A + B*u + C*u*u + D*u*u*u;
  449. x = xstart + u*cos(hdgstart) - v*sin(hdgstart);
  450. y = ystart + u*sin(hdgstart) + v*cos(hdgstart);
  451. fdis = sqrt(pow(x- oldx,2)+pow(y-oldy,2));
  452. if(fdis>(steplim*2.0))du = du/2.0;
  453. flen = flen + fdis;
  454. u = u + du;
  455. hdg = CalcHdg(QPointF(oldx,oldy),QPointF(x,y));
  456. double fdisnow = calcpointdis(QPointF(x,y),QPointF(xnow,ynow));
  457. if(fdisnow<fdismin)
  458. {
  459. fdismin = fdisnow;
  460. nearhead = hdg;
  461. nearx = x;
  462. neary = y;
  463. }
  464. oldx = x;
  465. oldy = y;
  466. }
  467. return fdismin;
  468. }
  469. /**
  470. * @brief GetNearPoint
  471. * 计算点到圆弧的最短距离,首先用点和圆弧中心点的直线与圆的交点,计算点到交点的距离,如果交点在圆弧上,则最短距离是交点之一,否则计算点到圆弧两个端点的距离。
  472. * @param x current x
  473. * @param y current y
  474. * @param pxodr OpenDrive
  475. * @param pObjRoad Road
  476. * @param pgeo Geometry of road
  477. * @param nearx near x
  478. * @param neary near y
  479. * @param nearhead nearhead
  480. * @param nearthresh near threshhold
  481. * @retval if == 0 successfull <0 fail.
  482. **/
  483. int GetNearPoint(const double x,const double y,OpenDrive * pxodr,Road ** pObjRoad,GeometryBlock ** pgeo, double & s,double & nearx,
  484. double & neary,double & nearhead,const double nearthresh,double &froads)
  485. {
  486. double dismin = std::numeric_limits<double>::infinity();
  487. s = dismin;
  488. int i;
  489. double frels;
  490. for(i=0;i<pxodr->GetRoadCount();i++)
  491. {
  492. int j;
  493. Road * proad = pxodr->GetRoad(i);
  494. double nx,ny,nh;
  495. for(j=0;j<proad->GetGeometryBlockCount();j++)
  496. {
  497. GeometryBlock * pgb = proad->GetGeometryBlock(j);
  498. double dis;
  499. RoadGeometry * pg;
  500. pg = pgb->GetGeometryAt(0);
  501. switch (pg->GetGeomType()) {
  502. case 0: //line
  503. dis = xodrfunc::GetLineDis((GeometryLine *) pg,x,y,nx,ny,nh,frels);
  504. // dis = GetLineDis((GeometryLine *) pg,x,y,nx,ny,nh);
  505. break;
  506. case 1:
  507. dis = xodrfunc::GetSpiralDis((GeometrySpiral *)pg,x,y,nx,ny,nh,frels);
  508. break;
  509. case 2: //arc
  510. dis = xodrfunc::GetArcDis((GeometryArc *)pg,x,y,nx,ny,nh,frels);
  511. break;
  512. case 3:
  513. dis = xodrfunc::GetPoly3Dis((GeometryPoly3 *)pg,x,y,nx,ny,nh,frels);
  514. break;
  515. case 4:
  516. dis = xodrfunc::GetParamPoly3Dis((GeometryParamPoly3 *)pg,x,y,nx,ny,nh,frels);
  517. break;
  518. default:
  519. dis = 100000.0;
  520. break;
  521. }
  522. if(dis < dismin)
  523. {
  524. dismin = dis;
  525. nearx = nx;
  526. neary = ny;
  527. nearhead = nh;
  528. s = dis;
  529. froads = frels;
  530. *pObjRoad = proad;
  531. *pgeo = pgb;
  532. }
  533. // if(pgb->CheckIfLine())
  534. // {
  535. // GeometryLine * pline = (GeometryLine *)pgb->GetGeometryAt(0);
  536. // double dis = GetLineDis(pline,x,y,nx,ny,nh);
  537. // if(dis < dismin)
  538. // {
  539. // dismin = dis;
  540. // nearx = nx;
  541. // neary = ny;
  542. // nearhead = nh;
  543. // s = dis;
  544. // *pObjRoad = proad;
  545. // *pgeo = pgb;
  546. // }
  547. // }
  548. // else
  549. // {
  550. // GeometryLine * pline1 = (GeometryLine *)pgb->GetGeometryAt(0);
  551. // double dis = GetLineDis(pline1,x,y,nx,ny,nh);
  552. // if(dis < dismin)
  553. // {
  554. // dismin = dis;
  555. // nearx = nx;
  556. // neary = ny;
  557. // nearhead = nh;
  558. // s = dis;
  559. // *pObjRoad = proad;
  560. // *pgeo = pgb;
  561. // }
  562. // GeometryArc * parc = (GeometryArc *)pgb->GetGeometryAt(1);
  563. // dis = GetArcDis(parc,x,y,nx,ny,nh);
  564. // if(dis < dismin)
  565. // {
  566. // dismin = dis;
  567. // nearx = nx;
  568. // neary = ny;
  569. // nearhead = nh;
  570. // s = dis;
  571. // *pObjRoad = proad;
  572. // *pgeo = pgb;
  573. // }
  574. // pline1 = (GeometryLine *)pgb->GetGeometryAt(2);
  575. // dis = GetLineDis(pline1,x,y,nx,ny,nh);
  576. // if(dis < dismin)
  577. // {
  578. // dismin = dis;
  579. // nearx = nx;
  580. // neary = ny;
  581. // nearhead = nh;
  582. // s = dis;
  583. // *pObjRoad = proad;
  584. // *pgeo = pgb;
  585. // }
  586. // }
  587. }
  588. }
  589. if(s > nearthresh)return -1;
  590. return 0;
  591. }
  592. int GetNearPointWithHead(const double x,const double y,const double hdg,OpenDrive * pxodr,std::vector<iv::nearroad> & xvectornear,
  593. const double nearthresh,bool bhdgvalid = true)
  594. {
  595. std::cout<<" near thresh "<<nearthresh<<std::endl;
  596. int i;
  597. double frels;
  598. int nminmode = 6;
  599. for(i=0;i<pxodr->GetRoadCount();i++)
  600. {
  601. int j;
  602. Road * proad = pxodr->GetRoad(i);
  603. double nx,ny,nh;
  604. bool bchange = false;
  605. double localdismin = std::numeric_limits<double>::infinity();
  606. double localnearx = 0;
  607. double localneary = 0;
  608. double localnearhead = 0;
  609. double locals = 0;
  610. double localfrels = 0;
  611. GeometryBlock * pgeolocal;
  612. localdismin = std::numeric_limits<double>::infinity();
  613. for(j=0;j<proad->GetGeometryBlockCount();j++)
  614. {
  615. GeometryBlock * pgb = proad->GetGeometryBlock(j);
  616. double dis;
  617. RoadGeometry * pg;
  618. pg = pgb->GetGeometryAt(0);
  619. switch (pg->GetGeomType()) {
  620. case 0: //line
  621. dis = xodrfunc::GetLineDis((GeometryLine *) pg,x,y,nx,ny,nh,frels);
  622. break;
  623. case 1:
  624. dis = xodrfunc::GetSpiralDis((GeometrySpiral *)pg,x,y,nx,ny,nh,frels);
  625. break;
  626. case 2: //arc
  627. dis = xodrfunc::GetArcDis((GeometryArc *)pg,x,y,nx,ny,nh,frels);
  628. break;
  629. case 3:
  630. dis = xodrfunc::GetPoly3Dis((GeometryPoly3 *)pg,x,y,nx,ny,nh,frels);
  631. break;
  632. case 4:
  633. dis = xodrfunc::GetParamPoly3Dis((GeometryParamPoly3 *)pg,x,y,nx,ny,nh,frels);
  634. break;
  635. default:
  636. dis = 100000.0;
  637. break;
  638. }
  639. if(dis<localdismin)
  640. {
  641. localdismin = dis;
  642. localnearx = nx;
  643. localneary = ny;
  644. localnearhead = nh;
  645. locals = dis;
  646. localfrels = frels;
  647. pgeolocal = pgb;
  648. bchange = true;
  649. }
  650. }
  651. std::cout<<" local dismin "<<localdismin<<std::endl;
  652. if(localdismin < nearthresh)
  653. {
  654. iv::nearroad xnear;
  655. xnear.pObjRoad = proad;
  656. xnear.pgeob = pgeolocal;
  657. xnear.nearx = localnearx;
  658. xnear.neary = localneary;
  659. xnear.fdis = localdismin;
  660. xnear.nearhead = localnearhead;
  661. xnear.frels = localfrels;
  662. if((xnear.fdis>0)&&(xnear.frels>0.00001)&&(xnear.frels<(proad->GetRoadLength()-0.00001)))
  663. {
  664. double fcalhdg = xodrfunc::CalcHdg(QPointF(xnear.nearx,xnear.neary),QPointF(x,y));
  665. double fhdgdiff = fcalhdg - xnear.nearhead;
  666. while(fhdgdiff<0)fhdgdiff = fhdgdiff + 2.0*M_PI;
  667. while(fhdgdiff>= 2.0*M_PI)fhdgdiff = fhdgdiff - 2.0*M_PI;
  668. if(fhdgdiff<M_PI)
  669. {
  670. double fdisroad = proad->GetRoadLeftWidth(xnear.frels);
  671. if(fdisroad>xnear.fdis)
  672. {
  673. xnear.fdis = 0;
  674. }
  675. else
  676. {
  677. xnear.fdis = xnear.fdis - fdisroad;
  678. }
  679. xnear.lr = 1;
  680. }
  681. else
  682. {
  683. double fdisroad = proad->GetRoadRightWidth(xnear.frels);
  684. if(fdisroad>xnear.fdis)
  685. {
  686. xnear.fdis = 0;
  687. }
  688. else
  689. {
  690. xnear.fdis = xnear.fdis - fdisroad;
  691. }
  692. xnear.lr = 2;
  693. }
  694. }
  695. else
  696. {
  697. if(bhdgvalid == false)
  698. {
  699. if(xnear.pObjRoad->GetLaneSectionCount()>0)
  700. {
  701. LaneSection * pLS = xnear.pObjRoad->GetLaneSection(0);
  702. if(pLS->GetRightLaneCount()>0)
  703. {
  704. xnear.lr = 2;
  705. }
  706. else
  707. {
  708. xnear.lr = 1;
  709. }
  710. }
  711. else
  712. {
  713. xnear.lr = 2;
  714. }
  715. }
  716. }
  717. LaneSection * pLS = xnear.pObjRoad->GetLaneSection(0);
  718. if(pLS != NULL)
  719. {
  720. if(xnear.fdis < 0.00001)
  721. {
  722. if(bhdgvalid == false)
  723. {
  724. xnear.nmode = 0;
  725. }
  726. else
  727. {
  728. double headdiff = hdg - xnear.nearhead;
  729. while(headdiff < 2.0*M_PI) headdiff = headdiff + 2.0*M_PI;
  730. while(headdiff >= 2.0*M_PI)headdiff = headdiff - 2.0*M_PI;
  731. if(((headdiff <M_PI/2.0)||(headdiff > M_PI*3.0/2.0))&&(pLS->GetRightLaneCount()>0))
  732. {
  733. xnear.nmode = 0;
  734. }
  735. else
  736. {
  737. xnear.nmode = 1;
  738. }
  739. }
  740. }
  741. else
  742. {
  743. if(xnear.fdis<5)
  744. {
  745. if(bhdgvalid == false)
  746. {
  747. xnear.nmode = 2;
  748. }
  749. else
  750. {
  751. double headdiff = hdg - xnear.nearhead;
  752. while(headdiff < 2.0*M_PI) headdiff = headdiff + 2.0*M_PI;
  753. while(headdiff >= 2.0*M_PI)headdiff = headdiff - 2.0*M_PI;
  754. if(((headdiff <M_PI/2.0)||(headdiff > M_PI*3.0/2.0))&&(pLS->GetRightLaneCount()>0))
  755. {
  756. xnear.nmode = 2;
  757. }
  758. else
  759. {
  760. xnear.nmode = 3;
  761. }
  762. }
  763. }
  764. else
  765. {
  766. if(bhdgvalid == false)
  767. {
  768. xnear.nmode = 4;
  769. }
  770. else
  771. {
  772. double headdiff = hdg - xnear.nearhead;
  773. while(headdiff < 2.0*M_PI) headdiff = headdiff + 2.0*M_PI;
  774. while(headdiff >= 2.0*M_PI)headdiff = headdiff - 2.0*M_PI;
  775. if(((headdiff <M_PI/2.0)||(headdiff > M_PI*3.0/2.0))&&(pLS->GetRightLaneCount()>0))
  776. {
  777. xnear.nmode = 4;
  778. }
  779. else
  780. {
  781. xnear.nmode = 5;
  782. }
  783. }
  784. }
  785. }
  786. if(xnear.nmode < nminmode)nminmode = xnear.nmode;
  787. if(xnear.pObjRoad->GetLaneSectionCount()>0)
  788. {
  789. LaneSection * pLS = xnear.pObjRoad->GetLaneSection(0);
  790. if((pLS->GetRightLaneCount()>0)&& ((pLS->GetLeftLaneCount()==0)) && (xnear.lr == 1))
  791. {
  792. xnear.lr = 2;
  793. }
  794. if((pLS->GetRightLaneCount() == 0)&& ((pLS->GetLeftLaneCount()>0)) && (xnear.lr == 2))
  795. {
  796. xnear.lr = 1;
  797. }
  798. }
  799. xvectornear.push_back(xnear);
  800. }
  801. }
  802. }
  803. if(xvectornear.size() == 0)
  804. {
  805. std::cout<<" no near. "<<std::endl;
  806. return -1;
  807. }
  808. // std::cout<<" near size: "<<xvectornear.size()<<std::endl;
  809. if(xvectornear.size() > 1)
  810. {
  811. int i;
  812. for(i=0;i<(int)xvectornear.size();i++)
  813. {
  814. if(xvectornear[i].nmode > nminmode)
  815. {
  816. xvectornear.erase(xvectornear.begin() + i);
  817. i = i-1;
  818. }
  819. }
  820. }
  821. // std::cout<<" after size: "<<xvectornear.size()<<std::endl;
  822. if((xvectornear.size()>1)&&(nminmode>=4)) //if dis > 5 select small dis
  823. {
  824. int i;
  825. while(xvectornear.size()>1)
  826. {
  827. if(xvectornear[1].fdis < xvectornear[0].fdis)
  828. {
  829. xvectornear.erase(xvectornear.begin());
  830. }
  831. else
  832. {
  833. xvectornear.erase(xvectornear.begin()+1);
  834. }
  835. }
  836. }
  837. return 0;
  838. }
  839. /**
  840. * @brief SelectRoadLeftRight
  841. * 选择左侧道路或右侧道路
  842. * 1.选择角度差小于90度的道路一侧,即同侧道路
  843. * 2.单行路,选择存在的那一侧道路。
  844. * @param pRoad 道路
  845. * @param head1 车的航向或目标点的航向
  846. * @param head_road 目标点的航向
  847. * @retval 1 left 2 right
  848. **/
  849. static int SelectRoadLeftRight(Road * pRoad,const double head1,const double head_road)
  850. {
  851. int nrtn = 2;
  852. double headdiff = head1 - head_road;
  853. while(headdiff < 2.0*M_PI) headdiff = headdiff + 2.0*M_PI;
  854. while(headdiff >= 2.0*M_PI)headdiff = headdiff - 2.0*M_PI;
  855. bool bSel = false;
  856. if((headdiff >=M_PI/2.0)&&(headdiff <= M_PI*3.0/2.0)) //if diff is
  857. {
  858. if(pRoad->GetLaneSection(0)->GetLeftLaneCount()>0)
  859. {
  860. nrtn = 1;
  861. bSel = true;
  862. }
  863. }
  864. else
  865. {
  866. if(pRoad->GetLaneSection(0)->GetRightLaneCount()>0)
  867. {
  868. nrtn = 2;
  869. bSel = true;
  870. }
  871. }
  872. if(bSel == false)
  873. {
  874. if(pRoad->GetLaneSection(0)->GetLeftLaneCount() >0)
  875. {
  876. nrtn = 1;
  877. }
  878. else
  879. {
  880. nrtn = 2;
  881. }
  882. }
  883. return nrtn;
  884. }
  885. //static double getlinereldis(GeometryLine * pline,double x,double y)
  886. //{
  887. // double x0,y0;
  888. // x0 = pline->GetX();
  889. // y0 = pline->GetY();
  890. // double dis = sqrt(pow(x-x0,2) + pow(y-y0,2));
  891. // if(dis > pline->GetS())
  892. // {
  893. // std::cout<<"getlinereldis exceed s S is "<<pline->GetS()<<" dis is "<<dis<<std::endl;
  894. // return dis;
  895. // }
  896. // return dis;
  897. //}
  898. //static double getarcreldis(GeometryArc * parc,double x,double y)
  899. //{
  900. // double x0,y0;
  901. // x0 = parc->GetX();
  902. // y0 = parc->GetY();
  903. // double dis = sqrt(pow(x-x0,2) + pow(y-y0,2));
  904. // double R = fabs(1.0/parc->GetCurvature());
  905. // double ang = 2.0* asin(dis/(2.0*R));
  906. // double frtn = ang * R;
  907. // return frtn;
  908. //}
  909. //static double getparampoly3reldis(GeometryParamPoly3 *parc, double xnow, double ynow)
  910. //{
  911. // double s = 0.1;
  912. // double xold,yold;
  913. // xold = parc->GetX();
  914. // yold = parc->GetY();
  915. // while(s < parc->GetLength())
  916. // {
  917. // double x, y,xtem,ytem;
  918. // xtem = parc->GetuA() + parc->GetuB() * s + parc->GetuC() * s*s + parc->GetuD() * s*s*s;
  919. // ytem = parc->GetvA() + parc->GetvB() * s + parc->GetvC() * s*s + parc->GetvD() * s*s*s;
  920. // x = xtem*cos(parc->GetHdg()) - ytem * sin(parc->GetHdg()) + parc->GetX();
  921. // y = xtem*sin(parc->GetHdg()) + ytem * cos(parc->GetHdg()) + parc->GetY();
  922. // if(sqrt(pow(xnow - x,2) + pow(ynow -y,2))<0.3)
  923. // {
  924. // break;
  925. // }
  926. // xold = x;
  927. // yold = y;
  928. // s = s+ 0.1;
  929. // }
  930. // return s;
  931. //}
  932. //static double getreldis(RoadGeometry * prg,double x,double y)
  933. //{
  934. // int ngeotype = prg->GetGeomType();
  935. // double frtn = 0;
  936. // switch (ngeotype) {
  937. // case 0:
  938. // frtn = getlinereldis((GeometryLine *)prg,x,y);
  939. // break;
  940. // case 1:
  941. // break;
  942. // case 2:
  943. // frtn = getarcreldis((GeometryArc *)prg,x,y);
  944. // break;
  945. // case 3:
  946. // break;
  947. // case 4:
  948. // frtn = getparampoly3reldis((GeometryParamPoly3 *)prg,x,y);
  949. // break;
  950. // default:
  951. // break;
  952. // }
  953. // return frtn;
  954. //}
  955. //static double getposinroad(Road * pRoad,GeometryBlock * pgeob,double x,double y)
  956. //{
  957. // RoadGeometry* prg = pgeob->GetGeometryAt(0);
  958. // double s1 = prg->GetS();
  959. // double srtn = s1;
  960. // return s1 + getreldis(prg,x,y);
  961. //}
  962. static std::vector<PlanPoint> getlinepoint(GeometryLine * pline,const double fspace = 0.1)
  963. {
  964. std::vector<PlanPoint> xvectorPP;
  965. int i;
  966. double s = pline->GetLength();
  967. int nsize = s/fspace;
  968. if(nsize ==0)nsize = 1;
  969. for(i=0;i<nsize;i++)
  970. {
  971. double x,y;
  972. x = pline->GetX() + i *fspace * cos(pline->GetHdg());
  973. y = pline->GetY() + i *fspace * sin(pline->GetHdg());
  974. PlanPoint pp;
  975. pp.x = x;
  976. pp.y = y;
  977. pp.dis = i*fspace;
  978. pp.hdg = pline->GetHdg();
  979. pp.mS = pline->GetS() + i*fspace;
  980. xvectorPP.push_back(pp);
  981. }
  982. return xvectorPP;
  983. }
  984. static std::vector<PlanPoint> getarcpoint(GeometryArc * parc,const double fspace = 0.1)
  985. {
  986. std::vector<PlanPoint> xvectorPP;
  987. // double fRtn = 1000.0;
  988. if(parc->GetCurvature() == 0.0)return xvectorPP;
  989. double R = fabs(1.0/parc->GetCurvature());
  990. //calculate arc center
  991. double x_center = parc->GetX() + (1.0/parc->GetCurvature()) * cos(parc->GetHdg() + M_PI/2.0);
  992. double y_center = parc->GetY() + (1.0/parc->GetCurvature()) * sin(parc->GetHdg()+ M_PI/2.0);
  993. double arcdiff = fspace/R;
  994. int nsize = parc->GetLength()/fspace;
  995. if(nsize == 0)nsize = 1;
  996. int i;
  997. for(i=0;i<nsize;i++)
  998. {
  999. double x,y;
  1000. PlanPoint pp;
  1001. if(parc->GetCurvature() > 0)
  1002. {
  1003. x = x_center + R * cos(parc->GetHdg() + i*arcdiff - M_PI/2.0);
  1004. y = y_center + R * sin(parc->GetHdg() + i*arcdiff - M_PI/2.0);
  1005. pp.hdg = parc->GetHdg() + i*arcdiff;
  1006. }
  1007. else
  1008. {
  1009. x = x_center + R * cos(parc->GetHdg() -i*arcdiff + M_PI/2.0);
  1010. y = y_center + R * sin(parc->GetHdg() -i*arcdiff + M_PI/2.0);
  1011. pp.hdg = parc->GetHdg() - i*arcdiff;
  1012. }
  1013. pp.x = x;
  1014. pp.y = y;
  1015. pp.dis = i*fspace;
  1016. pp.mS = parc->GetS() + i*fspace;
  1017. xvectorPP.push_back(pp);
  1018. }
  1019. return xvectorPP;
  1020. }
  1021. static std::vector<PlanPoint> getspiralpoint(GeometrySpiral * pspiral,const double fspace = 0.1)
  1022. {
  1023. double x,y,hdg;
  1024. double s = 0.0;
  1025. double s0 = pspiral->GetS();
  1026. std::vector<PlanPoint> xvectorPP;
  1027. PlanPoint pp;
  1028. while(s<pspiral->GetLength())
  1029. {
  1030. pspiral->GetCoords(s0+s,x,y,hdg);
  1031. pp.x = x;
  1032. pp.y = y;
  1033. pp.hdg = hdg;
  1034. pp.dis = s;
  1035. pp.mS = pspiral->GetS() + s;
  1036. xvectorPP.push_back(pp);
  1037. s = s+fspace;
  1038. }
  1039. return xvectorPP;
  1040. }
  1041. static std::vector<PlanPoint> getpoly3dpoint(GeometryPoly3 * ppoly,const double fspace = 0.1)
  1042. {
  1043. double x,y;
  1044. x = ppoly->GetX();
  1045. y = ppoly->GetY();
  1046. double A,B,C,D;
  1047. A = ppoly->GetA();
  1048. B = ppoly->GetB();
  1049. C = ppoly->GetC();
  1050. D = ppoly->GetD();
  1051. const double steplim = fspace;
  1052. double du = steplim;
  1053. double u = 0;
  1054. double v = 0;
  1055. double oldx,oldy;
  1056. oldx = x;
  1057. oldy = y;
  1058. double xstart,ystart;
  1059. xstart = x;
  1060. ystart = y;
  1061. double hdgstart = ppoly->GetHdg();
  1062. double flen = 0;
  1063. std::vector<PlanPoint> xvectorPP;
  1064. PlanPoint pp;
  1065. pp.x = xstart;
  1066. pp.y = ystart;
  1067. pp.hdg = hdgstart;
  1068. pp.dis = 0;
  1069. pp.mS = ppoly->GetS();
  1070. xvectorPP.push_back(pp);
  1071. u = du;
  1072. while(flen < ppoly->GetLength())
  1073. {
  1074. double fdis = 0;
  1075. v = A + B*u + C*u*u + D*u*u*u;
  1076. x = xstart + u*cos(hdgstart) - v*sin(hdgstart);
  1077. y = ystart + u*sin(hdgstart) + v*cos(hdgstart);
  1078. fdis = sqrt(pow(x- oldx,2)+pow(y-oldy,2));
  1079. double hdg = CalcHdg(QPointF(oldx,oldy),QPointF(x,y));
  1080. oldx = x;
  1081. oldy = y;
  1082. if(fdis>(steplim*2.0))du = du/2.0;
  1083. flen = flen + fdis;
  1084. u = u + du;
  1085. pp.x = x;
  1086. pp.y = y;
  1087. pp.hdg = hdg;
  1088. // s = s+ dt;
  1089. pp.dis = flen;;
  1090. pp.mS = ppoly->GetS() + flen;
  1091. xvectorPP.push_back(pp);
  1092. }
  1093. return xvectorPP;
  1094. }
  1095. static std::vector<PlanPoint> getparampoly3dpoint(GeometryParamPoly3 * parc,const double fspace = 0.1)
  1096. {
  1097. double s = 0.1;
  1098. std::vector<PlanPoint> xvectorPP;
  1099. PlanPoint pp;
  1100. double xold,yold;
  1101. xold = parc->GetX();
  1102. yold = parc->GetY();
  1103. double hdg0 = parc->GetHdg();
  1104. pp.x = xold;
  1105. pp.y = yold;
  1106. pp.hdg = hdg0;
  1107. pp.dis = 0;
  1108. // xvectorPP.push_back(pp);
  1109. double dt = 1.0;
  1110. double tcount = parc->GetLength()/0.1;
  1111. if(tcount > 0)
  1112. {
  1113. dt = 1.0/tcount;
  1114. }
  1115. double t;
  1116. s = dt;
  1117. s = 0;
  1118. double ua,ub,uc,ud,va,vb,vc,vd;
  1119. ua = parc->GetuA();ub= parc->GetuB();uc= parc->GetuC();ud = parc->GetuD();
  1120. va = parc->GetvA();vb= parc->GetvB();vc= parc->GetvC();vd = parc->GetvD();
  1121. double s_start = parc->GetS();
  1122. while(s < parc->GetLength())
  1123. {
  1124. double x, y,hdg;
  1125. parc->GetCoords(s_start+s,x,y,hdg);
  1126. pp.x = x;
  1127. pp.y = y;
  1128. pp.hdg = hdg;
  1129. s = s+ fspace;
  1130. pp.dis = pp.dis + fspace;;
  1131. pp.mS = s_start + s;
  1132. xvectorPP.push_back(pp);
  1133. }
  1134. return xvectorPP;
  1135. }
  1136. std::vector<PlanPoint> GetPoint(pathsection xpath,const double fspace = 0.1)
  1137. {
  1138. Road * pRoad = xpath.mpRoad;
  1139. //s_start s_end for select now section data.
  1140. double s_start,s_end;
  1141. LaneSection * pLS = pRoad->GetLaneSection(xpath.msectionid);
  1142. s_start = pLS->GetS();
  1143. if(xpath.msectionid == (pRoad->GetLaneSectionCount()-1))s_end = pRoad->GetRoadLength();
  1144. else
  1145. {
  1146. s_end = pRoad->GetLaneSection(xpath.msectionid+1)->GetS();
  1147. }
  1148. // if(xpath.mroadid == 10190)
  1149. // {
  1150. // int a = 1;
  1151. // a++;
  1152. // }
  1153. std::vector<PlanPoint> xvectorPPS;
  1154. double s = 0;
  1155. int i;
  1156. for(i=0;i<pRoad->GetGeometryBlockCount();i++)
  1157. {
  1158. GeometryBlock * pgb = pRoad->GetGeometryBlock(i);
  1159. RoadGeometry * prg = pgb->GetGeometryAt(0);
  1160. std::vector<PlanPoint> xvectorPP;
  1161. if(i<(pRoad->GetGeometryBlockCount() -0))
  1162. {
  1163. if(prg->GetS()>s_end)
  1164. {
  1165. continue;
  1166. }
  1167. if((prg->GetS() + prg->GetLength())<s_start)
  1168. {
  1169. continue;
  1170. }
  1171. }
  1172. double s1 = prg->GetLength();
  1173. switch (prg->GetGeomType()) {
  1174. case 0:
  1175. xvectorPP = getlinepoint((GeometryLine *)prg,fspace);
  1176. break;
  1177. case 1:
  1178. xvectorPP = getspiralpoint((GeometrySpiral *)prg,fspace);
  1179. break;
  1180. case 2:
  1181. xvectorPP = getarcpoint((GeometryArc *)prg,fspace);
  1182. break;
  1183. case 3:
  1184. xvectorPP = getpoly3dpoint((GeometryPoly3 *)prg,fspace);
  1185. break;
  1186. case 4:
  1187. xvectorPP = getparampoly3dpoint((GeometryParamPoly3 *)prg,fspace);
  1188. break;
  1189. default:
  1190. break;
  1191. }
  1192. int j;
  1193. if(prg->GetS()<s_start)
  1194. {
  1195. s1 = prg->GetLength() -(s_start - prg->GetS());
  1196. }
  1197. if((prg->GetS() + prg->GetLength())>s_end)
  1198. {
  1199. s1 = s_end - prg->GetS();
  1200. }
  1201. for(j=0;j<xvectorPP.size();j++)
  1202. {
  1203. PlanPoint pp = xvectorPP.at(j);
  1204. // double foffset = pRoad->GetLaneOffsetValue(pp.mS);
  1205. // if(fabs(foffset)>0.001)
  1206. // {
  1207. // pp.x = pp.x + foffset * cos(pp.hdg + M_PI/2.0);
  1208. // pp.y = pp.y + foffset * sin(pp.hdg + M_PI/2.0);
  1209. // }
  1210. pp.mfCurvature = pRoad->GetRoadCurvature(pp.mS);
  1211. if(((pp.dis+prg->GetS()) >= s_start) &&((pp.dis+prg->GetS()) <= s_end))
  1212. {
  1213. if(s_start > prg->GetS())
  1214. {
  1215. pp.dis = s + pp.dis -(s_start - prg->GetS());
  1216. }
  1217. else
  1218. {
  1219. pp.dis = s+ pp.dis;
  1220. }
  1221. pp.nRoadID = atoi(pRoad->GetRoadId().data());
  1222. xvectorPPS.push_back(pp);
  1223. }
  1224. // if((prg->GetS()>s_start)&&((prg->GetS() + prg->GetLength())<s_end))
  1225. // {
  1226. // pp.dis = s + pp.dis;
  1227. // pp.nRoadID = atoi(pRoad->GetRoadId().data());
  1228. // xvectorPPS.push_back(pp);
  1229. // }
  1230. // else
  1231. // {
  1232. // if(prg->GetS()<s_start)
  1233. // {
  1234. // }
  1235. // else
  1236. // {
  1237. // if(pp.dis<(s_end -prg->GetS()))
  1238. // {
  1239. // pp.dis = s + pp.dis;
  1240. // pp.nRoadID = atoi(pRoad->GetRoadId().data());
  1241. // xvectorPPS.push_back(pp);
  1242. // }
  1243. // }
  1244. // }
  1245. }
  1246. s = s+ s1;
  1247. }
  1248. return xvectorPPS;
  1249. }
  1250. std::vector<PlanPoint> GetPoint(Road * pRoad)
  1251. {
  1252. std::vector<PlanPoint> xvectorPPS;
  1253. double s = 0;
  1254. int i;
  1255. for(i=0;i<pRoad->GetGeometryBlockCount();i++)
  1256. {
  1257. GeometryBlock * pgb = pRoad->GetGeometryBlock(i);
  1258. RoadGeometry * prg = pgb->GetGeometryAt(0);
  1259. std::vector<PlanPoint> xvectorPP;
  1260. double s1 = prg->GetLength();
  1261. switch (prg->GetGeomType()) {
  1262. case 0:
  1263. xvectorPP = getlinepoint((GeometryLine *)prg);
  1264. break;
  1265. case 1:
  1266. xvectorPP = getspiralpoint((GeometrySpiral *)prg);
  1267. break;
  1268. case 2:
  1269. xvectorPP = getarcpoint((GeometryArc *)prg);
  1270. break;
  1271. case 3:
  1272. xvectorPP = getpoly3dpoint((GeometryPoly3 *)prg);
  1273. break;
  1274. case 4:
  1275. xvectorPP = getparampoly3dpoint((GeometryParamPoly3 *)prg);
  1276. break;
  1277. default:
  1278. break;
  1279. }
  1280. int j;
  1281. for(j=0;j<xvectorPP.size();j++)
  1282. {
  1283. PlanPoint pp = xvectorPP.at(j);
  1284. pp.dis = s + pp.dis;
  1285. pp.nRoadID = atoi(pRoad->GetRoadId().data());
  1286. xvectorPPS.push_back(pp);
  1287. }
  1288. s = s+ s1;
  1289. }
  1290. unsigned int j;
  1291. for(j=0;j<xvectorPPS.size();j++)
  1292. {
  1293. xvectorPPS.at(j).mfCurvature = pRoad->GetRoadCurvature(xvectorPPS.at(j).mS);
  1294. }
  1295. return xvectorPPS;
  1296. }
  1297. int indexinroadpoint(std::vector<PlanPoint> xvectorPP,double x,double y)
  1298. {
  1299. int nrtn = 0;
  1300. int i;
  1301. int dismin = 1000;
  1302. for(i=0;i<xvectorPP.size();i++)
  1303. {
  1304. double dis = sqrt(pow(xvectorPP.at(i).x - x,2) + pow(xvectorPP.at(i).y -y,2));
  1305. if(dis <dismin)
  1306. {
  1307. dismin = dis;
  1308. nrtn = i;
  1309. }
  1310. }
  1311. if(dismin > 10.0)
  1312. {
  1313. std::cout<<"indexinroadpoint near point is error. dis is "<<dismin<<std::endl;
  1314. }
  1315. return nrtn;
  1316. }
  1317. double getwidth(Road * pRoad, int nlane)
  1318. {
  1319. double frtn = 0;
  1320. int i;
  1321. int nlanecount = pRoad->GetLaneSection(0)->GetLaneCount();
  1322. for(i=0;i<nlanecount;i++)
  1323. {
  1324. if(nlane == pRoad->GetLaneSection(0)->GetLane(i)->GetId())
  1325. {
  1326. LaneWidth* pLW = pRoad->GetLaneSection(0)->GetLane(i)->GetLaneWidth(0);
  1327. if(pLW != 0)
  1328. {
  1329. frtn = pRoad->GetLaneSection(0)->GetLane(i)->GetLaneWidth(0)->GetA();
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. return frtn;
  1335. }
  1336. double getoff(Road * pRoad,int nlane)
  1337. {
  1338. double off = getwidth(pRoad,nlane)/2.0;
  1339. if(nlane < 0)off = off * (-1.0);
  1340. // int nlanecount = pRoad->GetLaneSection(0)->GetLaneCount();
  1341. int i;
  1342. if(nlane > 0)
  1343. off = off + getwidth(pRoad,0)/2.0;
  1344. else
  1345. off = off - getwidth(pRoad,0)/2.0;
  1346. if(nlane > 0)
  1347. {
  1348. for(i=1;i<nlane;i++)
  1349. {
  1350. off = off + getwidth(pRoad,i);
  1351. }
  1352. }
  1353. else
  1354. {
  1355. for(i=-1;i>nlane;i--)
  1356. {
  1357. off = off - getwidth(pRoad,i);
  1358. }
  1359. }
  1360. // return 0;
  1361. return off;
  1362. }
  1363. namespace iv {
  1364. struct lanewidthabcd
  1365. {
  1366. int nlane;
  1367. double A;
  1368. double B;
  1369. double C;
  1370. double D;
  1371. };
  1372. }
  1373. double getwidth(Road * pRoad, int nlane,std::vector<iv::lanewidthabcd> xvectorlwa,double s)
  1374. {
  1375. double frtn = 0;
  1376. int i;
  1377. int nlanecount = xvectorlwa.size();
  1378. for(i=0;i<nlanecount;i++)
  1379. {
  1380. if(nlane == xvectorlwa.at(i).nlane)
  1381. {
  1382. iv::lanewidthabcd * plwa = &xvectorlwa.at(i);
  1383. frtn = plwa->A + plwa->B*s + plwa->C *s*s + plwa->D *s*s*s;
  1384. break;
  1385. }
  1386. }
  1387. return frtn;
  1388. }
  1389. std::vector<iv::lanewidthabcd> GetLaneWidthABCD(Road * pRoad)
  1390. {
  1391. std::vector<iv::lanewidthabcd> xvectorlwa;
  1392. if(pRoad->GetLaneSectionCount()<1)return xvectorlwa;
  1393. int i;
  1394. LaneSection * pLS = pRoad->GetLaneSection(0);
  1395. // if(pRoad->GetLaneSectionCount() > 1)
  1396. // {
  1397. // for(i=0;i<(pRoad->GetLaneSectionCount()-1);i++)
  1398. // {
  1399. // if(s>pRoad->GetLaneSection(i+1)->GetS())
  1400. // {
  1401. // pLS = pRoad->GetLaneSection(i+1);
  1402. // }
  1403. // else
  1404. // {
  1405. // break;
  1406. // }
  1407. // }
  1408. // }
  1409. int nlanecount = pLS->GetLaneCount();
  1410. for(i=0;i<nlanecount;i++)
  1411. {
  1412. iv::lanewidthabcd xlwa;
  1413. LaneWidth* pLW = pLS->GetLane(i)->GetLaneWidth(0);
  1414. xlwa.nlane = pLS->GetLane(i)->GetId();
  1415. if(pLW != 0)
  1416. {
  1417. xlwa.A = pLW->GetA();
  1418. xlwa.B = pLW->GetB();
  1419. xlwa.C = pLW->GetC();
  1420. xlwa.D = pLW->GetD();
  1421. }
  1422. else
  1423. {
  1424. xlwa.A = 0;
  1425. xlwa.B = 0;
  1426. xlwa.C = 0;
  1427. xlwa.D = 0;
  1428. }
  1429. // if(strncmp(pLS->GetLane(i)->GetType().data(),"driving",256) == 0)
  1430. xvectorlwa.push_back(xlwa); //Calculate Road Width, not driving need add in.
  1431. }
  1432. return xvectorlwa;
  1433. }
  1434. inline double getoff(int nlane,double s,std::vector<iv::lanewidthabcd> xvectorLWA,std::vector<int> xvectorIndex)
  1435. {
  1436. int i;
  1437. double off = 0;
  1438. double a,b,c,d;
  1439. if(xvectorIndex.size() == 0)return off;
  1440. for(i=0;i<(xvectorIndex.size()-1);i++)
  1441. {
  1442. a = xvectorLWA[xvectorIndex[i]].A;
  1443. b = xvectorLWA[xvectorIndex[i]].B;
  1444. c = xvectorLWA[xvectorIndex[i]].C;
  1445. d = xvectorLWA[xvectorIndex[i]].D;
  1446. if(xvectorLWA[xvectorIndex[i]].nlane != 0)
  1447. {
  1448. off = off + a + b*s + c*s*s + d*s*s*s;
  1449. }
  1450. else
  1451. {
  1452. off = off + (a + b*s + c*s*s + d*s*s*s)/2.0;
  1453. }
  1454. }
  1455. i = xvectorIndex.size()-1;
  1456. a = xvectorLWA[xvectorIndex[i]].A;
  1457. b = xvectorLWA[xvectorIndex[i]].B;
  1458. c = xvectorLWA[xvectorIndex[i]].C;
  1459. d = xvectorLWA[xvectorIndex[i]].D;
  1460. off = off + (a + b*s + c*s*s + d*s*s*s)/2.0;
  1461. if(nlane < 0) off = off*(-1.0);
  1462. // std::cout<<"s is "<<s<<std::endl;
  1463. // std::cout<<" off is "<<off<<std::endl;
  1464. return off;
  1465. }
  1466. double GetRoadWidth(std::vector<iv::lanewidthabcd> xvectorLWA,int nlane,double s)
  1467. {
  1468. double fwidth = 0;
  1469. int i;
  1470. double a,b,c,d;
  1471. int nsize = xvectorLWA.size();
  1472. for(i=0;i<nsize;i++)
  1473. {
  1474. if(nlane*(xvectorLWA[i].nlane)>0)
  1475. {
  1476. a = xvectorLWA[i].A;
  1477. b = xvectorLWA[i].B;
  1478. c = xvectorLWA[i].C;
  1479. d = xvectorLWA[i].D;
  1480. fwidth = fwidth + a + b*s +c*s*s + d*s*s*s;
  1481. }
  1482. }
  1483. return fwidth;
  1484. }
  1485. int GetLaneOriTotal(Road * pRoad, int nlane,double s,int & nori, int & ntotal,double & fSpeed,bool & bNoavoid)
  1486. {
  1487. if(pRoad->GetLaneSectionCount() < 1)return -1;
  1488. LaneSection * pLS = pRoad->GetLaneSection(0);
  1489. int i;
  1490. if(pRoad->GetLaneSectionCount() > 1)
  1491. {
  1492. for(i=0;i<(pRoad->GetLaneSectionCount()-1);i++)
  1493. {
  1494. if(s>pRoad->GetLaneSection(i+1)->GetS())
  1495. {
  1496. pLS = pRoad->GetLaneSection(i+1);
  1497. }
  1498. else
  1499. {
  1500. break;
  1501. }
  1502. }
  1503. }
  1504. nori = 0;
  1505. ntotal = 0;
  1506. fSpeed = -1; //if -1 no speedlimit for map
  1507. pRoad->GetRoadSpeedMax(s,fSpeed); //Get Road Speed Limit.
  1508. pRoad->GetRoadNoavoid(s,bNoavoid);
  1509. int nlanecount = pLS->GetLaneCount();
  1510. for(i=0;i<nlanecount;i++)
  1511. {
  1512. int nlaneid = pLS->GetLane(i)->GetId();
  1513. if(nlaneid == nlane)
  1514. {
  1515. Lane * pLane = pLS->GetLane(i);
  1516. if(pLane->GetLaneSpeedCount() > 0)
  1517. {
  1518. LaneSpeed * pLSpeed = pLane->GetLaneSpeed(0);
  1519. int j;
  1520. int nspeedcount = pLane->GetLaneSpeedCount();
  1521. for(j=0;j<(nspeedcount -1);j++)
  1522. {
  1523. if((s-pLS->GetS())>=pLane->GetLaneSpeed(j+1)->GetS())
  1524. {
  1525. pLSpeed = pLane->GetLaneSpeed(j+1);
  1526. }
  1527. else
  1528. {
  1529. break;
  1530. }
  1531. }
  1532. if(pLSpeed->GetS()<=(s-pLS->GetS()))fSpeed = pLSpeed->GetMax();
  1533. }
  1534. }
  1535. if(nlane<0)
  1536. {
  1537. if(nlaneid < 0)
  1538. {
  1539. if(strncmp(pLS->GetLane(i)->GetType().data(),"driving",256) == 0)
  1540. {
  1541. ntotal++;
  1542. if(nlaneid<nlane)nori++;
  1543. }
  1544. }
  1545. }
  1546. else
  1547. {
  1548. if(nlaneid > 0)
  1549. {
  1550. if(strncmp(pLS->GetLane(i)->GetType().data(),"driving",256) == 0)
  1551. {
  1552. ntotal++;
  1553. if(nlaneid > nlane)nori++;
  1554. }
  1555. }
  1556. }
  1557. }
  1558. return 0;
  1559. }
  1560. std::vector<int> GetLWIndex(std::vector<iv::lanewidthabcd> xvectorLWA,int nlane)
  1561. {
  1562. std::vector<int> xvectorindex;
  1563. int i;
  1564. if(nlane >= 0)
  1565. {
  1566. for(i=0;i<=nlane;i++)
  1567. {
  1568. int j;
  1569. int nsize = xvectorLWA.size();
  1570. for(j=0;j<nsize;j++)
  1571. {
  1572. if(i == xvectorLWA.at(j).nlane )
  1573. {
  1574. xvectorindex.push_back(j);
  1575. break;
  1576. }
  1577. }
  1578. }
  1579. }
  1580. else
  1581. {
  1582. for(i=0;i>=nlane;i--)
  1583. {
  1584. int j;
  1585. int nsize = xvectorLWA.size();
  1586. for(j=0;j<nsize;j++)
  1587. {
  1588. if(i == xvectorLWA.at(j).nlane )
  1589. {
  1590. xvectorindex.push_back(j);
  1591. break;
  1592. }
  1593. }
  1594. }
  1595. }
  1596. return xvectorindex;
  1597. }
  1598. void CalcBorringRoad(pathsection xps,std::vector<PlanPoint> & xvectorPP)
  1599. {
  1600. if(xps.mpRoad->GetRoadBorrowCount() == 0)
  1601. {
  1602. return;
  1603. }
  1604. Road * pRoad = xps.mpRoad;
  1605. unsigned int nborrowsize = pRoad->GetRoadBorrowCount();
  1606. unsigned int i;
  1607. unsigned int nPPCount = xvectorPP.size();
  1608. for(i=0;i<nborrowsize;i++)
  1609. {
  1610. RoadBorrow * pborrow = pRoad->GetRoadBorrow(i);
  1611. if(pborrow == NULL)
  1612. {
  1613. std::cout<<"warning:can't get borrow"<<std::endl;
  1614. continue;
  1615. }
  1616. if((pborrow->GetMode() == "All")||((pborrow->GetMode()=="R2L")&&(xps.mainsel<0))||((pborrow->GetMode()=="L2R")&&(xps.mainsel>0)))
  1617. {
  1618. unsigned int j;
  1619. double soffset = pborrow->GetS();
  1620. double borrowlen = pborrow->GetLength();
  1621. for(j=0;j<xvectorPP.size();j++)
  1622. {
  1623. if((xvectorPP[j].mS>=soffset)&&(xvectorPP[j].mS<=(soffset + borrowlen)))
  1624. {
  1625. xvectorPP[j].mbBoringRoad = true;
  1626. }
  1627. }
  1628. }
  1629. }
  1630. }
  1631. void CalcInLaneAvoid(pathsection xps,std::vector<PlanPoint> & xvectorPP,const double fvehiclewidth,
  1632. const int nchang1,const int nchang2,const int nchangpoint)
  1633. {
  1634. if(xvectorPP.size()<2)return;
  1635. bool bInlaneavoid = false;
  1636. int i;
  1637. if((xps.mbStartToMainChange == false) && (xps.mbMainToEndChange == false))
  1638. {
  1639. if(xps.mpRoad->GetRoadLength()<30)
  1640. {
  1641. double hdgdiff = xvectorPP.at(xvectorPP.size()-1).hdg - xvectorPP.at(0).hdg;
  1642. if(hdgdiff<0)hdgdiff = hdgdiff + 2.0*M_PI;
  1643. if(hdgdiff>(M_PI/3.0)&&(hdgdiff<(5.0*M_PI/3.0)))
  1644. bInlaneavoid = false;
  1645. else
  1646. bInlaneavoid = true;
  1647. }
  1648. else
  1649. {
  1650. bInlaneavoid = true;
  1651. }
  1652. }
  1653. else
  1654. {
  1655. if(xps.mpRoad->GetRoadLength()>100)bInlaneavoid = true;
  1656. }
  1657. if(bInlaneavoid == false)
  1658. {
  1659. int nvpsize = xvectorPP.size();
  1660. for(i=0;i<nvpsize;i++)
  1661. {
  1662. xvectorPP.at(i).bInlaneAvoid = false;
  1663. xvectorPP.at(i).mx_left = xvectorPP.at(i).x;
  1664. xvectorPP.at(i).my_left = xvectorPP.at(i).y;
  1665. xvectorPP.at(i).mx_right = xvectorPP.at(i).x;
  1666. xvectorPP.at(i).my_right = xvectorPP.at(i).y;
  1667. }
  1668. }
  1669. else
  1670. {
  1671. int nvpsize = xvectorPP.size();
  1672. for(i=0;i<nvpsize;i++)xvectorPP.at(i).bInlaneAvoid = true;
  1673. if((xps.mbStartToMainChange == true)||(xps.mbMainToEndChange == true))
  1674. {
  1675. if(xps.mbStartToMainChange == true)
  1676. {
  1677. for(i=(nchang1 - nchangpoint/2);i<(nchang1 + nchangpoint/2);i++)
  1678. {
  1679. if((i>=0)&&(i<nvpsize))
  1680. xvectorPP.at(i).bInlaneAvoid = false;
  1681. }
  1682. }
  1683. if(xps.mbMainToEndChange)
  1684. {
  1685. for(i=(nchang2 - nchangpoint/2);i<(nchang2 + nchangpoint/2);i++)
  1686. {
  1687. if((i>=0)&&(i<nvpsize))
  1688. xvectorPP.at(i).bInlaneAvoid = false;
  1689. }
  1690. }
  1691. }
  1692. for(i=0;i<nvpsize;i++)
  1693. {
  1694. if(xvectorPP.at(i).bInlaneAvoid)
  1695. {
  1696. double foff = xvectorPP.at(i).mfDisToLaneLeft -0.3-fvehiclewidth*0.5; //30cm + 0.5vehcilewidth
  1697. if(foff < 0.3)
  1698. {
  1699. xvectorPP.at(i).bInlaneAvoid = false;
  1700. xvectorPP.at(i).mx_left = xvectorPP.at(i).x;
  1701. xvectorPP.at(i).my_left = xvectorPP.at(i).y;
  1702. xvectorPP.at(i).mx_right = xvectorPP.at(i).x;
  1703. xvectorPP.at(i).my_right = xvectorPP.at(i).y;
  1704. }
  1705. else
  1706. {
  1707. xvectorPP.at(i).mx_left = xvectorPP.at(i).x + foff * cos(xvectorPP.at(i).hdg + M_PI/2.0);
  1708. xvectorPP.at(i).my_left = xvectorPP.at(i).y + foff * sin(xvectorPP.at(i).hdg + M_PI/2.0);
  1709. xvectorPP.at(i).mx_right = xvectorPP.at(i).x + foff * cos(xvectorPP.at(i).hdg - M_PI/2.0);
  1710. xvectorPP.at(i).my_right = xvectorPP.at(i).y + foff * sin(xvectorPP.at(i).hdg - M_PI/2.0);
  1711. }
  1712. }
  1713. }
  1714. }
  1715. }
  1716. double getoff(Road * pRoad,int nlane,const double s)
  1717. {
  1718. int i;
  1719. int nLSCount = pRoad->GetLaneSectionCount();
  1720. double s_section = 0;
  1721. double foff = 0;
  1722. for(i=0;i<nLSCount;i++)
  1723. {
  1724. LaneSection * pLS = pRoad->GetLaneSection(i);
  1725. if(i<(nLSCount -1))
  1726. {
  1727. if(pRoad->GetLaneSection(i+1)->GetS()<s)
  1728. {
  1729. continue;
  1730. }
  1731. }
  1732. s_section = pLS->GetS();
  1733. int nlanecount = pLS->GetLaneCount();
  1734. int j;
  1735. for(j=0;j<nlanecount;j++)
  1736. {
  1737. Lane * pLane = pLS->GetLane(j);
  1738. int k;
  1739. double s_lane = s-s_section;
  1740. if(pLane->GetId() != 0)
  1741. {
  1742. for(k=0;k<pLane->GetLaneWidthCount();k++)
  1743. {
  1744. if(k<(pLane->GetLaneWidthCount()-1))
  1745. {
  1746. if((pLane->GetLaneWidth(k+1)->GetS()+s_section)<s)
  1747. {
  1748. continue;
  1749. }
  1750. }
  1751. s_lane = pLane->GetLaneWidth(k)->GetS();
  1752. break;
  1753. }
  1754. LaneWidth * pLW = pLane->GetLaneWidth(k);
  1755. if(pLW == 0)
  1756. {
  1757. std::cout<<"not find LaneWidth"<<std::endl;
  1758. break;
  1759. }
  1760. double fds = s - s_lane - s_section;
  1761. double fwidth= pLW->GetA() + pLW->GetB() * fds
  1762. +pLW->GetC() * pow(fds,2) + pLW->GetD() * pow(fds,3);
  1763. // if((pRoad->GetRoadId() == "211210") &&(nlane == -1) &&(pLane->GetId() == -1))
  1764. // {
  1765. // qDebug("fs is %f width is %f",fds,fwidth);
  1766. // }
  1767. if(nlane == pLane->GetId())
  1768. {
  1769. foff = foff + fwidth/2.0;
  1770. }
  1771. if((nlane*(pLane->GetId())>0)&&(abs(nlane)>abs(pLane->GetId())))
  1772. {
  1773. foff = foff + fwidth;
  1774. }
  1775. }
  1776. }
  1777. break;
  1778. }
  1779. if(pRoad->GetLaneOffsetCount()>0)
  1780. {
  1781. int nLOCount = pRoad->GetLaneOffsetCount();
  1782. int isel = -1;
  1783. for(i=0;i<(nLOCount-1);i++)
  1784. {
  1785. if(pRoad->GetLaneOffset(i+1)->GetS()>s)
  1786. {
  1787. isel = i;
  1788. break;
  1789. }
  1790. }
  1791. if(isel < 0)isel = nLOCount-1;
  1792. LaneOffset * pLO = pRoad->GetLaneOffset(isel);
  1793. double s_off = s - pLO->GetS();
  1794. double off1 = pLO->Geta() + pLO->Getb()*s_off + pLO->Getc() * s_off * s_off
  1795. +pLO->Getd() * s_off * s_off * s_off;
  1796. foff = foff - off1;
  1797. }
  1798. if(nlane<0)foff = foff*(-1);
  1799. return foff;
  1800. }
  1801. std::vector<PlanPoint> GetLanePoint(pathsection xps,std::vector<PlanPoint> xvPP,const double fvehiclewidth)
  1802. {
  1803. std::vector<PlanPoint> xvectorPP;
  1804. std::vector<iv::lanewidthabcd> xvectorLWA = GetLaneWidthABCD(xps.mpRoad);
  1805. std::vector<int> xvectorindex1,xvectorindex2,xvectorindex3;
  1806. int nchange1,nchange2;
  1807. int nlane1,nlane2,nlane3;
  1808. if(xps.mnStartLaneSel == xps.mnEndLaneSel)
  1809. {
  1810. xps.mainsel = xps.mnStartLaneSel;
  1811. xps.mbStartToMainChange = false;
  1812. xps.mbMainToEndChange = false;
  1813. }
  1814. else
  1815. {
  1816. if(xps.mpRoad->GetRoadLength() < 100)
  1817. {
  1818. xps.mainsel = xps.mnEndLaneSel;
  1819. xps.mbStartToMainChange = true;
  1820. xps.mbMainToEndChange = false;
  1821. }
  1822. }
  1823. double off1,off2,off3;
  1824. if(xps.mnStartLaneSel < 0)
  1825. {
  1826. off1 = getoff(xps.mpRoad,xps.mnStartLaneSel);
  1827. off2 = getoff(xps.mpRoad,xps.mainsel);
  1828. off3 = getoff(xps.mpRoad,xps.mnEndLaneSel);
  1829. xvectorindex1 = GetLWIndex(xvectorLWA,xps.mnStartLaneSel);
  1830. xvectorindex2 = GetLWIndex(xvectorLWA,xps.mainsel);
  1831. xvectorindex3 = GetLWIndex(xvectorLWA,xps.mnEndLaneSel);
  1832. nlane1 = xps.mnStartLaneSel;
  1833. nlane2 = xps.mainsel;
  1834. nlane3 = xps.mnEndLaneSel;
  1835. }
  1836. else
  1837. {
  1838. off3 = getoff(xps.mpRoad,xps.mnStartLaneSel);
  1839. off2 = getoff(xps.mpRoad,xps.mainsel);
  1840. off1 = getoff(xps.mpRoad,xps.mnEndLaneSel);
  1841. xvectorindex3 = GetLWIndex(xvectorLWA,xps.mnStartLaneSel);
  1842. xvectorindex2 = GetLWIndex(xvectorLWA,xps.mainsel);
  1843. xvectorindex1 = GetLWIndex(xvectorLWA,xps.mnEndLaneSel);
  1844. nlane3 = xps.mnStartLaneSel;
  1845. nlane2 = xps.mainsel;
  1846. nlane1 = xps.mnEndLaneSel;
  1847. }
  1848. int nchangepoint = 300;
  1849. if((nchangepoint * 3) > xvPP.size())
  1850. {
  1851. nchangepoint = xvPP.size()/3;
  1852. }
  1853. int nsize = xvPP.size();
  1854. nchange1 = nsize/3;
  1855. if(nchange1>500)nchange1 = 500;
  1856. nchange2 = nsize*2/3;
  1857. if( (nsize-nchange2)>500)nchange2 = nsize-500;
  1858. // if(nsize < 1000)
  1859. // {
  1860. // std::cout<<"GetLanePoint Error. road id is "<<xps.mpRoad->GetRoadId()<<std::endl;
  1861. // return xvectorPP;
  1862. // }
  1863. double x,y;
  1864. int i;
  1865. if(xps.mainsel < 0)
  1866. {
  1867. for(i=0;i<nsize;i++)
  1868. {
  1869. PlanPoint pp = xvPP.at(i);
  1870. // off1 = getoff(nlane1,pp.mS,xvectorLWA,xvectorindex1);
  1871. off1 = getoff(xps.mpRoad,nlane2,pp.mS);
  1872. pp.x = pp.x + off1 *cos(pp.hdg + M_PI/2.0);
  1873. pp.y = pp.y + off1 *sin(pp.hdg + M_PI/2.0);
  1874. pp.nlrchange = 1;
  1875. if(xps.mainsel != xps.secondsel)
  1876. {
  1877. off1 = getoff(xps.mpRoad,xps.secondsel,pp.mS);
  1878. pp.mfSecx = xvPP.at(i).x + off1 *cos(pp.hdg + M_PI/2.0);
  1879. pp.mfSecy = xvPP.at(i).y + off1 *sin(pp.hdg + M_PI/2.0);
  1880. if(xps.mainsel >xps.secondsel)
  1881. {
  1882. pp.nlrchange = 1;
  1883. }
  1884. else
  1885. {
  1886. pp.nlrchange = 2;
  1887. }
  1888. }
  1889. else
  1890. {
  1891. pp.mfSecx = pp.x ;
  1892. pp.mfSecy = pp.y ;
  1893. }
  1894. pp.mWidth = getwidth(xps.mpRoad,xps.mainsel,xvectorLWA,pp.mS);
  1895. pp.mfDisToLaneLeft = pp.mWidth/2.0;
  1896. pp.lanmp = 0;
  1897. pp.mfDisToRoadLeft = off1*(-1);
  1898. pp.mfRoadWidth = GetRoadWidth(xvectorLWA,xps.mainsel,pp.mS);
  1899. GetLaneOriTotal(xps.mpRoad,xps.mainsel,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed,pp.mbNoavoid);
  1900. xvectorPP.push_back(pp);
  1901. }
  1902. }
  1903. else
  1904. {
  1905. for(i=0;i<nsize;i++)
  1906. {
  1907. PlanPoint pp = xvPP.at(i);
  1908. // off1 = getoff(nlane1,pp.mS,xvectorLWA,xvectorindex1);
  1909. off1 = getoff(xps.mpRoad,nlane2,pp.mS);
  1910. pp.x = pp.x + off1 *cos(pp.hdg + M_PI/2.0);
  1911. pp.y = pp.y + off1 *sin(pp.hdg + M_PI/2.0);
  1912. pp.nlrchange = 1;
  1913. if(xps.mainsel != xps.secondsel)
  1914. {
  1915. off1 = getoff(xps.mpRoad,xps.secondsel,pp.mS);
  1916. pp.mfSecx = xvPP.at(i).x + off1 *cos(pp.hdg + M_PI/2.0);
  1917. pp.mfSecy = xvPP.at(i).y + off1 *sin(pp.hdg + M_PI/2.0);
  1918. if(xps.mainsel >xps.secondsel)
  1919. {
  1920. pp.nlrchange = 2;
  1921. }
  1922. else
  1923. {
  1924. pp.nlrchange = 1;
  1925. }
  1926. }
  1927. else
  1928. {
  1929. pp.mfSecx = pp.x ;
  1930. pp.mfSecy = pp.y ;
  1931. }
  1932. pp.mWidth = getwidth(xps.mpRoad,xps.mainsel,xvectorLWA,pp.mS);
  1933. pp.mfDisToLaneLeft = pp.mWidth/2.0;
  1934. pp.lanmp = 0;
  1935. pp.mfDisToRoadLeft = off1;
  1936. pp.mfRoadWidth = GetRoadWidth(xvectorLWA,xps.mainsel,pp.mS);
  1937. GetLaneOriTotal(xps.mpRoad,xps.mainsel,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed,pp.mbNoavoid);
  1938. pp.hdg = pp.hdg + M_PI;
  1939. xvectorPP.push_back(pp);
  1940. }
  1941. // for(i=0;i<(nchange1- nchangepoint/2);i++)
  1942. // {
  1943. // PlanPoint pp = xvPP.at(i);
  1944. // off1 = getoff(nlane1,pp.mS,xvectorLWA,xvectorindex1);
  1945. // pp.x = pp.x + off1 *cos(pp.hdg + M_PI/2.0);
  1946. // pp.y = pp.y + off1 *sin(pp.hdg + M_PI/2.0);
  1947. // pp.hdg = pp.hdg + M_PI;
  1948. // pp.mWidth = getwidth(xps.mpRoad,nlane1,xvectorLWA,pp.mS);
  1949. // pp.mfDisToLaneLeft = pp.mWidth/2.0;
  1950. // pp.lanmp = 0;
  1951. // pp.mfDisToRoadLeft = off1;
  1952. // pp.mfRoadWidth = GetRoadWidth(xvectorLWA,nlane1,pp.mS);
  1953. // GetLaneOriTotal(xps.mpRoad,nlane1,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  1954. // xvectorPP.push_back(pp);
  1955. // }
  1956. // for(i=(nchange1 - nchangepoint/2);i<(nchange1+nchangepoint/2);i++)
  1957. // {
  1958. // PlanPoint pp = xvPP.at(i);
  1959. // off1 = getoff(nlane1,pp.mS,xvectorLWA,xvectorindex1);
  1960. // off2 = getoff(nlane2,pp.mS,xvectorLWA,xvectorindex2);
  1961. // double offx = off1 + (off2 - off1) *(i-nchange1 + nchangepoint/2)/nchangepoint;
  1962. // pp.x = pp.x + offx *cos(pp.hdg + M_PI/2.0);
  1963. // pp.y = pp.y + offx *sin(pp.hdg + M_PI/2.0);
  1964. // pp.hdg = pp.hdg + M_PI;
  1965. // double flanewidth1 = getwidth(xps.mpRoad,nlane1,xvectorLWA,pp.mS);
  1966. // pp.mfDisToRoadLeft = offx;
  1967. // pp.mfRoadWidth = GetRoadWidth(xvectorLWA,nlane1,pp.mS);
  1968. // if(nlane1 == nlane2)
  1969. // {
  1970. // pp.mWidth = flanewidth1;
  1971. // pp.mfDisToLaneLeft = pp.mWidth/2.0;
  1972. // pp.lanmp = 0;
  1973. // GetLaneOriTotal(xps.mpRoad,nlane1,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  1974. // }
  1975. // else
  1976. // {
  1977. // if(nlane1 < nlane2)
  1978. // {
  1979. // pp.lanmp = -1;
  1980. // }
  1981. // else
  1982. // {
  1983. // pp.lanmp = 1;
  1984. // }
  1985. // if(i<nchange1)
  1986. // {
  1987. // pp.mWidth = getwidth(xps.mpRoad,nlane1,xvectorLWA,pp.mS);
  1988. // double fmove = pp.mWidth * (i-(nchange1 - nchangepoint/2))/nchangepoint;
  1989. // if(nlane1<nlane2)pp.mfDisToLaneLeft = pp.mWidth/2.0 - fmove;
  1990. // else pp.mfDisToLaneLeft = pp.mWidth/2.0 + fmove;
  1991. // GetLaneOriTotal(xps.mpRoad,nlane1,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  1992. // }
  1993. // else
  1994. // {
  1995. // pp.mWidth = getwidth(xps.mpRoad,nlane2,xvectorLWA,pp.mS);
  1996. // double fmove = pp.mWidth * (nchange1+nchangepoint/2 -i)/nchangepoint;
  1997. // if(nlane1<nlane2)pp.mfDisToLaneLeft = pp.mWidth/2.0 + fmove;
  1998. // else pp.mfDisToLaneLeft = pp.mWidth/2.0 - fmove;
  1999. // GetLaneOriTotal(xps.mpRoad,nlane2,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  2000. // }
  2001. // }
  2002. // xvectorPP.push_back(pp);
  2003. // }
  2004. // for(i=(nchange1 + nchangepoint/2);i<(nchange2-nchangepoint/2);i++)
  2005. // {
  2006. // PlanPoint pp = xvPP.at(i);
  2007. // off2 = getoff(nlane2,pp.mS,xvectorLWA,xvectorindex2);
  2008. // pp.x = pp.x + off2 *cos(pp.hdg + M_PI/2.0);
  2009. // pp.y = pp.y + off2 *sin(pp.hdg + M_PI/2.0);
  2010. // pp.hdg = pp.hdg + M_PI;
  2011. // pp.mWidth = getwidth(xps.mpRoad,nlane2,xvectorLWA,pp.mS);
  2012. // pp.mfDisToLaneLeft = pp.mWidth/2.0;
  2013. // pp.lanmp = 0;
  2014. // pp.mfDisToRoadLeft = off2;
  2015. // pp.mfRoadWidth = GetRoadWidth(xvectorLWA,nlane2,pp.mS);
  2016. // GetLaneOriTotal(xps.mpRoad,nlane2,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  2017. // xvectorPP.push_back(pp);
  2018. // }
  2019. // for(i=(nchange2 - nchangepoint/2);i<(nchange2+nchangepoint/2);i++)
  2020. // {
  2021. // PlanPoint pp = xvPP.at(i);
  2022. // off2 = getoff(nlane2,pp.mS,xvectorLWA,xvectorindex2);
  2023. // off3 = getoff(nlane3,pp.mS,xvectorLWA,xvectorindex3);
  2024. // double offx = off2 + (off3 - off2) *(i-nchange2 + nchangepoint/2)/nchangepoint;
  2025. // pp.x = pp.x + offx *cos(pp.hdg + M_PI/2.0);
  2026. // pp.y = pp.y + offx *sin(pp.hdg + M_PI/2.0);
  2027. // pp.hdg = pp.hdg + M_PI;
  2028. // double flanewidth1 = getwidth(xps.mpRoad,nlane2,xvectorLWA,pp.mS);
  2029. // pp.mfDisToRoadLeft = offx;
  2030. // pp.mfRoadWidth = GetRoadWidth(xvectorLWA,nlane2,pp.mS);
  2031. // if(nlane2 == nlane3)
  2032. // {
  2033. // pp.mWidth = flanewidth1;
  2034. // pp.mfDisToLaneLeft = pp.mWidth/2.0;
  2035. // pp.lanmp = 0;
  2036. // GetLaneOriTotal(xps.mpRoad,nlane2,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  2037. // }
  2038. // else
  2039. // {
  2040. // if(nlane2 < nlane3)
  2041. // {
  2042. // pp.lanmp = -1;
  2043. // }
  2044. // else
  2045. // {
  2046. // pp.lanmp = 1;
  2047. // }
  2048. // if(i<nchange2)
  2049. // {
  2050. // pp.mWidth = getwidth(xps.mpRoad,nlane2,xvectorLWA,pp.mS);
  2051. // double fmove = pp.mWidth * (i-(nchange2 - nchangepoint/2))/nchangepoint;
  2052. // if(nlane2<nlane3)pp.mfDisToLaneLeft = pp.mWidth/2.0 - fmove;
  2053. // else pp.mfDisToLaneLeft = pp.mWidth/2.0 + fmove;
  2054. // GetLaneOriTotal(xps.mpRoad,nlane2,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  2055. // }
  2056. // else
  2057. // {
  2058. // pp.mWidth = getwidth(xps.mpRoad,nlane3,xvectorLWA,pp.mS);
  2059. // double fmove = pp.mWidth * (nchange2+nchangepoint/2 -i)/nchangepoint;
  2060. // if(nlane2<nlane3)pp.mfDisToLaneLeft = pp.mWidth/2.0 + fmove;
  2061. // else pp.mfDisToLaneLeft = pp.mWidth/2.0 - fmove;
  2062. // GetLaneOriTotal(xps.mpRoad,nlane3,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  2063. // }
  2064. // }
  2065. // xvectorPP.push_back(pp);
  2066. // }
  2067. // for(i=(nchange2+ nchangepoint/2);i<nsize;i++)
  2068. // {
  2069. // PlanPoint pp = xvPP.at(i);
  2070. // off3 = getoff(nlane3,pp.mS,xvectorLWA,xvectorindex3);
  2071. // pp.x = pp.x + off3 *cos(pp.hdg + M_PI/2.0);
  2072. // pp.y = pp.y + off3 *sin(pp.hdg + M_PI/2.0);
  2073. // pp.hdg = pp.hdg + M_PI;
  2074. // pp.mWidth = getwidth(xps.mpRoad,nlane3,xvectorLWA,pp.mS);
  2075. // pp.mfDisToLaneLeft = pp.mWidth/2.0;
  2076. // pp.lanmp = 0;
  2077. // pp.mfDisToRoadLeft = off3;
  2078. // pp.mfRoadWidth = GetRoadWidth(xvectorLWA,nlane3,pp.mS);
  2079. // GetLaneOriTotal(xps.mpRoad,nlane3,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed);
  2080. // xvectorPP.push_back(pp);
  2081. // }
  2082. }
  2083. CalcInLaneAvoid(xps,xvectorPP,fvehiclewidth,nchange1,nchange2,nchangepoint);
  2084. if(xps.mpRoad->GetRoadBorrowCount()>0)
  2085. {
  2086. CalcBorringRoad(xps,xvectorPP);
  2087. }
  2088. if(xps.mnStartLaneSel > 0)
  2089. {
  2090. std::vector<PlanPoint> xvvPP;
  2091. int nvsize = xvectorPP.size();
  2092. for(i=0;i<nvsize;i++)
  2093. {
  2094. xvvPP.push_back(xvectorPP.at(nvsize-1-i));
  2095. }
  2096. AddSignalMark(xps.mpRoad,xps.mainsel,xvvPP);
  2097. return xvvPP;
  2098. }
  2099. // pRoad->GetLaneSection(xps.msectionid)
  2100. AddSignalMark(xps.mpRoad,xps.mainsel,xvectorPP);
  2101. return xvectorPP;
  2102. }
  2103. static void AddSignalMark(Road * pRoad, int nlane,std::vector<PlanPoint> & xvectorPP)
  2104. {
  2105. if(pRoad->GetSignalCount() == 0)return;
  2106. int nsigcount = pRoad->GetSignalCount();
  2107. int i;
  2108. for(i=0;i<nsigcount;i++)
  2109. {
  2110. Signal * pSig = pRoad->GetSignal(i);
  2111. int nfromlane = -100;
  2112. int ntolane = 100;
  2113. signal_laneValidity * pSig_laneValidity = pSig->GetlaneValidity();
  2114. if(pSig_laneValidity != 0)
  2115. {
  2116. nfromlane = pSig_laneValidity->GetfromLane();
  2117. ntolane = pSig_laneValidity->GettoLane();
  2118. }
  2119. if((nlane < 0)&&(nfromlane >= 0))
  2120. {
  2121. continue;
  2122. }
  2123. if((nlane > 0)&&(ntolane<=0))
  2124. {
  2125. continue;
  2126. }
  2127. double s = pSig->Gets();
  2128. double fpos = s/pRoad->GetRoadLength();
  2129. if(nlane > 0)fpos = 1.0 -fpos;
  2130. int npos = fpos *xvectorPP.size();
  2131. if(npos <0)npos = 0;
  2132. if(npos>=xvectorPP.size())npos = xvectorPP.size()-1;
  2133. while(xvectorPP.at(npos).nSignal != -1)
  2134. {
  2135. if(npos > 0)npos--;
  2136. else break;
  2137. }
  2138. while(xvectorPP.at(npos).nSignal != -1)
  2139. {
  2140. if(npos < (xvectorPP.size()-1))npos++;
  2141. else break;
  2142. }
  2143. xvectorPP.at(npos).nSignal = atoi(pSig->Gettype().data());
  2144. }
  2145. }
  2146. static std::vector<PlanPoint> GetPlanPoint(std::vector<pathsection> xpathsection,Road * pRoad,GeometryBlock * pgeob,
  2147. Road * pRoad_obj,GeometryBlock * pgeob_obj,
  2148. const double x_now,const double y_now,const double head,
  2149. double nearx,double neary, double nearhead,
  2150. double nearx_obj,double neary_obj,const double fvehiclewidth,const double flen = 1000)
  2151. {
  2152. std::vector<PlanPoint> xvectorPPs;
  2153. double fspace = 0.1;
  2154. if(flen<2000)fspace = 0.1;
  2155. else
  2156. {
  2157. if(flen<5000)fspace = 0.3;
  2158. else
  2159. {
  2160. if(flen<10000)fspace = 0.5;
  2161. else
  2162. fspace = 1.0;
  2163. }
  2164. }
  2165. int i;
  2166. // std::vector<PlanPoint> xvectorPP = GetPoint( xpathsection[0].mpRoad);
  2167. std::vector<PlanPoint> xvectorPP = GetPoint( xpathsection[0],fspace);
  2168. int indexstart = indexinroadpoint(xvectorPP,nearx,neary);
  2169. std::vector<PlanPoint> xvPP = GetLanePoint(xpathsection[0],xvectorPP,fvehiclewidth);
  2170. if(xpathsection[0].mainsel < 0)
  2171. {
  2172. for(i=indexstart;i<xvPP.size();i++)
  2173. {
  2174. xvectorPPs.push_back(xvPP.at(i));
  2175. }
  2176. }
  2177. else
  2178. {
  2179. for(i=(xvPP.size() -indexstart);i<xvPP.size();i++)
  2180. {
  2181. PlanPoint pp;
  2182. pp = xvPP.at(i);
  2183. // pp.hdg = pp.hdg +M_PI;
  2184. xvectorPPs.push_back(pp);
  2185. }
  2186. }
  2187. int npathlast = xpathsection.size() - 1;
  2188. // while(npathlast >= 0)
  2189. // {
  2190. // if(npathlast == 0)break;
  2191. // if(xpathsection[npathlast].mpRoad != xpathsection[npathlast - 1].mpRoad)break;
  2192. // }
  2193. for(i=1;i<(npathlast);i++)
  2194. {
  2195. // if(xpathsection[i].mpRoad == xpathsection[i-1].mpRoad)
  2196. // {
  2197. // if(xpathsection[i].mainsel * xpathsection[i-1].mainsel >0)
  2198. // {
  2199. // continue;
  2200. // }
  2201. // }
  2202. // if(xpathsection[i].mpRoad == xpathsection[npathlast].mpRoad)
  2203. // {
  2204. // if(xpathsection[i].mainsel * xpathsection[npathlast].mainsel > 0)
  2205. // {
  2206. // break;
  2207. // }
  2208. // }
  2209. // xvectorPP = GetPoint( xpathsection[i].mpRoad);
  2210. xvectorPP = GetPoint( xpathsection[i],fspace);
  2211. xvPP = GetLanePoint(xpathsection[i],xvectorPP,fvehiclewidth);
  2212. // std::cout<<" road id: "<<xpathsection[i].mroadid<<" section: "
  2213. // <<xpathsection[i].msectionid<<" size is "<<xvectorPP.size()<<std::endl;
  2214. // std::cout<<" road id is "<<xpathsection[i].mpRoad->GetRoadId().data()<<" size is "<<xvPP.size()<<std::endl;
  2215. int j;
  2216. for(j=0;j<xvPP.size();j++)
  2217. {
  2218. PlanPoint pp;
  2219. pp = xvPP.at(j);
  2220. // pp.hdg = pp.hdg +M_PI;
  2221. xvectorPPs.push_back(pp);
  2222. }
  2223. }
  2224. xvectorPP = GetPoint(xpathsection[npathlast],fspace);
  2225. // xvectorPP = GetPoint(xpathsection[npathlast].mpRoad);
  2226. int indexend = indexinroadpoint(xvectorPP,nearx_obj,neary_obj);
  2227. xvPP = GetLanePoint(xpathsection[npathlast],xvectorPP,fvehiclewidth);
  2228. int nlastsize;
  2229. if(xpathsection[npathlast].mainsel<0)
  2230. {
  2231. nlastsize = indexend;
  2232. }
  2233. else
  2234. {
  2235. if(indexend>0) nlastsize = xvPP.size() - indexend;
  2236. else nlastsize = xvPP.size();
  2237. }
  2238. for(i=0;i<nlastsize;i++)
  2239. {
  2240. xvectorPPs.push_back(xvPP.at(i));
  2241. }
  2242. //Find StartPoint
  2243. // if
  2244. // int a = 1;
  2245. return xvectorPPs;
  2246. }
  2247. std::vector<PlanPoint> gTempPP;
  2248. int getPoint(double q[3], double x, void* user_data) {
  2249. // printf("%f, %f, %f, %f\n", q[0], q[1], q[2], x);
  2250. PlanPoint pp;
  2251. pp.x = q[0];
  2252. pp.y = q[1];
  2253. pp.hdg = q[2];
  2254. pp.dis = x;
  2255. pp.speed = *((double *)user_data);
  2256. gTempPP.push_back(pp);
  2257. // std::cout<<pp.x<<" "<<" "<<pp.y<<" "<<pp.hdg<<std::endl;
  2258. return 0;
  2259. }
  2260. /**
  2261. * @brief getlanesel
  2262. * @param nSel
  2263. * @param pLaneSection
  2264. * @param nlr
  2265. * @return
  2266. */
  2267. int getlanesel(int nSel,LaneSection * pLaneSection,int nlr)
  2268. {
  2269. int nlane = nSel;
  2270. int mainselindex = 0;
  2271. if(nlr == 2)nlane = nlane*(-1);
  2272. int nlanecount = pLaneSection->GetLaneCount();
  2273. int i;
  2274. int nTrueSel = nSel;
  2275. if(nTrueSel <= 1)nTrueSel= 1;
  2276. int nCurSel = 1;
  2277. if(nlr == 2)nCurSel = nCurSel *(-1);
  2278. bool bOK = false;
  2279. int nxsel = 1;
  2280. int nlasttid = 0;
  2281. bool bfindlast = false;
  2282. while(bOK == false)
  2283. {
  2284. bool bHaveDriving = false;
  2285. int ncc = 0;
  2286. for(i=0;i<nlanecount;i++)
  2287. {
  2288. Lane * pLane = pLaneSection->GetLane(i);
  2289. if(strncmp(pLane->GetType().data(),"driving",255) == 0 )
  2290. {
  2291. if((nlr == 1)&&(pLane->GetId()>0))
  2292. {
  2293. ncc++;
  2294. }
  2295. if((nlr == 2)&&(pLane->GetId()<0))
  2296. {
  2297. ncc++;
  2298. }
  2299. if(ncc == nxsel)
  2300. {
  2301. bHaveDriving = true;
  2302. bfindlast = true;
  2303. nlasttid = pLane->GetId();
  2304. break;
  2305. }
  2306. }
  2307. }
  2308. if(bHaveDriving == true)
  2309. {
  2310. if(nxsel == nTrueSel)
  2311. {
  2312. return nlasttid;
  2313. }
  2314. else
  2315. {
  2316. nxsel++;
  2317. }
  2318. }
  2319. else
  2320. {
  2321. if(bfindlast)
  2322. {
  2323. return nlasttid;
  2324. }
  2325. else
  2326. {
  2327. std::cout<<"can't find lane."<<std::endl;
  2328. return 0;
  2329. }
  2330. //Use Last
  2331. }
  2332. }
  2333. return 0;
  2334. int k;
  2335. bool bFind = false;
  2336. while(bFind == false)
  2337. {
  2338. for(k=0;k<nlanecount;k++)
  2339. {
  2340. Lane * pLane = pLaneSection->GetLane(k);
  2341. if((nlane == pLane->GetId())&&(strncmp(pLane->GetType().data(),"driving",255) == 0))
  2342. {
  2343. bFind = true;
  2344. mainselindex = k;
  2345. break;
  2346. }
  2347. }
  2348. if(bFind)continue;
  2349. if(nlr == 1)nlane--;
  2350. else nlane++;
  2351. if(nlane == 0)
  2352. {
  2353. std::cout<<" Fail. can't find lane"<<std::endl;
  2354. break;
  2355. }
  2356. }
  2357. return nlane;
  2358. }
  2359. void checktrace(std::vector<PlanPoint> & xPlan)
  2360. {
  2361. int i;
  2362. int nsize = xPlan.size();
  2363. for(i=1;i<nsize;i++)
  2364. {
  2365. double dis = sqrt(pow(xPlan.at(i).x - xPlan.at(i-1).x,2) + pow(xPlan.at(i).y - xPlan.at(i-1).y,2));
  2366. if(dis > 0.3)
  2367. {
  2368. double hdg = CalcHdg(QPointF(xPlan.at(i-1).x,xPlan.at(i-1).y),QPointF(xPlan.at(i).x,xPlan.at(i).y));
  2369. int ncount = dis/0.1;
  2370. int j;
  2371. for(j=1;j<ncount;j++)
  2372. {
  2373. double x, y;
  2374. PlanPoint pp;
  2375. pp.x = xPlan.at(i-1).x + j*0.1 *cos(hdg);
  2376. pp.y = xPlan.at(i-1).y + j*0.1 *sin(hdg);
  2377. pp.hdg = hdg;
  2378. xPlan.insert(xPlan.begin()+i+j-1,pp);
  2379. }
  2380. qDebug("dis is %f",dis);
  2381. }
  2382. }
  2383. }
  2384. void ChangeLane(std::vector<PlanPoint> & xvectorPP)
  2385. {
  2386. int i = 0;
  2387. int nsize = xvectorPP.size();
  2388. for(i=0;i<nsize;i++)
  2389. {
  2390. if((xvectorPP[i].mfSecx == xvectorPP[i].x)&&(xvectorPP[i].mfSecy == xvectorPP[i].y))
  2391. {
  2392. }
  2393. else
  2394. {
  2395. int k;
  2396. for(k=i;k<(nsize-1);k++)
  2397. {
  2398. if((xvectorPP[k].mfSecx == xvectorPP[k].x)&&(xvectorPP[k].mfSecy == xvectorPP[k].y))
  2399. {
  2400. break;
  2401. }
  2402. }
  2403. int nnum = k-i;
  2404. int nchangepoint = 300;
  2405. double froadlen = sqrt(pow(xvectorPP[k].x - xvectorPP[i].x,2)
  2406. +pow(xvectorPP[k].y - xvectorPP[i].y,2));
  2407. const double fMAXCHANGE = 100;
  2408. if(froadlen<fMAXCHANGE)
  2409. {
  2410. nchangepoint = nnum;
  2411. }
  2412. else
  2413. {
  2414. nchangepoint = (fMAXCHANGE/froadlen) * nnum;
  2415. }
  2416. qDebug(" road %d len is %f changepoint is %d nnum is %d",
  2417. xvectorPP[k-1].nRoadID,froadlen,nchangepoint,nnum);
  2418. int nstart = i + nnum/2 -nchangepoint/2;
  2419. int nend = i+nnum/2 + nchangepoint/2;
  2420. if(nnum<300)
  2421. {
  2422. nstart = i;
  2423. nend = k;
  2424. }
  2425. int j;
  2426. for(j=i;j<nstart;j++)
  2427. {
  2428. xvectorPP[j].x = xvectorPP[j].mfSecx;
  2429. xvectorPP[j].y = xvectorPP[j].mfSecy;
  2430. }
  2431. nnum = nend - nstart;
  2432. for(j=nstart;j<nend;j++)
  2433. {
  2434. double fdis = sqrt(pow(xvectorPP[j].x - xvectorPP[j].mfSecx,2)
  2435. +pow(xvectorPP[j].y - xvectorPP[j].mfSecy,2));
  2436. double foff = fdis *(j-nstart)/nnum;
  2437. if(xvectorPP[j].nlrchange == 1)
  2438. {
  2439. // std::cout<<"foff is "<<foff<<std::endl;
  2440. xvectorPP[j].x = xvectorPP[j].mfSecx + foff *cos(xvectorPP[j].hdg + M_PI/2.0);
  2441. xvectorPP[j].y = xvectorPP[j].mfSecy + foff *sin(xvectorPP[j].hdg + M_PI/2.0);
  2442. xvectorPP[j].mfDisToRoadLeft = xvectorPP[j].mfDisToRoadLeft + fdis - foff;
  2443. }
  2444. else
  2445. {
  2446. xvectorPP[j].x = xvectorPP[j].mfSecx + foff *cos(xvectorPP[j].hdg - M_PI/2.0);
  2447. xvectorPP[j].y = xvectorPP[j].mfSecy + foff *sin(xvectorPP[j].hdg - M_PI/2.0);
  2448. xvectorPP[j].mfDisToRoadLeft = xvectorPP[j].mfDisToRoadLeft - fdis +foff;
  2449. }
  2450. }
  2451. i =k;
  2452. }
  2453. }
  2454. }
  2455. #include <QFile>
  2456. /**
  2457. * @brief MakePlan 全局路径规划
  2458. * @param pxd 有向图
  2459. * @param pxodr OpenDrive地图
  2460. * @param x_now 当前x坐标
  2461. * @param y_now 当前y坐标
  2462. * @param head 当前航向
  2463. * @param x_obj 目标点x坐标
  2464. * @param y_obj 目标点y坐标
  2465. * @param obj_dis 目标点到路径的距离
  2466. * @param srcnearthresh 当前点离最近路径的阈值,如果不在这个范围,寻找失败
  2467. * @param dstnearthresh 目标点离最近路径的阈值,如果不在这个范围,寻找失败
  2468. * @param nlanesel 车道偏好,1为内车道,数值越大越偏外,如果数值不满足,会选择最靠外的。
  2469. * @param xPlan 返回的轨迹点
  2470. * @return 0 成功 <0 失败
  2471. */
  2472. int MakePlan(xodrdijkstra * pxd,OpenDrive * pxodr,const double x_now,const double y_now,const double head,
  2473. const double x_obj,const double y_obj,const double & obj_dis,
  2474. const double srcnearthresh,const double dstnearthresh,
  2475. const int nlanesel,std::vector<PlanPoint> & xPlan,const double fvehiclewidth )
  2476. {
  2477. double s;double nearx;
  2478. double neary;double nearhead;
  2479. Road * pRoad;GeometryBlock * pgeob;
  2480. Road * pRoad_obj;GeometryBlock * pgeob_obj;
  2481. double s_obj;double nearx_obj;
  2482. double neary_obj;double nearhead_obj;
  2483. int lr_end = 2;
  2484. int lr_start = 2;
  2485. double fs1,fs2;
  2486. // int nfind = GetNearPoint(x_now,y_now,pxodr,&pRoad,&pgeob,s,nearx,neary,nearhead,srcnearthresh);
  2487. std::vector<iv::nearroad> xvectornearstart;
  2488. std::vector<iv::nearroad> xvectornearend;
  2489. GetNearPointWithHead(x_now,y_now,head,pxodr,xvectornearstart,srcnearthresh,true);
  2490. GetNearPointWithHead(x_obj,y_obj,0,pxodr,xvectornearend,dstnearthresh,false);
  2491. if(xvectornearstart.size()<1)
  2492. {
  2493. std::cout<<" plan fail. can't find start point."<<std::endl;
  2494. return -1;
  2495. }
  2496. if(xvectornearend.size() < 1)
  2497. {
  2498. std::cout<<" plan fail. can't find end point."<<std::endl;
  2499. return -2;
  2500. }
  2501. std::cout<<" start size: "<<xvectornearstart.size()<<std::endl;
  2502. std::cout<<" end size: "<<xvectornearend.size()<<std::endl;
  2503. std::vector<std::vector<PlanPoint>> xvectorplans;
  2504. std::vector<double> xvectorroutedis;
  2505. int i;
  2506. int j;
  2507. for(i=0;i<(int)xvectornearstart.size();i++)
  2508. {
  2509. for(j=0;j<(int)xvectornearend.size();j++)
  2510. {
  2511. iv::nearroad * pnearstart = &xvectornearstart[i];
  2512. iv::nearroad * pnearend = &xvectornearend[j];
  2513. pRoad = pnearstart->pObjRoad;
  2514. pgeob = pnearstart->pgeob;
  2515. pRoad_obj = pnearend->pObjRoad;
  2516. pgeob_obj = pnearend->pgeob;
  2517. lr_start = pnearstart->lr;
  2518. lr_end = pnearend->lr;
  2519. std::cout<<" lr start: "<<lr_start<<" lr end "<<lr_end<<std::endl;
  2520. nearx = pnearstart->nearx;
  2521. neary = pnearstart->neary;
  2522. nearx_obj = pnearend->nearx;
  2523. neary_obj = pnearend->neary;
  2524. bool bNeedDikstra = true;
  2525. if((atoi(pRoad->GetRoadId().data()) == atoi(pRoad_obj->GetRoadId().data()))&&(lr_start == lr_end) &&(pRoad->GetLaneSectionCount() == 1))
  2526. {
  2527. std::vector<PlanPoint> xvPP = GetPoint(pRoad);
  2528. int nindexstart = indexinroadpoint(xvPP,nearx,neary);
  2529. int nindexend = indexinroadpoint(xvPP,nearx_obj,neary_obj);
  2530. int nlane = getlanesel(nlanesel,pRoad->GetLaneSection(0),lr_start);
  2531. AddSignalMark(pRoad,nlane,xvPP);
  2532. if((nindexend >nindexstart)&&(lr_start == 2))
  2533. {
  2534. bNeedDikstra = false;
  2535. }
  2536. if((nindexend <nindexstart)&&(lr_start == 1))
  2537. {
  2538. bNeedDikstra = false;
  2539. }
  2540. if(bNeedDikstra == false)
  2541. {
  2542. std::vector<iv::lanewidthabcd> xvectorLWA = GetLaneWidthABCD(pRoad);
  2543. std::vector<int> xvectorindex1;
  2544. xvectorindex1 = GetLWIndex(xvectorLWA,nlane);
  2545. double foff = getoff(pRoad,nlane);
  2546. foff = foff + 0.0;
  2547. std::vector<PlanPoint> xvectorPP;
  2548. int i = nindexstart;
  2549. while(i!=nindexend)
  2550. {
  2551. if(lr_start == 2)
  2552. {
  2553. PlanPoint pp = xvPP.at(i);
  2554. foff = getoff(nlane,pp.mS,xvectorLWA,xvectorindex1);
  2555. pp.x = pp.x + foff *cos(pp.hdg + M_PI/2.0);
  2556. pp.y = pp.y + foff *sin(pp.hdg + M_PI/2.0);
  2557. pp.mWidth = getwidth(pRoad,nlane,xvectorLWA,pp.mS);
  2558. pp.mfDisToLaneLeft = pp.mWidth/2.0;
  2559. pp.lanmp = 0;
  2560. pp.mfDisToRoadLeft = foff *(-1.0);
  2561. pp.mfRoadWidth = GetRoadWidth(xvectorLWA,nlane,pp.mS);
  2562. pp.mLaneCur = abs(nlane);
  2563. pp.mVectorLaneWidth = pRoad->GetDrivingLaneWidthVector(pp.mS,lr_start);
  2564. pp.mLaneCount = pRoad->GetRightDrivingLaneCount(pp.mS);
  2565. GetLaneOriTotal(pRoad,nlane,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed,pp.mbNoavoid);
  2566. xvectorPP.push_back(pp);
  2567. i++;
  2568. }
  2569. else
  2570. {
  2571. PlanPoint pp = xvPP.at(i);
  2572. foff = getoff(nlane,pp.mS,xvectorLWA,xvectorindex1);
  2573. pp.x = pp.x + foff *cos(pp.hdg + M_PI/2.0);
  2574. pp.y = pp.y + foff *sin(pp.hdg + M_PI/2.0);
  2575. pp.hdg = pp.hdg + M_PI;
  2576. pp.mWidth = getwidth(pRoad,nlane,xvectorLWA,pp.mS);
  2577. pp.mfDisToLaneLeft = pp.mWidth/2.0;
  2578. pp.lanmp = 0;
  2579. pp.mfDisToRoadLeft = foff;
  2580. pp.mfRoadWidth = GetRoadWidth(xvectorLWA,nlane,pp.mS);
  2581. pp.mLaneCur = abs(nlane);
  2582. pp.mVectorLaneWidth = pRoad->GetDrivingLaneWidthVector(pp.mS,lr_start);
  2583. pp.mLaneCount = pRoad->GetLeftDrivingLaneCount(pp.mS);
  2584. GetLaneOriTotal(pRoad,nlane,pp.mS,pp.mnLaneori,pp.mnLaneTotal,pp.speed,pp.mbNoavoid);
  2585. xvectorPP.push_back(pp);
  2586. i--;
  2587. }
  2588. }
  2589. for(i=0;i<(int)xvectorPP.size();i++)
  2590. {
  2591. xvectorPP[i].mfCurvature = pRoad->GetRoadCurvature(xvectorPP[i].mS);
  2592. }
  2593. pathsection xps;
  2594. xps.mbStartToMainChange = false;
  2595. xps.mbMainToEndChange = false;
  2596. // CalcInLaneAvoid(xps,xvectorPP,fvehiclewidth,0,0,0);
  2597. xPlan = xvectorPP;
  2598. xvectorplans.push_back(xvectorPP);
  2599. xvectorroutedis.push_back(xvectorPP.size() * 0.1);
  2600. }
  2601. }
  2602. if(bNeedDikstra)
  2603. {
  2604. bool bSuc = false;
  2605. std::vector<int> xpath = pxd->getpath(atoi(pRoad->GetRoadId().data()),lr_start,atoi(pRoad_obj->GetRoadId().data()),lr_end,bSuc,fs1,fs2);
  2606. if(xpath.size()<1)
  2607. {
  2608. continue;
  2609. }
  2610. if(bSuc == false)
  2611. {
  2612. continue;
  2613. }
  2614. double flen = pxd->getpathlength(xpath);
  2615. std::vector<pathsection> xpathsection = pxd->getgpspoint(atoi(pRoad->GetRoadId().data()),lr_start,atoi(pRoad_obj->GetRoadId().data()),lr_end,xpath,nlanesel);
  2616. std::vector<PlanPoint> xvectorPP = GetPlanPoint(xpathsection,pRoad,pgeob,pRoad_obj,pgeob_obj,x_now,y_now,
  2617. head,nearx,neary,nearhead,nearx_obj,neary_obj,fvehiclewidth,flen);
  2618. ChangeLane(xvectorPP);
  2619. xvectorplans.push_back(xvectorPP);
  2620. xvectorroutedis.push_back(flen);
  2621. }
  2622. }
  2623. }
  2624. if(xvectorplans.size() > 0)
  2625. {
  2626. if(xvectorplans.size() == 1)
  2627. {
  2628. xPlan = xvectorplans[0];
  2629. }
  2630. else
  2631. {
  2632. int nsel = 0;
  2633. double fdismin = xvectorroutedis[0];
  2634. for(i=1;i<(int)xvectorroutedis.size();i++)
  2635. {
  2636. if(xvectorroutedis[i]<fdismin)
  2637. {
  2638. nsel = i;
  2639. }
  2640. }
  2641. xPlan = xvectorplans[nsel];
  2642. }
  2643. return 0;
  2644. }
  2645. std::cout<<" plan fail. can't find path."<<std::endl;
  2646. return -1000;
  2647. }