Costmap.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <functional>
5#include <optional>
6#include <utility>
7#include <vector>
8
9#include <Eigen/Core>
10
13
15{
16
17 class Costmap
18 {
19 public:
20 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
21
22 friend class CostmapBuilder;
24
26 {
27 // if set to false, distance to obstacles will be computed and not only a binary collision check
28 bool binaryGrid{false};
29
30 /// How big each cell is in the uniform grid.
31 float cellSize = 100.f; // [mm]
32
33 // the robot radius used to calculate this costmap
34 // e.g. for each cell, the distance to the nearest object is it's value + robotRadius
35 float robotRadius = 0.F;
36
37 float sceneBoundsMargin = 1000.f; // [mm]
38 };
39
40 struct Filter
41 {
42 Eigen::MatrixXf matrix;
43 bool useMinimum = false;
44
45 inline Filter&
47 {
49 return *this;
50 }
51
52 static Filter gaussian(int radius = 2, float sigma = 1.);
53 };
54
55 using Index = Eigen::Array2i;
56 using Position = Eigen::Vector2f;
57 using Grid = Eigen::MatrixXf;
58
59 // if 0, the cell is invalid
61
62 Costmap(const Grid& grid,
63 const Parameters& parameters,
64 const SceneBounds& sceneBounds,
65 const std::optional<Mask>& mask = std::nullopt,
66 const core::Pose2D& origin = core::Pose2D::Identity());
67
68 /**
69 * @brief Create a costmap with the same dimensions and parameters as the given one. Only the content (grid and mask) are altered.
70 *
71 * @param other The costmap providing the dimensions and parameters
72 * @param grid
73 * @param mask
74 * @return Costmap
75 */
76 static Costmap WithSameDimsAs(const Costmap& other,
77 const Grid& grid,
78 const std::optional<Mask>& mask = std::nullopt);
79
80 struct Vertex
81 {
82 Index index; // row corresponds to y; column corresponds to x
84 };
85
86 Position toPositionLocal(const Index& index) const;
88 std::pair<int, int> toIndexUnsanitized(const Eigen::Vector2f& globalPosition) const;
89 std::optional<Costmap::Vertex>
90 toVertexOrInvalid(const Eigen::Vector2f& globalPosition) const;
91 Vertex toVertex(const Position& globalPosition) const;
92
93 const Grid& getGrid() const;
94
95 Grid&
97 {
98 return grid;
99 }
100
101 bool isInCollision(const Position& p) const;
102
103 const Parameters& params() const noexcept;
104
105 // get the scene bounds in the costmap's base frame (aka 'origin')
106 const SceneBounds& getLocalSceneBounds() const noexcept;
107
108 core::Pose2D
110 {
111 const Eigen::Vector2f costmap_P_center{
112 (getLocalSceneBounds().max.x() - getLocalSceneBounds().min.x()) / 2 +
114 (getLocalSceneBounds().max.y() - getLocalSceneBounds().min.y()) / 2 +
116
117 return global_T_costmap * Eigen::Translation2f(costmap_P_center);
118 }
119
120 struct Optimum
121 {
122 float value;
125 };
126
127 Optimum optimum() const;
128
129 void filter(const Filter& filter);
130
131 bool add(const Costmap& other, float weight = 1.0);
132
133 enum class AddMode
134 {
138 };
139
140 bool addCostmapWithSameSizeAndPose(const Costmap& other, AddMode mode);
141 bool add(const Costmap& other, AddMode mode, bool keepThis = false);
142
143 //! checks whether the cell is masked out
144 bool isValid(const Index& index) const noexcept;
145
146 const std::optional<Mask>& getMask() const noexcept;
147
148 std::optional<Costmap::Mask>& getMutableMask() noexcept;
149
150
151 std::optional<float> value(const Index& index) const;
152
153 std::optional<float> value(const Position& position) const;
154
156
157 void forEachCell(const std::function<void(const Index&)>& f) const;
158 void forEachCellParallel(const std::function<void(const Index&)>& f) const;
159
160 /**
161 * @brief Merge a list of costmaps into this costmap
162 *
163 * @param costmaps list of other costmaps
164 * @param weights [weights of other costmaps..., weights of this costmap]
165 * @return the merged costmap
166 */
167 Costmap mergeInto(const std::vector<Costmap>& costmaps,
168 const std::vector<float>& weights) const;
169
170 const core::Pose2D&
171 origin() const
172 {
173 return global_T_costmap;
174 }
175
176 void
177 setOrigin(const core::Pose2D& globalPose)
178 {
179 global_T_costmap = globalPose;
180 }
181
182 std::size_t numberOfValidElements() const;
183
184 void excludeUnreachable(const Position& position);
185
186 std::pair<Index, Index> getValidBoundingBox() const;
187
188 void validateSizes() const;
189
190 /**
191 * @brief Find the closest collision-free position to a given position.
192 *
193 * Searches in expanding circles up to maxDistance for the closest position
194 * that is not in collision (grid value > 0).
195 *
196 * @param position The starting position to search from
197 * @param maxDistance Maximum search radius [mm]
198 * @return std::optional<Vertex> The closest collision-free vertex, or nullopt if none found
199 */
200 std::optional<Vertex>
201 findClosestCollisionFreeVertex(const Position& position, float maxDistance) const;
202
203 private:
204 Index findClosestValidIndex(const Index& index, int radius = 10);
205
206 Grid grid;
207 std::optional<Mask> mask;
208
209 SceneBounds sceneBounds;
210
211 const Parameters parameters;
212 core::Pose2D global_T_costmap = core::Pose2D::Identity();
213 };
214
215
216} // namespace armarx::navigation::algorithms
uint8_t index
std::optional< Vertex > findClosestCollisionFreeVertex(const Position &position, float maxDistance) const
Find the closest collision-free position to a given position.
Definition Costmap.cpp:860
Costmap mergeInto(const std::vector< Costmap > &costmaps, const std::vector< float > &weights) const
Merge a list of costmaps into this costmap.
Definition Costmap.cpp:596
bool addCostmapWithSameSizeAndPose(const Costmap &other, AddMode mode)
Definition Costmap.cpp:321
const core::Pose2D & origin() const
Definition Costmap.h:171
static Costmap WithSameDimsAs(const Costmap &other, const Grid &grid, const std::optional< Mask > &mask=std::nullopt)
Create a costmap with the same dimensions and parameters as the given one.
Definition Costmap.cpp:854
const SceneBounds & getLocalSceneBounds() const noexcept
Definition Costmap.cpp:165
bool isInCollision(const Position &p) const
Definition Costmap.cpp:314
Position toPositionLocal(const Index &index) const
Definition Costmap.cpp:61
std::pair< Index, Index > getValidBoundingBox() const
Definition Costmap.cpp:715
Vertex toVertex(const Position &globalPosition) const
Definition Costmap.cpp:146
void excludeUnreachable(const Position &position)
Definition Costmap.cpp:748
std::size_t numberOfValidElements() const
Definition Costmap.cpp:671
const Parameters & params() const noexcept
Definition Costmap.cpp:302
Eigen::Matrix< bool, Eigen::Dynamic, Eigen::Dynamic > Mask
Definition Costmap.h:60
std::optional< Costmap::Mask > & getMutableMask() noexcept
Definition Costmap.cpp:535
Position toPositionGlobal(const Index &index) const
Definition Costmap.cpp:75
const std::optional< Mask > & getMask() const noexcept
Definition Costmap.cpp:529
std::optional< Costmap::Vertex > toVertexOrInvalid(const Eigen::Vector2f &globalPosition) const
Definition Costmap.cpp:132
void filter(const Filter &filter)
Definition Costmap.cpp:244
void forEachCellParallel(const std::function< void(const Index &)> &f) const
Definition Costmap.cpp:231
void setOrigin(const core::Pose2D &globalPose)
Definition Costmap.h:177
bool isValid(const Index &index) const noexcept
checks whether the cell is masked out
Definition Costmap.cpp:82
bool add(const Costmap &other, float weight=1.0)
Definition Costmap.cpp:376
std::pair< int, int > toIndexUnsanitized(const Eigen::Vector2f &globalPosition) const
Definition Costmap.cpp:116
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:31
void forEachCell(const std::function< void(const Index &)> &f) const
Definition Costmap.cpp:219
std::optional< float > value(const Index &index) const
Definition Costmap.cpp:541
This file is part of ArmarX.
This file is part of ArmarX.
Eigen::Isometry2f Pose2D
Definition basic_types.h:34
static Filter gaussian(int radius=2, float sigma=1.)
Definition Costmap.cpp:654
float cellSize
How big each cell is in the uniform grid.
Definition Costmap.h:31