38#ifndef EASYNAV_COSTMAP_COMMON__COSTMAP_2D_HPP_
39#define EASYNAV_COSTMAP_COMMON__COSTMAP_2D_HPP_
47#include "geometry_msgs/msg/point.hpp"
48#include "nav_msgs/msg/occupancy_grid.hpp"
80 unsigned int cells_size_x,
unsigned int cells_size_y,
double resolution,
81 double origin_x,
double origin_y,
unsigned char default_value = 0);
93 explicit Costmap2D(
const nav_msgs::msg::OccupancyGrid & map);
108 bool inBounds(
unsigned int x,
unsigned int y)
const;
119 const Costmap2D & map,
double win_origin_x,
double win_origin_y,
146 unsigned int sx0,
unsigned int sy0,
unsigned int sxn,
unsigned int syn,
147 unsigned int dx0,
unsigned int dy0);
165 unsigned char getCost(
unsigned int mx,
unsigned int my)
const;
172 unsigned char getCost(
unsigned int index)
const;
180 void setCost(
unsigned int mx,
unsigned int my,
unsigned char cost);
189 void mapToWorld(
unsigned int mx,
unsigned int my,
double & wx,
double & wy)
const;
208 bool worldToMap(
double wx,
double wy,
unsigned int & mx,
unsigned int & my)
const;
246 inline unsigned int getIndex(
unsigned int mx,
unsigned int my)
const
257 inline void indexToCells(
unsigned int index,
unsigned int & mx,
unsigned int & my)
const
336 const std::vector<geometry_msgs::msg::Point> & polygon,
337 unsigned char cost_value);
346 const std::vector<geometry_msgs::msg::Point> & polygon,
347 std::vector<MapLocation> & polygon_map_region);
355 const std::vector<MapLocation> & polygon_map_region,
356 unsigned char new_cost_value);
363 const std::vector<MapLocation> & polygon_map_region);
371 const std::vector<MapLocation> & polygon,
372 std::vector<MapLocation> & polygon_cells);
380 const std::vector<MapLocation> & polygon,
381 std::vector<MapLocation> & polygon_cells);
388 virtual void updateOrigin(
double new_origin_x,
double new_origin_y);
394 unsigned int size_x,
unsigned int size_y,
double resolution,
double origin_x,
400 void resetMap(
unsigned int x0,
unsigned int y0,
unsigned int xn,
unsigned int yn);
406 unsigned int x0,
unsigned int y0,
unsigned int xn,
unsigned int yn,
unsigned char value);
436 template<
typename data_type>
438 data_type * source_map,
unsigned int sm_lower_left_x,
439 unsigned int sm_lower_left_y,
440 unsigned int sm_size_x, data_type * dest_map,
unsigned int dm_lower_left_x,
441 unsigned int dm_lower_left_y,
unsigned int dm_size_x,
unsigned int region_size_x,
442 unsigned int region_size_y)
445 data_type * sm_index = source_map + (sm_lower_left_y * sm_size_x + sm_lower_left_x);
446 data_type * dm_index = dest_map + (dm_lower_left_y * dm_size_x + dm_lower_left_x);
449 for (
unsigned int i = 0; i < region_size_y; ++i) {
450 memcpy(dm_index, sm_index, region_size_x *
sizeof(data_type));
451 sm_index += sm_size_x;
452 dm_index += dm_size_x;
471 virtual void initMaps(
unsigned int size_x,
unsigned int size_y);
484 template<
class ActionType>
486 ActionType at,
unsigned int x0,
unsigned int y0,
unsigned int x1,
488 unsigned int max_length = UINT_MAX,
unsigned int min_length = 0)
490 int dx_full = x1 - x0;
491 int dy_full = y1 - y0;
495 double dist = std::hypot(dx_full, dy_full);
496 if (dist < min_length) {
500 unsigned int min_x0, min_y0;
503 min_x0 = (
unsigned int)(x0 + dx_full / dist * min_length);
504 min_y0 = (
unsigned int)(y0 + dy_full / dist * min_length);
511 unsigned int offset = min_y0 *
size_x_ + min_x0;
513 int dx = x1 - min_x0;
514 int dy = y1 - min_y0;
516 unsigned int abs_dx = abs(dx);
517 unsigned int abs_dy = abs(dy);
519 int offset_dx = sign(dx);
520 int offset_dy = sign(dy) *
size_x_;
522 double scale = (dist == 0.0) ? 1.0 : std::min(1.0, max_length / dist);
524 if (abs_dx >= abs_dy) {
525 int error_y = abs_dx / 2;
528 at, abs_dx, abs_dy, error_y, offset_dx, offset_dy, offset, (
unsigned int)(scale * abs_dx));
533 int error_x = abs_dy / 2;
536 at, abs_dy, abs_dx, error_x, offset_dy, offset_dx, offset, (
unsigned int)(scale * abs_dy));
544 template<
class ActionType>
545 inline void bresenham2D(
546 ActionType at,
unsigned int abs_da,
unsigned int abs_db,
int error_b,
548 int offset_b,
unsigned int offset,
549 unsigned int max_length)
551 unsigned int end = std::min(max_length, abs_da);
552 for (
unsigned int i = 0; i < end; ++i) {
556 if ((
unsigned int)error_b >= abs_da) {
567 inline int sign(
int x)
569 return x > 0 ? 1.0 : -1.0;
587 MarkCell(
unsigned char * costmap,
unsigned char value)
588 : costmap_(costmap), value_(value)
593 costmap_[offset] = value_;
598 unsigned char value_;
605 const Costmap2D & costmap,
const unsigned char * ,
606 std::vector<MapLocation> & cells)
607 : costmap_(costmap), cells_(cells)
615 costmap_.indexToCells(offset, loc.
x, loc.
y);
616 loc.
cost = costmap_.getCost(loc.
x, loc.
y);
617 cells_.push_back(loc);
622 std::vector<MapLocation> & cells_;
MarkCell(unsigned char *costmap, unsigned char value)
Definition costmap_2d.hpp:587
PolygonOutlineCells(const Costmap2D &costmap, const unsigned char *, std::vector< MapLocation > &cells)
Definition costmap_2d.hpp:604
Costmap2D(unsigned int cells_size_x, unsigned int cells_size_y, double resolution, double origin_x, double origin_y, unsigned char default_value=0)
Constructor for a costmap.
Definition costmap_2d.cpp:50
MarkCell(unsigned char *costmap, unsigned char value)
Definition costmap_2d.hpp:587
void operator()(unsigned int offset)
Definition costmap_2d.hpp:591
PolygonOutlineCells(const Costmap2D &costmap, const unsigned char *, std::vector< MapLocation > &cells)
Definition costmap_2d.hpp:604
void operator()(unsigned int offset)
Definition costmap_2d.hpp:612
Costmap2D()
Default constructor.
Definition costmap_2d.cpp:268
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:491
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
Definition costmap_2d.cpp:291
bool inBounds(unsigned int x, unsigned int y) const
Checks whether a given cell coordinate is within the bounds of the costmap.
Definition costmap_2d.cpp:280
double getResolution() const
Accessor for the resolution of the costmap.
Definition costmap_2d.cpp:608
void setDefaultValue(unsigned char c)
Set the default background value of the costmap.
Definition costmap_2d.hpp:315
unsigned int size_x_
Definition costmap_2d.hpp:575
Costmap2D & operator=(const Costmap2D &map)
Overloaded assignment operator.
Definition costmap_2d.cpp:234
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 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:317
void indexToCells(unsigned int index, unsigned int &mx, unsigned int &my) const
Given an index... compute the associated map coordinates.
Definition costmap_2d.hpp:257
double getSizeInMetersY() const
Accessor for the y size of the costmap in meters.
Definition costmap_2d.cpp:593
unsigned int cellDistance(double world_dist)
Given distance in the world... convert it to cells.
Definition costmap_2d.cpp:285
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
Costmap2D(unsigned int cells_size_x, unsigned int cells_size_y, double resolution, double origin_x, double origin_y, unsigned char default_value=0)
Constructor for a costmap.
Definition costmap_2d.cpp:50
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
virtual void deleteMaps()
Deletes the costmap, static_map, and markers data structures.
Definition costmap_2d.cpp:94
void restoreMapRegionOccupiedByPolygon(const std::vector< MapLocation > &polygon_map_region)
Restores the corresponding map region using given map region.
Definition costmap_2d.cpp:461
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition costmap_2d.cpp:578
double getOriginX() const
Accessor for the x origin of the costmap.
Definition costmap_2d.cpp:598
double resolution_
Definition costmap_2d.hpp:577
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
Set the cost of a cell in the costmap.
Definition costmap_2d.cpp:306
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:359
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
Definition costmap_2d.cpp:125
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:485
bool worldToMapContinuous(double wx, double wy, float &mx, float &my) const
Convert from world coordinates to map coordinates.
Definition costmap_2d.cpp:338
virtual void updateOrigin(double new_origin_x, double new_origin_y)
Move the origin of the costmap to a new location....
Definition costmap_2d.cpp:381
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:469
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition costmap_2d.cpp:323
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition costmap_2d.cpp:583
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition costmap_2d.hpp:246
unsigned char * costmap_
Definition costmap_2d.hpp:580
friend class CostmapTester
Definition costmap_2d.hpp:67
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:437
unsigned char getDefaultValue()
Get the default background value of the costmap.
Definition costmap_2d.hpp:324
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:353
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
Definition costmap_2d.cpp:296
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
void toOccupancyGridMsg(nav_msgs::msg::OccupancyGrid &msg) const
Converts the current costmap into a nav_msgs::msg::OccupancyGrid message.
Definition costmap_2d.cpp:209
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
std::recursive_mutex mutex_t
Definition costmap_2d.hpp:416
unsigned int size_y_
Definition costmap_2d.hpp:576
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:452
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:437
double origin_y_
Definition costmap_2d.hpp:579
double getOriginY() const
Accessor for the y origin of the costmap.
Definition costmap_2d.cpp:603
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
Definition costmap_2d.cpp:311
virtual ~Costmap2D()
Destructor.
Definition costmap_2d.cpp:274
mutex_t * getMutex()
Definition costmap_2d.hpp:417
void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn)
Reset the costmap in bounds.
Definition costmap_2d.cpp:131
double origin_x_
Definition costmap_2d.hpp:578
unsigned char default_value_
Definition costmap_2d.hpp:581
double getSizeInMetersX() const
Accessor for the x size of the costmap in meters.
Definition costmap_2d.cpp:588
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:508
Provides a mapping for often used cost values.
Definition cost_values.hpp:41
Definition costmap_2d.hpp:55
unsigned char cost
Definition costmap_2d.hpp:58
unsigned int x
Definition costmap_2d.hpp:56
unsigned int y
Definition costmap_2d.hpp:57