VectorSpringForceField.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_VECTORSPRINGFORCEFIELD_H
00026 #define SOFA_COMPONENT_FORCEFIELD_VECTORSPRINGFORCEFIELD_H
00027
00028 #include <sofa/component/forcefield/SpringForceField.h>
00029 #include <sofa/core/componentmodel/behavior/ForceField.h>
00030 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00031 #include <sofa/defaulttype/Vec.h>
00032 #include <sofa/defaulttype/Mat.h>
00033 #include <sofa/component/topology/EdgeData.h>
00034 #include <sofa/component/topology/TopologyChangedEvent.h>
00035
00036 #include <sofa/component/topology/EdgeSetGeometryAlgorithms.h>
00037 #include <sofa/component/topology/EdgeSetTopologyModifier.h>
00038
00039
00040 namespace sofa
00041 {
00042
00043 namespace component
00044 {
00045
00046 namespace forcefield
00047 {
00048
00049 using namespace sofa::defaulttype;
00050 using sofa::core::objectmodel::Event;
00051
00052 template<class DataTypes>
00053 class VectorSpringForceField
00054 : public core::componentmodel::behavior::PairInteractionForceField<DataTypes>, public virtual core::objectmodel::BaseObject
00055
00056 {
00057 public:
00058 typedef typename core::componentmodel::behavior::PairInteractionForceField<DataTypes> Inherit;
00059 typedef typename DataTypes::VecCoord VecCoord;
00060 typedef typename DataTypes::VecDeriv VecDeriv;
00061 typedef typename DataTypes::Coord Coord;
00062 typedef typename DataTypes::Deriv Deriv;
00063 typedef typename Coord::value_type Real;
00064 typedef core::componentmodel::behavior::MechanicalState<DataTypes> MechanicalState;
00065 enum { N=Coord::static_size };
00066
00067 struct Spring
00068 {
00069 Real ks;
00070 Real kd;
00071 Deriv restVector;
00072
00073 Spring(Real _ks, Real _kd, Deriv _rl) : ks(_ks), kd(_kd), restVector(_rl)
00074 {
00075 }
00076 Spring() : ks(1.0), kd(1.0)
00077 {
00078 }
00079 };
00080 protected:
00081
00082 double m_potentialEnergy;
00084 bool useTopology;
00085
00087 sofa::helper::vector<topology::Edge> edgeArray;
00089 sofa::component::topology::EdgeData<Spring> springArray;
00090
00092 Data<std::string> m_filename;
00094 Data<double> m_stiffness;
00096 Data<double> m_viscosity;
00097
00098 void resizeArray(unsigned int n);
00099
00100 static void springCreationFunction(int ,
00101 void* param, Spring& t,
00102 const topology::Edge& e,
00103 const sofa::helper::vector< unsigned int > &ancestors,
00104 const sofa::helper::vector< double >& coefs);
00105
00106 public:
00107
00108 sofa::core::componentmodel::topology::BaseMeshTopology* _topology;
00109 sofa::component::topology::EdgeSetTopologyContainer* edgeCont;
00110 sofa::component::topology::EdgeSetGeometryAlgorithms<DataTypes>* edgeGeo;
00111 sofa::component::topology::EdgeSetTopologyModifier* edgeMod;
00112
00113 VectorSpringForceField(MechanicalState* _object=NULL);
00114
00115 VectorSpringForceField(MechanicalState* _object1, MechanicalState* _object2);
00116
00117 bool load(const char *filename);
00118
00119 core::componentmodel::behavior::MechanicalState<DataTypes>* getObject1() { return this->mstate1; }
00120 core::componentmodel::behavior::MechanicalState<DataTypes>* getObject2() { return this->mstate2; }
00121
00122 virtual void init();
00123
00124 void createDefaultSprings();
00125
00126 virtual void handleEvent( Event* e );
00127
00128 virtual void addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2);
00129
00130
00131 virtual void addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2, double kFactor, double bFactor);
00132
00133
00134
00135 virtual double getPotentialEnergy(const VecCoord&, const VecCoord&)
00136 { return m_potentialEnergy; }
00137
00138 Real getStiffness() const
00139 {
00140 return (Real)(m_stiffness.getValue());
00141 }
00142 const Real getViscosity() const
00143 {
00144 return (Real)(m_viscosity.getValue());
00145 }
00146 const topology::EdgeData<Spring>& getSpringArray() const
00147 {
00148 return springArray;
00149 }
00150
00151 void draw();
00152
00153
00154
00155 void clear(int reserve=0)
00156 {
00157 springArray.clear();
00158 if (reserve) springArray.reserve(reserve);
00159 }
00160
00161 void addSpring(int m1, int m2, SReal ks, SReal kd, Coord restVector);
00162
00164 class Loader;
00165 friend class Loader;
00166
00167 };
00168
00169 }
00170
00171 }
00172
00173 }
00174
00175 #endif