PairInteractionForceField.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
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 //     using sofa::core::objectmodel::Data;
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         // BUGFIX(Jeremie A.): We need to test dynamic casts with the right
00199         // DataTypes otherwise the factory don't know which template to
00200         // instanciate or make sure that the right template is used.
00201         // This means that InteractionForceFields in scene files
00202         // still need to appear after the affected objects...
00203         if (arg->getAttribute("object1") || arg->getAttribute("object2"))
00204         {
00205       //if (!arg->getAttribute("template")) //if a template is specified, the interaction forcefield can be created. If during init, no corresponding MechanicalState is found, it will be erased. It allows the saving of scenes containing PairInteractionForceField
00206       //{
00207             //return InteractionForceField::canCreate(obj, context, arg);
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             //if (context->getMechanicalState() == NULL) return false;
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 } // namespace behavior
00261 
00262 } // namespace componentmodel
00263 
00264 } // namespace core
00265 
00266 } // namespace sofa
00267 
00268 #endif

Generated on Sun Nov 23 02:29:21 2008 for Sofa by  doxygen 1.5.7.1