GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/traces/reader.cpp Lines: 0 69 0.0 %
Date: 2023-03-13 12:09:37 Branches: 0 130 0.0 %

Line Branch Exec Source
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 */
15
#include <dynamic-graph/all-commands.h>
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
27
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(sotReader, "Reader");
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) {
45
  signalRegistration(selectionSIN << vectorSOUT << matrixSOUT);
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 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
101
dynamicgraph::Vector &sotReader::getNextData(dynamicgraph::Vector &res,
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
134
dynamicgraph::Matrix &sotReader::getNextMatrix(dynamicgraph::Matrix &res,
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
 * ------------------------------------------------------ */
162
void sotReader::initCommands() {
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
}