ReadState.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 #include <sofa/component/misc/ReadState.inl>
00026 #include <sofa/core/ObjectFactory.h>
00027 
00028 namespace sofa
00029 {
00030 
00031 namespace component
00032 {
00033 
00034 namespace misc
00035 {
00036 
00037 SOFA_DECL_CLASS(ReadState)
00038 
00039 using namespace defaulttype;
00040 
00041 int ReadStateClass = core::RegisterObject("Read State vectors from file at each timestep")
00042 .add< ReadState >();
00043 
00044   
00045 //Create a Read State component each time a mechanical state is found
00046 simulation::Visitor::Result ReadStateCreator::processNodeTopDown( simulation::Node* gnode)
00047 {
00048     using namespace sofa::defaulttype;
00049     sofa::core::componentmodel::behavior::BaseMechanicalState * mstate = dynamic_cast<sofa::core::componentmodel::behavior::BaseMechanicalState *>( gnode->getMechanicalState());
00050     if (!mstate)   return Visitor::RESULT_CONTINUE; 
00051     //We have a mechanical state
00052     addReadState(mstate, gnode);
00053     return simulation::Visitor::RESULT_CONTINUE; 
00054 }
00055 
00056 void ReadStateCreator::addReadState(sofa::core::componentmodel::behavior::BaseMechanicalState *ms, simulation::Node* gnode)
00057 {
00058     sofa::core::objectmodel::BaseContext* context = gnode->getContext();    
00059     sofa::core::BaseMapping *mapping; context->get(mapping);
00060     if (createInMapping || mapping== NULL)
00061     {
00062     sofa::component::misc::ReadState *rs; context->get(rs);
00063     if (  rs == NULL ) 
00064     {
00065         rs = new sofa::component::misc::ReadState(); gnode->addObject(rs);
00066     }
00067     
00068     std::ostringstream ofilename;
00069     ofilename << sceneName << "_" << counterReadState << "_" << ms->getName()  << "_mstate.txt" ;
00070     
00071     rs->f_filename.setValue(ofilename.str());  rs->f_listening.setValue(false); //Desactivated only called by extern functions      
00072     if (init) rs->init();
00073     
00074     ++counterReadState;
00075     }
00076 }
00077   
00079 simulation::Visitor::Result ReadStateActivator::processNodeTopDown( simulation::Node* gnode)
00080 {
00081     sofa::component::misc::ReadState *rs = gnode->get< sofa::component::misc::ReadState >();
00082     if (rs != NULL) { changeStateReader(rs);} 
00083     
00084     return simulation::Visitor::RESULT_CONTINUE;
00085 }
00086 
00087 void ReadStateActivator::changeStateReader(sofa::component::misc::ReadState* rs)
00088 {
00089     rs->reset();  
00090     rs->f_listening.setValue(state);
00091 }
00092 
00093 
00094 //if state is true, we activate all the write states present in the scene. If not, we activate all the readers.
00095 simulation::Visitor::Result ReadStateModifier::processNodeTopDown( simulation::Node* gnode)
00096 {
00097     using namespace sofa::defaulttype;
00098     
00099     sofa::component::misc::ReadState*rs = gnode->get< sofa::component::misc::ReadState>();
00100     if (rs != NULL) {changeTimeReader(rs);}
00101     
00102     return simulation::Visitor::RESULT_CONTINUE;
00103 }
00104 
00105 } // namespace misc
00106 
00107 } // namespace component
00108 
00109 } // namespace sofa

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