11 #include <boost/tuple/tuple.hpp>
12 #include <boost/algorithm/string.hpp>
24 PLYReader::PLYReader(
const std::string &observationFileName) :
25 index_(-1), n_(-1), observationFileName_(observationFileName)
29 PLYReader::~PLYReader()
35 void PLYReader::init_()
39 in_.open(observationFileName_.c_str(), std::ios::in);
43 throw ObservationIOError(std::string(
"Could not open file ") +
44 observationFileName_ +
" for reading.");
50 if (!std::getline(in_, line) || cleanLine(line) !=
"ply")
51 throw ObservationIOError(
"Non-PLY format (PLY must start with a line `ply'.");
53 bool format =
false, px =
false, py =
false, pz =
false, header =
false;
54 while (std::getline(in_, 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() ==
"")
64 if (tokens.size() == 1 && tokens.front() ==
"end_header")
70 if (tokens.size() == 0)
continue;
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")
77 n_ = numify<int>(tokens.at(2));
79 else if (tokens.front() ==
"property" && tokens.size() >= 3 &&
82 else if (tokens.front() ==
"property" && tokens.size() >= 3 &&
85 else if (tokens.front() ==
"property" && tokens.size() >= 3 &&
89 if ( n_ < 0 || !header )
90 throw ObservationIOError(
"Non-PLY format.");
92 if (!format || !px || !py || !pz)
93 NUKLEI_WARN(
"Unsupported PLY header. PLY parsing may not work as expected.");
96 throw ObservationIOError(
"Non-PLY format.");
103 void PLYReader::reset()
105 NUKLEI_TRACE_BEGIN();
111 NUKLEI_UNIQUE_PTR<Observation> PLYReader::readObservation_()
113 if (!in_.is_open())
NUKLEI_THROW(
"Reader does not seem inited.");
119 return NUKLEI_UNIQUE_PTR<Observation>();
128 std::istringstream iss(line);
130 NUKLEI_UNIQUE_PTR<PLYObservation> observation(
new PLYObservation);
134 observation->setLoc(loc);
136 RGBColor c(.5,.5,.5);
137 observation->setColor(c);
139 return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
145 PLYWriter::PLYWriter(
const std::string &observationFileName) :
146 observationFileName_(observationFileName)
148 NUKLEI_TRACE_BEGIN();
152 PLYWriter::~PLYWriter()
156 void PLYWriter::init()
158 NUKLEI_TRACE_BEGIN();
161 }
catch (std::exception& e) {
162 throw ObservationIOError(e.what());
167 void PLYWriter::reset()
169 NUKLEI_TRACE_BEGIN();
175 void PLYWriter::writeBuffer()
177 NUKLEI_TRACE_BEGIN();
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;
189 void PLYWriter::writeObservation(
const Observation &o)
191 NUKLEI_TRACE_BEGIN();
193 kernel::base::ptr k = o.getKernel();
195 points_.push_back(k->getLoc());