Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
costmap_queue.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef COSTMAP_QUEUE__COSTMAP_QUEUE_HPP_
36 #define COSTMAP_QUEUE__COSTMAP_QUEUE_HPP_
37 
38 #include <cmath>
39 #include <vector>
40 #include <limits>
41 #include <memory>
42 #include "nav2_costmap_2d/costmap_2d.hpp"
43 #include "costmap_queue/map_based_queue.hpp"
44 
45 namespace costmap_queue
46 {
51 class CellData
52 {
53 public:
64  const double d, const unsigned int i, const unsigned int x, const unsigned int y,
65  const unsigned int sx, const unsigned int sy)
66  : distance_(d), index_(i), x_(x), y_(y), src_x_(sx), src_y_(sy)
67  {
68  }
69 
74  : distance_(std::numeric_limits<double>::max()), index_(0), x_(0), y_(0), src_x_(0), src_y_(0)
75  {
76  }
77 
78  static unsigned absolute_difference(const unsigned x, const unsigned y)
79  {
80  return (x > y) ? (x - y) : (y - x);
81  }
82 
83  double distance_;
84  unsigned int index_;
85  unsigned int x_, y_;
86  unsigned int src_x_, src_y_;
87 };
88 
108 class CostmapQueue : public MapBasedQueue<CellData>
109 {
110 public:
116  explicit CostmapQueue(nav2_costmap_2d::Costmap2D & costmap, bool manhattan = false);
117 
121  void reset() override;
122 
128  void enqueueCell(unsigned int x, unsigned int y);
129 
137 
143  virtual bool validCellToQueue(const CellData & /*cell*/) {return true;}
147  typedef std::shared_ptr<CostmapQueue> Ptr;
148 
149 protected:
153  void enqueueCell(
154  unsigned int index, unsigned int cur_x, unsigned int cur_y, unsigned int src_x,
155  unsigned int src_y);
156 
160  void computeCache();
161 
162  nav2_costmap_2d::Costmap2D & costmap_;
163  std::vector<bool> seen_;
164  int max_distance_;
165  bool manhattan_;
166 
167 protected:
176  inline double distanceLookup(
177  const unsigned int cur_x, const unsigned int cur_y,
178  const unsigned int src_x, const unsigned int src_y)
179  {
180  unsigned int dx = CellData::absolute_difference(cur_x, src_x);
181  unsigned int dy = CellData::absolute_difference(cur_y, src_y);
182  return cached_distances_[dx][dy];
183  }
184  std::vector<std::vector<double>> cached_distances_;
185  int cached_max_distance_;
186 };
187 } // namespace costmap_queue
188 
189 #endif // COSTMAP_QUEUE__COSTMAP_QUEUE_HPP_
Storage for cell information used during queue expansion.
CellData(const double d, const unsigned int i, const unsigned int x, const unsigned int y, const unsigned int sx, const unsigned int sy)
Real Constructor.
CellData()
Default Constructor - Should be used sparingly.
double distanceLookup(const unsigned int cur_x, const unsigned int cur_y, const unsigned int src_x, const unsigned int src_y)
Lookup pre-computed distances.
std::shared_ptr< CostmapQueue > Ptr
convenience typedef for a pointer
void enqueueCell(unsigned int x, unsigned int y)
Add a cell the queue.
CellData getNextCell()
Get the next cell to examine, and enqueue its neighbors as needed.
void reset() override
Clear the queue.
void computeCache()
Compute the cached distances.
CostmapQueue(nav2_costmap_2d::Costmap2D &costmap, bool manhattan=false)
constructor
virtual bool validCellToQueue(const CellData &)
Check to see if we should add this cell to the queue. Always true unless overridden.
Templatized interface for a priority queue.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:68