Nav2 Navigation Stack - kilted  kilted
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 <functional>
19 #include <memory>
20 #include <stdexcept>
21 #include <vector>
22 
23 #include "nav2_smac_planner/types.hpp"
24 #include "nav2_smac_planner/constants.hpp"
25 #include "nav2_smac_planner/collision_checker.hpp"
26 #include "nav2_smac_planner/node_hybrid.hpp"
27 
28 namespace nav2_smac_planner
29 {
30 
35 class Node2D
36 {
37 public:
38  typedef Node2D * NodePtr;
39  typedef std::unique_ptr<std::vector<Node2D>> Graph;
40  typedef std::vector<NodePtr> NodeVector;
41 
46  struct Coordinates
47  {
48  Coordinates() {}
49  Coordinates(const float & x_in, const float & y_in)
50  : x(x_in), y(y_in)
51  {}
52 
53  inline bool operator==(const Coordinates & rhs) const
54  {
55  return this->x == rhs.x && this->y == rhs.y;
56  }
57 
58  inline bool operator!=(const Coordinates & rhs) const
59  {
60  return !(*this == rhs);
61  }
62 
63  float x, y;
64  };
65  typedef std::vector<Coordinates> CoordinateVector;
66 
71  explicit Node2D(const uint64_t index);
72 
76  ~Node2D();
77 
83  bool operator==(const Node2D & rhs)
84  {
85  return this->_index == rhs._index;
86  }
87 
92  inline void setPose(const Coordinates & pose_in)
93  {
94  pose = pose_in;
95  }
96 
100  void reset();
105  inline float getAccumulatedCost()
106  {
107  return _accumulated_cost;
108  }
109 
114  inline void setAccumulatedCost(const float & cost_in)
115  {
116  _accumulated_cost = cost_in;
117  }
118 
123  inline float getCost()
124  {
125  return _cell_cost;
126  }
127 
132  inline void setCost(const float & cost)
133  {
134  _cell_cost = cost;
135  }
136 
141  inline bool wasVisited()
142  {
143  return _was_visited;
144  }
145 
149  inline void visited()
150  {
151  _was_visited = true;
152  _is_queued = false;
153  }
154 
159  inline bool & isQueued()
160  {
161  return _is_queued;
162  }
163 
167  inline void queued()
168  {
169  _is_queued = true;
170  }
171 
176  inline uint64_t getIndex()
177  {
178  return _index;
179  }
180 
187  bool isNodeValid(const bool & traverse_unknown, GridCollisionChecker * collision_checker);
188 
194  float getTraversalCost(const NodePtr & child);
195 
203  static inline uint64_t getIndex(
204  const unsigned int & x, const unsigned int & y, const unsigned int & width)
205  {
206  return static_cast<uint64_t>(x) + static_cast<uint64_t>(y) *
207  static_cast<uint64_t>(width);
208  }
209 
217  static inline Coordinates getCoords(
218  const uint64_t & index, const unsigned int & width, const unsigned int & angles)
219  {
220  if (angles != 1) {
221  throw std::runtime_error("Node type Node2D does not have a valid angle quantization.");
222  }
223 
224  return Coordinates(index % width, index / width);
225  }
226 
232  static inline Coordinates getCoords(const uint64_t & index)
233  {
234  const unsigned int & size_x = _neighbors_grid_offsets[3];
235  return Coordinates(index % size_x, index / size_x);
236  }
237 
244  static float getHeuristicCost(
245  const Coordinates & node_coords,
246  const CoordinateVector & goals_coords);
247 
257  static void initMotionModel(
258  const MotionModel & motion_model,
259  unsigned int & size_x,
260  unsigned int & size_y,
261  unsigned int & num_angle_quantization,
262  SearchInfo & search_info);
263 
271  void getNeighbors(
272  std::function<bool(const uint64_t &,
273  nav2_smac_planner::Node2D * &)> & validity_checker,
274  GridCollisionChecker * collision_checker,
275  const bool & traverse_unknown,
276  NodeVector & neighbors);
277 
283  bool backtracePath(CoordinateVector & path);
284 
285  Node2D * parent;
286  Coordinates pose;
287  static float cost_travel_multiplier;
288  static std::vector<int> _neighbors_grid_offsets;
289 
290 private:
291  float _cell_cost;
292  float _accumulated_cost;
293  uint64_t _index;
294  bool _was_visited;
295  bool _is_queued;
296  bool _in_collision{false};
297 };
298 
299 } // namespace nav2_smac_planner
300 
301 #endif // NAV2_SMAC_PLANNER__NODE_2D_HPP_
A costmap grid collision checker.
Node2D implementation for graph.
Definition: node_2d.hpp:36
bool wasVisited()
Gets if cell has been visited in search.
Definition: node_2d.hpp:141
bool & isQueued()
Gets if cell is currently queued in search.
Definition: node_2d.hpp:159
Node2D(const uint64_t index)
A constructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:28
bool isNodeValid(const bool &traverse_unknown, GridCollisionChecker *collision_checker)
Check if this node is valid.
Definition: node_2d.cpp:54
bool backtracePath(CoordinateVector &path)
Set the starting pose for planning, as a node index.
Definition: node_2d.cpp:155
float getTraversalCost(const NodePtr &child)
get traversal cost from this node to child node
Definition: node_2d.cpp:68
void setAccumulatedCost(const float &cost_in)
Sets the accumulated cost at this node.
Definition: node_2d.hpp:114
void queued()
Sets if cell is currently queued in search.
Definition: node_2d.hpp:167
static Coordinates getCoords(const uint64_t &index, const unsigned int &width, const unsigned int &angles)
Get index.
Definition: node_2d.hpp:217
static Coordinates getCoords(const uint64_t &index)
Get index.
Definition: node_2d.hpp:232
float getAccumulatedCost()
Gets the accumulated cost at this node.
Definition: node_2d.hpp:105
void setPose(const Coordinates &pose_in)
setting continuous coordinate search poses (in partial-cells)
Definition: node_2d.hpp:92
uint64_t getIndex()
Gets cell index.
Definition: node_2d.hpp:176
void getNeighbors(std::function< bool(const uint64_t &, 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:114
~Node2D()
A destructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:39
void visited()
Sets if cell has been visited in search.
Definition: node_2d.hpp:149
static float getHeuristicCost(const Coordinates &node_coords, const CoordinateVector &goals_coords)
Get cost of heuristic of node.
Definition: node_2d.cpp:86
static uint64_t getIndex(const unsigned int &x, const unsigned int &y, const unsigned int &width)
Get index.
Definition: node_2d.hpp:203
void setCost(const float &cost)
Gets the costmap cost at this node.
Definition: node_2d.hpp:132
void reset()
Reset method for new search.
Definition: node_2d.cpp:44
float getCost()
Gets the costmap cost at this node.
Definition: node_2d.hpp:123
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:97
bool operator==(const Node2D &rhs)
operator== for comparisons
Definition: node_2d.hpp:83
Node2D implementation of coordinate structure.
Definition: node_2d.hpp:47
Search properties and penalties.
Definition: types.hpp:36