IisObservationIO.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #include <fstream>
8 #include <cassert>
9 #include <algorithm>
10 #include <cmath>
11 #include <boost/tuple/tuple.hpp>
12 
14 #include <nuklei/IisObservation.h>
15 #ifdef NUKLEI_USE_TICPP
16 #define TIXML_USE_TICPP
17 #include "ticpp.h"
18 #endif
19 
20 namespace nuklei {
21 
22 
23 
24  IisReader::IisReader(const std::string &observationFileName) :
25  observationFileName_(observationFileName)
26  {
27  NUKLEI_TRACE_BEGIN();
28 #ifdef NUKLEI_USE_TICPP
29  in_ = boost::shared_ptr<ticpp::Document>(new ticpp::Document(observationFileName));
30 #else
31  NUKLEI_THROW("This function requires TICPP.");
32 #endif
33  NUKLEI_TRACE_END();
34  }
35 
36  IisReader::~IisReader()
37  {
38  }
39 
40 
41  void IisReader::init_()
42  {
43  NUKLEI_TRACE_BEGIN();
44 #ifdef NUKLEI_USE_TICPP
45  try {
46  in_->LoadFile();
47  ticpp::Element* kc = in_->FirstChildElement( "grasps" );
48  NUKLEI_ASSERT(kc != NULL);
49  e_ = boost::shared_ptr<ElementIterator>(new ElementIterator( "grasp" ));
50  *e_ = e_->begin(kc);
51  } catch (ticpp::Exception& e) {
52  throw ObservationIOError(e.what());
53  }
54 #else
55  NUKLEI_THROW("This function requires TICPP.");
56 #endif
57  NUKLEI_TRACE_END();
58  }
59 
60  void IisReader::reset()
61  {
62  NUKLEI_TRACE_BEGIN();
63  init();
64  NUKLEI_TRACE_END();
65  }
66 
67  NUKLEI_UNIQUE_PTR<Observation> IisReader::readObservation_()
68  {
69  NUKLEI_TRACE_BEGIN();
70 #ifdef NUKLEI_USE_TICPP
71  if (!e_) NUKLEI_THROW("Reader does not seem inited.");
72 
73  typedef ticpp::Element* ElementPtr;
74 
75  while (*e_ != e_->end())
76  {
77  ElementIterator grasp = *e_;
78  ++*e_;
79 
80  NUKLEI_UNIQUE_PTR<IisObservation> observation(new IisObservation);
81 
82  ElementIterator graspElement;
83  graspElement = graspElement.begin(&*grasp);
84  NUKLEI_ASSERT(graspElement != graspElement.end());
85 
86 
87 
88  while (graspElement->Value() != "pose")
89  {
90  if (graspElement == graspElement.end())
91  NUKLEI_THROW("Pose element not found.");
92  graspElement++;
93  }
94 
95  kernel::se3 k;
96 
97  {
98  ElementIterator pi;
99  pi = pi.begin(&*graspElement);
100  NUKLEI_ASSERT(pi != pi.end());
101  {
102  ticpp::Element* el = pi.Get();
103  NUKLEI_ASSERT(el->Value() == "position");
104  NUKLEI_ASSERT(el->GetAttribute("domain") == "R3");
105  el = el->FirstChildElement("euclidean");
106  std::string ls;
107  el->GetText(&ls);
108  k.loc_ = numify<Vector3>(ls);
109  }
110  pi++;
111  NUKLEI_ASSERT(pi != pi.end())
112  {
113  ticpp::Element* el = pi.Get();
114  NUKLEI_ASSERT(el->Value() == "orientation");
115  NUKLEI_ASSERT(el->GetAttribute("domain") == "SO3");
116 
117  ticpp::Element* quat = el->FirstChildElement("quaternion", false);
118  if (quat != NULL)
119  {
120  NUKLEI_ASSERT(quat->GetAttribute("format") == "wxyz");
121  std::string os;
122  quat->GetText(&os);
123  k.ori_ = la::normalized(numify<Quaternion>(os));
124  }
125  else
126  {
127  ticpp::Element* mat = el->FirstChildElement("rotmatrix");
128  std::string os;
129  mat->GetText(&os);
130  std::istringstream iss(os);
131  Matrix3 m;
132  NUKLEI_ASSERT(iss
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));
137  }
138  }
139  }
140 
141  observation->setKernel(k);
142 
143  oc.incLabel("input");
144 
145  return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
146  }
147 
148  // End of file reached.
149  return NUKLEI_UNIQUE_PTR<Observation>();
150 #else
151  NUKLEI_THROW("This function requires TICPP.");
152 #endif
153  NUKLEI_TRACE_END();
154  }
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169  IisWriter::IisWriter(const std::string &observationFileName) :
170  observationFileName_(observationFileName), totalWeight_(-1)
171  {
172  NUKLEI_TRACE_BEGIN();
173  NUKLEI_TRACE_END();
174  }
175 
176  IisWriter::~IisWriter()
177  {
178  }
179 
180  void IisWriter::init()
181  {
182  NUKLEI_TRACE_BEGIN();
183 #ifdef NUKLEI_USE_TICPP
184  totalWeight_ = -1;
185  try {
186  out_.reset(new ticpp::Document(observationFileName_));
187  } catch (ticpp::Exception& e) {
188  throw ObservationIOError(e.what());
189  }
190  ticpp::Declaration dec("1.0", "UTF-8", "");
191  out_->InsertEndChild(dec);
192  ticpp::Element kc;
193  kc.SetValue( "grasps" );
194  kc.SetAttribute( "xmlns", "http://iis.uibk.ac.at/ns/grasping" );
195  kc_ = out_->InsertEndChild(kc)->ToElement();
196 #else
197  NUKLEI_THROW("This function requires TICPP.");
198 #endif
199  NUKLEI_TRACE_END();
200  }
201 
202  void IisWriter::reset()
203  {
204  NUKLEI_TRACE_BEGIN();
205  init();
206  NUKLEI_TRACE_END();
207  }
208 
209  void IisWriter::writeBuffer()
210  {
211  NUKLEI_TRACE_BEGIN();
212 #ifdef NUKLEI_USE_TICPP
213  out_->SaveFile();
214 #else
215  NUKLEI_THROW("This function requires TICPP.");
216 #endif
217  NUKLEI_TRACE_END();
218  }
219 
220 #ifdef NUKLEI_USE_TICPP
221  static ticpp::Element* append(ticpp::Element* parent, const std::string& value)
222  {
223  NUKLEI_TRACE_BEGIN();
224  ticpp::Element child(value);
225  NUKLEI_ASSERT(parent != NULL);
226  return parent->InsertEndChild(child)->ToElement();
227  NUKLEI_TRACE_END();
228  }
229 #endif
230 
231  void IisWriter::writeObservation(const Observation &o)
232  {
233  NUKLEI_TRACE_BEGIN();
234 #ifdef NUKLEI_USE_TICPP
235  if (!out_) NUKLEI_THROW("Writer does not seem inited.");
236 
237  typedef ticpp::Element* ElementPtr;
238  const Observation& observation = dynamic_cast<const Observation&>(o);
239 
240  kernel::base::ptr k = observation.getKernel();
241  const kernel::se3& se3k = dynamic_cast<const kernel::se3&>(*k);
242 
243  ElementPtr grasp = append(kc_, "grasp");
244 
245  {
246  ElementPtr pose = append(grasp, "pose");
247 
248  ElementPtr position = append(pose, "position");
249  position->SetAttribute("domain", "R3");
250  ElementPtr euclidean = append(position, "euclidean");
251  euclidean->SetText(stringify(se3k.getLoc(), PRECISION));
252 
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());
266  }
267 
268 #else
269  NUKLEI_THROW("This function requires TICPP.");
270 #endif
271  NUKLEI_TRACE_END();
272  }
273 
274 }
Public namespace.
Definition: Color.cpp:9
std::string stringify(const T &x, int precision=-1, int width=0)
Converts an object to a std::string using operator<<.
Definition: Common.h:333
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
NUKLEI_UNIQUE_PTR< kernel::base > ptr
NUKLEI_UNIQUE_PTR for kernel::base.
Definition: Kernel.h:50
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.