Moby
Simulator.h
1 /****************************************************************************
2  * Copyright 2005 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef _SIMULATOR_H
8 #define _SIMULATOR_H
9 
10 #include <sys/times.h>
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <boost/shared_ptr.hpp>
15 
16 #include <Moby/Base.h>
17 #include <Moby/Log.h>
18 #include <Moby/RigidBody.h>
19 #include <Moby/Dissipation.h>
20 #include <Moby/ArticulatedBody.h>
21 
22 namespace osg {
23  class Node;
24  class Group;
25 }
26 
27 namespace Moby {
28 
29 class RigidBody;
30 class ArticulatedBody;
31 class RCArticulatedBody;
32 class VisualizationData;
33 
35 
40 class Simulator : public virtual Base
41 {
42  friend class ConstraintStabilization;
43  friend class ImpactConstraintHandler;
44  friend class RigidBody;
45  friend class RCArticulatedBody;
46 
47  public:
48  Simulator();
49  virtual ~Simulator();
50  virtual double step(double step_size);
51  ControlledBodyPtr find_dynamic_body(const std::string& name) const;
54  void update_visualization();
55  virtual void save_to_xml(XMLTreePtr node, std::list<boost::shared_ptr<const Base> >& shared_objects) const;
56  virtual void load_from_xml(boost::shared_ptr<const XMLTree> node, std::map<std::string, BasePtr>& id_map);
57 
59  double current_time;
60 
62  boost::shared_ptr<Dissipation> dissipator;
63 
65 
69  const std::vector<ControlledBodyPtr>& get_dynamic_bodies() const { return _bodies; }
70 
71  void add_transient_vdata(osg::Node* vdata);
72 
74  osg::Node* get_persistent_vdata() const { return (osg::Node*) _persistent_vdata; }
75 
77  osg::Node* get_transient_vdata() const { return (osg::Node*) _transient_vdata; }
78 
81 
83  double dynamics_time;
84 
86  std::vector<JointPtr> implicit_joints;
87 
88  protected:
89  void apply_impulse(boost::shared_ptr<Ravelin::DynamicBodyd> db, const Ravelin::SharedVectorNd& gj);
90  void solve(const std::vector<boost::shared_ptr<Ravelin::DynamicBodyd> >& island, const std::vector<JointPtr>& island_joints, const Ravelin::VectorNd& v, const Ravelin::VectorNd& f, double dt, Ravelin::VectorNd& a, Ravelin::VectorNd& lambda) const;
91  virtual double check_pairwise_constraint_violations(double t) { return 0.0; }
92  void find_islands(std::vector<std::vector<boost::shared_ptr<Ravelin::DynamicBodyd> > >& islands);
93  unsigned num_generalized_coordinates(const std::vector<boost::shared_ptr<Ravelin::DynamicBodyd> > & island) const;
94  osg::Group* _persistent_vdata;
95  osg::Group* _transient_vdata;
96  void calc_fwd_dyn(double dt);
97  void precalc_fwd_dyn();
98 
100  std::vector<ControlledBodyPtr> _bodies;
101 
103  Ravelin::VectorNd _current_dx;
104 
105  template <class ForwardIterator>
106  double integrate(double step_size, ForwardIterator begin, ForwardIterator end);
107 
109  double integrate(double step_size) { return integrate(step_size, _bodies.begin(), _bodies.end()); }
110 
111  private:
112  static Ravelin::VectorNd& ode(const Ravelin::VectorNd& x, double t, double dt, void* data, Ravelin::VectorNd& dx);
113 }; // end class
114 
115 // include inline functions
116 #include "Simulator.inl"
117 
118 } // end namespace
119 
120 #endif
121 
Abstract class for articulated bodies.
Definition: ArticulatedBody.h:26
Ravelin::VectorNd _current_dx
The derivative at the current time.
Definition: Simulator.h:103
void calc_fwd_dyn(double dt)
Calculates forward dynamics for bodies (does not consider unilateral constraints) ...
Definition: Simulator.cpp:482
boost::shared_ptr< Dissipation > dissipator
The dissipation mechanism for larger time steps.
Definition: Simulator.h:62
void find_islands(std::vector< std::vector< boost::shared_ptr< Ravelin::DynamicBodyd > > > &islands)
Finds islands.
Definition: Simulator.cpp:956
double dynamics_time
User time spent by dynamics on the last step.
Definition: Simulator.h:83
Class from which all Moby classes are derived.
Definition: Base.h:20
Defines an articulated body for use with reduced-coordinate dynamics algorithms.
Definition: RCArticulatedBody.h:43
double current_time
The current simulation time.
Definition: Simulator.h:59
virtual void load_from_xml(boost::shared_ptr< const XMLTree > node, std::map< std::string, BasePtr > &id_map)
Implements Base::load_from_xml()
Definition: Simulator.cpp:817
unsigned num_generalized_coordinates(const std::vector< boost::shared_ptr< Ravelin::DynamicBodyd > > &island) const
Gets the number of generalized coordinates in an island.
Definition: Simulator.cpp:808
Represents a single rigid body.
Definition: RigidBody.h:43
Simulator()
Sets up the simulator.
Definition: Simulator.cpp:41
const std::vector< ControlledBodyPtr > & get_dynamic_bodies() const
Gets the list of dynamic bodies in the simulator.
Definition: Simulator.h:69
boost::shared_ptr< ControlledBody > ControlledBodyPtr
Dynamic body smart pointer.
Definition: Types.h:89
Defines the mechanism for handling impact constraints.
Definition: ImpactConstraintHandler.h:37
void(* post_step_callback_fn)(Simulator *s)
Callback function after a step is completed.
Definition: Simulator.h:80
osg::Node * get_transient_vdata() const
Gets the transient (one-step) visualization data.
Definition: Simulator.h:77
boost::shared_ptr< XMLTree > XMLTreePtr
XML tree smart pointer.
Definition: Types.h:104
void update_visualization()
Updates all visualization under the simulator.
Definition: Simulator.cpp:304
double integrate(double step_size)
Integrates all dynamic bodies.
Definition: Simulator.h:109
void remove_dynamic_body(ControlledBodyPtr body)
Removes a dynamic body from the simulator.
Definition: Simulator.cpp:200
double integrate(double step_size, ForwardIterator begin, ForwardIterator end)
Integrates both position and velocity of rigid _bodies.
Definition: Simulator.h:13
Simulator for both unarticulated and articulated rigid bodies without contact.
Definition: Simulator.h:40
void add_dynamic_body(ControlledBodyPtr body)
Adds a dynamic body to the simulator.
Definition: Simulator.cpp:251
Projected constraint stabilization mechanism.
Definition: ConstraintStabilization.h:21
virtual double step(double step_size)
Steps the Simulator forward in time without contact.
Definition: Simulator.cpp:148
void apply_impulse(boost::shared_ptr< Ravelin::DynamicBodyd > db, const Ravelin::SharedVectorNd &gj)
Applies a generalized impulse to a dynamic body.
Definition: Simulator.cpp:356
virtual void save_to_xml(XMLTreePtr node, std::list< boost::shared_ptr< const Base > > &shared_objects) const
Implements Base::save_to_xml()
Definition: Simulator.cpp:1048
osg::Node * get_persistent_vdata() const
Gets the persistent visualization data.
Definition: Simulator.h:74
ControlledBodyPtr find_dynamic_body(const std::string &name) const
Finds the dynamic body in the simulator, if any.
Definition: Simulator.cpp:173
std::vector< JointPtr > implicit_joints
Set of implicit joints maintained in the simulation (does not include implicit joints belonging to RC...
Definition: Simulator.h:86
std::vector< ControlledBodyPtr > _bodies
The set of bodies in the simulation.
Definition: Simulator.h:100
void add_transient_vdata(osg::Node *vdata)
Adds transient visualization data to the simulator.
Definition: Simulator.cpp:311
void precalc_fwd_dyn()
Prepares to calculate forward dynamics for bodies.
Definition: Simulator.cpp:319