ReadState.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_READSTATE_INL
00026 #define SOFA_COMPONENT_MISC_READSTATE_INL
00027 
00028 #include <sofa/component/misc/ReadState.h>
00029 #include <sofa/simulation/common/MechanicalVisitor.h>
00030 #include <sofa/simulation/common/UpdateMappingVisitor.h>
00031 
00032 #include <sstream>
00033 
00034 namespace sofa
00035 {
00036 
00037 namespace component
00038 {
00039 
00040 namespace misc
00041 {
00042 
00043 ReadState::ReadState()
00044 : f_filename( initData(&f_filename, "filename", "output file name"))
00045 , f_interval( initData(&f_interval, 0.0, "interval", "time duration between inputs"))
00046 , f_shift( initData(&f_shift, 0.0, "shift", "shift between times in the file and times when they will be read"))
00047 , mmodel(NULL)
00048 , infile(NULL)
00049 , nextTime(0)
00050 {
00051     this->f_listening.setValue(true);
00052 }
00053 
00054 ReadState::~ReadState()
00055 {
00056     if (infile)
00057         delete infile;
00058 }
00059 
00060 void ReadState::init()
00061 {
00062 //     mmodel = dynamic_cast<core::componentmodel::behavior::MechanicalState<DataTypes>*>(this->getContext()->getMechanicalState());
00063     reset();
00064 }
00065 
00066 void ReadState::reset()
00067 {
00068     mmodel = dynamic_cast< sofa::core::componentmodel::behavior::BaseMechanicalState* >(this->getContext()->getMechanicalState());
00069     if (infile)
00070         delete infile;
00071     const std::string& filename = f_filename.getValue();
00072     if (!filename.empty())
00073     {
00074         infile = new std::ifstream(filename.c_str());
00075         if( !infile->is_open() )
00076         {
00077             std::cerr << "Error opening file "<<filename<<std::endl;
00078             delete infile;
00079             infile = NULL;
00080         }
00081     }
00082     nextTime = 0;
00083 }
00084 
00085 void ReadState::handleEvent(sofa::core::objectmodel::Event* event)
00086 {
00087     if (/* simulation::AnimateBeginEvent* ev = */ dynamic_cast<simulation::AnimateBeginEvent*>(event))
00088     {
00089       processReadState();
00090     }
00091     if (/* simulation::AnimateEndEvent* ev = */ dynamic_cast<simulation::AnimateEndEvent*>(event))
00092     {
00093     }
00094 }
00095 
00096 
00097 
00098     void ReadState::setTime(double time)
00099 {
00100  if (time < nextTime) {reset(); nextTime=0.0;}
00101 }
00102 
00103     void ReadState::processReadState(double time)
00104 {
00105  if (time == lastTime) return;
00106  setTime(time);
00107  processReadState(); 
00108 }
00109 
00110     void ReadState::processReadState()
00111 {
00112   bool updated = false;
00113   
00114   if (infile && mmodel)
00115   {
00116     double time = getContext()->getTime() + f_shift.getValue();
00117     lastTime = time;
00118     std::vector<std::string> validLines;
00119     std::string line, cmd;
00120     while (nextTime <= time && !infile->eof())
00121     {
00122       getline(*infile, line);
00123                 //std::cout << "line= "<<line<<std::endl;
00124       std::istringstream str(line);
00125       str >> cmd;
00126       if (cmd == "T=")
00127       {
00128         str >> nextTime;
00129     if (nextTime <= time) validLines.clear();
00130       }
00131 
00132       if (nextTime <= time) validLines.push_back(line);
00133     }
00134 
00135     for (std::vector<std::string>::iterator it=validLines.begin();it!=validLines.end();++it)
00136     {
00137     std::istringstream str(*it);
00138     cmd.clear();
00139     str >> cmd;
00140     if (cmd == "X=")
00141     {
00142     mmodel->readX(str);
00143         updated = true;
00144     }
00145     else if (cmd == "V=")
00146     {
00147     mmodel->readV(str);
00148         updated = true;
00149     }
00150     }
00151   }
00152   if (updated)
00153   {
00154             //std::cout<<"update from file"<<std::endl;
00155     sofa::simulation::MechanicalPropagatePositionAndVelocityVisitor action1;
00156     this->getContext()->executeVisitor(&action1);
00157     sofa::simulation::UpdateMappingVisitor action2;
00158     this->getContext()->executeVisitor(&action2);
00159   }
00160 }
00161 
00162 } // namespace misc
00163 
00164 } // namespace component
00165 
00166 } // namespace sofa
00167 
00168 #endif

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