ConstantForceField.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 :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_COMPONENT_INTERACTIONFORCEFIELD_ConstantForceField_INL
00026 #define SOFA_COMPONENT_INTERACTIONFORCEFIELD_ConstantForceField_INL
00027 
00028 #include <sofa/core/componentmodel/behavior/ForceField.inl>
00029 #include "ConstantForceField.h"
00030 #include <sofa/helper/system/config.h>
00031 #include <sofa/defaulttype/VecTypes.h>
00032 #include <sofa/defaulttype/RigidTypes.h>
00033 #include <sofa/helper/gl/template.h>
00034 #include <assert.h>
00035 #include <iostream>
00036 using std::cerr;
00037 using std::endl;
00038 
00039 namespace sofa
00040 {
00041 
00042   namespace component
00043   {
00044 
00045     namespace forcefield
00046     {
00047 
00048 
00049       template<class DataTypes>
00050       ConstantForceField<DataTypes>::ConstantForceField()
00051           : points(initData(&points, "points", "points where the forces are applied"))
00052           , forces(initData(&forces, "forces", "applied forces"))
00053       {}
00054 
00055 
00056       template<class DataTypes>
00057       void ConstantForceField<DataTypes>::addForce(VecDeriv& f1, const VecCoord& p1, const VecDeriv& )
00058       {
00059         f1.resize(p1.size());
00060         const VecIndex& indices = points.getValue();
00061         const VecDeriv& f = forces.getValue();
00062         for (unsigned int i=0; i<indices.size(); i++)
00063         {
00064           f1[indices[i]]+=f[i];
00065         }
00066       }
00067 
00068 
00069 
00070       template <class DataTypes>
00071       double ConstantForceField<DataTypes>::getPotentialEnergy(const VecCoord& x)
00072       {
00073         const VecIndex& indices = points.getValue();
00074         const VecDeriv& f = forces.getValue();
00075         double e=0;
00076         for (unsigned int i=0; i<indices.size(); i++)
00077         {
00078            e -= f[i]*x[indices[i]];
00079         }
00080         return e;
00081       }
00082 
00083 
00084 #ifndef SOFA_FLOAT
00085   template <>
00086   double ConstantForceField<defaulttype::Rigid3dTypes>::getPotentialEnergy(const VecCoord& );
00087   template <>
00088   double ConstantForceField<defaulttype::Rigid2dTypes>::getPotentialEnergy(const VecCoord& );
00089 #endif
00090 
00091 #ifndef SOFA_DOUBLE
00092   template <>
00093   double ConstantForceField<defaulttype::Rigid3fTypes>::getPotentialEnergy(const VecCoord& );
00094   template <>
00095   double ConstantForceField<defaulttype::Rigid2fTypes>::getPotentialEnergy(const VecCoord& );
00096 #endif
00097 
00098 
00099       template<class DataTypes>
00100       void ConstantForceField<DataTypes>::draw()
00101       {
00102         if (!getContext()->getShowForceFields()) return;  
00103         const VecIndex& indices = points.getValue();
00104         const VecDeriv& f = forces.getValue();
00105         const VecCoord& x = *this->mstate->getX();
00106         glDisable(GL_LIGHTING);
00107         glBegin(GL_LINES);
00108         glColor3f(0,1,0);
00109         for (unsigned int i=0; i<indices.size(); i++)
00110         {
00111           Real xx,xy,xz,fx,fy,fz;
00112           DataTypes::get(xx,xy,xz,x[indices[i]]);
00113           DataTypes::get(fx,fy,fz,f[i]);
00114           glVertex3f( (GLfloat)xx, (GLfloat)xy, (GLfloat)xz );
00115           glVertex3f( (GLfloat)(xx+fx), (GLfloat)(xy+fy), (GLfloat)(xz+fz) );
00116         }
00117         glEnd();
00118       }
00119 
00120 
00121       template <class DataTypes>
00122       bool ConstantForceField<DataTypes>::addBBox(double*, double* )
00123       {
00124         return false;
00125       }
00126 
00127     } // namespace forcefield
00128 
00129   } // namespace component
00130 
00131 } // namespace sofa
00132 
00133 #endif
00134 
00135 
00136 

Generated on Sun Nov 23 02:54:31 2008 for Sofa by  doxygen 1.5.7.1