Costmap.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <optional>
4 
5 #include <Eigen/Core>
6 
9 
11 {
12 
13  class Costmap
14  {
15  public:
16  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
17 
18  friend class CostmapBuilder;
19  friend class SocialCostmapBuilder;
20 
21  struct Parameters
22  {
23  // if set to false, distance to obstacles will be computed and not only a binary collision check
24  bool binaryGrid{false};
25 
26  /// How big each cell is in the uniform grid.
27  float cellSize = 100.f; // [mm]
28 
29  float sceneBoundsMargin = 1000.f;// [mm]
30  };
31 
32  struct Filter
33  {
34  Eigen::MatrixXf matrix;
35  bool useMinimum = false;
36 
37  inline Filter& setUseMinimum(bool value)
38  {
39  useMinimum = value;
40  return *this;
41  }
42 
43  static Filter gaussian(int radius = 2, float sigma = 1.);
44  };
45 
46  using Index = Eigen::Array2i;
47  using Position = Eigen::Vector2f;
48  using Grid = Eigen::MatrixXf;
49 
50  // if 0, the cell is invalid
52 
53  Costmap(const Grid& grid,
54  const Parameters& parameters,
55  const SceneBounds& sceneBounds,
56  const std::optional<Mask>& mask = std::nullopt,
58 
59  struct Vertex
60  {
61  Index index; // row corresponds to y; column corresponds to x
63  };
64 
65  Position toPositionLocal(const Index& index) const;
66  Position toPositionGlobal(const Index& index) const;
67  Vertex toVertex(const Position& globalPosition) const;
68 
69  const Grid& getGrid() const;
70  Grid&
72  {
73  return grid;
74  }
75 
76  bool isInCollision(const Position& p) const;
77 
78  const Parameters& params() const noexcept;
79 
80  // get the scene bounds in the costmap's base frame (aka 'origin')
81  const SceneBounds& getLocalSceneBounds() const noexcept;
82 
83  core::Pose2D
84  centerPose() const
85  {
86  const Eigen::Vector2f costmap_P_center{
87  (getLocalSceneBounds().max.x() - getLocalSceneBounds().min.x()) / 2 +
89  (getLocalSceneBounds().max.y() - getLocalSceneBounds().min.y()) / 2 +
90  getLocalSceneBounds().min.y()};
91 
92  return global_T_costmap * Eigen::Translation2f(costmap_P_center);
93  }
94 
95  struct Optimum
96  {
97  float value;
100  };
101 
102  Optimum optimum() const;
103 
104  void filter(const Filter &filter);
105 
106  bool add(const Costmap& other, float weight = 1.0);
107 
108  enum class AddMode
109  {
110  MIN,
111  MAX
112  };
113 
114  bool add(const Costmap& other, AddMode mode);
115 
116  //! checks whether the cell is masked out
117  bool isValid(const Index& index) const noexcept;
118 
119  const std::optional<Mask>& getMask() const noexcept;
120 
121  std::optional<Costmap::Mask>& getMutableMask() noexcept;
122 
123 
124  std::optional<float> value(const Index& index) const;
125 
126  std::optional<float> value(const Position& position) const;
127 
128  Costmap mergeInto(const std::vector<Costmap>& costmaps,
129  const std::vector<float>& weights) const;
130 
131  const core::Pose2D&
132  origin() const
133  {
134  return global_T_costmap;
135  }
136 
137  void
138  setOrigin(const core::Pose2D& globalPose)
139  {
140  global_T_costmap = globalPose;
141  }
142 
143  std::size_t numberOfValidElements() const;
144 
145  private:
146  void validateSizes() const;
147 
148  Grid grid;
149  std::optional<Mask> mask;
150 
151  const SceneBounds sceneBounds;
152 
153  const Parameters parameters;
154 
155  core::Pose2D global_T_costmap = core::Pose2D::Identity();
156  };
157 
158 
159 } // namespace armarx::navigation::algorithms
armarx::navigation::algorithms::Costmap::getMask
const std::optional< Mask > & getMask() const noexcept
Definition: Costmap.cpp:392
armarx::navigation::algorithms::Costmap::setOrigin
void setOrigin(const core::Pose2D &globalPose)
Definition: Costmap.h:138
armarx::navigation::algorithms::Costmap::Index
Eigen::Array2i Index
Definition: Costmap.h:46
armarx::navigation::algorithms::Costmap::getMutableMask
std::optional< Costmap::Mask > & getMutableMask() noexcept
Definition: Costmap.cpp:398
armarx::navigation::algorithms::Costmap::AddMode::MAX
@ MAX
index
uint8_t index
Definition: EtherCATFrame.h:59
basic_types.h
armarx::navigation::algorithms::Costmap::Costmap
Costmap(const Grid &grid, const Parameters &parameters, const SceneBounds &sceneBounds, const std::optional< Mask > &mask=std::nullopt, const core::Pose2D &origin=core::Pose2D::Identity())
Definition: Costmap.cpp:32
armarx::navigation::algorithms::Costmap::Filter::matrix
Eigen::MatrixXf matrix
Definition: Costmap.h:34
armarx::navigation::algorithms::Costmap::Vertex::index
Index index
Definition: Costmap.h:61
armarx::navigation::algorithms
This file is part of ArmarX.
Definition: aron_conversions.cpp:13
armarx::navigation::algorithms::Costmap::Optimum::index
Index index
Definition: Costmap.h:98
armarx::navigation::algorithms::Costmap::Grid
Eigen::MatrixXf Grid
Definition: Costmap.h:48
armarx::navigation::algorithms::Costmap::Filter::useMinimum
bool useMinimum
Definition: Costmap.h:35
armarx::navigation::algorithms::Costmap::Vertex
Definition: Costmap.h:59
armarx::navigation::algorithms::Costmap::Filter
Definition: Costmap.h:32
armarx::navigation::algorithms::Costmap::toPositionGlobal
Position toPositionGlobal(const Index &index) const
Definition: Costmap.cpp:71
armarx::navigation::algorithms::Costmap::Mask
Eigen::Matrix< bool, Eigen::Dynamic, Eigen::Dynamic > Mask
Definition: Costmap.h:51
armarx::navigation::algorithms::Costmap::getLocalSceneBounds
const SceneBounds & getLocalSceneBounds() const noexcept
Definition: Costmap.cpp:126
armarx::navigation::core::Pose2D
Eigen::Isometry2f Pose2D
Definition: basic_types.h:34
armarx::navigation::algorithms::Costmap::SocialCostmapBuilder
friend class SocialCostmapBuilder
Definition: Costmap.h:19
armarx::navigation::algorithms::Costmap::centerPose
core::Pose2D centerPose() const
Definition: Costmap.h:84
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::navigation::algorithms::Costmap::Parameters::binaryGrid
bool binaryGrid
Definition: Costmap.h:24
armarx::navigation::algorithms::Costmap::AddMode::MIN
@ MIN
armarx::navigation::algorithms::Costmap::Filter::setUseMinimum
Filter & setUseMinimum(bool value)
Definition: Costmap.h:37
armarx::navigation::algorithms::Costmap::params
const Parameters & params() const noexcept
Definition: Costmap.cpp:230
armarx::navigation::algorithms::Costmap::Optimum
Definition: Costmap.h:95
armarx::navigation::algorithms::Costmap::toPositionLocal
Position toPositionLocal(const Index &index) const
Definition: Costmap.cpp:57
armarx::navigation::algorithms::Costmap::toVertex
Vertex toVertex(const Position &globalPosition) const
Definition: Costmap.cpp:99
armarx::navigation::algorithms::Costmap::getGrid
const Grid & getGrid() const
Definition: Costmap.cpp:236
armarx::navigation::algorithms::Costmap::Position
Eigen::Vector2f Position
Definition: Costmap.h:47
armarx::navigation::algorithms::Costmap::value
std::optional< float > value(const Index &index) const
Definition: Costmap.cpp:404
armarx::navigation::algorithms::SceneBounds
Definition: types.h:29
armarx::navigation::algorithms::CostmapBuilder
Definition: CostmapBuilder.h:31
armarx::navigation::algorithms::Costmap::mergeInto
Costmap mergeInto(const std::vector< Costmap > &costmaps, const std::vector< float > &weights) const
Definition: Costmap.cpp:425
armarx::navigation::algorithms::SceneBounds::min
Eigen::Vector2f min
Definition: types.h:31
armarx::navigation::algorithms::Costmap::filter
void filter(const Filter &filter)
Definition: Costmap.cpp:175
armarx::navigation::algorithms::Costmap::AddMode
AddMode
Definition: Costmap.h:108
std
Definition: Application.h:66
armarx::navigation::algorithms::Costmap::Parameters::cellSize
float cellSize
How big each cell is in the uniform grid.
Definition: Costmap.h:27
armarx::navigation::algorithms::Costmap::Vertex::position
Position position
Definition: Costmap.h:62
armarx::navigation::algorithms::Costmap::Parameters
Definition: Costmap.h:21
armarx::navigation::algorithms::Costmap::optimum
Optimum optimum() const
Definition: Costmap.cpp:132
Eigen::Matrix< bool, Eigen::Dynamic, Eigen::Dynamic >
armarx::navigation::algorithms::Costmap::origin
const core::Pose2D & origin() const
Definition: Costmap.h:132
armarx::navigation::algorithms::Costmap::Parameters::sceneBoundsMargin
float sceneBoundsMargin
Definition: Costmap.h:29
armarx::navigation::algorithms::Costmap::isValid
bool isValid(const Index &index) const noexcept
checks whether the cell is masked out
Definition: Costmap.cpp:78
armarx::navigation::algorithms::Costmap::add
bool add(const Costmap &other, float weight=1.0)
Definition: Costmap.cpp:249
armarx::navigation::algorithms::Costmap::numberOfValidElements
std::size_t numberOfValidElements() const
Definition: Costmap.cpp:497
armarx::navigation::algorithms::Costmap::Filter::gaussian
static Filter gaussian(int radius=2, float sigma=1.)
Definition: Costmap.cpp:479
types.h
armarx::navigation::algorithms::SceneBounds::max
Eigen::Vector2f max
Definition: types.h:32
armarx::navigation::algorithms::Costmap::isInCollision
bool isInCollision(const Position &p) const
Definition: Costmap.cpp:242
armarx::navigation::algorithms::Costmap::getMutableGrid
Grid & getMutableGrid()
Definition: Costmap.h:71
armarx::navigation::algorithms::Costmap
Definition: Costmap.h:13
armarx::navigation::algorithms::Costmap::Optimum::position
Position position
Definition: Costmap.h:99
armarx::navigation::algorithms::Costmap::Optimum::value
float value
Definition: Costmap.h:97