00001 #ifndef OPENTISSUE_DYNAMICS_SPH_SPH_EMITTER_H 00002 #define OPENTISSUE_DYNAMICS_SPH_SPH_EMITTER_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 00012 #include <vector> 00013 00014 namespace OpenTissue 00015 { 00016 namespace sph 00017 { 00018 00022 template< typename Types > 00023 class Emitter 00024 { 00025 public: 00026 00027 typedef typename Types::vector vector; 00028 typedef typename Types::particle particle; 00029 typedef typename Types::particle_container particle_container; 00030 typedef typename Types::particle_ptr_container particle_ptr_container; 00031 00032 public: 00036 Emitter() 00037 : m_running(true) // auto start 00038 , m_rate(1) 00039 , m_currate(0) 00040 , m_batch(1) 00041 , m_particles(0) 00042 {} 00043 00047 virtual ~Emitter() 00048 {} 00049 00050 public: 00051 00057 bool active() const 00058 { 00059 return m_particles > 0; 00060 } 00061 00067 bool running() const 00068 { 00069 return m_running; 00070 } 00071 00075 size_t & rate() 00076 { 00077 return m_rate; 00078 } 00079 00083 const size_t& rate() const 00084 { 00085 return m_rate; 00086 } 00087 00091 size_t& batch() 00092 { 00093 return m_batch; 00094 } 00095 00099 const size_t& batch() const 00100 { 00101 return m_batch; 00102 } 00103 00104 virtual const vector& center() const = 0; 00105 00112 bool initialize(typename particle_container::iterator begin, typename particle_container::iterator end) 00113 { 00114 m_pars.clear(); 00115 m_particles = 0; 00116 for (typename particle_container::iterator par = begin; par != end; ++par) { 00117 par->fixed() = true; 00118 m_pars.push_back(&*par); 00119 ++m_particles; 00120 } 00121 return init(); 00122 } 00123 00129 void start() 00130 { 00131 m_running = true; 00132 } 00133 00139 bool execute() 00140 { 00141 if (!m_running || !m_particles) 00142 return false; 00143 00144 if (++m_currate < m_rate) 00145 return true; 00146 00147 const size_t new_particles = m_particles < m_batch ? 0 : m_particles - m_batch; 00148 typename particle_ptr_container::iterator par = m_pars.begin()+(m_pars.size()-m_particles); 00149 typename particle_ptr_container::iterator end = new_particles ? par+m_batch : m_pars.end(); 00150 for (; par != end; ++par) 00151 (*par)->fixed() = false; 00152 00153 m_currate = 0; 00154 m_particles = new_particles; 00155 00156 return true; 00157 } 00158 00164 void stop() 00165 { 00166 m_running = false; 00167 } 00168 00169 private: 00174 virtual bool init() = 0; 00175 00176 protected: 00177 bool m_running; 00178 size_t m_rate; 00179 size_t m_currate; 00180 size_t m_batch; 00181 size_t m_particles; 00182 particle_ptr_container m_pars; 00183 00184 }; // End class Emitter 00185 00186 } // namespace sph 00187 } // namespace OpenTissue 00188 00189 // OPENTISSUE_DYNAMICS_SPH_SPH_EMITTER_H 00190 #endif