Nav2 Navigation Stack - jazzy  jazzy
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 uint64_t 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 uint64_t 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 uint64_t getIndex(
189  const unsigned int & x, const unsigned int & y, const unsigned int & width)
190  {
191  return static_cast<uint64_t>(x) + static_cast<uint64_t>(y) *
192  static_cast<uint64_t>(width);
193  }
194 
202  static inline Coordinates getCoords(
203  const uint64_t & index, const unsigned int & width, const unsigned int & angles)
204  {
205  if (angles != 1) {
206  throw std::runtime_error("Node type Node2D does not have a valid angle quantization.");
207  }
208 
209  return Coordinates(index % width, index / width);
210  }
211 
217  static inline Coordinates getCoords(const uint64_t & index)
218  {
219  const unsigned int & size_x = _neighbors_grid_offsets[3];
220  return Coordinates(index % size_x, index / size_x);
221  }
222 
229  static float getHeuristicCost(
230  const Coordinates & node_coords,
231  const Coordinates & goal_coordinates);
232 
242  static void initMotionModel(
243  const MotionModel & motion_model,
244  unsigned int & size_x,
245  unsigned int & size_y,
246  unsigned int & num_angle_quantization,
247  SearchInfo & search_info);
248 
256  void getNeighbors(
257  std::function<bool(const uint64_t &,
258  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  uint64_t _index;
278  bool _was_visited;
279  bool _is_queued;
280  bool _in_collision{false};
281 };
282 
283 } // namespace nav2_smac_planner
284 
285 #endif // NAV2_SMAC_PLANNER__NODE_2D_HPP_
A costmap grid collision checker.
Node2D implementation for graph.
Definition: node_2d.hpp:40
bool wasVisited()
Gets if cell has been visited in search.
Definition: node_2d.hpp:126
bool & isQueued()
Gets if cell is currently queued in search.
Definition: node_2d.hpp:144
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:99
void queued()
Sets if cell is currently queued in search.
Definition: node_2d.hpp:152
static Coordinates getCoords(const uint64_t &index, const unsigned int &width, const unsigned int &angles)
Get index.
Definition: node_2d.hpp:202
static Coordinates getCoords(const uint64_t &index)
Get index.
Definition: node_2d.hpp:217
float getAccumulatedCost()
Gets the accumulated cost at this node.
Definition: node_2d.hpp:90
static float getHeuristicCost(const Coordinates &node_coords, const Coordinates &goal_coordinates)
Get cost of heuristic of node.
Definition: node_2d.cpp:86
uint64_t getIndex()
Gets cell index.
Definition: node_2d.hpp:161
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:134
static uint64_t 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:44
float getCost()
Gets the costmap cost at this node.
Definition: node_2d.hpp:108
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:77
Node2D implementation of coordinate structure.
Definition: node_2d.hpp:51
Search properties and penalties.
Definition: types.hpp:36