roadeditdialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "roadeditdialog.h"
  2. #include "ui_roadeditdialog.h"
  3. #include "math.h"
  4. #define VIEW_WIDTH 850
  5. #define VIEW_HEIGHT 350
  6. RoadEditDialog::RoadEditDialog(OpenDrive * pxodr,QWidget *parent) :
  7. QDialog(parent),
  8. ui(new Ui::RoadEditDialog)
  9. {
  10. mpxodr = pxodr;
  11. ui->setupUi(this);
  12. myview = new MyView(this);
  13. myview->setObjectName(QStringLiteral("graphicsView"));
  14. myview->setGeometry(QRect(30, 400, 900, 400));
  15. connect(myview,SIGNAL(dbclickxy(double,double)),this,SLOT(onClickXY(double,double)));
  16. image = new QImage(VIEW_WIDTH, VIEW_HEIGHT, QImage::Format_RGB32);//画布的初始化大小设为300*300,使用32位颜色
  17. myview->setCacheMode(myview->CacheBackground);
  18. painter = new QPainter(image);
  19. painter->end();
  20. scene = new QGraphicsScene;
  21. ui->comboBox_geotype->addItem("Line");
  22. ui->comboBox_geotype->addItem("Spiral");
  23. ui->comboBox_geotype->addItem("Arc");
  24. ui->comboBox_geotype->addItem("Poly");
  25. ui->comboBox_geotype->addItem("Param Poly");
  26. int i;
  27. int nroadcount = mpxodr->GetRoadCount();
  28. for(i=0;i<nroadcount;i++)
  29. {
  30. const char * strname = mpxodr->GetRoad(i)->GetRoadId().data();
  31. ui->comboBox_Road->addItem(strname);
  32. }
  33. }
  34. RoadEditDialog::~RoadEditDialog()
  35. {
  36. delete ui;
  37. }
  38. void RoadEditDialog::ExecPainter()
  39. {
  40. painter->begin(image);
  41. int nkeep = 30;
  42. Road * pRoad = mpCurRoad;
  43. // qDebug("time is %d",x.elapsed());
  44. image->fill(QColor(255, 255, 255));//对画布进行填充
  45. // std::vector<iv::GPSData> navigation_data = brain->navigation_data;
  46. painter->setRenderHint(QPainter::Antialiasing, true);//设置反锯齿模式,好看一点
  47. // painter->translate(mnMoveX,mnMoveY);
  48. // painter->translate(VIEW_WIDTH/2,VIEW_HEIGHT/2);
  49. double froad_xmin,froad_ymin,froad_xmax,froad_ymax;
  50. int nfac = 1;
  51. ServiceXODRTool.GetRoadMaxMin(pRoad,froad_xmin,froad_ymin,froad_xmax,froad_ymax);
  52. double fac_x,fac_y;
  53. fac_x = 100000;
  54. fac_y = 100000;
  55. if(froad_xmax > froad_xmin)
  56. {
  57. fac_x = (VIEW_WIDTH-nkeep*2)/(froad_xmax - froad_xmin);
  58. }
  59. if(froad_ymax > froad_ymin)
  60. {
  61. fac_y = (VIEW_HEIGHT - nkeep*2)/(froad_ymax - froad_ymin);
  62. }
  63. nfac = fac_x;
  64. if(fac_y < nfac)nfac = fac_y;
  65. painter->translate(nkeep,VIEW_HEIGHT-nkeep);
  66. painter->setPen(Qt::black);
  67. // painter->drawLine(VIEW_WIDTH/(-2),0,VIEW_WIDTH/2,0);
  68. // painter->drawLine(0,VIEW_HEIGHT/(-2),0,VIEW_HEIGHT/2);
  69. int i;
  70. // int nfac = 5;;
  71. painter->setPen(Qt::blue);
  72. // if(mbClick)
  73. // {
  74. // painter->setPen(Qt::red);
  75. // painter->drawEllipse(QPoint(mClickX ,mClickY),mnMarkSize,mnMarkSize);
  76. // painter->setPen(Qt::black);
  77. // }
  78. // if(mbSetObj)
  79. // {
  80. // painter->setPen(Qt::green);
  81. // painter->drawRect(mfObjX*nfac-mnMarkSize,mfObjY*nfac*(-1)-mnMarkSize,mnMarkSize*2,mnMarkSize*2);
  82. // painter->setPen(Qt::black);
  83. // }
  84. painter->setPen(Qt::green);
  85. double x0,y0;
  86. // GaussProjCal(glon0,glat0,&x0,&y0);
  87. painter->setPen(Qt::blue);
  88. // int nfac = nfac;
  89. // int selid = mpCBRoad->currentText().toInt();
  90. // continue;
  91. int j;
  92. painter->setPen(Qt::blue);
  93. for(j=0;j<pRoad->GetGeometryBlockCount();j++)
  94. {
  95. GeometryBlock * pgeob = pRoad->GetGeometryBlock(j);
  96. double x,y;
  97. double x_center,y_center;
  98. double R;
  99. RoadGeometry * pg;
  100. GeometryArc * parc;
  101. GeometryParamPoly3 * ppp3;
  102. GeometrySpiral *pSpiral;
  103. double rel_x,rel_y,rel_hdg;
  104. pg = pgeob->GetGeometryAt(0);
  105. x = pg->GetX();
  106. y = pg->GetY();
  107. painter->setPen(Qt::blue);
  108. if(j == mnSelGeo)
  109. {
  110. painter->setPen(Qt::green);
  111. }
  112. // if(j== 0)
  113. // {
  114. // if(selid == atoi(pRoad->GetRoadId().data()))
  115. // {
  116. // painter->setPen(Qt::green);
  117. // painter->drawEllipse(x*nfac-5,y*nfac*(-1)-5,10,10);
  118. // painter->setPen(Qt::red);
  119. // }
  120. // }
  121. switch (pg->GetGeomType()) {
  122. case 0:
  123. painter->drawLine(QPoint((x-froad_xmin)*nfac,(y-froad_ymin)*nfac*(-1)),
  124. QPoint(((x-froad_xmin) + pg->GetLength() * cos(pg->GetHdg()))*nfac,((y-froad_ymin) + pg->GetLength() * sin(pg->GetHdg()))*nfac*(-1)));
  125. break;
  126. case 1:
  127. pSpiral = (GeometrySpiral * )pg;
  128. {
  129. int ncount = pSpiral->GetLength() * nfac;
  130. double sstep = pSpiral->GetLength()/((double)ncount);
  131. int k;
  132. double x0,y0,hdg0,s0;
  133. x0 = pSpiral->GetX();
  134. y0 = pSpiral->GetY();
  135. s0 = pSpiral->GetS();
  136. hdg0 = pSpiral->GetHdg() ;
  137. painter->setPen(Qt::red);
  138. for(k=0;k<ncount;k++)
  139. {
  140. pSpiral->GetCoords(s0+sstep*k,rel_x,rel_y,rel_hdg);
  141. x = rel_x - froad_xmin;
  142. y = rel_y - froad_ymin;
  143. painter->drawPoint((int)(x*nfac),(int)(y*(-1.0*nfac)));
  144. }
  145. painter->setPen(Qt::blue);
  146. }
  147. // qDebug("spi");
  148. break;
  149. case 2:
  150. {
  151. parc = (GeometryArc *)pg;
  152. R = abs(1.0/parc->GetCurvature());
  153. if(parc->GetCurvature() > 0)
  154. {
  155. x_center = pg->GetX() + R *cos(pg->GetHdg() + M_PI/2.0);
  156. y_center = pg->GetY() + R * sin(pg->GetHdg() + M_PI/2.0);
  157. }
  158. else
  159. {
  160. x_center = pg->GetX() + R *cos(pg->GetHdg() -M_PI/2.0);
  161. y_center = pg->GetY() + R * sin(pg->GetHdg() - M_PI/2.0);
  162. }
  163. int k;
  164. int ncount = parc->GetLength() * nfac ;
  165. double curv = parc->GetCurvature();
  166. double hdgstep;
  167. double hdg0 = parc->GetHdg();
  168. double hdgnow = parc->GetHdg();
  169. if(ncount > 0) hdgstep= (parc->GetLength()/R)/ncount;
  170. for(k=0;k<ncount;k++)
  171. {
  172. double x_draw,y_draw;
  173. if(curv > 0)
  174. {
  175. hdgnow = hdg0 + k*hdgstep;
  176. x_draw = x_center + R *cos(hdgnow - M_PI/2.0);
  177. y_draw = y_center + R * sin(hdgnow - M_PI/2.0);
  178. }
  179. else
  180. {
  181. hdgnow = hdg0 - k * hdgstep;
  182. x_draw = x_center + R *cos(hdgnow + M_PI/2.0);
  183. y_draw = y_center + R * sin(hdgnow + M_PI/2.0);
  184. }
  185. x_draw = x_draw - froad_xmin;
  186. y_draw = y_draw - froad_ymin;
  187. painter->drawPoint(x_draw * nfac ,y_draw * nfac *(-1));
  188. }
  189. }
  190. break;
  191. case 4:
  192. {
  193. ppp3 = (GeometryParamPoly3 * )pg;
  194. int ncount = ppp3->GetLength()* nfac;
  195. double sstep;
  196. if(ncount > 0)sstep = ppp3->GetLength()/ncount;
  197. else sstep = 10000.0;
  198. double s = 0;
  199. while(s < ppp3->GetLength())
  200. {
  201. double xtem,ytem;
  202. xtem = ppp3->GetuA() + ppp3->GetuB() * s + ppp3->GetuC() * s*s + ppp3->GetuD() * s*s*s;
  203. ytem = ppp3->GetvA() + ppp3->GetvB() * s + ppp3->GetvC() * s*s + ppp3->GetvD() * s*s*s;
  204. x = xtem*cos(ppp3->GetHdg()) - ytem * sin(ppp3->GetHdg()) + ppp3->GetX();
  205. y = xtem*sin(ppp3->GetHdg()) + ytem * cos(ppp3->GetHdg()) + ppp3->GetY();
  206. x = x - froad_xmin;
  207. y = y- froad_ymin;
  208. painter->drawPoint((int)(x*nfac),(int)(y*(-1.0*nfac)));
  209. s = s+ sstep;
  210. }
  211. }
  212. break;
  213. default:
  214. break;
  215. }
  216. // painter->drawPoint((int)(x*nfac),(int)(y*(-1.0*nfac)));
  217. }
  218. painter->setPen(Qt::green);
  219. painter->end();
  220. }
  221. void RoadEditDialog::paintEvent(QPaintEvent * painter)
  222. {
  223. if(mpCurRoad != 0)
  224. {
  225. ExecPainter();
  226. scene->clear();
  227. scene->addPixmap(QPixmap::fromImage(*image));
  228. myview->setScene(scene);
  229. myview->show();
  230. }
  231. }
  232. void RoadEditDialog::on_comboBox_Road_activated(const QString &arg1)
  233. {
  234. }
  235. void RoadEditDialog::on_comboBox_Road_currentIndexChanged(int index)
  236. {
  237. Road * pRoad = mpxodr->GetRoad(index);
  238. if(pRoad == 0)
  239. {
  240. // QMessageBox::warning(this,"WARN","MainWindow::onClickCBRoadChange road is NULL");
  241. return;
  242. }
  243. mpCurRoad = pRoad;
  244. mnSelGeo = -1;
  245. ui->lineEdit_roadlen->setText(QString::number(pRoad->GetRoadLength()));
  246. ui->comboBox_Geo->clear();
  247. int nsize = pRoad->GetGeometryBlockCount();
  248. int i;
  249. for(i=0;i<nsize;i++)
  250. {
  251. ui->comboBox_Geo->addItem(QString("Geo ")+QString::number(i));
  252. }
  253. update();
  254. }
  255. void RoadEditDialog::on_comboBox_Geo_currentIndexChanged(int index)
  256. {
  257. if(mpCurRoad == 0 )return;
  258. if(index<0)return;
  259. mnSelGeo = index;
  260. if(index>= mpCurRoad->GetGeometryBlockCount())return;
  261. GeometryBlock * pgb = mpCurRoad->GetGeometryBlock(index);
  262. RoadGeometry * pg = pgb->GetGeometryAt(0);
  263. ui->lineEdit_s->setText(QString::number(pg->GetS()));
  264. ui->lineEdit_x->setText(QString::number(pg->GetX()));
  265. ui->lineEdit_y->setText(QString::number(pg->GetY()));
  266. ui->lineEdit_hdg->setText(QString::number(pg->GetHdg()));
  267. ui->lineEdit_len->setText(QString::number(pg->GetLength()));
  268. if((pg->GetGeomType()>=0)&&(pg->GetGeomType()<5))
  269. {
  270. ui->comboBox_geotype->setCurrentIndex(pg->GetGeomType());
  271. }
  272. }