Nav2 Navigation Stack - lyrical  lyrical
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;
42  typedef std::vector<Coordinates> CoordinateVector;
43 
44  struct NodeContext
45  {
46  float cost_travel_multiplier;
47  std::vector<int> neighbors_grid_offsets;
48  };
49 
54  explicit Node2D(const uint64_t index, NodeContext * ctx);
55 
59  ~Node2D();
60 
66  bool operator==(const Node2D & rhs) const
67  {
68  return this->_index == rhs._index;
69  }
70 
75  inline void setPose(const Coordinates & pose_in)
76  {
77  pose = pose_in;
78  }
79 
83  void reset();
88  inline float getAccumulatedCost()
89  {
90  return _accumulated_cost;
91  }
92 
97  inline void setAccumulatedCost(const float & cost_in)
98  {
99  _accumulated_cost = cost_in;
100  }
101 
106  inline float getCost()
107  {
108  return _cell_cost;
109  }
110 
115  inline void setCost(const float & cost)
116  {
117  _cell_cost = cost;
118  }
119 
124  inline bool wasVisited()
125  {
126  return _was_visited;
127  }
128 
132  inline void visited()
133  {
134  _was_visited = true;
135  _is_queued = false;
136  }
137 
142  inline bool & isQueued()
143  {
144  return _is_queued;
145  }
146 
150  inline void queued()
151  {
152  _is_queued = true;
153  }
154 
159  inline uint64_t getIndex()
160  {
161  return _index;
162  }
163 
170  bool isNodeValid(const bool & traverse_unknown, GridCollisionChecker * collision_checker);
171 
177  float getTraversalCost(const NodePtr & child);
178 
186  static inline uint64_t getIndex(
187  const unsigned int & x, const unsigned int & y, const unsigned int & width)
188  {
189  return static_cast<uint64_t>(x) + static_cast<uint64_t>(y) *
190  static_cast<uint64_t>(width);
191  }
192 
200  static inline Coordinates getCoords(
201  const uint64_t & index, const unsigned int & width, const unsigned int & angles)
202  {
203  if (angles != 1) {
204  throw std::runtime_error("Node type Node2D does not have a valid angle quantization.");
205  }
206 
207  return Coordinates(index % width, index / width);
208  }
209 
215  inline Coordinates getCoords(const uint64_t & index)
216  {
217  const unsigned int & size_x = _ctx->neighbors_grid_offsets[3];
218  return Coordinates(index % size_x, index / size_x);
219  }
220 
227  float getHeuristicCost(
228  const Coordinates & node_coords,
229  const CoordinateVector & goals_coords);
230 
240  static void initMotionModel(
241  NodeContext * ctx,
242  const MotionModel & motion_model,
243  unsigned int & size_x,
244  unsigned int & size_y,
245  unsigned int & num_angle_quantization,
246  SearchInfo & search_info);
247 
255  void getNeighbors(
256  std::function<bool(const uint64_t &,
257  nav2_smac_planner::Node2D * &)> & validity_checker,
258  GridCollisionChecker * collision_checker,
259  const bool & traverse_unknown,
260  NodeVector & neighbors);
261 
267  bool backtracePath(CoordinateVector & path);
268 
269  Node2D * parent;
270  Coordinates pose;
271 
272 private:
273  float _cell_cost;
274  float _accumulated_cost;
275  uint64_t _index;
276  bool _was_visited;
277  bool _is_queued;
278  bool _in_collision{false};
279  NodeContext * _ctx = nullptr;
280 };
281 
282 } // namespace nav2_smac_planner
283 
284 #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:124
bool & isQueued()
Gets if cell is currently queued in search.
Definition: node_2d.hpp:142
bool isNodeValid(const bool &traverse_unknown, GridCollisionChecker *collision_checker)
Check if this node is valid.
Definition: node_2d.cpp:51
bool backtracePath(CoordinateVector &path)
Set the starting pose for planning, as a node index.
Definition: node_2d.cpp:153
float getTraversalCost(const NodePtr &child)
get traversal cost from this node to child node
Definition: node_2d.cpp:65
void setAccumulatedCost(const float &cost_in)
Sets the accumulated cost at this node.
Definition: node_2d.hpp:97
void queued()
Sets if cell is currently queued in search.
Definition: node_2d.hpp:150
static Coordinates getCoords(const uint64_t &index, const unsigned int &width, const unsigned int &angles)
Get index.
Definition: node_2d.hpp:200
float getAccumulatedCost()
Gets the accumulated cost at this node.
Definition: node_2d.hpp:88
bool operator==(const Node2D &rhs) const
operator== for comparisons
Definition: node_2d.hpp:66
void setPose(const Coordinates &pose_in)
setting continuous coordinate search poses (in partial-cells)
Definition: node_2d.hpp:75
uint64_t getIndex()
Gets cell index.
Definition: node_2d.hpp:159
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:112
~Node2D()
A destructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:36
static void initMotionModel(NodeContext *ctx, 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
void visited()
Sets if cell has been visited in search.
Definition: node_2d.hpp:132
float getHeuristicCost(const Coordinates &node_coords, const CoordinateVector &goals_coords)
Get cost of heuristic of node.
Definition: node_2d.cpp:83
static uint64_t getIndex(const unsigned int &x, const unsigned int &y, const unsigned int &width)
Get index.
Definition: node_2d.hpp:186
Coordinates getCoords(const uint64_t &index)
Get coordinates using index.
Definition: node_2d.hpp:215
void setCost(const float &cost)
Gets the costmap cost at this node.
Definition: node_2d.hpp:115
Node2D(const uint64_t index, NodeContext *ctx)
A constructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:24
void reset()
Reset method for new search.
Definition: node_2d.cpp:41
float getCost()
Gets the costmap cost at this node.
Definition: node_2d.hpp:106
Implementation of coordinate2d structure.
Definition: types.hpp:224
Search properties and penalties.
Definition: types.hpp:38