SpringForceField.h

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 // Author: François Faure, INRIA-UJF, (C) 2006
00026 //
00027 // Copyright: See COPYING file that comes with this distribution
00028 #ifndef SOFA_COMPONENT_FORCEFIELD_SPRINGFORCEFIELD_H
00029 #define SOFA_COMPONENT_FORCEFIELD_SPRINGFORCEFIELD_H
00030 
00031 #include <sofa/core/componentmodel/behavior/PairInteractionForceField.h>
00032 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00033 #include <sofa/defaulttype/Vec.h>
00034 #include <sofa/helper/vector.h>
00035 
00036 
00037 namespace sofa
00038 {
00039 
00040 namespace component
00041 {
00042 
00043 namespace forcefield
00044 {
00045 
00046 using namespace sofa::defaulttype;
00047 
00049 template<class DataTypes>
00050 class SpringForceFieldInternalData
00051 {
00052 public:
00053 };
00054 
00056 template<class DataTypes>
00057 class SpringForceField : public core::componentmodel::behavior::PairInteractionForceField<DataTypes>, public virtual core::objectmodel::BaseObject
00058 {
00059 public:
00060     typedef typename core::componentmodel::behavior::PairInteractionForceField<DataTypes> Inherit;
00061     typedef typename DataTypes::VecCoord VecCoord;
00062     typedef typename DataTypes::VecDeriv VecDeriv;
00063     typedef typename DataTypes::Coord Coord;
00064     typedef typename DataTypes::Deriv Deriv;
00065     typedef typename Coord::value_type Real;
00066     typedef core::componentmodel::behavior::MechanicalState<DataTypes> MechanicalState;
00067     
00068     class Spring
00069     {
00070     public:
00071         int     m1, m2;  
00072         SReal  ks;      
00073         SReal  kd;      
00074         SReal  initpos; 
00075 
00076         Spring(int m1=0, int m2=0, SReal ks=0.0, SReal kd=0.0, SReal initpos=0.0)
00077         : m1(m1), m2(m2), ks(ks), kd(kd), initpos(initpos)
00078         {
00079         }
00080 
00081         inline friend std::istream& operator >> ( std::istream& in, Spring& s ){
00082             in>>s.m1>>s.m2>>s.ks>>s.kd>>s.initpos;
00083             return in;
00084         }
00085 
00086         inline friend std::ostream& operator << ( std::ostream& out, const Spring& s ){
00087             out<<s.m1<<" "<<s.m2<<" "<<s.ks<<" "<<s.kd<<" "<<s.initpos<<"\n";
00088             return out;
00089         }
00090       
00091     };
00092 protected:
00093 
00094     SReal m_potentialEnergy;
00095     Data<SReal> ks;
00096     Data<SReal> kd; 
00097     Data<sofa::helper::vector<Spring> > springs;
00098     class Loader;
00099 
00100     SpringForceFieldInternalData<DataTypes> data;
00101     friend class SpringForceFieldInternalData<DataTypes>;
00102 
00103     void addSpringForce(SReal& potentialEnergy, VecDeriv& f1, const VecCoord& p1, const VecDeriv& v1, VecDeriv& f2, const VecCoord& p2, const VecDeriv& v2, int i, const Spring& spring);
00104 
00105 public:
00106     SpringForceField(MechanicalState* object1, MechanicalState* object2, SReal _ks=100.0, SReal _kd=5.0);   
00107     SpringForceField(SReal _ks=100.0, SReal _kd=5.0);
00108 
00109     virtual void parse(core::objectmodel::BaseObjectDescription* arg);
00110 
00111     bool load(const char *filename);
00112 
00113     core::componentmodel::behavior::MechanicalState<DataTypes>* getObject1() { return this->mstate1; }
00114     core::componentmodel::behavior::MechanicalState<DataTypes>* getObject2() { return this->mstate2; }
00115 
00116     virtual void init();
00117 
00118     virtual void addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2);
00119     virtual void addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2);
00120     virtual double getPotentialEnergy(const VecCoord&, const VecCoord&) { return m_potentialEnergy; }
00121     SReal getStiffness() { return ks.getValue(); }
00122     SReal getDamping(){ return kd.getValue(); } 
00123     void setStiffness(SReal _ks){ ks.setValue(_ks); }
00124     void setDamping(SReal _kd){ kd.setValue(_kd); }
00125 
00126     void draw();
00127 
00128     // -- Modifiers
00129 
00130     void clear(int reserve=0)
00131     {
00132         sofa::helper::vector<Spring>& springs = *this->springs.beginEdit();
00133         springs.clear();
00134         if (reserve) springs.reserve(reserve);
00135             this->springs.endEdit();
00136     }
00137 
00138     void addSpring(int m1, int m2, SReal ks, SReal kd, SReal initlen)
00139     {
00140         springs.beginEdit()->push_back(Spring(m1,m2,ks,kd,initlen));
00141         springs.endEdit();
00142     }
00143 };
00144 
00145 } // namespace forcefield
00146 
00147 } // namespace component
00148 
00149 } // namespace sofa
00150 
00151 #endif

Generated on Sun Nov 23 03:09:30 2008 for Sofa by  doxygen 1.5.7.1