DART  6.7.3
World.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * This code incorporates portions of Open Dynamics Engine
19  * (Copyright (c) 2001-2004, Russell L. Smith. All rights
20  * reserved.) and portions of FCL (Copyright (c) 2011, Willow
21  * Garage, Inc. All rights reserved.), which were released under
22  * the same BSD license as below
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef DART_SIMULATION_WORLD_HPP_
40 #define DART_SIMULATION_WORLD_HPP_
41 
42 #include <string>
43 #include <vector>
44 #include <set>
45 
46 #include <Eigen/Dense>
47 
48 #include "dart/common/Timer.hpp"
51 #include "dart/common/Subject.hpp"
58 
59 namespace dart {
60 
61 namespace integration {
62 class Integrator;
63 } // namespace integration
64 
65 namespace dynamics {
66 class Skeleton;
67 } // namespace dynamics
68 
69 namespace constraint {
70 class ConstraintSolver;
71 } // namespace constraint
72 
73 namespace collision {
74 class CollisionResult;
75 } // namespace collision
76 
77 namespace simulation {
78 
80 
81 class World : public virtual common::Subject
83 {
84 public:
85 
87  = common::Signal<void(const std::string& _oldName,
88  const std::string& _newName)>;
89 
91  template <typename... Args>
92  static WorldPtr create(Args&&... args);
93 
94  //--------------------------------------------------------------------------
95  // Constructor and Destructor
96  //--------------------------------------------------------------------------
97 
99  static std::shared_ptr<World> create(const std::string& name = "world");
100 
102  World(const std::string& _name = "world");
103 
105  virtual ~World();
106 
112  std::shared_ptr<World> clone() const;
113 
114  //--------------------------------------------------------------------------
115  // Properties
116  //--------------------------------------------------------------------------
117 
119  const std::string& setName(const std::string& _newName);
120 
122  const std::string& getName() const;
123 
125  void setGravity(const Eigen::Vector3d& _gravity);
126 
128  const Eigen::Vector3d& getGravity() const;
129 
131  void setTimeStep(double _timeStep);
132 
134  double getTimeStep() const;
135 
136  //--------------------------------------------------------------------------
137  // Structural Properties
138  //--------------------------------------------------------------------------
139 
141  dynamics::SkeletonPtr getSkeleton(std::size_t _index) const;
142 
146  dynamics::SkeletonPtr getSkeleton(const std::string& _name) const;
147 
149  std::size_t getNumSkeletons() const;
150 
152  std::string addSkeleton(const dynamics::SkeletonPtr& _skeleton);
153 
155  void removeSkeleton(const dynamics::SkeletonPtr& _skeleton);
156 
159  std::set<dynamics::SkeletonPtr> removeAllSkeletons();
160 
162  bool hasSkeleton(const dynamics::ConstSkeletonPtr& skeleton) const;
163 
165  int getIndex(int _index) const;
166 
168  dynamics::SimpleFramePtr getSimpleFrame(std::size_t _index) const;
169 
171  dynamics::SimpleFramePtr getSimpleFrame(const std::string& _name) const;
172 
174  std::size_t getNumSimpleFrames() const;
175 
177  std::string addSimpleFrame(const dynamics::SimpleFramePtr& _frame);
178 
180  void removeSimpleFrame(const dynamics::SimpleFramePtr& _frame);
181 
184  std::set<dynamics::SimpleFramePtr> removeAllSimpleFrames();
185 
186  //--------------------------------------------------------------------------
187  // Collision checking
188  //--------------------------------------------------------------------------
189 
191  DART_DEPRECATED(6.0)
192  bool checkCollision(bool checkAllCollisions);
193 
199  bool checkCollision(
200  const collision::CollisionOption& option
201  = collision::CollisionOption(false, 1u, nullptr),
202  collision::CollisionResult* result = nullptr);
203 
208  const collision::CollisionResult& getLastCollisionResult() const;
209 
210  //--------------------------------------------------------------------------
211  // Simulation
212  //--------------------------------------------------------------------------
213 
215  void reset();
216 
220  void step(bool _resetCommand = true);
221 
223  void setTime(double _time);
224 
226  double getTime() const;
227 
232  int getSimFrames() const;
233 
234  //--------------------------------------------------------------------------
235  // Constraint
236  //--------------------------------------------------------------------------
237 
239  void setConstraintSolver(constraint::UniqueConstraintSolverPtr solver);
240 
242  constraint::ConstraintSolver* getConstraintSolver() const;
243 
245  void bake();
246 
249 
250 protected:
251 
254  const dynamics::ConstMetaSkeletonPtr& _skeleton);
255 
257  void handleSimpleFrameNameChange(const dynamics::Entity* _entity);
258 
260  std::string mName;
261 
263  std::vector<dynamics::SkeletonPtr> mSkeletons;
264 
265  std::map<dynamics::ConstMetaSkeletonPtr,
267 
270  std::vector<common::Connection> mNameConnectionsForSkeletons;
271 
273  dart::common::NameManager<dynamics::SkeletonPtr> mNameMgrForSkeletons;
274 
276  std::vector<dynamics::SimpleFramePtr> mSimpleFrames;
277 
280  std::vector<common::Connection> mNameConnectionsForSimpleFrames;
281 
283  std::map<const dynamics::SimpleFrame*, dynamics::SimpleFramePtr> mSimpleFrameToShared;
284 
286  dart::common::NameManager<dynamics::SimpleFramePtr> mNameMgrForSimpleFrames;
287 
292  std::vector<int> mIndices;
293 
295  Eigen::Vector3d mGravity;
296 
298  double mTimeStep;
299 
301  double mTime;
302 
304  int mFrame;
305 
307  std::unique_ptr<constraint::ConstraintSolver> mConstraintSolver;
308 
311 
312  //--------------------------------------------------------------------------
313  // Signals
314  //--------------------------------------------------------------------------
316 
317 public:
318  //--------------------------------------------------------------------------
319  // Slot registers
320  //--------------------------------------------------------------------------
321  common::SlotRegister<NameChangedSignal> onNameChanged;
322 
323 };
324 
325 } // namespace simulation
326 } // namespace dart
327 
328 #include "dart/simulation/detail/World-impl.hpp"
329 
330 #endif // DART_SIMULATION_WORLD_HPP_
#define DART_DEPRECATED(version)
Definition: Deprecated.hpp:51
const CollisionOption & option
Collision option of DART.
Definition: FCLCollisionDetector.cpp:154
CollisionResult * result
Collision result of DART.
Definition: FCLCollisionDetector.cpp:157
std::string * name
Definition: SkelParser.cpp:1642
class Recording
Definition: Recording.hpp:58
virtual ~World()
Destructor.
Definition: World.cpp:78
std::size_t getNumSkeletons() const
Get the number of skeletons.
Definition: World.cpp:279
std::map< dynamics::ConstMetaSkeletonPtr, dynamics::SkeletonPtr > mMapForSkeletons
Definition: World.hpp:266
std::unique_ptr< constraint::ConstraintSolver > mConstraintSolver
Constraint solver.
Definition: World.hpp:307
int getIndex(int _index) const
Get the dof index for the indexed skeleton.
Definition: World.cpp:402
const std::string & getName() const
Get the name of this World.
Definition: World.cpp:241
double getTimeStep() const
Get time step.
Definition: World.cpp:147
void handleSimpleFrameNameChange(const dynamics::Entity *_entity)
Register when a SimpleFrame's name is changed.
Definition: World.cpp:634
std::string mName
Name of this World.
Definition: World.hpp:260
dart::common::NameManager< dynamics::SkeletonPtr > mNameMgrForSkeletons
NameManager for keeping track of Skeletons.
Definition: World.hpp:273
std::map< const dynamics::SimpleFrame *, dynamics::SimpleFramePtr > mSimpleFrameToShared
Map from raw SimpleFrame pointers to their shared_ptrs.
Definition: World.hpp:283
void setTime(double _time)
Set current time.
Definition: World.cpp:204
std::set< dynamics::SkeletonPtr > removeAllSkeletons()
Remove all the skeletons in this world, and return a set of shared pointers to them,...
Definition: World.cpp:381
void step(bool _resetCommand=true)
Calculate the dynamics and integrate the world for one step.
Definition: World.cpp:162
void setConstraintSolver(constraint::UniqueConstraintSolverPtr solver)
Sets the constraint solver.
Definition: World.cpp:532
Eigen::Vector3d mGravity
Gravity.
Definition: World.hpp:295
std::vector< dynamics::SkeletonPtr > mSkeletons
Skeletons in this world.
Definition: World.hpp:263
void removeSkeleton(const dynamics::SkeletonPtr &_skeleton)
Remove a skeleton from this world.
Definition: World.cpp:326
common::SlotRegister< NameChangedSignal > onNameChanged
Definition: World.hpp:321
const collision::CollisionResult & getLastCollisionResult() const
Return the collision checking result of the last simulation step.
Definition: World.cpp:526
std::size_t getNumSimpleFrames() const
Get the number of Entities.
Definition: World.cpp:423
std::vector< dynamics::SimpleFramePtr > mSimpleFrames
Entities in this world.
Definition: World.hpp:276
void setTimeStep(double _timeStep)
Set time step.
Definition: World.cpp:132
double mTimeStep
Simulation time step.
Definition: World.hpp:298
std::vector< int > mIndices
The first indeices of each skeleton's dof in mDofs.
Definition: World.hpp:292
void bake()
Bake simulated current state and store it into mRecording.
Definition: World.cpp:554
std::string addSimpleFrame(const dynamics::SimpleFramePtr &_frame)
Add an Entity to this world.
Definition: World.cpp:429
void removeSimpleFrame(const dynamics::SimpleFramePtr &_frame)
Remove a SimpleFrame from this world.
Definition: World.cpp:461
int getSimFrames() const
Get the number of simulated frames.
Definition: World.cpp:216
const std::string & setName(const std::string &_newName)
Set the name of this World.
Definition: World.cpp:222
void reset()
Reset the time, frame counter and recorded histories.
Definition: World.cpp:153
std::vector< common::Connection > mNameConnectionsForSimpleFrames
Connections for noticing changes in Frame names TODO(MXG): Consider putting this functionality into N...
Definition: World.hpp:280
int mFrame
Current simulation frame number.
Definition: World.hpp:304
NameChangedSignal mNameChangedSignal
Definition: World.hpp:315
bool checkCollision(bool checkAllCollisions)
Deprecated. Please use checkCollision(~) instead.
Definition: World.cpp:506
const Eigen::Vector3d & getGravity() const
Get gravity.
Definition: World.cpp:258
void setGravity(const Eigen::Vector3d &_gravity)
Set gravity.
Definition: World.cpp:247
std::shared_ptr< World > clone() const
Create a clone of this World.
Definition: World.cpp:90
World(const std::string &_name="world")
Constructor.
Definition: World.cpp:61
void handleSkeletonNameChange(const dynamics::ConstMetaSkeletonPtr &_skeleton)
Register when a Skeleton's name is changed.
Definition: World.cpp:584
dynamics::SkeletonPtr getSkeleton(std::size_t _index) const
Get the indexed skeleton.
Definition: World.cpp:264
std::set< dynamics::SimpleFramePtr > removeAllSimpleFrames()
Remove all SimpleFrames in this world, and return a set of shared pointers to them,...
Definition: World.cpp:492
dart::common::NameManager< dynamics::SimpleFramePtr > mNameMgrForSimpleFrames
NameManager for keeping track of Entities.
Definition: World.hpp:286
std::string addSkeleton(const dynamics::SkeletonPtr &_skeleton)
Add a skeleton to this world.
Definition: World.cpp:285
bool hasSkeleton(const dynamics::ConstSkeletonPtr &skeleton) const
Returns wether this World contains a Skeleton.
Definition: World.cpp:395
Recording * getRecording()
Get recording.
Definition: World.cpp:578
std::vector< common::Connection > mNameConnectionsForSkeletons
Connections for noticing changes in Skeleton names TODO(MXG): Consider putting this functionality int...
Definition: World.hpp:270
static WorldPtr create(Args &&... args)
Creates World as shared_ptr.
Definition: World-impl.hpp:49
Recording * mRecording
Definition: World.hpp:310
constraint::ConstraintSolver * getConstraintSolver() const
Get the constraint solver.
Definition: World.cpp:548
double getTime() const
Get current time.
Definition: World.cpp:210
dynamics::SimpleFramePtr getSimpleFrame(std::size_t _index) const
Get the indexed Entity.
Definition: World.cpp:408
double mTime
Current simulation time.
Definition: World.hpp:301
#define DART_COMMON_DECLARE_SHARED_WEAK(X)
Definition: SmartPointer.hpp:41
Definition: Random-impl.hpp:92
::fcl::CollisionResult CollisionResult
Definition: BackwardCompatibility.hpp:157
std::unique_ptr< ConstraintSolver > UniqueConstraintSolverPtr
Definition: SmartPointer.hpp:41
std::shared_ptr< const Skeleton > ConstSkeletonPtr
Definition: SmartPointer.hpp:60
std::shared_ptr< const MetaSkeleton > ConstMetaSkeletonPtr
Definition: SmartPointer.hpp:68
std::shared_ptr< SimpleFrame > SimpleFramePtr
Definition: SmartPointer.hpp:53
std::shared_ptr< Skeleton > SkeletonPtr
Definition: SmartPointer.hpp:60
std::shared_ptr< World > WorldPtr
Definition: SmartPointer.hpp:41
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43