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 
13 namespace tuw_graph
14 {
15 #define GRAPH_INFO_NAME "graphInfo"
16 #define TREE_INFO_NAME "treeInfo"
17 #define DATA_NAME "graphData"
18 #define MAP_NAME "map.png"
19 
21  {
22 
23  }
24 
25  std::size_t Serializer::getHash(const std::vector<signed char> &_map, const std::vector<double> &_parameters) const
26  {
27  std::size_t seed = 0;
28 
29  for(const double & val : _parameters)
30  {
31  boost::hash_combine(seed, val);
32  }
33 
34  for(const signed char & val : _map)
35  {
36  boost::hash_combine(seed, val);
37  }
38 
39  return seed;
40  }
41 
42 
43  bool Serializer::load(const std::string &_mapPath, std::vector<Segment> &_segs, Eigen::Vector2d &_origin, float &_resolution)
44  {
45  {
46  boost::filesystem::path graf(_mapPath + GRAPH_INFO_NAME);
47  boost::filesystem::path tree(_mapPath + TREE_INFO_NAME);
48  boost::filesystem::path data(_mapPath + DATA_NAME);
49 
50  if(!boost::filesystem::exists(graf) | !boost::filesystem::exists(tree) | !boost::filesystem::exists(data) )
51  {
52  return false;
53  }
54  }
55 
56 
57  GraphInfo g;
58 
59  std::ifstream ifs(_mapPath + GRAPH_INFO_NAME);
60  assert(ifs.good());
61  boost::archive::xml_iarchive xml(ifs);
62  xml >> boost::serialization::make_nvp("GraphInfo", g);
63 
64 
65 
67 
68  std::ifstream ifti(_mapPath + TREE_INFO_NAME);
69  assert(ifti.good());
70  boost::archive::xml_iarchive xmlti(ifti);
71  xmlti >> boost::serialization::make_nvp("TreeInfo", t);
72 
73  _origin[0] = g.Origin.x;
74  _origin[1] = g.Origin.y;
75  _resolution = g.Resolution;
76 
77  std::vector<SegmentSerializer> segs;
78 
79  int *pred = t.Predecessors.get();
80  int *succ = t.Successors.get();
81  int *pts = t.Points.get();
82 
83  for(int i = 0; i < t.Length; i++)
84  {
85 
86  segs.emplace_back(pred[i], succ[i], pts[i]);
87  }
88 
89  GraphSerializer graph(segs);
90  std::ifstream ifsDist(_mapPath + DATA_NAME);
91  boost::archive::xml_iarchive iaDist(ifsDist);
92  iaDist >> boost::serialization::make_nvp("graph",graph);
93 
94  _segs.clear();
95 
96  //Generate Segment List
97  for(int i = 0; i < graph.Length; i++)
98  {
99  std::vector<Eigen::Vector2d> pts;
100 
101  for(int j = 0; j < graph.segments_[i].pointLength; j++)
102  {
103  PointSerializer *ptPtr = graph.segments_[i].points.get();
104  pts.emplace_back(ptPtr[j].x, ptPtr[j].y);
105  }
106 
107  Segment s(pts, graph.segments_[i].minDistance);
108  _segs.push_back(s);//std::make_shared<Segment>(pts, graph.segments_[i].minDistance));
109  }
110 
111 
112  //Add Dependancies
113  for(int i = 0; i < graph.Length; i++)
114  {
115  std::vector<uint32_t> predecessors;
116  std::vector<uint32_t> successors;
117 
118  for(int j = 0; j < graph.segments_[i].predecessorLength; j++)
119  {
120  int *predPtr = graph.segments_[i].predecessors.get();
121 
122  if(!_segs[i].containsPredecessor(predPtr[j]))
123  _segs[i].addPredecessor(predPtr[j]);
124  }
125 
126  for(int j = 0; j < graph.segments_[i].successorLength; j++)
127  {
128  int *succPtr = graph.segments_[i].successors.get();
129 
130  if(!_segs[i].containsSuccessor(succPtr[j]))
131  _segs[i].addSuccessor(succPtr[j]);
132  }
133 
134 
135  }
136 
137 
138  return true;
139  }
140  bool Serializer::load(const std::string &_mapPath, std::vector<Segment> &_segs, Eigen::Vector2d &_origin, float &_resolution, cv::Mat &_map){
141  if(load(_mapPath, _segs, _origin, _resolution) && boost::filesystem::exists(boost::filesystem::path(_mapPath + MAP_NAME))){
142  _map = cv::imread(_mapPath + MAP_NAME, cv::IMREAD_GRAYSCALE);
143  return true;
144  } else {
145  return false;
146  }
147  }
148 
153  void Serializer::save(const std::string &_mapPath, const std::vector<Segment> &_segs, const Eigen::Vector2d &_origin, const float &_resolution)
154  {
155  if(!boost::filesystem::exists(_mapPath))
156  boost::filesystem::create_directories(_mapPath);
157 
158 
159  //Save map info (Length of segments)
160  GraphInfo info(_origin, _resolution, _segs.size());
161  std::ofstream ofs(_mapPath + GRAPH_INFO_NAME);
162  assert(ofs.good());
163  boost::archive::xml_oarchive oa(ofs);
164  oa << boost::serialization::make_nvp("GraphInfo", info);
165  ofs.close();
166 
167  //Save data strucutre info (Length pred, succ, points)
168  TreeInfo tInfo(_segs);
169  std::ofstream ofsTree(_mapPath + TREE_INFO_NAME);
170  assert(ofsTree.good());
171  boost::archive::xml_oarchive ot(ofsTree);
172  ot << boost::serialization::make_nvp("TreeInfo", tInfo);
173  ofsTree.close();
174 
175  //Save data
176  std::vector<SegmentSerializer> segs;
177 
178  for(const auto & seg : _segs)
179  {
180  segs.emplace_back(seg);
181  }
182 
183  GraphSerializer graph(segs);
184 
185  std::ofstream ofsGraph(_mapPath + DATA_NAME);
186  boost::archive::xml_oarchive oaGraph(ofsGraph);
187  oaGraph << boost::serialization::make_nvp("graph", graph);
188  ofsGraph.close();
189 
190  }
191  void Serializer::save(const std::string &_mapPath, const std::vector<Segment> &_segs, const Eigen::Vector2d &_origin, const float &_resolution, const cv::Mat &_map){
192  save(_mapPath, _segs, _origin, _resolution);
193  if(!boost::filesystem::exists(_mapPath))
194  boost::filesystem::create_directories(_mapPath);
195  if(_map.size != 0){
196  cv::imwrite(_mapPath + MAP_NAME, _map);
197  }
198  }
199 
200 }
#define GRAPH_INFO_NAME
Definition: serializer.cpp:15
size_t getHash(const std::vector< signed char > &_map, const std::vector< double > &_parameters) const
generate a hash from a _map
Definition: serializer.cpp:25
XmlRpcServer s
#define TREE_INFO_NAME
Definition: serializer.cpp:16
std::unique_ptr< int > Predecessors
Definition: serializer.h:99
PointSerializer Origin
Definition: serializer.h:59
std::unique_ptr< int > predecessors
Definition: serializer.h:174
bool load(const std::string &_mapPath, std::vector< Segment > &_segs, Eigen::Vector2d &_origin, float &_resolution)
loads a graph from memory which is saved in plain text
Definition: serializer.cpp:43
std::unique_ptr< PointSerializer > points
Definition: serializer.h:178
std::unique_ptr< int > successors
Definition: serializer.h:175
#define MAP_NAME
Definition: serializer.cpp:18
void save(const std::string &_mapPath, const std::vector< Segment > &_segs, const Eigen::Vector2d &_origin, const float &_resolution)
saves the graph to a specific path in xml format
Definition: serializer.cpp:153
std::unique_ptr< int > Successors
Definition: serializer.h:100
std::unique_ptr< int > Points
Definition: serializer.h:101
SegmentSerializer * segments_
Definition: serializer.h:206
#define DATA_NAME
Definition: serializer.cpp:17


tuw_voronoi_graph
Author(s): Benjamin Binder
autogenerated on Mon Feb 28 2022 23:57:47