g2o
hyper_graph.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_AIS_HYPER_GRAPH_HH
28 #define G2O_AIS_HYPER_GRAPH_HH
29 
30 #include <map>
31 #include <set>
32 #include <bitset>
33 #include <cassert>
34 #include <vector>
35 #include <limits>
36 #include <cstddef>
37 
38 #include <unordered_map>
39 
40 #include "g2o_core_api.h"
41 
44 namespace g2o {
45 
56  {
57  public:
58 
62  enum G2O_CORE_API HyperGraphElementType {
64  HGET_EDGE,
66  HGET_CACHE,
67  HGET_DATA,
68  HGET_NUM_ELEMS // keep as last elem
69  };
70 
71  static const int UnassignedId = -1;
72  static const int InvalidId = -2;
73 
74  typedef std::bitset<HyperGraph::HGET_NUM_ELEMS> GraphElemBitset;
75 
80 
85  virtual ~HyperGraphElement() {}
89  virtual HyperGraphElementType elementType() const = 0;
90  HyperGraphElement* clone() const { return 0; }
91  };
92 
98  public:
99  Data();
100  ~Data();
102  virtual bool read(std::istream& is) = 0;
104  virtual bool write(std::ostream& os) const = 0;
105  virtual HyperGraph::HyperGraphElementType elementType() const { return HyperGraph::HGET_DATA;}
106  inline const Data* next() const {return _next;}
107  inline Data* next() {return _next;}
108  inline void setNext(Data* next_) { _next = next_; }
109  inline DataContainer* dataContainer() { return _dataContainer;}
110  inline const DataContainer* dataContainer() const { return _dataContainer;}
111  inline void setDataContainer(DataContainer * dataContainer_){ _dataContainer = dataContainer_;}
112  protected:
113  Data* _next; // linked list of multiple data;
115  };
116 
122  public:
123  DataContainer() {_userData = 0;}
124  virtual ~DataContainer() { delete _userData;}
126  const Data* userData() const { return _userData; }
127  Data* userData() { return _userData; }
128  void setUserData(Data* obs) { _userData = obs;}
129  void addUserData(Data* obs) { if (obs) { obs->setNext(_userData); _userData=obs; } }
130  protected:
132  };
133 
134 
135  typedef std::set<Edge*> EdgeSet;
136  typedef std::set<Vertex*> VertexSet;
137 
138  typedef std::unordered_map<int, Vertex*> VertexIDMap;
139  typedef std::vector<Vertex*> VertexContainer;
140 
143  public:
145  explicit Vertex(int id=InvalidId);
146  virtual ~Vertex();
148  int id() const {return _id;}
149  virtual void setId( int newId) { _id=newId; }
151  const EdgeSet& edges() const {return _edges;}
153  EdgeSet& edges() {return _edges;}
154  virtual HyperGraphElementType elementType() const { return HGET_VERTEX;}
155  protected:
156  int _id;
157  EdgeSet _edges;
158  };
159 
160 
166  public:
168  explicit Edge(int id = InvalidId);
169  virtual ~Edge();
170 
174  virtual void resize(size_t size);
178  const VertexContainer& vertices() const { return _vertices;}
182  VertexContainer& vertices() { return _vertices;}
186  const Vertex* vertex(size_t i) const { assert(i < _vertices.size() && "index out of bounds"); return _vertices[i];}
190  Vertex* vertex(size_t i) { assert(i < _vertices.size() && "index out of bounds"); return _vertices[i];}
194  void setVertex(size_t i, Vertex* v) { assert(i < _vertices.size() && "index out of bounds"); _vertices[i]=v;}
195 
196  int id() const {return _id;}
197  void setId(int id);
198  virtual HyperGraphElementType elementType() const { return HGET_EDGE;}
199 
200  int numUndefinedVertices() const;
201  protected:
202  VertexContainer _vertices;
203  int _id;
204  };
205 
206  public:
208  HyperGraph();
210  virtual ~HyperGraph();
211 
213  Vertex* vertex(int id);
215  const Vertex* vertex(int id) const;
216 
218  virtual bool removeVertex(Vertex* v, bool detach=false);
220  virtual bool removeEdge(Edge* e);
222  virtual void clear();
223 
225  const VertexIDMap& vertices() const {return _vertices;}
227  VertexIDMap& vertices() {return _vertices;}
228 
230  const EdgeSet& edges() const {return _edges;}
232  EdgeSet& edges() {return _edges;}
233 
240  virtual bool addVertex(Vertex* v);
241 
246  virtual bool addEdge(Edge* e);
247 
248 
253  virtual bool setEdgeVertex(Edge* e, int pos, Vertex* v);
254 
260  virtual bool mergeVertices(Vertex* vBig, Vertex* vSmall, bool erase);
261 
265  virtual bool detachVertex(Vertex* v);
266 
271  virtual bool changeId(Vertex* v, int newId);
272 
273  protected:
274  VertexIDMap _vertices;
275  EdgeSet _edges;
276 
277  private:
278  // Disable the copy constructor and assignment operator
279  HyperGraph(const HyperGraph&) { }
280  HyperGraph& operator= (const HyperGraph&) { return *this; }
281  };
282 
283 } // end namespace
284 
286 
287 #endif
const Data * next() const
Definition: hyper_graph.h:106
int id() const
returns the id
Definition: hyper_graph.h:148
const Vertex * vertex(size_t i) const
Definition: hyper_graph.h:186
virtual HyperGraph::HyperGraphElementType elementType() const
Definition: hyper_graph.h:105
data packet for a vertex. Extend this class to store in the vertices the potential additional informa...
Definition: hyper_graph.h:97
const Data * userData() const
the user data associated with this vertex
Definition: hyper_graph.h:126
std::vector< Vertex * > VertexContainer
Definition: hyper_graph.h:139
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition: hyper_graph.h:74
std::set< Vertex * > VertexSet
Definition: hyper_graph.h:136
HyperGraphElement * clone() const
Definition: hyper_graph.h:90
HGET_EDGE
Definition: hyper_graph.h:63
void setVertex(size_t i, Vertex *v)
Definition: hyper_graph.h:194
DataContainer * _dataContainer
Definition: hyper_graph.h:114
const VertexIDMap & vertices() const
Definition: hyper_graph.h:225
virtual void setId(int newId)
Definition: hyper_graph.h:149
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
Vertex * vertex(size_t i)
Definition: hyper_graph.h:190
const VertexContainer & vertices() const
Definition: hyper_graph.h:178
const EdgeSet & edges() const
returns the set of hyper-edges that are leaving/entering in this vertex
Definition: hyper_graph.h:151
void setDataContainer(DataContainer *dataContainer_)
Definition: hyper_graph.h:111
const EdgeSet & edges() const
Definition: hyper_graph.h:230
VertexContainer & vertices()
Definition: hyper_graph.h:182
HGET_DATA
Definition: hyper_graph.h:63
EdgeSet & edges()
Definition: hyper_graph.h:232
std::unordered_map< int, Vertex * > VertexIDMap
Definition: hyper_graph.h:138
DataContainer * dataContainer()
Definition: hyper_graph.h:109
abstract Vertex, your types must derive from that one
Definition: hyper_graph.h:142
virtual HyperGraphElementType elementType() const
Definition: hyper_graph.h:198
VertexIDMap & vertices()
Definition: hyper_graph.h:227
VertexIDMap _vertices
Definition: hyper_graph.h:274
void setNext(Data *next_)
Definition: hyper_graph.h:108
HGET_PARAMETER
Definition: hyper_graph.h:63
virtual HyperGraphElementType elementType() const
Definition: hyper_graph.h:154
Container class that implements an interface for adding/removing Data elements in a linked list...
Definition: hyper_graph.h:121
HGET_CACHE
Definition: hyper_graph.h:63
HyperGraph(const HyperGraph &)
Definition: hyper_graph.h:279
EdgeSet & edges()
returns the set of hyper-edges that are leaving/entering in this vertex
Definition: hyper_graph.h:153
int _id
unique id
Definition: hyper_graph.h:203
#define G2O_CORE_API
Definition: g2o_core_api.h:29
const DataContainer * dataContainer() const
Definition: hyper_graph.h:110
HGET_VERTEX
Definition: hyper_graph.h:63
VertexContainer _vertices
Definition: hyper_graph.h:202