1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
17 #ifndef _mitkUndirectedGraph_txx
18 #define _mitkUndirectedGraph_txx
22 template <class TVertex, class TEdge>
23 UndirectedGraph<TVertex, TEdge>::UndirectedGraph() : m_Graph(NULL)
27 template <class TVertex, class TEdge>
28 UndirectedGraph<TVertex, TEdge>::UndirectedGraph(const UndirectedGraph<TVertex, TEdge> &graph)
29 : m_Graph(graph.m_Graph)
33 template <class TVertex, class TEdge>
34 UndirectedGraph<TVertex, TEdge>::~UndirectedGraph()
38 template <class TVertex, class TEdge>
39 typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType UndirectedGraph<TVertex, TEdge>::AddVertex(
40 const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData)
42 VertexDescriptorType vertex = boost::add_vertex(m_Graph);
43 this->properties(vertex) = vertexData;
50 template <class TVertex, class TEdge>
51 void UndirectedGraph<TVertex, TEdge>::RemoveVertex(
52 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
54 boost::clear_vertex(vertex, m_Graph);
55 boost::remove_vertex(vertex, m_Graph);
59 template <class TVertex, class TEdge>
60 typename UndirectedGraph<TVertex, TEdge>::VertexType UndirectedGraph<TVertex, TEdge>::GetVertex(
61 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
63 return this->properties(vertex);
66 template <class TVertex, class TEdge>
67 void UndirectedGraph<TVertex, TEdge>::SetVertex(
68 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex,
69 const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData)
71 this->properties(vertex) = vertexData;
76 template <class TVertex, class TEdge>
77 typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType UndirectedGraph<TVertex, TEdge>::GetVertexDescriptor(
78 const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData) const
80 typename UndirectedGraph<TVertex, TEdge>::VertexIteratorType iterator, end;
82 boost::tie(iterator, end) = boost::vertices(m_Graph);
84 for (; iterator != end; ++iterator)
86 typename UndirectedGraph<TVertex, TEdge>::VertexType tempVertex;
88 // the value of an iterator is a descriptor
89 tempVertex = this->properties(*iterator);
91 if (vertexData == tempVertex)
94 MITK_ERROR << "Error in mitkUndirectedGraph::GetVertexDescriptor(...): vertex could not be found." << std::endl;
95 throw std::runtime_error(
96 "Exception thrown in mitkUndirectedGraph::GetVertexDescriptor(...): vertex could not be found.");
99 template <class TVertex, class TEdge>
100 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType UndirectedGraph<TVertex, TEdge>::AddEdge(
101 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexA,
102 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexB,
103 const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData)
105 EdgeDescriptorType edge;
106 bool inserted = false;
108 boost::tie(edge, inserted) = boost::add_edge(vertexA, vertexB, m_Graph);
112 MITK_ERROR << "Error in mitkUndirectedGraph::addEdge(...): edge could not be inserted." << std::endl;
113 throw std::runtime_error("Exception thrown in mitkUndirectedGraph::addEdge(...): edge could not be inserted.");
115 this->properties(edge) = edgeData;
122 template <class TVertex, class TEdge>
123 void UndirectedGraph<TVertex, TEdge>::RemoveEdge(
124 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
126 boost::remove_edge(edge, m_Graph);
130 template <class TVertex, class TEdge>
131 typename UndirectedGraph<TVertex, TEdge>::EdgeType UndirectedGraph<TVertex, TEdge>::GetEdge(
132 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
134 return this->properties(edge);
137 template <class TVertex, class TEdge>
138 void UndirectedGraph<TVertex, TEdge>::SetEdge(
139 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge,
140 const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData)
142 this->properties(edge) = edgeData;
147 template <class TVertex, class TEdge>
148 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType UndirectedGraph<TVertex, TEdge>::GetEdgeDescriptor(
149 const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData) const
151 typename UndirectedGraph<TVertex, TEdge>::EdgeIteratorType iterator, end;
152 boost::tie(iterator, end) = boost::edges(m_Graph);
154 for (; iterator != end; ++iterator)
156 typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
158 // the value of an iterator is a descriptor
159 tempEdge = this->properties(*iterator);
161 if (edgeData == tempEdge)
162 return iterator.dereference();
164 // if no edge match, throw error
165 MITK_ERROR << "Error in mitkUndirectedGraph::GetEdgeDescriptor(...): edge could not be found." << std::endl;
166 throw std::runtime_error(
167 "Exception thrown in mitkUndirectedGraph::GetEdgeDescriptor(...): edge could not be found.");
170 template <class TVertex, class TEdge>
171 std::pair<typename UndirectedGraph<TVertex, TEdge>::VertexType, typename UndirectedGraph<TVertex, TEdge>::VertexType>
172 UndirectedGraph<TVertex, TEdge>::GetVerticesOfAnEdge(
173 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge) const
175 typename UndirectedGraph<TVertex, TEdge>::VertexType sourceNode, targetNode;
177 sourceNode = this->properties(boost::source(edge, m_Graph));
178 targetNode = this->properties(boost::target(edge, m_Graph));
179 std::pair<typename UndirectedGraph<TVertex, TEdge>::VertexType,
180 typename UndirectedGraph<TVertex, TEdge>::VertexType>
181 nodePair(sourceNode, targetNode);
186 template <class TVertex, class TEdge>
187 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType
188 UndirectedGraph<TVertex, TEdge>::GetEdgeDescriptorByVerices(
189 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexA,
190 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexB) const
192 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType edge;
193 bool edgeExists(false);
194 boost::tie(edge, edgeExists) = boost::edge(vertexA, vertexB, m_Graph);
199 MITK_ERROR << "Error in mitkUndirectedGraph::GetEdgeDescriptorByVerices(...): edge could not be found."
201 throw std::runtime_error(
202 "Exception thrown in mitkUndirectedGraph::GetEdgeDescriptorByVerices(...): edge could not be found.");
206 template <class TVertex, class TEdge>
207 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> UndirectedGraph<TVertex, TEdge>::GetAllEdgesOfAVertex(
208 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex) const
210 typename UndirectedGraph<TVertex, TEdge>::OutEdgeIteratorType iterator, end;
211 boost::tie(iterator, end) = boost::out_edges(vertex, m_Graph);
213 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> vectorOfEdges;
214 for (; iterator != end; ++iterator)
216 typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
218 // the value of an iterator is a descriptor
219 tempEdge = this->properties(*iterator);
221 vectorOfEdges.push_back(tempEdge);
224 return vectorOfEdges;
227 template <class TVertex, class TEdge>
228 int UndirectedGraph<TVertex, TEdge>::GetNumberOfVertices() const
230 // Returns the number of vertices in the graph
231 return boost::num_vertices(m_Graph);
234 template <class TVertex, class TEdge>
235 int UndirectedGraph<TVertex, TEdge>::GetNumberOfEdges() const
237 // Returns the number of edges in the graph
238 return boost::num_edges(m_Graph);
241 template <class TVertex, class TEdge>
242 std::vector<typename UndirectedGraph<TVertex, TEdge>::VertexType>
243 UndirectedGraph<TVertex, TEdge>::GetVectorOfAllVertices() const
245 typename UndirectedGraph<TVertex, TEdge>::VertexIteratorType iterator, end;
247 // sets iterator to start end end to end
248 boost::tie(iterator, end) = boost::vertices(m_Graph);
250 std::vector<typename UndirectedGraph<TVertex, TEdge>::VertexType> vectorOfNodes;
252 for (; iterator != end; ++iterator)
254 typename UndirectedGraph<TVertex, TEdge>::VertexType tempVertex;
256 // the value of an iterator is a descriptor
257 tempVertex = this->properties(*iterator);
259 vectorOfNodes.push_back(tempVertex);
262 return vectorOfNodes;
265 template <class TVertex, class TEdge>
266 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> UndirectedGraph<TVertex, TEdge>::GetVectorOfAllEdges()
269 typename UndirectedGraph<TVertex, TEdge>::EdgeIteratorType iterator, end;
271 // sets iterator to start end end to end
272 boost::tie(iterator, end) = boost::edges(m_Graph);
274 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> vectorOfEdges;
276 for (; iterator != end; ++iterator)
278 typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
280 // the value of an iterator is a descriptor
281 tempEdge = this->properties(*iterator);
283 vectorOfEdges.push_back(tempEdge);
286 return vectorOfEdges;
289 template <class TVertex, class TEdge>
290 void UndirectedGraph<TVertex, TEdge>::Clear()
296 template <class TVertex, class TEdge>
297 const typename UndirectedGraph<TVertex, TEdge>::GraphType &UndirectedGraph<TVertex, TEdge>::GetGraph() const
303 template <class TVertex, class TEdge>
304 UndirectedGraph<TVertex, TEdge> &UndirectedGraph<TVertex, TEdge>::operator=(
305 const UndirectedGraph<TVertex, TEdge> &rhs)
307 m_Graph = rhs.m_Graph;
311 template <class TVertex, class TEdge>
312 TVertex &UndirectedGraph<TVertex, TEdge>::properties(
313 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
315 typename boost::property_map<GraphType, vertex_properties_t>::type param = boost::get(vertex_properties, m_Graph);
316 return param[vertex];
319 template <class TVertex, class TEdge>
320 const TVertex &UndirectedGraph<TVertex, TEdge>::properties(
321 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex) const
323 typename boost::property_map<GraphType, vertex_properties_t>::const_type param =
324 boost::get(vertex_properties, m_Graph);
325 return param[vertex];
328 template <class TVertex, class TEdge>
329 TEdge &UndirectedGraph<TVertex, TEdge>::properties(
330 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
332 typename boost::property_map<GraphType, edge_properties_t>::type param = boost::get(edge_properties, m_Graph);
336 template <class TVertex, class TEdge>
337 const TEdge &UndirectedGraph<TVertex, TEdge>::properties(
338 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge) const
340 typename boost::property_map<GraphType, edge_properties_t>::const_type param = boost::get(edge_properties, m_Graph);
346 #endif //_mitkUndirectedGraph_txx