00001 #ifndef OPENTISSUE_DYNAMICS_PSYS_FORCE_PSYS_GRID_FORCE_FIELD_H 00002 #define OPENTISSUE_DYNAMICS_PSYS_FORCE_PSYS_GRID_FORCE_FIELD_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/grid/grid.h> 00013 #include <OpenTissue/core/containers/grid/util/grid_value_at_point.h> 00014 00015 #include <cassert> 00016 00017 namespace OpenTissue 00018 { 00019 namespace psys 00020 { 00021 00022 template<typename types> 00023 class GridForceField 00024 : public types::force_type 00025 { 00026 public: 00027 00028 typedef typename types::math_types math_types; 00029 typedef typename math_types::real_type real_type; 00030 typedef typename math_types::vector3_type vector3_type; 00031 typedef typename types::system_type system_type; 00032 typedef OpenTissue::grid::Grid<vector3_type,math_types> grid_type; 00033 00034 protected: 00035 00036 grid_type * m_field; 00037 00038 public: 00039 00040 grid_type & field() { return *m_field; } 00041 grid_type const & field() const { return *m_field; } 00042 00043 public: 00044 00045 GridForceField() 00046 : m_field(0) 00047 {} 00048 00049 virtual ~GridForceField(){} 00050 00051 public: 00052 00053 void apply() 00054 { 00055 typedef typename system_type::particle_iterator particle_iterator; 00056 00057 if(!m_field) 00058 return; 00059 00060 particle_iterator p = this->owner()->particle_begin(); 00061 particle_iterator end = this->owner()->particle_end(); 00062 for(;p!=end;++p) 00063 { 00064 if( p->inv_mass() <= 0 ) 00065 continue; 00066 p->force() += OpenTissue::grid::value_at_point(*m_field, p->position() ); 00067 } 00068 } 00069 00070 public: 00071 00072 void init( grid_type const & field ) 00073 { 00074 if(field.empty()) 00075 { 00076 std::cerr << " GridForceField::init(...): field was empty, did you forget to initialize it?" << std::endl; 00077 m_field = 0; 00078 return; 00079 } 00080 m_field = const_cast<grid_type *>( &field ); 00081 } 00082 00083 }; 00084 00085 } // namespace psys 00086 } // namespace OpenTissue 00087 00088 // OPENTISSUE_DYNAMICS_PSYS_FORCE_PSYS_GRID_FORCE_FIELD_H 00089 #endif