/usr/include/sofa/component/misc/ReadState.inl is in libsofa1-dev 1.0~beta4-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | /******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_COMPONENT_MISC_READSTATE_INL
#define SOFA_COMPONENT_MISC_READSTATE_INL
#include <sofa/component/misc/ReadState.h>
#include <sofa/simulation/common/MechanicalVisitor.h>
#include <sofa/simulation/common/UpdateMappingVisitor.h>
#include <sstream>
namespace sofa
{
namespace component
{
namespace misc
{
ReadState::ReadState()
: f_filename( initData(&f_filename, "filename", "output file name"))
, f_interval( initData(&f_interval, 0.0, "interval", "time duration between inputs"))
, f_shift( initData(&f_shift, 0.0, "shift", "shift between times in the file and times when they will be read"))
, f_loop( initData(&f_loop, false, "loop", "set to 'true' to re-read the file when reaching the end"))
, mmodel(NULL)
, infile(NULL)
, nextTime(0)
{
this->f_listening.setValue(true);
}
ReadState::~ReadState()
{
if (infile)
delete infile;
}
void ReadState::init()
{
// mmodel = dynamic_cast<core::componentmodel::behavior::MechanicalState<DataTypes>*>(this->getContext()->getMechanicalState());
reset();
}
void ReadState::reset()
{
mmodel = dynamic_cast< sofa::core::componentmodel::behavior::BaseMechanicalState* >(this->getContext()->getMechanicalState());
if (infile)
delete infile;
const std::string& filename = f_filename.getFullPath();
if (!filename.empty())
{
infile = new std::ifstream(filename.c_str());
if( !infile->is_open() )
{
serr << "Error opening file "<<filename<<sendl;
delete infile;
infile = NULL;
}
}
nextTime = 0;
}
void ReadState::handleEvent(sofa::core::objectmodel::Event* event)
{
if (/* simulation::AnimateBeginEvent* ev = */ dynamic_cast<simulation::AnimateBeginEvent*>(event))
{
processReadState();
}
if (/* simulation::AnimateEndEvent* ev = */ dynamic_cast<simulation::AnimateEndEvent*>(event))
{
}
}
void ReadState::setTime(double time)
{
if (time < nextTime) {reset(); nextTime=0.0;}
}
void ReadState::processReadState(double time)
{
if (time == lastTime) return;
setTime(time);
processReadState();
}
void ReadState::processReadState()
{
static double totalTime = 0.0;
bool updated = false;
if (infile && mmodel)
{
double time = getContext()->getTime() + f_shift.getValue();
lastTime = time;
std::vector<std::string> validLines;
std::string line, cmd;
while (nextTime <= time && !infile->eof())
{
getline(*infile, line);
//sout << "line= "<<line<<sendl;
std::istringstream str(line);
str >> cmd;
if (cmd == "T=")
{
str >> nextTime;
nextTime += totalTime;
if (nextTime <= time)
validLines.clear();
}
if (nextTime <= time)
validLines.push_back(line);
}
for (std::vector<std::string>::iterator it=validLines.begin();it!=validLines.end();++it)
{
std::istringstream str(*it);
cmd.clear();
str >> cmd;
if (cmd == "X=")
{
mmodel->readX(str);
updated = true;
}
else if (cmd == "V=")
{
mmodel->readV(str);
updated = true;
}
}
}
if (f_loop.getValue() && infile->eof())
{
infile->clear();
infile->seekg(0);
totalTime = nextTime;
}
if (updated) {
//sout<<"update from file"<<sendl;
sofa::simulation::MechanicalPropagatePositionAndVelocityVisitor action1;
this->getContext()->executeVisitor(&action1);
sofa::simulation::UpdateMappingVisitor action2;
this->getContext()->executeVisitor(&action2);
}
}
} // namespace misc
} // namespace component
} // namespace sofa
#endif
|