TriangularBendingSprings.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 //
00026 // C++ Interface: TriangularBendingSprings
00027 //
00028 // Description: 
00029 //
00030 //
00031 // Author: The SOFA team </www.sofa-framework.org>, (C) 2007
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 #ifndef SOFA_COMPONENT_FORCEFIELD_TRIANGULARBENDINGSPRINGS_H
00037 #define SOFA_COMPONENT_FORCEFIELD_TRIANGULARBENDINGSPRINGS_H
00038 
00039 #if !defined(__GNUC__) || (__GNUC__ > 3 || (_GNUC__ == 3 && __GNUC_MINOR__ > 3))
00040 #pragma once
00041 #endif
00042 
00043 //#include <sofa/component/forcefield/StiffSpringForceField.h>
00044 #include <sofa/component/MechanicalObject.h>
00045 #include <map>
00046 
00047 #include <sofa/core/componentmodel/behavior/ForceField.h> // or "BaseForceField.h" ?
00048 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00049 #include <sofa/defaulttype/Vec.h>
00050 #include <sofa/defaulttype/Mat.h>
00051 
00052 #include <sofa/defaulttype/Mat.h>
00053 #include <sofa/component/topology/EdgeData.h>
00054 
00055 namespace sofa
00056 {
00057 
00058 namespace component
00059 {
00060 
00061 namespace forcefield
00062 {
00063 using namespace sofa::helper;
00064 using namespace sofa::defaulttype;
00065 using namespace sofa::component::topology;
00066 
00074 template<class DataTypes>
00075 class TriangularBendingSprings : public core::componentmodel::behavior::ForceField<DataTypes>, public virtual core::objectmodel::BaseObject
00076 {
00077 public:
00078   typedef core::componentmodel::behavior::ForceField<DataTypes> Inherited;
00079   //typedef typename DataTypes::Real Real;
00080   typedef typename DataTypes::VecCoord VecCoord;
00081 
00082   
00083     typedef typename DataTypes::VecDeriv VecDeriv;
00084     typedef typename DataTypes::Coord Coord;
00085     typedef typename DataTypes::Deriv Deriv;
00086     typedef typename Coord::value_type Real;
00087     //typedef core::componentmodel::behavior::MechanicalState<DataTypes> MechanicalState;
00088 
00089   enum { N=Coord::static_size };
00090   typedef defaulttype::Mat<N,N,Real> Mat;
00091 
00092     class Mat3 : public fixed_array<Deriv,3>
00093     {
00094     public:
00095         Deriv operator*(const Deriv& v)
00096         {
00097             return Deriv((*this)[0]*v,(*this)[1]*v,(*this)[2]*v);
00098         }
00099         Deriv transposeMultiply(const Deriv& v) 
00100         {
00101             return Deriv(v[0]*((*this)[0])[0]+v[1]*((*this)[1])[0]+v[2]*((*this)[2][0]),
00102                 v[0]*((*this)[0][1])+v[1]*((*this)[1][1])+v[2]*((*this)[2][1]),
00103                 v[0]*((*this)[0][2])+v[1]*((*this)[1][2])+v[2]*((*this)[2][2]));
00104         }
00105     };
00106 
00107     protected: 
00108 
00109     //Data<double> ks;
00110     //Data<double> kd;
00111 
00112   class EdgeInformation
00113     {
00114     public:
00115         Mat3 DfDx; 
00116 
00117         int     m1, m2;  
00118 
00119         double  ks;      
00120         double  kd;      
00121 
00122         double  restlength; 
00123 
00124         bool is_activated;
00125 
00126         bool is_initialized;
00127 
00128         EdgeInformation(int m1=0, int m2=0, /* double ks=getKs(), double kd=getKd(), */ double restlength=0.0, bool is_activated=false, bool is_initialized=false) 
00129             : m1(m1), m2(m2), /* ks(ks), kd(kd), */ restlength(restlength), is_activated(is_activated), is_initialized(is_initialized)
00130         {
00131         }
00132     };
00133 
00134     EdgeData<EdgeInformation> edgeInfo;
00135         
00136     sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00137 
00138     bool updateMatrix;
00139 
00140     Data<double> f_ks;
00141     Data<double> f_kd;
00142 
00143 public:
00144     
00145   TriangularBendingSprings(/*double _ks, double _kd*/); //component::MechanicalObject<DataTypes>* m_dof = NULL);
00146   //TriangularBendingSprings(); //MechanicalState<DataTypes> *mm1 = NULL, MechanicalState<DataTypes> *mm2 = NULL);
00147 
00148   virtual ~TriangularBendingSprings();
00149 
00150   virtual double getPotentialEnergy(const VecCoord& x);
00151     
00153     virtual void init();
00154      
00155     virtual void addForce(VecDeriv& f, const VecCoord& x, const VecDeriv& v);
00156     virtual void addDForce(VecDeriv& df, const VecDeriv& dx);
00157 
00158     virtual double getKs() const { return f_ks.getValue();}
00159     virtual double getKd() const { return f_kd.getValue();}
00160 
00161     void setKs(const double ks) {
00162         f_ks.setValue((double)ks);
00163     }
00164     void setKd(const double kd) {
00165         f_kd.setValue((double)kd);
00166     }
00167 
00168     // handle topological changes
00169     virtual void handleTopologyChange();
00170 
00171     void draw();
00172 
00173 protected:
00174 
00175     EdgeData<EdgeInformation> &getEdgeInfo() {return edgeInfo;}
00176 
00177     static void TriangularBSEdgeCreationFunction(int edgeIndex, void* param, 
00178         EdgeInformation &ei,
00179         const Edge& ,  const sofa::helper::vector< unsigned int > &,
00180         const sofa::helper::vector< double >&);
00181 
00182     static void TriangularBSTriangleCreationFunction(const sofa::helper::vector<unsigned int> &triangleAdded, 
00183         void* param, vector<EdgeInformation> &edgeData);
00184 
00185     static void TriangularBSTriangleDestructionFunction ( const sofa::helper::vector<unsigned int> &triangleRemoved, 
00186         void* param, vector<EdgeInformation> &edgeData);
00187 
00188     double m_potentialEnergy;
00189 
00190   //public:
00191     //Data<double> ks;
00192     //Data<double> kd;
00193 
00194 };
00195 
00196 } // namespace forcefield
00197 
00198 } // namespace component
00199 
00200 } // namespace sofa
00201 
00202 #endif

Generated on Sun Nov 23 03:17:13 2008 for Sofa by  doxygen 1.5.7.1