Parasol Planning Library (PPL)
PropertyMap.h
Go to the documentation of this file.
1 #ifndef PROPERTY_MAP_H_
2 #define PROPERTY_MAP_H_
3 
4 #include <queue>
5 #include <unordered_map>
6 
11 #include "MPLibrary/MPBaseObject.h"
12 #include "WorkspaceSkeleton.h"
13 
14 #include <containers/sequential/graph/directed_preds_graph.h>
15 
16 #include "Vector.h"
17 
18 using namespace mathtool;
19 using namespace std;
20 
21 
23 struct edgeHash {
24 
26  size_t operator()(const WorkspaceSkeleton::ED& _ed) const {
27  return hash<typename WorkspaceSkeleton::ED::edge_id_type>()(_ed.id());
28  }
29 };
30 
31 
37 template<typename EdgeProperty, typename VertexProperty=void>
38 class PropertyMap {
39 
40  public:
41 
44 
51 
54  typedef function<bool(EdgeProperty&)> EdgeFilterFunction;
55  typedef function<bool(VertexProperty&)> VertexFilterFunction;
56 
57  typedef unordered_map<ED, EdgeProperty, edgeHash> EdgeMapType;
58  typedef unordered_map<VD, VertexProperty> VertexMapType;
59 
63 
64  PropertyMap() = default;
65  PropertyMap(WorkspaceSkeleton* _ws) : m_skeleton(_ws) {}
66 
70 
76 
82 
86  void SetSkeleton(WorkspaceSkeleton* _ws) { m_skeleton = _ws;}
87  VertexMapType& GetVertexMap() { return m_vertexMap; }
88  EdgeMapType& GetEdgeMap() { return m_edgeMap; }
89  void SetVertexMap(VertexMapType& _vMap) { m_vertexMap = _vMap; }
90  void SetEdgeMap(EdgeMapType& _eMap) { m_edgeMap = _eMap; }
91 
93 
94  void SetVertexProperty(const VD& _v, const VertexProperty& _vp);
95  void SetEdgeProperty(const ED& _e, const EdgeProperty& _ep);
96  VertexProperty& GetVertexProperty(const VD& _v);
97  EdgeProperty& GetEdgeProperty(const ED& _e);
99 
102 
105 
106  void Write(const std::string& _file);
107 
110 
111  void Read(const std::string& _file);
113 
114 
115 
116  private:
117 
120  WorkspaceSkeleton* m_skeleton{nullptr};
121  EdgeMapType m_edgeMap;
122  VertexMapType m_vertexMap;
124 };
125 
128  bool _boundary = true);
129 
130 // void
131 // ClearanceFilteredSkeleton(double _t, MPBaseObject* _mp,
132 // WorkspaceSkeleton* _ws, bool _boundary = true);
133 
134 /*------------------------------------------------------------------------*/
135 
136 
137 template<typename EdgeProperty, typename VertexProperty>
138 EdgeProperty&
140 GetEdgeProperty(const ED& _e) {
141  return m_edgeMap.at(_e);
142 }
143 
144 template<typename EdgeProperty, typename VertexProperty>
145 void
147 SetEdgeProperty(const ED& _e, const EdgeProperty& _ep) {
148  m_edgeMap.emplace(_e, _ep);
149 }
150 
151 template<typename EdgeProperty, typename VertexProperty>
152 VertexProperty&
154 GetVertexProperty(const VD& _v) {
155  return m_vertexMap.at(_v);
156 }
157 
158 template<typename EdgeProperty, typename VertexProperty>
159 void
161 SetVertexProperty(const VD& _v, const VertexProperty& _vp) {
162  m_vertexMap.emplace(_v, _vp);
163 }
164 
165 template<typename EdgeProperty, typename VertexProperty>
169 
170  for(auto mit = m_vertexMap.begin();
171  mit != m_vertexMap.end(); ++mit) {
172  if(_f(mit->second)) {
173  m_skeleton->DeleteVertex(mit->first);
174  }
175  }
176 
177  return m_skeleton;
178 }
179 
180 template<typename EdgeProperty, typename VertexProperty>
184 
185  for(auto mit = m_edgeMap.begin();
186  mit != m_edgeMap.end(); ++mit) {
187  if(_f(mit->second)) {
188  m_skeleton->delete_edge(mit->first);
189  }
190  }
191 
192  return m_skeleton;
193 }
194 
195 /*------------------------------------- I/O helpers ---------------------------------*/
196 
197 template<typename EdgeProperty, typename VertexProperty>
198 void
200 Write(const std::string& _file) {
201  std::ofstream ofs(_file);
202 
203  ofs << m_vertexMap.size() <<" "<< m_edgeMap.size() << std::endl;
204  for(auto vit : m_vertexMap){
205  auto spk = this->GetVertexProperty(vit.first);
206  ofs << vit.first <<" "<< spk.size() <<" ";
207  for(auto s : spk)
208  ofs << s <<" ";
209  ofs << std::endl;
210  }
211 
212  for(auto eit : m_edgeMap) {
213  auto spks = this->GetEdgeProperty(eit.first);
214 
215  ofs << eit.first.id() << " ";
216  ofs << eit.first.source() << " " << eit.first.target() << " " << spks.size() << std::endl;
217 
218  for(auto spk : spks) {
219  ofs << spk.size() << " ";
220  for(auto s : spk) {
221  ofs << s << " ";
222  }
223  ofs << std::endl;
224  }
225  }
226 
227  ofs.close();
228 }
229 
230 template<typename EdgeProperty, typename VertexProperty>
231 void
233 Read(const std::string& _file) {
234  typedef WorkspaceSkeleton::VD VD;
235  typedef WorkspaceSkeleton::ED ED;
236 
237  std::fstream ifs(_file);
238  size_t nVerts, nEdges;
239 
240  ifs >> nVerts >> nEdges;
241 
242  for(size_t vit = 0; vit < nVerts; vit++) {
243  size_t propSize;
244  VD vid;
245  std::vector<Point3d> vertexProperty;
246 
247  ifs >> vid >> propSize;
248  for (size_t propit = 0; propit < propSize; propit++) {
249  Point3d prop;
250  ifs >> prop;
251  vertexProperty.push_back(prop);
252  }
253  this->SetVertexProperty(vid, vertexProperty);
254  }
255 
256  for(size_t eit = 0; eit < nEdges; eit++) {
257 
258  size_t proSize, eSource, eTarget, eDesID;
259  std::vector<vector<Point3d> > intermediateWitnesses;
260 
261  ifs >> eDesID;
262  ifs >> eSource >> eTarget >> proSize;
263  for (size_t pit = 0; pit < proSize; pit++) {
264 
265  std::vector<Point3d> edgeProperty;
266  size_t eSize;
267  ifs >> eSize;
268  for(size_t it = 0; it < eSize; it++) {
269  Point3d prop;
270  ifs >> prop;
271  edgeProperty.push_back(prop);
272  }
273 
274  intermediateWitnesses.push_back(edgeProperty);
275  }
276 
277  auto ed = ED(eSource, eTarget, eDesID);
278  this->SetEdgeProperty(ed, intermediateWitnesses);
279  }
280 }
281 
282 /*------------------------------------------------------------------------*/
283 #endif
PropertyMap< vector< double >, double > * ClearanceAnnotatedSkeleton(MPBaseObject *_mp, WorkspaceSkeleton *_ws, bool _boundary=true)
Function to generate the annotated clearance skeleton.
Definition: PropertyMap.cpp:9
virtual void DeleteVertex(const VID _v) noexcept
Definition: GenericStateGraph.h:663
Definition: MPBaseObject.h:46
Definition: PropertyMap.h:38
PropertyMap(WorkspaceSkeleton *_ws)
Definition: PropertyMap.h:65
VertexProperty & GetVertexProperty(const VD &_v)
Definition: PropertyMap.h:154
WorkspaceSkeleton * GetVertexFilteredSkeleton(VertexFilterFunction &&_f)
Definition: PropertyMap.h:168
void Read(const std::string &_file)
Definition: PropertyMap.h:233
unordered_map< VD, VertexProperty > VertexMapType
Definition: PropertyMap.h:58
EdgeMapType & GetEdgeMap()
Definition: PropertyMap.h:88
WorkspaceSkeleton * GetEdgeFilteredSkeleton(EdgeFilterFunction &&_f)
Definition: PropertyMap.h:183
PropertyMap()=default
WorkspaceSkeleton::VD VD
Definition: PropertyMap.h:47
VertexMapType & GetVertexMap()
Definition: PropertyMap.h:87
void SetEdgeProperty(const ED &_e, const EdgeProperty &_ep)
Definition: PropertyMap.h:147
GraphType::adj_edge_iterator adj_edge_iterator
Definition: PropertyMap.h:50
EdgeProperty & GetEdgeProperty(const ED &_e)
Definition: PropertyMap.h:140
void SetVertexProperty(const VD &_v, const VertexProperty &_vp)
Individual accessors.
Definition: PropertyMap.h:161
GraphType::vertex_iterator vertex_iterator
Definition: PropertyMap.h:49
function< bool(EdgeProperty &)> EdgeFilterFunction
Definition: PropertyMap.h:54
void SetVertexMap(VertexMapType &_vMap)
Definition: PropertyMap.h:89
void SetEdgeMap(EdgeMapType &_eMap)
Definition: PropertyMap.h:90
void SetSkeleton(WorkspaceSkeleton *_ws)
Definition: PropertyMap.h:86
WorkspaceSkeleton GraphType
Graph type is a directed multiedge graph of points and paths.
Definition: PropertyMap.h:46
void Write(const std::string &_file)
Definition: PropertyMap.h:200
WorkspaceSkeleton::ED ED
Definition: PropertyMap.h:48
unordered_map< ED, EdgeProperty, edgeHash > EdgeMapType
Definition: PropertyMap.h:57
function< bool(VertexProperty &)> VertexFilterFunction
Definition: PropertyMap.h:55
Geometric skeleton of the workspace.
Definition: WorkspaceSkeleton.h:22
BaseType::EID ED
Definition: WorkspaceSkeleton.h:32
BaseType::VID VD
Definition: WorkspaceSkeleton.h:31
BaseType::VI vertex_iterator
Definition: WorkspaceSkeleton.h:33
BaseType::EI adj_edge_iterator
Definition: WorkspaceSkeleton.h:35
Definition: Cfg.h:23
Hash for edge descriptor.
Definition: PropertyMap.h:23
size_t operator()(const WorkspaceSkeleton::ED &_ed) const
return a hashed value for a given edge descriptor
Definition: PropertyMap.h:26