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;
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 unsigned int SIZE = 1024;
61  char buffer[SIZE];
62  std::vector<double> newline;
63  while (datafile.good()) {
64  datafile.getline(buffer, SIZE);
65  const unsigned int gcount = (unsigned int)(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 unsigned int 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  unsigned int dim = 0;
122  for (unsigned int i = 0; i < curr.size(); ++i)
123  if (selection(i)) dim++;
124 
125  res.resize(dim);
126  int cursor = 0;
127  for (unsigned int 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 unsigned int 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 (int i = 0; i < rows; ++i)
142  for (int 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 int &row, const int &col) {
176  rows = row;
177  cols = col;
178 }
SignalTimeDependent< dynamicgraph::Matrix, int > matrixSOUT
Definition: reader.hh:65
Eigen::VectorXd Vector
Vec3f n
void load(const std::string &filename)
Definition: reader.cpp:56
sotReader(const std::string n)
Definition: reader.cpp:33
bool iteratorSet
Definition: reader.hh:79
int i
int rows
SignalArray< int > sotNOSIGNAL(0)
void signalRegistration(const SignalArray< int > &signals)
#define sotDEBUGOUT(level)
Definition: debug.hh:212
dynamicgraph::Vector & getNextData(dynamicgraph::Vector &res, const unsigned int time)
Definition: reader.cpp:101
void setNeedUpdateFromAllChildren(const bool b=true)
DataType::const_iterator currentData
Definition: reader.hh:78
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FeaturePosture, "FeaturePosture")
int dim
dynamicgraph::Matrix & getNextMatrix(dynamicgraph::Matrix &res, const unsigned int time)
Definition: reader.cpp:134
#define sotDEBUGIN(level)
Definition: debug.hh:211
void display(std::ostream &os) const
Definition: reader.cpp:151
virtual void initCommands()
Definition: reader.cpp:162
SignalTimeDependent< dynamicgraph::Vector, int > vectorSOUT
Definition: reader.hh:64
std::ostream & operator<<(std::ostream &os, const sotReader &t)
Definition: reader.cpp:155
void rewind(void)
Definition: reader.cpp:95
x
DataType dataSet
Definition: reader.hh:77
#define sotDEBUG(level)
Definition: debug.hh:165
Eigen::MatrixXd Matrix
void resize(const int &nbRow, const int &nbCol)
Definition: reader.cpp:175
int cols
Transform3f t
void clear(void)
Definition: reader.cpp:86
SignalPtr< Flags, int > selectionSIN
Definition: reader.hh:63
void addCommand(const std::string &name, command::Command *command)
int rows
Definition: reader.hh:81
int cols
Definition: reader.hh:81


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Wed Jun 21 2023 02:51:26