PlanningHelpers.cpp 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801
  1. /// \file PlanningHelpers.cpp
  2. /// \brief Helper functions for planning algorithms
  3. /// \author Hatem Darweesh
  4. /// \date Jun 16, 2016
  5. #include "op_planner/PlanningHelpers.h"
  6. #include "op_planner/MatrixOperations.h"
  7. #include <string>
  8. #include <float.h>
  9. using namespace UtilityHNS;
  10. using namespace std;
  11. namespace PlannerHNS
  12. {
  13. std::vector<std::pair<GPSPoint, GPSPoint> > PlanningHelpers::m_TestingClosestPoint;
  14. PlanningHelpers::PlanningHelpers()
  15. {
  16. }
  17. PlanningHelpers::~PlanningHelpers()
  18. {
  19. }
  20. bool PlanningHelpers::GetRelativeInfoRange(const std::vector<std::vector<WayPoint> >& trajectories, const WayPoint& p,const double& searchDistance, RelativeInfo& info)
  21. {
  22. if(trajectories.size() == 0) return false;
  23. vector<RelativeInfo> infos;
  24. for(unsigned int i=0; i < trajectories.size(); i++)
  25. {
  26. RelativeInfo info_item;
  27. GetRelativeInfo(trajectories.at(i), p, info_item);
  28. double angle_diff = UtilityH::AngleBetweenTwoAnglesPositive(info_item.perp_point.pos.a, p.pos.a)*RAD2DEG;
  29. if(angle_diff < 75)
  30. {
  31. info_item.iGlobalPath = i;
  32. infos.push_back(info_item);
  33. }
  34. }
  35. if(infos.size() == 0)
  36. return false;
  37. else if(infos.size() == 1)
  38. {
  39. info = infos.at(0);
  40. return true;
  41. }
  42. double minCost = DBL_MAX;
  43. int min_index = 0;
  44. for(unsigned int i=0 ; i< infos.size(); i++)
  45. {
  46. if(searchDistance > 0)
  47. {
  48. double laneChangeCost = trajectories.at(infos.at(i).iGlobalPath).at(infos.at(i).iFront).laneChangeCost;
  49. if(fabs(infos.at(i).perp_distance) < searchDistance && laneChangeCost < minCost)
  50. {
  51. min_index = i;
  52. minCost = laneChangeCost;
  53. }
  54. }
  55. else
  56. {
  57. if(fabs(infos.at(i).perp_distance) < minCost)
  58. {
  59. min_index = i;
  60. minCost = infos.at(i).perp_distance;
  61. }
  62. }
  63. }
  64. info = infos.at(min_index);
  65. return true;
  66. }
  67. bool PlanningHelpers::GetRelativeInfo(const std::vector<WayPoint>& trajectory, const WayPoint& p, RelativeInfo& info, const int& prevIndex )
  68. {
  69. if(trajectory.size() < 2) return false;
  70. WayPoint p0, p1;
  71. if(trajectory.size()==2)
  72. {
  73. p0 = trajectory.at(0);
  74. p1 = WayPoint((trajectory.at(0).pos.x+trajectory.at(1).pos.x)/2.0,
  75. (trajectory.at(0).pos.y+trajectory.at(1).pos.y)/2.0,
  76. (trajectory.at(0).pos.z+trajectory.at(1).pos.z)/2.0, trajectory.at(0).pos.a);
  77. info.iFront = 1;
  78. info.iBack = 0;
  79. }
  80. else
  81. {
  82. info.iFront = GetClosestNextPointIndexFast(trajectory, p, prevIndex);
  83. // WayPoint p2 = p;
  84. // int old_index = GetClosestNextPointIndex(trajectory, p2, prevIndex);
  85. // if(old_index != info.iFront)
  86. // cout << " Alert Alert !!!! fast: " << info.iFront << ", slow: " << old_index << endl;
  87. if(info.iFront > 0)
  88. info.iBack = info.iFront -1;
  89. else
  90. info.iBack = 0;
  91. if(info.iFront == 0)
  92. {
  93. p0 = trajectory.at(info.iFront);
  94. p1 = trajectory.at(info.iFront+1);
  95. }
  96. else if(info.iFront > 0 && info.iFront < trajectory.size()-1)
  97. {
  98. p0 = trajectory.at(info.iFront-1);
  99. p1 = trajectory.at(info.iFront);
  100. }
  101. else
  102. {
  103. p0 = trajectory.at(info.iFront-1);
  104. p1 = WayPoint((p0.pos.x+trajectory.at(info.iFront).pos.x)/2.0, (p0.pos.y+trajectory.at(info.iFront).pos.y)/2.0, (p0.pos.z+trajectory.at(info.iFront).pos.z)/2.0, p0.pos.a);
  105. }
  106. }
  107. WayPoint prevWP = p0;
  108. Mat3 rotationMat(-p1.pos.a);
  109. Mat3 translationMat(-p.pos.x, -p.pos.y);
  110. Mat3 invRotationMat(p1.pos.a);
  111. Mat3 invTranslationMat(p.pos.x, p.pos.y);
  112. p0.pos = translationMat*p0.pos;
  113. p0.pos = rotationMat*p0.pos;
  114. p1.pos = translationMat*p1.pos;
  115. p1.pos = rotationMat*p1.pos;
  116. double m = (p1.pos.y-p0.pos.y)/(p1.pos.x-p0.pos.x);
  117. info.perp_distance = p1.pos.y - m*p1.pos.x; // solve for x = 0
  118. if(std::isnan(info.perp_distance) || std::isinf(info.perp_distance)) info.perp_distance = 0;
  119. info.to_front_distance = fabs(p1.pos.x); // distance on the x axes
  120. info.perp_point = p1;
  121. info.perp_point.pos.x = 0; // on the same y axis of the car
  122. info.perp_point.pos.y = info.perp_distance; //perp distance between the car and the trajectory
  123. info.perp_point.pos = invRotationMat * info.perp_point.pos;
  124. info.perp_point.pos = invTranslationMat * info.perp_point.pos;
  125. info.from_back_distance = hypot(info.perp_point.pos.y - prevWP.pos.y, info.perp_point.pos.x - prevWP.pos.x);
  126. info.angle_diff = UtilityH::AngleBetweenTwoAnglesPositive(p1.pos.a, p.pos.a)*RAD2DEG;
  127. return true;
  128. }
  129. bool PlanningHelpers::GetRelativeInfoLimited(const std::vector<WayPoint>& trajectory, const WayPoint& p, RelativeInfo& info, const int& prevIndex )
  130. {
  131. if(trajectory.size() < 2) return false;
  132. WayPoint p0, p1;
  133. if(trajectory.size()==2)
  134. {
  135. vector<WayPoint> _trajectory;
  136. p0 = trajectory.at(0);
  137. p1 = p0;
  138. p1 = WayPoint((trajectory.at(0).pos.x+trajectory.at(1).pos.x)/2.0,
  139. (trajectory.at(0).pos.y+trajectory.at(1).pos.y)/2.0,
  140. (trajectory.at(0).pos.z+trajectory.at(1).pos.z)/2.0, trajectory.at(0).pos.a);
  141. _trajectory.push_back(p0);
  142. _trajectory.push_back(p1);
  143. _trajectory.push_back(trajectory.at(1));
  144. info.iFront = GetClosestNextPointIndexFast(_trajectory, p, prevIndex);
  145. if(info.iFront > 0)
  146. info.iBack = info.iFront -1;
  147. else
  148. info.iBack = 0;
  149. if(info.iFront == 0)
  150. {
  151. p0 = _trajectory.at(info.iFront);
  152. p1 = _trajectory.at(info.iFront+1);
  153. }
  154. else if(info.iFront > 0 && info.iFront < _trajectory.size()-1)
  155. {
  156. p0 = _trajectory.at(info.iFront-1);
  157. p1 = _trajectory.at(info.iFront);
  158. }
  159. else
  160. {
  161. p0 = _trajectory.at(info.iFront-1);
  162. p1 = WayPoint((p0.pos.x+_trajectory.at(info.iFront).pos.x)/2.0, (p0.pos.y+_trajectory.at(info.iFront).pos.y)/2.0, (p0.pos.z+_trajectory.at(info.iFront).pos.z)/2.0, p0.pos.a);
  163. }
  164. WayPoint prevWP = p0;
  165. Mat3 rotationMat(-p1.pos.a);
  166. Mat3 translationMat(-p.pos.x, -p.pos.y);
  167. Mat3 invRotationMat(p1.pos.a);
  168. Mat3 invTranslationMat(p.pos.x, p.pos.y);
  169. p0.pos = translationMat*p0.pos;
  170. p0.pos = rotationMat*p0.pos;
  171. p1.pos = translationMat*p1.pos;
  172. p1.pos = rotationMat*p1.pos;
  173. double m = (p1.pos.y-p0.pos.y)/(p1.pos.x-p0.pos.x);
  174. info.perp_distance = p1.pos.y - m*p1.pos.x; // solve for x = 0
  175. if(std::isnan(info.perp_distance) || std::isinf(info.perp_distance)) info.perp_distance = 0;
  176. info.to_front_distance = fabs(p1.pos.x); // distance on the x axes
  177. info.perp_point = p1;
  178. info.perp_point.pos.x = 0; // on the same y axis of the car
  179. info.perp_point.pos.y = info.perp_distance; //perp distance between the car and the _trajectory
  180. info.perp_point.pos = invRotationMat * info.perp_point.pos;
  181. info.perp_point.pos = invTranslationMat * info.perp_point.pos;
  182. info.from_back_distance = hypot(info.perp_point.pos.y - prevWP.pos.y, info.perp_point.pos.x - prevWP.pos.x);
  183. info.angle_diff = UtilityH::AngleBetweenTwoAnglesPositive(p1.pos.a, p.pos.a)*RAD2DEG;
  184. info.bAfter = false;
  185. info.bBefore = false;
  186. if(info.iFront == 0)
  187. {
  188. info.bBefore = true;
  189. }
  190. else if(info.iFront == _trajectory.size()-1)
  191. {
  192. int s = _trajectory.size();
  193. double angle_befor_last = UtilityH::FixNegativeAngle(atan2(_trajectory.at(s-2).pos.y - _trajectory.at(s-1).pos.y, _trajectory.at(s-2).pos.x - _trajectory.at(s-1).pos.x));
  194. double angle_from_perp = UtilityH::FixNegativeAngle(atan2(info.perp_point.pos.y - _trajectory.at(s-1).pos.y, info.perp_point.pos.x - _trajectory.at(s-1).pos.x));
  195. double diff_last_perp = UtilityH::AngleBetweenTwoAnglesPositive(angle_befor_last, angle_from_perp);
  196. info.after_angle = diff_last_perp;
  197. if(diff_last_perp > M_PI_2)
  198. {
  199. info.bAfter = true;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. info.iFront = GetClosestNextPointIndexFast(trajectory, p, prevIndex);
  206. if(info.iFront > 0)
  207. info.iBack = info.iFront -1;
  208. else
  209. info.iBack = 0;
  210. if(info.iFront == 0)
  211. {
  212. p0 = trajectory.at(info.iFront);
  213. p1 = trajectory.at(info.iFront+1);
  214. }
  215. else if(info.iFront > 0 && info.iFront < trajectory.size()-1)
  216. {
  217. p0 = trajectory.at(info.iFront-1);
  218. p1 = trajectory.at(info.iFront);
  219. }
  220. else
  221. {
  222. p0 = trajectory.at(info.iFront-1);
  223. p1 = WayPoint((p0.pos.x+trajectory.at(info.iFront).pos.x)/2.0, (p0.pos.y+trajectory.at(info.iFront).pos.y)/2.0, (p0.pos.z+trajectory.at(info.iFront).pos.z)/2.0, p0.pos.a);
  224. }
  225. WayPoint prevWP = p0;
  226. Mat3 rotationMat(-p1.pos.a);
  227. Mat3 translationMat(-p.pos.x, -p.pos.y);
  228. Mat3 invRotationMat(p1.pos.a);
  229. Mat3 invTranslationMat(p.pos.x, p.pos.y);
  230. p0.pos = translationMat*p0.pos;
  231. p0.pos = rotationMat*p0.pos;
  232. p1.pos = translationMat*p1.pos;
  233. p1.pos = rotationMat*p1.pos;
  234. double m = (p1.pos.y-p0.pos.y)/(p1.pos.x-p0.pos.x);
  235. info.perp_distance = p1.pos.y - m*p1.pos.x; // solve for x = 0
  236. if(std::isnan(info.perp_distance) || std::isinf(info.perp_distance)) info.perp_distance = 0;
  237. info.to_front_distance = fabs(p1.pos.x); // distance on the x axes
  238. info.perp_point = p1;
  239. info.perp_point.pos.x = 0; // on the same y axis of the car
  240. info.perp_point.pos.y = info.perp_distance; //perp distance between the car and the trajectory
  241. info.perp_point.pos = invRotationMat * info.perp_point.pos;
  242. info.perp_point.pos = invTranslationMat * info.perp_point.pos;
  243. info.from_back_distance = hypot(info.perp_point.pos.y - prevWP.pos.y, info.perp_point.pos.x - prevWP.pos.x);
  244. info.angle_diff = UtilityH::AngleBetweenTwoAnglesPositive(p1.pos.a, p.pos.a)*RAD2DEG;
  245. info.bAfter = false;
  246. info.bBefore = false;
  247. if(info.iFront == 0)
  248. {
  249. info.bBefore = true;
  250. }
  251. else if(info.iFront == trajectory.size()-1)
  252. {
  253. int s = trajectory.size();
  254. double angle_befor_last = UtilityH::FixNegativeAngle(atan2(trajectory.at(s-2).pos.y - trajectory.at(s-1).pos.y, trajectory.at(s-2).pos.x - trajectory.at(s-1).pos.x));
  255. double angle_from_perp = UtilityH::FixNegativeAngle(atan2(info.perp_point.pos.y - trajectory.at(s-1).pos.y, info.perp_point.pos.x - trajectory.at(s-1).pos.x));
  256. double diff_last_perp = UtilityH::AngleBetweenTwoAnglesPositive(angle_befor_last, angle_from_perp);
  257. info.after_angle = diff_last_perp;
  258. if(diff_last_perp > M_PI_2)
  259. {
  260. info.bAfter = true;
  261. }
  262. }
  263. }
  264. return true;
  265. }
  266. bool PlanningHelpers::GetThreePointsInfo(const WayPoint& p0, const WayPoint& p1, const WayPoint& p2, WayPoint& perp_p, double& long_d, double lat_d)
  267. {
  268. if(p0.pos.x == p1.pos.x && p0.pos.y == p1.pos.y) return false;
  269. if(p0.pos.x == p2.pos.x && p0.pos.y == p2.pos.y) return false;
  270. if(p1.pos.x == p2.pos.x && p1.pos.y == p2.pos.y) return false;
  271. perp_p = p1;
  272. WayPoint first_p = p0;
  273. double angle_x = atan2(p1.pos.y-p0.pos.y, p1.pos.x-p0.pos.x);
  274. Mat3 rotationMat(-angle_x);
  275. Mat3 translationMat(-p2.pos.x, -p2.pos.y);
  276. Mat3 invRotationMat(angle_x);
  277. Mat3 invTranslationMat(p2.pos.x, p2.pos.y);
  278. first_p.pos = translationMat*first_p.pos;
  279. first_p.pos = rotationMat*first_p.pos;
  280. perp_p.pos = translationMat*perp_p.pos;
  281. perp_p.pos = rotationMat*perp_p.pos;
  282. if(perp_p.pos.x-first_p.pos.x == 0) return false;
  283. double m = (perp_p.pos.y-first_p.pos.y)/(perp_p.pos.x-first_p.pos.x);
  284. lat_d = perp_p.pos.y - m*perp_p.pos.x; // solve for x = 0
  285. if(std::isnan(lat_d) || std::isinf(lat_d)) return false;
  286. if(perp_p.pos.x < 0)
  287. return false;
  288. perp_p.pos.x = 0; // on the same y axis of the car
  289. perp_p.pos.y = lat_d; //perp distance between the car and the trajectory
  290. perp_p.pos = invRotationMat * perp_p.pos;
  291. perp_p.pos = invTranslationMat * perp_p.pos;
  292. long_d = hypot(perp_p.pos.y - first_p.pos.y, perp_p.pos.x - first_p.pos.x);
  293. return true;
  294. }
  295. WayPoint PlanningHelpers::GetFollowPointOnTrajectory(const std::vector<WayPoint>& trajectory, const RelativeInfo& init_p, const double& distance, unsigned int& point_index)
  296. {
  297. WayPoint follow_point;
  298. if(trajectory.size()==0) return follow_point;
  299. //condition 1, if far behind the first point on the trajectory
  300. int local_i = init_p.iFront;
  301. if(init_p.iBack == 0 && init_p.iBack == init_p.iFront && init_p.from_back_distance > distance)
  302. {
  303. follow_point = trajectory.at(init_p.iFront);
  304. follow_point.pos.x = init_p.perp_point.pos.x + distance * cos(follow_point.pos.a);
  305. follow_point.pos.y = init_p.perp_point.pos.y + distance * sin(follow_point.pos.a);
  306. }
  307. //condition 2, if far after the last point on the trajectory
  308. else if(init_p.iFront == (int)trajectory.size() - 1)
  309. {
  310. follow_point = trajectory.at(init_p.iFront);
  311. follow_point.pos.x = init_p.perp_point.pos.x + distance * cos(follow_point.pos.a);
  312. follow_point.pos.y = init_p.perp_point.pos.y + distance * sin(follow_point.pos.a);
  313. }
  314. else
  315. {
  316. double d = init_p.to_front_distance;
  317. while(local_i < (int)trajectory.size()-1 && d < distance)
  318. {
  319. local_i++;
  320. d += hypot(trajectory.at(local_i).pos.y - trajectory.at(local_i-1).pos.y, trajectory.at(local_i).pos.x - trajectory.at(local_i-1).pos.x);
  321. }
  322. double d_part = distance - d;
  323. follow_point = trajectory.at(local_i);
  324. follow_point.pos.x = follow_point.pos.x + d_part * cos(follow_point.pos.a);
  325. follow_point.pos.y = follow_point.pos.y + d_part * sin(follow_point.pos.a);
  326. }
  327. point_index = local_i;
  328. return follow_point;
  329. }
  330. double PlanningHelpers::GetExactDistanceOnTrajectory(const std::vector<WayPoint>& trajectory, const RelativeInfo& p1,const RelativeInfo& p2)
  331. {
  332. if(trajectory.size() == 0) return 0;
  333. if(p2.iFront == p1.iFront && p2.iBack == p1.iBack)
  334. {
  335. return p2.to_front_distance - p1.to_front_distance;
  336. }
  337. else if(p2.iBack >= p1.iFront)
  338. {
  339. double d_on_path = p1.to_front_distance + p2.from_back_distance;
  340. for(int i = p1.iFront; i < p2.iBack; i++)
  341. d_on_path += hypot(trajectory.at(i+1).pos.y - trajectory.at(i).pos.y, trajectory.at(i+1).pos.x - trajectory.at(i).pos.x);
  342. return d_on_path;
  343. }
  344. else if(p2.iFront <= p1.iBack)
  345. {
  346. double d_on_path = p1.from_back_distance + p2.to_front_distance;
  347. for(int i = p2.iFront; i < p1.iBack; i++)
  348. d_on_path += hypot(trajectory.at(i+1).pos.y - trajectory.at(i).pos.y, trajectory.at(i+1).pos.x - trajectory.at(i).pos.x);
  349. return -d_on_path;
  350. }
  351. else
  352. {
  353. return 0;
  354. }
  355. }
  356. int PlanningHelpers::GetClosestNextPointIndex_obsolete(const vector<WayPoint>& trajectory, const WayPoint& p,const int& prevIndex )
  357. {
  358. if(trajectory.size() == 0 || prevIndex < 0) return 0;
  359. double d = 0, minD = DBL_MAX;
  360. int min_index = prevIndex;
  361. for(unsigned int i=prevIndex; i< trajectory.size(); i++)
  362. {
  363. d = distance2pointsSqr(trajectory.at(i).pos, p.pos);
  364. if(d < minD)
  365. {
  366. min_index = i;
  367. minD = d;
  368. }
  369. }
  370. // cout << "Slow=> Start: " << 0;
  371. // cout << ", End: " << trajectory.size();
  372. // cout << ", Minimum Before: " << min_index;
  373. if(min_index < (int)trajectory.size()-2)
  374. {
  375. GPSPoint curr, next;
  376. curr = trajectory.at(min_index).pos;
  377. next = trajectory.at(min_index+1).pos;
  378. GPSPoint v_1(p.pos.x - curr.x ,p.pos.y - curr.y,0,0);
  379. double norm1 = pointNorm(v_1);
  380. GPSPoint v_2(next.x - curr.x,next.y - curr.y,0,0);
  381. double norm2 = pointNorm(v_2);
  382. double dot_pro = v_1.x*v_2.x + v_1.y*v_2.y;
  383. double a = UtilityH::FixNegativeAngle(acos(dot_pro/(norm1*norm2)));
  384. if(a <= M_PI_2)
  385. min_index = min_index+1;
  386. }
  387. // cout << ", Minimum After: " << min_index << ", Big O: " << trajectory.size() << endl;
  388. return min_index;
  389. }
  390. int PlanningHelpers::GetClosestNextPointIndexFastV2(const vector<WayPoint>& trajectory, const WayPoint& p,const int& prevIndex)
  391. {
  392. int size = (int)trajectory.size();
  393. if(size < 2 || prevIndex < 0) return 0;
  394. double d = 0, minD = DBL_MAX;
  395. double resolution = hypot(trajectory[1].pos.y -trajectory[0].pos.y , trajectory[1].pos.x -trajectory[0].pos.x);
  396. double d_to_zero = hypot(p.pos.y -trajectory[0].pos.y , p.pos.x - trajectory[0].pos.x);
  397. double d_to_size = hypot(trajectory[size-1].pos.y - p.pos.y , trajectory[size-1].pos.x - p.pos.x);
  398. int iStart = d_to_zero / resolution;
  399. WayPoint perp_p;
  400. double lat_d = 0;
  401. double long_d = 0;
  402. if(iStart > 0 && iStart < size-1 && GetThreePointsInfo(trajectory.at(0), trajectory.at(iStart), p, perp_p, long_d, lat_d))
  403. {
  404. // m_TestingClosestPoint.push_back(make_pair(trajectory.at(0).pos, p.pos));
  405. // m_TestingClosestPoint.push_back(make_pair(trajectory.at(iStart).pos, p.pos));
  406. iStart = long_d / resolution;
  407. }
  408. if(iStart>0)
  409. iStart--;
  410. int iEnd = size - (d_to_size / resolution);
  411. if(iEnd >= 0 && iEnd < size-2 && GetThreePointsInfo(trajectory.at(size-1), trajectory.at(iEnd), p, perp_p, long_d, lat_d))
  412. {
  413. // m_TestingClosestPoint.push_back(make_pair(trajectory.at(size-1).pos, p.pos));
  414. // m_TestingClosestPoint.push_back(make_pair(trajectory.at(iEnd).pos, p.pos));
  415. iEnd = size - (long_d / resolution);
  416. }
  417. if(iEnd < size-1)
  418. iEnd++;
  419. // cout << "Fast=>";
  420. // cout << " Start: " << iStart;
  421. // cout << ", End: " << iEnd;
  422. double d_from_start = d_to_zero;
  423. if(iStart < size)
  424. d_from_start = hypot(trajectory[iStart].pos.y - p.pos.y , trajectory[iStart].pos.x - p.pos.x);
  425. double d_from_end = d_to_size;
  426. if(iEnd >= 0)
  427. d_from_end = hypot(trajectory[iEnd].pos.y - p.pos.y , trajectory[iEnd].pos.x - p.pos.x);
  428. if(iStart >= size && iEnd < 0)
  429. {
  430. if(d_to_zero < d_to_size)
  431. {
  432. iStart = 0;
  433. iEnd = size/2 -1;
  434. }
  435. else
  436. {
  437. iStart = size/2;
  438. iEnd = size - 1;
  439. }
  440. }
  441. else
  442. {
  443. if(iStart >=size || (d_from_start > d_to_zero))
  444. iStart = 0;
  445. if(iEnd < 0 || (d_from_end > d_to_size))
  446. iEnd = size-1;
  447. }
  448. if(iStart > iEnd)
  449. iEnd = size-1;
  450. int min_index = iStart;
  451. int ncout = 0;
  452. for(int i=iStart; i<= iEnd; i++)
  453. {
  454. d = distance2pointsSqr(trajectory[i].pos, p.pos);
  455. if(d < minD)
  456. {
  457. min_index = i;
  458. minD = d;
  459. }
  460. ncout++;
  461. }
  462. // cout << ", Minimum Before: " << min_index;
  463. if(min_index < size-2)
  464. {
  465. GPSPoint curr, next;
  466. curr = trajectory[min_index].pos;
  467. next = trajectory[min_index+1].pos;
  468. GPSPoint v_1(p.pos.x - curr.x ,p.pos.y - curr.y,0,0);
  469. double norm1 = pointNorm(v_1);
  470. GPSPoint v_2(next.x - curr.x,next.y - curr.y,0,0);
  471. double norm2 = pointNorm(v_2);
  472. double dot_pro = v_1.x*v_2.x + v_1.y*v_2.y;
  473. double a = UtilityH::FixNegativeAngle(acos(dot_pro/(norm1*norm2)));
  474. if(a <= M_PI_2)
  475. min_index = min_index+1;
  476. }
  477. // m_TestingClosestPoint.push_back(make_pair(trajectory.at(min_index).pos, p.pos));
  478. // cout << ", Minimum After: " << min_index << ", Big O: " << ncout << endl;
  479. // cout << "d_zero: " << d_to_zero << ", d_start: " << d_from_start << endl;
  480. // cout << "d_size: " << d_to_size << ", d_end: " << d_from_end << endl;
  481. return min_index;
  482. }
  483. int PlanningHelpers::GetClosestNextPointIndexFast(const vector<WayPoint>& trajectory, const WayPoint& p,const int& prevIndex )
  484. {
  485. int size = (int)trajectory.size();
  486. if(size < 2 || prevIndex < 0) return 0;
  487. double d = 0, minD = DBL_MAX;
  488. int min_index = prevIndex;
  489. int iStart = prevIndex;
  490. int iEnd = size;
  491. double resolution = hypot(trajectory[1].pos.y -trajectory[0].pos.y , trajectory[1].pos.x -trajectory[0].pos.x);
  492. //divide every 5 meters
  493. int skip_factor = 5;
  494. if(resolution > skip_factor)
  495. resolution = skip_factor;
  496. int skip = 1;
  497. if(resolution > 0)
  498. skip = skip_factor/resolution;
  499. for(int i=0; i< size; i+=skip)
  500. {
  501. if(i+skip/2 < size)
  502. d = (distance2pointsSqr(trajectory[i].pos, p.pos) + distance2pointsSqr(trajectory[i+skip/2].pos, p.pos))/2.0;
  503. else
  504. d = distance2pointsSqr(trajectory[i].pos, p.pos);
  505. if(d < minD)
  506. {
  507. iStart = i-skip;
  508. iEnd = i+skip;
  509. minD = d;
  510. min_index = i;
  511. }
  512. }
  513. if((size - skip/2 - 1) > 0)
  514. d = (distance2pointsSqr(trajectory[size-1].pos, p.pos) + distance2pointsSqr(trajectory[size - skip/2 -1 ].pos, p.pos))/2.0;
  515. else
  516. d = distance2pointsSqr(trajectory[size-1].pos, p.pos);
  517. if(d < minD)
  518. {
  519. iStart = size-skip;
  520. iEnd = size+skip;
  521. minD = d;
  522. min_index = size-1;
  523. }
  524. if(iStart < 0) iStart = 0;
  525. if(iEnd >= size) iEnd = size -1;
  526. for(int i=iStart; i< iEnd; i++)
  527. {
  528. d = distance2pointsSqr(trajectory[i].pos, p.pos);
  529. if(d < minD)
  530. {
  531. min_index = i;
  532. minD = d;
  533. }
  534. }
  535. if(min_index < size-1)
  536. {
  537. GPSPoint curr, next;
  538. curr = trajectory[min_index].pos;
  539. next = trajectory[min_index+1].pos;
  540. GPSPoint v_1(p.pos.x - curr.x ,p.pos.y - curr.y,0,0);
  541. double norm1 = pointNorm(v_1);
  542. GPSPoint v_2(next.x - curr.x,next.y - curr.y,0,0);
  543. double norm2 = pointNorm(v_2);
  544. double dot_pro = v_1.x*v_2.x + v_1.y*v_2.y;
  545. double a = UtilityH::FixNegativeAngle(acos(dot_pro/(norm1*norm2)));
  546. if(a <= M_PI_2)
  547. min_index = min_index+1;
  548. }
  549. //m_TestingClosestPoint.push_back(make_pair(trajectory.at(min_index).pos, p.pos));
  550. return min_index;
  551. }
  552. int PlanningHelpers::GetClosestNextPointIndexDirectionFast(const vector<WayPoint>& trajectory, const WayPoint& p,const int& prevIndex )
  553. {
  554. int size = (int)trajectory.size();
  555. if(size < 2 || prevIndex < 0) return 0;
  556. double d = 0, minD = DBL_MAX;
  557. int min_index = prevIndex;
  558. for(unsigned int i=prevIndex; i< size; i++)
  559. {
  560. d = distance2pointsSqr(trajectory[i].pos, p.pos);
  561. double angle_diff = UtilityH::AngleBetweenTwoAnglesPositive(trajectory[i].pos.a, p.pos.a)*RAD2DEG;
  562. if(d < minD && angle_diff < 45)
  563. {
  564. min_index = i;
  565. minD = d;
  566. }
  567. }
  568. if(min_index < (int)trajectory.size()-2)
  569. {
  570. GPSPoint curr, next;
  571. curr = trajectory.at(min_index).pos;
  572. next = trajectory.at(min_index+1).pos;
  573. GPSPoint v_1(p.pos.x - curr.x ,p.pos.y - curr.y,0,0);
  574. double norm1 = pointNorm(v_1);
  575. GPSPoint v_2(next.x - curr.x,next.y - curr.y,0,0);
  576. double norm2 = pointNorm(v_2);
  577. double dot_pro = v_1.x*v_2.x + v_1.y*v_2.y;
  578. double a = UtilityH::FixNegativeAngle(acos(dot_pro/(norm1*norm2)));
  579. if(a <= M_PI_2)
  580. min_index = min_index+1;
  581. }
  582. return min_index;
  583. }
  584. int PlanningHelpers::GetClosestPointIndex_obsolete(const vector<WayPoint>& trajectory, const WayPoint& p,const int& prevIndex )
  585. {
  586. if(trajectory.size() == 0 || prevIndex < 0) return 0;
  587. double d = 0, minD = DBL_MAX;
  588. int min_index = prevIndex;
  589. for(unsigned int i=prevIndex; i< trajectory.size(); i++)
  590. {
  591. d = distance2pointsSqr(trajectory.at(i).pos, p.pos);
  592. if(d < minD)
  593. {
  594. min_index = i;
  595. minD = d;
  596. }
  597. }
  598. return min_index;
  599. }
  600. WayPoint PlanningHelpers::GetPerpendicularOnTrajectory_obsolete(const vector<WayPoint>& trajectory, const WayPoint& p, double& distance, const int& prevIndex )
  601. {
  602. if(trajectory.size() < 2) return p;
  603. WayPoint p0, p1, p2, perp;
  604. if(trajectory.size()==2)
  605. {
  606. p0 = trajectory.at(0);
  607. p1 = WayPoint((trajectory.at(0).pos.x+trajectory.at(1).pos.x)/2.0,
  608. (trajectory.at(0).pos.y+trajectory.at(1).pos.y)/2.0,
  609. (trajectory.at(0).pos.z+trajectory.at(1).pos.z)/2.0, trajectory.at(0).pos.a);
  610. p2 = trajectory.at(1);
  611. }
  612. else
  613. {
  614. int next_index = GetClosestNextPointIndex_obsolete(trajectory, p, prevIndex);
  615. if(next_index == 0)
  616. {
  617. p0 = trajectory[next_index];
  618. p1 = trajectory[next_index+1];
  619. p2 = trajectory[next_index+2];
  620. }
  621. else if(next_index > 0 && next_index < trajectory.size()-1)
  622. {
  623. p0 = trajectory[next_index-1];
  624. p1 = trajectory[next_index];
  625. p2 = trajectory[next_index+1];
  626. }
  627. else
  628. {
  629. p0 = trajectory[next_index-1];
  630. p2 = trajectory[next_index];
  631. p1 = WayPoint((p0.pos.x+p2.pos.x)/2.0, (p0.pos.y+p2.pos.y)/2.0, (p0.pos.z+p2.pos.z)/2.0, p0.pos.a);
  632. }
  633. }
  634. Mat3 rotationMat(-p1.pos.a);
  635. Mat3 translationMat(-p.pos.x, -p.pos.y);
  636. Mat3 invRotationMat(p1.pos.a);
  637. Mat3 invTranslationMat(p.pos.x, p.pos.y);
  638. p0.pos = translationMat*p0.pos;
  639. p0.pos = rotationMat*p0.pos;
  640. p1.pos = translationMat*p1.pos;
  641. p1.pos = rotationMat*p1.pos;
  642. p2.pos = translationMat*p2.pos;
  643. p2.pos= rotationMat*p2.pos;
  644. double m = (p1.pos.y-p0.pos.y)/(p1.pos.x-p0.pos.x);
  645. double d = p1.pos.y - m*p1.pos.x; // solve for x = 0
  646. distance = p1.pos.x; // distance on the x axes
  647. perp = p1;
  648. perp.pos.x = 0; // on the same y axis of the car
  649. perp.pos.y = d; //perp distance between the car and the trajectory
  650. perp.pos = invRotationMat * perp.pos;
  651. perp.pos = invTranslationMat * perp.pos;
  652. return perp;
  653. }
  654. double PlanningHelpers::GetPerpDistanceToTrajectorySimple_obsolete(const vector<WayPoint>& trajectory, const WayPoint& p,const int& prevIndex)
  655. {
  656. if(trajectory.size() < 2)
  657. return 0;
  658. WayPoint p0, p1, p2;
  659. int next_index = 0;
  660. if(trajectory.size()==2)
  661. {
  662. p0 = trajectory.at(0);
  663. p2 = trajectory.at(1);
  664. p1 = WayPoint((p0.pos.x+p2.pos.x)/2.0, (p0.pos.y+p2.pos.y)/2.0, (p0.pos.z+p2.pos.z)/2.0, p0.pos.a);
  665. }
  666. else
  667. {
  668. next_index = GetClosestNextPointIndexFast(trajectory, p, prevIndex);
  669. if(next_index == 0)
  670. {
  671. p0 = trajectory[next_index];
  672. p1 = trajectory[next_index+1];
  673. p2 = trajectory[next_index+2];
  674. }
  675. else if(next_index > 0 && next_index < trajectory.size()-1)
  676. {
  677. p0 = trajectory[next_index-1];
  678. p1 = trajectory[next_index];
  679. p2 = trajectory[next_index+1];
  680. }
  681. else
  682. {
  683. p0 = trajectory[next_index-1];
  684. p2 = trajectory[next_index];
  685. p1 = WayPoint((p0.pos.x+p2.pos.x)/2.0, (p0.pos.y+p2.pos.y)/2.0, (p0.pos.z+p2.pos.z)/2.0, p0.pos.a);
  686. }
  687. }
  688. Mat3 rotationMat(-p1.pos.a);
  689. Mat3 translationMat(-p.pos.x, -p.pos.y);
  690. p0.pos = translationMat*p0.pos;
  691. p0.pos = rotationMat*p0.pos;
  692. p1.pos = translationMat*p1.pos;
  693. p1.pos = rotationMat*p1.pos;
  694. p2.pos = translationMat*p2.pos;
  695. p2.pos = rotationMat*p2.pos;
  696. double m = (p1.pos.y-p0.pos.y)/(p1.pos.x-p0.pos.x);
  697. double d = p1.pos.y - m*p1.pos.x;
  698. if(std::isnan(d) || std::isinf(d))
  699. {
  700. //assert(false);
  701. d = 0;
  702. }
  703. return d;
  704. }
  705. double PlanningHelpers::GetPerpDistanceToVectorSimple_obsolete(const WayPoint& point1, const WayPoint& point2, const WayPoint& pose)
  706. {
  707. WayPoint p1 = point1, p2 = point2;
  708. Mat3 rotationMat(-p1.pos.a);
  709. Mat3 translationMat(-pose.pos.x, -pose.pos.y);
  710. p1.pos = translationMat*p1.pos;
  711. p1.pos = rotationMat*p1.pos;
  712. p2.pos = translationMat*p2.pos;
  713. p2.pos = rotationMat*p2.pos;
  714. double m = (p2.pos.y-p1.pos.y)/(p2.pos.x-p1.pos.x);
  715. double d = p2.pos.y - m*p2.pos.x;
  716. if(std::isnan(d) || std::isinf(d))
  717. {
  718. //assert(false);
  719. d = 0;
  720. }
  721. return d;
  722. }
  723. WayPoint PlanningHelpers::GetNextPointOnTrajectory_obsolete(const vector<WayPoint>& trajectory, const double& distance, const int& currIndex)
  724. {
  725. assert(trajectory.size()>0);
  726. int local_currIndex = currIndex;
  727. if(local_currIndex < 0 || local_currIndex >= trajectory.size())
  728. return trajectory.at(0);
  729. WayPoint p1 = trajectory.at(local_currIndex);
  730. WayPoint p2;
  731. double d = 0;
  732. while(local_currIndex < (trajectory.size()-1) && d < distance)
  733. {
  734. local_currIndex++;
  735. p2 = p1;
  736. p1 = trajectory.at(local_currIndex);
  737. d += distance2points(p1.pos, p2.pos);
  738. }
  739. if(local_currIndex >= trajectory.size()-1)
  740. return p1;
  741. double distance_diff = distance - d;
  742. p2 = trajectory.at(local_currIndex);
  743. p1 = trajectory.at(local_currIndex+1);
  744. GPSPoint uv(p1.pos.x - p2.pos.x, p1.pos.y - p2.pos.y ,0,0);
  745. double v_norm = pointNorm(uv);
  746. assert(v_norm != 0);
  747. uv.x = (uv.x / v_norm) * distance_diff;
  748. uv.y = (uv.y / v_norm) * distance_diff;
  749. double ydiff = p1.pos.y-p2.pos.y;
  750. double xdiff = p1.pos.x-p2.pos.x;
  751. double a = atan2(ydiff,xdiff);
  752. WayPoint abs_waypoint = p2;
  753. abs_waypoint.pos.x = p2.pos.x + uv.x;
  754. abs_waypoint.pos.y = p2.pos.y + uv.y;
  755. abs_waypoint.pos.a = a;
  756. return abs_waypoint;
  757. }
  758. double PlanningHelpers::GetDistanceOnTrajectory_obsolete(const std::vector<WayPoint>& path, const int& start_index, const WayPoint& p)
  759. {
  760. int end_point_index = GetClosestPointIndex_obsolete(path, p);
  761. if(end_point_index > 0)
  762. end_point_index--;
  763. double padding_distance = distance2points(path.at(end_point_index).pos, p.pos);
  764. double d_on_path = 0;
  765. if(end_point_index >= start_index)
  766. {
  767. for(unsigned int i = start_index; i < end_point_index; i++)
  768. d_on_path += distance2points(path.at(i).pos, path.at(i+1).pos);
  769. d_on_path += padding_distance;
  770. }
  771. else
  772. {
  773. for(unsigned int i = start_index; i > end_point_index; i--)
  774. d_on_path -= distance2points(path.at(i).pos, path.at(i-1).pos);
  775. }
  776. return d_on_path;
  777. }
  778. bool PlanningHelpers::CompareTrajectories(const std::vector<WayPoint>& path1, const std::vector<WayPoint>& path2)
  779. {
  780. if(path1.size() != path2.size())
  781. return false;
  782. for(unsigned int i=0; i< path1.size(); i++)
  783. {
  784. if(path1.at(i).v != path2.at(i).v || path1.at(i).pos.x != path2.at(i).pos.x || path1.at(i).pos.y != path2.at(i).pos.y || path1.at(i).pos.alt != path2.at(i).pos.alt || path1.at(i).pos.lon != path2.at(i).pos.lon)
  785. return false;
  786. }
  787. return true;
  788. }
  789. double PlanningHelpers::GetDistanceToClosestStopLineAndCheck(const std::vector<WayPoint>& path, const WayPoint& p, const double& giveUpDistance, int& stopLineID, int& stopSignID, int& trafficLightID, const int& prevIndex)
  790. {
  791. trafficLightID = stopSignID = stopLineID = -1;
  792. RelativeInfo info;
  793. GetRelativeInfo(path, p, info, prevIndex);
  794. for(unsigned int i=info.iBack; i<path.size(); i++)
  795. {
  796. if(path.at(i).stopLineID > 0 && path.at(i).pLane)
  797. {
  798. for(unsigned int j = 0; j < path.at(i).pLane->stopLines.size(); j++)
  799. {
  800. if(path.at(i).pLane->stopLines.at(j).id == path.at(i).stopLineID)
  801. {
  802. stopLineID = path.at(i).stopLineID;
  803. RelativeInfo stop_info;
  804. WayPoint stopLineWP ;
  805. stopLineWP.pos = path.at(i).pLane->stopLines.at(j).points.at(0);
  806. GetRelativeInfo(path, stopLineWP, stop_info);
  807. double localDistance = GetExactDistanceOnTrajectory(path, info, stop_info);
  808. if(localDistance > giveUpDistance)
  809. {
  810. stopSignID = path.at(i).pLane->stopLines.at(j).stopSignID;
  811. trafficLightID = path.at(i).pLane->stopLines.at(j).trafficLightID;
  812. return localDistance;
  813. }
  814. }
  815. }
  816. }
  817. }
  818. return -1;
  819. }
  820. void PlanningHelpers::CreateManualBranchFromTwoPoints(WayPoint& p1,WayPoint& p2 , const double& distance, const DIRECTION_TYPE& direction, std::vector<WayPoint>& path)
  821. {
  822. WayPoint endWP, midWP;
  823. double branch_angle = 0;
  824. if(direction == FORWARD_RIGHT_DIR)
  825. {
  826. branch_angle = p1.pos.a-M_PI_2;
  827. }
  828. else if(direction == FORWARD_LEFT_DIR)
  829. {
  830. branch_angle = p1.pos.a+M_PI_2;
  831. }
  832. endWP.pos.y = p2.pos.y + distance*sin(branch_angle);
  833. endWP.pos.x = p2.pos.x + distance*cos(branch_angle);
  834. midWP = p2;
  835. midWP.pos.x = (p1.pos.x+p2.pos.x)/2.0;
  836. midWP.pos.y = (p1.pos.y+p2.pos.y)/2.0;
  837. endWP.bDir = midWP.bDir = direction;
  838. path.clear();
  839. path.push_back(p1);
  840. path.push_back(p2);
  841. path.push_back(endWP);
  842. //PlanningHelpers::SmoothPath(path, 0.4, 0.1);
  843. PlanningHelpers::FixPathDensity(path, 1);
  844. PlanningHelpers::SmoothPath(path, 0.4, 0.25);
  845. PlanningHelpers::FixPathDensity(path, 0.5);
  846. PlanningHelpers::SmoothPath(path, 0.25, 0.4);
  847. for(unsigned int i=0; i < path.size(); i++)
  848. {
  849. if(direction == FORWARD_LEFT_DIR)
  850. {
  851. path.at(i).state = INITIAL_STATE;
  852. path.at(i).beh_state = BEH_BRANCH_LEFT_STATE;
  853. path.at(i).laneId = -2;
  854. }
  855. if(direction == FORWARD_RIGHT_DIR)
  856. {
  857. path.at(i).state = INITIAL_STATE;
  858. path.at(i).beh_state = BEH_BRANCH_RIGHT_STATE;
  859. path.at(i).laneId = -3;
  860. }
  861. }
  862. }
  863. void PlanningHelpers::CreateManualBranch(std::vector<WayPoint>& path, const int& degree, const DIRECTION_TYPE& direction)
  864. {
  865. if(path.size() < 5) return;
  866. //start branch point
  867. WayPoint branch_start = path.at(path.size()-5);
  868. WayPoint last_wp = path.at(path.size()-1);
  869. WayPoint endWP;
  870. vector<WayPoint> goal_path;
  871. double branch_angle = 0;
  872. if(direction == FORWARD_RIGHT_DIR)
  873. {
  874. branch_angle = last_wp.pos.a-M_PI_2;
  875. }
  876. else if(direction == FORWARD_LEFT_DIR)
  877. {
  878. branch_angle = last_wp.pos.a+M_PI_2;
  879. }
  880. endWP.pos.y = last_wp.pos.y + 10*sin(branch_angle);
  881. endWP.pos.x = last_wp.pos.x + 10*cos(branch_angle);
  882. WayPoint wp = last_wp;
  883. wp.pos.x = (last_wp.pos.x+endWP.pos.x)/2.0;
  884. wp.pos.y = (last_wp.pos.y+endWP.pos.y)/2.0;
  885. endWP.bDir = wp.bDir = direction;
  886. goal_path.push_back(wp);
  887. goal_path.push_back(endWP);
  888. goal_path.insert(goal_path.begin(), path.end()-5, path.end());
  889. PlanningHelpers::SmoothPath(goal_path, 0.25, 0.25);
  890. PlanningHelpers::FixPathDensity(goal_path, 0.75);
  891. PlanningHelpers::SmoothPath(goal_path, 0.25, 0.35);
  892. path.erase(path.end()-5, path.end());
  893. path.insert(path.end(), goal_path.begin(), goal_path.end());
  894. PlanningHelpers::CalcAngleAndCost(path);
  895. for(unsigned int i=0; i < path.size(); i++)
  896. {
  897. if(direction == FORWARD_LEFT_DIR)
  898. {
  899. path.at(i).state = INITIAL_STATE;
  900. path.at(i).beh_state = BEH_BRANCH_LEFT_STATE;
  901. }
  902. if(direction == FORWARD_RIGHT_DIR)
  903. {
  904. path.at(i).state = INITIAL_STATE;
  905. path.at(i).beh_state = BEH_BRANCH_RIGHT_STATE;
  906. }
  907. }
  908. }
  909. void PlanningHelpers::FixPathDensity(vector<WayPoint>& path, const double& distanceDensity)
  910. {
  911. if(path.size() == 0 || distanceDensity==0) return;
  912. double d = 0, a = 0;
  913. double margin = distanceDensity*0.01;
  914. double remaining = 0;
  915. int nPoints = 0;
  916. vector<WayPoint> fixedPath;
  917. fixedPath.push_back(path.at(0));
  918. for(unsigned int si = 0, ei=1; ei < path.size(); )
  919. {
  920. d += hypot(path.at(ei).pos.x- path.at(ei-1).pos.x, path.at(ei).pos.y- path.at(ei-1).pos.y) + remaining;
  921. a = atan2(path.at(ei).pos.y - path.at(si).pos.y, path.at(ei).pos.x - path.at(si).pos.x);
  922. if(d < distanceDensity - margin ) // skip
  923. {
  924. ei++;
  925. remaining = 0;
  926. }
  927. else if(d > (distanceDensity + margin)) // skip
  928. {
  929. WayPoint pm = path.at(si);
  930. nPoints = d / distanceDensity;
  931. for(int k = 0; k < nPoints; k++)
  932. {
  933. pm.pos.x = pm.pos.x + distanceDensity * cos(a);
  934. pm.pos.y = pm.pos.y + distanceDensity * sin(a);
  935. fixedPath.push_back(pm);
  936. }
  937. remaining = d - nPoints*distanceDensity;
  938. si++;
  939. path.at(si).pos = pm.pos;
  940. d = 0;
  941. ei++;
  942. }
  943. else
  944. {
  945. d = 0;
  946. remaining = 0;
  947. fixedPath.push_back(path.at(ei));
  948. ei++;
  949. si = ei - 1;
  950. }
  951. }
  952. path = fixedPath;
  953. }
  954. void PlanningHelpers::SmoothPath(vector<WayPoint>& path, double weight_data,
  955. double weight_smooth, double tolerance)
  956. {
  957. if (path.size() <= 2 )
  958. {
  959. //cout << "Can't Smooth Path, Path_in Size=" << path.size() << endl;
  960. return;
  961. }
  962. const vector<WayPoint>& path_in = path;
  963. vector<WayPoint> smoothPath_out = path_in;
  964. double change = tolerance;
  965. double xtemp, ytemp;
  966. int nIterations = 0;
  967. int size = path_in.size();
  968. while (change >= tolerance)
  969. {
  970. change = 0.0;
  971. for (int i = 1; i < size - 1; i++)
  972. {
  973. // if (smoothPath_out[i].pos.a != smoothPath_out[i - 1].pos.a)
  974. // continue;
  975. xtemp = smoothPath_out[i].pos.x;
  976. ytemp = smoothPath_out[i].pos.y;
  977. smoothPath_out[i].pos.x += weight_data
  978. * (path_in[i].pos.x - smoothPath_out[i].pos.x);
  979. smoothPath_out[i].pos.y += weight_data
  980. * (path_in[i].pos.y - smoothPath_out[i].pos.y);
  981. smoothPath_out[i].pos.x += weight_smooth
  982. * (smoothPath_out[i - 1].pos.x + smoothPath_out[i + 1].pos.x
  983. - (2.0 * smoothPath_out[i].pos.x));
  984. smoothPath_out[i].pos.y += weight_smooth
  985. * (smoothPath_out[i - 1].pos.y + smoothPath_out[i + 1].pos.y
  986. - (2.0 * smoothPath_out[i].pos.y));
  987. change += fabs(xtemp - smoothPath_out[i].pos.x);
  988. change += fabs(ytemp - smoothPath_out[i].pos.y);
  989. }
  990. nIterations++;
  991. }
  992. path = smoothPath_out;
  993. }
  994. void PlanningHelpers::PredictConstantTimeCostForTrajectory(std::vector<PlannerHNS::WayPoint>& path, const PlannerHNS::WayPoint& currPose, const double& minVelocity, const double& minDist)
  995. {
  996. if(path.size() == 0) return;
  997. for(unsigned int i = 0 ; i < path.size(); i++)
  998. path.at(i).timeCost = -1;
  999. if(currPose.v == 0 || currPose.v < minVelocity) return;
  1000. RelativeInfo info;
  1001. PlanningHelpers::GetRelativeInfo(path, currPose, info);
  1002. double total_distance = 0;
  1003. double accum_time = 0;
  1004. path.at(info.iFront).timeCost = 0;
  1005. if(info.iFront == 0 ) info.iFront++;
  1006. for(unsigned int i=info.iFront; i<path.size(); i++)
  1007. {
  1008. total_distance += hypot(path.at(i).pos.x- path.at(i-1).pos.x,path.at(i).pos.y- path.at(i-1).pos.y);
  1009. accum_time = total_distance/currPose.v;
  1010. path.at(i).timeCost = accum_time;
  1011. }
  1012. }
  1013. void PlanningHelpers::FixAngleOnly(std::vector<WayPoint>& path)
  1014. {
  1015. if(path.size() <= 2) return;
  1016. path[0].pos.a = UtilityH::FixNegativeAngle(atan2(path[1].pos.y - path[0].pos.y, path[1].pos.x - path[0].pos.x ));
  1017. for(int j = 1; j < path.size()-1; j++)
  1018. path[j].pos.a = UtilityH::FixNegativeAngle(atan2(path[j+1].pos.y - path[j].pos.y, path[j+1].pos.x - path[j].pos.x ));
  1019. int j = (int)path.size()-1;
  1020. path[j].pos.a = path[j-1].pos.a;
  1021. for(int j = 0; j < path.size()-1; j++)
  1022. {
  1023. if(path.at(j).pos.x == path.at(j+1).pos.x && path.at(j).pos.y == path.at(j+1).pos.y)
  1024. path.at(j).pos.a = path.at(j+1).pos.a;
  1025. }
  1026. }
  1027. double PlanningHelpers::CalcAngleAndCost(vector<WayPoint>& path, const double& lastCost, const bool& bSmooth)
  1028. {
  1029. if(path.size() < 2) return 0;
  1030. if(path.size() == 2)
  1031. {
  1032. path[0].pos.a = UtilityH::FixNegativeAngle(atan2(path[1].pos.y - path[0].pos.y, path[1].pos.x - path[0].pos.x ));
  1033. path[0].cost = lastCost;
  1034. path[1].pos.a = path[0].pos.a;
  1035. path[1].cost = path[0].cost + distance2points(path[0].pos, path[1].pos);
  1036. return path[1].cost;
  1037. }
  1038. path[0].pos.a = UtilityH::FixNegativeAngle(atan2(path[1].pos.y - path[0].pos.y, path[1].pos.x - path[0].pos.x ));
  1039. path[0].cost = lastCost;
  1040. for(int j = 1; j < path.size()-1; j++)
  1041. {
  1042. path[j].pos.a = UtilityH::FixNegativeAngle(atan2(path[j+1].pos.y - path[j].pos.y, path[j+1].pos.x - path[j].pos.x ));
  1043. path[j].cost = path[j-1].cost + distance2points(path[j-1].pos, path[j].pos);
  1044. }
  1045. int j = (int)path.size()-1;
  1046. path[j].pos.a = path[j-1].pos.a;
  1047. path[j].cost = path[j-1].cost + distance2points(path[j-1].pos, path[j].pos);
  1048. for(int j = 0; j < path.size()-1; j++)
  1049. {
  1050. if(path.at(j).pos.x == path.at(j+1).pos.x && path.at(j).pos.y == path.at(j+1).pos.y)
  1051. path.at(j).pos.a = path.at(j+1).pos.a;
  1052. }
  1053. return path[j].cost;
  1054. }
  1055. double PlanningHelpers::CalcAngleAndCostAndCurvatureAnd2D(vector<WayPoint>& path, const double& lastCost)
  1056. {
  1057. if(path.size() < 2) return -1;
  1058. path[0].pos.a = atan2(path[1].pos.y - path[0].pos.y, path[1].pos.x - path[0].pos.x );
  1059. path[0].cost = lastCost;
  1060. double k = 0;
  1061. GPSPoint center;
  1062. for(unsigned int j = 1; j < path.size()-1; j++)
  1063. {
  1064. k = CalcCircle(path[j-1].pos,path[j].pos, path[j+1].pos, center);
  1065. if(k > 150.0 || std::isnan(k))
  1066. k = 150.0;
  1067. if(k<1.0)
  1068. path[j].cost = 0;
  1069. else
  1070. path[j].cost = 1.0-1.0/k;
  1071. path[j].pos.a = atan2(path[j+1].pos.y - path[j].pos.y, path[j+1].pos.x - path[j].pos.x );
  1072. }
  1073. unsigned int j = path.size()-1;
  1074. path[0].cost = path[1].cost;
  1075. path[j].cost = path[j-1].cost;
  1076. path[j].pos.a = path[j-1].pos.a;
  1077. path[j].cost = path[j-1].cost ;
  1078. return path[j].cost;
  1079. }
  1080. double PlanningHelpers::CalcCircle(const GPSPoint& pt1, const GPSPoint& pt2, const GPSPoint& pt3, GPSPoint& center)
  1081. {
  1082. double yDelta_a= pt2.y - pt1.y;
  1083. double xDelta_a= pt2.x - pt1.x;
  1084. double yDelta_b= pt3.y - pt2.y;
  1085. double xDelta_b= pt3.x - pt2.x;
  1086. if (fabs(xDelta_a) <= 0.000000000001 && fabs(yDelta_b) <= 0.000000000001)
  1087. {
  1088. center.x= 0.5*(pt2.x + pt3.x);
  1089. center.y= 0.5*(pt1.y + pt2.y);
  1090. return distance2points(center,pt1);
  1091. }
  1092. double aSlope=yDelta_a/xDelta_a;
  1093. double bSlope=yDelta_b/xDelta_b;
  1094. if (fabs(aSlope-bSlope) <= 0.000000000001)
  1095. {
  1096. return 100000;
  1097. }
  1098. center.x= (aSlope*bSlope*(pt1.y - pt3.y) + bSlope*(pt1.x + pt2 .x) - aSlope*(pt2.x+pt3.x) )/(2.0* (bSlope-aSlope) );
  1099. center.y = -1.0*(center.x - (pt1.x+pt2.x)/2.0)/aSlope + (pt1.y+pt2.y)/2.0;
  1100. return distance2points(center,pt1);
  1101. }
  1102. void PlanningHelpers::ExtractPartFromPointToDistance(const vector<WayPoint>& originalPath, const WayPoint& pos, const double& minDistance,
  1103. const double& pathDensity, vector<WayPoint>& extractedPath, const double& SmoothDataWeight, const double& SmoothWeight, const double& SmoothTolerance)
  1104. {
  1105. extractedPath.clear();
  1106. unsigned int close_index = GetClosestNextPointIndexDirectionFast(originalPath, pos);
  1107. // int i_slow = GetClosestNextPointIndexDirection(originalPath, pos);
  1108. // if(close_index != i_slow)
  1109. // cout << "Aler Alert !!! fast: " << close_index << ", slow: " << i_slow << endl;
  1110. //vector<WayPoint> tempPath;
  1111. double d_limit = 0;
  1112. if(close_index >= 2) close_index -=2;
  1113. else close_index = 0;
  1114. for(unsigned int i=close_index; i< originalPath.size(); i++)
  1115. {
  1116. extractedPath.push_back(originalPath.at(i));
  1117. if(i>0)
  1118. d_limit += hypot(originalPath.at(i).pos.y - originalPath.at(i-1).pos.y, originalPath.at(i).pos.x - originalPath.at(i-1).pos.x);
  1119. if(d_limit > minDistance)
  1120. break;
  1121. }
  1122. if(extractedPath.size() < 2)
  1123. {
  1124. cout << endl << "### Planner Z . Extracted Rollout Path is too Small, Size = " << extractedPath.size() << endl;
  1125. return;
  1126. }
  1127. FixPathDensity(extractedPath, pathDensity);
  1128. SmoothPath(extractedPath, SmoothDataWeight, SmoothWeight , SmoothTolerance);
  1129. CalcAngleAndCost(extractedPath);
  1130. //extractedPath = tempPath;
  1131. //tempPath.clear();
  1132. //TestQuadraticSpline(extractedPath, tempPath);
  1133. }
  1134. void PlanningHelpers::ExtractPartFromPointToDistanceDirectionFast(const vector<WayPoint>& originalPath, const WayPoint& pos, const double& minDistance,
  1135. const double& pathDensity, vector<WayPoint>& extractedPath)
  1136. {
  1137. if(originalPath.size() < 2 ) return;
  1138. extractedPath.clear();
  1139. int close_index = GetClosestNextPointIndexDirectionFast(originalPath, pos);
  1140. double d = 0;
  1141. if(close_index + 1 >= originalPath.size())
  1142. close_index = originalPath.size() - 2;
  1143. for(int i=close_index; i >= 0; i--)
  1144. {
  1145. extractedPath.insert(extractedPath.begin(), originalPath.at(i));
  1146. if(i < originalPath.size())
  1147. d += hypot(originalPath.at(i).pos.y - originalPath.at(i+1).pos.y, originalPath.at(i).pos.x - originalPath.at(i+1).pos.x);
  1148. if(d > 10)
  1149. break;
  1150. }
  1151. //extractedPath.push_back(info.perp_point);
  1152. d = 0;
  1153. for(int i=close_index+1; i < (int)originalPath.size(); i++)
  1154. {
  1155. extractedPath.push_back(originalPath.at(i));
  1156. if(i > 0)
  1157. d += hypot(originalPath.at(i).pos.y - originalPath.at(i-1).pos.y, originalPath.at(i).pos.x - originalPath.at(i-1).pos.x);
  1158. if(d > minDistance)
  1159. break;
  1160. }
  1161. if(extractedPath.size() < 2)
  1162. {
  1163. cout << endl << "### Planner Z . Extracted Rollout Path is too Small, Size = " << extractedPath.size() << endl;
  1164. return;
  1165. }
  1166. FixPathDensity(extractedPath, pathDensity);
  1167. CalcAngleAndCost(extractedPath);
  1168. }
  1169. void PlanningHelpers::ExtractPartFromPointToDistanceFast(const vector<WayPoint>& originalPath, const WayPoint& pos, const double& minDistance,
  1170. const double& pathDensity, vector<WayPoint>& extractedPath, const double& SmoothDataWeight, const double& SmoothWeight, const double& SmoothTolerance)
  1171. {
  1172. extractedPath.clear();
  1173. RelativeInfo info;
  1174. GetRelativeInfo(originalPath, pos, info);
  1175. double d = 0;
  1176. if(info.iBack > 0)
  1177. info.iBack--;
  1178. for(int i=info.iBack; i >= 0; i--)
  1179. {
  1180. extractedPath.insert(extractedPath.begin(), originalPath.at(i));
  1181. if(i < originalPath.size())
  1182. d += hypot(originalPath.at(i).pos.y - originalPath.at(i+1).pos.y, originalPath.at(i).pos.x - originalPath.at(i+1).pos.x);
  1183. if(d > 10)
  1184. break;
  1185. }
  1186. //extractedPath.push_back(info.perp_point);
  1187. d = 0;
  1188. for(int i=info.iBack+1; i < (int)originalPath.size(); i++)
  1189. {
  1190. extractedPath.push_back(originalPath.at(i));
  1191. if(i > 0)
  1192. d += hypot(originalPath.at(i).pos.y - originalPath.at(i-1).pos.y, originalPath.at(i).pos.x - originalPath.at(i-1).pos.x);
  1193. if(d > minDistance)
  1194. break;
  1195. }
  1196. if(extractedPath.size() < 2)
  1197. {
  1198. cout << endl << "### Planner Z . Extracted Rollout Path is too Small, Size = " << extractedPath.size() << endl;
  1199. return;
  1200. }
  1201. FixPathDensity(extractedPath, pathDensity);
  1202. CalcAngleAndCost(extractedPath);
  1203. }
  1204. void PlanningHelpers::CalculateRollInTrajectories(const WayPoint& carPos, const double& speed, const vector<WayPoint>& originalCenter, int& start_index,
  1205. int& end_index, vector<double>& end_laterals ,
  1206. vector<vector<WayPoint> >& rollInPaths, const double& max_roll_distance,
  1207. const double& maxSpeed, const double& carTipMargin, const double& rollInMargin,
  1208. const double& rollInSpeedFactor, const double& pathDensity, const double& rollOutDensity,
  1209. const int& rollOutNumber, const double& SmoothDataWeight, const double& SmoothWeight,
  1210. const double& SmoothTolerance, const bool& bHeadingSmooth,
  1211. std::vector<WayPoint>& sampledPoints)
  1212. {
  1213. WayPoint p;
  1214. double dummyd = 0;
  1215. int iLimitIndex = (carTipMargin/0.3)/pathDensity;
  1216. if(iLimitIndex >= originalCenter.size())
  1217. iLimitIndex = originalCenter.size() - 1;
  1218. //Get Closest Index
  1219. RelativeInfo info;
  1220. GetRelativeInfo(originalCenter, carPos, info);
  1221. double remaining_distance = 0;
  1222. int close_index = info.iBack;
  1223. for(unsigned int i=close_index; i< originalCenter.size()-1; i++)
  1224. {
  1225. if(i>0)
  1226. remaining_distance += distance2points(originalCenter[i].pos, originalCenter[i+1].pos);
  1227. }
  1228. double initial_roll_in_distance = info.perp_distance ; //GetPerpDistanceToTrajectorySimple(originalCenter, carPos, close_index);
  1229. vector<WayPoint> RollOutStratPath;
  1230. ///*** Smoothing From Car Heading Section ***///
  1231. // if(bHeadingSmooth)
  1232. // {
  1233. // unsigned int num_of_strait_points = carTipMargin / pathDensity;
  1234. // int closest_for_each_iteration = 0;
  1235. // WayPoint np = GetPerpendicularOnTrajectory_obsolete(originalCenter, carPos, dummyd, closest_for_each_iteration);
  1236. // np.pos = carPos.pos;
  1237. //
  1238. // RollOutStratPath.push_back(np);
  1239. // for(unsigned int i = 0; i < num_of_strait_points; i++)
  1240. // {
  1241. // p = RollOutStratPath.at(i);
  1242. // p.pos.x = p.pos.x + pathDensity*cos(p.pos.a);
  1243. // p.pos.y = p.pos.y + pathDensity*sin(p.pos.a);
  1244. // np = GetPerpendicularOnTrajectory_obsolete(originalCenter, p, dummyd, closest_for_each_iteration);
  1245. // np.pos = p.pos;
  1246. // RollOutStratPath.push_back(np);
  1247. // }
  1248. //
  1249. // initial_roll_in_distance = GetPerpDistanceToTrajectorySimple_obsolete(originalCenter, RollOutStratPath.at(RollOutStratPath.size()-1), close_index);
  1250. // }
  1251. ///*** -------------------------------- ***///
  1252. //printf("\n Lateral Distance: %f" , initial_roll_in_distance);
  1253. //calculate the starting index
  1254. double d_limit = 0;
  1255. unsigned int far_index = close_index;
  1256. //calculate end index
  1257. double start_distance = rollInSpeedFactor*speed+rollInMargin;
  1258. if(start_distance > remaining_distance)
  1259. start_distance = remaining_distance;
  1260. d_limit = 0;
  1261. for(unsigned int i=close_index; i< originalCenter.size(); i++)
  1262. {
  1263. if(i>0)
  1264. d_limit += distance2points(originalCenter[i].pos, originalCenter[i-1].pos);
  1265. if(d_limit >= start_distance)
  1266. {
  1267. far_index = i;
  1268. break;
  1269. }
  1270. }
  1271. int centralTrajectoryIndex = rollOutNumber/2;
  1272. vector<double> end_distance_list;
  1273. for(int i=0; i< rollOutNumber+1; i++)
  1274. {
  1275. double end_roll_in_distance = rollOutDensity*(i - centralTrajectoryIndex);
  1276. end_distance_list.push_back(end_roll_in_distance);
  1277. }
  1278. start_index = close_index;
  1279. end_index = far_index;
  1280. end_laterals = end_distance_list;
  1281. //calculate the actual calculation starting index
  1282. d_limit = 0;
  1283. unsigned int smoothing_start_index = start_index;
  1284. unsigned int smoothing_end_index = end_index;
  1285. for(unsigned int i=smoothing_start_index; i< originalCenter.size(); i++)
  1286. {
  1287. if(i > 0)
  1288. d_limit += distance2points(originalCenter[i].pos, originalCenter[i-1].pos);
  1289. if(d_limit > carTipMargin)
  1290. break;
  1291. smoothing_start_index++;
  1292. }
  1293. d_limit = 0;
  1294. for(unsigned int i=end_index; i< originalCenter.size(); i++)
  1295. {
  1296. if(i > 0)
  1297. d_limit += distance2points(originalCenter[i].pos, originalCenter[i-1].pos);
  1298. if(d_limit > carTipMargin)
  1299. break;
  1300. smoothing_end_index++;
  1301. }
  1302. int nSteps = end_index - smoothing_start_index;
  1303. vector<double> inc_list;
  1304. rollInPaths.clear();
  1305. vector<double> inc_list_inc;
  1306. for(int i=0; i< rollOutNumber+1; i++)
  1307. {
  1308. double diff = end_laterals.at(i)-initial_roll_in_distance;
  1309. inc_list.push_back(diff/(double)nSteps);
  1310. rollInPaths.push_back(vector<WayPoint>());
  1311. inc_list_inc.push_back(0);
  1312. }
  1313. vector<vector<WayPoint> > execluded_from_smoothing;
  1314. for(unsigned int i=0; i< rollOutNumber+1 ; i++)
  1315. execluded_from_smoothing.push_back(vector<WayPoint>());
  1316. //Insert First strait points within the tip of the car range
  1317. for(unsigned int j = start_index; j < smoothing_start_index; j++)
  1318. {
  1319. p = originalCenter.at(j);
  1320. double original_speed = p.v;
  1321. for(unsigned int i=0; i< rollOutNumber+1 ; i++)
  1322. {
  1323. p.pos.x = originalCenter.at(j).pos.x - initial_roll_in_distance*cos(p.pos.a + M_PI_2);
  1324. p.pos.y = originalCenter.at(j).pos.y - initial_roll_in_distance*sin(p.pos.a + M_PI_2);
  1325. if(i!=centralTrajectoryIndex)
  1326. p.v = original_speed * LANE_CHANGE_SPEED_FACTOR;
  1327. else
  1328. p.v = original_speed ;
  1329. if(j < iLimitIndex)
  1330. execluded_from_smoothing.at(i).push_back(p);
  1331. else
  1332. rollInPaths.at(i).push_back(p);
  1333. sampledPoints.push_back(p);
  1334. }
  1335. }
  1336. for(unsigned int j = smoothing_start_index; j < end_index; j++)
  1337. {
  1338. p = originalCenter.at(j);
  1339. double original_speed = p.v;
  1340. for(unsigned int i=0; i< rollOutNumber+1 ; i++)
  1341. {
  1342. inc_list_inc[i] += inc_list[i];
  1343. double d = inc_list_inc[i];
  1344. p.pos.x = originalCenter.at(j).pos.x - initial_roll_in_distance*cos(p.pos.a + M_PI_2) - d*cos(p.pos.a+ M_PI_2);
  1345. p.pos.y = originalCenter.at(j).pos.y - initial_roll_in_distance*sin(p.pos.a + M_PI_2) - d*sin(p.pos.a+ M_PI_2);
  1346. if(i!=centralTrajectoryIndex)
  1347. p.v = original_speed * LANE_CHANGE_SPEED_FACTOR;
  1348. else
  1349. p.v = original_speed ;
  1350. rollInPaths.at(i).push_back(p);
  1351. sampledPoints.push_back(p);
  1352. }
  1353. }
  1354. //Insert last strait points to make better smoothing
  1355. for(unsigned int j = end_index; j < smoothing_end_index; j++)
  1356. {
  1357. p = originalCenter.at(j);
  1358. double original_speed = p.v;
  1359. for(unsigned int i=0; i< rollOutNumber+1 ; i++)
  1360. {
  1361. double d = end_laterals.at(i);
  1362. p.pos.x = originalCenter.at(j).pos.x - d*cos(p.pos.a + M_PI_2);
  1363. p.pos.y = originalCenter.at(j).pos.y - d*sin(p.pos.a + M_PI_2);
  1364. if(i!=centralTrajectoryIndex)
  1365. p.v = original_speed * LANE_CHANGE_SPEED_FACTOR;
  1366. else
  1367. p.v = original_speed ;
  1368. rollInPaths.at(i).push_back(p);
  1369. sampledPoints.push_back(p);
  1370. }
  1371. }
  1372. for(unsigned int i=0; i< rollOutNumber+1 ; i++)
  1373. rollInPaths.at(i).insert(rollInPaths.at(i).begin(), execluded_from_smoothing.at(i).begin(), execluded_from_smoothing.at(i).end());
  1374. ///*** Smoothing From Car Heading Section ***///
  1375. // if(bHeadingSmooth)
  1376. // {
  1377. // for(unsigned int i=0; i< rollOutNumber+1 ; i++)
  1378. // {
  1379. // unsigned int cut_index = GetClosestNextPointIndex(rollInPaths.at(i), RollOutStratPath.at(RollOutStratPath.size()-1));
  1380. // rollInPaths.at(i).erase(rollInPaths.at(i).begin(), rollInPaths.at(i).begin()+cut_index);
  1381. // rollInPaths.at(i).insert(rollInPaths.at(i).begin(), RollOutStratPath.begin(), RollOutStratPath.end());
  1382. // }
  1383. // }
  1384. ///*** -------------------------------- ***///
  1385. d_limit = 0;
  1386. for(unsigned int j = smoothing_end_index; j < originalCenter.size(); j++)
  1387. {
  1388. if(j > 0)
  1389. d_limit += distance2points(originalCenter.at(j).pos, originalCenter.at(j-1).pos);
  1390. if(d_limit > max_roll_distance)
  1391. break;
  1392. p = originalCenter.at(j);
  1393. double original_speed = p.v;
  1394. for(unsigned int i=0; i< rollInPaths.size() ; i++)
  1395. {
  1396. double d = end_laterals.at(i);
  1397. p.pos.x = originalCenter.at(j).pos.x - d*cos(p.pos.a + M_PI_2);
  1398. p.pos.y = originalCenter.at(j).pos.y - d*sin(p.pos.a + M_PI_2);
  1399. if(i!=centralTrajectoryIndex)
  1400. p.v = original_speed * LANE_CHANGE_SPEED_FACTOR;
  1401. else
  1402. p.v = original_speed ;
  1403. rollInPaths.at(i).push_back(p);
  1404. sampledPoints.push_back(p);
  1405. }
  1406. }
  1407. for(unsigned int i=0; i< rollOutNumber+1 ; i++)
  1408. {
  1409. SmoothPath(rollInPaths.at(i), SmoothDataWeight, SmoothWeight, SmoothTolerance);
  1410. }
  1411. // for(unsigned int i=0; i< rollInPaths.size(); i++)
  1412. // CalcAngleAndCost(rollInPaths.at(i));
  1413. }
  1414. bool PlanningHelpers::FindInList(const std::vector<int>& list,const int& x)
  1415. {
  1416. for(unsigned int i = 0 ; i < list.size(); i++)
  1417. {
  1418. if(list.at(i) == x)
  1419. return true;
  1420. }
  1421. return false;
  1422. }
  1423. void PlanningHelpers::RemoveWithValue(std::vector<int>& list,const int& x)
  1424. {
  1425. for(unsigned int i = 0 ; i < list.size(); i++)
  1426. {
  1427. if(list.at(i) == x)
  1428. {
  1429. list.erase(list.begin()+i);
  1430. }
  1431. }
  1432. }
  1433. std::vector<int> PlanningHelpers::GetUniqueLeftRightIds(const std::vector<WayPoint>& path)
  1434. {
  1435. vector<int> sideLanes;
  1436. for(unsigned int iwp = 0; iwp < path.size(); iwp++)
  1437. {
  1438. if(path.at(iwp).LeftPointId>0)
  1439. {
  1440. bool bFound = false;
  1441. for(unsigned int is = 0 ; is < sideLanes.size(); is++)
  1442. {
  1443. if(sideLanes.at(is) == path.at(iwp).LeftPointId)
  1444. {
  1445. bFound = true;
  1446. break;
  1447. }
  1448. }
  1449. if(!bFound)
  1450. sideLanes.push_back(path.at(iwp).LeftPointId);
  1451. }
  1452. if(path.at(iwp).RightPointId>0)
  1453. {
  1454. bool bFound = false;
  1455. for(unsigned int is = 0 ; is < sideLanes.size(); is++)
  1456. {
  1457. if(sideLanes.at(is) == path.at(iwp).RightPointId)
  1458. {
  1459. bFound = true;
  1460. break;
  1461. }
  1462. }
  1463. if(!bFound)
  1464. sideLanes.push_back(path.at(iwp).RightPointId);
  1465. }
  1466. //RemoveWithValue(sideLanes, path.at(iwp).laneId);
  1467. }
  1468. return sideLanes;
  1469. }
  1470. void PlanningHelpers::SmoothSpeedProfiles(vector<WayPoint>& path_in, double weight_data, double weight_smooth, double tolerance )
  1471. {
  1472. if (path_in.size() <= 1)
  1473. return;
  1474. vector<WayPoint> newpath = path_in;
  1475. double change = tolerance;
  1476. double xtemp;
  1477. int nIterations = 0;
  1478. int size = newpath.size();
  1479. while (change >= tolerance)
  1480. {
  1481. change = 0.0;
  1482. for (int i = 1; i < size -1; i++)
  1483. {
  1484. xtemp = newpath[i].v;
  1485. newpath[i].v += weight_data * (path_in[i].v - newpath[i].v);
  1486. newpath[i].v += weight_smooth * (newpath[i - 1].v + newpath[i + 1].v - (2.0 * newpath[i].v));
  1487. change += fabs(xtemp - newpath[i].v);
  1488. }
  1489. nIterations++;
  1490. }
  1491. path_in = newpath;
  1492. }
  1493. void PlanningHelpers::SmoothCurvatureProfiles(vector<WayPoint>& path_in, double weight_data, double weight_smooth, double tolerance)
  1494. {
  1495. if (path_in.size() <= 1)
  1496. return;
  1497. vector<WayPoint> newpath = path_in;
  1498. double change = tolerance;
  1499. double xtemp;
  1500. int nIterations = 0;
  1501. int size = newpath.size();
  1502. while (change >= tolerance)
  1503. {
  1504. change = 0.0;
  1505. for (int i = 1; i < size -1; i++)
  1506. {
  1507. xtemp = newpath[i].cost;
  1508. newpath[i].cost += weight_data * (path_in[i].cost - newpath[i].cost);
  1509. newpath[i].cost += weight_smooth * (newpath[i - 1].cost + newpath[i + 1].cost - (2.0 * newpath[i].cost));
  1510. change += fabs(xtemp - newpath[i].cost);
  1511. }
  1512. nIterations++;
  1513. }
  1514. path_in = newpath;
  1515. }
  1516. void PlanningHelpers::SmoothWayPointsDirections(vector<WayPoint>& path_in, double weight_data, double weight_smooth, double tolerance )
  1517. {
  1518. if (path_in.size() <= 1)
  1519. return;
  1520. vector<WayPoint> newpath = path_in;
  1521. double change = tolerance;
  1522. double xtemp;
  1523. int nIterations = 0;
  1524. int size = newpath.size();
  1525. while (change >= tolerance)
  1526. {
  1527. change = 0.0;
  1528. for (int i = 1; i < size -1; i++)
  1529. {
  1530. xtemp = newpath[i].pos.a;
  1531. newpath[i].pos.a += weight_data * (path_in[i].pos.a - newpath[i].pos.a);
  1532. newpath[i].pos.a += weight_smooth * (newpath[i - 1].pos.a + newpath[i + 1].pos.a - (2.0 * newpath[i].pos.a));
  1533. change += fabs(xtemp - newpath[i].pos.a);
  1534. }
  1535. nIterations++;
  1536. }
  1537. path_in = newpath;
  1538. }
  1539. void PlanningHelpers::SmoothGlobalPathSpeed(vector<WayPoint>& path)
  1540. {
  1541. CalcAngleAndCostAndCurvatureAnd2D(path);
  1542. SmoothSpeedProfiles(path, 0.45,0.25, 0.01);
  1543. }
  1544. void PlanningHelpers::GenerateRecommendedSpeed(vector<WayPoint>& path, const double& max_speed, const double& speedProfileFactor)
  1545. {
  1546. CalcAngleAndCostAndCurvatureAnd2D(path);
  1547. SmoothCurvatureProfiles(path, 0.4, 0.3, 0.01);
  1548. double v = 0;
  1549. for(unsigned int i = 0 ; i < path.size(); i++)
  1550. {
  1551. double k_ratio = path.at(i).cost*10.0;
  1552. double local_max = (path.at(i).v >= 0 && max_speed > path.at(i).v) ? path.at(i).v : max_speed;
  1553. if(k_ratio >= 9.5)
  1554. v = local_max;
  1555. else if(k_ratio <= 8.5)
  1556. v = 1.0*speedProfileFactor;
  1557. else
  1558. {
  1559. k_ratio = k_ratio - 8.5;
  1560. v = (local_max - 1.0) * k_ratio + 1.0;
  1561. v = v*speedProfileFactor;
  1562. }
  1563. if(v > local_max)
  1564. path.at(i).v = local_max;
  1565. else
  1566. path.at(i).v = v;
  1567. }
  1568. SmoothSpeedProfiles(path, 0.4,0.3, 0.01);
  1569. }
  1570. WayPoint* PlanningHelpers::BuildPlanningSearchTreeV2(WayPoint* pStart,
  1571. const WayPoint& goalPos,
  1572. const vector<int>& globalPath,
  1573. const double& DistanceLimit,
  1574. const bool& bEnableLaneChange,
  1575. vector<WayPoint*>& all_cells_to_delete)
  1576. {
  1577. if(!pStart) return NULL;
  1578. vector<pair<WayPoint*, WayPoint*> >nextLeafToTrace;
  1579. WayPoint* pZero = 0;
  1580. WayPoint* wp = new WayPoint();
  1581. *wp = *pStart;
  1582. nextLeafToTrace.push_back(make_pair(pZero, wp));
  1583. all_cells_to_delete.push_back(wp);
  1584. double distance = 0;
  1585. double before_change_distance = 0;
  1586. WayPoint* pGoalCell = 0;
  1587. double nCounter = 0;
  1588. while(nextLeafToTrace.size()>0)
  1589. {
  1590. nCounter++;
  1591. unsigned int min_cost_index = 0;
  1592. double min_cost = DBL_MAX;
  1593. for(unsigned int i=0; i < nextLeafToTrace.size(); i++)
  1594. {
  1595. if(nextLeafToTrace.at(i).second->cost < min_cost)
  1596. {
  1597. min_cost = nextLeafToTrace.at(i).second->cost;
  1598. min_cost_index = i;
  1599. }
  1600. }
  1601. WayPoint* pH = nextLeafToTrace.at(min_cost_index).second;
  1602. assert(pH != 0);
  1603. nextLeafToTrace.erase(nextLeafToTrace.begin()+min_cost_index);
  1604. double distance_to_goal = distance2points(pH->pos, goalPos.pos);
  1605. double angle_to_goal = UtilityH::AngleBetweenTwoAnglesPositive(UtilityH::FixNegativeAngle(pH->pos.a), UtilityH::FixNegativeAngle(goalPos.pos.a));
  1606. if( distance_to_goal <= 0.1 && angle_to_goal < M_PI_4)
  1607. {
  1608. cout << "Goal Found, LaneID: " << pH->laneId <<", Distance : " << distance_to_goal << ", Angle: " << angle_to_goal*RAD2DEG << endl;
  1609. pGoalCell = pH;
  1610. break;
  1611. }
  1612. else
  1613. {
  1614. if(pH->pLeft && !CheckLaneExits(all_cells_to_delete, pH->pLeft->pLane) && !CheckNodeExits(all_cells_to_delete, pH->pLeft) && bEnableLaneChange && before_change_distance > LANE_CHANGE_MIN_DISTANCE)
  1615. {
  1616. wp = new WayPoint();
  1617. *wp = *pH->pLeft;
  1618. double d = hypot(wp->pos.y - pH->pos.y, wp->pos.x - pH->pos.x);
  1619. distance += d;
  1620. before_change_distance = -LANE_CHANGE_MIN_DISTANCE*3;
  1621. for(unsigned int a = 0; a < wp->actionCost.size(); a++)
  1622. {
  1623. //if(wp->actionCost.at(a).first == LEFT_TURN_ACTION)
  1624. d += wp->actionCost.at(a).second;
  1625. }
  1626. wp->cost = pH->cost + d;
  1627. wp->pRight = pH;
  1628. wp->pLeft = 0;
  1629. nextLeafToTrace.push_back(make_pair(pH, wp));
  1630. all_cells_to_delete.push_back(wp);
  1631. }
  1632. if(pH->pRight && !CheckLaneExits(all_cells_to_delete, pH->pRight->pLane) && !CheckNodeExits(all_cells_to_delete, pH->pRight) && bEnableLaneChange && before_change_distance > LANE_CHANGE_MIN_DISTANCE)
  1633. {
  1634. wp = new WayPoint();
  1635. *wp = *pH->pRight;
  1636. double d = hypot(wp->pos.y - pH->pos.y, wp->pos.x - pH->pos.x);
  1637. distance += d;
  1638. before_change_distance = -LANE_CHANGE_MIN_DISTANCE*3;
  1639. for(unsigned int a = 0; a < wp->actionCost.size(); a++)
  1640. {
  1641. //if(wp->actionCost.at(a).first == RIGHT_TURN_ACTION)
  1642. d += wp->actionCost.at(a).second;
  1643. }
  1644. wp->cost = pH->cost + d ;
  1645. wp->pLeft = pH;
  1646. wp->pRight = 0;
  1647. nextLeafToTrace.push_back(make_pair(pH, wp));
  1648. all_cells_to_delete.push_back(wp);
  1649. }
  1650. for(unsigned int i =0; i< pH->pFronts.size(); i++)
  1651. {
  1652. if(CheckLaneIdExits(globalPath, pH->pLane) && pH->pFronts.at(i) && !CheckNodeExits(all_cells_to_delete, pH->pFronts.at(i)))
  1653. {
  1654. wp = new WayPoint();
  1655. *wp = *pH->pFronts.at(i);
  1656. double d = hypot(wp->pos.y - pH->pos.y, wp->pos.x - pH->pos.x);
  1657. distance += d;
  1658. before_change_distance += d;
  1659. for(unsigned int a = 0; a < wp->actionCost.size(); a++)
  1660. {
  1661. //if(wp->actionCost.at(a).first == FORWARD_ACTION)
  1662. d += wp->actionCost.at(a).second;
  1663. }
  1664. wp->cost = pH->cost + d;
  1665. wp->pBacks.push_back(pH);
  1666. nextLeafToTrace.push_back(make_pair(pH, wp));
  1667. all_cells_to_delete.push_back(wp);
  1668. }
  1669. }
  1670. }
  1671. if(distance > DistanceLimit && globalPath.size()==0)
  1672. {
  1673. //if(!pGoalCell)
  1674. cout << "Goal Not Found, LaneID: " << pH->laneId <<", Distance : " << distance << endl;
  1675. pGoalCell = pH;
  1676. break;
  1677. }
  1678. //pGoalCell = pH;
  1679. }
  1680. while(nextLeafToTrace.size()!=0)
  1681. nextLeafToTrace.pop_back();
  1682. //closed_nodes.clear();
  1683. return pGoalCell;
  1684. }
  1685. WayPoint* PlanningHelpers::BuildPlanningSearchTreeStraight(WayPoint* pStart,
  1686. const double& DistanceLimit,
  1687. vector<WayPoint*>& all_cells_to_delete)
  1688. {
  1689. if(!pStart) return NULL;
  1690. vector<pair<WayPoint*, WayPoint*> >nextLeafToTrace;
  1691. WayPoint* pZero = 0;
  1692. WayPoint* wp = new WayPoint();
  1693. *wp = *pStart;
  1694. wp->cost = 0;
  1695. nextLeafToTrace.push_back(make_pair(pZero, wp));
  1696. all_cells_to_delete.push_back(wp);
  1697. double distance = 0;
  1698. WayPoint* pGoalCell = 0;
  1699. double nCounter = 0;
  1700. while(nextLeafToTrace.size()>0)
  1701. {
  1702. nCounter++;
  1703. unsigned int min_cost_index = 0;
  1704. double min_cost = DBL_MAX;
  1705. for(unsigned int i=0; i < nextLeafToTrace.size(); i++)
  1706. {
  1707. if(nextLeafToTrace.at(i).second->cost < min_cost)
  1708. {
  1709. min_cost = nextLeafToTrace.at(i).second->cost;
  1710. min_cost_index = i;
  1711. }
  1712. }
  1713. WayPoint* pH = nextLeafToTrace.at(min_cost_index).second;
  1714. assert(pH != 0);
  1715. nextLeafToTrace.erase(nextLeafToTrace.begin()+min_cost_index);
  1716. for(unsigned int i =0; i< pH->pFronts.size(); i++)
  1717. {
  1718. if(pH->pFronts.at(i) && !CheckNodeExits(all_cells_to_delete, pH->pFronts.at(i)))
  1719. {
  1720. wp = new WayPoint();
  1721. *wp = *pH->pFronts.at(i);
  1722. double d = hypot(wp->pos.y - pH->pos.y, wp->pos.x - pH->pos.x);
  1723. distance += d;
  1724. // for(unsigned int a = 0; a < wp->actionCost.size(); a++)
  1725. // {
  1726. // //if(wp->actionCost.at(a).first == FORWARD_ACTION)
  1727. // d += wp->actionCost.at(a).second;
  1728. // }
  1729. wp->cost = pH->cost + d;
  1730. wp->pBacks.push_back(pH);
  1731. if(wp->cost < DistanceLimit)
  1732. {
  1733. nextLeafToTrace.push_back(make_pair(pH, wp));
  1734. all_cells_to_delete.push_back(wp);
  1735. }
  1736. else
  1737. delete wp;
  1738. }
  1739. }
  1740. // if(pH->pLeft && !CheckLaneExits(all_cells_to_delete, pH->pLeft->pLane))
  1741. // {
  1742. // wp = new WayPoint();
  1743. // *wp = *pH->pLeft;
  1744. // double d = hypot(wp->pos.y - pH->pos.y, wp->pos.x - pH->pos.x);
  1745. //
  1746. // for(unsigned int a = 0; a < wp->actionCost.size(); a++)
  1747. // {
  1748. // //if(wp->actionCost.at(a).first == LEFT_TURN_ACTION)
  1749. // d += wp->actionCost.at(a).second;
  1750. // }
  1751. //
  1752. // wp->cost = pH->cost + d + LANE_CHANGE_COST;
  1753. // wp->pRight = pH;
  1754. // wp->pRight = 0;
  1755. //
  1756. // nextLeafToTrace.push_back(make_pair(pH, wp));
  1757. // all_cells_to_delete.push_back(wp);
  1758. // }
  1759. //
  1760. // if(pH->pRight && !CheckLaneExits(all_cells_to_delete, pH->pRight->pLane))
  1761. // {
  1762. // wp = new WayPoint();
  1763. // *wp = *pH->pRight;
  1764. // double d = hypot(wp->pos.y - pH->pos.y, wp->pos.x - pH->pos.x);;
  1765. //
  1766. // for(unsigned int a = 0; a < wp->actionCost.size(); a++)
  1767. // {
  1768. // //if(wp->actionCost.at(a).first == RIGHT_TURN_ACTION)
  1769. // d += wp->actionCost.at(a).second;
  1770. // }
  1771. //
  1772. // wp->cost = pH->cost + d + LANE_CHANGE_COST;
  1773. // wp->pLeft = pH;
  1774. // wp->pRight = 0;
  1775. // nextLeafToTrace.push_back(make_pair(pH, wp));
  1776. // all_cells_to_delete.push_back(wp);
  1777. // }
  1778. pGoalCell = pH;
  1779. }
  1780. while(nextLeafToTrace.size()!=0)
  1781. nextLeafToTrace.pop_back();
  1782. return pGoalCell;
  1783. }
  1784. int PlanningHelpers::PredictiveIgnorIdsDP(WayPoint* pStart, const double& DistanceLimit,
  1785. vector<WayPoint*>& all_cells_to_delete,vector<WayPoint*>& end_waypoints, std::vector<int>& lanes_ids)
  1786. {
  1787. if(!pStart) return 0;
  1788. vector<pair<WayPoint*, WayPoint*> >nextLeafToTrace;
  1789. WayPoint* pZero = 0;
  1790. WayPoint* wp = new WayPoint();
  1791. *wp = *pStart;
  1792. wp->cost = 0;
  1793. wp->pLeft = 0;
  1794. wp->pRight = 0;
  1795. nextLeafToTrace.push_back(make_pair(pZero, wp));
  1796. all_cells_to_delete.push_back(wp);
  1797. double distance = 0;
  1798. end_waypoints.clear();
  1799. double nCounter = 0;
  1800. while(nextLeafToTrace.size()>0)
  1801. {
  1802. nCounter++;
  1803. WayPoint* pH = nextLeafToTrace.at(0).second;
  1804. assert(pH != 0);
  1805. nextLeafToTrace.erase(nextLeafToTrace.begin()+0);
  1806. for(unsigned int i =0; i< pH->pFronts.size(); i++)
  1807. {
  1808. if(pH->pFronts.at(i) && !CheckNodeExits(all_cells_to_delete, pH->pFronts.at(i)))
  1809. {
  1810. if(pH->cost < DistanceLimit)
  1811. {
  1812. wp = new WayPoint();
  1813. *wp = *pH->pFronts.at(i);
  1814. double d = distance2points(wp->pos, pH->pos);
  1815. distance += d;
  1816. wp->cost = pH->cost + d;
  1817. wp->pBacks.push_back(pH);
  1818. wp->pLeft = 0;
  1819. wp->pRight = 0;
  1820. bool bFoundLane = false;
  1821. for(unsigned int k = 0 ; k < lanes_ids.size(); k++)
  1822. {
  1823. if(wp->laneId == lanes_ids.at(k))
  1824. {
  1825. bFoundLane = true;
  1826. break;
  1827. }
  1828. }
  1829. if(!bFoundLane)
  1830. nextLeafToTrace.push_back(make_pair(pH, wp));
  1831. all_cells_to_delete.push_back(wp);
  1832. }
  1833. else
  1834. {
  1835. end_waypoints.push_back(pH);
  1836. }
  1837. }
  1838. }
  1839. }
  1840. while(nextLeafToTrace.size()!=0)
  1841. nextLeafToTrace.pop_back();
  1842. //closed_nodes.clear();
  1843. return end_waypoints.size();
  1844. }
  1845. int PlanningHelpers::PredictiveDP(WayPoint* pStart, const double& DistanceLimit,
  1846. vector<WayPoint*>& all_cells_to_delete,vector<WayPoint*>& end_waypoints)
  1847. {
  1848. if(!pStart) return 0;
  1849. vector<pair<WayPoint*, WayPoint*> >nextLeafToTrace;
  1850. WayPoint* pZero = 0;
  1851. WayPoint* wp = new WayPoint();
  1852. *wp = *pStart;
  1853. wp->pLeft = 0;
  1854. wp->pRight = 0;
  1855. nextLeafToTrace.push_back(make_pair(pZero, wp));
  1856. all_cells_to_delete.push_back(wp);
  1857. double distance = 0;
  1858. end_waypoints.clear();
  1859. double nCounter = 0;
  1860. while(nextLeafToTrace.size()>0)
  1861. {
  1862. nCounter++;
  1863. WayPoint* pH = nextLeafToTrace.at(0).second;
  1864. assert(pH != 0);
  1865. nextLeafToTrace.erase(nextLeafToTrace.begin()+0);
  1866. for(unsigned int i =0; i< pH->pFronts.size(); i++)
  1867. {
  1868. if(pH->pFronts.at(i) && !CheckNodeExits(all_cells_to_delete, pH->pFronts.at(i)))
  1869. {
  1870. if(pH->cost < DistanceLimit)
  1871. {
  1872. wp = new WayPoint();
  1873. *wp = *pH->pFronts.at(i);
  1874. double d = distance2points(wp->pos, pH->pos);
  1875. distance += d;
  1876. wp->cost = pH->cost + d;
  1877. wp->pBacks.push_back(pH);
  1878. wp->pLeft = 0;
  1879. wp->pRight = 0;
  1880. nextLeafToTrace.push_back(make_pair(pH, wp));
  1881. all_cells_to_delete.push_back(wp);
  1882. }
  1883. else
  1884. {
  1885. end_waypoints.push_back(pH);
  1886. }
  1887. }
  1888. }
  1889. }
  1890. while(nextLeafToTrace.size()!=0)
  1891. nextLeafToTrace.pop_back();
  1892. //closed_nodes.clear();
  1893. return end_waypoints.size();
  1894. }
  1895. bool PlanningHelpers::CheckLaneIdExits(const std::vector<int>& lanes, const Lane* pL)
  1896. {
  1897. if(lanes.size()==0) return true;
  1898. for(unsigned int i=0; i< lanes.size(); i++)
  1899. {
  1900. if(lanes.at(i) == pL->id)
  1901. return true;
  1902. }
  1903. return false;
  1904. }
  1905. WayPoint* PlanningHelpers::CheckLaneExits(const vector<WayPoint*>& nodes, const Lane* pL)
  1906. {
  1907. if(nodes.size()==0) return nullptr;
  1908. for(unsigned int i=0; i< nodes.size(); i++)
  1909. {
  1910. if(nodes.at(i)->pLane == pL)
  1911. return nodes.at(i);
  1912. }
  1913. return nullptr;
  1914. }
  1915. WayPoint* PlanningHelpers::CheckNodeExits(const vector<WayPoint*>& nodes, const WayPoint* pL)
  1916. {
  1917. if(nodes.size()==0) return nullptr;
  1918. for(unsigned int i=0; i< nodes.size(); i++)
  1919. {
  1920. if(nodes.at(i)->laneId == pL->laneId && nodes.at(i)->id == pL->id)
  1921. return nodes.at(i);
  1922. }
  1923. return nullptr;
  1924. }
  1925. WayPoint* PlanningHelpers::CreateLaneHeadCell(Lane* pLane, WayPoint* pLeft, WayPoint* pRight,
  1926. WayPoint* pBack)
  1927. {
  1928. if(!pLane) return nullptr;
  1929. if(pLane->points.size()==0) return nullptr;
  1930. WayPoint* c = new WayPoint;
  1931. c->pLane = pLane;
  1932. c->pos = pLane->points.at(0).pos;
  1933. c->v = pLane->speed;
  1934. c->laneId = pLane->id;
  1935. c->pLeft = pLeft;
  1936. if(pLeft)
  1937. c->cost = pLeft->cost;
  1938. c->pRight = pRight;
  1939. if(pRight)
  1940. c->cost = pRight->cost;
  1941. if(pBack)
  1942. {
  1943. pBack->pFronts.push_back(c);
  1944. c->pBacks.push_back(pBack);
  1945. c->cost = pBack->cost + distance2points(c->pos, pBack->pos);
  1946. for(unsigned int i=0; i< c->pBacks.size(); i++)
  1947. {
  1948. if(c->pBacks.at(i)->cost < c->cost)
  1949. c->cost = c->pBacks.at(i)->cost;
  1950. }
  1951. }
  1952. return c;
  1953. }
  1954. double PlanningHelpers::GetLanePoints(Lane* l, const WayPoint& prevWayPointIndex,
  1955. const double& minDistance , const double& prevCost, vector<WayPoint>& points)
  1956. {
  1957. if(l == NULL || minDistance<=0) return 0;
  1958. int index = 0;
  1959. WayPoint p1, p2;
  1960. WayPoint idx;
  1961. p2 = p1 = l->points.at(index);
  1962. p1.pLane = l;
  1963. p1.cost = prevCost;
  1964. p2.cost = p1.cost + distance2points(p1.pos, p2.pos);
  1965. points.push_back(p1);
  1966. for(unsigned int i=index+1; i<l->points.size(); i++)
  1967. {
  1968. p2 = l->points.at(i);
  1969. p2.pLane = l;
  1970. p2.cost = p1.cost + distance2points(p1.pos, p2.pos);
  1971. points.push_back(p2);
  1972. if(p2.cost >= minDistance)
  1973. break;
  1974. p1 = p2;
  1975. }
  1976. return p2.cost;
  1977. }
  1978. WayPoint* PlanningHelpers::GetMinCostCell(const vector<WayPoint*>& cells, const vector<int>& globalPathIds)
  1979. {
  1980. if(cells.size() == 1)
  1981. {
  1982. // for(unsigned int j = 0; j < cells.at(0)->actionCost.size(); j++)
  1983. // cout << "Cost (" << cells.at(0)->laneId << ") of going : " << cells.at(0)->actionCost.at(j).first << ", is : " << cells.at(0)->actionCost.at(j).second << endl;
  1984. return cells.at(0);
  1985. }
  1986. WayPoint* pC = cells.at(0); //cost is distance
  1987. for(unsigned int i=1; i < cells.size(); i++)
  1988. {
  1989. bool bFound = false;
  1990. if(globalPathIds.size()==0)
  1991. bFound = true;
  1992. int iLaneID = cells.at(i)->id;
  1993. for(unsigned int j=0; j < globalPathIds.size(); j++)
  1994. {
  1995. if(globalPathIds.at(j) == iLaneID)
  1996. {
  1997. bFound = true;
  1998. break;
  1999. }
  2000. }
  2001. // for(unsigned int j = 0; j < cells.at(0)->actionCost.size(); j++)
  2002. // cout << "Cost ("<< i <<") of going : " << cells.at(0)->actionCost.at(j).first << ", is : " << cells.at(0)->actionCost.at(j).second << endl;
  2003. if(cells.at(i)->cost < pC->cost && bFound == true)
  2004. pC = cells.at(i);
  2005. }
  2006. return pC;
  2007. }
  2008. void PlanningHelpers::ExtractPlanAlernatives(const std::vector<WayPoint>& singlePath, std::vector<std::vector<WayPoint> >& allPaths)
  2009. {
  2010. if(singlePath.size() == 0)
  2011. return;
  2012. allPaths.clear();
  2013. std::vector<WayPoint> path;
  2014. path.push_back(singlePath.at(0));
  2015. double skip_distance = 8;
  2016. double d = 0;
  2017. bool bStartSkip = false;
  2018. for(unsigned int i= 1; i < singlePath.size(); i++)
  2019. {
  2020. if(singlePath.at(i).bDir != FORWARD_DIR && singlePath.at(i).pLane && singlePath.at(i).pFronts.size() > 0)
  2021. {
  2022. bStartSkip = true;
  2023. WayPoint start_point = singlePath.at(i-1);
  2024. cout << "Current Velocity = " << start_point.v << endl;
  2025. RelativeInfo start_info;
  2026. PlanningHelpers::GetRelativeInfo(start_point.pLane->points, start_point, start_info);
  2027. vector<WayPoint*> local_cell_to_delete;
  2028. PlannerHNS::WayPoint* pStart = &start_point.pLane->points.at(start_info.iFront);
  2029. WayPoint* pLaneCell = PlanningHelpers::BuildPlanningSearchTreeStraight(pStart, BACKUP_STRAIGHT_PLAN_DISTANCE, local_cell_to_delete);
  2030. if(pLaneCell)
  2031. {
  2032. vector<WayPoint> straight_path;
  2033. vector<vector<WayPoint> > tempCurrentForwardPathss;
  2034. vector<int> globalPathIds;
  2035. PlanningHelpers::TraversePathTreeBackwards(pLaneCell, pStart, globalPathIds, straight_path, tempCurrentForwardPathss);
  2036. if(straight_path.size() > 2)
  2037. {
  2038. straight_path.insert(straight_path.begin(), path.begin(), path.end());
  2039. for(unsigned int ic = 0; ic < straight_path.size(); ic++)
  2040. straight_path.at(ic).laneChangeCost = 1;
  2041. allPaths.push_back(straight_path);
  2042. }
  2043. }
  2044. }
  2045. if(bStartSkip)
  2046. {
  2047. d += hypot(singlePath.at(i).pos.y - singlePath.at(i-1).pos.y, singlePath.at(i).pos.x - singlePath.at(i-1).pos.x);
  2048. if(d > skip_distance)
  2049. {
  2050. d = 0;
  2051. bStartSkip = false;
  2052. }
  2053. }
  2054. if(!bStartSkip)
  2055. path.push_back(singlePath.at(i));
  2056. }
  2057. allPaths.push_back(path);
  2058. }
  2059. void PlanningHelpers::TraversePathTreeBackwards(WayPoint* pHead, WayPoint* pStartWP,const vector<int>& globalPathIds,
  2060. vector<WayPoint>& localPath, std::vector<std::vector<WayPoint> >& localPaths)
  2061. {
  2062. if(pHead != NULL && pHead->id != pStartWP->id)
  2063. {
  2064. if(pHead->pBacks.size()>0)
  2065. {
  2066. localPaths.push_back(localPath);
  2067. TraversePathTreeBackwards(GetMinCostCell(pHead->pBacks, globalPathIds),pStartWP, globalPathIds, localPath, localPaths);
  2068. pHead->bDir = FORWARD_DIR;
  2069. localPath.push_back(*pHead);
  2070. }
  2071. else if(pHead->pLeft && pHead->cost > 0)
  2072. {
  2073. //vector<Vector2D> forward_path;
  2074. //TravesePathTreeForwards(pHead->pLeft, forward_path, FORWARD_RIGHT);
  2075. //localPaths.push_back(forward_path);
  2076. cout << "Global Lane Change Right " << endl;
  2077. TraversePathTreeBackwards(pHead->pLeft,pStartWP, globalPathIds, localPath, localPaths);
  2078. pHead->bDir = FORWARD_RIGHT_DIR;
  2079. localPath.push_back(*pHead);
  2080. }
  2081. else if(pHead->pRight && pHead->cost > 0)
  2082. {
  2083. //vector<Vector2D> forward_path;
  2084. //TravesePathTreeForwards(pHead->pRight, forward_path, FORWARD_LEFT);
  2085. //localPaths.push_back(forward_path);
  2086. cout << "Global Lane Change Left " << endl;
  2087. TraversePathTreeBackwards(pHead->pRight,pStartWP, globalPathIds, localPath, localPaths);
  2088. pHead->bDir = FORWARD_LEFT_DIR;
  2089. localPath.push_back(*pHead);
  2090. }
  2091. // else
  2092. // cout << "Err: PlannerZ -> NULL Back Pointer " << pHead;
  2093. }
  2094. else
  2095. assert(pHead);
  2096. }
  2097. ACTION_TYPE PlanningHelpers::GetBranchingDirection(WayPoint& currWP, WayPoint& nextWP)
  2098. {
  2099. ACTION_TYPE t = FORWARD_ACTION;
  2100. // //first Get the average of the next 3 waypoint directions
  2101. // double angle = 0;
  2102. // if(nextWP.pLane->id == 487)
  2103. // angle = 11;
  2104. //
  2105. // int counter = 0;
  2106. // angle = 0;
  2107. //
  2108. // for(unsigned int i=0; i < nextWP.pLane->points.size() && counter < 10; i++, counter++)
  2109. // {
  2110. // angle += nextWP.pLane->points.at(i).pos.a;
  2111. // }
  2112. // angle = angle / counter;
  2113. //
  2114. // //Get Circular angle for correct subtraction
  2115. // double circle_angle = UtilityH::GetCircularAngle(currWP.pos.a, angle);
  2116. //
  2117. // if( currWP.pos.a - circle_angle > (7.5*DEG2RAD))
  2118. // {
  2119. // t = RIGHT_TURN_ACTION;
  2120. // cout << "Right Lane, Average Angle = " << angle*RAD2DEG << ", Circle Angle = " << circle_angle*RAD2DEG << ", currAngle = " << currWP.pos.a*RAD2DEG << endl;
  2121. // }
  2122. // else if( currWP.pos.a - circle_angle < (-7.5*DEG2RAD))
  2123. // {
  2124. // t = LEFT_TURN_ACTION;
  2125. // cout << "Left Lane, Average Angle = " << angle*RAD2DEG << ", Circle Angle = " << circle_angle*RAD2DEG << ", currAngle = " << currWP.pos.a*RAD2DEG << endl;
  2126. // }
  2127. return t;
  2128. }
  2129. void PlanningHelpers::CalcContourPointsForDetectedObjects(const WayPoint& currPose, vector<DetectedObject>& obj_list, const double& filterDistance)
  2130. {
  2131. vector<DetectedObject> res_list;
  2132. for(unsigned int i = 0; i < obj_list.size(); i++)
  2133. {
  2134. GPSPoint center = obj_list.at(i).center.pos;
  2135. double distance = distance2points(center, currPose.pos);
  2136. if(distance < filterDistance)
  2137. {
  2138. DetectedObject obj = obj_list.at(i);
  2139. Mat3 rotationMat(center.a);
  2140. Mat3 translationMat(center.x, center.y);
  2141. double w2 = obj.w/2.0;
  2142. double h2 = obj.l/2.0;
  2143. double z = center.z + obj.h/2.0;
  2144. GPSPoint left_bottom(-w2, -h2, z,0);
  2145. GPSPoint right_bottom(w2,-h2, z,0);
  2146. GPSPoint right_top(w2,h2, z,0);
  2147. GPSPoint left_top(-w2,h2, z,0);
  2148. left_bottom = rotationMat * left_bottom;
  2149. right_bottom = rotationMat * right_bottom;
  2150. right_top = rotationMat * right_top;
  2151. left_top = rotationMat * left_top;
  2152. left_bottom = translationMat * left_bottom;
  2153. right_bottom = translationMat * right_bottom;
  2154. right_top = translationMat * right_top;
  2155. left_top = translationMat * left_top;
  2156. obj.contour.clear();
  2157. obj.contour.push_back(left_bottom);
  2158. obj.contour.push_back(right_bottom);
  2159. obj.contour.push_back(right_top);
  2160. obj.contour.push_back(left_top);
  2161. res_list.push_back(obj);
  2162. }
  2163. }
  2164. obj_list = res_list;
  2165. }
  2166. double PlanningHelpers::GetVelocityAhead(const std::vector<WayPoint>& path, const RelativeInfo& info, int& prev_index, const double& reasonable_brake_distance)
  2167. {
  2168. if(path.size()==0) return 0;
  2169. double min_v = path.at(info.iBack).v;
  2170. double d = info.to_front_distance;
  2171. int local_i = info.iFront;
  2172. while(local_i < path.size()-1 && d < reasonable_brake_distance)
  2173. {
  2174. local_i++;
  2175. d += hypot(path.at(local_i).pos.y - path.at(local_i-1).pos.y, path.at(local_i).pos.x - path.at(local_i-1).pos.x);
  2176. if(path.at(local_i).v < min_v)
  2177. min_v = path.at(local_i).v;
  2178. }
  2179. if(local_i < prev_index && prev_index < path.size())
  2180. {
  2181. min_v = path.at(prev_index).v;
  2182. }
  2183. else
  2184. prev_index = local_i;
  2185. return min_v;
  2186. }
  2187. void PlanningHelpers::WritePathToFile(const string& fileName, const vector<WayPoint>& path)
  2188. {
  2189. DataRW dataFile;
  2190. ostringstream str_header;
  2191. str_header << "laneID" << "," << "wpID" << "," "x" << "," << "y" << "," << "a"<<","<< "cost" << "," << "Speed" << "," ;
  2192. vector<string> dataList;
  2193. for(unsigned int i=0; i<path.size(); i++)
  2194. {
  2195. ostringstream strwp;
  2196. strwp << path.at(i).laneId << "," << path.at(i).id <<","<<path.at(i).pos.x<<","<< path.at(i).pos.y
  2197. <<","<< path.at(i).pos.a << "," << path.at(i).cost << "," << path.at(i).v << ",";
  2198. dataList.push_back(strwp.str());
  2199. }
  2200. dataFile.WriteLogData("", fileName, str_header.str(), dataList);
  2201. }
  2202. LIGHT_INDICATOR PlanningHelpers::GetIndicatorsFromPath(const std::vector<WayPoint>& path, const WayPoint& pose, const double& seachDistance)
  2203. {
  2204. if(path.size() < 2)
  2205. return INDICATOR_NONE;
  2206. LIGHT_INDICATOR ind = INDICATOR_NONE;
  2207. RelativeInfo info;
  2208. PlanningHelpers::GetRelativeInfo(path, pose, info);
  2209. if(info.perp_point.actionCost.size() > 0)
  2210. {
  2211. if(info.perp_point.actionCost.at(0).first == LEFT_TURN_ACTION)
  2212. ind = INDICATOR_LEFT;
  2213. else if(info.perp_point.actionCost.at(0).first == RIGHT_TURN_ACTION)
  2214. ind = INDICATOR_RIGHT;
  2215. }
  2216. double total_d = 0;
  2217. for(unsigned int i=info.iFront; i < path.size()-2; i++)
  2218. {
  2219. total_d+= hypot(path.at(i+1).pos.y - path.at(i).pos.y, path.at(i+1).pos.x - path.at(i).pos.x);
  2220. if(path.at(i).actionCost.size() > 0)
  2221. {
  2222. if(path.at(i).actionCost.at(0).first == LEFT_TURN_ACTION)
  2223. return INDICATOR_LEFT;
  2224. else if(path.at(i).actionCost.at(0).first == RIGHT_TURN_ACTION)
  2225. return INDICATOR_RIGHT;
  2226. }
  2227. if(total_d > seachDistance)
  2228. break;
  2229. }
  2230. return ind;
  2231. }
  2232. PlannerHNS::WayPoint PlanningHelpers::GetRealCenter(const PlannerHNS::WayPoint& currState, const double& wheel_base)
  2233. {
  2234. PlannerHNS::WayPoint pose_center = currState;
  2235. PlannerHNS::Mat3 rotationMat(-currState.pos.a);
  2236. PlannerHNS::Mat3 translationMat(-currState.pos.x, -currState.pos.y);
  2237. PlannerHNS::Mat3 rotationMatInv(currState.pos.a);
  2238. PlannerHNS::Mat3 translationMatInv(currState.pos.x, currState.pos.y);
  2239. pose_center.pos = translationMat*pose_center.pos;
  2240. pose_center.pos = rotationMat*pose_center.pos;
  2241. pose_center.pos.x += wheel_base/3.0;
  2242. pose_center.pos = rotationMatInv*pose_center.pos;
  2243. pose_center.pos = translationMatInv*pose_center.pos;
  2244. return pose_center;
  2245. }
  2246. double PlanningHelpers::frunge ( double x )
  2247. {
  2248. double fx;
  2249. fx = 1.0 / ( 1.0 + 25.0 * x * x );
  2250. return fx;
  2251. }
  2252. double PlanningHelpers::fprunge ( double x )
  2253. {
  2254. double bot;
  2255. double fx;
  2256. bot = 1.0 + 25.0 * x * x;
  2257. fx = -50.0 * x / ( bot * bot );
  2258. return fx;
  2259. }
  2260. double PlanningHelpers::fpprunge ( double x )
  2261. {
  2262. double bot;
  2263. double fx;
  2264. bot = 1.0 + 25.0 * x * x;
  2265. fx = ( -50.0 + 3750.0 * x * x ) / ( bot * bot * bot );
  2266. return fx;
  2267. }
  2268. } /* namespace PlannerHNS */