QuadularBendingSprings.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_QUADULARBENDINGSPRINGS_H
00026 #define SOFA_COMPONENT_FORCEFIELD_QUADULARBENDINGSPRINGS_H
00027 
00028 #include <sofa/component/MechanicalObject.h>
00029 #include <map>
00030 #include <set>
00031 
00032 #include <sofa/core/componentmodel/behavior/ForceField.h> // or "BaseForceField.h" ?
00033 #include <sofa/core/VisualModel.h>
00034 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00035 #include <sofa/defaulttype/Vec.h>
00036 #include <sofa/defaulttype/Mat.h>
00037 #include <sofa/component/topology/QuadData.h>
00038 #include <sofa/component/topology/EdgeData.h>
00039 
00040 #include <sofa/defaulttype/Mat.h>
00041 #include <sofa/helper/fixed_array.h>
00042 
00043 namespace sofa
00044 {
00045 
00046 namespace component
00047 {
00048 
00049 namespace forcefield
00050 {
00051 
00052 using namespace sofa::helper;
00053 using namespace sofa::defaulttype;
00054 using namespace sofa::component::topology;
00055 
00060 template<class DataTypes>
00061 class QuadularBendingSprings : public core::componentmodel::behavior::ForceField<DataTypes>
00062 {
00063 public:
00064     typedef core::componentmodel::behavior::ForceField<DataTypes> Inherited;
00065     //typedef typename DataTypes::Real Real;
00066     typedef typename DataTypes::VecCoord VecCoord;
00067 
00068     typedef typename DataTypes::VecDeriv VecDeriv;
00069     typedef typename DataTypes::Coord Coord;
00070     typedef typename DataTypes::Deriv Deriv;
00071     typedef typename Coord::value_type Real;
00072 
00073     enum { N=Coord::static_size };
00074   typedef defaulttype::Mat<N,N,Real> Mat;
00075 
00076     class Mat3 : public fixed_array<Deriv,3>
00077     {
00078     public:
00079         Deriv operator*(const Deriv& v)
00080         {
00081             return Deriv((*this)[0]*v,(*this)[1]*v,(*this)[2]*v);
00082         }
00083         Deriv transposeMultiply(const Deriv& v) 
00084         {
00085             return Deriv(v[0]*((*this)[0])[0]+v[1]*((*this)[1])[0]+v[2]*((*this)[2][0]),
00086                 v[0]*((*this)[0][1])+v[1]*((*this)[1][1])+v[2]*((*this)[2][1]),
00087                 v[0]*((*this)[0][2])+v[1]*((*this)[1][2])+v[2]*((*this)[2][2]));
00088         }
00089     };
00090 
00091     protected: 
00092 
00093   class EdgeInformation
00094     {
00095     public:
00096         Mat3 DfDx; 
00097 
00098         int     m1, m2;  
00099         int     m3, m4;  
00100 
00101         double  ks;      
00102         double  kd;      
00103 
00104         double  restlength1; 
00105         double  restlength2; 
00106 
00107         bool is_activated;
00108 
00109         bool is_initialized;
00110 
00111         EdgeInformation(int m1=0, int m2=0, int m3=0, int m4=0, double restlength1=0.0, double restlength2=0.0, bool is_activated=false, bool is_initialized=false) 
00112             : m1(m1), m2(m2), m3(m3), m4(m4), restlength1(restlength1), restlength2(restlength2), is_activated(is_activated), is_initialized(is_initialized)
00113         {
00114         }
00115     };
00116 
00117     EdgeData<EdgeInformation> edgeInfo;
00118             
00119     sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00120 
00121     bool updateMatrix;
00122 
00123     Data<double> f_ks;
00124     Data<double> f_kd;
00125 
00126 public:
00127 
00128     QuadularBendingSprings();
00129 
00130     ~QuadularBendingSprings();
00131 
00132     virtual double getPotentialEnergy(const VecCoord& x);
00133     
00135     virtual void init();
00136      
00137     virtual void addForce(VecDeriv& f, const VecCoord& x, const VecDeriv& v);
00138     virtual void addDForce(VecDeriv& df, const VecDeriv& dx);
00139 
00140     virtual double getKs() const { return f_ks.getValue();}
00141     virtual double getKd() const { return f_kd.getValue();}
00142 
00143     void setKs(const double ks) {
00144         f_ks.setValue((double)ks);
00145     }
00146     void setKd(const double kd) {
00147         f_kd.setValue((double)kd);
00148     }
00149 
00150     // handle topological changes
00151     virtual void handleTopologyChange();
00152 
00153     // -- VisualModel interface
00154     void draw();
00155     void initTextures() { };
00156     void update() { };
00157     
00158 protected:
00159 
00160     EdgeData<EdgeInformation> &getEdgeInfo() {return edgeInfo;}
00161 
00162     static void QuadularBSEdgeCreationFunction(int edgeIndex, void* param, 
00163         EdgeInformation &ei,
00164         const Edge& ,  const sofa::helper::vector< unsigned int > &,
00165         const sofa::helper::vector< double >&);
00166 
00167     static void QuadularBSQuadCreationFunction(const sofa::helper::vector<unsigned int> &quadAdded, 
00168         void* param, vector<EdgeInformation> &edgeData);
00169 
00170     static void QuadularBSQuadDestructionFunction ( const sofa::helper::vector<unsigned int> &quadAdded, 
00171         void* param, vector<EdgeInformation> &edgeData);
00172 
00173     double m_potentialEnergy;
00174 
00175     //typedef std::pair<unsigned,unsigned> IndexPair;
00176     //void addSpring( unsigned, unsigned, std::set<IndexPair>& );
00177 
00179 
00180     //component::MechanicalObject<DataTypes>* dof;
00181 
00182 };
00183 
00184 } // namespace forcefield
00185 
00186 } // namespace component
00187 
00188 } // namespace sofa
00189 
00190 #endif

Generated on Sun Nov 23 03:04:32 2008 for Sofa by  doxygen 1.5.7.1