GlobeEngine
UndirectedGraph.h
Go to the documentation of this file.
1 //
2 // VBOVertex.h
3 // GlobeEngine
4 //
5 // Created by Mathias Thöny on 27.12.11.
6 // Copyright (c) 2011 University of Zurich. All rights reserved.
7 //
8 
9 #ifndef GlobeEngine_UndirectedGraph_h
10 #define GlobeEngine_UndirectedGraph_h
11 #include "VBOVertex.h"
12 #include "OpenGL_Includes.h"
13 #include <iostream>
14 
15 #include "UndirectedEdge.h"
16 #include "GraphNode.h"
17 #include "Graph.h"
18 
19 namespace geGraph {
20 
21  template <class NODETYPE, class EDGETYPE, class U> class UndirectedGraph : public Graph < NODETYPE, EDGETYPE, U>
22  {
23  public:
25 
26  /*
27  * add an undirected edge by their indices
28  */
29  unsigned int addUndirectedEdge(unsigned int _nodeIdx1, unsigned int _nodeIdx2){
30  return this->addWeightedUndirectedEdge(_nodeIdx1, _nodeIdx2, 1.0f);
31  }
32 
33  unsigned int addWeightedUndirectedEdge(unsigned int _nodeIdx1, unsigned int _nodeIdx2, float _weight){
34  unsigned int newEdgeID = this->edges.size();
35  std::pair<U , U > fwcomb(_nodeIdx1, _nodeIdx2);
36  std::pair<U , U > bwcomb(_nodeIdx2, _nodeIdx1);
37 
38  typename std::map< std::pair < U , U >, U >::const_iterator edgeFWItr = this->forwardEdges.find(fwcomb);
39  if(edgeFWItr != this->forwardEdges.end()) {
40  //std::cout << " found a doubled edge in FW edges" << edgeFWItr->second << std::endl;
41  return UINT_MAX;
42  }else{
43  typename std::map< std::pair < U , U >, U >::const_iterator edgeBWItr = this->forwardEdges.find(bwcomb);
44  if(edgeBWItr != this->forwardEdges.end()) {
45  //std::cout << " found a doubled edge in BW edges: " << edgeBWItr->second << std::endl;
46  return UINT_MAX;
47  }else{
48  // edge can be added
49  EDGETYPE newUEdge(newEdgeID, _nodeIdx1, _nodeIdx2, _weight);
50  this->edges.push_back(newUEdge);
51  this->nodes[_nodeIdx1].addEdge(newEdgeID);
52  this->nodes[_nodeIdx2].addEdge(newEdgeID);
53 
54  this->forwardEdges.insert(std::pair< std::pair< U , U > , U >(fwcomb, newEdgeID));
55  this->backwardEdges.insert(std::pair< std::pair< U , U >, U >(bwcomb, newEdgeID));
56  return newEdgeID;
57  }
58  }
59  return UINT_MAX;
60  }
61 
62  U getEdgeByNodeIDs(unsigned int _nodeIdx1, unsigned int _nodeIdx2){
63  std::pair<U , U > edgeToSearch(_nodeIdx1, _nodeIdx2);
64  typename std::map< std::pair < U , U >, U >::const_iterator edgeItr = this->forwardEdges.find(edgeToSearch);
65  if(edgeItr != this->forwardEdges.end()) {
66  return edgeItr->second;
67  }else{
68  //std::pair<U , U > oppositeEdgeToSearch(_nodeIdx2, _nodeIdx1);
69  typename std::map< std::pair < U , U >, U >::const_iterator edgeBWItr = this->backwardEdges.find(edgeToSearch);
70  if(edgeBWItr != this->backwardEdges.end()) {
71  return edgeBWItr->second;
72  }else{
73  std::cout << "No edge for NodeIDs found." << std::endl;
74  return UINT_MAX;
75  }
76  }
77  }
78 
79  friend std::ostream& operator<< (std::ostream &out, const UndirectedGraph< NODETYPE, EDGETYPE, U> &graph) {
80  out << "graph has "<< graph.nodes.size() << " nodes: " << std::endl;
81  for (int i = 0; i < graph.nodes.size(); i++)
82  {
83  out << graph.nodes[i] << std::endl;
84  }
85  return out;
86  }
87  };
88 
89  /* graphs with a maximum of 4'294'967'295 Edges */
92 
95 
96 }
97 #endif
UndirectedGraph< ge::Vertex2d, UndirectedEdgeui, unsigned int > UndirectedGraph2Dd
Definition: UndirectedGraph.h:93
UndirectedGraph()
Definition: UndirectedGraph.h:24
U getEdgeByNodeIDs(unsigned int _nodeIdx1, unsigned int _nodeIdx2)
Definition: UndirectedGraph.h:62
Definition: Graph.h:22
UndirectedGraph< ge::Vertex3d, UndirectedEdgeui, unsigned int > Graph3Dd
Definition: UndirectedGraph.h:91
std::map< std::pair< U, U >, U > forwardEdges
Definition: Graph.h:148
unsigned int addUndirectedEdge(unsigned int _nodeIdx1, unsigned int _nodeIdx2)
Definition: UndirectedGraph.h:29
Definition: DirectedEdge.h:16
std::vector< EDGETYPE > edges
Definition: Graph.h:147
std::vector< GraphNode< U > > nodes
Definition: Graph.h:145
unsigned int addWeightedUndirectedEdge(unsigned int _nodeIdx1, unsigned int _nodeIdx2, float _weight)
Definition: UndirectedGraph.h:33
Definition: UndirectedGraph.h:21
UndirectedGraph< ge::Vertex2d, UndirectedEdgeui, unsigned int > Graph2Dd
Definition: UndirectedGraph.h:90
std::map< std::pair< U, U >, U > backwardEdges
Definition: Graph.h:149
UndirectedGraph< ge::Vertex3d, UndirectedEdgeui, unsigned int > UndirectedGraph3Dd
Definition: UndirectedGraph.h:94