EdgePressureForceField.inl

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #include <sofa/component/forcefield/EdgePressureForceField.h>
00026 #include <sofa/component/topology/EdgeSubsetData.inl>
00027 #include <sofa/helper/gl/template.h>
00028 #include <vector>
00029 #include <set>
00030 
00031 #ifdef _WIN32
00032 #include <windows.h>
00033 #endif
00034 
00035 // #define DEBUG_TRIANGLEFEM
00036 
00037 namespace sofa
00038 {
00039 
00040 namespace component
00041 {
00042 
00043 namespace forcefield
00044 {
00045 
00046 using namespace sofa::defaulttype;
00047 using namespace core::componentmodel::topology;
00048 
00049 using std::cerr;
00050 using std::cout;
00051 using std::endl;
00052 
00053 template <class DataTypes> EdgePressureForceField<DataTypes>::~EdgePressureForceField()
00054 {
00055 }
00056 // Handle topological changes
00057 template <class DataTypes> void  EdgePressureForceField<DataTypes>::handleTopologyChange()
00058 {   
00059     std::list<const TopologyChange *>::const_iterator itBegin=_topology->firstChange();
00060     std::list<const TopologyChange *>::const_iterator itEnd=_topology->lastChange();
00061 
00062 
00063     edgePressureMap.handleTopologyEvents(itBegin,itEnd,_topology->getNbEdges());
00064 
00065 }
00066 template <class DataTypes> void EdgePressureForceField<DataTypes>::init()
00067 {
00068     //std::cerr << "initializing EdgePressureForceField" << std::endl;
00069     this->core::componentmodel::behavior::ForceField<DataTypes>::init();
00070         
00071     _topology = this->getContext()->getMeshTopology();
00072     this->getContext()->get(edgeGeo);
00073 
00074     assert(edgeGeo!=0);
00075 
00076     if (edgeGeo==NULL)
00077     {
00078         std::cerr << "ERROR(EdgePressureForceField): object must have an EdgeSetTopology.\n";
00079         return;
00080     }
00081 
00082     if (dmin.getValue()!=dmax.getValue()) {
00083         selectEdgesAlongPlane();
00084     }
00085     if (edgeList.getValue().length()>0) {
00086         selectEdgesFromString();
00087     }
00088 
00089     initEdgeInformation();
00090 
00091 }
00092 
00093 
00094 template <class DataTypes> 
00095 void EdgePressureForceField<DataTypes>::addForce(VecDeriv& f, const VecCoord& /*x*/, const VecDeriv& /*v*/)
00096 {
00097     Deriv force;
00098 
00099     typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;
00100 
00101     for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00102     {
00103         force=(*it).second.force/2;
00104         f[_topology->getEdge((*it).first)[0]]+=force;
00105         f[_topology->getEdge((*it).first)[1]]+=force;
00106 
00107     }
00108 }
00109 
00110 template <class DataTypes> 
00111     double EdgePressureForceField<DataTypes>::getPotentialEnergy(const VecCoord& /*x*/)
00112 {
00113     cerr<<"EdgePressureForceField::getPotentialEnergy-not-implemented !!!"<<endl;
00114     return 0;
00115 }
00116 
00117 template<class DataTypes>
00118 void EdgePressureForceField<DataTypes>::initEdgeInformation()
00119 {   
00120     typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;
00121 
00122     for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00123     {
00124       (*it).second.length=edgeGeo->computeRestEdgeLength((*it).first);
00125       (*it).second.force=pressure.getValue()*(*it).second.length;
00126     }
00127 }
00128 
00129 
00130 template<class DataTypes>
00131 void EdgePressureForceField<DataTypes>::updateEdgeInformation()
00132 {
00133     typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;
00134 
00135     for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00136     {
00137         (*it).second.force=((*it).second.length)*pressure.getValue();
00138     }
00139 }
00140 
00141 
00142 template <class DataTypes>
00143 void EdgePressureForceField<DataTypes>::selectEdgesAlongPlane()
00144 {
00145     const VecCoord& x = *this->mstate->getX0();
00146     std::vector<bool> vArray;
00147     unsigned int i;
00148 
00149     vArray.resize(x.size());
00150 
00151     for( i=0; i<x.size();++i)
00152     {
00153         vArray[i]=isPointInPlane(x[i]);
00154     }   
00155 
00156     for (int n=0;n<_topology->getNbEdges();++n) 
00157     {
00158         if ((vArray[_topology->getEdge(n)[0]]) && (vArray[_topology->getEdge(n)[1]])) 
00159         {
00160             // insert a dummy element : computation of pressure done later
00161             EdgePressureInformation t;
00162             edgePressureMap[n]=t;
00163         }
00164     }
00165 }
00166 
00167 template <class DataTypes>
00168 void EdgePressureForceField<DataTypes>::selectEdgesFromString()
00169 {
00170     std::string inputString=edgeList.getValue();
00171     unsigned int i;
00172     do {
00173         const char *str=inputString.c_str();
00174         for(i=0;(i<inputString.length())&&(str[i]!=',');++i) ;
00175         EdgePressureInformation t;
00176 
00177         if (i==inputString.length()) {
00178             edgePressureMap[(unsigned int)atoi(str)]=t;
00179             inputString+=i;
00180         } else {
00181             inputString[i]='\0';
00182             edgePressureMap[(unsigned int)atoi(str)]=t;
00183             inputString+=i+1;
00184         }
00185     } while (inputString.length()>0);
00186 
00187 }
00188 template<class DataTypes>
00189 void EdgePressureForceField<DataTypes>::draw()
00190 {
00191     if (!getContext()->getShowForceFields()) return;
00192     if (!this->mstate) return;
00193 
00194     if (getContext()->getShowWireFrame())
00195         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00196 
00197 
00198     const VecCoord& x = *this->mstate->getX();
00199 
00200     glDisable(GL_LIGHTING);
00201 
00202     glBegin(GL_LINES);
00203     glColor4f(0,1,0,1);
00204     
00205     typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;    
00206 
00207     for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00208     {
00209         helper::gl::glVertexT(x[_topology->getEdge((*it).first)[0]]);
00210         helper::gl::glVertexT(x[_topology->getEdge((*it).first)[1]]);
00211     }
00212     glEnd();
00213 
00214 
00215     if (getContext()->getShowWireFrame())
00216         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00217 }
00218 
00219 } // namespace forcefield
00220 
00221 } // namespace component
00222 
00223 } // namespace sofa

Generated on Sun Nov 23 02:55:10 2008 for Sofa by  doxygen 1.5.7.1