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_MISC_MONITOR_H 00026 #define SOFA_COMPONENT_MISC_MONITOR_H 00027 #include <sofa/core/VisualModel.h> 00028 #include <sofa/core/componentmodel/behavior/MechanicalState.h> 00029 #include <sofa/defaulttype/Vec.h> 00030 00031 namespace sofa 00032 { 00033 00034 namespace component 00035 { 00036 00037 namespace misc 00038 { 00039 00040 using namespace core::objectmodel; 00041 using namespace std; 00042 00043 template <class DataTypes> 00044 class Monitor: public virtual core::VisualModel 00045 { 00046 public: 00047 00048 typedef typename DataTypes::VecReal VecReal; 00049 typedef typename DataTypes::VecCoord VecCoord; 00050 typedef typename DataTypes::VecDeriv VecDeriv; 00051 typedef typename DataTypes::Real Real; 00052 typedef typename DataTypes::Coord Coord; 00053 typedef typename DataTypes::Deriv Deriv; 00054 00055 class MonitorData 00056 { 00057 public: 00058 00059 MonitorData():vels(NULL),forces(NULL),pos(NULL) 00060 { 00061 }; 00062 00063 /*copy mechanical state vectors (forces, positions, velocities) 00064 * into internal state vectors 00065 */ 00066 void setValues ( VecDeriv* _vels, VecDeriv* _forces, VecCoord* _pos ) 00067 { 00068 vels = _vels; 00069 forces = _forces; 00070 pos = _pos; 00071 } 00072 00074 00075 //copy the given vector into the posIndices vector 00076 void setIndPos ( sofa::helper::vector < int > &_IdxPos ) 00077 { 00078 posIndices = _IdxPos; 00079 } 00080 00081 //copy the given vector into the initPosIndices vector (used at initialization) 00082 void setIndPosInit ( sofa::helper::vector < int > &_IdxPos ) 00083 { 00084 initPosIndices = _IdxPos; 00085 } 00086 00087 //accessor to the indices vector of monitored positions 00088 const sofa::helper::vector < int > &getIndPos() const 00089 { 00090 return posIndices; 00091 } 00092 00093 //accessor to the initial indices vector of monitored positions (used by reset()) 00094 const sofa::helper::vector < int > &getIndPosInit () const 00095 { 00096 return initPosIndices; 00097 } 00098 00100 00101 //copy the given vector into the velsIndices vector 00102 void setIndVels ( sofa::helper::vector < int > &_IdxVels ) 00103 { 00104 velsIndices = _IdxVels; 00105 } 00106 00107 //copy the given vector into the initVelsIndices vector (used at initialization) 00108 void setIndVelsInit ( sofa::helper::vector < int > &_IdxVels ) 00109 { 00110 initVelsIndices = _IdxVels; 00111 } 00112 00113 //accessor to the indices vector of monitored velocities 00114 const sofa::helper::vector < int > &getIndVels() const 00115 { 00116 return velsIndices; 00117 } 00118 00119 //accessor to the initial indices vector of monitored velocities (used by reset()) 00120 const sofa::helper::vector < int > &getIndVelsInit () const 00121 { 00122 return initVelsIndices; 00123 } 00124 00126 00127 //copy the given vector into the forcesIndices vector 00128 void setIndForces ( sofa::helper::vector < int > &_IdxForces ) 00129 { 00130 forcesIndices = _IdxForces; 00131 } 00132 00133 //copy the given vector into the initForcesIndices vector (used at initialization) 00134 void setIndForcesInit ( sofa::helper::vector < int > &_IdxForces ) 00135 { 00136 initForcesIndices = _IdxForces; 00137 } 00138 00139 //accessor to the indices vector of monitored forces 00140 const sofa::helper::vector < int > &getIndForces() const 00141 { 00142 return forcesIndices; 00143 } 00144 00145 //accessor to the initial indices vector of monitored forces (used by reset()) 00146 const sofa::helper::vector < int > &getIndForcesInit () const 00147 { 00148 return initForcesIndices; 00149 } 00150 00152 00153 //return the size of the indice vector of positions 00154 unsigned int sizeIdxPos() const 00155 { 00156 return posIndices.size(); 00157 } 00158 00159 //return the size of the indice vector of velocities 00160 unsigned int sizeIdxVels() const 00161 { 00162 return velsIndices.size(); 00163 } 00164 00165 //return the size of the indice vector of forces 00166 unsigned int sizeIdxForces() const 00167 { 00168 return forcesIndices.size(); 00169 } 00170 00172 00173 //return the size of mechanical state position vector 00174 unsigned int getSizeVecPos() const 00175 { 00176 if (pos) 00177 return pos -> size(); 00178 else 00179 return 0; 00180 } 00181 00182 //return the size of mechanical state velocity vector 00183 unsigned int getSizeVecVels() const 00184 { 00185 if (vels) 00186 return vels -> size(); 00187 else 00188 return 0; 00189 } 00190 00191 //return the size of mechanical state force vector 00192 unsigned int getSizeVecForces() const 00193 { 00194 if (forces) 00195 return forces -> size(); 00196 else 00197 return 0; 00198 } 00199 00201 00202 //clear the indices vectors of monitored particles 00203 void clearVecIndices() 00204 { 00205 posIndices.clear(); 00206 velsIndices.clear(); 00207 forcesIndices.clear(); 00208 } 00209 00210 00211 inline friend std::ostream& operator << ( std::ostream& out, const MonitorData& m ) 00212 { 00213 out << "P"; 00214 out << m.sizeIdxPos(); 00215 out << " ["; 00216 for (unsigned int i = 0; i < m.sizeIdxPos(); i++) 00217 out << (m.getIndPos())[i] << " "; 00218 00219 out << "] "; 00220 out << "V"; 00221 out << m.sizeIdxVels(); 00222 out << " ["; 00223 for (unsigned int i = 0; i < m.sizeIdxVels(); i++) 00224 out << (m.getIndVels())[i] << " "; 00225 00226 out << "] "; 00227 out << "F"; 00228 out << m.sizeIdxForces(); 00229 out << " ["; 00230 for (unsigned int i = 0; i < m.sizeIdxForces(); i++) 00231 out << (m.getIndForces())[i] << " "; 00232 00233 return out; 00234 } 00235 00242 inline friend std::istream& operator >> ( std::istream& in, MonitorData &m ) 00243 { 00244 char temp; 00245 int nbParticles = 0; 00246 int number = 0; 00247 00248 in >> temp; // skip the "P" 00249 in >> nbParticles; //number of positions to monitor 00250 in >> temp; //skip the "[" 00251 for (int i = 0; i < nbParticles; i++) 00252 { 00253 in >> number; 00254 m.posIndices.push_back(number); 00255 } 00256 00257 in >> temp; //skip the "]" 00258 in >> temp; //skip the "V" 00259 in >> nbParticles; //number of velocities to monitor 00260 in >> temp; //skip the "[" 00261 for (int i = 0; i < nbParticles; i++) 00262 { 00263 in >> number; 00264 m.velsIndices.push_back(number); 00265 } 00266 00267 in >> temp; //skip the "]" 00268 in >> temp; //skip the "F" 00269 in >> nbParticles; //number of forces to monitor 00270 in >> temp; //skip the "[" 00271 for (int i = 0; i < nbParticles; i++) 00272 { 00273 in >> number; 00274 m.forcesIndices.push_back(number); 00275 } 00276 return in; 00277 } 00278 00279 //return the position vector of particle number "index" 00280 const Coord getPos ( unsigned int index ) const 00281 { 00282 return ((*pos)[posIndices[index]]); 00283 } 00284 00285 //return the velocity vector of particle number "index" 00286 const Deriv getVel ( unsigned int index ) const 00287 { 00288 return (*vels)[velsIndices[index]]; 00289 } 00290 00291 //return the force vector of particle number "index" 00292 const Deriv getForce ( unsigned int index ) const 00293 { 00294 return (*forces)[forcesIndices[index]]; 00295 } 00296 00297 const VecCoord* getMechPos () const 00298 { 00299 return pos; 00300 } 00301 00302 //get the vector containing the saved positions (for trajectories) 00303 sofa::helper::vector < VecCoord >* getSavePos() 00304 { 00305 return &savePos; 00306 } 00307 00308 protected: 00309 00311 sofa::helper::vector < int > posIndices; 00313 sofa::helper::vector < int > velsIndices; 00315 sofa::helper::vector < int > forcesIndices; 00316 00318 sofa::helper::vector < int > initPosIndices; 00320 sofa::helper::vector < int > initVelsIndices; 00322 sofa::helper::vector < int > initForcesIndices; 00323 00324 VecDeriv* vels; 00325 VecDeriv* forces; 00326 VecCoord* pos; 00327 00329 sofa::helper::vector < VecCoord > savePos; 00330 00331 }; 00332 00333 Data < bool > saveXToGnuplot; 00334 Data < bool > saveVToGnuplot; 00335 Data < bool > saveFToGnuplot; 00336 00337 Data < MonitorData > monitoring; 00338 00339 Data < bool > showPositions; 00340 Data <defaulttype::Vector3> positionsColor; 00341 00342 Data < bool > showVelocities; 00343 Data< defaulttype::Vector3 > velocitiesColor; 00344 00345 Data < bool > showForces; 00346 Data< defaulttype::Vector3 > forcesColor; 00347 00348 Data < double > showMinThreshold; 00349 00350 Data < bool > showTrajectories; 00351 Data < double > trajectoriesPrecision; 00352 Data< defaulttype::Vector3 > trajectoriesColor; 00353 00354 00355 Monitor (); 00356 ~Monitor (); 00357 00359 void setIndForces ( sofa::helper::vector < int > &_IdxForces ); 00360 00362 void setIndVels ( sofa::helper::vector < int > &_IdxVels ); 00363 00365 void setIndPos ( sofa::helper::vector < int > &_IdxPos ); 00366 00367 //init data 00368 virtual void init (); 00369 00370 //reset monitored values 00371 virtual void reset (); 00372 00376 virtual void reinit(); 00377 00382 virtual void fwdDraw ( Pass ); 00383 00385 virtual void initGnuplot ( const std::string path ); 00386 00388 virtual void exportGnuplot ( Real time ); 00389 00390 00391 virtual std::string getTemplateName() const 00392 { 00393 return templateName(this); 00394 } 00395 00396 static std::string templateName(const Monitor<DataTypes>* = NULL) 00397 { 00398 return DataTypes::Name(); 00399 } 00400 protected: 00401 00403 inline double vectorNorm (Deriv currentVel) 00404 { 00405 return sqrt (currentVel[0]*currentVel[0] + currentVel[1]*currentVel[1] + currentVel[2]*currentVel[2]); 00406 } 00408 00409 std::ofstream* saveGnuplotX; 00410 std::ofstream* saveGnuplotV; 00411 std::ofstream* saveGnuplotF; 00412 00413 core::componentmodel::behavior::MechanicalState<DataTypes>* mmodel; 00414 00416 double internalDt; 00417 }; 00418 00419 } // namespace misc 00420 00421 } // namespace component 00422 00423 } // namespace sofa 00424 00425 #endif
1.5.7.1