PLYObservationIO.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/PLYObservation.h>
16 #include <nuklei/Common.h>
17 #include <nuklei/Match.h>
18 #include <nuklei/Indenter.h>
19 
20 namespace nuklei {
21 
22 
23 
24  PLYReader::PLYReader(const std::string &observationFileName) :
25  index_(-1), n_(-1), observationFileName_(observationFileName)
26  {
27  }
28 
29  PLYReader::~PLYReader()
30  {
31  }
32 
33 
34 
35  void PLYReader::init_()
36  {
37  NUKLEI_TRACE_BEGIN();
38  NUKLEI_ASSERT(!in_.is_open());
39  in_.open(observationFileName_.c_str(), std::ios::in);
40  std::string dump;
41  n_ = -1;
42  if (!in_.is_open())
43  throw ObservationIOError(std::string("Could not open file ") +
44  observationFileName_ + " for reading.");
45  try {
46  // Note: we only read PLY that *begin* with verticies.
47 
48 
49  std::string line;
50  if (!std::getline(in_, line) || cleanLine(line) != "ply")
51  throw ObservationIOError("Non-PLY format (PLY must start with a line `ply'.");
52 
53  bool format = false, px = false, py = false, pz = false, header = false;
54  while (std::getline(in_, line))
55  {
56  cleanLine(line);
57  std::vector<std::string> tokens;
58  boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
59  if (tokens.front() == "")
60  tokens.erase(tokens.begin());
61  if (tokens.back() == "")
62  tokens.pop_back();
63 
64  if (tokens.size() == 1 && tokens.front() == "end_header")
65  {
66  header = true;
67  break;
68  }
69 
70  if (tokens.size() == 0) continue;
71 
72  if (tokens.front() == "comment") continue;
73  else if (line == "format ascii 1.0") format = true;
74  else if (tokens.front() == "element" && tokens.size() >= 3 &&
75  tokens.at(1) == "vertex")
76  {
77  n_ = numify<int>(tokens.at(2));
78  }
79  else if (tokens.front() == "property" && tokens.size() >= 3 &&
80  tokens.at(2) == "x")
81  px = true;
82  else if (tokens.front() == "property" && tokens.size() >= 3 &&
83  tokens.at(2) == "y")
84  py = true;
85  else if (tokens.front() == "property" && tokens.size() >= 3 &&
86  tokens.at(2) == "z")
87  pz = true;
88  }
89  if ( n_ < 0 || !header )
90  throw ObservationIOError("Non-PLY format.");
91 
92  if (!format || !px || !py || !pz)
93  NUKLEI_WARN("Unsupported PLY header. PLY parsing may not work as expected.");
94 
95  } catch (Error &e) {
96  throw ObservationIOError("Non-PLY format.");
97  }
98  index_ = 0;
99  NUKLEI_TRACE_END();
100  }
101 
102 
103  void PLYReader::reset()
104  {
105  NUKLEI_TRACE_BEGIN();
106  init();
107  NUKLEI_TRACE_END();
108  }
109 
110 
111  NUKLEI_UNIQUE_PTR<Observation> PLYReader::readObservation_()
112  {
113  if (!in_.is_open()) NUKLEI_THROW("Reader does not seem inited.");
114 
115  if (index_ == n_)
116  {
117  // We don't check that we reached the EOF because
118  // there may be more data following (e.g. triangles).
119  return NUKLEI_UNIQUE_PTR<Observation>();
120  }
121  index_++;
122 
123  std::string line;
124 
125  NUKLEI_ASSERT(std::getline(in_, line));
126  cleanLine(line);
127 
128  std::istringstream iss(line);
129 
130  NUKLEI_UNIQUE_PTR<PLYObservation> observation(new PLYObservation);
131 
132  Vector3 loc;
133  NUKLEI_ASSERT(iss >> loc[0] >> loc[1] >> loc[2]);
134  observation->setLoc(loc);
135 
136  RGBColor c(.5,.5,.5);
137  observation->setColor(c);
138 
139  return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
140  }
141 
142 
143 
144 
145  PLYWriter::PLYWriter(const std::string &observationFileName) :
146  observationFileName_(observationFileName)
147  {
148  NUKLEI_TRACE_BEGIN();
149  NUKLEI_TRACE_END();
150  }
151 
152  PLYWriter::~PLYWriter()
153  {
154  }
155 
156  void PLYWriter::init()
157  {
158  NUKLEI_TRACE_BEGIN();
159  try {
160  points_.clear();
161  } catch (std::exception& e) {
162  throw ObservationIOError(e.what());
163  }
164  NUKLEI_TRACE_END();
165  }
166 
167  void PLYWriter::reset()
168  {
169  NUKLEI_TRACE_BEGIN();
170  init();
171  NUKLEI_TRACE_END();
172  }
173 
174 
175  void PLYWriter::writeBuffer()
176  {
177  NUKLEI_TRACE_BEGIN();
178 
179  std::ofstream ofs(observationFileName_.c_str());
180  ofs << "ply\nformat ascii 1.0\nelement vertex " << points_.size() <<
181  "\nproperty float x\nproperty float y\nproperty float z\nend_header\n";
182  for (std::vector<Vector3>::const_iterator i = points_.begin();
183  i != points_.end(); ++i)
184  ofs << stringify(*i, PRECISION) << std::endl;
185 
186  NUKLEI_TRACE_END();
187  }
188 
189  void PLYWriter::writeObservation(const Observation &o)
190  {
191  NUKLEI_TRACE_BEGIN();
192 
193  kernel::base::ptr k = o.getKernel();
194 
195  points_.push_back(k->getLoc());
196 
197  NUKLEI_TRACE_END();
198  }
199 
200 
201 
202 }
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.