EdgePressureForceField.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 #include <sofa/component/forcefield/EdgePressureForceField.h>
00026 #include <sofa/component/topology/EdgeSubsetData.inl>
00027 #include <sofa/helper/gl/template.h>
00028 #include <vector>
00029 #include <set>
00030
00031 #ifdef _WIN32
00032 #include <windows.h>
00033 #endif
00034
00035
00036
00037 namespace sofa
00038 {
00039
00040 namespace component
00041 {
00042
00043 namespace forcefield
00044 {
00045
00046 using namespace sofa::defaulttype;
00047 using namespace core::componentmodel::topology;
00048
00049 using std::cerr;
00050 using std::cout;
00051 using std::endl;
00052
00053 template <class DataTypes> EdgePressureForceField<DataTypes>::~EdgePressureForceField()
00054 {
00055 }
00056
00057 template <class DataTypes> void EdgePressureForceField<DataTypes>::handleTopologyChange()
00058 {
00059 std::list<const TopologyChange *>::const_iterator itBegin=_topology->firstChange();
00060 std::list<const TopologyChange *>::const_iterator itEnd=_topology->lastChange();
00061
00062
00063 edgePressureMap.handleTopologyEvents(itBegin,itEnd,_topology->getNbEdges());
00064
00065 }
00066 template <class DataTypes> void EdgePressureForceField<DataTypes>::init()
00067 {
00068
00069 this->core::componentmodel::behavior::ForceField<DataTypes>::init();
00070
00071 _topology = this->getContext()->getMeshTopology();
00072 this->getContext()->get(edgeGeo);
00073
00074 assert(edgeGeo!=0);
00075
00076 if (edgeGeo==NULL)
00077 {
00078 std::cerr << "ERROR(EdgePressureForceField): object must have an EdgeSetTopology.\n";
00079 return;
00080 }
00081
00082 if (dmin.getValue()!=dmax.getValue()) {
00083 selectEdgesAlongPlane();
00084 }
00085 if (edgeList.getValue().length()>0) {
00086 selectEdgesFromString();
00087 }
00088
00089 initEdgeInformation();
00090
00091 }
00092
00093
00094 template <class DataTypes>
00095 void EdgePressureForceField<DataTypes>::addForce(VecDeriv& f, const VecCoord& , const VecDeriv& )
00096 {
00097 Deriv force;
00098
00099 typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;
00100
00101 for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00102 {
00103 force=(*it).second.force/2;
00104 f[_topology->getEdge((*it).first)[0]]+=force;
00105 f[_topology->getEdge((*it).first)[1]]+=force;
00106
00107 }
00108 }
00109
00110 template <class DataTypes>
00111 double EdgePressureForceField<DataTypes>::getPotentialEnergy(const VecCoord& )
00112 {
00113 cerr<<"EdgePressureForceField::getPotentialEnergy-not-implemented !!!"<<endl;
00114 return 0;
00115 }
00116
00117 template<class DataTypes>
00118 void EdgePressureForceField<DataTypes>::initEdgeInformation()
00119 {
00120 typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;
00121
00122 for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00123 {
00124 (*it).second.length=edgeGeo->computeRestEdgeLength((*it).first);
00125 (*it).second.force=pressure.getValue()*(*it).second.length;
00126 }
00127 }
00128
00129
00130 template<class DataTypes>
00131 void EdgePressureForceField<DataTypes>::updateEdgeInformation()
00132 {
00133 typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;
00134
00135 for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00136 {
00137 (*it).second.force=((*it).second.length)*pressure.getValue();
00138 }
00139 }
00140
00141
00142 template <class DataTypes>
00143 void EdgePressureForceField<DataTypes>::selectEdgesAlongPlane()
00144 {
00145 const VecCoord& x = *this->mstate->getX0();
00146 std::vector<bool> vArray;
00147 unsigned int i;
00148
00149 vArray.resize(x.size());
00150
00151 for( i=0; i<x.size();++i)
00152 {
00153 vArray[i]=isPointInPlane(x[i]);
00154 }
00155
00156 for (int n=0;n<_topology->getNbEdges();++n)
00157 {
00158 if ((vArray[_topology->getEdge(n)[0]]) && (vArray[_topology->getEdge(n)[1]]))
00159 {
00160
00161 EdgePressureInformation t;
00162 edgePressureMap[n]=t;
00163 }
00164 }
00165 }
00166
00167 template <class DataTypes>
00168 void EdgePressureForceField<DataTypes>::selectEdgesFromString()
00169 {
00170 std::string inputString=edgeList.getValue();
00171 unsigned int i;
00172 do {
00173 const char *str=inputString.c_str();
00174 for(i=0;(i<inputString.length())&&(str[i]!=',');++i) ;
00175 EdgePressureInformation t;
00176
00177 if (i==inputString.length()) {
00178 edgePressureMap[(unsigned int)atoi(str)]=t;
00179 inputString+=i;
00180 } else {
00181 inputString[i]='\0';
00182 edgePressureMap[(unsigned int)atoi(str)]=t;
00183 inputString+=i+1;
00184 }
00185 } while (inputString.length()>0);
00186
00187 }
00188 template<class DataTypes>
00189 void EdgePressureForceField<DataTypes>::draw()
00190 {
00191 if (!getContext()->getShowForceFields()) return;
00192 if (!this->mstate) return;
00193
00194 if (getContext()->getShowWireFrame())
00195 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00196
00197
00198 const VecCoord& x = *this->mstate->getX();
00199
00200 glDisable(GL_LIGHTING);
00201
00202 glBegin(GL_LINES);
00203 glColor4f(0,1,0,1);
00204
00205 typename topology::EdgeSubsetData<EdgePressureInformation>::iterator it;
00206
00207 for(it=edgePressureMap.begin(); it!=edgePressureMap.end(); it++ )
00208 {
00209 helper::gl::glVertexT(x[_topology->getEdge((*it).first)[0]]);
00210 helper::gl::glVertexT(x[_topology->getEdge((*it).first)[1]]);
00211 }
00212 glEnd();
00213
00214
00215 if (getContext()->getShowWireFrame())
00216 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00217 }
00218
00219 }
00220
00221 }
00222
00223 }