ed
serialization.cpp
Go to the documentation of this file.
2 #include "ed/mask.h"
3 
4 #include "ed/world_model.h"
5 #include "ed/update_request.h"
6 #include "ed/entity.h"
7 #include "ed/convex_hull_calc.h"
8 
9 #include <tue/config/reader.h>
10 #include <tue/config/writer.h>
11 
12 #include <geolib/Shape.h>
13 #include <geolib/Box.h>
14 
17 
18 namespace ed
19 {
20 
21 // ----------------------------------------------------------------------------------------------------
22 
23 //void serialize(const WorldModel& wm, ed::io::Writer& w, unsigned long since_revision)
24 //{
25 
26 //}
27 
29 
30 //void serialize(const Entity& wm, ed::io::Writer& w, unsigned long since_revision)
31 //{
32 
33 //}
34 
35 // ----------------------------------------------------------------------------------------------------
36 
38 {
39  if (r.readArray("entities"))
40  {
41  while(r.nextArrayItem())
42  {
43  std::string id;
44  if (!r.readValue("id", id))
45  {
46  std::cout << "Deserialze: Entities should have field 'id'" << std::endl;
47  return false;
48  }
49 
50  std::string type;
51  if (r.readValue("type", type))
52  {
53  req.setType(id, type);
54  }
55 
56  double existence_prob;
57  if (r.readValue("existence_prob", existence_prob))
58  {
59  req.setExistenceProbability(id, existence_prob);
60  }
61 
62  double timestamp = 0;
63  if (r.readGroup("timestamp"))
64  {
65  ed::deserializeTimestamp(r, timestamp);
66  req.setLastUpdateTimestamp(id, timestamp);
67  r.endGroup();
68  }
69 
71  if (r.readGroup("pose"))
72  {
73  ed::deserialize(r, pose);
74  req.setPose(id, pose);
75  r.endGroup();
76  }
77 
78  if (r.readGroup("convex_hull"))
79  {
80  ed::ConvexHull chull;
81  ed::deserialize(r, chull);
82 
84  req.setConvexHullNew(id, chull, pose, timestamp);
85  r.endGroup();
86  }
87 
88  if (r.readGroup("mesh"))
89  {
90  geo::ShapePtr shape(new geo::Shape);
91  ed::deserialize(r, *shape);
92  req.setVisual(id, shape);
93  req.setCollision(id, shape);
94  r.endGroup();
95  }
96 
97  std::string data_str;
98  if (r.readValue("data", data_str))
99  {
100  std::replace(data_str.begin(), data_str.end(), '|', '"');
101  std::replace(data_str.begin(), data_str.end(), '^', '\n');
102 
103  tue::Configuration cfg;
104  if (tue::config::loadFromYAMLString(data_str, cfg))
105  req.addData(id, cfg.data());
106  }
107 
108 // if (r.readArray("properties"))
109 // {
110 // while(r.nextArrayItem())
111 // {
112 // std::string prop_name;
113 // if (!r.readValue("name", prop_name))
114 // continue;
115 
116 // const ed::PropertyKeyDBEntry* entry = data.world.getPropertyInfo(prop_name);
117 // if (!entry)
118 // {
119 // error += "For entity '" + id + "': unknown property '" + prop_name +"'.\n";
120 // continue;
121 // }
122 
123 // if (!entry->info->serializable())
124 // {
125 // error += "For entity '" + id + "': property '" + prop_name +"' is not serializable.\n";
126 // continue;
127 // }
128 
129 // ed::Variant value;
130 // if (entry->info->deserialize(r, value))
131 // {
132 // req.setProperty(id, entry, value);
133 // ROS_INFO_STREAM("Sync plugin: setProperty " << id);
134 // } else
135 // error += "For entity '" + id + "': deserialization of property '" + prop_name +"' failed.\n";
136 // }
137 
138 // r.endArray();
139 // }
140  }
141 
142  r.endArray();
143  }
144 
145  return true;
146 }
147 
148 // ----------------------------------------------------------------------------------------------------
149 
150 void serialize(const geo::Pose3D& pose, ed::io::Writer& w)
151 {
152  w.writeValue("x", pose.t.x);
153  w.writeValue("y", pose.t.y);
154  w.writeValue("z", pose.t.z);
155 
156  geo::Quaternion q = pose.getQuaternion();
157  w.writeValue("qx", q.x);
158  w.writeValue("qy", q.y);
159  w.writeValue("qz", q.z);
160  w.writeValue("qw", q.w);
161 }
162 
163 // ----------------------------------------------------------------------------------------------------
164 
166 {
167  // Read position
168  if (r.readValue("x", pose.t.x))
169  {
170  r.readValue("y", pose.t.y);
171  r.readValue("z", pose.t.z);
172  }
173  else if (r.readGroup("pos") || r.readGroup("t"))
174  {
175  r.readValue("x", pose.t.x);
176  r.readValue("y", pose.t.y);
177  r.readValue("z", pose.t.z);
178  r.endGroup();
179  }
180  else
181  return false;
182 
183  // Read orientation
184  geo::Quaternion q;
185  if (r.readValue("qx", q.x))
186  {
187  r.readValue("qy", q.y);
188  r.readValue("qz", q.z);
189  r.readValue("qw", q.w);
190 
191  pose.R.setRotation(q);
192  }
193  else if (r.readGroup("rot") || r.readGroup("R"))
194  {
195  r.readValue("xx", pose.R.xx);
196  r.readValue("xy", pose.R.xy);
197  r.readValue("xz", pose.R.xz);
198  r.readValue("yx", pose.R.yx);
199  r.readValue("yy", pose.R.yy);
200  r.readValue("yz", pose.R.yz);
201  r.readValue("zx", pose.R.zx);
202  r.readValue("zy", pose.R.zy);
203  r.readValue("zz", pose.R.zz);
204  r.endGroup();
205  }
206  else
207  return false;
208 
209  return true;
210 }
211 
212 // ----------------------------------------------------------------------------------------------------
213 
215 {
216  if (!r.readGroup(group))
217  return false;
218 
219  pose.t = geo::Vec3(0, 0, 0);
220  r.value("x", pose.t.x, tue::config::OPTIONAL);
221  r.value("y", pose.t.y, tue::config::OPTIONAL);
222  r.value("z", pose.t.z, tue::config::OPTIONAL);
223 
224  double roll = 0, pitch = 0, yaw = 0;
225  r.value("X", roll, tue::config::OPTIONAL);
226  r.value("Y", pitch, tue::config::OPTIONAL);
227  r.value("Z", yaw, tue::config::OPTIONAL);
228  r.value("roll", roll, tue::config::OPTIONAL);
229  r.value("pitch", pitch, tue::config::OPTIONAL);
230  r.value("yaw", yaw, tue::config::OPTIONAL);
231 
232  // Set rotation
233  pose.R.setRPY(roll, pitch, yaw);
234 
235  geo::Quaternion q;
236  if (r.value("qx", q.x, tue::config::OPTIONAL)
237  && r.value("qy", q.y, tue::config::OPTIONAL)
238  && r.value("qz", q.z, tue::config::OPTIONAL)
239  && r.value("qw", q.w, tue::config::OPTIONAL))
240  {
241  pose.R.setRotation(q);
242  }
243 
244  r.endGroup();
245  return true;
246 }
247 
248 // ----------------------------------------------------------------------------------------------------
249 
251 {
252  if (!r.readGroup(group))
253  return false;
254 
255  r.value("x", p.x);
256  r.value("y", p.y);
257  r.value("z", p.z);
258 
259  r.endGroup();
260  return true;
261 }
262 
263 // ----------------------------------------------------------------------------------------------------
264 
266 {
267  w.writeArray("points");
268  for (std::vector<geo::Vec2f>::const_iterator it = ch.points.begin(); it != ch.points.end(); ++it)
269  {
270  w.addArrayItem();
271  w.writeValue("x", it->x); w.writeValue("y", it->y);
272  w.endArrayItem();
273  }
274  w.endArray();
275  w.writeValue("z_min", ch.z_min);
276  w.writeValue("z_max", ch.z_max);
277 }
278 
279 // ----------------------------------------------------------------------------------------------------
280 
282 {
283  if (r.readArray("points"))
284  {
285  while(r.nextArrayItem())
286  {
287  geo::Vec2f p;
288  r.readValue("x", p.x);
289  r.readValue("y", p.y);
290  ch.points.push_back(p);
291  }
292  r.endArray();
293  }
294 
295  r.readValue("z_min", ch.z_min);
296  r.readValue("z_max", ch.z_max);
297 
300 
301  return true;
302 }
303 
304 // ----------------------------------------------------------------------------------------------------
305 
307 {
308  w.writeArray("vertices");
309  const std::vector<geo::Vector3>& vertices = s.getMesh().getPoints();
310  for(std::vector<geo::Vector3>::const_iterator it = vertices.begin(); it != vertices.end(); ++it)
311  {
312  w.addArrayItem();
313  w.writeValue("x", it->x); w.writeValue("y", it->y); w.writeValue("z", it->z);
314  w.endArrayItem();
315  }
316  w.endArray();
317 
318  w.writeArray("triangles");
319  const std::vector<geo::TriangleI>& triangles = s.getMesh().getTriangleIs();
320  for(unsigned int i = 0; i < triangles.size(); ++i)
321  {
322  w.addArrayItem();
323  w.writeValue("i1", static_cast<int>(triangles[i].i1_));
324  w.writeValue("i2", static_cast<int>(triangles[i].i2_));
325  w.writeValue("i3", static_cast<int>(triangles[i].i3_));
326  w.endArrayItem();
327 
328  }
329  w.endArray();
330 }
331 
332 // ----------------------------------------------------------------------------------------------------
333 
335 {
336  geo::Mesh mesh;
337 
338  // Vertices
339  if (r.readArray("vertices"))
340  {
341  while(r.nextArrayItem())
342  {
343  geo::Vector3 p;
344  r.readValue("x", p.x);
345  r.readValue("y", p.y);
346  r.readValue("z", p.z);
347  mesh.addPoint(p);
348  }
349 
350  r.endArray();
351  }
352  else
353  return false;
354 
355  // Triangles
356  if (r.readArray("triangles"))
357  {
358  while(r.nextArrayItem())
359  {
360  int i1, i2, i3;
361  r.readValue("i1", i1);
362  r.readValue("i2", i2);
363  r.readValue("i3", i3);
364  mesh.addTriangle(i1, i2, i3);
365  }
366 
367  r.endArray();
368  }
369  else
370  return false;
371 
372  s.setMesh(mesh);
373  return true;
374 }
375 
376 // ----------------------------------------------------------------------------------------------------
377 
378 bool deserialize(tue::config::Reader& r_orig, const std::string& group, geo::Shape& s)
379 {
380  tue::config::Reader r = r_orig;
381 
382  geo::Mesh mesh;
383 
384  if (r.readArray(group))
385  {
386  while(r.nextArrayItem())
387  {
388  if (r.readGroup("box"))
389  {
390  geo::Vec3 min, max, size;
391  if (deserialize(r, "min", min))
392  {
393  if (!deserialize(r, "max", max))
394  return false;
395  }
396  else if (deserialize(r, "size", size))
397  {
398  min = -0.5 * size;
399  max = 0.5 * size;
400  }
401  else
402  {
403  return false;
404  }
405 
406  geo::Pose3D pose;
407  if (!deserialize(r, "pose", pose))
408  pose = geo::Pose3D::identity();
409 
410  mesh.add(geo::Box(min, max).getMesh().getTransformed(pose));
411  r.endGroup();
412  }
413  }
414  r.endArray();
415  }
416  else if(r.readGroup(group))
417  {
418  if (r.readGroup("box"))
419  {
420  geo::Vec3 min, max, size;
421  if (deserialize(r, "min", min))
422  {
423  if (!deserialize(r, "max", max))
424  return false;
425  }
426  else if (deserialize(r, "size", size))
427  {
428  min = -0.5 * size;
429  max = 0.5 * size;
430  }
431  else
432  {
433  return false;
434  }
435 
436  geo::Pose3D pose;
437  if (!deserialize(r, "pose", pose))
438  pose = geo::Pose3D::identity();
439 
440  mesh.add(geo::Box(min, max).getMesh().getTransformed(pose));
441  r.endGroup();
442  }
443  }
444  else
445  return false;
446 
447  if (mesh.getTriangleIs().empty())
448  return false;
449 
450  s.setMesh(mesh);
451 
452  return true;
453 }
454 
455 // ----------------------------------------------------------------------------------------------------
456 
457 void serializeTimestamp(double time, ed::io::Writer& w)
458 {
459  w.writeValue("sec", (int)time);
460  w.writeValue("nsec", (int)((time - (int)time) * 1e9));
461 }
462 
463 // ----------------------------------------------------------------------------------------------------
464 
465 bool deserializeTimestamp(ed::io::Reader& r, double& time)
466 {
467  int sec, nsec;
468  r.readValue("sec", sec);
469  r.readValue("nsec", nsec);
470  time = sec + (double)nsec / 1e9;
471  return true;
472 }
473 
474 // ----------------------------------------------------------------------------------------------------
475 
477 {
478  const static int MASK_SERIALIZATION_VERSION = 0;
479 
480  m << MASK_SERIALIZATION_VERSION;
481 
482  m << mask.width();
483  m << mask.height();
484 
485  // Determine size
486  int size = 0;
487  for(ImageMask::const_iterator it = mask.begin(); it != mask.end(); ++it)
488  ++size;
489 
490  m << size;
491 
492  for(ImageMask::const_iterator it = mask.begin(); it != mask.end(); ++it)
493  {
494  cv::Point2i pt = it();
495  m << pt.y * mask.width() + pt.x;
496  }
497 }
498 
499 // ----------------------------------------------------------------------------------------------------
500 
502 {
503  int version;
504  m >> version;
505 
506  int width, height;
507  m >> width;
508  m >> height;
509  mask = ImageMask(width, height);
510 
511  int size;
512  m >> size;
513 
514  for(int i = 0; i < size; ++i)
515  {
516  int idx;
517  m >> idx;
518 
519  mask.addPoint(idx % width, idx / width);
520  }
521 
522  return true;
523 }
524 
525 
526 
527 // ----------------------------------------------------------------------------------------------------
528 //
529 // SERIALIZATION
530 //
531 // ----------------------------------------------------------------------------------------------------
532 
533 // ----------------------------------------------------------------------------------------------------
534 
535 //void serialize(const WorldModel& wm, tue::config::Writer& w)
536 //{
537 // w.writeArray("entities");
538 
539 // for(WorldModel::const_iterator it = wm.begin(); it != wm.end(); ++it)
540 // {
541 // const EntityConstPtr& e = *it;
542 
543 // std::cout << e->id() << std::endl;
544 
545 // w.addArrayItem();
546 
547 // w.setValue("id", e->id().str());
548 // w.setValue("type", e->type());
549 
550 // if (e->has_pose())
551 // {
552 // w.writeGroup("pose");
553 // w.setValue("x", e->pose().t.x);
554 // w.setValue("y", e->pose().t.y);
555 // w.setValue("z", e->pose().t.z);
556 
557 // double X, Y, Z;
558 // getEulerYPR(e->pose().R, Z, Y, X);
559 // w.setValue("X", X);
560 // w.setValue("Y", Y);
561 // w.setValue("Z", Z);
562 // w.endGroup();
563 // }
564 
565 // w.endArrayItem();
566 // }
567 
568 // w.endArray();
569 //}
570 
571 // ----------------------------------------------------------------------------------------------------
572 //
573 // DESERIALIZATION
574 //
575 // ----------------------------------------------------------------------------------------------------
576 
577 
578 
579 // ----------------------------------------------------------------------------------------------------
580 
581 //void deserialize(tue::config::Reader& r, UpdateRequest& req)
582 //{
583 // if (r.readArray("entities"))
584 // {
585 // while(r.nextArrayItem())
586 // {
587 // std::string id;
588 // if (!r.value("id", id))
589 // continue;
590 
591 // if (r.readGroup("pose"))
592 // {
593 // geo::Pose3D pose;
594 
595 // if (!r.value("x", pose.t.x) || !r.value("y", pose.t.y) || !r.value("z", pose.t.z))
596 // continue;
597 
598 // double rx = 0, ry = 0, rz = 0;
599 // r.value("rx", rx, tue::config::OPTIONAL);
600 // r.value("ry", ry, tue::config::OPTIONAL);
601 // r.value("rz", rz, tue::config::OPTIONAL);
602 
603 // pose.R.setRPY(rx, ry, rz);
604 
605 // req.setPose(id, pose);
606 
607 // r.endGroup();
608 // }
609 // }
610 
611 // r.endArray();
612 // }
613 //}
614 
615 }
geo::Vector3::y
const real & y() const
convex_hull_calc.h
geo::Shape::setMesh
virtual void setMesh(const Mesh &mesh)
geo::Mat3T::setRotation
void setRotation(const QuaternionT< T > &q)
geo::Transform3T::getQuaternion
QuaternionT< T > getQuaternion() const
ed::ConvexHull::z_min
float z_min
Definition: convex_hull.h:16
ed::ImageMask::const_iterator
Definition: mask.h:112
ed::UpdateRequest
Definition: update_request.h:24
geo::QuaternionT
std::string
tue::config::Reader::nextArrayItem
bool nextArrayItem()
std::shared_ptr
ed::ImageMask::width
int width() const
Definition: mask.h:59
ed::UpdateRequest::setExistenceProbability
void setExistenceProbability(const UUID &id, double prob)
Definition: update_request.h:119
ed::ConvexHull
Definition: convex_hull.h:11
ed::UpdateRequest::addData
void addData(const UUID &id, const tue::config::DataConstPointer &data)
Definition: update_request.h:148
ed::convex_hull::calculateEdgesAndNormals
void calculateEdgesAndNormals(ConvexHull &chull)
Definition: convex_hull_calc.cpp:111
geo::Mesh::getTriangleIs
const std::vector< TriangleI > & getTriangleIs() const
entity.h
ed::deserialize
bool deserialize(io::Reader &r, UpdateRequest &req)
Definition: serialization.cpp:37
tue::config::Reader::value
bool value(const std::string &name, T &value, const RequiredOrOptional=REQUIRED) const
geo::Vec2T::y
T y
geo::Transform3T::identity
static Transform3T identity()
std::vector
std::vector::size
T size(T... args)
geo::Mat3T::xz
T xz
geo::Box
ed::io::Reader::endArray
virtual bool endArray()=0
geo::Vec3T
Shape.h
geo::Vec2T::x
T x
reader.h
geo::Mesh::add
void add(const Mesh &mesh)
geo::Transform3T
tue::serialization::OutputArchive
geo::Shape::getMesh
virtual const Mesh & getMesh() const
geo::QuaternionT::w
T w
ed::deserializeTimestamp
bool deserializeTimestamp(ed::io::Reader &r, double &time)
Definition: serialization.cpp:465
ed::UpdateRequest::setLastUpdateTimestamp
void setLastUpdateTimestamp(const UUID &id, double t)
Definition: update_request.h:126
std::replace
T replace(T... args)
tue::config::loadFromYAMLString
bool loadFromYAMLString(const std::string &string, ReaderWriter &config, const ResolveConfig &resolve_config=ResolveConfig::defaultConfig())
std::cout
geo::Vec3T::y
T y
tue::config::ReaderWriter
ed::io::Writer::endArray
virtual void endArray()=0
ed::io::Writer::writeValue
virtual void writeValue(const std::string &key, float f)=0
tue::config::Reader::readGroup
bool readGroup(const std::string &name, const RequiredOrOptional opt=OPTIONAL)
geo::Mesh::getPoints
const std::vector< geo::Vector3 > & getPoints() const
update_request.h
tue::config::ReaderWriter::data
DataPointer data() const
geo::QuaternionT::y
T y
tue::config::Reader
ed::io::Reader::readValue
virtual bool readValue(const std::string &, float &f)=0
ed::ImageMask::addPoint
void addPoint(const cv::Point2i &p)
Definition: mask.h:71
geo::Mat3T::zx
T zx
geo::QuaternionT::x
T x
ed::io::Reader::readArray
virtual bool readArray(const std::string &name)=0
geo::Mat3T::zz
T zz
ed::UpdateRequest::setPose
void setPose(const UUID &id, const geo::Pose3D &pose)
Definition: update_request.h:132
tue::config::Reader::endGroup
bool endGroup()
geo::Mat3T::yy
T yy
ed::io::Reader
Definition: reader.h:14
geo::Transform3T::t
Vec3T< T > t
mask.h
ed::UpdateRequest::setCollision
void setCollision(const UUID &id, const geo::ShapeConstPtr &collision)
Definition: update_request.h:57
tue::config::OPTIONAL
OPTIONAL
geo::Mat3T::xx
T xx
ed::UpdateRequest::setConvexHullNew
void setConvexHullNew(const UUID &id, const ed::ConvexHull &convex_hull, const geo::Pose3D &pose, double time, std::string source="")
Definition: update_request.h:87
geo::Vector3
yaml.h
geo::Vec3
Vec3T< real > Vec3
ed::io::Writer::writeArray
virtual void writeArray(const std::string &key)=0
geo::Vector3::z
const real & z() const
ed::io::Reader::readGroup
virtual bool readGroup(const std::string &name)=0
ed::UpdateRequest::setType
void setType(const UUID &id, const std::string &type)
Definition: update_request.h:106
ed::io::Writer
Definition: writer.h:17
geo::Mat3T::zy
T zy
ed::UpdateRequest::setVisual
void setVisual(const UUID &id, const geo::ShapeConstPtr &visual)
Definition: update_request.h:53
tue::serialization::InputArchive
configuration.h
std::endl
T endl(T... args)
ed::io::Reader::nextArrayItem
virtual bool nextArrayItem()=0
geo::Mat3T::yx
T yx
std::string::begin
T begin(T... args)
geo::Transform3T::R
Mat3T< T > R
ed::io::Writer::endArrayItem
virtual void endArrayItem()=0
ed::io::Reader::endGroup
virtual bool endGroup()=0
geo::Mesh::addTriangle
void addTriangle(unsigned int i1, unsigned int i2, unsigned int i3)
geo::Vector3::x
const real & x() const
ed::ImageMask
Definition: mask.h:17
geo::Mat3T::yz
T yz
geo::Vec3T::z
T z
geo::QuaternionT::z
T z
serialization.h
ed
Definition: convex_hull.h:8
ed::io::Writer::addArrayItem
virtual void addArrayItem()=0
Box.h
tue::config::Reader::readArray
bool readArray(const std::string &name, const RequiredOrOptional opt=OPTIONAL)
std::string::end
T end(T... args)
ed::ImageMask::begin
const_iterator begin(int width=0) const
Definition: mask.h:180
ed::serialize
void serialize(const geo::Pose3D &pose, ed::io::Writer &w)
Definition: serialization.cpp:150
ed::ConvexHull::z_max
float z_max
Definition: convex_hull.h:16
ed::convex_hull::calculateArea
void calculateArea(ConvexHull &c)
calculateArea calculate the area of the ConvexHull in xy-plane.
Definition: convex_hull_calc.cpp:221
tue::config::Reader::endArray
bool endArray()
ed::ImageMask::end
const_iterator end() const
Definition: mask.h:188
geo::Mat3T::xy
T xy
geo::Mesh
geo::Mat3T::setRPY
void setRPY(T roll, T pitch, T yaw)
geo::Mesh::addPoint
unsigned int addPoint(const geo::Vector3 &p)
geo::Vec3T::x
T x
geo::Vec2T
ed::ImageMask::height
int height() const
Definition: mask.h:65
geo::Shape
ed::serializeTimestamp
void serializeTimestamp(double time, ed::io::Writer &w)
Definition: serialization.cpp:457
writer.h
world_model.h
ed::ConvexHull::points
std::vector< geo::Vec2f > points
Definition: convex_hull.h:13