xvmainwindow2.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include "xvmainwindow2.h"
  2. #include "ui_xvmainwindow2.h"
  3. #include <QMessageBox>
  4. #include <QFileDialog>
  5. #include <string.h>
  6. #include "xodrfunc.h"
  7. #include "roaddigit.h"
  8. #include "xodrscenfunc.h"
  9. #define VIEW_WIDTH 5000
  10. #define VIEW_HEIGHT 5000
  11. static bool IsNaN(double dat)
  12. {
  13. qint64 & ref=*(qint64 *)&dat;
  14. return (ref&0x7FF0000000000000) == 0x7FF0000000000000 && (ref&0xfffffffffffff)!=0;
  15. }
  16. XVMainWindow::XVMainWindow(QWidget *parent) :
  17. QMainWindow(parent),
  18. ui(new Ui::XVMainWindow)
  19. {
  20. ui->setupUi(this);
  21. myview = new MyView(this);
  22. myview->setObjectName(QStringLiteral("graphicsView"));
  23. myview->setGeometry(QRect(30, 30, 600, 600));
  24. connect(myview,SIGNAL(dbclickxy(double,double)),this,SLOT(onClickXY(double,double)));
  25. connect(myview,SIGNAL(beishuchange(double)),this,SLOT(onbeishuchange(double)));
  26. myview->setCacheMode(myview->CacheBackground);
  27. mpscene = new QGraphicsScene(0,0,VIEW_WIDTH,VIEW_HEIGHT);
  28. mpscene->setBackgroundBrush(Qt::darkGreen);
  29. myview->setScene(mpscene);
  30. mfViewMoveX = VIEW_WIDTH/2.0;
  31. mfViewMoveY = (-1.0)*VIEW_HEIGHT/2.0;
  32. connect(&mFileDialog,SIGNAL(accepted()),this,SLOT(onFileOpen()));
  33. setWindowTitle("ADC OpenDrive Viewer2 (20230201)");
  34. }
  35. XVMainWindow::~XVMainWindow()
  36. {
  37. delete mpscene;
  38. delete myview;
  39. delete ui;
  40. }
  41. void XVMainWindow::resizeEvent(QResizeEvent *event)
  42. {
  43. qDebug("resize");
  44. QSize sizemain = ui->centralwidget->size();
  45. qDebug("size x = %d y=%d",sizemain.width(),sizemain.height());
  46. mfViewWidth = sizemain.width();
  47. mfViewHeight = sizemain.height() - 30;
  48. myview->setGeometry(0,30,sizemain.width(),sizemain.height()-30);
  49. }
  50. void XVMainWindow::on_actionLoad_triggered()
  51. {
  52. if(mxodr.GetRoadCount() > 0)
  53. {
  54. QMessageBox::StandardButton button;
  55. char strquest[256];
  56. snprintf(strquest,256,"Do you Want to Load New File ?");
  57. button=QMessageBox::question(this,"Move",strquest,QMessageBox::Yes|QMessageBox::No);
  58. if(button==QMessageBox::No)
  59. {
  60. return;
  61. }
  62. else if(button==QMessageBox::Yes)
  63. {
  64. }
  65. }
  66. #ifndef ANDROID
  67. QString strpath = QFileDialog::getOpenFileName(this,"Load XODR",".","*.xodr");
  68. if(strpath.isEmpty())return;
  69. LoadXODR(strpath);
  70. OpenDrive * pxodr = &mxodr;
  71. UpdateScene();
  72. #else
  73. // QMessageBox::warning(this,"warning","no file dialog.",QMessageBox::YesAll);
  74. // QString strpath = "/storage/emulated/0/map.xodr";
  75. mFileDialog.open();
  76. #endif
  77. }
  78. void XVMainWindow::LoadXODR(QString strpath)
  79. {
  80. mxodr.Clear();
  81. OpenDrive * pxodr = &mxodr; //because add to xodr,so don't delete
  82. OpenDriveXmlParser x(pxodr);
  83. if(!x.ReadFile(strpath.toStdString()))
  84. {
  85. QMessageBox::warning(this,"warn","Can't load xodr file.");
  86. return;
  87. }
  88. mvectorRoadSample.clear();
  89. int nroadnum = static_cast<int>(pxodr->GetRoadCount()) ;
  90. int i;
  91. double froadlen = 0;
  92. double fxmin,fxmax,fymin,fymax;
  93. fxmin = std::numeric_limits<double>::max() *(1.0);
  94. fxmax = std::numeric_limits<double>::max()*(-1.0);
  95. fymin = std::numeric_limits<double>::max() *(1.0);
  96. fymax = std::numeric_limits<double>::max()*(-1.0);
  97. for(i=0;i<nroadnum;i++)
  98. {
  99. Road * pRoad = pxodr->GetRoad(i);
  100. if(IsNaN(pRoad->GetRoadLength()))
  101. {
  102. pxodr->DeleteRoad(i);
  103. i--;
  104. nroadnum--;
  105. qDebug("delete road %s because length is NaN",pRoad->GetRoadId().data());
  106. }
  107. else
  108. {
  109. froadlen = froadlen + pRoad->GetRoadLength();
  110. iv::RoadSample xSample(pRoad);
  111. mvectorRoadSample.push_back(xSample);
  112. if(pRoad->GetGeometryBlockCount()>0)
  113. {
  114. double fx,fy;
  115. fx = pRoad->GetGeometryBlock(0)->GetGeometryAt(0)->GetX();
  116. fy = pRoad->GetGeometryBlock(0)->GetGeometryAt(0)->GetY();
  117. if(fx>fxmax)fxmax = fx;
  118. if(fx<fxmin)fxmin = fx;
  119. if(fy>fymax)fymax = fy;
  120. if(fy<fymin)fymin = fy;
  121. }
  122. }
  123. }
  124. double fmovex = 0;
  125. double fmovey = 0;
  126. if(((fxmax>1000)&&(fxmin>1000))||((fxmax<-1000)&&(fxmin<-1000)))
  127. {
  128. fmovex = (fxmax + fxmin)/2.0;
  129. }
  130. if(((fymax>1000)&&(fymin>1000))||((fymax<-1000)&&(fymin<-1000)))
  131. {
  132. fmovey = (fymax + fymin)/2.0;
  133. }
  134. mfViewMoveX = mfViewMoveX - fmovex;
  135. mfViewMoveY = mfViewMoveY - fmovey;
  136. qDebug("view move is %f",mfViewMoveX,mfViewMoveY);
  137. char strout[256];
  138. snprintf(strout,256,"Road count is %d. Total Len is %f",mxodr.GetRoadCount(),froadlen);
  139. QMessageBox::information(this,"Info",strout,QMessageBox::YesAll);
  140. }
  141. void XVMainWindow::UpdateScene()
  142. {
  143. int i;
  144. int nsize = mvectorviewitem.size();
  145. for(i=0;i<nsize;i++)
  146. {
  147. mpscene->removeItem(mvectorviewitem.at(i));
  148. delete mvectorviewitem.at(i);
  149. }
  150. mvectorviewitem.clear();
  151. nsize = static_cast<int>(mvectorRoadSample.size()) ;
  152. for(i=0;i<nsize;i++)
  153. {
  154. std::vector<QGraphicsPathItem *> xvectorlanepath = xodrscenfunc::GetRoadViewItem(&(mvectorRoadSample[i]));
  155. int j;
  156. int ncount = xvectorlanepath.size();
  157. for(j=0;j<ncount;j++)
  158. {
  159. QGraphicsPathItem * pitem = xvectorlanepath[j];
  160. // pitem->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  161. pitem->setPos(mfViewMoveX,-mfViewMoveY);
  162. mpscene->addItem(pitem);
  163. mvectorviewitem.push_back(pitem);
  164. }
  165. }
  166. return;
  167. nsize = mxodr.GetRoadCount();
  168. std::vector<RoadDigit> xvectorrd;
  169. for(i=0;i<nsize;i++)
  170. {
  171. RoadDigit xrd(mxodr.GetRoad(i),5.0); //Space Must <= Broken dot dis
  172. xvectorrd.push_back(xrd);
  173. }
  174. for(i=0;i<nsize;i++)
  175. {
  176. std::vector<QGraphicsPathItem *> xvectorlanepath = xodrscenfunc::GetRoadLaneItem(&(xvectorrd[i]));
  177. int j;
  178. int ncount = xvectorlanepath.size();
  179. for(j=0;j<ncount;j++)
  180. {
  181. QGraphicsPathItem * pitem = xvectorlanepath[j];
  182. // pitem->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  183. pitem->setPos(mfViewMoveX,-mfViewMoveY);
  184. mpscene->addItem(pitem);
  185. mvectorviewitem.push_back(pitem);
  186. }
  187. }
  188. for(i=0;i<nsize;i++)
  189. {
  190. std::vector<QGraphicsPathItem *> xvectormarkpath = xodrscenfunc::GetRoadMarkItem(&(xvectorrd[i]));
  191. int j;
  192. int ncount = xvectormarkpath.size();
  193. for(j=0;j<ncount;j++)
  194. {
  195. QGraphicsPathItem * pitem = xvectormarkpath[j];
  196. // pitem->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  197. pitem->setPos(mfViewMoveX,-mfViewMoveY);
  198. mpscene->addItem(pitem);
  199. mvectorviewitem.push_back(pitem);
  200. }
  201. }
  202. }
  203. void XVMainWindow::on_actionZoom_In_triggered()
  204. {
  205. myview->zoomIn();
  206. }
  207. void XVMainWindow::on_actionZoom_Out_triggered()
  208. {
  209. myview->zoomOut();
  210. }
  211. void XVMainWindow::on_actionZoom_One_triggered()
  212. {
  213. myview->zoomone();
  214. }
  215. void XVMainWindow::onClickXY(double x, double y)
  216. {
  217. double selx,sely;
  218. double lon,lat;
  219. // qDebug(" x is %f y is %f",x,y);
  220. // selx = (x - (1.0/mfbeishu) * VIEW_WIDTH/2);
  221. // sely = (y - (1.0/mfbeishu) *VIEW_HEIGHT/2) * (-1);
  222. // qDebug("beishu is %f ",mfbeishu);
  223. // selx = (x - (1.0/mfbeishu) *(mfViewWidth/2) + (mfViewWidth/2)) ;
  224. // sely = (y - (1.0/mfbeishu) *(mfViewHeight/2) + (mfViewHeight/2)) * (-1);
  225. selx = x - VIEW_WIDTH/2;
  226. sely = (y - VIEW_HEIGHT/2)*(-1);
  227. mfselx = selx *1.0/mfbeishu ;
  228. mfsely = sely *1.0/mfbeishu;
  229. // selx = x;
  230. // sely = y;
  231. // mfselx = selx;
  232. // mfsely = sely;
  233. // double x0,y0;
  234. // GaussProjCal(glon0,glat0,&x0,&y0);
  235. // GaussProjInvCal(x0+selx,y0+sely,&lon,&lat);
  236. double rel_x,rel_y;
  237. rel_x = selx - mfViewMoveX + VIEW_WIDTH/2 ;
  238. rel_y = sely - mfViewMoveY - VIEW_HEIGHT/2;
  239. qDebug("selx is %f sely is %f ",rel_x,rel_y);
  240. unsigned int nRoadCount = static_cast<unsigned int>(mvectorRoadSample.size());
  241. unsigned int i;
  242. int64_t time1 = std::chrono::system_clock::now().time_since_epoch().count();
  243. bool bFind = false;
  244. char strout[1000];
  245. for(i=0;i<nRoadCount;i++)
  246. {
  247. double s,t;
  248. int nlane;
  249. std::string strlanetype;
  250. if(mvectorRoadSample[i].PointInRoad(rel_x,rel_y,s,t,nlane,strlanetype))
  251. {
  252. std::cout<<" In Road"<<mvectorRoadSample[i].GetRoadID()<<" s:"<<s<<" t:"<<t<<" nlane:"<<nlane<<std::endl;
  253. if(bFind == false)
  254. {
  255. snprintf(strout,1000,"x:%7.3f y:%7.3f |Road:%s s:%f t:%f nlane:%d %s",
  256. rel_x,rel_y,mvectorRoadSample[i].GetRoadID().data(),s,t,nlane,strlanetype.data());
  257. // mpLabel_Status->setText(strout);
  258. bFind = true;
  259. }
  260. else
  261. {
  262. char strtem[1000];
  263. snprintf(strtem,1000,"| %s %d",mvectorRoadSample[i].GetRoadID().data(),nlane);
  264. strncat(strout,strtem,1000);
  265. }
  266. }
  267. }
  268. if(bFind == false)
  269. {
  270. snprintf(strout,1000,"x:%7.3f y:%7.3f",rel_x,rel_y);
  271. }
  272. ui->statusbar->showMessage(strout,10000);
  273. int64_t time2 = std::chrono::system_clock::now().time_since_epoch().count();
  274. std::cout<<" find use time: "<<(time2 - time1)<<std::endl;
  275. return;
  276. Road * pRoad = 0;
  277. GeometryBlock * pgeob;
  278. double fdis,nearx,neary,hdg;
  279. double fs;
  280. int nlane;
  281. if(xodrfunc::GetNearPoint(rel_x,rel_y,&mxodr,&pRoad,&pgeob,fdis,nearx,neary,hdg,50,&fs,&nlane) == 0)
  282. {
  283. qDebug("s:%f dis is %f nlane is %d",fs,fdis,nlane);
  284. char strout[1000];
  285. snprintf(strout,1000,"Road:%s s:%f dis:%f nlane:%d",pRoad->GetRoadId().data(),fs,fdis,nlane);
  286. // mpLabel_Status->setText(strout);
  287. ui->statusbar->showMessage(strout,10000);
  288. }
  289. else
  290. {
  291. char strout[1000];
  292. snprintf(strout,1000,"Click x:%f y:%f",rel_x,rel_y);
  293. ui->statusbar->showMessage(strout,10000);
  294. qDebug("not found near road.");
  295. }
  296. }
  297. void XVMainWindow::on_actionSet_Move_triggered()
  298. {
  299. QMessageBox::StandardButton button;
  300. char strquest[256];
  301. snprintf(strquest,256,"Want to Move Center to x:%f y:%f ?",-(mfViewMoveX - mfselx),-(mfViewMoveY-mfsely));
  302. button=QMessageBox::question(this,"Move",strquest,QMessageBox::Yes|QMessageBox::No);
  303. if(button==QMessageBox::No)
  304. {
  305. return;
  306. }
  307. else if(button==QMessageBox::Yes)
  308. {
  309. }
  310. mfViewMoveX = mfViewMoveX - mfselx;
  311. mfViewMoveY = mfViewMoveY - mfsely;
  312. int nsize = mvectorviewitem.size();
  313. int i;
  314. for(i=0;i<nsize;i++)
  315. {
  316. mpscene->removeItem(mvectorviewitem.at(i));
  317. }
  318. for(i=0;i<nsize;i++)
  319. {
  320. // mvectorviewitem.at(i)->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  321. mvectorviewitem.at(i)->setPos(mfViewMoveX ,-mfViewMoveY);
  322. mpscene->addItem(mvectorviewitem.at(i));
  323. }
  324. myview->zoomIn();
  325. myview->zoomOut();
  326. }
  327. void XVMainWindow::on_actionReset_Move_triggered()
  328. {
  329. mfViewMoveX = 0;
  330. mfViewMoveY = 0;
  331. int nsize = mvectorviewitem.size();
  332. int i;
  333. for(i=0;i<nsize;i++)
  334. {
  335. mpscene->removeItem(mvectorviewitem.at(i));
  336. }
  337. for(i=0;i<nsize;i++)
  338. {
  339. mvectorviewitem.at(i)->setPos(mfViewMoveX ,-mfViewMoveY);
  340. // mvectorviewitem.at(i)->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  341. mpscene->addItem(mvectorviewitem.at(i));
  342. }
  343. myview->zoomIn();
  344. myview->zoomOut();
  345. }
  346. void XVMainWindow::paintEvent(QPaintEvent * event)
  347. {
  348. if(mbRefresh)
  349. {
  350. myview->setScene(mpscene);
  351. mbRefresh = false;
  352. }
  353. }
  354. void XVMainWindow::onFileOpen()
  355. {
  356. QString strpath = mFileDialog.currentFile();
  357. if(strpath.isEmpty())return;
  358. LoadXODR(strpath);
  359. OpenDrive * pxodr = &mxodr;
  360. UpdateScene();
  361. }
  362. void XVMainWindow::on_actionHelp_triggered()
  363. {
  364. QString helpinfo = tr("Load:加载文件(后缀名为.xodr)\nZoom In:放大\nZomm Out:缩小\nZoom One:恢复默认视图\nSet Move:移动显示中心\n"
  365. "Reset Move:恢复默认显示中心\n\ntips: 在屏幕上双击选择道路(查看道路id)或者选择点(用来移动中心点 \n "
  366. "在屏幕上可以移动查看区域");
  367. QMessageBox::information(this,"Help",helpinfo,QMessageBox::Yes);
  368. }
  369. void XVMainWindow::onbeishuchange(double fbeishu)
  370. {
  371. mfbeishu = fbeishu;
  372. }