00001 #ifndef OPENTISSUE_DYNAMICS_MBD_MBD_MATERIAL_LIBRARY_H 00002 #define OPENTISSUE_DYNAMICS_MBD_MBD_MATERIAL_LIBRARY_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 <OpenTissue/core/containers/containers_hash_map.h> 00013 #include <OpenTissue/utility/utility_map_data_iterator.h> 00014 00015 #include <boost/iterator/indirect_iterator.hpp> 00016 00017 namespace OpenTissue 00018 { 00019 namespace mbd 00020 { 00021 00211 template<typename mbd_types> 00212 class MaterialLibrary 00213 { 00214 public: 00215 00216 typedef typename mbd_types::math_policy::real_type real_type; 00217 typedef typename mbd_types::math_policy::index_type index_type; 00218 typedef typename mbd_types::math_policy::index_type size_type; 00219 typedef typename mbd_types::material_type material_type; 00220 00221 protected: 00222 00223 typedef typename stdext::hash_map<index_type, material_type*> material_ptr_lut_type; 00224 typedef OpenTissue::utility::map_data_iterator<typename material_ptr_lut_type::iterator> material_ptr_lut_iterator; 00225 00226 public: 00227 00228 typedef boost::indirect_iterator<material_ptr_lut_iterator> material_iterator; 00229 00230 protected: 00231 00232 material_ptr_lut_type m_storage; 00233 material_type m_default; 00234 00235 public: 00236 00237 MaterialLibrary() 00238 { 00239 add(m_default); 00240 } 00241 00242 virtual ~MaterialLibrary() { clear(); } 00243 00244 public: 00245 00246 void add(material_type const & m) 00247 { 00248 assert(m_storage.find(m.hash_key())==m_storage.end() || !"MaterialLibrary::add(): Material already exist"); 00249 m_storage.insert( std::pair<index_type, material_type*>(m.hash_key(), const_cast<material_type*>(&m) ) ); 00250 } 00251 00252 material_type * get(index_type A, index_type B) 00253 { 00254 typename material_ptr_lut_type::iterator mit = m_storage.find(material_type::hash_key(A,B)); 00255 if(mit==m_storage.end()) 00256 return 0; 00257 return mit->second; 00258 } 00259 00260 void clear() 00261 { 00262 m_storage.clear(); 00263 m_default.clear(); 00264 add(m_default); 00265 } 00266 00267 material_type * default_material() { return &m_default; } 00268 00269 size_type size_materials() const { return m_storage.size(); } 00270 00271 material_iterator material_begin() { return material_iterator( material_ptr_lut_iterator( m_storage.begin() ) );} 00272 material_iterator material_end() { return material_iterator( material_ptr_lut_iterator( m_storage.end() ) );} 00273 00274 }; 00275 00276 } // namespace mbd 00277 } // namespace OpenTissue 00278 // OPENTISSUE_DYNAMICS_MBD_MBD_MATERIAL_LIBRARY_H 00279 #endif