CompareState.cpp

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 
00026 #ifndef SOFA_COMPONENT_MISC_COMPARESTATE_INL
00027 #define SOFA_COMPONENT_MISC_COMPARESTATE_INL
00028 
00029 #include <sofa/component/misc/CompareState.h>
00030 #include <sofa/simulation/common/MechanicalVisitor.h>
00031 #include <sofa/simulation/common/UpdateMappingVisitor.h>
00032 
00033 #include <sstream>
00034 #include <sofa/core/ObjectFactory.h>
00035 
00036 namespace sofa
00037 {
00038 
00039 namespace component
00040 {
00041 
00042 namespace misc
00043 {
00044 
00045 
00046 
00047     int CompareStateClass = core::RegisterObject("Compare State vectors from a reference frame to the associated Mechanical State")
00048     .add< CompareState >();
00049 
00050       CompareState::CompareState(): ReadState()
00051       {
00052     totalError_X=0.0;
00053     totalError_V=0.0;
00054       }
00055 
00056 
00057 //-------------------------------- handleEvent-------------------------------------------
00058       void CompareState::handleEvent(sofa::core::objectmodel::Event* event)
00059       {
00060     if (/* simulation::AnimateBeginEvent* ev = */ dynamic_cast<simulation::AnimateBeginEvent*>(event))
00061       {
00062         processCompareState();
00063       }
00064     if (/* simulation::AnimateEndEvent* ev = */ dynamic_cast<simulation::AnimateEndEvent*>(event))
00065           {
00066           }
00067       }
00068 
00069 //-------------------------------- processCompareState------------------------------------
00070       void CompareState::processCompareState()
00071       {  
00072     if (infile && mmodel)
00073       {
00074         double time = getContext()->getTime() + f_shift.getValue();
00075         lastTime = time;
00076         std::vector<std::string> validLines;
00077         std::string line, cmd;
00078         while (nextTime <= time && !infile->eof())
00079           {
00080         getline(*infile, line);
00081                 std::istringstream str(line);
00082         str >> cmd;
00083         if (cmd == "T=")
00084           {
00085             str >> nextTime;
00086             if (nextTime <= time) validLines.clear();
00087           }
00088 
00089         if (nextTime <= time) validLines.push_back(line);
00090           }
00091 
00092         for (std::vector<std::string>::iterator it=validLines.begin();it!=validLines.end();++it)
00093           {
00094         std::istringstream str(*it);
00095         cmd.clear();
00096         str >> cmd;
00097         if (cmd == "X=")
00098           {         
00099             totalError_X += mmodel->compareX(str);
00100           }
00101         else if (cmd == "V=")
00102           {
00103             totalError_V += mmodel->compareV(str);
00104           }
00105           }
00106       }
00107       }
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 //Create a Compare State component each time a mechanical state is found
00116 simulation::Visitor::Result CompareStateCreator::processNodeTopDown( simulation::Node* gnode)
00117 {
00118     using namespace sofa::defaulttype;
00119     sofa::core::componentmodel::behavior::BaseMechanicalState * mstate = dynamic_cast<sofa::core::componentmodel::behavior::BaseMechanicalState *>( gnode->getMechanicalState());
00120     if (!mstate)   return simulation::Visitor::RESULT_CONTINUE; 
00121     //We have a mechanical state
00122     addCompareState(mstate, gnode);
00123     return simulation::Visitor::RESULT_CONTINUE; 
00124 }
00125 
00126   
00127   
00128 void CompareStateCreator::addCompareState(sofa::core::componentmodel::behavior::BaseMechanicalState *ms, simulation::Node* gnode)
00129 {
00130     
00131     sofa::core::objectmodel::BaseContext* context = gnode->getContext();    
00132     sofa::core::BaseMapping *mapping; context->get(mapping);
00133     if (createInMapping || mapping== NULL)
00134     {
00135         sofa::component::misc::CompareState *rs; context->get(rs);
00136         if (  rs == NULL ) 
00137         {
00138             rs = new sofa::component::misc::CompareState(); gnode->addObject(rs);
00139         }
00140       
00141         std::ostringstream ofilename;
00142         ofilename << sceneName << "_" << counterCompareState << "_" << ms->getName()  << "_mstate.txt" ;
00143       
00144         rs->f_filename.setValue(ofilename.str());  rs->f_listening.setValue(false); //Desactivated only called by extern functions      
00145         if (init) rs->init();
00146         
00147         ++counterCompareState;
00148     }
00149 }
00150     
00151       
00152   
00153 //Create a Compare State component each time a mechanical state is found
00154 simulation::Visitor::Result CompareStateResult::processNodeTopDown( simulation::Node* gnode)
00155 {
00156     sofa::component::misc::CompareState *cv;
00157     gnode->get(cv);
00158     if (!cv)   return simulation::Visitor::RESULT_CONTINUE; 
00159     //We have a mechanical state
00160     error += cv->getError();
00161     return simulation::Visitor::RESULT_CONTINUE; 
00162 }
00163 
00164 
00165 
00166 } // namespace misc
00167 
00168 } // namespace component
00169 
00170 } // namespace sofa
00171 
00172 #endif

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