TxtObservationIO.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #include <fstream>
8 #include <cassert>
9 #include <algorithm>
10 #include <cmath>
11 #include <boost/tuple/tuple.hpp>
12 #include <boost/algorithm/string.hpp>
13 
15 #include <nuklei/TxtObservation.h>
16 
17 namespace nuklei {
18 
19 
20 
21  TxtReader::TxtReader(const std::string &observationFileName) :
22  observationFileName(observationFileName)
23  {
24  }
25 
26  TxtReader::~TxtReader()
27  {
28  }
29 
30 
31 
32  void TxtReader::init_()
33  {
34  NUKLEI_ASSERT(!in_.is_open());
35  in_.open(observationFileName.c_str(), std::ios::in);
36  if (!in_.is_open())
37  throw ObservationIOError(std::string("Could not open file ") +
38  observationFileName + " for reading.");
39 
40  std::string line;
41  int ntok = -1;
42  while (std::getline(in_, line))
43  {
44  cleanLine(line);
45  std::vector<std::string> tokens;
46  boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
47  if (tokens.front() == "")
48  tokens.erase(tokens.begin());
49  if (tokens.back() == "")
50  tokens.pop_back();
51  if (tokens.size() == 0)
52  {
53  if (in_.good())
54  throw ObservationIOError("Unexpected empty line.");
55  }
56  if (tokens.size() == 3)
57  {
58  if (ntok == -1) ntok = 3;
59  else if (ntok != 3) throw ObservationIOError("Wrong number of tokens.");
60  }
61  else if (tokens.size() == 6)
62  {
63  if (ntok == -1) ntok = 7;
64  else if (ntok != 7) throw ObservationIOError("Wrong number of tokens.");
65  }
66  else if (tokens.size() == 7)
67  {
68  if (ntok == -1) ntok = 7;
69  else if (ntok != 7) throw ObservationIOError("Wrong number of tokens.");
70  }
71  else {
72  throw ObservationIOError("Wrong number of tokens on line (" + stringify(tokens.size()) + ")");
73  }
74  }
75 
76  in_.close();
77  in_.open(observationFileName.c_str(), std::ios::in);
78  if (!in_.is_open())
79  throw ObservationIOError(std::string("Could not open file ") +
80  observationFileName + " for reading.");
81  }
82 
83 
84  void TxtReader::reset()
85  {
86  in_.close();
87  init();
88  }
89 
90 
91  NUKLEI_UNIQUE_PTR<Observation> TxtReader::readObservation_()
92  {
93  if (!in_.is_open()) NUKLEI_THROW("Reader does not seem inited.");
94 
95  if (!in_.good()) return NUKLEI_UNIQUE_PTR<Observation>();
96 
97  std::string line;
98 
99  while (std::getline(in_, line))
100  {
101  cleanLine(line);
102  NUKLEI_UNIQUE_PTR<TxtObservation> observation(new TxtObservation);
103 
104  std::vector<std::string> tokens;
105  boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
106  if (tokens.front() == "")
107  tokens.erase(tokens.begin());
108  if (tokens.back() == "")
109  tokens.pop_back();
110  if (tokens.size() == 0)
111  {
112  // end of file
113  }
114  if (tokens.size() == 3)
115  {
116  kernel::r3 k;
117  k.loc_ = Vector3(numify<double>(tokens.at(0)), numify<double>(tokens.at(1)), numify<double>(tokens.at(2)));
118  observation->setKernel(k);
119  }
120  else if (tokens.size() == 6)
121  {
122  kernel::r3xs2p k;
123  k.loc_ = Vector3(numify<double>(tokens.at(0)), numify<double>(tokens.at(1)), numify<double>(tokens.at(2)));
124  k.dir_ = la::normalized(Vector3(numify<double>(tokens.at(3)),
125  numify<double>(tokens.at(4)),
126  numify<double>(tokens.at(5))
127  )
128  );
129  observation->setKernel(k);
130  }
131  else if (tokens.size() == 7)
132  {
133  kernel::se3 k;
134  k.loc_ = Vector3(numify<double>(tokens.at(0)), numify<double>(tokens.at(1)), numify<double>(tokens.at(2)));
135  k.ori_ = la::normalized(Quaternion(numify<double>(tokens.at(3)),
136  numify<double>(tokens.at(4)),
137  numify<double>(tokens.at(5)),
138  numify<double>(tokens.at(6))
139  )
140  );
141  observation->setKernel(k);
142  }
143  else {
144  NUKLEI_THROW("Wrong number of tokens on line (" << tokens.size() << ")");
145  }
146 
147  return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
148  }
149 
150  // End of file reached.
151  return NUKLEI_UNIQUE_PTR<Observation>();
152  }
153 
154 
155 
156 
157  TxtWriter::TxtWriter(const std::string &observationFileName) :
158  observationFileName_(observationFileName)
159  {
160  NUKLEI_TRACE_BEGIN();
161  NUKLEI_TRACE_END();
162  }
163 
164  TxtWriter::~TxtWriter()
165  {
166  }
167 
168  void TxtWriter::init()
169  {
170  NUKLEI_TRACE_BEGIN();
171  try {
172  points_.clear();
173  } catch (std::exception& e) {
174  throw ObservationIOError(e.what());
175  }
176  NUKLEI_TRACE_END();
177  }
178 
179  void TxtWriter::reset()
180  {
181  NUKLEI_TRACE_BEGIN();
182  init();
183  NUKLEI_TRACE_END();
184  }
185 
186 
187  void TxtWriter::writeBuffer()
188  {
189  NUKLEI_TRACE_BEGIN();
190 
191  std::ofstream ofs(observationFileName_.c_str());
192  for (KernelCollection::iterator i = points_.begin(); i != points_.end(); ++i)
193  {
194  {
195  kernel::r3* k = dynamic_cast<kernel::r3*>(&*i);
196  if (k != NULL)
197  {
198  ofs << stringify(k->loc_, PRECISION) << std::endl;
199  continue;
200  }
201  }
202  {
203  kernel::r3xs2p* k = dynamic_cast<kernel::r3xs2p*>(&*i);
204  if (k != NULL)
205  {
206  ofs << stringify(k->loc_, PRECISION) << " " << stringify(k->dir_, PRECISION) << std::endl;
207  continue;
208  }
209  }
210  {
211  kernel::se3* k = dynamic_cast<kernel::se3*>(&*i);
212  if (k != NULL)
213  {
214  ofs << stringify(k->loc_, PRECISION) << " " << stringify(k->ori_, PRECISION) << std::endl;
215  continue;
216  }
217  }
218  NUKLEI_THROW("Unsupported kernel.");
219  }
220 
221  NUKLEI_TRACE_END();
222  }
223 
224  void TxtWriter::writeObservation(const Observation &o)
225  {
226  NUKLEI_TRACE_BEGIN();
227 
228  kernel::base::ptr k = o.getKernel();
229 
230  points_.add(*k);
231 
232  NUKLEI_TRACE_END();
233  }
234 
235 
236 
237 
238 }
Public namespace.
Definition: Color.cpp:9
std::string stringify(const T &x, int precision=-1, int width=0)
Converts an object to a std::string using operator<<.
Definition: Common.h:333
r3xs2_base< groupS::s2p > r3xs2p
Definition: Kernel.h:623
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.