Parasol Planning Library (PPL)
ClearanceMap.h
Go to the documentation of this file.
1 #ifndef CLEARANCE_MAP_H_
2 #define CLEARANCE_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 class WorkspaceSkeleton;
22 class Cfg;
23 
27 struct Filtration{
28  double m_min;
29  Filtration(double _a){m_min = _a;}
30 
34  bool operator()(double _i){ return _i < m_min; }
35 };
36 
43 template<class MPTraits>
44 class ClearanceMap : public MPBaseObject<MPTraits> {
45 
46  public:
47 
50 
52  typedef stapl::sequential::directed_preds_graph<
53  stapl::MULTIEDGES, Point3d, vector<Point3d>> GraphType;
54  typedef GraphType::vertex_descriptor VD;
55  typedef GraphType::edge_descriptor ED;
56  typedef typename GraphType::vertex_iterator vertex_iterator;
57  typedef typename GraphType::adj_edge_iterator adj_edge_iterator;
58 
59  typedef typename MPTraits::CfgType CfgType;
60 
61  typedef function<bool(double)> FilterFunction;
62 
64 
65 
68  ClearanceMap() = default;
69  void Construct(WorkspaceSkeleton* _s);
71 
76  map<ED, double>& GetAnnotatedClearanceMap(FilterFunction&& _f);
81  WorkspaceSkeleton* GetAnnotatedSkeleton(FilterFunction&& _f);
82 
84  map<ED, double>& GetClearanceMap(){ return m_clearanceMap; }
85 
86 
87  private:
88 
91 
94  void ComputeMinClearance(const ED& _ed);
95 
99  double GetClearanceInfo(const Point3d& _p);
100 
103  void Remove2Nodes();
107 
112  struct EdgeDescriptorComp{
113 
119  bool operator()(const ED& _a, const ED& _b) const{
120  if(_a.source() > _b.source()) return true;
121  else if(_a.source() == _b.source()){
122  if(_a.target() > _b.target()){
123  return true;
124  }
125  }
126  return false;
127  }
128  };
129 
130  map<ED, double, EdgeDescriptorComp> m_clearanceMap;
131  WorkspaceSkeleton* m_skeleton{nullptr};
132 
134 };
135 
136 
137 
138 /*----------------------------------------------------------------------------*/
139 
140 template<class MPTraits>
141 void
144  m_skeleton = _s;
145  for(auto eit = m_skeleton->GetGraph().edges_begin();
146  eit != m_skeleton->GetGraph().edges_end(); eit++){
147  ComputeMinClearance(eit->descriptor());
148  }
149 }
150 
151 template<class MPTraits>
152 void
154 ComputeMinClearance(const ED& _ed){
155  vertex_iterator vit;
156  adj_edge_iterator eit;
157  m_skeleton->GetGraph().find_edge(_ed, vit, eit);
158 
159  const vector<Point3d>& vp = eit->property();
160 
161  // Compute the minimum clearance of points along the edge
162  double min = numeric_limits<double>::max();
163  for(const auto &n : vp){
164  double c = GetClearanceInfo(n);
165  if(c < min) min = c;
166  }
167 
168  if(m_clearanceMap.find(_ed) == m_clearanceMap.end()){
169  m_clearanceMap.insert(make_pair(_ed, min));
170  }
171 
172 }
173 
174 
175 template<class MPTraits>
179 
180  auto graph = m_skeleton->GetGraph();
181 
182  for(auto mit = m_clearanceMap.begin();
183  mit != m_clearanceMap.end(); ++mit){
184  if(_f(mit->second)){
185  m_clearanceMap[mit->first] = numeric_limits<double>::infinity();
186  graph.delete_edge(mit->first);
187  }
188  }
189  m_skeleton->SetGraph(graph);
190  Remove2Nodes();
191 
192  return m_skeleton;
193 
194 }
195 
196 template<class MPTraits>
197 map<typename ClearanceMap<MPTraits>::ED, double>&
200  for(auto mit = m_clearanceMap.begin();
201  mit != m_clearanceMap.end(); ++mit){
202  if(_f(mit->second)){
203  m_clearanceMap[mit->first] = numeric_limits<double>::infinity();
204  }
205  }
206  return m_clearanceMap;
207 }
208 
209 template<class MPTraits>
210 double
212 GetClearanceInfo(const Point3d& _p){
213  //auto boundary = this->GetEnvironment()->GetBoundary();
214  auto vc = this->GetValidityChecker("pqp_solid");
215  auto pointRobot = this->GetMPProblem()->GetRobot("point");
216 
217  CfgType cfg(_p, pointRobot);
218  CDInfo cdInfo;
219  cdInfo.ResetVars();
220  cdInfo.m_retAllInfo = true;
221  vc->IsValid(cfg, cdInfo, "Obtain clearance map");
222 
223  // Check against boundary
224  /*
225  const double boundaryClearance = boundary->GetClearance(_p);
226  if(boundaryClearance < cdInfo.m_minDist){
227  cdInfo.m_objectPoint = boundary->GetClearancePoint(_p);
228  cdInfo.m_minDist = boundaryClearance;
229  }
230  */
231  return cdInfo.m_minDist;
232 }
233 
234 template<class MPTraits>
235 void
237 Remove2Nodes() {
238  bool removed;
239  auto graph = m_skeleton->GetGraph();
240  do {
241  removed = false;
242  for(auto vit = graph.begin(); vit != graph.end(); ++vit) {
243  //detect 2-node
244  if(vit->predecessors().size() == 1 && vit->size() == 1) {
245  //get in and out edge
246  auto pred = graph.find_vertex(vit->predecessors()[0]);
247  ED in, out;
248  vector<Vector3d> inPath;
249  vector<Vector3d> outPath;
250  for(auto eit : *pred) {
251  if(eit.target() == vit->descriptor()){
252  in = eit.descriptor();
253  inPath = eit.property();
254  }
255  }
256  out = (*vit->begin()).descriptor();
257  outPath = (*vit->begin()).property();
258 
259  //Merge edge
260  vector<Vector3d> mergedPath;
261  for(size_t i = 0; i < inPath.size(); i++){
262  mergedPath.push_back(inPath[i]);
263  }
264  for(size_t i = 1; i < outPath.size(); i++){
265  mergedPath.push_back(outPath[i]);
266  }
267  ED neweid = graph.add_edge(in.source(), out.target(), mergedPath);
268 
269  //Delete old edges and delete old vertex
270  graph.delete_edge(in);
271  graph.delete_edge(out);
272  cout << "deleted 2-node ID: " << vit->descriptor() << endl;
273  graph.delete_vertex(vit->descriptor());
274  --vit;
275  removed = true;
276  }
277  }
278  } while(removed);
279  m_skeleton->SetGraph(graph);
280 }
281 
282 
283 /*----------------------------------------------------------------------------*/
284 
285 #endif
Definition: Cfg.h:38
Definition: ClearanceMap.h:44
GraphType::vertex_descriptor VD
Definition: ClearanceMap.h:54
GraphType::adj_edge_iterator adj_edge_iterator
Definition: ClearanceMap.h:57
GraphType::vertex_iterator vertex_iterator
Definition: ClearanceMap.h:56
WorkspaceSkeleton * GetAnnotatedSkeleton(FilterFunction &&_f)
Definition: ClearanceMap.h:178
void Construct(WorkspaceSkeleton *_s)
Definition: ClearanceMap.h:143
function< bool(double)> FilterFunction
Definition: ClearanceMap.h:61
GraphType::edge_descriptor ED
Definition: ClearanceMap.h:55
stapl::sequential::directed_preds_graph< stapl::MULTIEDGES, Point3d, vector< Point3d > > GraphType
Graph type is a directed multiedge graph of points and paths.
Definition: ClearanceMap.h:53
map< ED, double > & GetAnnotatedClearanceMap(FilterFunction &&_f)
Definition: ClearanceMap.h:199
ClearanceMap()=default
map< ED, double > & GetClearanceMap()
Get the clerance map.
Definition: ClearanceMap.h:84
MPTraits::CfgType CfgType
Definition: ClearanceMap.h:59
Definition: MPBaseObject.h:46
Geometric skeleton of the workspace.
Definition: WorkspaceSkeleton.h:22
Definition: Cfg.h:23
Definition: CDInfo.h:139
bool m_retAllInfo
Consider all collisions or only the first?
Definition: CDInfo.h:173
double m_minDist
Distance between Robot and closest obstacle.
Definition: CDInfo.h:176
void ResetVars(const bool _retAllInfo=false)
Definition: CDInfo.cpp:162
Filtration function.
Definition: ClearanceMap.h:27
Filtration(double _a)
Set the minimum clearance to the given clearance value.
Definition: ClearanceMap.h:29
double m_min
Minimum clearance.
Definition: ClearanceMap.h:28
bool operator()(double _i)
Definition: ClearanceMap.h:34
C CfgType
Definition: ParallelCfgTraits.h:145