ForceField.inl

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_FORCEFIELD_INL
00028 #define SOFA_CORE_COMPONENTMODEL_BEHAVIOR_FORCEFIELD_INL
00029 
00030 #include <sofa/core/objectmodel/DataPtr.h>
00031 #include "ForceField.h"
00032 
00033 namespace sofa
00034 {
00035 
00036 namespace core
00037 {
00038 
00039 namespace componentmodel
00040 {
00041 
00042 namespace behavior
00043 {
00044 
00045 template<class DataTypes>
00046 ForceField<DataTypes>::ForceField(MechanicalState<DataTypes> *mm)
00047 : mstate(mm)
00048 {
00049 }
00050 
00051 template<class DataTypes>
00052 ForceField<DataTypes>::~ForceField()
00053 {
00054 }
00055 
00056 template<class DataTypes>
00057 void ForceField<DataTypes>::init()
00058 {
00059     BaseForceField::init();
00060     mstate = dynamic_cast< MechanicalState<DataTypes>* >(getContext()->getMechanicalState());
00061 }
00062 
00063 template<class DataTypes>
00064 void ForceField<DataTypes>::addForce()
00065 {
00066     if (mstate)
00067         addForce(*mstate->getF(), *mstate->getX(), *mstate->getV()); 
00068 }
00069 
00070 template<class DataTypes>
00071 void ForceField<DataTypes>::addDForce(double kFactor, double bFactor)
00072 {
00073     if (mstate)
00074         addDForce(*mstate->getF(), *mstate->getDx(), kFactor, bFactor);
00075 }
00076 
00077 template<class DataTypes>
00078 void ForceField<DataTypes>::addDForceV(double kFactor, double bFactor)
00079 {
00080     if (mstate)
00081         addDForce(*mstate->getF(), *mstate->getV(), kFactor, bFactor);
00082 }
00083 template<class DataTypes>
00084 void ForceField<DataTypes>::addDForce(VecDeriv& /*df*/, const VecDeriv& /*dx*/)
00085 {
00086     std::cerr << "ERROR("<<getClassName()<<"): addDForce not implemented.\n";
00087 }
00088 
00089 template<class DataTypes>
00090 void ForceField<DataTypes>::addDForce(VecDeriv& df, const VecDeriv& dx, double kFactor, double /*bFactor*/)
00091 {
00092     if (kFactor == 1.0)
00093         addDForce(df, dx);
00094     else if (kFactor != 0.0)
00095     {
00096     BaseMechanicalState::VecId vtmp(BaseMechanicalState::VecId::V_DERIV,BaseMechanicalState::VecId::V_FIRST_DYNAMIC_INDEX);
00097     mstate->vAvail(vtmp);
00098     mstate->vAlloc(vtmp);
00099     BaseMechanicalState::VecId vdx(BaseMechanicalState::VecId::V_DERIV,0);
00101     for (vdx.index=0;vdx.index<vtmp.index;++vdx.index)
00102         if (mstate->getVecDeriv(vdx.index) == &dx)
00103         break;
00104     mstate->vOp(vtmp,BaseMechanicalState::VecId::null(),vdx,kFactor);
00105         addDForce(df, *mstate->getVecDeriv(vtmp.index));
00106     mstate->vFree(vtmp);
00107     }
00108 }
00109 
00110 template<class DataTypes>
00111 double ForceField<DataTypes>::getPotentialEnergy()
00112 {
00113     if (mstate)
00114         return getPotentialEnergy(*mstate->getX());
00115     else return 0;
00116 }
00117 
00118 } // namespace behavior
00119 
00120 } // namespace componentmodel
00121 
00122 } // namespace core
00123 
00124 } // namespace sofa
00125 
00126 #endif

Generated on Sun Nov 23 02:28:48 2008 for Sofa by  doxygen 1.5.7.1