Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
costmap_2d.hpp
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Eitan Marder-Eppstein
36  * David V. Lu!!
37  *********************************************************************/
38 #ifndef NAV2_COSTMAP_2D__COSTMAP_2D_HPP_
39 #define NAV2_COSTMAP_2D__COSTMAP_2D_HPP_
40 
41 #include <string.h>
42 #include <stdio.h>
43 #include <limits.h>
44 #include <algorithm>
45 #include <cmath>
46 #include <string>
47 #include <vector>
48 #include <queue>
49 #include <mutex>
50 #include "geometry_msgs/msg/point.hpp"
51 #include "nav_msgs/msg/occupancy_grid.hpp"
52 
53 namespace nav2_costmap_2d
54 {
55 
56 // convenient for storing x/y point pairs
58 {
59  unsigned int x;
60  unsigned int y;
61  unsigned char cost;
62 };
63 
68 class Costmap2D
69 {
70  friend class CostmapTester; // Need this for gtest to work correctly
71 
72 public:
82  Costmap2D(
83  unsigned int cells_size_x, unsigned int cells_size_y, double resolution,
84  double origin_x, double origin_y, unsigned char default_value = 0);
85 
90  Costmap2D(const Costmap2D & map);
91 
96  explicit Costmap2D(const nav_msgs::msg::OccupancyGrid & map);
97 
103  Costmap2D & operator=(const Costmap2D & map);
104 
113  bool copyCostmapWindow(
114  const Costmap2D & map, double win_origin_x, double win_origin_y,
115  double win_size_x,
116  double win_size_y);
117 
129  bool copyWindow(
130  const Costmap2D & source,
131  unsigned int sx0, unsigned int sy0, unsigned int sxn, unsigned int syn,
132  unsigned int dx0, unsigned int dy0);
133 
137  Costmap2D();
138 
142  virtual ~Costmap2D();
143 
150  unsigned char getCost(unsigned int mx, unsigned int my) const;
151 
157  unsigned char getCost(unsigned int index) const;
158 
165  void setCost(unsigned int mx, unsigned int my, unsigned char cost);
166 
174  void mapToWorld(unsigned int mx, unsigned int my, double & wx, double & wy) const;
175 
183  void mapToWorldNoBounds(int mx, int my, double & wx, double & wy) const;
184 
193  bool worldToMap(double wx, double wy, unsigned int & mx, unsigned int & my) const;
194 
203  bool worldToMapContinuous(double wx, double wy, float & mx, float & my) const;
204 
213  void worldToMapNoBounds(double wx, double wy, int & mx, int & my) const;
214 
223  void worldToMapEnforceBounds(double wx, double wy, int & mx, int & my) const;
224 
231  inline unsigned int getIndex(unsigned int mx, unsigned int my) const
232  {
233  return my * size_x_ + mx;
234  }
235 
242  inline void indexToCells(unsigned int index, unsigned int & mx, unsigned int & my) const
243  {
244  my = index / size_x_;
245  mx = index - (my * size_x_);
246  }
247 
252  unsigned char * getCharMap() const;
253 
258  unsigned int getSizeInCellsX() const;
259 
264  unsigned int getSizeInCellsY() const;
265 
270  double getSizeInMetersX() const;
271 
276  double getSizeInMetersY() const;
277 
282  double getOriginX() const;
283 
288  double getOriginY() const;
289 
294  double getResolution() const;
295 
300  void setDefaultValue(unsigned char c)
301  {
302  default_value_ = c;
303  }
304 
309  unsigned char getDefaultValue()
310  {
311  return default_value_;
312  }
313 
321  const std::vector<geometry_msgs::msg::Point> & polygon,
322  unsigned char cost_value);
323 
331  const std::vector<geometry_msgs::msg::Point> & polygon,
332  std::vector<MapLocation> & polygon_map_region);
333 
340  const std::vector<MapLocation> & polygon_map_region,
341  unsigned char new_cost_value);
342 
348  const std::vector<MapLocation> & polygon_map_region);
349 
355  void polygonOutlineCells(
356  const std::vector<MapLocation> & polygon,
357  std::vector<MapLocation> & polygon_cells);
358 
364  void convexFillCells(
365  const std::vector<MapLocation> & polygon,
366  std::vector<MapLocation> & polygon_cells);
367 
373  virtual void updateOrigin(double new_origin_x, double new_origin_y);
374 
379  bool saveMap(std::string file_name);
380 
384  void resizeMap(
385  unsigned int size_x, unsigned int size_y, double resolution, double origin_x,
386  double origin_y);
387 
391  void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn);
392 
396  void resetMapToValue(
397  unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn, unsigned char value);
398 
404  unsigned int cellDistance(double world_dist);
405 
406  // Provide a typedef to ease future code maintenance
407  typedef std::recursive_mutex mutex_t;
408  mutex_t * getMutex()
409  {
410  return access_;
411  }
412 
413 protected:
427  template<typename data_type>
429  data_type * source_map, unsigned int sm_lower_left_x,
430  unsigned int sm_lower_left_y,
431  unsigned int sm_size_x, data_type * dest_map, unsigned int dm_lower_left_x,
432  unsigned int dm_lower_left_y, unsigned int dm_size_x, unsigned int region_size_x,
433  unsigned int region_size_y)
434  {
435  // we'll first need to compute the starting points for each map
436  data_type * sm_index = source_map + (sm_lower_left_y * sm_size_x + sm_lower_left_x);
437  data_type * dm_index = dest_map + (dm_lower_left_y * dm_size_x + dm_lower_left_x);
438 
439  // now, we'll copy the source map into the destination map
440  for (unsigned int i = 0; i < region_size_y; ++i) {
441  memcpy(dm_index, sm_index, region_size_x * sizeof(data_type));
442  sm_index += sm_size_x;
443  dm_index += dm_size_x;
444  }
445  }
446 
450  virtual void deleteMaps();
451 
455  virtual void resetMaps();
456 
462  virtual void initMaps(unsigned int size_x, unsigned int size_y);
463 
464 private:
465  mutex_t * access_;
466 
467 protected:
468  unsigned int size_x_;
469  unsigned int size_y_;
470  double resolution_;
471  double origin_x_;
472  double origin_y_;
473  unsigned char * costmap_;
474  unsigned char default_value_;
475 
476  // *INDENT-OFF* Uncrustify doesn't handle indented public/private labels
477  class MarkCell
478  {
479  public:
480  MarkCell(unsigned char * costmap, unsigned char value)
481  : costmap_(costmap), value_(value)
482  {
483  }
484  inline void operator()(unsigned int offset)
485  {
486  costmap_[offset] = value_;
487  }
488 
489  private:
490  unsigned char * costmap_;
491  unsigned char value_;
492  };
493 
495  {
496  public:
498  const Costmap2D & costmap, const unsigned char * /*char_map*/,
499  std::vector<MapLocation> & cells)
500  : costmap_(costmap), cells_(cells)
501  {
502  }
503 
504  // just push the relevant cells back onto the list
505  inline void operator()(unsigned int offset)
506  {
507  MapLocation loc;
508  costmap_.indexToCells(offset, loc.x, loc.y);
509  loc.cost = costmap_.getCost(loc.x, loc.y);
510  cells_.push_back(loc);
511  }
512 
513  private:
514  const Costmap2D & costmap_;
515  std::vector<MapLocation> & cells_;
516  };
517  // *INDENT-ON*
518 };
519 } // namespace nav2_costmap_2d
520 
521 #endif // NAV2_COSTMAP_2D__COSTMAP_2D_HPP_
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
Definition: costmap_2d.cpp:280
void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn)
Reset the costmap in bounds.
Definition: costmap_2d.cpp:131
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition: costmap_2d.hpp:231
void resetMapToValue(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn, unsigned char value)
Reset the costmap in bounds to a value.
Definition: costmap_2d.cpp:136
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
Resize the costmap.
Definition: costmap_2d.cpp:111
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
Definition: costmap_2d.cpp:265
virtual ~Costmap2D()
Destructor.
Definition: costmap_2d.cpp:248
void polygonOutlineCells(const std::vector< MapLocation > &polygon, std::vector< MapLocation > &polygon_cells)
Get the map cells that make up the outline of a polygon.
Definition: costmap_2d.cpp:460
void mapToWorldNoBounds(int mx, int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates with no bounds checking.
Definition: costmap_2d.cpp:286
virtual void deleteMaps()
Deletes the costmap, static_map, and markers data structures.
Definition: costmap_2d.cpp:94
bool saveMap(std::string file_name)
Save the costmap out to a pgm file.
Definition: costmap_2d.cpp:583
bool copyWindow(const Costmap2D &source, unsigned int sx0, unsigned int sy0, unsigned int sxn, unsigned int syn, unsigned int dx0, unsigned int dy0)
Copies the (x0,y0)..(xn,yn) window from source costmap into a current costmap.
Definition: costmap_2d.cpp:185
void worldToMapEnforceBounds(double wx, double wy, int &mx, int &my) const
Convert from world coordinates to map coordinates, constraining results to legal bounds.
Definition: costmap_2d.cpp:328
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
Definition: costmap_2d.cpp:260
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:292
void setDefaultValue(unsigned char c)
Set the default background value of the costmap.
Definition: costmap_2d.hpp:300
void copyMapRegion(data_type *source_map, unsigned int sm_lower_left_x, unsigned int sm_lower_left_y, unsigned int sm_size_x, data_type *dest_map, unsigned int dm_lower_left_x, unsigned int dm_lower_left_y, unsigned int dm_size_x, unsigned int region_size_x, unsigned int region_size_y)
Copy a region of a source map into a destination map.
Definition: costmap_2d.hpp:428
void convexFillCells(const std::vector< MapLocation > &polygon, std::vector< MapLocation > &polygon_cells)
Get the map cells that fill a convex polygon.
Definition: costmap_2d.cpp:478
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:578
virtual void updateOrigin(double new_origin_x, double new_origin_y)
Move the origin of the costmap to a new location.... keeping data when it can.
Definition: costmap_2d.cpp:350
Costmap2D()
Default constructor.
Definition: costmap_2d.cpp:242
bool setConvexPolygonCost(const std::vector< geometry_msgs::msg::Point > &polygon, unsigned char cost_value)
Sets the cost of a convex polygon to a desired value.
Definition: costmap_2d.cpp:406
double getSizeInMetersY() const
Accessor for the y size of the costmap in meters.
Definition: costmap_2d.cpp:563
void indexToCells(unsigned int index, unsigned int &mx, unsigned int &my) const
Given an index... compute the associated map coordinates.
Definition: costmap_2d.hpp:242
double getSizeInMetersX() const
Accessor for the x size of the costmap in meters.
Definition: costmap_2d.cpp:558
void setMapRegionOccupiedByPolygon(const std::vector< MapLocation > &polygon_map_region, unsigned char new_cost_value)
Sets the given map region to desired value.
Definition: costmap_2d.cpp:421
void restoreMapRegionOccupiedByPolygon(const std::vector< MapLocation > &polygon_map_region)
Restores the corresponding map region using given map region.
Definition: costmap_2d.cpp:430
void worldToMapNoBounds(double wx, double wy, int &mx, int &my) const
Convert from world coordinates to map coordinates without checking for legal bounds.
Definition: costmap_2d.cpp:322
bool worldToMapContinuous(double wx, double wy, float &mx, float &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:307
virtual void initMaps(unsigned int size_x, unsigned int size_y)
Initializes the costmap, static_map, and markers data structures.
Definition: costmap_2d.cpp:102
bool getMapRegionOccupiedByPolygon(const std::vector< geometry_msgs::msg::Point > &polygon, std::vector< MapLocation > &polygon_map_region)
Gets the map region occupied by polygon.
Definition: costmap_2d.cpp:438
bool copyCostmapWindow(const Costmap2D &map, double win_origin_x, double win_origin_y, double win_size_x, double win_size_y)
Turn this costmap into a copy of a window of a costmap passed in.
Definition: costmap_2d.cpp:146
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition: costmap_2d.cpp:548
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
Definition: costmap_2d.cpp:125
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:573
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition: costmap_2d.cpp:553
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:568
unsigned char getDefaultValue()
Get the default background value of the costmap.
Definition: costmap_2d.hpp:309
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
Set the cost of a cell in the costmap.
Definition: costmap_2d.cpp:275
unsigned int cellDistance(double world_dist)
Given distance in the world... convert it to cells.
Definition: costmap_2d.cpp:254
Costmap2D & operator=(const Costmap2D &map)
Overloaded assignment operator.
Definition: costmap_2d.cpp:208