12 currPath = std::list<Point>();
13 close = std::unordered_map<int, Node>();
14 open = std::vector<std::list<Node>>();
16 glPathCreated =
false;
23 currPath = obj.currPath;
26 openSize = obj.openSize;
27 glPathCreated = obj.glPathCreated;
28 visChecker = obj.visChecker;
40 if(currPath.size() == 0)
46 if(currPath.size() > 1)
48 float sqDistToCurr = (currPath.front() - curr).SquaredEuclideanNorm();
49 float sqDelta = options->delta * options->delta;
50 if(sqDistToCurr < sqDelta)
53 next = currPath.front();
58 Node currNode = map->GetClosestNode(curr), nextNode = map->GetClosestNode(currPath.front());
59 if(currNode == nextNode || visChecker.checkLine(currNode.
i, currNode.
j, nextNode.i, nextNode.j, *map))
61 next = currPath.front();
65 Point last = currPath.front();
68 bool isLastAccessible = SearchPath(map->GetClosestNode(curr), map->GetClosestNode(last));
71 next = currPath.front();
80 bool ThetaStar::SearchPath(
const Node &start,
const Node &goal)
93 open.resize(map->GetHeight());
100 curNode.
H = ComputeHFromCellToCell(curNode.
i, curNode.
j, goal.
i, goal.
j);
101 curNode.
F = options->hweight * curNode.
H;
105 bool pathfound =
false;
106 while (!StopCriterion())
109 close.insert({curNode.
i * map->GetWidth() + curNode.
j, curNode});
111 open[curNode.
i].pop_front();
118 std::list<Node> successors = FindSuccessors(curNode);
119 std::list<Node>::iterator it = successors.begin();
120 auto parent = &(close.find(curNode.
i * map->GetWidth() + curNode.
j)->second);
121 while (it != successors.end()) {
123 it->H = ComputeHFromCellToCell(it->i, it->j, goal.
i, goal.
j);
124 *it = ResetParent(*it, *it->parent);
125 it->F = it->g + options->hweight * it->H;
133 MakePrimaryPath(curNode);
139 bool ThetaStar::StopCriterion()
const 149 void ThetaStar::AddOpen(
Node newNode)
151 std::list<Node>::iterator iter, pos;
153 if (open[newNode.
i].size() == 0) {
154 open[newNode.
i].push_back(newNode);
159 pos = open[newNode.
i].end();
160 bool posFound =
false;
161 for (iter = open[newNode.
i].begin(); iter != open[newNode.
i].end(); ++iter)
163 if (!posFound && iter->F >= newNode.
F)
165 if(iter->F == newNode.
F)
167 if((options->breakingties ==
CN_SP_BT_GMAX && newNode.
g >= iter->g)
168 || (options->breakingties ==
CN_SP_BT_GMIN && newNode.
g <= iter->g))
180 if (iter->j == newNode.
j)
182 if (newNode.
F >= iter->F)
190 iter->parent = newNode.
parent;
193 open[newNode.
i].erase(iter);
200 open[newNode.
i].insert(pos, newNode);
204 Node ThetaStar::FindMin()
207 min.
F = std::numeric_limits<double>::infinity();
208 for (
int i = 0; i < open.size(); i++)
210 if(!open[i].empty() && open[i].begin()->F <= min.
F)
212 if(open[i].begin()->F == min.
F)
214 if((options->breakingties ==
CN_SP_BT_GMAX && open[i].begin()->g >= min.
g)
215 || (options->breakingties ==
CN_SP_BT_GMIN && open[i].begin()->g <= min.
g))
217 min = *open[i].begin();
222 min = *open[i].begin();
230 float ThetaStar::ComputeHFromCellToCell(
int i1,
int j1,
int i2,
int j2)
const 232 switch (options->metrictype)
235 return static_cast<float>(sqrt((i2 - i1)*(i2 - i1)+(j2 - j1)*(j2 - j1)));
237 return static_cast<float>(abs(abs(i2 - i1) - abs(j2 - j1)) + sqrt(2) * (std::min(abs(i2 - i1),abs(j2 - j1))));
239 return (abs(i2 - i1) + abs(j2 - j1));
241 return std::max(abs(i2 - i1),abs(j2 - j1));
251 if (parent.
parent ==
nullptr)
253 if(current == *parent.
parent)
256 if (visChecker.checkLine(parent.
parent->
i, parent.
parent->
j, current.
i, current.
j, *map))
266 float ThetaStar::Distance(
int i1,
int j1,
int i2,
int j2)
const 268 return static_cast<float>(sqrt(pow(i1 - i2, 2.0f) + pow(j1 - j2, 2.0f)));
272 void ThetaStar::MakePrimaryPath(
Node curNode)
276 Node current = curNode;
279 currPath.push_front(map->GetPoint(current));
280 current = *current.
parent;
286 std::list<Node> ThetaStar::FindSuccessors(
Node curNode)
289 std::list<Node> successors;
290 for (
int i = -1; i <= +1; i++)
291 for (
int j = -1; j <= +1; j++)
292 if ((i != 0 || j != 0) && map->CellOnGrid(curNode.
i + i, curNode.
j + j) &&
293 (visChecker.checkTraversability(curNode.
i + i, curNode.
j + j, *map)))
295 if (i != 0 && j != 0)
297 if (!options->cutcorners)
299 if (map->CellIsObstacle(curNode.
i, curNode.
j + j) ||
300 map->CellIsObstacle(curNode.
i + i, curNode.
j))
303 else if (!options->allowsqueeze)
305 if (map->CellIsObstacle(curNode.
i, curNode.
j + j) &&
306 map->CellIsObstacle(curNode.
i + i, curNode.
j))
310 if (close.find((curNode.
i + i) * map->GetWidth() + curNode.
j + j) == close.end())
312 newNode.
i = curNode.
i + i;
313 newNode.
j = curNode.
j + j;
315 newNode.
g = curNode.
g + 1;
317 newNode.
g = curNode.
g + sqrt(2);
318 successors.push_front(newNode);
329 Node start = map->GetClosestNode(glStart);
330 Node goal = map->GetClosestNode(glGoal);
332 glPathCreated = SearchPath(start, goal);
335 return glPathCreated;
343 currPath = obj.currPath;
346 openSize = obj.openSize;
347 glPathCreated = obj.glPathCreated;
348 visChecker = obj.visChecker;
bool CreateGlobalPath() override
Finds path from global start to global goal position using Theta* algorithm.
PathPlanner class implements base pathfinder interface and behavior.
ThetaStar(const Map &map, const EnvironmentOptions &options, const Point &start, const Point &goal, const float &radius)
ThetaStar constructor with parametes.
The Node class defines a cell of grid (see Map class)
double g
g-value of node for A* based algorithms.
float GetCellSize() const
Return size of cell side.
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
#define CN_SP_BT_GMIN
Gmin constant.
#define CN_SP_MT_EUCL
Euclid distance constant.
~ThetaStar() override
ThetaStar destructor.
File contains ThetaStar class.
#define CN_SP_MT_MANH
Manhattan distance constant.
ThetaStar & operator=(const ThetaStar &obj)
Assignment operator.
#define CN_SP_MT_CHEB
Chebyshev distance constant.
bool GetNext(const Point &curr, Point &next) override
Returns current goal of agent from global path.
Class EnvironmentOptions contains environment and algoritms parameters.
double F
F = g + h. Value for A* based algorithms.
Map class describes static environment.
#define CN_SP_MT_DIAG
Diagonal distance constant.
ThetaStar * Clone() const override
Method for cloning inheritors objects. Creates copy of object in memmory and return pointer to copy...
int i
The number of row. Part of (i,j) address of cell on grid.
ThetaStar class implements Theta* algorithm, which creates path from start to goal position...
int j
The number of column. Part of (i,j) address of cell on grid.
PathPlanner & operator=(const PathPlanner &obj)
Assignment operator.
#define CN_SP_BT_GMAX
Gmax constant.
This class implements line-of-sight function between two cells on the grid for a variable size of age...
Node * parent
The node from which the transition to this node was made. Value for A* based algorithms.
double H
h-value of node for A* based algorithms.