CrdObservationIO.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 
14 #include <nuklei/CrdObservation.h>
15 
16 namespace nuklei {
17 
18 
19 
20  CrdReader::CrdReader(const std::string &observationFileName) :
21  observationFileName(observationFileName)
22  {
23  }
24 
25  CrdReader::~CrdReader()
26  {
27  }
28 
29 
30 
31  void CrdReader::init_()
32  {
33  NUKLEI_ASSERT(!in_.is_open());
34  in_.open(observationFileName.c_str(), std::ios::in);
35  if (!in_.is_open())
36  throw ObservationIOError(std::string("Could not open file ") +
37  observationFileName + " for reading.");
38 
39  int count = -1;
40  bool syncpc = false;
41  // In a CRD file, the first line shows the number of points in the file.
42  // To be compatible with SYNCPC, if the first line is syncpc, it is ignored.
43  {
44  std::string firstline;
45  if (! std::getline(in_, firstline) )
46  throw ObservationIOError("Input does not look like CRD (file read error).");
47  cleanLine(firstline);
48  if (firstline == "syncpc")
49  {
50  if (! std::getline(in_, firstline) )
51  throw ObservationIOError("Input does not look like CRD (file read error).");
52  syncpc = true;
53  }
54  try {
55  count = numify<int>(firstline);
56  } catch (BadStrNumConversion &e)
57  {
58  throw ObservationIOError("Input does not look like CRD (no point count).");
59  }
60  if (!(count >= 0))
61  throw ObservationIOError("Input does not look like CRD (no point count).");
62  }
63  //if (! (in_ >> count) || ! (count >= 0) )
64  // throw ObservationIOError("Input does not look like CRD.");
65  in_.close();
66 
67  // Since starting with an int is a pretty weak file signature,
68  // we run a thourough line count.
69 
70  in_.open(observationFileName.c_str(), std::ios::in);
71  int lcount = 0; std::string dump;
72  while (std::getline(in_, dump)) lcount++;
73  if (lcount != count+1 + (syncpc?1:0))
74  throw ObservationIOError("Input does not look like CRD (wrong count).");
75  in_.close();
76 
77  in_.open(observationFileName.c_str(), std::ios::in);
78  std::string line;
79  std::getline(in_, line);
80  if (syncpc) std::getline(in_, line);
81  }
82 
83 
84  void CrdReader::reset()
85  {
86  in_.close();
87  init();
88  }
89 
90 
91  NUKLEI_UNIQUE_PTR<Observation> CrdReader::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  std::istringstream iss(line);
103 
104  NUKLEI_UNIQUE_PTR<CrdObservation> observation(new CrdObservation);
105 
106  Vector3 loc;
107  if ( ! (iss >> loc[0] >> loc[1] >> loc[2]) ) return NUKLEI_UNIQUE_PTR<Observation>();
108  observation->setLoc(loc);
109 
110  Vector3 rgbColor(.5,.5,.5);
111  coord_t ix(0), iy(0);
112  if ( (iss >> ix >> iy >> rgbColor[0] >> rgbColor[1] >> rgbColor[2]) )
113  {
114  RGBColor c(rgbColor/255.);
115  observation->setColor(c);
116  }
117  else
118  {
119  RGBColor c(.5,.5,.5);
120  observation->setColor(c);
121  }
122 
123  return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
124  }
125 
126  // End of file reached.
127  return NUKLEI_UNIQUE_PTR<Observation>();
128  }
129 
130 
131 
132 
133  CrdWriter::CrdWriter(const std::string &observationFileName, bool syncpc) :
134  observationFileName_(observationFileName), syncpc_(syncpc)
135  {
136  NUKLEI_TRACE_BEGIN();
137  NUKLEI_TRACE_END();
138  }
139 
140  CrdWriter::~CrdWriter()
141  {
142  }
143 
144  void CrdWriter::init()
145  {
146  NUKLEI_TRACE_BEGIN();
147  try {
148  points_.clear();
149  } catch (std::exception& e) {
150  throw ObservationIOError(e.what());
151  }
152  NUKLEI_TRACE_END();
153  }
154 
155  void CrdWriter::reset()
156  {
157  NUKLEI_TRACE_BEGIN();
158  init();
159  NUKLEI_TRACE_END();
160  }
161 
162 
163  void CrdWriter::writeBuffer()
164  {
165  NUKLEI_TRACE_BEGIN();
166 
167  std::ofstream ofs(observationFileName_.c_str());
168  if (syncpc_)
169  {
170  ofs << "syncpc\n";
171  }
172  ofs << points_.size() << "\n";
173  for (std::vector<Vector3>::const_iterator i = points_.begin();
174  i != points_.end(); ++i)
175  ofs << stringify(*i, PRECISION) << std::endl;
176 
177  NUKLEI_TRACE_END();
178  }
179 
180  void CrdWriter::writeObservation(const Observation &o)
181  {
182  NUKLEI_TRACE_BEGIN();
183 
184  kernel::base::ptr k = o.getKernel();
185 
186  points_.push_back(k->getLoc());
187 
188  NUKLEI_TRACE_END();
189  }
190 
191 
192 
193 
194 }
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
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
NUKLEI_UNIQUE_PTR< kernel::base > ptr
NUKLEI_UNIQUE_PTR for kernel::base.
Definition: Kernel.h:50
double coord_t
Type for point coordinates.
Definition: Definitions.h:25
#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.