Parasol Planning Library (PPL)
ReebGraphConstruction.h
Go to the documentation of this file.
1 #ifndef PMPL_REEB_GRAPH_CONSTRUCTION_H_
2 #define PMPL_REEB_GRAPH_CONSTRUCTION_H_
3 
4 #include <vector>
5 #include <set>
6 #include <unordered_set>
7 using namespace std;
8 
9 #include <boost/functional/hash.hpp>
10 
11 #include <containers/sequential/graph/directed_preds_graph.h>
12 
13 #include <Vector.h>
14 using namespace mathtool;
15 
16 #include "Utilities/IOUtils.h"
17 
18 class Environment;
20 class WorkspaceSkeleton;
21 class XMLNode;
22 
23 
39 
40  public:
41 
44 
46  struct Parameters {
47  std::string filename;
48  bool write{false};
49  bool read{false};
50  bool debug{false};
51  };
52 
54 
55  typedef tuple<size_t, size_t, size_t> Triangle;
57 
61  struct ReebNode {
62 
66  ReebNode(size_t _vIndx = -1, const Vector3d& _v = Vector3d(),
67  double _w = numeric_limits<double>::max()) :
68  m_vertexIndex(_vIndx), m_vertex(_v), m_w(_w) {}
69 
70  friend istream& operator>>(istream& _is, ReebNode& _rn);
71  friend ostream& operator<<(ostream& _os, const ReebNode& _rn);
72 
73  size_t m_vertexIndex;
74  Vector3d m_vertex;
75  double m_w;
76  size_t m_order{0};
77  size_t m_tetra{0};
78  };
79 
84  struct ReebNodeComp {
85 
89  bool operator()(const ReebNode& _a, const ReebNode& _b) {
90 
95  auto vertComp = [](const Vector3d _a, const Vector3d _b) {
96  return _a[0] - _b[0] > ReebTolerance() ||
97  (fabs(_a[0] - _b[0]) < ReebTolerance() && (_a[1] - _b[1] > ReebTolerance() ||
98  (fabs(_a[1] - _b[1]) < ReebTolerance() && _a[2] - _b[2] > ReebTolerance())));
99  };
100 
101  const Vector3d& a = _a.m_vertex;
102  const Vector3d& b = _b.m_vertex;
103  bool ret = _b.m_vertexIndex != _a.m_vertexIndex &&
104  (_a.m_w - _b.m_w > ReebTolerance() ||
105  (fabs(_a.m_w - _b.m_w) < ReebTolerance() &&
106  (vertComp(a, b) || (a == b &&
107  _a.m_vertexIndex < _b.m_vertexIndex))));
108  return ret;
109  }
110 
112  static constexpr double ReebTolerance() {return 0.000001;}
113  };
114 
115  struct MeshEdge;
116 
121  struct ReebArc {
122 
126  ReebArc(size_t _s = -1, size_t _t = -1, MeshEdge* _m = nullptr) :
127  m_source(_s), m_target(_t) {
128  if(_m)
129  m_edges.insert(_m);
130  }
131 
132  friend istream& operator>>(istream& _is, ReebArc& _ra);
133  friend ostream& operator<<(ostream& _os, const ReebArc& _ra);
134 
135  size_t m_source;
136  size_t m_target;
137  unordered_set<MeshEdge*> m_edges;
138  unordered_set<size_t> m_tetra;
139  vector<Vector3d> m_path;
140  };
141 
142  typedef stapl::sequential::directed_preds_graph<
143  stapl::MULTIEDGES, ReebNode, ReebArc>
146  typedef ReebGraph::edge_descriptor RGEID;
147 
152  struct ReebArcComp {
153 
155  ReebArcComp(ReebGraph* _rg) : m_rg(_rg) {}
156 
160  bool operator()(const RGEID& _a, const RGEID& _b) {
161  bool ret = false;
162  ReebNode& s0 = m_rg->find_vertex(_a.source())->property();
163  ReebNode& s1 = m_rg->find_vertex(_b.source())->property();
164  if(s0.m_order < s1.m_order)
165  ret = true;
166  else if(s0.m_order == s1.m_order) {
167  ReebNode& t0 = m_rg->find_vertex(_a.target())->property();
168  ReebNode& t1 = m_rg->find_vertex(_b.target())->property();
169  if(t0.m_order < t1.m_order)
170  ret = true;
171  else if(t0.m_order == t1.m_order)
172  if(_a.id() < _b.id())
173  ret = true;
174  }
175  return ret;
176  }
177 
179  };
180 
181  typedef set<RGEID, ReebArcComp> ArcSet;
182 
186  struct MeshEdge {
187 
191  MeshEdge(size_t _s, size_t _t, ReebGraph* _rg) :
192  m_source(_s), m_target(_t), m_numTris(0), m_arcs(ReebArcComp(_rg)) {
193  }
194 
195  size_t m_source;
196  size_t m_target;
197  size_t m_numTris;
199  };
200 
204  struct MeshEdgeHash {
205 
208  size_t operator()(const MeshEdge* const _m) const {
209  return h(make_pair(_m->m_source, _m->m_target));
210  }
211 
212  boost::hash<pair<size_t, size_t>> h;
213  };
214 
218  struct MeshEdgeEq {
219 
223  bool operator()(const MeshEdge* const _a, const MeshEdge* const _b) const {
224  return _a->m_source == _b->m_source && _a->m_target == _b->m_target;
225  }
226  };
227 
228  typedef stapl::sequential::directed_preds_graph<
229  stapl::MULTIEDGES, Vector3d, vector<Vector3d>
232 
236 
238 
241  static void SetDefaultParameters(XMLNode& _node);
242 
246 
251  void Construct(const WorkspaceDecomposition* _decomposition);
252 
256  WorkspaceSkeleton GetSkeleton();
257 
262 
264  ReebGraph& GetReebGraph() {return m_reebGraph;}
265 
269 
272  void Read(const string& _filename);
273 
276  void Write(const string& _filename);
277 
279 
280  private:
281 
284 
287  void Initialize(const WorkspaceDecomposition* _decomposition);
288 
300  void Construct();
301 
306  void CreateNode(size_t _i, const Vector3d& _v, double _w);
307 
318  MeshEdge* CreateArc(size_t _s, size_t _t,
319  const unordered_set<size_t>& _tetra = unordered_set<size_t>());
320 
333  void MergePaths(MeshEdge* _e0, MeshEdge* _e1, MeshEdge* _e2);
334 
352  void GlueByMergeSorting(ArcSet& _a0, MeshEdge* _e0,
353  ArcSet& _a1, MeshEdge* _e1);
354 
364  void MergeArcs(RGEID _a0, RGEID _a1);
365 
368  ReebArc& GetReebArc(RGEID _a);
369 
372  void DeleteMeshEdge(MeshEdge* _e);
373 
375  void Remove2Nodes();
376 
383  void Embed(const WorkspaceDecomposition* _tetrahedralization);
384 
386  void Uniqueify();
387 
391 
392  Parameters m_params;
393 
394  vector<Vector3d> m_vertices;
395 
397  unordered_set<MeshEdge*, MeshEdgeHash, MeshEdgeEq> m_edges;
398 
400  vector<pair<Triangle, unordered_set<size_t>>> m_triangles;
401 
402  ReebGraph m_reebGraph;
403 
405 };
406 
407 #endif
ostream & operator<<(ostream &_os, const Cfg &_cfg)
Definition: Cfg.cpp:1280
istream & operator>>(istream &_is, Cfg &_cfg)
Definition: Cfg.cpp:1273
void Read(GenericStateGraph *_g, const std::string &_filename)
Definition: GenericStateGraph.h:1337
Definition: Environment.h:137
Definition: ReebGraphConstruction.h:38
stapl::sequential::directed_preds_graph< stapl::MULTIEDGES, Vector3d, vector< Vector3d > > FlowGraph
Definition: ReebGraphConstruction.h:230
ReebGraph & GetReebGraph()
Get the embedding graph.
Definition: ReebGraphConstruction.h:264
static Parameters m_defaultParams
The default parameters.
Definition: ReebGraphConstruction.h:53
set< RGEID, ReebArcComp > ArcSet
Set of ReebArcs.
Definition: ReebGraphConstruction.h:181
stapl::sequential::directed_preds_graph< stapl::MULTIEDGES, ReebNode, ReebArc > ReebGraph
Definition: ReebGraphConstruction.h:144
tuple< size_t, size_t, size_t > Triangle
Definition: ReebGraphConstruction.h:55
ReebGraph::edge_descriptor RGEID
ReebGraph edge descriptor.
Definition: ReebGraphConstruction.h:146
Definition: WorkspaceDecomposition.h:24
Geometric skeleton of the workspace.
Definition: WorkspaceSkeleton.h:22
Definition: XMLNode.h:27
Definition: Cfg.h:23
Mesh edge equivalence. Based on source/target pair.
Definition: ReebGraphConstruction.h:218
bool operator()(const MeshEdge *const _a, const MeshEdge *const _b) const
Definition: ReebGraphConstruction.h:223
Hash function for MeshEdge. Hash based on source/target pair.
Definition: ReebGraphConstruction.h:204
size_t operator()(const MeshEdge *const _m) const
Definition: ReebGraphConstruction.h:208
boost::hash< pair< size_t, size_t > > h
Definition: ReebGraphConstruction.h:212
Tetrahedralization edge. Stores associates ReebEdges.
Definition: ReebGraphConstruction.h:186
MeshEdge(size_t _s, size_t _t, ReebGraph *_rg)
Definition: ReebGraphConstruction.h:191
size_t m_target
Target vertex index.
Definition: ReebGraphConstruction.h:196
size_t m_numTris
Number of triangles which it is incident on.
Definition: ReebGraphConstruction.h:197
ArcSet m_arcs
Set of Reeb Arcs.
Definition: ReebGraphConstruction.h:198
size_t m_source
Source vertex index.
Definition: ReebGraphConstruction.h:195
The parameters for this object.
Definition: ReebGraphConstruction.h:46
std::string filename
Input/output filename for embedded reeb graph.
Definition: ReebGraphConstruction.h:47
Definition: ReebGraphConstruction.h:152
ReebGraph * m_rg
ReebGraph pointer.
Definition: ReebGraphConstruction.h:178
ReebArcComp(ReebGraph *_rg)
Definition: ReebGraphConstruction.h:155
bool operator()(const RGEID &_a, const RGEID &_b)
Definition: ReebGraphConstruction.h:160
Definition: ReebGraphConstruction.h:121
vector< Vector3d > m_path
Embedded ReebArc.
Definition: ReebGraphConstruction.h:139
unordered_set< size_t > m_tetra
Related tetrahedron.
Definition: ReebGraphConstruction.h:138
ReebArc(size_t _s=-1, size_t _t=-1, MeshEdge *_m=nullptr)
Definition: ReebGraphConstruction.h:126
size_t m_target
Target vertex index.
Definition: ReebGraphConstruction.h:136
unordered_set< MeshEdge * > m_edges
Related mesh edges.
Definition: ReebGraphConstruction.h:137
size_t m_source
Source vertex index.
Definition: ReebGraphConstruction.h:135
Definition: ReebGraphConstruction.h:84
bool operator()(const ReebNode &_a, const ReebNode &_b)
Definition: ReebGraphConstruction.h:89
static constexpr double ReebTolerance()
Definition: ReebGraphConstruction.h:112
Vertex property of ReebGraph.
Definition: ReebGraphConstruction.h:61
size_t m_vertexIndex
Vertex Index.
Definition: ReebGraphConstruction.h:73
ReebNode(size_t _vIndx=-1, const Vector3d &_v=Vector3d(), double _w=numeric_limits< double >::max())
Definition: ReebGraphConstruction.h:66
Vector3d m_vertex
Vertex.
Definition: ReebGraphConstruction.h:74
size_t m_order
Total ordering of vertex.
Definition: ReebGraphConstruction.h:76
double m_w
Morse function value.
Definition: ReebGraphConstruction.h:75