g2o
hyper_graph.cpp
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 #include "hyper_graph.h"
28 
29 #include <assert.h>
30 #include <queue>
31 
32 namespace g2o {
33 
35  _next = 0;
36  _dataContainer = 0;
37  }
38 
40  delete _next;
41  }
42 
43  HyperGraph::Vertex::Vertex(int id) : _id(id)
44  {
45  }
46 
48  {
49  }
50 
52  {
53  }
54 
56  {
57  }
58 
60  int undefined=0;
61  for (size_t i=0; i<_vertices.size(); i++){
62  if (!_vertices[i])
63  undefined++;
64  }
65  return undefined;
66  }
67 
68  void HyperGraph::Edge::resize(size_t size)
69  {
70  _vertices.resize(size, 0);
71  }
72 
74  {
75  _id = id;
76  }
77 
79  {
80  VertexIDMap::iterator it=_vertices.find(id);
81  if (it==_vertices.end())
82  return 0;
83  return it->second;
84  }
85 
87  {
88  VertexIDMap::const_iterator it=_vertices.find(id);
89  if (it==_vertices.end())
90  return 0;
91  return it->second;
92  }
93 
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  }
102 
107  bool HyperGraph::changeId(Vertex* v, int newId){
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  }
116 
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  }
129 
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  }
140 
141  bool HyperGraph::mergeVertices(Vertex* vBig, Vertex* vSmall, bool erase)
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  }
165 
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  }
181 
182  bool HyperGraph::removeVertex(Vertex* v, bool detach)
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  }
205 
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  }
224 
226  {
227  }
228 
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  }
238 
240  {
241  clear();
242  }
243 
244 } // end namespace
int id() const
returns the id
Definition: hyper_graph.h:148
const Vertex * vertex(size_t i) const
Definition: hyper_graph.h:186
virtual bool removeVertex(Vertex *v, bool detach=false)
removes a vertex from the graph. Returns true on success (vertex was present)
virtual bool changeId(Vertex *v, int newId)
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
void setVertex(size_t i, Vertex *v)
Definition: hyper_graph.h:194
DataContainer * _dataContainer
Definition: hyper_graph.h:114
virtual bool addEdge(Edge *e)
virtual void setId(int newId)
Definition: hyper_graph.h:149
virtual bool setEdgeVertex(Edge *e, int pos, Vertex *v)
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
int numUndefinedVertices() const
Definition: hyper_graph.cpp:59
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
Vertex(int id=InvalidId)
creates a vertex having an ID specified by the argument
Definition: hyper_graph.cpp:43
virtual void clear()
clears the graph and empties all structures.
abstract Vertex, your types must derive from that one
Definition: hyper_graph.h:142
virtual bool detachVertex(Vertex *v)
virtual bool addVertex(Vertex *v)
Definition: hyper_graph.cpp:94
virtual void resize(size_t size)
Definition: hyper_graph.cpp:68
virtual bool mergeVertices(Vertex *vBig, Vertex *vSmall, bool erase)
HyperGraph()
constructs an empty hyper graph
virtual bool removeEdge(Edge *e)
removes a vertex from the graph. Returns true on success (edge was present)
int _id
unique id
Definition: hyper_graph.h:203
Edge(int id=InvalidId)
creates and empty edge with no vertices
Definition: hyper_graph.cpp:51
virtual ~HyperGraph()
destroys the hyper-graph and all the vertices of the graph
VertexContainer _vertices
Definition: hyper_graph.h:202