PairInteractionForceField.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 #ifndef SOFA_CORE_COMPONENTMODEL_BEHAVIOR_PAIRINTERACTIONFORCEFIELD_H
00028 #define SOFA_CORE_COMPONENTMODEL_BEHAVIOR_PAIRINTERACTIONFORCEFIELD_H
00029
00030 #include <sofa/core/componentmodel/behavior/InteractionForceField.h>
00031 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00032
00033 #include <sofa/core/objectmodel/Data.h>
00034 #include <sofa/defaulttype/Vec.h>
00035
00036 namespace sofa
00037 {
00038
00039 namespace core
00040 {
00041
00042 namespace componentmodel
00043 {
00044
00045 namespace behavior
00046 {
00047
00054 template<class TDataTypes>
00055 class PairInteractionForceField : public InteractionForceField
00056 {
00057 public:
00058
00059 typedef TDataTypes DataTypes;
00060 typedef typename DataTypes::VecCoord VecCoord;
00061 typedef typename DataTypes::VecDeriv VecDeriv;
00062 typedef typename DataTypes::Coord Coord;
00063 typedef typename DataTypes::Deriv Deriv;
00064
00065
00066 PairInteractionForceField(MechanicalState<DataTypes> *mm1 = NULL, MechanicalState<DataTypes> *mm2 = NULL);
00067
00068 virtual ~PairInteractionForceField();
00069
00070 virtual void init();
00071
00073 MechanicalState<DataTypes>* getMState1() { return mstate1; }
00074 BaseMechanicalState* getMechModel1() { return mstate1; }
00076 MechanicalState<DataTypes>* getMState2() { return mstate2; }
00077 BaseMechanicalState* getMechModel2() { return mstate2; }
00078
00079
00080
00081
00083 BaseMechanicalState* getMState(sofa::core::objectmodel::BaseContext* context, std::string path);
00084
00087
00098 virtual void addForce();
00099
00113 virtual void addDForce(double kFactor, double bFactor);
00114
00123 virtual void addDForceV(double kFactor, double bFactor);
00124
00133 virtual double getPotentialEnergy();
00134
00144 virtual void addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2) = 0;
00145
00162 virtual void addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2);
00163
00180 virtual void addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2, double kFactor, double bFactor);
00181
00189 virtual double getPotentialEnergy(const VecCoord& x1, const VecCoord& x2) =0;
00190
00192
00195 template<class T>
00196 static bool canCreate(T*& obj, objectmodel::BaseContext* context, objectmodel::BaseObjectDescription* arg)
00197 {
00198
00199
00200
00201
00202
00203 if (arg->getAttribute("object1") || arg->getAttribute("object2"))
00204 {
00205
00206
00207
00208 if (dynamic_cast<MechanicalState<DataTypes>*>(arg->findObject(arg->getAttribute("object1",".."))) == NULL)
00209 return false;
00210 if (dynamic_cast<MechanicalState<DataTypes>*>(arg->findObject(arg->getAttribute("object2",".."))) == NULL)
00211 return false;
00212
00213 }
00214 else
00215 {
00216
00217 if (dynamic_cast<MechanicalState<DataTypes>*>(context->getMechanicalState()) == NULL)
00218 return false;
00219 }
00220 return InteractionForceField::canCreate(obj, context, arg);
00221 }
00222
00224 template<class T>
00225 static void create(T*& obj, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
00226 {
00227 core::componentmodel::behavior::InteractionForceField::create(obj, context, arg);
00228 if (arg && (arg->getAttribute("object1") || arg->getAttribute("object2")))
00229 {
00230 obj->_object1.setValue(arg->getAttribute("object1",".."));
00231 obj->_object2.setValue(arg->getAttribute("object2",".."));
00232 obj->mstate1 = dynamic_cast<MechanicalState<DataTypes>*>(arg->findObject(arg->getAttribute("object1","..")));
00233 obj->mstate2 = dynamic_cast<MechanicalState<DataTypes>*>(arg->findObject(arg->getAttribute("object2","..")));
00234 }
00235 else if (context)
00236 {
00237 obj->mstate1 =
00238 obj->mstate2 =
00239 dynamic_cast<MechanicalState<DataTypes>*>(context->getMechanicalState());
00240 }
00241 }
00242
00243 virtual std::string getTemplateName() const
00244 {
00245 return templateName(this);
00246 }
00247
00248 static std::string templateName(const PairInteractionForceField<DataTypes>* = NULL)
00249 {
00250 return DataTypes::Name();
00251 }
00252
00253 protected:
00254 sofa::core::objectmodel::Data< std::string > _object1;
00255 sofa::core::objectmodel::Data< std::string > _object2;
00256 MechanicalState<DataTypes> *mstate1;
00257 MechanicalState<DataTypes> *mstate2;
00258 };
00259
00260 }
00261
00262 }
00263
00264 }
00265
00266 }
00267
00268 #endif