ConstantForceField.inl
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_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 }
00128
00129 }
00130
00131 }
00132
00133 #endif
00134
00135
00136