QuadularBendingSprings.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 #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>
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
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
00151 virtual void handleTopologyChange();
00152
00153
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
00176
00177
00179
00180
00181
00182 };
00183
00184 }
00185
00186 }
00187
00188 }
00189
00190 #endif