Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
node_2d.hpp
1 // Copyright (c) 2020, Samsung Research America
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License. Reserved.
14 
15 #ifndef NAV2_SMAC_PLANNER__NODE_2D_HPP_
16 #define NAV2_SMAC_PLANNER__NODE_2D_HPP_
17 
18 #include <math.h>
19 #include <vector>
20 #include <iostream>
21 #include <memory>
22 #include <queue>
23 #include <limits>
24 #include <utility>
25 #include <functional>
26 
27 #include "nav2_smac_planner/types.hpp"
28 #include "nav2_smac_planner/constants.hpp"
29 #include "nav2_smac_planner/collision_checker.hpp"
30 #include "nav2_smac_planner/node_hybrid.hpp"
31 
32 namespace nav2_smac_planner
33 {
34 
39 class Node2D
40 {
41 public:
42  typedef Node2D * NodePtr;
43  typedef std::unique_ptr<std::vector<Node2D>> Graph;
44  typedef std::vector<NodePtr> NodeVector;
45 
50  struct Coordinates
51  {
52  Coordinates() {}
53  Coordinates(const float & x_in, const float & y_in)
54  : x(x_in), y(y_in)
55  {}
56 
57  float x, y;
58  };
59  typedef std::vector<Coordinates> CoordinateVector;
60 
65  explicit Node2D(const unsigned int index);
66 
70  ~Node2D();
71 
77  bool operator==(const Node2D & rhs)
78  {
79  return this->_index == rhs._index;
80  }
81 
85  void reset();
90  inline float & getAccumulatedCost()
91  {
92  return _accumulated_cost;
93  }
94 
99  inline void setAccumulatedCost(const float & cost_in)
100  {
101  _accumulated_cost = cost_in;
102  }
103 
108  inline float & getCost()
109  {
110  return _cell_cost;
111  }
112 
117  inline void setCost(const float & cost)
118  {
119  _cell_cost = cost;
120  }
121 
126  inline bool & wasVisited()
127  {
128  return _was_visited;
129  }
130 
134  inline void visited()
135  {
136  _was_visited = true;
137  _is_queued = false;
138  }
139 
144  inline bool & isQueued()
145  {
146  return _is_queued;
147  }
148 
152  inline void queued()
153  {
154  _is_queued = true;
155  }
156 
161  inline unsigned int & getIndex()
162  {
163  return _index;
164  }
165 
172  bool isNodeValid(const bool & traverse_unknown, GridCollisionChecker * collision_checker);
173 
179  float getTraversalCost(const NodePtr & child);
180 
188  static inline unsigned int getIndex(
189  const unsigned int & x, const unsigned int & y, const unsigned int & width)
190  {
191  return x + y * width;
192  }
193 
201  static inline Coordinates getCoords(
202  const unsigned int & index, const unsigned int & width, const unsigned int & angles)
203  {
204  if (angles != 1) {
205  throw std::runtime_error("Node type Node2D does not have a valid angle quantization.");
206  }
207 
208  return Coordinates(index % width, index / width);
209  }
210 
216  static inline Coordinates getCoords(const unsigned int & index)
217  {
218  const unsigned int & size_x = _neighbors_grid_offsets[3];
219  return Coordinates(index % size_x, index / size_x);
220  }
221 
229  static float getHeuristicCost(
230  const Coordinates & node_coords,
231  const Coordinates & goal_coordinates,
232  const nav2_costmap_2d::Costmap2D * costmap);
233 
243  static void initMotionModel(
244  const MotionModel & motion_model,
245  unsigned int & size_x,
246  unsigned int & size_y,
247  unsigned int & num_angle_quantization,
248  SearchInfo & search_info);
249 
257  void getNeighbors(
258  std::function<bool(const unsigned int &, nav2_smac_planner::Node2D * &)> & validity_checker,
259  GridCollisionChecker * collision_checker,
260  const bool & traverse_unknown,
261  NodeVector & neighbors);
262 
268  bool backtracePath(CoordinateVector & path);
269 
270  Node2D * parent;
271  static float cost_travel_multiplier;
272  static std::vector<int> _neighbors_grid_offsets;
273 
274 private:
275  float _cell_cost;
276  float _accumulated_cost;
277  unsigned int _index;
278  bool _was_visited;
279  bool _is_queued;
280 };
281 
282 } // namespace nav2_smac_planner
283 
284 #endif // NAV2_SMAC_PLANNER__NODE_2D_HPP_
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:68
A costmap grid collision checker.
Node2D implementation for graph.
Definition: node_2d.hpp:40
bool & isQueued()
Gets if cell is currently queued in search.
Definition: node_2d.hpp:144
bool isNodeValid(const bool &traverse_unknown, GridCollisionChecker *collision_checker)
Check if this node is valid.
Definition: node_2d.cpp:52
bool backtracePath(CoordinateVector &path)
Set the starting pose for planning, as a node index.
Definition: node_2d.cpp:151
float getTraversalCost(const NodePtr &child)
get traversal cost from this node to child node
Definition: node_2d.cpp:64
void setAccumulatedCost(const float &cost_in)
Sets the accumulated cost at this node.
Definition: node_2d.hpp:99
float & getAccumulatedCost()
Gets the accumulated cost at this node.
Definition: node_2d.hpp:90
void queued()
Sets if cell is currently queued in search.
Definition: node_2d.hpp:152
static Coordinates getCoords(const unsigned int &index, const unsigned int &width, const unsigned int &angles)
Get index.
Definition: node_2d.hpp:201
void getNeighbors(std::function< bool(const unsigned int &, nav2_smac_planner::Node2D *&)> &validity_checker, GridCollisionChecker *collision_checker, const bool &traverse_unknown, NodeVector &neighbors)
Retrieve all valid neighbors of a node.
Definition: node_2d.cpp:111
unsigned int & getIndex()
Gets cell index.
Definition: node_2d.hpp:161
~Node2D()
A destructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:38
void visited()
Sets if cell has been visited in search.
Definition: node_2d.hpp:134
static Coordinates getCoords(const unsigned int &index)
Get index.
Definition: node_2d.hpp:216
bool & wasVisited()
Gets if cell has been visited in search.
Definition: node_2d.hpp:126
float & getCost()
Gets the costmap cost at this node.
Definition: node_2d.hpp:108
static float getHeuristicCost(const Coordinates &node_coords, const Coordinates &goal_coordinates, const nav2_costmap_2d::Costmap2D *costmap)
Get cost of heuristic of node.
Definition: node_2d.cpp:82
static unsigned int getIndex(const unsigned int &x, const unsigned int &y, const unsigned int &width)
Get index.
Definition: node_2d.hpp:188
void setCost(const float &cost)
Gets the costmap cost at this node.
Definition: node_2d.hpp:117
void reset()
Reset method for new search.
Definition: node_2d.cpp:43
Node2D(const unsigned int index)
A constructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:28
static void initMotionModel(const MotionModel &motion_model, unsigned int &size_x, unsigned int &size_y, unsigned int &num_angle_quantization, SearchInfo &search_info)
Initialize the neighborhood to be used in A* We support 4-connect (VON_NEUMANN) and 8-connect (MOORE)
Definition: node_2d.cpp:94
bool operator==(const Node2D &rhs)
operator== for comparisons
Definition: node_2d.hpp:77
Node2D implementation of coordinate structure.
Definition: node_2d.hpp:51
Search properties and penalties.
Definition: types.hpp:36