00001 #ifndef OPENTISSUE_DYNAMICS_PSYS_UTIL_DIRECT_MESH_COUPLING_H 00002 #define OPENTISSUE_DYNAMICS_PSYS_UTIL_DIRECT_MESH_COUPLING_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 <map> 00013 00014 namespace OpenTissue 00015 { 00016 namespace psys 00017 { 00018 00019 template< typename types > 00020 class DirectMeshCoupling 00021 { 00022 public: 00023 00024 typedef typename types::math_types math_types; 00025 typedef typename math_types::real_type real_type; 00026 typedef typename math_types::vector3_type vector3_type; 00027 typedef typename types::particle_type particle_type; 00028 00029 public: 00030 00031 typedef typename types::mesh_type mesh_type; 00032 typedef typename mesh_type::vertex_type vertex_type; 00033 00034 typedef std::map<vertex_type*, particle_type*> particle_lut_type; 00035 00036 protected: 00037 00038 particle_lut_type m_particle_lut; 00039 mesh_type * m_mesh; 00040 00041 00042 public: 00043 00044 DirectMeshCoupling() 00045 : m_mesh(0) 00046 {} 00047 00048 public: 00049 00050 mesh_type & mesh() { return *m_mesh; } 00051 mesh_type const & mesh() const { return *m_mesh; } 00052 00053 particle_type & particle( vertex_type const & v ) { return *m_particle_lut[ const_cast<vertex_type*>(&v) ]; } 00054 particle_type const & particle( vertex_type const & v ) const { return *m_particle_lut[ const_cast<vertex_type*>(&v) ]; } 00055 00056 00065 particle_type * operator()( vertex_type const * v ) { return &(particle(*v)); } 00066 00067 00068 public: 00069 00070 bool empty() const { return !m_mesh; } 00071 00072 void clear() 00073 { 00074 m_particle_lut.clear(); 00075 m_mesh = 0; 00076 } 00077 00078 template<typename particle_system_type> 00079 void init(particle_system_type & system, mesh_type & mesh) 00080 { 00081 typedef typename particle_system_type::particle_iterator particle_iterator; 00082 00083 m_particle_lut.clear(); 00084 system.clear(); 00085 m_mesh = &mesh; 00086 00087 typename mesh_type::vertex_iterator begin = mesh.vertex_begin(); 00088 typename mesh_type::vertex_iterator end = mesh.vertex_end(); 00089 typename mesh_type::vertex_iterator v = begin; 00090 00091 for(;v!=end;++v) 00092 system.create_particle( particle_type() ); 00093 00094 particle_iterator p = system.particle_begin(); 00095 for(v=begin;v!=end;++v,++p) 00096 { 00097 p->bind(v->m_coord); 00098 m_particle_lut[ &(*v) ] = &(*p); 00099 } 00100 } 00101 00102 }; 00103 00104 } // namespace psys 00105 } // namespace OpenTissue 00106 00107 // OPENTISSUE_DYNAMICS_PSYS_UTIL_DIRECT_MESH_COUPLING_H 00108 #endif