Monitor.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_MISC_MONITOR_INL
00026 #define SOFA_COMPONENT_MISC_MONITOR_INL
00027 
00028 #include <sofa/component/misc/Monitor.h>
00029 #include <sofa/defaulttype/DataTypeInfo.h>
00030 #include <sofa/core/objectmodel/Context.h>
00031 #include <sofa/helper/gl/template.h>
00032 #include <sofa/core/VisualModel.h>
00033 #include <sofa/core/objectmodel/Data.h>
00034 #include <sofa/helper/gl/Axis.h>
00035 #include <fstream>
00036 #include <sofa/defaulttype/Vec.h>
00037 #include <cmath>
00038 
00039 
00040 
00041 namespace sofa
00042 {
00043 
00044 namespace component
00045 {
00046 
00047 namespace misc
00048 {
00049 
00050     using namespace sofa::defaulttype;
00051     using namespace std;
00052 
00053 
00055     template <class DataTypes>
00056     Monitor<DataTypes>::Monitor()
00057     : saveXToGnuplot ( initData ( &saveXToGnuplot, false, "ExportPositions", "export monitored positions as gnuplot file" ) )
00058     , saveVToGnuplot ( initData ( &saveVToGnuplot, false, "ExportVelocities", "export monitored velocities as gnuplot file" ) )
00059     , saveFToGnuplot ( initData ( &saveFToGnuplot, false, "ExportForces", "export monitored forces as gnuplot file" ) )
00060     , monitoring( initData (&monitoring, "MonitoredParticles", "monitoring of desired particles"))
00061     ,showPositions (initData (&showPositions, false, "ShowPositions", "see the monitored positions"))
00062     ,positionsColor (initData (&positionsColor,Vector3 (1.0, 1.0, 0.0), "PositionsColor", "define the color of positions"))
00063     
00064     ,showVelocities (initData (&showVelocities, false, "ShowVelocities", "see the monitored velocities"))
00065     ,velocitiesColor(initData (&velocitiesColor,Vector3 (1.0, 1.0, 0.0), "VelocitiesColor", "define the color of velocities"))
00066     
00067     ,showForces (initData (&showForces, false, "ShowForces", "see the monitored forces"))
00068     ,forcesColor (initData (&forcesColor,Vector3 (1.0, 1.0, 0.0), "ForcesColor", "define the color of forces"))
00069     ,showMinThreshold (initData (&showMinThreshold, 0.01 ,"ShowMinThreshold", "under this value, vectors are not represented"))
00070     ,showTrajectories (initData (&showTrajectories, false ,"ShowTrajectories", "print the trajectory of monitored particles"))
00071     ,trajectoriesPrecision (initData (&trajectoriesPrecision, 0.1,"TrajectoriesPrecision", "set the dt between to save of positions"))
00072     ,trajectoriesColor(initData (&trajectoriesColor,Vector3 (1.0, 1.0, 0.0), "TrajectoriesColor", "define the color of the trajectories"))
00073     ,saveGnuplotX ( NULL ), saveGnuplotV ( NULL ), saveGnuplotF ( NULL )
00074     ,internalDt(0.0)
00075     {}
00077 
00078     
00079 
00081     template <class DataTypes>
00082     Monitor<DataTypes>::~Monitor()
00083     {
00084         if (saveGnuplotX) delete ( saveGnuplotX );
00085         if (saveGnuplotV) delete ( saveGnuplotV );
00086         if (saveGnuplotF) delete ( saveGnuplotF );
00087     }
00089     
00090     
00091     
00093     template<class DataTypes>
00094     void Monitor<DataTypes>::setIndPos ( sofa::helper::vector < int > &_IdxPos )
00095     {
00096         monitoring.beginEdit() -> setIndPos (_IdxPos);
00097         monitoring.endEdit();
00098     }
00100     
00101     
00102     
00104     template<class DataTypes>
00105     void Monitor<DataTypes>::setIndVels ( sofa::helper::vector < int > &_IdxVels )
00106     {
00107         monitoring.beginEdit() -> setIndVels (_IdxVels);
00108         monitoring.endEdit();
00109     }
00111     
00112     
00113     
00115     template<class DataTypes>
00116     void Monitor<DataTypes>::setIndForces ( sofa::helper::vector < int > &_IdxForces )
00117     {
00118         monitoring.beginEdit() -> setIndForces (_IdxForces);
00119         monitoring.endEdit();
00120     }
00122     
00123     
00124 
00126     template<class DataTypes>
00127     void Monitor<DataTypes>::init()
00128     {
00129         mmodel =
00130             dynamic_cast<core::componentmodel::behavior::MechanicalState<DataTypes>*>
00131             ( this->getContext()->getMechanicalState() );
00132 
00133         monitoring.beginEdit() ->setValues (mmodel -> getV(), mmodel -> getF(), mmodel -> getX());
00134         sofa::helper::vector < int > initialPosIndices = monitoring.beginEdit()->getIndPos();
00135         sofa::helper::vector < int > initialVelsIndices = monitoring.beginEdit()->getIndVels();
00136         sofa::helper::vector < int > initialForcesIndices = monitoring.beginEdit()->getIndForces();
00137         
00138         monitoring.beginEdit()->setIndPosInit (initialPosIndices);
00139         monitoring.beginEdit()->setIndVelsInit (initialVelsIndices);
00140         monitoring.beginEdit()->setIndForcesInit (initialForcesIndices);
00141         monitoring.endEdit();
00142         
00143         
00144         initGnuplot ("./");
00145     }
00147 
00148 
00149 
00151     template<class DataTypes>
00152     void Monitor<DataTypes>::reset()
00153     {       
00154         monitoring.beginEdit()->clearVecIndices();
00155         monitoring.beginEdit() ->setValues (mmodel -> getV(), mmodel -> getF(), mmodel -> getX());
00156         sofa::helper::vector < int > initialPosIndices = monitoring.beginEdit()->getIndPosInit();
00157         sofa::helper::vector < int > initialVelsIndices = monitoring.beginEdit()->getIndVelsInit();
00158         sofa::helper::vector < int > initialForcesIndices = monitoring.beginEdit()->getIndForcesInit();
00159         
00160         monitoring.beginEdit()->setIndPos (initialPosIndices);
00161         monitoring.beginEdit()->setIndVels (initialVelsIndices);
00162         monitoring.beginEdit()->setIndForces (initialForcesIndices);
00163         monitoring.endEdit();
00164         
00165         monitoring.beginEdit()->getSavePos()->clear();
00166         internalDt = 0.0;
00167     }
00169 
00170 
00171 
00173     template<class DataTypes>
00174     void Monitor<DataTypes>::reinit()
00175     {
00176         initGnuplot ( "./" );
00177     }
00179 
00180 
00181 
00183     template<class DataTypes>
00184     void Monitor<DataTypes>::fwdDraw ( Pass pass = Std )
00185     {
00186         if ( pass == core::VisualModel::Std )
00187         {           
00188             monitoring.beginEdit() ->setValues (mmodel -> getV(), mmodel -> getF(), mmodel -> getX());
00189             monitoring.endEdit();
00190             
00191             if ( saveXToGnuplot.getValue() || saveVToGnuplot.getValue() || saveFToGnuplot.getValue() )
00192                 exportGnuplot ( (Real) this ->getTime() );
00193 
00194         }
00195         glDisable(GL_LIGHTING);
00196         if (showPositions.getValue())
00197         {
00198             glPointSize(5.0);
00199             glBegin (GL_POINTS);
00200             glColor3d ((*positionsColor.beginEdit())[0], (*positionsColor.beginEdit())[1], (*positionsColor.beginEdit())[2]);
00201             for (unsigned int i=0; i < monitoring.beginEdit() -> sizeIdxPos(); i++)
00202             {
00203                 Coord posvertex = monitoring.beginEdit() ->getPos(i);
00204                 glVertex3d (posvertex[0], posvertex[1], posvertex[2]);
00205             }
00206             glEnd ();
00207         }
00208         
00209         if (showVelocities.getValue())
00210         {
00211             const VecCoord* mechPos = monitoring.beginEdit() -> getMechPos();
00212             for (unsigned int i=0; i < monitoring.beginEdit() -> sizeIdxVels(); i++)
00213             {
00214             sofa::helper::vector <int> posOfVel = monitoring.beginEdit() -> getIndVels ();
00215             
00216             
00217             Coord baseVelVertex = mechPos [0] [posOfVel[i]];
00218             Deriv topVelVertex = monitoring.beginEdit() -> getVel(i);
00219             
00220             if (vectorNorm(topVelVertex) > showMinThreshold.getValue())
00221             {
00222                 for (unsigned short int j = 0; j < 3; j++)
00223                 {
00224                     topVelVertex[j] = baseVelVertex[j] + topVelVertex[j];
00225                 }
00226                             
00227                 glColor3d ((*velocitiesColor.beginEdit())[0], (*velocitiesColor.beginEdit())[1], (*velocitiesColor.beginEdit())[2]);
00228                 helper::gl::Axis::draw(baseVelVertex, topVelVertex, 0.1);
00229             }
00230             }
00231         }
00232         
00233         if (showForces.getValue() && monitoring.beginEdit() -> getSizeVecForces())
00234         {
00235             const VecCoord* mechPos = monitoring.beginEdit() -> getMechPos();
00236             for (unsigned int i=0; i < monitoring.beginEdit() -> sizeIdxForces(); i++)
00237             {
00238             sofa::helper::vector <int> posOfForces = monitoring.beginEdit() -> getIndForces ();
00239             
00240             Coord baseVelVertex = mechPos [0] [posOfForces[i]];
00241             Deriv topVelVertex = monitoring.beginEdit() -> getVel(i);
00242             if (vectorNorm(topVelVertex) > showMinThreshold.getValue())
00243             {   
00244                 topVelVertex = baseVelVertex + topVelVertex;
00245             
00246                 glColor3d ((*forcesColor.beginEdit())[0], (*forcesColor.beginEdit())[1], (*forcesColor.beginEdit())[2]);
00247                 helper::gl::Axis::draw(baseVelVertex, topVelVertex, 0.2);
00248             }
00249             }
00250         }
00251         
00252         if (showTrajectories.getValue())
00253         {
00254             internalDt += this -> getContext()->getDt();
00255             VecCoord instantPositions;
00256                 
00257             if (trajectoriesPrecision.getValue() <= internalDt)
00258             {
00259                 internalDt = 0.0;
00260                 for (unsigned int i=0; i < monitoring.beginEdit() -> sizeIdxPos(); i++)
00261                 {
00262                     instantPositions.push_back(monitoring.beginEdit() ->getPos(i));
00263                 }
00264                 monitoring.beginEdit()-> getSavePos()->push_back(instantPositions);
00265             }
00266             const unsigned int instantPositionsSize =  (*monitoring.beginEdit()->getMechPos())[0].size();//instantPositions.size();
00267             
00268             //printing those positions
00269             glLineWidth (1);
00270             glColor3d ((*trajectoriesColor.beginEdit())[0], (*trajectoriesColor.beginEdit())[1], (*trajectoriesColor.beginEdit())[2]);
00271             glBegin (GL_LINES);
00272             for (unsigned int i = 1; i < monitoring.beginEdit()->getSavePos()->size(); i++)
00273             {
00274                 for (unsigned int j = 0; j < instantPositionsSize; j++)
00275                 {
00276                     glVertex3d (((*monitoring.beginEdit()->getSavePos())[i-1][j][0]),
00277                                 ((*monitoring.beginEdit()->getSavePos())[i-1][j][1]),
00278                                 ((*monitoring.beginEdit()->getSavePos())[i-1][j][2]));
00279                                 
00280                     glVertex3d (((*monitoring.beginEdit()->getSavePos())[i][j][0]),
00281                                 ((*monitoring.beginEdit()->getSavePos())[i][j][1]),
00282                                 ((*monitoring.beginEdit()->getSavePos())[i][j][2]));    
00283                 }
00284             }
00285             glEnd();
00286             
00287         }
00288         
00289     }
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00299     template<class DataTypes>
00300     void Monitor<DataTypes>::initGnuplot ( const std::string path )
00301     {
00302         if ( !this->getName().empty() )
00303         {
00304             if ( saveXToGnuplot.getValue() )
00305             {
00306                 if ( saveGnuplotX != NULL ) delete saveGnuplotX;
00307                 saveGnuplotX = new std::ofstream ( ( path + this->getName() +"_x.txt" ).c_str() );
00308                 ( *saveGnuplotX ) << "# Gnuplot File : positions of "
00309                 << monitoring.beginEdit()->sizeIdxPos() << " particle(s) monitored"
00310                 <<  endl;
00311                 ( *saveGnuplotX ) << "# 1st Column : time, others : particle(s) number ";
00312                 
00313                 sofa::helper::vector< int > posIdx = monitoring.beginEdit()->getIndPos();
00314                 for (unsigned int i = 0; i < posIdx.size(); i++)
00315                     ( *saveGnuplotX ) << posIdx.at(i) << " ";
00316                 ( *saveGnuplotX ) << endl;
00317                 
00318                 monitoring.endEdit();
00319             }
00320 
00321             if ( saveVToGnuplot.getValue() )
00322             {
00323                 if ( saveGnuplotV != NULL ) delete saveGnuplotV;
00324                 
00325                 saveGnuplotV = new std::ofstream ( ( path + this->getName() +"_v.txt" ).c_str() );
00326                 ( *saveGnuplotV ) << "# Gnuplot File : velocities of "
00327                 << monitoring.beginEdit()->sizeIdxVels() << " particle(s) monitored"
00328                 <<  endl;
00329                 ( *saveGnuplotV ) << "# 1st Column : time, others : particle(s) number ";
00330                 
00331                 sofa::helper::vector< int > velsIdx = monitoring.beginEdit()->getIndVels();
00332                 for (unsigned int i = 0; i < velsIdx.size(); i++)
00333                     ( *saveGnuplotV ) << velsIdx.at(i) << " ";
00334                 ( *saveGnuplotV ) << endl;
00335                 monitoring.endEdit();
00336                 
00337 
00338             }
00339             
00340 
00341             
00342             if ( saveFToGnuplot.getValue() )
00343             {
00344                 if ( saveGnuplotF != NULL ) delete saveGnuplotF;
00345                 saveGnuplotF = new std::ofstream ( ( path + this->getName() +"_f.txt" ).c_str() );
00346                 ( *saveGnuplotF ) << "# Gnuplot File : forces of "
00347                 << monitoring.beginEdit()->sizeIdxForces() << " particle(s) monitored"
00348                 <<  endl;
00349                 ( *saveGnuplotF ) << "# 1st Column : time, others : particle(s) number ";
00350                 
00351                 sofa::helper::vector< int > forcesIdx = monitoring.beginEdit()->getIndForces();
00352                 for (unsigned int i = 0; i < forcesIdx.size(); i++)
00353                     ( *saveGnuplotF ) << forcesIdx.at(i) << " ";
00354                 ( *saveGnuplotF ) << endl;
00355                 monitoring.endEdit();
00356             }
00357 
00358         }
00359     }
00361 
00362 
00363 
00365     template<class DataTypes>
00366     void Monitor<DataTypes>::exportGnuplot ( Real time )
00367     {
00368         if ( saveXToGnuplot.getValue() )
00369         {
00370             ( *saveGnuplotX ) << time <<"\t" ;
00371 
00372             for (unsigned int i = 0; i < monitoring.beginEdit()->sizeIdxPos(); i++)
00373                 ( *saveGnuplotX ) << monitoring.beginEdit() -> getPos(i) << "\t";
00374             ( *saveGnuplotX ) << endl;
00375             monitoring.endEdit();
00376         }
00377         if ( saveVToGnuplot.getValue() && monitoring.beginEdit()->getSizeVecVels() )
00378         {
00379             ( *saveGnuplotV ) << time <<"\t";
00380 
00381             for (unsigned int i = 0; i < monitoring.beginEdit()->sizeIdxVels(); i++)
00382                 ( *saveGnuplotV ) << monitoring.beginEdit() -> getVel(i) << "\t";
00383             ( *saveGnuplotV ) << endl;
00384             monitoring.endEdit();
00385         }
00386         
00387         if ( saveFToGnuplot.getValue() && monitoring.beginEdit()->getSizeVecForces())
00388         {
00389             ( *saveGnuplotF ) << time <<"\t";
00390             
00391             for (unsigned int i = 0; i < monitoring.beginEdit()->sizeIdxForces(); i++)
00392                 ( *saveGnuplotF ) << monitoring.beginEdit() -> getForce (i) << "\t";
00393             ( *saveGnuplotF ) << endl;
00394             monitoring.endEdit();
00395         }
00396     }
00398 
00399 } // namespace misc
00400 
00401 } // namespace component
00402 
00403 } // namespace sofa
00404 
00405 #endif

Generated on Sun Nov 23 03:42:27 2008 for Sofa by  doxygen 1.5.7.1