Robowflex  v0.1
Making MoveIt Easy
world.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_DART_WORLD_
4 #define ROBOWFLEX_DART_WORLD_
5 
6 #include <mutex>
7 
8 #include <dart/dynamics/Skeleton.hpp>
9 #include <dart/collision/CollisionFilter.hpp>
10 #include <dart/collision/DistanceFilter.hpp>
11 #include <dart/collision/CollisionGroup.hpp>
12 #include <dart/simulation/World.hpp>
13 
15 
16 namespace robowflex
17 {
18  namespace darts
19  {
20  /** \cond IGNORE */
22  ROBOWFLEX_CLASS_FORWARD(Structure)
23  /** \endcond */
24 
25  /** \brief A wrapper for the ACM to pass into signed distance computation.
26  * Returns based on if the objects would collide.
27  */
28  class DistanceCollisionWrapper : public dart::collision::DistanceFilter
29  {
30  public:
31  /** \brief Constructor.
32  * \param[in] filter Collision filter to wrap.
33  */
35 
36  bool needDistance(const dart::collision::CollisionObject *object1,
37  const dart::collision::CollisionObject *object2) const override;
38 
39  private:
41  };
42 
43  /** \cond IGNORE */
45  /** \endcond */
46 
47  /** \class robowflex::darts::WorldPtr
48  \brief A shared pointer wrapper for robowflex::darts::World. */
49 
50  /** \class robowflex::darts::WorldConstPtr
51  \brief A const shared pointer wrapper for robowflex::darts::World. */
52 
53  /** \brief Wrapper for a dart::simulation::World, which contains a set of skeletons (i.e., Robots and
54  * Structures). The World is the main object that is used by motion planning as it contains a holistic
55  * view of the scene.
56  */
57  class World
58  {
60 
61  public:
62  /** \name Constuctors
63  \{ */
64 
65  /** \brief Create an empty world.
66  * \param[in] name Name for the world.
67  */
68  World(const std::string &name = "world");
69 
70  /** \brief Clone this world.
71  * \param[in] suffix Suffix to append to all names in the world (i.e., robots).
72  */
73  WorldPtr clone(const std::string &suffix = "") const;
74 
75  /** \} */
76 
77  /** \name World Entity Management
78  \{ */
79 
80  /** \brief Add a robot to the world.
81  * \param[in] robot Robot to add.
82  */
83  void addRobot(RobotPtr robot);
84 
85  /** \brief Remove a robot from the world.
86  * \param[in] name Name of robot to remove.
87  */
88  void removeRobot(const std::string &name);
89 
90  /** \brief Remove a robot from the world.
91  * \param[in] robot Robot to remove.
92  */
93  void removeRobot(const RobotPtr &robot);
94 
95  /** \brief Get a robot in the world.
96  * \param[in] name Name of robot to get.
97  * \return The robot, or nullptr if non-existent.
98  */
99  RobotPtr getRobot(const std::string &name);
100 
101  /** \brief Get a robot in the world.
102  * \param[in] name Name of robot to get.
103  * \return The robot, or nullptr if non-existent.
104  */
105  RobotConstPtr getRobotConst(const std::string &name) const;
106 
107  /** \brief Add a structure to the world.
108  * \param[in] structure Structure to add.
109  */
110  void addStructure(StructurePtr structure);
111 
112  /** \brief Remove a structure from the world.
113  * \param[in] name Name of structure to remove.
114  */
115  void removeStructure(const std::string &name);
116 
117  /** \brief Remove a structure from the world.
118  * \param[in] structure Structure to remove.
119  */
120  void removeStructure(const StructurePtr &structure);
121 
122  /** \brief Get a structure in the world.
123  * \param[in] name Name of structure to get.
124  * \return The structure, or nullptr if non-existent.
125  */
126  StructurePtr getStructure(const std::string &name);
127 
128  /** \brief Get a structure in the world.
129  * \param[in] name Name of structure to get.
130  * \return The structure, or nullptr if non-existent.
131  */
132  StructureConstPtr getStructureConst(const std::string &name) const;
133 
134  /** \brief Get the set of structures in the world.
135  * \return A constant reference to the set of structures.
136  */
137  const std::map<std::string, StructurePtr> &getStructures();
138 
139  /** \} */
140 
141  /** \name Getters and Setters
142  \{ */
143 
144  /** \brief Get the name of this world.
145  * \return The name of the world.
146  */
147  const std::string &getName() const;
148 
149  /** \brief Get the bounds on the workspace.
150  * \return A pair of the lower and upper bounds on the workspace.
151  */
152  std::pair<Eigen::Vector3d, Eigen::Vector3d> getWorkspaceBounds() const;
153 
154  /** \brief Get the lower bounds of the workspace.
155  * \return A 3D vector of the lower X-, Y-, and Z-bounds on the workspace.
156  */
157  Eigen::Vector3d &getWorkspaceLow();
158 
159  /** \brief Get the lower bounds of the workspace.
160  * \return A 3D vector of the lower X-, Y-, and Z-bounds on the workspace.
161  */
162  const Eigen::Vector3d &getWorkspaceLowConst() const;
163 
164  /** \brief Get the upper bounds of the workspace.
165  * \return A 3D vector of the upper X-, Y-, and Z-bounds on the workspace.
166  */
167  Eigen::Vector3d &getWorkspaceHigh();
168 
169  /** \brief Get the upper bounds of the workspace.
170  * \return A 3D vector of the upper X-, Y-, and Z-bounds on the workspace.
171  */
172  const Eigen::Vector3d &getWorkspaceHighConst() const;
173 
174  /** \brief Get the Dart world.
175  * \return The underlying Dart world.
176  */
177  dart::simulation::WorldPtr getSim();
178 
179  /** \brief Get the Dart world.
180  * \return The underlying Dart world.
181  */
182  const dart::simulation::WorldPtr &getSimConst() const;
183 
184  /** \brief Get the index in the world of a skeleton.
185  * \param[in] skeleton The skeleton to get the index of.
186  * \return The index of a skeleton in the world.
187  */
188  unsigned int getSkeletonIndex(const dart::dynamics::SkeletonPtr &skeleton) const;
189 
190  /** \} */
191 
192  /** \name Collisions
193  \{ */
194 
195  /** \brief Get a collision filter that allows collisions between all body nodes. Useful for
196  * constructing a custom filter to select one or a few objects.
197  * \return A collision filter that considers no collisions.
198  */
200 
201  /** \brief Get a collision filter that is a copy of the default filter.
202  * \return A copy of the default collision filter.
203  */
205 
206  /** \brief Checks if world is currently in collision.
207  * \param[in] filter Custom collision filter to use. If null, will use default collision filter.
208  * \return True if the world is currently in collision, false otherwise.
209  */
210  bool inCollision(const std::shared_ptr<dart::collision::CollisionFilter> &filter = nullptr) const;
211 
212  /** \brief Gets the current signed distance to collision in the world.
213  * \return The signed distance to collision for the current state of the world.
214  */
215  double distanceToCollision() const;
216 
217  /** \brief Force forward kinematics to update.
218  */
219  void forceUpdate();
220 
221  /** \} */
222 
223  /** \brief Clear all IK modules created in skeletons in the world.
224  */
225  void clearIKModules();
226 
227  /** \brief Grab internal recursive lock for world.
228  */
229  void lock();
230 
231  /** \brief Unlock internal recursive world lock.
232  */
233  void unlock();
234 
235  /** \brief Open the Open Scene Graph visualization for this world.
236  */
237  void openOSGViewer();
238 
239  /** \brief Get the collision detector for this world.
240  * \return A pointer to the world's collision detector.
241  */
242  dart::collision::CollisionDetectorPtr getCollider() const;
243 
244  /** \brief Get the self-collision group for a skeleton in this world.
245  * \param[in] name The name of the skeleton.
246  * \return A pointer to the self-collision group for the skeleton.
247  */
249  getSelfCollisionGroup(const std::string &name) const;
250 
251  /** \brief Get the group for everything other than a given skeleton in this world.
252  * \param[in] name The name of the skeleton.
253  * \return A pointer to the other-collision group for the skeleton.
254  */
256  getOtherCollisionGroup(const std::string &name) const;
257 
258  /** \brief Collision filter information.
259  */
261  {
262  std::shared_ptr<dart::collision::CollisionGroup> self; ///< All nodes belonging to a
263  ///< skeleton.
265  };
266 
267  /** \brief Get the collision info (self and other collision groups) for a skeleton in this
268  * world.
269  * \param[in] name The name of the skeleton.
270  * \return A struct containing pointers to the skeleton's self and other collision groups.
271  */
272  CollisionInfo getCollisionInfo(const std::string &name) const;
273 
274  /** \brief Get the current world collision filter (composite of all skeleton filters).
275  * This is more efficient than constructing a new filter from
276  * robowflex::darts::World::getDefaultFilter() or robowflex::darts::World::getAllValidFilter().
277  * \return A pointer to the current world collision filter.
278  */
280 
281  private:
282  /** \brief Add a new collision filter (ACM) for a skeleton.
283  * \param[in] name Name for collision filter.
284  * \param[in] skeleton Skeleton collision filter is for.
285  */
286  void addSkeletonCollider(const std::string &name, const dart::dynamics::SkeletonPtr &skeleton);
287 
288  /** \brief Remove a collision filter (ACM).
289  * \param[in] name Name for collision filter.
290  * \param[in] skeleton Skeleton collision filter is for.
291  */
292  void removeSkeletonCollider(const std::string &name, const dart::dynamics::SkeletonPtr &skeleton);
293 
294  dart::simulation::WorldPtr world_; ///< Underlying world.
295 
296  Eigen::Vector3d low_{-5, -5, -5}; ///< Lower workspace bounds.
297  Eigen::Vector3d high_{5, 5, 5}; ///< Upper workspace bounds.
298 
301 
302  dart::collision::CollisionGroupPtr all_; ///< All collision groups in world.
303  std::map<std::string, CollisionInfo> collision_; ///< Map of skeleton names to collision
304  ///< filters.
305 
307  ///< filter of skeleton
308  ///< filters.
309  dart::collision::CollisionDetectorPtr collider_; ///< Collision checker.
310  const std::string name_; ///< Name of world.
311 
312  std::recursive_mutex mutex_; ///< Internal lock.
313  };
314  } // namespace darts
315 } // namespace robowflex
316 
317 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
#define ROBOWFLEX_EIGEN
Macro for classes with fixed width Eigen classes.
Definition: class_forward.h:25
A const shared pointer wrapper for robowflex::Robot.
A shared pointer wrapper for robowflex::Robot.
A wrapper for the ACM to pass into signed distance computation. Returns based on if the objects would...
Definition: world.h:29
std::shared_ptr< dart::collision::CollisionFilter > filter_
Wrapped collision filter.
Definition: world.h:40
A const shared pointer wrapper for robowflex::darts::Structure.
A shared pointer wrapper for robowflex::darts::Structure.
Definition: structure.h:34
A shared pointer wrapper for robowflex::darts::World.
Wrapper for a dart::simulation::World, which contains a set of skeletons (i.e., Robots and Structures...
Definition: world.h:58
dart::simulation::WorldPtr world_
Underlying world.
Definition: world.h:294
std::map< std::string, RobotPtr > robots_
Robots in world.
Definition: world.h:299
std::recursive_mutex mutex_
Internal lock.
Definition: world.h:312
dart::collision::CollisionDetectorPtr collider_
Collision checker.
Definition: world.h:309
std::map< std::string, CollisionInfo > collision_
Definition: world.h:303
dart::collision::CollisionDetectorPtr getCollider() const
Get the collision detector for this world.
const std::string name_
Name of world.
Definition: world.h:310
std::shared_ptr< dart::collision::CompositeCollisionFilter > filter_
Definition: world.h:306
std::map< std::string, StructurePtr > structures_
Structures in world.
Definition: world.h:300
dart::collision::CollisionGroupPtr all_
All collision groups in world.
Definition: world.h:302
Functions for loading and animating robots in Blender.
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
Collision filter information.
Definition: world.h:261
std::shared_ptr< dart::collision::CollisionGroup > others
All other nodes in world.
Definition: world.h:264