11 #include <boost/tuple/tuple.hpp>
15 #ifdef NUKLEI_USE_TICPP
16 #define TIXML_USE_TICPP
24 CoViS3DReader::CoViS3DReader(
const std::string &observationFileName) :
25 observationFileName(observationFileName)
31 CoViS3DReader::~CoViS3DReader()
35 CoViS3DXMLReader::CoViS3DXMLReader(
const std::string &observationFileName) :
36 CoViS3DReader(observationFileName)
39 #ifdef NUKLEI_USE_TICPP
40 in_ = boost::shared_ptr<ticpp::Document>(
new ticpp::Document(observationFileName));
47 CoViS3DXMLReader::~CoViS3DXMLReader()
51 CoViS3DWandererReader::CoViS3DWandererReader(
const std::string &observationFileName) :
52 CoViS3DReader(observationFileName)
58 CoViS3DWandererReader::~CoViS3DWandererReader()
63 void CoViS3DReader::init_()
66 std::string errorsCat = std::string(
"Error in CoViS3DReader::init.") +
67 "\nErrors at each format attempt were:";
70 reader_ = boost::shared_ptr<CoViS3DReader>(
new CoViS3DXMLReader(observationFileName));
73 }
catch (ObservationIOError &e) {
74 errorsCat +=
"\n" + std::string(e.what());
77 reader_ = boost::shared_ptr<CoViS3DReader>(
new CoViS3DWandererReader(observationFileName));
80 }
catch (ObservationIOError &e) {
81 errorsCat +=
"\n" + std::string(e.what());
83 throw ObservationIOError(errorsCat);
87 void CoViS3DXMLReader::init_()
90 #ifdef NUKLEI_USE_TICPP
93 ticpp::Element* scene = in_->FirstChildElement(
"Scene" );
95 e_ = boost::shared_ptr<ElementIterator>(
new ElementIterator(
"Primitive3D" ));
96 *e_ = e_->begin(scene);
97 }
catch (ticpp::Exception& e) {
98 throw ObservationIOError(e.what());
106 void CoViS3DWandererReader::init_()
108 NUKLEI_TRACE_BEGIN();
110 in_.open(observationFileName.c_str(), std::ios::in);
112 throw ObservationIOError(std::string(
"Could not open file ") +
113 observationFileName +
" for reading.");
118 throw ObservationIOError
119 (std::string(
"Unexpected first char `") +
121 "' in file " + observationFileName +
".");
127 void CoViS3DReader::reset()
129 NUKLEI_TRACE_BEGIN();
134 void CoViS3DXMLReader::reset()
136 NUKLEI_TRACE_BEGIN();
141 void CoViS3DWandererReader::reset()
143 NUKLEI_TRACE_BEGIN();
149 NUKLEI_UNIQUE_PTR<Observation> CoViS3DReader::readObservation_()
151 NUKLEI_TRACE_BEGIN();
152 return reader_->readObservation_();
156 NUKLEI_UNIQUE_PTR<Observation> CoViS3DXMLReader::readObservation_()
158 NUKLEI_TRACE_BEGIN();
159 #ifdef NUKLEI_USE_TICPP
162 typedef ticpp::Element* ElementPtr;
164 while (*e_ != e_->end())
166 ElementIterator primitive = *e_;
169 NUKLEI_UNIQUE_PTR<CoViS3DObservation> observation(
new CoViS3DObservation);
171 ElementIterator primitiveElement;
172 primitiveElement = primitiveElement.begin(&*primitive);
175 while (primitiveElement->Value() !=
"Location")
177 primitiveElement++;
NUKLEI_ASSERT(primitiveElement != primitiveElement.end());
183 ticpp::Element* el = primitiveElement->FirstChildElement(
"Cartesian3D");
185 el->GetAttribute(
"x", &l[0]);
186 el->GetAttribute(
"y", &l[1]);
187 el->GetAttribute(
"z", &l[2]);
188 observation->setLoc(l);
191 ticpp::Element* el = primitiveElement->FirstChildElement(
"Cartesian3DCovariance");
193 std::string covString;
194 el->GetText(&covString);
195 std::istringstream iss(covString);
196 for (
int i = 0; i < 3; ++i)
for (
int j = 0; j < 3; ++j)
198 observation->setCovMatrix(cov);
202 primitiveElement++;
NUKLEI_ASSERT(primitiveElement != primitiveElement.end());
204 while (primitiveElement->Value() !=
"Orientation")
206 primitiveElement++;
NUKLEI_ASSERT(primitiveElement != primitiveElement.end());
211 ElementPtr dirGamma = primitiveElement->FirstChildElement(
"DirGammaOrientation");
213 ElementPtr el = dirGamma->FirstChildElement(
"Direction");
214 el = el->FirstChildElement(
"Spherical");
216 el->GetAttribute(
"phi", &phi);
217 el->GetAttribute(
"psi", &psi);
218 observation->setPhiPsi(phi, psi);
221 ElementPtr el = dirGamma->FirstChildElement(
"GammaVector");
222 el = el->FirstChildElement(
"Cartesian3D");
224 el->GetAttribute(
"x", &l[0]);
225 el->GetAttribute(
"y", &l[1]);
226 el->GetAttribute(
"z", &l[2]);
227 observation->setGamma(l);
231 primitiveElement++;
NUKLEI_ASSERT(primitiveElement != primitiveElement.end());
235 primitiveElement++;
NUKLEI_ASSERT(primitiveElement != primitiveElement.end());
239 if (primitiveElement->Value() ==
"Source2D")
240 primitiveElement++;
NUKLEI_ASSERT(primitiveElement != primitiveElement.end());
245 ticpp::Element* el = primitiveElement->FirstChildElement(
"Angle");
251 primitiveElement++;
NUKLEI_ASSERT(primitiveElement != primitiveElement.end());
255 RGBColor::ptr color(
new RGBColor);
257 ElementPtr el = primitiveElement->FirstChildElement(
"Left");
258 el = el->FirstChildElement(
"RGB");
259 el->GetAttribute(
"r", &color->R());
260 el->GetAttribute(
"g", &color->G());
261 el->GetAttribute(
"b", &color->B());
262 observation->setLeftColor(*color);
265 ElementPtr el = primitiveElement->FirstChildElement(
"Right");
266 el = el->FirstChildElement(
"RGB");
267 el->GetAttribute(
"r", &color->R());
268 el->GetAttribute(
"g", &color->G());
269 el->GetAttribute(
"b", &color->B());
270 observation->setRightColor(*color);
274 ElementPtr el = primitiveElement->FirstChildElement(
"Middle");
275 el = el->FirstChildElement(
"RGB");
276 el->GetAttribute(
"r", &color->R());
277 el->GetAttribute(
"g", &color->G());
278 el->GetAttribute(
"b", &color->B());
283 oc.incLabel(
"input");
285 return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
289 return NUKLEI_UNIQUE_PTR<Observation>();
296 NUKLEI_UNIQUE_PTR<Observation> CoViS3DWandererReader::readObservation_()
298 NUKLEI_TRACE_BEGIN();
299 if (!in_.is_open())
NUKLEI_THROW(
"Reader does not seem inited.");
304 while (in_.peek() != EOF)
308 NUKLEI_UNIQUE_PTR<CoViS3DObservation> observation(
new CoViS3DObservation);
317 for (
int d = 0; d < 3; d++)
319 observation->setLoc(l);
335 for (
int d = 0; d < 3; d++)
357 NUKLEI_ASSERT(in_ >> dummy[0] && in_ >> dummy[1] && in_ >> dummy[2]);
364 observation->setPhiPsi(phi, psi);
367 NUKLEI_ASSERT(rfe(observation->getDirection().Dot(dummy), 1));
392 RGBColor::ptr color(
new RGBColor);
393 for (
int d = 0; d < 3; d++)
395 observation->setLeftColor(*color);
397 for (
int d = 0; d < 3; d++)
401 for (
int d = 0; d < 3; d++)
403 observation->setRightColor(*color);
414 oc.incLabel(
"input");
416 return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
420 return NUKLEI_UNIQUE_PTR<Observation>();
424 CoViS3DXMLWriter::CoViS3DXMLWriter(
const std::string &observationFileName) :
425 observationFileName(observationFileName)
429 CoViS3DXMLWriter::~CoViS3DXMLWriter()
433 void CoViS3DXMLWriter::init()
435 NUKLEI_TRACE_BEGIN();
436 #ifdef NUKLEI_USE_TICPP
438 out_.reset(
new ticpp::Document(observationFileName));
439 ticpp::Declaration dec(
"1.0",
"UTF-8",
"");
440 out_->InsertEndChild(dec);
441 ticpp::Element scene;
442 scene.SetValue(
"Scene" );
443 scene.SetAttribute(
"version",
"1.0" );
444 scene_ = out_->InsertEndChild(scene)->ToElement();
445 }
catch (ticpp::Exception& e) {
446 throw ObservationIOError(e.what());
454 void CoViS3DXMLWriter::reset()
456 NUKLEI_TRACE_BEGIN();
461 void CoViS3DXMLWriter::writeBuffer()
463 NUKLEI_TRACE_BEGIN();
464 #ifdef NUKLEI_USE_TICPP
472 #ifdef NUKLEI_USE_TICPP
473 static ticpp::Element* append(ticpp::Element* parent,
const std::string& value)
475 NUKLEI_TRACE_BEGIN();
476 ticpp::Element child(value);
477 return parent->InsertEndChild(child)->ToElement();
482 void CoViS3DXMLWriter::writeObservation(
const Observation &o)
484 NUKLEI_TRACE_BEGIN();
485 #ifdef NUKLEI_USE_TICPP
486 if (!out_)
NUKLEI_THROW(
"Writer does not seem inited.");
488 typedef ticpp::Element* ElementPtr;
489 const CoViS3DObservation& observation =
dynamic_cast<const CoViS3DObservation&
>(o);
491 ElementPtr primitive = append(scene_,
"Primitive3D");
492 primitive->SetAttribute(
"type",
"l");
493 primitive->SetAttribute(
"confidence", observation.getWeight());
494 primitive->SetAttribute(
"length", 4);
497 ElementPtr loc = append(primitive,
"Location");
498 ElementPtr el = append(loc,
"Cartesian3D");
499 Vector3 l = observation.getLoc();
501 if (observation.getCovMatrix().isDefined()) cov = *observation.getCovMatrix();
502 else cov = Matrix3::IDENTITY;
503 el->SetAttribute(
"x", l[0]);
504 el->SetAttribute(
"y", l[1]);
505 el->SetAttribute(
"z", l[2]);
506 el = append(loc,
"Cartesian3DCovariance");
507 el->SetText(
stringify(Vector3(cov.GetRow(0))) +
" " +
508 stringify(Vector3(cov.GetRow(1))) +
" " +
513 ElementPtr ori = append(primitive,
"Orientation");
514 ElementPtr dirGamma = append(ori,
"DirGammaOrientation");
516 ElementPtr el = append(append(dirGamma,
"GammaVector"),
"Cartesian3D");
517 kernel::se3::ptr se3k = observation.getKernel()->polySe3Proj();
519 la::copyRotation(ori, se3k->ori_);
520 Vector3 l = ori.GetColumn(1);
522 el->SetAttribute(
"x", l[0]);
523 el->SetAttribute(
"y", l[1]);
524 el->SetAttribute(
"z", l[2]);
527 ElementPtr dir = append(dirGamma,
"Direction");
528 ElementPtr el = append(dir,
"Spherical");
530 boost::tie(phi, psi) = observation.getPhiPsi();
531 el->SetAttribute(
"phi", phi);
532 el->SetAttribute(
"psi", psi);
533 append(dir,
"Conf")->SetText(-1);
535 append(ori,
"Cartesian3DCovariance")->SetText
542 ElementPtr el = append(append(primitive,
"IntrinsicDimensionality"),
544 el->SetAttribute(
"c0", 0);
545 el->SetAttribute(
"c1", 1);
546 el->SetAttribute(
"c2", 0);
552 ElementPtr phase = append(primitive,
"Source2D");
553 ElementPtr first = append(phase,
"First");
555 ElementPtr second = append(phase,
"Second");
560 ElementPtr phase = append(primitive,
"Phase");
561 ElementPtr el = append(phase,
"Angle");
564 el = append(phase,
"Conf");
569 ElementPtr colors = append(primitive,
"Colors");
571 const RGBColor c(observation.getLeftColor());
572 ElementPtr side = append(colors,
"Left");
573 ElementPtr rgb = append(side,
"RGB");
574 rgb->SetAttribute(
"r", c.at(0));
575 rgb->SetAttribute(
"g", c.at(1));
576 rgb->SetAttribute(
"b", c.at(2));
577 append(side,
"Conf")->SetText(1);
580 const RGBColor c(observation.getRightColor());
581 ElementPtr side = append(colors,
"Right");
582 ElementPtr rgb = append(side,
"RGB");
583 rgb->SetAttribute(
"r", c.at(0));
584 rgb->SetAttribute(
"g", c.at(1));
585 rgb->SetAttribute(
"b", c.at(2));
586 append(side,
"Conf")->SetText(1);
589 ElementPtr side = append(colors,
"Middle");
590 ElementPtr rgb = append(side,
"RGB");
591 rgb->SetAttribute(
"r", 0);
592 rgb->SetAttribute(
"g", 0);
593 rgb->SetAttribute(
"b", 0);
594 append(side,
"Conf")->SetText(1);