SpringEdgeDataForceField.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_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
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;
00069 Real kd;
00070 Real restLength;
00071 Mat3 dfdx;
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
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
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 }
00145
00146 }
00147
00148 }
00149
00150 #endif