g2o
Loading...
Searching...
No Matches
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 <bitset>
31#include <cassert>
32#include <cstddef>
33#include <set>
34#include <unordered_map>
35#include <vector>
36
37#include "g2o_core_api.h"
38
41namespace g2o {
42
54 public:
58 enum G2O_CORE_API HyperGraphElementType {
64 HGET_NUM_ELEMS // keep as last elem
65 };
66
67 static const int UnassignedId = -1;
68 static const int InvalidId = -2;
69
70 typedef std::bitset<HyperGraph::HGET_NUM_ELEMS> GraphElemBitset;
71
76
81 virtual ~HyperGraphElement() {}
85 virtual HyperGraphElementType elementType() const = 0;
86 };
87
94 public:
95 Data();
96 ~Data();
98 virtual bool read(std::istream& is) = 0;
100 virtual bool write(std::ostream& os) const = 0;
101 virtual HyperGraph::HyperGraphElementType elementType() const {
102 return HyperGraph::HGET_DATA;
103 }
104 inline const Data* next() const { return _next; }
105 inline Data* next() { return _next; }
106 inline void setNext(Data* next_) { _next = next_; }
107 inline DataContainer* dataContainer() { return _dataContainer; }
108 inline const DataContainer* dataContainer() const { return _dataContainer; }
109 inline void setDataContainer(DataContainer* dataContainer_) {
110 _dataContainer = dataContainer_;
111 }
112
113 protected:
114 Data* _next; // linked list of multiple data;
116 };
117
123 public:
124 DataContainer() { _userData = 0; }
125 virtual ~DataContainer() { delete _userData; }
127 const Data* userData() const { return _userData; }
128 Data* userData() { return _userData; }
129 void setUserData(Data* obs) { _userData = obs; }
130 void addUserData(Data* obs) {
131 if (obs) {
132 obs->setNext(_userData);
133 _userData = obs;
134 }
135 }
136
137 protected:
139 };
140
141 typedef std::set<Edge*> EdgeSet;
142 typedef std::set<Vertex*> VertexSet;
143
144 typedef std::unordered_map<int, Vertex*> VertexIDMap;
145 typedef std::vector<Vertex*> VertexContainer;
146
149 public:
151 explicit Vertex(int id = InvalidId);
152 virtual ~Vertex();
154 int id() const { return _id; }
155 virtual void setId(int newId) { _id = newId; }
157 const EdgeSet& edges() const { return _edges; }
159 EdgeSet& edges() { return _edges; }
160 virtual HyperGraphElementType elementType() const { return HGET_VERTEX; }
161
162 protected:
163 int _id;
165 };
166
173 public:
175 explicit Edge(int id = InvalidId);
176 virtual ~Edge();
177
181 virtual void resize(size_t size);
186 const VertexContainer& vertices() const { return _vertices; }
191 VertexContainer& vertices() { return _vertices; }
195 const Vertex* vertex(size_t i) const {
196 assert(i < _vertices.size() && "index out of bounds");
197 return _vertices[i];
198 }
202 Vertex* vertex(size_t i) {
203 assert(i < _vertices.size() && "index out of bounds");
204 return _vertices[i];
205 }
209 void setVertex(size_t i, Vertex* v) {
210 assert(i < _vertices.size() && "index out of bounds");
211 _vertices[i] = v;
212 }
213
214 int id() const { return _id; }
215 void setId(int id);
216 virtual HyperGraphElementType elementType() const { return HGET_EDGE; }
217
218 int numUndefinedVertices() const;
219
220 protected:
222 int _id;
223 };
224
225 public:
227 HyperGraph();
229 virtual ~HyperGraph();
230
233 Vertex* vertex(int id);
236 const Vertex* vertex(int id) const;
237
240 virtual bool removeVertex(Vertex* v, bool detach = false);
243 virtual bool removeEdge(Edge* e);
245 virtual void clear();
246
248 const VertexIDMap& vertices() const { return _vertices; }
250 VertexIDMap& vertices() { return _vertices; }
251
253 const EdgeSet& edges() const { return _edges; }
255 EdgeSet& edges() { return _edges; }
256
263 virtual bool addVertex(Vertex* v);
264
269 virtual bool addEdge(Edge* e);
270
275 virtual bool setEdgeVertex(Edge* e, int pos, Vertex* v);
276
282 virtual bool mergeVertices(Vertex* vBig, Vertex* vSmall, bool erase);
283
287 virtual bool detachVertex(Vertex* v);
288
294 virtual bool changeId(Vertex* v, int newId);
295
296 protected:
299
300 private:
301 // Disable the copy constructor and assignment operator
303 HyperGraph& operator=(const HyperGraph&) { return *this; }
304};
305
306} // namespace g2o
307
309
310#endif
Container class that implements an interface for adding/removing Data elements in a linked list.
const Data * userData() const
the user data associated with this vertex
data packet for a vertex. Extend this class to store in the vertices the potential additional informa...
Definition hyper_graph.h:93
const Data * next() const
virtual bool read(std::istream &is)=0
read the data from a stream
DataContainer * _dataContainer
void setNext(Data *next_)
virtual HyperGraph::HyperGraphElementType elementType() const
DataContainer * dataContainer()
virtual bool write(std::ostream &os) const =0
write the data to a stream
const DataContainer * dataContainer() const
void setDataContainer(DataContainer *dataContainer_)
virtual HyperGraphElementType elementType() const
void setVertex(size_t i, Vertex *v)
const VertexContainer & vertices() const
VertexContainer _vertices
const Vertex * vertex(size_t i) const
VertexContainer & vertices()
Vertex * vertex(size_t i)
abstract Vertex, your types must derive from that one
int id() const
returns the id
const EdgeSet & edges() const
returns the set of hyper-edges that are leaving/entering in this vertex
virtual HyperGraphElementType elementType() const
virtual void setId(int newId)
EdgeSet & edges()
returns the set of hyper-edges that are leaving/entering in this vertex
HyperGraph & operator=(const HyperGraph &)
std::unordered_map< int, Vertex * > VertexIDMap
std::set< Edge * > EdgeSet
std::set< Vertex * > VertexSet
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition hyper_graph.h:70
VertexIDMap _vertices
std::vector< Vertex * > VertexContainer
VertexIDMap & vertices()
const EdgeSet & edges() const
HyperGraph(const HyperGraph &)
const VertexIDMap & vertices() const
EdgeSet & edges()
#define G2O_CORE_API
HGET_DATA
Definition hyper_graph.h:63
HGET_CACHE
Definition hyper_graph.h:62
HGET_EDGE
Definition hyper_graph.h:60
HGET_PARAMETER
Definition hyper_graph.h:61
HGET_VERTEX
Definition hyper_graph.h:59
virtual HyperGraphElementType elementType() const =0