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


tuw_voronoi_graph
Author(s): Benjamin Binder
autogenerated on Wed Mar 2 2022 01:10:12