SpringForceField.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 #ifndef SOFA_COMPONENT_FORCEFIELD_SPRINGFORCEFIELD_H
00029 #define SOFA_COMPONENT_FORCEFIELD_SPRINGFORCEFIELD_H
00030
00031 #include <sofa/core/componentmodel/behavior/PairInteractionForceField.h>
00032 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00033 #include <sofa/defaulttype/Vec.h>
00034 #include <sofa/helper/vector.h>
00035
00036
00037 namespace sofa
00038 {
00039
00040 namespace component
00041 {
00042
00043 namespace forcefield
00044 {
00045
00046 using namespace sofa::defaulttype;
00047
00049 template<class DataTypes>
00050 class SpringForceFieldInternalData
00051 {
00052 public:
00053 };
00054
00056 template<class DataTypes>
00057 class SpringForceField : public core::componentmodel::behavior::PairInteractionForceField<DataTypes>, public virtual core::objectmodel::BaseObject
00058 {
00059 public:
00060 typedef typename core::componentmodel::behavior::PairInteractionForceField<DataTypes> Inherit;
00061 typedef typename DataTypes::VecCoord VecCoord;
00062 typedef typename DataTypes::VecDeriv VecDeriv;
00063 typedef typename DataTypes::Coord Coord;
00064 typedef typename DataTypes::Deriv Deriv;
00065 typedef typename Coord::value_type Real;
00066 typedef core::componentmodel::behavior::MechanicalState<DataTypes> MechanicalState;
00067
00068 class Spring
00069 {
00070 public:
00071 int m1, m2;
00072 SReal ks;
00073 SReal kd;
00074 SReal initpos;
00075
00076 Spring(int m1=0, int m2=0, SReal ks=0.0, SReal kd=0.0, SReal initpos=0.0)
00077 : m1(m1), m2(m2), ks(ks), kd(kd), initpos(initpos)
00078 {
00079 }
00080
00081 inline friend std::istream& operator >> ( std::istream& in, Spring& s ){
00082 in>>s.m1>>s.m2>>s.ks>>s.kd>>s.initpos;
00083 return in;
00084 }
00085
00086 inline friend std::ostream& operator << ( std::ostream& out, const Spring& s ){
00087 out<<s.m1<<" "<<s.m2<<" "<<s.ks<<" "<<s.kd<<" "<<s.initpos<<"\n";
00088 return out;
00089 }
00090
00091 };
00092 protected:
00093
00094 SReal m_potentialEnergy;
00095 Data<SReal> ks;
00096 Data<SReal> kd;
00097 Data<sofa::helper::vector<Spring> > springs;
00098 class Loader;
00099
00100 SpringForceFieldInternalData<DataTypes> data;
00101 friend class SpringForceFieldInternalData<DataTypes>;
00102
00103 void addSpringForce(SReal& potentialEnergy, VecDeriv& f1, const VecCoord& p1, const VecDeriv& v1, VecDeriv& f2, const VecCoord& p2, const VecDeriv& v2, int i, const Spring& spring);
00104
00105 public:
00106 SpringForceField(MechanicalState* object1, MechanicalState* object2, SReal _ks=100.0, SReal _kd=5.0);
00107 SpringForceField(SReal _ks=100.0, SReal _kd=5.0);
00108
00109 virtual void parse(core::objectmodel::BaseObjectDescription* arg);
00110
00111 bool load(const char *filename);
00112
00113 core::componentmodel::behavior::MechanicalState<DataTypes>* getObject1() { return this->mstate1; }
00114 core::componentmodel::behavior::MechanicalState<DataTypes>* getObject2() { return this->mstate2; }
00115
00116 virtual void init();
00117
00118 virtual void addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2);
00119 virtual void addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2);
00120 virtual double getPotentialEnergy(const VecCoord&, const VecCoord&) { return m_potentialEnergy; }
00121 SReal getStiffness() { return ks.getValue(); }
00122 SReal getDamping(){ return kd.getValue(); }
00123 void setStiffness(SReal _ks){ ks.setValue(_ks); }
00124 void setDamping(SReal _kd){ kd.setValue(_kd); }
00125
00126 void draw();
00127
00128
00129
00130 void clear(int reserve=0)
00131 {
00132 sofa::helper::vector<Spring>& springs = *this->springs.beginEdit();
00133 springs.clear();
00134 if (reserve) springs.reserve(reserve);
00135 this->springs.endEdit();
00136 }
00137
00138 void addSpring(int m1, int m2, SReal ks, SReal kd, SReal initlen)
00139 {
00140 springs.beginEdit()->push_back(Spring(m1,m2,ks,kd,initlen));
00141 springs.endEdit();
00142 }
00143 };
00144
00145 }
00146
00147 }
00148
00149 }
00150
00151 #endif