TriangularBendingSprings.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00044 #include <sofa/component/MechanicalObject.h>
00045 #include <map>
00046
00047 #include <sofa/core/componentmodel/behavior/ForceField.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
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
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
00110
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 restlength=0.0, bool is_activated=false, bool is_initialized=false)
00129 : m1(m1), m2(m2), 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();
00146
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
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
00191
00192
00193
00194 };
00195
00196 }
00197
00198 }
00199
00200 }
00201
00202 #endif