costmap_2d
costmap_2d.h
Go to the documentation of this file.
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 COSTMAP_2D_COSTMAP_2D_H_
39 #define COSTMAP_2D_COSTMAP_2D_H_
40 
41 #include <vector>
42 #include <queue>
43 #include <geometry_msgs/Point.h>
44 #include <boost/thread.hpp>
45 
46 namespace costmap_2d
47 {
48 
49 // convenient for storing x/y point pairs
50 struct MapLocation
51 {
52  unsigned int x;
53  unsigned int y;
54 };
55 
60 class Costmap2D
61 {
62  friend class CostmapTester; // Need this for gtest to work correctly
63 public:
73  Costmap2D(unsigned int cells_size_x, unsigned int cells_size_y, double resolution,
74  double origin_x, double origin_y, unsigned char default_value = 0);
75 
80  Costmap2D(const Costmap2D& map);
81 
87  Costmap2D& operator=(const Costmap2D& map);
88 
97  bool copyCostmapWindow(const Costmap2D& map, double win_origin_x, double win_origin_y, double win_size_x,
98  double win_size_y);
99 
103  Costmap2D();
104 
108  virtual ~Costmap2D();
109 
116  unsigned char getCost(unsigned int mx, unsigned int my) const;
117 
124  double getTimeStamp(unsigned int mx, unsigned int my) const;
125 
132  void setCost(unsigned int mx, unsigned int my, unsigned char cost);
133 
141  void mapToWorld(unsigned int mx, unsigned int my, double& wx, double& wy) const;
142 
151  bool worldToMap(double wx, double wy, unsigned int& mx, unsigned int& my) const;
152 
161  void worldToMapNoBounds(double wx, double wy, int& mx, int& my) const;
162 
171  void worldToMapEnforceBounds(double wx, double wy, int& mx, int& my) const;
172 
179  inline unsigned int getIndex(unsigned int mx, unsigned int my) const
180  {
181  return my * size_x_ + mx;
182  }
183 
190  inline void indexToCells(unsigned int index, unsigned int& mx, unsigned int& my) const
191  {
192  my = index / size_x_;
193  mx = index - (my * size_x_);
194  }
195 
200  unsigned char* getCharMap() const;
201 
206  unsigned int getSizeInCellsX() const;
207 
212  unsigned int getSizeInCellsY() const;
213 
218  double getSizeInMetersX() const;
219 
224  double getSizeInMetersY() const;
225 
230  double getOriginX() const;
231 
236  double getOriginY() const;
237 
242  double getResolution() const;
243 
244  void setDefaultValue(unsigned char c)
245  {
246  default_value_ = c;
247  }
248 
249  unsigned char getDefaultValue()
250  {
251  return default_value_;
252  }
253 
260  bool setConvexPolygonCost(const std::vector<geometry_msgs::Point>& polygon, unsigned char cost_value);
261 
267  void polygonOutlineCells(const std::vector<MapLocation>& polygon, std::vector<MapLocation>& polygon_cells);
268 
274  void convexFillCells(const std::vector<MapLocation>& polygon, std::vector<MapLocation>& polygon_cells);
275 
281  virtual void updateOrigin(double new_origin_x, double new_origin_y);
282 
287  bool saveMap(std::string file_name);
288 
289  void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x,
290  double origin_y);
291 
292  void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn);
293 
299  unsigned int cellDistance(double world_dist);
300 
301  // Provide a typedef to ease future code maintenance
302  typedef boost::recursive_mutex mutex_t;
303  mutex_t* getMutex()
304  {
305  return access_;
306  }
307 
308 protected:
322  template<typename data_type>
323  void copyMapRegion(data_type* source_map, unsigned int sm_lower_left_x, unsigned int sm_lower_left_y,
324  unsigned int sm_size_x, data_type* dest_map, unsigned int dm_lower_left_x,
325  unsigned int dm_lower_left_y, unsigned int dm_size_x, unsigned int region_size_x,
326  unsigned int region_size_y)
327  {
328  // we'll first need to compute the starting points for each map
329  data_type* sm_index = source_map + (sm_lower_left_y * sm_size_x + sm_lower_left_x);
330  data_type* dm_index = dest_map + (dm_lower_left_y * dm_size_x + dm_lower_left_x);
331 
332  // now, we'll copy the source map into the destination map
333  for (unsigned int i = 0; i < region_size_y; ++i)
334  {
335  memcpy(dm_index, sm_index, region_size_x * sizeof(data_type));
336  sm_index += sm_size_x;
337  dm_index += dm_size_x;
338  }
339  }
340 
344  virtual void deleteMaps();
345 
349  virtual void resetMaps();
350 
356  virtual void initMaps(unsigned int size_x, unsigned int size_y);
357 
367  template<class ActionType>
368  inline void raytraceLine(ActionType at, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1,
369  unsigned int max_length = UINT_MAX)
370  {
371  int dx = x1 - x0;
372  int dy = y1 - y0;
373 
374  unsigned int abs_dx = abs(dx);
375  unsigned int abs_dy = abs(dy);
376 
377  int offset_dx = sign(dx);
378  int offset_dy = sign(dy) * size_x_;
379 
380  unsigned int offset = y0 * size_x_ + x0;
381 
382  // we need to chose how much to scale our dominant dimension, based on the maximum length of the line
383  double dist = hypot(dx, dy);
384  double scale = (dist == 0.0) ? 1.0 : std::min(1.0, max_length / dist);
385 
386  // if x is dominant
387  if (abs_dx >= abs_dy)
388  {
389  int error_y = abs_dx / 2;
390  bresenham2D(at, abs_dx, abs_dy, error_y, offset_dx, offset_dy, offset, (unsigned int)(scale * abs_dx));
391  return;
392  }
393 
394  // otherwise y is dominant
395  int error_x = abs_dy / 2;
396  bresenham2D(at, abs_dy, abs_dx, error_x, offset_dy, offset_dx, offset, (unsigned int)(scale * abs_dy));
397  }
398 
399 private:
403  template<class ActionType>
404  inline void bresenham2D(ActionType at, unsigned int abs_da, unsigned int abs_db, int error_b, int offset_a,
405  int offset_b, unsigned int offset, unsigned int max_length)
406  {
407  unsigned int end = std::min(max_length, abs_da);
408  for (unsigned int i = 0; i < end; ++i)
409  {
410  at(offset);
411  offset += offset_a;
412  error_b += abs_db;
413  if ((unsigned int)error_b >= abs_da)
414  {
415  offset += offset_b;
416  error_b -= abs_da;
417  }
418  }
419  at(offset);
420  }
421 
422  inline int sign(int x)
423  {
424  return x > 0 ? 1.0 : -1.0;
425  }
426 
427  mutex_t* access_;
428 protected:
429  unsigned int size_x_;
430  unsigned int size_y_;
431  double resolution_;
432  double origin_x_;
433  double origin_y_;
434  unsigned char* costmap_;
435  unsigned char default_value_;
436 
437  double* timestamps_; // Timestamp last update
438 
439  class MarkCell
440  {
441  public:
442  MarkCell(unsigned char* costmap, double* timestamps, unsigned char value, double time) :
443  costmap_(costmap), timestamps_(timestamps), value_(value), time_(time)
444  {
445  }
446  inline void operator()(unsigned int offset)
447  {
448  costmap_[offset] = value_;
449  timestamps_[offset] = time_;
450  }
451  private:
452  unsigned char* costmap_;
453  double* timestamps_;
454  unsigned char value_;
455  double time_;
456  };
457 
459  {
460  public:
461  PolygonOutlineCells(const Costmap2D& costmap, const unsigned char* char_map, std::vector<MapLocation>& cells) :
462  costmap_(costmap), char_map_(char_map), cells_(cells)
463  {
464  }
465 
466  // just push the relevant cells back onto the list
467  inline void operator()(unsigned int offset)
468  {
470  costmap_.indexToCells(offset, loc.x, loc.y);
471  cells_.push_back(loc);
472  }
473 
474  private:
476  const unsigned char* char_map_;
478  };
479 };
480 } // namespace costmap_2d
481 
482 #endif // COSTMAP_2D_COSTMAP_2D_H
costmap_2d::Costmap2D::timestamps_
double * timestamps_
Definition: costmap_2d.h:473
costmap_2d::Costmap2D::getSizeInMetersX
double getSizeInMetersX() const
Accessor for the x size of the costmap in meters.
Definition: costmap_2d.cpp:461
std::string
costmap_2d::Costmap2D::sign
int sign(int x)
Definition: costmap_2d.h:458
costmap_2d::Costmap2D::initMaps
virtual void initMaps(unsigned int size_x, unsigned int size_y)
Initializes the costmap, static_map, and markers data structures.
Definition: costmap_2d.cpp:67
costmap_2d::Costmap2D::worldToMap
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:224
costmap_2d::Costmap2D::default_value_
unsigned char default_value_
Definition: costmap_2d.h:471
costmap_2d::Costmap2D::getSizeInMetersY
double getSizeInMetersY() const
Accessor for the y size of the costmap in meters.
Definition: costmap_2d.cpp:466
costmap_2d::Costmap2D::MarkCell::time_
double time_
Definition: costmap_2d.h:491
vector
costmap_2d::Costmap2D::PolygonOutlineCells::costmap_
const Costmap2D & costmap_
Definition: costmap_2d.h:511
costmap_2d::Costmap2D::origin_x_
double origin_x_
Definition: costmap_2d.h:468
costmap_2d::Costmap2D::~Costmap2D
virtual ~Costmap2D()
Destructor.
Definition: costmap_2d.cpp:185
costmap_2d::Costmap2D::PolygonOutlineCells::cells_
std::vector< MapLocation > & cells_
Definition: costmap_2d.h:513
costmap_2d::Costmap2D
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.h:96
costmap_2d::Costmap2D::mapToWorld
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
Definition: costmap_2d.cpp:218
costmap_2d::Costmap2D::getCharMap
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
Definition: costmap_2d.cpp:197
costmap_2d::Costmap2D::getOriginX
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:471
costmap_2d::Costmap2D::getDefaultValue
unsigned char getDefaultValue()
Definition: costmap_2d.h:285
costmap_2d::Costmap2D::CostmapTester
friend class CostmapTester
Definition: costmap_2d.h:98
queue
costmap_2d::Costmap2D::Costmap2D
Costmap2D()
Default constructor.
Definition: costmap_2d.cpp:179
costmap_2d::Costmap2D::access_
mutex_t * access_
Definition: costmap_2d.h:463
costmap_2d::Costmap2D::polygonOutlineCells
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:365
costmap_2d::Costmap2D::MarkCell::MarkCell
MarkCell(unsigned char *costmap, double *timestamps, unsigned char value, double time)
Definition: costmap_2d.h:478
costmap_2d::Costmap2D::operator=
Costmap2D & operator=(const Costmap2D &map)
Overloaded assignment operator.
Definition: costmap_2d.cpp:145
costmap_2d::MapLocation::y
unsigned int y
Definition: costmap_2d.h:125
costmap_2d::MapLocation::x
unsigned int x
Definition: costmap_2d.h:124
costmap_2d::Costmap2D::resizeMap
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
Definition: costmap_2d.cpp:75
costmap_2d::Costmap2D::setCost
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
Set the cost of a cell in the costmap.
Definition: costmap_2d.cpp:212
costmap_2d::Costmap2D::MarkCell::operator()
void operator()(unsigned int offset)
Definition: costmap_2d.h:482
costmap_2d::Costmap2D::PolygonOutlineCells
Definition: costmap_2d.h:494
costmap_2d::Costmap2D::PolygonOutlineCells::operator()
void operator()(unsigned int offset)
Definition: costmap_2d.h:503
costmap_2d::Costmap2D::saveMap
bool saveMap(std::string file_name)
Save the costmap out to a pgm file.
Definition: costmap_2d.cpp:486
costmap_2d::MapLocation
Definition: costmap_2d.h:86
costmap_2d::Costmap2D::indexToCells
void indexToCells(unsigned int index, unsigned int &mx, unsigned int &my) const
Given an index... compute the associated map coordinates.
Definition: costmap_2d.h:226
costmap_2d::Costmap2D::PolygonOutlineCells::PolygonOutlineCells
PolygonOutlineCells(const Costmap2D &costmap, const unsigned char *char_map, std::vector< MapLocation > &cells)
Definition: costmap_2d.h:497
costmap_2d::Costmap2D::size_y_
unsigned int size_y_
Definition: costmap_2d.h:466
costmap_2d::Costmap2D::getSizeInCellsY
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition: costmap_2d.cpp:456
costmap_2d::Costmap2D::PolygonOutlineCells::char_map_
const unsigned char * char_map_
Definition: costmap_2d.h:512
costmap_2d::Costmap2D::resetMap
void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn)
Definition: costmap_2d.cpp:97
costmap_2d::Costmap2D::getMutex
mutex_t * getMutex()
Definition: costmap_2d.h:339
std::min
T min(T... args)
costmap_2d::Costmap2D::size_x_
unsigned int size_x_
Definition: costmap_2d.h:465
costmap_2d::Costmap2D::worldToMapEnforceBounds
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:244
costmap_2d::Costmap2D::getOriginY
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:476
costmap_2d::Costmap2D::copyCostmapWindow
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:108
costmap_2d::Costmap2D::cellDistance
unsigned int cellDistance(double world_dist)
Given distance in the world... convert it to cells.
Definition: costmap_2d.cpp:191
costmap_2d::Costmap2D::MarkCell::value_
unsigned char value_
Definition: costmap_2d.h:490
costmap_2d::Costmap2D::resolution_
double resolution_
Definition: costmap_2d.h:467
costmap_2d::Costmap2D::resetMaps
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
Definition: costmap_2d.cpp:90
costmap_2d::Costmap2D::deleteMaps
virtual void deleteMaps()
Deletes the costmap, static_map, and markers data structures.
Definition: costmap_2d.cpp:57
costmap_2d::Costmap2D::setDefaultValue
void setDefaultValue(unsigned char c)
Definition: costmap_2d.h:280
costmap_2d::Costmap2D::raytraceLine
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.
Definition: costmap_2d.h:404
costmap_2d::Costmap2D::getTimeStamp
double getTimeStamp(unsigned int mx, unsigned int my) const
Get the last update timestamp of the cell.
Definition: costmap_2d.cpp:207
costmap_2d::Costmap2D::copyMapRegion
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.h:359
costmap_2d::Costmap2D::origin_y_
double origin_y_
Definition: costmap_2d.h:469
costmap_2d::Costmap2D::getResolution
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:481
costmap_2d::Costmap2D::updateOrigin
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:276
std::memcpy
T memcpy(T... args)
costmap_2d::Costmap2D::setConvexPolygonCost
bool setConvexPolygonCost(const std::vector< geometry_msgs::Point > &polygon, unsigned char cost_value)
Sets the cost of a convex polygon to a desired value.
Definition: costmap_2d.cpp:335
costmap_2d::Costmap2D::getIndex
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition: costmap_2d.h:215
costmap_2d::Costmap2D::costmap_
unsigned char * costmap_
Definition: costmap_2d.h:470
costmap_2d::Costmap2D::getSizeInCellsX
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition: costmap_2d.cpp:451
costmap_2d::Costmap2D::MarkCell::timestamps_
double * timestamps_
Definition: costmap_2d.h:489
costmap_2d::Costmap2D::bresenham2D
void bresenham2D(ActionType at, unsigned int abs_da, unsigned int abs_db, int error_b, int offset_a, int offset_b, unsigned int offset, unsigned int max_length)
A 2D implementation of Bresenham's raytracing algorithm... applies an action at each step.
Definition: costmap_2d.h:440
costmap_2d::Costmap2D::mutex_t
boost::recursive_mutex mutex_t
Definition: costmap_2d.h:338
costmap_2d::Costmap2D::worldToMapNoBounds
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:238
costmap_2d::Costmap2D::convexFillCells
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:380
costmap_2d::Costmap2D::MarkCell::costmap_
unsigned char * costmap_
Definition: costmap_2d.h:488
costmap_2d::Costmap2D::getCost
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
Definition: costmap_2d.cpp:202
costmap_2d
Definition: array_parser.h:37