reader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 /* --------------------------------------------------------------------- */
11 /* --- INCLUDE --------------------------------------------------------- */
12 /* --------------------------------------------------------------------- */
13 
14 /* SOT */
16 #include <dynamic-graph/factory.h>
17 
18 #include <boost/bind.hpp>
19 #include <sot/core/debug.hh>
20 #include <sot/core/reader.hh>
21 #include <sstream>
22 
23 using namespace dynamicgraph;
24 using namespace dynamicgraph::sot;
25 using namespace std;
26 
28 
29 /* --------------------------------------------------------------------- */
30 /* --- CLASS ----------------------------------------------------------- */
31 /* --------------------------------------------------------------------- */
32 
33 sotReader::sotReader(const std::string n)
34  : Entity(n),
35  selectionSIN(NULL, "Reader(" + n + ")::input(flag)::selec"),
36  vectorSOUT(boost::bind(&sotReader::getNextData, this, _1, _2),
37  sotNOSIGNAL, "Reader(" + n + ")::vector"),
38  matrixSOUT(boost::bind(&sotReader::getNextMatrix, this, _1, _2),
39  vectorSOUT, "Reader(" + n + ")::matrix"),
40  dataSet(),
41  currentData(),
42  iteratorSet(false),
43  rows(0),
44  cols(0) {
46  selectionSIN = true;
47  vectorSOUT.setNeedUpdateFromAllChildren(true);
48 
49  initCommands();
50 }
51 
52 /* --------------------------------------------------------------------- */
53 /* --------------------------------------------------------------------- */
54 /* --------------------------------------------------------------------- */
55 
56 void sotReader::load(const string &filename) {
57  sotDEBUGIN(15);
58 
59  std::ifstream datafile(filename.c_str());
60  const std::size_t SIZE = 1024;
61  char buffer[SIZE];
62  std::vector<double> newline;
63  while (datafile.good()) {
64  datafile.getline(buffer, SIZE);
65  const std::size_t gcount = (std::size_t)(datafile.gcount());
66  if (gcount >= SIZE) { /* TODO read error, line to long. */
67  }
68  std::istringstream iss(buffer);
69  newline.clear();
70  sotDEBUG(25) << "Get line = '" << buffer << "'" << std::endl;
71  while (1) {
72  double x;
73  iss >> x;
74  if (!iss.fail())
75  newline.push_back(x);
76  else
77  break;
78  sotDEBUG(45) << "New data = " << x << std::endl;
79  }
80  if (newline.size() > 0) dataSet.push_back(newline);
81  }
82 
83  sotDEBUGOUT(15);
84 }
85 
86 void sotReader::clear(void) {
87  sotDEBUGIN(15);
88 
89  dataSet.clear();
90  iteratorSet = false;
91 
92  sotDEBUGOUT(15);
93 }
94 
95 void sotReader::rewind(void) {
96  sotDEBUGIN(15);
97  iteratorSet = false;
98  sotDEBUGOUT(15);
99 }
100 
102  const std::size_t time) {
103  sotDEBUGIN(15);
104 
105  if (!iteratorSet) {
106  sotDEBUG(15) << "Start the list" << std::endl;
107  currentData = dataSet.begin();
108  iteratorSet = true;
109  } else if (currentData != dataSet.end()) {
110  ++currentData;
111  }
112 
113  if (currentData == dataSet.end()) {
114  sotDEBUGOUT(15);
115  return res;
116  }
117 
118  const Flags &selection = selectionSIN(time);
119  const std::vector<double> &curr = *currentData;
120 
121  std::size_t dim = 0;
122  for (std::size_t i = 0; i < curr.size(); ++i)
123  if (selection(i)) dim++;
124 
125  res.resize(dim);
126  size_type cursor = 0;
127  for (std::size_t i = 0; i < curr.size(); ++i)
128  if (selection(i)) res(cursor++) = curr[i];
129 
130  sotDEBUGOUT(15);
131  return res;
132 }
133 
135  const std::size_t time) {
136  sotDEBUGIN(15);
137  const dynamicgraph::Vector &vect = vectorSOUT(time);
138  if (vect.size() < rows * cols) return res;
139 
140  res.resize(rows, cols);
141  for (size_type i = 0; i < rows; ++i)
142  for (size_type j = 0; j < cols; ++j) res(i, j) = vect(i * cols + j);
143 
144  sotDEBUGOUT(15);
145  return res;
146 }
147 /* --------------------------------------------------------------------- */
148 /* --------------------------------------------------------------------- */
149 /* --------------------------------------------------------------------- */
150 
151 void sotReader::display(std::ostream &os) const {
152  os << CLASS_NAME << " " << name << endl;
153 }
154 
155 std::ostream &operator<<(std::ostream &os, const sotReader &t) {
156  t.display(os);
157  return os;
158 }
159 
160 /* --- Command line interface
161  * ------------------------------------------------------ */
163  namespace dc = ::dynamicgraph::command;
164  addCommand("clear", dc::makeCommandVoid0(*this, &sotReader::clear,
165  "Clear the data loaded"));
166  addCommand("rewind",
167  dc::makeCommandVoid0(
168  *this, &sotReader::rewind,
169  "Reset the iterator to the beginning of the data set"));
170  addCommand("load",
171  dc::makeCommandVoid1(*this, &sotReader::load, "load file"));
172  addCommand("resize", dc::makeCommandVoid2(*this, &sotReader::resize, " "));
173 }
174 
175 void sotReader::resize(const size_type &row, const size_type &col) {
176  rows = row;
177  cols = col;
178 }
dynamicgraph::sot::sotReader::rewind
void rewind(void)
Definition: reader.cpp:95
reader.hh
dynamicgraph::sot::sotReader::load
void load(const std::string &filename)
Definition: reader.cpp:56
dynamicgraph::sot::sotReader::sotReader
sotReader(const std::string n)
Definition: reader.cpp:33
dynamicgraph
dynamicgraph::sot::sotReader::clear
void clear(void)
Definition: reader.cpp:86
i
int i
rows
int rows
dynamicgraph::Entity
boost
dynamicgraph::sot::sotReader::display
void display(std::ostream &os) const
Definition: reader.cpp:151
dynamicgraph::Entity::name
std::string name
dynamicgraph::Matrix
Eigen::MatrixXd Matrix
dynamicgraph::sot::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FeaturePosture, "FeaturePosture")
debug.hh
dynamicgraph::sot::sotReader::getNextData
dynamicgraph::Vector & getNextData(dynamicgraph::Vector &res, const std::size_t time)
Definition: reader.cpp:101
res
res
sotDEBUGOUT
#define sotDEBUGOUT(level)
Definition: debug.hh:215
dim
int dim
filename
filename
sotDEBUGIN
#define sotDEBUGIN(level)
Definition: debug.hh:214
x
x
dynamicgraph::sot::sotReader::getNextMatrix
dynamicgraph::Matrix & getNextMatrix(dynamicgraph::Matrix &res, const std::size_t time)
Definition: reader.cpp:134
dynamicgraph::sot::sotReader::vectorSOUT
SignalTimeDependent< dynamicgraph::Vector, sigtime_t > vectorSOUT
Definition: reader.hh:62
dynamicgraph::sotNOSIGNAL
SignalArray< sigtime_t > sotNOSIGNAL(0)
dynamicgraph::size_type
Matrix::Index size_type
dynamicgraph::Vector
Eigen::VectorXd Vector
dynamicgraph::sot::sotReader::cols
size_type cols
Definition: reader.hh:79
dynamicgraph::sot::sotReader::matrixSOUT
SignalTimeDependent< dynamicgraph::Matrix, sigtime_t > matrixSOUT
Definition: reader.hh:63
dynamicgraph::sot::operator<<
SOT_CORE_EXPORT std::ostream & operator<<(std::ostream &os, const VectorMultiBound &v)
Definition: multi-bound.cpp:228
dynamicgraph::sot::sotReader::rows
size_type rows
Definition: reader.hh:79
dynamicgraph::sot::sotReader::iteratorSet
bool iteratorSet
Definition: reader.hh:77
dynamicgraph::sot::sotReader::initCommands
virtual void initCommands()
Definition: reader.cpp:162
dynamicgraph::sot::sotReader::dataSet
DataType dataSet
Definition: reader.hh:75
dynamicgraph::sot
cols
int cols
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
dynamicgraph::sot::sotReader::selectionSIN
SignalPtr< Flags, sigtime_t > selectionSIN
Definition: reader.hh:61
t
Transform3f t
all-commands.h
dynamicgraph::sot::Flags
Definition: flags.hh:33
dynamicgraph::sot::sotReader::resize
void resize(const size_type &nbRow, const size_type &nbCol)
Definition: reader.cpp:175
dynamicgraph::sot::sotReader::currentData
DataType::const_iterator currentData
Definition: reader.hh:76
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
n
Vec3f n
dynamicgraph::sot::sotReader
Definition: reader.hh:57
sotDEBUG
#define sotDEBUG(level)
Definition: debug.hh:168


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:31