11 #include <boost/tuple/tuple.hpp>
12 #include <boost/algorithm/string.hpp>
21 TxtReader::TxtReader(
const std::string &observationFileName) :
22 observationFileName(observationFileName)
26 TxtReader::~TxtReader()
32 void TxtReader::init_()
35 in_.open(observationFileName.c_str(), std::ios::in);
37 throw ObservationIOError(std::string(
"Could not open file ") +
38 observationFileName +
" for reading.");
42 while (std::getline(in_, 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() ==
"")
51 if (tokens.size() == 0)
54 throw ObservationIOError(
"Unexpected empty line.");
56 if (tokens.size() == 3)
58 if (ntok == -1) ntok = 3;
59 else if (ntok != 3)
throw ObservationIOError(
"Wrong number of tokens.");
61 else if (tokens.size() == 6)
63 if (ntok == -1) ntok = 7;
64 else if (ntok != 7)
throw ObservationIOError(
"Wrong number of tokens.");
66 else if (tokens.size() == 7)
68 if (ntok == -1) ntok = 7;
69 else if (ntok != 7)
throw ObservationIOError(
"Wrong number of tokens.");
72 throw ObservationIOError(
"Wrong number of tokens on line (" +
stringify(tokens.size()) +
")");
77 in_.open(observationFileName.c_str(), std::ios::in);
79 throw ObservationIOError(std::string(
"Could not open file ") +
80 observationFileName +
" for reading.");
84 void TxtReader::reset()
91 NUKLEI_UNIQUE_PTR<Observation> TxtReader::readObservation_()
93 if (!in_.is_open())
NUKLEI_THROW(
"Reader does not seem inited.");
95 if (!in_.good())
return NUKLEI_UNIQUE_PTR<Observation>();
99 while (std::getline(in_, line))
102 NUKLEI_UNIQUE_PTR<TxtObservation> observation(
new TxtObservation);
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() ==
"")
110 if (tokens.size() == 0)
114 if (tokens.size() == 3)
117 k.loc_ = Vector3(numify<double>(tokens.at(0)), numify<double>(tokens.at(1)), numify<double>(tokens.at(2)));
118 observation->setKernel(k);
120 else if (tokens.size() == 6)
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))
129 observation->setKernel(k);
131 else if (tokens.size() == 7)
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))
141 observation->setKernel(k);
144 NUKLEI_THROW(
"Wrong number of tokens on line (" << tokens.size() <<
")");
147 return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
151 return NUKLEI_UNIQUE_PTR<Observation>();
157 TxtWriter::TxtWriter(
const std::string &observationFileName) :
158 observationFileName_(observationFileName)
160 NUKLEI_TRACE_BEGIN();
164 TxtWriter::~TxtWriter()
168 void TxtWriter::init()
170 NUKLEI_TRACE_BEGIN();
173 }
catch (std::exception& e) {
174 throw ObservationIOError(e.what());
179 void TxtWriter::reset()
181 NUKLEI_TRACE_BEGIN();
187 void TxtWriter::writeBuffer()
189 NUKLEI_TRACE_BEGIN();
191 std::ofstream ofs(observationFileName_.c_str());
192 for (KernelCollection::iterator i = points_.begin(); i != points_.end(); ++i)
195 kernel::r3* k =
dynamic_cast<kernel::r3*
>(&*i);
198 ofs <<
stringify(k->loc_, PRECISION) << std::endl;
206 ofs <<
stringify(k->loc_, PRECISION) <<
" " <<
stringify(k->dir_, PRECISION) << std::endl;
211 kernel::se3* k =
dynamic_cast<kernel::se3*
>(&*i);
214 ofs <<
stringify(k->loc_, PRECISION) <<
" " <<
stringify(k->ori_, PRECISION) << std::endl;
224 void TxtWriter::writeObservation(
const Observation &o)
226 NUKLEI_TRACE_BEGIN();
228 kernel::base::ptr k = o.getKernel();