45 Costmap2D::Costmap2D(
unsigned int cells_size_x,
unsigned int cells_size_y,
double resolution,
46 double origin_x,
double origin_y,
unsigned char default_value) :
47 size_x_(cells_size_x), size_y_(cells_size_y), resolution_(resolution), origin_x_(origin_x),
48 origin_y_(origin_y), costmap_(NULL), default_value_(default_value)
60 boost::unique_lock<mutex_t> lock(*
access_);
67 boost::unique_lock<mutex_t> lock(*
access_);
69 costmap_ =
new unsigned char[size_x * size_y];
73 double origin_x,
double origin_y)
89 boost::unique_lock<mutex_t> lock(*
access_);
95 boost::unique_lock<mutex_t> lock(*(
access_));
96 unsigned int len = xn - x0;
115 unsigned int lower_left_x, lower_left_y, upper_right_x, upper_right_y;
116 if (!map.
worldToMap(win_origin_x, win_origin_y, lower_left_x, lower_left_y)
117 || !map.
worldToMap(win_origin_x + win_size_x, win_origin_y + win_size_y, upper_right_x, upper_right_y))
123 size_x_ = upper_right_x - lower_left_x;
124 size_y_ = upper_right_y - lower_left_y;
184 return (
unsigned int)cells_dist;
263 int cell_ox, cell_oy;
268 if (cell_ox == 0 && cell_oy == 0)
273 double new_grid_ox, new_grid_oy;
282 int lower_left_x, lower_left_y, upper_right_x, upper_right_y;
283 lower_left_x =
min(
max(cell_ox, 0), size_x);
284 lower_left_y =
min(
max(cell_oy, 0), size_y);
285 upper_right_x =
min(
max(cell_ox + size_x, 0), size_x);
286 upper_right_y =
min(
max(cell_oy + size_y, 0), size_y);
288 unsigned int cell_size_x = upper_right_x - lower_left_x;
289 unsigned int cell_size_y = upper_right_y - lower_left_y;
292 unsigned char* local_map =
new unsigned char[cell_size_x * cell_size_y];
305 int start_x = lower_left_x - cell_ox;
306 int start_y = lower_left_y - cell_oy;
318 std::vector<MapLocation> map_polygon;
319 for (
unsigned int i = 0; i < polygon.size(); ++i)
322 if (!
worldToMap(polygon[i].x, polygon[i].y, loc.
x, loc.
y))
327 map_polygon.push_back(loc);
330 std::vector<MapLocation> polygon_cells;
336 for (
unsigned int i = 0; i < polygon_cells.size(); ++i)
338 unsigned int index =
getIndex(polygon_cells[i].x, polygon_cells[i].y);
347 for (
unsigned int i = 0; i < polygon.size() - 1; ++i)
349 raytraceLine(cell_gatherer, polygon[i].x, polygon[i].y, polygon[i + 1].x, polygon[i + 1].y);
351 if (!polygon.empty())
353 unsigned int last_index = polygon.size() - 1;
355 raytraceLine(cell_gatherer, polygon[last_index].x, polygon[last_index].y, polygon[0].x, polygon[0].y);
362 if (polygon.size() < 3)
371 while (i < polygon_cells.size() - 1)
373 if (polygon_cells[i].x > polygon_cells[i + 1].x)
375 swap = polygon_cells[i];
376 polygon_cells[i] = polygon_cells[i + 1];
377 polygon_cells[i + 1] = swap;
389 unsigned int min_x = polygon_cells[0].
x;
390 unsigned int max_x = polygon_cells[polygon_cells.size() - 1].x;
393 for (
unsigned int x = min_x; x <= max_x; ++x)
395 if (i >= polygon_cells.size() - 1)
398 if (polygon_cells[i].y < polygon_cells[i + 1].y)
400 min_pt = polygon_cells[i];
401 max_pt = polygon_cells[i + 1];
405 min_pt = polygon_cells[i + 1];
406 max_pt = polygon_cells[i];
410 while (i < polygon_cells.size() && polygon_cells[i].x == x)
412 if (polygon_cells[i].y < min_pt.
y)
413 min_pt = polygon_cells[i];
414 else if (polygon_cells[i].y > max_pt.
y)
415 max_pt = polygon_cells[i];
421 for (
unsigned int y = min_pt.
y; y < max_pt.
y; ++y)
425 polygon_cells.push_back(pt);
467 FILE *fp = fopen(file_name.c_str(),
"w");
475 for (
unsigned int iy = 0; iy <
size_y_; iy++)
477 for (
unsigned int ix = 0; ix <
size_x_; ix++)
479 unsigned char cost =
getCost(ix, iy);
480 fprintf(fp,
"%d ", cost);
double getSizeInMetersX() const
Accessor for the x size of the costmap in meters.
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
Set the cost of a cell in the costmap.
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
virtual void initMaps(unsigned int size_x, unsigned int size_y)
Initializes the costmap, static_map, and markers data structures.
unsigned int cellDistance(double world_dist)
Given distance in the world... convert it to cells.
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
void worldToMapEnforceBounds(double wx, double wy, int &mx, int &my) const
Convert from world coordinates to map coordinates, constraining results to legal bounds.
double getOriginY() const
Accessor for the y origin of the costmap.
virtual void deleteMaps()
Deletes the costmap, static_map, and markers data structures.
double getSizeInMetersY() const
Accessor for the y size of the costmap in meters.
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.
void raytraceLine(ActionType at, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, unsigned int max_length=UINT_MAX)
Raytrace a line and apply some action at each step.
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
boost::recursive_mutex mutex_t
void convexFillCells(const std::vector< MapLocation > &polygon, std::vector< MapLocation > &polygon_cells)
Get the map cells that fill a convex polygon.
bool saveMap(std::string file_name)
Save the costmap out to a pgm file.
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
void polygonOutlineCells(const std::vector< MapLocation > &polygon, std::vector< MapLocation > &polygon_cells)
Get the map cells that make up the outline of a polygon.
bool setConvexPolygonCost(const std::vector< geometry_msgs::Point > &polygon, unsigned char cost_value)
Sets the cost of a convex polygon to a desired value.
double min(double a, double b)
Costmap2D & operator=(const Costmap2D &map)
Overloaded assignment operator.
virtual ~Costmap2D()
Destructor.
unsigned char default_value_
double getOriginX() const
Accessor for the x origin of the costmap.
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.
Costmap2D()
Default constructor.
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
double getResolution() const
Accessor for the resolution of the costmap.
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.
void worldToMapNoBounds(double wx, double wy, int &mx, int &my) const
Convert from world coordinates to map coordinates without checking for legal bounds.
A 2D costmap provides a mapping between points in the world and their associated "costs".
void swap(scoped_ptr< T > &, scoped_ptr< T > &)
double max(double a, double b)
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn)