roaddigit.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "roaddigit.h"
  2. #include <math.h>
  3. #include "xodrfunc.h"
  4. RoadDigit::RoadDigit(Road * pRoad,double fspace)
  5. {
  6. mpRoad = pRoad;
  7. UpdateSpace(fspace);
  8. }
  9. std::vector<iv::RoadDigitUnit> * RoadDigit::GetRDU()
  10. {
  11. return &mvectorRDU;
  12. }
  13. void RoadDigit::UpdateSpace(double fspace)
  14. {
  15. if(mpRoad == 0)return;
  16. CalcLine(fspace);
  17. CalcLane();
  18. }
  19. void RoadDigit::CalcLine(double fspace)
  20. {
  21. unsigned int j;
  22. iv::RoadDigitUnit rdu;
  23. bool bLastGeo = false;
  24. for(j=0;j<mpRoad->GetGeometryBlockCount();j++)
  25. {
  26. GeometryBlock * pgeob = mpRoad->GetGeometryBlock(j);
  27. double x,y;
  28. double x_center,y_center;
  29. double R;
  30. RoadGeometry * pg;
  31. GeometryArc * parc;
  32. GeometryParamPoly3 * ppp3;
  33. GeometrySpiral *pSpiral;
  34. double rel_x,rel_y,rel_hdg;
  35. pg = pgeob->GetGeometryAt(0);
  36. x = pg->GetX();
  37. y = pg->GetY();
  38. if(j == (mpRoad->GetGeometryBlockCount() -1))
  39. {
  40. bLastGeo = true;
  41. }
  42. switch (pg->GetGeomType()) {
  43. case 0:
  44. {
  45. rdu.mS = pg->GetS();
  46. rdu.mX = pg->GetX();
  47. rdu.mY = pg->GetY();
  48. rdu.mfHdg = pg->GetHdg();
  49. mvectorRDU.push_back(rdu);
  50. int ncount = pg->GetLength() /fspace;
  51. if(ncount<2)ncount = 2;
  52. double fstep;
  53. if(ncount > 0)fstep = pg->GetLength()/ncount;
  54. int i;
  55. if(bLastGeo )ncount = ncount+1;
  56. for(i=1;i<ncount;i++)
  57. {
  58. double xtem,ytem;
  59. xtem = x + (i*fstep)*cos(pg->GetHdg());
  60. ytem = y + (i*fstep)*sin(pg->GetHdg());
  61. rdu.mS = pg->GetS() + i*fstep;
  62. rdu.mX = xtem;
  63. rdu.mY = ytem;
  64. rdu.mfHdg = pg->GetHdg();
  65. mvectorRDU.push_back(rdu);
  66. }
  67. }
  68. break;
  69. case 1:
  70. pSpiral = (GeometrySpiral * )pg;
  71. {
  72. int ncount = pSpiral->GetLength()/fspace;
  73. if(ncount < 5)ncount = 5;
  74. double sstep = pSpiral->GetLength()/((double)ncount);
  75. int k;
  76. double x0,y0,hdg0,s0;
  77. x0 = pSpiral->GetX();
  78. y0 = pSpiral->GetY();
  79. s0 = pSpiral->GetS();
  80. hdg0 = pSpiral->GetHdg() ;
  81. rdu.mS = s0;
  82. rdu.mX = x0;
  83. rdu.mY = y0;
  84. rdu.mfHdg = hdg0;
  85. mvectorRDU.push_back(rdu);
  86. pSpiral->GetCoords(s0,rel_x,rel_y,rel_hdg);
  87. if(bLastGeo )ncount = ncount+1;
  88. for(k=1;k<ncount;k++)
  89. {
  90. pSpiral->GetCoords(s0+sstep*k,rel_x,rel_y,rel_hdg);
  91. rdu.mS = s0 + sstep * k;
  92. rdu.mX = rel_x;
  93. rdu.mY = rel_y;
  94. rdu.mfHdg = rel_hdg;
  95. mvectorRDU.push_back(rdu);
  96. }
  97. }
  98. break;
  99. case 2:
  100. {
  101. parc = (GeometryArc *)pg;
  102. R = abs(1.0/parc->GetCurvature());
  103. if(parc->GetCurvature() > 0)
  104. {
  105. x_center = pg->GetX() + R *cos(pg->GetHdg() + M_PI/2.0);
  106. y_center = pg->GetY() + R * sin(pg->GetHdg() + M_PI/2.0);
  107. }
  108. else
  109. {
  110. x_center = pg->GetX() + R *cos(pg->GetHdg() -M_PI/2.0);
  111. y_center = pg->GetY() + R * sin(pg->GetHdg() - M_PI/2.0);
  112. }
  113. rdu.mS = pg->GetS();
  114. rdu.mX = pg->GetX();
  115. rdu.mY = pg->GetY();
  116. rdu.mfHdg = pg->GetHdg();
  117. mvectorRDU.push_back(rdu);
  118. int k;
  119. int ncount = parc->GetLength() /fspace;
  120. if(ncount< 5)ncount = 5;
  121. double curv = parc->GetCurvature();
  122. double hdgstep;
  123. double hdg0 = parc->GetHdg();
  124. double hdgnow = parc->GetHdg();
  125. if(ncount > 0) hdgstep= (parc->GetLength()/R)/ncount;
  126. double x_draw,y_draw;
  127. if(curv > 0)
  128. {
  129. hdgnow = hdg0 ;
  130. x_draw = x_center + R *cos(hdgnow - M_PI/2.0);
  131. y_draw = y_center + R * sin(hdgnow - M_PI/2.0);
  132. }
  133. else
  134. {
  135. hdgnow = hdg0;
  136. x_draw = x_center + R *cos(hdgnow + M_PI/2.0);
  137. y_draw = y_center + R * sin(hdgnow + M_PI/2.0);
  138. }
  139. if(bLastGeo )ncount = ncount+1;
  140. for(k=1;k<ncount;k++)
  141. {
  142. if(curv > 0)
  143. {
  144. hdgnow = hdg0 + k*hdgstep;
  145. x_draw = x_center + R *cos(hdgnow - M_PI/2.0);
  146. y_draw = y_center + R * sin(hdgnow - M_PI/2.0);
  147. }
  148. else
  149. {
  150. hdgnow = hdg0 - k * hdgstep;
  151. x_draw = x_center + R *cos(hdgnow + M_PI/2.0);
  152. y_draw = y_center + R * sin(hdgnow + M_PI/2.0);
  153. }
  154. rdu.mS = pg->GetS() + hdgstep * k* R;
  155. rdu.mX = x_draw;
  156. rdu.mY = y_draw;
  157. rdu.mfHdg = hdgnow;
  158. mvectorRDU.push_back(rdu);
  159. }
  160. }
  161. break;
  162. case 4:
  163. {
  164. ppp3 = (GeometryParamPoly3 * )pg;
  165. int ncount = ppp3->GetLength() /fspace;
  166. if(ncount < 5)ncount = 5;
  167. double sstep;
  168. if(ncount > 0)sstep = ppp3->GetLength()/ncount;
  169. else sstep = 10000.0;
  170. double s = 0;
  171. double xtem,ytem;
  172. xtem = ppp3->GetuA() + ppp3->GetuB() * s + ppp3->GetuC() * s*s + ppp3->GetuD() * s*s*s;
  173. ytem = ppp3->GetvA() + ppp3->GetvB() * s + ppp3->GetvC() * s*s + ppp3->GetvD() * s*s*s;
  174. x = xtem*cos(ppp3->GetHdg()) - ytem * sin(ppp3->GetHdg()) + ppp3->GetX();
  175. y = xtem*sin(ppp3->GetHdg()) + ytem * cos(ppp3->GetHdg()) + ppp3->GetY();
  176. s = s+ sstep;
  177. rdu.mS = pg->GetS();
  178. rdu.mX = pg->GetX();
  179. rdu.mY = pg->GetY();
  180. rdu.mfHdg = pg->GetHdg();
  181. mvectorRDU.push_back(rdu);
  182. double flastx = pg->GetX();
  183. double flasty = pg->GetY();
  184. while(s <= ppp3->GetLength())
  185. {
  186. xtem = ppp3->GetuA() + ppp3->GetuB() * s + ppp3->GetuC() * s*s + ppp3->GetuD() * s*s*s;
  187. ytem = ppp3->GetvA() + ppp3->GetvB() * s + ppp3->GetvC() * s*s + ppp3->GetvD() * s*s*s;
  188. x = xtem*cos(ppp3->GetHdg()) - ytem * sin(ppp3->GetHdg()) + ppp3->GetX();
  189. y = xtem*sin(ppp3->GetHdg()) + ytem * cos(ppp3->GetHdg()) + ppp3->GetY();
  190. rdu.mS = pg->GetS() + s;
  191. rdu.mX = x;
  192. rdu.mY = y;
  193. rdu.mfHdg = xodrfunc::CalcHdg(QPointF(flastx,flasty),QPointF(x,y));
  194. mvectorRDU.push_back(rdu);
  195. s = s+ sstep;
  196. }
  197. }
  198. break;
  199. default:
  200. break;
  201. }
  202. // painter->drawPoint((int)(x*mnfac),(int)(y*(-1.0*mnfac)));
  203. }
  204. }
  205. void RoadDigit::CalcLane()
  206. {
  207. int i;
  208. int ncount = mvectorRDU.size();
  209. for(i=0;i<ncount;i++)
  210. {
  211. std::vector<iv::LanePoint> xvectorlp2 = xodrfunc::GetAllLanePoint(mpRoad,mvectorRDU[i].mS,
  212. mvectorRDU[i].mX,mvectorRDU[i].mY,mvectorRDU[i].mfHdg);
  213. mvectorRDU[i].mvectorLanePoint = xvectorlp2;
  214. }
  215. }