1 /*============================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center (DKFZ)
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
11 ============================================================================*/
13 #ifndef _mitkUndirectedGraph_txx
14 #define _mitkUndirectedGraph_txx
18 template <class TVertex, class TEdge>
19 UndirectedGraph<TVertex, TEdge>::UndirectedGraph()
23 template <class TVertex, class TEdge>
24 UndirectedGraph<TVertex, TEdge>::~UndirectedGraph()
28 template <class TVertex, class TEdge>
29 typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType UndirectedGraph<TVertex, TEdge>::AddVertex(
30 const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData)
32 VertexDescriptorType vertex = boost::add_vertex(m_Graph);
33 this->properties(vertex) = vertexData;
40 template <class TVertex, class TEdge>
41 void UndirectedGraph<TVertex, TEdge>::RemoveVertex(
42 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
44 boost::clear_vertex(vertex, m_Graph);
45 boost::remove_vertex(vertex, m_Graph);
49 template <class TVertex, class TEdge>
50 typename UndirectedGraph<TVertex, TEdge>::VertexType UndirectedGraph<TVertex, TEdge>::GetVertex(
51 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
53 return this->properties(vertex);
56 template <class TVertex, class TEdge>
57 void UndirectedGraph<TVertex, TEdge>::SetVertex(
58 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex,
59 const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData)
61 this->properties(vertex) = vertexData;
66 template <class TVertex, class TEdge>
67 typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType UndirectedGraph<TVertex, TEdge>::GetVertexDescriptor(
68 const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData) const
70 typename UndirectedGraph<TVertex, TEdge>::VertexIteratorType iterator, end;
72 boost::tie(iterator, end) = boost::vertices(m_Graph);
74 for (; iterator != end; ++iterator)
76 typename UndirectedGraph<TVertex, TEdge>::VertexType tempVertex;
78 // the value of an iterator is a descriptor
79 tempVertex = this->properties(*iterator);
81 if (vertexData == tempVertex)
84 MITK_ERROR << "Error in mitkUndirectedGraph::GetVertexDescriptor(...): vertex could not be found." << std::endl;
85 throw std::runtime_error(
86 "Exception thrown in mitkUndirectedGraph::GetVertexDescriptor(...): vertex could not be found.");
89 template <class TVertex, class TEdge>
90 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType UndirectedGraph<TVertex, TEdge>::AddEdge(
91 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexA,
92 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexB,
93 const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData)
95 EdgeDescriptorType edge;
96 bool inserted = false;
98 boost::tie(edge, inserted) = boost::add_edge(vertexA, vertexB, m_Graph);
102 MITK_ERROR << "Error in mitkUndirectedGraph::addEdge(...): edge could not be inserted." << std::endl;
103 throw std::runtime_error("Exception thrown in mitkUndirectedGraph::addEdge(...): edge could not be inserted.");
105 this->properties(edge) = edgeData;
112 template <class TVertex, class TEdge>
113 void UndirectedGraph<TVertex, TEdge>::RemoveEdge(
114 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
116 boost::remove_edge(edge, m_Graph);
120 template <class TVertex, class TEdge>
121 typename UndirectedGraph<TVertex, TEdge>::EdgeType UndirectedGraph<TVertex, TEdge>::GetEdge(
122 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
124 return this->properties(edge);
127 template <class TVertex, class TEdge>
128 void UndirectedGraph<TVertex, TEdge>::SetEdge(
129 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge,
130 const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData)
132 this->properties(edge) = edgeData;
137 template <class TVertex, class TEdge>
138 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType UndirectedGraph<TVertex, TEdge>::GetEdgeDescriptor(
139 const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData) const
141 typename UndirectedGraph<TVertex, TEdge>::EdgeIteratorType iterator, end;
142 boost::tie(iterator, end) = boost::edges(m_Graph);
144 for (; iterator != end; ++iterator)
146 typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
148 // the value of an iterator is a descriptor
149 tempEdge = this->properties(*iterator);
151 if (edgeData == tempEdge)
152 return iterator.dereference();
154 // if no edge match, throw error
155 MITK_ERROR << "Error in mitkUndirectedGraph::GetEdgeDescriptor(...): edge could not be found." << std::endl;
156 throw std::runtime_error(
157 "Exception thrown in mitkUndirectedGraph::GetEdgeDescriptor(...): edge could not be found.");
160 template <class TVertex, class TEdge>
161 std::pair<typename UndirectedGraph<TVertex, TEdge>::VertexType, typename UndirectedGraph<TVertex, TEdge>::VertexType>
162 UndirectedGraph<TVertex, TEdge>::GetVerticesOfAnEdge(
163 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge) const
165 typename UndirectedGraph<TVertex, TEdge>::VertexType sourceNode, targetNode;
167 sourceNode = this->properties(boost::source(edge, m_Graph));
168 targetNode = this->properties(boost::target(edge, m_Graph));
169 std::pair<typename UndirectedGraph<TVertex, TEdge>::VertexType,
170 typename UndirectedGraph<TVertex, TEdge>::VertexType>
171 nodePair(sourceNode, targetNode);
176 template <class TVertex, class TEdge>
177 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType
178 UndirectedGraph<TVertex, TEdge>::GetEdgeDescriptorByVerices(
179 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexA,
180 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexB) const
182 typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType edge;
183 bool edgeExists(false);
184 boost::tie(edge, edgeExists) = boost::edge(vertexA, vertexB, m_Graph);
189 MITK_ERROR << "Error in mitkUndirectedGraph::GetEdgeDescriptorByVerices(...): edge could not be found."
191 throw std::runtime_error(
192 "Exception thrown in mitkUndirectedGraph::GetEdgeDescriptorByVerices(...): edge could not be found.");
196 template <class TVertex, class TEdge>
197 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> UndirectedGraph<TVertex, TEdge>::GetAllEdgesOfAVertex(
198 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex) const
200 typename UndirectedGraph<TVertex, TEdge>::OutEdgeIteratorType iterator, end;
201 boost::tie(iterator, end) = boost::out_edges(vertex, m_Graph);
203 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> vectorOfEdges;
204 for (; iterator != end; ++iterator)
206 typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
208 // the value of an iterator is a descriptor
209 tempEdge = this->properties(*iterator);
211 vectorOfEdges.push_back(tempEdge);
214 return vectorOfEdges;
217 template <class TVertex, class TEdge>
218 int UndirectedGraph<TVertex, TEdge>::GetNumberOfVertices() const
220 // Returns the number of vertices in the graph
221 return boost::num_vertices(m_Graph);
224 template <class TVertex, class TEdge>
225 int UndirectedGraph<TVertex, TEdge>::GetNumberOfEdges() const
227 // Returns the number of edges in the graph
228 return boost::num_edges(m_Graph);
231 template <class TVertex, class TEdge>
232 std::vector<typename UndirectedGraph<TVertex, TEdge>::VertexType>
233 UndirectedGraph<TVertex, TEdge>::GetVectorOfAllVertices() const
235 typename UndirectedGraph<TVertex, TEdge>::VertexIteratorType iterator, end;
237 // sets iterator to start end end to end
238 boost::tie(iterator, end) = boost::vertices(m_Graph);
240 std::vector<typename UndirectedGraph<TVertex, TEdge>::VertexType> vectorOfNodes;
242 for (; iterator != end; ++iterator)
244 typename UndirectedGraph<TVertex, TEdge>::VertexType tempVertex;
246 // the value of an iterator is a descriptor
247 tempVertex = this->properties(*iterator);
249 vectorOfNodes.push_back(tempVertex);
252 return vectorOfNodes;
255 template <class TVertex, class TEdge>
256 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> UndirectedGraph<TVertex, TEdge>::GetVectorOfAllEdges()
259 typename UndirectedGraph<TVertex, TEdge>::EdgeIteratorType iterator, end;
261 // sets iterator to start end end to end
262 boost::tie(iterator, end) = boost::edges(m_Graph);
264 std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> vectorOfEdges;
266 for (; iterator != end; ++iterator)
268 typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
270 // the value of an iterator is a descriptor
271 tempEdge = this->properties(*iterator);
273 vectorOfEdges.push_back(tempEdge);
276 return vectorOfEdges;
279 template <class TVertex, class TEdge>
280 void UndirectedGraph<TVertex, TEdge>::Clear()
286 template <class TVertex, class TEdge>
287 const typename UndirectedGraph<TVertex, TEdge>::GraphType &UndirectedGraph<TVertex, TEdge>::GetGraph() const
293 template <class TVertex, class TEdge>
294 UndirectedGraph<TVertex, TEdge> &UndirectedGraph<TVertex, TEdge>::operator=(
295 const UndirectedGraph<TVertex, TEdge> &rhs)
297 m_Graph = rhs.m_Graph;
301 template <class TVertex, class TEdge>
302 TVertex &UndirectedGraph<TVertex, TEdge>::properties(
303 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
305 typename boost::property_map<GraphType, vertex_properties_t>::type param = boost::get(vertex_properties, m_Graph);
306 return param[vertex];
309 template <class TVertex, class TEdge>
310 const TVertex &UndirectedGraph<TVertex, TEdge>::properties(
311 const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex) const
313 typename boost::property_map<GraphType, vertex_properties_t>::const_type param =
314 boost::get(vertex_properties, m_Graph);
315 return param[vertex];
318 template <class TVertex, class TEdge>
319 TEdge &UndirectedGraph<TVertex, TEdge>::properties(
320 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
322 typename boost::property_map<GraphType, edge_properties_t>::type param = boost::get(edge_properties, m_Graph);
326 template <class TVertex, class TEdge>
327 const TEdge &UndirectedGraph<TVertex, TEdge>::properties(
328 const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge) const
330 typename boost::property_map<GraphType, edge_properties_t>::const_type param = boost::get(edge_properties, m_Graph);
336 #endif //_mitkUndirectedGraph_txx