SpringEdgeDataForceField.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_SPRINGEDGEDATAFORCEFIELD_H
00026 #define SOFA_COMPONENT_FORCEFIELD_SPRINGEDGEDATAFORCEFIELD_H
00027 
00028 #include <sofa/core/componentmodel/behavior/ForceField.h>
00029 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00030 #include <sofa/component/topology/EdgeData.h>
00031 #include <sofa/component/topology/EdgeData.inl>
00032 #include <sofa/defaulttype/Vec.h>
00033 #include <vector>
00034 
00035 
00036 namespace sofa
00037 {
00038 
00039 namespace component
00040 {
00041 
00042 namespace forcefield
00043 {
00044 
00045 using namespace sofa::defaulttype;
00046 
00047 //using namespace sofa::core::objectmodel;
00048 
00049 template<class DataTypes>
00050 class SpringEdgeDataForceField : public core::componentmodel::behavior::ForceField<DataTypes>, public  virtual core::objectmodel::BaseObject
00051 {
00052 public:
00053     typedef typename DataTypes::VecCoord VecCoord;
00054     typedef typename DataTypes::VecDeriv VecDeriv;
00055     typedef typename DataTypes::Coord Coord;
00056     typedef typename DataTypes::Deriv Deriv;
00057     typedef typename Coord::value_type Real;
00058     class Mat3 : public fixed_array<Deriv,3>
00059     {
00060     public:
00061         Deriv operator*(const Deriv& v) const
00062         {
00063             return Deriv((*this)[0]*v,(*this)[1]*v,(*this)[2]*v);
00064         }
00065     };
00066 
00067     struct Spring {
00068         Real  ks;           // spring stiffness
00069         Real  kd;           // damping factor 
00070         Real  restLength;   // rest length of the spring 
00071         Mat3  dfdx; // the edge stiffness matrix
00072         
00073         Spring(Real _ks, Real _kd, Real _rl) : ks(_ks), kd(_kd), restLength(_rl) {
00074         }
00075         Spring() : ks(1.0),kd(1.0),restLength(0.0) {
00076         }
00077     };
00078 protected:
00079     double m_potentialEnergy;
00081     bool updateMatrix;
00083     component::topology::EdgeData<Spring> springArray;
00084     core::componentmodel::behavior::MechanicalState<DataTypes>* object;
00086     component::topology::EdgeSetTopology<DataTypes> *topology;
00088     DataField<std::string> m_filename;
00090     DataField<double> m_stiffness;
00092     DataField<double> m_viscosity;
00093 
00094     //void addSpringForce(double& potentialEnergy, VecDeriv& f1, const VecCoord& p1, const VecDeriv& v1, VecDeriv& f2, const VecCoord& p2, const VecDeriv& v2, int i, const Spring& spring);
00095     z   void addSpring(int m1, int m2, SReal ks, SReal kd, SReal initlen);
00096 
00097     void resizeArray(unsigned int n);
00098 
00099 public:
00100     
00101     SpringEdgeDataForceField(core::componentmodel::behavior::MechanicalState<DataTypes>* _object);
00102     
00103     bool load(const char *filename);
00104     
00105     core::componentmodel::behavior::MechanicalState<DataTypes>* getObject() { return object; }
00106 
00107     virtual void init();
00108 
00109     void createDefaultSprings();
00110     
00111     virtual void handleEvent( Event* e );
00112     
00113     virtual void addForce (VecDeriv& f, const VecCoord& x, const VecDeriv& v);
00114 
00115     virtual void addDForce (VecDeriv& df, const VecDeriv& dx);
00116 
00117     virtual double getPotentialEnergy(const VecCoord& ) { return m_potentialEnergy; }
00118 
00119     Real getStiffness() const {
00120         return (Real)(m_stiffness.getValue());
00121     }
00122     const Real getViscosity() const {
00123         return (Real)(m_viscosity.getValue());
00124     }
00125     const component::topology::EdgeData<Spring> &getSpringArray() const{
00126         return springArray;
00127     }
00128     void draw();
00129     
00130     // -- Modifiers
00131     
00132     void clear(int reserve=0)
00133     {
00134         springArray.clear();
00135         if (reserve) springArray.reserve(reserve);
00136     }
00138     class Loader;
00139     friend class Loader;
00140     
00141 };
00142 
00143 
00144 } // namespace forcefield
00145 
00146 } // namespace component
00147 
00148 } // namespace sofa
00149 
00150 #endif

Generated on Sun Nov 23 03:08:18 2008 for Sofa by  doxygen 1.5.7.1