xvmainwindow.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "xvmainwindow.h"
  2. #include "ui_xvmainwindow.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 Viewer");
  34. }
  35. XVMainWindow::~XVMainWindow()
  36. {
  37. delete ui;
  38. }
  39. void XVMainWindow::resizeEvent(QResizeEvent *event)
  40. {
  41. qDebug("resize");
  42. QSize sizemain = ui->centralwidget->size();
  43. qDebug("size x = %d y=%d",sizemain.width(),sizemain.height());
  44. mfViewWidth = sizemain.width();
  45. mfViewHeight = sizemain.height() - 30;
  46. myview->setGeometry(0,30,sizemain.width(),sizemain.height()-30);
  47. }
  48. void XVMainWindow::on_actionLoad_triggered()
  49. {
  50. if(mxodr.GetRoadCount() > 0)
  51. {
  52. QMessageBox::StandardButton button;
  53. char strquest[256];
  54. snprintf(strquest,256,"Do you Want to Load New File ?");
  55. button=QMessageBox::question(this,"Move",strquest,QMessageBox::Yes|QMessageBox::No);
  56. if(button==QMessageBox::No)
  57. {
  58. return;
  59. }
  60. else if(button==QMessageBox::Yes)
  61. {
  62. }
  63. }
  64. #ifndef ANDROID
  65. QString strpath = QFileDialog::getOpenFileName(this,"Load XODR",".","*.xodr");
  66. if(strpath.isEmpty())return;
  67. LoadXODR(strpath);
  68. UpdateScene();
  69. #else
  70. // QMessageBox::warning(this,"warning","no file dialog.",QMessageBox::YesAll);
  71. // QString strpath = "/storage/emulated/0/map.xodr";
  72. mFileDialog.open();
  73. #endif
  74. }
  75. void XVMainWindow::LoadXODR(QString strpath)
  76. {
  77. mxodr.Clear();
  78. OpenDrive * pxodr = &mxodr; //because add to xodr,so don't delete
  79. OpenDriveXmlParser x(pxodr);
  80. if(!x.ReadFile(strpath.toStdString()))
  81. {
  82. QMessageBox::warning(this,"warn","Can't load xodr file.");
  83. return;
  84. }
  85. int nroadnum = pxodr->GetRoadCount();
  86. int i;
  87. double froadlen = 0;
  88. double fxmin,fxmax,fymin,fymax;
  89. fxmin = std::numeric_limits<double>::max() *(1.0);
  90. fxmax = std::numeric_limits<double>::max()*(-1.0);
  91. fymin = std::numeric_limits<double>::max() *(1.0);
  92. fymax = std::numeric_limits<double>::max()*(-1.0);
  93. for(i=0;i<nroadnum;i++)
  94. {
  95. Road * pRoad = pxodr->GetRoad(i);
  96. if(IsNaN(pRoad->GetRoadLength()))
  97. {
  98. pxodr->DeleteRoad(i);
  99. i--;
  100. nroadnum--;
  101. qDebug("delete road %s because length is NaN",pRoad->GetRoadId().data());
  102. }
  103. else
  104. {
  105. froadlen = froadlen + pRoad->GetRoadLength();
  106. if(pRoad->GetGeometryBlockCount()>0)
  107. {
  108. double fx,fy;
  109. fx = pRoad->GetGeometryBlock(0)->GetGeometryAt(0)->GetX();
  110. fy = pRoad->GetGeometryBlock(0)->GetGeometryAt(0)->GetY();
  111. if(fx>fxmax)fxmax = fx;
  112. if(fx<fxmin)fxmin = fx;
  113. if(fy>fymax)fymax = fy;
  114. if(fy<fymin)fymin = fy;
  115. }
  116. }
  117. }
  118. double fmovex = 0;
  119. double fmovey = 0;
  120. if(((fxmax>1000)&&(fxmin>1000))||((fxmax<-1000)&&(fxmin<-1000)))
  121. {
  122. fmovex = (fxmax + fxmin)/2.0;
  123. }
  124. if(((fymax>1000)&&(fymin>1000))||((fymax<-1000)&&(fymin<-1000)))
  125. {
  126. fmovey = (fymax + fymin)/2.0;
  127. }
  128. mfViewMoveX = mfViewMoveX - fmovex;
  129. mfViewMoveY = mfViewMoveY - fmovey;
  130. qDebug("view move is %f",mfViewMoveX,mfViewMoveY);
  131. char strout[256];
  132. snprintf(strout,256,"Road count is %d. Total Len is %f",mxodr.GetRoadCount(),froadlen);
  133. QMessageBox::information(this,"Info",strout,QMessageBox::YesAll);
  134. }
  135. void XVMainWindow::UpdateScene()
  136. {
  137. int i;
  138. int nsize = mvectorviewitem.size();
  139. for(i=0;i<nsize;i++)
  140. {
  141. mpscene->removeItem(mvectorviewitem.at(i));
  142. delete mvectorviewitem.at(i);
  143. }
  144. mvectorviewitem.clear();
  145. nsize = mxodr.GetRoadCount();
  146. std::vector<RoadDigit> xvectorrd;
  147. for(i=0;i<nsize;i++)
  148. {
  149. RoadDigit xrd(mxodr.GetRoad(i),10.0);
  150. xvectorrd.push_back(xrd);
  151. }
  152. for(i=0;i<nsize;i++)
  153. {
  154. std::vector<QGraphicsPathItem *> xvectorlanepath = xodrscenfunc::GetRoadLaneItem(&(xvectorrd[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. for(i=0;i<nsize;i++)
  167. {
  168. std::vector<QGraphicsPathItem *> xvectormarkpath = xodrscenfunc::GetRoadMarkItem(&(xvectorrd[i]));
  169. int j;
  170. int ncount = xvectormarkpath.size();
  171. for(j=0;j<ncount;j++)
  172. {
  173. QGraphicsPathItem * pitem = xvectormarkpath[j];
  174. // pitem->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  175. pitem->setPos(mfViewMoveX,-mfViewMoveY);
  176. mpscene->addItem(pitem);
  177. mvectorviewitem.push_back(pitem);
  178. }
  179. }
  180. }
  181. void XVMainWindow::on_actionZoom_In_triggered()
  182. {
  183. myview->zoomIn();
  184. }
  185. void XVMainWindow::on_actionZoom_Out_triggered()
  186. {
  187. myview->zoomOut();
  188. }
  189. void XVMainWindow::on_actionZoom_One_triggered()
  190. {
  191. myview->zoomone();
  192. }
  193. void XVMainWindow::onClickXY(double x, double y)
  194. {
  195. double selx,sely;
  196. double lon,lat;
  197. // qDebug(" x is %f y is %f",x,y);
  198. // selx = (x - (1.0/mfbeishu) * VIEW_WIDTH/2);
  199. // sely = (y - (1.0/mfbeishu) *VIEW_HEIGHT/2) * (-1);
  200. // qDebug("beishu is %f ",mfbeishu);
  201. // selx = (x - (1.0/mfbeishu) *(mfViewWidth/2) + (mfViewWidth/2)) ;
  202. // sely = (y - (1.0/mfbeishu) *(mfViewHeight/2) + (mfViewHeight/2)) * (-1);
  203. selx = x - VIEW_WIDTH/2;
  204. sely = (y - VIEW_HEIGHT/2)*(-1);
  205. mfselx = selx *1.0/mfbeishu ;
  206. mfsely = sely *1.0/mfbeishu;
  207. qDebug("selx is %f sely is %f ",mfselx,mfsely);
  208. // selx = x;
  209. // sely = y;
  210. // mfselx = selx;
  211. // mfsely = sely;
  212. // double x0,y0;
  213. // GaussProjCal(glon0,glat0,&x0,&y0);
  214. // GaussProjInvCal(x0+selx,y0+sely,&lon,&lat);
  215. double rel_x,rel_y;
  216. rel_x = selx - mfViewMoveX + VIEW_WIDTH/2 ;
  217. rel_y = sely - mfViewMoveY - VIEW_HEIGHT/2;
  218. Road * pRoad = 0;
  219. GeometryBlock * pgeob;
  220. double fdis,nearx,neary,hdg;
  221. double fs;
  222. int nlane;
  223. if(xodrfunc::GetNearPoint(rel_x,rel_y,&mxodr,&pRoad,&pgeob,fdis,nearx,neary,hdg,50,&fs,&nlane) == 0)
  224. {
  225. qDebug("s:%f dis is %f nlane is %d",fs,fdis,nlane);
  226. char strout[1000];
  227. snprintf(strout,1000,"Road:%s s:%f dis:%f nlane:%d",pRoad->GetRoadId().data(),fs,fdis,nlane);
  228. // mpLabel_Status->setText(strout);
  229. ui->statusbar->showMessage(strout,10000);
  230. }
  231. else
  232. {
  233. char strout[1000];
  234. snprintf(strout,1000,"Click x:%f y:%f",rel_x,rel_y);
  235. ui->statusbar->showMessage(strout,10000);
  236. qDebug("not found near road.");
  237. }
  238. }
  239. void XVMainWindow::on_actionSet_Move_triggered()
  240. {
  241. QMessageBox::StandardButton button;
  242. char strquest[256];
  243. snprintf(strquest,256,"Want to Move Center to x:%f y:%f ?",-(mfViewMoveX - mfselx),-(mfViewMoveY-mfsely));
  244. button=QMessageBox::question(this,"Move",strquest,QMessageBox::Yes|QMessageBox::No);
  245. if(button==QMessageBox::No)
  246. {
  247. return;
  248. }
  249. else if(button==QMessageBox::Yes)
  250. {
  251. }
  252. mfViewMoveX = mfViewMoveX - mfselx;
  253. mfViewMoveY = mfViewMoveY - mfsely;
  254. int nsize = mvectorviewitem.size();
  255. int i;
  256. for(i=0;i<nsize;i++)
  257. {
  258. mpscene->removeItem(mvectorviewitem.at(i));
  259. }
  260. for(i=0;i<nsize;i++)
  261. {
  262. // mvectorviewitem.at(i)->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  263. mvectorviewitem.at(i)->setPos(mfViewMoveX ,-mfViewMoveY);
  264. mpscene->addItem(mvectorviewitem.at(i));
  265. }
  266. myview->zoomIn();
  267. myview->zoomOut();
  268. }
  269. void XVMainWindow::on_actionReset_Move_triggered()
  270. {
  271. mfViewMoveX = 0;
  272. mfViewMoveY = 0;
  273. int nsize = mvectorviewitem.size();
  274. int i;
  275. for(i=0;i<nsize;i++)
  276. {
  277. mpscene->removeItem(mvectorviewitem.at(i));
  278. }
  279. for(i=0;i<nsize;i++)
  280. {
  281. mvectorviewitem.at(i)->setPos(mfViewMoveX ,-mfViewMoveY);
  282. // mvectorviewitem.at(i)->setPos(mfViewMoveX +VIEW_WIDTH/2,-mfViewMoveY+VIEW_HEIGHT/2);
  283. mpscene->addItem(mvectorviewitem.at(i));
  284. }
  285. myview->zoomIn();
  286. myview->zoomOut();
  287. }
  288. void XVMainWindow::paintEvent(QPaintEvent * event)
  289. {
  290. if(mbRefresh)
  291. {
  292. myview->setScene(mpscene);
  293. mbRefresh = false;
  294. }
  295. }
  296. void XVMainWindow::onFileOpen()
  297. {
  298. QString strpath = mFileDialog.currentFile();
  299. if(strpath.isEmpty())return;
  300. LoadXODR(strpath);
  301. UpdateScene();
  302. }
  303. void XVMainWindow::on_actionHelp_triggered()
  304. {
  305. QString helpinfo = tr("Load:加载文件(后缀名为.xodr)\nZoom In:放大\nZomm Out:缩小\nZoom One:恢复默认视图\nSet Move:移动显示中心\n"
  306. "Reset Move:恢复默认显示中心\n\ntips: 在屏幕上双击选择道路(查看道路id)或者选择点(用来移动中心点 \n "
  307. "在屏幕上可以移动查看区域");
  308. QMessageBox::information(this,"Help",helpinfo,QMessageBox::Yes);
  309. }
  310. void XVMainWindow::onbeishuchange(double fbeishu)
  311. {
  312. mfbeishu = fbeishu;
  313. }