ndsdataproc.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. #include "ndsdataproc.h"
  2. #include <QFile>
  3. #include "gnss_coordinate_convert.h"
  4. #include <QDateTime>
  5. #include <QPointF>
  6. #include "geofit.h"
  7. #include "circlefitting.h"
  8. #ifndef INNDSPROC
  9. #include "mainwindow.h"
  10. #endif
  11. #ifndef INNDSPROC
  12. extern MainWindow * gw;
  13. #endif
  14. NDSDataProc::NDSDataProc()
  15. {
  16. }
  17. //-1 can't open line data file
  18. //-2 can't open vehicle data file
  19. //-3 no valid line data.
  20. //-4 no valid vehicle data.
  21. int NDSDataProc::ProcNDSData(std::string strlinepath,std::string strvehiclepath,OpenDrive * mpxodr,int nleftlanecount,int nrightlanecount)
  22. {
  23. double fExtend = 100;
  24. mnProc = 0;
  25. mstrState = "Loading Data.";
  26. QFile xFileline;
  27. QFile xFilevehicle;
  28. xFileline.setFileName(strlinepath.data());
  29. xFilevehicle.setFileName(strvehiclepath.data());
  30. if(!xFileline.open(QIODevice::ReadOnly))
  31. {
  32. mnProc = 100;
  33. mstrState = " Can't Read Line File. ";
  34. return -1;
  35. }
  36. if(!xFilevehicle.open(QIODevice::ReadOnly))
  37. {
  38. xFileline.close();
  39. mnProc = 100;
  40. mstrState = " Can't Read Vehicle File. ";
  41. return -2;
  42. }
  43. double flaneavgwidthtotal = 0;
  44. double flaneavgwidth = 3.5;
  45. std::vector<iv::nds_line> xvectorline;
  46. std::vector<iv::nds_vehicle> xvectorvehicle;
  47. std::vector<iv::nds_vl> xvectorvl;
  48. QByteArray ba = xFileline.readAll();
  49. QList<QByteArray> baline =ba.split('\n');//x.split(QRegExp("\n ")) ;//ba.split('\n');
  50. int nline = baline.size();
  51. int i;
  52. for(i=1;i<nline;i++)
  53. {
  54. QString x(baline[i]);
  55. x = x.trimmed();
  56. // QList<QByteArray> badata = baline[i].split('\t');
  57. QStringList badata = x.split(QRegExp("[,]"));
  58. if(badata.size()>=36)
  59. {
  60. iv::nds_line xline;
  61. xline.localtime = QString(badata[1]).toLongLong();
  62. if(badata[6] == "Normal")xline.feature = 0;
  63. if(badata[6] == "Left1")xline.feature = 1;
  64. if(badata[6] == "Right1")xline.feature = 2;
  65. xline.centerdeparture = badata[8].toDouble();
  66. xline.lanewidth = badata[9].toDouble();
  67. xline.laneheading = badata[10].toDouble();
  68. xline.lanecurv = badata[11].toDouble();
  69. xline.nlanetype = badata[23].toDouble();
  70. if(xline.feature >=0)xvectorline.push_back(xline);
  71. flaneavgwidthtotal = flaneavgwidthtotal + xline.lanewidth;
  72. }
  73. }
  74. if(nline>0)
  75. {
  76. flaneavgwidth = flaneavgwidthtotal/nline;
  77. }
  78. if(xvectorline.size() < 2)
  79. {
  80. xFileline.close();
  81. xFilevehicle.close();
  82. mnProc = 100;
  83. mstrState = " No Line Data. ";
  84. return -3;
  85. }
  86. // int64_t localtime;
  87. // int locationmode;
  88. // double lon;
  89. // double lat;
  90. // double height;
  91. // double heading;
  92. ba = xFilevehicle.readAll();
  93. baline =ba.split('\n');//x.split(QRegExp("\n ")) ;//ba.split('\n');
  94. nline = baline.size();
  95. for(i=1;i<nline;i++)
  96. {
  97. QString x(baline[i]);
  98. x = x.trimmed();
  99. // QList<QByteArray> badata = baline[i].split('\t');
  100. QStringList badata = x.split(QRegExp("[,]"));
  101. if(badata.size()>=15)
  102. {
  103. iv::nds_vehicle xvehicle;
  104. xvehicle.localtime = badata[1].toLongLong();
  105. xvehicle.locationmode = badata[4].toInt();
  106. xvehicle.lon = badata[5].toDouble();
  107. xvehicle.lat = badata[6].toDouble();
  108. xvehicle.height = badata[7].toDouble();
  109. xvehicle.heading = badata[9].toDouble();
  110. if(xvehicle.heading<0)xvehicle.heading = xvehicle.heading + 360;
  111. if(xvehicle.locationmode > 0)xvectorvehicle.push_back(xvehicle);
  112. }
  113. }
  114. xFileline.close();
  115. xFilevehicle.close();
  116. if(xvectorvehicle.size()<2)
  117. {
  118. mnProc = 100;
  119. mstrState = " No Vehicle Data. ";
  120. return -4 ;
  121. }
  122. double flon0,flat0;
  123. if(mpxodr->GetRoadCount() == 0)
  124. {
  125. flon0 = xvectorvehicle[0].lon;
  126. flat0 = xvectorvehicle[0].lat;
  127. mpxodr->SetHeader(1,1,"adcmap",1.1,QDateTime::currentDateTime().toString("yyyy-MM-dd").toLatin1().data(),0,0,0,0,flat0,flon0,360);
  128. }
  129. double x0,y0;
  130. GaussProjCal(flon0,flat0,&x0,&y0);
  131. int j;
  132. int nlinecount = xvectorline.size();
  133. int nvehiclecount = xvectorvehicle.size();
  134. mnProc = 10;
  135. mstrState = " Calulation x y. ";
  136. for(i=0;i<nvehiclecount;i++)
  137. {
  138. iv::nds_vehicle * p;
  139. p = &xvectorvehicle[i];
  140. double x,y;
  141. GaussProjCal(p->lon,p->lat,&x,&y);
  142. p->frel_x = x - x0;
  143. p->frel_y = y - y0;
  144. }
  145. double fS = 0;
  146. for(i=1;i<xvectorvehicle.size();i++)
  147. {
  148. iv::nds_vehicle *p1,*p2;
  149. p1 = &xvectorvehicle[i-1];
  150. p2 = &xvectorvehicle[i];
  151. fS = fS + sqrt(pow(p2->frel_x - p1->frel_x,2)+pow(p2->frel_y - p1->frel_y,2));
  152. }
  153. std::cout<<" s: "<<fS<<std::endl;
  154. int nlinenow = 0;
  155. mnProc = 20;
  156. mstrState = " Get Vehicle And Line Relation. ";
  157. for(i=0;i<nvehiclecount;i++)
  158. {
  159. if(nlinenow >= nlinecount)
  160. {
  161. break;
  162. }
  163. iv::nds_vl xvl;
  164. xvl.mvehindex = i;
  165. while(xvectorvehicle[i].localtime > xvectorline[nlinenow].localtime)
  166. {
  167. nlinenow++;
  168. }
  169. while(xvectorvehicle[i].localtime == xvectorline[nlinenow].localtime)
  170. {
  171. xvl.mvectorlineindex.push_back(nlinenow);
  172. // std::cout<<" veh: "<<i<<" line: "<<nlinenow<<std::endl;
  173. nlinenow++;
  174. }
  175. xvectorvl.push_back(xvl);
  176. }
  177. //Erase no line info point.
  178. // std::cout<<" vl size "<<xvectorvl.size()<<std::endl;
  179. i= 0;
  180. while(i<(int)xvectorvl.size())
  181. {
  182. if(xvectorvl[i].mvectorlineindex.size() == 0)
  183. {
  184. xvectorvl.erase(xvectorvl.begin()+i);
  185. continue;
  186. }
  187. i++;
  188. }
  189. //Erase dis<0.5 point
  190. i=1;
  191. while(i<(int)xvectorvl.size())
  192. {
  193. iv::nds_vehicle v1 = xvectorvehicle[xvectorvl[i-1].mvehindex];
  194. iv::nds_vehicle v2 = xvectorvehicle[xvectorvl[i].mvehindex];
  195. double fdis = sqrt(pow(v1.frel_x - v2.frel_x,2) + pow(v1.frel_y - v2.frel_y,2));
  196. if(fdis < 0.5)
  197. {
  198. xvectorvl.erase(xvectorvl.begin()+i);
  199. continue;
  200. }
  201. i++;
  202. }
  203. // std::cout<<" vl size "<<xvectorvl.size()<<std::endl;
  204. double fdismin = 100000;
  205. double fdismax = 0;
  206. int nvlsize = xvectorvl.size();
  207. fS = 0;
  208. for(i=1;i<nvlsize;i++)
  209. {
  210. iv::nds_vehicle v1 = xvectorvehicle[xvectorvl[i-1].mvehindex];
  211. iv::nds_vehicle v2 = xvectorvehicle[xvectorvl[i].mvehindex];
  212. double fdis = sqrt(pow(v1.frel_x - v2.frel_x,2) + pow(v1.frel_y - v2.frel_y,2));
  213. xvectorvehicle[xvectorvl[i].mvehindex].fdistolast = fdis;
  214. fS = fS + fdis;
  215. if(fdis<fdismin)fdismin = fdis;
  216. if(fdis>fdismax)fdismax = fdis;
  217. if(fdis> 50)std::cout<<" index "<<xvectorvl[i].mvehindex<<" dis "<<fdis<<" time "<<xvectorvehicle[xvectorvl[i].mvehindex].localtime<<std::endl;
  218. // if(fdis<0.6)std::cout<<" index "<<xvectorvl[i].mvehindex<<" dis "<<fdis<<" time "<<xvectorvehicle[xvectorvl[i].mvehindex].localtime<<std::endl;
  219. }
  220. for(i=0;i<nvlsize;i++)
  221. {
  222. iv::nds_vehicle *pv1 = &xvectorvehicle[xvectorvl[i].mvehindex];
  223. double fhdg = (90-pv1->heading)*M_PI/360.0;
  224. if(fhdg<0)fhdg = fhdg + 2.0*M_PI;
  225. pv1->fhdg = fhdg;
  226. iv::nds_line * pline1 = &xvectorline[xvectorvl[i].mvectorlineindex[0]];
  227. pv1->flanewidth = pline1->lanewidth;
  228. iv::nds_line * pline2 = NULL;
  229. if(xvectorvl[i].mvectorlineindex.size()>=2)
  230. {
  231. pline2 = &xvectorline[xvectorvl[i].mvectorlineindex[1]];
  232. }
  233. pv1->fx = pv1->frel_x + (pline1->lanewidth/2.0 - pline1->centerdeparture)*cos(fhdg + M_PI/2.0);
  234. pv1->fy = pv1->frel_y + (pline1->lanewidth/2.0 - pline1->centerdeparture)*sin(fhdg + M_PI/2.0);
  235. if(pline1->feature == 1)
  236. {
  237. pv1->nleftlanetype = pline1->nlanetype;
  238. }
  239. if(pline1->feature == 2)
  240. {
  241. pv1->nrightlanetype = pline1->nlanetype;
  242. }
  243. if(pline2 != NULL)
  244. {
  245. if(pline2->feature == 1)
  246. {
  247. pv1->nleftlanetype = pline2->nlanetype;
  248. }
  249. if(pline2->feature == 2)
  250. {
  251. pv1->nrightlanetype = pline2->nlanetype;
  252. }
  253. }
  254. }
  255. mnProc = 30;
  256. mstrState = " Fitting Geo. ";
  257. std::cout<<" fdis "<<fS<<" min "<<fdismin<<" max "<<fdismax<<std::endl;
  258. double LINE_ERROR = 0.10;
  259. std::vector<geobase> xvectorgeo;
  260. bool bComplete = false;
  261. // int j;
  262. int ncurpos = 0;
  263. int nrange = xvectorvl.size();
  264. // double fXLast;
  265. // double fYLast;
  266. // bool bFirst = true;
  267. bool bHaveLastxyhdg = false;
  268. double fLastX,fLastY,fLastHdg;
  269. double fEndX,fEndY,fEndHdg;
  270. double fStartX,fStartY,fStartHdg;
  271. while(bComplete == false)
  272. {
  273. VectorXd x_veh(nrange);
  274. VectorXd y_veh(nrange);
  275. for(j=0;j<nrange;j++)
  276. {
  277. x_veh[j] = xvectorvehicle[xvectorvl[j+ncurpos].mvehindex].fx;//mvectorrtkdata.at(j+ncurpos).mfrelx;
  278. y_veh[j] = xvectorvehicle[xvectorvl[j+ncurpos].mvehindex].fy;//mvectorrtkdata.at(j+ncurpos).mfrely;
  279. }
  280. if(nrange<1)
  281. {
  282. break;
  283. }
  284. bool bArcOk = false;
  285. bool bLineOk = false;
  286. double dismax = 0;
  287. QPointF sp,ep;
  288. double fR,flen;
  289. double fhdgstart,fhdgend;
  290. if(nrange >= 3)
  291. {
  292. geofit::Inst().arcfitanddis(x_veh,y_veh,dismax,fR,sp,ep,fhdgstart,fhdgend,flen);
  293. if(dismax < LINE_ERROR)
  294. {
  295. bArcOk = true;
  296. std::cout<<" a arc is ok "<<"pos is "<<ncurpos<<" range is "<<nrange<<" error is "<<dismax<<std::endl;
  297. }
  298. }
  299. auto coeffs = polyfit(x_veh, y_veh, 1);
  300. dismax = 0;
  301. for(j=0;j<nrange;j++)
  302. {
  303. double A = coeffs[1];
  304. double B = -1;
  305. double C = coeffs[0];
  306. double dis = fabs(A*x_veh[j] + B*y_veh[j] +C )/sqrt(pow(A,2)+pow(B,2));
  307. if(dis>dismax)dismax = dis;
  308. }
  309. if(dismax < LINE_ERROR)
  310. {
  311. bLineOk = true;
  312. std::cout<<" a line is ok "<<"pos is "<<ncurpos<<" range is "<<nrange<<" error is "<<dismax<<std::endl;
  313. }
  314. if((nrange <= 2)||bLineOk||bArcOk)
  315. {
  316. if((bLineOk)||(nrange<=2))
  317. {
  318. std::cout<<"use line"<<std::endl;
  319. geobase xgeo;
  320. xgeo.mnStartPoint = ncurpos;
  321. xgeo.mnEndPoint = ncurpos + nrange -1 ;
  322. double x0,y0,x1,y1;
  323. geofit::Inst().getcrosspoint(coeffs[1],-1,coeffs[0],x_veh[0],
  324. y_veh[0],x0,y0);
  325. geofit::Inst().getcrosspoint(coeffs[1],-1,coeffs[0],x_veh[nrange -1],
  326. y_veh[nrange - 1],x1,y1);
  327. xgeo.mfA = coeffs[1];
  328. xgeo.mfB = -1;
  329. xgeo.mfC = coeffs[0];
  330. xgeo.mfHdg = geofit::Inst().CalcHdg(x0,y0,x1,y1);
  331. xgeo.mfX = x0;
  332. xgeo.mfY = y0;
  333. xgeo.mfLen = sqrt(pow(x1-x0,2)+pow(y1-y0,2));
  334. xgeo.mnType = 0;
  335. fStartX = x0;
  336. fStartY = y0;
  337. fStartHdg = xgeo.mfHdg;
  338. fEndX = x1;
  339. fEndY = y1;
  340. fEndHdg = xgeo.mfHdg;
  341. xvectorgeo.push_back(xgeo);
  342. }
  343. else
  344. {
  345. if(bArcOk)
  346. {
  347. std::cout<<"use arc"<<std::endl;
  348. geobase xgeo;
  349. xgeo.mnStartPoint = ncurpos;
  350. xgeo.mnEndPoint = ncurpos + nrange -1;
  351. xgeo.mfHdg = fhdgstart;
  352. xgeo.mfHdgStart = fhdgstart;
  353. xgeo.mfHdgEnd = fhdgend;
  354. xgeo.mfX = sp.x();
  355. xgeo.mfY = sp.y();
  356. xgeo.mfLen = flen;
  357. xgeo.mnType = 1;
  358. xgeo.mfEndX = ep.x();
  359. xgeo.mfEndY = ep.y();
  360. xgeo.mR = fR;
  361. fStartX = xgeo.mfX;
  362. fStartY = xgeo.mfY;
  363. fStartHdg = xgeo.mfHdgStart;
  364. fEndX = xgeo.mfEndX;
  365. fEndY = xgeo.mfEndY;
  366. fEndHdg = xgeo.mfHdgEnd;
  367. xvectorgeo.push_back(xgeo);
  368. }
  369. }
  370. // std::cout<<" a line is ok "<<"pos is "<<ncurpos<<" range is "<<nrange<<" error is "<<dismax<<std::endl;
  371. if(bHaveLastxyhdg)
  372. {
  373. std::vector<geobase> xvectorgeobe = geofit::CreateBezierGeo(fLastX,fLastY,
  374. fLastHdg,fStartX,fStartY,fStartHdg);
  375. if(xvectorgeobe.size()>0)
  376. {
  377. xvectorgeobe[0].mnStartPoint = ncurpos-1;
  378. xvectorgeobe[0].mnEndPoint = ncurpos;
  379. xvectorgeo.insert(xvectorgeo.begin()+(xvectorgeo.size()-1),xvectorgeobe[0]);
  380. }
  381. }
  382. bHaveLastxyhdg = true;
  383. fLastX = fEndX;
  384. fLastY = fEndY;
  385. fLastHdg = fEndHdg;
  386. // ncurpos = ncurpos + nrange -1;
  387. ncurpos = ncurpos + nrange; //Skip 1 point, use bezier
  388. nrange = xvectorvl.size() - ncurpos;
  389. }
  390. else
  391. {
  392. if(nrange > 30)
  393. nrange = nrange/2;
  394. else
  395. nrange = nrange -1;
  396. if(nrange<2)nrange = 2;
  397. }
  398. if(ncurpos>=(int)(xvectorvl.size()-1))
  399. {
  400. if(ncurpos == (int)(xvectorvl.size()-1))
  401. {
  402. std::cout<<"Last section, 2 Points, use line."<<std::endl;
  403. VectorXd x_veh1(2);
  404. VectorXd y_veh1(2);
  405. x_veh1[0] = fLastX;
  406. y_veh1[0] = fLastY;
  407. x_veh1[1] = xvectorvehicle[xvectorvl[ncurpos].mvehindex].fx;
  408. y_veh1[1] = xvectorvehicle[xvectorvl[ncurpos].mvehindex].fy;
  409. auto coeffs = polyfit(x_veh1, y_veh1, 1);
  410. geobase xgeo;
  411. xgeo.mnStartPoint = ncurpos-1;
  412. xgeo.mnEndPoint = ncurpos;
  413. xgeo.mfA = coeffs[1];
  414. xgeo.mfB = -1;
  415. xgeo.mfC = coeffs[0];
  416. xgeo.mfHdg = geofit::Inst().CalcHdg(x_veh1[0],y_veh1[0],x_veh1[1],y_veh1[1]);
  417. xgeo.mfX = x_veh1[0];
  418. xgeo.mfY = y_veh1[0];
  419. xgeo.mfLen = sqrt(pow(x_veh1[1]-x_veh1[0],2)+pow(y_veh1[1]-y_veh1[0],2));
  420. xgeo.mnType = 0;
  421. xvectorgeo.push_back(xgeo);
  422. }
  423. bComplete = true;
  424. }
  425. }
  426. double snow = 0;
  427. for(i=0;i<(int)xvectorgeo.size();i++)
  428. {
  429. geobase * pgeo = &xvectorgeo[i];
  430. // std::cout<<"geo len: "<<pgeo->mfLen<<" type"<<pgeo->mnType<<std::endl;
  431. double tems = 0;
  432. for(j=(pgeo->mnStartPoint+1);j<=pgeo->mnEndPoint;j++)
  433. {
  434. xvectorvehicle[xvectorvl[j].mvehindex].s = snow + tems + xvectorvehicle[xvectorvl[j].mvehindex].fdistolast;
  435. tems = tems + xvectorvehicle[xvectorvl[j].mvehindex].fdistolast;
  436. }
  437. snow = snow + pgeo->mfLen;
  438. }
  439. mnProc = 90;
  440. mstrState = " Fitting Width and Height. ";
  441. //Width
  442. std::vector<iv::widthabcd> xvectorwidthabcd;
  443. bComplete = false;
  444. ncurpos = 0;
  445. nrange = xvectorvl.size();
  446. double flanewidtherror = 0.1; //1cm
  447. if(nrange <2)
  448. {
  449. mstrState = " No Valid Data. Complete.";
  450. mnProc = 100;
  451. return 0;
  452. }
  453. while(bComplete == false)
  454. {
  455. double ele_coff[4];
  456. for(j=0;j<4;j++)ele_coff[j] = 0;
  457. if(nrange>0)ele_coff[0] = xvectorvehicle[xvectorvl[ncurpos].mvehindex].flanewidth;
  458. int M = nrange;
  459. VectorXd x_vehhg(M);
  460. VectorXd y_vehhg(M);
  461. for(j=0;j<M;j++)
  462. {
  463. x_vehhg[j] = xvectorvehicle[xvectorvl[ncurpos+j].mvehindex].s - xvectorvehicle[xvectorvl[ncurpos].mvehindex].s;
  464. y_vehhg[j] =xvectorvehicle[xvectorvl[ncurpos+j].mvehindex].flanewidth;
  465. }
  466. int MX = 3;
  467. if(M<4)MX = M -1;
  468. if(MX>3)MX = 3;
  469. if(MX>0)
  470. {
  471. VectorXd coeffs = polyfit(x_vehhg, y_vehhg, MX);
  472. for(j=0;j<=MX;j++)
  473. {
  474. ele_coff[j] = coeffs[j];
  475. }
  476. }
  477. double ferror = 0;
  478. for(j=0;j<M;j++)
  479. {
  480. double s = x_vehhg[j];
  481. double fvalue = fabs(ele_coff[0] + ele_coff[1]*s + ele_coff[2]*s*s+ele_coff[3]*s*s*s - y_vehhg[j]);
  482. if(fvalue>ferror)ferror = fvalue;
  483. }
  484. if(ferror>flanewidtherror)
  485. {
  486. if(nrange>10)nrange = nrange/2;
  487. else nrange = nrange -1;
  488. }
  489. else
  490. {
  491. iv::widthabcd xabcd;
  492. xabcd.s = xvectorvehicle[xvectorvl[ncurpos].mvehindex].s;
  493. xabcd.A = ele_coff[0];
  494. xabcd.B = ele_coff[1];
  495. xabcd.C = ele_coff[2];
  496. xabcd.D = ele_coff[3];
  497. xvectorwidthabcd.push_back(xabcd);
  498. ncurpos = ncurpos + nrange-1;
  499. nrange = xvectorvl.size() - ncurpos;
  500. }
  501. if(ncurpos>=(int)(xvectorvl.size()-1))
  502. {
  503. bComplete = true;
  504. }
  505. }
  506. std::vector<iv::eleabcd> xvectoreleabcd;
  507. bComplete = false;
  508. ncurpos = 0;
  509. nrange = xvectorvl.size();
  510. double feleerror = 0.5; //1cm
  511. while(bComplete == false)
  512. {
  513. double ele_coff[4];
  514. for(j=0;j<4;j++)ele_coff[j] = 0;
  515. if(nrange>0)ele_coff[0] = xvectorvehicle[xvectorvl[ncurpos].mvehindex].height;
  516. int M = nrange;
  517. VectorXd x_vehhg(M);
  518. VectorXd y_vehhg(M);
  519. for(j=0;j<M;j++)
  520. {
  521. x_vehhg[j] = xvectorvehicle[xvectorvl[ncurpos+j].mvehindex].s - xvectorvehicle[xvectorvl[ncurpos].mvehindex].s;
  522. y_vehhg[j] =xvectorvehicle[xvectorvl[ncurpos+j].mvehindex].height;
  523. }
  524. int MX = 3;
  525. if(M<4)MX = M -1;
  526. if(MX>3)MX = 3;
  527. if(MX>0)
  528. {
  529. VectorXd coeffs = polyfit(x_vehhg, y_vehhg, MX);
  530. for(j=0;j<=MX;j++)
  531. {
  532. ele_coff[j] = coeffs[j];
  533. }
  534. }
  535. double ferror = 0;
  536. for(j=0;j<M;j++)
  537. {
  538. double s = x_vehhg[j];
  539. double fvalue = fabs(ele_coff[0] + ele_coff[1]*s + ele_coff[2]*s*s+ele_coff[3]*s*s*s - y_vehhg[j]);
  540. if(fvalue>feleerror)ferror = fvalue;
  541. }
  542. if(ferror>flanewidtherror)
  543. {
  544. if(nrange>10)nrange = nrange/2;
  545. else nrange = nrange -1;
  546. }
  547. else
  548. {
  549. iv::eleabcd xabcd;
  550. xabcd.s = xvectorvehicle[xvectorvl[ncurpos].mvehindex].s;
  551. xabcd.A = ele_coff[0];
  552. xabcd.B = ele_coff[1];
  553. xabcd.C = ele_coff[2];
  554. xabcd.D = ele_coff[3];
  555. xvectoreleabcd.push_back(xabcd);
  556. ncurpos = ncurpos + nrange-1;
  557. nrange = xvectorvl.size() - ncurpos;
  558. }
  559. if(ncurpos>=(int)(xvectorvl.size()-1))
  560. {
  561. bComplete = true;
  562. }
  563. }
  564. std::cout<<" last s: "<<xvectorvehicle[xvectorvl[nvlsize-1].mvehindex].s<<std::endl;
  565. std::cout<<" geo size "<<xvectorgeo.size()<<std::endl;
  566. std::cout<<"complete "<<std::endl;
  567. #ifndef INNDSPROC
  568. mpxodr->AddRoad("",xvectorvehicle[xvectorvl[nvlsize-1].mvehindex].s, QString::number(gw->CreateRoadID()).toStdString(),"-1");
  569. #else
  570. mpxodr->AddRoad("",xvectorvehicle[xvectorvl[nvlsize-1].mvehindex].s, QString::number(1).toStdString(),"-1");
  571. #endif
  572. Road * p = mpxodr->GetRoad(mpxodr->GetRoadCount() - 1);
  573. // p->AddElevation(0,xlaneheightcoff.A,xlaneheightcoff.B,xlaneheightcoff.C,xlaneheightcoff.D);
  574. double s = 0;
  575. j= 0;
  576. // for(j=0;j<4;j++)
  577. for(j=0;j<xvectorgeo.size();j++)
  578. {
  579. p->AddGeometryBlock();
  580. GeometryBlock * pgb = p->GetGeometryBlock(j);
  581. geobase * pline;
  582. geobase * pbez;
  583. geobase * parc;
  584. switch(xvectorgeo[j].mnType)
  585. {
  586. case 0:
  587. pline = &xvectorgeo[j];
  588. pgb->AddGeometryLine(s,pline->mfX,pline->mfY,pline->mfHdg,pline->mfLen);
  589. break;
  590. case 1:
  591. parc = &xvectorgeo[j];
  592. pgb->AddGeometryArc(s,parc->mfX,parc->mfY,parc->mfHdgStart,parc->mfLen,1.0/parc->mR);
  593. break;
  594. case 2:
  595. pbez = &xvectorgeo[j];
  596. std::cout<<"u0:"<<pbez->mfu[0]<<std::endl;
  597. pgb->AddGeometryParamPoly3(s,pbez->mfX,pbez->mfY,
  598. pbez->mfHdg,pbez->mfLen,pbez->mfu[0],
  599. pbez->mfu[1],pbez->mfu[2],pbez->mfu[3],pbez->mfv[0],
  600. pbez->mfv[1],pbez->mfv[2],pbez->mfv[3]);
  601. break;
  602. }
  603. s = s + xvectorgeo[j].mfLen;
  604. }
  605. p->SetRoadLength(s);
  606. p->AddLaneSection(0);
  607. LaneSection * pLS = p->GetLaneSection(0);
  608. pLS->AddLane(0,0,"none",false);
  609. int noldtype = 0;
  610. int ntype = 0;
  611. Lane * pCenterLane = pLS->GetLastCenterLane();
  612. if(nleftlanecount == 0)
  613. {
  614. if(nvlsize > 0)
  615. {
  616. ntype = xvectorvehicle[xvectorvl[0].mvehindex].nleftlanetype;
  617. pCenterLane->AddRoadMarkRecord(xvectorvehicle[xvectorvl[0].mvehindex].s,roadmarktypetostr(ntype),"standard","white",0.15,"none");
  618. noldtype = ntype;
  619. }
  620. for(i=1;i<(int)(nvlsize-1);i++)
  621. {
  622. int ntype = xvectorvehicle[xvectorvl[i].mvehindex].nleftlanetype;
  623. if(ntype!= noldtype )
  624. {
  625. pCenterLane->AddRoadMarkRecord(xvectorvehicle[xvectorvl[i].mvehindex].s,roadmarktypetostr(ntype),"standard","white",0.15,"none");
  626. noldtype = ntype;
  627. }
  628. }
  629. }
  630. else
  631. {
  632. pCenterLane->AddRoadMarkRecord(0,"solid","standard","yellow",0.15,"none");
  633. p->AddLaneOffset(0,nleftlanecount*flaneavgwidth,0,0,0);
  634. int k;
  635. for(k=0;k<nleftlanecount;k++)
  636. {
  637. pLS->AddLane(-1,(-1)*(pLS->GetRightLaneCount()+1),"driving",false);
  638. Lane * pLane = pLS->GetLastRightLane();
  639. pLane->AddWidthRecord(0,flaneavgwidth,0,0,0);
  640. if(k != (nleftlanecount-1))
  641. {
  642. pLane->AddRoadMarkRecord(0,"broken","standard","standard",0.15,"none");
  643. }
  644. else
  645. {
  646. if(nvlsize > 0)
  647. {
  648. ntype = xvectorvehicle[xvectorvl[0].mvehindex].nleftlanetype;
  649. pLane->AddRoadMarkRecord(xvectorvehicle[xvectorvl[0].mvehindex].s,roadmarktypetostr(ntype),"standard","white",0.15,"none");
  650. noldtype = ntype;
  651. }
  652. for(i=1;i<(int)(nvlsize-1);i++)
  653. {
  654. int ntype = xvectorvehicle[xvectorvl[i].mvehindex].nleftlanetype;
  655. if(ntype!= noldtype )
  656. {
  657. pLane->AddRoadMarkRecord(xvectorvehicle[xvectorvl[i].mvehindex].s,roadmarktypetostr(ntype),"standard","white",0.15,"none");
  658. noldtype = ntype;
  659. }
  660. }
  661. }
  662. }
  663. }
  664. pLS->AddLane(-1,(-1)*(pLS->GetRightLaneCount()+1),"driving",false);
  665. Lane * pLane = pLS->GetLastRightLane();
  666. for(j=0;j<(int)xvectorwidthabcd.size();j++)
  667. {
  668. iv::widthabcd * pwa = &xvectorwidthabcd[j];
  669. pLane->AddWidthRecord(pwa->s,pwa->A,pwa->B,pwa->C,pwa->D);
  670. }
  671. for(j=0;j<(int)xvectoreleabcd.size();j++)
  672. {
  673. iv::eleabcd * pwa = &xvectoreleabcd[j];
  674. p->AddElevation(pwa->s,pwa->A,pwa->B,pwa->C,pwa->D);
  675. }
  676. noldtype = 0;
  677. ntype = 0;
  678. if(nvlsize > 0)
  679. {
  680. ntype = xvectorvehicle[xvectorvl[0].mvehindex].nrightlanetype;
  681. pLane->AddRoadMarkRecord(xvectorvehicle[xvectorvl[0].mvehindex].s,roadmarktypetostr(ntype),"standard","white",0.15,"none");
  682. noldtype = ntype;
  683. }
  684. for(i=1;i<(int)(nvlsize-1);i++)
  685. {
  686. int ntype = xvectorvehicle[xvectorvl[i].mvehindex].nrightlanetype;
  687. if(ntype!= noldtype )
  688. {
  689. pLane->AddRoadMarkRecord(xvectorvehicle[xvectorvl[i].mvehindex].s,roadmarktypetostr(ntype),"standard","white",0.15,"none");
  690. noldtype = ntype;
  691. }
  692. }
  693. for(i=0;i<nrightlanecount;i++)
  694. {
  695. pLS->AddLane(-1,(-1)*(pLS->GetRightLaneCount()+1),"driving",false);
  696. Lane * pLane = pLS->GetLastRightLane();
  697. pLane->AddWidthRecord(0,flaneavgwidth,0,0,0);
  698. if(i != (nrightlanecount-1))
  699. {
  700. pLane->AddRoadMarkRecord(0,"broken","standard","standard",0.15,"none");
  701. }
  702. else
  703. {
  704. pLane->AddRoadMarkRecord(0,"solid","standard","standard",0.15,"none");
  705. }
  706. }
  707. if(fExtend>0.1)
  708. {
  709. double endx,endy,endhdg;
  710. p->GetGeometryCoords(p->GetRoadLength()-0.001,endx,endy,endhdg);
  711. p->AddGeometryBlock();
  712. GeometryBlock * pgeob = p->GetLastAddedGeometryBlock();
  713. pgeob->AddGeometryLine(p->GetRoadLength(),endx,endy,endhdg,fExtend);
  714. p->SetRoadLength(p->GetRoadLength()+ fExtend);
  715. }
  716. mnProc = 100;
  717. mstrState = " Complete. ";
  718. return 0;
  719. }
  720. std::string NDSDataProc::roadmarktypetostr(int ntype)
  721. {
  722. switch (ntype) {
  723. case 1:
  724. return "none";
  725. break;
  726. case 2:
  727. return "broken";
  728. break;
  729. case 3:
  730. return "solid";
  731. break;
  732. default:
  733. return "none";
  734. break;
  735. }
  736. }