g2o
Classes | Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Protected Attributes | Private Member Functions | List of all members
g2o::HyperGraph Class Reference

#include <hyper_graph.h>

Inheritance diagram for g2o::HyperGraph:
Inheritance graph
[legend]

Classes

class  Data
 data packet for a vertex. Extend this class to store in the vertices the potential additional information you need (e.g. images, laser scans, ...). More...
 
class  DataContainer
 Container class that implements an interface for adding/removing Data elements in a linked list. More...
 
class  Edge
 
struct  HyperGraphElement
 
class  Vertex
 abstract Vertex, your types must derive from that one More...
 

Public Types

typedef std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
 
typedef std::set< Edge * > EdgeSet
 
typedef std::set< Vertex * > VertexSet
 
typedef std::unordered_map< int, Vertex * > VertexIDMap
 
typedef std::vector< Vertex * > VertexContainer
 

Public Member Functions

 HyperGraph ()
 constructs an empty hyper graph More...
 
virtual ~HyperGraph ()
 destroys the hyper-graph and all the vertices of the graph More...
 
Vertexvertex (int id)
 returns a vertex id in the hyper-graph, or 0 if the vertex id is not present More...
 
const Vertexvertex (int id) const
 returns a vertex id in the hyper-graph, or 0 if the vertex id is not present More...
 
virtual bool removeVertex (Vertex *v, bool detach=false)
 removes a vertex from the graph. Returns true on success (vertex was present) More...
 
virtual bool removeEdge (Edge *e)
 removes a vertex from the graph. Returns true on success (edge was present) More...
 
virtual void clear ()
 clears the graph and empties all structures. More...
 
const VertexIDMapvertices () const
 
VertexIDMapvertices ()
 
const EdgeSetedges () const
 
EdgeSetedges ()
 
virtual bool addVertex (Vertex *v)
 
virtual bool addEdge (Edge *e)
 
virtual bool setEdgeVertex (Edge *e, int pos, Vertex *v)
 
virtual bool mergeVertices (Vertex *vBig, Vertex *vSmall, bool erase)
 
virtual bool detachVertex (Vertex *v)
 
virtual bool changeId (Vertex *v, int newId)
 

Public Attributes

class G2O_CORE_API Data
 
class G2O_CORE_API DataContainer
 
class G2O_CORE_API Vertex
 
class G2O_CORE_API Edge
 

Static Public Attributes

static const int UnassignedId = -1
 
static const int InvalidId = -2
 

Protected Attributes

VertexIDMap _vertices
 
EdgeSet _edges
 

Private Member Functions

 HyperGraph (const HyperGraph &)
 
HyperGraphoperator= (const HyperGraph &)
 

Detailed Description

Class that models a directed Hyper-Graph. An hyper graph is a graph where an edge can connect one or more nodes. Both Vertices and Edges of an hyoper graph derive from the same class HyperGraphElement, thus one can implement generic algorithms that operate transparently on edges or vertices (see HyperGraphAction).

The vertices are uniquely identified by an int id, while the edges are identfied by their pointers.

Definition at line 55 of file hyper_graph.h.

Member Typedef Documentation

typedef std::set<Edge*> g2o::HyperGraph::EdgeSet

Definition at line 135 of file hyper_graph.h.

typedef std::bitset<HyperGraph::HGET_NUM_ELEMS> g2o::HyperGraph::GraphElemBitset

Definition at line 74 of file hyper_graph.h.

Definition at line 139 of file hyper_graph.h.

typedef std::unordered_map<int, Vertex*> g2o::HyperGraph::VertexIDMap

Definition at line 138 of file hyper_graph.h.

typedef std::set<Vertex*> g2o::HyperGraph::VertexSet

Definition at line 136 of file hyper_graph.h.

Constructor & Destructor Documentation

g2o::HyperGraph::HyperGraph ( )

constructs an empty hyper graph

Definition at line 225 of file hyper_graph.cpp.

226  {
227  }
g2o::HyperGraph::~HyperGraph ( )
virtual

destroys the hyper-graph and all the vertices of the graph

Definition at line 239 of file hyper_graph.cpp.

References clear().

240  {
241  clear();
242  }
virtual void clear()
clears the graph and empties all structures.
g2o::HyperGraph::HyperGraph ( const HyperGraph )
inlineprivate

Definition at line 279 of file hyper_graph.h.

279 { }

Member Function Documentation

bool g2o::HyperGraph::addEdge ( Edge e)
virtual

Adds an edge to the graph. If the edge is already in the graph, it does nothing and returns false. Otherwise it returns true.

Reimplemented in g2o::OptimizableGraph.

Definition at line 117 of file hyper_graph.cpp.

References _edges, g2o::HyperGraph::Vertex::edges(), and g2o::HyperGraph::Edge::vertices().

Referenced by g2o::OptimizableGraph::addEdge().

118  {
119  std::pair<EdgeSet::iterator, bool> result = _edges.insert(e);
120  if (! result.second)
121  return false;
122  for (std::vector<Vertex*>::iterator it = e->vertices().begin(); it != e->vertices().end(); ++it) {
123  Vertex* v = *it;
124  if (v)
125  v->edges().insert(e);
126  }
127  return true;
128  }
class G2O_CORE_API Vertex
Definition: hyper_graph.h:78
bool g2o::HyperGraph::addVertex ( Vertex v)
virtual

adds a vertex to the graph. The id of the vertex should be set before invoking this function. the function fails if another vertex with the same id is already in the graph. returns true, on success, or false on failure.

Reimplemented in g2o::OptimizableGraph.

Definition at line 94 of file hyper_graph.cpp.

References g2o::HyperGraph::Edge::_vertices, g2o::HyperGraph::Vertex::id(), and g2o::HyperGraph::Edge::vertex().

Referenced by g2o::OptimizableGraph::addVertex().

95  {
96  Vertex* vn=vertex(v->id());
97  if (vn)
98  return false;
99  _vertices.insert( std::make_pair(v->id(),v) );
100  return true;
101  }
Vertex * vertex(int id)
returns a vertex id in the hyper-graph, or 0 if the vertex id is not present
Definition: hyper_graph.cpp:78
class G2O_CORE_API Vertex
Definition: hyper_graph.h:78
VertexIDMap _vertices
Definition: hyper_graph.h:274
bool g2o::HyperGraph::changeId ( Vertex v,
int  newId 
)
virtual

changes the id of a vertex already in the graph, and updates the bookkeeping @ returns false if the vertex is not in the graph;

Definition at line 107 of file hyper_graph.cpp.

References g2o::HyperGraph::Edge::_vertices, g2o::HyperGraph::Vertex::id(), g2o::HyperGraph::Vertex::setId(), and g2o::HyperGraph::Edge::vertex().

107  {
108  Vertex* v2 = vertex(v->id());
109  if (v != v2)
110  return false;
111  _vertices.erase(v->id());
112  v->setId(newId);
113  _vertices.insert(std::make_pair(v->id(), v));
114  return true;
115  }
Vertex * vertex(int id)
returns a vertex id in the hyper-graph, or 0 if the vertex id is not present
Definition: hyper_graph.cpp:78
class G2O_CORE_API Vertex
Definition: hyper_graph.h:78
VertexIDMap _vertices
Definition: hyper_graph.h:274
void g2o::HyperGraph::clear ( )
virtual

clears the graph and empties all structures.

Reimplemented in g2o::SparseOptimizer.

Definition at line 229 of file hyper_graph.cpp.

References _edges, and g2o::HyperGraph::Edge::_vertices.

Referenced by g2o::SparseOptimizer::clear(), g2o::OptimizableGraph::clearParameters(), ~HyperGraph(), and g2o::OptimizableGraph::~OptimizableGraph().

230  {
231  for (VertexIDMap::iterator it=_vertices.begin(); it!=_vertices.end(); ++it)
232  delete (it->second);
233  for (EdgeSet::iterator it=_edges.begin(); it!=_edges.end(); ++it)
234  delete (*it);
235  _vertices.clear();
236  _edges.clear();
237  }
VertexIDMap _vertices
Definition: hyper_graph.h:274
bool g2o::HyperGraph::detachVertex ( Vertex v)
virtual

detaches a vertex from all connected edges

Definition at line 166 of file hyper_graph.cpp.

References g2o::HyperGraph::Edge::_vertices, g2o::HyperGraph::Vertex::edges(), g2o::HyperGraph::Vertex::id(), setEdgeVertex(), g2o::HyperGraph::Edge::vertex(), and g2o::HyperGraph::Edge::vertices().

Referenced by removeVertex().

166  {
167  VertexIDMap::iterator it=_vertices.find(v->id());
168  if (it==_vertices.end())
169  return false;
170  assert(it->second==v);
171  EdgeSet tmp(v->edges());
172  for (EdgeSet::iterator it=tmp.begin(); it!=tmp.end(); ++it){
173  HyperGraph::Edge* e = *it;
174  for (size_t i = 0 ; i<e->vertices().size(); i++){
175  if (v == e->vertex(i))
176  setEdgeVertex(e,i,0);
177  }
178  }
179  return true;
180  }
virtual bool setEdgeVertex(Edge *e, int pos, Vertex *v)
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
VertexIDMap _vertices
Definition: hyper_graph.h:274
class G2O_CORE_API Edge
Definition: hyper_graph.h:79
const EdgeSet& g2o::HyperGraph::edges ( ) const
inline
EdgeSet& g2o::HyperGraph::edges ( )
inline
Returns
the set of edges of the hyper graph

Definition at line 232 of file hyper_graph.h.

232 {return _edges;}
bool g2o::HyperGraph::mergeVertices ( Vertex vBig,
Vertex vSmall,
bool  erase 
)
virtual

merges two (valid) vertices, adjusts the bookkeeping and relabels all edges. the observations of vSmall are retargeted to vBig. If erase = true, vSmall is deleted from the graph repeatedly calls setEdgeVertex(...)

Definition at line 141 of file hyper_graph.cpp.

References g2o::HyperGraph::Edge::_vertices, g2o::HyperGraph::Vertex::edges(), g2o::HyperGraph::Vertex::id(), removeVertex(), setEdgeVertex(), g2o::HyperGraph::Edge::vertex(), and g2o::HyperGraph::Edge::vertices().

142  {
143  VertexIDMap::iterator it=_vertices.find(vBig->id());
144  if (it==_vertices.end())
145  return false;
146 
147  it=_vertices.find(vSmall->id());
148  if (it==_vertices.end())
149  return false;
150 
151  EdgeSet tmp(vSmall->edges());
152  bool ok = true;
153  for(EdgeSet::iterator it=tmp.begin(); it!=tmp.end(); ++it){
154  HyperGraph::Edge* e = *it;
155  for (size_t i=0; i<e->vertices().size(); i++){
156  Vertex* v=e->vertex(i);
157  if (v==vSmall)
158  ok &= setEdgeVertex(e,i,vBig);
159  }
160  }
161  if (erase)
162  removeVertex(vSmall);
163  return ok;
164  }
virtual bool removeVertex(Vertex *v, bool detach=false)
removes a vertex from the graph. Returns true on success (vertex was present)
virtual bool setEdgeVertex(Edge *e, int pos, Vertex *v)
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
class G2O_CORE_API Vertex
Definition: hyper_graph.h:78
VertexIDMap _vertices
Definition: hyper_graph.h:274
class G2O_CORE_API Edge
Definition: hyper_graph.h:79
HyperGraph& g2o::HyperGraph::operator= ( const HyperGraph )
inlineprivate

Definition at line 280 of file hyper_graph.h.

280 { return *this; }
bool g2o::HyperGraph::removeEdge ( Edge e)
virtual

removes a vertex from the graph. Returns true on success (edge was present)

Definition at line 206 of file hyper_graph.cpp.

References _edges, g2o::HyperGraph::Vertex::edges(), and g2o::HyperGraph::Edge::vertices().

Referenced by removeVertex().

207  {
208  EdgeSet::iterator it = _edges.find(e);
209  if (it == _edges.end())
210  return false;
211  _edges.erase(it);
212  for (std::vector<Vertex*>::iterator vit = e->vertices().begin(); vit != e->vertices().end(); ++vit) {
213  Vertex* v = *vit;
214  if (!v)
215  continue;
216  it = v->edges().find(e);
217  assert(it!=v->edges().end());
218  v->edges().erase(it);
219  }
220 
221  delete e;
222  return true;
223  }
class G2O_CORE_API Vertex
Definition: hyper_graph.h:78
bool g2o::HyperGraph::removeVertex ( Vertex v,
bool  detach = false 
)
virtual

removes a vertex from the graph. Returns true on success (vertex was present)

Reimplemented in g2o::SparseOptimizer.

Definition at line 182 of file hyper_graph.cpp.

References g2o::HyperGraph::Edge::_vertices, detachVertex(), g2o::HyperGraph::Vertex::edges(), g2o::HyperGraph::Vertex::id(), and removeEdge().

Referenced by mergeVertices(), and g2o::SparseOptimizer::removeVertex().

183  {
184  if (detach){
185  bool result = detachVertex(v);
186  if (! result) {
187  assert (0 && "inconsistency in detaching vertex, ");
188  }
189  }
190  VertexIDMap::iterator it=_vertices.find(v->id());
191  if (it==_vertices.end())
192  return false;
193  assert(it->second==v);
194  //remove all edges which are entering or leaving v;
195  EdgeSet tmp(v->edges());
196  for (EdgeSet::iterator it=tmp.begin(); it!=tmp.end(); ++it){
197  if (!removeEdge(*it)){
198  assert(0 && "error in erasing vertex");
199  }
200  }
201  _vertices.erase(it);
202  delete v;
203  return true;
204  }
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
virtual bool detachVertex(Vertex *v)
VertexIDMap _vertices
Definition: hyper_graph.h:274
virtual bool removeEdge(Edge *e)
removes a vertex from the graph. Returns true on success (edge was present)
bool g2o::HyperGraph::setEdgeVertex ( HyperGraph::Edge e,
int  pos,
HyperGraph::Vertex v 
)
virtual

Sets the vertex un position "pos" within the edge and keeps the bookkeeping consistent. If v ==0, the vertex is set to "invalid"

Reimplemented in g2o::OptimizableGraph.

Definition at line 130 of file hyper_graph.cpp.

References g2o::HyperGraph::Vertex::edges(), g2o::HyperGraph::Edge::setVertex(), and g2o::HyperGraph::Edge::vertex().

Referenced by detachVertex(), mergeVertices(), and g2o::OptimizableGraph::setEdgeVertex().

131  {
132  Vertex* vOld = e->vertex(pos);
133  if (vOld)
134  vOld->edges().erase(e);
135  e->setVertex(pos, v);
136  if (v)
137  v->edges().insert(e);
138  return true;
139  }
class G2O_CORE_API Vertex
Definition: hyper_graph.h:78
HyperGraph::Vertex * g2o::HyperGraph::vertex ( int  id)

returns a vertex id in the hyper-graph, or 0 if the vertex id is not present

Definition at line 78 of file hyper_graph.cpp.

References g2o::HyperGraph::Edge::_vertices.

Referenced by g2o::OptimizableGraph::vertex().

79  {
80  VertexIDMap::iterator it=_vertices.find(id);
81  if (it==_vertices.end())
82  return 0;
83  return it->second;
84  }
VertexIDMap _vertices
Definition: hyper_graph.h:274
const HyperGraph::Vertex * g2o::HyperGraph::vertex ( int  id) const

returns a vertex id in the hyper-graph, or 0 if the vertex id is not present

Definition at line 86 of file hyper_graph.cpp.

References g2o::HyperGraph::Edge::_vertices.

87  {
88  VertexIDMap::const_iterator it=_vertices.find(id);
89  if (it==_vertices.end())
90  return 0;
91  return it->second;
92  }
VertexIDMap _vertices
Definition: hyper_graph.h:274
const VertexIDMap& g2o::HyperGraph::vertices ( ) const
inline
VertexIDMap& g2o::HyperGraph::vertices ( )
inline
Returns
the map id -> vertex where the vertices are stored

Definition at line 227 of file hyper_graph.h.

227 {return _vertices;}
VertexIDMap _vertices
Definition: hyper_graph.h:274

Member Data Documentation

EdgeSet g2o::HyperGraph::_edges
protected

Definition at line 275 of file hyper_graph.h.

Referenced by addEdge(), clear(), and removeEdge().

VertexIDMap g2o::HyperGraph::_vertices
protected

Definition at line 274 of file hyper_graph.h.

Definition at line 76 of file hyper_graph.h.

Definition at line 77 of file hyper_graph.h.

Definition at line 79 of file hyper_graph.h.

const int g2o::HyperGraph::InvalidId = -2
static

Definition at line 72 of file hyper_graph.h.

const int g2o::HyperGraph::UnassignedId = -1
static

Definition at line 78 of file hyper_graph.h.


The documentation for this class was generated from the following files: