Costmap3D.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5#include <optional>
6#include <string>
7#include <utility>
8#include <vector>
9
10#include <Eigen/Core>
11
15
17{
18 namespace smoothing
19 {
20 class Costmap3DWrapper;
21 } // namespace smoothing
22
24 {
25 public:
26 friend class Costmap3DBuilder;
28
30 {
31 // if set to false, distance to obstacles will be computed and not only a binary collision check
32 bool binaryGrid{false};
33
34 /// How big each cell is in the uniform grid.
35 float cellSize = 100.F; // [mm]
36
37 /// How many orientations of the robot each cell contains
38 int orientations = 72; // 360 deg / 72 = 5 deg discretization
39
40 float sceneBoundsMargin = 1000.F; // [mm]
41
43 {
44 std::string rootNode = "";
45 std::string packageName = "";
46 std::string relativePath = "";
48 };
49
50 using Index = Eigen::Array2i;
51 using Position = Eigen::Vector2f;
52
53 // Simpler Grid version, but probably much slower than storing the orientations together
54 // each vector element corresponds to one orientation
55 using Grid = std::vector<Eigen::MatrixXf>;
56
57 using RotationIndex = int;
59
60 // if 0, the cell is invalid
62
63 Costmap3D(const Grid& grid,
64 const Parameters& parameters,
65 const SceneBounds& sceneBounds,
66 const std::optional<Mask>& mask = std::nullopt,
67 const core::Pose2D& origin = core::Pose2D::Identity());
68
69 /**
70 * @brief Create a Costmap3D with the same dimensions and parameters as the given one. Only the content (grid and mask) are altered.
71 *
72 * @param other The Costmap3D providing the dimensions and parameters
73 * @param grid
74 * @param mask
75 * @return Costmap3D
76 */
77 static Costmap3D WithSameDimsAs(const Costmap3D& other,
78 const Grid& grid,
79 const std::optional<Mask>& mask = std::nullopt);
80
81 struct Vertex
82 {
83 Index index; // row corresponds to y; column corresponds to x
85 };
86
87 Position toPositionLocal(const Index& index) const;
89 Vertex toVertex(const Position& globalPosition) const;
90
96
98
100
101 Eigen::Rotation2Df
102 rotationLocal(const RotationDegrees degrees) const
103 {
104 return Eigen::Rotation2Df{degrees * static_cast<float>(M_PI) / 180.F};
105 }
106
108 globalPose(const Index& index, const RotationDegrees& rotationDeg) const
109 {
110 Eigen::Rotation2Df rot{rotationDeg * static_cast<float>(M_PI) / 180.F};
111 return global_T_Costmap3D * Eigen::Translation2f(toPositionLocal(index)) * rot;
112 }
113
115 rotationDegrees(const core::Pose2D& pose) const
116 {
117 return Eigen::Rotation2Df{pose.linear()}.angle() * 180.F / M_PI;
118 }
119
120 constexpr static Rotation ALL_ORIENTATIONS = {.index = -1, .degrees = -1};
121
122 const Grid& getGrid() const;
123
124 Grid&
126 {
127 return grid;
128 }
129
130 bool isInCollision(const Position& p,
131 std::optional<Rotation> rotation = std::nullopt) const;
132
133 const Parameters& params() const noexcept;
134
135 // get the scene bounds in the Costmap3D's base frame (aka 'origin')
136 const SceneBounds& getLocalSceneBounds() const noexcept;
137
138 core::Pose2D
139 centerPose(std::optional<Rotation> rotation = std::nullopt) const
140 {
141 const Eigen::Vector2f Costmap3D_P_center{
142 (getLocalSceneBounds().max.x() - getLocalSceneBounds().min.x()) / 2 +
144 (getLocalSceneBounds().max.y() - getLocalSceneBounds().min.y()) / 2 +
146
147 auto rotationDeg = 0.F;
148 if (rotation.has_value())
149 {
150 rotationDeg = rotation->degrees;
151 }
152 Eigen::Rotation2Df rot{rotationDeg * static_cast<float>(M_PI) / 180.F};
153
154 // TODO: Debug
155 return global_T_Costmap3D * Eigen::Translation2f(Costmap3D_P_center) * rot;
156 }
157
158 struct Optimum
159 {
160 float value;
163 };
164
165 Optimum optimum(Rotation rotation = ALL_ORIENTATIONS) const;
166
167 //! checks whether the cell is masked out
168 bool isValid(const Index& index) const noexcept;
169
170 bool isWithinRange(const Index& index) const noexcept;
171
172 bool isMaskedOut(const Index& index) const noexcept;
173
174 const std::optional<Mask>& getMask() const noexcept;
175
176 std::optional<Costmap3D::Mask>& getMutableMask() noexcept;
177
178 float value_ignore_mask(const Index& index, const RotationIndex rot_index) const;
179
180 std::optional<float> value(const Index& index, const RotationIndex rot_index) const;
181
182 std::optional<float> value(const Position& position,
183 const RotationDegrees rotation_degrees) const;
184
186
187 const core::Pose2D&
188 origin() const
189 {
190 return global_T_Costmap3D;
191 }
192
193 void
195 {
196 global_T_Costmap3D = globalPose;
197 }
198
199 std::size_t numberOfValidElements() const;
200
201 std::pair<Index, Index> getValidBoundingBox() const;
202
203 Index getSize() const;
204
206 bool initializeMask = false) const;
207
208 void convertToPseudoSDF();
210
211 private:
212 void validateSizes() const;
213
214 Index findClosestValidIndex(const Index& index);
215
216 Grid grid;
217 std::optional<Mask> mask;
218
219 SceneBounds sceneBounds;
220
221 const Parameters parameters;
222
223 core::Pose2D global_T_Costmap3D = core::Pose2D::Identity();
224 };
225
226
227} // namespace armarx::navigation::algorithms::orientation_aware
#define float
Definition 16_Level.h:22
uint8_t index
#define M_PI
Definition MathTools.h:17
Costmap costmapForOrientation(RotationIndex rotationIndex, bool initializeMask=false) const
bool isWithinRange(const Index &index) const noexcept
const SceneBounds & getLocalSceneBounds() const noexcept
static Costmap3D WithSameDimsAs(const Costmap3D &other, const Grid &grid, const std::optional< Mask > &mask=std::nullopt)
Create a Costmap3D with the same dimensions and parameters as the given one.
Costmap3D(const Grid &grid, const Parameters &parameters, const SceneBounds &sceneBounds, const std::optional< Mask > &mask=std::nullopt, const core::Pose2D &origin=core::Pose2D::Identity())
Definition Costmap3D.cpp:27
core::Pose2D globalPose(const Index &index, const RotationDegrees &rotationDeg) const
Definition Costmap3D.h:108
bool isInCollision(const Position &p, std::optional< Rotation > rotation=std::nullopt) const
const std::optional< Mask > & getMask() const noexcept
Eigen::Rotation2Df rotationLocal(const RotationDegrees degrees) const
Definition Costmap3D.h:102
bool isMaskedOut(const Index &index) const noexcept
Eigen::Matrix< bool, Eigen::Dynamic, Eigen::Dynamic > Mask
Definition Costmap3D.h:61
Rotation rotationFromIndex(RotationIndex index) const
Rotation closestRotationFromDegrees(RotationDegrees degrees) const
std::optional< Costmap3D::Mask > & getMutableMask() noexcept
std::optional< float > value(const Index &index, const RotationIndex rot_index) const
RotationDegrees rotationDegrees(const core::Pose2D &pose) const
Definition Costmap3D.h:115
Vertex toVertex(const Position &globalPosition) const
core::Pose2D centerPose(std::optional< Rotation > rotation=std::nullopt) const
Definition Costmap3D.h:139
bool isValid(const Index &index) const noexcept
checks whether the cell is masked out
Definition Costmap3D.cpp:87
Optimum optimum(Rotation rotation=ALL_ORIENTATIONS) const
float value_ignore_mask(const Index &index, const RotationIndex rot_index) const
This file is part of ArmarX.
Eigen::Isometry2f Pose2D
Definition basic_types.h:34
int orientations
How many orientations of the robot each cell contains.
Definition Costmap3D.h:38
float cellSize
How big each cell is in the uniform grid.
Definition Costmap3D.h:35
struct armarx::navigation::algorithms::orientation_aware::Costmap3D::Parameters::RobotModel robotModel