ReadState.inl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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 ( dynamic_cast<simulation::AnimateBeginEvent*>(event))
00088 {
00089 processReadState();
00090 }
00091 if ( 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
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
00155 sofa::simulation::MechanicalPropagatePositionAndVelocityVisitor action1;
00156 this->getContext()->executeVisitor(&action1);
00157 sofa::simulation::UpdateMappingVisitor action2;
00158 this->getContext()->executeVisitor(&action2);
00159 }
00160 }
00161
00162 }
00163
00164 }
00165
00166 }
00167
00168 #endif