Nav2 Navigation Stack - humble  humble
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 };
62 
67 class Costmap2D
68 {
69  friend class CostmapTester; // Need this for gtest to work correctly
70 
71 public:
81  Costmap2D(
82  unsigned int cells_size_x, unsigned int cells_size_y, double resolution,
83  double origin_x, double origin_y, unsigned char default_value = 0);
84 
89  Costmap2D(const Costmap2D & map);
90 
95  explicit Costmap2D(const nav_msgs::msg::OccupancyGrid & map);
96 
102  Costmap2D & operator=(const Costmap2D & map);
103 
112  bool copyCostmapWindow(
113  const Costmap2D & map, double win_origin_x, double win_origin_y,
114  double win_size_x,
115  double win_size_y);
116 
128  bool copyWindow(
129  const Costmap2D & source,
130  unsigned int sx0, unsigned int sy0, unsigned int sxn, unsigned int syn,
131  unsigned int dx0, unsigned int dy0);
132 
136  Costmap2D();
137 
141  virtual ~Costmap2D();
142 
149  unsigned char getCost(unsigned int mx, unsigned int my) const;
150 
156  unsigned char getCost(unsigned int index) const;
157 
164  void setCost(unsigned int mx, unsigned int my, unsigned char cost);
165 
173  void mapToWorld(unsigned int mx, unsigned int my, double & wx, double & wy) const;
174 
183  bool worldToMap(double wx, double wy, unsigned int & mx, unsigned int & my) const;
184 
193  void worldToMapNoBounds(double wx, double wy, int & mx, int & my) const;
194 
203  void worldToMapEnforceBounds(double wx, double wy, int & mx, int & my) const;
204 
211  inline unsigned int getIndex(unsigned int mx, unsigned int my) const
212  {
213  return my * size_x_ + mx;
214  }
215 
222  inline void indexToCells(unsigned int index, unsigned int & mx, unsigned int & my) const
223  {
224  my = index / size_x_;
225  mx = index - (my * size_x_);
226  }
227 
232  unsigned char * getCharMap() const;
233 
238  unsigned int getSizeInCellsX() const;
239 
244  unsigned int getSizeInCellsY() const;
245 
250  double getSizeInMetersX() const;
251 
256  double getSizeInMetersY() const;
257 
262  double getOriginX() const;
263 
268  double getOriginY() const;
269 
274  double getResolution() const;
275 
280  void setDefaultValue(unsigned char c)
281  {
282  default_value_ = c;
283  }
284 
289  unsigned char getDefaultValue()
290  {
291  return default_value_;
292  }
293 
301  const std::vector<geometry_msgs::msg::Point> & polygon,
302  unsigned char cost_value);
303 
309  void polygonOutlineCells(
310  const std::vector<MapLocation> & polygon,
311  std::vector<MapLocation> & polygon_cells);
312 
318  void convexFillCells(
319  const std::vector<MapLocation> & polygon,
320  std::vector<MapLocation> & polygon_cells);
321 
327  virtual void updateOrigin(double new_origin_x, double new_origin_y);
328 
333  bool saveMap(std::string file_name);
334 
338  void resizeMap(
339  unsigned int size_x, unsigned int size_y, double resolution, double origin_x,
340  double origin_y);
341 
345  void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn);
346 
350  void resetMapToValue(
351  unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn, unsigned char value);
352 
358  unsigned int cellDistance(double world_dist);
359 
360  // Provide a typedef to ease future code maintenance
361  typedef std::recursive_mutex mutex_t;
362  mutex_t * getMutex()
363  {
364  return access_;
365  }
366 
367 protected:
381  template<typename data_type>
383  data_type * source_map, unsigned int sm_lower_left_x,
384  unsigned int sm_lower_left_y,
385  unsigned int sm_size_x, data_type * dest_map, unsigned int dm_lower_left_x,
386  unsigned int dm_lower_left_y, unsigned int dm_size_x, unsigned int region_size_x,
387  unsigned int region_size_y)
388  {
389  // we'll first need to compute the starting points for each map
390  data_type * sm_index = source_map + (sm_lower_left_y * sm_size_x + sm_lower_left_x);
391  data_type * dm_index = dest_map + (dm_lower_left_y * dm_size_x + dm_lower_left_x);
392 
393  // now, we'll copy the source map into the destination map
394  for (unsigned int i = 0; i < region_size_y; ++i) {
395  memcpy(dm_index, sm_index, region_size_x * sizeof(data_type));
396  sm_index += sm_size_x;
397  dm_index += dm_size_x;
398  }
399  }
400 
404  virtual void deleteMaps();
405 
409  virtual void resetMaps();
410 
416  virtual void initMaps(unsigned int size_x, unsigned int size_y);
417 
429  template<class ActionType>
430  inline void raytraceLine(
431  ActionType at, unsigned int x0, unsigned int y0, unsigned int x1,
432  unsigned int y1,
433  unsigned int max_length = UINT_MAX, unsigned int min_length = 0)
434  {
435  int dx_full = x1 - x0;
436  int dy_full = y1 - y0;
437 
438  // we need to chose how much to scale our dominant dimension,
439  // based on the maximum length of the line
440  double dist = std::hypot(dx_full, dy_full);
441  if (dist < min_length) {
442  return;
443  }
444 
445  unsigned int min_x0, min_y0;
446  if (dist > 0.0) {
447  // Adjust starting point and offset to start from min_length distance
448  min_x0 = (unsigned int)(x0 + dx_full / dist * min_length);
449  min_y0 = (unsigned int)(y0 + dy_full / dist * min_length);
450  } else {
451  // dist can be 0 if [x0, y0]==[x1, y1].
452  // In this case only this cell should be processed.
453  min_x0 = x0;
454  min_y0 = y0;
455  }
456  unsigned int offset = min_y0 * size_x_ + min_x0;
457 
458  int dx = x1 - min_x0;
459  int dy = y1 - min_y0;
460 
461  unsigned int abs_dx = abs(dx);
462  unsigned int abs_dy = abs(dy);
463 
464  int offset_dx = sign(dx);
465  int offset_dy = sign(dy) * size_x_;
466 
467  double scale = (dist == 0.0) ? 1.0 : std::min(1.0, max_length / dist);
468  // if x is dominant
469  if (abs_dx >= abs_dy) {
470  int error_y = abs_dx / 2;
471 
472  bresenham2D(
473  at, abs_dx, abs_dy, error_y, offset_dx, offset_dy, offset, (unsigned int)(scale * abs_dx));
474  return;
475  }
476 
477  // otherwise y is dominant
478  int error_x = abs_dy / 2;
479 
480  bresenham2D(
481  at, abs_dy, abs_dx, error_x, offset_dy, offset_dx, offset, (unsigned int)(scale * abs_dy));
482  }
483 
484 private:
489  template<class ActionType>
490  inline void bresenham2D(
491  ActionType at, unsigned int abs_da, unsigned int abs_db, int error_b,
492  int offset_a,
493  int offset_b, unsigned int offset,
494  unsigned int max_length)
495  {
496  unsigned int end = std::min(max_length, abs_da);
497  for (unsigned int i = 0; i < end; ++i) {
498  at(offset);
499  offset += offset_a;
500  error_b += abs_db;
501  if ((unsigned int)error_b >= abs_da) {
502  offset += offset_b;
503  error_b -= abs_da;
504  }
505  }
506  at(offset);
507  }
508 
512  inline int sign(int x)
513  {
514  return x > 0 ? 1.0 : -1.0;
515  }
516 
517  mutex_t * access_;
518 
519 protected:
520  unsigned int size_x_;
521  unsigned int size_y_;
522  double resolution_;
523  double origin_x_;
524  double origin_y_;
525  unsigned char * costmap_;
526  unsigned char default_value_;
527 
528  // *INDENT-OFF* Uncrustify doesn't handle indented public/private labels
529  class MarkCell
530  {
531  public:
532  MarkCell(unsigned char * costmap, unsigned char value)
533  : costmap_(costmap), value_(value)
534  {
535  }
536  inline void operator()(unsigned int offset)
537  {
538  costmap_[offset] = value_;
539  }
540 
541  private:
542  unsigned char * costmap_;
543  unsigned char value_;
544  };
545 
547  {
548  public:
550  const Costmap2D & costmap, const unsigned char * /*char_map*/,
551  std::vector<MapLocation> & cells)
552  : costmap_(costmap), cells_(cells)
553  {
554  }
555 
556  // just push the relevant cells back onto the list
557  inline void operator()(unsigned int offset)
558  {
559  MapLocation loc;
560  costmap_.indexToCells(offset, loc.x, loc.y);
561  cells_.push_back(loc);
562  }
563 
564  private:
565  const Costmap2D & costmap_;
566  std::vector<MapLocation> & cells_;
567  };
568  // *INDENT-ON*
569 };
570 } // namespace nav2_costmap_2d
571 
572 #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:68
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
Definition: costmap_2d.cpp:281
void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn)
Reset the costmap in bounds.
Definition: costmap_2d.cpp:130
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition: costmap_2d.hpp:211
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:135
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:108
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
Definition: costmap_2d.cpp:266
virtual ~Costmap2D()
Destructor.
Definition: costmap_2d.cpp:249
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:415
virtual void deleteMaps()
Deletes the costmap, static_map, and markers data structures.
Definition: costmap_2d.cpp:93
bool saveMap(std::string file_name)
Save the costmap out to a pgm file.
Definition: costmap_2d.cpp:536
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:187
void raytraceLine(ActionType at, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, unsigned int max_length=UINT_MAX, unsigned int min_length=0)
Raytrace a line and apply some action at each step.
Definition: costmap_2d.hpp:430
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:308
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
Definition: costmap_2d.cpp:261
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:287
void setDefaultValue(unsigned char c)
Set the default background value of the costmap.
Definition: costmap_2d.hpp:280
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:382
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:432
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:531
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:330
Costmap2D()
Default constructor.
Definition: costmap_2d.cpp:243
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:386
double getSizeInMetersY() const
Accessor for the y size of the costmap in meters.
Definition: costmap_2d.cpp:516
void indexToCells(unsigned int index, unsigned int &mx, unsigned int &my) const
Given an index... compute the associated map coordinates.
Definition: costmap_2d.hpp:222
double getSizeInMetersX() const
Accessor for the x size of the costmap in meters.
Definition: costmap_2d.cpp:511
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:302
virtual void initMaps(unsigned int size_x, unsigned int size_y)
Initializes the costmap, static_map, and markers data structures.
Definition: costmap_2d.cpp:101
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:145
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition: costmap_2d.cpp:501
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
Definition: costmap_2d.cpp:124
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:526
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition: costmap_2d.cpp:506
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:521
unsigned char getDefaultValue()
Get the default background value of the costmap.
Definition: costmap_2d.hpp:289
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
Set the cost of a cell in the costmap.
Definition: costmap_2d.cpp:276
unsigned int cellDistance(double world_dist)
Given distance in the world... convert it to cells.
Definition: costmap_2d.cpp:255
Costmap2D & operator=(const Costmap2D &map)
Overloaded assignment operator.
Definition: costmap_2d.cpp:210