turning_base.cpp
Go to the documentation of this file.
1 //=============================================================================
2 // Copyright (C) 2021-2024 Wageningen University - All Rights Reserved
3 // Author: Gonzalo Mier
4 // BSD-3 License
5 //=============================================================================
6 
8 
9 namespace f2c::pp {
10 
12  const F2CPoint& start_pos, double start_angle,
13  const F2CPoint& end_pos, double end_angle) {
14  double dist_start_end = start_pos.distance(end_pos);
15  auto dir = end_pos - start_pos;
16  auto angle = F2CPoint::mod_2pi(dir.getAngleFromPoint());
17  bool inverted {false};
18  start_angle = F2CPoint::mod_2pi(start_angle - angle);
19  end_angle = F2CPoint::mod_2pi(end_angle - angle);
20  if (start_angle > boost::math::constants::pi<double>()) {
21  start_angle = F2CPoint::mod_2pi(-start_angle);
22  end_angle = F2CPoint::mod_2pi(-end_angle);
23  inverted = true;
24  }
25  return {dist_start_end, angle, start_angle, end_angle,
26  static_cast<double>(inverted)};
27 }
28 
29 
31  const F2CPoint& start_pos, double start_angle,
32  const F2CPoint& end_pos, double end_angle) {
33  auto turn_values =
34  transformToNormalTurn(start_pos, start_angle, end_pos, end_angle);
35  double dist_start_end = turn_values[0];
36  double rot_angle = turn_values[1];
37  double start_angle_t = turn_values[2];
38  double end_angle_t = turn_values[3];
39  double inverted = turn_values[4];
40 
41  F2CPath path;
42  if (using_cache) {
44  dist_start_end, start_angle_t, end_angle_t);
45  } else {
47  dist_start_end, start_angle_t, end_angle_t);
48  }
49  if (path.size() <= 1) {return F2CPath();}
50 
51  if (inverted) {
52  std::for_each(path.begin(), path.end(), [] (auto& s) {
53  s.point.setY(-s.point.getY());
54  s.angle = F2CPoint::mod_2pi(-s.angle);});
55  }
56  for (auto&& s : path) {
57  s.point = F2CPoint(.0, .0).rotateFromPoint(rot_angle, s.point) + start_pos;
58  s.angle = F2CPoint::mod_2pi(s.angle + rot_angle);
59  }
60 
61  correctPath(path, start_pos, end_pos);
62  return path;
63 }
64 
66  const F2CPoint& end_pos, float max_error_dist) {
67  if (path.size() < 2) {return;}
68 
69  auto is_near = [max_error_dist](const F2CPoint& a, const F2CPoint& b) {
70  return (a.distance(b) < max_error_dist);
71  };
72  if (is_near(path[0].point, start_pos)) {
73  path[0].point = start_pos;
74  }
75  if (is_near(path.back().point, end_pos)) {
76  path.back().point = end_pos;
77  }
78 }
79 
80 
82  double dist_start_end, double start_angle, double end_angle) {
83  std::vector<int> v_turn {
84  static_cast<int>(1e3 * robot.getMaxCurv()),
85  static_cast<int>(1e3 * robot.getMaxDiffCurv()),
86  static_cast<int>(1e3 * dist_start_end),
87  static_cast<int>(1e3 * start_angle),
88  static_cast<int>(1e3 * end_angle)
89  };
90  auto it = path_cache_.find(v_turn);
91  if (it != path_cache_.end()) {
92  return it->second;
93  }
94  auto path = createSimpleTurn(robot, dist_start_end, start_angle, end_angle);
95  path_cache_.insert({v_turn, path});
96  return path;
97 }
98 
99 
101  double dist_start_end, double end_angle,
102  double max_dist, double max_rot_error) {
103  for (auto&& s : path) {
104  if (s.point.getY() < -max_dist) {
105  return false;
106  }
107  }
108  F2CPoint p_end = path.atEnd();
109  return (cos(path.back().angle - end_angle) >= 1 - max_rot_error) &&
110  (fabs(p_end.getX() - dist_start_end) < max_dist) &&
111  (fabs(p_end.getY()) < max_dist);
112 }
113 
115  return this->discretization;
116 }
117 
119  this->discretization = d;
120 }
121 
123  return this->using_cache;
124 }
125 
127  this->using_cache = c;
128 }
129 
130 
131 } // namespace f2c::pp
132 
f2c::pp
Path planning algorithms' namespace.
Definition: dubins_curves.h:14
2_objective_functions.path
path
Definition: 2_objective_functions.py:88
f2c::pp::TurningBase::getUsingCache
bool getUsingCache() const
Get if turns are being cached or not.
Definition: turning_base.cpp:122
f2c::pp::TurningBase::createSimpleTurn
virtual F2CPath createSimpleTurn(const F2CRobot &robot, double dist_start_pos, double start_angle, double end_angle)=0
Create a turn.
f2c::pp::TurningBase::using_cache
bool using_cache
Definition: turning_base.h:93
2_objective_functions.robot
robot
Definition: 2_objective_functions.py:76
f2c::types::Point::rotateFromPoint
Point rotateFromPoint(double angle, const Point &p_r) const
Definition: Point.cpp:121
f2c::types::Path
Definition: Path.h:23
f2c::pp::TurningBase::path_cache_
std::map< std::vector< int >, F2CPath > path_cache_
Definition: turning_base.h:91
f2c::pp::TurningBase::discretization
double discretization
Definition: turning_base.h:92
f2c::pp::TurningBase::transformToNormalTurn
static std::vector< double > transformToNormalTurn(const F2CPoint &start_pos, double start_angle, const F2CPoint &end_pos, double end_angle)
Transform the turn parameters representation from two points with two angles to one distance and two ...
Definition: turning_base.cpp:11
f2c::types::Geometry< OGRPoint, wkbPoint >::mod_2pi
static double mod_2pi(double val)
Definition: Geometry_impl.hpp:174
f2c::types::Point
Definition: Point.h:21
f2c::types::Robot
Definition: Robot.h:25
f2c::types::Geometry::distance
double distance(const Geometry< T2, R2 > &p) const
Compute shortest distance between this and another geometry.
Definition: Geometry_impl.hpp:139
f2c::pp::TurningBase::setUsingCache
void setUsingCache(bool c)
Set if cache should be used when planning same turn as before.
Definition: turning_base.cpp:126
f2c::pp::TurningBase::createTurnIfNotCached
F2CPath createTurnIfNotCached(const F2CRobot &robot, double dist_start_pos, double start_angle, double end_angle)
Generate a turn if it has not been computed before.
Definition: turning_base.cpp:81
F2CPath
f2c::types::Path F2CPath
Definition: types.h:47
f2c::pp::TurningBase::isTurnValid
static bool isTurnValid(const F2CPath &path, double dist_start_end, double end_angle, double max_dist_error=0.05, double max_rot_error=0.1)
Check if turn is valid.
Definition: turning_base.cpp:100
f2c::pp::TurningBase::createTurn
F2CPath createTurn(const F2CRobot &robot, const F2CPoint &start_pos, double start_angle, const F2CPoint &end_pos, double end_angle)
Create a turn that goes from one point with a certain angle to another point.
Definition: turning_base.cpp:30
turning_base.h
f2c::types::Point::getY
double getY() const
Definition: Point.cpp:96
f2c::types::Point::getX
double getX() const
Definition: Point.cpp:95
f2c::pp::TurningBase::getDiscretization
double getDiscretization() const
Get discretization distance from points in the turn.
Definition: turning_base.cpp:114
F2CPoint
f2c::types::Point F2CPoint
Definition: types.h:38
f2c::pp::TurningBase::correctPath
static void correctPath(F2CPath &path, const F2CPoint &start_pos, const F2CPoint &end_pos, float max_error_dist=0.05)
Definition: turning_base.cpp:65
f2c::pp::TurningBase::setDiscretization
void setDiscretization(double d)
Set discretization distance from points in the turn.
Definition: turning_base.cpp:118


fields2cover
Author(s):
autogenerated on Fri Apr 25 2025 02:18:31