00001 #ifndef OPENTISSUE_DYNAMICS_MBD_MBD_SIMULATOR_INTERFACE_H 00002 #define OPENTISSUE_DYNAMICS_MBD_MBD_SIMULATOR_INTERFACE_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 #include <OpenTissue/dynamics/mbd/mbd_compute_scripted_motions.h> 00012 00013 namespace OpenTissue 00014 { 00015 namespace mbd 00016 { 00023 template <typename mbd_types> 00024 class SimulatorInterface 00025 { 00026 public: 00027 00028 typedef mbd_types types; 00029 00030 protected: 00031 00032 typedef typename mbd_types::math_policy math_policy; 00033 typedef typename math_policy::real_type real_type; 00034 typedef typename math_policy::value_traits value_traits; 00035 00036 typedef typename mbd_types::configuration_type configuration_type; 00037 typedef typename mbd_types::sleepy_policy sleepy_policy; 00038 typedef typename mbd_types::stepper_policy stepper_policy; 00039 typedef typename mbd_types::collision_detection_policy collision_detection; 00040 00041 private: 00042 00043 real_type m_time; 00044 collision_detection m_collision_detection; 00045 configuration_type * m_configuration; 00046 sleepy_policy m_sleepy; 00047 stepper_policy m_stepper; 00048 00049 public: 00050 00051 stepper_policy * get_stepper() { return &m_stepper; } 00052 stepper_policy const * get_stepper() const { return &m_stepper; } 00053 sleepy_policy * get_sleepy() { return &m_sleepy; } 00054 sleepy_policy const * get_sleepy() const { return &m_sleepy; } 00055 00056 collision_detection const * get_collision_detection() const 00057 { 00058 assert(m_collision_detection || !"SimulatorInterface::get_collision_detection(): null pointer, did you forget to invoke init?"); 00059 return m_collision_detection; 00060 } 00061 00062 configuration_type const * get_configuration() const 00063 { 00064 assert(m_configuration || !"SimulatorInterface::get_configuration(): null pointer, did you forget to invoke init?"); 00065 return m_configuration; 00066 } 00067 00068 collision_detection * get_collision_detection() { return &m_collision_detection; } 00069 00070 configuration_type * get_configuration() 00071 { 00072 assert(m_configuration || !"SimulatorInterface::get_configuration(): null pointer, did you forget to invoke init?"); 00073 return m_configuration; 00074 } 00075 00076 public: 00077 00078 SimulatorInterface( ) 00079 : m_time( value_traits::zero() ) 00080 , m_configuration(0) 00081 {} 00082 00083 virtual ~SimulatorInterface(){} 00084 00085 public: 00086 00087 virtual void run(real_type const & time_step) = 0; 00088 00089 void init(configuration_type & configuration) 00090 { 00091 this->reset_time(); 00092 this->m_configuration = &configuration; 00093 m_collision_detection.clear(); 00094 m_collision_detection.init(configuration); 00095 configuration.connect(m_collision_detection); 00096 m_stepper.connect(configuration); 00097 mbd::compute_scripted_motions(*(this->m_configuration->get_all_body_group()),this->time()); 00098 } 00099 00100 void clear() 00101 { 00102 this->reset_time(); 00103 this->m_configuration = 0; 00104 m_collision_detection.clear(); 00105 m_stepper.clear(); 00106 m_sleepy.clear(); 00107 } 00108 00109 public: 00110 00111 real_type const & time( ) const { return m_time; } 00112 00113 void reset_time() { m_time = value_traits::zero(); } 00114 00115 protected: 00116 00117 void update_time( real_type const & time_step ) 00118 { 00119 assert( time_step >= value_traits::zero() || !"SimulatorInterface::update_time(): time step value must be non-negative"); 00120 m_time += time_step; 00121 } 00122 00123 }; 00124 00125 } // namespace mbd 00126 } // namespace OpenTissue 00127 // OPENTISSUE_DYNAMICS_MBD_MBD_SIMULATOR_INTERFACE_H 00128 #endif