VectorSpringForceField.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 #ifndef SOFA_COMPONENT_FORCEFIELD_VECTORSPRINGFORCEFIELD_H
00026 #define SOFA_COMPONENT_FORCEFIELD_VECTORSPRINGFORCEFIELD_H
00027 
00028 #include <sofa/component/forcefield/SpringForceField.h>
00029 #include <sofa/core/componentmodel/behavior/ForceField.h>
00030 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00031 #include <sofa/defaulttype/Vec.h>
00032 #include <sofa/defaulttype/Mat.h>
00033 #include <sofa/component/topology/EdgeData.h>
00034 #include <sofa/component/topology/TopologyChangedEvent.h>
00035 
00036 #include <sofa/component/topology/EdgeSetGeometryAlgorithms.h>
00037 #include <sofa/component/topology/EdgeSetTopologyModifier.h>
00038 
00039 
00040 namespace sofa
00041 {
00042 
00043 namespace component
00044 {
00045 
00046 namespace forcefield
00047 {
00048 
00049 using namespace sofa::defaulttype;
00050 using sofa::core::objectmodel::Event;
00051 
00052 template<class DataTypes>
00053 class VectorSpringForceField
00054 : public core::componentmodel::behavior::PairInteractionForceField<DataTypes>, public virtual core::objectmodel::BaseObject
00055 //: public core::componentmodel::behavior::ForceField<DataTypes>
00056 {
00057 public:
00058     typedef typename core::componentmodel::behavior::PairInteractionForceField<DataTypes> Inherit;
00059     typedef typename DataTypes::VecCoord VecCoord;
00060     typedef typename DataTypes::VecDeriv VecDeriv;
00061     typedef typename DataTypes::Coord Coord;
00062     typedef typename DataTypes::Deriv Deriv;
00063     typedef typename Coord::value_type Real;
00064     typedef core::componentmodel::behavior::MechanicalState<DataTypes> MechanicalState;
00065     enum { N=Coord::static_size };
00066 
00067     struct Spring
00068     {
00069         Real  ks;          
00070         Real  kd;          
00071         Deriv restVector;  
00072 
00073         Spring(Real _ks, Real _kd, Deriv _rl) : ks(_ks), kd(_kd), restVector(_rl)
00074         {
00075         }
00076         Spring() : ks(1.0), kd(1.0)
00077         {
00078         }
00079     };
00080 protected:
00081 
00082     double m_potentialEnergy;
00084     bool useTopology;
00085     
00087     sofa::helper::vector<topology::Edge> edgeArray;
00089     sofa::component::topology::EdgeData<Spring> springArray;
00090 
00092     Data<std::string> m_filename;
00094     Data<double> m_stiffness;
00096     Data<double> m_viscosity;
00097 
00098     void resizeArray(unsigned int n);
00099 
00100     static void springCreationFunction(int /*index*/,
00101         void* param, Spring& t,
00102         const topology::Edge& e,
00103         const sofa::helper::vector< unsigned int > &ancestors,
00104         const sofa::helper::vector< double >& coefs);
00105 
00106 public:
00107 
00108     sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00109     sofa::component::topology::EdgeSetTopologyContainer* edgeCont; 
00110     sofa::component::topology::EdgeSetGeometryAlgorithms<DataTypes>* edgeGeo; 
00111     sofa::component::topology::EdgeSetTopologyModifier* edgeMod; 
00112 
00113     VectorSpringForceField(MechanicalState* _object=NULL);
00114 
00115     VectorSpringForceField(MechanicalState* _object1, MechanicalState* _object2);
00116 
00117     bool load(const char *filename);
00118 
00119     core::componentmodel::behavior::MechanicalState<DataTypes>* getObject1() { return this->mstate1; }
00120     core::componentmodel::behavior::MechanicalState<DataTypes>* getObject2() { return this->mstate2; }
00121 
00122     virtual void init();
00123 
00124     void createDefaultSprings();
00125 
00126     virtual void handleEvent( Event* e );
00127 
00128     virtual void addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2);
00129     //virtual void addForce (VecDeriv& f, const VecCoord& x, const VecDeriv& v);
00130 
00131     virtual void addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2, double kFactor, double bFactor);
00132     //virtual void addDForce (VecDeriv& df, const VecDeriv& dx);
00133 
00134     //virtual double getPotentialEnergy(const VecCoord& )
00135     virtual double getPotentialEnergy(const VecCoord&, const VecCoord&)
00136     { return m_potentialEnergy; }
00137 
00138     Real getStiffness() const
00139     {
00140         return (Real)(m_stiffness.getValue());
00141     }
00142     const Real getViscosity() const
00143     {
00144         return (Real)(m_viscosity.getValue());
00145     }
00146     const topology::EdgeData<Spring>& getSpringArray() const
00147     {
00148         return springArray;
00149     }
00150 
00151     void draw();
00152 
00153     // -- Modifiers
00154 
00155     void clear(int reserve=0)
00156     {
00157         springArray.clear();
00158         if (reserve) springArray.reserve(reserve);
00159     }
00160 
00161     void addSpring(int m1, int m2, SReal ks, SReal kd, Coord restVector);
00162 
00164     class Loader;
00165     friend class Loader;
00166 
00167 };
00168 
00169 } // namespace forcefield
00170 
00171 } // namespace component
00172 
00173 } // namespace sofa
00174 
00175 #endif

Generated on Sun Nov 23 03:22:00 2008 for Sofa by  doxygen 1.5.7.1