11 #include <boost/tuple/tuple.hpp>
15 #ifdef NUKLEI_USE_TICPP
16 #define TIXML_USE_TICPP
24 IisReader::IisReader(
const std::string &observationFileName) :
25 observationFileName_(observationFileName)
28 #ifdef NUKLEI_USE_TICPP
29 in_ = boost::shared_ptr<ticpp::Document>(
new ticpp::Document(observationFileName));
36 IisReader::~IisReader()
41 void IisReader::init_()
44 #ifdef NUKLEI_USE_TICPP
47 ticpp::Element* kc = in_->FirstChildElement(
"grasps" );
49 e_ = boost::shared_ptr<ElementIterator>(
new ElementIterator(
"grasp" ));
51 }
catch (ticpp::Exception& e) {
52 throw ObservationIOError(e.what());
60 void IisReader::reset()
67 NUKLEI_UNIQUE_PTR<Observation> IisReader::readObservation_()
70 #ifdef NUKLEI_USE_TICPP
73 typedef ticpp::Element* ElementPtr;
75 while (*e_ != e_->end())
77 ElementIterator grasp = *e_;
80 NUKLEI_UNIQUE_PTR<IisObservation> observation(
new IisObservation);
82 ElementIterator graspElement;
83 graspElement = graspElement.begin(&*grasp);
88 while (graspElement->Value() !=
"pose")
90 if (graspElement == graspElement.end())
99 pi = pi.begin(&*graspElement);
102 ticpp::Element* el = pi.Get();
105 el = el->FirstChildElement(
"euclidean");
108 k.loc_ = numify<Vector3>(ls);
113 ticpp::Element* el = pi.Get();
117 ticpp::Element* quat = el->FirstChildElement(
"quaternion",
false);
123 k.ori_ = la::normalized(numify<Quaternion>(os));
127 ticpp::Element* mat = el->FirstChildElement(
"rotmatrix");
130 std::istringstream iss(os);
133 >> m(0,0) >> m(0,1) >> m(0,2)
134 >> m(1,0) >> m(1,1) >> m(1,2)
135 >> m(2,0) >> m(2,1) >> m(2,2));
136 k.ori_ = la::quaternionCopy(la::normalized(m));
141 observation->setKernel(k);
143 oc.incLabel(
"input");
145 return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
149 return NUKLEI_UNIQUE_PTR<Observation>();
169 IisWriter::IisWriter(
const std::string &observationFileName) :
170 observationFileName_(observationFileName), totalWeight_(-1)
172 NUKLEI_TRACE_BEGIN();
176 IisWriter::~IisWriter()
180 void IisWriter::init()
182 NUKLEI_TRACE_BEGIN();
183 #ifdef NUKLEI_USE_TICPP
186 out_.reset(
new ticpp::Document(observationFileName_));
187 }
catch (ticpp::Exception& e) {
188 throw ObservationIOError(e.what());
190 ticpp::Declaration dec(
"1.0",
"UTF-8",
"");
191 out_->InsertEndChild(dec);
193 kc.SetValue(
"grasps" );
194 kc.SetAttribute(
"xmlns",
"http://iis.uibk.ac.at/ns/grasping" );
195 kc_ = out_->InsertEndChild(kc)->ToElement();
202 void IisWriter::reset()
204 NUKLEI_TRACE_BEGIN();
209 void IisWriter::writeBuffer()
211 NUKLEI_TRACE_BEGIN();
212 #ifdef NUKLEI_USE_TICPP
220 #ifdef NUKLEI_USE_TICPP
221 static ticpp::Element* append(ticpp::Element* parent,
const std::string& value)
223 NUKLEI_TRACE_BEGIN();
224 ticpp::Element child(value);
226 return parent->InsertEndChild(child)->ToElement();
231 void IisWriter::writeObservation(
const Observation &o)
233 NUKLEI_TRACE_BEGIN();
234 #ifdef NUKLEI_USE_TICPP
235 if (!out_)
NUKLEI_THROW(
"Writer does not seem inited.");
237 typedef ticpp::Element* ElementPtr;
238 const Observation& observation =
dynamic_cast<const Observation&
>(o);
241 const kernel::se3& se3k =
dynamic_cast<const kernel::se3&
>(*k);
243 ElementPtr grasp = append(kc_,
"grasp");
246 ElementPtr pose = append(grasp,
"pose");
248 ElementPtr position = append(pose,
"position");
249 position->SetAttribute(
"domain",
"R3");
250 ElementPtr euclidean = append(position,
"euclidean");
251 euclidean->SetText(
stringify(se3k.getLoc(), PRECISION));
253 ElementPtr orientation = append(pose,
"orientation");
254 orientation->SetAttribute(
"domain",
"SO3");
255 ElementPtr q = append(orientation,
"quaternion");
256 q->SetAttribute(
"format",
"wxyz");
257 q->SetText(
stringify(se3k.ori_, PRECISION));
258 ElementPtr rotmatrix = append(orientation,
"rotmatrix");
259 std::ostringstream oss;
260 Matrix3 m = la::matrixCopy(se3k.ori_);
261 oss.precision(PRECISION);
262 oss << m(0,0) <<
" " << m(0,1) <<
" " << m(0,2)
263 <<
" " << m(1,0) <<
" " << m(1,1) <<
" " << m(1,2)
264 <<
" " << m(2,0) <<
" " << m(2,1) <<
" " << m(2,2);
265 rotmatrix->SetText(oss.str());