PlanningHelpers.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /// \file PlanningHelpers.h
  2. /// \brief Helper functions for planning algorithms
  3. /// \author Hatem Darweesh
  4. /// \date Jun 16, 2016
  5. #ifndef PLANNINGHELPERS_H_
  6. #define PLANNINGHELPERS_H_
  7. #include "RoadNetwork.h"
  8. #include "op_utility/UtilityH.h"
  9. #include "op_utility/DataRW.h"
  10. #include "tinyxml.h"
  11. namespace PlannerHNS {
  12. #define distance2points(from , to) sqrt(pow(to.x - from.x, 2) + pow(to.y - from.y, 2))
  13. #define distance2pointsSqr(from , to) pow(to.x - from.x, 2) + pow(to.y - from.y, 2)
  14. #define pointNorm(v) sqrt(v.x*v.x + v.y*v.y)
  15. #define angle2points(from , to) atan2(to.y - from.y, to.x - from.x )
  16. #define LANE_CHANGE_SPEED_FACTOR 0.5
  17. #define LANE_CHANGE_COST 3.0 // meters
  18. #define BACKUP_STRAIGHT_PLAN_DISTANCE 75 //meters
  19. #define LANE_CHANGE_MIN_DISTANCE 5
  20. class PlanningHelpers
  21. {
  22. public:
  23. static std::vector<std::pair<GPSPoint, GPSPoint> > m_TestingClosestPoint;
  24. public:
  25. PlanningHelpers();
  26. virtual ~PlanningHelpers();
  27. static bool GetRelativeInfo(const std::vector<WayPoint>& trajectory, const WayPoint& p, RelativeInfo& info, const int& prevIndex = 0);
  28. static bool GetRelativeInfoRange(const std::vector<std::vector<WayPoint> >& trajectories, const WayPoint& p, const double& searchDistance, RelativeInfo& info);
  29. static bool GetRelativeInfoLimited(const std::vector<WayPoint>& trajectory, const WayPoint& p, RelativeInfo& info, const int& prevIndex = 0);
  30. static WayPoint GetFollowPointOnTrajectory(const std::vector<WayPoint>& trajectory, const RelativeInfo& init_p, const double& distance, unsigned int& point_index);
  31. static double GetExactDistanceOnTrajectory(const std::vector<WayPoint>& trajectory, const RelativeInfo& p1,const RelativeInfo& p2);
  32. static int GetClosestNextPointIndex_obsolete(const std::vector<WayPoint>& trajectory, const WayPoint& p, const int& prevIndex = 0);
  33. static int GetClosestNextPointIndexFast(const std::vector<WayPoint>& trajectory, const WayPoint& p, const int& prevIndex = 0);
  34. static int GetClosestNextPointIndexFastV2(const std::vector<WayPoint>& trajectory, const WayPoint& p, const int& prevIndex = 0);
  35. static int GetClosestNextPointIndexDirectionFast(const std::vector<WayPoint>& trajectory, const WayPoint& p, const int& prevIndex = 0);
  36. static int GetClosestNextPointIndexDirectionFastV2(const std::vector<WayPoint>& trajectory, const WayPoint& p, const int& prevIndex = 0);
  37. static int GetClosestPointIndex_obsolete(const std::vector<WayPoint>& trajectory, const WayPoint& p,const int& prevIndex = 0 );
  38. static WayPoint GetPerpendicularOnTrajectory_obsolete(const std::vector<WayPoint>& trajectory, const WayPoint& p, double& distance, const int& prevIndex = 0); static double GetPerpDistanceToTrajectorySimple_obsolete(const std::vector<WayPoint>& trajectory, const WayPoint& p, const int& prevIndex = 0);
  39. static double GetPerpDistanceToVectorSimple_obsolete(const WayPoint& p1, const WayPoint& p2, const WayPoint& pose);
  40. static WayPoint GetNextPointOnTrajectory_obsolete(const std::vector<WayPoint>& trajectory, const double& distance, const int& currIndex = 0);
  41. static double GetDistanceOnTrajectory_obsolete(const std::vector<WayPoint>& path, const int& start_index, const WayPoint& p);
  42. static void CreateManualBranch(std::vector<WayPoint>& path, const int& degree, const DIRECTION_TYPE& direction);
  43. static void CreateManualBranchFromTwoPoints(WayPoint& p1,WayPoint& p2 , const double& distance, const DIRECTION_TYPE& direction, std::vector<WayPoint>& path);
  44. static void FixPathDensity(std::vector<WayPoint>& path, const double& distanceDensity);
  45. static void SmoothPath(std::vector<WayPoint>& path, double weight_data =0.25,double weight_smooth = 0.25,double tolerance = 0.01);
  46. static double CalcCircle(const GPSPoint& pt1, const GPSPoint& pt2, const GPSPoint& pt3, GPSPoint& center);
  47. static void FixAngleOnly(std::vector<WayPoint>& path);
  48. static double CalcAngleAndCost(std::vector<WayPoint>& path, const double& lastCost = 0, const bool& bSmooth = true );
  49. //static double CalcAngleAndCostSimple(std::vector<WayPoint>& path, const double& lastCost = 0);
  50. static double CalcAngleAndCostAndCurvatureAnd2D(std::vector<WayPoint>& path, const double& lastCost = 0);
  51. static void PredictConstantTimeCostForTrajectory(std::vector<PlannerHNS::WayPoint>& path, const PlannerHNS::WayPoint& currPose, const double& minVelocity, const double& minDist);
  52. static double GetAccurateDistanceOnTrajectory(std::vector<WayPoint>& path, const int& start_index, const WayPoint& p);
  53. static void ExtractPartFromPointToDistance(const std::vector<WayPoint>& originalPath, const WayPoint& pos, const double& minDistance,
  54. const double& pathDensity, std::vector<WayPoint>& extractedPath, const double& SmoothDataWeight, const double& SmoothWeight, const double& SmoothTolerance);
  55. static void ExtractPartFromPointToDistanceFast(const std::vector<WayPoint>& originalPath, const WayPoint& pos, const double& minDistance,
  56. const double& pathDensity, std::vector<WayPoint>& extractedPath, const double& SmoothDataWeight, const double& SmoothWeight, const double& SmoothTolerance);
  57. static void ExtractPartFromPointToDistanceDirectionFast(const std::vector<WayPoint>& originalPath, const WayPoint& pos, const double& minDistance,
  58. const double& pathDensity, std::vector<WayPoint>& extractedPath);
  59. static void CalculateRollInTrajectories(const WayPoint& carPos, const double& speed, const std::vector<WayPoint>& originalCenter, int& start_index,
  60. int& end_index, std::vector<double>& end_laterals ,
  61. std::vector<std::vector<WayPoint> >& rollInPaths, const double& max_roll_distance,
  62. const double& maxSpeed, const double& carTipMargin, const double& rollInMargin,
  63. const double& rollInSpeedFactor, const double& pathDensity, const double& rollOutDensity,
  64. const int& rollOutNumber, const double& SmoothDataWeight, const double& SmoothWeight,
  65. const double& SmoothTolerance, const bool& bHeadingSmooth,
  66. std::vector<WayPoint>& sampledPoints);
  67. static void SmoothSpeedProfiles(std::vector<WayPoint>& path_in, double weight_data, double weight_smooth, double tolerance = 0.1);
  68. static void SmoothCurvatureProfiles(std::vector<WayPoint>& path_in, double weight_data, double weight_smooth, double tolerance = 0.1);
  69. static void SmoothWayPointsDirections(std::vector<WayPoint>& path_in, double weight_data, double weight_smooth, double tolerance = 0.1);
  70. static void SmoothGlobalPathSpeed(std::vector<WayPoint>& path);
  71. static void GenerateRecommendedSpeed(std::vector<WayPoint>& path, const double& max_speed, const double& speedProfileFactor);
  72. // static WayPoint* BuildPlanningSearchTree(Lane* l, const WayPoint& prevWayPointIndex,
  73. // const WayPoint& startPos, const WayPoint& goalPos,
  74. // const std::vector<int>& globalPath, const double& DistanceLimit,
  75. // int& nMaxLeftBranches, int& nMaxRightBranches,
  76. // std::vector<WayPoint*>& all_cells_to_delete );
  77. static WayPoint* BuildPlanningSearchTreeV2(WayPoint* pStart,
  78. const WayPoint& goalPos,
  79. const std::vector<int>& globalPath, const double& DistanceLimit,
  80. const bool& bEnableLaneChange,
  81. std::vector<WayPoint*>& all_cells_to_delete );
  82. static WayPoint* BuildPlanningSearchTreeStraight(WayPoint* pStart,
  83. const double& DistanceLimit,
  84. std::vector<WayPoint*>& all_cells_to_delete );
  85. static int PredictiveDP(WayPoint* pStart, const double& DistanceLimit,
  86. std::vector<WayPoint*>& all_cells_to_delete, std::vector<WayPoint*>& end_waypoints);
  87. static int PredictiveIgnorIdsDP(WayPoint* pStart, const double& DistanceLimit,
  88. std::vector<WayPoint*>& all_cells_to_delete, std::vector<WayPoint*>& end_waypoints, std::vector<int>& lanes_ids);
  89. static bool CheckLaneIdExits(const std::vector<int>& lanes, const Lane* pL);
  90. static WayPoint* CheckLaneExits(const std::vector<WayPoint*>& nodes, const Lane* pL);
  91. static WayPoint* CheckNodeExits(const std::vector<WayPoint*>& nodes, const WayPoint* pL);
  92. static WayPoint* CreateLaneHeadCell(Lane* pLane, WayPoint* pLeft, WayPoint* pRight,
  93. WayPoint* pBack);
  94. static double GetLanePoints(Lane* l, const WayPoint& prevWayPointIndex,
  95. const double& minDistance , const double& prevCost, std::vector<WayPoint>& points);
  96. static WayPoint* GetMinCostCell(const std::vector<WayPoint*>& cells, const std::vector<int>& globalPathIds);
  97. static void TraversePathTreeBackwards(WayPoint* pHead, WayPoint* pStartWP, const std::vector<int>& globalPathIds,
  98. std::vector<WayPoint>& localPath, std::vector<std::vector<WayPoint> >& localPaths);
  99. static void ExtractPlanAlernatives(const std::vector<WayPoint>& singlePath, std::vector<std::vector<WayPoint> >& allPaths);
  100. static std::vector<int> GetUniqueLeftRightIds(const std::vector<WayPoint>& path);
  101. static bool FindInList(const std::vector<int>& list,const int& x);
  102. static void RemoveWithValue(std::vector<int>& list,const int& x);
  103. static ACTION_TYPE GetBranchingDirection(WayPoint& currWP, WayPoint& nextWP);
  104. static void CalcContourPointsForDetectedObjects(const WayPoint& currPose, std::vector<DetectedObject>& obj_list, const double& filterDistance = 100);
  105. static double GetVelocityAhead(const std::vector<WayPoint>& path, const RelativeInfo& info,int& prev_index, const double& reasonable_brake_distance);
  106. static bool CompareTrajectories(const std::vector<WayPoint>& path1, const std::vector<WayPoint>& path2);
  107. static double GetDistanceToClosestStopLineAndCheck(const std::vector<WayPoint>& path, const WayPoint& p, const double& giveUpDistance, int& stopLineID,int& stopSignID, int& trafficLightID, const int& prevIndex = 0);
  108. static bool GetThreePointsInfo(const WayPoint& p0, const WayPoint& p1, const WayPoint& p2, WayPoint& perp_p, double& long_d, double lat_d);
  109. static void WritePathToFile(const std::string& fileName, const std::vector<WayPoint>& path);
  110. static LIGHT_INDICATOR GetIndicatorsFromPath(const std::vector<WayPoint>& path, const WayPoint& pose, const double& seachDistance);
  111. static PlannerHNS::WayPoint GetRealCenter(const PlannerHNS::WayPoint& currState, const double& wheel_base);
  112. static void TestQuadraticSpline(const std::vector<WayPoint>& center_line, std::vector<WayPoint>& path);
  113. static double frunge ( double x );
  114. static double fprunge ( double x );
  115. static double fpprunge ( double x );
  116. };
  117. } /* namespace PlannerHNS */
  118. #endif /* PLANNINGHELPERS_H_ */