11 #include <boost/tuple/tuple.hpp>
15 #ifdef NUKLEI_USE_TICPP
16 #define TIXML_USE_TICPP
20 namespace tt = tinyxml2;
27 NukleiReader::NukleiReader(
const std::string &observationFileName) :
28 observationFileName_(observationFileName)
31 #ifdef NUKLEI_USE_TICPP
32 in_ = boost::shared_ptr<ticpp::Document>(
new ticpp::Document(observationFileName));
34 in_ = boost::shared_ptr<tt::XMLDocument>(
new tt::XMLDocument());
39 NukleiReader::~NukleiReader()
44 void NukleiReader::init_()
47 #ifdef NUKLEI_USE_TICPP
50 ticpp::Element* kc = in_->FirstChildElement(
"kernelCollection" );
52 e_ = boost::shared_ptr<ElementIterator>(
new ElementIterator(
"kernel" ));
54 }
catch (ticpp::Exception& e) {
55 throw ObservationIOError(e.what());
58 in_->LoadFile(observationFileName_.c_str());
59 if (in_->ErrorID() != 0)
60 throw ObservationIOError(
"Error parsing with TinyXML-2");
61 tt::XMLElement* kc = in_->FirstChildElement(
"kernelCollection" );
63 throw ObservationIOError(
"Cannot find element kernelCollection");
64 e_ = kc->FirstChildElement(
"kernel" );
69 void NukleiReader::reset()
76 NUKLEI_UNIQUE_PTR<Observation> NukleiReader::readObservation_()
79 #ifdef NUKLEI_USE_TICPP
82 typedef ticpp::Element* ElementPtr;
84 while (*e_ != e_->end())
86 ElementIterator kernel = *e_;
89 NUKLEI_UNIQUE_PTR<NukleiObservation> observation(
new NukleiObservation);
91 ElementIterator kernelElement;
92 kernelElement = kernelElement.begin(&*kernel);
97 if (kernelElement->Value() ==
"weight")
99 kernelElement->GetText(&w);
100 kernelElement++;
NUKLEI_ASSERT(kernelElement != kernelElement.end());
109 li = li.begin(&*kernelElement);
112 ticpp::Element* el = li.Get();
116 loc = numify<Vector3>(ls);
121 ticpp::Element* el = li.Get();
125 loc_h = numify<coord_t>(ws);
135 if (kernelElement != kernelElement.end())
138 std::string domain = kernelElement->GetAttribute(
"domain");
140 if (domain ==
"se3" || domain ==
"so3")
147 oi = oi.begin(&*kernelElement);
150 ticpp::Element* el = &*oi;
154 se3k.ori_ = la::normalized(numify<Quaternion>(os));
159 ticpp::Element* el = &*oi;
163 se3k.ori_h_ = numify<coord_t>(ws);
167 k.reset(
new kernel::se3(se3k));
169 else if (domain ==
"s2p")
173 r3xs2pk.loc_h_ = loc_h;
176 oi = oi.begin(&*kernelElement);
179 ticpp::Element* el = &*oi;
183 r3xs2pk.dir_ = la::normalized(numify<Vector3>(os));
188 ticpp::Element* el = &*oi;
192 r3xs2pk.dir_h_ = numify<coord_t>(ws);
198 else if (domain ==
"s2")
202 r3xs2k.loc_h_ = loc_h;
205 oi = oi.begin(&*kernelElement);
208 ticpp::Element* el = &*oi;
212 r3xs2k.dir_ = la::normalized(numify<Vector3>(os));
217 ticpp::Element* el = &*oi;
221 r3xs2k.dir_h_ = numify<coord_t>(ws);
233 k.reset(
new kernel::r3(r3k));
240 observation->setKernel(*k);
242 oc.incLabel(
"input");
244 return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
248 return NUKLEI_UNIQUE_PTR<Observation>();
250 if (!in_->RootElement())
NUKLEI_THROW(
"Reader does not seem inited.");
255 XMLElement* kernel = e_;
256 e_ = kernel->NextSiblingElement(
"kernel" );
258 NUKLEI_UNIQUE_PTR<NukleiObservation> observation(
new NukleiObservation);
260 XMLElement* kernelIter = kernel->FirstChildElement();
265 if (std::string(kernelIter->Name()) ==
"weight")
268 kernelIter = kernelIter->NextSiblingElement();
276 NUKLEI_ASSERT(std::string(kernelIter->Name()) ==
"location");
277 XMLElement* li = kernelIter->FirstChildElement();
281 loc = numify<Vector3>(li->GetText());
283 li = li->NextSiblingElement();
294 kernelIter = kernelIter->NextSiblingElement();
298 NUKLEI_ASSERT(std::string(kernelIter->Value()) ==
"orientation");
301 const char* tmp = kernelIter->Attribute(
"domain");
306 if (domain ==
"se3" || domain ==
"so3")
312 XMLElement* oi = kernelIter->FirstChildElement();
316 se3k.ori_ = la::normalized(numify<Quaternion>(oi->GetText()));
318 oi = oi->NextSiblingElement();
326 k.reset(
new kernel::se3(se3k));
328 else if (domain ==
"s2p")
332 r3xs2pk.loc_h_ = loc_h;
334 XMLElement* oi = kernelIter->FirstChildElement();
338 r3xs2pk.dir_ = la::normalized(numify<Vector3>(oi->GetText()));
340 oi = oi->NextSiblingElement();
350 else if (domain ==
"s2")
354 r3xs2k.loc_h_ = loc_h;
356 XMLElement* oi = kernelIter->FirstChildElement();
360 r3xs2k.dir_ = la::normalized(numify<Vector3>(oi->GetText()));
362 oi = oi->NextSiblingElement();
378 k.reset(
new kernel::r3(r3k));
385 observation->setKernel(*k);
387 oc.incLabel(
"input");
389 return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
392 return NUKLEI_UNIQUE_PTR<Observation>();
410 NukleiWriter::NukleiWriter(
const std::string &observationFileName) :
411 observationFileName_(observationFileName), totalWeight_(-1)
413 NUKLEI_TRACE_BEGIN();
417 NukleiWriter::~NukleiWriter()
421 void NukleiWriter::init()
423 NUKLEI_TRACE_BEGIN();
424 #ifdef NUKLEI_USE_TICPP
427 out_.reset(
new ticpp::Document(observationFileName_));
428 }
catch (ticpp::Exception& e) {
429 throw ObservationIOError(e.what());
431 ticpp::Declaration dec(
"1.0",
"UTF-8",
"");
432 out_->InsertEndChild(dec);
434 kc.SetValue(
"kernelCollection" );
435 kc.SetAttribute(
"version",
"1.0" );
436 kc_ = out_->InsertEndChild(kc)->ToElement();
439 out_.reset(
new tt::XMLDocument());
441 out_->InsertEndChild(out_->NewDeclaration());
443 kc_ = out_->NewElement(
"kernelCollection");
444 kc_->SetAttribute(
"version",
"1.0" );
445 out_->InsertEndChild(kc_);
450 void NukleiWriter::reset()
452 NUKLEI_TRACE_BEGIN();
457 void NukleiWriter::writeBuffer()
459 NUKLEI_TRACE_BEGIN();
460 #ifdef NUKLEI_USE_TICPP
463 NUKLEI_ASSERT(out_->SaveFile(observationFileName_.c_str()) == 0);
468 #ifdef NUKLEI_USE_TICPP
469 static ticpp::Element* append(ticpp::Element* parent,
const std::string& value)
471 NUKLEI_TRACE_BEGIN();
472 ticpp::Element child(value);
474 return parent->InsertEndChild(child)->ToElement();
478 static tt::XMLElement* append(tt::XMLElement* parent,
const std::string& value)
480 NUKLEI_TRACE_BEGIN();
482 return parent->InsertEndChild(parent->GetDocument()->NewElement(value.c_str()))->ToElement();
487 void NukleiWriter::writeObservation(
const Observation &o)
489 NUKLEI_TRACE_BEGIN();
490 #ifdef NUKLEI_USE_TICPP
491 if (!out_)
NUKLEI_THROW(
"Writer does not seem inited.");
493 typedef ticpp::Element* ElementPtr;
494 const Observation& observation =
dynamic_cast<const Observation&
>(o);
498 ElementPtr kernel = append(kc_,
"kernel");
500 if (totalWeight_ == -1) totalWeight_ = k->getWeight();
501 else totalWeight_ += k->getWeight();
504 ElementPtr w = append(kernel,
"weight");
505 w->SetText(
stringify(k->getWeight(), PRECISION));
508 ElementPtr loc = append(kernel,
"location");
509 loc->SetAttribute(
"domain",
"r3");
510 ElementPtr v3 = append(loc,
"vector3");
511 v3->SetText(
stringify(k->getLoc(), PRECISION));
512 ElementPtr width = append(loc,
"width");
513 width->SetText(
stringify(k->getLocH(), PRECISION));
516 if (k->polyType() == kernel::base::SE3)
518 const kernel::se3& se3k =
dynamic_cast<const kernel::se3&
>(*k);
519 ElementPtr ori = append(kernel,
"orientation");
520 ori->SetAttribute(
"domain",
"so3");
521 ElementPtr q = append(ori,
"quaternion");
522 q->SetAttribute(
"format",
"wxyz");
523 q->SetText(
stringify(se3k.ori_, PRECISION));
524 ElementPtr width = append(ori,
"width");
525 width->SetText(
stringify(se3k.ori_h_, PRECISION));
527 else if (k->polyType() == kernel::base::R3XS2P)
530 ElementPtr ori = append(kernel,
"orientation");
531 ori->SetAttribute(
"domain",
"s2p");
532 ElementPtr q = append(ori,
"vector3");
533 q->SetText(
stringify(r3xs2pk.dir_, PRECISION));
534 ElementPtr width = append(ori,
"width");
535 width->SetText(
stringify(r3xs2pk.dir_h_, PRECISION));
537 else if (k->polyType() == kernel::base::R3XS2)
540 ElementPtr ori = append(kernel,
"orientation");
541 ori->SetAttribute(
"domain",
"s2");
542 ElementPtr q = append(ori,
"vector3");
543 q->SetText(
stringify(r3xs2k.dir_, PRECISION));
544 ElementPtr width = append(ori,
"width");
545 width->SetText(
stringify(r3xs2k.dir_h_, PRECISION));
548 if (!out_)
NUKLEI_THROW(
"Writer does not seem inited.");
552 const Observation& observation =
dynamic_cast<const Observation&
>(o);
556 XMLElement* kernel = append(kc_,
"kernel");
558 if (totalWeight_ == -1) totalWeight_ = k->getWeight();
559 else totalWeight_ += k->getWeight();
562 XMLElement* w = append(kernel,
"weight");
563 w->SetText(
stringify(k->getWeight(), PRECISION).c_str());
566 XMLElement* loc = append(kernel,
"location");
567 loc->SetAttribute(
"domain",
"r3");
568 XMLElement* v3 = append(loc,
"vector3");
569 v3->SetText(
stringify(k->getLoc(), PRECISION).c_str());
570 XMLElement* width = append(loc,
"width");
571 width->SetText(
stringify(k->getLocH(), PRECISION).c_str());
574 if (k->polyType() == kernel::base::SE3)
576 const kernel::se3& se3k =
dynamic_cast<const kernel::se3&
>(*k);
577 XMLElement* ori = append(kernel,
"orientation");
578 ori->SetAttribute(
"domain",
"so3");
579 XMLElement* q = append(ori,
"quaternion");
580 q->SetAttribute(
"format",
"wxyz");
581 q->SetText(
stringify(se3k.ori_, PRECISION).c_str());
582 XMLElement* width = append(ori,
"width");
583 width->SetText(
stringify(se3k.ori_h_, PRECISION).c_str());
585 else if (k->polyType() == kernel::base::R3XS2P)
588 XMLElement* ori = append(kernel,
"orientation");
589 ori->SetAttribute(
"domain",
"s2p");
590 XMLElement* q = append(ori,
"vector3");
591 q->SetText(
stringify(r3xs2pk.dir_, PRECISION).c_str());
592 XMLElement* width = append(ori,
"width");
593 width->SetText(
stringify(r3xs2pk.dir_h_, PRECISION).c_str());
595 else if (k->polyType() == kernel::base::R3XS2)
598 XMLElement* ori = append(kernel,
"orientation");
599 ori->SetAttribute(
"domain",
"s2");
600 XMLElement* q = append(ori,
"vector3");
601 q->SetText(
stringify(r3xs2k.dir_, PRECISION).c_str());
602 XMLElement* width = append(ori,
"width");
603 width->SetText(
stringify(r3xs2k.dir_h_, PRECISION).c_str());