CostmapBuilder.h
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2022
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
24#include <functional>
25#include <memory>
26#include <optional>
27#include <string>
28#include <variant>
29#include <vector>
30
31#include <Eigen/Core>
32
33#include <SimoxUtility/meta/enum/EnumNames.hpp>
34#include <VirtualRobot/VirtualRobot.h>
35
39
40#if COSTMAP_BUILDER_SIMOX_CONTROL
41#include <simox/control/environment/CollisionRobot.h>
42#include <simox/control/robot/RobotInterface.h>
43
44#include <hpp/fcl/broadphase/broadphase_collision_manager.h>
45#include <hpp/fcl/collision_object.h>
46#endif
47
49{
50
52 {
53 public:
59 const static simox::meta::EnumNames<DistanceCalculator> DistanceCalculatorNames;
60
62 {
64 int numThreads = 0; // 0 to let OpenMP decide how many threads to use
65
66 bool restrictToRooms = false;
67 /*!
68 * Only applicable when restrictToRooms is true.
69 * The list of rooms for which the costmap should be generated, leave empty to
70 * include all rooms.
71 */
72 std::vector<std::string> roomEnableList;
73
74 /*!
75 * Robot footprint radius in mm. Only applicable when restrictToRooms is true.
76 * When >= 0, cells closer than this distance to a room boundary are excluded.
77 * -1 disables the footprint restriction.
78 */
80
81 /*!
82 * The maximum distance from the costmap to an obstacle to be included for costmap
83 * calculation. Every obstacles further away will be discarded.
84 */
85 float maxFilterDistance = 1000.F;
86
87 Eigen::Vector3f collisionModelScaleFactor = Eigen::Vector3f::Ones();
88
89 /// All poses that are further away to obstacles are set to this value
90 float maxDistance = 2000.F;
91
92 bool fakeModeForTesting = false;
93 };
94
96 const VirtualRobot::SceneObjectSetPtr& obstacles,
97 const std::vector<VirtualRobot::RobotPtr>& articulatedObjects,
98 const std::vector<Room>& rooms,
99 const Costmap::Parameters& parameters,
100 const std::string& robotCollisonModelName,
101 const CostmapBuilderParams& builderParameters);
102
103 Costmap create(const SceneBounds& init = SceneBounds());
105
106
107 static Eigen::MatrixXf createUniformGrid(const SceneBounds& sceneBounds,
108 const Costmap::Parameters& parameters);
109
110
111 static void updateMask(Costmap& costmap);
112
113 private:
114#if COSTMAP_BUILDER_SIMOX_CONTROL
115 struct CollisionSetupSC
116 {
117 std::unique_ptr<simox::control::robot::RobotInterface> robot;
118 std::unique_ptr<simox::control::environment::CollisionRobot<hpp::fcl::OBBRSS>>
119 collisionRobot;
120
121 /*!
122 * Thread-local wrappers around the shared obstacle geometry. Copying a
123 * `CollisionObject` only duplicates its transform and AABB; the (expensive) BVH
124 * geometry is shared via `shared_ptr` and is const during queries.
125 *
126 * Obstacles are static, so these are never updated after `setup()`.
127 */
128 std::vector<std::unique_ptr<hpp::fcl::CollisionObject>> obstacleColObjects;
129 std::unique_ptr<hpp::fcl::BroadPhaseCollisionManager> obstacleCollisionManager;
130 };
131#else
132 struct CollisionSetupSC
133 {
134 // not supported without SimoxControl
135 };
136#endif
137 struct CollisionSetupSx
138 {
139 VirtualRobot::RobotPtr collisionRobot;
140 VirtualRobot::CollisionModelPtr robotCollisionModel;
141 VirtualRobot::SceneObjectSetPtr filteredObstacles;
142 };
143
144 using CollisionSetup = std::variant<std::monostate, CollisionSetupSC, CollisionSetupSx>;
145
146 float computeCost(const Costmap::Position& position,
147 const CollisionSetup& collisionSetupVariant);
148
149 void fillGridCosts(Costmap& costmap);
150 void extendGridCosts(Costmap& costmap);
151
152 // fn: collisionRobot, robotCollisionModel, mask, costs, index -> costs
153 // expects mask calculation based on costs after this function
154 void applyFnToCostmap(Costmap& costmap,
155 const std::function<float(const CollisionSetup& collisionSetup,
156 const std::optional<float>,
157 const float,
158 const Costmap::Index&)>& fn);
159
160 VirtualRobot::SceneObjectSetPtr filterObjectsForCostmap(const Costmap& costmap);
161
162#if COSTMAP_BUILDER_SIMOX_CONTROL
163 using SharedObstacleColObjects = std::vector<std::shared_ptr<hpp::fcl::CollisionObject>>;
164
165 /*!
166 * Converts all obstacles into hpp-fcl collision objects.
167 *
168 * Must be called exactly once, serially. The conversion reads VirtualRobot state that is
169 * shared between the source scene objects (`RobotFactory::createRobot` hands the
170 * `CollisionModel` over by pointer instead of cloning it), so running it per-thread
171 * inside the OpenMP region races and yields displaced obstacle geometry.
172 */
173 SharedObstacleColObjects buildSharedObstacleCollisionObjects() const;
174#endif
175
176 void initializeEmptyMask(Costmap& costmap);
177
178 const VirtualRobot::RobotPtr robot;
179 const VirtualRobot::SceneObjectSetPtr obstacles;
180 const std::vector<VirtualRobot::RobotPtr> articulatedObjects;
181 std::vector<Room> rooms;
182 const Costmap::Parameters parameters;
183 const std::string robotCollisionModelName;
184
185 const CostmapBuilderParams builderParameters;
186 };
187
188} // namespace armarx::navigation::algorithms
static const simox::meta::EnumNames< DistanceCalculator > DistanceCalculatorNames
static Eigen::MatrixXf createUniformGrid(const SceneBounds &sceneBounds, const Costmap::Parameters &parameters)
CostmapBuilder(const VirtualRobot::RobotPtr &robot, const VirtualRobot::SceneObjectSetPtr &obstacles, const std::vector< VirtualRobot::RobotPtr > &articulatedObjects, const std::vector< Room > &rooms, const Costmap::Parameters &parameters, const std::string &robotCollisonModelName, const CostmapBuilderParams &builderParameters)
Costmap create(const SceneBounds &init=SceneBounds())
Brief description of class rooms.
Definition rooms.h:39
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file is part of ArmarX.
float maxDistance
All poses that are further away to obstacles are set to this value.