OffObservationIO.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/OffObservation.h>
15 
16 namespace nuklei {
17 
18 
19 
20  OffReader::OffReader(const std::string &observationFileName) :
21  observationFileName(observationFileName), count_(0)
22  {
23  }
24 
25  OffReader::~OffReader()
26  {
27  }
28 
29 
30 
31  void OffReader::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  {
41  std::string firstline;
42  if (! std::getline(in_, firstline) )
43  throw ObservationIOError("Input does not look like OFF (file read error).");
44  cleanLine(firstline);
45  if (firstline != "OFF")
46  throw ObservationIOError("Input does not look like OFF (marker error).");
47  if (! std::getline(in_, firstline) )
48  throw ObservationIOError("Input does not look like OFF (file read error).");
49  try {
50  count = numify<int>(firstline, false);
51  } catch (BadStrNumConversion &e)
52  {
53  throw ObservationIOError("Input does not look like OFF (no point count).");
54  }
55  if (!(count >= 0))
56  throw ObservationIOError("Input does not look like OFF (no point count).");
57  }
58  count_ = count;
59  }
60 
61 
62  void OffReader::reset()
63  {
64  in_.close();
65  init();
66  }
67 
68 
69  NUKLEI_UNIQUE_PTR<Observation> OffReader::readObservation_()
70  {
71  if (!in_.is_open()) NUKLEI_THROW("Reader does not seem inited.");
72 
73  if (!in_.good()) return NUKLEI_UNIQUE_PTR<Observation>();
74 
75  std::string line;
76 
77  if (count_ > 0)
78  {
79  if (!std::getline(in_, line))
80  {
81  throw ObservationIOError("Unexpected end of file.");
82  }
83  cleanLine(line);
84 
85  std::istringstream iss(line);
86 
87  NUKLEI_UNIQUE_PTR<OffObservation> observation(new OffObservation);
88 
89  Vector3 loc;
90  if ( ! (iss >> loc[0] >> loc[1] >> loc[2]) )
91  throw ObservationIOError("Parsing error.");
92  observation->setLoc(loc);
93 
94  RGBColor c(.5,.5,.5);
95  observation->setColor(c);
96 
97  count_--;
98 
99  return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
100  }
101 
102  // End of file reached.
103  return NUKLEI_UNIQUE_PTR<Observation>();
104  }
105 
106 
107 
108 
109  OffWriter::OffWriter(const std::string &observationFileName) :
110  observationFileName_(observationFileName)
111  {
112  NUKLEI_TRACE_BEGIN();
113  NUKLEI_TRACE_END();
114  }
115 
116  OffWriter::~OffWriter()
117  {
118  }
119 
120  void OffWriter::init()
121  {
122  NUKLEI_TRACE_BEGIN();
123  try {
124  points_.clear();
125  } catch (std::exception& e) {
126  throw ObservationIOError(e.what());
127  }
128  NUKLEI_TRACE_END();
129  }
130 
131  void OffWriter::reset()
132  {
133  NUKLEI_TRACE_BEGIN();
134  init();
135  NUKLEI_TRACE_END();
136  }
137 
138 
139  void OffWriter::writeBuffer()
140  {
141  NUKLEI_TRACE_BEGIN();
142 
143  std::ofstream ofs(observationFileName_.c_str());
144  ofs << "OFF\n";
145  ofs << points_.size() << " 0 0\n"; // we have 0 faces
146  for (std::vector<Vector3>::const_iterator i = points_.begin();
147  i != points_.end(); ++i)
148  ofs << stringify(*i, PRECISION) << std::endl;
149 
150  NUKLEI_TRACE_END();
151  }
152 
153  void OffWriter::writeObservation(const Observation &o)
154  {
155  NUKLEI_TRACE_BEGIN();
156 
157  kernel::base::ptr k = o.getKernel();
158 
159  points_.push_back(k->getLoc());
160 
161  NUKLEI_TRACE_END();
162  }
163 
164 
165 
166 
167 }
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
#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.