serializer.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
3 #include <memory>
4 #include <opencv/cv.hpp>
5 #include <queue>
6 #include <string>
7 
8 
9 #include <boost/archive/xml_iarchive.hpp>
10 #include <boost/archive/xml_oarchive.hpp>
11 
12 using namespace cv;
13 
14 namespace tuw_graph
15 {
16 #define GRAPH_INFO_NAME "graphInfo"
17 #define TREE_INFO_NAME "treeInfo"
18 #define DATA_NAME "graphData"
19 
20  Serializer::Serializer()
21  {
22 
23  }
24 
25  std::size_t Serializer::getHash(const std::vector<signed char> &_map, Eigen::Vector2d _origin, float _resolution)
26  {
27  std::size_t seed = 0;
28 
29  boost::hash_combine(seed, _origin[0]);
30  boost::hash_combine(seed, _origin[1]);
31  boost::hash_combine(seed, _resolution);
32 
33  for(const signed char & val : _map)
34  {
35  boost::hash_combine(seed, val);
36  }
37 
38  return seed;
39  }
40 
41 
42  bool Serializer::load(const std::string &_mapPath, std::vector<Segment> &_segs, Eigen::Vector2d &_origin, float &_resolution)
43  {
44 
45  boost::filesystem::path graf(_mapPath + GRAPH_INFO_NAME);
46  boost::filesystem::path tree(_mapPath + TREE_INFO_NAME);
47  boost::filesystem::path data(_mapPath + DATA_NAME);
48 
49  if(!boost::filesystem::exists(graf) | !boost::filesystem::exists(tree) | !boost::filesystem::exists(data))
50  {
51  return false;
52  }
53 
54 
55  GraphInfo g;
56 
57  std::ifstream ifs(_mapPath + GRAPH_INFO_NAME);
58  assert(ifs.good());
59  boost::archive::xml_iarchive xml(ifs);
60  xml >> boost::serialization::make_nvp("GraphInfo", g);
61 
62 
63 
65 
66  std::ifstream ifti(_mapPath + TREE_INFO_NAME);
67  assert(ifti.good());
68  boost::archive::xml_iarchive xmlti(ifti);
69  xmlti >> boost::serialization::make_nvp("TreeInfo", t);
70 
71  _origin[0] = g.Origin.x;
72  _origin[1] = g.Origin.y;
73  _resolution = g.Resolution;
74 
75  std::vector<SegmentSerializer> segs;
76 
77  int *pred = t.Predecessors.get();
78  int *succ = t.Successors.get();
79  int *pts = t.Points.get();
80 
81  for(int i = 0; i < t.Length; i++)
82  {
83 
84  segs.emplace_back(pred[i], succ[i], pts[i]);
85  }
86 
87  GraphSerializer graph(segs);
88  std::ifstream ifsDist(_mapPath + DATA_NAME);
89  boost::archive::xml_iarchive iaDist(ifsDist);
90  iaDist >> boost::serialization::make_nvp("graph",graph);
91 
92  _segs.clear();
93 
94  //Generate Segment List
95  for(int i = 0; i < graph.Length; i++)
96  {
97  std::vector<Eigen::Vector2d> pts;
98 
99  for(int j = 0; j < graph.segments_[i].pointLength; j++)
100  {
101  PointSerializer *ptPtr = graph.segments_[i].points.get();
102  pts.emplace_back(ptPtr[j].x, ptPtr[j].y);
103  }
104 
105  Segment s(pts, graph.segments_[i].minDistance);
106  _segs.push_back(s);//std::make_shared<Segment>(pts, graph.segments_[i].minDistance));
107  }
108 
109 
110  //Add Dependancies
111  for(int i = 0; i < graph.Length; i++)
112  {
113  std::vector<uint32_t> predecessors;
114  std::vector<uint32_t> successors;
115 
116  for(int j = 0; j < graph.segments_[i].predecessorLength; j++)
117  {
118  int *predPtr = graph.segments_[i].predecessors.get();
119 
120  if(!_segs[i].containsPredecessor(predPtr[j]))
121  _segs[i].addPredecessor(predPtr[j]);
122  }
123 
124  for(int j = 0; j < graph.segments_[i].successorLength; j++)
125  {
126  int *succPtr = graph.segments_[i].successors.get();
127 
128  if(!_segs[i].containsSuccessor(succPtr[j]))
129  _segs[i].addSuccessor(succPtr[j]);
130  }
131 
132 
133  }
134 
135  return true;
136  }
137 
142  void Serializer::save(const std::string &_mapPath, const std::vector<Segment> &_segs, const Eigen::Vector2d &_origin, const float &_resolution)
143  {
144  if(!boost::filesystem::exists(_mapPath))
145  boost::filesystem::create_directories(_mapPath);
146 
147 
148  //Save map info (Length of segments)
149  GraphInfo info(_origin, _resolution, _segs.size());
150  std::ofstream ofs(_mapPath + GRAPH_INFO_NAME);
151  assert(ofs.good());
152  boost::archive::xml_oarchive oa(ofs);
153  oa << boost::serialization::make_nvp("GraphInfo", info);
154  ofs.close();
155 
156  //Save data strucutre info (Length pred, succ, points)
157  TreeInfo tInfo(_segs);
158  std::ofstream ofsTree(_mapPath + TREE_INFO_NAME);
159  assert(ofsTree.good());
160  boost::archive::xml_oarchive ot(ofsTree);
161  ot << boost::serialization::make_nvp("TreeInfo", tInfo);
162  ofsTree.close();
163 
164  //Save data
165  std::vector<SegmentSerializer> segs;
166 
167  for(const auto & seg : _segs)
168  {
169  segs.emplace_back(seg);
170  }
171 
172  GraphSerializer graph(segs);
173 
174  std::ofstream ofsGraph(_mapPath + DATA_NAME);
175  boost::archive::xml_oarchive oaGraph(ofsGraph);
176  oaGraph << boost::serialization::make_nvp("graph", graph);
177  ofsGraph.close();
178  }
179 
180 }
#define GRAPH_INFO_NAME
Definition: serializer.cpp:16
XmlRpcServer s
TFSIMD_FORCE_INLINE const tfScalar & y() const
#define TREE_INFO_NAME
Definition: serializer.cpp:17
std::unique_ptr< int > Predecessors
Definition: serializer.h:100
PointSerializer Origin
Definition: serializer.h:60
std::unique_ptr< int > predecessors
Definition: serializer.h:175
TFSIMD_FORCE_INLINE const tfScalar & x() const
std::unique_ptr< PointSerializer > points
Definition: serializer.h:179
std::unique_ptr< int > successors
Definition: serializer.h:176
std::unique_ptr< int > Successors
Definition: serializer.h:101
std::unique_ptr< int > Points
Definition: serializer.h:102
SegmentSerializer * segments_
Definition: serializer.h:207
#define DATA_NAME
Definition: serializer.cpp:18


tuw_voronoi_graph
Author(s): Benjamin Binder
autogenerated on Mon Jun 10 2019 15:42:44